Skip to content

Commit

Permalink
make getEIP1559Compatibility async (#1084)
Browse files Browse the repository at this point in the history
This PR Makes `getEIP1559Compatibility` method async
Related issue
[#1022](https://app.zenhub.com/workspaces/core-621e46b4d7103800171d1b02/issues/gh/metamask/core/1022)
  • Loading branch information
cryptodev-2s authored and MajorLift committed Oct 11, 2023
1 parent a4af699 commit 41f7fa6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/network-controller/src/NetworkController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3908,7 +3908,7 @@ async function setFakeProvider(
lookupNetworkMock.mockResolvedValue(undefined);
}
if (stubGetEIP1559CompatibilityWhileSetting) {
lookupGetEIP1559CompatibilityMock.mockResolvedValue(undefined);
lookupGetEIP1559CompatibilityMock.mockResolvedValue(false);
}

controller.providerConfig = buildProviderConfig();
Expand Down
55 changes: 30 additions & 25 deletions packages/network-controller/src/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,35 +400,40 @@ export class NetworkController extends BaseControllerV2<
this.refreshNetwork();
}

getEIP1559Compatibility() {
#getLatestBlock(): Promise<Block> {
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;
}
}

Expand Down

0 comments on commit 41f7fa6

Please sign in to comment.