From f33881d4a314d6bbf4c58a962723e0ab8feba837 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 18 May 2020 17:46:20 -0700 Subject: handle reissuance tokens --- routes/assets.js | 69 +++++++++++++++++++++++++++++++++++++++--------- routes/liquid/receive.js | 49 +++++++++++++++++++++++++++++----- routes/send.js | 31 ++++++++++++++++------ 3 files changed, 122 insertions(+), 27 deletions(-) diff --git a/routes/assets.js b/routes/assets.js index 866c02de..dcc2059b 100644 --- a/routes/assets.js +++ b/routes/assets.js @@ -56,7 +56,7 @@ app.post("/assets", auth, async (req, res) => { contract_hash } ]); - const { asset, hex } = ria[0]; + const { asset, hex, token } = ria[0]; const brt = await lq.blindRawTransaction(hex, true, [], false); const srt = await lq.signRawTransactionWithWallet(brt); const allowed = (await lq.testMempoolAccept([srt.hex]))[0].allowed; @@ -74,7 +74,12 @@ app.post("/assets", auth, async (req, res) => { }); if (Math.round(funded.fee * SATS) > account.balance) { - l.error("amount exceeds balance", asset_amount, funded.fee, account.balance); + l.error( + "amount exceeds balance", + asset_amount, + funded.fee, + account.balance + ); throw new Error(`Insufficient funds to pay fee of ${funded.fee} BTC`); } @@ -94,18 +99,56 @@ app.post("/assets", auth, async (req, res) => { { transaction } ); - const payment = await db.Payment.create({ - account_id: account.id, - user_id, - hash: txid, - amount: asset_amount * SATS, - received: true, - confirmed: false, - address: asset_address, - network: "LBTC" - }); + const asset_payment = await db.Payment.create( + { + account_id: account.id, + user_id, + hash: txid, + amount: asset_amount * SATS, + received: true, + confirmed: false, + address: asset_address, + network: "LBTC" + }, + { transaction } + ); + + account = await db.Account.create( + { + asset: token, + user_id, + ticker: `${ticker}REISSUANCETOKEN`, + precision: 8, + name: `${name} Reissuance Token`, + balance: 0, + pending: token_amount * SATS + }, + { transaction } + ); - issuances[txid] = { user_id, asset, asset_amount, payment_id: payment.id }; + const token_payment = await db.Payment.create( + { + account_id: account.id, + user_id, + hash: txid, + amount: token_amount * SATS, + received: true, + confirmed: false, + address: token_address, + network: "LBTC" + }, + { transaction } + ); + + issuances[txid] = { + user_id, + asset, + asset_amount, + token, + token_amount, + asset_payment_id: asset_payment.id, + token_payment_id: token_payment.id, + }; let user = await getUserById(user_id, transaction); emit(user.username, "user", user); diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index 803caa90..0ce77635 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -58,6 +58,18 @@ zmqRawTx.on("message", async (topic, message, sequence) => { if (assets[asset]) { ({ ticker, precision, name } = assets[asset]); + } else { + const existing = await db.Account.findOne({ + where: { + asset + }, + order: [["id", "ASC"]], + limit: 1 + }); + + if (existing) { + ({ ticker, precision, name } = existing); + } } params = { ...params, ...{ ticker, precision, name } }; @@ -136,8 +148,16 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { json.tx.map(async tx => { if (issuances[tx.txid]) { await db.transaction(async transaction => { - const { user_id, asset, asset_amount, payment_id } = issuances[tx.txid]; - const account = await db.Account.findOne({ + const { + user_id, + asset, + asset_amount, + asset_payment_id, + token, + token_amount, + token_payment_id + } = issuances[tx.txid]; + let account = await db.Account.findOne({ where: { user_id, asset }, lock: transaction.LOCK.UPDATE, transaction @@ -146,8 +166,26 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { account.pending = 0; await account.save({ transaction }); - const payment = await db.Payment.findOne({ - where: { id: payment_id }, + let payment = await db.Payment.findOne({ + where: { id: asset_payment_id }, + lock: transaction.LOCK.UPDATE, + transaction + }); + + payment.confirmed = true; + await payment.save({ transaction }); + + account = await db.Account.findOne({ + where: { user_id, asset: token }, + lock: transaction.LOCK.UPDATE, + transaction + }); + account.balance = token_amount * SATS; + account.pending = 0; + await account.save({ transaction }); + + payment = await db.Payment.findOne({ + where: { id: token_payment_id }, lock: transaction.LOCK.UPDATE, transaction }); @@ -158,8 +196,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { const user = await getUserById(user_id, transaction); emit(user.username, "user", user); }); - } - else if (hashes.includes(tx.txid)) queue[tx.txid] = 1; + } else if (hashes.includes(tx.txid)) queue[tx.txid] = 1; }); }); diff --git a/routes/send.js b/routes/send.js index bca04e6b..7091c4bc 100644 --- a/routes/send.js +++ b/routes/send.js @@ -9,14 +9,14 @@ module.exports = async (req, res) => { return res.status(500).send("Amount must be greater than zero"); try { - await db.transaction(async (transaction) => { + await db.transaction(async transaction => { let account = await db.Account.findOne({ where: { user_id: user.id, - asset, + asset }, lock: transaction.LOCK.UPDATE, - transaction, + transaction }); if (account.balance < amount) { @@ -37,7 +37,7 @@ module.exports = async (req, res) => { currency: user.currency, confirmed: true, hash: "Internal Transfer", - network: "COINOS", + network: "COINOS" }, { transaction } ); @@ -52,18 +52,18 @@ module.exports = async (req, res) => { res.send(payment); user = await db.User.findOne({ - where: { username }, + where: { username } }); let params = { user_id: user.id, - asset, + asset }; account = await db.Account.findOne({ where: params, lock: transaction.LOCK.UPDATE, - transaction, + transaction }); if (account) { @@ -79,6 +79,21 @@ module.exports = async (req, res) => { if (assets[asset]) { ({ ticker, precision, name } = assets[asset]); + } else { + const existing = await db.Account.findOne({ + where: { + asset + }, + order: [["id", "ASC"]], + limit: 1 + }); + + l.info("existing", existing); + + if (existing) { + l.info(existing.name); + ({ ticker, precision, name } = existing); + } } params = { ...params, ...{ ticker, precision, name } }; @@ -97,7 +112,7 @@ module.exports = async (req, res) => { confirmed: true, hash: "Internal Transfer", network: "COINOS", - received: true, + received: true }, { transaction } ); -- cgit v1.2.3