diff --git a/app/util/transactions.js b/app/util/transactions.js index d150f103e2c..13dc472f4a1 100644 --- a/app/util/transactions.js +++ b/app/util/transactions.js @@ -3,6 +3,7 @@ import { rawEncode, rawDecode } from 'ethereumjs-abi'; import Engine from '../core/Engine'; import { strings } from '../../locales/i18n'; import contractMap from 'eth-contract-metadata'; +import { isSmartContractCode } from 'gaba/util'; export const TOKEN_METHOD_TRANSFER = 'transfer'; export const TOKEN_METHOD_APPROVE = 'approve'; @@ -29,14 +30,6 @@ class ActionKeys { static cache = {}; } -/** - * Utility class with the single responsibility - * of caching SmartContractAddresses - */ -class SmartContractAddresses { - static cache = {}; -} - /** * Utility class with the single responsibility * of caching CollectibleAddresses @@ -140,31 +133,18 @@ export function getMethodData(data) { * Returns wether the given address is a contract * * @param {string} address - Ethereum address - * @param {string} transactionHash? - Transaction hash * @returns {boolean} - Wether the given address is a contract */ -export async function isSmartContractAddress(address, transactionHash) { +export async function isSmartContractAddress(address) { address = toChecksumAddress(address); // If in contract map we don't need to cache it if (contractMap[address]) { return Promise.resolve(true); } - // Look in cache memory only for specific time (transaction hash) - if (transactionHash) { - const cache = SmartContractAddresses.cache[(address, transactionHash)]; - if (cache) { - return Promise.resolve(cache); - } - } const { TransactionController } = Engine.context; const code = address ? await TransactionController.query('getCode', [address]) : undefined; - // Geth will return '0x', and ganache-core v2.2.1 will return '0x0' - const codeIsEmpty = !code || code === '0x' || code === '0x0'; - // Save in cache for specific time (transaction hash) - if (transactionHash) { - SmartContractAddresses.cache[(address, transactionHash)] = !codeIsEmpty; - } - return !codeIsEmpty; + const isSmartContract = isSmartContractCode(code); + return isSmartContract; } /** @@ -225,7 +205,8 @@ export async function getTransactionActionKey(transaction) { return ret; } } - const toSmartContract = await isSmartContractAddress(to, transactionHash); + const toSmartContract = + transaction.toSmartContract !== undefined ? transaction.toSmartContract : await isSmartContractAddress(to); if (toSmartContract) { // There is no data or unknown method data, if is smart contract ret = SMART_CONTRACT_INTERACTION_ACTION_KEY;