Skip to content
Prev Previous commit
Next Next commit
Pass APIKey instead of authToken in gas-utils
  • Loading branch information
OGPoyraz committed Mar 26, 2024
commit cd6bb24d1fb5e5215ff46f85b3e820c1064a910e
10 changes: 5 additions & 5 deletions packages/gas-fee-controller/src/GasFeeController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ describe('GasFeeController', () => {
calculateTimeEstimate,
clientId: '99999',
ethQuery: expect.any(EthQuery),
infuraAuthToken: expect.any(String),
infuraAPIKey: expect.any(String),
});
});

Expand Down Expand Up @@ -400,7 +400,7 @@ describe('GasFeeController', () => {
calculateTimeEstimate,
clientId: '99999',
ethQuery: expect.any(EthQuery),
infuraAuthToken: expect.any(String),
infuraAPIKey: expect.any(String),
});
});

Expand Down Expand Up @@ -707,7 +707,7 @@ describe('GasFeeController', () => {
calculateTimeEstimate,
clientId: '99999',
ethQuery: expect.any(EthQuery),
infuraAuthToken: expect.any(String),
infuraAPIKey: expect.any(String),
});
});

Expand Down Expand Up @@ -815,7 +815,7 @@ describe('GasFeeController', () => {
calculateTimeEstimate,
clientId: '99999',
ethQuery: expect.any(EthQuery),
infuraAuthToken: expect.any(String),
infuraAPIKey: expect.any(String),
});
});

Expand Down Expand Up @@ -912,7 +912,7 @@ describe('GasFeeController', () => {
calculateTimeEstimate,
clientId: '99999',
ethQuery: expect.any(EthQuery),
infuraAuthToken: expect.any(String),
infuraAPIKey: expect.any(String),
});
});

Expand Down
7 changes: 3 additions & 4 deletions packages/gas-fee-controller/src/GasFeeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { v1 as random } from 'uuid';
import determineGasFeeCalculations from './determineGasFeeCalculations';
import fetchGasEstimatesViaEthFeeHistory from './fetchGasEstimatesViaEthFeeHistory';
import {
buildInfuraAuthToken,
calculateTimeEstimate,
fetchGasEstimates,
fetchLegacyGasPriceEstimates,
Expand Down Expand Up @@ -275,7 +274,7 @@ export class GasFeeController extends StaticIntervalPollingController<

private readonly getCurrentAccountEIP1559Compatibility;

private readonly infuraAuthToken: string;
private readonly infuraAPIKey: string;

private currentChainId;

Expand Down Expand Up @@ -350,7 +349,7 @@ 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 = buildInfuraAuthToken(infuraAPIKey);
this.infuraAPIKey = infuraAPIKey;

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

Expand Down Expand Up @@ -473,7 +472,7 @@ export class GasFeeController extends StaticIntervalPollingController<
calculateTimeEstimate,
clientId: this.clientId,
ethQuery,
infuraAuthToken: this.infuraAuthToken,
infuraAPIKey: this.infuraAPIKey,
});

if (shouldUpdateState) {
Expand Down
14 changes: 7 additions & 7 deletions packages/gas-fee-controller/src/determineGasFeeCalculations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { GAS_ESTIMATE_TYPES } from './GasFeeController';
* @param args.calculateTimeEstimate - A function that determine time estimate bounds.
* @param args.clientId - An identifier that an API can use to know who is asking for estimates.
* @param args.ethQuery - An EthQuery instance we can use to talk to Ethereum directly.
* @param args.infuraAuthToken - Infura auth token to use for the requests.
* @param args.infuraAPIKey - Infura API key to use for requests to Infura.
* @returns The gas fee calculations.
*/
export default async function determineGasFeeCalculations({
Expand All @@ -46,13 +46,13 @@ export default async function determineGasFeeCalculations({
calculateTimeEstimate,
clientId,
ethQuery,
infuraAuthToken,
infuraAPIKey,
}: {
isEIP1559Compatible: boolean;
isLegacyGasAPICompatible: boolean;
fetchGasEstimates: (
url: string,
infuraAuthToken: string,
infuraAPIKey: string,
clientId?: string,
) => Promise<GasFeeEstimates>;
fetchGasEstimatesUrl: string;
Expand All @@ -63,7 +63,7 @@ export default async function determineGasFeeCalculations({
) => Promise<GasFeeEstimates>;
fetchLegacyGasPriceEstimates: (
url: string,
infuraAuthToken: string,
infuraAPIKey: string,
clientId?: string,
) => Promise<LegacyGasPriceEstimate>;
fetchLegacyGasPriceEstimatesUrl: string;
Expand All @@ -79,15 +79,15 @@ export default async function determineGasFeeCalculations({
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ethQuery: any;
infuraAuthToken: string;
infuraAPIKey: string;
}): Promise<GasFeeCalculations> {
try {
if (isEIP1559Compatible) {
let estimates: GasFeeEstimates;
try {
estimates = await fetchGasEstimates(
fetchGasEstimatesUrl,
infuraAuthToken,
infuraAPIKey,
clientId,
);
} catch {
Expand All @@ -108,7 +108,7 @@ export default async function determineGasFeeCalculations({
} else if (isLegacyGasAPICompatible) {
const estimates = await fetchLegacyGasPriceEstimates(
fetchLegacyGasPriceEstimatesUrl,
infuraAuthToken,
infuraAPIKey,
clientId,
);
return {
Expand Down
13 changes: 7 additions & 6 deletions packages/gas-fee-controller/src/gas-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ const mockEIP1559ApiResponses: GasFeeEstimates[] = [
},
];

const INFURA_AUTH_TOKEN_MOCK = 'test';
const INFURA_API_KEY_MOCK = 'test';
const INFURA_AUTH_TOKEN_MOCK = 'dGVzdDo=';
const INFURA_GAS_API_URL_MOCK = 'https://gas.api.infura.io';

describe('gas utils', () => {
Expand All @@ -86,7 +87,7 @@ describe('gas utils', () => {
handleFetchMock.mockResolvedValue(mockEIP1559ApiResponses[0]);
const result = await fetchGasEstimates(
INFURA_GAS_API_URL_MOCK,
INFURA_AUTH_TOKEN_MOCK,
INFURA_API_KEY_MOCK,
);

expect(handleFetchMock).toHaveBeenCalledTimes(1);
Expand All @@ -103,7 +104,7 @@ describe('gas utils', () => {
handleFetchMock.mockResolvedValue(mockEIP1559ApiResponses[0]);
const result = await fetchGasEstimates(
INFURA_GAS_API_URL_MOCK,
INFURA_AUTH_TOKEN_MOCK,
INFURA_API_KEY_MOCK,
clientIdMock,
);

Expand Down Expand Up @@ -143,7 +144,7 @@ describe('gas utils', () => {
handleFetchMock.mockResolvedValue(mockEIP1559ApiResponses[1]);
const result = await fetchGasEstimates(
INFURA_GAS_API_URL_MOCK,
INFURA_AUTH_TOKEN_MOCK,
INFURA_API_KEY_MOCK,
);
expect(result).toMatchObject(expectedResult);
});
Expand All @@ -158,7 +159,7 @@ describe('gas utils', () => {
});
const result = await fetchLegacyGasPriceEstimates(
INFURA_GAS_API_URL_MOCK,
INFURA_AUTH_TOKEN_MOCK,
INFURA_API_KEY_MOCK,
);

expect(handleFetchMock).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -187,7 +188,7 @@ describe('gas utils', () => {
});
const result = await fetchLegacyGasPriceEstimates(
INFURA_GAS_API_URL_MOCK,
INFURA_AUTH_TOKEN_MOCK,
INFURA_API_KEY_MOCK,
clientIdMock,
);

Expand Down
12 changes: 7 additions & 5 deletions packages/gas-fee-controller/src/gas-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ export function normalizeGWEIDecimalNumbers(n: string | number) {
* Fetch gas estimates from the given URL.
*
* @param url - The gas estimate URL.
* @param infuraAuthToken - The infura auth token to use for the request.
* @param infuraAPIKey - The Infura API key used for infura API requests.
* @param clientId - The client ID used to identify to the API who is asking for estimates.
* @returns The gas estimates.
*/
export async function fetchGasEstimates(
url: string,
infuraAuthToken: string,
infuraAPIKey: string,
clientId?: string,
): Promise<GasFeeEstimates> {
const infuraAuthToken = buildInfuraAuthToken(infuraAPIKey);
const estimates = await handleFetch(url, {
headers: getHeaders(infuraAuthToken, clientId),
});
Expand Down Expand Up @@ -88,15 +89,16 @@ export async function fetchGasEstimates(
* high values from that API.
*
* @param url - The URL to fetch gas price estimates from.
* @param infuraAuthToken - The infura auth token to use for the request.
* @param infuraAPIKey - The Infura API key used for infura API requests.
* @param clientId - The client ID used to identify to the API who is asking for estimates.
* @returns The gas price estimates.
*/
export async function fetchLegacyGasPriceEstimates(
url: string,
infuraAuthToken: string,
infuraAPIKey: string,
clientId?: string,
): Promise<LegacyGasPriceEstimate> {
const infuraAuthToken = buildInfuraAuthToken(infuraAPIKey);
const result = await handleFetch(url, {
referrer: url,
referrerPolicy: 'no-referrer-when-downgrade',
Expand Down Expand Up @@ -198,7 +200,7 @@ export function calculateTimeEstimate(
* @param infuraAPIKey - The Infura API key.
* @returns The base64 encoded auth token.
*/
export function buildInfuraAuthToken(infuraAPIKey: string) {
function buildInfuraAuthToken(infuraAPIKey: string) {
// We intentionally leave the password empty, as Infura does not require one
return Buffer.from(`${infuraAPIKey}:`).toString('base64');
}
Expand Down