diff --git a/packages/network-controller/src/NetworkController.test.ts b/packages/network-controller/src/NetworkController.test.ts index 9354580212f..25f762e2634 100644 --- a/packages/network-controller/src/NetworkController.test.ts +++ b/packages/network-controller/src/NetworkController.test.ts @@ -3908,7 +3908,7 @@ async function setFakeProvider( lookupNetworkMock.mockResolvedValue(undefined); } if (stubGetEIP1559CompatibilityWhileSetting) { - lookupGetEIP1559CompatibilityMock.mockResolvedValue(undefined); + lookupGetEIP1559CompatibilityMock.mockResolvedValue(false); } controller.providerConfig = buildProviderConfig(); diff --git a/packages/network-controller/src/NetworkController.ts b/packages/network-controller/src/NetworkController.ts index 42c7de3002e..2980ee1b07d 100644 --- a/packages/network-controller/src/NetworkController.ts +++ b/packages/network-controller/src/NetworkController.ts @@ -400,35 +400,40 @@ export class NetworkController extends BaseControllerV2< this.refreshNetwork(); } - getEIP1559Compatibility() { + #getLatestBlock(): Promise { + return new Promise((resolve, reject) => { + this.ethQuery.sendAsync( + { method: 'eth_getBlockByNumber', params: ['latest', false] }, + (error: Error, block: Block) => { + if (error) { + reject(error); + } else { + resolve(block); + } + }, + ); + }); + } + + async getEIP1559Compatibility() { const { networkDetails = {} } = this.state; - if (!networkDetails.isEIP1559Compatible) { - if (typeof this.ethQuery?.sendAsync !== 'function') { - return Promise.resolve(true); - } - return new Promise((resolve, reject) => { - this.ethQuery.sendAsync( - { method: 'eth_getBlockByNumber', params: ['latest', false] }, - (error: Error, block: Block) => { - if (error) { - reject(error); - } else { - const isEIP1559Compatible = - typeof block.baseFeePerGas !== 'undefined'; - if (networkDetails.isEIP1559Compatible !== isEIP1559Compatible) { - this.update((state) => { - state.networkDetails.isEIP1559Compatible = - isEIP1559Compatible; - }); - } - resolve(isEIP1559Compatible); - } - }, - ); + if ( + networkDetails.isEIP1559Compatible || + typeof this.ethQuery?.sendAsync !== 'function' + ) { + return true; + } + + const latestBlock = await this.#getLatestBlock(); + const isEIP1559Compatible = + typeof latestBlock.baseFeePerGas !== 'undefined'; + if (networkDetails.isEIP1559Compatible !== isEIP1559Compatible) { + this.update((state) => { + state.networkDetails.isEIP1559Compatible = isEIP1559Compatible; }); } - return Promise.resolve(true); + return isEIP1559Compatible; } }