aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/conversion-util.js
blob: 8f221450074ac26efb794ed67391c154158ddddf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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,
}