Skip to content

Commit 18f7461

Browse files
committed
fix minor comments
1 parent 2b1dc54 commit 18f7461

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

packages/gas-fee-controller/src/GasFeeController.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ import { v1 as random } from 'uuid';
2525
import determineGasFeeCalculations from './determineGasFeeCalculations';
2626
import fetchGasEstimatesViaEthFeeHistory from './fetchGasEstimatesViaEthFeeHistory';
2727
import {
28+
buildInfuraAuthToken,
29+
calculateTimeEstimate,
2830
fetchGasEstimates,
2931
fetchLegacyGasPriceEstimates,
3032
fetchEthGasPriceEstimate,
31-
calculateTimeEstimate,
3233
} from './gas-util';
3334

3435
export const GAS_API_BASE_URL = 'https://gas.api.infura.io';
@@ -352,9 +353,10 @@ export class GasFeeController extends StaticIntervalPollingController<
352353
this.EIP1559APIEndpoint = `${GAS_API_BASE_URL}/networks/<chain_id>/suggestedGasFees`;
353354
this.legacyAPIEndpoint = `${GAS_API_BASE_URL}/networks/<chain_id>/gasPrices`;
354355
this.clientId = clientId;
355-
this.infuraAuthToken = Buffer.from(
356-
`${infuraAPIKey}:${infuraAPIKeySecret}`,
357-
).toString('base64');
356+
this.infuraAuthToken = buildInfuraAuthToken(
357+
infuraAPIKey,
358+
infuraAPIKeySecret,
359+
);
358360

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

packages/gas-fee-controller/src/gas-util.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ describe('gas utils', () => {
9191

9292
expect(handleFetchMock).toHaveBeenCalledTimes(1);
9393
expect(handleFetchMock).toHaveBeenCalledWith(INFURA_GAS_API_URL_MOCK, {
94-
headers: {
94+
headers: expect.objectContaining({
9595
Authorization: `Basic ${INFURA_AUTH_TOKEN_MOCK}`,
96-
},
96+
}),
9797
});
9898
expect(result).toMatchObject(mockEIP1559ApiResponses[0]);
9999
});
@@ -109,10 +109,10 @@ describe('gas utils', () => {
109109

110110
expect(handleFetchMock).toHaveBeenCalledTimes(1);
111111
expect(handleFetchMock).toHaveBeenCalledWith(INFURA_GAS_API_URL_MOCK, {
112-
headers: {
112+
headers: expect.objectContaining({
113113
Authorization: `Basic ${INFURA_AUTH_TOKEN_MOCK}`,
114114
'X-Client-Id': clientIdMock,
115-
},
115+
}),
116116
});
117117
expect(result).toMatchObject(mockEIP1559ApiResponses[0]);
118118
});

packages/gas-fee-controller/src/gas-util.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ export async function fetchGasEstimates(
4343
clientId?: string,
4444
): Promise<GasFeeEstimates> {
4545
const estimates = await handleFetch(url, {
46-
headers: {
47-
Authorization: `Basic ${infuraAuthToken}`,
48-
// Only add the clientId header if clientId is a non-empty string
49-
...(clientId?.trim() ? makeClientIdHeader(clientId) : {}),
50-
},
46+
headers: getHeaders(infuraAuthToken, clientId),
5147
});
5248
return {
5349
low: {
@@ -106,12 +102,7 @@ export async function fetchLegacyGasPriceEstimates(
106102
referrerPolicy: 'no-referrer-when-downgrade',
107103
method: 'GET',
108104
mode: 'cors',
109-
headers: {
110-
'Content-Type': 'application/json',
111-
Authorization: `Basic ${infuraAuthToken}`,
112-
// Only add the clientId header if clientId is a non-empty string
113-
...(clientId?.trim() ? makeClientIdHeader(clientId) : {}),
114-
},
105+
headers: getHeaders(infuraAuthToken, clientId),
115106
});
116107
return {
117108
low: result.SafeGasPrice,
@@ -200,3 +191,35 @@ export function calculateTimeEstimate(
200191
upperTimeBound,
201192
};
202193
}
194+
195+
/**
196+
* Build an infura auth token from the given API key and secret.
197+
*
198+
* @param infuraAPIKey - The Infura API key.
199+
* @param infuraAPIKeySecret - The Infura API key secret.
200+
* @returns The base64 encoded auth token.
201+
*/
202+
export function buildInfuraAuthToken(
203+
infuraAPIKey: string,
204+
infuraAPIKeySecret: string,
205+
) {
206+
return Buffer.from(`${infuraAPIKey}:${infuraAPIKeySecret}`).toString(
207+
'base64',
208+
);
209+
}
210+
211+
/**
212+
* Get the headers for a request to the gas fee API.
213+
*
214+
* @param infuraAuthToken - The Infura auth token to use for the request.
215+
* @param clientId - The client ID used to identify to the API who is asking for estimates.
216+
* @returns The headers for the request.
217+
*/
218+
function getHeaders(infuraAuthToken: string, clientId?: string) {
219+
return {
220+
'Content-Type': 'application/json',
221+
Authorization: `Basic ${infuraAuthToken}`,
222+
// Only add the clientId header if clientId is a non-empty string
223+
...(clientId?.trim() ? makeClientIdHeader(clientId) : {}),
224+
};
225+
}

0 commit comments

Comments
 (0)