Skip to content
1 change: 0 additions & 1 deletion packages/gas-fee-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"deepmerge": "^4.2.2",
"jest": "^27.5.1",
"jest-when": "^3.4.2",
"nock": "^13.3.1",
"sinon": "^9.2.4",
"ts-jest": "^27.1.4",
"typedoc": "^0.24.8",
Expand Down
94 changes: 39 additions & 55 deletions packages/gas-fee-controller/src/GasFeeController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import {
fetchEthGasPriceEstimate,
calculateTimeEstimate,
} from './gas-util';
import { GAS_ESTIMATE_TYPES, GasFeeController } from './GasFeeController';
import {
GAS_API_BASE_URL,
GAS_ESTIMATE_TYPES,
GasFeeController,
} from './GasFeeController';
import type {
GasFeeState,
GasFeeStateChange,
Expand Down Expand Up @@ -218,21 +222,19 @@ describe('GasFeeController', () => {
* GasFeeController.
* @param options.getCurrentNetworkLegacyGasAPICompatibility - Sets
* getCurrentNetworkLegacyGasAPICompatibility on the GasFeeController.
* @param options.legacyAPIEndpoint - Sets legacyAPIEndpoint on the GasFeeController.
* @param options.EIP1559APIEndpoint - Sets EIP1559APIEndpoint on the GasFeeController.
* @param options.clientId - Sets clientId on the GasFeeController.
* @param options.networkControllerState - State object to initialize
* NetworkController with.
* @param options.interval - The polling interval.
* @param options.state - The initial GasFeeController state
* @param options.infuraAPIKey - The Infura API key.
*/
async function setupGasFeeController({
getIsEIP1559Compatible = jest.fn().mockResolvedValue(true),
getCurrentNetworkLegacyGasAPICompatibility = jest
.fn()
.mockReturnValue(false),
legacyAPIEndpoint = 'http://legacy.endpoint/<chain_id>',
EIP1559APIEndpoint = 'http://eip-1559.endpoint/<chain_id>',
infuraAPIKey = 'INFURA_API_KEY',
clientId,
getChainId,
networkControllerState = {},
Expand All @@ -242,12 +244,11 @@ describe('GasFeeController', () => {
getChainId?: jest.Mock<Hex>;
getIsEIP1559Compatible?: jest.Mock<Promise<boolean>>;
getCurrentNetworkLegacyGasAPICompatibility?: jest.Mock<boolean>;
legacyAPIEndpoint?: string;
EIP1559APIEndpoint?: string;
clientId?: string;
networkControllerState?: Partial<NetworkState>;
state?: GasFeeState;
interval?: number;
infuraAPIKey?: string;
} = {}) {
const controllerMessenger = getControllerMessenger();
networkController = await setupNetworkController({
Expand All @@ -262,11 +263,10 @@ describe('GasFeeController', () => {
messenger,
getCurrentNetworkLegacyGasAPICompatibility,
getCurrentNetworkEIP1559Compatibility: getIsEIP1559Compatible, // change this for networkDetails.state.networkDetails.isEIP1559Compatible ???
legacyAPIEndpoint,
EIP1559APIEndpoint,
state,
clientId,
interval,
infuraAPIKey,
});
}

Expand Down Expand Up @@ -319,8 +319,6 @@ describe('GasFeeController', () => {
getCurrentNetworkLegacyGasAPICompatibility: jest
.fn()
.mockReturnValue(true),
legacyAPIEndpoint: 'https://some-legacy-endpoint/<chain_id>',
EIP1559APIEndpoint: 'https://some-eip-1559-endpoint/<chain_id>',
networkControllerState: {
providerConfig: {
type: NetworkType.rpc,
Expand All @@ -338,15 +336,15 @@ describe('GasFeeController', () => {
isEIP1559Compatible: false,
isLegacyGasAPICompatible: true,
fetchGasEstimates,
fetchGasEstimatesUrl: 'https://some-eip-1559-endpoint/1337',
fetchGasEstimatesUrl: `${GAS_API_BASE_URL}/networks/1337/suggestedGasFees`,
fetchGasEstimatesViaEthFeeHistory,
fetchLegacyGasPriceEstimates,
fetchLegacyGasPriceEstimatesUrl:
'https://some-legacy-endpoint/1337',
fetchLegacyGasPriceEstimatesUrl: `${GAS_API_BASE_URL}/networks/1337/gasPrices`,
fetchEthGasPriceEstimate,
calculateTimeEstimate,
clientId: '99999',
ethQuery: expect.any(EthQuery),
infuraAuthToken: expect.any(String),
});
});

Expand Down Expand Up @@ -375,8 +373,6 @@ describe('GasFeeController', () => {
getCurrentNetworkLegacyGasAPICompatibility: jest
.fn()
.mockReturnValue(true),
legacyAPIEndpoint: 'https://some-legacy-endpoint/<chain_id>',
EIP1559APIEndpoint: 'https://some-eip-1559-endpoint/<chain_id>',
networkControllerState: {
providerConfig: {
type: NetworkType.rpc,
Expand All @@ -396,15 +392,15 @@ describe('GasFeeController', () => {
isEIP1559Compatible: false,
isLegacyGasAPICompatible: true,
fetchGasEstimates,
fetchGasEstimatesUrl: 'https://some-eip-1559-endpoint/1337',
fetchGasEstimatesUrl: `${GAS_API_BASE_URL}/networks/1337/suggestedGasFees`,
fetchGasEstimatesViaEthFeeHistory,
fetchLegacyGasPriceEstimates,
fetchLegacyGasPriceEstimatesUrl:
'https://some-legacy-endpoint/1337',
fetchLegacyGasPriceEstimatesUrl: `${GAS_API_BASE_URL}/networks/1337/gasPrices`,
fetchEthGasPriceEstimate,
calculateTimeEstimate,
clientId: '99999',
ethQuery: expect.any(EthQuery),
infuraAuthToken: expect.any(String),
});
});

Expand Down Expand Up @@ -686,8 +682,6 @@ describe('GasFeeController', () => {
it('should call determineGasFeeCalculations correctly', async () => {
await setupGasFeeController({
...defaultConstructorOptions,
legacyAPIEndpoint: 'https://some-legacy-endpoint/<chain_id>',
EIP1559APIEndpoint: 'https://some-eip-1559-endpoint/<chain_id>',
networkControllerState: {
providerConfig: {
type: NetworkType.rpc,
Expand All @@ -705,14 +699,15 @@ describe('GasFeeController', () => {
isEIP1559Compatible: false,
isLegacyGasAPICompatible: true,
fetchGasEstimates,
fetchGasEstimatesUrl: 'https://some-eip-1559-endpoint/1337',
fetchGasEstimatesUrl: `${GAS_API_BASE_URL}/networks/1337/suggestedGasFees`,
fetchGasEstimatesViaEthFeeHistory,
fetchLegacyGasPriceEstimates,
fetchLegacyGasPriceEstimatesUrl: 'https://some-legacy-endpoint/1337',
fetchLegacyGasPriceEstimatesUrl: `${GAS_API_BASE_URL}/networks/1337/gasPrices`,
fetchEthGasPriceEstimate,
calculateTimeEstimate,
clientId: '99999',
ethQuery: expect.any(EthQuery),
infuraAuthToken: expect.any(String),
});
});

Expand All @@ -737,47 +732,44 @@ describe('GasFeeController', () => {
it('should call determineGasFeeCalculations correctly when getChainId returns a number input', async () => {
await setupGasFeeController({
...defaultConstructorOptions,
legacyAPIEndpoint: 'http://legacy.endpoint/<chain_id>',
getChainId: jest.fn().mockReturnValue(1),
});

await gasFeeController._fetchGasFeeEstimateData();

expect(mockedDetermineGasFeeCalculations).toHaveBeenCalledWith(
expect.objectContaining({
fetchLegacyGasPriceEstimatesUrl: 'http://legacy.endpoint/1',
fetchLegacyGasPriceEstimatesUrl: `${GAS_API_BASE_URL}/networks/1/gasPrices`,
}),
);
});

it('should call determineGasFeeCalculations correctly when getChainId returns a hexstring input', async () => {
await setupGasFeeController({
...defaultConstructorOptions,
legacyAPIEndpoint: 'http://legacy.endpoint/<chain_id>',
getChainId: jest.fn().mockReturnValue('0x1'),
});

await gasFeeController.fetchGasFeeEstimates();

expect(mockedDetermineGasFeeCalculations).toHaveBeenCalledWith(
expect.objectContaining({
fetchLegacyGasPriceEstimatesUrl: 'http://legacy.endpoint/1',
fetchLegacyGasPriceEstimatesUrl: `${GAS_API_BASE_URL}/networks/1/gasPrices`,
}),
);
});

it('should call determineGasFeeCalculations correctly when getChainId returns a numeric string input', async () => {
await setupGasFeeController({
...defaultConstructorOptions,
legacyAPIEndpoint: 'http://legacy.endpoint/<chain_id>',
getChainId: jest.fn().mockReturnValue('1'),
});

await gasFeeController.fetchGasFeeEstimates();

expect(mockedDetermineGasFeeCalculations).toHaveBeenCalledWith(
expect.objectContaining({
fetchLegacyGasPriceEstimatesUrl: 'http://legacy.endpoint/1',
fetchLegacyGasPriceEstimatesUrl: `${GAS_API_BASE_URL}/networks/1/gasPrices`,
}),
);
});
Expand All @@ -798,8 +790,6 @@ describe('GasFeeController', () => {
it('should call determineGasFeeCalculations correctly', async () => {
await setupGasFeeController({
...defaultConstructorOptions,
legacyAPIEndpoint: 'https://some-legacy-endpoint/<chain_id>',
EIP1559APIEndpoint: 'https://some-eip-1559-endpoint/<chain_id>',
networkControllerState: {
providerConfig: {
type: NetworkType.rpc,
Expand All @@ -817,14 +807,15 @@ describe('GasFeeController', () => {
isEIP1559Compatible: true,
isLegacyGasAPICompatible: false,
fetchGasEstimates,
fetchGasEstimatesUrl: 'https://some-eip-1559-endpoint/1337',
fetchGasEstimatesUrl: `${GAS_API_BASE_URL}/networks/1337/suggestedGasFees`,
fetchGasEstimatesViaEthFeeHistory,
fetchLegacyGasPriceEstimates,
fetchLegacyGasPriceEstimatesUrl: 'https://some-legacy-endpoint/1337',
fetchLegacyGasPriceEstimatesUrl: `${GAS_API_BASE_URL}/networks/1337/gasPrices`,
fetchEthGasPriceEstimate,
calculateTimeEstimate,
clientId: '99999',
ethQuery: expect.any(EthQuery),
infuraAuthToken: expect.any(String),
});
});

Expand All @@ -849,15 +840,14 @@ describe('GasFeeController', () => {
it('should call determineGasFeeCalculations with a URL that contains the chain ID', async () => {
await setupGasFeeController({
...defaultConstructorOptions,
EIP1559APIEndpoint: 'http://eip-1559.endpoint/<chain_id>',
getChainId: jest.fn().mockReturnValue('0x1'),
});

await gasFeeController.fetchGasFeeEstimates();

expect(mockedDetermineGasFeeCalculations).toHaveBeenCalledWith(
expect.objectContaining({
fetchGasEstimatesUrl: 'http://eip-1559.endpoint/1',
fetchGasEstimatesUrl: `${GAS_API_BASE_URL}/networks/1/suggestedGasFees`,
}),
);
});
Expand Down Expand Up @@ -899,8 +889,6 @@ describe('GasFeeController', () => {
it('should call determineGasFeeCalculations correctly', async () => {
await setupGasFeeController({
...defaultConstructorOptions,
legacyAPIEndpoint: 'https://some-legacy-endpoint/<chain_id>',
EIP1559APIEndpoint: 'https://some-eip-1559-endpoint/<chain_id>',
clientId: '99999',
});

Expand All @@ -912,16 +900,19 @@ describe('GasFeeController', () => {
isEIP1559Compatible: true,
isLegacyGasAPICompatible: false,
fetchGasEstimates,
fetchGasEstimatesUrl: 'https://some-eip-1559-endpoint/5',
fetchGasEstimatesUrl: `${GAS_API_BASE_URL}/networks/${convertHexToDecimal(
ChainId.goerli,
)}/suggestedGasFees`,
fetchGasEstimatesViaEthFeeHistory,
fetchLegacyGasPriceEstimates,
fetchLegacyGasPriceEstimatesUrl: `https://some-legacy-endpoint/${convertHexToDecimal(
fetchLegacyGasPriceEstimatesUrl: `${GAS_API_BASE_URL}/networks/${convertHexToDecimal(
ChainId.goerli,
)}`,
)}/gasPrices`,
fetchEthGasPriceEstimate,
calculateTimeEstimate,
clientId: '99999',
ethQuery: expect.any(EthQuery),
infuraAuthToken: expect.any(String),
});
});

Expand All @@ -931,10 +922,6 @@ describe('GasFeeController', () => {
await gasFeeController.fetchGasFeeEstimates({
networkClientId: 'goerli',
});
console.log(
'gasFeeController.state.gasFeeEstimatesByChainId: ',
gasFeeController.state.gasFeeEstimatesByChainId,
);

expect(
gasFeeController.state.gasFeeEstimatesByChainId?.[ChainId.goerli],
Expand All @@ -954,7 +941,6 @@ describe('GasFeeController', () => {
it('should call determineGasFeeCalculations with a URL that contains the chain ID', async () => {
await setupGasFeeController({
...defaultConstructorOptions,
EIP1559APIEndpoint: 'http://eip-1559.endpoint/<chain_id>',
});

await gasFeeController.fetchGasFeeEstimates({
Expand All @@ -963,9 +949,9 @@ describe('GasFeeController', () => {

expect(mockedDetermineGasFeeCalculations).toHaveBeenCalledWith(
expect.objectContaining({
fetchGasEstimatesUrl: `http://eip-1559.endpoint/${convertHexToDecimal(
fetchGasEstimatesUrl: `${GAS_API_BASE_URL}/networks/${convertHexToDecimal(
ChainId.sepolia,
)}`,
)}/suggestedGasFees`,
}),
);
});
Expand All @@ -980,8 +966,6 @@ describe('GasFeeController', () => {
getCurrentNetworkLegacyGasAPICompatibility: jest
.fn()
.mockReturnValue(true),
legacyAPIEndpoint: 'https://some-legacy-endpoint/<chain_id>',
EIP1559APIEndpoint: 'https://some-eip-1559-endpoint/<chain_id>',
networkControllerState: {
networksMetadata: {
goerli: {
Expand All @@ -1007,9 +991,9 @@ describe('GasFeeController', () => {
expect(mockedDetermineGasFeeCalculations).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
fetchGasEstimatesUrl: `https://some-eip-1559-endpoint/${convertHexToDecimal(
fetchGasEstimatesUrl: `${GAS_API_BASE_URL}/networks/${convertHexToDecimal(
ChainId.goerli,
)}`,
)}/suggestedGasFees`,
}),
);
await clock.tickAsync(pollingInterval / 2);
Expand All @@ -1018,9 +1002,9 @@ describe('GasFeeController', () => {
expect(mockedDetermineGasFeeCalculations).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
fetchGasEstimatesUrl: `https://some-eip-1559-endpoint/${convertHexToDecimal(
fetchGasEstimatesUrl: `${GAS_API_BASE_URL}/networks/${convertHexToDecimal(
ChainId.goerli,
)}`,
)}/suggestedGasFees`,
}),
);
expect(
Expand All @@ -1031,9 +1015,9 @@ describe('GasFeeController', () => {
await clock.tickAsync(pollingInterval);
expect(mockedDetermineGasFeeCalculations).toHaveBeenCalledWith(
expect.objectContaining({
fetchGasEstimatesUrl: `https://some-eip-1559-endpoint/${convertHexToDecimal(
fetchGasEstimatesUrl: `${GAS_API_BASE_URL}/networks/${convertHexToDecimal(
ChainId.sepolia,
)}`,
)}/suggestedGasFees`,
}),
);
});
Expand Down
Loading