Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify gas estimation flow #169

Merged
merged 2 commits into from
Jan 12, 2025
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
5 changes: 5 additions & 0 deletions .changeset/nine-parrots-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@abstract-foundation/agw-client': patch
---

Use gas estimation from zks_estimateFee instead of making a separate call for eth_estimateGas
9 changes: 8 additions & 1 deletion packages/agw-client/src/actions/prepareTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ export async function prepareTransactionRequest<
});
}

let gasFromFeeEstimation: bigint | undefined;
if (parameters.includes('fees')) {
if (
typeof request.maxFeePerGas === 'undefined' ||
Expand Down Expand Up @@ -346,6 +347,7 @@ export async function prepareTransactionRequest<
);
maxFeePerGas = feeEstimation.maxFeePerGas;
maxPriorityFeePerGas = feeEstimation.maxPriorityFeePerGas;
gasFromFeeEstimation = feeEstimation.gasLimit;
}

if (
Expand All @@ -359,10 +361,15 @@ export async function prepareTransactionRequest<

request.maxPriorityFeePerGas = maxPriorityFeePerGas;
request.maxFeePerGas = maxFeePerGas;
request.gas = gasFromFeeEstimation;
}
}

if (parameters.includes('gas') && typeof gas === 'undefined')
if (
parameters.includes('gas') &&
typeof gas === 'undefined' &&
gasFromFeeEstimation === undefined // if gas was set by fee estimation, don't estimate again
)
request.gas = await getAction(
client,
estimateGas,
Expand Down
19 changes: 10 additions & 9 deletions packages/agw-client/test/src/actions/prepareTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { address } from '../../constants.js';

const RAW_SIGNATURE =
'0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
const MOCK_GAS_LIMIT = 158774n;
const MOCK_ETH_ESTIMATE_GAS_LIMIT = 158774n;
const MOCK_ZKS_ESTIMATE_GAS_LIMIT = 1403904n;
const MOCK_FEE_PER_GAS = 250000001n;
const MOCK_NONCE = 34;

Expand All @@ -35,7 +36,7 @@ const baseClientRequestSpy = vi.fn(async ({ method, params }) => {
return anvilAbstractTestnet.chain.id;
}
if (method === 'eth_estimateGas') {
return MOCK_GAS_LIMIT;
return MOCK_ETH_ESTIMATE_GAS_LIMIT;
}
return anvilAbstractTestnet.getClient().request({ method, params } as any);
});
Expand Down Expand Up @@ -63,7 +64,7 @@ const publicClient = createPublicClient({
publicClient.request = (async ({ method, params }) => {
if (method === 'zks_estimateFee') {
return {
gas_limit: '0x156c00',
gas_limit: toHex(MOCK_ZKS_ESTIMATE_GAS_LIMIT),
gas_per_pubdata_limit: '0x143b',
max_fee_per_gas: toHex(MOCK_FEE_PER_GAS),
max_priority_fee_per_gas: '0x0',
Expand Down Expand Up @@ -98,7 +99,7 @@ test('minimum', async () => {
chain: anvilAbstractTestnet.chain,
from: address.smartAccountAddress,
chainId: anvilAbstractTestnet.chain.id,
gas: MOCK_GAS_LIMIT,
gas: MOCK_ZKS_ESTIMATE_GAS_LIMIT,
nonce: MOCK_NONCE,
maxFeePerGas: MOCK_FEE_PER_GAS,
maxPriorityFeePerGas: 0n,
Expand All @@ -121,7 +122,7 @@ test('is initial transaction', async () => {
from: address.signerAddress,
chain: anvilAbstractTestnet.chain,
chainId: anvilAbstractTestnet.chain.id,
gas: MOCK_GAS_LIMIT,
gas: MOCK_ZKS_ESTIMATE_GAS_LIMIT,
nonce: MOCK_NONCE,
maxFeePerGas: MOCK_FEE_PER_GAS,
maxPriorityFeePerGas: 0n,
Expand All @@ -146,7 +147,7 @@ test('with fees', async () => {
chain: anvilAbstractTestnet.chain,
from: address.smartAccountAddress,
chainId: anvilAbstractTestnet.chain.id,
gas: MOCK_GAS_LIMIT,
gas: MOCK_ETH_ESTIMATE_GAS_LIMIT,
nonce: MOCK_NONCE,
maxFeePerGas: 10000n,
maxPriorityFeePerGas: 0n,
Expand All @@ -171,7 +172,7 @@ test('to contract deployer', async () => {
chain: anvilAbstractTestnet.chain,
from: address.smartAccountAddress,
chainId: anvilAbstractTestnet.chain.id,
gas: MOCK_GAS_LIMIT,
gas: MOCK_ETH_ESTIMATE_GAS_LIMIT,
nonce: MOCK_NONCE,
maxFeePerGas: 25000000n, // Default fee for contract deployments
maxPriorityFeePerGas: 0n,
Expand All @@ -192,7 +193,7 @@ test('with chainId but not chain', async () => {
...transaction,
from: address.smartAccountAddress,
chainId: anvilAbstractTestnet.chain.id,
gas: MOCK_GAS_LIMIT,
gas: MOCK_ZKS_ESTIMATE_GAS_LIMIT,
nonce: MOCK_NONCE,
maxFeePerGas: MOCK_FEE_PER_GAS,
maxPriorityFeePerGas: 0n,
Expand All @@ -210,7 +211,7 @@ test('with no chainId or chain', async () => {
...transaction,
from: address.smartAccountAddress,
chainId: anvilAbstractTestnet.chain.id,
gas: MOCK_GAS_LIMIT,
gas: MOCK_ZKS_ESTIMATE_GAS_LIMIT,
nonce: MOCK_NONCE,
maxFeePerGas: MOCK_FEE_PER_GAS,
maxPriorityFeePerGas: 0n,
Expand Down
Loading