From 7a3a07bc711ea57fd8a84e809e7ecd0d6e64b5c2 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 8 Jun 2020 13:04:02 -0700 Subject: reduce network traffic --- routes/assets.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'routes/assets.js') diff --git a/routes/assets.js b/routes/assets.js index 9c60500c..25ed8ea6 100644 --- a/routes/assets.js +++ b/routes/assets.js @@ -56,15 +56,15 @@ app.post("/assets", auth, async (req, res) => { params.token_amount = token_amount; params.token_address = token_address; params.token_amount = parseInt(params.token_amount); - } + } let ria; try { ria = await lq.rawIssueAsset(funded.hex, [params]); - } catch(e) { + } catch (e) { l.info(asset_amount, token_amount, params); throw new Error(e.message); - } + } const { asset, hex, token } = ria[0]; const brt = await lq.blindRawTransaction(hex, true, [], false); @@ -79,10 +79,16 @@ app.post("/assets", auth, async (req, res) => { user_id, asset: config.liquid.btcasset }, + include: { + model: db.User, + as: "user" + }, lock: transaction.LOCK.UPDATE, transaction }); + let { user } = account; + if (Math.round(funded.fee * SATS) > account.balance) { l.error( "amount exceeds balance", @@ -95,6 +101,7 @@ app.post("/assets", auth, async (req, res) => { account.balance -= Math.round(funded.fee * SATS); await account.save({ transaction }); + emit(user.username, "account", account); account = await db.Account.create( { @@ -109,6 +116,8 @@ app.post("/assets", auth, async (req, res) => { { transaction } ); + emit(user.username, "account", account); + const asset_payment = await db.Payment.create( { account_id: account.id, @@ -122,12 +131,13 @@ app.post("/assets", auth, async (req, res) => { }, { transaction } ); + emit(user.username, "payment", asset_payment); issuances[txid] = { user_id, asset, asset_amount, - asset_payment_id: asset_payment.id, + asset_payment_id: asset_payment.id }; if (token_amount) { @@ -143,6 +153,7 @@ app.post("/assets", auth, async (req, res) => { }, { transaction } ); + emit(user.username, "account", account); const token_payment = await db.Payment.create( { @@ -157,15 +168,12 @@ app.post("/assets", auth, async (req, res) => { }, { transaction } ); + emit(user.username, "payment", token_payment); issuances[txid].token = token; issuances[txid].token_amount = token_amount; issuances[txid].token_payment_id = token_payment.id; } - - - let user = await getUserById(user_id, transaction); - emit(user.username, "user", user); }); res.send(issuances[txid]); -- cgit v1.2.3