Skip to content

Commit fb624ed

Browse files
committed
graph, chain: code cleanup and refactor
1 parent a134343 commit fb624ed

File tree

9 files changed

+35
-45
lines changed

9 files changed

+35
-45
lines changed

chain/ethereum/src/codec.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,13 @@ where
4141
}
4242
}
4343

44-
// impl TryDecodeProto<[u8; 256], H2048> for &[u8] {}
45-
// impl TryDecodeProto<[u8; 32], H256> for &[u8] {}
46-
// impl TryDecodeProto<[u8; 20], H160> for &[u8] {}
47-
4844
impl TryDecodeProto<[u8; 32], B256> for &[u8] {}
4945
impl TryDecodeProto<[u8; 256], B2048> for &[u8] {}
5046
impl TryDecodeProto<[u8; 20], Address> for &[u8] {}
5147

52-
impl From<&BigInt> for alloy::primitives::U256 {
48+
impl From<&BigInt> for U256 {
5349
fn from(val: &BigInt) -> Self {
54-
alloy::primitives::U256::from_be_slice(&val.bytes)
50+
U256::from_be_slice(&val.bytes)
5551
}
5652
}
5753

@@ -78,7 +74,7 @@ impl<'a> TryInto<EthereumCall> for CallAt<'a> {
7874
.call
7975
.value
8076
.as_ref()
81-
.map_or_else(|| alloy::primitives::U256::from(0), |v| v.into()),
77+
.map_or_else(|| U256::from(0), |v| v.into()),
8278
gas_used: self.call.gas_consumed,
8379
input: Bytes::from(self.call.input.clone()),
8480
output: Bytes::from(self.call.return_data.clone()),

chain/ethereum/src/data_source.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,9 +1589,6 @@ fn string_to_b256(s: &str) -> B256 {
15891589
sponge.update(&data);
15901590
sponge.finalize(&mut result);
15911591

1592-
// This was deprecated but the replacement seems to not be available in the
1593-
// version web3 uses.
1594-
#[allow(deprecated)]
15951592
B256::from_slice(&result)
15961593
}
15971594

chain/ethereum/src/ethereum_adapter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,7 @@ async fn filter_call_triggers_from_unsuccessful_transactions(
20472047
match &block.block {
20482048
BlockFinality::Final(ref block) => block
20492049
.transactions()
2050+
.ok_or_else(|| anyhow!("Block transactions not available"))?
20502051
.iter()
20512052
.filter(|transaction| transaction_hashes.contains(transaction.inner.tx_hash()))
20522053
.collect(),

graph/src/blockchain/types.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@ impl fmt::LowerHex for BlockHash {
8585
}
8686
}
8787

88-
// impl From<H256> for BlockHash {
89-
// fn from(hash: H256) -> Self {
90-
// BlockHash(hash.as_bytes().into())
91-
// }
92-
// }
93-
9488
impl From<Vec<u8>> for BlockHash {
9589
fn from(bytes: Vec<u8>) -> Self {
9690
BlockHash(bytes.as_slice().into())

graph/src/components/ethereum/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ impl BlockWrapper {
3434
self.0.header.timestamp
3535
}
3636

37-
pub fn transactions(&self) -> &[Transaction] {
38-
&self.0.transactions.as_transactions().unwrap()
37+
pub fn transactions(&self) -> Option<&[Transaction]> {
38+
self.0.transactions.as_transactions()
3939
}
4040

4141
pub fn inner(&self) -> &AlloyBlock {

graph/src/data_source/common.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,11 @@ impl CallDecl {
415415
self.expr.validate_args()
416416
}
417417

418-
pub fn address_for_log(&self, log: &Log, params: &[abi::DynSolParam]) -> Result<Address, Error> {
418+
pub fn address_for_log(
419+
&self,
420+
log: &Log,
421+
params: &[abi::DynSolParam],
422+
) -> Result<Address, Error> {
419423
self.address_for_log_with_abi(log, params)
420424
}
421425

@@ -1643,9 +1647,9 @@ mod tests {
16431647
let parser = ExprParser::new();
16441648

16451649
let tuple_fields = vec![
1646-
DynSolValue::Uint(U256::from(8u8), 8), // index 0: uint8
1647-
DynSolValue::Address(Address::from([1u8; 20])), // index 1: address
1648-
DynSolValue::Uint(U256::from(1000u64), 256), // index 2: uint256
1650+
DynSolValue::Uint(U256::from(8u8), 8), // index 0: uint8
1651+
DynSolValue::Address(Address::from([1u8; 20])), // index 1: address
1652+
DynSolValue::Uint(U256::from(1000u64), 256), // index 2: uint256
16491653
];
16501654

16511655
// Test extract_struct_field with numeric indices
@@ -1687,9 +1691,9 @@ mod tests {
16871691

16881692
#[test]
16891693
fn test_declarative_call_error_context() {
1690-
use alloy::rpc::types::Log;
1691-
use crate::abi::{DynSolValue, DynSolParam};
1694+
use crate::abi::{DynSolParam, DynSolValue};
16921695
use alloy::primitives::U256;
1696+
use alloy::rpc::types::Log;
16931697

16941698
let parser = ExprParser::new();
16951699

@@ -1769,14 +1773,14 @@ mod tests {
17691773
// The parser thinks there should be 3 fields based on ABI, but at runtime we provide only 2
17701774
let base_struct = DynSolValue::Tuple(vec![
17711775
DynSolValue::Address(alloy::primitives::Address::from([1u8; 20])), // addr at index 0
1772-
DynSolValue::Uint(U256::from(100u64), 256), // amount at index 1
1773-
// Missing the active field at index 2!
1776+
DynSolValue::Uint(U256::from(100u64), 256), // amount at index 1
1777+
// Missing the active field at index 2!
17741778
]);
17751779

17761780
let params = vec![DynSolParam {
17771781
name: "complexAsset".to_string(),
17781782
value: DynSolValue::Tuple(vec![
1779-
base_struct, // base with only 2 fields
1783+
base_struct, // base with only 2 fields
17801784
DynSolValue::String("metadata".to_string()), // metadata at index 1
17811785
DynSolValue::Array(vec![]), // values at index 2
17821786
]),
@@ -1806,13 +1810,13 @@ mod tests {
18061810
// string name; // index 1
18071811
// }
18081812
let inner_struct = DynSolValue::Tuple(vec![
1809-
DynSolValue::Address(Address::from([0x42; 20])), // token.addr
1810-
DynSolValue::String("TokenName".to_string()), // token.name
1813+
DynSolValue::Address(Address::from([0x42; 20])), // token.addr
1814+
DynSolValue::String("TokenName".to_string()), // token.name
18111815
]);
18121816

18131817
let outer_struct = DynSolValue::Tuple(vec![
1814-
DynSolValue::Uint(U256::from(1u8), 8), // asset.kind
1815-
inner_struct, // asset.token
1818+
DynSolValue::Uint(U256::from(1u8), 8), // asset.kind
1819+
inner_struct, // asset.token
18161820
DynSolValue::Uint(U256::from(1000u64), 256), // asset.amount
18171821
]);
18181822

runtime/wasm/src/to_from/external.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use graph::{
1616
},
1717
runtime::FromAscObj,
1818
};
19-
use graph_runtime_derive::AscType;
2019

2120
use crate::asc_abi::class::*;
2221

tests/src/contract.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ use std::str::FromStr;
33
use graph::prelude::{
44
lazy_static,
55
serde_json::{self, Value},
6-
web3::{
7-
api::{Eth, Namespace},
8-
contract::{tokens::Tokenize, Contract as Web3Contract, Options},
9-
transports::Http,
10-
types::{Address, Block, BlockId, BlockNumber, Bytes, TransactionReceipt, H256},
11-
},
6+
};
7+
8+
use web3::{
9+
api::{Eth, Namespace},
10+
contract::{tokens::Tokenize, Contract as Web3Contract, Options},
11+
transports::Http,
12+
types::{Address, Block, BlockId, BlockNumber, Bytes, TransactionReceipt, H256},
1213
};
1314
// web3 version 0.18 does not expose this; once the graph crate updates to
1415
// version 0.19, we can use web3::signing::SecretKey from the graph crate
@@ -161,16 +162,16 @@ impl Contract {
161162
if contract.name == "DeclaredCallsContract" {
162163
status!("contracts", "Emitting transfers from DeclaredCallsContract");
163164
let addr1 = "0x1111111111111111111111111111111111111111"
164-
.parse::<graph::prelude::web3::types::Address>()
165+
.parse::<web3::types::Address>()
165166
.unwrap();
166167
let addr2 = "0x2222222222222222222222222222222222222222"
167-
.parse::<graph::prelude::web3::types::Address>()
168+
.parse::<web3::types::Address>()
168169
.unwrap();
169170
let addr3 = "0x3333333333333333333333333333333333333333"
170-
.parse::<graph::prelude::web3::types::Address>()
171+
.parse::<web3::types::Address>()
171172
.unwrap();
172173
let addr4 = "0x4444444444444444444444444444444444444444"
173-
.parse::<graph::prelude::web3::types::Address>()
174+
.parse::<web3::types::Address>()
174175
.unwrap();
175176

176177
contract

tests/tests/runner_tests.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ use graph::env::{EnvVars, TEST_WITH_NO_REORG};
1515
use graph::ipfs::test_utils::add_files_to_local_ipfs_node_for_testing;
1616
use graph::object;
1717
use graph::prelude::alloy::primitives::{Address, B256, U256};
18-
use graph::prelude::{
19-
hex, CheapClone, SubgraphAssignmentProvider, SubgraphName, SubgraphStore,
20-
};
18+
use graph::prelude::{hex, CheapClone, SubgraphAssignmentProvider, SubgraphName, SubgraphStore};
2119
use graph_tests::fixture::ethereum::{
2220
chain, empty_block, generate_empty_blocks_for_range, genesis, push_test_command, push_test_log,
2321
push_test_polling_trigger,

0 commit comments

Comments
 (0)