diff options
| author | Adam Soltys <[email protected]> | 2021-09-24 02:19:30 -0400 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2021-09-24 02:19:30 -0400 |
| commit | 18814242ad055ecd6f9de1d23209958e12aee69c (patch) | |
| tree | f9abc8b8716b5587c8a71700df62ebfe2098f4f6 /routes/liquid/send.js | |
| parent | fd44b77a4ddac5beef34f65be8e5558f4b4291f9 (diff) | |
| download | coinos-server-18814242ad055ecd6f9de1d23209958e12aee69c.tar.gz coinos-server-18814242ad055ecd6f9de1d23209958e12aee69c.zip | |
always create account on issuance
Diffstat (limited to 'routes/liquid/send.js')
| -rw-r--r-- | routes/liquid/send.js | 256 |
1 files changed, 130 insertions, 126 deletions
diff --git a/routes/liquid/send.js b/routes/liquid/send.js index 0c0b2012..f5c46a6f 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -3,165 +3,169 @@ const lcad = config.liquid.cadasset; const { Transaction } = require("@asoltys/liquidjs-lib"); sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { - l.info("sending liquid", amount, address); - if (!tx) { - ({ tx } = await liquidTx({ - address, - asset, - amount, - feeRate: 100, - replaceable: false, - user - })); - } + try { + l.info("sending liquid", amount, address); + if (!tx) { + ({ tx } = await liquidTx({ + address, + asset, + amount, + feeRate: 200, + replaceable: false, + user + })); + } - const isChange = async address => - (await lq.getAddressInfo(address)).ismine && - !Object.keys(addresses).includes(address); + const isChange = async address => + (await lq.getAddressInfo(address)).ismine && + !Object.keys(addresses).includes(address); - let totals = {}; - let change = {}; - let fee = 0; + let totals = {}; + let change = {}; + let fee = 0; - let { vout } = await lq.decodeRawTransaction(tx.hex); + let { vout } = await lq.decodeRawTransaction(tx.hex); - for (let i = 0; i < vout.length; i++) { - let { - asset, - value, - scriptPubKey: { type, addresses } - } = vout[i]; + for (let i = 0; i < vout.length; i++) { + let { + asset, + value, + scriptPubKey: { type, addresses } + } = vout[i]; - if (type === "fee") fee = toSats(value); - else { - if (!totals[asset]) totals[asset] = change[asset] = 0; - totals[asset] += toSats(value); + if (type === "fee") fee = toSats(value); + else { + if (!totals[asset]) totals[asset] = change[asset] = 0; + totals[asset] += toSats(value); - if (addresses) { - if (await isChange(addresses[0])) { - change[asset] += toSats(value); + if (addresses) { + if (await isChange(addresses[0])) { + change[asset] += toSats(value); + } } } } - } - const assets = Object.keys(totals); - const payments = []; + const assets = Object.keys(totals); + const payments = []; + + let main, signed; + 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 + }); - let main, signed; - 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 (faucet) { + covered = faucet.balance; + if (covered > fee) covered = fee; + await faucet.decrement({ balance: covered }, { transaction }); + await faucet.reload({ transaction }); + await faucet.save({ transaction }); + } + } + + total += fee - covered; + } + + if (limit && total > limit + fee) + throw new Error("Tx amount exceeds authorized amount"); - if (asset === btc) { - let covered = 0; - let nonbtc = assets.filter(a => a !== btc); - if (nonbtc.length === 1) { - let faucet = await db.Account.findOne({ + if (asset !== btc || total) { + l.info("creating liquid payment", user.username, asset, total, fee); + + let account = await db.Account.findOne({ where: { - asset: nonbtc[0], - user_id: null + user_id: user.id, + asset, + pubkey: null }, lock: transaction.LOCK.UPDATE, + order: [["balance", "DESC"]], 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 }); + 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}` + ); } - } - total += fee - covered; - } + await account.decrement({ balance: total }, { transaction }); + await account.reload({ transaction }); - if (limit && total > limit + fee) - throw new Error("Tx amount exceeds authorized amount"); - - if (asset !== btc || total) { - l.info("creating liquid payment", user.username, asset, total, fee); - - let account = await db.Account.findOne({ - where: { + let payment = { + amount: -amount, + account_id: account.id, + fee, + memo, user_id: user.id, - asset, - pubkey: null - }, - lock: transaction.LOCK.UPDATE, - order: [["balance", "DESC"]], - transaction - }); + rate: app.get("rates")[user.currency], + currency: user.currency, + address, + confirmed: true, + received: false, + network: "liquid" + }; - 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}` - ); + payment.account = account; + payments.push(payment); } - - 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); } - } - signed = await lq.signRawTransactionWithWallet( - await lq.blindRawTransaction(tx.hex) - ); - let txid = Transaction.fromHex(signed.hex).getId(); + signed = await lq.signRawTransactionWithWallet( + await lq.blindRawTransaction(tx.hex) + ); + let txid = Transaction.fromHex(signed.hex).getId(); - 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 }); + 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); - }); + emit(user.username, "account", main.account); + emit(user.username, "payment", main); + }); - if (config.liquid.walletpass) - await lq.walletPassphrase(config.liquid.walletpass, 300); + if (config.liquid.walletpass) + await lq.walletPassphrase(config.liquid.walletpass, 300); - let txid = await lq.sendRawTransaction(signed.hex); - l.info("sent liquid tx", txid, address); + let txid = await lq.sendRawTransaction(signed.hex); + l.info("sent liquid tx", txid, address); - return main; + return main; + } catch (e) { + l.error("problem sending liquid", e.message); + } }; module.exports = ah(async (req, res) => { |
