From b117a110d357164df1068e245ee1376b75d1320c Mon Sep 17 00:00:00 2001 From: David Murdoch <187813+davidmurdoch@users.noreply.github.com> Date: Thu, 13 Feb 2025 15:34:46 -0500 Subject: [PATCH 1/2] refactor: remove circular dependency between `actions.ts` and `swaps.util.ts` --- development/circular-deps.jsonc | 4 ---- ui/pages/swaps/swaps.util.test.js | 14 +++++++------- ui/pages/swaps/swaps.util.ts | 11 ++++++++++- ui/store/actions.ts | 9 --------- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/development/circular-deps.jsonc b/development/circular-deps.jsonc index 18589944546e..2545505070b0 100644 --- a/development/circular-deps.jsonc +++ b/development/circular-deps.jsonc @@ -81,9 +81,5 @@ "ui/pages/snap-account-redirect/components/snap-account-redirect-message.tsx", "ui/pages/snap-account-redirect/components/url-display-box.tsx", "ui/pages/snap-account-redirect/snap-account-redirect.tsx" - ], - [ - "ui/pages/swaps/swaps.util.ts", - "ui/store/actions.ts" ] ] diff --git a/ui/pages/swaps/swaps.util.test.js b/ui/pages/swaps/swaps.util.test.js index d081e8d58ee1..07aada0e2773 100644 --- a/ui/pages/swaps/swaps.util.test.js +++ b/ui/pages/swaps/swaps.util.test.js @@ -18,14 +18,8 @@ import { LINEA, BASE, } from '../../../shared/constants/swaps'; -import { estimateGasFee } from '../../store/actions'; -import { - TOKENS, - EXPECTED_TOKENS_RESULT, - AGGREGATOR_METADATA, - TOP_ASSETS, -} from './swaps-util-test-constants'; import { + estimateGasFee, fetchTokens, fetchAggregatorMetadata, fetchTopAssets, @@ -39,6 +33,12 @@ import { fetchTopAssetsList, getSwap1559GasFeeEstimates, } from './swaps.util'; +import { + TOKENS, + EXPECTED_TOKENS_RESULT, + AGGREGATOR_METADATA, + TOP_ASSETS, +} from './swaps-util-test-constants'; jest.mock('../../../shared/lib/storage-helpers', () => ({ getStorageItem: jest.fn(), diff --git a/ui/pages/swaps/swaps.util.ts b/ui/pages/swaps/swaps.util.ts index 7beb613bce58..36799e0be7ae 100644 --- a/ui/pages/swaps/swaps.util.ts +++ b/ui/pages/swaps/swaps.util.ts @@ -3,8 +3,10 @@ import { Hex, Json } from '@metamask/utils'; import { IndividualTxFees } from '@metamask/smart-transactions-controller/dist/types'; import { FeeMarketGasFeeEstimates, + GasFeeEstimates, TransactionParams, } from '@metamask/transaction-controller'; +import { NetworkClientId } from '@metamask/network-controller'; import { ALLOWED_CONTRACT_ADDRESSES, ARBITRUM, @@ -50,7 +52,7 @@ import { sumHexes, } from '../../../shared/modules/conversion.utils'; import { EtherDenomination } from '../../../shared/constants/common'; -import { estimateGasFee } from '../../store/actions'; +import { submitRequestToBackground } from '../../store/background-connection'; const CACHE_REFRESH_FIVE_MINUTES = 300000; const USD_CURRENCY_CODE = 'usd'; @@ -851,3 +853,10 @@ export async function getTransaction1559GasFeeEstimates( maxPriorityFeePerGas, }; } +export function estimateGasFee(request: { + transactionParams: TransactionParams; + chainId?: Hex; + networkClientId?: NetworkClientId; +}): Promise<{ estimates: GasFeeEstimates }> { + return submitRequestToBackground('estimateGasFee', [request]); +} diff --git a/ui/store/actions.ts b/ui/store/actions.ts index 245341600e19..e0c410930be3 100644 --- a/ui/store/actions.ts +++ b/ui/store/actions.ts @@ -29,7 +29,6 @@ import { UpdateProposedNamesResult, } from '@metamask/name-controller'; import { - GasFeeEstimates, TransactionMeta, TransactionParams, TransactionType, @@ -4511,14 +4510,6 @@ export function estimateGas(params: TransactionParams): Promise { return submitRequestToBackground('estimateGas', [params]); } -export function estimateGasFee(request: { - transactionParams: TransactionParams; - chainId?: Hex; - networkClientId?: NetworkClientId; -}): Promise<{ estimates: GasFeeEstimates }> { - return submitRequestToBackground('estimateGasFee', [request]); -} - export async function updateTokenType( tokenAddress: string, ): Promise { From b4efac84a9f448a938ac22408697f742b2101eb8 Mon Sep 17 00:00:00 2001 From: David Murdoch <187813+davidmurdoch@users.noreply.github.com> Date: Fri, 14 Feb 2025 12:01:49 -0500 Subject: [PATCH 2/2] better mock --- ui/pages/swaps/swaps.util.gas.ts | 15 +++++++++++++++ ui/pages/swaps/swaps.util.test.js | 4 ++-- ui/pages/swaps/swaps.util.ts | 11 +---------- 3 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 ui/pages/swaps/swaps.util.gas.ts diff --git a/ui/pages/swaps/swaps.util.gas.ts b/ui/pages/swaps/swaps.util.gas.ts new file mode 100644 index 000000000000..bc81b547c82b --- /dev/null +++ b/ui/pages/swaps/swaps.util.gas.ts @@ -0,0 +1,15 @@ +import { NetworkClientId } from '@metamask/network-controller'; +import { + GasFeeEstimates, + TransactionParams, +} from '@metamask/transaction-controller'; +import { Hex } from '@metamask/utils'; +import { submitRequestToBackground } from '../../store/background-connection'; + +export function estimateGasFee(request: { + transactionParams: TransactionParams; + chainId?: Hex; + networkClientId?: NetworkClientId; +}): Promise<{ estimates: GasFeeEstimates }> { + return submitRequestToBackground('estimateGasFee', [request]); +} diff --git a/ui/pages/swaps/swaps.util.test.js b/ui/pages/swaps/swaps.util.test.js index 07aada0e2773..0973577638de 100644 --- a/ui/pages/swaps/swaps.util.test.js +++ b/ui/pages/swaps/swaps.util.test.js @@ -19,7 +19,6 @@ import { BASE, } from '../../../shared/constants/swaps'; import { - estimateGasFee, fetchTokens, fetchAggregatorMetadata, fetchTopAssets, @@ -33,6 +32,7 @@ import { fetchTopAssetsList, getSwap1559GasFeeEstimates, } from './swaps.util'; +import { estimateGasFee } from './swaps.util.gas'; import { TOKENS, EXPECTED_TOKENS_RESULT, @@ -45,7 +45,7 @@ jest.mock('../../../shared/lib/storage-helpers', () => ({ setStorageItem: jest.fn(), })); -jest.mock('../../store/actions', () => ({ +jest.mock('./swaps.util.gas', () => ({ estimateGasFee: jest.fn(), })); diff --git a/ui/pages/swaps/swaps.util.ts b/ui/pages/swaps/swaps.util.ts index 36799e0be7ae..0ccb2dcc99f7 100644 --- a/ui/pages/swaps/swaps.util.ts +++ b/ui/pages/swaps/swaps.util.ts @@ -3,10 +3,8 @@ import { Hex, Json } from '@metamask/utils'; import { IndividualTxFees } from '@metamask/smart-transactions-controller/dist/types'; import { FeeMarketGasFeeEstimates, - GasFeeEstimates, TransactionParams, } from '@metamask/transaction-controller'; -import { NetworkClientId } from '@metamask/network-controller'; import { ALLOWED_CONTRACT_ADDRESSES, ARBITRUM, @@ -52,7 +50,7 @@ import { sumHexes, } from '../../../shared/modules/conversion.utils'; import { EtherDenomination } from '../../../shared/constants/common'; -import { submitRequestToBackground } from '../../store/background-connection'; +import { estimateGasFee } from './swaps.util.gas'; const CACHE_REFRESH_FIVE_MINUTES = 300000; const USD_CURRENCY_CODE = 'usd'; @@ -853,10 +851,3 @@ export async function getTransaction1559GasFeeEstimates( maxPriorityFeePerGas, }; } -export function estimateGasFee(request: { - transactionParams: TransactionParams; - chainId?: Hex; - networkClientId?: NetworkClientId; -}): Promise<{ estimates: GasFeeEstimates }> { - return submitRequestToBackground('estimateGasFee', [request]); -}