diff options
| author | Adam Soltys <[email protected]> | 2019-12-31 22:12:26 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2019-12-31 22:12:29 -0800 |
| commit | 3b841f05921311cf149b7987d76738fcb053a52e (patch) | |
| tree | 7314c78627dcacc1a5d54066b834005755bb9631 /sendCoins.js | |
| parent | ae5e489e8a21293ae3f4d9a3e1940a62ddd01011 (diff) | |
| download | coinos-server-3b841f05921311cf149b7987d76738fcb053a52e.tar.gz coinos-server-3b841f05921311cf149b7987d76738fcb053a52e.zip | |
better payment detection logic
Diffstat (limited to 'sendCoins.js')
| -rw-r--r-- | sendCoins.js | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/sendCoins.js b/sendCoins.js index 63f50355..67c545b9 100644 --- a/sendCoins.js +++ b/sendCoins.js @@ -7,20 +7,28 @@ const SATS = 100000000; module.exports = (app, bc, db, emit) => async (req, res) => { let { address, amount } = req.body; - amount = parseInt(amount); + let rawtex, hex, fee; - let rawtx = await bc.createRawTransaction([], { - [address]: (amount / SATS).toFixed(8) - }); - rawtx = await bc.fundRawTransaction(rawtx, { - subtractFeeFromOutputs: amount === req.user.balance ? [0] : [] - }); - let { hex, fee } = rawtx; - fee = parseInt(fee * SATS); + try { + amount = parseInt(amount); - l("sending coins", req.user.username, amount, address); + rawtx = await bc.createRawTransaction([], { + [address]: (amount / SATS).toFixed(8) + }); + + rawtx = await bc.fundRawTransaction(rawtx, { + subtractFeeFromOutputs: amount === req.user.balance ? [0] : [] + }); + + ({ hex, fee } = rawtx); + fee = parseInt(fee * SATS); + } catch (e) { + l(e); + return res.status(500).send(e.message); + } try { + l("sending coins", req.user.username, amount, address); await db.transaction(async transaction => { let { balance } = await db.User.findOne({ where: { @@ -39,6 +47,7 @@ module.exports = (app, bc, db, emit) => async (req, res) => { await req.user.save({ transaction }); }); } catch (e) { + l(e); return res.status(500).send("Not enough satoshis"); } @@ -64,7 +73,8 @@ module.exports = (app, bc, db, emit) => async (req, res) => { user_id: req.user.id, hash: txid, rate: app.get("ask"), - currency: "CAD" + currency: "CAD", + address }, { transaction } ); @@ -73,6 +83,6 @@ module.exports = (app, bc, db, emit) => async (req, res) => { res.send({ txid, tx, amount, fees: fee }); } catch (e) { l(e); - res.status(500).send(e.message); + return res.status(500).send(e.message); } }; |
