Skip to content

Commit 8ab16ef

Browse files
committed
chain/ethereum: migrate get_balance call to use alloy
1 parent 51b184e commit 8ab16ef

File tree

6 files changed

+57
-17
lines changed

6 files changed

+57
-17
lines changed

chain/ethereum/src/adapter.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use alloy::transports::RpcError;
2+
use alloy::transports::TransportErrorKind;
13
use anyhow::Error;
24
use graph::abi;
35
use graph::blockchain::ChainIdentifier;
@@ -9,7 +11,6 @@ use graph::firehose::CombinedFilter;
911
use graph::firehose::LogFilter;
1012
use graph::prelude::web3::types::Bytes;
1113
use graph::prelude::web3::types::H160;
12-
use graph::prelude::web3::types::U256;
1314
use itertools::Itertools;
1415
use prost::Message;
1516
use prost_types::Any;
@@ -95,6 +96,8 @@ impl EventSignatureWithTopics {
9596
pub enum EthereumRpcError {
9697
#[error("call error: {0}")]
9798
Web3Error(web3::Error),
99+
#[error("call error: {0}")]
100+
AlloyError(RpcError<TransportErrorKind>),
98101
#[error("ethereum node took too long to perform call")]
99102
Timeout,
100103
}
@@ -1171,9 +1174,9 @@ pub trait EthereumAdapter: Send + Sync + 'static {
11711174
async fn get_balance(
11721175
&self,
11731176
logger: &Logger,
1174-
address: H160,
1177+
address: alloy::primitives::Address,
11751178
block_ptr: BlockPtr,
1176-
) -> Result<U256, EthereumRpcError>;
1179+
) -> Result<alloy::primitives::U256, EthereumRpcError>;
11771180

11781181
// Returns the compiled bytecode of a smart contract
11791182
async fn get_code(

chain/ethereum/src/ethereum_adapter.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use graph::futures03::{
2222
self, compat::Future01CompatExt, FutureExt, StreamExt, TryFutureExt, TryStreamExt,
2323
};
2424
use graph::prelude::tokio::try_join;
25-
use graph::prelude::web3::types::U256;
2625
use graph::slog::o;
2726
use graph::tokio::sync::RwLock;
2827
use graph::tokio::time::timeout;
@@ -262,7 +261,7 @@ impl EthereumAdapter {
262261
client: _,
263262
metrics: _,
264263
provider: _,
265-
rpc_url,
264+
url: rpc_url,
266265
} => rpc_url.clone(),
267266
Transport::IPC(_ipc) => todo!(),
268267
Transport::WS(_web_socket) => todo!(),
@@ -649,6 +648,14 @@ impl EthereumAdapter {
649648
}
650649
}
651650

651+
fn block_ptr_to_alloy_block_id(&self, block_ptr: &BlockPtr) -> alloy_rpc_types::BlockId {
652+
if !self.supports_eip_1898 {
653+
alloy_rpc_types::BlockId::number(block_ptr.number as u64)
654+
} else {
655+
alloy_rpc_types::BlockId::hash(B256::new(*block_ptr.hash_as_h256().as_fixed_bytes()))
656+
}
657+
}
658+
652659
async fn code(
653660
&self,
654661
logger: &Logger,
@@ -687,13 +694,13 @@ impl EthereumAdapter {
687694
async fn balance(
688695
&self,
689696
logger: &Logger,
690-
address: Address,
697+
address: alloy::primitives::Address,
691698
block_ptr: BlockPtr,
692-
) -> Result<U256, EthereumRpcError> {
693-
let web3 = self.web3.clone();
699+
) -> Result<alloy::primitives::U256, EthereumRpcError> {
700+
let alloy = self.alloy.clone();
694701
let logger = Logger::new(&logger, o!("provider" => self.provider.clone()));
695702

696-
let block_id = self.block_ptr_to_id(&block_ptr);
703+
let block_id = self.block_ptr_to_alloy_block_id(&block_ptr);
697704
let retry_log_message = format!("eth_getBalance RPC call for block {}", block_ptr);
698705

699706
retry(retry_log_message, &logger)
@@ -705,13 +712,12 @@ impl EthereumAdapter {
705712
.limit(ENV_VARS.request_retries)
706713
.timeout_secs(ENV_VARS.json_rpc_timeout.as_secs())
707714
.run(move || {
708-
let web3 = web3.cheap_clone();
715+
let alloy = alloy.cheap_clone();
709716
async move {
710-
let result: Result<U256, web3::Error> =
711-
web3.eth().balance(address, Some(block_id)).boxed().await;
717+
let result = alloy.get_balance(address).block_id(block_id).await;
712718
match result {
713719
Ok(balance) => Ok(balance),
714-
Err(err) => Err(EthereumRpcError::Web3Error(err)),
720+
Err(err) => Err(EthereumRpcError::AlloyError(err)),
715721
}
716722
}
717723
})
@@ -1508,9 +1514,9 @@ impl EthereumAdapterTrait for EthereumAdapter {
15081514
async fn get_balance(
15091515
&self,
15101516
logger: &Logger,
1511-
address: H160,
1517+
address: alloy::primitives::Address,
15121518
block_ptr: BlockPtr,
1513-
) -> Result<U256, EthereumRpcError> {
1519+
) -> Result<alloy::primitives::U256, EthereumRpcError> {
15141520
debug!(
15151521
logger, "eth_getBalance";
15161522
"address" => format!("{}", address),

chain/ethereum/src/runtime/runtime_adapter.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,18 @@ fn eth_get_balance(
224224
let logger = &ctx.logger;
225225
let block_ptr = &ctx.block_ptr;
226226

227-
let address: H160 = asc_get(ctx.heap, wasm_ptr.into(), &ctx.gas, 0)?;
227+
let address: alloy::primitives::Address = asc_get(ctx.heap, wasm_ptr.into(), &ctx.gas, 0)?;
228228

229229
let result = graph::block_on(eth_adapter.get_balance(logger, address, block_ptr.clone()));
230230

231231
match result {
232232
Ok(v) => {
233-
let bigint = BigInt::from_unsigned_u256(&v);
233+
let bigint = BigInt::from_unsigned_alloy_u256(&v);
234234
Ok(asc_new(ctx.heap, &bigint, &ctx.gas)?)
235235
}
236236
// Retry on any kind of error
237237
Err(EthereumRpcError::Web3Error(e)) => Err(HostExportError::PossibleReorg(e.into())),
238+
Err(EthereumRpcError::AlloyError(e)) => Err(HostExportError::PossibleReorg(e.into())),
238239
Err(EthereumRpcError::Timeout) => Err(HostExportError::PossibleReorg(
239240
EthereumRpcError::Timeout.into(),
240241
)),
@@ -267,6 +268,7 @@ fn eth_has_code(
267268
Ok(v) => Ok(asc_new(ctx.heap, &AscWrapped { inner: v }, &ctx.gas)?),
268269
// Retry on any kind of error
269270
Err(EthereumRpcError::Web3Error(e)) => Err(HostExportError::PossibleReorg(e.into())),
271+
Err(EthereumRpcError::AlloyError(e)) => Err(HostExportError::PossibleReorg(e.into())),
270272
Err(EthereumRpcError::Timeout) => Err(HostExportError::PossibleReorg(
271273
EthereumRpcError::Timeout.into(),
272274
)),

graph/src/data/store/scalar/bigint.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ impl BigInt {
186186
BigInt::from_unsigned_bytes_le(&bytes).unwrap()
187187
}
188188

189+
pub fn from_unsigned_alloy_u256(n: &alloy::primitives::U256) -> Self {
190+
let bytes = n.to_le_bytes_trimmed_vec();
191+
// Unwrap: 256 bits is much less than BigInt::MAX_BITS
192+
BigInt::from_unsigned_bytes_le(&bytes).unwrap()
193+
}
194+
189195
pub fn from_signed_u256(n: &U256) -> Self {
190196
let mut bytes: [u8; 32] = [0; 32];
191197
n.to_little_endian(&mut bytes);

runtime/wasm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version.workspace = true
44
edition.workspace = true
55

66
[dependencies]
7+
alloy = { workspace = true }
78
async-trait = "0.1.50"
89
hex = "0.4.3"
910
graph = { path = "../../graph" }

runtime/wasm/src/to_from/external.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@ impl FromAscObj<Uint8Array> for web3::H160 {
4646
}
4747
}
4848

49+
impl FromAscObj<Uint8Array> for alloy::primitives::Address {
50+
fn from_asc_obj<H: AscHeap + ?Sized>(
51+
typed_array: Uint8Array,
52+
heap: &H,
53+
gas: &GasCounter,
54+
depth: usize,
55+
) -> Result<Self, DeterministicHostError> {
56+
let data = <[u8; 20]>::from_asc_obj(typed_array, heap, gas, depth)?;
57+
Ok(Self::from(data))
58+
}
59+
}
60+
61+
impl ToAscObj<Uint8Array> for alloy::primitives::Address {
62+
fn to_asc_obj<H: AscHeap + ?Sized>(
63+
&self,
64+
heap: &mut H,
65+
gas: &GasCounter,
66+
) -> Result<Uint8Array, HostExportError> {
67+
self.as_slice().to_asc_obj(heap, gas)
68+
}
69+
}
70+
4971
impl FromAscObj<Uint8Array> for web3::H256 {
5072
fn from_asc_obj<H: AscHeap + ?Sized>(
5173
typed_array: Uint8Array,

0 commit comments

Comments
 (0)