diff options
Diffstat (limited to 'ui/app/conversion-util.js')
-rw-r--r-- | ui/app/conversion-util.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/ui/app/conversion-util.js b/ui/app/conversion-util.js new file mode 100644 index 000000000..8f2214500 --- /dev/null +++ b/ui/app/conversion-util.js @@ -0,0 +1,50 @@ +const { + numericBalance, + parseBalance, + formatBalance, + normalizeToWei, + valueTable, +} = require('./util') +const hexToBn = require('../../app/scripts/lib/hex-to-bn') +const { BN } = require('ethereumjs-util') +const GWEI_MULTIPLIER = normalizeToWei(hexToBn(valueTable.gwei.toString(16)), 'gwei'); + +const conversionUtil = (value, { + fromCurrency, + toCurrency, + fromFormat, + toFormat, + precision = 2, + conversionRate, +}) => { + let result; + + if (fromFormat === 'BN') { + if (fromCurrency !== 'GWEI') { + result = normalizeToWei(value, 'gwei') + } + else { + result = value + } + + result = result.toString(16) + result = formatBalance(result, 9) + result = result.split(' ') + result = Number(result[0]) * 1000000000 + } + + if (fromCurrency === 'GWEI') { + result = result / 1000000000 + } + + if (toCurrency === 'USD') { + result = result * conversionRate + result = result.toFixed(precision) + } + + return result +}; + +module.exports = { + conversionUtil, +}
\ No newline at end of file |