apibara-dna-sdk 2.0.1

The SDK to connect and stream data from Apibara DNA services.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Starknet-specific types and utilities.
//!
//! This module contains the auto-generated gRPC types that are specific to Starknet.
//!
//! ## Builders
//!
//! We provide the following builders to simplify generating the [Filter].
//!
//!  - [FilterBuilder] - the root filter builder.
//!  - [TransactionFilterBuilder] - the transaction filter builder.
//!  - [EventFilterBuilder] - the event filter builder.
//!  - [MessageToL1FilterBuilder] - the message to L1 filter builder.
//!  - [StorageDiffFilterBuilder] - the storage diff filter builder.
//!  - [ContractChangeFilterBuilder] - the contract change filter builder.
//!  - [NonceUpdateFilterBuilder] - the nonce update filter builder.
pub use apibara_dna_protocol::starknet::*;

/// Builder for [Filter].
#[derive(Debug, Clone)]
pub struct FilterBuilder {
    inner: Filter,
}

/// Builder for [TransactionFilter].
///
/// Generally speaking, you can filter transactions by their type and status.
///
/// You can also "join" the transaction with its:
///
///  - receipt
///  - events
///  - messages to L1
///  - trace (if enabled in the server deployment)
///
/// By default, no join is performed.
#[derive(Debug, Clone)]
pub struct TransactionFilterBuilder {
    inner: TransactionFilter,
}

/// Builder for [EventFilter].
///
/// You can also "join" the event with its:
///
///  - receipt
///  - transaction
///  - siblings (other events emitted by the same transaction)
///  - messages to L1
///  - transaction's trace (if enabled in the server deployment)
///
/// By default, no join is performed.
#[derive(Debug, Clone)]
pub struct EventFilterBuilder {
    inner: EventFilter,
}

/// Builder for [MessageToL1Filter].
///
/// You can also "join" the message with its:
///
///  - receipt
///  - transaction
///  - events emitted by the same transaction
///  - siblings (other messages emitted by the same transaction)
///  - transaction's trace (if enabled in the server deployment)
///
/// By default, no join is performed.
#[derive(Debug, Clone)]
pub struct MessageToL1FilterBuilder {
    inner: MessageToL1Filter,
}

/// Builder for [StorageDiffFilter].
#[derive(Debug, Clone)]
pub struct StorageDiffFilterBuilder {
    inner: StorageDiffFilter,
}

/// Builder for [ContractChangeFilter].
#[derive(Debug, Clone)]
pub struct ContractChangeFilterBuilder {
    inner: ContractChangeFilter,
}

/// Builder for [NonceUpdateFilter].
#[derive(Debug, Clone)]
pub struct NonceUpdateFilterBuilder {
    inner: NonceUpdateFilter,
}

impl FilterBuilder {
    /// Creates a new [FilterBuilder].
    ///
    /// The default header filter is set to [HeaderFilter::OnDataOrOnNewBlock].
    /// All other fields are empty.
    pub fn new() -> Self {
        let inner = Filter {
            header: HeaderFilter::OnDataOrOnNewBlock as i32,
            ..Default::default()
        };
        Self { inner }
    }

    /// Sets the header filter.
    pub fn with_header(mut self, header: HeaderFilter) -> Self {
        self.inner.header = header as i32;
        self
    }

    /// Adds a [TransactionFilter].
    ///
    /// Use [TransactionFilterBuilder] to create a [TransactionFilter].
    pub fn add_transaction(mut self, transaction: TransactionFilter) -> Self {
        self.inner.transactions.push(transaction);
        self
    }

    /// Adds a [EventFilter].
    ///
    /// Use [EventFilterBuilder] to create a [EventFilter].
    pub fn add_event(mut self, event: EventFilter) -> Self {
        self.inner.events.push(event);
        self
    }

    /// Adds a [MessageToL1Filter].
    ///
    /// Use [MessageToL1FilterBuilder] to create a [MessageToL1Filter].
    pub fn add_message(mut self, message: MessageToL1Filter) -> Self {
        self.inner.messages.push(message);
        self
    }

    /// Adds a [StorageDiffFilter].
    ///
    /// Use [StorageDiffFilterBuilder] to create a [StorageDiffFilter].
    pub fn add_storage_diff(mut self, storage_diff: StorageDiffFilter) -> Self {
        self.inner.storage_diffs.push(storage_diff);
        self
    }

    /// Adds a [ContractChangeFilter].
    ///
    /// Use [ContractChangeFilterBuilder] to create a [ContractChangeFilter].
    pub fn add_contract_change(mut self, contract_change: ContractChangeFilter) -> Self {
        self.inner.contract_changes.push(contract_change);
        self
    }

    /// Adds a [NonceUpdateFilter].
    ///
    /// Use [NonceUpdateFilterBuilder] to create a [NonceUpdateFilter].
    pub fn add_nonce_update(mut self, nonce_update: NonceUpdateFilter) -> Self {
        self.inner.nonce_updates.push(nonce_update);
        self
    }

    /// Builds the [Filter].
    pub fn build(self) -> Filter {
        self.inner
    }
}

impl Default for FilterBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl TransactionFilterBuilder {
    /// Requests transactions of any type.
    pub fn all_transaction_types() -> Self {
        let inner = TransactionFilter {
            inner: None,
            ..Default::default()
        };
        Self { inner }
    }

    /// Requests only "invoke v0" transactions.
    pub fn only_invoke_v0() -> Self {
        use transaction_filter::Inner;
        let inner = TransactionFilter {
            inner: Some(Inner::InvokeV0(InvokeTransactionV0Filter::default())),
            ..Default::default()
        };
        Self { inner }
    }

    /// Requests only "invoke v1" transactions.
    pub fn only_invoke_v1() -> Self {
        use transaction_filter::Inner;
        let inner = TransactionFilter {
            inner: Some(Inner::InvokeV1(InvokeTransactionV1Filter::default())),
            ..Default::default()
        };
        Self { inner }
    }

    /// Requests only "invoke v3" transactions.
    pub fn only_invoke_v3() -> Self {
        use transaction_filter::Inner;
        let inner = TransactionFilter {
            inner: Some(Inner::InvokeV3(InvokeTransactionV3Filter::default())),
            ..Default::default()
        };
        Self { inner }
    }

    /// Requests only "deploy" transactions.
    pub fn only_deploy() -> Self {
        use transaction_filter::Inner;
        let inner = TransactionFilter {
            inner: Some(Inner::Deploy(DeployTransactionFilter::default())),
            ..Default::default()
        };
        Self { inner }
    }

    /// Requests only "declare v0" transactions.
    pub fn only_declare_v0() -> Self {
        use transaction_filter::Inner;
        let inner = TransactionFilter {
            inner: Some(Inner::DeclareV0(DeclareV0TransactionFilter::default())),
            ..Default::default()
        };
        Self { inner }
    }

    /// Requests only "declare v1" transactions.
    pub fn only_declare_v1() -> Self {
        use transaction_filter::Inner;
        let inner = TransactionFilter {
            inner: Some(Inner::DeclareV1(DeclareV1TransactionFilter::default())),
            ..Default::default()
        };
        Self { inner }
    }

    /// Requests only "declare v2" transactions.
    pub fn only_declare_v2() -> Self {
        use transaction_filter::Inner;
        let inner = TransactionFilter {
            inner: Some(Inner::DeclareV2(DeclareV2TransactionFilter::default())),
            ..Default::default()
        };
        Self { inner }
    }

    /// Requests only "declare v3" transactions.
    pub fn only_declare_v3() -> Self {
        use transaction_filter::Inner;
        let inner = TransactionFilter {
            inner: Some(Inner::DeclareV3(DeclareV3TransactionFilter::default())),
            ..Default::default()
        };
        Self { inner }
    }

    /// Requests only "l1 handler" transactions.
    pub fn only_l1_handler() -> Self {
        use transaction_filter::Inner;
        let inner = TransactionFilter {
            inner: Some(Inner::L1Handler(L1HandlerTransactionFilter::default())),
            ..Default::default()
        };
        Self { inner }
    }

    /// Requests only "deploy account v1" transactions.
    pub fn only_deploy_account_v1() -> Self {
        use transaction_filter::Inner;
        let inner = TransactionFilter {
            inner: Some(Inner::DeployAccountV1(
                DeployAccountV1TransactionFilter::default(),
            )),
            ..Default::default()
        };
        Self { inner }
    }

    /// Requests only "deploy account v3" transactions.
    pub fn only_deploy_account_v3() -> Self {
        use transaction_filter::Inner;
        let inner = TransactionFilter {
            inner: Some(Inner::DeployAccountV3(
                DeployAccountV3TransactionFilter::default(),
            )),
            ..Default::default()
        };
        Self { inner }
    }

    /// Assign a new id to the filter.
    ///
    /// Data produced by this filter will be associated with the given id.
    pub fn with_id(mut self, id: u32) -> Self {
        self.inner.id = id;
        self
    }

    /// Requests only transactions with the given status.
    pub fn with_transaction_status(mut self, status: TransactionStatusFilter) -> Self {
        self.inner.set_transaction_status(status);
        self
    }

    /// Requests to include the transaction's receipt.
    pub fn with_receipt(mut self) -> Self {
        self.inner.include_receipt = Some(true);
        self
    }

    /// Requests to include the transaction's events.
    pub fn with_events(mut self) -> Self {
        self.inner.include_events = Some(true);
        self
    }

    /// Requests to include the transaction's messages to L1.
    pub fn with_messages(mut self) -> Self {
        self.inner.include_messages = Some(true);
        self
    }

    /// Requests to include the transaction's trace.
    pub fn with_trace(mut self) -> Self {
        self.inner.include_trace = Some(true);
        self
    }

    /// Returns the [TransactionFilter].
    pub fn build(self) -> TransactionFilter {
        self.inner
    }
}

impl EventFilterBuilder {
    /// Returns a new [EventFilterBuilder] that includes all contracts and events.
    ///
    /// This is the default when no contract address and keys are specified.
    pub fn all_contracts_and_events() -> Self {
        let inner = EventFilter::default();
        Self { inner }
    }

    /// Returns a new [EventFilterBuilder] that includes only events emitted by the given contract.
    pub fn single_contract(address: FieldElement) -> Self {
        let inner = EventFilter {
            address: Some(address),
            ..Default::default()
        };
        Self { inner }
    }

    /// Returns events emitted by the specified contract address.
    pub fn with_address(mut self, address: FieldElement) -> Self {
        self.inner.address = Some(address);
        self
    }

    /// Returns events with the specified keys.
    ///
    /// On Starknet, the first key is the hash of the event's name.
    /// All other keys are the "indexed" fields of the event.
    ///
    /// The iterator should return [Option::None] if you need _all_ values for that key.
    ///
    /// The `strict` parameter determines how the DNA server matches keys of different lengths.
    ///
    ///  - if `strict` is `false`, this filter is essentially a prefix match.
    ///  - if `strict` is `true`, this filter is an exact match.
    ///
    /// ## Example
    ///
    /// Imagine a `Transfer` event with the following indexed fields: from, to.
    ///
    ///  - `[Some(TRANSFER_HASH), Some(FROM_ADDR), Some(TO_ADDR)]` - filters all transfer events from and to the specified addresses.
    ///  - `[Some(TRANSFER_HASH), Some(FROM_ADDR), None]` - filters all transfer events from the specified address.
    ///  - `[Some(TRANSFER_HASH), None, Some(TO_ADDR)]` - filters all transfer events to the specified address.
    ///  - `[Some(TRANSFER_HASH), None, None]` - filters all transfer events.
    ///
    /// If you set `strict` to `false`, the last example can be rewritten as `[Some(TRANSFER_HASH)]`.
    pub fn with_keys(
        mut self,
        strict: bool,
        keys: impl IntoIterator<Item = Option<FieldElement>>,
    ) -> Self {
        self.inner.keys = keys.into_iter().map(|key| Key { value: key }).collect();
        self.inner.strict = Some(strict);
        self
    }

    /// Assign a new id to the filter.
    ///
    /// Data produced by this filter will be associated with the given id.
    pub fn with_id(mut self, id: u32) -> Self {
        self.inner.id = id;
        self
    }

    /// Requests only events emitted by transactions with the given status.
    pub fn with_transaction_status(mut self, status: TransactionStatusFilter) -> Self {
        self.inner.set_transaction_status(status);
        self
    }

    /// Requests to include the transaction.
    pub fn with_transaction(mut self) -> Self {
        self.inner.include_transaction = Some(true);
        self
    }

    /// Requests to include the transaction's receipt.
    pub fn with_receipt(mut self) -> Self {
        self.inner.include_receipt = Some(true);
        self
    }

    /// Requests to include the transaction's events.
    pub fn with_siblings(mut self) -> Self {
        self.inner.include_siblings = Some(true);
        self
    }

    /// Requests to include the transaction's messages to L1.
    pub fn with_messages(mut self) -> Self {
        self.inner.include_messages = Some(true);
        self
    }

    /// Requests to include the transaction's trace.
    pub fn with_transaction_trace(mut self) -> Self {
        self.inner.include_transaction_trace = Some(true);
        self
    }

    /// Returns the [EventFilter].
    pub fn build(self) -> EventFilter {
        self.inner
    }
}

impl MessageToL1FilterBuilder {
    /// Returns a [MessageToL1FilterBuilder] that includes all messages.
    pub fn all_messages() -> Self {
        let inner = MessageToL1Filter::default();
        Self { inner }
    }

    /// Requests only messages sent from the given address.
    pub fn with_from_address(mut self, from_address: FieldElement) -> Self {
        self.inner.from_address = Some(from_address);
        self
    }

    /// Requests only messages sent to the given address.
    pub fn with_to_address(mut self, to_address: FieldElement) -> Self {
        self.inner.to_address = Some(to_address);
        self
    }

    /// Assign a new id to the filter.
    ///
    /// Data produced by this filter will be associated with the given id.
    pub fn with_id(mut self, id: u32) -> Self {
        self.inner.id = id;
        self
    }

    /// Requests only events emitted by transactions with the given status.
    pub fn with_transaction_status(mut self, status: TransactionStatusFilter) -> Self {
        self.inner.set_transaction_status(status);
        self
    }

    /// Requests to include the transaction's receipt.
    pub fn with_receipt(mut self) -> Self {
        self.inner.include_receipt = Some(true);
        self
    }

    /// Requests to include the messages emitted by the same transaction.
    pub fn with_siblings(mut self) -> Self {
        self.inner.include_siblings = Some(true);
        self
    }

    /// Requests to include the transaction's events.
    pub fn with_events(mut self) -> Self {
        self.inner.include_events = Some(true);
        self
    }

    /// Requests to include the transaction's trace.
    pub fn with_transaction_trace(mut self) -> Self {
        self.inner.include_transaction_trace = Some(true);
        self
    }

    /// Returns the [MessageToL1Filter].
    pub fn build(self) -> MessageToL1Filter {
        self.inner
    }
}

impl StorageDiffFilterBuilder {
    /// Returns a [StorageDiffFilterBuilder] that includes all contracts.
    pub fn all_contracts() -> Self {
        let inner = StorageDiffFilter::default();
        Self { inner }
    }

    /// Returns a new [StorageDiffFilterBuilder] that includes only changes to the specified contract.
    pub fn single_contract(address: FieldElement) -> Self {
        let inner = StorageDiffFilter {
            contract_address: Some(address),
            ..Default::default()
        };
        Self { inner }
    }

    /// Returns storage diffs for the specified contract address.
    pub fn with_address(mut self, address: FieldElement) -> Self {
        self.inner.contract_address = Some(address);
        self
    }

    /// Assign a new id to the filter.
    ///
    /// Data produced by this filter will be associated with the given id.
    pub fn with_id(mut self, id: u32) -> Self {
        self.inner.id = id;
        self
    }

    /// Returns the [StorageDiffFilter].
    pub fn build(self) -> StorageDiffFilter {
        self.inner
    }
}

impl ContractChangeFilterBuilder {
    /// Returns a [ContractChangeFilterBuilder] that includes all changes.
    pub fn all_changes() -> Self {
        let inner = ContractChangeFilter::default();
        Self { inner }
    }

    /// Returns a [ContractChangeFilterBuilder] that includes only declared class changes.
    pub fn declared_class() -> Self {
        use contract_change_filter::Change;
        let inner = ContractChangeFilter {
            change: Some(Change::DeclaredClass(DeclaredClassFilter::default())),
            ..Default::default()
        };
        Self { inner }
    }

    /// Returns a [ContractChangeFilterBuilder] that includes only replaced class changes.
    pub fn replaced_class() -> Self {
        use contract_change_filter::Change;
        let inner = ContractChangeFilter {
            change: Some(Change::ReplacedClass(ReplacedClassFilter::default())),
            ..Default::default()
        };
        Self { inner }
    }

    /// Returns a [ContractChangeFilterBuilder] that includes only deployed contract changes.
    pub fn deployed_contract() -> Self {
        use contract_change_filter::Change;
        let inner = ContractChangeFilter {
            change: Some(Change::DeployedContract(DeployedContractFilter::default())),
            ..Default::default()
        };
        Self { inner }
    }

    /// Assign a new id to the filter.
    ///
    /// Data produced by this filter will be associated with the given id.
    pub fn with_id(mut self, id: u32) -> Self {
        self.inner.id = id;
        self
    }

    /// Returns the [ContractChangeFilter].
    pub fn build(self) -> ContractChangeFilter {
        self.inner
    }
}

impl NonceUpdateFilterBuilder {
    /// Returns a [NonceUpdateFilterBuilder] that includes all contracts.
    pub fn all_contracts() -> Self {
        let inner = NonceUpdateFilter::default();
        Self { inner }
    }

    /// Returns a new [NonceUpdateFilterBuilder] that includes only changes to the specified contract.
    pub fn single_contract(address: FieldElement) -> Self {
        let inner = NonceUpdateFilter {
            contract_address: Some(address),
            ..Default::default()
        };
        Self { inner }
    }

    /// Returns nonce updates for the specified contract address.
    pub fn with_address(mut self, address: FieldElement) -> Self {
        self.inner.contract_address = Some(address);
        self
    }

    /// Assign a new id to the filter.
    ///
    /// Data produced by this filter will be associated with the given id.
    pub fn with_id(mut self, id: u32) -> Self {
        self.inner.id = id;
        self
    }

    /// Returns the [NonceUpdateFilter].
    pub fn build(self) -> NonceUpdateFilter {
        self.inner
    }
}