mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-22 17:33:23 +01:00
Wraps calls to symbol() and decimals() in try catch
This commit is contained in:
parent
4e7b0ff15c
commit
6de450488b
@ -8,7 +8,7 @@ const abiDecoder = require('abi-decoder')
|
|||||||
abiDecoder.addABI(abi)
|
abiDecoder.addABI(abi)
|
||||||
const inherits = require('util').inherits
|
const inherits = require('util').inherits
|
||||||
const actions = require('../../actions')
|
const actions = require('../../actions')
|
||||||
const util = require('../../util')
|
const { getSymbolAndDecimals } = require('../../token-util')
|
||||||
const ConfirmSendEther = require('./confirm-send-ether')
|
const ConfirmSendEther = require('./confirm-send-ether')
|
||||||
const ConfirmSendToken = require('./confirm-send-token')
|
const ConfirmSendToken = require('./confirm-send-token')
|
||||||
const ConfirmDeployContract = require('./confirm-deploy-contract')
|
const ConfirmDeployContract = require('./confirm-deploy-contract')
|
||||||
@ -26,6 +26,7 @@ function mapStateToProps (state) {
|
|||||||
const {
|
const {
|
||||||
conversionRate,
|
conversionRate,
|
||||||
identities,
|
identities,
|
||||||
|
tokens: existingTokens,
|
||||||
} = state.metamask
|
} = state.metamask
|
||||||
const accounts = state.metamask.accounts
|
const accounts = state.metamask.accounts
|
||||||
const selectedAddress = state.metamask.selectedAddress || Object.keys(accounts)[0]
|
const selectedAddress = state.metamask.selectedAddress || Object.keys(accounts)[0]
|
||||||
@ -33,6 +34,7 @@ function mapStateToProps (state) {
|
|||||||
conversionRate,
|
conversionRate,
|
||||||
identities,
|
identities,
|
||||||
selectedAddress,
|
selectedAddress,
|
||||||
|
existingTokens,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,6 +68,7 @@ PendingTx.prototype.componentDidUpdate = function (prevProps, prevState) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
PendingTx.prototype.setTokenData = async function () {
|
PendingTx.prototype.setTokenData = async function () {
|
||||||
|
const { existingTokens } = this.props
|
||||||
const txMeta = this.gatherTxMeta()
|
const txMeta = this.gatherTxMeta()
|
||||||
const txParams = txMeta.txParams || {}
|
const txParams = txMeta.txParams || {}
|
||||||
|
|
||||||
@ -89,19 +92,14 @@ PendingTx.prototype.setTokenData = async function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isTokenTransaction) {
|
if (isTokenTransaction) {
|
||||||
const token = util.getContractAtAddress(txParams.to)
|
const { symbol, decimals } = await getSymbolAndDecimals(txParams.to, existingTokens)
|
||||||
const results = await Promise.all([
|
|
||||||
token.symbol(),
|
|
||||||
token.decimals(),
|
|
||||||
])
|
|
||||||
const [ symbol, decimals ] = results
|
|
||||||
|
|
||||||
if (symbol[0] && decimals[0]) {
|
if (symbol && decimals) {
|
||||||
this.setState({
|
this.setState({
|
||||||
transactionType: TX_TYPES.SEND_TOKEN,
|
transactionType: TX_TYPES.SEND_TOKEN,
|
||||||
tokenAddress: txParams.to,
|
tokenAddress: txParams.to,
|
||||||
tokenSymbol: symbol[0],
|
tokenSymbol: symbol,
|
||||||
tokenDecimals: decimals[0],
|
tokenDecimals: decimals,
|
||||||
isFetching: false,
|
isFetching: false,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
@ -493,7 +493,7 @@ SendTransactionScreen.prototype.renderFooter = function () {
|
|||||||
history,
|
history,
|
||||||
} = this.props
|
} = this.props
|
||||||
|
|
||||||
const missingTokenBalance = selectedToken && !tokenBalance
|
const missingTokenBalance = selectedToken && (tokenBalance === null || tokenBalance === undefined)
|
||||||
const noErrors = !amountError && toError === null
|
const noErrors = !amountError && toError === null
|
||||||
|
|
||||||
return h('div.page-container__footer', [
|
return h('div.page-container__footer', [
|
||||||
|
@ -1,14 +1,6 @@
|
|||||||
const abi = require('human-standard-token-abi')
|
const util = require('./util')
|
||||||
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)
|
|
||||||
|
|
||||||
|
function tokenInfoGetter () {
|
||||||
const tokens = {}
|
const tokens = {}
|
||||||
|
|
||||||
return async (address) => {
|
return async (address) => {
|
||||||
@ -16,21 +8,41 @@ const tokenInfoGetter = function () {
|
|||||||
return tokens[address]
|
return tokens[address]
|
||||||
}
|
}
|
||||||
|
|
||||||
const contract = TokenContract.at(address)
|
tokens[address] = await getSymbolAndDecimals(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]
|
return tokens[address]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
const [ symbol = [], decimals = [] ] = result
|
||||||
|
|
||||||
|
return {
|
||||||
|
symbol: symbol[0],
|
||||||
|
decimals: decimals[0],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function calcTokenAmount (value, decimals) {
|
function calcTokenAmount (value, decimals) {
|
||||||
const multiplier = Math.pow(10, Number(decimals || 0))
|
const multiplier = Math.pow(10, Number(decimals || 0))
|
||||||
const amount = Number(value / multiplier)
|
const amount = Number(value / multiplier)
|
||||||
@ -42,4 +54,5 @@ function calcTokenAmount (value, decimals) {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
tokenInfoGetter,
|
tokenInfoGetter,
|
||||||
calcTokenAmount,
|
calcTokenAmount,
|
||||||
|
getSymbolAndDecimals,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user