From ed3482fc15d3d8f8e8b3bceff7d473fd29601280 Mon Sep 17 00:00:00 2001 From: Zoltan Date: Wed, 22 Sep 2021 18:59:06 +0300 Subject: feat: added withdraw endpoint for bitcoin --- routes/bitcoin/send.js | 61 +++++--------------------------------------------- 1 file changed, 6 insertions(+), 55 deletions(-) (limited to 'routes/bitcoin/send.js') diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index 470889b3..e0756662 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -1,4 +1,5 @@ -const btc = config.liquid.btcasset; +const decrementAccount = require('./util/decrementacc'); +const addPayment = require('./util/addPayment'); module.exports = ah(async (req, res) => { let { user } = req; @@ -31,67 +32,17 @@ module.exports = ah(async (req, res) => { total = total - change + fee; let amount = total - fee; - let account, params; + let account; try { - await db.transaction(async transaction => { - account = await db.Account.findOne({ - where: { - id: user.account_id - }, - lock: transaction.LOCK.UPDATE, - transaction - }); - - if (account.asset !== btc) { - account = await db.Account.findOne({ - where: { - user_id: user.id, - asset: btc, - pubkey: null - }, - lock: transaction.LOCK.UPDATE, - transaction - }); - } - - if (total > account.balance) { - l.error("amount exceeds balance", amount, fee, account.balance); - throw new Error("Insufficient funds"); - } - - await account.decrement({ balance: total }, { transaction }); - await account.reload({ transaction }); - }); - - params = { - amount: -amount, - fee, - memo, - account_id: account.id, - user_id: user.id, - rate: app.get("rates")[user.currency], - currency: user.currency, - address, - confirmed: true, - received: false, - network: "bitcoin" - }; + account = await decrementAccount(user, amount); if (config.bitcoin.walletpass) await bc.walletPassphrase(config.bitcoin.walletpass, 300); hex = (await bc.signRawTransactionWithWallet(hex)).hex; - params.hash = await bc.sendRawTransaction(hex); - - let payment = await db.Payment.create(params); - - payment = payment.get({ plain: true }); - payment.account = account.get({ plain: true }); - - emit(user.username, "payment", payment); - res.send(payment); + const txid = await bc.sendRawTransaction(hex); - payments.push(params.hash); + res.send(await addPayment(account, user, address, amount, memo, txid, fee)); l.info("sent bitcoin", user.username, total); } catch (e) { l.error("error sending bitcoin", e.message); -- cgit v1.2.3