hardcode ens contract address to isERC721Transaction function, fallback to 18/unknown in methods fetching token decimals

This commit is contained in:
Mikhail Mikheev 2020-06-26 13:22:34 +04:00
parent f39b0903ca
commit 46c227881a
2 changed files with 42 additions and 17 deletions

View File

@ -26,7 +26,7 @@ export const getEthAsToken = (balance: string): Token => {
})
}
export const isAddressAToken = async (tokenAddress): Promise<boolean> => {
export const isAddressAToken = async (tokenAddress: string): Promise<boolean> => {
// SECOND APPROACH:
// They both seem to work the same
// const tokenContract = await getStandardTokenContract()
@ -45,26 +45,38 @@ export const isTokenTransfer = (tx: any): boolean => {
}
export const isSendERC721Transaction = (tx: any, txCode: string, knownTokens: any) => {
// "0x57f1887a8BF19b14fC0dF6Fd9B2acc9Af147eA85" - ens token contract, includes safeTransferFrom
// but no proper ERC721 standard implemented
return (
(txCode && txCode.includes(SAFE_TRANSFER_FROM_WITHOUT_DATA_HASH)) ||
(txCode &&
txCode.includes(SAFE_TRANSFER_FROM_WITHOUT_DATA_HASH) &&
tx.to !== '0x57f1887a8BF19b14fC0dF6Fd9B2acc9Af147eA85') ||
(isTokenTransfer(tx) && !knownTokens.get(tx.to))
)
}
export const getERC721Symbol = memoize(
async (contractAddress: string): Promise<string> => {
let tokenSymbol = 'UNKNOWN'
try {
const ERC721token = await getERC721TokenContract()
const tokenInstance = await ERC721token.at(contractAddress)
return tokenInstance.symbol()
tokenSymbol = tokenInstance.symbol()
} catch (err) {
console.error(`Failed to retrieve token symbol for ERC721 token ${contractAddress}`)
}
return tokenSymbol
},
)
export const getERC20DecimalsAndSymbol = async (
tokenAddress: string,
): Promise<{ decimals: number; symbol: string }> => {
const tokenInfos = await getTokenInfos(tokenAddress)
const tokenInfo = { decimals: 18, symbol: 'UNKNOWN' }
try {
const storedTokenInfo = await getTokenInfos(tokenAddress)
if (tokenInfos === null) {
if (storedTokenInfo === null) {
const [tokenDecimals, tokenSymbol] = await generateBatchRequests({
abi: ALTERNATIVE_TOKEN_ABI,
address: tokenAddress,
@ -73,8 +85,11 @@ export const getERC20DecimalsAndSymbol = async (
return { decimals: Number(tokenDecimals), symbol: tokenSymbol }
}
} catch (err) {
console.error(`Failed to retrieve token info for ERC20 token ${tokenAddress}`)
}
return { decimals: Number(tokenInfos.decimals), symbol: tokenInfos.symbol }
return tokenInfo
}
export const isSendERC20Transaction = async (

View File

@ -253,7 +253,17 @@ export const buildTx = async ({
const refundParams = await getRefundParams(tx, getERC20DecimalsAndSymbol)
const decodedParams = getDecodedParams(tx)
const confirmations = getConfirmations(tx)
const { decimals = 18, symbol = 'ETH' } = isSendERC20Tx ? await getERC20DecimalsAndSymbol(tx.to) : {}
let tokenDecimals = 18
let tokenSymbol = 'ETH'
if (isSendERC20Tx) {
const { decimals, symbol } = await getERC20DecimalsAndSymbol(tx.to)
tokenDecimals = decimals
tokenSymbol = symbol
} else if (isSendERC721Tx) {
const { symbol } = await getERC721Symbol(tx.to)
tokenSymbol = symbol
}
const txToStore = makeTransaction({
baseGas: tx.baseGas,
@ -263,7 +273,7 @@ export const buildTx = async ({
creationTx: tx.creationTx,
customTx: isCustomTx,
data: tx.data ? tx.data : EMPTY_DATA,
decimals,
decimals: tokenDecimals,
decodedParams,
executionDate: tx.executionDate,
executionTxHash: tx.transactionHash,
@ -286,7 +296,7 @@ export const buildTx = async ({
safeTxGas: tx.safeTxGas,
safeTxHash: tx.safeTxHash,
submissionDate: tx.submissionDate,
symbol: isSendERC721Tx ? await getERC721Symbol(tx.to) : symbol,
symbol: tokenSymbol,
upgradeTx: isUpgradeTx,
value: tx.value.toString(),
})