Skip to content

Commit

Permalink
feat(cast): Move non tx methods to alloy (#7129)
Browse files Browse the repository at this point in the history
* chore: add alloy contract

* feat(cast): migrate most methods to alloy

* chore: leave todo for converting a tx envelope into an rpc tx

* fix: use proper type for storage

* readd decodetx for now

* chore: extend txbuilder to build an alloy tx request

* feat: migrate most methods bar send/decode raw tx

* fix: include tx data

* simplify txbuilder

* chore: simplify back access_list

* chore: remove unnecesary conversion

* fmt

* doctests

* fmt

* do not use trait

* Update crates/cast/bin/main.rs

Co-authored-by: Matthias Seitz <[email protected]>

* cleanup builder

* clippy

* fix doc comments

---------

Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
Evalir and mattsse authored Feb 16, 2024
1 parent 3de8241 commit f9d6c99
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 279 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/cast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ alloy-rlp.workspace = true
alloy-providers.workspace = true
alloy-rpc-types.workspace = true
alloy-signer.workspace = true
alloy-contract.workspace = true
alloy-consensus.workspace = true

ethers-core.workspace = true
ethers-providers.workspace = true
Expand Down
30 changes: 17 additions & 13 deletions crates/cast/bin/cmd/access_list.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use alloy_primitives::Address;
use alloy_providers::provider::TempProvider;
use alloy_rpc_types::BlockId;
use cast::{Cast, TxBuilder};
use clap::Parser;
use ethers_core::types::{BlockId, NameOrAddress};
use ethers_providers::Middleware;
use eyre::{Result, WrapErr};
use foundry_cli::{
opts::{EthereumOpts, TransactionOpts},
utils,
};
use foundry_common::types::ToEthers;
use foundry_common::ens::NameOrAddress;
use foundry_config::{Chain, Config};
use std::str::FromStr;

Expand Down Expand Up @@ -65,10 +66,18 @@ impl AccessListArgs {
let chain = utils::get_chain(config.chain, &provider).await?;
let sender = eth.wallet.sender().await;

let to = match to {
Some(NameOrAddress::Name(name)) => {
Some(NameOrAddress::Name(name).resolve(&alloy_provider).await?)
}
Some(NameOrAddress::Address(addr)) => Some(addr),
None => None,
};

access_list(
&provider,
alloy_provider,
sender.to_ethers(),
sender,
to,
sig,
args,
Expand All @@ -84,16 +93,11 @@ impl AccessListArgs {
}

#[allow(clippy::too_many_arguments)]
async fn access_list<
M: Middleware,
P: TempProvider,
F: Into<NameOrAddress>,
T: Into<NameOrAddress>,
>(
async fn access_list<M: Middleware, P: TempProvider>(
provider: M,
alloy_provider: P,
from: F,
to: Option<T>,
from: Address,
to: Option<Address>,
sig: Option<String>,
args: Vec<String>,
data: Option<String>,
Expand All @@ -105,7 +109,7 @@ async fn access_list<
where
M::Error: 'static,
{
let mut builder = TxBuilder::new(&provider, from, to, chain, tx.legacy).await?;
let mut builder = TxBuilder::new(&alloy_provider, from, to, chain, tx.legacy).await?;
builder
.gas(tx.gas_limit)
.gas_price(tx.gas_price)
Expand All @@ -124,7 +128,7 @@ where

let builder_output = builder.peek();

let cast = Cast::new(&provider, alloy_provider);
let cast = Cast::new(&provider, &alloy_provider);

let access_list: String = cast.access_list(builder_output, block, to_json).await?;

Expand Down
31 changes: 17 additions & 14 deletions crates/cast/bin/cmd/call.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
use alloy_primitives::U256;
use alloy_providers::provider::TempProvider;
use alloy_rpc_types::BlockId;
use cast::{Cast, TxBuilder};
use clap::Parser;
use ethers_core::types::{BlockId, NameOrAddress};
use eyre::{Result, WrapErr};
use foundry_cli::{
opts::{EthereumOpts, TransactionOpts},
utils::{self, handle_traces, parse_ether_value, TraceResult},
};
use foundry_common::{
runtime_client::RuntimeClient,
types::{ToAlloy, ToEthers},
};
use foundry_common::{ens::NameOrAddress, types::ToAlloy};
use foundry_compilers::EvmVersion;
use foundry_config::{find_project_root_path, Config};
use foundry_evm::{executors::TracingExecutor, opts::EvmOpts};
use std::str::FromStr;

type Provider = ethers_providers::Provider<RuntimeClient>;

/// CLI arguments for `cast call`.
#[derive(Debug, Parser)]
pub struct CallArgs {
Expand Down Expand Up @@ -120,8 +116,15 @@ impl CallArgs {
let chain = utils::get_chain(config.chain, &provider).await?;
let sender = eth.wallet.sender().await;

let mut builder: TxBuilder<'_, Provider> =
TxBuilder::new(&provider, sender.to_ethers(), to, chain, tx.legacy).await?;
let to = match to {
Some(NameOrAddress::Name(name)) => {
Some(NameOrAddress::Name(name).resolve(&alloy_provider).await?)
}
Some(NameOrAddress::Address(addr)) => Some(addr),
None => None,
};

let mut builder = TxBuilder::new(&alloy_provider, sender, to, chain, tx.legacy).await?;

builder
.gas(tx.gas_limit)
Expand Down Expand Up @@ -196,16 +199,16 @@ impl CallArgs {
}
};

let builder_output = builder.build();
let builder_output = builder.build_alloy();
println!("{}", Cast::new(provider, alloy_provider).call(builder_output, block).await?);

Ok(())
}
}

/// fills the builder from create arg
async fn fill_create(
builder: &mut TxBuilder<'_, Provider>,
async fn fill_create<P: TempProvider>(
builder: &mut TxBuilder<'_, P>,
value: Option<U256>,
code: String,
sig: Option<String>,
Expand All @@ -226,8 +229,8 @@ async fn fill_create(
}

/// fills the builder from args
async fn fill_tx(
builder: &mut TxBuilder<'_, Provider>,
async fn fill_tx<P: TempProvider>(
builder: &mut TxBuilder<'_, P>,
value: Option<U256>,
sig: Option<String>,
args: Vec<String>,
Expand Down
15 changes: 12 additions & 3 deletions crates/cast/bin/cmd/estimate.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use alloy_primitives::U256;
use cast::{Cast, TxBuilder};
use clap::Parser;
use ethers_core::types::NameOrAddress;
use eyre::Result;
use foundry_cli::{
opts::{EtherscanOpts, RpcOpts},
utils::{self, parse_ether_value},
};
use foundry_common::ens::NameOrAddress;
use foundry_config::{figment::Figment, Config};
use std::str::FromStr;

Expand Down Expand Up @@ -87,7 +87,16 @@ impl EstimateArgs {
let chain = utils::get_chain(config.chain, &provider).await?;
let api_key = config.get_etherscan_api_key(Some(chain));

let mut builder = TxBuilder::new(&provider, from, to, chain, false).await?;
let from = from.resolve(&alloy_provider).await?;
let to = match to {
Some(NameOrAddress::Name(name)) => {
Some(NameOrAddress::Name(name).resolve(&alloy_provider).await?)
}
Some(NameOrAddress::Address(addr)) => Some(addr),
None => None,
};

let mut builder = TxBuilder::new(&alloy_provider, from, to, chain, false).await?;
builder.etherscan_api_key(api_key);

match command {
Expand All @@ -110,7 +119,7 @@ impl EstimateArgs {
};

let builder_output = builder.peek();
let gas = Cast::new(&provider, alloy_provider).estimate(builder_output).await?;
let gas = Cast::new(&provider, &alloy_provider).estimate(builder_output).await?;
println!("{gas}");
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions crates/cast/bin/cmd/logs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_rpc_types::BlockHashOrNumber;
use alloy_rpc_types::BlockId;
use cast::Cast;
use clap::Parser;
use ethers_core::{
Expand All @@ -23,13 +23,13 @@ pub struct LogsArgs {
///
/// Can also be the tags earliest, finalized, safe, latest, or pending.
#[clap(long)]
from_block: Option<BlockHashOrNumber>,
from_block: Option<BlockId>,

/// The block height to stop query at.
///
/// Can also be the tags earliest, finalized, safe, latest, or pending.
#[clap(long)]
to_block: Option<BlockHashOrNumber>,
to_block: Option<BlockId>,

/// The contract address to filter on.
#[clap(
Expand Down
23 changes: 16 additions & 7 deletions crates/cast/bin/cmd/send.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloy_primitives::Address;
use alloy_providers::provider::TempProvider;
use cast::{Cast, TxBuilder};
use clap::Parser;
use ethers_core::types::NameOrAddress;
use ethers_middleware::MiddlewareBuilder;
use ethers_providers::Middleware;
use ethers_signers::Signer;
Expand All @@ -12,6 +12,7 @@ use foundry_cli::{
};
use foundry_common::{
cli_warn,
ens::NameOrAddress,
types::{ToAlloy, ToEthers},
};
use foundry_config::{Chain, Config};
Expand Down Expand Up @@ -119,6 +120,14 @@ impl SendTxArgs {
let chain = utils::get_chain(config.chain, &provider).await?;
let api_key = config.get_etherscan_api_key(Some(chain));

let to = match to {
Some(NameOrAddress::Name(name)) => {
Some(NameOrAddress::Name(name).resolve(&alloy_provider).await?)
}
Some(NameOrAddress::Address(addr)) => Some(addr),
None => None,
};

// Case 1:
// Default to sending via eth_sendTransaction if the --unlocked flag is passed.
// This should be the only way this RPC method is used as it requires a local node
Expand Down Expand Up @@ -155,7 +164,7 @@ impl SendTxArgs {
cast_send(
provider,
alloy_provider,
config.sender.to_ethers(),
config.sender,
to,
code,
(sig, args),
Expand Down Expand Up @@ -199,7 +208,7 @@ corresponds to the sender, or let foundry automatically detect it by not specify
cast_send(
provider,
alloy_provider,
from,
from.to_alloy(),
to,
code,
(sig, args),
Expand All @@ -216,11 +225,11 @@ corresponds to the sender, or let foundry automatically detect it by not specify
}

#[allow(clippy::too_many_arguments)]
async fn cast_send<M: Middleware, P: TempProvider, F: Into<NameOrAddress>, T: Into<NameOrAddress>>(
async fn cast_send<M: Middleware, P: TempProvider>(
provider: M,
alloy_provider: P,
from: F,
to: Option<T>,
from: Address,
to: Option<Address>,
code: Option<String>,
args: (String, Vec<String>),
tx: TransactionOpts,
Expand All @@ -235,7 +244,7 @@ where
{
let (sig, params) = args;
let params = if !sig.is_empty() { Some((&sig[..], params)) } else { None };
let mut builder = TxBuilder::new(&provider, from, to, chain, tx.legacy).await?;
let mut builder = TxBuilder::new(&alloy_provider, from, to, chain, tx.legacy).await?;
builder
.etherscan_api_key(etherscan_api_key)
.gas(tx.gas_limit)
Expand Down
Loading

0 comments on commit f9d6c99

Please sign in to comment.