diff options
| author | Adam Soltys <[email protected]> | 2021-02-19 21:46:21 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2021-02-19 21:46:21 -0800 |
| commit | fb2a1a9f7262c58636b62fe1eee1b95e88a481c8 (patch) | |
| tree | fc05ad9ac87f402ec48e08d8792e44df72631d23 /routes/liquid/send.js | |
| parent | 035afbbbc1c00f15d3956dbd21adb3a9c82c223e (diff) | |
| download | coinos-server-fb2a1a9f7262c58636b62fe1eee1b95e88a481c8.tar.gz coinos-server-fb2a1a9f7262c58636b62fe1eee1b95e88a481c8.zip | |
instant lightning->liquid conversion
Diffstat (limited to 'routes/liquid/send.js')
| -rw-r--r-- | routes/liquid/send.js | 185 |
1 files changed, 94 insertions, 91 deletions
diff --git a/routes/liquid/send.js b/routes/liquid/send.js index 01b1a41d..ee62c1b8 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -1,10 +1,7 @@ const btc = config.liquid.btcasset; const lcad = config.liquid.cadasset; -module.exports = ah(async (req, res) => { - let { user } = req; - let { address, memo, tx } = req.body; - +sendLiquid = async ({ user, address, memo, tx }) => { const isChange = async address => (await lq.getAddressInfo(address)).ismine && !Object.keys(addresses).includes(address); @@ -38,113 +35,119 @@ module.exports = ah(async (req, res) => { const assets = Object.keys(totals); const payments = []; - try { - await db.transaction(async transaction => { - for (let i = 0; i < assets.length; i++) { - let asset = assets[i]; - let amount = totals[asset]; - if (change[asset]) amount -= change[asset]; - let total = amount; - - if (asset === btc) { - let covered = 0; - let nonbtc = assets.filter(a => a !== btc); - if (nonbtc.length === 1) { - let faucet = await db.Account.findOne({ - where: { - asset: nonbtc[0], - user_id: null - }, - lock: transaction.LOCK.UPDATE, - transaction - }); - - if (faucet) { - covered = faucet.balance; - if (covered > fee) covered = fee; - await faucet.decrement({ balance: covered }, { transaction }); - await faucet.reload({ transaction }); - await faucet.save({ transaction }); - } + return db.transaction(async transaction => { + for (let i = 0; i < assets.length; i++) { + let asset = assets[i]; + let amount = totals[asset]; + if (change[asset]) amount -= change[asset]; + let total = amount; - total += fee - covered; - } - } - - if (asset !== btc || total) { - l.info("creating liquid payment", user.username, asset, total, fee); - - let account = await db.Account.findOne({ + if (asset === btc) { + let covered = 0; + let nonbtc = assets.filter(a => a !== btc); + if (nonbtc.length === 1) { + let faucet = await db.Account.findOne({ where: { - user_id: user.id, - asset, - pubkey: null + asset: nonbtc[0], + user_id: null }, lock: transaction.LOCK.UPDATE, - order: [['balance', 'DESC']], transaction }); - if (total > account.balance) { - l.warn("amount exceeds balance", { - total, - fee, - balance: account.balance - }); - throw new Error( - `Insufficient funds, need ${total} ${ - account.ticker === "BTC" ? "SAT" : account.ticker - }, have ${account.balance}` - ); + if (faucet) { + covered = faucet.balance; + if (covered > fee) covered = fee; + await faucet.decrement({ balance: covered }, { transaction }); + await faucet.reload({ transaction }); + await faucet.save({ transaction }); } - await account.decrement({ balance: total }, { transaction }); - await account.reload({ transaction }); + total += fee - covered; + } + } - let payment = { - amount: -amount, - account_id: account.id, - fee, - memo, + if (asset !== btc || total) { + l.info("creating liquid payment", user.username, asset, total, fee); + + let account = await db.Account.findOne({ + where: { user_id: user.id, - rate: app.get("rates")[user.currency], - currency: user.currency, - address, - confirmed: true, - received: false, - network: "liquid" - }; + asset, + pubkey: null + }, + lock: transaction.LOCK.UPDATE, + order: [["balance", "DESC"]], + transaction + }); - payment.account = account; - payments.push(payment); + if (total > account.balance) { + l.warn("amount exceeds balance", { + total, + fee, + balance: account.balance + }); + throw new Error( + `Insufficient funds, need ${total} ${ + account.ticker === "BTC" ? "SAT" : account.ticker + }, have ${account.balance}` + ); } + + await account.decrement({ balance: total }, { transaction }); + await account.reload({ transaction }); + + let payment = { + amount: -amount, + account_id: account.id, + fee, + memo, + user_id: user.id, + rate: app.get("rates")[user.currency], + currency: user.currency, + address, + confirmed: true, + received: false, + network: "liquid" + }; + + payment.account = account; + payments.push(payment); } + } - if (config.liquid.walletpass) - await lq.walletPassphrase(config.liquid.walletpass, 300); + if (config.liquid.walletpass) + await lq.walletPassphrase(config.liquid.walletpass, 300); - let blinded = await lq.blindRawTransaction(tx.hex); - let signed = await lq.signRawTransactionWithWallet(blinded); - let txid = await lq.sendRawTransaction(signed.hex); + let blinded = await lq.blindRawTransaction(tx.hex); + let signed = await lq.signRawTransactionWithWallet(blinded); + let txid = await lq.sendRawTransaction(signed.hex); - let main; - for (let i = 0; i < assets.length; i++) { - p = payments[i]; - if (p) { - let { account } = p; - p.hash = txid; - p = await db.Payment.create(p, { transaction }); - if (account.ticker !== "BTC" || !main) { - main = p.get({ plain: true }); - main.account = account.get({ plain: true }); - } + let main; + for (let i = 0; i < assets.length; i++) { + p = payments[i]; + if (p) { + let { account } = p; + p.hash = txid; + p = await db.Payment.create(p, { transaction }); + if (account.ticker !== "BTC" || !main) { + main = p.get({ plain: true }); + main.account = account.get({ plain: true }); } } + } - emit(user.username, "account", main.account); - emit(user.username, "payment", main); - res.send(main); - }); + emit(user.username, "account", main.account); + emit(user.username, "payment", main); + return main; + }); +}; + +module.exports = ah(async (req, res) => { + let { user } = req; + let { address, memo, tx } = req.body; + try { + res.send(await sendLiquid({ user, address, memo, tx })); } catch (e) { l.error("problem sending liquid", user.username, e.message); return res.status(500).send(e.message); |
