aboutsummaryrefslogtreecommitdiffstats
path: root/routes/liquid
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-06-08 13:04:02 -0700
committerAdam Soltys <[email protected]>2020-06-08 13:04:02 -0700
commit7a3a07bc711ea57fd8a84e809e7ecd0d6e64b5c2 (patch)
treeb31a06affa4b932990744997c271f6d65502d60c /routes/liquid
parent298672d3f50938ba06a16f8e04d607b7033703b8 (diff)
downloadcoinos-server-7a3a07bc711ea57fd8a84e809e7ecd0d6e64b5c2.tar.gz
coinos-server-7a3a07bc711ea57fd8a84e809e7ecd0d6e64b5c2.zip
reduce network traffic
Diffstat (limited to 'routes/liquid')
-rw-r--r--routes/liquid/receive.js90
-rw-r--r--routes/liquid/send.js4
2 files changed, 55 insertions, 39 deletions
diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js
index 47058c4d..b3f82a2d 100644
--- a/routes/liquid/receive.js
+++ b/routes/liquid/receive.js
@@ -28,11 +28,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
const address = o.scriptPubKey.addresses[0];
if (Object.keys(addresses).includes(address)) {
- let user = await db.User.findOne({
- where: {
- username: addresses[address]
- }
- });
+ let user = await getUser(addresses[address]);
let confirmed = 0;
@@ -69,7 +65,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
if (existing) {
({ ticker, precision, name } = existing);
- }
+ }
}
params = { ...params, ...{ ticker, precision, name } };
@@ -103,7 +99,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
const currency = invoice ? invoice.currency : user.currency;
const rate = invoice ? invoice.rate : app.get("rates")[user.currency];
- const tip = invoice ? invoice.tip : null;
+ const tip = invoice ? invoice.tip : 0;
let payment = await db.Payment.create({
account_id: account.id,
@@ -123,9 +119,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
payment = payment.get({ plain: true });
payment.account = account.get({ plain: true });
- user = await getUser(user.username);
emit(user.username, "payment", payment);
- emit(user.username, "user", user);
l.info("liquid detected", user.username, asset, value);
}
})
@@ -139,8 +133,6 @@ zmqRawBlock.on("message", async (topic, message, sequence) => {
where: { confirmed: 0 }
});
- const hashes = payments.map(p => p.hash);
-
const block = elements.Block.fromHex(message.toString("hex"), true);
const hash = await lq.getBlockHash(block.height);
const json = await lq.getBlock(hash, 2);
@@ -157,6 +149,9 @@ zmqRawBlock.on("message", async (topic, message, sequence) => {
token_amount,
token_payment_id
} = issuances[tx.txid];
+
+ const user = await getUserById(user_id);
+
let account = await db.Account.findOne({
where: { user_id, asset },
lock: transaction.LOCK.UPDATE,
@@ -168,12 +163,18 @@ zmqRawBlock.on("message", async (topic, message, sequence) => {
let payment = await db.Payment.findOne({
where: { id: asset_payment_id },
+ include: {
+ model: db.Account,
+ as: "account"
+ },
lock: transaction.LOCK.UPDATE,
transaction
});
payment.confirmed = true;
await payment.save({ transaction });
+ emit(user.username, "account", account);
+ emit(user.username, "payment", payment);
if (token) {
account = await db.Account.findOne({
@@ -187,53 +188,68 @@ zmqRawBlock.on("message", async (topic, message, sequence) => {
payment = await db.Payment.findOne({
where: { id: token_payment_id },
+ include: {
+ model: db.Account,
+ as: "account"
+ },
lock: transaction.LOCK.UPDATE,
transaction
});
payment.confirmed = true;
await payment.save({ transaction });
- }
- const user = await getUserById(user_id, transaction);
- emit(user.username, "user", user);
+ emit(user.username, "account", account);
+ emit(user.username, "payment", payment);
+ }
});
- } else if (hashes.includes(tx.txid)) queue[tx.txid] = 1;
+ } else if (payments.find(p => p.hash === tx.txid)) queue[tx.txid] = 1;
});
});
setInterval(async () => {
try {
- let arr = Object.keys(queue);
+ const arr = Object.keys(queue);
for (let i = 0; i < arr.length; i++) {
- let hash = arr[i];
+ const hash = arr[i];
- let p = await db.Payment.findOne({
+ const p = await db.Payment.findOne({
where: { hash, confirmed: 0, received: 1 },
- include: {
- model: db.Account,
- as: "account"
- }
+ include: [
+ {
+ model: db.Account,
+ as: "account"
+ },
+ {
+ model: db.User,
+ as: "user"
+ }
+ ]
});
- p.confirmed = 1;
- p.account.balance += p.amount + p.tip;
- p.account.pending -= Math.min(p.account.pending, p.amount + p.tip);
+ const { user } = p;
+
+ if (p) {
+ p.confirmed = 1;
+ p.account.balance += p.amount + p.tip;
+ p.account.pending -= Math.min(p.account.pending, p.amount + p.tip);
- await p.account.save();
- await p.save();
+ await p.account.save();
+ await p.save();
- let user = await getUserById(p.user_id);
- emit(user.username, "user", user);
- emit(user.username, "payment", p);
- l.info(
- "liquid confirmed",
- user.username,
- p.account.asset,
- p.amount,
- p.tip
- );
+ emit(user.username, "account", p.account);
+ emit(user.username, "payment", p);
+ l.info(
+ "liquid confirmed",
+ user.username,
+ p.account.asset,
+ p.amount,
+ p.tip
+ );
+ } else {
+ l.warn("couldn't find payment", hash);
+ }
delete queue[hash];
}
diff --git a/routes/liquid/send.js b/routes/liquid/send.js
index e4344238..321adba2 100644
--- a/routes/liquid/send.js
+++ b/routes/liquid/send.js
@@ -115,8 +115,8 @@ module.exports = async (req, res) => {
}
}
- user = await getUser(user.username, transaction);
- emit(user.username, "user", user);
+ emit(user.username, "account", main.account);
+ emit(user.username, "payment", main);
res.send(main);
});
} catch (e) {