diff options
| author | Adam Soltys <[email protected]> | 2022-10-07 21:10:48 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-10-07 21:10:48 -0700 |
| commit | 756017c5bf6b1858c231d39670b217a606f4e454 (patch) | |
| tree | d5eb774ac6c83ff2079766ba2a1f65ce3dd64c31 /routes | |
| parent | 0eb3dcabf63cda38dec86f9b3d4accac6c057f4d (diff) | |
| download | coinos-server-756017c5bf6b1858c231d39670b217a606f4e454.tar.gz coinos-server-756017c5bf6b1858c231d39670b217a606f4e454.zip | |
fix liquid asset receiving
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/assets.js | 104 | ||||
| -rw-r--r-- | routes/liquid/receive.js | 48 |
2 files changed, 107 insertions, 45 deletions
diff --git a/routes/assets.js b/routes/assets.js index 53e89602..ecc44d84 100644 --- a/routes/assets.js +++ b/routes/assets.js @@ -225,30 +225,60 @@ app.post("/assets", auth, async (req, res) => { account.id ); - if (!address) { - const asset_payment = await db.Payment.create( + const { id: invoice_id } = await db.Invoice.create( + { + account_id: account.id, + user_id, + amount: Math.round(params.asset_amount * SATS), + address: asset_address, + currency: user.currency, + rate: store.rates[user.currency], + network: "liquid" + }, + { transaction } + ); + + const asset_payment = await db.Payment.create( + { + account_id: account.id, + user_id, + invoice_id, + hash: txid, + amount: Math.round(params.asset_amount * SATS), + received: true, + confirmed: false, + address: asset_address, + network: "liquid" + }, + { transaction } + ); + emit(user.username, "payment", asset_payment); + + if (address) { + const asset_send = await db.Payment.create( { account_id: account.id, user_id, + invoice_id, hash: txid, - amount: Math.round(params.asset_amount * SATS), - received: true, + amount: -Math.round(params.asset_amount * SATS), + received: false, confirmed: false, - address: asset_address, + address, network: "liquid" }, { transaction } ); - emit(user.username, "payment", asset_payment); - - store.issuances[txid] = { - user_id, - asset, - asset_amount: params.asset_amount, - asset_payment_id: asset_payment.id - }; + emit(user.username, "payment", asset_send); } + store.issuances[txid] = { + user_id, + asset, + asset_amount: params.asset_amount, + asset_payment_id: asset_payment.id + }; + if (token_amount) { account = await db.Account.create( { @@ -265,26 +295,54 @@ app.post("/assets", auth, async (req, res) => { ); emit(user.username, "account", account); - if (!address) { - const token_payment = await db.Payment.create( + const { id: invoice_id } = await db.Invoice.create( + { + account_id: account.id, + user_id, + amount: Math.round(params.asset_amount * SATS), + address: asset_address, + currency: user.currency, + rate: store.rates[user.currency], + network: "liquid" + }, + { transaction } + ); + + const token_payment = await db.Payment.create( + { + account_id: account.id, + user_id, + hash: txid, + amount: Math.round(token_amount * SATS), + received: true, + confirmed: false, + address: token_address, + network: "liquid" + }, + { transaction } + ); + emit(user.username, "payment", token_payment); + + if (address) { + const token_send = await db.Payment.create( { account_id: account.id, user_id, hash: txid, - amount: Math.round(token_amount * SATS), - received: true, + amount: -Math.round(token_amount * SATS), + received: false, confirmed: false, - address: token_address, + address, network: "liquid" }, { transaction } ); - emit(user.username, "payment", token_payment); - - store.issuances[txid].token = token; - store.issuances[txid].token_amount = token_amount; - store.issuances[txid].token_payment_id = token_payment.id; + emit(user.username, "payment", token_send); } + + store.issuances[txid].token = token; + store.issuances[txid].token_amount = token_amount; + store.issuances[txid].token_payment_id = token_payment.id; } }); diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index a8d73718..8f482d39 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -1,6 +1,6 @@ import db from "$db"; import config from "$config"; -import { getBlockHeight, getUser, toSats } from "$lib/utils"; +import { getBlockHeight, getUser, getUserById, toSats, SATS } from "$lib/utils"; import { emit } from "$lib/sockets"; import store from "$lib/store"; import { sendLiquid } from "$routes/liquid/send"; @@ -98,30 +98,31 @@ const queue = {}; const seen = []; zmqRawTx.on("message", async (topic, message, sequence) => { - const hex = message.toString("hex"); - - let unblinded, tx, blinded, txid; try { - txid = Transaction.fromHex(hex).getId(); - if (seen.includes(txid)) return; - seen.push(txid); - if (seen.length > 5000) seen.shift(); + const hex = message.toString("hex"); - unblinded = await lq.unblindRawTransaction(hex); - tx = await lq.decodeRawTransaction(unblinded.hex); - blinded = await lq.decodeRawTransaction(hex); - } catch (e) { - return err("problem decoding liquid tx", e.message); - } + let unblinded, tx, blinded, txid; + try { + txid = Transaction.fromHex(hex).getId(); + if (seen.includes(txid)) return; + seen.push(txid); + if (seen.length > 5000) seen.shift(); + + unblinded = await lq.unblindRawTransaction(hex); + tx = await lq.decodeRawTransaction(unblinded.hex); + blinded = await lq.decodeRawTransaction(hex); + } catch (e) { + return err("problem decoding liquid tx", e.message); + } - Promise.all( - tx.vout.map(async o => { + for (let o of tx.vout) { try { - if (!(o.scriptPubKey && o.scriptPubKey.address)) return; + if (!(o.scriptPubKey && o.scriptPubKey.addresses)) return; const { asset } = o; const value = toSats(o.value); - const { address } = o.scriptPubKey; + const { addresses } = o.scriptPubKey; + const address = addresses[0]; if ( Object.keys(store.addresses).includes(address) && @@ -212,10 +213,13 @@ zmqRawTx.on("message", async (topic, message, sequence) => { }); } } catch (e) { + console.log(e); err("Problem processing transaction", e.message, e.stack); } - }) - ); + } + } catch (e) { + console.log(e); + } }); zmqRawBlock.on("message", async (topic, message, sequence) => { @@ -229,7 +233,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { hash = await lq.getBlockHash(getBlockHeight(message)); json = await lq.getBlock(hash, 2); - json.tx.map(async tx => { + for (let tx of json.tx) { if (store.issuances[tx.txid]) { await db.transaction(async transaction => { const { @@ -302,7 +306,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { } }); } else if (payments.find(p => p.hash === tx.txid)) queue[tx.txid] = 1; - }); + } } catch (e) { return console.log(e); } |
