@@ -2,7 +2,7 @@ use graph::{
2
2
anyhow:: { anyhow, Error } ,
3
3
blockchain:: { self , Block as BlockchainBlock , TriggerWithHandler } ,
4
4
components:: { link_resolver:: LinkResolver , store:: StoredDynamicDataSource } ,
5
- data:: subgraph:: DataSourceContext ,
5
+ data:: subgraph:: { DataSourceContext , SubgraphManifestValidationError } ,
6
6
prelude:: { async_trait, BlockNumber , DataSourceTemplateInfo , Deserialize , Link , Logger } ,
7
7
semver,
8
8
} ;
@@ -16,10 +16,11 @@ use crate::{
16
16
trigger:: { StarknetEventTrigger , StarknetTrigger } ,
17
17
} ;
18
18
19
+ pub const STARKNET_KIND : & str = "starknet" ;
19
20
const BLOCK_HANDLER_KIND : & str = "block" ;
20
21
const EVENT_HANDLER_KIND : & str = "event" ;
21
22
22
- #[ derive( Clone ) ]
23
+ #[ derive( Debug , Clone ) ]
23
24
pub struct DataSource {
24
25
pub kind : String ,
25
26
pub network : String ,
@@ -28,7 +29,7 @@ pub struct DataSource {
28
29
pub mapping : Mapping ,
29
30
}
30
31
31
- #[ derive( Clone ) ]
32
+ #[ derive( Debug , Clone ) ]
32
33
pub struct Mapping {
33
34
pub block_handler : Option < MappingBlockHandler > ,
34
35
pub event_handlers : Vec < MappingEventHandler > ,
@@ -44,7 +45,7 @@ pub struct UnresolvedDataSource {
44
45
pub mapping : UnresolvedMapping ,
45
46
}
46
47
47
- #[ derive( Clone , PartialEq , Eq , Deserialize ) ]
48
+ #[ derive( Debug , Clone , PartialEq , Eq , Deserialize ) ]
48
49
#[ serde( rename_all = "camelCase" ) ]
49
50
pub struct Source {
50
51
pub start_block : BlockNumber ,
@@ -63,12 +64,12 @@ pub struct UnresolvedMapping {
63
64
pub file : Link ,
64
65
}
65
66
66
- #[ derive( Clone , PartialEq , Eq , Deserialize ) ]
67
+ #[ derive( Debug , Clone , PartialEq , Eq , Deserialize ) ]
67
68
pub struct MappingBlockHandler {
68
69
pub handler : String ,
69
70
}
70
71
71
- #[ derive( Clone , PartialEq , Eq , Deserialize ) ]
72
+ #[ derive( Debug , Clone , PartialEq , Eq , Deserialize ) ]
72
73
pub struct MappingEventHandler {
73
74
pub handler : String ,
74
75
pub event_selector : Felt ,
@@ -201,7 +202,34 @@ impl blockchain::DataSource<Chain> for DataSource {
201
202
}
202
203
203
204
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
205
233
}
206
234
207
235
fn api_version ( & self ) -> semver:: Version {
0 commit comments