Skip to content

Commit

Permalink
fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zerosnacks committed Apr 9, 2024
1 parent df0dd73 commit e3575e8
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 21 deletions.
7 changes: 5 additions & 2 deletions examples/providers/examples/builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Example of using the `ProviderBuilder` to create a provider with a signer and network.
use alloy::{
network::EthereumSigner,
network::{EthereumSigner, TransactionBuilder},
node_bindings::Anvil,
primitives::U256,
providers::{Provider, ProviderBuilder},
Expand Down Expand Up @@ -31,7 +31,10 @@ async fn main() -> Result<()> {
.on_http(rpc_url)?;

// Create a transaction.
let tx = TransactionRequest::default().to(Some(bob)).value(U256::from(100));
let tx = TransactionRequest::default()
.with_from(alice)
.with_to(bob.into())
.with_value(U256::from(100));

// Send the transaction and wait for the receipt.
let pending_tx = provider.send_transaction(tx).await?;
Expand Down
4 changes: 3 additions & 1 deletion examples/transactions/examples/gas_price_usd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Example of how to get the gas price in USD using the Chainlink ETH/USD feed.
use alloy::{
network::TransactionBuilder,
node_bindings::Anvil,
primitives::{address, utils::format_units, Address, Bytes, U256},
providers::{Provider, ProviderBuilder},
Expand Down Expand Up @@ -37,7 +38,8 @@ async fn main() -> Result<()> {
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 tx = TransactionRequest::default().with_to(ETH_USD_FEED.into()).with_input(input);

let response = provider.call(&tx, None).await?;
let result = U256::from_str(&response.to_string())?;

Expand Down
6 changes: 3 additions & 3 deletions examples/transactions/examples/trace_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ async fn main() -> Result<()> {

// Build a transaction to send 100 wei from Alice to Bob.
let tx = TransactionRequest::default()
.with_value(U256::from(100))
.with_from(alice.into())
.with_to(bob.into());
.with_from(alice)
.with_to(bob.into())
.with_value(U256::from(100));

// Trace the transaction on top of the latest block.
let trace_type = [TraceType::Trace];
Expand Down
6 changes: 5 additions & 1 deletion examples/transactions/examples/transfer_eth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Example of how to transfer ETH from one account to another.
use alloy::{
network::TransactionBuilder,
node_bindings::Anvil,
primitives::U256,
providers::{Provider, ProviderBuilder},
Expand All @@ -23,7 +24,10 @@ async fn main() -> Result<()> {
let bob = anvil.addresses()[1];

// Build a transaction to send 100 wei from Alice to Bob.
let tx = TransactionRequest::default().from(alice).value(U256::from(100)).to(Some(bob));
let tx = TransactionRequest::default()
.with_from(alice)
.with_to(bob.into())
.with_value(U256::from(100));

// Send the transaction and wait for the receipt.
let pending_tx = provider.send_transaction(tx).await?;
Expand Down
8 changes: 5 additions & 3 deletions examples/wallets/examples/keystore_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async fn main() -> Result<()> {
let keystore_file_path =
PathBuf::from(env::var("CARGO_MANIFEST_DIR")?).join("examples/keystore/alice.json");
let signer = Wallet::decrypt_keystore(keystore_file_path, password)?;
let alice = signer.address();

// Create a provider with the signer.
let rpc_url = anvil.endpoint().parse()?;
Expand All @@ -32,10 +33,11 @@ async fn main() -> Result<()> {
.signer(EthereumSigner::from(signer))
.on_http(rpc_url)?;

// Build a transaction to send 100 wei to Vitalik.
// Build a transaction to send 100 wei to Vitalik from Alice.
let tx = TransactionRequest::default()
.with_value(U256::from(100))
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into());
.with_from(alice)
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into())
.with_value(U256::from(100));

// Send the transaction and wait for the receipt.
let receipt = provider.send_transaction(tx).await?.get_receipt().await?;
Expand Down
11 changes: 8 additions & 3 deletions examples/wallets/examples/ledger_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ use alloy::{
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
rpc::types::eth::request::TransactionRequest,
signers::ledger::{HDPath, LedgerSigner},
signers::{
ledger::{HDPath, LedgerSigner},
Signer,
},
};
use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
// Instantiate the application by acquiring a lock on the Ledger device.
let signer = LedgerSigner::new(HDPath::LedgerLive(0), Some(1)).await?;
let from = signer.address();

// Create a provider with the signer.
let rpc_url = "http://localhost:8545".parse()?;
Expand All @@ -23,8 +27,9 @@ async fn main() -> Result<()> {

// Build a transaction to send 100 wei to Vitalik.
let tx = TransactionRequest::default()
.with_value(U256::from(100))
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into());
.with_from(from)
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into())
.with_value(U256::from(100));

// Send the transaction and wait for the receipt.
let receipt =
Expand Down
2 changes: 2 additions & 0 deletions examples/wallets/examples/private_key_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async fn main() -> Result<()> {

// Set up signer from the first default Anvil account (Alice).
let signer: LocalWallet = anvil.keys()[0].clone().into();
let alice = signer.address();

// Create a provider with the signer.
let rpc_url = anvil.endpoint().parse()?;
Expand All @@ -27,6 +28,7 @@ async fn main() -> Result<()> {

// Build a transaction to send 100 wei to Vitalik.
let tx = TransactionRequest::default()
.with_from(alice)
.with_value(U256::from(100))
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into());

Expand Down
11 changes: 8 additions & 3 deletions examples/wallets/examples/trezor_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ use alloy::{
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
rpc::types::eth::request::TransactionRequest,
signers::trezor::{HDPath, TrezorSigner},
signers::{
trezor::{HDPath, TrezorSigner},
Signer,
},
};
use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
// Instantiate the application by acquiring a lock on the Trezor device.
let signer = TrezorSigner::new(HDPath::TrezorLive(0), Some(1)).await?;
let from = signer.address();

// Create a provider with the signer.
let rpc_url = "http://localhost:8545".parse()?;
Expand All @@ -23,8 +27,9 @@ async fn main() -> Result<()> {

// Build a transaction to send 100 wei to Vitalik.
let tx = TransactionRequest::default()
.with_value(U256::from(100))
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into());
.with_from(from)
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into())
.with_value(U256::from(100));

// Send the transaction and wait for the receipt.
let receipt =
Expand Down
15 changes: 10 additions & 5 deletions examples/wallets/examples/yubi_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ use alloy::{
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
rpc::types::eth::request::TransactionRequest,
signers::wallet::{
yubihsm::{Connector, Credentials, UsbConfig},
YubiWallet,
signers::{
wallet::{
yubihsm::{Connector, Credentials, UsbConfig},
YubiWallet,
},
Signer,
},
};
use eyre::Result;
Expand All @@ -22,6 +25,7 @@ async fn main() -> Result<()> {
// `from_key` method to upload a key you already have, or the `new` method
// to generate a new keypair.
let signer = YubiWallet::connect(connector, Credentials::default(), 0);
let from = signer.address();

// Create a provider with the signer.
let rpc_url = "http://localhost:8545".parse()?;
Expand All @@ -32,8 +36,9 @@ async fn main() -> Result<()> {

// Build a transaction to send 100 wei to Vitalik.
let tx = TransactionRequest::default()
.with_value(U256::from(100))
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into());
.with_from(from)
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into())
.with_value(U256::from(100));

// Send the transaction and wait for the receipt.
let receipt =
Expand Down

0 comments on commit e3575e8

Please sign in to comment.