Skip to content

Commit

Permalink
Add gas-estimate-multiplier cli argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoopmann committed Jul 31, 2022
1 parent 293a0d6 commit 502a2be
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
1 change: 1 addition & 0 deletions cli/src/cmd/forge/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl DebugArgs {
legacy: false,
broadcast: false,
skip_simulation: false,
gas_estimate_multiplier: 130,
opts: BuildArgs {
args: self.opts,
names: false,
Expand Down
4 changes: 3 additions & 1 deletion cli/src/cmd/forge/script/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ impl ScriptArgs {
let typed_tx = tx.typed_tx_mut();

if has_different_gas_calc(chain) || self.skip_simulation {
typed_tx.set_gas(provider.estimate_gas(typed_tx).await? * 2);
typed_tx.set_gas(
provider.estimate_gas(typed_tx).await? * self.gas_estimate_multiplier / 100,
);
}

total_gas += *typed_tx.gas().expect("gas is set");
Expand Down
5 changes: 2 additions & 3 deletions cli/src/cmd/forge/script/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,8 @@ impl ScriptArgs {
runner.executor.env_mut().block.number += U256::one();
}

// We inflate the gas used by the transaction by x1.3 since the estimation
// might be off
tx.gas = Some(U256::from(result.gas * 13 / 10));
// We inflate the gas used by the user specified percentage
tx.gas = Some(U256::from(result.gas * self.gas_estimate_multiplier / 100));

if !result.success {
failed = true;
Expand Down
9 changes: 9 additions & 0 deletions cli/src/cmd/forge/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ pub struct ScriptArgs {
#[clap(long, help = "Skips on-chain simulation")]
pub skip_simulation: bool,

#[clap(
long,
short,
default_value = "130",
value_name = "GAS_ESTIMATE_MULTIPLIER",
help = "Relative percentage to multiply gas estimates by"
)]
pub gas_estimate_multiplier: u64,

#[clap(flatten, next_help_heading = "BUILD OPTIONS")]
pub opts: BuildArgs,

Expand Down
35 changes: 24 additions & 11 deletions cli/tests/it/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use foundry_cli_test_utils::{
util::{TestCommand, TestProject},
ScriptOutcome, ScriptTester,
};
use foundry_utils::rpc;

use regex::Regex;
use std::{env, path::PathBuf, str::FromStr};
Expand Down Expand Up @@ -215,6 +216,7 @@ forgetest_async!(
can_broadcast_script_skipping_simulation,
|prj: TestProject, mut cmd: TestCommand| async move {
foundry_cli_test_utils::util::initialize(prj.root());
// This example script would fail in on-chain simulation
let script = prj
.inner()
.add_source(
Expand All @@ -224,26 +226,34 @@ forgetest_async!(
pragma solidity 0.8.10;
import "forge-std/Script.sol";
contract Counter {
uint256 public count;
function increment() public {
count += 1;
contract HashChecker {
bytes32 public lastHash;
function update() public {
bytes32 newHash = blockhash(block.number - 1);
require(newHash != lastHash, "Hash didn't change");
lastHash = newHash;
}
}
contract Demo is Script {
function run() external returns (uint256 result, uint8) {
vm.startBroadcast();
Counter counter = new Counter();
counter.increment();
counter.increment();
assert(counter.count() == 2);
vm.stopBroadcast();
HashChecker hashChecker = new HashChecker();
uint numUpdates = 8;
vm.roll(block.number - numUpdates);
for(uint i = 0; i < numUpdates; i++) {
vm.roll(block.number + 1);
hashChecker.update();
}
}
}"#,
)
.unwrap();

let (_api, handle) = spawn(NodeConfig::test()).await;
let node_config = NodeConfig::test()
.with_eth_rpc_url(Some(rpc::next_http_archive_rpc_endpoint()))
.silent();

let (_api, handle) = spawn(node_config).await;
let target_contract = script.display().to_string() + ":Demo";
let private_key =
"ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".to_string();
Expand All @@ -258,7 +268,10 @@ contract Demo is Script {
&handle.http_endpoint(),
"-vvvvv",
"--broadcast",
"--slow",
"--skip-simulation",
"--gas-estimate-multiplier",
"200",
"--private-key",
&private_key,
]);
Expand All @@ -267,8 +280,8 @@ contract Demo is Script {

println!("{}", output.to_string());

assert!(output.contains("Waiting for receipts"));
assert!(output.contains("SKIPPING ON CHAIN SIMULATION"));
assert!(output.contains("ONCHAIN EXECUTION COMPLETE & SUCCESSFUL"));
}
);

Expand Down

0 comments on commit 502a2be

Please sign in to comment.