Skip to content

Commit

Permalink
handle address code with gaba
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanmino committed Mar 26, 2019
1 parent f7639d6 commit 8053717
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions app/util/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8053717

Please sign in to comment.