aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Tseung <alextsg@users.noreply.github.com>2018-08-28 06:28:29 +0800
committerGitHub <noreply@github.com>2018-08-28 06:28:29 +0800
commit0259eb02140fec1db9861506a6ff7890911af652 (patch)
tree0d6320080b61793053299514ae67b9402d0ec854
parent4b17ec67ecd7c16b942fc49aedb8e53732adbb96 (diff)
parent7924dbc0c99730c500169c1a31cccd64b75cbce0 (diff)
downloadtangerine-wallet-browser-0259eb02140fec1db9861506a6ff7890911af652.tar
tangerine-wallet-browser-0259eb02140fec1db9861506a6ff7890911af652.tar.gz
tangerine-wallet-browser-0259eb02140fec1db9861506a6ff7890911af652.tar.bz2
tangerine-wallet-browser-0259eb02140fec1db9861506a6ff7890911af652.tar.lz
tangerine-wallet-browser-0259eb02140fec1db9861506a6ff7890911af652.tar.xz
tangerine-wallet-browser-0259eb02140fec1db9861506a6ff7890911af652.tar.zst
tangerine-wallet-browser-0259eb02140fec1db9861506a6ff7890911af652.zip
Merge pull request #5121 from MetaMask/i5099-approve
Prevent token symbol from showing as null in the confirm screen
-rw-r--r--ui/app/token-util.js116
1 files changed, 87 insertions, 29 deletions
diff --git a/ui/app/token-util.js b/ui/app/token-util.js
index 8798ed266..3d61ad1ca 100644
--- a/ui/app/token-util.js
+++ b/ui/app/token-util.js
@@ -1,55 +1,113 @@
const log = require('loglevel')
const util = require('./util')
const BigNumber = require('bignumber.js')
+import contractMap from 'eth-contract-metadata'
-function tokenInfoGetter () {
- const tokens = {}
+const casedContractMap = Object.keys(contractMap).reduce((acc, base) => {
+ return {
+ ...acc,
+ [base.toLowerCase()]: contractMap[base],
+ }
+}, {})
- return async (address) => {
- if (tokens[address]) {
- return tokens[address]
+const DEFAULT_SYMBOL = ''
+const DEFAULT_DECIMALS = '0'
+
+async function getSymbolFromContract (tokenAddress) {
+ const token = util.getContractAtAddress(tokenAddress)
+
+ try {
+ const result = await token.symbol()
+ return result[0]
+ } catch (error) {
+ log.warn(`symbol() call for token at address ${tokenAddress} resulted in error:`, error)
+ }
+}
+
+async function getDecimalsFromContract (tokenAddress) {
+ const token = util.getContractAtAddress(tokenAddress)
+
+ try {
+ const result = await token.decimals()
+ const decimalsBN = result[0]
+ return decimalsBN && decimalsBN.toString()
+ } catch (error) {
+ log.warn(`decimals() call for token at address ${tokenAddress} resulted in error:`, error)
+ }
+}
+
+function getContractMetadata (tokenAddress) {
+ return tokenAddress && casedContractMap[tokenAddress.toLowerCase()]
+}
+
+async function getSymbol (tokenAddress) {
+ let symbol = await getSymbolFromContract(tokenAddress)
+
+ if (!symbol) {
+ const contractMetadataInfo = getContractMetadata(tokenAddress)
+
+ if (contractMetadataInfo) {
+ symbol = contractMetadataInfo.symbol
}
+ }
- tokens[address] = await getSymbolAndDecimals(address)
+ return symbol
+}
- return tokens[address]
+async function getDecimals (tokenAddress) {
+ let decimals = await getDecimalsFromContract(tokenAddress)
+
+ if (!decimals || decimals === '0') {
+ const contractMetadataInfo = getContractMetadata(tokenAddress)
+
+ if (contractMetadataInfo) {
+ decimals = contractMetadataInfo.decimals
+ }
}
+
+ return decimals
}
-async function getSymbolAndDecimals (tokenAddress, existingTokens = []) {
+export async function getSymbolAndDecimals (tokenAddress, existingTokens = []) {
const existingToken = existingTokens.find(({ address }) => tokenAddress === address)
+
if (existingToken) {
- return existingToken
+ return {
+ symbol: existingToken.symbol,
+ decimals: existingToken.decimals,
+ }
}
- let result = []
+ let symbol, decimals
+
try {
- const token = util.getContractAtAddress(tokenAddress)
+ symbol = await getSymbol(tokenAddress)
+ decimals = await getDecimals(tokenAddress)
+ } catch (error) {
+ log.warn(`symbol() and decimal() calls for token at address ${tokenAddress} resulted in error:`, error)
+ }
- result = await Promise.all([
- token.symbol(),
- token.decimals(),
- ])
- } catch (err) {
- log.warn(`symbol() and decimal() calls for token at address ${tokenAddress} resulted in error:`, err)
+ return {
+ symbol: symbol || DEFAULT_SYMBOL,
+ decimals: decimals || DEFAULT_DECIMALS,
}
+}
+
+export function tokenInfoGetter () {
+ const tokens = {}
- const [ symbol = [], decimals = [] ] = result
+ return async (address) => {
+ if (tokens[address]) {
+ return tokens[address]
+ }
- return {
- symbol: symbol[0] || null,
- decimals: decimals[0] && decimals[0].toString() || null,
+ tokens[address] = await getSymbolAndDecimals(address)
+
+ return tokens[address]
}
}
-function calcTokenAmount (value, decimals) {
+export function calcTokenAmount (value, decimals) {
const multiplier = Math.pow(10, Number(decimals || 0))
return new BigNumber(String(value)).div(multiplier).toNumber()
}
-
-
-module.exports = {
- tokenInfoGetter,
- calcTokenAmount,
- getSymbolAndDecimals,
-}