diff --git a/lib/plugins.js b/lib/plugins.js index 7f0ed759..89c6a018 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -238,6 +238,7 @@ function plugins (settings, deviceId) { const pingPromise = recordPing(deviceTime, machineVersion, machineModel) const currentConfigVersionPromise = fetchCurrentConfigVersion() const currentAvailablePromoCodes = loyalty.getNumberOfAvailablePromoCodes() + const supportsBatchingPromise = cryptoCodes.map(c => wallet.supportsBatching(settings, c)) const timezoneObj = { utcOffset: timezone[0], dstOffset: timezone[1] } const promises = [ @@ -246,6 +247,7 @@ function plugins (settings, deviceId) { currentConfigVersionPromise, timezoneObj ].concat( + supportsBatchingPromise, tickerPromises, balancePromises, testnetPromises, @@ -258,9 +260,10 @@ function plugins (settings, deviceId) { const configVersion = arr[2] const tz = arr[3] const cryptoCodesCount = cryptoCodes.length - const tickers = arr.slice(4, cryptoCodesCount + 4) - const balances = arr.slice(cryptoCodesCount + 4, 2 * cryptoCodesCount + 4) - const testNets = arr.slice(2 * cryptoCodesCount + 4, arr.length - 2) + const batchableCoins = arr.slice(4, cryptoCodesCount + 4) + const tickers = arr.slice(cryptoCodesCount + 4, 2 * cryptoCodesCount + 4) + const balances = arr.slice(2 * cryptoCodesCount + 4, 3 * cryptoCodesCount + 4) + const testNets = arr.slice(3 * cryptoCodesCount + 4, arr.length - 1) const coinParams = _.zip(cryptoCodes, testNets) const coinsWithoutRate = _.map(mapCoinSettings, coinParams) const areThereAvailablePromoCodes = arr[arr.length - 1] > 0 @@ -278,16 +281,19 @@ function plugins (settings, deviceId) { } function sendCoins (tx) { - if (wallet.supportsBatching(settings, tx.cryptoCode)) { - return transactionBatching.addTransactionToBatch(tx) - .then(() => ({ - batched: true, - sendPending: false, - error: null, - errorCode: null - })) - } - return wallet.sendCoins(settings, tx) + return wallet.supportsBatching(settings, tx.cryptoCode) + .then(supportsBatching => { + if (supportsBatching) { + return transactionBatching.addTransactionToBatch(tx) + .then(() => ({ + batched: true, + sendPending: false, + error: null, + errorCode: null + })) + } + return wallet.sendCoins(settings, tx) + }) } function recordPing (deviceTime, version, model) { diff --git a/lib/plugins/wallet/bitcoincashd/bitcoincashd.js b/lib/plugins/wallet/bitcoincashd/bitcoincashd.js index 3d08156c..14d628ce 100644 --- a/lib/plugins/wallet/bitcoincashd/bitcoincashd.js +++ b/lib/plugins/wallet/bitcoincashd/bitcoincashd.js @@ -129,11 +129,17 @@ function cryptoNetwork (account, cryptoCode, settings, operatorId) { .then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main') } +function supportsBatching (account, cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => _.isFunction(sendCoinsBatch)) +} + module.exports = { balance, sendCoins, newAddress, getStatus, newFunding, - cryptoNetwork + cryptoNetwork, + supportsBatching } diff --git a/lib/plugins/wallet/bitcoind/bitcoind.js b/lib/plugins/wallet/bitcoind/bitcoind.js index 516e7f39..7ccb88ea 100644 --- a/lib/plugins/wallet/bitcoind/bitcoind.js +++ b/lib/plugins/wallet/bitcoind/bitcoind.js @@ -170,6 +170,11 @@ function fetchRBF (txId) { }) } +function supportsBatching (account, cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => _.isFunction(sendCoinsBatch)) +} + module.exports = { balance, sendCoins, @@ -179,5 +184,6 @@ module.exports = { cryptoNetwork, fetchRBF, estimateFee, - sendCoinsBatch + sendCoinsBatch, + supportsBatching } diff --git a/lib/plugins/wallet/bitgo/bitgo.js b/lib/plugins/wallet/bitgo/bitgo.js index 448e2f84..02abaaa6 100644 --- a/lib/plugins/wallet/bitgo/bitgo.js +++ b/lib/plugins/wallet/bitgo/bitgo.js @@ -157,6 +157,11 @@ function cryptoNetwork (account, cryptoCode, settings, operatorId) { .then(() => account.environment === 'test' ? 'test' : 'main') } +function supportsBatching (account, cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => _.isFunction(sendCoinsBatch)) +} + module.exports = { NAME, balance, @@ -164,5 +169,6 @@ module.exports = { newAddress, getStatus, newFunding, - cryptoNetwork + cryptoNetwork, + supportsBatching } diff --git a/lib/plugins/wallet/dashd/dashd.js b/lib/plugins/wallet/dashd/dashd.js index db356210..78fa15c1 100644 --- a/lib/plugins/wallet/dashd/dashd.js +++ b/lib/plugins/wallet/dashd/dashd.js @@ -112,10 +112,16 @@ function newFunding (account, cryptoCode, settings, operatorId) { })) } +function supportsBatching (account, cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => _.isFunction(sendCoinsBatch)) +} + module.exports = { balance, sendCoins, newAddress, getStatus, - newFunding + newFunding, + supportsBatching } diff --git a/lib/plugins/wallet/geth/base.js b/lib/plugins/wallet/geth/base.js index 8b4cdab0..0f5329fc 100644 --- a/lib/plugins/wallet/geth/base.js +++ b/lib/plugins/wallet/geth/base.js @@ -29,7 +29,8 @@ module.exports = { newFunding, privateKey, isStrictAddress, - connect + connect, + supportsBatching } function connect (url) { @@ -222,3 +223,8 @@ function newFunding (account, cryptoCode, settings, operatorId) { })) }) } + +function supportsBatching (account, cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => _.isFunction(sendCoinsBatch)) +} diff --git a/lib/plugins/wallet/litecoind/litecoind.js b/lib/plugins/wallet/litecoind/litecoind.js index 38373e3b..25d744d0 100644 --- a/lib/plugins/wallet/litecoind/litecoind.js +++ b/lib/plugins/wallet/litecoind/litecoind.js @@ -112,10 +112,16 @@ function newFunding (account, cryptoCode, settings, operatorId) { })) } +function supportsBatching (account, cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => _.isFunction(sendCoinsBatch)) +} + module.exports = { balance, sendCoins, newAddress, getStatus, - newFunding + newFunding, + supportsBatching } diff --git a/lib/plugins/wallet/mock-wallet/mock-wallet.js b/lib/plugins/wallet/mock-wallet/mock-wallet.js index 4a945869..31fa8f40 100644 --- a/lib/plugins/wallet/mock-wallet/mock-wallet.js +++ b/lib/plugins/wallet/mock-wallet/mock-wallet.js @@ -3,8 +3,10 @@ const _ = require('lodash/fp') const BN = require('../../../bn') const E = require('../../../error') const { utils: coinUtils } = require('lamassu-coins') +const consoleLogLevel = require('console-log-level') const NAME = 'FakeWallet' +const BATCHABLE_COINS = ['BTC'] const SECONDS = 1000 const PUBLISH_TIME = 3 * SECONDS @@ -59,9 +61,6 @@ function sendCoins (account, tx, settings, operatorId) { }) } -<<<<<<< HEAD -function newAddress (account, info, tx, settings, operatorId) { -======= function sendCoinsBatch (account, txs, cryptoCode) { sendCount = sendCount + txs.length return new Promise((resolve, reject) => { @@ -81,7 +80,6 @@ function sendCoinsBatch (account, txs, cryptoCode) { } function newAddress () { ->>>>>>> feat: add transaction batching module t0 = Date.now() return Promise.resolve('') } @@ -114,6 +112,10 @@ function getStatus (account, tx, requested, settings, operatorId) { return Promise.resolve({status: 'confirmed'}) } +function supportsBatching (account, cryptoCode) { + return Promise.resolve(_.includes(cryptoCode, BATCHABLE_COINS)) +} + module.exports = { NAME, balance, @@ -121,5 +123,6 @@ module.exports = { sendCoins, newAddress, getStatus, - newFunding + newFunding, + supportsBatching } diff --git a/lib/plugins/wallet/zcashd/zcashd.js b/lib/plugins/wallet/zcashd/zcashd.js index d35099a9..b564449f 100644 --- a/lib/plugins/wallet/zcashd/zcashd.js +++ b/lib/plugins/wallet/zcashd/zcashd.js @@ -138,10 +138,16 @@ function newFunding (account, cryptoCode, settings, operatorId) { })) } +function supportsBatching (account, cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => _.isFunction(sendCoinsBatch)) +} + module.exports = { balance, sendCoins, newAddress, getStatus, - newFunding + newFunding, + supportsBatching } diff --git a/lib/wallet.js b/lib/wallet.js index d1d3db82..a9fb15a6 100644 --- a/lib/wallet.js +++ b/lib/wallet.js @@ -230,7 +230,7 @@ function isStrictAddress (settings, cryptoCode, toAddress) { function supportsBatching (settings, cryptoCode) { return fetchWallet(settings, cryptoCode) - .then(r => _.isFunction(r.wallet.sendCoinsBatch)) + .then(r => r.wallet.supportsBatching(settings, cryptoCode)) } const coinFilter = ['ETH']