aboutsummaryrefslogtreecommitdiffstats
path: root/routes/bitcoin
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-04-29 20:59:05 -0700
committerAdam Soltys <[email protected]>2020-04-29 21:03:33 -0700
commitfa84d5a65844602c7e97e9bf0e5a51befdebaf53 (patch)
tree3e0335eea275a5de9040eb3cc6dfc62c6e190575 /routes/bitcoin
parent001c0291ad6eabe5684138422adfa0e851d507a6 (diff)
downloadcoinos-server-fa84d5a65844602c7e97e9bf0e5a51befdebaf53.tar.gz
coinos-server-fa84d5a65844602c7e97e9bf0e5a51befdebaf53.zip
update account balance properly after lightning payment
Diffstat (limited to 'routes/bitcoin')
-rw-r--r--routes/bitcoin/receive.js12
-rw-r--r--routes/bitcoin/send.js4
2 files changed, 14 insertions, 2 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js
index deded625..1aae1467 100644
--- a/routes/bitcoin/receive.js
+++ b/routes/bitcoin/receive.js
@@ -73,10 +73,22 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
addresses[user.address] = user.username;
+ let totalOutputs = tx.outs.reduce((a, b) => a + b.value, 0);
+ let totalInputs = 0;
+ for (let i = 0; i < tx.ins.length; i++) {
+ let { hash, index } = tx.ins[i];
+ hash = reverse(hash).toString("hex");
+ let hex = await bc.getRawTransaction(hash.toString('hex'));
+ let inputTx = bitcoin.Transaction.fromHex(hex);
+ totalInputs += inputTx.outs[index].value;
+ }
+ let fee = totalInputs - totalOutputs;
+
let payment = await db.Payment.create({
account_id: account.id,
user_id: user.id,
hash,
+ fee,
amount: value - tip,
currency,
rate,
diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js
index 42027f71..d9324028 100644
--- a/routes/bitcoin/send.js
+++ b/routes/bitcoin/send.js
@@ -31,7 +31,7 @@ module.exports = async (req, res) => {
total = total - change + fee;
let amount = total - fee;
- l.info("attempting bitcoin payment", user.username, total, change, amount, fee);
+ l.info("attempting bitcoin payment", user.username, { total, change, amount, fee });
let account, params;
try {
@@ -46,7 +46,7 @@ module.exports = async (req, res) => {
});
if (total > account.balance) {
- l.error("amount exceeds balance", amount, fee, balance);
+ l.error("amount exceeds balance", amount, fee, account.balance);
throw new Error("insufficient funds");
}