Skip to content
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

chore: limit bridge quote request frequency and cancel requests #27237

Merged
merged 10 commits into from
Nov 12, 2024
1 change: 1 addition & 0 deletions app/scripts/constants/sentry-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const SENTRY_BACKGROUND_STATE = {
quotes: [],
quotesLastFetched: true,
quotesLoadingStatus: true,
quotesRefreshCount: true,
},
},
CronjobController: {
Expand Down
180 changes: 162 additions & 18 deletions app/scripts/controllers/bridge/bridge-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { flushPromises } from '../../../../test/lib/timer-helpers';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import * as bridgeUtil from '../../../../ui/pages/bridge/bridge.util';
import * as balanceUtils from '../../../../shared/modules/bridge-utils/balance';
import BridgeController from './bridge-controller';
import { BridgeControllerMessenger } from './types';
import { DEFAULT_BRIDGE_CONTROLLER_STATE } from './constants';
Expand Down Expand Up @@ -37,7 +38,7 @@ describe('BridgeController', function () {
.reply(200, {
'extension-config': {
refreshRate: 3,
maxRefreshCount: 1,
maxRefreshCount: 3,
},
'extension-support': true,
'src-network-allowlist': [10, 534352],
Expand Down Expand Up @@ -78,7 +79,7 @@ describe('BridgeController', function () {
destNetworkAllowlist: [CHAIN_IDS.POLYGON, CHAIN_IDS.ARBITRUM],
srcNetworkAllowlist: [CHAIN_IDS.OPTIMISM, CHAIN_IDS.SCROLL],
extensionConfig: {
maxRefreshCount: 1,
maxRefreshCount: 3,
refreshRate: 3,
},
};
Expand Down Expand Up @@ -236,7 +237,13 @@ describe('BridgeController', function () {
bridgeController,
'startPollingByNetworkClientId',
);
messengerMock.call.mockReturnValue({ address: '0x123' } as never);
const hasSufficientBalanceSpy = jest
.spyOn(balanceUtils, 'hasSufficientBalance')
.mockResolvedValue(true);
messengerMock.call.mockReturnValue({
address: '0x123',
provider: jest.fn(),
} as never);

const fetchBridgeQuotesSpy = jest
.spyOn(bridgeUtil, 'fetchBridgeQuotes')
Expand Down Expand Up @@ -276,13 +283,17 @@ describe('BridgeController', function () {
slippage: 0.5,
walletAddress: '0x123',
};
bridgeController.updateBridgeQuoteRequestParams(quoteParams);
await bridgeController.updateBridgeQuoteRequestParams(quoteParams);

expect(stopAllPollingSpy).toHaveBeenCalledTimes(1);
expect(startPollingByNetworkClientIdSpy).toHaveBeenCalledTimes(1);
expect(hasSufficientBalanceSpy).toHaveBeenCalledTimes(1);
expect(startPollingByNetworkClientIdSpy).toHaveBeenCalledWith(
'1',
quoteRequest,
expect.anything(),
{
...quoteRequest,
insufficientBal: false,
},
);

expect(bridgeController.state.bridgeState).toStrictEqual(
Expand All @@ -299,14 +310,20 @@ describe('BridgeController', function () {
jest.advanceTimersByTime(1000);
await flushPromises();
expect(fetchBridgeQuotesSpy).toHaveBeenCalledTimes(1);
expect(fetchBridgeQuotesSpy).toHaveBeenCalledWith(quoteRequest);
expect(fetchBridgeQuotesSpy).toHaveBeenCalledWith(
{
...quoteRequest,
insufficientBal: false,
},
expect.any(AbortSignal),
);
expect(bridgeController.state.bridgeState.quotesLastFetched).toStrictEqual(
undefined,
);

const firstFetchTime =
bridgeController.state.bridgeState.quotesLastFetched ?? 0;
expect(firstFetchTime).toBeGreaterThan(0);
expect(bridgeController.state.bridgeState).toEqual(
expect.objectContaining({
quoteRequest: { ...quoteRequest, walletAddress: undefined },
quoteRequest: { ...quoteRequest, insufficientBal: false },
quotes: [],
quotesLoadingStatus: 0,
}),
Expand All @@ -317,26 +334,27 @@ describe('BridgeController', function () {
await flushPromises();
expect(bridgeController.state.bridgeState).toEqual(
expect.objectContaining({
quoteRequest: { ...quoteRequest, walletAddress: undefined },
quoteRequest: { ...quoteRequest, insufficientBal: false },
quotes: [1, 2, 3],
quotesLoadingStatus: 1,
}),
);
expect(bridgeController.state.bridgeState.quotesLastFetched).toStrictEqual(
firstFetchTime,
);
const firstFetchTime =
bridgeController.state.bridgeState.quotesLastFetched ?? 0;
expect(firstFetchTime).toBeGreaterThan(0);

// After 2nd fetch
jest.advanceTimersByTime(50000);
await flushPromises();
expect(fetchBridgeQuotesSpy).toHaveBeenCalledTimes(2);
expect(bridgeController.state.bridgeState).toEqual(
expect.objectContaining({
quoteRequest: { ...quoteRequest, walletAddress: undefined },
quoteRequest: { ...quoteRequest, insufficientBal: false },
quotes: [5, 6, 7],
quotesLoadingStatus: 1,
quotesRefreshCount: 2,
}),
);
expect(fetchBridgeQuotesSpy).toHaveBeenCalledTimes(2);
const secondFetchTime =
bridgeController.state.bridgeState.quotesLastFetched;
expect(secondFetchTime).toBeGreaterThan(firstFetchTime);
Expand All @@ -347,14 +365,140 @@ describe('BridgeController', function () {
expect(fetchBridgeQuotesSpy).toHaveBeenCalledTimes(3);
expect(bridgeController.state.bridgeState).toEqual(
expect.objectContaining({
quoteRequest: { ...quoteRequest, walletAddress: undefined },
quoteRequest: { ...quoteRequest, insufficientBal: false },
quotes: [5, 6, 7],
quotesLoadingStatus: 2,
quotesRefreshCount: 3,
}),
);
expect(bridgeController.state.bridgeState.quotesLastFetched).toStrictEqual(
secondFetchTime,
);

expect(hasSufficientBalanceSpy).toHaveBeenCalledTimes(1);
});

it('updateBridgeQuoteRequestParams should only poll once if insufficientBal=true', async function () {
jest.useFakeTimers();
const stopAllPollingSpy = jest.spyOn(bridgeController, 'stopAllPolling');
const startPollingByNetworkClientIdSpy = jest.spyOn(
bridgeController,
'startPollingByNetworkClientId',
);
const hasSufficientBalanceSpy = jest
.spyOn(balanceUtils, 'hasSufficientBalance')
.mockResolvedValue(false);
messengerMock.call.mockReturnValue({
address: '0x123',
provider: jest.fn(),
} as never);

const fetchBridgeQuotesSpy = jest
.spyOn(bridgeUtil, 'fetchBridgeQuotes')
.mockImplementationOnce(async () => {
return await new Promise((resolve) => {
return setTimeout(() => {
resolve([1, 2, 3] as never);
}, 5000);
});
});

fetchBridgeQuotesSpy.mockImplementation(async () => {
return await new Promise((resolve) => {
return setTimeout(() => {
resolve([5, 6, 7] as never);
}, 10000);
});
});

const quoteParams = {
srcChainId: 1,
destChainId: 10,
srcTokenAddress: '0x0000000000000000000000000000000000000000',
destTokenAddress: '0x123',
srcTokenAmount: '1000000000000000000',
};
const quoteRequest = {
...quoteParams,
slippage: 0.5,
walletAddress: '0x123',
};
await bridgeController.updateBridgeQuoteRequestParams(quoteParams);

expect(stopAllPollingSpy).toHaveBeenCalledTimes(1);
expect(startPollingByNetworkClientIdSpy).toHaveBeenCalledTimes(1);
expect(hasSufficientBalanceSpy).toHaveBeenCalledTimes(1);
expect(startPollingByNetworkClientIdSpy).toHaveBeenCalledWith(
expect.anything(),
{
...quoteRequest,
insufficientBal: true,
},
);

expect(bridgeController.state.bridgeState).toStrictEqual(
expect.objectContaining({
quoteRequest: { ...quoteRequest, walletAddress: undefined },
quotes: DEFAULT_BRIDGE_CONTROLLER_STATE.quotes,
quotesLastFetched: DEFAULT_BRIDGE_CONTROLLER_STATE.quotesLastFetched,
quotesLoadingStatus:
DEFAULT_BRIDGE_CONTROLLER_STATE.quotesLoadingStatus,
}),
);

// Loading state
jest.advanceTimersByTime(1000);
await flushPromises();
expect(fetchBridgeQuotesSpy).toHaveBeenCalledTimes(1);
expect(fetchBridgeQuotesSpy).toHaveBeenCalledWith(
{
...quoteRequest,
insufficientBal: true,
},
expect.any(AbortSignal),
);
expect(bridgeController.state.bridgeState.quotesLastFetched).toStrictEqual(
undefined,
);

expect(bridgeController.state.bridgeState).toEqual(
expect.objectContaining({
quoteRequest: { ...quoteRequest, insufficientBal: true },
quotes: [],
quotesLoadingStatus: 0,
}),
);

// After first fetch
jest.advanceTimersByTime(10000);
await flushPromises();
expect(bridgeController.state.bridgeState).toEqual(
expect.objectContaining({
quoteRequest: { ...quoteRequest, insufficientBal: true },
quotes: [1, 2, 3],
quotesLoadingStatus: 1,
quotesRefreshCount: 1,
}),
);
const firstFetchTime =
bridgeController.state.bridgeState.quotesLastFetched ?? 0;
expect(firstFetchTime).toBeGreaterThan(0);

// After 2nd fetch
jest.advanceTimersByTime(50000);
await flushPromises();
expect(fetchBridgeQuotesSpy).toHaveBeenCalledTimes(1);
expect(bridgeController.state.bridgeState).toEqual(
expect.objectContaining({
quoteRequest: { ...quoteRequest, insufficientBal: true },
quotes: [1, 2, 3],
quotesLoadingStatus: 1,
quotesRefreshCount: 1,
}),
);
const secondFetchTime =
bridgeController.state.bridgeState.quotesLastFetched;
expect(secondFetchTime).toStrictEqual(firstFetchTime);
});

it('updateBridgeQuoteRequestParams should not trigger quote polling if request is invalid', function () {
Expand Down
Loading