From 14b363bcfcfdd03f2232c2d0779ee90fc5cc27e6 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 18 Oct 2022 21:00:50 -0700 Subject: improve asset issuance to internal user --- routes/assets.js | 116 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 41 deletions(-) (limited to 'routes/assets.js') diff --git a/routes/assets.js b/routes/assets.js index ecc44d84..de539946 100644 --- a/routes/assets.js +++ b/routes/assets.js @@ -120,7 +120,7 @@ app.post("/assets", auth, async (req, res) => { l( "attempting issuance", req.user.username, - contract, + JSON.stringify(contract), "address", address, "pubkey", @@ -177,6 +177,7 @@ app.post("/assets", auth, async (req, res) => { as: "user" }, order: [["balance", "DESC"]], + lock: transaction.LOCK.UPDATE, transaction }); @@ -199,23 +200,51 @@ app.post("/assets", auth, async (req, res) => { await account.reload({ transaction }); emit(user.username, "account", account); + let invoice; + let recipient = user; + if (address) { + invoice = await db.Invoice.findOne({ + where: { address }, + include: { + model: db.User, + as: "user" + } + }); + recipient = invoice.user; + } + + if (!invoice) { + invoice = 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 } + ); + } + account = await db.Account.create( { asset, contract, domain, - user_id, + user_id: invoice.user_id, ticker, precision, name, balance: 0, network: "liquid", - pending: address ? 0 : Math.round(params.asset_amount * SATS) + pending: Math.round(params.asset_amount * SATS) }, { transaction } ); - emit(user.username, "account", account); + emit(recipient.username, "account", account); l( "issued asset", user.username, @@ -225,24 +254,11 @@ app.post("/assets", auth, async (req, res) => { account.id ); - 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, + user_id: invoice.user_id, + invoice_id: invoice.id, hash: txid, amount: Math.round(params.asset_amount * SATS), received: true, @@ -252,14 +268,14 @@ app.post("/assets", auth, async (req, res) => { }, { transaction } ); - emit(user.username, "payment", asset_payment); + emit(recipient.username, "payment", asset_payment); - if (address) { + if (invoice.user_id !== user.id) { const asset_send = await db.Payment.create( { account_id: account.id, user_id, - invoice_id, + invoice_id: invoice.id, hash: txid, amount: -Math.round(params.asset_amount * SATS), received: false, @@ -269,7 +285,7 @@ app.post("/assets", auth, async (req, res) => { }, { transaction } ); - emit(user.username, "payment", asset_send); + emit(recipient.username, "payment", asset_send); } store.issuances[txid] = { @@ -280,10 +296,39 @@ app.post("/assets", auth, async (req, res) => { }; if (token_amount) { + invoice = null; + recipient = user; + + if (address) { + invoice = await db.Invoice.findOne({ + where: { address }, + include: { + model: db.User, + as: "user" + } + }); + recipient = invoice.user; + } + + if (!invoice) { + invoice = await db.Invoice.create( + { + account_id: account.id, + user_id, + amount: Math.round(params.token_amount * SATS), + address: token_address, + currency: user.currency, + rate: store.rates[user.currency], + network: "liquid" + }, + { transaction } + ); + } + account = await db.Account.create( { asset: token, - user_id, + user_id: invoice.user_id, domain, ticker: `${ticker}REISSUANCETOKEN`, precision: 8, @@ -293,24 +338,12 @@ app.post("/assets", auth, async (req, res) => { }, { transaction } ); - emit(user.username, "account", account); - - 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 } - ); + emit(recipient.username, "account", account); const token_payment = await db.Payment.create( { account_id: account.id, + invoice_id: invoice.id, user_id, hash: txid, amount: Math.round(token_amount * SATS), @@ -321,12 +354,13 @@ app.post("/assets", auth, async (req, res) => { }, { transaction } ); - emit(user.username, "payment", token_payment); + emit(recipient.username, "payment", token_payment); - if (address) { + if (invoice.user_id !== user.id) { const token_send = await db.Payment.create( { account_id: account.id, + invoice_id: invoice.id, user_id, hash: txid, amount: -Math.round(token_amount * SATS), @@ -337,7 +371,7 @@ app.post("/assets", auth, async (req, res) => { }, { transaction } ); - emit(user.username, "payment", token_send); + emit(recipient.username, "payment", token_send); } store.issuances[txid].token = token; -- cgit v1.2.3