Skip to content

Commit 453379f

Browse files
committed
fix: Refresh EIP-1559 compatibility on network switch
Signed-off-by: dan437 <80175477+dan437@users.noreply.github.com>
1 parent 0d67473 commit 453379f

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

packages/network-controller/src/NetworkController.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,8 +1966,22 @@ export class NetworkController extends BaseController<
19661966
}
19671967
const { EIPS } = metadata;
19681968

1969-
// may want to include some 'freshness' value - something to make sure we refetch this from time to time
1970-
return EIPS[1559];
1969+
// If EIP-1559 compatibility is already cached, return it
1970+
if (EIPS[1559] !== undefined) {
1971+
return EIPS[1559];
1972+
}
1973+
1974+
// Otherwise, determine it now and cache the result
1975+
const isEIP1559Compatible = await this.#determineEIP1559Compatibility(
1976+
networkClientId,
1977+
);
1978+
this.update((state) => {
1979+
if (isEIP1559Compatible !== undefined) {
1980+
state.networksMetadata[networkClientId].EIPS[1559] =
1981+
isEIP1559Compatible;
1982+
}
1983+
});
1984+
return isEIP1559Compatible;
19711985
}
19721986

19731987
/**

packages/network-controller/tests/NetworkController.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3602,6 +3602,84 @@ describe('NetworkController', () => {
36023602
},
36033603
);
36043604
});
3605+
3606+
it('determines EIP-1559 compatibility if metadata exists but EIPS[1559] is undefined', async () => {
3607+
await withController(
3608+
{
3609+
state: {
3610+
networksMetadata: {
3611+
'linea-mainnet': {
3612+
EIPS: {},
3613+
status: NetworkStatus.Available,
3614+
},
3615+
},
3616+
},
3617+
infuraProjectId: 'some-infura-project-id',
3618+
},
3619+
async ({ controller }) => {
3620+
const fakeProvider = buildFakeProvider([
3621+
{
3622+
request: {
3623+
method: 'eth_getBlockByNumber',
3624+
params: ['latest', false],
3625+
},
3626+
response: {
3627+
result: POST_1559_BLOCK,
3628+
},
3629+
},
3630+
]);
3631+
const fakeNetworkClient = buildFakeClient(fakeProvider);
3632+
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
3633+
3634+
const isEIP1559Compatible =
3635+
await controller.getEIP1559Compatibility('linea-mainnet');
3636+
3637+
expect(isEIP1559Compatible).toBe(true);
3638+
expect(
3639+
controller.state.networksMetadata['linea-mainnet'].EIPS[1559],
3640+
).toBe(true);
3641+
},
3642+
);
3643+
});
3644+
3645+
it('returns false if network does not support EIP-1559 when EIPS[1559] is undefined', async () => {
3646+
await withController(
3647+
{
3648+
state: {
3649+
networksMetadata: {
3650+
'linea-mainnet': {
3651+
EIPS: {},
3652+
status: NetworkStatus.Available,
3653+
},
3654+
},
3655+
},
3656+
infuraProjectId: 'some-infura-project-id',
3657+
},
3658+
async ({ controller }) => {
3659+
const fakeProvider = buildFakeProvider([
3660+
{
3661+
request: {
3662+
method: 'eth_getBlockByNumber',
3663+
params: ['latest', false],
3664+
},
3665+
response: {
3666+
result: PRE_1559_BLOCK,
3667+
},
3668+
},
3669+
]);
3670+
const fakeNetworkClient = buildFakeClient(fakeProvider);
3671+
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
3672+
3673+
const isEIP1559Compatible =
3674+
await controller.getEIP1559Compatibility('linea-mainnet');
3675+
3676+
expect(isEIP1559Compatible).toBe(false);
3677+
expect(
3678+
controller.state.networksMetadata['linea-mainnet'].EIPS[1559],
3679+
).toBe(false);
3680+
},
3681+
);
3682+
});
36053683
});
36063684

36073685
describe('if a provider has been set but networksMetadata[selectedNetworkClientId].EIPS in state already has a "1559" property', () => {

0 commit comments

Comments
 (0)