diff options
author | Alexander Tseung <alextsg@gmail.com> | 2018-01-17 10:17:28 +0800 |
---|---|---|
committer | Alexander Tseung <alextsg@gmail.com> | 2018-01-17 10:17:28 +0800 |
commit | 81f86cfab6da37f1a87f3243c6a082f8835ab81b (patch) | |
tree | 6af56231142544c5983bbe24d74644011596d003 /ui/app/token-util.js | |
parent | aa76c5c73c5a9f0507ef69d88a43cf3bee5d60c0 (diff) | |
parent | 77eb7b2db692cc40bf5f8e36c5e695e8f82c76ec (diff) | |
download | tangerine-wallet-browser-81f86cfab6da37f1a87f3243c6a082f8835ab81b.tar tangerine-wallet-browser-81f86cfab6da37f1a87f3243c6a082f8835ab81b.tar.gz tangerine-wallet-browser-81f86cfab6da37f1a87f3243c6a082f8835ab81b.tar.bz2 tangerine-wallet-browser-81f86cfab6da37f1a87f3243c6a082f8835ab81b.tar.lz tangerine-wallet-browser-81f86cfab6da37f1a87f3243c6a082f8835ab81b.tar.xz tangerine-wallet-browser-81f86cfab6da37f1a87f3243c6a082f8835ab81b.tar.zst tangerine-wallet-browser-81f86cfab6da37f1a87f3243c6a082f8835ab81b.zip |
Merge branch 'uat' into uat-master-011618
Diffstat (limited to 'ui/app/token-util.js')
-rw-r--r-- | ui/app/token-util.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/ui/app/token-util.js b/ui/app/token-util.js new file mode 100644 index 000000000..f84051ef5 --- /dev/null +++ b/ui/app/token-util.js @@ -0,0 +1,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, +} |