Skip to content

Commit e295cfd

Browse files
committed
graph: Add util functions for alloy web3 conversions
1 parent d9c4ecd commit e295cfd

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

graph/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ pub mod prelude {
176176
};
177177
pub use crate::log::split::split_logger;
178178
pub use crate::util::cache_weight::CacheWeight;
179+
pub use crate::util::conversions::{
180+
alloy_address_to_h160, alloy_address_to_web3_address, alloy_log_to_web3_log, b256_to_h256,
181+
h160_to_alloy_address, h256_to_b256, web3_address_to_alloy_address,
182+
};
179183
pub use crate::util::futures::{retry, TimeoutError};
180184
pub use crate::util::stats::MovingStats;
181185

graph/src/util/conversions.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use crate::prelude::alloy::primitives::{Address as AlloyAddress, B256};
2+
use crate::prelude::alloy::rpc::types::Log as AlloyLog;
3+
/// Type conversion utilities between web3 and alloy types
4+
use crate::prelude::web3::types::{Address as Web3Address, Log as Web3Log, H160, H256, U256, U64};
5+
6+
/// Converts web3 H256 to alloy B256
7+
pub fn h256_to_b256(h: H256) -> B256 {
8+
B256::from_slice(h.as_bytes())
9+
}
10+
11+
/// Converts alloy B256 to web3 H256
12+
pub fn b256_to_h256(b: B256) -> H256 {
13+
H256::from_slice(b.as_slice())
14+
}
15+
16+
/// Converts web3 H160 to alloy Address
17+
pub fn h160_to_alloy_address(h: H160) -> AlloyAddress {
18+
AlloyAddress::from_slice(h.as_bytes())
19+
}
20+
21+
/// Converts alloy Address to web3 H160
22+
pub fn alloy_address_to_h160(addr: AlloyAddress) -> H160 {
23+
H160::from_slice(addr.as_slice())
24+
}
25+
26+
/// Converts web3 Address to alloy Address
27+
pub fn web3_address_to_alloy_address(addr: Web3Address) -> AlloyAddress {
28+
h160_to_alloy_address(addr)
29+
}
30+
31+
/// Converts alloy Address to web3 Address
32+
pub fn alloy_address_to_web3_address(addr: AlloyAddress) -> Web3Address {
33+
alloy_address_to_h160(addr)
34+
}
35+
36+
/// Converts alloy Log to web3 Log
37+
pub fn alloy_log_to_web3_log(log: AlloyLog) -> Web3Log {
38+
Web3Log {
39+
address: alloy_address_to_h160(log.address()),
40+
topics: log.topics().iter().map(|t| b256_to_h256(*t)).collect(),
41+
data: log.data().data.clone().into(),
42+
block_hash: log.block_hash.map(b256_to_h256),
43+
block_number: log.block_number.map(|n| U64::from(n)),
44+
transaction_hash: log.transaction_hash.map(b256_to_h256),
45+
transaction_index: log.transaction_index.map(|i| U64::from(i)),
46+
log_index: log.log_index.map(|i| U256::from(i)),
47+
transaction_log_index: None, // alloy Log doesn't have transaction_log_index
48+
log_type: None, // alloy Log doesn't have log_type
49+
removed: Some(log.removed),
50+
}
51+
}
52+
53+
#[cfg(test)]
54+
mod tests {
55+
use super::*;
56+
57+
#[test]
58+
fn test_h256_to_b256_conversion() {
59+
let h = H256::from([1u8; 32]);
60+
let b = h256_to_b256(h);
61+
assert_eq!(b.as_slice(), &[1u8; 32]);
62+
}
63+
64+
#[test]
65+
fn test_b256_to_h256_conversion() {
66+
let b = B256::from([2u8; 32]);
67+
let h = b256_to_h256(b);
68+
assert_eq!(h.as_bytes(), &[2u8; 32]);
69+
}
70+
71+
#[test]
72+
fn test_round_trip_conversion() {
73+
let original_h = H256::from([42u8; 32]);
74+
let b = h256_to_b256(original_h);
75+
let converted_h = b256_to_h256(b);
76+
assert_eq!(original_h, converted_h);
77+
}
78+
79+
#[test]
80+
fn test_h160_to_alloy_address_conversion() {
81+
let h = H160::from([1u8; 20]);
82+
let addr = h160_to_alloy_address(h);
83+
assert_eq!(addr.as_slice(), &[1u8; 20]);
84+
}
85+
86+
#[test]
87+
fn test_alloy_address_to_h160_conversion() {
88+
let addr = AlloyAddress::from([2u8; 20]);
89+
let h = alloy_address_to_h160(addr);
90+
assert_eq!(h.as_bytes(), &[2u8; 20]);
91+
}
92+
93+
#[test]
94+
fn test_address_round_trip_conversion() {
95+
let original_h = H160::from([42u8; 20]);
96+
let addr = h160_to_alloy_address(original_h);
97+
let converted_h = alloy_address_to_h160(addr);
98+
assert_eq!(original_h, converted_h);
99+
}
100+
}

graph/src/util/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ pub mod monitored;
3535
pub mod intern;
3636

3737
pub mod herd_cache;
38+
39+
/// Type conversion utilities between web3 and alloy types
40+
pub mod conversions;

0 commit comments

Comments
 (0)