aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/token-util.js
blob: f84051ef5e43121db7e68b4340cbcff2d6fe2c0f (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
const abi = require('human-standard-token-abi')
const Eth = require('ethjs-query')
const EthContract = require('ethjs-contract')

const tokenInfoGetter = function () {
  if (typeof global.ethereumProvider === 'undefined') return

  const eth = new Eth(global.ethereumProvider)
  const contract = new EthContract(eth)
  const TokenContract = contract(abi)

  const tokens = {}

  return async (address) => {
    if (tokens[address]) {
      return tokens[address]
    }

    const contract = TokenContract.at(address)

    const result = await Promise.all([
      contract.symbol(),
      contract.decimals(),
    ])

    const [ symbol = [], decimals = [] ] = result

    tokens[address] = { symbol: symbol[0], decimals: decimals[0] }

    return tokens[address]
  }
}

function calcTokenAmount (value, decimals) {
  const multiplier = Math.pow(10, Number(decimals || 0))
  const amount = Number(value / multiplier)

  return amount
}


module.exports = {
  tokenInfoGetter,
  calcTokenAmount,
}