diff options
| author | Adam Soltys <[email protected]> | 2019-10-12 22:09:59 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2019-10-12 22:09:59 -0700 |
| commit | 7c77d82d6bb52662b98461c67b7240182ded8002 (patch) | |
| tree | c43d258563384d9f5e5786182b6b8a07b97272be /sendCoins.js | |
| parent | 720c020c86d885ac7bc3bc85898f192ff4711e59 (diff) | |
| download | coinos-server-7c77d82d6bb52662b98461c67b7240182ded8002.tar.gz coinos-server-7c77d82d6bb52662b98461c67b7240182ded8002.zip | |
better on-chain fee handling
Diffstat (limited to 'sendCoins.js')
| -rw-r--r-- | sendCoins.js | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/sendCoins.js b/sendCoins.js index 31d12c26..7eebdc8a 100644 --- a/sendCoins.js +++ b/sendCoins.js @@ -3,17 +3,24 @@ const config = require("./config"); const reverse = require("buffer-reverse"); const l = console.log; +const SATS = 100000000; module.exports = (app, bc, db, emit) => async (req, res) => { - const MINFEE = 3000; - let { address, amount } = req.body; + amount = parseInt(amount); - l("sending coins", req.user.username, amount, address); + 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); - if (amount === req.user.balance) { - amount = req.user.balance - MINFEE; - } + rawtx = (await bc.signRawTransactionWithWallet(hex)).hex; + + l("sending coins", req.user.username, amount, address); try { await db.transaction(async transaction => { @@ -26,12 +33,12 @@ module.exports = (app, bc, db, emit) => async (req, res) => { { transaction } ); - if (amount > balance) { - l("amount exceeds balance", amount, balance); + if (amount !== balance && amount + fee > balance) { + l("amount exceeds balance", amount, fee, balance); throw new Error("insufficient funds"); } - req.user.balance -= parseInt(amount) + 10000; + req.user.balance -= amount + fee; await req.user.save({ transaction }); }); } catch (e) { @@ -42,20 +49,12 @@ module.exports = (app, bc, db, emit) => async (req, res) => { if (config.bitcoin.walletpass) await bc.walletPassphrase(config.bitcoin.walletpass, 300); - let txid = await bc.sendToAddress(address, (amount / 100000000).toFixed(8)); + let txid = await bc.sendRawTransaction(rawtx); let txhex = await bc.getRawTransaction(txid); let tx = bitcoin.Transaction.fromHex(txhex); - let inputTotal = await tx.ins.reduce(async (a, input) => { - let h = await bc.getRawTransaction(reverse(input.hash).toString("hex")); - return a + bitcoin.Transaction.fromHex(h).outs[input.index].value; - }, 0); - let outputTotal = tx.outs.reduce((a, b) => a + b.value, 0); - - let fees = inputTotal - outputTotal; - let total = Math.min(parseInt(amount) + fees, req.user.balance); - req.user.balance += 10000 - fees; + let total = Math.min(amount + fee, req.user.balance); if (req.user.balance < 0) req.user.balance = 0; await db.transaction(async transaction => { @@ -74,7 +73,7 @@ module.exports = (app, bc, db, emit) => async (req, res) => { ); }); - res.send({ txid, tx, amount, fees }); + res.send({ txid, tx, amount, fees: fee }); } catch (e) { l(e); res.status(500).send(e.message); |
