diff options
| author | Adam Soltys <[email protected]> | 2021-08-09 20:55:17 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2021-08-09 20:55:17 -0700 |
| commit | 56559c7b9749fe62fe2fe0ba90725391d83b8bf7 (patch) | |
| tree | 48d6a0630d05d6787b4410708a5efceff472636b /routes/liquid | |
| parent | 803ecab2b5c3a00d107372ea6757cd6ce0f8224d (diff) | |
| parent | 8d6f65079868ae6134f593752e3380180591623d (diff) | |
| download | coinos-server-56559c7b9749fe62fe2fe0ba90725391d83b8bf7.tar.gz coinos-server-56559c7b9749fe62fe2fe0ba90725391d83b8bf7.zip | |
Merge branch 'v1.0.0'
Diffstat (limited to 'routes/liquid')
| -rw-r--r-- | routes/liquid/fee.js | 9 | ||||
| -rw-r--r-- | routes/liquid/receive.js | 71 | ||||
| -rw-r--r-- | routes/liquid/send.js | 34 |
3 files changed, 58 insertions, 56 deletions
diff --git a/routes/liquid/fee.js b/routes/liquid/fee.js index 2070873a..fad69f13 100644 --- a/routes/liquid/fee.js +++ b/routes/liquid/fee.js @@ -3,11 +3,6 @@ const buildTx = require("../../lib/buildliquidtx"); liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => { let tx, fee; let node = lq; - /* - if (asset === config.liquid.btcasset) { - node = rare; - } - */ if (user.account.pubkey) { let psbt = await buildTx({ @@ -64,6 +59,8 @@ liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => { } ); + l.info("funding tx for fee estimate", tx); + tx = await node.fundRawTransaction(tx, params); let blinded = await node.blindRawTransaction(tx.hex); @@ -81,7 +78,7 @@ module.exports = ah(async (req, res) => { let tx = await liquidTx({ ...req.body, user: req.user }) res.send(tx); } catch (e) { - l.error("error estimating liquid fee", e.message, e.stack); + l.error("error estimating liquid fee", e.message); return res.status(500).send(e.message); } }); diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index 3a6aab10..2396755f 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -15,7 +15,7 @@ const getAccount = async (params, transaction) => { let account = await db.Account.findOne({ where: params, lock: transaction.LOCK.UPDATE, - transaction + transaction, }); if (account) { @@ -37,12 +37,12 @@ const getAccount = async (params, transaction) => { const existing = await db.Account.findOne({ where: { asset, - pubkey + pubkey, }, order: [["id", "ASC"]], limit: 1, lock: transaction.LOCK.UPDATE, - transaction + transaction, }); if (existing) { @@ -65,6 +65,8 @@ const zmqRawTx = zmq.socket("sub"); zmqRawTx.connect(config.liquid.zmqrawtx); zmqRawTx.subscribe("rawtx"); +const queue = {}; + zmqRawTx.on("message", async (topic, message, sequence) => { const hex = message.toString("hex"); @@ -80,7 +82,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { if (payments.includes(blinded.txid)) return; Promise.all( - tx.vout.map(async o => { + tx.vout.map(async (o) => { try { if (!(o.scriptPubKey && o.scriptPubKey.addresses)) return; @@ -92,14 +94,14 @@ zmqRawTx.on("message", async (topic, message, sequence) => { Object.keys(addresses).includes(address) && !change.includes(address) ) { - await db.transaction(async transaction => { + await db.transaction(async (transaction) => { let user = await getUser(addresses[address], transaction); let invoice = await db.Invoice.findOne({ where: { unconfidential: address, user_id: user.id, - network: "liquid" + network: "liquid", }, order: [["id", "DESC"]], }); @@ -110,10 +112,10 @@ zmqRawTx.on("message", async (topic, message, sequence) => { let account = await db.Account.findOne({ where: { - id: invoice.account_id + id: invoice.account_id, }, lock: transaction.LOCK.UPDATE, - transaction + transaction, }); if ( @@ -131,7 +133,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { asset, pubkey: account.pubkey, pending: value, - index: 0 + index: 0, }, transaction ); @@ -162,7 +164,8 @@ zmqRawTx.on("message", async (topic, message, sequence) => { tip, confirmed, address, - network: "liquid" + network: "liquid", + invoice_id: invoice.id, }, { transaction } ); @@ -185,12 +188,10 @@ zmqRawTx.on("message", async (topic, message, sequence) => { ); }); -let queue = {}; - zmqRawBlock.on("message", async (topic, message, sequence) => { try { const payments = await db.Payment.findAll({ - where: { confirmed: 0 } + where: { confirmed: 0 }, }); const block = Block.fromHex(message.toString("hex"), true); @@ -200,9 +201,9 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { hash = await lq.getBlockHash(block.height); json = await lq.getBlock(hash, 2); - json.tx.map(async tx => { + json.tx.map(async (tx) => { if (issuances[tx.txid]) { - await db.transaction(async transaction => { + await db.transaction(async (transaction) => { const { user_id, asset, @@ -210,7 +211,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { asset_payment_id, token, token_amount, - token_payment_id + token_payment_id, } = issuances[tx.txid]; const user = await getUserById(user_id); @@ -218,7 +219,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { let account = await db.Account.findOne({ where: { user_id, asset }, lock: transaction.LOCK.UPDATE, - transaction + transaction, }); account.balance = asset_amount * SATS; account.pending = 0; @@ -228,10 +229,10 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { where: { id: asset_payment_id }, include: { model: db.Account, - as: "account" + as: "account", }, lock: transaction.LOCK.UPDATE, - transaction + transaction, }); payment.confirmed = true; @@ -246,7 +247,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { account = await db.Account.findOne({ where: { user_id, asset: token }, lock: transaction.LOCK.UPDATE, - transaction + transaction, }); account.balance = token_amount * SATS; account.pending = 0; @@ -256,10 +257,10 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { where: { id: token_payment_id }, include: { model: db.Account, - as: "account" + as: "account", }, lock: transaction.LOCK.UPDATE, - transaction + transaction, }); payment.confirmed = true; @@ -272,7 +273,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { emit(user.username, "payment", payment); } }); - } else if (payments.find(p => p.hash === tx.txid)) queue[tx.txid] = 1; + } else if (payments.find((p) => p.hash === tx.txid)) queue[tx.txid] = 1; }); } catch (e) { return console.log(e); @@ -286,26 +287,26 @@ setInterval(async () => { for (let i = 0; i < arr.length; i++) { const hash = arr[i]; - let account, address, user, total; - await db.transaction(async transaction => { - let p = await db.Payment.findOne({ + let account, address, user, total, p; + await db.transaction(async (transaction) => { + p = await db.Payment.findOne({ where: { hash, confirmed: 0, received: 1 }, include: [ { model: db.Account, - as: "account" + as: "account", }, { model: db.Invoice, - as: "invoice" + as: "invoice", }, { model: db.User, - as: "user" - } + as: "user", + }, ], lock: transaction.LOCK.UPDATE, - transaction + transaction, }); ({ account, address, user } = p); @@ -338,7 +339,7 @@ setInterval(async () => { ); notify(user, `${total} SAT payment confirmed`); - await callWebhook(p.invoice, p); + callWebhook(p.invoice, p); } else { l.warn("couldn't find liquid payment", hash); } @@ -347,7 +348,7 @@ setInterval(async () => { }); let c = convert[address]; - if (address && c) { + if (address && c && p.account.asset === network.assetHash) { l.info( "liquid detected for conversion request", address, @@ -361,11 +362,11 @@ setInterval(async () => { address: c.address, amount: total - 100, user, - limit: total + limit: total, }); } } } catch (e) { l.error("problem processing queued liquid transaction", e.message, e.stack); } -}, 1000); +}); diff --git a/routes/liquid/send.js b/routes/liquid/send.js index 19b7344f..5abc692f 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -1,5 +1,6 @@ const btc = config.liquid.btcasset; const lcad = config.liquid.cadasset; +const { Transaction } = require("@asoltys/liquidjs-lib"); sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { if (amount > 10000000) throw new Error("Amount too large"); @@ -48,7 +49,8 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { const assets = Object.keys(totals); const payments = []; - return db.transaction(async 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]; @@ -75,12 +77,12 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { await faucet.reload({ transaction }); await faucet.save({ transaction }); } - - total += fee - covered; } + + total += fee - covered; } - if (limit && total > limit) + if (limit && total > limit + fee) throw new Error("Tx amount exceeds authorized amount"); if (asset !== btc || total) { @@ -132,16 +134,11 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { } } - 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); - - l.info("sent liquid tx", txid, address); + signed = await lq.signRawTransactionWithWallet( + await lq.blindRawTransaction(tx.hex) + ); + let txid = Transaction.fromHex(signed.hex).getId(); - let main; for (let i = 0; i < assets.length; i++) { p = payments[i]; if (p) { @@ -157,15 +154,22 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { emit(user.username, "account", main.account); emit(user.username, "payment", main); - return main; }); + + 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); + + return main; }; module.exports = ah(async (req, res) => { let { user } = req; try { - res.send(await sendLiquid({...req.body, user })); + res.send(await sendLiquid({ ...req.body, user })); } catch (e) { l.error("problem sending liquid", user.username, e.message); return res.status(500).send(e); |
