Skip to content

Commit d882612

Browse files
authored
fix: gas estimation on zksync for safe accounts (#623)
* fix: gas estimation on zksync for safe accounts * fix: console log * fix: bump default gas estimation * fix: test
1 parent 3ce88fe commit d882612

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

packages/contract-helpers/src/commons/gasStation.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BigNumber, providers } from 'ethers';
22
import { estimateGas, estimateGasByNetwork } from './gasStation';
3+
import { ChainId } from './types';
34

45
describe('gasStation', () => {
56
const provider: providers.Provider = new providers.JsonRpcProvider();
@@ -41,5 +42,33 @@ describe('gasStation', () => {
4142
const gas = await estimateGasByNetwork(tx, provider, 10);
4243
expect(gas).toEqual(BigNumber.from(110));
4344
});
45+
it('Expects to return 350000 for zksync when connected with contract address', async () => {
46+
jest
47+
.spyOn(provider, 'getNetwork')
48+
.mockImplementationOnce(async () =>
49+
Promise.resolve({ chainId: ChainId.zksync, name: 'zksync' }),
50+
);
51+
52+
jest
53+
.spyOn(provider, 'getCode')
54+
.mockImplementationOnce(async () => Promise.resolve('0x1234'));
55+
56+
const gas = await estimateGasByNetwork({ from: '0x123abc' }, provider);
57+
expect(gas).toEqual(BigNumber.from(350000));
58+
});
59+
it('Expects to return default for zksync when connected with EOA', async () => {
60+
jest
61+
.spyOn(provider, 'getNetwork')
62+
.mockImplementationOnce(async () =>
63+
Promise.resolve({ chainId: ChainId.zksync, name: 'zksync' }),
64+
);
65+
66+
jest
67+
.spyOn(provider, 'getCode')
68+
.mockImplementationOnce(async () => Promise.resolve('0x'));
69+
70+
const gas = await estimateGasByNetwork({ from: '0x123abc' }, provider);
71+
expect(gas).toEqual(BigNumber.from(130));
72+
});
4473
});
4574
});

packages/contract-helpers/src/commons/gasStation.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,21 @@ export const estimateGasByNetwork = async (
2121
provider: providers.Provider,
2222
gasSurplus?: number,
2323
): Promise<BigNumber> => {
24-
const estimatedGas = await provider.estimateGas(tx);
2524
const providerNework: providers.Network = await provider.getNetwork();
25+
if (providerNework.chainId === ChainId.zksync && tx.from) {
26+
/**
27+
* Trying to estimate gas on zkSync when connected with a smart contract address
28+
* will fail. In that case, we'll just return a default value for all transactions.
29+
*
30+
* See here for more details: https://github.com/zkSync-Community-Hub/zksync-developers/discussions/144
31+
*/
32+
const data = await provider.getCode(tx.from);
33+
if (data !== '0x') {
34+
return BigNumber.from(350000);
35+
}
36+
}
37+
38+
const estimatedGas = await provider.estimateGas(tx);
2639

2740
if (providerNework.chainId === ChainId.polygon) {
2841
return estimatedGas.add(estimatedGas.mul(POLYGON_SURPLUS).div(100));

0 commit comments

Comments
 (0)