From 75bd92372f9649fecd6159f2ba9839efc5061cd6 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 17 Dec 2021 02:15:14 -0500 Subject: use current account when possible --- lib/send.js | 71 ++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 22 deletions(-) (limited to 'lib') diff --git a/lib/send.js b/lib/send.js index e08e9033..20b4c319 100644 --- a/lib/send.js +++ b/lib/send.js @@ -22,18 +22,28 @@ send = async (amount, memo, hash, user) => { let error, fee, total, preimage, payment; await db.transaction(async transaction => { - let account = await db.Account.findOne({ - where: { - user_id: user.id, - asset: config.liquid.btcasset, - pubkey: null, - balance: { - [Op.gte]: amount - } - }, - lock: transaction.LOCK.UPDATE, - transaction - }); + let account; + if (user.account.asset === config.liquid.btcasset) + account = await db.Account.findOne({ + where: { + id: user.account.id + }, + lock: transaction.LOCK.UPDATE, + transaction + }); + else + account = await db.Account.findOne({ + where: { + user_id: user.id, + asset: config.liquid.btcasset, + pubkey: null, + balance: { + [Op.gte]: amount + } + }, + lock: transaction.LOCK.UPDATE, + transaction + }); if (!account || account.balance < amount) { throw new Error("Insufficient funds"); @@ -42,6 +52,8 @@ send = async (amount, memo, hash, user) => { await account.decrement({ balance: amount }, { transaction }); await account.reload({ transaction }); + l.info("debited account", account.id); + if (config.lna.clightning) { let m = await lna.pay(hash); let sent; @@ -58,7 +70,7 @@ send = async (amount, memo, hash, user) => { amt: amount, payment_request: hash, max_parts: 10, - fee_limit: { fixed: Math.min(10000, account.balance) }, + fee_limit: { fixed: Math.min(10000, account.balance) } }); if (!m) throw new Error(`lightning payment failed ${amount} ${hash}`); @@ -79,15 +91,28 @@ send = async (amount, memo, hash, user) => { }); await db.transaction(async transaction => { - let account = await db.Account.findOne({ - where: { - user_id: user.id, - asset: config.liquid.btcasset, - pubkey: null - }, - lock: transaction.LOCK.UPDATE, - transaction - }); + let account; + if (user.account.asset === config.liquid.btcasset) + account = await db.Account.findOne({ + where: { + id: user.account.id + }, + lock: transaction.LOCK.UPDATE, + transaction + }); + else + account = await db.Account.findOne({ + where: { + user_id: user.id, + asset: config.liquid.btcasset, + pubkey: null, + balance: { + [Op.gte]: fee + } + }, + lock: transaction.LOCK.UPDATE, + transaction + }); total = parseInt(total); fee = parseInt(fee); @@ -112,6 +137,8 @@ send = async (amount, memo, hash, user) => { { transaction } ); + l.info("created payment record", payment.id); + payment = payment.get({ plain: true }); payment.account = account.get({ plain: true }); -- cgit v1.2.3