Skip to content

chore: remove cacheOptions from bridge-controller fetch requests #5953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions packages/bridge-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Pass abortSignal to spot-prices fetch call, which cancels exchange rate fetching when quote parameters change ([#5953](https://github.com/MetaMask/core/pull/5953))

### Changed

- Remove cacheOptions from fetch calls that don't need to be cached ([#5953](https://github.com/MetaMask/core/pull/5953))
- Bump `@metamask/controller-utils` to `^11.10.0` ([#5935](https://github.com/MetaMask/core/pull/5935))

## [32.1.1]
Expand Down
1 change: 1 addition & 0 deletions packages/bridge-controller/src/bridge-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ export class BridgeController extends StaticIntervalPollingController<BridgePoll
currencies: new Set([currency]),
clientId: this.#clientId,
fetchFn: this.#fetchFn,
signal: this.#abortController?.signal,
});
const exchangeRates = toExchangeRates(currency, pricesByAssetId);
this.update((state) => {
Expand Down
15 changes: 4 additions & 11 deletions packages/bridge-controller/src/utils/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ describe('fetch', () => {
expect(mockFetchFn).toHaveBeenCalledWith(
'https://bridge.api.cx.metamask.io/getQuote?walletAddress=0x388C818CA8B9251b393131C08a736A67ccB19297&destWalletAddress=0x388C818CA8B9251b393131C08a736A67ccB19297&srcChainId=1&destChainId=10&srcTokenAddress=0x0000000000000000000000000000000000000000&destTokenAddress=0x0000000000000000000000000000000000000000&srcTokenAmount=20000&insufficientBal=false&resetApproval=false&slippage=0.5',
{
cacheOptions: {
cacheRefreshTime: 0,
},
functionName: 'fetchBridgeQuotes',
headers: { 'X-Client-Id': 'extension' },
signal,
Expand Down Expand Up @@ -212,9 +209,6 @@ describe('fetch', () => {
expect(mockFetchFn).toHaveBeenCalledWith(
'https://bridge.api.cx.metamask.io/getQuote?walletAddress=0x388C818CA8B9251b393131C08a736A67ccB19297&destWalletAddress=0x388C818CA8B9251b393131C08a736A67ccB19297&srcChainId=1&destChainId=10&srcTokenAddress=0x0000000000000000000000000000000000000000&destTokenAddress=0x0000000000000000000000000000000000000000&srcTokenAmount=20000&insufficientBal=false&resetApproval=false&slippage=0.5',
{
cacheOptions: {
cacheRefreshTime: 0,
},
functionName: 'fetchBridgeQuotes',
headers: { 'X-Client-Id': 'extension' },
signal,
Expand Down Expand Up @@ -275,9 +269,6 @@ describe('fetch', () => {
expect(mockFetchFn).toHaveBeenCalledWith(
'https://bridge.api.cx.metamask.io/getQuote?walletAddress=0x388C818CA8B9251b393131C08a736A67ccB19297&destWalletAddress=0x388C818CA8B9251b393131C08a736A67ccB19297&srcChainId=1&destChainId=10&srcTokenAddress=0x0000000000000000000000000000000000000000&destTokenAddress=0x0000000000000000000000000000000000000000&srcTokenAmount=20000&insufficientBal=false&resetApproval=false&slippage=0.5',
{
cacheOptions: {
cacheRefreshTime: 0,
},
functionName: 'fetchBridgeQuotes',
headers: { 'X-Client-Id': 'extension' },
signal,
Expand All @@ -296,6 +287,7 @@ describe('fetch', () => {
});

it('should fetch and combine prices for multiple currencies successfully', async () => {
const abortController = new AbortController();
mockFetchFn
.mockResolvedValueOnce({
'eip155:1/erc20:0x123': { USD: '1.5' },
Expand All @@ -315,6 +307,7 @@ describe('fetch', () => {
baseUrl: 'https://api.example.com',
fetchFn: mockFetchFn,
clientId: 'test',
signal: abortController.signal,
assetIds: new Set([
'eip155:1/erc20:0x123',
'eip155:1/erc20:0x456',
Expand All @@ -340,15 +333,15 @@ describe('fetch', () => {
'https://price.api.cx.metamask.io/v3/spot-prices?assetIds=eip155%3A1%2Ferc20%3A0x123%2Ceip155%3A1%2Ferc20%3A0x456&vsCurrency=USD',
{
headers: { 'X-Client-Id': 'test' },
cacheOptions: { cacheRefreshTime: 30000 },
signal: abortController.signal,
functionName: 'fetchAssetExchangeRates',
},
);
expect(mockFetchFn).toHaveBeenCalledWith(
'https://price.api.cx.metamask.io/v3/spot-prices?assetIds=eip155%3A1%2Ferc20%3A0x123%2Ceip155%3A1%2Ferc20%3A0x456&vsCurrency=EUR',
{
headers: { 'X-Client-Id': 'test' },
cacheOptions: { cacheRefreshTime: 30000 },
signal: abortController.signal,
functionName: 'fetchAssetExchangeRates',
},
);
Expand Down
6 changes: 3 additions & 3 deletions packages/bridge-controller/src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export async function fetchBridgeQuotes(
const quotes: unknown[] = await fetchFn(url, {
headers: getClientIdHeader(clientId),
signal,
cacheOptions: { cacheRefreshTime: 0 },
functionName: 'fetchBridgeQuotes',
});

Expand Down Expand Up @@ -136,8 +135,9 @@ const fetchAssetPricesForCurrency = async (request: {
assetIds: Set<CaipAssetType>;
clientId: string;
fetchFn: FetchFunction;
signal?: AbortSignal;
}): Promise<Record<CaipAssetType, { [currency: string]: string }>> => {
const { currency, assetIds, clientId, fetchFn } = request;
const { currency, assetIds, clientId, fetchFn, signal } = request;
const validAssetIds = Array.from(assetIds).filter(Boolean);
if (validAssetIds.length === 0) {
return {};
Expand All @@ -150,7 +150,7 @@ const fetchAssetPricesForCurrency = async (request: {
const url = `https://price.api.cx.metamask.io/v3/spot-prices?${queryParams}`;
const priceApiResponse = (await fetchFn(url, {
headers: getClientIdHeader(clientId),
cacheOptions: { cacheRefreshTime: Number(Duration.Second * 30) },
signal,
functionName: 'fetchAssetExchangeRates',
})) as Record<CaipAssetType, { [currency: string]: number }>;

Expand Down
Loading