aboutsummaryrefslogtreecommitdiffstats
path: root/bitcoinPayments.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-01-05 18:39:30 -0800
committerAdam Soltys <[email protected]>2020-01-05 18:39:30 -0800
commitb0e279aff714164a01ece9d2d933e4649d3ad48c (patch)
tree2ceabc01f0e8f3f54e4f91ec4aed98bcc43c1488 /bitcoinPayments.js
parent3b841f05921311cf149b7987d76738fcb053a52e (diff)
downloadcoinos-server-b0e279aff714164a01ece9d2d933e4649d3ad48c.tar.gz
coinos-server-b0e279aff714164a01ece9d2d933e4649d3ad48c.zip
queue on-chain payments
Diffstat (limited to 'bitcoinPayments.js')
-rw-r--r--bitcoinPayments.js67
1 files changed, 28 insertions, 39 deletions
diff --git a/bitcoinPayments.js b/bitcoinPayments.js
index cdcfde5a..cd9aa713 100644
--- a/bitcoinPayments.js
+++ b/bitcoinPayments.js
@@ -22,6 +22,7 @@ module.exports = (app, bc, db, addresses, payments, emit) => {
let tx = bitcoin.Transaction.fromHex(message);
let hash = reverse(tx.getHash()).toString("hex");
+ console.log(hash);
if (payments.includes(hash)) return;
@@ -74,7 +75,7 @@ module.exports = (app, bc, db, addresses, payments, emit) => {
await user.save();
emit(user.username, "user", user);
- await db.Payment.create({
+ const payment = await db.Payment.create({
user_id: user.id,
hash,
amount: o.value,
@@ -86,6 +87,7 @@ module.exports = (app, bc, db, addresses, payments, emit) => {
address
});
+ emit(user.username, "payment", payment);
emit(user.username, "tx", message);
}
})
@@ -99,54 +101,41 @@ module.exports = (app, bc, db, addresses, payments, emit) => {
where: { confirmed: false }
});
- const addresses = payments.map(p => p.address);
+ const hashes = payments.map(p => p.hash);
let block = bitcoin.Block.fromHex(message.toString("hex"));
block.transactions.map(tx => {
- tx.outs.map(o => {
- try {
- let address = bitcoin.address.fromOutputScript(o.script, NETWORK);
- if (addresses.includes(address)) {
- queue[address] || (queue[address] = 0)
- queue[address] += o.value;
- }
- } catch (e) {
- return;
- }
- })
+ let hash = reverse(tx.getHash()).toString("hex");
+ if (hashes.includes(hash)) queue[hash] = 1;
});
});
- setInterval(() => {
- Object.keys(queue).map(async address => {
- l(address);
+ setInterval(async () => {
+ let arr = Object.keys(queue);
+ for (let i = 0; i < arr.length; i++) {
+ let hash = arr[i];
- let user = await db.User.findOne({
- where: {
- username: addresses[address],
- }
- });
+ let p = await db.Payment.findOne({
+ include: [{ model: db.User, as: "user" }],
+ where: { hash, confirmed: 0 }
+ })
+
+ p.confirmed = 1;
+
+ let user = await p.getUser();
+ user.balance += p.amount;
+ user.pending -= p.amount;
+ emit(user.username, "user", user);
- l(user.pending);
+ await user.save();
+ await p.save();
- const payments = await db.Payment.findAll({
- where: { confirmed: false, address }
+ let payments = await db.Payment.findAll({
+ where: { user_id: user.id }
});
- await Promise.all(payments.map(async p => {
- if (p.amount <= queue[address]) {
- l(user.pending, p.amount, queue[address]);
- queue[address] -= p.amount;
- if (queue[address] <= 0) delete queue[address];
- p.confirmed = 1;
- await p.save();
- user.balance += p.amount;
- user.pending -= p.amount;
- await user.save();
- emit(user.username, "user", user);
- emit(user.username, "block", 1);
- }
- }));
- });
+ emit(user.username, "payments", payments);
+ delete queue[hash];
+ }
}, 1000);
};