Merge pull request #4113 from MetaMask/i4107-send-tokens-without-symbols-decimal-methods

Wraps calls to symbol() and decimals() in try catch (Fixes #4107)
feature/default_network_editable
kumavis 7 years ago committed by GitHub
commit 141884ee6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      ui/app/components/pages/add-token.js
  2. 38
      ui/app/components/pending-tx/index.js
  3. 2
      ui/app/send-v2.js
  4. 46
      ui/app/token-util.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,
})
}

@ -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,30 +92,15 @@ 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]) {
this.setState({
transactionType: TX_TYPES.SEND_TOKEN,
tokenAddress: txParams.to,
tokenSymbol: symbol[0],
tokenDecimals: decimals[0],
isFetching: false,
})
} else {
this.setState({
transactionType: TX_TYPES.SEND_TOKEN,
tokenAddress: txParams.to,
tokenSymbol: null,
tokenDecimals: null,
isFetching: false,
})
}
const { symbol, decimals } = await getSymbolAndDecimals(txParams.to, existingTokens)
this.setState({
transactionType: TX_TYPES.SEND_TOKEN,
tokenAddress: txParams.to,
tokenSymbol: symbol,
tokenDecimals: decimals,
isFetching: false,
})
} else {
this.setState({
transactionType: TX_TYPES.SEND_ETHER,

@ -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', [

@ -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,
}

Loading…
Cancel
Save