2021-02-04 19:15:23 +01:00
|
|
|
import { MethodRegistry } from 'eth-method-registry';
|
|
|
|
import log from 'loglevel';
|
2021-03-04 19:16:33 +01:00
|
|
|
|
2021-04-28 21:53:59 +02:00
|
|
|
import { addHexPrefix } from '../../../app/scripts/lib/util';
|
2018-07-31 07:03:20 +02:00
|
|
|
import {
|
2023-01-18 15:47:29 +01:00
|
|
|
TransactionType,
|
|
|
|
TransactionGroupStatus,
|
|
|
|
TransactionStatus,
|
|
|
|
TransactionEnvelopeType,
|
2021-04-28 21:53:59 +02:00
|
|
|
} from '../../../shared/constants/transaction';
|
2021-10-20 17:12:07 +02:00
|
|
|
import { readAddressAsContract } from '../../../shared/modules/contract-utils';
|
2022-09-16 21:05:21 +02:00
|
|
|
import fetchWithCache from '../../../shared/lib/fetch-with-cache';
|
2020-08-18 21:18:25 +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.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
);
|
2022-06-17 23:11:09 +02:00
|
|
|
fourByteResponse.results.sort((a, b) => {
|
|
|
|
return new Date(a.created_at).getTime() < new Date(b.created_at).getTime()
|
|
|
|
? -1
|
|
|
|
: 1;
|
|
|
|
});
|
|
|
|
return fourByteResponse.results[0].text_signature;
|
|
|
|
}
|
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
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2019-06-18 14:17:14 +02:00
|
|
|
* @param {string} fourBytePrefix - The prefix from the method code associated with the data
|
2022-07-27 15:28:05 +02:00
|
|
|
* @returns {object}
|
2018-10-24 17:27:16 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export async function getMethodDataAsync(fourBytePrefix) {
|
2019-06-18 14:17:14 +02:00
|
|
|
try {
|
2022-06-17 23:11:09 +02:00
|
|
|
const fourByteSig = await 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
|
|
|
}
|
|
|
|
|
2022-07-26 19:01:14 +02:00
|
|
|
if (!fourByteSig) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return {};
|
2019-06-18 14:17:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-26 19:01:14 +02:00
|
|
|
const parsedResult = registry.parse(fourByteSig);
|
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
|
|
|
|
*
|
2021-03-10 21:16:44 +01:00
|
|
|
* @param {TRANSACTION_TYPES[keyof TRANSACTION_TYPES]} type - The type 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
|
|
|
*/
|
2021-03-10 21:16:44 +01:00
|
|
|
export function isTokenMethodAction(type) {
|
2019-06-28 05:53:12 +02:00
|
|
|
return [
|
2023-01-18 15:47:29 +01:00
|
|
|
TransactionType.tokenMethodTransfer,
|
|
|
|
TransactionType.tokenMethodApprove,
|
|
|
|
TransactionType.tokenMethodSetApprovalForAll,
|
|
|
|
TransactionType.tokenMethodTransferFrom,
|
|
|
|
TransactionType.tokenMethodSafeTransferFrom,
|
2021-03-10 21:16:44 +01:00
|
|
|
].includes(type);
|
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) {
|
2022-08-03 21:35:34 +02:00
|
|
|
const { isContractAddress } = await readAddressAsContract(
|
|
|
|
global.eth,
|
|
|
|
address,
|
|
|
|
);
|
|
|
|
return isContractAddress;
|
2018-08-24 01:44:38 +02:00
|
|
|
}
|
2018-08-31 21:36:07 +02:00
|
|
|
|
2021-10-06 20:29:57 +02:00
|
|
|
export function isLegacyTransaction(txParams) {
|
2023-01-18 15:47:29 +01:00
|
|
|
return txParams?.type === TransactionEnvelopeType.legacy;
|
2021-10-06 20:29:57 +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.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} transaction - The txMeta object of a transaction.
|
|
|
|
* @param {object} transaction.txReceipt - The transaction receipt.
|
2018-10-16 00:00:47 +02:00
|
|
|
* @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') {
|
2023-01-18 15:47:29 +01:00
|
|
|
return TransactionStatus.failed;
|
2018-10-16 00:00:47 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
2023-01-18 15:47:29 +01:00
|
|
|
status === TransactionStatus.confirmed &&
|
|
|
|
type === TransactionType.cancel
|
2020-11-03 00:41:28 +01:00
|
|
|
) {
|
2023-01-18 15:47:29 +01:00
|
|
|
return TransactionGroupStatus.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
|
|
|
|
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.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
|
|
|
* @param {Function} t - The translation function
|
2021-03-10 21:16:44 +01:00
|
|
|
* @param {TRANSACTION_TYPES[keyof TRANSACTION_TYPES]} type - The transaction type constant
|
2021-09-15 23:54:51 +02:00
|
|
|
* @param {string} nativeCurrency - The native currency of the currently selected network
|
2021-02-08 17:06:58 +01:00
|
|
|
* @returns {string} The transaction category title
|
|
|
|
*/
|
2021-09-15 23:54:51 +02:00
|
|
|
export function getTransactionTypeTitle(t, type, nativeCurrency = 'ETH') {
|
2021-03-10 21:16:44 +01:00
|
|
|
switch (type) {
|
2023-01-18 15:47:29 +01:00
|
|
|
case TransactionType.tokenMethodTransfer: {
|
2021-02-08 17:06:58 +01:00
|
|
|
return t('transfer');
|
|
|
|
}
|
2023-01-18 15:47:29 +01:00
|
|
|
case TransactionType.tokenMethodTransferFrom: {
|
2021-02-08 18:30:27 +01:00
|
|
|
return t('transferFrom');
|
2021-02-08 17:06:58 +01:00
|
|
|
}
|
2023-01-18 15:47:29 +01:00
|
|
|
case TransactionType.tokenMethodSafeTransferFrom: {
|
2022-03-17 19:35:40 +01:00
|
|
|
return t('safeTransferFrom');
|
|
|
|
}
|
2023-01-18 15:47:29 +01:00
|
|
|
case TransactionType.tokenMethodApprove: {
|
2021-02-08 17:06:58 +01:00
|
|
|
return t('approve');
|
|
|
|
}
|
2023-01-18 15:47:29 +01:00
|
|
|
case TransactionType.tokenMethodSetApprovalForAll: {
|
2022-07-12 01:32:55 +02:00
|
|
|
return t('setApprovalForAll');
|
|
|
|
}
|
2023-01-18 15:47:29 +01:00
|
|
|
case TransactionType.simpleSend: {
|
2021-09-15 23:54:51 +02:00
|
|
|
return t('sendingNativeAsset', [nativeCurrency]);
|
2021-02-08 17:06:58 +01:00
|
|
|
}
|
2023-01-18 15:47:29 +01:00
|
|
|
case TransactionType.contractInteraction: {
|
2021-02-08 17:06:58 +01:00
|
|
|
return t('contractInteraction');
|
|
|
|
}
|
2023-01-18 15:47:29 +01:00
|
|
|
case TransactionType.deployContract: {
|
2021-02-08 17:06:58 +01:00
|
|
|
return t('contractDeployment');
|
|
|
|
}
|
2023-01-18 15:47:29 +01:00
|
|
|
case TransactionType.swap: {
|
2021-02-08 17:06:58 +01:00
|
|
|
return t('swap');
|
|
|
|
}
|
2023-01-18 15:47:29 +01:00
|
|
|
case TransactionType.swapApproval: {
|
2021-02-08 17:06:58 +01:00
|
|
|
return t('swapApproval');
|
|
|
|
}
|
|
|
|
default: {
|
2021-03-10 21:16:44 +01:00
|
|
|
throw new Error(`Unrecognized transaction type: ${type}`);
|
2021-02-08 17:06:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|