blokli-client 0.33.1

Client connector to Blokli
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
//! Cynic types for Curvy indexing, synchronization, contract reads, and subscriptions.
//!
//! Ownership is deliberately not decided by Blokli. Consumers pass pending-note
//! metadata to the Curvy SDK scanner and retain only note IDs owned by the node.

use super::{Hex32, InvalidAddressError, QueryFailedError, Uint64, Uint256, schema};
use crate::errors::{BlokliClientError, ErrorKind};

/// Exclusive chain-position cursor used by paginated Curvy event queries.
#[derive(cynic::InputObject, Clone, Debug, Eq, PartialEq)]
pub struct CurvyEventCursor {
    pub block: Uint64,
    pub transaction_index: Uint64,
    pub log_index: Uint64,
    pub event_item_index: Uint64,
    pub block_hash: Option<Hex32>,
}

impl CurvyEventCursor {
    /// Creates an unanchored exclusive cursor from chain-position components.
    pub fn new(block: u64, transaction_index: u64, log_index: u64, event_item_index: u64) -> Self {
        Self {
            block: Uint64(block.to_string()),
            transaction_index: Uint64(transaction_index.to_string()),
            log_index: Uint64(log_index.to_string()),
            event_item_index: Uint64(event_item_index.to_string()),
            block_hash: None,
        }
    }
}

/// Canonical chain position and transaction identity for a Curvy event item.
#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyEventPosition {
    pub transaction_hash: Hex32,
    pub block_hash: Hex32,
    pub block: Uint64,
    pub transaction_index: Uint64,
    pub log_index: Uint64,
    pub event_item_index: Uint64,
}

impl From<&CurvyEventPosition> for CurvyEventCursor {
    fn from(position: &CurvyEventPosition) -> Self {
        Self {
            block: position.block.clone(),
            transaction_index: position.transaction_index.clone(),
            log_index: position.log_index.clone(),
            event_item_index: position.event_item_index.clone(),
            block_hash: Some(position.block_hash.clone()),
        }
    }
}

/// One pending-note announcement. Feed this metadata to the Curvy SDK ownership scanner.
#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyPendingNote {
    pub note_id: Hex32,
    pub ephemeral_key: Vec<Uint256>,
    pub view_tag: i32,
    pub token_id: Uint256,
    pub amount: Uint256,
    pub is_plaintext: bool,
    pub position: CurvyEventPosition,
}

/// One committed note.
#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyCommittedNote {
    pub batch_index: Hex32,
    pub note_id: Hex32,
    pub leaf_index: Uint64,
    pub position: CurvyEventPosition,
}

/// One committed nullifier.
#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyCommittedNullifier {
    pub batch_index: Hex32,
    pub nullifier: Hex32,
    pub nullifier_index: Uint64,
    pub position: CurvyEventPosition,
}

#[derive(cynic::QueryVariables, Clone, Debug, Default)]
pub struct CurvyEventPageVariables {
    pub from_block: Option<Uint64>,
    pub after: Option<CurvyEventCursor>,
    pub first: Option<i32>,
}

#[derive(cynic::QueryVariables, Clone, Debug, Default)]
pub struct CurvyEventSubscriptionVariables {
    pub from_block: Option<Uint64>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot", variables = "CurvyEventPageVariables")]
pub struct QueryCurvyPendingNotes {
    #[arguments(fromBlock: $from_block, after: $after, first: $first)]
    pub curvy_pending_notes: CurvyPendingNotesResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot", variables = "CurvyEventPageVariables")]
pub struct QueryCurvyCommittedNotes {
    #[arguments(fromBlock: $from_block, after: $after, first: $first)]
    pub curvy_committed_notes: CurvyCommittedNotesResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot", variables = "CurvyEventPageVariables")]
pub struct QueryCurvyCommittedNullifiers {
    #[arguments(fromBlock: $from_block, after: $after, first: $first)]
    pub curvy_committed_nullifiers: CurvyCommittedNullifiersResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "SubscriptionRoot", variables = "CurvyEventSubscriptionVariables")]
pub struct SubscribeCurvyPendingNote {
    #[arguments(fromBlock: $from_block)]
    pub curvy_pending_note: CurvyPendingNote,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "SubscriptionRoot", variables = "CurvyEventSubscriptionVariables")]
pub struct SubscribeCurvyCommittedNote {
    #[arguments(fromBlock: $from_block)]
    pub curvy_committed_note: CurvyCommittedNote,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "SubscriptionRoot", variables = "CurvyEventSubscriptionVariables")]
pub struct SubscribeCurvyCommittedNullifier {
    #[arguments(fromBlock: $from_block)]
    pub curvy_committed_nullifier: CurvyCommittedNullifier,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyPendingNotes {
    pub notes: Vec<CurvyPendingNote>,
}

#[derive(cynic::InlineFragments, Debug)]
pub enum CurvyPendingNotesResult {
    CurvyPendingNotes(CurvyPendingNotes),
    QueryFailedError(QueryFailedError),
    #[cynic(fallback)]
    Unknown,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyCommittedNotes {
    pub notes: Vec<CurvyCommittedNote>,
}

#[derive(cynic::InlineFragments, Debug)]
pub enum CurvyCommittedNotesResult {
    CurvyCommittedNotes(CurvyCommittedNotes),
    QueryFailedError(QueryFailedError),
    #[cynic(fallback)]
    Unknown,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyCommittedNullifiers {
    pub nullifiers: Vec<CurvyCommittedNullifier>,
}

#[derive(cynic::InlineFragments, Debug)]
pub enum CurvyCommittedNullifiersResult {
    CurvyCommittedNullifiers(CurvyCommittedNullifiers),
    QueryFailedError(QueryFailedError),
    #[cynic(fallback)]
    Unknown,
}

/// Finalized Curvy synchronization checkpoint.
#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvySyncCheckpoint {
    pub block_number: Uint64,
    pub block_hash: Hex32,
    pub aggregator_address: String,
    pub tree_version: i32,
    pub tree_depth: i32,
    pub shard_height: i32,
    pub shard_size: Uint64,
    pub note_count: Uint64,
    pub nullifier_count: Uint64,
    pub shard_count: Uint64,
    pub notes_root: Hex32,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvySyncNote {
    pub leaf_index: Uint64,
    pub note_id: Hex32,
    pub batch_index: Hex32,
    pub announcement: Option<CurvyPendingNote>,
    pub commit_position: CurvyEventPosition,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvySyncNotePage {
    pub checkpoint: Hex32,
    pub notes: Vec<CurvySyncNote>,
    pub next_index: Uint64,
    pub total: Uint64,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvySyncNullifierPage {
    pub checkpoint: Hex32,
    pub nullifiers: Vec<CurvyCommittedNullifier>,
    pub next_index: Uint64,
    pub total: Uint64,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyShardRoot {
    pub shard_index: Uint64,
    pub root: Hex32,
    pub completion_position: CurvyEventPosition,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyShardRootPage {
    pub checkpoint: Hex32,
    pub shard_roots: Vec<CurvyShardRoot>,
    pub next_index: Uint64,
    pub total: Uint64,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyAggregatorState {
    pub notes_tree_root: Hex32,
    pub notes_batch_index: Uint256,
    pub nullifiers_batch_index: Uint256,
    pub note_index: Uint256,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyNoteStatus {
    pub status: i32,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyBooleanValue {
    pub value: bool,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyVaultFees {
    pub deposit_fee: Uint256,
    pub withdrawal_fee: Uint256,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyAggregatorFees {
    pub protocol_fee_per_thousand: Uint256,
    pub commitment_fee_root: Hex32,
    pub fee_note_public_key: Vec<Uint256>,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyGasFees {
    pub token_id: Uint256,
    pub portal_deployment: Uint256,
    pub pending_note_commitment: Uint256,
    pub withdrawal: Uint256,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyVaultToken {
    pub token_address: String,
    pub gas_fees: CurvyGasFees,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyVaultTokenCount {
    pub count: Uint256,
}

#[derive(cynic::QueryFragment, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CurvyAddress {
    pub address: String,
}

#[derive(cynic::QueryVariables, Clone, Debug, Default)]
pub struct CurvyCheckpointVariables {
    pub block_hash: Option<Hex32>,
}

#[derive(cynic::QueryVariables, Clone, Debug)]
pub struct CurvySyncPageVariables {
    pub checkpoint: Hex32,
    pub from_index: Option<Uint64>,
    pub first: Option<i32>,
}

#[derive(cynic::QueryVariables, Clone, Debug)]
pub struct CurvyNoteIdVariables {
    pub note_id: Hex32,
}

#[derive(cynic::QueryVariables, Clone, Debug)]
pub struct CurvyRootVariables {
    pub root: Hex32,
}

#[derive(cynic::QueryVariables, Clone, Debug)]
pub struct CurvyNullifierVariables {
    pub nullifier: Hex32,
}

#[derive(cynic::QueryVariables, Clone, Debug)]
pub struct CurvyVaultTokenVariables {
    pub token_id: Uint256,
}

#[derive(cynic::QueryVariables, Clone, Debug)]
pub struct CurvyEntryPortalVariables {
    pub owner_hash: Uint256,
    pub recovery: String,
}

#[derive(cynic::QueryVariables, Clone, Debug)]
pub struct CurvyExitPortalVariables {
    pub exit_address: String,
    pub exit_chain_id: Uint256,
    pub recovery: String,
}

#[derive(cynic::QueryVariables, Clone, Debug)]
pub struct CurvyPortalVariables {
    pub portal_address: String,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot", variables = "CurvyCheckpointVariables")]
pub struct QueryCurvySyncCheckpoint {
    #[arguments(blockHash: $block_hash)]
    pub curvy_sync_checkpoint: CurvySyncCheckpointResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot", variables = "CurvySyncPageVariables")]
pub struct QueryCurvySyncNotes {
    #[arguments(checkpoint: $checkpoint, fromIndex: $from_index, first: $first)]
    pub curvy_sync_notes: CurvySyncNotesResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot", variables = "CurvySyncPageVariables")]
pub struct QueryCurvySyncNullifiers {
    #[arguments(checkpoint: $checkpoint, fromIndex: $from_index, first: $first)]
    pub curvy_sync_nullifiers: CurvySyncNullifiersResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot", variables = "CurvySyncPageVariables")]
pub struct QueryCurvyShardRoots {
    #[arguments(checkpoint: $checkpoint, fromIndex: $from_index, first: $first)]
    pub curvy_shard_roots: CurvyShardRootsResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot")]
pub struct QueryCurvyAggregatorState {
    pub curvy_aggregator_state: CurvyAggregatorStateResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot", variables = "CurvyNoteIdVariables")]
pub struct QueryCurvyNoteStatus {
    #[arguments(noteId: $note_id)]
    pub curvy_note_status: CurvyNoteStatusResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot", variables = "CurvyRootVariables")]
pub struct QueryCurvyValidNotesRoot {
    #[arguments(root: $root)]
    pub curvy_valid_notes_root: CurvyValidNotesRootResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot", variables = "CurvyNullifierVariables")]
pub struct QueryCurvyNullifierSpent {
    #[arguments(nullifier: $nullifier)]
    pub curvy_nullifier_spent: CurvyNullifierSpentResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot")]
pub struct QueryCurvyVaultFees {
    pub curvy_vault_fees: CurvyVaultFeesResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot")]
pub struct QueryCurvyAggregatorFees {
    pub curvy_aggregator_fees: CurvyAggregatorFeesResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot")]
pub struct QueryCurvyVaultTokenCount {
    pub curvy_vault_token_count: CurvyVaultTokenCountResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot", variables = "CurvyVaultTokenVariables")]
pub struct QueryCurvyVaultToken {
    #[arguments(tokenId: $token_id)]
    pub curvy_vault_token: CurvyVaultTokenResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot", variables = "CurvyEntryPortalVariables")]
pub struct QueryCurvyEntryPortalAddress {
    #[arguments(ownerHash: $owner_hash, recovery: $recovery)]
    pub curvy_entry_portal_address: CurvyEntryPortalAddressResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot", variables = "CurvyExitPortalVariables")]
pub struct QueryCurvyExitPortalAddress {
    #[arguments(exitAddress: $exit_address, exitChainId: $exit_chain_id, recovery: $recovery)]
    pub curvy_exit_portal_address: CurvyExitPortalAddressResult,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "QueryRoot", variables = "CurvyPortalVariables")]
pub struct QueryCurvyPortalRegistered {
    #[arguments(portalAddress: $portal_address)]
    pub curvy_portal_registered: CurvyPortalRegisteredResult,
}

macro_rules! simple_union {
    ($name:ident, $success:ident, $value:ty) => {
        #[derive(cynic::InlineFragments, Debug)]
        pub enum $name {
            $success($value),
            QueryFailedError(QueryFailedError),
            #[cynic(fallback)]
            Unknown,
        }

        impl From<$name> for Result<$value, BlokliClientError> {
            fn from(value: $name) -> Self {
                match value {
                    $name::$success(value) => Ok(value),
                    $name::QueryFailedError(error) => Err(error.into()),
                    $name::Unknown => Err(ErrorKind::NoData.into()),
                }
            }
        }
    };
}

simple_union!(CurvySyncCheckpointResult, CurvySyncCheckpoint, CurvySyncCheckpoint);
simple_union!(CurvySyncNotesResult, CurvySyncNotePage, CurvySyncNotePage);
simple_union!(
    CurvySyncNullifiersResult,
    CurvySyncNullifierPage,
    CurvySyncNullifierPage
);
simple_union!(CurvyShardRootsResult, CurvyShardRootPage, CurvyShardRootPage);
simple_union!(CurvyAggregatorStateResult, CurvyAggregatorState, CurvyAggregatorState);
simple_union!(CurvyNoteStatusResult, CurvyNoteStatus, CurvyNoteStatus);
simple_union!(CurvyValidNotesRootResult, CurvyBooleanValue, CurvyBooleanValue);
simple_union!(CurvyNullifierSpentResult, CurvyBooleanValue, CurvyBooleanValue);
simple_union!(CurvyVaultFeesResult, CurvyVaultFees, CurvyVaultFees);
simple_union!(CurvyAggregatorFeesResult, CurvyAggregatorFees, CurvyAggregatorFees);
simple_union!(CurvyVaultTokenCountResult, CurvyVaultTokenCount, CurvyVaultTokenCount);
simple_union!(CurvyVaultTokenResult, CurvyVaultToken, CurvyVaultToken);

macro_rules! address_union {
    ($name:ident) => {
        #[derive(cynic::InlineFragments, Debug)]
        pub enum $name {
            CurvyAddress(CurvyAddress),
            InvalidAddressError(InvalidAddressError),
            QueryFailedError(QueryFailedError),
            #[cynic(fallback)]
            Unknown,
        }

        impl From<$name> for Result<CurvyAddress, BlokliClientError> {
            fn from(value: $name) -> Self {
                match value {
                    $name::CurvyAddress(value) => Ok(value),
                    $name::InvalidAddressError(error) => Err(error.into()),
                    $name::QueryFailedError(error) => Err(error.into()),
                    $name::Unknown => Err(ErrorKind::NoData.into()),
                }
            }
        }
    };
}

address_union!(CurvyEntryPortalAddressResult);
address_union!(CurvyExitPortalAddressResult);

#[derive(cynic::InlineFragments, Debug)]
pub enum CurvyPortalRegisteredResult {
    CurvyBooleanValue(CurvyBooleanValue),
    InvalidAddressError(InvalidAddressError),
    QueryFailedError(QueryFailedError),
    #[cynic(fallback)]
    Unknown,
}

impl From<CurvyPortalRegisteredResult> for Result<CurvyBooleanValue, BlokliClientError> {
    fn from(value: CurvyPortalRegisteredResult) -> Self {
        match value {
            CurvyPortalRegisteredResult::CurvyBooleanValue(value) => Ok(value),
            CurvyPortalRegisteredResult::InvalidAddressError(error) => Err(error.into()),
            CurvyPortalRegisteredResult::QueryFailedError(error) => Err(error.into()),
            CurvyPortalRegisteredResult::Unknown => Err(ErrorKind::NoData.into()),
        }
    }
}

impl From<CurvyPendingNotesResult> for Result<CurvyPendingNotes, BlokliClientError> {
    fn from(value: CurvyPendingNotesResult) -> Self {
        match value {
            CurvyPendingNotesResult::CurvyPendingNotes(notes) => Ok(notes),
            CurvyPendingNotesResult::QueryFailedError(error) => Err(error.into()),
            CurvyPendingNotesResult::Unknown => Err(ErrorKind::NoData.into()),
        }
    }
}

impl From<CurvyCommittedNotesResult> for Result<CurvyCommittedNotes, BlokliClientError> {
    fn from(value: CurvyCommittedNotesResult) -> Self {
        match value {
            CurvyCommittedNotesResult::CurvyCommittedNotes(notes) => Ok(notes),
            CurvyCommittedNotesResult::QueryFailedError(error) => Err(error.into()),
            CurvyCommittedNotesResult::Unknown => Err(ErrorKind::NoData.into()),
        }
    }
}

impl From<CurvyCommittedNullifiersResult> for Result<CurvyCommittedNullifiers, BlokliClientError> {
    fn from(value: CurvyCommittedNullifiersResult) -> Self {
        match value {
            CurvyCommittedNullifiersResult::CurvyCommittedNullifiers(nullifiers) => Ok(nullifiers),
            CurvyCommittedNullifiersResult::QueryFailedError(error) => Err(error.into()),
            CurvyCommittedNullifiersResult::Unknown => Err(ErrorKind::NoData.into()),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::fmt::Debug;

    use super::{
        CurvyAddress, CurvyBooleanValue, CurvyCommittedNotes, CurvyCommittedNotesResult, CurvyCommittedNullifiers,
        CurvyCommittedNullifiersResult, CurvyEntryPortalAddressResult, CurvyEventCursor, CurvyEventPosition,
        CurvyNoteStatus, CurvyNoteStatusResult, CurvyPendingNotes, CurvyPendingNotesResult,
        CurvyPortalRegisteredResult, Hex32, InvalidAddressError, QueryFailedError, Uint64,
    };
    use crate::errors::{BlokliClientError, ErrorKind};

    fn query_failed_error() -> QueryFailedError {
        QueryFailedError {
            __typename: "QueryFailedError".to_owned(),
            message: "query failed".to_owned(),
            code: "QUERY_FAILED".to_owned(),
        }
    }

    fn invalid_address_error() -> InvalidAddressError {
        InvalidAddressError {
            __typename: "InvalidAddressError".to_owned(),
            message: "invalid address".to_owned(),
            code: "INVALID_ADDRESS".to_owned(),
        }
    }

    fn assert_no_data<T: Debug>(result: Result<T, BlokliClientError>) {
        assert!(matches!(
            result.expect_err("conversion should fail").kind(),
            ErrorKind::NoData
        ));
    }

    #[test]
    fn event_position_converts_to_anchored_cursor() {
        let position = CurvyEventPosition {
            transaction_hash: Hex32("0xtx".to_owned()),
            block_hash: Hex32("0xblock".to_owned()),
            block: Uint64("10".to_owned()),
            transaction_index: Uint64("2".to_owned()),
            log_index: Uint64("3".to_owned()),
            event_item_index: Uint64("4".to_owned()),
        };

        let cursor = CurvyEventCursor::from(&position);

        assert_eq!(
            cursor,
            CurvyEventCursor {
                block: Uint64("10".to_owned()),
                transaction_index: Uint64("2".to_owned()),
                log_index: Uint64("3".to_owned()),
                event_item_index: Uint64("4".to_owned()),
                block_hash: Some(Hex32("0xblock".to_owned())),
            }
        );
        assert_eq!(CurvyEventCursor::new(10, 2, 3, 4).block_hash, None);
    }

    #[test]
    fn simple_union_converts_success_and_errors() {
        let status: Result<CurvyNoteStatus, BlokliClientError> =
            CurvyNoteStatusResult::CurvyNoteStatus(CurvyNoteStatus { status: 2 }).into();
        assert_eq!(status.expect("status should convert").status, 2);

        let error: Result<CurvyNoteStatus, BlokliClientError> =
            CurvyNoteStatusResult::QueryFailedError(query_failed_error()).into();
        assert!(matches!(
            error.expect_err("query failure should convert").kind(),
            ErrorKind::BlokliError {
                kind: "query failed",
                ..
            }
        ));

        assert_no_data(Result::<CurvyNoteStatus, BlokliClientError>::from(
            CurvyNoteStatusResult::Unknown,
        ));
    }

    #[test]
    fn address_union_converts_every_variant() {
        let address: Result<CurvyAddress, BlokliClientError> =
            CurvyEntryPortalAddressResult::CurvyAddress(CurvyAddress {
                address: "0x1234".to_owned(),
            })
            .into();
        assert_eq!(address.expect("address should convert").address, "0x1234");

        let invalid: Result<CurvyAddress, BlokliClientError> =
            CurvyEntryPortalAddressResult::InvalidAddressError(invalid_address_error()).into();
        assert!(matches!(
            invalid.expect_err("invalid address should convert").kind(),
            ErrorKind::BlokliError {
                kind: "invalid address",
                ..
            }
        ));

        let failed: Result<CurvyAddress, BlokliClientError> =
            CurvyEntryPortalAddressResult::QueryFailedError(query_failed_error()).into();
        assert!(matches!(
            failed.expect_err("query failure should convert").kind(),
            ErrorKind::BlokliError {
                kind: "query failed",
                ..
            }
        ));

        assert_no_data(Result::<CurvyAddress, BlokliClientError>::from(
            CurvyEntryPortalAddressResult::Unknown,
        ));
    }

    #[test]
    fn portal_registered_union_converts_every_variant() {
        let registered: Result<CurvyBooleanValue, BlokliClientError> =
            CurvyPortalRegisteredResult::CurvyBooleanValue(CurvyBooleanValue { value: true }).into();
        assert!(registered.expect("boolean should convert").value);

        let invalid: Result<CurvyBooleanValue, BlokliClientError> =
            CurvyPortalRegisteredResult::InvalidAddressError(invalid_address_error()).into();
        assert!(matches!(
            invalid.expect_err("invalid address should convert").kind(),
            ErrorKind::BlokliError {
                kind: "invalid address",
                ..
            }
        ));

        let failed: Result<CurvyBooleanValue, BlokliClientError> =
            CurvyPortalRegisteredResult::QueryFailedError(query_failed_error()).into();
        assert!(matches!(
            failed.expect_err("query failure should convert").kind(),
            ErrorKind::BlokliError {
                kind: "query failed",
                ..
            }
        ));

        assert_no_data(Result::<CurvyBooleanValue, BlokliClientError>::from(
            CurvyPortalRegisteredResult::Unknown,
        ));
    }

    #[test]
    fn event_page_unions_convert_success_and_errors() {
        let pending: Result<CurvyPendingNotes, BlokliClientError> =
            CurvyPendingNotesResult::CurvyPendingNotes(CurvyPendingNotes { notes: Vec::new() }).into();
        assert!(pending.expect("pending notes should convert").notes.is_empty());
        let committed: Result<CurvyCommittedNotes, BlokliClientError> =
            CurvyCommittedNotesResult::CurvyCommittedNotes(CurvyCommittedNotes { notes: Vec::new() }).into();
        assert!(committed.expect("committed notes should convert").notes.is_empty());
        let nullifiers: Result<CurvyCommittedNullifiers, BlokliClientError> =
            CurvyCommittedNullifiersResult::CurvyCommittedNullifiers(CurvyCommittedNullifiers {
                nullifiers: Vec::new(),
            })
            .into();
        assert!(nullifiers.expect("nullifiers should convert").nullifiers.is_empty());

        let pending_error: Result<CurvyPendingNotes, BlokliClientError> =
            CurvyPendingNotesResult::QueryFailedError(query_failed_error()).into();
        assert!(matches!(
            pending_error.expect_err("query failure should convert").kind(),
            ErrorKind::BlokliError { .. }
        ));
        let committed_error: Result<CurvyCommittedNotes, BlokliClientError> =
            CurvyCommittedNotesResult::QueryFailedError(query_failed_error()).into();
        assert!(matches!(
            committed_error.expect_err("query failure should convert").kind(),
            ErrorKind::BlokliError { .. }
        ));
        let nullifier_error: Result<CurvyCommittedNullifiers, BlokliClientError> =
            CurvyCommittedNullifiersResult::QueryFailedError(query_failed_error()).into();
        assert!(matches!(
            nullifier_error.expect_err("query failure should convert").kind(),
            ErrorKind::BlokliError { .. }
        ));

        assert_no_data(Result::<CurvyPendingNotes, BlokliClientError>::from(
            CurvyPendingNotesResult::Unknown,
        ));
        assert_no_data(Result::<CurvyCommittedNotes, BlokliClientError>::from(
            CurvyCommittedNotesResult::Unknown,
        ));
        assert_no_data(Result::<CurvyCommittedNullifiers, BlokliClientError>::from(
            CurvyCommittedNullifiersResult::Unknown,
        ));
    }
}