Skip to content

Commit

Permalink
Use EIP1559 parameters only in zksync execution and fix multiplier
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrigada committed Jan 15, 2025
1 parent 6961fc6 commit 90409aa
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion crates/cast/bin/cmd/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ async fn cast_send_zk<P: Provider<T, AnyNetwork>, Z: ZksyncProvider<T>, T: Trans
zk_tx.set_paymaster_params(paymaster_params);
}

foundry_zksync_core::estimate_gas(&mut zk_tx, &zk_provider, 130).await?;
foundry_zksync_core::estimate_fee(&mut zk_tx, &zk_provider, 130).await?;

let cast = ZkCast::new(zk_provider, Cast::new(provider));
let pending_tx = cast.send_zk(zk_tx).await?;
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ impl CreateArgs {
deployer.tx.set_gas_price(gas_price);

// estimate fee
foundry_zksync_core::estimate_gas(&mut deployer.tx, &provider, 130).await?;
foundry_zksync_core::estimate_fee(&mut deployer.tx, &provider, 130).await?;

if !is_legacy {
let estimate = provider.estimate_eip1559_fees(None).await.wrap_err("Failed to estimate EIP1559 fees. This chain might not support EIP1559, try adding --legacy to your command.")?;
Expand Down
4 changes: 1 addition & 3 deletions crates/forge/tests/it/zk/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use foundry_test_utils::{forgetest_async, util, TestProject};

use foundry_test_utils::{util::OutputExt, ZkSyncNode};

forgetest_async!(script_execution_with_gas_price, |prj, cmd| {
forgetest_async!(zk_script_execution_with_gas_price_specified_by_user, |prj, cmd| {
setup_gas_prj(&mut prj);

let node = ZkSyncNode::start().await;
Expand All @@ -23,8 +23,6 @@ forgetest_async!(script_execution_with_gas_price, |prj, cmd| {
"--rpc-url",
url.as_str(),
"--slow",
"--evm-version",
"shanghai",
"-vvvvv",
"--broadcast",
"--with-gas-price",
Expand Down
33 changes: 20 additions & 13 deletions crates/script/src/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub async fn send_transaction(
},
);
}
foundry_zksync_core::estimate_gas(&mut zk_tx, &zk_provider, estimate_multiplier)
foundry_zksync_core::estimate_fee(&mut zk_tx, &zk_provider, estimate_multiplier)
.await?;

let zk_signer = alloy_zksync::wallet::ZksyncWallet::new(signer.default_signer());
Expand Down Expand Up @@ -320,12 +320,17 @@ impl BundledState {
) {
(true, Some(gas_price), priority_fee) => (
Some(gas_price.to()),
// NOTE(zk): Zksync is marked as legacy in alloy chains but it is compliant
// with EIP-1559 so we need to pass down the user provided values
priority_fee.map(|fee| Eip1559Estimation {
max_fee_per_gas: gas_price.to(),
max_priority_fee_per_gas: fee.to(),
}),
if self.script_config.config.zksync.run_in_zk_mode() {
// NOTE(zk): Zksync is marked as legacy in alloy chains but it is
// compliant with EIP-1559 so we need to
// pass down the user provided values
priority_fee.map(|fee| Eip1559Estimation {
max_fee_per_gas: gas_price.to(),
max_priority_fee_per_gas: fee.to(),
})
} else {
None
},
),
(true, None, _) => (Some(provider.get_gas_price().await?), None),
(false, Some(max_fee_per_gas), Some(max_priority_fee_per_gas)) => (
Expand Down Expand Up @@ -376,12 +381,14 @@ impl BundledState {

if let Some(gas_price) = gas_price {
tx.set_gas_price(gas_price);
// NOTE(zk): Also set EIP-1559 fees for zk transactions
if let Some(eip1559_fees) = eip1559_fees {
tx.set_max_priority_fee_per_gas(
eip1559_fees.max_priority_fee_per_gas,
);
tx.set_max_fee_per_gas(eip1559_fees.max_fee_per_gas);
if self.script_config.config.zksync.run_in_zk_mode() {
// NOTE(zk): Also set EIP-1559 fees for zk transactions
if let Some(eip1559_fees) = eip1559_fees {
tx.set_max_priority_fee_per_gas(
eip1559_fees.max_priority_fee_per_gas,
);
tx.set_max_fee_per_gas(eip1559_fees.max_fee_per_gas);
}
}
} else {
let eip1559_fees = eip1559_fees.expect("was set above");
Expand Down
10 changes: 3 additions & 7 deletions crates/zksync/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,16 @@ pub struct EstimatedGas {

/// Estimates the gas parameters for the provided transaction.
/// This will call `estimateFee` method on the rpc and set the gas parameters on the transaction.
pub async fn estimate_gas<P: ZksyncProvider<T>, T: Transport + Clone>(
pub async fn estimate_fee<P: ZksyncProvider<T>, T: Transport + Clone>(
tx: &mut ZkTransactionRequest,
provider: P,
estimate_multiplier: u64,
) -> Result<()> {
let fee = provider.estimate_fee(tx.clone()).await?;
tx.set_gas_limit(fee.gas_limit * estimate_multiplier / 100);
// If user provided a gas price, use it for both maxFeePerGas
let max_fee = tx
.max_fee_per_gas()
.map_or(fee.max_fee_per_gas * (estimate_multiplier as u128) / 100, |price| price);
let max_priority_fee = tx
.max_priority_fee_per_gas()
.map_or(fee.max_priority_fee_per_gas * (estimate_multiplier as u128) / 100, |price| price);
let max_fee = tx.max_fee_per_gas().unwrap_or(fee.max_fee_per_gas);
let max_priority_fee = tx.max_priority_fee_per_gas().unwrap_or(fee.max_priority_fee_per_gas);
tx.set_max_fee_per_gas(max_fee);
tx.set_max_priority_fee_per_gas(max_priority_fee);
tx.set_gas_per_pubdata(fee.gas_per_pubdata_limit);
Expand Down

0 comments on commit 90409aa

Please sign in to comment.