Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/contract-helpers/src/commons/gasStation.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BigNumber, providers } from 'ethers';
import { estimateGas, estimateGasByNetwork } from './gasStation';
import { ChainId } from './types';

describe('gasStation', () => {
const provider: providers.Provider = new providers.JsonRpcProvider();
Expand Down Expand Up @@ -41,5 +42,33 @@ describe('gasStation', () => {
const gas = await estimateGasByNetwork(tx, provider, 10);
expect(gas).toEqual(BigNumber.from(110));
});
it('Expects to return 350000 for zksync when connected with contract address', async () => {
jest
.spyOn(provider, 'getNetwork')
.mockImplementationOnce(async () =>
Promise.resolve({ chainId: ChainId.zksync, name: 'zksync' }),
);

jest
.spyOn(provider, 'getCode')
.mockImplementationOnce(async () => Promise.resolve('0x1234'));

const gas = await estimateGasByNetwork({ from: '0x123abc' }, provider);
expect(gas).toEqual(BigNumber.from(350000));
});
it('Expects to return default for zksync when connected with EOA', async () => {
jest
.spyOn(provider, 'getNetwork')
.mockImplementationOnce(async () =>
Promise.resolve({ chainId: ChainId.zksync, name: 'zksync' }),
);

jest
.spyOn(provider, 'getCode')
.mockImplementationOnce(async () => Promise.resolve('0x'));

const gas = await estimateGasByNetwork({ from: '0x123abc' }, provider);
expect(gas).toEqual(BigNumber.from(130));
});
});
});
15 changes: 14 additions & 1 deletion packages/contract-helpers/src/commons/gasStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@ export const estimateGasByNetwork = async (
provider: providers.Provider,
gasSurplus?: number,
): Promise<BigNumber> => {
const estimatedGas = await provider.estimateGas(tx);
const providerNework: providers.Network = await provider.getNetwork();
if (providerNework.chainId === ChainId.zksync && tx.from) {
/**
* Trying to estimate gas on zkSync when connected with a smart contract address
* will fail. In that case, we'll just return a default value for all transactions.
*
* See here for more details: https://github.com/zkSync-Community-Hub/zksync-developers/discussions/144
*/
const data = await provider.getCode(tx.from);
if (data !== '0x') {
return BigNumber.from(350000);
}
}

const estimatedGas = await provider.estimateGas(tx);

if (providerNework.chainId === ChainId.polygon) {
return estimatedGas.add(estimatedGas.mul(POLYGON_SURPLUS).div(100));
Expand Down
Loading