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

Rename executePoll -> _executePoll + stopPollingByNetworkClientId -> stopPollingByPollingToken #1810

Merged
merged 5 commits into from
Oct 12, 2023
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
16 changes: 8 additions & 8 deletions packages/assets-controllers/src/TokenListController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1294,8 +1294,9 @@ describe('TokenListController', () => {
});
});

describe('executePoll', () => {
describe('startPollingByNetworkClient', () => {
it('should call fetchTokenListByChainId with the correct chainId', async () => {
jest.useFakeTimers();
nock(tokenService.TOKEN_END_POINT_API)
.get(`/tokens/${convertHexToDecimal(ChainId.sepolia)}`)
.reply(200, sampleSepoliaTokenList)
Expand All @@ -1315,28 +1316,27 @@ describe('TokenListController', () => {
},
}),
);
const pollingIntervalTime = 1000;
const messenger = getRestrictedMessenger(controllerMessenger);
const controller = new TokenListController({
chainId: ChainId.mainnet,
preventPollingOnNetworkRestart: false,
messenger,
state: expiredCacheExistingState,
interval: pollingIntervalTime,
});
expect(controller.state.tokenList).toStrictEqual(
expiredCacheExistingState.tokenList,
);

await controller.executePoll('sepolia');
controller.startPollingByNetworkClientId('sepolia');
jest.advanceTimersByTime(pollingIntervalTime);
await flushPromises();

expect(fetchTokenListByChainIdSpy.mock.calls[0]).toStrictEqual(
expect.arrayContaining([ChainId.sepolia]),
);
expect(controller.state.tokenList).toStrictEqual(
sampleSepoliaTokensChainCache,
);
});
});

describe('startPollingByNetworkClient', () => {
it('should start polling against the token list API at the interval passed to the constructor', async () => {
jest.useFakeTimers();
const pollingIntervalTime = 1000;
Expand Down
3 changes: 2 additions & 1 deletion packages/assets-controllers/src/TokenListController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,11 @@ export class TokenListController extends PollingController<
/**
* Fetching token list from the Token Service API.
*
* @private
* @param networkClientId - The ID of the network client triggering the fetch.
* @returns A promise that resolves when this operation completes.
mcmire marked this conversation as resolved.
Show resolved Hide resolved
*/
async executePoll(networkClientId: string): Promise<void> {
async _executePoll(networkClientId: string): Promise<void> {
return this.fetchTokenList(networkClientId);
}

Expand Down
73 changes: 24 additions & 49 deletions packages/gas-fee-controller/src/GasFeeController.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ControllerMessenger } from '@metamask/base-controller';
import { NetworkType, toHex } from '@metamask/controller-utils';
import {
ChainId,
convertHexToDecimal,
NetworkType,
toHex,
} from '@metamask/controller-utils';
import EthQuery from '@metamask/eth-query';
import { NetworkController, NetworkStatus } from '@metamask/network-controller';
import type {
Expand Down Expand Up @@ -868,8 +873,10 @@ describe('GasFeeController', () => {
});
});
});
describe('executePoll', () => {
it('should call determineGasFeeCalculations with a URL that contains the chain ID', async () => {

describe('polling (by networkClientId)', () => {
it('should call determineGasFeeCalculations (via _executePoll) with a URL that contains the chainId corresponding to the networkClientId after the interval passed via the constructor', async () => {
const pollingInterval = 10000;
await setupGasFeeController({
getIsEIP1559Compatible: jest.fn().mockResolvedValue(false),
getCurrentNetworkLegacyGasAPICompatibility: jest
Expand All @@ -879,7 +886,7 @@ describe('GasFeeController', () => {
EIP1559APIEndpoint: 'https://some-eip-1559-endpoint/<chain_id>',
networkControllerState: {
networksMetadata: {
mainnet: {
goerli: {
EIPS: {
1559: true,
},
Expand All @@ -894,50 +901,6 @@ describe('GasFeeController', () => {
},
},
clientId: '99999',
});

await gasFeeController.executePoll('mainnet');
await gasFeeController.executePoll('sepolia');

expect(mockedDetermineGasFeeCalculations).toHaveBeenCalledWith(
expect.objectContaining({
fetchGasEstimatesUrl: 'https://some-eip-1559-endpoint/1',
}),
);
expect(mockedDetermineGasFeeCalculations).toHaveBeenCalledWith(
expect.objectContaining({
fetchGasEstimatesUrl: 'https://some-eip-1559-endpoint/11155111',
}),
);
expect(mockedDetermineGasFeeCalculations).not.toHaveBeenCalledWith(
expect.objectContaining({
fetchGasEstimatesUrl: 'https://some-eip-1559-endpoint/5',
}),
);
});
});

describe('polling (by networkClientId)', () => {
it('should call determineGasFeeCalculations (via executePoll) with a URL that contains the chain ID after the interval passed via the constructor', async () => {
const pollingInterval = 10000;
await setupGasFeeController({
getIsEIP1559Compatible: jest.fn().mockResolvedValue(false),
getCurrentNetworkLegacyGasAPICompatibility: jest
.fn()
.mockReturnValue(true),
legacyAPIEndpoint: 'https://some-legacy-endpoint/<chain_id>',
EIP1559APIEndpoint: 'https://some-eip-1559-endpoint/<chain_id>',
networkControllerState: {
networksMetadata: {
goerli: {
EIPS: {
1559: true,
},
status: NetworkStatus.Available,
},
},
},
clientId: '99999',
interval: pollingInterval,
});

Expand All @@ -947,12 +910,24 @@ describe('GasFeeController', () => {
await clock.tickAsync(pollingInterval / 2);
expect(mockedDetermineGasFeeCalculations).toHaveBeenCalledWith(
expect.objectContaining({
fetchGasEstimatesUrl: 'https://some-eip-1559-endpoint/5',
fetchGasEstimatesUrl: `https://some-eip-1559-endpoint/${convertHexToDecimal(
ChainId.goerli,
)}`,
}),
);
expect(
gasFeeController.state.gasFeeEstimatesByChainId?.['0x5'],
).toStrictEqual(buildMockGasFeeStateFeeMarket());

gasFeeController.startPollingByNetworkClientId('sepolia');
await clock.tickAsync(pollingInterval);
expect(mockedDetermineGasFeeCalculations).toHaveBeenCalledWith(
expect.objectContaining({
fetchGasEstimatesUrl: `https://some-eip-1559-endpoint/${convertHexToDecimal(
ChainId.sepolia,
)}`,
}),
);
});
});
});
10 changes: 8 additions & 2 deletions packages/gas-fee-controller/src/GasFeeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ export class GasFeeController extends PollingController<
'NetworkController:getNetworkClientById',
networkClientId,
);

const isLegacyGasAPICompatible =
networkClient.configuration.chainId === '0x38';

Expand Down Expand Up @@ -548,7 +547,14 @@ export class GasFeeController extends PollingController<
}, this.intervalDelay);
}

async executePoll(networkClientId: string): Promise<void> {
/**
* Fetching token list from the Token Service API.
*
* @private
* @param networkClientId - The ID of the network client triggering the fetch.
* @returns A promise that resolves when this operation completes.
*/
async _executePoll(networkClientId: string): Promise<void> {
mcmire marked this conversation as resolved.
Show resolved Hide resolved
await this.#fetchGasFeeEstimateForNetworkClientId(networkClientId);
}

Expand Down
Loading
Loading