aboutsummaryrefslogtreecommitdiffstats
path: root/routes/bitcoin
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-06-21 22:44:07 -0700
committerAdam Soltys <[email protected]>2022-06-21 22:44:15 -0700
commit268d283d830d8cb8f35415f2f168bae69c8a9b6e (patch)
tree2bbf4c0193698f7c514f878b453319c34ebc709f /routes/bitcoin
parent3f120a096a0465ccacd35f404237a6fb7cbe7ad2 (diff)
downloadcoinos-server-268d283d830d8cb8f35415f2f168bae69c8a9b6e.tar.gz
coinos-server-268d283d830d8cb8f35415f2f168bae69c8a9b6e.zip
fixes for receiving liquid assets
Diffstat (limited to 'routes/bitcoin')
-rw-r--r--routes/bitcoin/receive.js151
1 files changed, 81 insertions, 70 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js
index 45dadbbe..b11a5de7 100644
--- a/routes/bitcoin/receive.js
+++ b/routes/bitcoin/receive.js
@@ -49,88 +49,99 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
Object.keys(addresses).includes(address) &&
!change.includes(address)
) {
- let user = await db.User.findOne({
- where: {
- username: addresses[address]
- }
- });
+ await db.transaction(async transaction => {
+ let user = await db.User.findOne({
+ where: {
+ username: addresses[address]
+ },
+ transaction
+ });
- const invoice = await db.Invoice.findOne({
- where: {
- address,
- user_id: user.id,
- network: "bitcoin"
- },
- order: [["id", "DESC"]],
- include: {
- model: db.Account,
- as: "account"
- }
- });
+ const invoice = await db.Invoice.findOne({
+ where: {
+ address,
+ user_id: user.id,
+ network: "bitcoin"
+ },
+ order: [["id", "DESC"]],
+ include: {
+ model: db.Account,
+ as: "account"
+ },
+ transaction
+ });
- if (!invoice) return;
+ if (!invoice) return;
- const currency = invoice ? invoice.currency : user.currency;
- const rate = invoice ? invoice.rate : app.get("rates")[user.currency];
- const tip = invoice ? invoice.tip : 0;
- const memo = invoice ? invoice.memo : "";
+ const currency = invoice ? invoice.currency : user.currency;
+ const rate = invoice
+ ? invoice.rate
+ : app.get("rates")[user.currency];
+ const tip = invoice ? invoice.tip : 0;
+ const memo = invoice ? invoice.memo : "";
- let confirmed = false;
+ let confirmed = false;
- let { account } = invoice;
+ let { account } = invoice;
- if (account.asset !== config.liquid.btcasset) {
- account = await db.Account.findOne({
- where: {
- user_id: user.id,
- asset: config.liquid.btcasset,
- pubkey: null
- }
- });
- }
+ if (account.asset !== config.liquid.btcasset) {
+ account = await db.Account.findOne({
+ where: {
+ user_id: user.id,
+ asset: config.liquid.btcasset,
+ pubkey: null
+ },
+ transaction
+ });
+ }
- account.pending += value;
- await account.save();
+ await account.increment({ pending: value }, { transaction });
+ await account.save({ transaction });
- if (config.bitcoin.walletpass)
- await bc.walletPassphrase(config.bitcoin.walletpass, 300);
+ if (config.bitcoin.walletpass)
+ await bc.walletPassphrase(config.bitcoin.walletpass, 300);
- 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);
- let inputTx = bitcoin.Transaction.fromHex(hex);
- totalInputs += inputTx.outs[index].value;
- }
- let fee = totalInputs - totalOutputs;
+ 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);
+ 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,
- memo,
- amount: value - tip,
- currency,
- rate,
- received: true,
- tip,
- confirmed,
- address,
- network: "bitcoin",
- invoice_id: invoice.id
- });
- payment = payment.get({ plain: true });
- payment.account = account.get({ plain: true });
+ let payment = await db.Payment.create(
+ {
+ account_id: account.id,
+ user_id: user.id,
+ hash,
+ fee,
+ memo,
+ amount: value - tip,
+ currency,
+ rate,
+ received: true,
+ tip,
+ confirmed,
+ address,
+ network: "bitcoin",
+ invoice_id: invoice.id
+ },
+ { transaction }
+ );
- emit(user.username, "account", account);
+ payment = payment.get({ plain: true });
+ payment.account = account.get({ plain: true });
- emit(user.username, "payment", payment);
- l.info("bitcoin detected", user.username, value);
- notify(user, `${value} SAT payment detected`);
- callWebhook(invoice, payment);
+ emit(user.username, "account", account);
+
+ emit(user.username, "payment", payment);
+ l.info("bitcoin detected", user.username, value);
+ notify(user, `${value} SAT payment detected`);
+ callWebhook(invoice, payment);
+ });
}
} catch (e) {
console.log(e);