Skip to content

Commit 45dda04

Browse files
committed
chain: Modernize EthereumAdapter.get_balance and get_code
1 parent 1560163 commit 45dda04

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed

chain/ethereum/src/adapter.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use graph::data_source::common::ContractCall;
77
use graph::firehose::CallToFilter;
88
use graph::firehose::CombinedFilter;
99
use graph::firehose::LogFilter;
10-
use graph::futures01::Future;
1110
use graph::prelude::web3::types::Bytes;
1211
use graph::prelude::web3::types::H160;
1312
use graph::prelude::web3::types::U256;
@@ -1170,20 +1169,20 @@ pub trait EthereumAdapter: Send + Sync + 'static {
11701169
cache: Arc<dyn EthereumCallCache>,
11711170
) -> Result<Vec<(Option<Vec<Token>>, call::Source)>, ContractCallError>;
11721171

1173-
fn get_balance(
1172+
async fn get_balance(
11741173
&self,
11751174
logger: &Logger,
11761175
address: H160,
11771176
block_ptr: BlockPtr,
1178-
) -> Box<dyn Future<Item = U256, Error = EthereumRpcError> + Send>;
1177+
) -> Result<U256, EthereumRpcError>;
11791178

11801179
// Returns the compiled bytecode of a smart contract
1181-
fn get_code(
1180+
async fn get_code(
11821181
&self,
11831182
logger: &Logger,
11841183
address: H160,
11851184
block_ptr: BlockPtr,
1186-
) -> Box<dyn Future<Item = Bytes, Error = EthereumRpcError> + Send>;
1185+
) -> Result<Bytes, EthereumRpcError>;
11871186
}
11881187

11891188
#[cfg(test)]

chain/ethereum/src/ethereum_adapter.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,12 @@ impl EthereumAdapter {
500500
}
501501
}
502502

503-
fn code(
503+
async fn code(
504504
&self,
505505
logger: &Logger,
506506
address: Address,
507507
block_ptr: BlockPtr,
508-
) -> impl Future<Item = Bytes, Error = EthereumRpcError> + Send {
508+
) -> Result<Bytes, EthereumRpcError> {
509509
let web3 = self.web3.clone();
510510
let logger = Logger::new(&logger, o!("provider" => self.provider.clone()));
511511

@@ -531,17 +531,16 @@ impl EthereumAdapter {
531531
}
532532
}
533533
})
534+
.await
534535
.map_err(|e| e.into_inner().unwrap_or(EthereumRpcError::Timeout))
535-
.boxed()
536-
.compat()
537536
}
538537

539-
fn balance(
538+
async fn balance(
540539
&self,
541540
logger: &Logger,
542541
address: Address,
543542
block_ptr: BlockPtr,
544-
) -> impl Future<Item = U256, Error = EthereumRpcError> + Send {
543+
) -> Result<U256, EthereumRpcError> {
545544
let web3 = self.web3.clone();
546545
let logger = Logger::new(&logger, o!("provider" => self.provider.clone()));
547546

@@ -567,9 +566,8 @@ impl EthereumAdapter {
567566
}
568567
}
569568
})
569+
.await
570570
.map_err(|e| e.into_inner().unwrap_or(EthereumRpcError::Timeout))
571-
.boxed()
572-
.compat()
573571
}
574572

575573
async fn call(
@@ -1470,32 +1468,32 @@ impl EthereumAdapterTrait for EthereumAdapter {
14701468
})
14711469
}
14721470

1473-
fn get_balance(
1471+
async fn get_balance(
14741472
&self,
14751473
logger: &Logger,
14761474
address: H160,
14771475
block_ptr: BlockPtr,
1478-
) -> Box<dyn Future<Item = U256, Error = EthereumRpcError> + Send> {
1476+
) -> Result<U256, EthereumRpcError> {
14791477
debug!(
14801478
logger, "eth_getBalance";
14811479
"address" => format!("{}", address),
14821480
"block" => format!("{}", block_ptr)
14831481
);
1484-
Box::new(self.balance(logger, address, block_ptr))
1482+
self.balance(logger, address, block_ptr).await
14851483
}
14861484

1487-
fn get_code(
1485+
async fn get_code(
14881486
&self,
14891487
logger: &Logger,
14901488
address: H160,
14911489
block_ptr: BlockPtr,
1492-
) -> Box<dyn Future<Item = Bytes, Error = EthereumRpcError> + Send> {
1490+
) -> Result<Bytes, EthereumRpcError> {
14931491
debug!(
14941492
logger, "eth_getCode";
14951493
"address" => format!("{}", address),
14961494
"block" => format!("{}", block_ptr)
14971495
);
1498-
Box::new(self.code(logger, address, block_ptr))
1496+
self.code(logger, address, block_ptr).await
14991497
}
15001498

15011499
async fn next_existing_ptr_to_number(

chain/ethereum/src/runtime/runtime_adapter.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use graph::data::store::scalar::BigInt;
1414
use graph::data::subgraph::API_VERSION_0_0_9;
1515
use graph::data_source;
1616
use graph::data_source::common::{ContractCall, MappingABI};
17-
use graph::futures03::compat::Future01CompatExt;
1817
use graph::prelude::web3::types::H160;
1918
use graph::runtime::gas::Gas;
2019
use graph::runtime::{AscIndexId, IndexForAscTypeId};
@@ -227,11 +226,7 @@ fn eth_get_balance(
227226

228227
let address: H160 = asc_get(ctx.heap, wasm_ptr.into(), &ctx.gas, 0)?;
229228

230-
let result = graph::block_on(
231-
eth_adapter
232-
.get_balance(logger, address, block_ptr.clone())
233-
.compat(),
234-
);
229+
let result = graph::block_on(eth_adapter.get_balance(logger, address, block_ptr.clone()));
235230

236231
match result {
237232
Ok(v) => {
@@ -265,12 +260,8 @@ fn eth_has_code(
265260

266261
let address: H160 = asc_get(ctx.heap, wasm_ptr.into(), &ctx.gas, 0)?;
267262

268-
let result = graph::block_on(
269-
eth_adapter
270-
.get_code(logger, address, block_ptr.clone())
271-
.compat(),
272-
)
273-
.map(|v| !v.0.is_empty());
263+
let result = graph::block_on(eth_adapter.get_code(logger, address, block_ptr.clone()))
264+
.map(|v| !v.0.is_empty());
274265

275266
match result {
276267
Ok(v) => Ok(asc_new(ctx.heap, &AscWrapped { inner: v }, &ctx.gas)?),

0 commit comments

Comments
 (0)