Skip to content

Commit

Permalink
Fix EIP-1559 network compatibility check (#1457)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistevam authored Jul 25, 2023
1 parent 6437c7a commit 3aec70e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
25 changes: 16 additions & 9 deletions packages/network-controller/src/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ export class NetworkController extends BaseControllerV2<
* appropriately.
*
* @returns A promise that resolves to true if the network supports EIP-1559
* and false otherwise.
* , false otherwise, or `undefined` if unable to determine the compatibility.
*/
async getEIP1559Compatibility() {
const { EIPS } = this.state.networkDetails;
Expand All @@ -917,22 +917,29 @@ export class NetworkController extends BaseControllerV2<

const isEIP1559Compatible = await this.#determineEIP1559Compatibility();
this.update((state) => {
state.networkDetails.EIPS[1559] = isEIP1559Compatible;
if (isEIP1559Compatible !== undefined) {
state.networkDetails.EIPS[1559] = isEIP1559Compatible;
}
});
return isEIP1559Compatible;
}

/**
* Retrieves the latest block from the currently selected network; if the
* block has a `baseFeePerGas` property, then we know that the network
* supports EIP-1559; otherwise it doesn't.
* Retrieves and checks the latest block from the currently selected
* network; if the block has a `baseFeePerGas` property, then we know
* that the network supports EIP-1559; otherwise it doesn't.
*
* @returns A promise that resolves to true if the network supports EIP-1559
* and false otherwise.
* @returns A promise that resolves to `true` if the network supports EIP-1559,
* `false` otherwise, or `undefined` if unable to retrieve the last block.
*/
async #determineEIP1559Compatibility(): Promise<boolean> {
async #determineEIP1559Compatibility(): Promise<boolean | undefined> {
const latestBlock = await this.#getLatestBlock();
return latestBlock?.baseFeePerGas !== undefined;

if (!latestBlock) {
return undefined;
}

return latestBlock.baseFeePerGas !== undefined;
}

/**
Expand Down
43 changes: 17 additions & 26 deletions packages/network-controller/tests/NetworkController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4074,50 +4074,41 @@ describe('NetworkController', () => {
});

describe('if the request for the latest block responds with null', () => {
it('sets the "1559" property to false', async () => {
const latestBlockRespondsNull = {
request: {
method: 'eth_getBlockByNumber',
params: ['latest', false],
},
response: {
result: null,
},
};
it('keeps the "1559" property as undefined', async () => {
await withController(async ({ controller }) => {
setFakeProvider(controller, {
stubs: [
{
request: {
method: 'eth_getBlockByNumber',
params: ['latest', false],
},
response: {
result: null,
},
},
],
stubs: [latestBlockRespondsNull],
stubLookupNetworkWhileSetting: true,
});

await controller.getEIP1559Compatibility();

expect(controller.state.networkDetails.EIPS[1559]).toBe(false);
expect(
controller.state.networkDetails.EIPS[1559],
).toBeUndefined();
});
});

it('returns false', async () => {
it('returns undefined', async () => {
await withController(async ({ controller }) => {
setFakeProvider(controller, {
stubs: [
{
request: {
method: 'eth_getBlockByNumber',
params: ['latest', false],
},
response: {
result: null,
},
},
],
stubs: [latestBlockRespondsNull],
stubLookupNetworkWhileSetting: true,
});

const isEIP1559Compatible =
await controller.getEIP1559Compatibility();

expect(isEIP1559Compatible).toBe(false);
expect(isEIP1559Compatible).toBeUndefined();
});
});
});
Expand Down

0 comments on commit 3aec70e

Please sign in to comment.