diff options
| author | Adam Soltys <[email protected]> | 2021-12-17 02:15:14 -0500 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2021-12-17 02:15:14 -0500 |
| commit | 75bd92372f9649fecd6159f2ba9839efc5061cd6 (patch) | |
| tree | 6b511dd0b6ff405d4dcc8a262d8f527f3696d647 /lib/send.js | |
| parent | cced852e8df945b95deb332cc1162522d9c67137 (diff) | |
| download | coinos-server-75bd92372f9649fecd6159f2ba9839efc5061cd6.tar.gz coinos-server-75bd92372f9649fecd6159f2ba9839efc5061cd6.zip | |
use current account when possible
Diffstat (limited to 'lib/send.js')
| -rw-r--r-- | lib/send.js | 71 |
1 files changed, 49 insertions, 22 deletions
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 }); |
