aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/token-util.js
diff options
context:
space:
mode:
authorThomas <thomas.b.huang@gmail.com>2018-05-17 13:44:51 +0800
committerThomas <thomas.b.huang@gmail.com>2018-05-17 13:44:51 +0800
commitd9be7f989a86e3fdfd83e4c632fd08cefd8309e5 (patch)
treebbfeb7098997244ce7e8ce28e896faa2dfb6bb6e /ui/app/token-util.js
parent06e25205b200b976e286c670cc5e703439dab05c (diff)
parentf441153211c7920573f8bfb699bfda1b6de7efe9 (diff)
downloadtangerine-wallet-browser-d9be7f989a86e3fdfd83e4c632fd08cefd8309e5.tar
tangerine-wallet-browser-d9be7f989a86e3fdfd83e4c632fd08cefd8309e5.tar.gz
tangerine-wallet-browser-d9be7f989a86e3fdfd83e4c632fd08cefd8309e5.tar.bz2
tangerine-wallet-browser-d9be7f989a86e3fdfd83e4c632fd08cefd8309e5.tar.lz
tangerine-wallet-browser-d9be7f989a86e3fdfd83e4c632fd08cefd8309e5.tar.xz
tangerine-wallet-browser-d9be7f989a86e3fdfd83e4c632fd08cefd8309e5.tar.zst
tangerine-wallet-browser-d9be7f989a86e3fdfd83e4c632fd08cefd8309e5.zip
Merge branch 'testing' of https://github.com/tmashuang/metamask-extension into testing
Diffstat (limited to 'ui/app/token-util.js')
-rw-r--r--ui/app/token-util.js46
1 files changed, 28 insertions, 18 deletions
diff --git a/ui/app/token-util.js b/ui/app/token-util.js
index f84051ef5..920442bfc 100644
--- a/ui/app/token-util.js
+++ b/ui/app/token-util.js
@@ -1,14 +1,6 @@
-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 util = require('./util')
+function tokenInfoGetter () {
const tokens = {}
return async (address) => {
@@ -16,18 +8,35 @@ const tokenInfoGetter = function () {
return tokens[address]
}
- const contract = TokenContract.at(address)
+ tokens[address] = await getSymbolAndDecimals(address)
- const result = await Promise.all([
- contract.symbol(),
- contract.decimals(),
- ])
+ return tokens[address]
+ }
+}
- const [ symbol = [], decimals = [] ] = result
+async function getSymbolAndDecimals (tokenAddress, existingTokens = []) {
+ const existingToken = existingTokens.find(({ address }) => tokenAddress === address)
+ if (existingToken) {
+ return existingToken
+ }
+
+ let result = []
+ try {
+ const token = util.getContractAtAddress(tokenAddress)
+
+ result = await Promise.all([
+ token.symbol(),
+ token.decimals(),
+ ])
+ } catch (err) {
+ console.log(`symbol() and decimal() calls for token at address ${tokenAddress} resulted in error:`, err)
+ }
- tokens[address] = { symbol: symbol[0], decimals: decimals[0] }
+ const [ symbol = [], decimals = [] ] = result
- return tokens[address]
+ return {
+ symbol: symbol[0] || null,
+ decimals: decimals[0] && decimals[0].toString() || null,
}
}
@@ -42,4 +51,5 @@ function calcTokenAmount (value, decimals) {
module.exports = {
tokenInfoGetter,
calcTokenAmount,
+ getSymbolAndDecimals,
}