Parse inputs to BigNumber arithmetic in conversion util (#9502)

feature/default_network_editable
Erik Marks 4 years ago committed by GitHub
parent 436b01387c
commit 24d7d1ae2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      ui/app/helpers/utils/conversion-util.js

@ -150,13 +150,26 @@ const conversionUtil = (value, {
value: value || '0', value: value || '0',
}) })
const getBigNumber = (value, base) => {
// We don't include 'number' here, because BigNumber will throw if passed
// a number primitive it considers unsafe.
if (
typeof value === 'string' ||
value instanceof BigNumber
) {
return new BigNumber(value, base)
}
return new BigNumber(String(value), base)
}
const addCurrencies = (a, b, options = {}) => { const addCurrencies = (a, b, options = {}) => {
const { const {
aBase, aBase,
bBase, bBase,
...conversionOptions ...conversionOptions
} = options } = options
const value = (new BigNumber(a.toString(), aBase)).add(b.toString(), bBase) const value = getBigNumber(a, aBase).add(getBigNumber(b, bBase))
return converter({ return converter({
value, value,
@ -170,7 +183,8 @@ const subtractCurrencies = (a, b, options = {}) => {
bBase, bBase,
...conversionOptions ...conversionOptions
} = options } = options
const value = (new BigNumber(String(a), aBase)).minus(b, bBase) const value = getBigNumber(a, aBase)
.minus(getBigNumber(b, bBase))
return converter({ return converter({
value, value,
@ -185,10 +199,8 @@ const multiplyCurrencies = (a, b, options = {}) => {
...conversionOptions ...conversionOptions
} = options } = options
const bigNumberA = new BigNumber(String(a), multiplicandBase) const value = getBigNumber(a, multiplicandBase)
const bigNumberB = new BigNumber(String(b), multiplierBase) .times(getBigNumber(b, multiplierBase))
const value = bigNumberA.times(bigNumberB)
return converter({ return converter({
value, value,

Loading…
Cancel
Save