aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/token-util.js
diff options
context:
space:
mode:
authorChi Kei Chan <chikeichan@gmail.com>2018-01-20 07:29:36 +0800
committerChi Kei Chan <chikeichan@gmail.com>2018-01-20 07:29:36 +0800
commit77c545336b38aefc6105cde1799b18066df8bef9 (patch)
tree121a140131c8cb1f4b746d8f37d4a403b0910aad /ui/app/token-util.js
parent98b5a62fa74aa6730a25df28dfe5032cfb487697 (diff)
parent4e63924e607a07f94ff0a741a036ab352b0b7a3f (diff)
downloadtangerine-wallet-browser-77c545336b38aefc6105cde1799b18066df8bef9.tar
tangerine-wallet-browser-77c545336b38aefc6105cde1799b18066df8bef9.tar.gz
tangerine-wallet-browser-77c545336b38aefc6105cde1799b18066df8bef9.tar.bz2
tangerine-wallet-browser-77c545336b38aefc6105cde1799b18066df8bef9.tar.lz
tangerine-wallet-browser-77c545336b38aefc6105cde1799b18066df8bef9.tar.xz
tangerine-wallet-browser-77c545336b38aefc6105cde1799b18066df8bef9.tar.zst
tangerine-wallet-browser-77c545336b38aefc6105cde1799b18066df8bef9.zip
Merge branch 'uat' into newmaster
Diffstat (limited to 'ui/app/token-util.js')
-rw-r--r--ui/app/token-util.js45
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,
+}