Skip to content

Commit

Permalink
fix: use hardcoded Infura gas API urls (#4068)
Browse files Browse the repository at this point in the history
## Explanation

<!--
Thanks for your contribution! Take a moment to answer these questions so
that reviewers have the information they need to properly understand
your changes:

* What is the current state of things and why does it need to change?
* What is the solution your changes offer and how does it work?
* Are there any changes whose purpose might not obvious to those
unfamiliar with the domain?
* If your primary goal was to update one package but you found you had
to update another one along the way, why did you do so?
* If you had to upgrade a dependency, why did you do so?
-->

`legacyAPIEndpoint` and `EIP1559APIEndpoint` options are the gas API
urls used in `GasFeeController`.
This PR aims to remove those and put the new Infura URL as hardcoded. 

New Infura API also expects an auth header on requests, hence this PR is
adding new options `infuraAPIKey` to constructor.

## References

<!--
Are there any issues that this pull request is tied to? Are there other
links that reviewers should consult to understand these changes better?

For example:

* Fixes #12345
* Related to #67890
-->

Fixes: MetaMask/MetaMask-planning#2254

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/gas-fee-controller`

- **BREAKING**: Removed the constructor options `legacyAPIEndpoint` and
`EIP1559APIEndpoint`. These URLs are now hardcoded within the
controller.
- **BREAKING**: The controller's constructor now requires
`infuraAPIKey`. This is used to construct and send the Authorization
header for Infura gas API requests.

## Checklist

- [X] I've updated the test suite for new or updated code as appropriate
- [X] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [X] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
  • Loading branch information
OGPoyraz authored Mar 27, 2024
1 parent add19e7 commit 850461d
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 119 deletions.
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),
infuraAPIKey: 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),
infuraAPIKey: 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),
infuraAPIKey: 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),
infuraAPIKey: 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),
infuraAPIKey: 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

0 comments on commit 850461d

Please sign in to comment.