From 6de450488b8303e01d4be60c571e99f995711e51 Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 27 Apr 2018 20:45:36 -0230 Subject: [PATCH 1/3] Wraps calls to symbol() and decimals() in try catch --- ui/app/components/pending-tx/index.js | 20 +++++------ ui/app/send-v2.js | 2 +- ui/app/token-util.js | 49 +++++++++++++++++---------- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/ui/app/components/pending-tx/index.js b/ui/app/components/pending-tx/index.js index 6ee83ba7e..392650e01 100644 --- a/ui/app/components/pending-tx/index.js +++ b/ui/app/components/pending-tx/index.js @@ -8,7 +8,7 @@ const abiDecoder = require('abi-decoder') abiDecoder.addABI(abi) const inherits = require('util').inherits const actions = require('../../actions') -const util = require('../../util') +const { getSymbolAndDecimals } = require('../../token-util') const ConfirmSendEther = require('./confirm-send-ether') const ConfirmSendToken = require('./confirm-send-token') const ConfirmDeployContract = require('./confirm-deploy-contract') @@ -26,6 +26,7 @@ function mapStateToProps (state) { const { conversionRate, identities, + tokens: existingTokens, } = state.metamask const accounts = state.metamask.accounts const selectedAddress = state.metamask.selectedAddress || Object.keys(accounts)[0] @@ -33,6 +34,7 @@ function mapStateToProps (state) { conversionRate, identities, selectedAddress, + existingTokens, } } @@ -66,6 +68,7 @@ PendingTx.prototype.componentDidUpdate = function (prevProps, prevState) { } PendingTx.prototype.setTokenData = async function () { + const { existingTokens } = this.props const txMeta = this.gatherTxMeta() const txParams = txMeta.txParams || {} @@ -89,19 +92,14 @@ PendingTx.prototype.setTokenData = async function () { } if (isTokenTransaction) { - const token = util.getContractAtAddress(txParams.to) - const results = await Promise.all([ - token.symbol(), - token.decimals(), - ]) - const [ symbol, decimals ] = results - - if (symbol[0] && decimals[0]) { + const { symbol, decimals } = await getSymbolAndDecimals(txParams.to, existingTokens) + + if (symbol && decimals) { this.setState({ transactionType: TX_TYPES.SEND_TOKEN, tokenAddress: txParams.to, - tokenSymbol: symbol[0], - tokenDecimals: decimals[0], + tokenSymbol: symbol, + tokenDecimals: decimals, isFetching: false, }) } else { diff --git a/ui/app/send-v2.js b/ui/app/send-v2.js index 30d3d3152..bd00b186e 100644 --- a/ui/app/send-v2.js +++ b/ui/app/send-v2.js @@ -493,7 +493,7 @@ SendTransactionScreen.prototype.renderFooter = function () { history, } = this.props - const missingTokenBalance = selectedToken && !tokenBalance + const missingTokenBalance = selectedToken && (tokenBalance === null || tokenBalance === undefined) const noErrors = !amountError && toError === null return h('div.page-container__footer', [ diff --git a/ui/app/token-util.js b/ui/app/token-util.js index f84051ef5..2ae3b15b9 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,38 @@ 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 { + symbol: existingToken.symbol, + decimals: existingToken.decimals, + } + } + + 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], + decimals: decimals[0], } } @@ -42,4 +54,5 @@ function calcTokenAmount (value, decimals) { module.exports = { tokenInfoGetter, calcTokenAmount, + getSymbolAndDecimals, } From b71dbf52d190a597ad3e06e4717cc641e1abe57a Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 27 Apr 2018 21:33:56 -0230 Subject: [PATCH 2/3] Convert decimals to string in getSymbolAndDecimals; return null for symbol or decimals. --- ui/app/components/pages/add-token.js | 2 +- ui/app/components/pending-tx/index.js | 24 +++++++----------------- ui/app/token-util.js | 4 ++-- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/ui/app/components/pages/add-token.js b/ui/app/components/pages/add-token.js index 566e42450..8d52571d0 100644 --- a/ui/app/components/pages/add-token.js +++ b/ui/app/components/pages/add-token.js @@ -192,7 +192,7 @@ AddTokenScreen.prototype.attemptToAutoFillTokenParams = async function (address) if (symbol && decimals) { this.setState({ customSymbol: symbol, - customDecimals: decimals.toString(), + customDecimals: decimals, autoFilled: true, }) } diff --git a/ui/app/components/pending-tx/index.js b/ui/app/components/pending-tx/index.js index 392650e01..fb409cb92 100644 --- a/ui/app/components/pending-tx/index.js +++ b/ui/app/components/pending-tx/index.js @@ -94,23 +94,13 @@ PendingTx.prototype.setTokenData = async function () { if (isTokenTransaction) { const { symbol, decimals } = await getSymbolAndDecimals(txParams.to, existingTokens) - if (symbol && decimals) { - this.setState({ - transactionType: TX_TYPES.SEND_TOKEN, - tokenAddress: txParams.to, - tokenSymbol: symbol, - tokenDecimals: decimals, - isFetching: false, - }) - } else { - this.setState({ - transactionType: TX_TYPES.SEND_TOKEN, - tokenAddress: txParams.to, - tokenSymbol: null, - tokenDecimals: null, - isFetching: false, - }) - } + this.setState({ + transactionType: TX_TYPES.SEND_TOKEN, + tokenAddress: txParams.to, + tokenSymbol: symbol, + tokenDecimals: decimals, + isFetching: false, + }) } else { this.setState({ transactionType: TX_TYPES.SEND_ETHER, diff --git a/ui/app/token-util.js b/ui/app/token-util.js index 2ae3b15b9..1b0091826 100644 --- a/ui/app/token-util.js +++ b/ui/app/token-util.js @@ -38,8 +38,8 @@ async function getSymbolAndDecimals (tokenAddress, existingTokens = []) { const [ symbol = [], decimals = [] ] = result return { - symbol: symbol[0], - decimals: decimals[0], + symbol: symbol[0] || null, + decimals: decimals[0] && decimals[0].toString() || null, } } From 9f9e8789cc707fa3c53d64edeb2d51e84defcbc6 Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 27 Apr 2018 21:38:57 -0230 Subject: [PATCH 3/3] Simplify return of existing token in getSymbolAndDecimals --- ui/app/token-util.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ui/app/token-util.js b/ui/app/token-util.js index 1b0091826..920442bfc 100644 --- a/ui/app/token-util.js +++ b/ui/app/token-util.js @@ -17,10 +17,7 @@ function tokenInfoGetter () { async function getSymbolAndDecimals (tokenAddress, existingTokens = []) { const existingToken = existingTokens.find(({ address }) => tokenAddress === address) if (existingToken) { - return { - symbol: existingToken.symbol, - decimals: existingToken.decimals, - } + return existingToken } let result = []