diff --git a/lib/plugins/wallet/bitgo/bitgo.js b/lib/plugins/wallet/bitgo/bitgo.js index e41489b4..5dab08df 100644 --- a/lib/plugins/wallet/bitgo/bitgo.js +++ b/lib/plugins/wallet/bitgo/bitgo.js @@ -1,3 +1,5 @@ +const _ = require('lodash/fp') + const BitGo = require('bitgo') const { toLegacyAddress, toCashAddress } = require('bchaddrjs') @@ -52,7 +54,8 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) { const params = { address: getLegacyAddress(address, cryptoCode), amount: cryptoAtoms.toNumber(), - walletPassphrase: account[`${cryptoCode}WalletPassphrase`] + walletPassphrase: account[`${cryptoCode}WalletPassphrase`], + enforceMinConfirmsForChange: false } return wallet.send(params) }) @@ -94,14 +97,28 @@ function newAddress (account, info) { } function getStatus (account, toAddress, requested, cryptoCode) { - const bitgo = buildBitgo(account) return checkCryptoCode(cryptoCode) - .then(() => bitgo.blockchain().getAddress({ address: getLegacyAddress(toAddress, cryptoCode) })) - .then(rec => { - if (rec.balance === 0) return { status: 'notSeen' } - if (requested.gt(rec.balance)) return { status: 'insufficientFunds' } - if (requested.gt(rec.confirmedBalance)) return { status: 'authorized' } - return { status: 'confirmed' } + .then(() => getWallet(account, cryptoCode)) + .then(wallet => wallet.transfers({ type: 'receive', address: toAddress })) + .then(({ transfers }) => { + const filterConfirmed = _.filter(it => + it.state === 'confirmed' && it.type === 'receive' + ) + const filterPending = _.filter(it => + (it.state === 'confirmed' || it.state === 'unconfirmed') && + it.type === 'receive' + ) + + const sum = _.reduce((acc, val) => val.add(acc), BN(0)) + const toBn = _.map(it => BN(it.valueString)) + + const confirmed = _.compose(sum, toBn, filterConfirmed)(transfers) + const pending = _.compose(sum, toBn, filterPending)(transfers) + + if (confirmed.gte(requested)) return { status: 'confirmed' } + if (pending.gte(requested)) return { status: 'authorized' } + if (pending.gt(0)) return { status: 'insufficientFunds' } + return { status: 'notSeen' } }) } diff --git a/package.json b/package.json index 9fb71cb5..761f32de 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "bitcoind-rpc": "^0.7.0", "bitcore-lib": "^0.15.0", "bitcore-lib-cash": "git+https://github.com/bitpay/bitcore-lib.git#cash", - "bitgo": "4.40.2", + "bitgo": "4.47.0", "body-parser": "^1.15.1", "coinbase": "^2.0.6", "console-log-level": "^1.4.0",