Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/transaction-controller/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 81.29,
functions: 94.49,
lines: 95.24,
statements: 95.34,
branches: 84.02,
functions: 92.68,
lines: 95.35,
statements: 95.46,
},
},

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { v1 as random } from 'uuid';

import { CHAIN_IDS } from './constants';
import type {
EtherscanTokenTransactionMeta,
EtherscanTransactionMeta,
Expand Down Expand Up @@ -63,7 +64,6 @@ const ETHERSCAN_TOKEN_TRANSACTION_MOCK: EtherscanTokenTransactionMeta = {

const ETHERSCAN_TRANSACTION_RESPONSE_MOCK: EtherscanTransactionResponse<EtherscanTransactionMeta> =
{
status: '1',
result: [
ETHERSCAN_TRANSACTION_SUCCESS_MOCK,
ETHERSCAN_TRANSACTION_ERROR_MOCK,
Expand All @@ -72,7 +72,6 @@ const ETHERSCAN_TRANSACTION_RESPONSE_MOCK: EtherscanTransactionResponse<Ethersca

const ETHERSCAN_TOKEN_TRANSACTION_RESPONSE_MOCK: EtherscanTransactionResponse<EtherscanTokenTransactionMeta> =
{
status: '1',
result: [
ETHERSCAN_TOKEN_TRANSACTION_MOCK,
ETHERSCAN_TOKEN_TRANSACTION_MOCK,
Expand All @@ -81,7 +80,6 @@ const ETHERSCAN_TOKEN_TRANSACTION_RESPONSE_MOCK: EtherscanTransactionResponse<Et

const ETHERSCAN_TRANSACTION_RESPONSE_EMPTY_MOCK: EtherscanTransactionResponse<EtherscanTransactionMeta> =
{
status: '0',
result: [],
};

Expand Down Expand Up @@ -159,6 +157,26 @@ describe('EtherscanRemoteTransactionSource', () => {
randomMock.mockReturnValue(ID_MOCK);
});

describe('isSupportedNetwork', () => {
it('returns true if chain ID in constant', () => {
expect(
new EtherscanRemoteTransactionSource().isSupportedNetwork(
CHAIN_IDS.MAINNET,
'1',
),
).toBe(true);
});

it('returns false if chain ID not in constant', () => {
expect(
new EtherscanRemoteTransactionSource().isSupportedNetwork(
'0x1324567891234',
'1',
),
).toBe(false);
});
});

describe('fetchTransactions', () => {
it('returns normalized transactions fetched from Etherscan', async () => {
fetchEtherscanTransactionsMock.mockResolvedValueOnce(
Expand Down Expand Up @@ -191,5 +209,17 @@ describe('EtherscanRemoteTransactionSource', () => {
EXPECTED_NORMALISED_TOKEN_TRANSACTION,
]);
});

it('returns no normalized token transactions if flag disabled', async () => {
fetchEtherscanTokenTransactionsMock.mockResolvedValueOnce(
ETHERSCAN_TOKEN_TRANSACTION_RESPONSE_MOCK,
);

const transactions = await new EtherscanRemoteTransactionSource({
includeTokenTransfers: false,
}).fetchTransactions({} as any);

expect(transactions).toStrictEqual([]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import type { Hex } from '@metamask/utils';
import { BN } from 'ethereumjs-util';
import { v1 as random } from 'uuid';

import { ETHERSCAN_SUPPORTED_NETWORKS } from './constants';
import type {
EtherscanTokenTransactionMeta,
EtherscanTransactionMeta,
EtherscanTransactionMetaBase,
EtherscanTransactionRequest,
EtherscanTransactionResponse,
} from './etherscan';
import {
fetchEtherscanTokenTransactions,
Expand All @@ -25,20 +28,41 @@ import { TransactionStatus } from './types';
export class EtherscanRemoteTransactionSource
implements RemoteTransactionSource
{
/**
* Retrieve transaction data from Etherscan.
*
* @param request - The configuration required to fetch Etherscan transaction data.
* @returns An array of transaction metadata.
*/
#apiKey?: string;

#includeTokenTransfers: boolean;

constructor({
apiKey,
includeTokenTransfers,
}: { apiKey?: string; includeTokenTransfers?: boolean } = {}) {
this.#apiKey = apiKey;
this.#includeTokenTransfers = includeTokenTransfers ?? true;
}

isSupportedNetwork(chainId: Hex, _networkId: string): boolean {
return Object.keys(ETHERSCAN_SUPPORTED_NETWORKS).includes(chainId);
}

async fetchTransactions(
request: RemoteTransactionSourceRequest,
): Promise<TransactionMeta[]> {
const etherscanRequest: EtherscanTransactionRequest = {
...request,
apiKey: this.#apiKey,
chainId: request.currentChainId,
};

const transactionPromise = fetchEtherscanTransactions(etherscanRequest);

const tokenTransactionPromise = this.#includeTokenTransfers
? fetchEtherscanTokenTransactions(etherscanRequest)
: Promise.resolve({
result: [] as EtherscanTokenTransactionMeta[],
} as EtherscanTransactionResponse<EtherscanTokenTransactionMeta>);

const [etherscanTransactions, etherscanTokenTransactions] =
await Promise.all([
fetchEtherscanTransactions(request),
fetchEtherscanTokenTransactions(request),
]);
await Promise.all([transactionPromise, tokenTransactionPromise]);

const transactions = etherscanTransactions.result.map((tx) =>
this.#normalizeTransaction(
Expand Down
Loading