Skip to content
Prev Previous commit
Next Next commit
fix minor comments
  • Loading branch information
OGPoyraz committed Mar 26, 2024
commit 831fa3ad5b0a63c2d2fde637d9f7bec80d853ec6
10 changes: 6 additions & 4 deletions packages/gas-fee-controller/src/GasFeeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import { v1 as random } from 'uuid';
import determineGasFeeCalculations from './determineGasFeeCalculations';
import fetchGasEstimatesViaEthFeeHistory from './fetchGasEstimatesViaEthFeeHistory';
import {
buildInfuraAuthToken,
calculateTimeEstimate,
fetchGasEstimates,
fetchLegacyGasPriceEstimates,
fetchEthGasPriceEstimate,
calculateTimeEstimate,
} from './gas-util';

export const GAS_API_BASE_URL = 'https://gas.api.infura.io';
Expand Down Expand Up @@ -352,9 +353,10 @@ export class GasFeeController extends StaticIntervalPollingController<
this.EIP1559APIEndpoint = `${GAS_API_BASE_URL}/networks/<chain_id>/suggestedGasFees`;
this.legacyAPIEndpoint = `${GAS_API_BASE_URL}/networks/<chain_id>/gasPrices`;
this.clientId = clientId;
this.infuraAuthToken = Buffer.from(
`${infuraAPIKey}:${infuraAPIKeySecret}`,
).toString('base64');
this.infuraAuthToken = buildInfuraAuthToken(
infuraAPIKey,
infuraAPIKeySecret,
);

this.ethQuery = new EthQuery(this.#getProvider());

Expand Down
8 changes: 4 additions & 4 deletions packages/gas-fee-controller/src/gas-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ describe('gas utils', () => {

expect(handleFetchMock).toHaveBeenCalledTimes(1);
expect(handleFetchMock).toHaveBeenCalledWith(INFURA_GAS_API_URL_MOCK, {
headers: {
headers: expect.objectContaining({
Authorization: `Basic ${INFURA_AUTH_TOKEN_MOCK}`,
},
}),
});
expect(result).toMatchObject(mockEIP1559ApiResponses[0]);
});
Expand All @@ -109,10 +109,10 @@ describe('gas utils', () => {

expect(handleFetchMock).toHaveBeenCalledTimes(1);
expect(handleFetchMock).toHaveBeenCalledWith(INFURA_GAS_API_URL_MOCK, {
headers: {
headers: expect.objectContaining({
Authorization: `Basic ${INFURA_AUTH_TOKEN_MOCK}`,
'X-Client-Id': clientIdMock,
},
}),
});
expect(result).toMatchObject(mockEIP1559ApiResponses[0]);
});
Expand Down
45 changes: 34 additions & 11 deletions packages/gas-fee-controller/src/gas-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ export async function fetchGasEstimates(
clientId?: string,
): Promise<GasFeeEstimates> {
const estimates = await handleFetch(url, {
headers: {
Authorization: `Basic ${infuraAuthToken}`,
// Only add the clientId header if clientId is a non-empty string
...(clientId?.trim() ? makeClientIdHeader(clientId) : {}),
},
headers: getHeaders(infuraAuthToken, clientId),
});
return {
low: {
Expand Down Expand Up @@ -106,12 +102,7 @@ export async function fetchLegacyGasPriceEstimates(
referrerPolicy: 'no-referrer-when-downgrade',
method: 'GET',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${infuraAuthToken}`,
// Only add the clientId header if clientId is a non-empty string
...(clientId?.trim() ? makeClientIdHeader(clientId) : {}),
},
headers: getHeaders(infuraAuthToken, clientId),
});
return {
low: result.SafeGasPrice,
Expand Down Expand Up @@ -200,3 +191,35 @@ export function calculateTimeEstimate(
upperTimeBound,
};
}

/**
* Build an infura auth token from the given API key and secret.
*
* @param infuraAPIKey - The Infura API key.
* @param infuraAPIKeySecret - The Infura API key secret.
* @returns The base64 encoded auth token.
*/
export function buildInfuraAuthToken(
infuraAPIKey: string,
infuraAPIKeySecret: string,
) {
return Buffer.from(`${infuraAPIKey}:${infuraAPIKeySecret}`).toString(
'base64',
);
}

/**
* Get the headers for a request to the gas fee API.
*
* @param infuraAuthToken - The Infura auth token to use for the request.
* @param clientId - The client ID used to identify to the API who is asking for estimates.
* @returns The headers for the request.
*/
function getHeaders(infuraAuthToken: string, clientId?: string) {
return {
'Content-Type': 'application/json',
Authorization: `Basic ${infuraAuthToken}`,
// Only add the clientId header if clientId is a non-empty string
...(clientId?.trim() ? makeClientIdHeader(clientId) : {}),
};
}