aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-01-29 21:09:21 -0800
committerAdam Soltys <[email protected]>2024-01-29 21:09:21 -0800
commit0c223a4835711a287f17835af959b47f72ea68c9 (patch)
tree1b5d58d0e57ad9b4b9597c249a3b4373f9963188 /lib
parentda2ac3f9ba4e5e756088b7b9d79f1ac8c88aef4e (diff)
downloadcoinos-server-0c223a4835711a287f17835af959b47f72ea68c9.tar.gz
coinos-server-0c223a4835711a287f17835af959b47f72ea68c9.zip
calculate platform fee properly
Diffstat (limited to 'lib')
-rw-r--r--lib/db.js2
-rw-r--r--lib/payments.js17
2 files changed, 10 insertions, 9 deletions
diff --git a/lib/db.js b/lib/db.js
index 6a84a7a2..58472269 100644
--- a/lib/db.js
+++ b/lib/db.js
@@ -24,7 +24,7 @@ end
redis.call('decrby', creditKey, tostring(math.floor(covered)))
redis.call('decrby', balanceKey, tostring(math.floor(amount + tip + fee + ourfee)))
-return {ok = ourfee}
+return ourfee
`;
let debit = defineScript({
diff --git a/lib/payments.js b/lib/payments.js
index 11afd17f..ee030a5e 100644
--- a/lib/payments.js
+++ b/lib/payments.js
@@ -64,10 +64,10 @@ export let debit = async ({
if (!amount || amount < 0) fail("Amount must be greater than zero");
let ourfee = [types.bitcoin, types.liquid, types.lightning].includes(type)
- ? Math.round(amount * config.fee)
+ ? Math.round((amount + fee + tip) * config.fee)
: 0;
- let r = await db.debit(
+ ourfee = await db.debit(
`balance:${uid}`,
`credit:${type}:${uid}`,
amount || 0,
@@ -76,7 +76,7 @@ export let debit = async ({
ourfee || 0,
);
- if (!r || r.err) fail("Problem debiting account");
+ if (ourfee.err) fail(ourfee.err);
let id = v4();
let p = {
@@ -84,7 +84,7 @@ export let debit = async ({
amount: -amount,
fee,
hash,
- ourfee: r.ourfee,
+ ourfee,
memo,
iid,
uid,
@@ -216,13 +216,12 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => {
};
export let autowithdraw = async (user) => {
+ let { username, autowithdraw, id, destination, threshold, reserve } = user;
try {
- let { username, autowithdraw, id, destination, threshold, reserve } = user;
if (autowithdraw) {
let to = destination.trim();
let balance = await g(`balance:${id}`);
let amount = balance - reserve;
- console.log("AMT", amount, balance, reserve, threshold);
if (balance > threshold) await pay({ amount, to, user });
}
} catch (e) {
@@ -506,8 +505,6 @@ export let build = async ({ amount, address, feeRate, user, subtract }) => {
}
let replaceable = true;
- let ourfee = Math.round(amount * config.fee);
- let credit = await g(`credit:${type}:${user.id}`);
let outs = [{ [address]: btc(amount) }];
@@ -526,6 +523,10 @@ export let build = async ({ amount, address, feeRate, user, subtract }) => {
let dust = 547;
let balance = await g(`balance:${user.id}`);
+ let ourfee = Math.round(amount * config.fee);
+ let credit = await g(`credit:${type}:${user.id}`);
+ let covered = Math.min(credit, ourfee);
+ ourfee -= covered;
if (subtract || amount + fee + ourfee > balance) {
if (amount <= fee + ourfee + dust)