diff options
| author | Adam Soltys <[email protected]> | 2020-04-28 22:09:22 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-04-28 22:33:02 -0700 |
| commit | 63c18105e6c66b3b4698c398f356bef1b496816f (patch) | |
| tree | e7ee85e7c3502ed81954448a76be028143990948 /routes/bitcoin | |
| parent | 994dd93f6c45c590ae59a699cbf425d96518da1e (diff) | |
| download | coinos-server-63c18105e6c66b3b4698c398f356bef1b496816f.tar.gz coinos-server-63c18105e6c66b3b4698c398f356bef1b496816f.zip | |
include account in emitted payments objects
Diffstat (limited to 'routes/bitcoin')
| -rw-r--r-- | routes/bitcoin/receive.js | 11 | ||||
| -rw-r--r-- | routes/bitcoin/send.js | 82 |
2 files changed, 45 insertions, 48 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index acc80b09..deded625 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -70,12 +70,10 @@ zmqRawTx.on("message", async (topic, message, sequence) => { user.address = await bc.getNewAddress("", "bech32"); await user.save(); - user = await getUser(user.username); - emit(user.username, "user", user); addresses[user.address] = user.username; - const payment = await db.Payment.create({ + let payment = await db.Payment.create({ account_id: account.id, user_id: user.id, hash, @@ -88,9 +86,13 @@ zmqRawTx.on("message", async (topic, message, sequence) => { address, network: "BTC" }); + payment = payment.get({ plain: true }); + payment.account = account.get({ plain: true }); - l.info("bitcoin detected", user.username, o.value); + user = await getUser(user.username); emit(user.username, "payment", payment); + emit(user.username, "user", user); + l.info("bitcoin detected", user.username, o.value); } }) ); @@ -135,6 +137,7 @@ setInterval(async () => { let user = await getUserById(p.user_id); emit(user.username, "user", user); + emit(user.username, "payment", p); l.info("bitcoin confirmed", user.username, p.amount, p.tip); delete queue[hash]; } else { diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index e7cba943..42027f71 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -8,10 +8,9 @@ module.exports = async (req, res) => { let fee = toSats(tx.fee); - const isChange = async address => - (await bc.getAddressInfo(address)).ismine && ( - !Object.keys(addresses).includes(address) || - address === user.address); + const isChange = async address => + (await bc.getAddressInfo(address)).ismine && + (!Object.keys(addresses).includes(address) || address === user.address); tx = await bc.decodeRawTransaction(hex); @@ -21,40 +20,55 @@ module.exports = async (req, res) => { for (let i = 0; i < tx.vout.length; i++) { let o = tx.vout[i]; total += toSats(o.value); - + if (o.scriptPubKey.addresses) { - if ((await isChange(o.scriptPubKey.addresses[0]))) { + if (await isChange(o.scriptPubKey.addresses[0])) { change += toSats(o.value); } } - } + } - total = (total - change) + fee; + total = total - change + fee; let amount = total - fee; - l.info("attempting bitcoin payment", user.username, amount, fee); + l.info("attempting bitcoin payment", user.username, total, change, amount, fee); + let account, params; try { await db.transaction(async transaction => { - let { balance } = await db.User.findOne({ + account = await db.Account.findOne({ where: { - username: user.username + user_id: user.id, + asset: config.liquid.btcasset }, lock: transaction.LOCK.UPDATE, transaction }); - if (amount !== balance && total > balance) { + if (total > account.balance) { l.error("amount exceeds balance", amount, fee, balance); throw new Error("insufficient funds"); } - user.balance -= total; - await user.save({ transaction }); + account.balance -= total; + await account.save({ transaction }); + + params = { + amount: -total, + fee, + account_id: account.id, + user_id: user.id, + rate: app.get("rates")[user.currency], + currency: user.currency, + address, + confirmed: true, + received: false, + network: "BTC" + }; }); } catch (e) { - l.warn("insufficient funds for bitcoin payment", user.username); - return res.status(500).send("Not enough satoshis"); + l.error("problem creating bitcoin payment", user.username, e.message); + return res.status(500).send(e.message); } try { @@ -62,37 +76,17 @@ module.exports = async (req, res) => { await bc.walletPassphrase(config.bitcoin.walletpass, 300); hex = (await bc.signRawTransactionWithWallet(hex)).hex; - let txid = await bc.sendRawTransaction(hex); + params.hash = await bc.sendRawTransaction(hex); - await db.transaction(async transaction => { - await user.save({ transaction }); - user = await getUser(user.username); + let payment = await db.Payment.create(params); - const payment = await db.Payment.create( - { - amount: -total, - fee, - account_id: user.account.id, - user_id: user.id, - hash: txid, - rate: app.get("rates")[user.currency], - currency: user.currency, - address, - confirmed: true, - received: false, - network: "BTC" - }, - { transaction } - ); - - user = await getUser(user.username); - emit(user.username, "payment", payment); - emit(user.username, "user", user); + user = await getUser(user.username); + emit(user.username, "user", user); - res.send(payment); - - l.info("sent bitcoin", user.username, total); - }); + payment = payment.get({ plain: true }); + payment.account = account.get({ plain: true }); + res.send(payment); + l.info("sent bitcoin", user.username, total); } catch (e) { l.error("error sending bitcoin", e.message); return res.status(500).send(e.message); |
