@@ -2,7 +2,7 @@ use graph::{
22 anyhow:: { anyhow, Error } ,
33 blockchain:: { self , Block as BlockchainBlock , TriggerWithHandler } ,
44 components:: { link_resolver:: LinkResolver , store:: StoredDynamicDataSource } ,
5- data:: subgraph:: DataSourceContext ,
5+ data:: subgraph:: { DataSourceContext , SubgraphManifestValidationError } ,
66 prelude:: { async_trait, BlockNumber , DataSourceTemplateInfo , Deserialize , Link , Logger } ,
77 semver,
88} ;
@@ -16,10 +16,11 @@ use crate::{
1616 trigger:: { StarknetEventTrigger , StarknetTrigger } ,
1717} ;
1818
19+ pub const STARKNET_KIND : & str = "starknet" ;
1920const BLOCK_HANDLER_KIND : & str = "block" ;
2021const EVENT_HANDLER_KIND : & str = "event" ;
2122
22- #[ derive( Clone ) ]
23+ #[ derive( Debug , Clone ) ]
2324pub struct DataSource {
2425 pub kind : String ,
2526 pub network : String ,
@@ -28,7 +29,7 @@ pub struct DataSource {
2829 pub mapping : Mapping ,
2930}
3031
31- #[ derive( Clone ) ]
32+ #[ derive( Debug , Clone ) ]
3233pub struct Mapping {
3334 pub block_handler : Option < MappingBlockHandler > ,
3435 pub event_handlers : Vec < MappingEventHandler > ,
@@ -44,7 +45,7 @@ pub struct UnresolvedDataSource {
4445 pub mapping : UnresolvedMapping ,
4546}
4647
47- #[ derive( Clone , PartialEq , Eq , Deserialize ) ]
48+ #[ derive( Debug , Clone , PartialEq , Eq , Deserialize ) ]
4849#[ serde( rename_all = "camelCase" ) ]
4950pub struct Source {
5051 pub start_block : BlockNumber ,
@@ -63,12 +64,12 @@ pub struct UnresolvedMapping {
6364 pub file : Link ,
6465}
6566
66- #[ derive( Clone , PartialEq , Eq , Deserialize ) ]
67+ #[ derive( Debug , Clone , PartialEq , Eq , Deserialize ) ]
6768pub struct MappingBlockHandler {
6869 pub handler : String ,
6970}
7071
71- #[ derive( Clone , PartialEq , Eq , Deserialize ) ]
72+ #[ derive( Debug , Clone , PartialEq , Eq , Deserialize ) ]
7273pub struct MappingEventHandler {
7374 pub handler : String ,
7475 pub event_selector : Felt ,
@@ -201,7 +202,34 @@ impl blockchain::DataSource<Chain> for DataSource {
201202 }
202203
203204 fn validate ( & self ) -> Vec < Error > {
204- Default :: default ( )
205+ let mut errors = Vec :: new ( ) ;
206+
207+ if self . kind != STARKNET_KIND {
208+ errors. push ( anyhow ! (
209+ "data source has invalid `kind`, expected {} but found {}" ,
210+ STARKNET_KIND ,
211+ self . kind
212+ ) )
213+ }
214+
215+ // Validate that there's at least one handler of any kind
216+ if self . mapping . block_handler . is_none ( ) && self . mapping . event_handlers . is_empty ( ) {
217+ errors. push ( anyhow ! ( "data source does not define any handler" ) ) ;
218+ }
219+
220+ // Validate that `source` address must not be present if there's no event handler
221+ if self . mapping . event_handlers . is_empty ( ) && self . address ( ) . is_some ( ) {
222+ errors. push ( anyhow ! (
223+ "data source cannot have source address without event handlers"
224+ ) ) ;
225+ }
226+
227+ // Validate that `source` address must be present when there's at least 1 event handler
228+ if !self . mapping . event_handlers . is_empty ( ) && self . address ( ) . is_none ( ) {
229+ errors. push ( SubgraphManifestValidationError :: SourceAddressRequired . into ( ) ) ;
230+ }
231+
232+ errors
205233 }
206234
207235 fn api_version ( & self ) -> semver:: Version {
0 commit comments