aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-02-23 15:47:16 -0800
committerAdam Soltys <[email protected]>2023-02-23 15:47:16 -0800
commit797d10c9322b2de3ec14fa85689ae70435e98c8f (patch)
treebc8ade7f56b236b9690bc003d88de7e953928695
parente5b3b8454bd3c72ec1f6e2c214a2ccb717c54486 (diff)
downloadcoinos-server-797d10c9322b2de3ec14fa85689ae70435e98c8f.tar.gz
coinos-server-797d10c9322b2de3ec14fa85689ae70435e98c8f.zip
fix fee subtraction
-rw-r--r--lib/payments.js18
-rw-r--r--routes/payments.js49
2 files changed, 40 insertions, 27 deletions
diff --git a/lib/payments.js b/lib/payments.js
index 89652ff4..aaf39210 100644
--- a/lib/payments.js
+++ b/lib/payments.js
@@ -25,9 +25,11 @@ export let debit = async (
type = types.internal,
id = v4()
) => {
- let ref;
+ let ref, tip;
let invoice = await g(`invoice:${hash}`);
- if (invoice) ref = invoice.uid;
+ if (invoice) {
+ ref = invoice.uid;
+ }
let { id: uid, currency, username } = user;
@@ -44,16 +46,21 @@ export let debit = async (
let covered = Math.min(credit, ourfee) || 0;
ourfee -= covered;
- l("debiting", user.username, balance, amount, ourfee, amount + ourfee);
- if (balance < amount + ourfee) fail("Insufficient funds");
+ l("debiting", user.username, balance, amount, fee, ourfee, amount + fee + ourfee);
+ if (balance < amount + fee + ourfee) fail("Insufficient funds");
await db
.multi()
.decrBy(`credit:${type}:${uid}`, covered)
- .decrBy(`balance:${uid}`, amount + ourfee)
+ .decrBy(`balance:${uid}`, amount + fee + ourfee)
.exec();
});
});
+ if (invoice?.tip) {
+ tip = invoice.tip;
+ amount -= tip;
+ }
+
let p = {
id,
amount: -amount,
@@ -66,6 +73,7 @@ export let debit = async (
currency,
type,
ref,
+ tip,
created: Date.now()
};
diff --git a/routes/payments.js b/routes/payments.js
index ea711cb1..38611d19 100644
--- a/routes/payments.js
+++ b/routes/payments.js
@@ -14,11 +14,10 @@ import ln from "$lib/ln";
export default {
async create({ body, user }, res) {
- let { amount, hash, maxfee, name, memo, payreq, tip, username } = body;
+ let { amount, hash, maxfee, name, memo, payreq, username } = body;
amount = parseInt(amount);
maxfee = maxfee ? parseInt(maxfee) : 0;
- tip = parseInt(tip);
await requirePin({ body, user });
@@ -181,20 +180,19 @@ export default {
try {
if (wallet === config.bitcoin.wallet) {
let { confirmations, details } = await bc.getTransaction(txid);
- if (details.category === "receive") {
- for (let { address, amount, vout } of details) {
- let p = await g(`payment:${txid}:${vout}`);
- if (!p || confirmations < 1) {
- await credit(
- address,
- sats(amount),
- "",
- `${txid}:${vout}`,
- types.bitcoin
- );
- } else {
- await confirm(address, txid, vout);
- }
+ for (let { address, amount, category, vout } of details) {
+ if (category !== "receive") continue;
+ let p = await g(`payment:${txid}:${vout}`);
+ if (!p || confirmations < 1) {
+ await credit(
+ address,
+ sats(amount),
+ "",
+ `${txid}:${vout}`,
+ types.bitcoin
+ );
+ } else {
+ await confirm(address, txid, vout);
}
}
}
@@ -205,14 +203,17 @@ export default {
}
},
- async fee(req, res) {
+ async fee({ body: { amount, address, feeRate, subtract }, user }, res) {
try {
- let { amount, address, feeRate, subtract } = req.body;
-
let subtractFeeFromOutputs = subtract ? [0] : [];
let replaceable = true;
- let outs = [{ [address]: btc(amount) }];
- let count = await bc.getBlockCount();
+
+ let ourfee = Math.round(amount * config.fee);
+ let credit = await g(`credit:bitcoin:${user.id}`);
+ let covered = Math.min(credit, ourfee) || 0;
+ ourfee -= covered;
+
+ if (subtract) amount -= ourfee;
let { feerate: min } = await bc.estimateSmartFee(40);
let { feerate: max } = await bc.estimateSmartFee(1);
@@ -220,6 +221,7 @@ export default {
if (feeRate) feeRate = btc(feeRate);
else feeRate = max;
+ let outs = [{ [address]: btc(amount) }];
let raw = await bc.createRawTransaction([], outs, 0, replaceable);
let tx = await bc.fundRawTransaction(raw, {
@@ -236,6 +238,7 @@ export default {
res.send({ feeRate, min, max, fee, tx });
} catch (e) {
+ console.log(e);
bail(res, "problem estimating fee");
}
},
@@ -245,7 +248,7 @@ export default {
await requirePin(req);
let { user } = req;
- let { address, memo, tx } = req.body;
+ let { address, memo, tx, subtract } = req.body;
let { hex, fee } = tx;
if (config.bitcoin.walletpass)
@@ -280,6 +283,8 @@ export default {
total = total - change + fee;
let amount = total - fee;
+ console.log("AMOUNT", amount);
+
if (change && !amount) fail("Cannot send to unregistered coinos address");
await debit(txid, amount, fee, null, user, types.bitcoin, txid);