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 | |
| parent | 720c020c86d885ac7bc3bc85898f192ff4711e59 (diff) | |
| download | coinos-server-7c77d82d6bb52662b98461c67b7240182ded8002.tar.gz coinos-server-7c77d82d6bb52662b98461c67b7240182ded8002.zip | |
better on-chain fee handling
| -rw-r--r-- | package.json | 2 | ||||
| -rw-r--r-- | sendCoins.js | 39 | ||||
| -rw-r--r-- | yarn.lock | 11 |
3 files changed, 25 insertions, 27 deletions
diff --git a/package.json b/package.json index ff857061..74801a53 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "axios": "^0.19.0", "bcrypt": "^3.0.6", "bip39": "^3.0.2", - "bitcoin-core": "^2.0.0", + "bitcoin-core": "^3.0.0", "bitcoinaverage": "^1.2.1", "bitcoinjs-lib": "^5.0.5", "bluebird": "^3.5.5", 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); @@ -1150,13 +1150,12 @@ bip66@^1.1.0, bip66@^1.1.5: dependencies: safe-buffer "^5.0.1" -bitcoin-core@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/bitcoin-core/-/bitcoin-core-2.0.0.tgz#defcd4b3bfb6738ea88051b5fe5a944e32c09df0" - integrity sha512-I6I7ejzbXfyMtR5/z0BhfjTeN8PMQGD+1B1rpu6IoLcwqoybaSa+P4zYnja/p2xdqjdctmgbOtAaCaaqyVCycQ== +bitcoin-core@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/bitcoin-core/-/bitcoin-core-3.0.0.tgz#3ad59a16b6748d8b60937affae6c5f1df2db770d" + integrity sha512-fdh8V/5lxDXwbq6KUCd9PjbTDe1kPGFM1brSFByuzMb+VSArr1qF422qbAJvW0dLQSaRhVDb7WC+a602QM70FQ== dependencies: "@uphold/request-logger" "^2.0.0" - bluebird "^3.4.1" debugnyan "^1.0.0" json-bigint "^0.2.0" lodash "^4.0.0" @@ -1252,7 +1251,7 @@ block-stream@*: dependencies: inherits "~2.0.0" -bluebird@^3.1.1, bluebird@^3.3.4, bluebird@^3.4.0, bluebird@^3.4.1, bluebird@^3.4.6, bluebird@^3.5.0, bluebird@^3.5.3, bluebird@^3.5.5: +bluebird@^3.1.1, bluebird@^3.3.4, bluebird@^3.4.0, bluebird@^3.4.6, bluebird@^3.5.0, bluebird@^3.5.3, bluebird@^3.5.5: version "3.5.5" resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.5.tgz#a8d0afd73251effbbd5fe384a77d73003c17a71f" integrity sha512-5am6HnnfN+urzt4yfg7IgTbotDjIT/u8AJpEt0sIU9FtXfVeezXAPKswrG+xKUCOYAINpSdgZVDU6QFh+cuH3w== |
