Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd6a0c0

Browse files
committed
graph, store: Parse and persist field topic0
1 parent 2f0bf68 commit cd6a0c0

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

graph/src/data/graphql/values.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use graphql_parser::query::Value;
33
use std::collections::BTreeMap;
44
use std::str::FromStr;
55

6-
use crate::web3::types::H160;
6+
use crate::web3::types::{H160, H256};
77

88
pub trait TryFromValue: Sized {
99
fn try_from_value(value: &Value) -> Result<Self, Error>;
@@ -37,6 +37,20 @@ impl TryFromValue for H160 {
3737
}
3838
}
3939

40+
impl TryFromValue for H256 {
41+
fn try_from_value(value: &Value) -> Result<Self, Error> {
42+
match value {
43+
Value::String(s) => {
44+
// `H160::from_str` takes a hex string with no leading `0x`.
45+
let string = s.trim_start_matches("0x");
46+
H256::from_str(string)
47+
.map_err(|e| format_err!("Cannot parse H256 value from string `{}`: {}", s, e))
48+
}
49+
_ => Err(format_err!("Cannot parse value into an H256: {:?}", value)),
50+
}
51+
}
52+
}
53+
4054
impl<T> TryFromValue for Vec<T>
4155
where
4256
T: TryFromValue,

graph/src/data/subgraph/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::ops::Deref;
1212
use std::str::FromStr;
1313
use std::sync::Arc;
1414
use tokio::prelude::*;
15-
use web3::types::Address;
15+
use web3::types::{Address, H256};
1616

1717
use crate::components::link_resolver::LinkResolver;
1818
use crate::components::store::StoreError;
@@ -406,13 +406,15 @@ impl UnresolvedMappingABI {
406406
#[derive(Clone, Debug, Hash, Eq, PartialEq, Deserialize)]
407407
pub struct MappingEventHandler {
408408
pub event: String,
409+
pub topic0: Option<H256>,
409410
pub handler: String,
410411
}
411412

412413
impl From<EthereumContractEventHandlerEntity> for MappingEventHandler {
413414
fn from(entity: EthereumContractEventHandlerEntity) -> Self {
414415
Self {
415416
event: entity.event,
417+
topic0: entity.topic0,
416418
handler: entity.handler,
417419
}
418420
}

graph/src/data/subgraph/schema.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ impl TryFromValue for EthereumContractAbiEntity {
765765
#[derive(Debug)]
766766
pub struct EthereumContractEventHandlerEntity {
767767
pub event: String,
768+
pub topic0: Option<H256>,
768769
pub handler: String,
769770
}
770771

@@ -778,6 +779,7 @@ impl EthereumContractEventHandlerEntity {
778779
let mut entity = Entity::new();
779780
entity.set("id", id);
780781
entity.set("event", self.event);
782+
entity.set("topic0", self.topic0.map_or(Value::Null, Value::from));
781783
entity.set("handler", self.handler);
782784
vec![set_entity_operation(Self::TYPENAME, id, entity)]
783785
}
@@ -787,6 +789,7 @@ impl From<super::MappingEventHandler> for EthereumContractEventHandlerEntity {
787789
fn from(event_handler: super::MappingEventHandler) -> Self {
788790
Self {
789791
event: event_handler.event,
792+
topic0: event_handler.topic0,
790793
handler: event_handler.handler,
791794
}
792795
}
@@ -804,6 +807,7 @@ impl TryFromValue for EthereumContractEventHandlerEntity {
804807

805808
Ok(Self {
806809
event: map.get_required("event")?,
810+
topic0: map.get_optional("topic0")?,
807811
handler: map.get_required("handler")?,
808812
})
809813
}

store/postgres/src/subgraphs.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type EthereumContractAbi @entity {
9090
type EthereumContractEventHandler @entity {
9191
id: ID!
9292
event: String!
93+
topic0: Bytes
9394
handler: String!
9495
}
9596

0 commit comments

Comments
 (0)