Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(queries): query examples #6

Merged
merged 9 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ cargo run --example mnemonic_signer
- [ ] Retry
- [ ] RW
- [ ] WS
- [ ] Queries
- [ ] Blocks
- [ ] Contracts
- [ ] Events
- [ ] Paginated logs
- [ ] UniswapV2 pair
- [ ] Transactions
- [x] Queries
- [x] [Contract storage](./examples/queries/examples/query_contract_storage.rs)
- [x] [Contract deployed bytecode](./examples/queries/examples/query_deployed_bytecode.rs)
- [x] [Logs](./examples/queries/examples/query_logs.rs)
- [ ] Subscriptions
- [ ] Watch blocks
- [ ] Subscribe events by type
Expand Down
25 changes: 25 additions & 0 deletions examples/queries/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "examples-queries"

publish.workspace = true
version.workspace = true
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[dev-dependencies]
alloy.workspace = true
# alloy-network.workspace = true
# alloy-primitives.workspace = true
# alloy-provider = { workspace = true, features = ["pubsub", "ws"] }
# alloy-rpc-types.workspace = true
# alloy-rpc-client.workspace = true
# alloy-transport-http.workspace = true

eyre.workspace = true
futures-util = "0.3"
reqwest.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
25 changes: 25 additions & 0 deletions examples/queries/examples/query_contract_storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! Example of querying contract storage from the Ethereum network.

use alloy::{
network::Ethereum,
primitives::{address, U256},
providers::{Provider, RootProvider},
};
use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
let url = "https://eth.merkle.io".parse().unwrap();
let provider = RootProvider::<Ethereum, _>::new_http(url);

// Get slot0 from USDC-ETH Uniswap V3 pool
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);

Ok(())
}
25 changes: 25 additions & 0 deletions examples/queries/examples/query_deployed_bytecode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! Example of querying deployed bytecode of a contract on Ethereum network.

use alloy::{
network::Ethereum,
primitives::address,
providers::{Provider, RootProvider},
rpc::types::eth::{BlockId, BlockNumberOrTag},
};
use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
let url = "https://eth.merkle.io".parse().unwrap();
let provider = RootProvider::<Ethereum, _>::new_http(url);

// Get bytecode of USDC-ETH Uniswap V3 pool
let pool_address = address!("88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640");

let bytecode =
provider.get_code_at(pool_address, BlockId::Number(BlockNumberOrTag::Latest)).await?;

println!("Bytecode: {:?}", bytecode);

Ok(())
}
51 changes: 51 additions & 0 deletions examples/queries/examples/query_logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use alloy::{
network::Ethereum,
primitives::{address, fixed_bytes},
providers::{Provider, RootProvider},
rpc::types::eth::Filter,
};
use eyre::Result;
#[tokio::main]
async fn main() -> Result<()> {
let url = "https://eth.merkle.io".parse().unwrap();
let provider = RootProvider::<Ethereum, _>::new_http(url);

// Get logs from the latest block
let latest_block = provider.get_block_number().await?;

// Get all logs from the latest block
let filter = Filter::new().from_block(latest_block);

let logs = provider.get_logs(&filter).await?;

for log in logs {
println!("{:?}", log);
}

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)")

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);

let logs = provider.get_logs(&filter).await?;

for log in logs {
println!("UNI token logs: {:?}", log);
}

Ok(())
}
Loading