2021-02-04 19:15:23 +01:00
|
|
|
import { MethodRegistry } from 'eth-method-registry';
|
|
|
|
import abi from 'human-standard-token-abi';
|
|
|
|
import { ethers } from 'ethers';
|
|
|
|
import log from 'loglevel';
|
|
|
|
import { addHexPrefix } from '../../../../app/scripts/lib/util';
|
|
|
|
import { getEtherscanNetworkPrefix } from '../../../lib/etherscan-prefix-for-network';
|
2018-07-31 07:03:20 +02:00
|
|
|
import {
|
2020-11-03 23:57:51 +01:00
|
|
|
TRANSACTION_CATEGORIES,
|
2020-11-07 08:38:12 +01:00
|
|
|
TRANSACTION_GROUP_STATUSES,
|
2020-11-03 23:57:51 +01:00
|
|
|
TRANSACTION_STATUSES,
|
|
|
|
TRANSACTION_TYPES,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../../shared/constants/transaction';
|
|
|
|
import fetchWithCache from './fetch-with-cache';
|
2020-08-18 21:18:25 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import { addCurrencies } from './conversion-util';
|
2018-08-31 21:36:07 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const hstInterface = new ethers.utils.Interface(abi);
|
2018-08-06 07:25:58 +02:00
|
|
|
|
2020-08-22 04:29:19 +02:00
|
|
|
/**
|
|
|
|
* @typedef EthersContractCall
|
|
|
|
* @type object
|
|
|
|
* @property {any[]} args - The args/params to the function call.
|
|
|
|
* An array-like object with numerical and string indices.
|
|
|
|
* @property {string} name - The name of the function.
|
|
|
|
* @property {string} signature - The function signature.
|
|
|
|
* @property {string} sighash - The function signature hash.
|
|
|
|
* @property {EthersBigNumber} value - The ETH value associated with the call.
|
|
|
|
* @property {FunctionFragment} functionFragment - The Ethers function fragment
|
|
|
|
* representation of the function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {EthersContractCall | undefined}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getTokenData(data) {
|
2020-08-22 04:29:19 +02:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
return hstInterface.parseTransaction({ data });
|
2020-08-22 04:29:19 +02:00
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.debug('Failed to parse transaction data.', error, data);
|
|
|
|
return undefined;
|
2020-08-22 04:29:19 +02:00
|
|
|
}
|
2018-08-06 07:25:58 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function getMethodFrom4Byte(fourBytePrefix) {
|
|
|
|
const fourByteResponse = await fetchWithCache(
|
|
|
|
`https://www.4byte.directory/api/v1/signatures/?hex_signature=${fourBytePrefix}`,
|
|
|
|
{
|
|
|
|
referrerPolicy: 'no-referrer-when-downgrade',
|
|
|
|
body: null,
|
|
|
|
method: 'GET',
|
|
|
|
mode: 'cors',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2019-09-04 21:10:23 +02:00
|
|
|
if (fourByteResponse.count === 1) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return fourByteResponse.results[0].text_signature;
|
2019-06-18 14:17:14 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
2019-06-18 14:17:14 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
let registry;
|
2018-08-06 07:25:58 +02:00
|
|
|
|
2018-10-24 17:27:16 +02:00
|
|
|
/**
|
2019-06-18 14:17:14 +02:00
|
|
|
* Attempts to return the method data from the MethodRegistry library, the message registry library and the token abi, in that order of preference
|
|
|
|
* @param {string} fourBytePrefix - The prefix from the method code associated with the data
|
2018-10-24 17:27:16 +02:00
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export async function getMethodDataAsync(fourBytePrefix) {
|
2019-06-18 14:17:14 +02:00
|
|
|
try {
|
|
|
|
const fourByteSig = getMethodFrom4Byte(fourBytePrefix).catch((e) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.error(e);
|
|
|
|
return null;
|
|
|
|
});
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2020-03-17 15:14:27 +01:00
|
|
|
if (!registry) {
|
2021-02-04 19:15:23 +01:00
|
|
|
registry = new MethodRegistry({ provider: global.ethereumProvider });
|
2020-03-17 15:14:27 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let sig = await registry.lookup(fourBytePrefix);
|
2019-06-18 14:17:14 +02:00
|
|
|
|
|
|
|
if (!sig) {
|
2021-02-04 19:15:23 +01:00
|
|
|
sig = await fourByteSig;
|
2019-03-29 15:48:08 +01:00
|
|
|
}
|
2018-10-24 17:27:16 +02:00
|
|
|
|
2019-06-18 14:17:14 +02:00
|
|
|
if (!sig) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return {};
|
2019-06-18 14:17:14 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const parsedResult = registry.parse(sig);
|
2018-08-06 07:25:58 +02:00
|
|
|
|
2019-06-18 14:17:14 +02:00
|
|
|
return {
|
|
|
|
name: parsedResult.name,
|
|
|
|
params: parsedResult.args,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-06-18 14:17:14 +02:00
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.error(error);
|
|
|
|
return {};
|
2019-06-18 14:17:14 +02:00
|
|
|
}
|
2018-08-06 07:25:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-22 19:22:56 +01:00
|
|
|
/**
|
|
|
|
* Returns four-byte method signature from data
|
|
|
|
*
|
|
|
|
* @param {string} data - The hex data (@code txParams.data) of a transaction
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {string} The four-byte method signature
|
2019-01-22 19:22:56 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getFourBytePrefix(data = '') {
|
2021-02-04 19:15:23 +01:00
|
|
|
const prefixedData = addHexPrefix(data);
|
|
|
|
const fourBytePrefix = prefixedData.slice(0, 10);
|
|
|
|
return fourBytePrefix;
|
2019-01-22 19:22:56 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 05:53:12 +02:00
|
|
|
/**
|
2020-11-03 00:41:28 +01:00
|
|
|
* Given an transaction category, returns a boolean which indicates whether the transaction is calling an erc20 token method
|
|
|
|
*
|
|
|
|
* @param {string} transactionCategory - The category of transaction being evaluated
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {boolean} whether the transaction is calling an erc20 token method
|
2020-11-03 00:41:28 +01:00
|
|
|
*/
|
|
|
|
export function isTokenMethodAction(transactionCategory) {
|
2019-06-28 05:53:12 +02:00
|
|
|
return [
|
2020-11-03 23:57:51 +01:00
|
|
|
TRANSACTION_CATEGORIES.TOKEN_METHOD_TRANSFER,
|
|
|
|
TRANSACTION_CATEGORIES.TOKEN_METHOD_APPROVE,
|
|
|
|
TRANSACTION_CATEGORIES.TOKEN_METHOD_TRANSFER_FROM,
|
2021-02-04 19:15:23 +01:00
|
|
|
].includes(transactionCategory);
|
2019-06-28 05:53:12 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getLatestSubmittedTxWithNonce(
|
|
|
|
transactions = [],
|
|
|
|
nonce = '0x0',
|
|
|
|
) {
|
2018-08-03 05:20:15 +02:00
|
|
|
if (!transactions.length) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return {};
|
2018-08-03 05:20:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return transactions.reduce((acc, current) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { submittedTime, txParams: { nonce: currentNonce } = {} } = current;
|
2018-08-03 05:20:15 +02:00
|
|
|
|
2018-08-12 06:12:30 +02:00
|
|
|
if (currentNonce === nonce) {
|
2020-08-13 22:00:09 +02:00
|
|
|
if (!acc.submittedTime) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return current;
|
2020-08-13 22:00:09 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return submittedTime > acc.submittedTime ? current : acc;
|
2018-08-03 05:20:15 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return acc;
|
|
|
|
}, {});
|
2018-08-03 05:20:15 +02:00
|
|
|
}
|
2018-08-24 01:44:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export async function isSmartContractAddress(address) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const code = await global.eth.getCode(address);
|
2018-10-21 06:52:41 +02:00
|
|
|
// Geth will return '0x', and ganache-core v2.2.1 will return '0x0'
|
2021-02-04 19:15:23 +01:00
|
|
|
const codeIsEmpty = !code || code === '0x' || code === '0x0';
|
|
|
|
return !codeIsEmpty;
|
2018-08-24 01:44:38 +02:00
|
|
|
}
|
2018-08-31 21:36:07 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function sumHexes(...args) {
|
2020-11-13 07:08:18 +01:00
|
|
|
const total = args.reduce((acc, hexAmount) => {
|
|
|
|
return addCurrencies(acc, hexAmount, {
|
2018-08-31 21:36:07 +02:00
|
|
|
toNumericBase: 'hex',
|
2020-11-13 07:08:18 +01:00
|
|
|
aBase: 16,
|
|
|
|
bBase: 16,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-08-31 21:36:07 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return addHexPrefix(total);
|
2018-08-31 21:36:07 +02:00
|
|
|
}
|
2018-10-16 00:00:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a status key for a transaction. Requires parsing the txMeta.txReceipt on top of
|
|
|
|
* txMeta.status because txMeta.status does not reflect on-chain errors.
|
|
|
|
* @param {Object} transaction - The txMeta object of a transaction.
|
|
|
|
* @param {Object} transaction.txReceipt - The transaction receipt.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getStatusKey(transaction) {
|
|
|
|
const {
|
|
|
|
txReceipt: { status: receiptStatus } = {},
|
|
|
|
type,
|
|
|
|
status,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = transaction;
|
2018-10-16 00:00:47 +02:00
|
|
|
|
|
|
|
// There was an on-chain failure
|
2018-12-09 21:48:06 +01:00
|
|
|
if (receiptStatus === '0x0') {
|
2021-02-04 19:15:23 +01:00
|
|
|
return TRANSACTION_STATUSES.FAILED;
|
2018-10-16 00:00:47 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
2020-11-03 23:57:51 +01:00
|
|
|
status === TRANSACTION_STATUSES.CONFIRMED &&
|
|
|
|
type === TRANSACTION_TYPES.CANCEL
|
2020-11-03 00:41:28 +01:00
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return TRANSACTION_GROUP_STATUSES.CANCELLED;
|
2018-12-09 21:48:06 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return transaction.status;
|
2018-10-16 00:00:47 +02:00
|
|
|
}
|
2019-05-09 19:27:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an external block explorer URL at which a transaction can be viewed.
|
|
|
|
* @param {number} networkId
|
|
|
|
* @param {string} hash
|
|
|
|
* @param {Object} rpcPrefs
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getBlockExplorerUrlForTx(networkId, hash, rpcPrefs = {}) {
|
2019-05-09 19:27:14 +02:00
|
|
|
if (rpcPrefs.blockExplorerUrl) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return `${rpcPrefs.blockExplorerUrl.replace(/\/+$/u, '')}/tx/${hash}`;
|
2019-05-09 19:27:14 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const prefix = getEtherscanNetworkPrefix(networkId);
|
|
|
|
return `https://${prefix}etherscan.io/tx/${hash}`;
|
2019-05-09 19:27:14 +02:00
|
|
|
}
|
2021-02-08 17:06:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a title for the given transaction category.
|
|
|
|
*
|
|
|
|
* This will throw an error if the transaction category is unrecognized and no default is provided.
|
|
|
|
* @param {function} t - The translation function
|
|
|
|
* @param {TRANSACTION_CATEGORIES[keyof TRANSACTION_CATEGORIES]} transactionCategory - The transaction category constant
|
|
|
|
* @returns {string} The transaction category title
|
|
|
|
*/
|
|
|
|
export function getTransactionCategoryTitle(t, transactionCategory) {
|
|
|
|
switch (transactionCategory) {
|
|
|
|
case TRANSACTION_CATEGORIES.TOKEN_METHOD_TRANSFER: {
|
|
|
|
return t('transfer');
|
|
|
|
}
|
|
|
|
case TRANSACTION_CATEGORIES.TOKEN_METHOD_TRANSFER_FROM: {
|
2021-02-08 18:30:27 +01:00
|
|
|
return t('transferFrom');
|
2021-02-08 17:06:58 +01:00
|
|
|
}
|
|
|
|
case TRANSACTION_CATEGORIES.TOKEN_METHOD_APPROVE: {
|
|
|
|
return t('approve');
|
|
|
|
}
|
|
|
|
case TRANSACTION_CATEGORIES.SENT_ETHER: {
|
|
|
|
return t('sentEther');
|
|
|
|
}
|
|
|
|
case TRANSACTION_CATEGORIES.CONTRACT_INTERACTION: {
|
|
|
|
return t('contractInteraction');
|
|
|
|
}
|
|
|
|
case TRANSACTION_CATEGORIES.DEPLOY_CONTRACT: {
|
|
|
|
return t('contractDeployment');
|
|
|
|
}
|
|
|
|
case TRANSACTION_CATEGORIES.SWAP: {
|
|
|
|
return t('swap');
|
|
|
|
}
|
|
|
|
case TRANSACTION_CATEGORIES.SWAP_APPROVAL: {
|
|
|
|
return t('swapApproval');
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new Error(
|
|
|
|
`Unrecognized transaction category: ${transactionCategory}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|