Skip to content

Commit 9fa253a

Browse files
committed
WIP start integrating abstract class into GasFeeController
1 parent 942a65f commit 9fa253a

File tree

2 files changed

+103
-8
lines changed

2 files changed

+103
-8
lines changed

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

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import type { RestrictedControllerMessenger } from '@metamask/base-controller';
22
import { BaseControllerV2 } from '@metamask/base-controller';
3-
import { convertHexToDecimal, safelyExecute } from '@metamask/controller-utils';
3+
import { ChainId, convertHexToDecimal, safelyExecute } from '@metamask/controller-utils';
44
import EthQuery from '@metamask/eth-query';
55
import type {
6+
NetworkClientId,
7+
NetworkController,
8+
NetworkControllerGetEIP1559CompatibilityAction,
9+
NetworkControllerGetNetworkClientByIdAction,
610
NetworkControllerGetStateAction,
711
NetworkControllerStateChangeEvent,
812
NetworkState,
@@ -20,6 +24,7 @@ import {
2024
fetchEthGasPriceEstimate,
2125
calculateTimeEstimate,
2226
} from './gas-util';
27+
import GasFeeControllerPolling from './GasFeeControllerPolling';
2328

2429
export const LEGACY_GAS_PRICES_API_URL = `https://api.metaswap.codefi.network/gasPrices`;
2530

@@ -150,6 +155,10 @@ type FallbackGasFeeEstimates = {
150155
};
151156

152157
const metadata = {
158+
gasFeeEstimatesByChainId: {
159+
persist: true,
160+
anonymous: false,
161+
},
153162
gasFeeEstimates: { persist: true, anonymous: false },
154163
estimatedGasFeeTimeBounds: { persist: true, anonymous: false },
155164
gasEstimateType: { persist: true, anonymous: false },
@@ -190,12 +199,18 @@ export type FetchGasFeeEstimateOptions = {
190199
* @property gasFeeEstimates - Gas fee estimate data based on new EIP-1559 properties
191200
* @property estimatedGasFeeTimeBounds - Estimates representing the minimum and maximum
192201
*/
193-
export type GasFeeState =
202+
export type GasFeeStateOld =
194203
| GasFeeStateEthGasPrice
195204
| GasFeeStateFeeMarket
196205
| GasFeeStateLegacy
197206
| GasFeeStateNoEstimates;
198207

208+
export type GasFeeEstimatesByChainId = {
209+
gasFeeEstimatesByChainId: Record<string, GasFeeStateOld>;
210+
};
211+
212+
export type GasFeeState = GasFeeEstimatesByChainId & GasFeeStateOld;
213+
199214
const name = 'GasFeeController';
200215

201216
export type GasFeeStateChange = {
@@ -210,13 +225,19 @@ export type GetGasFeeState = {
210225

211226
type GasFeeMessenger = RestrictedControllerMessenger<
212227
typeof name,
213-
GetGasFeeState | NetworkControllerGetStateAction,
228+
| GetGasFeeState
229+
| NetworkControllerGetStateAction
230+
| NetworkControllerGetNetworkClientByIdAction
231+
| NetworkControllerGetEIP1559CompatibilityAction,
214232
GasFeeStateChange | NetworkControllerStateChangeEvent,
215-
NetworkControllerGetStateAction['type'],
233+
| NetworkControllerGetStateAction['type']
234+
| NetworkControllerGetNetworkClientByIdAction['type']
235+
| NetworkControllerGetEIP1559CompatibilityAction['type'],
216236
NetworkControllerStateChangeEvent['type']
217237
>;
218238

219239
const defaultState: GasFeeState = {
240+
gasFeeEstimatesByChainId: {},
220241
gasFeeEstimates: {},
221242
estimatedGasFeeTimeBounds: {},
222243
gasEstimateType: GAS_ESTIMATE_TYPES.NONE,
@@ -225,7 +246,7 @@ const defaultState: GasFeeState = {
225246
/**
226247
* Controller that retrieves gas fee estimate data and polls for updated data on a set interval
227248
*/
228-
export class GasFeeController extends BaseControllerV2<
249+
export class GasFeeController extends GasFeeControllerPolling<
229250
typeof name,
230251
GasFeeState,
231252
GasFeeMessenger
@@ -373,6 +394,65 @@ export class GasFeeController extends BaseControllerV2<
373394
return _pollToken;
374395
}
375396

397+
async #fetchGasFeeEstimateForNetworkClientId(networkClientId: string) {
398+
let isEIP1559Compatible = false;
399+
400+
const networkClient = await this.messagingSystem.call(
401+
'NetworkController:getNetworkClientById',
402+
networkClientId,
403+
);
404+
405+
const isLegacyGasAPICompatible =
406+
networkClient.configuration.chainId === '0x38';
407+
408+
const decimalChainId = convertHexToDecimal(
409+
networkClient.configuration.chainId,
410+
);
411+
412+
try {
413+
const result = await this.messagingSystem.call(
414+
'NetworkController:getEIP1559Compatibility',
415+
networkClientId,
416+
);
417+
418+
isEIP1559Compatible = result || false;
419+
} catch (e) {
420+
console.error(e);
421+
isEIP1559Compatible = false;
422+
}
423+
424+
const ethQuery = new EthQuery(networkClient.provider);
425+
426+
const gasFeeCalculations = await determineGasFeeCalculations({
427+
isEIP1559Compatible,
428+
isLegacyGasAPICompatible,
429+
fetchGasEstimates,
430+
fetchGasEstimatesUrl: this.EIP1559APIEndpoint.replace(
431+
'<chain_id>',
432+
`${decimalChainId}`,
433+
),
434+
fetchGasEstimatesViaEthFeeHistory,
435+
fetchLegacyGasPriceEstimates,
436+
fetchLegacyGasPriceEstimatesUrl: this.legacyAPIEndpoint.replace(
437+
'<chain_id>',
438+
`${decimalChainId}`,
439+
),
440+
fetchEthGasPriceEstimate,
441+
calculateTimeEstimate,
442+
clientId: this.clientId,
443+
ethQuery,
444+
});
445+
446+
// update state per chainid?
447+
this.update((state) => {
448+
state.gasFeeEstimatesByChainId[networkClient.configuration.chainId] = {
449+
gasFeeEstimates: gasFeeCalculations.gasFeeEstimates,
450+
estimatedGasFeeTimeBounds: gasFeeCalculations.estimatedGasFeeTimeBounds,
451+
gasEstimateType: gasFeeCalculations.gasEstimateType,
452+
} as any;
453+
});
454+
}
455+
376456
/**
377457
* Gets and sets gasFeeEstimates in state.
378458
*
@@ -470,6 +550,10 @@ export class GasFeeController extends BaseControllerV2<
470550
}, this.intervalDelay);
471551
}
472552

553+
async executePoll(networkClientId: string): Promise<void> {
554+
await this.#fetchGasFeeEstimateForNetworkClientId(networkClientId);
555+
}
556+
473557
private resetState() {
474558
this.update(() => {
475559
return defaultState;

packages/network-controller/src/NetworkController.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,17 @@ export type NetworkControllerGetNetworkClientByIdAction = {
458458
handler: NetworkController['getNetworkClientById'];
459459
};
460460

461+
export type NetworkControllerGetEIP1559CompatibilityAction = {
462+
type: `NetworkController:getEIP1559Compatibility`;
463+
handler: NetworkController['getEIP1559Compatibility'];
464+
};
465+
461466
export type NetworkControllerActions =
462467
| NetworkControllerGetStateAction
463468
| NetworkControllerGetProviderConfigAction
464469
| NetworkControllerGetEthQueryAction
465-
| NetworkControllerGetNetworkClientByIdAction;
470+
| NetworkControllerGetNetworkClientByIdAction
471+
| NetworkControllerGetEIP1559CompatibilityAction;
466472

467473
export type NetworkControllerMessenger = RestrictedControllerMessenger<
468474
typeof name,
@@ -612,6 +618,11 @@ export class NetworkController extends BaseControllerV2<
612618
this.getNetworkClientById.bind(this),
613619
);
614620

621+
this.messagingSystem.registerActionHandler(
622+
`${this.name}:getEIP1559Compatibility`,
623+
this.getEIP1559Compatibility.bind(this),
624+
);
625+
615626
this.#previousProviderConfig = this.state.providerConfig;
616627
}
617628

@@ -1072,7 +1083,7 @@ export class NetworkController extends BaseControllerV2<
10721083
*/
10731084
async getEIP1559Compatibility(networkClientId?: NetworkClientId) {
10741085
if (networkClientId) {
1075-
return this.get1555CompatibilityWithNetworkClientId(networkClientId);
1086+
return this.get1559CompatibilityWithNetworkClientId(networkClientId);
10761087
}
10771088
if (!this.#ethQuery) {
10781089
return false;
@@ -1097,7 +1108,7 @@ export class NetworkController extends BaseControllerV2<
10971108
return isEIP1559Compatible;
10981109
}
10991110

1100-
async get1555CompatibilityWithNetworkClientId(
1111+
async get1559CompatibilityWithNetworkClientId(
11011112
networkClientId: NetworkClientId,
11021113
) {
11031114
let metadata = this.state.networksMetadata[networkClientId];

0 commit comments

Comments
 (0)