From c8adaabf852367675e3bb9afc2012127f01aae3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Fri, 21 May 2021 16:21:54 +0100 Subject: [PATCH] feat: add batch_id to cash_in_txs feat: bitcoind sendmany request feat: check if wallet supports transaction batching --- lib/plugins/wallet/bitcoind/bitcoind.js | 16 ++++++++++++++ lib/wallet.js | 5 +++++ .../1621556014244-add-btc-tx-batching.js | 22 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 migrations/1621556014244-add-btc-tx-batching.js diff --git a/lib/plugins/wallet/bitcoind/bitcoind.js b/lib/plugins/wallet/bitcoind/bitcoind.js index 7f9c802a..d374b0fd 100644 --- a/lib/plugins/wallet/bitcoind/bitcoind.js +++ b/lib/plugins/wallet/bitcoind/bitcoind.js @@ -79,6 +79,22 @@ function sendCoins (account, tx, settings, operatorId, feeMultiplier) { }) } +function sendCoinsBatch (account, txs, cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => { + const txAddressAmountPairs = _.map(tx => [tx.address, tx.cryptoAtoms.shift(-unitScale).toFixed(8)], txs) + return Promise.all([JSON.stringify(_.fromPairs(txAddressAmountPairs))]) + }) + .then(([obj]) => fetch('sendmany', ['', obj])) + .then(res => ({ + txid: res.txid + })) + .catch(err => { + if (err.code === -6) throw new E.InsufficientFundsError() + throw err + }) +} + function newAddress (account, info, tx, settings, operatorId) { return checkCryptoCode(info.cryptoCode) .then(() => fetch('getnewaddress')) diff --git a/lib/wallet.js b/lib/wallet.js index a6999ede..6ec32192 100644 --- a/lib/wallet.js +++ b/lib/wallet.js @@ -210,6 +210,11 @@ function isStrictAddress (settings, cryptoCode, toAddress) { }) } +function supportsBatching (settings, cryptoCode) { + return fetchWallet(settings, cryptoCode) + .then(r => _.isFunction(r.wallet.sendCoinsBatch)) +} + const coinFilter = ['ETH'] const balance = (settings, cryptoCode) => { diff --git a/migrations/1621556014244-add-btc-tx-batching.js b/migrations/1621556014244-add-btc-tx-batching.js new file mode 100644 index 00000000..4750b0fb --- /dev/null +++ b/migrations/1621556014244-add-btc-tx-batching.js @@ -0,0 +1,22 @@ +var db = require('./db') + +exports.up = function (next) { + var sql = [ + `CREATE TYPE transaction_batch_status AS ENUM('open', 'failed', 'sent')`, + `CREATE TABLE transaction_batches ( + id UUID PRIMARY KEY, + crypto_code TEXT NOT NULL, + status transaction_batch_status NOT NULL DEFAULT 'open', + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + closed_at TIMESTAMPTZ, + error_message TEXT + )`, + `ALTER TABLE cash_in_txs ADD COLUMN batch_id REFERENCES transaction_batches(id)` + ] + + db.multi(sql, next) +} + +exports.down = function (next) { + next() +}