From b51d336419209bfc3f96f55608e08ed2d9de857f Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 12:19:24 +0000 Subject: [PATCH 01/23] use ? --- examples/anvil/examples/deploy_contract_anvil.rs | 4 ++-- examples/contracts/examples/deploy_from_artifact.rs | 4 ++-- examples/contracts/examples/deploy_from_contract.rs | 4 ++-- examples/layers/examples/nonce_layer.rs | 4 ++-- examples/layers/examples/recommended_layers.rs | 4 ++-- examples/layers/examples/signer_layer.rs | 4 ++-- examples/providers/examples/builder.rs | 2 +- examples/queries/examples/query_contract_storage.rs | 2 +- examples/queries/examples/query_deployed_bytecode.rs | 2 +- examples/queries/examples/query_logs.rs | 2 +- examples/transactions/examples/gas_price_usd.rs | 2 +- examples/transactions/examples/trace_call.rs | 4 +--- examples/transactions/examples/trace_transaction.rs | 2 +- examples/transactions/examples/transfer_erc20.rs | 2 +- examples/transactions/examples/transfer_eth.rs | 2 +- examples/wallets/examples/create_keystore.rs | 2 +- examples/wallets/examples/keystore_signer.rs | 4 ++-- examples/wallets/examples/ledger_signer.rs | 4 ++-- examples/wallets/examples/private_key_signer.rs | 4 ++-- examples/wallets/examples/sign_message.rs | 2 +- examples/wallets/examples/trezor_signer.rs | 4 ++-- examples/wallets/examples/yubi_signer.rs | 4 ++-- 22 files changed, 33 insertions(+), 35 deletions(-) diff --git a/examples/anvil/examples/deploy_contract_anvil.rs b/examples/anvil/examples/deploy_contract_anvil.rs index a1c55994..9166acb8 100644 --- a/examples/anvil/examples/deploy_contract_anvil.rs +++ b/examples/anvil/examples/deploy_contract_anvil.rs @@ -38,10 +38,10 @@ async fn main() -> Result<()> { let wallet: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with a signer and the network. - let http = anvil.endpoint().parse()?; + let url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(http)); + .on_client(RpcClient::new_http(url)); println!("Anvil running at `{}`", anvil.endpoint()); diff --git a/examples/contracts/examples/deploy_from_artifact.rs b/examples/contracts/examples/deploy_from_artifact.rs index 8bf1ad80..a3efb8c4 100644 --- a/examples/contracts/examples/deploy_from_artifact.rs +++ b/examples/contracts/examples/deploy_from_artifact.rs @@ -28,10 +28,10 @@ async fn main() -> Result<()> { let wallet: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with a signer. - let http = anvil.endpoint().parse()?; + let url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(http)); + .on_client(RpcClient::new_http(url)); println!("Anvil running at `{}`", anvil.endpoint()); diff --git a/examples/contracts/examples/deploy_from_contract.rs b/examples/contracts/examples/deploy_from_contract.rs index 6853aded..7c4ee9bd 100644 --- a/examples/contracts/examples/deploy_from_contract.rs +++ b/examples/contracts/examples/deploy_from_contract.rs @@ -38,10 +38,10 @@ async fn main() -> Result<()> { let wallet: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with a signer. - let http = anvil.endpoint().parse()?; + let url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(http)); + .on_client(RpcClient::new_http(url)); println!("Anvil running at `{}`", anvil.endpoint()); diff --git a/examples/layers/examples/nonce_layer.rs b/examples/layers/examples/nonce_layer.rs index 0e6b1864..95b0de96 100644 --- a/examples/layers/examples/nonce_layer.rs +++ b/examples/layers/examples/nonce_layer.rs @@ -30,14 +30,14 @@ async fn main() -> Result<()> { let from = wallet.address(); // Create a provider with the signer. - let http = anvil.endpoint().parse()?; + let url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() // Add the `ManagedNonceLayer` to the provider. // It is generally recommended to use the `.with_recommended_layers()` method, which // includes the `ManagedNonceLayer`. .layer(ManagedNonceLayer) .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(http)); + .on_client(RpcClient::new_http(url)); // Create an EIP-1559 type transaction. let tx = TransactionRequest::default() diff --git a/examples/layers/examples/recommended_layers.rs b/examples/layers/examples/recommended_layers.rs index 1efb549c..25425412 100644 --- a/examples/layers/examples/recommended_layers.rs +++ b/examples/layers/examples/recommended_layers.rs @@ -21,7 +21,7 @@ async fn main() -> Result<()> { let from = wallet.address(); // Create a provider with the signer. - let http = anvil.endpoint().parse()?; + let url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() // Adds the `GasEstimatorLayer` and the `ManagedNonceLayer` layers. .with_recommended_layers() @@ -29,7 +29,7 @@ async fn main() -> Result<()> { // .with_gas_estimation() // .with_nonce_management() .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(http)); + .on_client(RpcClient::new_http(url)); // Create an EIP-1559 type transaction. // Notice that the `nonce` field is set by the `ManagedNonceLayer`. diff --git a/examples/layers/examples/signer_layer.rs b/examples/layers/examples/signer_layer.rs index 82fb4fa8..30141973 100644 --- a/examples/layers/examples/signer_layer.rs +++ b/examples/layers/examples/signer_layer.rs @@ -21,11 +21,11 @@ async fn main() -> Result<()> { let from = wallet.address(); // Create a provider with the signer. - let http = anvil.endpoint().parse()?; + let url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() // Add the `SignerLayer` to the provider .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(http)); + .on_client(RpcClient::new_http(url)); // Create a legacy type transaction. let tx = TransactionRequest::default() diff --git a/examples/providers/examples/builder.rs b/examples/providers/examples/builder.rs index e9e0d566..8201f527 100644 --- a/examples/providers/examples/builder.rs +++ b/examples/providers/examples/builder.rs @@ -21,7 +21,7 @@ async fn main() -> Result<()> { let signer = Wallet::from(pk.to_owned()); // Setup the HTTP transport which is consumed by the RPC client - let rpc_client = RpcClient::new_http(anvil.endpoint().parse().unwrap()); + let rpc_client = RpcClient::new_http(anvil.endpoint().parse()?); let provider_with_signer = ProviderBuilder::new() .signer(EthereumSigner::from(signer)) .provider(RootProvider::new(rpc_client)); diff --git a/examples/queries/examples/query_contract_storage.rs b/examples/queries/examples/query_contract_storage.rs index e583a187..9b23d575 100644 --- a/examples/queries/examples/query_contract_storage.rs +++ b/examples/queries/examples/query_contract_storage.rs @@ -9,7 +9,7 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { - let url = "https://eth.merkle.io".parse().unwrap(); + let url = "https://eth.merkle.io".parse()?; let provider = RootProvider::::new_http(url); // Get slot0 from USDC-ETH Uniswap V3 pool diff --git a/examples/queries/examples/query_deployed_bytecode.rs b/examples/queries/examples/query_deployed_bytecode.rs index 990308f6..95b45572 100644 --- a/examples/queries/examples/query_deployed_bytecode.rs +++ b/examples/queries/examples/query_deployed_bytecode.rs @@ -10,7 +10,7 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { - let url = "https://eth.merkle.io".parse().unwrap(); + let url = "https://eth.merkle.io".parse()?; let provider = RootProvider::::new_http(url); // Get bytecode of USDC-ETH Uniswap V3 pool diff --git a/examples/queries/examples/query_logs.rs b/examples/queries/examples/query_logs.rs index 004418e5..8a68f09b 100644 --- a/examples/queries/examples/query_logs.rs +++ b/examples/queries/examples/query_logs.rs @@ -7,7 +7,7 @@ use alloy::{ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { - let url = "https://eth.merkle.io".parse().unwrap(); + let url = "https://eth.merkle.io".parse()?; let provider = RootProvider::::new_http(url); // Get logs from the latest block diff --git a/examples/transactions/examples/gas_price_usd.rs b/examples/transactions/examples/gas_price_usd.rs index 6b880750..ceb6c08b 100644 --- a/examples/transactions/examples/gas_price_usd.rs +++ b/examples/transactions/examples/gas_price_usd.rs @@ -26,7 +26,7 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; - let url = anvil.endpoint().parse().unwrap(); + let url = anvil.endpoint().parse()?; let provider = HttpProvider::::new_http(url); let call = latestAnswerCall {}.abi_encode(); diff --git a/examples/transactions/examples/trace_call.rs b/examples/transactions/examples/trace_call.rs index 47ceb047..e257e3c7 100644 --- a/examples/transactions/examples/trace_call.rs +++ b/examples/transactions/examples/trace_call.rs @@ -11,7 +11,6 @@ use alloy::{ }, }; use eyre::Result; -use reqwest::Url; #[tokio::main] async fn main() -> Result<()> { @@ -19,8 +18,7 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; - let provider = - HttpProvider::::new_http("https://eth.merkle.io".parse::().unwrap()); + let provider = HttpProvider::::new_http("https://eth.merkle.io".parse()?); let from = anvil.addresses()[0]; let to = anvil.addresses()[1]; diff --git a/examples/transactions/examples/trace_transaction.rs b/examples/transactions/examples/trace_transaction.rs index e2ee45c5..d67e78e6 100644 --- a/examples/transactions/examples/trace_transaction.rs +++ b/examples/transactions/examples/trace_transaction.rs @@ -18,7 +18,7 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; - let url = anvil.endpoint().parse().unwrap(); + let url = anvil.endpoint().parse()?; let provider = HttpProvider::::new_http(url); let hash = fixed_bytes!("97a02abf405d36939e5b232a5d4ef5206980c5a6661845436058f30600c52df7"); // Hash of the tx we want to trace diff --git a/examples/transactions/examples/transfer_erc20.rs b/examples/transactions/examples/transfer_erc20.rs index d28fbbcc..3882269a 100644 --- a/examples/transactions/examples/transfer_erc20.rs +++ b/examples/transactions/examples/transfer_erc20.rs @@ -19,7 +19,7 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; - let url = anvil.endpoint().parse().unwrap(); + let url = anvil.endpoint().parse()?; let provider = HttpProvider::::new_http(url); let from = anvil.addresses()[0]; diff --git a/examples/transactions/examples/transfer_eth.rs b/examples/transactions/examples/transfer_eth.rs index f0b02b32..45753978 100644 --- a/examples/transactions/examples/transfer_eth.rs +++ b/examples/transactions/examples/transfer_eth.rs @@ -15,7 +15,7 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; - let url = anvil.endpoint().parse().unwrap(); + let url = anvil.endpoint().parse()?; let provider = HttpProvider::::new_http(url); let from = anvil.addresses()[0]; diff --git a/examples/wallets/examples/create_keystore.rs b/examples/wallets/examples/create_keystore.rs index 2b2c1150..595d3bc6 100644 --- a/examples/wallets/examples/create_keystore.rs +++ b/examples/wallets/examples/create_keystore.rs @@ -19,7 +19,7 @@ async fn main() -> Result<()> { // Create a keystore file from the private key of Alice, returning a [Wallet] instance. let (wallet, file_path) = - Wallet::encrypt_keystore(&dir, &mut rng, private_key, password, None).unwrap(); + Wallet::encrypt_keystore(&dir, &mut rng, private_key, password, None)?; let keystore_file_path = dir.path().join(file_path.clone()); diff --git a/examples/wallets/examples/keystore_signer.rs b/examples/wallets/examples/keystore_signer.rs index 64a73ee5..b9e161a7 100644 --- a/examples/wallets/examples/keystore_signer.rs +++ b/examples/wallets/examples/keystore_signer.rs @@ -26,10 +26,10 @@ async fn main() -> Result<()> { let wallet = Wallet::decrypt_keystore(keystore_file_path, password)?; // Create a provider with the signer. - let http = anvil.endpoint().parse()?; + let url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(http)); + .on_client(RpcClient::new_http(url)); // Create a transaction. let tx = TransactionRequest { diff --git a/examples/wallets/examples/ledger_signer.rs b/examples/wallets/examples/ledger_signer.rs index 1b7a238b..a6ac9221 100644 --- a/examples/wallets/examples/ledger_signer.rs +++ b/examples/wallets/examples/ledger_signer.rs @@ -15,10 +15,10 @@ async fn main() -> Result<()> { let signer = LedgerSigner::new(HDPath::LedgerLive(0), Some(1)).await?; // Create a provider with the signer. - let http = "http://localhost:8545".parse()?; + let url = "http://localhost:8545".parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(signer)) - .on_client(RpcClient::new_http(http)); + .on_client(RpcClient::new_http(url)); // Create a transaction. let tx = TransactionRequest { diff --git a/examples/wallets/examples/private_key_signer.rs b/examples/wallets/examples/private_key_signer.rs index c55560ea..8faf6c45 100644 --- a/examples/wallets/examples/private_key_signer.rs +++ b/examples/wallets/examples/private_key_signer.rs @@ -19,10 +19,10 @@ async fn main() -> Result<()> { let wallet: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with the signer. - let http = anvil.endpoint().parse()?; + let url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(http)); + .on_client(RpcClient::new_http(url)); // Create a transaction. let tx = TransactionRequest { diff --git a/examples/wallets/examples/sign_message.rs b/examples/wallets/examples/sign_message.rs index 81267249..51a7e57d 100644 --- a/examples/wallets/examples/sign_message.rs +++ b/examples/wallets/examples/sign_message.rs @@ -21,7 +21,7 @@ async fn main() -> Result<()> { println!("Signature produced by {:?}: {:?}", wallet.address(), signature); println!( "Signature recovered address: {:?}", - signature.recover_address_from_msg(&message[..]).unwrap() + signature.recover_address_from_msg(&message[..])? ); Ok(()) diff --git a/examples/wallets/examples/trezor_signer.rs b/examples/wallets/examples/trezor_signer.rs index 4884e1b2..71d766d5 100644 --- a/examples/wallets/examples/trezor_signer.rs +++ b/examples/wallets/examples/trezor_signer.rs @@ -15,10 +15,10 @@ async fn main() -> Result<()> { let signer = TrezorSigner::new(HDPath::TrezorLive(0), Some(1)).await?; // Create a provider with the signer. - let http = "http://localhost:8545".parse()?; + let url = "http://localhost:8545".parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(signer)) - .on_client(RpcClient::new_http(http)); + .on_client(RpcClient::new_http(url)); // Create a transaction. let tx = TransactionRequest { diff --git a/examples/wallets/examples/yubi_signer.rs b/examples/wallets/examples/yubi_signer.rs index d1ebb908..933d607e 100644 --- a/examples/wallets/examples/yubi_signer.rs +++ b/examples/wallets/examples/yubi_signer.rs @@ -24,10 +24,10 @@ async fn main() -> Result<()> { let signer = YubiWallet::connect(connector, Credentials::default(), 0); // Create a provider with the signer. - let http = "http://localhost:8545".parse()?; + let url = "http://localhost:8545".parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(signer)) - .on_client(RpcClient::new_http(http)); + .on_client(RpcClient::new_http(url)); // Create a transaction. let tx = TransactionRequest { From 9d3f46838ee5d7a13af61d5effe4e348b7009c2a Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 12:39:25 +0000 Subject: [PATCH 02/23] clean up --- README.md | 17 ++++++++++++++++- examples/layers/examples/signer_layer.rs | 2 +- examples/transactions/examples/decode_input.rs | 5 ++--- examples/transactions/examples/gas_price_usd.rs | 1 + examples/transactions/examples/trace_call.rs | 4 +++- .../transactions/examples/trace_transaction.rs | 5 ++++- .../transactions/examples/transfer_erc20.rs | 11 ++++++----- 7 files changed, 33 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 14f94db3..ada4826f 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ Example code using [alloy](https://github.com/alloy-rs/alloy) and [alloy-core](https://github.com/alloy-rs/core). These examples demonstrate the main features of [Alloy](https://github.com/alloy-rs/alloy) and how to use them. -To run an example, use the command `cargo run --example `. + +To run an example, use the command `cargo run --example `: ```sh cargo run --example mnemonic_signer @@ -66,3 +67,17 @@ cargo run --example mnemonic_signer - [x] [Yubi signer](./examples/wallets/examples/yubi_signer.rs) - [x] [Keystore signer](./examples/wallets/examples/keystore_signer.rs) - [x] [Create keystore](./examples/wallets/examples/create_keystore.rs) + +## Development + +To run all examples: + +```sh +cargo run --example 2>&1 | grep -E '^ ' | xargs -n1 cargo run --example +``` + +To run all examples, logging the failing ones: + +```sh +cargo run --example 2>&1 | grep -E '^ ' | xargs -n1 cargo run --quiet --example $1 1>/dev/null +``` \ No newline at end of file diff --git a/examples/layers/examples/signer_layer.rs b/examples/layers/examples/signer_layer.rs index 30141973..dd38d692 100644 --- a/examples/layers/examples/signer_layer.rs +++ b/examples/layers/examples/signer_layer.rs @@ -60,7 +60,7 @@ async fn main() -> Result<()> { println!("Transaction hash matches node hash: {}", transaction_hash == node_hash); let receipt = - provider.get_transaction_receipt(transaction_hash).await.unwrap().expect("no receipt"); + provider.get_transaction_receipt(transaction_hash).await?.expect("Receipt not found"); let receipt_hash = receipt.transaction_hash; assert_eq!(receipt_hash, node_hash); diff --git a/examples/transactions/examples/decode_input.rs b/examples/transactions/examples/decode_input.rs index 334ee5ff..d03e54f8 100644 --- a/examples/transactions/examples/decode_input.rs +++ b/examples/transactions/examples/decode_input.rs @@ -20,7 +20,6 @@ async fn main() -> Result<()> { println!("Decoding https://etherscan.io/tx/0xd1b449d8b1552156957309bffb988924569de34fbf21b51e7af31070cc80fe9a"); let input = "0x38ed173900000000000000000000000000000000000000000001a717cc0a3e4f84c00000000000000000000000000000000000000000000000000000000000000283568400000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000201f129111c60401630932d9f9811bd5b5fff34e000000000000000000000000000000000000000000000000000000006227723d000000000000000000000000000000000000000000000000000000000000000200000000000000000000000095ad61b0a150d79219dcf64e1e6cc01f0b64c4ce000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7"; - let input = hex::decode(input)?; // swapExactTokensForTokensCall generated by the sol! macro above @@ -32,9 +31,9 @@ async fn main() -> Result<()> { println!( "Swap {} of token {} to {} of token {}", decoded.amountIn, - path.first().unwrap(), + path.first().expect("path is empty"), decoded.amountOutMin, - path.last().unwrap() + path.last().expect("path is empty") ); } Err(e) => { diff --git a/examples/transactions/examples/gas_price_usd.rs b/examples/transactions/examples/gas_price_usd.rs index ceb6c08b..b95e2e79 100644 --- a/examples/transactions/examples/gas_price_usd.rs +++ b/examples/transactions/examples/gas_price_usd.rs @@ -26,6 +26,7 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; + // Create a provider. let url = anvil.endpoint().parse()?; let provider = HttpProvider::::new_http(url); diff --git a/examples/transactions/examples/trace_call.rs b/examples/transactions/examples/trace_call.rs index e257e3c7..02387157 100644 --- a/examples/transactions/examples/trace_call.rs +++ b/examples/transactions/examples/trace_call.rs @@ -18,7 +18,9 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; - let provider = HttpProvider::::new_http("https://eth.merkle.io".parse()?); + // Create a provider. + let url = "https://eth.merkle.io".parse()?; + let provider = HttpProvider::::new_http(url); let from = anvil.addresses()[0]; let to = anvil.addresses()[1]; diff --git a/examples/transactions/examples/trace_transaction.rs b/examples/transactions/examples/trace_transaction.rs index d67e78e6..047fdf98 100644 --- a/examples/transactions/examples/trace_transaction.rs +++ b/examples/transactions/examples/trace_transaction.rs @@ -18,9 +18,12 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; + // Create a provider. let url = anvil.endpoint().parse()?; let provider = HttpProvider::::new_http(url); - let hash = fixed_bytes!("97a02abf405d36939e5b232a5d4ef5206980c5a6661845436058f30600c52df7"); // Hash of the tx we want to trace + + // Hash of the tx we want to trace + let hash = fixed_bytes!("97a02abf405d36939e5b232a5d4ef5206980c5a6661845436058f30600c52df7"); // Default tracing let default_options = GethDebugTracingOptions::default(); diff --git a/examples/transactions/examples/transfer_erc20.rs b/examples/transactions/examples/transfer_erc20.rs index 3882269a..47f1d21b 100644 --- a/examples/transactions/examples/transfer_erc20.rs +++ b/examples/transactions/examples/transfer_erc20.rs @@ -19,6 +19,7 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; + // Create a provider. let url = anvil.endpoint().parse()?; let provider = HttpProvider::::new_http(url); @@ -64,7 +65,7 @@ async fn deploy_token_contract( // Compile the contract let bytecode = ERC20Example::BYTECODE.to_owned(); - let tx_req = TransactionRequest { + let tx = TransactionRequest { from: Some(from), input: Some(bytecode).into(), to: None, @@ -72,10 +73,10 @@ async fn deploy_token_contract( }; // Deploy the contract - let pending_tx = provider.send_transaction(tx_req).await?; + let pending_tx = provider.send_transaction(tx).await?; // Wait for the transaction to be mined - let _tx = provider.get_transaction_by_hash(pending_tx.tx_hash().to_owned()).await?; + let _ = provider.get_transaction_by_hash(pending_tx.tx_hash().to_owned()).await?; // Get receipt let receipt = provider.get_transaction_receipt(pending_tx.tx_hash().to_owned()).await?; @@ -92,13 +93,13 @@ async fn balance_of( let call = ERC20Example::balanceOfCall { account }.abi_encode(); let input = Bytes::from(call); - let tx_req = TransactionRequest { + let tx = TransactionRequest { to: Some(contract_address), input: Some(input).into(), ..Default::default() }; - let result = provider.call(&tx_req, None).await?; + let result = provider.call(&tx, None).await?; let result = ERC20Example::balanceOfCall::abi_decode_returns(&result, true)?; Ok(result._0) } From 52e59fe26577b2b6fd642f26614549fea6c390df Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 12:45:52 +0000 Subject: [PATCH 03/23] undo --- README.md | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/README.md b/README.md index ada4826f..302d8d99 100644 --- a/README.md +++ b/README.md @@ -67,17 +67,3 @@ cargo run --example mnemonic_signer - [x] [Yubi signer](./examples/wallets/examples/yubi_signer.rs) - [x] [Keystore signer](./examples/wallets/examples/keystore_signer.rs) - [x] [Create keystore](./examples/wallets/examples/create_keystore.rs) - -## Development - -To run all examples: - -```sh -cargo run --example 2>&1 | grep -E '^ ' | xargs -n1 cargo run --example -``` - -To run all examples, logging the failing ones: - -```sh -cargo run --example 2>&1 | grep -E '^ ' | xargs -n1 cargo run --quiet --example $1 1>/dev/null -``` \ No newline at end of file From 18e57492db8e670d9721b0ddd56cf833fd52d64f Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 12:57:15 +0000 Subject: [PATCH 04/23] clean up examples, avoid importing Anvil when only used for addresses --- .../transactions/examples/decode_input.rs | 5 +++-- .../transactions/examples/gas_price_usd.rs | 20 ++++++++----------- examples/transactions/examples/trace_call.rs | 15 +++++--------- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/examples/transactions/examples/decode_input.rs b/examples/transactions/examples/decode_input.rs index d03e54f8..4fc1c376 100644 --- a/examples/transactions/examples/decode_input.rs +++ b/examples/transactions/examples/decode_input.rs @@ -28,12 +28,13 @@ async fn main() -> Result<()> { match decoded { Ok(decoded) => { let path = decoded.path; + println!( "Swap {} of token {} to {} of token {}", decoded.amountIn, - path.first().expect("path is empty"), + path.first().expect("Path is empty"), decoded.amountOutMin, - path.last().expect("path is empty") + path.last().expect("Path is empty") ); } Err(e) => { diff --git a/examples/transactions/examples/gas_price_usd.rs b/examples/transactions/examples/gas_price_usd.rs index b95e2e79..d9e9756e 100644 --- a/examples/transactions/examples/gas_price_usd.rs +++ b/examples/transactions/examples/gas_price_usd.rs @@ -13,6 +13,7 @@ use eyre::Result; use std::str::FromStr; const ETH_USD_FEED: Address = address!("5f4eC3Df9cbd43714FE2740f5E3616155c5b8419"); +const ETH_USD_FEED_DECIMALS: u8 = 8; const ETH_DECIMALS: u32 = 18; sol!( @@ -32,18 +33,13 @@ async fn main() -> Result<()> { let call = latestAnswerCall {}.abi_encode(); let input = Bytes::from(call); - let tx = TransactionRequest::default().to(Some(ETH_USD_FEED)).input(Some(input).into()); - - let res = provider.call(&tx, None).await?; - - let u = U256::from_str(res.to_string().as_str()); + let response = provider.call(&tx, None).await?; + let result = U256::from_str(response.to_string().as_str())?; let wei_per_gas = provider.get_gas_price().await?; - let gwei = format_units(wei_per_gas, "gwei")?.parse::()?; - - let usd = usd_value(wei_per_gas, u.unwrap())?; + let usd = get_usd_value(wei_per_gas, result)?; println!("Gas price in Gwei: {}", gwei); println!("Gas price in USD: {}", usd); @@ -51,10 +47,10 @@ async fn main() -> Result<()> { Ok(()) } -fn usd_value(amount: U256, price_usd: U256) -> Result { +fn get_usd_value(amount: U256, price_usd: U256) -> Result { let base: U256 = U256::from(10).pow(U256::from(ETH_DECIMALS)); let value: U256 = amount * price_usd / base; - let usd_price_decimals: u8 = 8; - let f: String = format_units(value, usd_price_decimals)?; - Ok(f.parse::()?) + let formatted = format_units(value, ETH_USD_FEED_DECIMALS)?.parse::()?; + + Ok(formatted) } diff --git a/examples/transactions/examples/trace_call.rs b/examples/transactions/examples/trace_call.rs index 02387157..d0045fbf 100644 --- a/examples/transactions/examples/trace_call.rs +++ b/examples/transactions/examples/trace_call.rs @@ -2,8 +2,7 @@ use alloy::{ network::Ethereum, - node_bindings::Anvil, - primitives::U256, + primitives::{address, U256}, providers::{HttpProvider, Provider}, rpc::types::{ eth::{BlockId, BlockNumberOrTag, TransactionRequest}, @@ -14,20 +13,16 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { - // Spin up a forked Anvil node. - // Ensure `anvil` is available in $PATH - let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; - // Create a provider. let url = "https://eth.merkle.io".parse()?; let provider = HttpProvider::::new_http(url); - let from = anvil.addresses()[0]; - let to = anvil.addresses()[1]; + let alice = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266"); + let bob = address!("70997970C51812dc3A010C7d01b50e0d17dc79C8"); let tx_req = TransactionRequest { - from: Some(from), - to: Some(to), + from: Some(alice), + to: Some(bob), value: Some(U256::from(100)), ..Default::default() }; From b142536353f9befaf96d7a40b13dd5c7024ddd1e Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 14:48:51 +0000 Subject: [PATCH 05/23] url -> rpc_url, more consistency for code blocks --- .../anvil/examples/deploy_contract_anvil.rs | 4 +- .../examples/deploy_from_artifact.rs | 4 +- .../examples/deploy_from_contract.rs | 4 +- examples/layers/examples/nonce_layer.rs | 22 +++++----- .../layers/examples/recommended_layers.rs | 22 +++++----- examples/layers/examples/signer_layer.rs | 28 ++++++------ examples/providers/examples/builder.rs | 27 ++++++------ examples/providers/examples/http.rs | 6 +-- .../examples/query_contract_storage.rs | 7 +-- .../examples/query_deployed_bytecode.rs | 4 +- examples/queries/examples/query_logs.rs | 4 +- .../transactions/examples/gas_price_usd.rs | 4 +- examples/transactions/examples/trace_call.rs | 12 +++--- .../examples/trace_transaction.rs | 16 +++---- .../transactions/examples/transfer_erc20.rs | 43 ++++++++++--------- .../transactions/examples/transfer_eth.rs | 24 ++++++----- examples/wallets/examples/keystore_signer.rs | 8 ++-- examples/wallets/examples/ledger_signer.rs | 4 +- .../wallets/examples/private_key_signer.rs | 4 +- examples/wallets/examples/sign_message.rs | 12 +++--- examples/wallets/examples/sign_permit_hash.rs | 12 +++--- examples/wallets/examples/trezor_signer.rs | 4 +- examples/wallets/examples/yubi_signer.rs | 4 +- 23 files changed, 142 insertions(+), 137 deletions(-) diff --git a/examples/anvil/examples/deploy_contract_anvil.rs b/examples/anvil/examples/deploy_contract_anvil.rs index 9166acb8..71556a8e 100644 --- a/examples/anvil/examples/deploy_contract_anvil.rs +++ b/examples/anvil/examples/deploy_contract_anvil.rs @@ -38,10 +38,10 @@ async fn main() -> Result<()> { let wallet: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with a signer and the network. - let url = anvil.endpoint().parse()?; + let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(url)); + .on_client(RpcClient::new_http(rpc_url)); println!("Anvil running at `{}`", anvil.endpoint()); diff --git a/examples/contracts/examples/deploy_from_artifact.rs b/examples/contracts/examples/deploy_from_artifact.rs index a3efb8c4..556023d7 100644 --- a/examples/contracts/examples/deploy_from_artifact.rs +++ b/examples/contracts/examples/deploy_from_artifact.rs @@ -28,10 +28,10 @@ async fn main() -> Result<()> { let wallet: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with a signer. - let url = anvil.endpoint().parse()?; + let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(url)); + .on_client(RpcClient::new_http(rpc_url)); println!("Anvil running at `{}`", anvil.endpoint()); diff --git a/examples/contracts/examples/deploy_from_contract.rs b/examples/contracts/examples/deploy_from_contract.rs index 7c4ee9bd..1126a0ed 100644 --- a/examples/contracts/examples/deploy_from_contract.rs +++ b/examples/contracts/examples/deploy_from_contract.rs @@ -38,10 +38,10 @@ async fn main() -> Result<()> { let wallet: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with a signer. - let url = anvil.endpoint().parse()?; + let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(url)); + .on_client(RpcClient::new_http(rpc_url)); println!("Anvil running at `{}`", anvil.endpoint()); diff --git a/examples/layers/examples/nonce_layer.rs b/examples/layers/examples/nonce_layer.rs index 95b0de96..8a6f32f3 100644 --- a/examples/layers/examples/nonce_layer.rs +++ b/examples/layers/examples/nonce_layer.rs @@ -25,23 +25,23 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().try_spawn()?; - // Set up the wallets. + // Set up the wallet for Alice. let wallet: LocalWallet = anvil.keys()[0].clone().into(); - let from = wallet.address(); + let alice = wallet.address(); // Create a provider with the signer. - let url = anvil.endpoint().parse()?; + let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() // Add the `ManagedNonceLayer` to the provider. // It is generally recommended to use the `.with_recommended_layers()` method, which // includes the `ManagedNonceLayer`. .layer(ManagedNonceLayer) .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(url)); + .on_client(RpcClient::new_http(rpc_url)); // Create an EIP-1559 type transaction. let tx = TransactionRequest::default() - .with_from(from) + .with_from(alice) .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()) .with_value(U256::from(100)) // Notice that without the `GasEstimatorLayer`, you need to set the gas related fields. @@ -54,18 +54,18 @@ async fn main() -> Result<()> { // Send the transaction, the nonce (0) is automatically managed by the provider. let builder = provider.send_transaction(tx.clone()).await?; let node_hash = *builder.tx_hash(); - let pending_transaction = provider.get_transaction_by_hash(node_hash).await?; - assert_eq!(pending_transaction.nonce, 0); + let pending_tx = provider.get_transaction_by_hash(node_hash).await?; + assert_eq!(pending_tx.nonce, 0); - println!("Transaction sent with nonce: {}", pending_transaction.nonce); + println!("Transaction sent with nonce: {}", pending_tx.nonce); // Send the transaction, the nonce (1) is automatically managed by the provider. let builder = provider.send_transaction(tx).await?; let node_hash = *builder.tx_hash(); - let pending_transaction = provider.get_transaction_by_hash(node_hash).await?; - assert_eq!(pending_transaction.nonce, 1); + let pending_tx = provider.get_transaction_by_hash(node_hash).await?; + assert_eq!(pending_tx.nonce, 1); - println!("Transaction sent with nonce: {}", pending_transaction.nonce); + println!("Transaction sent with nonce: {}", pending_tx.nonce); Ok(()) } diff --git a/examples/layers/examples/recommended_layers.rs b/examples/layers/examples/recommended_layers.rs index 25425412..03c6a34c 100644 --- a/examples/layers/examples/recommended_layers.rs +++ b/examples/layers/examples/recommended_layers.rs @@ -16,12 +16,12 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().try_spawn()?; - // Set up the wallets. + // Set up the wallet for Alice. let wallet: LocalWallet = anvil.keys()[0].clone().into(); - let from = wallet.address(); + let alice = wallet.address(); // Create a provider with the signer. - let url = anvil.endpoint().parse()?; + let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() // Adds the `GasEstimatorLayer` and the `ManagedNonceLayer` layers. .with_recommended_layers() @@ -29,13 +29,13 @@ async fn main() -> Result<()> { // .with_gas_estimation() // .with_nonce_management() .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(url)); + .on_client(RpcClient::new_http(rpc_url)); // Create an EIP-1559 type transaction. // Notice that the `nonce` field is set by the `ManagedNonceLayer`. // Notice that without the `GasEstimatorLayer`, you need to set the gas related fields. let tx = TransactionRequest::default() - .with_from(from) + .with_from(alice) .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()) .with_value(U256::from(100)) .with_chain_id(anvil.chain_id()); @@ -43,18 +43,18 @@ async fn main() -> Result<()> { // Send the transaction, the nonce (0) is automatically managed by the provider. let builder = provider.send_transaction(tx.clone()).await?; let node_hash = *builder.tx_hash(); - let pending_transaction = provider.get_transaction_by_hash(node_hash).await?; - assert_eq!(pending_transaction.nonce, 0); + let pending_tx = provider.get_transaction_by_hash(node_hash).await?; + assert_eq!(pending_tx.nonce, 0); - println!("Transaction sent with nonce: {}", pending_transaction.nonce); + println!("Transaction sent with nonce: {}", pending_tx.nonce); // Send the transaction, the nonce (1) is automatically managed by the provider. let builder = provider.send_transaction(tx).await?; let node_hash = *builder.tx_hash(); - let pending_transaction = provider.get_transaction_by_hash(node_hash).await?; - assert_eq!(pending_transaction.nonce, 1); + let pending_tx = provider.get_transaction_by_hash(node_hash).await?; + assert_eq!(pending_tx.nonce, 1); - println!("Transaction sent with nonce: {}", pending_transaction.nonce); + println!("Transaction sent with nonce: {}", pending_tx.nonce); Ok(()) } diff --git a/examples/layers/examples/signer_layer.rs b/examples/layers/examples/signer_layer.rs index dd38d692..4750bff6 100644 --- a/examples/layers/examples/signer_layer.rs +++ b/examples/layers/examples/signer_layer.rs @@ -16,22 +16,22 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().try_spawn()?; - // Set up the wallets. + // Set up the wallet for Alice. let wallet: LocalWallet = anvil.keys()[0].clone().into(); - let from = wallet.address(); + let alice = wallet.address(); // Create a provider with the signer. - let url = anvil.endpoint().parse()?; + let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() // Add the `SignerLayer` to the provider .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(url)); + .on_client(RpcClient::new_http(rpc_url)); // Create a legacy type transaction. let tx = TransactionRequest::default() // Notice that without the `ManagedNonceLayer`, you need to manually set the nonce field. .with_nonce(0) - .with_from(from) + .with_from(alice) .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()) .with_value(U256::from(100)) // Notice that without the `GasEstimatorLayer`, you need to set the gas related fields. @@ -46,21 +46,17 @@ async fn main() -> Result<()> { node_hash == b256!("eb56033eab0279c6e9b685a5ec55ea0ff8d06056b62b7f36974898d4fbb57e64") ); - let pending = builder.register().await?; - let pending_transaction_hash = *pending.tx_hash(); + let pending_tx = builder.register().await?; + let pending_tx_hash = *pending_tx.tx_hash(); - println!( - "Pending transaction hash matches node hash: {}", - pending_transaction_hash == node_hash - ); + println!("Pending transaction hash matches node hash: {}", pending_tx_hash == node_hash); - let transaction_hash = pending.await?; - assert_eq!(transaction_hash, node_hash); + let tx_hash = pending_tx.await?; + assert_eq!(tx_hash, node_hash); - println!("Transaction hash matches node hash: {}", transaction_hash == node_hash); + println!("Transaction hash matches node hash: {}", tx_hash == node_hash); - let receipt = - provider.get_transaction_receipt(transaction_hash).await?.expect("Receipt not found"); + let receipt = provider.get_transaction_receipt(tx_hash).await?.expect("Receipt not found"); let receipt_hash = receipt.transaction_hash; assert_eq!(receipt_hash, node_hash); diff --git a/examples/providers/examples/builder.rs b/examples/providers/examples/builder.rs index 8201f527..f0133747 100644 --- a/examples/providers/examples/builder.rs +++ b/examples/providers/examples/builder.rs @@ -6,7 +6,7 @@ use alloy::{ primitives::U256, providers::{Provider, ProviderBuilder, RootProvider}, rpc::{client::RpcClient, types::eth::TransactionRequest}, - signers::wallet::Wallet, + signers::wallet::LocalWallet, }; use eyre::Result; @@ -16,33 +16,34 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().block_time(1).try_spawn()?; - let pk = &anvil.keys()[0]; - let from = anvil.addresses()[0]; - let signer = Wallet::from(pk.to_owned()); + // Set up the wallet for Alice. + let wallet: LocalWallet = anvil.keys()[0].clone().into(); - // Setup the HTTP transport which is consumed by the RPC client + // Create two users, Alice and Bob. + let alice = wallet.address(); + let bob = anvil.addresses()[1]; + + // Setup the HTTP transport which is consumed by the RPC client. let rpc_client = RpcClient::new_http(anvil.endpoint().parse()?); let provider_with_signer = ProviderBuilder::new() - .signer(EthereumSigner::from(signer)) + .signer(EthereumSigner::from(wallet)) .provider(RootProvider::new(rpc_client)); - let to = anvil.addresses()[1]; - - let mut tx_req = TransactionRequest::default() - .to(Some(to)) + let mut tx = TransactionRequest::default() + .to(Some(bob)) .value(U256::from(100)) .nonce(0) .gas_limit(U256::from(21000)); - tx_req.set_gas_price(U256::from(20e9)); + tx.set_gas_price(U256::from(20e9)); - let pending_tx = provider_with_signer.send_transaction(tx_req).await?; + let pending_tx = provider_with_signer.send_transaction(tx).await?; println!("Pending transaction...{:?}", pending_tx.tx_hash()); let receipt = pending_tx.get_receipt().await?; - assert_eq!(receipt.from, from); + assert_eq!(receipt.from, alice); Ok(()) } diff --git a/examples/providers/examples/http.rs b/examples/providers/examples/http.rs index 2d94ed57..39eda8e7 100644 --- a/examples/providers/examples/http.rs +++ b/examples/providers/examples/http.rs @@ -9,17 +9,17 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { - // Setup the HTTP transport which is consumed by the RPC client + // Setup the HTTP transport which is consumed by the RPC client. let rpc_url = "https://eth.merkle.io".parse()?; - // Create the RPC client + // Create the RPC client. let rpc_client = RpcClient::new_http(rpc_url); // Provider can then be instantiated using the RPC client, HttpProvider is an alias // RootProvider. RootProvider requires two generics N: Network and T: Transport let provider = HttpProvider::::new(rpc_client); - // Get latest block number + // Get latest block number. let latest_block = provider.get_block_number().await?; println!("Latest block number: {}", latest_block); diff --git a/examples/queries/examples/query_contract_storage.rs b/examples/queries/examples/query_contract_storage.rs index 9b23d575..6e3c29b3 100644 --- a/examples/queries/examples/query_contract_storage.rs +++ b/examples/queries/examples/query_contract_storage.rs @@ -9,10 +9,11 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { - let url = "https://eth.merkle.io".parse()?; - let provider = RootProvider::::new_http(url); + // Create a provider. + let rpc_url = "https://eth.merkle.io".parse()?; + let provider = RootProvider::::new_http(rpc_url); - // Get slot0 from USDC-ETH Uniswap V3 pool + // Get storage slot 0 from USDC-ETH Uniswap V3 pool. let pool_address = address!("88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"); let storage_slot = U256::from(0); diff --git a/examples/queries/examples/query_deployed_bytecode.rs b/examples/queries/examples/query_deployed_bytecode.rs index 95b45572..11551354 100644 --- a/examples/queries/examples/query_deployed_bytecode.rs +++ b/examples/queries/examples/query_deployed_bytecode.rs @@ -10,8 +10,8 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { - let url = "https://eth.merkle.io".parse()?; - let provider = RootProvider::::new_http(url); + let rpc_url = "https://eth.merkle.io".parse()?; + let provider = RootProvider::::new_http(rpc_url); // Get bytecode of USDC-ETH Uniswap V3 pool let pool_address = address!("88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"); diff --git a/examples/queries/examples/query_logs.rs b/examples/queries/examples/query_logs.rs index 8a68f09b..caddd843 100644 --- a/examples/queries/examples/query_logs.rs +++ b/examples/queries/examples/query_logs.rs @@ -7,8 +7,8 @@ use alloy::{ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { - let url = "https://eth.merkle.io".parse()?; - let provider = RootProvider::::new_http(url); + let rpc_url = "https://eth.merkle.io".parse()?; + let provider = RootProvider::::new_http(rpc_url); // Get logs from the latest block let latest_block = provider.get_block_number().await?; diff --git a/examples/transactions/examples/gas_price_usd.rs b/examples/transactions/examples/gas_price_usd.rs index d9e9756e..97c45cdc 100644 --- a/examples/transactions/examples/gas_price_usd.rs +++ b/examples/transactions/examples/gas_price_usd.rs @@ -28,8 +28,8 @@ async fn main() -> Result<()> { let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; // Create a provider. - let url = anvil.endpoint().parse()?; - let provider = HttpProvider::::new_http(url); + let rpc_url = anvil.endpoint().parse()?; + let provider = HttpProvider::::new_http(rpc_url); let call = latestAnswerCall {}.abi_encode(); let input = Bytes::from(call); diff --git a/examples/transactions/examples/trace_call.rs b/examples/transactions/examples/trace_call.rs index d0045fbf..e7ddcb17 100644 --- a/examples/transactions/examples/trace_call.rs +++ b/examples/transactions/examples/trace_call.rs @@ -14,24 +14,24 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { // Create a provider. - let url = "https://eth.merkle.io".parse()?; - let provider = HttpProvider::::new_http(url); + let rpc_url = "https://eth.merkle.io".parse()?; + let provider = HttpProvider::::new_http(rpc_url); let alice = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266"); let bob = address!("70997970C51812dc3A010C7d01b50e0d17dc79C8"); - let tx_req = TransactionRequest { + let tx = TransactionRequest { from: Some(alice), to: Some(bob), value: Some(U256::from(100)), ..Default::default() }; let trace_type = [TraceType::Trace]; - let res = provider - .trace_call(&tx_req, &trace_type, Some(BlockId::Number(BlockNumberOrTag::Latest))) + let result = provider + .trace_call(&tx, &trace_type, Some(BlockId::Number(BlockNumberOrTag::Latest))) .await?; - println!("{:?}", res.trace); + println!("{:?}", result.trace); Ok(()) } diff --git a/examples/transactions/examples/trace_transaction.rs b/examples/transactions/examples/trace_transaction.rs index 047fdf98..6f7ed3d2 100644 --- a/examples/transactions/examples/trace_transaction.rs +++ b/examples/transactions/examples/trace_transaction.rs @@ -19,17 +19,17 @@ async fn main() -> Result<()> { let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; // Create a provider. - let url = anvil.endpoint().parse()?; - let provider = HttpProvider::::new_http(url); + let rpc_url = anvil.endpoint().parse()?; + let provider = HttpProvider::::new_http(rpc_url); // Hash of the tx we want to trace let hash = fixed_bytes!("97a02abf405d36939e5b232a5d4ef5206980c5a6661845436058f30600c52df7"); // Default tracing let default_options = GethDebugTracingOptions::default(); - let res = provider.debug_trace_transaction(hash, default_options).await?; + let result = provider.debug_trace_transaction(hash, default_options).await?; - println!("DEFAULT_TRACE: {:?}", res); + println!("DEFAULT_TRACE: {:?}", result); // Call tracing let call_options = GethDebugTracingOptions { @@ -41,9 +41,9 @@ async fn main() -> Result<()> { tracer: Some(GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::CallTracer)), ..Default::default() }; - let res = provider.debug_trace_transaction(hash, call_options).await?; + let result = provider.debug_trace_transaction(hash, call_options).await?; - println!("CALL_TRACE: {:?}", res); + println!("CALL_TRACE: {:?}", result); // JS tracer let js_options = GethDebugTracingOptions { @@ -51,9 +51,9 @@ async fn main() -> Result<()> { ..Default::default() }; - let res = provider.debug_trace_transaction(hash, js_options).await?; + let result = provider.debug_trace_transaction(hash, js_options).await?; - println!("JS_TRACER: {:?}", res); + println!("JS_TRACER: {:?}", result); Ok(()) } diff --git a/examples/transactions/examples/transfer_erc20.rs b/examples/transactions/examples/transfer_erc20.rs index 47f1d21b..d086c748 100644 --- a/examples/transactions/examples/transfer_erc20.rs +++ b/examples/transactions/examples/transfer_erc20.rs @@ -11,6 +11,7 @@ use alloy::{ }; use eyre::Result; +// Codegen from artifact. sol!(ERC20Example, "examples/contracts/ERC20Example.json"); #[tokio::main] @@ -20,40 +21,39 @@ async fn main() -> Result<()> { let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; // Create a provider. - let url = anvil.endpoint().parse()?; - let provider = HttpProvider::::new_http(url); + let rpc_url = anvil.endpoint().parse()?; + let provider = HttpProvider::::new_http(rpc_url); - let from = anvil.addresses()[0]; + // Create two users, Alice and Bob. + let alice = anvil.addresses()[0]; + let bob = anvil.addresses()[1]; - let contract_address = deploy_token_contract(&provider, from).await?; + let contract_address = deploy_token_contract(&provider, alice).await?; println!("Deployed contract at: {}", contract_address); - let to = anvil.addresses()[1]; - - let input = ERC20Example::transferCall { to, amount: U256::from(100) }.abi_encode(); - // Convert to Bytes + let input = ERC20Example::transferCall { to: bob, amount: U256::from(100) }.abi_encode(); let input = Bytes::from(input); - let transfer_tx = TransactionRequest { - from: Some(from), + let tx = TransactionRequest { + from: Some(alice), to: Some(contract_address), input: Some(input).into(), ..Default::default() }; - let pending_tx = provider.send_transaction(transfer_tx).await?; + let pending_tx = provider.send_transaction(tx).await?; println!("Transfer tx: {:?}", pending_tx.tx_hash()); // Wait for confirmation let _ = pending_tx.with_required_confirmations(1); - let to_bal = balance_of(&provider, to, contract_address).await?; - let from_bal = balance_of(&provider, from, contract_address).await?; + let from_bal = balance_of(&provider, alice, contract_address).await?; + let to_bal = balance_of(&provider, bob, contract_address).await?; - assert_eq!(to_bal, U256::from(100)); assert_eq!(from_bal, U256::from(999999999999999999900_i128)); + assert_eq!(to_bal, U256::from(100)); Ok(()) } @@ -62,7 +62,7 @@ async fn deploy_token_contract( provider: &HttpProvider, from: Address, ) -> Result
{ - // Compile the contract + // Compile the contract. let bytecode = ERC20Example::BYTECODE.to_owned(); let tx = TransactionRequest { @@ -72,16 +72,18 @@ async fn deploy_token_contract( ..Default::default() }; - // Deploy the contract + // Deploy the contract. let pending_tx = provider.send_transaction(tx).await?; - // Wait for the transaction to be mined + // Wait for the transaction to be mined. let _ = provider.get_transaction_by_hash(pending_tx.tx_hash().to_owned()).await?; - // Get receipt + // Get receipt. let receipt = provider.get_transaction_receipt(pending_tx.tx_hash().to_owned()).await?; + // Get the contract address. let contract_address = receipt.unwrap().contract_address.unwrap(); + Ok(contract_address) } @@ -100,6 +102,7 @@ async fn balance_of( }; let result = provider.call(&tx, None).await?; - let result = ERC20Example::balanceOfCall::abi_decode_returns(&result, true)?; - Ok(result._0) + let result = ERC20Example::balanceOfCall::abi_decode_returns(&result, true)?._0; + + Ok(result) } diff --git a/examples/transactions/examples/transfer_eth.rs b/examples/transactions/examples/transfer_eth.rs index 45753978..db2d7921 100644 --- a/examples/transactions/examples/transfer_eth.rs +++ b/examples/transactions/examples/transfer_eth.rs @@ -3,7 +3,7 @@ use alloy::{ network::Ethereum, node_bindings::Anvil, - primitives::{address, Address, U256}, + primitives::U256, providers::{HttpProvider, Provider}, rpc::types::eth::TransactionRequest, }; @@ -15,21 +15,25 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; - let url = anvil.endpoint().parse()?; - let provider = HttpProvider::::new_http(url); + // Create a provider. + let rpc_url = anvil.endpoint().parse()?; + let provider = HttpProvider::::new_http(rpc_url); - let from = anvil.addresses()[0]; - // Transfer 1ETH from 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 to Address::ZERO - let tx = TransactionRequest::default().from(from).value(U256::from(1)).to(Some(Address::ZERO)); + // Create two users, Alice and Bob. + let alice = anvil.addresses()[0]; + let bob = anvil.addresses()[1]; + + // Transfer 1 wei from Alice to Bob. + let tx = TransactionRequest::default().from(alice).value(U256::from(1)).to(Some(bob)); + let pending_tx = provider.send_transaction(tx).await?; + let hash = pending_tx.tx_hash(); - let tx = provider.send_transaction(tx).await?; - let hash = tx.tx_hash(); println!("Pending transaction hash: {}", hash); let transaction = provider.get_transaction_by_hash(hash.to_owned()).await?; - assert_eq!(transaction.from, address!("f39fd6e51aad88f6f4ce6ab8827279cfffb92266")); - assert_eq!(transaction.to, Some(Address::ZERO)); + assert_eq!(transaction.from, alice); + assert_eq!(transaction.to, Some(bob)); assert_eq!(transaction.value, U256::from(1)); Ok(()) diff --git a/examples/wallets/examples/keystore_signer.rs b/examples/wallets/examples/keystore_signer.rs index b9e161a7..545e0f33 100644 --- a/examples/wallets/examples/keystore_signer.rs +++ b/examples/wallets/examples/keystore_signer.rs @@ -23,13 +23,13 @@ async fn main() -> Result<()> { // The private key belongs to Alice, the first default Anvil account. let keystore_file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?).join("examples/keystore/alice.json"); - let wallet = Wallet::decrypt_keystore(keystore_file_path, password)?; + let signer = Wallet::decrypt_keystore(keystore_file_path, password)?; // Create a provider with the signer. - let url = anvil.endpoint().parse()?; + let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() - .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(url)); + .signer(EthereumSigner::from(signer)) + .on_client(RpcClient::new_http(rpc_url)); // Create a transaction. let tx = TransactionRequest { diff --git a/examples/wallets/examples/ledger_signer.rs b/examples/wallets/examples/ledger_signer.rs index a6ac9221..af650eab 100644 --- a/examples/wallets/examples/ledger_signer.rs +++ b/examples/wallets/examples/ledger_signer.rs @@ -15,10 +15,10 @@ async fn main() -> Result<()> { let signer = LedgerSigner::new(HDPath::LedgerLive(0), Some(1)).await?; // Create a provider with the signer. - let url = "http://localhost:8545".parse()?; + let rpc_url = "http://localhost:8545".parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(signer)) - .on_client(RpcClient::new_http(url)); + .on_client(RpcClient::new_http(rpc_url)); // Create a transaction. let tx = TransactionRequest { diff --git a/examples/wallets/examples/private_key_signer.rs b/examples/wallets/examples/private_key_signer.rs index 8faf6c45..36a5d7fd 100644 --- a/examples/wallets/examples/private_key_signer.rs +++ b/examples/wallets/examples/private_key_signer.rs @@ -19,10 +19,10 @@ async fn main() -> Result<()> { let wallet: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with the signer. - let url = anvil.endpoint().parse()?; + let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(wallet)) - .on_client(RpcClient::new_http(url)); + .on_client(RpcClient::new_http(rpc_url)); // Create a transaction. let tx = TransactionRequest { diff --git a/examples/wallets/examples/sign_message.rs b/examples/wallets/examples/sign_message.rs index 51a7e57d..c3275ec6 100644 --- a/examples/wallets/examples/sign_message.rs +++ b/examples/wallets/examples/sign_message.rs @@ -5,20 +5,20 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { - // Setup up wallet. - let wallet = LocalWallet::random(); + // Setup up the signer. + let signer = LocalWallet::random(); // Optionally, the wallet's chain id can be set, in order to use EIP-155 // replay protection with different chains - let wallet = wallet.with_chain_id(Some(1337)); + let signer = signer.with_chain_id(Some(1337)); // The message to sign. let message = b"hello"; - // Sign the message asynchronously with the wallet. - let signature = wallet.sign_message(message).await?; + // Sign the message asynchronously with the signer. + let signature = signer.sign_message(message).await?; - println!("Signature produced by {:?}: {:?}", wallet.address(), signature); + println!("Signature produced by {:?}: {:?}", signer.address(), signature); println!( "Signature recovered address: {:?}", signature.recover_address_from_msg(&message[..])? diff --git a/examples/wallets/examples/sign_permit_hash.rs b/examples/wallets/examples/sign_permit_hash.rs index 4c1045a0..781cf0e8 100644 --- a/examples/wallets/examples/sign_permit_hash.rs +++ b/examples/wallets/examples/sign_permit_hash.rs @@ -22,8 +22,8 @@ sol! { #[tokio::main] async fn main() -> Result<()> { - // Setup up wallet. - let wallet = LocalWallet::random(); + // Setup up signer. + let signer = LocalWallet::random(); let domain = eip712_domain! { name: "Uniswap V2", @@ -34,7 +34,7 @@ async fn main() -> Result<()> { }; let permit = Permit { - owner: wallet.address(), + owner: signer.address(), spender: address!("B4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc"), value: U256::from(100), nonce: U256::from(0), @@ -45,14 +45,14 @@ async fn main() -> Result<()> { let hash = permit.eip712_signing_hash(&domain); // Sign the hash asynchronously with the wallet. - let signature = wallet.sign_hash(&hash).await?; + let signature = signer.sign_hash(&hash).await?; println!( "Recovered address matches wallet address: {:?}", - signature.recover_address_from_prehash(&hash)? == wallet.address() + signature.recover_address_from_prehash(&hash)? == signer.address() ); - println!("Wallet signature matches: {:?}", wallet.sign_hash(&hash).await? == signature); + println!("Wallet signature matches: {:?}", signer.sign_hash(&hash).await? == signature); Ok(()) } diff --git a/examples/wallets/examples/trezor_signer.rs b/examples/wallets/examples/trezor_signer.rs index 71d766d5..e7e50f33 100644 --- a/examples/wallets/examples/trezor_signer.rs +++ b/examples/wallets/examples/trezor_signer.rs @@ -15,10 +15,10 @@ async fn main() -> Result<()> { let signer = TrezorSigner::new(HDPath::TrezorLive(0), Some(1)).await?; // Create a provider with the signer. - let url = "http://localhost:8545".parse()?; + let rpc_url = "http://localhost:8545".parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(signer)) - .on_client(RpcClient::new_http(url)); + .on_client(RpcClient::new_http(rpc_url)); // Create a transaction. let tx = TransactionRequest { diff --git a/examples/wallets/examples/yubi_signer.rs b/examples/wallets/examples/yubi_signer.rs index 933d607e..35683242 100644 --- a/examples/wallets/examples/yubi_signer.rs +++ b/examples/wallets/examples/yubi_signer.rs @@ -24,10 +24,10 @@ async fn main() -> Result<()> { let signer = YubiWallet::connect(connector, Credentials::default(), 0); // Create a provider with the signer. - let url = "http://localhost:8545".parse()?; + let rpc_url = "http://localhost:8545".parse()?; let provider = ProviderBuilder::new() .signer(EthereumSigner::from(signer)) - .on_client(RpcClient::new_http(url)); + .on_client(RpcClient::new_http(rpc_url)); // Create a transaction. let tx = TransactionRequest { From 09471adbc4154fb38cf00ce8809dd3c93f999ecc Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 15:05:59 +0000 Subject: [PATCH 06/23] additional comments, wallet -> signer --- .../anvil/examples/deploy_contract_anvil.rs | 6 +-- .../examples/deploy_from_artifact.rs | 6 +-- .../examples/deploy_from_contract.rs | 4 +- examples/layers/examples/nonce_layer.rs | 8 ++-- .../layers/examples/recommended_layers.rs | 8 ++-- examples/layers/examples/signer_layer.rs | 8 ++-- examples/providers/examples/builder.rs | 6 ++- .../transactions/examples/transfer_erc20.rs | 38 +++++++++---------- .../wallets/examples/private_key_signer.rs | 4 +- 9 files changed, 42 insertions(+), 46 deletions(-) diff --git a/examples/anvil/examples/deploy_contract_anvil.rs b/examples/anvil/examples/deploy_contract_anvil.rs index 71556a8e..fd375816 100644 --- a/examples/anvil/examples/deploy_contract_anvil.rs +++ b/examples/anvil/examples/deploy_contract_anvil.rs @@ -34,13 +34,13 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().try_spawn()?; - // Set up wallet - let wallet: LocalWallet = anvil.keys()[0].clone().into(); + // Set up signer. + let signer: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with a signer and the network. let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() - .signer(EthereumSigner::from(wallet)) + .signer(EthereumSigner::from(signer)) .on_client(RpcClient::new_http(rpc_url)); println!("Anvil running at `{}`", anvil.endpoint()); diff --git a/examples/contracts/examples/deploy_from_artifact.rs b/examples/contracts/examples/deploy_from_artifact.rs index 556023d7..5de26b16 100644 --- a/examples/contracts/examples/deploy_from_artifact.rs +++ b/examples/contracts/examples/deploy_from_artifact.rs @@ -24,13 +24,13 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().try_spawn()?; - // Set up wallet - let wallet: LocalWallet = anvil.keys()[0].clone().into(); + // Set up signer. + let signer: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with a signer. let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() - .signer(EthereumSigner::from(wallet)) + .signer(EthereumSigner::from(signer)) .on_client(RpcClient::new_http(rpc_url)); println!("Anvil running at `{}`", anvil.endpoint()); diff --git a/examples/contracts/examples/deploy_from_contract.rs b/examples/contracts/examples/deploy_from_contract.rs index 1126a0ed..19dc6e6b 100644 --- a/examples/contracts/examples/deploy_from_contract.rs +++ b/examples/contracts/examples/deploy_from_contract.rs @@ -35,12 +35,12 @@ async fn main() -> Result<()> { let anvil = Anvil::new().try_spawn()?; // Set up wallet - let wallet: LocalWallet = anvil.keys()[0].clone().into(); + let signer: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with a signer. let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() - .signer(EthereumSigner::from(wallet)) + .signer(EthereumSigner::from(signer)) .on_client(RpcClient::new_http(rpc_url)); println!("Anvil running at `{}`", anvil.endpoint()); diff --git a/examples/layers/examples/nonce_layer.rs b/examples/layers/examples/nonce_layer.rs index 8a6f32f3..7c39c421 100644 --- a/examples/layers/examples/nonce_layer.rs +++ b/examples/layers/examples/nonce_layer.rs @@ -25,9 +25,8 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().try_spawn()?; - // Set up the wallet for Alice. - let wallet: LocalWallet = anvil.keys()[0].clone().into(); - let alice = wallet.address(); + // Set up signer. + let signer: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; @@ -36,12 +35,11 @@ async fn main() -> Result<()> { // It is generally recommended to use the `.with_recommended_layers()` method, which // includes the `ManagedNonceLayer`. .layer(ManagedNonceLayer) - .signer(EthereumSigner::from(wallet)) + .signer(EthereumSigner::from(signer)) .on_client(RpcClient::new_http(rpc_url)); // Create an EIP-1559 type transaction. let tx = TransactionRequest::default() - .with_from(alice) .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()) .with_value(U256::from(100)) // Notice that without the `GasEstimatorLayer`, you need to set the gas related fields. diff --git a/examples/layers/examples/recommended_layers.rs b/examples/layers/examples/recommended_layers.rs index 03c6a34c..045b31ee 100644 --- a/examples/layers/examples/recommended_layers.rs +++ b/examples/layers/examples/recommended_layers.rs @@ -16,9 +16,8 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().try_spawn()?; - // Set up the wallet for Alice. - let wallet: LocalWallet = anvil.keys()[0].clone().into(); - let alice = wallet.address(); + // Set up signer. + let signer: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; @@ -28,14 +27,13 @@ async fn main() -> Result<()> { // Alternatively, you can add the layers individually: // .with_gas_estimation() // .with_nonce_management() - .signer(EthereumSigner::from(wallet)) + .signer(EthereumSigner::from(signer)) .on_client(RpcClient::new_http(rpc_url)); // Create an EIP-1559 type transaction. // Notice that the `nonce` field is set by the `ManagedNonceLayer`. // Notice that without the `GasEstimatorLayer`, you need to set the gas related fields. let tx = TransactionRequest::default() - .with_from(alice) .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()) .with_value(U256::from(100)) .with_chain_id(anvil.chain_id()); diff --git a/examples/layers/examples/signer_layer.rs b/examples/layers/examples/signer_layer.rs index 4750bff6..9cbc76d1 100644 --- a/examples/layers/examples/signer_layer.rs +++ b/examples/layers/examples/signer_layer.rs @@ -16,22 +16,20 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().try_spawn()?; - // Set up the wallet for Alice. - let wallet: LocalWallet = anvil.keys()[0].clone().into(); - let alice = wallet.address(); + // Set up signer. + let signer: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() // Add the `SignerLayer` to the provider - .signer(EthereumSigner::from(wallet)) + .signer(EthereumSigner::from(signer)) .on_client(RpcClient::new_http(rpc_url)); // Create a legacy type transaction. let tx = TransactionRequest::default() // Notice that without the `ManagedNonceLayer`, you need to manually set the nonce field. .with_nonce(0) - .with_from(alice) .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()) .with_value(U256::from(100)) // Notice that without the `GasEstimatorLayer`, you need to set the gas related fields. diff --git a/examples/providers/examples/builder.rs b/examples/providers/examples/builder.rs index f0133747..75aac39d 100644 --- a/examples/providers/examples/builder.rs +++ b/examples/providers/examples/builder.rs @@ -16,7 +16,7 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().block_time(1).try_spawn()?; - // Set up the wallet for Alice. + // Set up signer. let wallet: LocalWallet = anvil.keys()[0].clone().into(); // Create two users, Alice and Bob. @@ -29,6 +29,7 @@ async fn main() -> Result<()> { .signer(EthereumSigner::from(wallet)) .provider(RootProvider::new(rpc_client)); + // Create a transaction. let mut tx = TransactionRequest::default() .to(Some(bob)) .value(U256::from(100)) @@ -37,13 +38,16 @@ async fn main() -> Result<()> { tx.set_gas_price(U256::from(20e9)); + // Send the transaction. let pending_tx = provider_with_signer.send_transaction(tx).await?; println!("Pending transaction...{:?}", pending_tx.tx_hash()); + // Wait for the transaction to be included. let receipt = pending_tx.get_receipt().await?; assert_eq!(receipt.from, alice); + assert_eq!(receipt.to, Some(bob)); Ok(()) } diff --git a/examples/transactions/examples/transfer_erc20.rs b/examples/transactions/examples/transfer_erc20.rs index d086c748..29c37ac1 100644 --- a/examples/transactions/examples/transfer_erc20.rs +++ b/examples/transactions/examples/transfer_erc20.rs @@ -30,8 +30,6 @@ async fn main() -> Result<()> { let contract_address = deploy_token_contract(&provider, alice).await?; - println!("Deployed contract at: {}", contract_address); - let input = ERC20Example::transferCall { to: bob, amount: U256::from(100) }.abi_encode(); let input = Bytes::from(input); @@ -42,18 +40,16 @@ async fn main() -> Result<()> { ..Default::default() }; - let pending_tx = provider.send_transaction(tx).await?; + // Broadcast the transaction and wait for the receipt. + let receipt = provider.send_transaction(tx).await?.get_receipt().await?; - println!("Transfer tx: {:?}", pending_tx.tx_hash()); + println!("Send transaction: {:?}", receipt.transaction_hash); - // Wait for confirmation - let _ = pending_tx.with_required_confirmations(1); + let alice_balance = balance_of(&provider, alice, contract_address).await?; + let bob_balance = balance_of(&provider, bob, contract_address).await?; - let from_bal = balance_of(&provider, alice, contract_address).await?; - let to_bal = balance_of(&provider, bob, contract_address).await?; - - assert_eq!(from_bal, U256::from(999999999999999999900_i128)); - assert_eq!(to_bal, U256::from(100)); + assert_eq!(alice_balance, U256::from(999999999999999999900_i128)); + assert_eq!(bob_balance, U256::from(100)); Ok(()) } @@ -65,6 +61,7 @@ async fn deploy_token_contract( // Compile the contract. let bytecode = ERC20Example::BYTECODE.to_owned(); + // Create a transaction. let tx = TransactionRequest { from: Some(from), input: Some(bytecode).into(), @@ -72,17 +69,13 @@ async fn deploy_token_contract( ..Default::default() }; - // Deploy the contract. - let pending_tx = provider.send_transaction(tx).await?; - - // Wait for the transaction to be mined. - let _ = provider.get_transaction_by_hash(pending_tx.tx_hash().to_owned()).await?; - - // Get receipt. - let receipt = provider.get_transaction_receipt(pending_tx.tx_hash().to_owned()).await?; + // Broadcast the transaction and wait for the receipt. + let receipt = provider.send_transaction(tx).await?.get_receipt().await?; // Get the contract address. - let contract_address = receipt.unwrap().contract_address.unwrap(); + let contract_address = receipt.contract_address.expect("Contract address not found"); + + println!("Deployed contract at: {}", contract_address); Ok(contract_address) } @@ -92,16 +85,21 @@ async fn balance_of( account: Address, contract_address: Address, ) -> Result { + // Encode the call. let call = ERC20Example::balanceOfCall { account }.abi_encode(); let input = Bytes::from(call); + // Create a transaction. let tx = TransactionRequest { to: Some(contract_address), input: Some(input).into(), ..Default::default() }; + // Call the contract. let result = provider.call(&tx, None).await?; + + // Decode the result. let result = ERC20Example::balanceOfCall::abi_decode_returns(&result, true)?._0; Ok(result) diff --git a/examples/wallets/examples/private_key_signer.rs b/examples/wallets/examples/private_key_signer.rs index 36a5d7fd..9b0dd380 100644 --- a/examples/wallets/examples/private_key_signer.rs +++ b/examples/wallets/examples/private_key_signer.rs @@ -16,12 +16,12 @@ async fn main() -> Result<()> { let anvil = Anvil::new().block_time(1).try_spawn()?; // Set up a local wallet from the first default Anvil account (Alice). - let wallet: LocalWallet = anvil.keys()[0].clone().into(); + let signer: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() - .signer(EthereumSigner::from(wallet)) + .signer(EthereumSigner::from(signer)) .on_client(RpcClient::new_http(rpc_url)); // Create a transaction. From f27a37a7cf64198c722c21c2c225799b741c58ba Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 15:36:02 +0000 Subject: [PATCH 07/23] add extra comments --- .../examples/event_multiplexer.rs | 33 +++++++++-------- .../examples/subscribe_blocks.rs | 13 ++++--- .../examples/watch_contract_event.rs | 35 ++++++++++++------- 3 files changed, 50 insertions(+), 31 deletions(-) diff --git a/examples/subscriptions/examples/event_multiplexer.rs b/examples/subscriptions/examples/event_multiplexer.rs index 8b8ef71b..b6c05328 100644 --- a/examples/subscriptions/examples/event_multiplexer.rs +++ b/examples/subscriptions/examples/event_multiplexer.rs @@ -2,7 +2,7 @@ use alloy::{network::Ethereum, node_bindings::Anvil, primitives::I256, sol, sol_types::SolEvent}; use alloy_provider::RootProvider; -use alloy_rpc_client::RpcClient; +use alloy_rpc_client::{RpcClient, WsConnect}; use eyre::Result; use futures_util::StreamExt; use std::str::FromStr; @@ -40,32 +40,37 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().block_time(1).try_spawn()?; - let ws = alloy_rpc_client::WsConnect::new(anvil.ws_endpoint()); + // Create a provider. + let ws = WsConnect::new(anvil.ws_endpoint()); let provider = RootProvider::::new(RpcClient::connect_pubsub(ws).await?); - let deployed_contract = EventMultiplexer::deploy(provider.clone()).await?; + // Deploy the `EventExample` contract. + let contract = EventMultiplexer::deploy(provider).await?; - println!("Deployed contract at: {:?}", deployed_contract.address()); + println!("Deployed contract at: {:?}", contract.address()); - let add_filter = deployed_contract.Add_filter().watch().await?; - let sub_filter = deployed_contract.Sub_filter().watch().await?; - let mul_filter = deployed_contract.Mul_filter().watch().await?; - let div_filter = deployed_contract.Div_filter().watch().await?; + // Create filters for each event. + let add_filter = contract.Add_filter().watch().await?; + let sub_filter = contract.Sub_filter().watch().await?; + let mul_filter = contract.Mul_filter().watch().await?; + let div_filter = contract.Div_filter().watch().await?; let a = I256::from_str("1").unwrap(); let b = I256::from_str("1").unwrap(); - // Build calls - let add_call = deployed_contract.add(a, b); - let sub_call = deployed_contract.sub(a, b); - let mul_call = deployed_contract.mul(a, b); - let div_call = deployed_contract.div(a, b); - // Send calls + // Build calls. + let add_call = contract.add(a, b); + let sub_call = contract.sub(a, b); + let mul_call = contract.mul(a, b); + let div_call = contract.div(a, b); + + // Send calls. let _ = add_call.send().await?; let _ = sub_call.send().await?; let _ = mul_call.send().await?; let _ = div_call.send().await?; + // Convert the filters into streams. let mut add_stream = add_filter.into_stream(); let mut sub_stream = sub_filter.into_stream(); let mut mul_stream = mul_filter.into_stream(); diff --git a/examples/subscriptions/examples/subscribe_blocks.rs b/examples/subscriptions/examples/subscribe_blocks.rs index bcbb3617..7bde3697 100644 --- a/examples/subscriptions/examples/subscribe_blocks.rs +++ b/examples/subscriptions/examples/subscribe_blocks.rs @@ -1,4 +1,4 @@ -//! Example of subscribing to blocks and watching blocks. +//! Example of subscribing to blocks and watching block headers by polling. use alloy::{network::Ethereum, node_bindings::Anvil}; use alloy_provider::{Provider, RootProvider}; @@ -12,21 +12,24 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH let anvil = Anvil::new().block_time(1).try_spawn()?; + // Create a provider. let ws = alloy_rpc_client::WsConnect::new(anvil.ws_endpoint()); let provider = RootProvider::::new(RpcClient::connect_pubsub(ws).await?); - let sub = provider.subscribe_blocks().await?; - let mut stream = sub.into_stream().take(2); + // Subscribe to blocks. + let subscription = provider.subscribe_blocks().await?; + let mut stream = subscription.into_stream().take(2); while let Some(block) = stream.next().await { - println!("Subscribed Block: {:?}", block.header.number); + println!("Received block number: {:?}", block.header.number.unwrap().to_string()); } + // Poll for block headers. let poller = provider.watch_blocks().await?; let mut stream = poller.into_stream().flat_map(stream::iter).take(2); while let Some(block_hash) = stream.next().await { - println!("Watched Block: {:?}", block_hash); + println!("Polled for block header: {:?}", block_hash); } Ok(()) diff --git a/examples/subscriptions/examples/watch_contract_event.rs b/examples/subscriptions/examples/watch_contract_event.rs index 91d2ee65..691ecaab 100644 --- a/examples/subscriptions/examples/watch_contract_event.rs +++ b/examples/subscriptions/examples/watch_contract_event.rs @@ -6,9 +6,11 @@ use alloy_rpc_client::RpcClient; use eyre::Result; use futures_util::StreamExt; +// Codegen from embedded Solidity code and precompiled bytecode. +// solc v0.8.24; solc a.sol --via-ir --optimize --bin sol!( - #[sol(rpc, bytecode = "0x60806040526000805534801561001457600080fd5b50610260806100246000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80632baeceb71461004657806361bc221a14610050578063d09de08a1461006e575b600080fd5b61004e610078565b005b6100586100d9565b6040516100659190610159565b60405180910390f35b6100766100df565b005b600160008082825461008a91906101a3565b925050819055506000543373ffffffffffffffffffffffffffffffffffffffff167fdc69c403b972fc566a14058b3b18e1513da476de6ac475716e489fae0cbe4a2660405160405180910390a3565b60005481565b60016000808282546100f191906101e6565b925050819055506000543373ffffffffffffffffffffffffffffffffffffffff167ff6d1d8d205b41f9fb9549900a8dba5d669d68117a3a2b88c1ebc61163e8117ba60405160405180910390a3565b6000819050919050565b61015381610140565b82525050565b600060208201905061016e600083018461014a565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006101ae82610140565b91506101b983610140565b92508282039050818112600084121682821360008512151617156101e0576101df610174565b5b92915050565b60006101f182610140565b91506101fc83610140565b92508282019050828112156000831216838212600084121516171561022457610223610174565b5b9291505056fea26469706673582212208d0d34c26bfd2938ff07dd54c3fcc2bc4509e4ae654edff58101e5e7ab8cf18164736f6c63430008180033")] - contract EventExample { + #[sol(rpc, bytecode = "0x60808060405234610019575f8055610143908161001e8239f35b5f80fdfe60806040526004361015610011575f80fd5b5f3560e01c80632baeceb7146100c357806361bc221a146100a75763d09de08a1461003a575f80fd5b346100a3575f3660031901126100a3575f5460018101905f60018312911290801582169115161761008f57805f55337ff6d1d8d205b41f9fb9549900a8dba5d669d68117a3a2b88c1ebc61163e8117ba5f80a3005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a3575f3660031901126100a35760205f54604051908152f35b346100a3575f3660031901126100a3575f545f19810190811360011661008f57805f55337fdc69c403b972fc566a14058b3b18e1513da476de6ac475716e489fae0cbe4a265f80a300fea2646970667358221220c045c027059726f9175a4abd427eb3f7a3fe8e27108bc19e4ae46055e7c1842c64736f6c63430008180033")] + contract Counter { int256 public counter = 0; event Increment(address indexed by, int256 indexed value); @@ -28,28 +30,37 @@ sol!( #[tokio::main] async fn main() -> Result<()> { + // Spin up a local Anvil node. + // Ensure `anvil` is available in $PATH let anvil = Anvil::new().block_time(1).try_spawn()?; - let ws = alloy_rpc_client::WsConnect::new(anvil.ws_endpoint()); + // Create a WebSocket provider. + let ws_rpc_url = anvil.ws_endpoint(); + let ws = alloy_rpc_client::WsConnect::new(ws_rpc_url); let provider = RootProvider::::new(RpcClient::connect_pubsub(ws).await?); - let deployed_contract = EventExample::deploy(provider.clone()).await?; + // Deploy the `Counter` contract. + let contract = Counter::deploy(provider.clone()).await?; - println!("Deployed contract at: {:?}", deployed_contract.address()); + println!("Deployed contract at: {:?}", contract.address()); - let increment_filter = deployed_contract.Increment_filter().watch().await?; - let decrement_filter = deployed_contract.Decrement_filter().watch().await?; + // Create filters for each event. + let increment_filter = contract.Increment_filter().watch().await?; + let decrement_filter = contract.Decrement_filter().watch().await?; - // Build a call to increment the counter - let increment_call = deployed_contract.increment(); - // Build a call to decrement the counter - let decrement_call = deployed_contract.decrement(); - // Send the call 2 times + // Build a call to increment the counter. + let increment_call = contract.increment(); + + // Build a call to decrement the counter. + let decrement_call = contract.decrement(); + + // Send the call 2 times. for _ in 0..2 { let _ = increment_call.send().await?; let _ = decrement_call.send().await?; } + // Listen for the events. increment_filter .into_stream() .take(2) From 0ee6a33ecde6eb7c433a250049c9611030325e0c Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 15:37:31 +0000 Subject: [PATCH 08/23] rerun bytecode and document settings --- examples/subscriptions/examples/event_multiplexer.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/subscriptions/examples/event_multiplexer.rs b/examples/subscriptions/examples/event_multiplexer.rs index b6c05328..c35a76dd 100644 --- a/examples/subscriptions/examples/event_multiplexer.rs +++ b/examples/subscriptions/examples/event_multiplexer.rs @@ -7,9 +7,11 @@ use eyre::Result; use futures_util::StreamExt; use std::str::FromStr; +// Codegen from embedded Solidity code and precompiled bytecode. +// solc v0.8.24; solc a.sol --via-ir --optimize --bin sol!( #[derive(Debug)] - #[sol(rpc, bytecode = "0x608060405234801561001057600080fd5b50610485806100206000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c80634350913814610051578063a5f3c23b1461006d578063adefc37b14610089578063bbe93d91146100a5575b600080fd5b61006b60048036038101906100669190610248565b6100c1565b005b61008760048036038101906100829190610248565b610114565b005b6100a3600480360381019061009e9190610248565b610167565b005b6100bf60048036038101906100ba9190610248565b6101ba565b005b80826100cd91906102e6565b3373ffffffffffffffffffffffffffffffffffffffff167f1c1e8bbe327890ea8d3f5b22370a56c3fcef7ff82f306161f64647fe5d28588160405160405180910390a35050565b80826101209190610350565b3373ffffffffffffffffffffffffffffffffffffffff167f6da406ea462447ed7804b4a4dc69c67b53d3d45a50381ae3e9cf878c9d7c23df60405160405180910390a35050565b80826101739190610394565b3373ffffffffffffffffffffffffffffffffffffffff167f32e913bf2ad35da1e845597618bb9f3f80642a68dd39f30a093a7838aa61fb2760405160405180910390a35050565b80826101c691906103d7565b3373ffffffffffffffffffffffffffffffffffffffff167fd7a123d4c8e44db3186e04b9c96c102287276929c930f2e8abcaa555ef5dcacc60405160405180910390a35050565b600080fd5b6000819050919050565b61022581610212565b811461023057600080fd5b50565b6000813590506102428161021c565b92915050565b6000806040838503121561025f5761025e61020d565b5b600061026d85828601610233565b925050602061027e85828601610233565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006102f182610212565b91506102fc83610212565b92508261030c5761030b610288565b5b600160000383147f800000000000000000000000000000000000000000000000000000000000000083141615610345576103446102b7565b5b828205905092915050565b600061035b82610212565b915061036683610212565b92508282019050828112156000831216838212600084121516171561038e5761038d6102b7565b5b92915050565b600061039f82610212565b91506103aa83610212565b92508282039050818112600084121682821360008512151617156103d1576103d06102b7565b5b92915050565b60006103e282610212565b91506103ed83610212565b92508282026103fb81610212565b91507f80000000000000000000000000000000000000000000000000000000000000008414600084121615610433576104326102b7565b5b8282058414831517610448576104476102b7565b5b509291505056fea2646970667358221220386c6c77ebc5f1bae50f37d123c5a510f2f678b30900c2d5ebf09f68c9353f4b64736f6c63430008180033")] + #[sol(rpc, bytecode = "0x6080806040523461001657610213908161001b8239f35b5f80fdfe6080604052600480361015610012575f80fd5b5f3560e01c80634350913814610165578063a5f3c23b14610116578063adefc37b146100c75763bbe93d9114610046575f80fd5b346100c357610054366101c7565b8181029291905f8212600160ff1b8214166100b057818405149015171561009d5750337fd7a123d4c8e44db3186e04b9c96c102287276929c930f2e8abcaa555ef5dcacc5f80a3005b601190634e487b7160e01b5f525260245ffd5b601183634e487b7160e01b5f525260245ffd5b5f80fd5b50346100c3576100d6366101c7565b91905f838203931281841281169184139015161761009d5750337f32e913bf2ad35da1e845597618bb9f3f80642a68dd39f30a093a7838aa61fb275f80a3005b50346100c357610125366101c7565b91905f838201938412911290801582169115161761009d5750337f6da406ea462447ed7804b4a4dc69c67b53d3d45a50381ae3e9cf878c9d7c23df5f80a3005b50346100c357610174366101c7565b9182156101b457600160ff1b82145f1984141661009d575005337f1c1e8bbe327890ea8d3f5b22370a56c3fcef7ff82f306161f64647fe5d2858815f80a3005b601290634e487b7160e01b5f525260245ffd5b60409060031901126100c357600435906024359056fea26469706673582212208fc27b878cb877b8c4e5dee739e0a35a64f82759549ed8f6d4ddab5ded9717bb64736f6c63430008180033")] contract EventMultiplexer { event Add(address indexed sender, int256 indexed value); event Sub(address indexed sender, int256 indexed value); From 5f138b47aabd669a00599bd17ee855d7eee26bb4 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 15:38:11 +0000 Subject: [PATCH 09/23] remove redundant macro --- examples/subscriptions/examples/event_multiplexer.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/subscriptions/examples/event_multiplexer.rs b/examples/subscriptions/examples/event_multiplexer.rs index c35a76dd..9f9098ef 100644 --- a/examples/subscriptions/examples/event_multiplexer.rs +++ b/examples/subscriptions/examples/event_multiplexer.rs @@ -10,7 +10,6 @@ use std::str::FromStr; // Codegen from embedded Solidity code and precompiled bytecode. // solc v0.8.24; solc a.sol --via-ir --optimize --bin sol!( - #[derive(Debug)] #[sol(rpc, bytecode = "0x6080806040523461001657610213908161001b8239f35b5f80fdfe6080604052600480361015610012575f80fd5b5f3560e01c80634350913814610165578063a5f3c23b14610116578063adefc37b146100c75763bbe93d9114610046575f80fd5b346100c357610054366101c7565b8181029291905f8212600160ff1b8214166100b057818405149015171561009d5750337fd7a123d4c8e44db3186e04b9c96c102287276929c930f2e8abcaa555ef5dcacc5f80a3005b601190634e487b7160e01b5f525260245ffd5b601183634e487b7160e01b5f525260245ffd5b5f80fd5b50346100c3576100d6366101c7565b91905f838203931281841281169184139015161761009d5750337f32e913bf2ad35da1e845597618bb9f3f80642a68dd39f30a093a7838aa61fb275f80a3005b50346100c357610125366101c7565b91905f838201938412911290801582169115161761009d5750337f6da406ea462447ed7804b4a4dc69c67b53d3d45a50381ae3e9cf878c9d7c23df5f80a3005b50346100c357610174366101c7565b9182156101b457600160ff1b82145f1984141661009d575005337f1c1e8bbe327890ea8d3f5b22370a56c3fcef7ff82f306161f64647fe5d2858815f80a3005b601290634e487b7160e01b5f525260245ffd5b60409060031901126100c357600435906024359056fea26469706673582212208fc27b878cb877b8c4e5dee739e0a35a64f82759549ed8f6d4ddab5ded9717bb64736f6c63430008180033")] contract EventMultiplexer { event Add(address indexed sender, int256 indexed value); From c74d4ed669481239b5b5504e70bb4019c4002190 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 15:55:00 +0000 Subject: [PATCH 10/23] additional comments, best practices --- .../examples/query_contract_storage.rs | 6 ++---- .../examples/query_deployed_bytecode.rs | 6 +++--- examples/queries/examples/query_logs.rs | 21 ++++++++++++------- .../transactions/examples/decode_input.rs | 11 +++++----- .../transactions/examples/gas_price_usd.rs | 6 +++--- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/examples/queries/examples/query_contract_storage.rs b/examples/queries/examples/query_contract_storage.rs index 6e3c29b3..d5b8f04e 100644 --- a/examples/queries/examples/query_contract_storage.rs +++ b/examples/queries/examples/query_contract_storage.rs @@ -1,4 +1,4 @@ -//! Example of querying contract storage from the Ethereum network. +//! Example of querying contract storage on the Ethereum network. use alloy::{ network::Ethereum, @@ -13,11 +13,9 @@ async fn main() -> Result<()> { let rpc_url = "https://eth.merkle.io".parse()?; let provider = RootProvider::::new_http(rpc_url); - // Get storage slot 0 from USDC-ETH Uniswap V3 pool. + // Get storage slot 0 from the Uniswap V3 USDC-ETH pool on Ethereum mainnet. let pool_address = address!("88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"); - let storage_slot = U256::from(0); - let storage = provider.get_storage_at(pool_address, storage_slot, None).await?; println!("Slot 0: {:?}", storage); diff --git a/examples/queries/examples/query_deployed_bytecode.rs b/examples/queries/examples/query_deployed_bytecode.rs index 11551354..4a2b73b3 100644 --- a/examples/queries/examples/query_deployed_bytecode.rs +++ b/examples/queries/examples/query_deployed_bytecode.rs @@ -1,4 +1,4 @@ -//! Example of querying deployed bytecode of a contract on Ethereum network. +//! Example of querying deployed bytecode of a contract on the Ethereum network. use alloy::{ network::Ethereum, @@ -10,12 +10,12 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { + // Create a provider. let rpc_url = "https://eth.merkle.io".parse()?; let provider = RootProvider::::new_http(rpc_url); - // Get bytecode of USDC-ETH Uniswap V3 pool + // Get the bytecode of the Uniswap V3 USDC-ETH pool on Ethereum mainnet. let pool_address = address!("88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"); - let bytecode = provider.get_code_at(pool_address, BlockId::Number(BlockNumberOrTag::Latest)).await?; diff --git a/examples/queries/examples/query_logs.rs b/examples/queries/examples/query_logs.rs index caddd843..b5c1f081 100644 --- a/examples/queries/examples/query_logs.rs +++ b/examples/queries/examples/query_logs.rs @@ -1,3 +1,5 @@ +//! Example of querying logs from the Ethereum network. + use alloy::{ network::Ethereum, primitives::{address, fixed_bytes}, @@ -5,46 +7,49 @@ use alloy::{ rpc::types::eth::Filter, }; use eyre::Result; + #[tokio::main] async fn main() -> Result<()> { + // Create a provider. let rpc_url = "https://eth.merkle.io".parse()?; let provider = RootProvider::::new_http(rpc_url); // Get logs from the latest block let latest_block = provider.get_block_number().await?; - // Get all logs from the latest block + // Create a filter to get all logs from the latest block. let filter = Filter::new().from_block(latest_block); + // Get all logs from the latest block that match the filter. let logs = provider.get_logs(&filter).await?; for log in logs { println!("{:?}", log); } + // Get all logs from the latest block that match the transfer event signature/topic. let tranfer_event_signature = fixed_bytes!("ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"); - - // Get all logs from the latest block that match the transfer event signature/topic let filter = Filter::new().event_signature(tranfer_event_signature).from_block(latest_block); // You could also use the event name instead of the event signature like so: // .event("Transfer(address,address,uint256)") + // Get all logs from the latest block that match the filter. let logs = provider.get_logs(&filter).await?; for log in logs { println!("Transfer event: {:?}", log); } - let uni_address = address!("1f9840a85d5aF5bf1D1762F925BDADdC4201F984"); - - // Get all from the latest block emitted by the UNI token address - let filter = Filter::new().address(uni_address).from_block(latest_block); + // Get all from the latest block emitted by the UNI token address. + let uniswap_token_address = address!("1f9840a85d5aF5bf1D1762F925BDADdC4201F984"); + let filter = Filter::new().address(uniswap_token_address).from_block(latest_block); + // Get all logs from the latest block that match the filter. let logs = provider.get_logs(&filter).await?; for log in logs { - println!("UNI token logs: {:?}", log); + println!("Uniswap token logs: {:?}", log); } Ok(()) diff --git a/examples/transactions/examples/decode_input.rs b/examples/transactions/examples/decode_input.rs index 4fc1c376..7c37c361 100644 --- a/examples/transactions/examples/decode_input.rs +++ b/examples/transactions/examples/decode_input.rs @@ -3,15 +3,14 @@ use alloy::{primitives::hex, sol, sol_types::SolCall}; use eyre::Result; -// Use the sol! macro to generate bindings for the swapExactTokensForTokens function +// Codegen from excerpt of Uniswap V2 interface. sol!( - #[derive(Debug)] function swapExactTokensForTokens( - uint amountIn, - uint amountOutMin, + uint256 amountIn, + uint256 amountOutMin, address[] calldata path, address to, - uint deadline + uint256 deadline ) external returns (uint[] memory amounts); ); @@ -22,7 +21,7 @@ async fn main() -> Result<()> { let input = "0x38ed173900000000000000000000000000000000000000000001a717cc0a3e4f84c00000000000000000000000000000000000000000000000000000000000000283568400000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000201f129111c60401630932d9f9811bd5b5fff34e000000000000000000000000000000000000000000000000000000006227723d000000000000000000000000000000000000000000000000000000000000000200000000000000000000000095ad61b0a150d79219dcf64e1e6cc01f0b64c4ce000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7"; let input = hex::decode(input)?; - // swapExactTokensForTokensCall generated by the sol! macro above + // Decode the input using the generated `swapExactTokensForTokens` bindings. let decoded = swapExactTokensForTokensCall::abi_decode(&input, false); match decoded { diff --git a/examples/transactions/examples/gas_price_usd.rs b/examples/transactions/examples/gas_price_usd.rs index 97c45cdc..e32a6afb 100644 --- a/examples/transactions/examples/gas_price_usd.rs +++ b/examples/transactions/examples/gas_price_usd.rs @@ -16,8 +16,8 @@ const ETH_USD_FEED: Address = address!("5f4eC3Df9cbd43714FE2740f5E3616155c5b8419 const ETH_USD_FEED_DECIMALS: u8 = 8; const ETH_DECIMALS: u32 = 18; +// Codegen from excerpt of Chainlink Aggregator interface. sol!( - #[derive(Debug)] function latestAnswer() external view returns (int256); ); @@ -48,8 +48,8 @@ async fn main() -> Result<()> { } fn get_usd_value(amount: U256, price_usd: U256) -> Result { - let base: U256 = U256::from(10).pow(U256::from(ETH_DECIMALS)); - let value: U256 = amount * price_usd / base; + let base = U256::from(10).pow(U256::from(ETH_DECIMALS)); + let value = amount * price_usd / base; let formatted = format_units(value, ETH_USD_FEED_DECIMALS)?.parse::()?; Ok(formatted) From 12ebf146418d49dbfb2b2ef37891674d0a717e31 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 15:57:46 +0000 Subject: [PATCH 11/23] improve consistency; --- examples/transactions/examples/gas_price_usd.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/examples/transactions/examples/gas_price_usd.rs b/examples/transactions/examples/gas_price_usd.rs index e32a6afb..21148284 100644 --- a/examples/transactions/examples/gas_price_usd.rs +++ b/examples/transactions/examples/gas_price_usd.rs @@ -31,13 +31,19 @@ async fn main() -> Result<()> { let rpc_url = anvil.endpoint().parse()?; let provider = HttpProvider::::new_http(rpc_url); + // Create a call to get the latest answer from the Chainlink ETH/USD feed. let call = latestAnswerCall {}.abi_encode(); let input = Bytes::from(call); + + // Call the Chainlink ETH/USD feed contract. let tx = TransactionRequest::default().to(Some(ETH_USD_FEED)).input(Some(input).into()); let response = provider.call(&tx, None).await?; - let result = U256::from_str(response.to_string().as_str())?; + let result = U256::from_str(&response.to_string())?; + // Get the gas price of the network. let wei_per_gas = provider.get_gas_price().await?; + + // Convert the gas price to Gwei and USD. let gwei = format_units(wei_per_gas, "gwei")?.parse::()?; let usd = get_usd_value(wei_per_gas, result)?; From 092e4cdc1f6c3dbf5aa7b9fd5850c89b562662bd Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 16:08:27 +0000 Subject: [PATCH 12/23] improve readability --- examples/transactions/examples/trace_call.rs | 4 ++++ .../examples/trace_transaction.rs | 4 ++-- .../transactions/examples/transfer_erc20.rs | 4 ++++ .../transactions/examples/transfer_eth.rs | 20 ++++++++++++------- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/examples/transactions/examples/trace_call.rs b/examples/transactions/examples/trace_call.rs index e7ddcb17..6e416a58 100644 --- a/examples/transactions/examples/trace_call.rs +++ b/examples/transactions/examples/trace_call.rs @@ -17,15 +17,19 @@ async fn main() -> Result<()> { let rpc_url = "https://eth.merkle.io".parse()?; let provider = HttpProvider::::new_http(rpc_url); + // Create two users, Alice and Bob. let alice = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266"); let bob = address!("70997970C51812dc3A010C7d01b50e0d17dc79C8"); + // Create a transaction to send 100 wei from Alice to Bob. let tx = TransactionRequest { from: Some(alice), to: Some(bob), value: Some(U256::from(100)), ..Default::default() }; + + // Trace the transaction on top of the latest block. let trace_type = [TraceType::Trace]; let result = provider .trace_call(&tx, &trace_type, Some(BlockId::Number(BlockNumberOrTag::Latest))) diff --git a/examples/transactions/examples/trace_transaction.rs b/examples/transactions/examples/trace_transaction.rs index 6f7ed3d2..abcda04c 100644 --- a/examples/transactions/examples/trace_transaction.rs +++ b/examples/transactions/examples/trace_transaction.rs @@ -31,7 +31,7 @@ async fn main() -> Result<()> { println!("DEFAULT_TRACE: {:?}", result); - // Call tracing + // Trace with built-in call tracer. let call_options = GethDebugTracingOptions { config: GethDefaultTracingOptions { disable_storage: Some(true), @@ -45,7 +45,7 @@ async fn main() -> Result<()> { println!("CALL_TRACE: {:?}", result); - // JS tracer + // Trace using a custom JavaScript tracer. let js_options = GethDebugTracingOptions { tracer: Some(GethDebugTracerType::JsTracer("{data: [], fault: function(log) {}, step: function(log) { if(log.op.toString() == \"DELEGATECALL\") this.data.push(log.stack.peek(0)); }, result: function() { return this.data; }}".into())), ..Default::default() diff --git a/examples/transactions/examples/transfer_erc20.rs b/examples/transactions/examples/transfer_erc20.rs index 29c37ac1..9cbe290b 100644 --- a/examples/transactions/examples/transfer_erc20.rs +++ b/examples/transactions/examples/transfer_erc20.rs @@ -28,11 +28,14 @@ async fn main() -> Result<()> { let alice = anvil.addresses()[0]; let bob = anvil.addresses()[1]; + // Deploy the `ERC20Example` contract. let contract_address = deploy_token_contract(&provider, alice).await?; + // Create the transaction input to transfer 100 tokens from Alice to Bob. let input = ERC20Example::transferCall { to: bob, amount: U256::from(100) }.abi_encode(); let input = Bytes::from(input); + // Create a transaction with the input. let tx = TransactionRequest { from: Some(alice), to: Some(contract_address), @@ -45,6 +48,7 @@ async fn main() -> Result<()> { println!("Send transaction: {:?}", receipt.transaction_hash); + // Check the balances of Alice and Bob after the transfer. let alice_balance = balance_of(&provider, alice, contract_address).await?; let bob_balance = balance_of(&provider, bob, contract_address).await?; diff --git a/examples/transactions/examples/transfer_eth.rs b/examples/transactions/examples/transfer_eth.rs index db2d7921..f61c2c1b 100644 --- a/examples/transactions/examples/transfer_eth.rs +++ b/examples/transactions/examples/transfer_eth.rs @@ -23,18 +23,24 @@ async fn main() -> Result<()> { let alice = anvil.addresses()[0]; let bob = anvil.addresses()[1]; - // Transfer 1 wei from Alice to Bob. + // Create a transaction to transfer 1 wei from Alice to Bob. let tx = TransactionRequest::default().from(alice).value(U256::from(1)).to(Some(bob)); + + // Send the transaction. let pending_tx = provider.send_transaction(tx).await?; - let hash = pending_tx.tx_hash(); - println!("Pending transaction hash: {}", hash); + println!("Pending transaction...{:?}", pending_tx.tx_hash()); + + // Wait for the transaction to be included. + let receipt = pending_tx.get_receipt().await?; - let transaction = provider.get_transaction_by_hash(hash.to_owned()).await?; + println!( + "Transaction included in block: {:?}", + receipt.block_number.expect("Failed to get block number").to_string() + ); - assert_eq!(transaction.from, alice); - assert_eq!(transaction.to, Some(bob)); - assert_eq!(transaction.value, U256::from(1)); + assert_eq!(receipt.from, alice); + assert_eq!(receipt.to, Some(bob)); Ok(()) } From 8d4f530ed84e28a3aeb5000f070bf0eb8f1e7eeb Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 16:10:28 +0000 Subject: [PATCH 13/23] add links --- examples/transactions/examples/decode_input.rs | 5 +++-- examples/transactions/examples/gas_price_usd.rs | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/transactions/examples/decode_input.rs b/examples/transactions/examples/decode_input.rs index 7c37c361..0135b00b 100644 --- a/examples/transactions/examples/decode_input.rs +++ b/examples/transactions/examples/decode_input.rs @@ -3,7 +3,8 @@ use alloy::{primitives::hex, sol, sol_types::SolCall}; use eyre::Result; -// Codegen from excerpt of Uniswap V2 interface. +// Codegen from excerpt of Uniswap V2 Router interface. +// See: https://docs.uniswap.org/contracts/v2/reference/smart-contracts/router-02 sol!( function swapExactTokensForTokens( uint256 amountIn, @@ -11,7 +12,7 @@ sol!( address[] calldata path, address to, uint256 deadline - ) external returns (uint[] memory amounts); + ) external returns (uint256[] memory amounts); ); #[tokio::main] diff --git a/examples/transactions/examples/gas_price_usd.rs b/examples/transactions/examples/gas_price_usd.rs index 21148284..ec193eac 100644 --- a/examples/transactions/examples/gas_price_usd.rs +++ b/examples/transactions/examples/gas_price_usd.rs @@ -17,6 +17,7 @@ const ETH_USD_FEED_DECIMALS: u8 = 8; const ETH_DECIMALS: u32 = 18; // Codegen from excerpt of Chainlink Aggregator interface. +// See: https://etherscan.io/address/0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419#code sol!( function latestAnswer() external view returns (int256); ); From a02938740080d18bc59bc0334054d49869e92743 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 16:20:06 +0000 Subject: [PATCH 14/23] move out users into own block --- examples/layers/examples/nonce_layer.rs | 7 ++++++- examples/layers/examples/recommended_layers.rs | 7 ++++++- examples/layers/examples/signer_layer.rs | 7 ++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/examples/layers/examples/nonce_layer.rs b/examples/layers/examples/nonce_layer.rs index 7c39c421..72e8c670 100644 --- a/examples/layers/examples/nonce_layer.rs +++ b/examples/layers/examples/nonce_layer.rs @@ -28,6 +28,10 @@ async fn main() -> Result<()> { // Set up signer. let signer: LocalWallet = anvil.keys()[0].clone().into(); + // Create two users, Alice and Vitalik. + let alice = signer.address(); + let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"); + // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() @@ -40,7 +44,8 @@ async fn main() -> Result<()> { // Create an EIP-1559 type transaction. let tx = TransactionRequest::default() - .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()) + .with_from(alice) + .with_to(vitalik.into()) .with_value(U256::from(100)) // Notice that without the `GasEstimatorLayer`, you need to set the gas related fields. .with_gas_limit(U256::from(21000)) diff --git a/examples/layers/examples/recommended_layers.rs b/examples/layers/examples/recommended_layers.rs index 045b31ee..2009a092 100644 --- a/examples/layers/examples/recommended_layers.rs +++ b/examples/layers/examples/recommended_layers.rs @@ -19,6 +19,10 @@ async fn main() -> Result<()> { // Set up signer. let signer: LocalWallet = anvil.keys()[0].clone().into(); + // Create two users, Alice and Vitalik. + let alice = signer.address(); + let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"); + // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() @@ -34,7 +38,8 @@ async fn main() -> Result<()> { // Notice that the `nonce` field is set by the `ManagedNonceLayer`. // Notice that without the `GasEstimatorLayer`, you need to set the gas related fields. let tx = TransactionRequest::default() - .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()) + .with_from(alice) + .with_to(vitalik.into()) .with_value(U256::from(100)) .with_chain_id(anvil.chain_id()); diff --git a/examples/layers/examples/signer_layer.rs b/examples/layers/examples/signer_layer.rs index 9cbc76d1..fe1241ff 100644 --- a/examples/layers/examples/signer_layer.rs +++ b/examples/layers/examples/signer_layer.rs @@ -19,6 +19,10 @@ async fn main() -> Result<()> { // Set up signer. let signer: LocalWallet = anvil.keys()[0].clone().into(); + // Create two users, Alice and Vitalik. + let alice = signer.address(); + let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"); + // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() @@ -28,9 +32,10 @@ async fn main() -> Result<()> { // Create a legacy type transaction. let tx = TransactionRequest::default() + .with_from(alice) // Notice that without the `ManagedNonceLayer`, you need to manually set the nonce field. .with_nonce(0) - .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()) + .with_to(vitalik.into()) .with_value(U256::from(100)) // Notice that without the `GasEstimatorLayer`, you need to set the gas related fields. .with_gas_price(U256::from(20e9)) From bf8c831b4602d498f67bacca37599f8731c292bd Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 16:33:34 +0000 Subject: [PATCH 15/23] broadcast -> send --- examples/providers/examples/builder.rs | 7 ++++++- examples/queries/examples/query_contract_storage.rs | 2 +- examples/subscriptions/examples/event_multiplexer.rs | 6 +++--- examples/subscriptions/examples/watch_contract_event.rs | 2 +- examples/transactions/examples/transfer_erc20.rs | 4 ++-- examples/transactions/examples/transfer_eth.rs | 2 +- examples/wallets/examples/keystore_signer.rs | 4 ++-- examples/wallets/examples/ledger_signer.rs | 2 +- examples/wallets/examples/private_key_signer.rs | 4 ++-- examples/wallets/examples/trezor_signer.rs | 2 +- examples/wallets/examples/yubi_signer.rs | 2 +- 11 files changed, 21 insertions(+), 16 deletions(-) diff --git a/examples/providers/examples/builder.rs b/examples/providers/examples/builder.rs index 75aac39d..11a02e11 100644 --- a/examples/providers/examples/builder.rs +++ b/examples/providers/examples/builder.rs @@ -38,7 +38,7 @@ async fn main() -> Result<()> { tx.set_gas_price(U256::from(20e9)); - // Send the transaction. + // Send the transaction and wait for the receipt. let pending_tx = provider_with_signer.send_transaction(tx).await?; println!("Pending transaction...{:?}", pending_tx.tx_hash()); @@ -46,6 +46,11 @@ async fn main() -> Result<()> { // Wait for the transaction to be included. let receipt = pending_tx.get_receipt().await?; + println!( + "Transaction included in block: {:?}", + receipt.block_number.expect("Failed to get block number").to_string() + ); + assert_eq!(receipt.from, alice); assert_eq!(receipt.to, Some(bob)); diff --git a/examples/queries/examples/query_contract_storage.rs b/examples/queries/examples/query_contract_storage.rs index d5b8f04e..bfcaee12 100644 --- a/examples/queries/examples/query_contract_storage.rs +++ b/examples/queries/examples/query_contract_storage.rs @@ -1,4 +1,4 @@ -//! Example of querying contract storage on the Ethereum network. +//! Example of querying contract storage from the Ethereum network. use alloy::{ network::Ethereum, diff --git a/examples/subscriptions/examples/event_multiplexer.rs b/examples/subscriptions/examples/event_multiplexer.rs index 9f9098ef..d0f064e1 100644 --- a/examples/subscriptions/examples/event_multiplexer.rs +++ b/examples/subscriptions/examples/event_multiplexer.rs @@ -1,4 +1,4 @@ -//! Example of multiplexing watching event logs. +//! Example of multiplexing the watching of event logs. use alloy::{network::Ethereum, node_bindings::Anvil, primitives::I256, sol, sol_types::SolEvent}; use alloy_provider::RootProvider; @@ -59,13 +59,13 @@ async fn main() -> Result<()> { let a = I256::from_str("1").unwrap(); let b = I256::from_str("1").unwrap(); - // Build calls. + // Build the transaction calls. let add_call = contract.add(a, b); let sub_call = contract.sub(a, b); let mul_call = contract.mul(a, b); let div_call = contract.div(a, b); - // Send calls. + // Send the transaction calls. let _ = add_call.send().await?; let _ = sub_call.send().await?; let _ = mul_call.send().await?; diff --git a/examples/subscriptions/examples/watch_contract_event.rs b/examples/subscriptions/examples/watch_contract_event.rs index 691ecaab..50ac5b75 100644 --- a/examples/subscriptions/examples/watch_contract_event.rs +++ b/examples/subscriptions/examples/watch_contract_event.rs @@ -54,7 +54,7 @@ async fn main() -> Result<()> { // Build a call to decrement the counter. let decrement_call = contract.decrement(); - // Send the call 2 times. + // Send the transaction call twice for each event. for _ in 0..2 { let _ = increment_call.send().await?; let _ = decrement_call.send().await?; diff --git a/examples/transactions/examples/transfer_erc20.rs b/examples/transactions/examples/transfer_erc20.rs index 9cbe290b..a6c618c7 100644 --- a/examples/transactions/examples/transfer_erc20.rs +++ b/examples/transactions/examples/transfer_erc20.rs @@ -43,7 +43,7 @@ async fn main() -> Result<()> { ..Default::default() }; - // Broadcast the transaction and wait for the receipt. + // Send the transaction and wait for the receipt. let receipt = provider.send_transaction(tx).await?.get_receipt().await?; println!("Send transaction: {:?}", receipt.transaction_hash); @@ -73,7 +73,7 @@ async fn deploy_token_contract( ..Default::default() }; - // Broadcast the transaction and wait for the receipt. + // Send the transaction and wait for the receipt. let receipt = provider.send_transaction(tx).await?.get_receipt().await?; // Get the contract address. diff --git a/examples/transactions/examples/transfer_eth.rs b/examples/transactions/examples/transfer_eth.rs index f61c2c1b..18f1ece7 100644 --- a/examples/transactions/examples/transfer_eth.rs +++ b/examples/transactions/examples/transfer_eth.rs @@ -26,7 +26,7 @@ async fn main() -> Result<()> { // Create a transaction to transfer 1 wei from Alice to Bob. let tx = TransactionRequest::default().from(alice).value(U256::from(1)).to(Some(bob)); - // Send the transaction. + // Send the transaction and wait for the receipt. let pending_tx = provider.send_transaction(tx).await?; println!("Pending transaction...{:?}", pending_tx.tx_hash()); diff --git a/examples/wallets/examples/keystore_signer.rs b/examples/wallets/examples/keystore_signer.rs index 545e0f33..fc4bd241 100644 --- a/examples/wallets/examples/keystore_signer.rs +++ b/examples/wallets/examples/keystore_signer.rs @@ -1,4 +1,4 @@ -//! Example of using a keystore wallet to sign and broadcast a transaction on a local Anvil node. +//! Example of using a keystore wallet to sign and send a transaction. use alloy::{ network::EthereumSigner, @@ -41,7 +41,7 @@ async fn main() -> Result<()> { ..Default::default() }; - // Broadcast the transaction and wait for the receipt. + // Send the transaction and wait for the receipt. let receipt = provider.send_transaction(tx).await?.get_receipt().await?; println!("Send transaction: {:?}", receipt.transaction_hash); diff --git a/examples/wallets/examples/ledger_signer.rs b/examples/wallets/examples/ledger_signer.rs index af650eab..1e40a95a 100644 --- a/examples/wallets/examples/ledger_signer.rs +++ b/examples/wallets/examples/ledger_signer.rs @@ -29,7 +29,7 @@ async fn main() -> Result<()> { ..Default::default() }; - // Broadcast the transaction and wait for the receipt. + // Send the transaction and wait for the receipt. let receipt = provider.send_transaction(tx).await?.with_required_confirmations(3).get_receipt().await?; diff --git a/examples/wallets/examples/private_key_signer.rs b/examples/wallets/examples/private_key_signer.rs index 9b0dd380..05cea52c 100644 --- a/examples/wallets/examples/private_key_signer.rs +++ b/examples/wallets/examples/private_key_signer.rs @@ -1,4 +1,4 @@ -//! Example of using a local wallet to sign and broadcast a transaction on a local Anvil node. +//! Example of using a local wallet to sign and send a transaction. use alloy::{ network::EthereumSigner, @@ -34,7 +34,7 @@ async fn main() -> Result<()> { ..Default::default() }; - // Broadcast the transaction and wait for the receipt. + // Send the transaction and wait for the receipt. let receipt = provider.send_transaction(tx).await?.get_receipt().await?; println!("Send transaction: {:?}", receipt.transaction_hash); diff --git a/examples/wallets/examples/trezor_signer.rs b/examples/wallets/examples/trezor_signer.rs index e7e50f33..079b529c 100644 --- a/examples/wallets/examples/trezor_signer.rs +++ b/examples/wallets/examples/trezor_signer.rs @@ -29,7 +29,7 @@ async fn main() -> Result<()> { ..Default::default() }; - // Broadcast the transaction and wait for the receipt. + // Send the transaction and wait for the receipt. let receipt = provider.send_transaction(tx).await?.with_required_confirmations(3).get_receipt().await?; diff --git a/examples/wallets/examples/yubi_signer.rs b/examples/wallets/examples/yubi_signer.rs index 35683242..805ee7a8 100644 --- a/examples/wallets/examples/yubi_signer.rs +++ b/examples/wallets/examples/yubi_signer.rs @@ -38,7 +38,7 @@ async fn main() -> Result<()> { ..Default::default() }; - // Broadcast the transaction and wait for the receipt. + // Send the transaction and wait for the receipt. let receipt = provider.send_transaction(tx).await?.with_required_confirmations(3).get_receipt().await?; From ca8d3d189e9e71f489da1bc3e7172324da38bbb7 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 16:41:29 +0000 Subject: [PATCH 16/23] use trailing . for comments --- examples/providers/examples/builder.rs | 2 +- examples/providers/examples/ws_with_auth.rs | 6 +++--- examples/subscriptions/examples/event_multiplexer.rs | 4 ++-- examples/transactions/examples/transfer_erc20.rs | 2 +- examples/transactions/examples/transfer_eth.rs | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/providers/examples/builder.rs b/examples/providers/examples/builder.rs index 11a02e11..44440064 100644 --- a/examples/providers/examples/builder.rs +++ b/examples/providers/examples/builder.rs @@ -13,7 +13,7 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { // Spin up a local Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().block_time(1).try_spawn()?; // Set up signer. diff --git a/examples/providers/examples/ws_with_auth.rs b/examples/providers/examples/ws_with_auth.rs index b9456cfa..09e96d99 100644 --- a/examples/providers/examples/ws_with_auth.rs +++ b/examples/providers/examples/ws_with_auth.rs @@ -35,21 +35,21 @@ async fn main() -> Result<()> { println!("Awaiting blocks..."); - // Spawning the block processing for basic auth as a new task + // Spawning the block processing for basic auth as a new task. let basic_handle = tokio::spawn(async move { while let Some(block) = stream_basic.next().await { println!("From basic {:?}", block.header.number); } }); - // Similarly for bearer auth + // Similarly for bearer auth. let bearer_handle = tokio::spawn(async move { while let Some(block) = stream_bearer.next().await { println!("From bearer {:?}", block.header.number); } }); - // Wait for both tasks to complete + // Wait for both tasks to complete. let _ = tokio::try_join!(basic_handle, bearer_handle)?; Ok(()) diff --git a/examples/subscriptions/examples/event_multiplexer.rs b/examples/subscriptions/examples/event_multiplexer.rs index d0f064e1..68b3d89e 100644 --- a/examples/subscriptions/examples/event_multiplexer.rs +++ b/examples/subscriptions/examples/event_multiplexer.rs @@ -38,7 +38,7 @@ sol!( #[tokio::main] async fn main() -> Result<()> { // Spin up a local Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().block_time(1).try_spawn()?; // Create a provider. @@ -84,7 +84,7 @@ async fn main() -> Result<()> { // Use tokio::select! to multiplex the streams and capture the log // tokio::select! will return the first event that arrives from any of the streams - // The for loop helps capture all the logs + // The for loop helps capture all the logs. for _ in 0..4 { let log = tokio::select! { Some(log) = add_stream.next() => { diff --git a/examples/transactions/examples/transfer_erc20.rs b/examples/transactions/examples/transfer_erc20.rs index a6c618c7..b9360da7 100644 --- a/examples/transactions/examples/transfer_erc20.rs +++ b/examples/transactions/examples/transfer_erc20.rs @@ -17,7 +17,7 @@ sol!(ERC20Example, "examples/contracts/ERC20Example.json"); #[tokio::main] async fn main() -> Result<()> { // Spin up a forked Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; // Create a provider. diff --git a/examples/transactions/examples/transfer_eth.rs b/examples/transactions/examples/transfer_eth.rs index 18f1ece7..4db7f451 100644 --- a/examples/transactions/examples/transfer_eth.rs +++ b/examples/transactions/examples/transfer_eth.rs @@ -12,7 +12,7 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { // Spin up a forked Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; // Create a provider. From 1801d55f81ba93ef02391de91f28d4340e1e54b0 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Wed, 27 Mar 2024 17:20:52 +0000 Subject: [PATCH 17/23] clean up comments --- examples/anvil/examples/deploy_contract_anvil.rs | 4 ++-- examples/anvil/examples/fork_anvil.rs | 2 +- examples/anvil/examples/local_anvil.rs | 2 +- examples/contracts/examples/deploy_from_artifact.rs | 4 ++-- examples/contracts/examples/deploy_from_contract.rs | 4 ++-- examples/contracts/examples/generate.rs | 2 +- examples/layers/examples/nonce_layer.rs | 4 ++-- examples/layers/examples/recommended_layers.rs | 4 ++-- examples/layers/examples/signer_layer.rs | 4 ++-- examples/providers/examples/builder.rs | 4 ++-- examples/providers/examples/http.rs | 2 +- examples/providers/examples/ipc.rs | 7 ++++--- examples/providers/examples/ws.rs | 7 +++++++ examples/providers/examples/ws_with_auth.rs | 10 +++++++--- examples/subscriptions/examples/subscribe_blocks.rs | 2 +- .../subscriptions/examples/watch_contract_event.rs | 2 +- examples/transactions/examples/gas_price_usd.rs | 2 +- examples/transactions/examples/trace_transaction.rs | 2 +- examples/wallets/examples/keystore_signer.rs | 2 +- examples/wallets/examples/private_key_signer.rs | 2 +- examples/wallets/examples/sign_message.rs | 4 ++-- examples/wallets/examples/sign_permit_hash.rs | 2 +- 22 files changed, 45 insertions(+), 33 deletions(-) diff --git a/examples/anvil/examples/deploy_contract_anvil.rs b/examples/anvil/examples/deploy_contract_anvil.rs index fd375816..d929b8ab 100644 --- a/examples/anvil/examples/deploy_contract_anvil.rs +++ b/examples/anvil/examples/deploy_contract_anvil.rs @@ -31,10 +31,10 @@ sol! { #[tokio::main] async fn main() -> Result<()> { // Spin up a local Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().try_spawn()?; - // Set up signer. + // Set up signer from the first default Anvil account (Alice). let signer: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with a signer and the network. diff --git a/examples/anvil/examples/fork_anvil.rs b/examples/anvil/examples/fork_anvil.rs index 345a8ead..03ff1ca5 100644 --- a/examples/anvil/examples/fork_anvil.rs +++ b/examples/anvil/examples/fork_anvil.rs @@ -6,7 +6,7 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { // Spin up a forked Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; println!("Anvil running at `{}`", anvil.endpoint()); diff --git a/examples/anvil/examples/local_anvil.rs b/examples/anvil/examples/local_anvil.rs index c7a1a9ef..c69d7ab1 100644 --- a/examples/anvil/examples/local_anvil.rs +++ b/examples/anvil/examples/local_anvil.rs @@ -6,7 +6,7 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { // Spin up a local Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().block_time(1).chain_id(1337).try_spawn()?; println!("Anvil running at `{}`", anvil.endpoint()); diff --git a/examples/contracts/examples/deploy_from_artifact.rs b/examples/contracts/examples/deploy_from_artifact.rs index 5de26b16..ce80fa6d 100644 --- a/examples/contracts/examples/deploy_from_artifact.rs +++ b/examples/contracts/examples/deploy_from_artifact.rs @@ -21,10 +21,10 @@ sol!( #[tokio::main] async fn main() -> Result<()> { // Spin up a local Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().try_spawn()?; - // Set up signer. + // Set up signer from the first default Anvil account (Alice). let signer: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with a signer. diff --git a/examples/contracts/examples/deploy_from_contract.rs b/examples/contracts/examples/deploy_from_contract.rs index 19dc6e6b..ad7a2c52 100644 --- a/examples/contracts/examples/deploy_from_contract.rs +++ b/examples/contracts/examples/deploy_from_contract.rs @@ -31,10 +31,10 @@ sol! { #[tokio::main] async fn main() -> Result<()> { // Spin up a local Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().try_spawn()?; - // Set up wallet + // Set up signer from the first default Anvil account (Alice). let signer: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with a signer. diff --git a/examples/contracts/examples/generate.rs b/examples/contracts/examples/generate.rs index 4c7500a5..f521c7d6 100644 --- a/examples/contracts/examples/generate.rs +++ b/examples/contracts/examples/generate.rs @@ -13,7 +13,7 @@ sol!( #[tokio::main] async fn main() -> Result<()> { // Spin up a forked Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; // Create a provider. diff --git a/examples/layers/examples/nonce_layer.rs b/examples/layers/examples/nonce_layer.rs index 72e8c670..f7d8a237 100644 --- a/examples/layers/examples/nonce_layer.rs +++ b/examples/layers/examples/nonce_layer.rs @@ -22,10 +22,10 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { // Spin up a local Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().try_spawn()?; - // Set up signer. + // Set up signer from the first default Anvil account (Alice). let signer: LocalWallet = anvil.keys()[0].clone().into(); // Create two users, Alice and Vitalik. diff --git a/examples/layers/examples/recommended_layers.rs b/examples/layers/examples/recommended_layers.rs index 2009a092..7d274c87 100644 --- a/examples/layers/examples/recommended_layers.rs +++ b/examples/layers/examples/recommended_layers.rs @@ -13,10 +13,10 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { // Spin up a local Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().try_spawn()?; - // Set up signer. + // Set up signer from the first default Anvil account (Alice). let signer: LocalWallet = anvil.keys()[0].clone().into(); // Create two users, Alice and Vitalik. diff --git a/examples/layers/examples/signer_layer.rs b/examples/layers/examples/signer_layer.rs index fe1241ff..d204a4e9 100644 --- a/examples/layers/examples/signer_layer.rs +++ b/examples/layers/examples/signer_layer.rs @@ -13,10 +13,10 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { // Spin up a local Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().try_spawn()?; - // Set up signer. + // Set up signer from the first default Anvil account (Alice). let signer: LocalWallet = anvil.keys()[0].clone().into(); // Create two users, Alice and Vitalik. diff --git a/examples/providers/examples/builder.rs b/examples/providers/examples/builder.rs index 44440064..bf626acd 100644 --- a/examples/providers/examples/builder.rs +++ b/examples/providers/examples/builder.rs @@ -16,14 +16,14 @@ async fn main() -> Result<()> { // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().block_time(1).try_spawn()?; - // Set up signer. + // Set up signer from the first default Anvil account (Alice). let wallet: LocalWallet = anvil.keys()[0].clone().into(); // Create two users, Alice and Bob. let alice = wallet.address(); let bob = anvil.addresses()[1]; - // Setup the HTTP transport which is consumed by the RPC client. + // Set up the HTTP transport which is consumed by the RPC client. let rpc_client = RpcClient::new_http(anvil.endpoint().parse()?); let provider_with_signer = ProviderBuilder::new() .signer(EthereumSigner::from(wallet)) diff --git a/examples/providers/examples/http.rs b/examples/providers/examples/http.rs index 39eda8e7..a1517b4c 100644 --- a/examples/providers/examples/http.rs +++ b/examples/providers/examples/http.rs @@ -9,7 +9,7 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { - // Setup the HTTP transport which is consumed by the RPC client. + // Set up the HTTP transport which is consumed by the RPC client. let rpc_url = "https://eth.merkle.io".parse()?; // Create the RPC client. diff --git a/examples/providers/examples/ipc.rs b/examples/providers/examples/ipc.rs index 078a6b86..6aace60f 100644 --- a/examples/providers/examples/ipc.rs +++ b/examples/providers/examples/ipc.rs @@ -10,15 +10,16 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { - // Setup the IPC transport which is consumed by the RPC client + // Set up the IPC transport which is consumed by the RPC client. let ipc_path = "/tmp/reth.ipc"; - // IPC transport + // Create the IPC connection object. let ipc = IpcConnect::new(ipc_path.to_string()); - // RPC client using IPC transport + // Connect to the IPC client. let ipc_client = RpcClient::connect_pubsub(ipc).await?; + // Create the provider. let provider = RootProvider::::new(ipc_client); let latest_block = provider.get_block_number().await?; diff --git a/examples/providers/examples/ws.rs b/examples/providers/examples/ws.rs index 65991af3..60c5a07e 100644 --- a/examples/providers/examples/ws.rs +++ b/examples/providers/examples/ws.rs @@ -10,17 +10,24 @@ use futures_util::StreamExt; #[tokio::main] async fn main() -> Result<()> { + // Set up the WS transport which is consumed by the RPC client. let rpc_url = "wss://eth-mainnet.g.alchemy.com/v2/your-api-key"; + // Create the WS connection object. let ws_transport = WsConnect::new(rpc_url); + // Connect to the WS client. let rpc_client = RpcClient::connect_pubsub(ws_transport).await?; + // Create the provider. let provider = RootProvider::::new(rpc_client); + // Subscribe to new blocks. let sub = provider.subscribe_blocks().await?; + // Wait and take the next 4 blocks. let mut stream = sub.into_stream().take(4); + println!("Awaiting blocks..."); while let Some(block) = stream.next().await { diff --git a/examples/providers/examples/ws_with_auth.rs b/examples/providers/examples/ws_with_auth.rs index 09e96d99..116f45e2 100644 --- a/examples/providers/examples/ws_with_auth.rs +++ b/examples/providers/examples/ws_with_auth.rs @@ -10,26 +10,30 @@ use futures_util::StreamExt; #[tokio::main] async fn main() -> Result<()> { + // Set up the WS transport which is consumed by the RPC client. let rpc_url = "wss://your-ws-endpoint.com/"; + // Create authorization methods. let auth = Authorization::basic("username", "password"); let auth_bearer = Authorization::bearer("bearer-token"); + // Create the WS connection object with authentication. let ws_transport_basic = WsConnect::with_auth(rpc_url, Some(auth)); - let ws_transport_bearer = WsConnect::with_auth(rpc_url, Some(auth_bearer)); + // Connect to the WS client. let rpc_client_basic = RpcClient::connect_pubsub(ws_transport_basic).await?; - let rpc_client_bearer = RpcClient::connect_pubsub(ws_transport_bearer).await?; + // Create the provider. let provider_basic = RootProvider::::new(rpc_client_basic); - let provider_bearer = RootProvider::::new(rpc_client_bearer); + // Subscribe to new blocks. let sub_basic = provider_basic.subscribe_blocks(); let sub_bearer = provider_bearer.subscribe_blocks(); + // Wait and take the next 4 blocks. let mut stream_basic = sub_basic.await?.into_stream().take(4); let mut stream_bearer = sub_bearer.await?.into_stream().take(4); diff --git a/examples/subscriptions/examples/subscribe_blocks.rs b/examples/subscriptions/examples/subscribe_blocks.rs index 7bde3697..3e5b08f3 100644 --- a/examples/subscriptions/examples/subscribe_blocks.rs +++ b/examples/subscriptions/examples/subscribe_blocks.rs @@ -9,7 +9,7 @@ use futures_util::{stream, StreamExt}; #[tokio::main] async fn main() -> Result<()> { // Spin up a local Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().block_time(1).try_spawn()?; // Create a provider. diff --git a/examples/subscriptions/examples/watch_contract_event.rs b/examples/subscriptions/examples/watch_contract_event.rs index 50ac5b75..1bba64ce 100644 --- a/examples/subscriptions/examples/watch_contract_event.rs +++ b/examples/subscriptions/examples/watch_contract_event.rs @@ -31,7 +31,7 @@ sol!( #[tokio::main] async fn main() -> Result<()> { // Spin up a local Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().block_time(1).try_spawn()?; // Create a WebSocket provider. diff --git a/examples/transactions/examples/gas_price_usd.rs b/examples/transactions/examples/gas_price_usd.rs index ec193eac..e0212b01 100644 --- a/examples/transactions/examples/gas_price_usd.rs +++ b/examples/transactions/examples/gas_price_usd.rs @@ -25,7 +25,7 @@ sol!( #[tokio::main] async fn main() -> Result<()> { // Spin up a forked Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; // Create a provider. diff --git a/examples/transactions/examples/trace_transaction.rs b/examples/transactions/examples/trace_transaction.rs index abcda04c..6e5aa5a8 100644 --- a/examples/transactions/examples/trace_transaction.rs +++ b/examples/transactions/examples/trace_transaction.rs @@ -15,7 +15,7 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { // Spin up a forked Anvil node. - // Ensure `anvil` is available in $PATH + // Ensure `anvil` is available in $PATH. let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?; // Create a provider. diff --git a/examples/wallets/examples/keystore_signer.rs b/examples/wallets/examples/keystore_signer.rs index fc4bd241..6a5741e1 100644 --- a/examples/wallets/examples/keystore_signer.rs +++ b/examples/wallets/examples/keystore_signer.rs @@ -19,7 +19,7 @@ async fn main() -> Result<()> { // Password to decrypt the keystore file with. let password = "test"; - // Set up a wallet using Alice's keystore file. + // Set up signer using Alice's keystore file. // The private key belongs to Alice, the first default Anvil account. let keystore_file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?).join("examples/keystore/alice.json"); diff --git a/examples/wallets/examples/private_key_signer.rs b/examples/wallets/examples/private_key_signer.rs index 05cea52c..b3bd88aa 100644 --- a/examples/wallets/examples/private_key_signer.rs +++ b/examples/wallets/examples/private_key_signer.rs @@ -15,7 +15,7 @@ async fn main() -> Result<()> { // Spin up an Anvil node. let anvil = Anvil::new().block_time(1).try_spawn()?; - // Set up a local wallet from the first default Anvil account (Alice). + // Set up signer from the first default Anvil account (Alice). let signer: LocalWallet = anvil.keys()[0].clone().into(); // Create a provider with the signer. diff --git a/examples/wallets/examples/sign_message.rs b/examples/wallets/examples/sign_message.rs index c3275ec6..8f830a69 100644 --- a/examples/wallets/examples/sign_message.rs +++ b/examples/wallets/examples/sign_message.rs @@ -5,11 +5,11 @@ use eyre::Result; #[tokio::main] async fn main() -> Result<()> { - // Setup up the signer. + // Set up a random signer. let signer = LocalWallet::random(); // Optionally, the wallet's chain id can be set, in order to use EIP-155 - // replay protection with different chains + // replay protection with different chains. let signer = signer.with_chain_id(Some(1337)); // The message to sign. diff --git a/examples/wallets/examples/sign_permit_hash.rs b/examples/wallets/examples/sign_permit_hash.rs index 781cf0e8..aeebf7d7 100644 --- a/examples/wallets/examples/sign_permit_hash.rs +++ b/examples/wallets/examples/sign_permit_hash.rs @@ -22,7 +22,7 @@ sol! { #[tokio::main] async fn main() -> Result<()> { - // Setup up signer. + // Set up a random signer. let signer = LocalWallet::random(); let domain = eip712_domain! { From 26cf01f83a4eeff4e0aae30706f56445e68bc230 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Thu, 28 Mar 2024 07:59:23 +0000 Subject: [PATCH 18/23] add clippy lints --- CONTRIBUTING.md | 7 +- Cargo.toml | 66 +++++++++++++++++++ examples/anvil/Cargo.toml | 4 +- .../anvil/examples/deploy_contract_anvil.rs | 1 + examples/big-numbers/Cargo.toml | 4 +- examples/contracts/Cargo.toml | 4 +- .../examples/deploy_from_artifact.rs | 1 + .../examples/deploy_from_contract.rs | 1 + examples/contracts/examples/generate.rs | 1 + examples/layers/Cargo.toml | 4 +- examples/providers/Cargo.toml | 4 +- examples/queries/Cargo.toml | 4 +- examples/subscriptions/Cargo.toml | 4 +- .../examples/event_multiplexer.rs | 1 + .../examples/watch_contract_event.rs | 1 + examples/transactions/Cargo.toml | 4 +- .../transactions/examples/decode_input.rs | 1 + .../transactions/examples/gas_price_usd.rs | 1 + .../transactions/examples/transfer_erc20.rs | 6 +- examples/wallets/Cargo.toml | 4 +- examples/wallets/examples/create_keystore.rs | 2 +- examples/wallets/examples/sign_permit_hash.rs | 1 + 22 files changed, 113 insertions(+), 13 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3f18c94e..4bd9e183 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -126,10 +126,13 @@ This section lists some commonly needed commands. ```sh cargo check --all-features -cargo +nightly fmt --all cargo build --all-features cargo test --all-features -cargo +nightly clippy --all-features +cargo +nightly fmt --all +cargo +nightly clippy \ + --examples \ + --all-features \ + -- -D warnings ``` ### Tests diff --git a/Cargo.toml b/Cargo.toml index cc56a751..b2f55674 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,72 @@ repository = "https://github.com/alloy-rs/examples" publish = false exclude = ["examples/"] +[workspace.lints] +rust.missing_debug_implementations = "warn" +rust.missing_docs = "warn" +rust.unreachable_pub = "warn" +rustdoc.all = "warn" +rust.unused_must_use = "deny" +rust.rust_2018_idioms = "deny" + +[workspace.lints.clippy] +# These are some of clippy's nursery (i.e., experimental) lints that we like. +# By default, nursery lints are allowed. Some of the lints below have made good +# suggestions which we fixed. The others didn't have any findings, so we can +# assume they don't have that many false positives. Let's enable them to +# prevent future problems. +branches_sharing_code = "warn" +clear_with_drain = "warn" +derive_partial_eq_without_eq = "warn" +empty_line_after_outer_attr = "warn" +equatable_if_let = "warn" +imprecise_flops = "warn" +iter_on_empty_collections = "warn" +iter_with_drain = "warn" +large_stack_frames = "warn" +manual_clamp = "warn" +mutex_integer = "warn" +needless_pass_by_ref_mut = "warn" +nonstandard_macro_braces = "warn" +or_fun_call = "warn" +path_buf_push_overwrite = "warn" +read_zero_byte_vec = "warn" +redundant_clone = "warn" +suboptimal_flops = "warn" +suspicious_operation_groupings = "warn" +trailing_empty_array = "warn" +trait_duplication_in_bounds = "warn" +transmute_undefined_repr = "warn" +trivial_regex = "warn" +tuple_array_conversions = "warn" +uninhabited_references = "warn" +unused_peekable = "warn" +unused_rounding = "warn" +useless_let_if_seq = "warn" + +# These are nursery lints which have findings. Allow them for now. Some are not +# quite mature enough for use in our codebase and some we don't really want. +# Explicitly listing should make it easier to fix in the future. +as_ptr_cast_mut = "allow" +cognitive_complexity = "allow" +collection_is_never_read = "allow" +debug_assert_with_mut_call = "allow" +empty_line_after_doc_comments = "allow" +fallible_impl_from = "allow" +future_not_send = "allow" +iter_on_single_items = "allow" +missing_const_for_fn = "allow" +needless_collect = "allow" +non_send_fields_in_send_ty = "allow" +option_if_let_else = "allow" +redundant_pub_crate = "allow" +significant_drop_in_scrutinee = "allow" +significant_drop_tightening = "allow" +string_lit_as_bytes = "allow" +type_repetition_in_bounds = "allow" +unnecessary_struct_initialization = "allow" +use_self = "allow" + [workspace.dependencies] alloy = { git = "https://github.com/alloy-rs/alloy", rev = "f7333c4", features = [ # "dyn-abi", diff --git a/examples/anvil/Cargo.toml b/examples/anvil/Cargo.toml index 7d05a6e4..89ebcca5 100644 --- a/examples/anvil/Cargo.toml +++ b/examples/anvil/Cargo.toml @@ -1,6 +1,5 @@ [package] name = "examples-anvil" - publish.workspace = true version.workspace = true edition.workspace = true @@ -10,6 +9,9 @@ license.workspace = true homepage.workspace = true repository.workspace = true +[lints] +workspace = true + [dev-dependencies] alloy.workspace = true diff --git a/examples/anvil/examples/deploy_contract_anvil.rs b/examples/anvil/examples/deploy_contract_anvil.rs index d929b8ab..19fe496f 100644 --- a/examples/anvil/examples/deploy_contract_anvil.rs +++ b/examples/anvil/examples/deploy_contract_anvil.rs @@ -13,6 +13,7 @@ use eyre::Result; // Codegen from embedded Solidity code and precompiled bytecode. sol! { + #[allow(missing_docs)] // solc v0.8.24; solc a.sol --via-ir --optimize --bin #[sol(rpc, bytecode="608080604052346100155760d2908161001a8239f35b5f80fdfe60808060405260043610156011575f80fd5b5f3560e01c9081633fb5c1cb1460865781638381f58a14606f575063d09de08a146039575f80fd5b34606b575f366003190112606b575f545f1981146057576001015f55005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b34606b575f366003190112606b576020905f548152f35b34606b576020366003190112606b576004355f5500fea2646970667358221220bdecd3c1dd631eb40587cafcd6e8297479db76db6a328e18ad1ea5b340852e3864736f6c63430008180033")] contract Counter { diff --git a/examples/big-numbers/Cargo.toml b/examples/big-numbers/Cargo.toml index a8972527..17a46eef 100644 --- a/examples/big-numbers/Cargo.toml +++ b/examples/big-numbers/Cargo.toml @@ -1,6 +1,5 @@ [package] name = "examples-big-numbers" - publish.workspace = true version.workspace = true edition.workspace = true @@ -10,6 +9,9 @@ license.workspace = true homepage.workspace = true repository.workspace = true +[lints] +workspace = true + [dev-dependencies] alloy.workspace = true diff --git a/examples/contracts/Cargo.toml b/examples/contracts/Cargo.toml index e634e711..d02f79ea 100644 --- a/examples/contracts/Cargo.toml +++ b/examples/contracts/Cargo.toml @@ -1,6 +1,5 @@ [package] name = "examples-contracts" - publish.workspace = true version.workspace = true edition.workspace = true @@ -10,6 +9,9 @@ license.workspace = true homepage.workspace = true repository.workspace = true +[lints] +workspace = true + [dev-dependencies] alloy.workspace = true diff --git a/examples/contracts/examples/deploy_from_artifact.rs b/examples/contracts/examples/deploy_from_artifact.rs index ce80fa6d..57f0e631 100644 --- a/examples/contracts/examples/deploy_from_artifact.rs +++ b/examples/contracts/examples/deploy_from_artifact.rs @@ -13,6 +13,7 @@ use eyre::Result; // Codegen from artifact. sol!( + #[allow(missing_docs)] #[sol(rpc)] Counter, "examples/artifacts/Counter.json" diff --git a/examples/contracts/examples/deploy_from_contract.rs b/examples/contracts/examples/deploy_from_contract.rs index ad7a2c52..b982904a 100644 --- a/examples/contracts/examples/deploy_from_contract.rs +++ b/examples/contracts/examples/deploy_from_contract.rs @@ -13,6 +13,7 @@ use eyre::Result; // Codegen from embedded Solidity code and precompiled bytecode. sol! { + #[allow(missing_docs)] // solc v0.8.24; solc a.sol --via-ir --optimize --bin #[sol(rpc, bytecode="608080604052346100155760d2908161001a8239f35b5f80fdfe60808060405260043610156011575f80fd5b5f3560e01c9081633fb5c1cb1460865781638381f58a14606f575063d09de08a146039575f80fd5b34606b575f366003190112606b575f545f1981146057576001015f55005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b34606b575f366003190112606b576020905f548152f35b34606b576020366003190112606b576004355f5500fea2646970667358221220bdecd3c1dd631eb40587cafcd6e8297479db76db6a328e18ad1ea5b340852e3864736f6c63430008180033")] contract Counter { diff --git a/examples/contracts/examples/generate.rs b/examples/contracts/examples/generate.rs index f521c7d6..4055e3ee 100644 --- a/examples/contracts/examples/generate.rs +++ b/examples/contracts/examples/generate.rs @@ -5,6 +5,7 @@ use eyre::Result; // Codegen from ABI file to interact with the contract. sol!( + #[allow(missing_docs)] #[sol(rpc)] IERC20, "examples/abi/IERC20.json" diff --git a/examples/layers/Cargo.toml b/examples/layers/Cargo.toml index 2f182349..bbd8ee43 100644 --- a/examples/layers/Cargo.toml +++ b/examples/layers/Cargo.toml @@ -1,6 +1,5 @@ [package] name = "examples-layers" - publish.workspace = true version.workspace = true edition.workspace = true @@ -10,6 +9,9 @@ license.workspace = true homepage.workspace = true repository.workspace = true +[lints] +workspace = true + [dev-dependencies] alloy.workspace = true diff --git a/examples/providers/Cargo.toml b/examples/providers/Cargo.toml index 6e2768c3..f48fae12 100644 --- a/examples/providers/Cargo.toml +++ b/examples/providers/Cargo.toml @@ -1,6 +1,5 @@ [package] name = "examples-providers" - publish.workspace = true version.workspace = true edition.workspace = true @@ -10,6 +9,9 @@ license.workspace = true homepage.workspace = true repository.workspace = true +[lints] +workspace = true + [dev-dependencies] alloy.workspace = true # Temp dependency fix to enable relevant features - Ref: https://github.com/alloy-rs/examples/pull/3#discussion_r1537842062 diff --git a/examples/queries/Cargo.toml b/examples/queries/Cargo.toml index edac004c..b279e731 100644 --- a/examples/queries/Cargo.toml +++ b/examples/queries/Cargo.toml @@ -1,6 +1,5 @@ [package] name = "examples-queries" - publish.workspace = true version.workspace = true edition.workspace = true @@ -10,6 +9,9 @@ license.workspace = true homepage.workspace = true repository.workspace = true +[lints] +workspace = true + [dev-dependencies] alloy.workspace = true diff --git a/examples/subscriptions/Cargo.toml b/examples/subscriptions/Cargo.toml index 2a939ab6..d44e0a47 100644 --- a/examples/subscriptions/Cargo.toml +++ b/examples/subscriptions/Cargo.toml @@ -1,6 +1,5 @@ [package] name = "examples-subscriptions" - publish.workspace = true version.workspace = true edition.workspace = true @@ -10,6 +9,9 @@ license.workspace = true homepage.workspace = true repository.workspace = true +[lints] +workspace = true + [dev-dependencies] alloy.workspace = true # Temp fix for enabling features. Ref: https://github.com/alloy-rs/examples/pull/3/#discussion_r1537842062 diff --git a/examples/subscriptions/examples/event_multiplexer.rs b/examples/subscriptions/examples/event_multiplexer.rs index 68b3d89e..e411d98d 100644 --- a/examples/subscriptions/examples/event_multiplexer.rs +++ b/examples/subscriptions/examples/event_multiplexer.rs @@ -10,6 +10,7 @@ use std::str::FromStr; // Codegen from embedded Solidity code and precompiled bytecode. // solc v0.8.24; solc a.sol --via-ir --optimize --bin sol!( + #[allow(missing_docs)] #[sol(rpc, bytecode = "0x6080806040523461001657610213908161001b8239f35b5f80fdfe6080604052600480361015610012575f80fd5b5f3560e01c80634350913814610165578063a5f3c23b14610116578063adefc37b146100c75763bbe93d9114610046575f80fd5b346100c357610054366101c7565b8181029291905f8212600160ff1b8214166100b057818405149015171561009d5750337fd7a123d4c8e44db3186e04b9c96c102287276929c930f2e8abcaa555ef5dcacc5f80a3005b601190634e487b7160e01b5f525260245ffd5b601183634e487b7160e01b5f525260245ffd5b5f80fd5b50346100c3576100d6366101c7565b91905f838203931281841281169184139015161761009d5750337f32e913bf2ad35da1e845597618bb9f3f80642a68dd39f30a093a7838aa61fb275f80a3005b50346100c357610125366101c7565b91905f838201938412911290801582169115161761009d5750337f6da406ea462447ed7804b4a4dc69c67b53d3d45a50381ae3e9cf878c9d7c23df5f80a3005b50346100c357610174366101c7565b9182156101b457600160ff1b82145f1984141661009d575005337f1c1e8bbe327890ea8d3f5b22370a56c3fcef7ff82f306161f64647fe5d2858815f80a3005b601290634e487b7160e01b5f525260245ffd5b60409060031901126100c357600435906024359056fea26469706673582212208fc27b878cb877b8c4e5dee739e0a35a64f82759549ed8f6d4ddab5ded9717bb64736f6c63430008180033")] contract EventMultiplexer { event Add(address indexed sender, int256 indexed value); diff --git a/examples/subscriptions/examples/watch_contract_event.rs b/examples/subscriptions/examples/watch_contract_event.rs index 1bba64ce..0dada981 100644 --- a/examples/subscriptions/examples/watch_contract_event.rs +++ b/examples/subscriptions/examples/watch_contract_event.rs @@ -9,6 +9,7 @@ use futures_util::StreamExt; // Codegen from embedded Solidity code and precompiled bytecode. // solc v0.8.24; solc a.sol --via-ir --optimize --bin sol!( + #[allow(missing_docs)] #[sol(rpc, bytecode = "0x60808060405234610019575f8055610143908161001e8239f35b5f80fdfe60806040526004361015610011575f80fd5b5f3560e01c80632baeceb7146100c357806361bc221a146100a75763d09de08a1461003a575f80fd5b346100a3575f3660031901126100a3575f5460018101905f60018312911290801582169115161761008f57805f55337ff6d1d8d205b41f9fb9549900a8dba5d669d68117a3a2b88c1ebc61163e8117ba5f80a3005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a3575f3660031901126100a35760205f54604051908152f35b346100a3575f3660031901126100a3575f545f19810190811360011661008f57805f55337fdc69c403b972fc566a14058b3b18e1513da476de6ac475716e489fae0cbe4a265f80a300fea2646970667358221220c045c027059726f9175a4abd427eb3f7a3fe8e27108bc19e4ae46055e7c1842c64736f6c63430008180033")] contract Counter { int256 public counter = 0; diff --git a/examples/transactions/Cargo.toml b/examples/transactions/Cargo.toml index f887fe70..b2d4f770 100644 --- a/examples/transactions/Cargo.toml +++ b/examples/transactions/Cargo.toml @@ -1,6 +1,5 @@ [package] name = "examples-transactions" - publish.workspace = true version.workspace = true edition.workspace = true @@ -10,6 +9,9 @@ license.workspace = true homepage.workspace = true repository.workspace = true +[lints] +workspace = true + [dev-dependencies] alloy.workspace = true diff --git a/examples/transactions/examples/decode_input.rs b/examples/transactions/examples/decode_input.rs index 0135b00b..7fc3871a 100644 --- a/examples/transactions/examples/decode_input.rs +++ b/examples/transactions/examples/decode_input.rs @@ -6,6 +6,7 @@ use eyre::Result; // Codegen from excerpt of Uniswap V2 Router interface. // See: https://docs.uniswap.org/contracts/v2/reference/smart-contracts/router-02 sol!( + #[allow(missing_docs)] function swapExactTokensForTokens( uint256 amountIn, uint256 amountOutMin, diff --git a/examples/transactions/examples/gas_price_usd.rs b/examples/transactions/examples/gas_price_usd.rs index e0212b01..b21981f6 100644 --- a/examples/transactions/examples/gas_price_usd.rs +++ b/examples/transactions/examples/gas_price_usd.rs @@ -19,6 +19,7 @@ const ETH_DECIMALS: u32 = 18; // Codegen from excerpt of Chainlink Aggregator interface. // See: https://etherscan.io/address/0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419#code sol!( + #[allow(missing_docs)] function latestAnswer() external view returns (int256); ); diff --git a/examples/transactions/examples/transfer_erc20.rs b/examples/transactions/examples/transfer_erc20.rs index b9360da7..daa154f0 100644 --- a/examples/transactions/examples/transfer_erc20.rs +++ b/examples/transactions/examples/transfer_erc20.rs @@ -12,7 +12,11 @@ use alloy::{ use eyre::Result; // Codegen from artifact. -sol!(ERC20Example, "examples/contracts/ERC20Example.json"); +sol!( + #[allow(missing_docs)] + ERC20Example, + "examples/contracts/ERC20Example.json" +); #[tokio::main] async fn main() -> Result<()> { diff --git a/examples/wallets/Cargo.toml b/examples/wallets/Cargo.toml index eb186361..0d04582e 100644 --- a/examples/wallets/Cargo.toml +++ b/examples/wallets/Cargo.toml @@ -1,6 +1,5 @@ [package] name = "examples-wallets" - publish.workspace = true version.workspace = true edition.workspace = true @@ -10,6 +9,9 @@ license.workspace = true homepage.workspace = true repository.workspace = true +[lints] +workspace = true + [dev-dependencies] alloy.workspace = true diff --git a/examples/wallets/examples/create_keystore.rs b/examples/wallets/examples/create_keystore.rs index 595d3bc6..4d720775 100644 --- a/examples/wallets/examples/create_keystore.rs +++ b/examples/wallets/examples/create_keystore.rs @@ -21,7 +21,7 @@ async fn main() -> Result<()> { let (wallet, file_path) = Wallet::encrypt_keystore(&dir, &mut rng, private_key, password, None)?; - let keystore_file_path = dir.path().join(file_path.clone()); + let keystore_file_path = dir.path().join(file_path); println!("Wrote keystore for {:?} to {:?}", wallet.address(), keystore_file_path); diff --git a/examples/wallets/examples/sign_permit_hash.rs b/examples/wallets/examples/sign_permit_hash.rs index aeebf7d7..e7448b3f 100644 --- a/examples/wallets/examples/sign_permit_hash.rs +++ b/examples/wallets/examples/sign_permit_hash.rs @@ -10,6 +10,7 @@ use eyre::Result; use serde::Serialize; sol! { + #[allow(missing_docs)] #[derive(Debug, Serialize)] struct Permit { address owner; From fd77487deb0bc173dffa759797bce88a8fa3e5d8 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Thu, 28 Mar 2024 08:00:37 +0000 Subject: [PATCH 19/23] fixed_bytes! -> b256! --- examples/queries/examples/query_logs.rs | 4 ++-- examples/transactions/examples/trace_transaction.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/queries/examples/query_logs.rs b/examples/queries/examples/query_logs.rs index b5c1f081..20d044d4 100644 --- a/examples/queries/examples/query_logs.rs +++ b/examples/queries/examples/query_logs.rs @@ -2,7 +2,7 @@ use alloy::{ network::Ethereum, - primitives::{address, fixed_bytes}, + primitives::{address, b256}, providers::{Provider, RootProvider}, rpc::types::eth::Filter, }; @@ -29,7 +29,7 @@ async fn main() -> Result<()> { // Get all logs from the latest block that match the transfer event signature/topic. let tranfer_event_signature = - fixed_bytes!("ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"); + b256!("ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"); let filter = Filter::new().event_signature(tranfer_event_signature).from_block(latest_block); // You could also use the event name instead of the event signature like so: // .event("Transfer(address,address,uint256)") diff --git a/examples/transactions/examples/trace_transaction.rs b/examples/transactions/examples/trace_transaction.rs index 6e5aa5a8..f6250675 100644 --- a/examples/transactions/examples/trace_transaction.rs +++ b/examples/transactions/examples/trace_transaction.rs @@ -3,7 +3,7 @@ use alloy::{ network::Ethereum, node_bindings::Anvil, - primitives::fixed_bytes, + primitives::b256, providers::{HttpProvider, Provider}, rpc::types::trace::geth::{ GethDebugBuiltInTracerType, GethDebugTracerType, GethDebugTracingOptions, @@ -23,7 +23,7 @@ async fn main() -> Result<()> { let provider = HttpProvider::::new_http(rpc_url); // Hash of the tx we want to trace - let hash = fixed_bytes!("97a02abf405d36939e5b232a5d4ef5206980c5a6661845436058f30600c52df7"); + let hash = b256!("97a02abf405d36939e5b232a5d4ef5206980c5a6661845436058f30600c52df7"); // Default tracing let default_options = GethDebugTracingOptions::default(); From cff6acd0d676620eddf220004178db0b21eaaeae Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Thu, 28 Mar 2024 08:02:35 +0000 Subject: [PATCH 20/23] inline format string lint setting --- Cargo.toml | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b2f55674..4eb6470b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,29 +55,7 @@ uninhabited_references = "warn" unused_peekable = "warn" unused_rounding = "warn" useless_let_if_seq = "warn" - -# These are nursery lints which have findings. Allow them for now. Some are not -# quite mature enough for use in our codebase and some we don't really want. -# Explicitly listing should make it easier to fix in the future. -as_ptr_cast_mut = "allow" -cognitive_complexity = "allow" -collection_is_never_read = "allow" -debug_assert_with_mut_call = "allow" -empty_line_after_doc_comments = "allow" -fallible_impl_from = "allow" -future_not_send = "allow" -iter_on_single_items = "allow" -missing_const_for_fn = "allow" -needless_collect = "allow" -non_send_fields_in_send_ty = "allow" -option_if_let_else = "allow" -redundant_pub_crate = "allow" -significant_drop_in_scrutinee = "allow" -significant_drop_tightening = "allow" -string_lit_as_bytes = "allow" -type_repetition_in_bounds = "allow" -unnecessary_struct_initialization = "allow" -use_self = "allow" +uninlined_format_args = "warn" [workspace.dependencies] alloy = { git = "https://github.com/alloy-rs/alloy", rev = "f7333c4", features = [ From 38c003203ce1a448a32133ebd450689c9d07a219 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Thu, 28 Mar 2024 08:02:50 +0000 Subject: [PATCH 21/23] apply fix --- examples/anvil/examples/deploy_contract_anvil.rs | 2 +- examples/contracts/examples/deploy_from_artifact.rs | 2 +- examples/contracts/examples/deploy_from_contract.rs | 2 +- examples/providers/examples/http.rs | 2 +- examples/providers/examples/ipc.rs | 2 +- examples/queries/examples/query_contract_storage.rs | 2 +- examples/queries/examples/query_deployed_bytecode.rs | 2 +- examples/queries/examples/query_logs.rs | 6 +++--- examples/subscriptions/examples/event_multiplexer.rs | 8 ++++---- examples/subscriptions/examples/subscribe_blocks.rs | 2 +- examples/subscriptions/examples/watch_contract_event.rs | 8 ++++---- examples/transactions/examples/decode_input.rs | 2 +- examples/transactions/examples/gas_price_usd.rs | 4 ++-- examples/transactions/examples/trace_transaction.rs | 6 +++--- examples/transactions/examples/transfer_erc20.rs | 2 +- examples/wallets/examples/create_keystore.rs | 2 +- 16 files changed, 27 insertions(+), 27 deletions(-) diff --git a/examples/anvil/examples/deploy_contract_anvil.rs b/examples/anvil/examples/deploy_contract_anvil.rs index 19fe496f..152665d9 100644 --- a/examples/anvil/examples/deploy_contract_anvil.rs +++ b/examples/anvil/examples/deploy_contract_anvil.rs @@ -55,7 +55,7 @@ async fn main() -> Result<()> { let contract_address = contract_builder.gas(estimate).gas_price(base_fee).nonce(0).deploy().await?; - println!("Deployed contract at address: {:?}", contract_address); + println!("Deployed contract at address: {contract_address:?}"); let contract = Counter::new(contract_address, &provider); diff --git a/examples/contracts/examples/deploy_from_artifact.rs b/examples/contracts/examples/deploy_from_artifact.rs index 57f0e631..26970c26 100644 --- a/examples/contracts/examples/deploy_from_artifact.rs +++ b/examples/contracts/examples/deploy_from_artifact.rs @@ -45,7 +45,7 @@ async fn main() -> Result<()> { let contract_address = contract_builder.gas(estimate).gas_price(base_fee).nonce(0).deploy().await?; - println!("Deployed contract at address: {:?}", contract_address); + println!("Deployed contract at address: {contract_address:?}"); let contract = Counter::new(contract_address, &provider); diff --git a/examples/contracts/examples/deploy_from_contract.rs b/examples/contracts/examples/deploy_from_contract.rs index b982904a..9ac06ba4 100644 --- a/examples/contracts/examples/deploy_from_contract.rs +++ b/examples/contracts/examples/deploy_from_contract.rs @@ -55,7 +55,7 @@ async fn main() -> Result<()> { let contract_address = contract_builder.gas(estimate).gas_price(base_fee).nonce(0).deploy().await?; - println!("Deployed contract at address: {:?}", contract_address); + println!("Deployed contract at address: {contract_address:?}"); let contract = Counter::new(contract_address, &provider); diff --git a/examples/providers/examples/http.rs b/examples/providers/examples/http.rs index a1517b4c..39d6425f 100644 --- a/examples/providers/examples/http.rs +++ b/examples/providers/examples/http.rs @@ -22,7 +22,7 @@ async fn main() -> Result<()> { // Get latest block number. let latest_block = provider.get_block_number().await?; - println!("Latest block number: {}", latest_block); + println!("Latest block number: {latest_block}"); Ok(()) } diff --git a/examples/providers/examples/ipc.rs b/examples/providers/examples/ipc.rs index 6aace60f..6e7d9125 100644 --- a/examples/providers/examples/ipc.rs +++ b/examples/providers/examples/ipc.rs @@ -24,7 +24,7 @@ async fn main() -> Result<()> { let latest_block = provider.get_block_number().await?; - println!("Latest block: {}", latest_block); + println!("Latest block: {latest_block}"); Ok(()) } diff --git a/examples/queries/examples/query_contract_storage.rs b/examples/queries/examples/query_contract_storage.rs index bfcaee12..f4a3769a 100644 --- a/examples/queries/examples/query_contract_storage.rs +++ b/examples/queries/examples/query_contract_storage.rs @@ -18,7 +18,7 @@ async fn main() -> Result<()> { let storage_slot = U256::from(0); let storage = provider.get_storage_at(pool_address, storage_slot, None).await?; - println!("Slot 0: {:?}", storage); + println!("Slot 0: {storage:?}"); Ok(()) } diff --git a/examples/queries/examples/query_deployed_bytecode.rs b/examples/queries/examples/query_deployed_bytecode.rs index 4a2b73b3..22061089 100644 --- a/examples/queries/examples/query_deployed_bytecode.rs +++ b/examples/queries/examples/query_deployed_bytecode.rs @@ -19,7 +19,7 @@ async fn main() -> Result<()> { let bytecode = provider.get_code_at(pool_address, BlockId::Number(BlockNumberOrTag::Latest)).await?; - println!("Bytecode: {:?}", bytecode); + println!("Bytecode: {bytecode:?}"); Ok(()) } diff --git a/examples/queries/examples/query_logs.rs b/examples/queries/examples/query_logs.rs index 20d044d4..a79644f0 100644 --- a/examples/queries/examples/query_logs.rs +++ b/examples/queries/examples/query_logs.rs @@ -24,7 +24,7 @@ async fn main() -> Result<()> { let logs = provider.get_logs(&filter).await?; for log in logs { - println!("{:?}", log); + println!("{log:?}"); } // Get all logs from the latest block that match the transfer event signature/topic. @@ -38,7 +38,7 @@ async fn main() -> Result<()> { let logs = provider.get_logs(&filter).await?; for log in logs { - println!("Transfer event: {:?}", log); + println!("Transfer event: {log:?}"); } // Get all from the latest block emitted by the UNI token address. @@ -49,7 +49,7 @@ async fn main() -> Result<()> { let logs = provider.get_logs(&filter).await?; for log in logs { - println!("Uniswap token logs: {:?}", log); + println!("Uniswap token logs: {log:?}"); } Ok(()) diff --git a/examples/subscriptions/examples/event_multiplexer.rs b/examples/subscriptions/examples/event_multiplexer.rs index e411d98d..6b1b79cd 100644 --- a/examples/subscriptions/examples/event_multiplexer.rs +++ b/examples/subscriptions/examples/event_multiplexer.rs @@ -105,13 +105,13 @@ async fn main() -> Result<()> { let topic = &log.topics[0]; if topic == add_log { - println!("Received Add: {:?}", log); + println!("Received Add: {log:?}"); } else if topic == sub_log { - println!("Received Sub: {:?}", log); + println!("Received Sub: {log:?}"); } else if topic == mul_log { - println!("Received Mul: {:?}", log); + println!("Received Mul: {log:?}"); } else if topic == div_log { - println!("Received Div: {:?}", log); + println!("Received Div: {log:?}"); } } diff --git a/examples/subscriptions/examples/subscribe_blocks.rs b/examples/subscriptions/examples/subscribe_blocks.rs index 3e5b08f3..613df443 100644 --- a/examples/subscriptions/examples/subscribe_blocks.rs +++ b/examples/subscriptions/examples/subscribe_blocks.rs @@ -29,7 +29,7 @@ async fn main() -> Result<()> { let mut stream = poller.into_stream().flat_map(stream::iter).take(2); while let Some(block_hash) = stream.next().await { - println!("Polled for block header: {:?}", block_hash); + println!("Polled for block header: {block_hash:?}"); } Ok(()) diff --git a/examples/subscriptions/examples/watch_contract_event.rs b/examples/subscriptions/examples/watch_contract_event.rs index 0dada981..49e93863 100644 --- a/examples/subscriptions/examples/watch_contract_event.rs +++ b/examples/subscriptions/examples/watch_contract_event.rs @@ -68,10 +68,10 @@ async fn main() -> Result<()> { .for_each(|log| async { match log { Ok((_event, log)) => { - println!("Received Increment: {:?}", log); + println!("Received Increment: {log:?}"); } Err(e) => { - println!("Error: {:?}", e); + println!("Error: {e:?}"); } } }) @@ -83,10 +83,10 @@ async fn main() -> Result<()> { .for_each(|log| async { match log { Ok((_event, log)) => { - println!("Received Decrement: {:?}", log); + println!("Received Decrement: {log:?}"); } Err(e) => { - println!("Error: {:?}", e); + println!("Error: {e:?}"); } } }) diff --git a/examples/transactions/examples/decode_input.rs b/examples/transactions/examples/decode_input.rs index 7fc3871a..6031d0a5 100644 --- a/examples/transactions/examples/decode_input.rs +++ b/examples/transactions/examples/decode_input.rs @@ -39,7 +39,7 @@ async fn main() -> Result<()> { ); } Err(e) => { - println!("Error decoding input: {:?}", e); + println!("Error decoding input: {e:?}"); } } diff --git a/examples/transactions/examples/gas_price_usd.rs b/examples/transactions/examples/gas_price_usd.rs index b21981f6..8b4cf7e7 100644 --- a/examples/transactions/examples/gas_price_usd.rs +++ b/examples/transactions/examples/gas_price_usd.rs @@ -49,8 +49,8 @@ async fn main() -> Result<()> { let gwei = format_units(wei_per_gas, "gwei")?.parse::()?; let usd = get_usd_value(wei_per_gas, result)?; - println!("Gas price in Gwei: {}", gwei); - println!("Gas price in USD: {}", usd); + println!("Gas price in Gwei: {gwei}"); + println!("Gas price in USD: {usd}"); Ok(()) } diff --git a/examples/transactions/examples/trace_transaction.rs b/examples/transactions/examples/trace_transaction.rs index f6250675..04a232de 100644 --- a/examples/transactions/examples/trace_transaction.rs +++ b/examples/transactions/examples/trace_transaction.rs @@ -29,7 +29,7 @@ async fn main() -> Result<()> { let default_options = GethDebugTracingOptions::default(); let result = provider.debug_trace_transaction(hash, default_options).await?; - println!("DEFAULT_TRACE: {:?}", result); + println!("DEFAULT_TRACE: {result:?}"); // Trace with built-in call tracer. let call_options = GethDebugTracingOptions { @@ -43,7 +43,7 @@ async fn main() -> Result<()> { }; let result = provider.debug_trace_transaction(hash, call_options).await?; - println!("CALL_TRACE: {:?}", result); + println!("CALL_TRACE: {result:?}"); // Trace using a custom JavaScript tracer. let js_options = GethDebugTracingOptions { @@ -53,7 +53,7 @@ async fn main() -> Result<()> { let result = provider.debug_trace_transaction(hash, js_options).await?; - println!("JS_TRACER: {:?}", result); + println!("JS_TRACER: {result:?}"); Ok(()) } diff --git a/examples/transactions/examples/transfer_erc20.rs b/examples/transactions/examples/transfer_erc20.rs index daa154f0..9b028202 100644 --- a/examples/transactions/examples/transfer_erc20.rs +++ b/examples/transactions/examples/transfer_erc20.rs @@ -83,7 +83,7 @@ async fn deploy_token_contract( // Get the contract address. let contract_address = receipt.contract_address.expect("Contract address not found"); - println!("Deployed contract at: {}", contract_address); + println!("Deployed contract at: {contract_address}"); Ok(contract_address) } diff --git a/examples/wallets/examples/create_keystore.rs b/examples/wallets/examples/create_keystore.rs index 4d720775..e65370ba 100644 --- a/examples/wallets/examples/create_keystore.rs +++ b/examples/wallets/examples/create_keystore.rs @@ -40,7 +40,7 @@ async fn main() -> Result<()> { // Display the contents of the keystore file. let keystore_contents = read_to_string(keystore_file_path)?; - println!("Keystore file contents: {:?}", keystore_contents); + println!("Keystore file contents: {keystore_contents:?}"); Ok(()) } From 59989d173f46518c497a07a8ab416e7ad1e27b4e Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Thu, 28 Mar 2024 08:27:06 +0000 Subject: [PATCH 22/23] update instructions --- CONTRIBUTING.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4bd9e183..8924ccce 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -125,9 +125,8 @@ less work from you. This section lists some commonly needed commands. ```sh -cargo check --all-features -cargo build --all-features -cargo test --all-features +cargo check --examples --all-features +cargo build --examples --all-features cargo +nightly fmt --all cargo +nightly clippy \ --examples \ From 9c404985165084736252e0194253e33a3958bda9 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Thu, 28 Mar 2024 08:34:23 +0000 Subject: [PATCH 23/23] add pull request template --- .github/PULL_REQUEST_TEMPLATE.md | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 .github/PULL_REQUEST_TEMPLATE.md diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 00000000..e2cefa70 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,33 @@ + + + + +## Motivation + + + +## Solution + + + +## PR Checklist + +- [ ] Added Documentation +- [ ] Breaking changes \ No newline at end of file