aboutsummaryrefslogtreecommitdiffstats
path: root/routes/bitcoin
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/bitcoin
parent298672d3f50938ba06a16f8e04d607b7033703b8 (diff)
downloadcoinos-server-7a3a07bc711ea57fd8a84e809e7ecd0d6e64b5c2.tar.gz
coinos-server-7a3a07bc711ea57fd8a84e809e7ecd0d6e64b5c2.zip
reduce network traffic
Diffstat (limited to 'routes/bitcoin')
-rw-r--r--routes/bitcoin/receive.js59
-rw-r--r--routes/bitcoin/send.js6
2 files changed, 35 insertions, 30 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js
index 77aabd68..6928dd5c 100644
--- a/routes/bitcoin/receive.js
+++ b/routes/bitcoin/receive.js
@@ -25,7 +25,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
if (payments.includes(hash)) return;
Promise.all(
- tx.outs.map(async (o) => {
+ tx.outs.map(async o => {
const { value } = o;
let address;
@@ -40,29 +40,29 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
let user = await db.User.findOne({
where: {
- username: addresses[address],
- },
+ username: addresses[address]
+ }
});
const invoice = await db.Invoice.findOne({
where: {
user_id: user.id,
- network: "BTC",
+ network: "BTC"
},
- order: [["id", "DESC"]],
+ order: [["id", "DESC"]]
});
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 confirmed = false;
const account = await db.Account.findOne({
where: {
user_id: user.id,
- asset: config.liquid.btcasset,
- },
+ asset: config.liquid.btcasset
+ }
});
account.pending += value;
@@ -99,14 +99,13 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
tip,
confirmed,
address,
- network: "BTC",
+ network: "BTC"
});
payment = payment.get({ plain: true });
payment.account = account.get({ plain: true });
- user = await getUser(user.username);
+ emit(user.username, "account", account);
emit(user.username, "payment", payment);
- emit(user.username, "user", user);
l.info("bitcoin detected", user.username, o.value);
}
})
@@ -117,32 +116,38 @@ let queue = {};
zmqRawBlock.on("message", async (topic, message, sequence) => {
const payments = await db.Payment.findAll({
- where: { confirmed: false },
+ where: { confirmed: false }
});
- const hashes = payments.map((p) => p.hash);
-
let block = bitcoin.Block.fromHex(message.toString("hex"));
- block.transactions.map((tx) => {
+ block.transactions.map(tx => {
let hash = reverse(tx.getHash()).toString("hex");
- if (hashes.includes(hash)) queue[hash] = 1;
+ if (payments.find(p => p.hash === hash)) queue[hash] = 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"
+ }
+ ]
});
+ const { user } = p;
+
if (p) {
p.confirmed = 1;
p.account.balance += p.amount + p.tip;
@@ -151,14 +156,14 @@ setInterval(async () => {
await p.account.save();
await p.save();
- let user = await getUserById(p.user_id);
- emit(user.username, "user", user);
+ emit(user.username, "account", p.account);
emit(user.username, "payment", p);
l.info("bitcoin confirmed", user.username, p.amount, p.tip);
- delete queue[hash];
} else {
- l.warn("Couldn't find payment", hash);
+ l.warn("couldn't find payment", hash);
}
+
+ delete queue[hash];
}
} catch (e) {
l.error("problem processing queued bitcoin transaction", e.message);
diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js
index b47af00e..5dafba35 100644
--- a/routes/bitcoin/send.js
+++ b/routes/bitcoin/send.js
@@ -80,12 +80,12 @@ module.exports = async (req, res) => {
let payment = await db.Payment.create(params);
- user = await getUser(user.username);
- emit(user.username, "user", user);
-
payment = payment.get({ plain: true });
payment.account = account.get({ plain: true });
+
+ emit(user.username, "payment", payment);
res.send(payment);
+
l.info("sent bitcoin", user.username, total);
} catch (e) {
l.error("error sending bitcoin", e.message);