Skip to content

Commit

Permalink
feat(queries): query examples: logs, contract_storage and bytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-atreya committed Mar 14, 2024
1 parent 1cb4aea commit ccb71eb
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license = "MIT OR Apache-2.0"
homepage = "https://github.com/alloy-rs/examples"
repository = "https://github.com/alloy-rs/examples"
publish = false
exclude = ["examples/"]

[workspace.dependencies]
alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "d5967ab", default-features = false }
Expand Down
32 changes: 32 additions & 0 deletions examples/queries/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "examples-queries"
version = "0.0.0"
publish = false

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

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dev-dependencies]
alloy-primitives.workspace = true
alloy-sol-types = { workspace = true, features = ["json"] }
alloy-provider = { workspace = true, features = ["pubsub", "ws"] }
alloy-contract.workspace = true
alloy-pubsub.workspace = true
alloy-rpc-types.workspace = true
alloy-rpc-client.workspace = true
alloy-rpc-trace-types.workspace = true
alloy-node-bindings.workspace = true
alloy-transport.workspace = true
alloy-transport-http.workspace = true
alloy-network.workspace = true
tokio = { version = "1.36.0", features = ["rt-multi-thread", "macros"] }
eyre = "0.6.12"
reqwest = "0.11.26"
futures-util = "0.3"
29 changes: 29 additions & 0 deletions examples/queries/examples/query_contract_storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use alloy_network::Ethereum;
use alloy_primitives::{address, fixed_bytes};
use alloy_provider::{HttpProvider, Provider};
use alloy_rpc_client::RpcClient;
use alloy_transport_http::Http;
use eyre::Result;
use reqwest::Client;

#[tokio::main]
async fn main() -> Result<()> {
let provider = init();

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

let storage_slot =
fixed_bytes!("0000000000000000000000000000000000000000000000000000000000000000");

let storage = provider.get_storage_at(pool_address, storage_slot.into(), None).await?;

println!("Slot 0: {:?}", storage);

Ok(())
}

fn init() -> HttpProvider<Ethereum> {
let http = Http::<Client>::new("https://eth.llamarpc.com".parse().unwrap());
HttpProvider::new(RpcClient::new(http, true))
}
28 changes: 28 additions & 0 deletions examples/queries/examples/query_deployed_bytecode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use alloy_network::Ethereum;
use alloy_primitives::address;
use alloy_provider::{HttpProvider, Provider};
use alloy_rpc_client::RpcClient;
use alloy_rpc_types::{BlockId, BlockNumberOrTag};
use alloy_transport_http::Http;
use eyre::Result;
use reqwest::Client;

#[tokio::main]
async fn main() -> Result<()> {
let provider = init();

// 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(())
}

fn init() -> HttpProvider<Ethereum> {
let http = Http::<Client>::new("https://eth.llamarpc.com".parse().unwrap());
HttpProvider::new(RpcClient::new(http, true))
}
32 changes: 32 additions & 0 deletions examples/queries/examples/query_logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use alloy_network::Ethereum;
use alloy_provider::{HttpProvider, Provider};
use alloy_rpc_client::RpcClient;
use alloy_rpc_types::Filter;
use alloy_transport_http::Http;
use eyre::Result;
use reqwest::Client;
#[tokio::main]
async fn main() -> Result<()> {
let provider = init();

// Get logs from the latest block
let latest_block = provider.get_block_number().await?;
let filter = Filter::new().from_block(latest_block);
// .address(vec![address!("1f9840a85d5aF5bf1D1762F925BDADdC4201F984")]) // Emitted by the
// UNI token
// .event("Transfer(address,address,uint256)")
// .event_signature(fixed_bytes!("
// ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")); // Using transfer event
// signature
let logs = provider.get_logs(&filter).await?;

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

fn init() -> HttpProvider<Ethereum> {
let http = Http::<Client>::new("https://eth.llamarpc.com".parse().unwrap());
HttpProvider::new(RpcClient::new(http, true))
}

0 comments on commit ccb71eb

Please sign in to comment.