Skip to content

Commit

Permalink
fix warnings/redudant imports/conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
lcfr-eth committed Jan 10, 2025
1 parent 8acedba commit 4977c21
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 125 deletions.
12 changes: 5 additions & 7 deletions examples/providers/examples/mempool_tracecall.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Example of using trace_call on pending transactions.
//! Example of using `trace_call` on pending transactions.
use alloy::{
providers::{ext::TraceApi, ProviderBuilder, Provider, WsConnect},
rpc::types::{trace::parity::TraceType},
providers::{ext::TraceApi, Provider, ProviderBuilder, WsConnect},
rpc::types::trace::parity::TraceType,
};

use eyre::Result;
Expand All @@ -23,13 +22,12 @@ async fn main() -> Result<()> {

let handle = tokio::spawn(async move {
while let Some(tx) = stream.next().await {
let trace_type = [TraceType::Trace];
let trace_type = [TraceType::Trace];
let result = provider.trace_call(&tx.into(), &trace_type).await;
println!("{:?}", result.unwrap().trace);
}
}
});

handle.await?;
Ok(())
}

39 changes: 13 additions & 26 deletions examples/providers/examples/trace_block.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,35 @@
//! Example of using trace_block to examine transactions of the latest block.
//! Example of using `trace_block` to examine transactions of the latest block.
use alloy::{
providers::{ext::TraceApi, Provider, ProviderBuilder, WsConnect},
rpc::types::trace::parity::{Action, TraceOutput},
rpc::types::{BlockId, BlockNumberOrTag},
rpc::types::{trace::parity::Action, BlockId, BlockNumberOrTag},
};

use eyre::Result;
use futures_util::StreamExt;

#[tokio::main]
async fn main() -> Result<()> {
let ws_url = "wss://eth-mainnet.g.alchemy.com/v2/your-api-key";

//let ws_url = "wss://eth-mainnet.g.alchemy.com/v2/your-api-key";
let ws_url = "ws://10.0.0.145:8546";
let ws = WsConnect::new(ws_url);
let provider = ProviderBuilder::new().on_ws(ws).await?;


let subscription = provider.subscribe_blocks().await?;
let mut stream = subscription.into_stream();

while let Some(block) = stream.next().await {
println!(
"Received block number: {}",
block.inner.number
);
println!("Received block number: {}", block.inner.number);

let traces = provider.trace_block(
BlockId::Number(
BlockNumberOrTag::Number(
block.inner.number
)
)
).await?;
let traces = provider
.trace_block(BlockId::Number(BlockNumberOrTag::Number(block.inner.number)))
.await?;

for trace in traces {
match trace.trace.action {
Action::Call(tx) => {
if tx.input.0.len() < 4 {
continue;
}
println!("{:?}", tx);
},
_ => {}
if let Action::Call(tx) = trace.trace.action {
if tx.input.0.len() < 4 {
continue;
}
println!("{:?}", tx);
}
}
}
Expand Down
57 changes: 25 additions & 32 deletions examples/tx-hash-mining/examples/tx_hash_miner_data.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
//! Example of mining a vanity Transaction hash using the data field of a DynamicTyped input/function.
//! Example of mining a vanity Transaction hash using the data field of a `DynamicTyped`
//! input/function.
/// Dynamic types encode the length of the data at the start of the data:
/// appended bytes beyond the end of the datas length will be ignored by evm when decoding the data.
/// We can use this to mine a transaction hash with a specific prefix.
/// ➜ x = "HelloWorld"
/// appended bytes beyond the end of the datas length will be ignored by evm when decoding the
/// data. We can use this to mine a transaction hash with a specific prefix.
/// ➜ x = "`HelloWorld`"
/// Type: string
/// ├ UTF-8: HelloWorld
/// ├ UTF-8: `HelloWorld`
/// ├ Hex (Memory):
/// ├─ Length ([0x00:0x20]): 0x000000000000000000000000000000000000000000000000000000000000000a
/// ├─ Contents ([0x20:..]): 0x48656c6c6f576f726c6400000000000000000000000000000000000000000000
/// ├─ Length ([0x00:0x20]):
/// 0x000000000000000000000000000000000000000000000000000000000000000a ├─ Contents
/// ([0x20:..]): 0x48656c6c6f576f726c6400000000000000000000000000000000000000000000
///
/// ➜ abi.encodeWithSignature("setName(string)", "HelloWorld")
/// ➜ abi.encodeWithSignature("setName(string)", "`HelloWorld`")
/// Type: dynamic bytes
/// ├ Hex (Memory):
/// ├─ Length ([0x00:0x20]): 0x0000000000000000000000000000000000000000000000000000000000000064
/// ├─ Contents ([0x20:..]): 0xc47f00270000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a48656c6c6f576f726c640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
/// ├─ Length ([0x00:0x20]):
/// 0x0000000000000000000000000000000000000000000000000000000000000064 ├─ Contents
/// ([0x20:..]):
/// 0xc47f00270000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a48656c6c6f576f726c640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
///
/// cd dynamic-example && forge script scripts/Counter.s.sol --rpc-url http://127.0.0.1:8545 --private-key $PK --broadcast
/// cd dynamic-example && forge script scripts/Counter.s.sol --rpc-url `<http://127.0.0.1:8545>` --private-key $PK --broadcast
/// ...
/// cargo example --
/// Transaction: 0xdead2a52c2d7517e2b7d6a4092bb9061496906c018fd4c1d5cce5a2dec96da2c
Expand All @@ -26,26 +29,20 @@
/// Block Number: 2
/// Block Hash: 0xa29bcce5697f3febdcd150ceb73f558b35d53dcadc485f0ba928f94f245c5de0
/// Block Time: "Mon, 16 Sep 2024 01:51:30 +0000"
use alloy::{
consensus::TxEnvelope,
hex,
network::{EthereumWallet, TransactionBuilder},
primitives::{U256, Bytes},
primitives::{Bytes, U256},
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
signers::local::PrivateKeySigner,
consensus::TxEnvelope,
signers::local::{
coins_bip39::English,
MnemonicBuilder, LocalSignerError,
},
hex,
signers::local::{coins_bip39::English, LocalSignerError, MnemonicBuilder},
sol,
sol_types::SolCall,
};

use eyre::Result;
use rand::Rng;
use tokio;

sol!(
#[allow(missing_docs)]
Expand All @@ -65,25 +62,21 @@ async fn main() -> Result<()> {
// set to local anvil provider
let rpc_url = "http://127.0.0.1:8545".parse()?;

let new_wallet = create_wallet()
.await
.unwrap();
let new_wallet = create_wallet().await.unwrap();

let wallet = EthereumWallet::from(new_wallet.clone());

let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(wallet.clone())
.on_http(rpc_url);
let provider =
ProviderBuilder::new().with_recommended_fillers().wallet(wallet.clone()).on_http(rpc_url);

let nonce = provider.get_transaction_count(new_wallet.address()).await?;
let eip1559_est = provider.estimate_eip1559_fees(None).await?;

let call = setNameCall { s: "hello".to_string() }.abi_encode();
let input = Bytes::from(call);
let input = Bytes::from(call);

let mut tx_envelope;
let mut rng = rand::thread_rng();
let mut rng = rand::thread_rng();

loop {
let mut input_vec = input.to_vec();
Expand All @@ -94,7 +87,7 @@ async fn main() -> Result<()> {
let modified_input = Bytes::from(input_vec);

let tx = TransactionRequest::default()
// this should be the local address dynamicExample is deployed to via anvil
// this should be the local address dynamicExample is deployed to via anvil
.with_to("0xdEAD000000000000000042069420694206942069".parse()?)
.with_nonce(nonce)
.with_chain_id(31337)
Expand Down
27 changes: 8 additions & 19 deletions examples/tx-hash-mining/examples/tx_hash_miner_fee.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
//! Example of mining a vanity Transaction hash using the max_fee_per_gas field.
//! Example of mining a vanity Transaction hash using the `max_fee_per_gas` field.
use alloy::{
consensus::TxEnvelope,
hex,
network::{EthereumWallet, TransactionBuilder},
primitives::U256,
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
signers::local::PrivateKeySigner,
consensus::TxEnvelope,
hex,
signers::local::{
coins_bip39::English,
MnemonicBuilder, LocalSignerError,
},

signers::local::{coins_bip39::English, LocalSignerError, MnemonicBuilder, PrivateKeySigner},
};
use eyre::Result;
use tokio;

async fn create_wallet() -> Result<PrivateKeySigner, LocalSignerError> {
let wallet = MnemonicBuilder::<English>::default()
Expand All @@ -29,16 +22,12 @@ async fn create_wallet() -> Result<PrivateKeySigner, LocalSignerError> {
async fn main() -> Result<()> {
let rpc_url = "https://eth.merkle.io".parse()?;

let new_wallet = create_wallet()
.await
.unwrap();
let new_wallet = create_wallet().await.unwrap();

let wallet = EthereumWallet::from(new_wallet.clone());

let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(wallet.clone())
.on_http(rpc_url);
let provider =
ProviderBuilder::new().with_recommended_fillers().wallet(wallet.clone()).on_http(rpc_url);

let nonce = provider.get_transaction_count(new_wallet.address()).await?;
let eip1559_est = provider.estimate_eip1559_fees(None).await?;
Expand All @@ -55,7 +44,7 @@ async fn main() -> Result<()> {
.with_value(U256::from(0))
.with_gas_limit(21_000)
.with_max_priority_fee_per_gas(eip1559_est.max_priority_fee_per_gas)
.with_max_fee_per_gas(max_fee_per_gas.into());
.with_max_fee_per_gas(max_fee_per_gas);

tx_envelope = tx.build(&wallet).await?;

Expand Down
35 changes: 13 additions & 22 deletions examples/tx-hash-mining/examples/tx_hash_miner_gas.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
//! Example of mining a vanity Transaction hash using the GAS field.
/// Extra gas is refunded to the sender. This can be used to mine a tx hash that starts with a specific prefix
/// by increasing the gas limit from X until the tx hash starts with the desired prefix.
//! Example of mining a vanity Transaction hash using the `gas` field.
/// Extra gas is refunded to the sender. This can be used to mine a tx hash that starts with a
/// specific prefix by increasing the gas limit from X until the tx hash starts with the
/// desired prefix.
use alloy::{
consensus::TxEnvelope,
hex,
network::{EthereumWallet, TransactionBuilder},
primitives::U256,
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
signers::local::PrivateKeySigner,
consensus::TxEnvelope,
hex,
signers::local::{
coins_bip39::English,
MnemonicBuilder, LocalSignerError,
},

signers::local::{coins_bip39::English, LocalSignerError, MnemonicBuilder},
};
use eyre::Result;
use tokio;

async fn create_wallet() -> Result<PrivateKeySigner, LocalSignerError> {
async fn create_wallet() -> Result<PrivateKeySigner, LocalSignerError> {
let wallet = MnemonicBuilder::<English>::default()
.word_count(12)
.derivation_path("m/44'/60'/0'/2/1")?
Expand All @@ -31,17 +26,13 @@ async fn create_wallet() -> Result<PrivateKeySigner, LocalSignerError> {
#[tokio::main]
async fn main() -> Result<()> {
let rpc_url = "https://eth.merkle.io".parse()?;

let new_wallet = create_wallet()
.await
.unwrap();

let new_wallet = create_wallet().await.unwrap();

let wallet = EthereumWallet::from(new_wallet.clone());

let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(wallet.clone())
.on_http(rpc_url);
let provider =
ProviderBuilder::new().with_recommended_fillers().wallet(wallet.clone()).on_http(rpc_url);

let nonce = provider.get_transaction_count(new_wallet.address()).await?;
let eip1559_est = provider.estimate_eip1559_fees(None).await?;
Expand All @@ -55,7 +46,7 @@ async fn main() -> Result<()> {
.with_nonce(nonce)
.with_chain_id(1)
.with_value(U256::from(0))
.with_gas_limit(gas.into())
.with_gas_limit(gas)
.with_max_priority_fee_per_gas(eip1559_est.max_priority_fee_per_gas)
.with_max_fee_per_gas(eip1559_est.max_fee_per_gas);

Expand Down
26 changes: 7 additions & 19 deletions examples/tx-hash-mining/examples/tx_hash_miner_priority.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
//! Example of mining a vanity Transaction hash using the max_priority_fee_per_gas field.
//! Example of mining a vanity Transaction hash using the `max_priority_fee_per_gas` field.
use alloy::{
consensus::TxEnvelope,
hex,
network::{EthereumWallet, TransactionBuilder},
primitives::U256,
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
signers::local::PrivateKeySigner,
consensus::TxEnvelope,
hex,
signers::local::{
coins_bip39::English,
MnemonicBuilder, LocalSignerError,
},

signers::local::{coins_bip39::English, LocalSignerError, MnemonicBuilder, PrivateKeySigner},
};
use eyre::Result;
use tokio;

async fn create_wallet() -> Result<PrivateKeySigner, LocalSignerError> {
let wallet = MnemonicBuilder::<English>::default()
Expand All @@ -29,16 +22,12 @@ async fn create_wallet() -> Result<PrivateKeySigner, LocalSignerError> {
async fn main() -> Result<()> {
let rpc_url = "https://eth.merkle.io".parse()?;

let new_wallet = create_wallet()
.await
.unwrap();
let new_wallet = create_wallet().await.unwrap();

let wallet = EthereumWallet::from(new_wallet.clone());

let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(wallet.clone())
.on_http(rpc_url);
let provider =
ProviderBuilder::new().with_recommended_fillers().wallet(wallet.clone()).on_http(rpc_url);

let nonce = provider.get_transaction_count(new_wallet.address()).await?;
let eip1559_est = provider.estimate_eip1559_fees(None).await?;
Expand Down Expand Up @@ -72,4 +61,3 @@ async fn main() -> Result<()> {
}
Ok(())
}

0 comments on commit 4977c21

Please sign in to comment.