chore: update big number package

This commit is contained in:
José Oliveira 2021-06-15 22:34:36 +01:00 committed by Josh Harvey
parent 8aa18dd21c
commit ea44478b48
30 changed files with 186 additions and 144 deletions

View file

@ -11,7 +11,7 @@ function ticker (fiatCode, cryptoCode) {
return axios.get('https://bitpay.com/rates/' + cryptoCode + '/' + fiatCode)
.then(r => {
const data = r.data.data
const price = BN(data.rate.toString())
const price = new BN(data.rate.toString())
return {
rates: {
ask: price,

View file

@ -40,8 +40,8 @@ function getCurrencyRates (ticker, fiatCode, cryptoCode) {
return ticker.fetchTicker(symbol)
.then(res => ({
rates: {
ask: BN(res.ask),
bid: BN(res.bid)
ask: new BN(res.ask),
bid: new BN(res.bid)
}
}))
} catch (e) {
@ -52,7 +52,7 @@ function getCurrencyRates (ticker, fiatCode, cryptoCode) {
function findCurrencyRates (fxRates, fiatCode) {
const rates = _.find(_.matchesProperty('code', fiatCode), fxRates)
if (!rates || !rates.rate) throw new Error(`Unsupported currency: ${fiatCode}`)
return BN(rates.rate.toString())
return new BN(rates.rate.toString())
}
module.exports = { ticker }

View file

@ -3,8 +3,8 @@ const BN = require('../../bn')
function ticker (fiatCode, cryptoCode) {
return Promise.resolve({
rates: {
ask: BN(105),
bid: BN(100)
ask: new BN(105),
bid: new BN(100)
}
})
}

View file

@ -35,13 +35,13 @@ function checkCryptoCode (cryptoCode) {
function accountBalance (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) => BN(balance).shift(unitScale).round())
.then(({ balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
}
function accountUnconfirmedBalance (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) => BN(balance).shift(unitScale).round())
.then(({ unconfirmed_balance: balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
}
// We want a balance that includes all spends (0 conf) but only deposits that
@ -52,14 +52,14 @@ function balance (account, cryptoCode, settings, operatorId) {
function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [toAddress, coins]))
.then((txId) => fetch('gettransaction', [txId]))
.then((res) => _.pick(['fee', 'txid'], res))
.then((pickedObj) => {
return {
fee: BN(pickedObj.fee).abs().shift(unitScale).round(),
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid
}
})
@ -76,7 +76,7 @@ function newAddress (account, info, tx, settings, operatorId) {
function addressBalance (address, confs) {
return fetch('getreceivedbyaddress', [address, confs])
.then(r => BN(r).shift(unitScale).round())
.then(r => new BN(r).shiftedBy(unitScale).decimalPlaces(0))
}
function confirmedBalance (address, cryptoCode) {

View file

@ -35,13 +35,13 @@ function checkCryptoCode (cryptoCode) {
function accountBalance (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) => BN(balance).shift(unitScale).round())
.then(({ balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
}
function accountUnconfirmedBalance (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) => BN(balance).shift(unitScale).round())
.then(({ unconfirmed_balance: balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
}
// We want a balance that includes all spends (0 conf) but only deposits that
@ -52,7 +52,7 @@ function balance (account, cryptoCode, settings, operatorId) {
function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [toAddress, coins]))
@ -60,7 +60,7 @@ function sendCoins (account, tx, settings, operatorId) {
.then((res) => _.pick(['fee', 'txid'], res))
.then((pickedObj) => {
return {
fee: BN(pickedObj.fee).abs().shift(unitScale).round(),
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid
}
})
@ -77,7 +77,7 @@ function newAddress (account, info, tx, settings, operatorId) {
function addressBalance (address, confs) {
return fetch('getreceivedbyaddress', [address, confs])
.then(r => BN(r).shift(unitScale).round())
.then(r => new BN(r).shiftedBy(unitScale).decimalPlaces(0))
}
function confirmedBalance (address, cryptoCode) {

View file

@ -70,7 +70,7 @@ function sendCoins (account, tx, settings, operatorId) {
let fee = parseFloat(result.transfer.feeString)
let txid = result.transfer.txid
return { txid: txid, fee: BN(fee).round() }
return { txid: txid, fee: new BN(fee).decimalPlaces(0) }
})
.catch(err => {
if (err.message === 'insufficient funds') throw new E.InsufficientFundsError()
@ -81,7 +81,7 @@ function sendCoins (account, tx, settings, operatorId) {
function balance (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(() => getWallet(account, cryptoCode))
.then(wallet => BN(wallet._wallet.spendableBalanceString))
.then(wallet => new BN(wallet._wallet.spendableBalanceString))
}
function newAddress (account, info, tx, settings, operatorId) {
@ -120,8 +120,8 @@ function getStatus (account, tx, requested, settings, operatorId) {
it.type === 'receive'
)
const sum = _.reduce((acc, val) => val.add(acc), BN(0))
const toBn = _.map(it => BN(it.valueString))
const sum = _.reduce((acc, val) => val.plus(acc), new BN(0))
const toBn = _.map(it => new BN(it.valueString))
const confirmed = _.compose(sum, toBn, filterConfirmed)(transfers)
const pending = _.compose(sum, toBn, filterPending)(transfers)
@ -143,8 +143,8 @@ function newFunding (account, cryptoCode, settings, operatorId) {
const fundingAddress = result.address
return wallet.updateAddress({ address: fundingAddress, label: 'Funding Address' })
.then(() => ({
fundingPendingBalance: BN(wallet._wallet.balanceString),
fundingConfirmedBalance: BN(wallet._wallet.confirmedBalanceString),
fundingPendingBalance: new BN(wallet._wallet.balanceString),
fundingConfirmedBalance: new BN(wallet._wallet.confirmedBalanceString),
fundingAddress: getCashAddress(fundingAddress, cryptoCode)
}))
})

View file

@ -36,13 +36,13 @@ function checkCryptoCode (cryptoCode) {
function accountBalance (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) => BN(balance).shift(unitScale).round())
.then(({ balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
}
function accountUnconfirmedBalance (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) => BN(balance).shift(unitScale).round())
.then(({ unconfirmed_balance: balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
}
// We want a balance that includes all spends (0 conf) but only deposits that
@ -53,7 +53,7 @@ function balance (account, cryptoCode, settings, operatorId) {
function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [toAddress, coins]))
@ -61,7 +61,7 @@ function sendCoins (account, tx, settings, operatorId) {
.then((res) => _.pick(['fee', 'txid'], res))
.then((pickedObj) => {
return {
fee: BN(pickedObj.fee).abs().shift(unitScale).round(),
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid
}
})
@ -78,7 +78,7 @@ function newAddress (account, info, tx, settings, operatorId) {
function addressBalance (address, confs) {
return fetch('getreceivedbyaddress', [address, confs])
.then(r => BN(r).shift(unitScale).round())
.then(r => new BN(r).shiftedBy(unitScale).decimalPlaces(0))
}
function confirmedBalance (address, cryptoCode) {

View file

@ -38,7 +38,7 @@ function connect (url) {
}
}
const hex = bigNum => '0x' + bigNum.truncated().toString(16)
const hex = bigNum => '0x' + bigNum.integerValue(BN.ROUND_DOWN).toString(16)
function privateKey (account) {
return defaultWallet(account).getPrivateKey()
@ -57,7 +57,7 @@ function sendCoins (account, tx, settings, operatorId) {
.then(tx => {
if (!tx) return { txid }
const fee = BN(tx.gas).mul(BN(tx.gasPrice)).round()
const fee = new BN(tx.gas).times(new BN(tx.gasPrice)).decimalPlaces(0)
return { txid, fee }
})

View file

@ -35,13 +35,13 @@ function checkCryptoCode (cryptoCode) {
function accountBalance (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) => BN(balance).shift(unitScale).round())
.then(({ balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
}
function accountUnconfirmedBalance (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) => BN(balance).shift(unitScale).round())
.then(({ unconfirmed_balance: balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
}
// We want a balance that includes all spends (0 conf) but only deposits that
@ -52,7 +52,7 @@ function balance (account, cryptoCode, settings, operatorId) {
function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [toAddress, coins]))
@ -60,7 +60,7 @@ function sendCoins (account, tx, settings, operatorId) {
.then((res) => _.pick(['fee', 'txid'], res))
.then((pickedObj) => {
return {
fee: BN(pickedObj.fee).abs().shift(unitScale).round(),
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid
}
})
@ -77,7 +77,7 @@ function newAddress (account, info, tx, settings, operatorId) {
function addressBalance (address, confs) {
return fetch('getreceivedbyaddress', [address, confs])
.then(r => BN(r).shift(unitScale).round())
.then(r => new BN(r).shiftedBy(unitScale).decimalPlaces(0))
}
function confirmedBalance (address, cryptoCode) {

View file

@ -38,7 +38,7 @@ function balance (acount, cryptoCode, settings, operatorId) {
.then(c => c.channelBalance({}))
.then(_.get('balance'))
.then(BN)
.then(r => r.shift(unitScale).round())
.then(r => r.shiftedBy(unitScale).decimalPlaces(0))
}
function sendCoins (account, tx, settings, operatorId) {

View file

@ -14,7 +14,7 @@ let t0
function _balance (cryptoCode) {
const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode)
const unitScale = cryptoRec.unitScale
return BN(10).shift(unitScale).round()
return new BN(10).shiftedBy(unitScale).decimalPlaces(0)
}
function balance (account, cryptoCode, settings, operatorId) {
@ -24,7 +24,7 @@ function balance (account, cryptoCode, settings, operatorId) {
function pendingBalance (account, cryptoCode) {
return balance(account, cryptoCode)
.then(b => b.mul(1.1))
.then(b => b.times(1.1))
}
function confirmedBalance (account, cryptoCode) {
@ -36,7 +36,7 @@ let sendCount = 100
function isInsufficient (cryptoAtoms, cryptoCode) {
const b = _balance(cryptoCode)
return cryptoAtoms.gt(b.div(1000).mul(sendCount))
return cryptoAtoms.gt(b.div(1000).times(sendCount))
}
function sendCoins (account, tx, settings, operatorId) {
@ -52,7 +52,7 @@ function sendCoins (account, tx, settings, operatorId) {
console.log('[%s] DEBUG: Mock wallet sending %s cryptoAtoms to %s',
cryptoCode, cryptoAtoms.toString(), toAddress)
return resolve({ txid: '<txHash>', fee: BN(0) })
return resolve({ txid: '<txHash>', fee: new BN(0) })
}, 2000)
})
}
@ -81,7 +81,7 @@ function getStatus (account, tx, requested, settings, operatorId) {
const { toAddress, cryptoCode } = tx
const elapsed = Date.now() - t0
if (elapsed < PUBLISH_TIME) return Promise.resolve({ receivedCryptoAtoms: BN(0), status: 'notSeen' })
if (elapsed < PUBLISH_TIME) return Promise.resolve({ receivedCryptoAtoms: new BN(0), status: 'notSeen' })
if (elapsed < AUTHORIZE_TIME) return Promise.resolve({ receivedCryptoAtoms: requested, status: 'published' })
if (elapsed < CONFIRM_TIME) return Promise.resolve({ receivedCryptoAtoms: requested, status: 'authorized' })

View file

@ -30,7 +30,7 @@ function balance (account, cryptoCode, settings, operatorId) {
})
.then(({ data }) => {
if (data.error) throw new Error(JSON.stringify(data.error))
return BN(data.balance)
return new BN(data.balance)
})
}
@ -48,7 +48,7 @@ function sendCoins (account, tx, settings, operatorId) {
.then(({ data }) => {
if (data.error && data.error.errorCode === 'sc-001') throw new E.InsufficientFundsError()
else if (data.error) throw new Error(JSON.stringify(data.error))
const fee = BN(data.fee).round()
const fee = new BN(data.fee).decimalPlaces()
const txid = data.txid
return { txid, fee }
})
@ -74,8 +74,8 @@ function getStatus (account, tx, requested, settings, operatorId) {
.then(() => axios.get(`/balance/${tx.toAddress}?cryptoCode=${tx.cryptoCode}`))
.then(({ data }) => {
if (data.error) throw new Error(JSON.stringify(data.error))
const confirmed = BN(data.confirmedBalance)
const pending = BN(data.pendingBalance)
const confirmed = new BN(data.confirmedBalance)
const pending = new BN(data.pendingBalance)
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }

View file

@ -37,13 +37,13 @@ function checkCryptoCode (cryptoCode) {
function accountBalance (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) => BN(balance).shift(unitScale).round())
.then(({ balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
}
function accountUnconfirmedBalance (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) => BN(balance).shift(unitScale).round())
.then(({ unconfirmed_balance: balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
}
// We want a balance that includes all spends (0 conf) but only deposits that
@ -54,7 +54,7 @@ function balance (account, cryptoCode, settings, operatorId) {
function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
const checkSendStatus = function (opid) {
return new Promise((resolve, reject) => {
fetch('z_getoperationstatus', [[opid]])
@ -87,7 +87,7 @@ function sendCoins (account, tx, settings, operatorId) {
})
.then((pickedObj) => {
return {
fee: BN(pickedObj.fee).abs().shift(unitScale).round(),
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid
}
})
@ -104,7 +104,7 @@ function newAddress (account, info, tx, settings, operatorId) {
function addressBalance (address, confs) {
return fetch('getreceivedbyaddress', [address, confs])
.then(r => BN(r).shift(unitScale).round())
.then(r => new BN(r).shiftedBy(unitScale).decimalPlaces(0))
}
function confirmedBalance (address, cryptoCode) {