Skip to content

Commit

Permalink
fix(cli): unify transaction options (foundry-rs#2172)
Browse files Browse the repository at this point in the history
* fix(cli): unify transaction options

* fix(cli): separate option modules
  • Loading branch information
0xYYY authored Jun 30, 2022
1 parent d000eec commit 0d44687
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 338 deletions.
59 changes: 27 additions & 32 deletions cli/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,7 @@ async fn main() -> eyre::Result<()> {
sig,
cast_async,
args,
gas,
gas_price,
priority_gas_price,
value,
mut nonce,
legacy,
mut tx,
confirmations,
to_json,
resend,
Expand All @@ -298,7 +293,7 @@ async fn main() -> eyre::Result<()> {
};

if resend {
nonce = Some(provider.get_transaction_count(from, None).await?);
tx.nonce = Some(provider.get_transaction_count(from, None).await?);
}

match signer {
Expand All @@ -308,15 +303,15 @@ async fn main() -> eyre::Result<()> {
from,
to,
(sig, args),
gas,
gas_price,
priority_gas_price,
value,
nonce,
tx.gas_limit,
tx.gas_price,
tx.priority_gas_price,
tx.value,
tx.nonce,
chain,
config.etherscan_api_key,
cast_async,
legacy,
tx.legacy,
confirmations,
to_json,
)
Expand All @@ -328,15 +323,15 @@ async fn main() -> eyre::Result<()> {
from,
to,
(sig, args),
gas,
gas_price,
priority_gas_price,
value,
nonce,
tx.gas_limit,
tx.gas_price,
tx.priority_gas_price,
tx.value,
tx.nonce,
chain,
config.etherscan_api_key,
cast_async,
legacy,
tx.legacy,
confirmations,
to_json,
)
Expand All @@ -348,15 +343,15 @@ async fn main() -> eyre::Result<()> {
from,
to,
(sig, args),
gas,
gas_price,
priority_gas_price,
value,
nonce,
tx.gas_limit,
tx.gas_price,
tx.priority_gas_price,
tx.value,
tx.nonce,
chain,
config.etherscan_api_key,
cast_async,
legacy,
tx.legacy,
confirmations,
to_json,
)
Expand All @@ -368,23 +363,23 @@ async fn main() -> eyre::Result<()> {
Address::from_str("00a329c0648769A73afAc7F9381E08FB43dBEA72").unwrap()
{
if resend {
nonce = Some(provider.get_transaction_count(config.sender, None).await?);
tx.nonce = Some(provider.get_transaction_count(config.sender, None).await?);
}

cast_send(
provider,
config.sender,
to,
(sig, args),
gas,
gas_price,
priority_gas_price,
value,
nonce,
tx.gas_limit,
tx.gas_price,
tx.priority_gas_price,
tx.value,
tx.nonce,
chain,
config.etherscan_api_key,
cast_async,
legacy,
tx.legacy,
confirmations,
to_json,
)
Expand Down
82 changes: 13 additions & 69 deletions cli/src/cmd/forge/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use super::verify;
use crate::{
cmd::{forge::build::CoreBuildArgs, utils, RetryArgs},
compile,
opts::{EthereumOpts, WalletType},
utils::{get_http_provider, parse_ether_value, parse_u256},
opts::{EthereumOpts, TransactionOpts, WalletType},
utils::get_http_provider,
};
use clap::{Parser, ValueHint};
use ethers::{
abi::{Abi, Constructor, Token},
prelude::{artifacts::BytecodeObject, ContractFactory, Middleware},
solc::info::ContractInfo,
types::{transaction::eip2718::TypedTransaction, Chain, U256},
types::{transaction::eip2718::TypedTransaction, Chain},
};
use eyre::Context;
use foundry_config::Config;
Expand Down Expand Up @@ -50,68 +50,12 @@ pub struct CreateArgs {
)]
constructor_args_path: Option<PathBuf>,

#[clap(
long,
help_heading = "TRANSACTION OPTIONS",
help = "Send a legacy transaction instead of an EIP1559 transaction.",
long_help = r#"Send a legacy transaction instead of an EIP1559 transaction.
This is automatically enabled for common networks without EIP1559."#
)]
legacy: bool,

#[clap(
long = "gas-price",
help_heading = "TRANSACTION OPTIONS",
help = "Gas price for legacy transactions, or max fee per gas for EIP1559 transactions.",
env = "ETH_GAS_PRICE",
parse(try_from_str = parse_ether_value),
value_name = "PRICE"
)]
gas_price: Option<U256>,

#[clap(
long,
help_heading = "TRANSACTION OPTIONS",
help = "Nonce for the transaction.",
parse(try_from_str = parse_u256),
value_name = "NONCE"
)]
nonce: Option<U256>,

#[clap(
long = "gas-limit",
help_heading = "TRANSACTION OPTIONS",
help = "Gas limit for the transaction.",
env = "ETH_GAS_LIMIT",
parse(try_from_str = parse_u256),
value_name = "GAS_LIMIT"
)]
gas_limit: Option<U256>,

#[clap(
long = "priority-fee",
help_heading = "TRANSACTION OPTIONS",
help = "Gas priority fee for EIP1559 transactions.",
env = "ETH_GAS_PRIORITY_FEE", parse(try_from_str = parse_ether_value),
value_name = "PRICE"
)]
priority_fee: Option<U256>,
#[clap(
long,
help_heading = "TRANSACTION OPTIONS",
help = "Ether to send in the transaction.",
long_help = r#"Ether to send in the transaction, either specified in wei, or as a string with a unit type.
Examples: 1ether, 10gwei, 0.01ether"#,
parse(try_from_str = parse_ether_value),
value_name = "VALUE"
)]
value: Option<U256>,

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

#[clap(flatten, next_help_heading = "TRANSACTION OPTIONS")]
tx: TransactionOpts,

#[clap(flatten, next_help_heading = "ETHEREUM OPTIONS")]
eth: EthereumOpts,

Expand Down Expand Up @@ -223,12 +167,12 @@ impl CreateArgs {
e
}
})?;
let is_legacy =
self.legacy || Chain::try_from(chain).map(|x| Chain::is_legacy(&x)).unwrap_or_default();
let is_legacy = self.tx.legacy ||
Chain::try_from(chain).map(|x| Chain::is_legacy(&x)).unwrap_or_default();
let mut deployer = if is_legacy { deployer.legacy() } else { deployer };

// set tx value if specified
if let Some(value) = self.value {
if let Some(value) = self.tx.value {
deployer.tx.set_value(value);
}

Expand All @@ -237,22 +181,22 @@ impl CreateArgs {
provider.fill_transaction(&mut deployer.tx, None).await?;

// set gas price if specified
if let Some(gas_price) = self.gas_price {
if let Some(gas_price) = self.tx.gas_price {
deployer.tx.set_gas_price(gas_price);
}

// set gas limit if specified
if let Some(gas_limit) = self.gas_limit {
if let Some(gas_limit) = self.tx.gas_limit {
deployer.tx.set_gas(gas_limit);
}

// set nonce if specified
if let Some(nonce) = self.nonce {
if let Some(nonce) = self.tx.nonce {
deployer.tx.set_nonce(nonce);
}

// set priority fee if specified
if let Some(priority_fee) = self.priority_fee {
if let Some(priority_fee) = self.tx.priority_gas_price {
if is_legacy {
panic!("there is no priority fee for legacy txs");
}
Expand Down
44 changes: 4 additions & 40 deletions cli/src/opts/cast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{ClapChain, EthereumOpts};
use super::{ClapChain, EthereumOpts, TransactionOpts};
use crate::{
cmd::cast::{find_block::FindBlockArgs, rpc::RpcArgs, run::RunArgs, wallet::WalletSubcommands},
utils::{parse_ether_value, parse_u256},
Expand Down Expand Up @@ -361,48 +361,12 @@ Examples:
sig: Option<String>,
#[clap(help = "The arguments of the function to call.", value_name = "ARGS")]
args: Vec<String>,
#[clap(long, help = "Gas limit for the transaction.", parse(try_from_str = parse_u256), value_name = "GAS")]
gas: Option<U256>,
#[clap(
long = "gas-price",
help = "Gas price for legacy transactions, or max fee per gas for EIP1559 transactions.",
env = "ETH_GAS_PRICE",
parse(try_from_str = parse_ether_value),
value_name = "PRICE"
)]
gas_price: Option<U256>,
#[clap(
long = "priority-gas-price",
help = "Max priority fee per gas for EIP1559 transactions.",
env = "ETH_PRIORITY_GAS_PRICE",
parse(try_from_str = parse_ether_value),
value_name = "PRICE"
)]
priority_gas_price: Option<U256>,
#[clap(
long,
help = "Ether to send in the transaction.",
long_help = r#"Ether to send in the transaction, either specified in wei, or as a string with a unit type.
Examples: 1ether, 10gwei, 0.01ether"#,
parse(try_from_str = parse_ether_value),
value_name = "VALUE"
)]
value: Option<U256>,
#[clap(long, help = "nonce for the transaction", parse(try_from_str = parse_u256), value_name = "NONCE")]
nonce: Option<U256>,
#[clap(long, env = "CAST_ASYNC")]
cast_async: bool,
#[clap(flatten)]
#[clap(flatten, next_help_heading = "TRANSACTION OPTIONS")]
tx: TransactionOpts,
#[clap(flatten, next_help_heading = "ETHEREUM OPTIONS")]
eth: EthereumOpts,
#[clap(
long,
help = "Send a legacy transaction instead of an EIP1559 transaction.",
long_help = r#"Send a legacy transaction instead of an EIP1559 transaction.
This is automatically enabled for common networks without EIP1559."#
)]
legacy: bool,
#[clap(
short,
long,
Expand Down
19 changes: 19 additions & 0 deletions cli/src/opts/chain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use clap::Parser;
use ethers::types::Chain;
use strum::VariantNames;

// Helper for exposing enum values for `Chain`
// TODO: Is this a duplicate of config/src/chain.rs?
#[derive(Debug, Clone, Parser)]
pub struct ClapChain {
#[clap(
short = 'c',
long = "chain",
env = "CHAIN",
default_value = "mainnet",
// if Chain implemented ArgEnum, we'd get this for free
possible_values = Chain::VARIANTS,
value_name = "CHAIN"
)]
pub inner: Chain,
}
Loading

0 comments on commit 0d44687

Please sign in to comment.