From e04e2f78af094e0caad19116c0872b0649be0187 Mon Sep 17 00:00:00 2001 From: David Murdoch <187813+davidmurdoch@users.noreply.github.com> Date: Wed, 19 Feb 2025 15:18:19 -0500 Subject: [PATCH] refactor: remove circular dependency between `actions.ts` and `swaps.util.ts` (#30306) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR solves an issue with circular dependencies. It does not change the behavior of the application. It only rearranges functions/files to avoid circular references. --- development/circular-deps.jsonc | 4 ---- ui/pages/swaps/swaps.util.gas.ts | 15 +++++++++++++++ ui/pages/swaps/swaps.util.test.js | 16 ++++++++-------- ui/pages/swaps/swaps.util.ts | 2 +- ui/store/actions.ts | 9 --------- 5 files changed, 24 insertions(+), 22 deletions(-) create mode 100644 ui/pages/swaps/swaps.util.gas.ts diff --git a/development/circular-deps.jsonc b/development/circular-deps.jsonc index 1fa13db041a0..d36302a5e26d 100644 --- a/development/circular-deps.jsonc +++ b/development/circular-deps.jsonc @@ -58,9 +58,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.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 d081e8d58ee1..0973577638de 100644 --- a/ui/pages/swaps/swaps.util.test.js +++ b/ui/pages/swaps/swaps.util.test.js @@ -18,13 +18,6 @@ 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 { fetchTokens, fetchAggregatorMetadata, @@ -39,13 +32,20 @@ import { fetchTopAssetsList, getSwap1559GasFeeEstimates, } from './swaps.util'; +import { estimateGasFee } from './swaps.util.gas'; +import { + TOKENS, + EXPECTED_TOKENS_RESULT, + AGGREGATOR_METADATA, + TOP_ASSETS, +} from './swaps-util-test-constants'; jest.mock('../../../shared/lib/storage-helpers', () => ({ getStorageItem: jest.fn(), 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 7beb613bce58..0ccb2dcc99f7 100644 --- a/ui/pages/swaps/swaps.util.ts +++ b/ui/pages/swaps/swaps.util.ts @@ -50,7 +50,7 @@ import { sumHexes, } from '../../../shared/modules/conversion.utils'; import { EtherDenomination } from '../../../shared/constants/common'; -import { estimateGasFee } from '../../store/actions'; +import { estimateGasFee } from './swaps.util.gas'; const CACHE_REFRESH_FIVE_MINUTES = 300000; const USD_CURRENCY_CODE = 'usd'; diff --git a/ui/store/actions.ts b/ui/store/actions.ts index deb1c10650e2..033cf0185161 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 {