diff options
| author | Adam Soltys <[email protected]> | 2019-09-09 23:27:57 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2019-09-09 23:27:57 -0700 |
| commit | e2658f928b71c825f044a6a3808349fec5e48560 (patch) | |
| tree | a882826c5b9ac8d0df8179b57fa64e82f0a15eef | |
| parent | 6442678745c7d1c3e89e6ce277c73dc5a6cab348 (diff) | |
| download | coinos-server-e2658f928b71c825f044a6a3808349fec5e48560.tar.gz coinos-server-e2658f928b71c825f044a6a3808349fec5e48560.zip | |
clear out pending payments properly
| -rw-r--r-- | index.js | 2 | ||||
| -rw-r--r-- | sockets.js | 1 | ||||
| -rw-r--r-- | zmq.js | 25 |
3 files changed, 17 insertions, 11 deletions
@@ -102,7 +102,7 @@ const authy = new Client({ key: config.authy.key }); attributes: ["hash"] })).map(p => p.hash); - require("./zmq")(bc, db, addresses, payments); + require("./zmq")(app, bc, db, addresses, payments, emit); app.post("/register", async (req, res) => { let err = m => res.status(500).send(m); @@ -1,5 +1,6 @@ import jwt from "jsonwebtoken"; import io from "socket.io"; +import whitelist from "./whitelist"; /* eslint-disable-next-line */ const pick = (O, ...K) => K.reduce((o, k) => ((o[k] = O[k]), o), {}); @@ -1,6 +1,7 @@ import reverse from "buffer-reverse"; import zmq from "zeromq"; import config from "./config"; +import Sequelize from "sequelize"; const bitcoin = require("bitcoinjs-lib"); const l = console.log; @@ -13,7 +14,7 @@ const zmqRawTx = zmq.socket("sub"); zmqRawTx.connect(config.bitcoin.zmqrawtx); zmqRawTx.subscribe("rawtx"); -module.exports = (bc, db, addresses, payments) => { +module.exports = (app, bc, db, addresses, payments, emit) => { zmqRawTx.on("message", (topic, message, sequence) => { message = message.toString("hex"); @@ -36,10 +37,8 @@ module.exports = (bc, db, addresses, payments) => { bitcoin.networks[network] ); } catch (e) { - l(e); return; } - l(o); if (Object.keys(addresses).includes(address)) { payments.push(hash); @@ -88,6 +87,7 @@ module.exports = (bc, db, addresses, payments) => { confirmed }); + l("tx", message); emit(user.username, "tx", message); } }) @@ -103,22 +103,27 @@ module.exports = (bc, db, addresses, payments) => { let block = bitcoin.Block.fromHex(message); l("block", block.getHash().toString("hex")); - await db.Payment.findAll({ - include: { model: db.User, as: "user" }, - where: { confirmed: false } - }).map(async p => { + const processPayment = async p => { if ((await bc.getRawTransaction(p.hash, true)).confirmations > 0) { - let user = p.user; + let user = await db.User.findByPk(p.user_id); + l(user.pending); p.confirmed = true; user.balance += p.amount; user.pending -= p.amount; await user.save(); await p.save(); emit(user.username, "user", user); + emit(user.username, "block", message); } - }); + }; + + await db.Payment.findAll({ + where: { confirmed: false } + }).reduce(async (prev, payment) => { + const result = await prev; + return [...result, await processPayment(payment)]; + }, Promise.resolve([])); - socket.emit("block", message); break; } } |
