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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
use crate::{
    fuel_core_graphql_api::{
        api_service::ConsensusProvider,
        database::ReadView,
    },
    graphql_api::Config,
    query::{
        BlockQueryData,
        ChainQueryData,
    },
    schema::{
        block::Block,
        scalars::{
            Address,
            AssetId,
            U16,
            U32,
            U64,
        },
    },
};
use async_graphql::{
    Context,
    Enum,
    Object,
    Union,
};
use fuel_core_types::{
    fuel_tx,
    fuel_tx::GasCostsValues,
};
use std::{
    ops::Deref,
    sync::Arc,
};

pub struct ChainInfo;
pub struct ConsensusParameters(Arc<fuel_tx::ConsensusParameters>);
pub struct TxParameters(fuel_tx::TxParameters);
pub struct PredicateParameters(fuel_tx::PredicateParameters);
pub struct ScriptParameters(fuel_tx::ScriptParameters);
pub struct ContractParameters(fuel_tx::ContractParameters);
pub struct FeeParameters(fuel_tx::FeeParameters);

pub struct GasCosts(fuel_tx::GasCosts);

#[derive(Clone, Copy, Debug, Enum, Eq, PartialEq)]
pub enum GasCostsVersion {
    V1,
}

#[derive(Clone, Copy, Debug, Enum, Eq, PartialEq)]
pub enum FeeParametersVersion {
    V1,
}

#[derive(Clone, Copy, Debug, Enum, Eq, PartialEq)]
pub enum ContractParametersVersion {
    V1,
}

#[derive(Clone, Copy, Debug, Enum, Eq, PartialEq)]
pub enum ScriptParametersVersion {
    V1,
}

#[derive(Clone, Copy, Debug, Enum, Eq, PartialEq)]
pub enum PredicateParametersVersion {
    V1,
}

#[derive(Clone, Copy, Debug, Enum, Eq, PartialEq)]
pub enum TxParametersVersion {
    V1,
}

#[derive(Clone, Copy, Debug, Enum, Eq, PartialEq)]
pub enum ConsensusParametersVersion {
    V1,
}

#[derive(Union)]
pub enum DependentCost {
    LightOperation(LightOperation),
    HeavyOperation(HeavyOperation),
}

pub struct LightOperation {
    base: u64,
    units_per_gas: u64,
}

pub struct HeavyOperation {
    base: u64,
    gas_per_unit: u64,
}

impl From<fuel_tx::DependentCost> for DependentCost {
    fn from(value: fuel_tx::DependentCost) -> Self {
        match value {
            fuel_tx::DependentCost::LightOperation {
                base,
                units_per_gas,
            } => DependentCost::LightOperation(LightOperation {
                base,
                units_per_gas,
            }),
            fuel_tx::DependentCost::HeavyOperation { base, gas_per_unit } => {
                DependentCost::HeavyOperation(HeavyOperation { base, gas_per_unit })
            }
        }
    }
}

#[Object]
impl ConsensusParameters {
    async fn version(&self) -> ConsensusParametersVersion {
        match self.0.as_ref() {
            fuel_tx::ConsensusParameters::V1(_) => ConsensusParametersVersion::V1,
        }
    }

    async fn tx_params(&self, ctx: &Context<'_>) -> async_graphql::Result<TxParameters> {
        let params = ctx
            .data_unchecked::<ConsensusProvider>()
            .latest_consensus_params();

        Ok(TxParameters(params.tx_params().to_owned()))
    }

    async fn predicate_params(
        &self,
        ctx: &Context<'_>,
    ) -> async_graphql::Result<PredicateParameters> {
        let params = ctx
            .data_unchecked::<ConsensusProvider>()
            .latest_consensus_params();

        Ok(PredicateParameters(params.predicate_params().to_owned()))
    }

    async fn script_params(
        &self,
        ctx: &Context<'_>,
    ) -> async_graphql::Result<ScriptParameters> {
        let params = ctx
            .data_unchecked::<ConsensusProvider>()
            .latest_consensus_params();

        Ok(ScriptParameters(params.script_params().to_owned()))
    }

    async fn contract_params(
        &self,
        ctx: &Context<'_>,
    ) -> async_graphql::Result<ContractParameters> {
        let params = ctx
            .data_unchecked::<ConsensusProvider>()
            .latest_consensus_params();

        Ok(ContractParameters(params.contract_params().to_owned()))
    }

    async fn fee_params(
        &self,
        ctx: &Context<'_>,
    ) -> async_graphql::Result<FeeParameters> {
        let params = ctx
            .data_unchecked::<ConsensusProvider>()
            .latest_consensus_params();

        Ok(FeeParameters(params.fee_params().to_owned()))
    }

    async fn base_asset_id(&self, ctx: &Context<'_>) -> async_graphql::Result<AssetId> {
        let params = ctx
            .data_unchecked::<ConsensusProvider>()
            .latest_consensus_params();

        Ok(AssetId(*params.base_asset_id()))
    }

    async fn block_gas_limit(&self, ctx: &Context<'_>) -> async_graphql::Result<U64> {
        let params = ctx
            .data_unchecked::<ConsensusProvider>()
            .latest_consensus_params();

        Ok(params.block_gas_limit().into())
    }

    async fn chain_id(&self) -> U64 {
        (*self.0.chain_id()).into()
    }

    async fn gas_costs(&self, ctx: &Context<'_>) -> async_graphql::Result<GasCosts> {
        let params = ctx
            .data_unchecked::<ConsensusProvider>()
            .latest_consensus_params();

        Ok(GasCosts(params.gas_costs().clone()))
    }

    async fn privileged_address(
        &self,
        ctx: &Context<'_>,
    ) -> async_graphql::Result<Address> {
        let params = ctx
            .data_unchecked::<ConsensusProvider>()
            .latest_consensus_params();

        Ok(Address(*params.privileged_address()))
    }
}

#[Object]
impl TxParameters {
    async fn version(&self) -> TxParametersVersion {
        match self.0 {
            fuel_tx::TxParameters::V1(_) => TxParametersVersion::V1,
        }
    }

    async fn max_inputs(&self) -> U16 {
        self.0.max_inputs().into()
    }

    async fn max_outputs(&self) -> U16 {
        self.0.max_outputs().into()
    }

    async fn max_witnesses(&self) -> U32 {
        self.0.max_witnesses().into()
    }

    async fn max_gas_per_tx(&self) -> U64 {
        self.0.max_gas_per_tx().into()
    }

    async fn max_size(&self) -> U64 {
        self.0.max_size().into()
    }

    async fn max_bytecode_subsections(&self) -> U16 {
        self.0.max_bytecode_subsections().into()
    }
}

#[Object]
impl PredicateParameters {
    async fn version(&self) -> PredicateParametersVersion {
        match self.0 {
            fuel_tx::PredicateParameters::V1(_) => PredicateParametersVersion::V1,
        }
    }

    async fn max_predicate_length(&self) -> U64 {
        self.0.max_predicate_length().into()
    }

    async fn max_predicate_data_length(&self) -> U64 {
        self.0.max_predicate_data_length().into()
    }

    async fn max_gas_per_predicate(&self) -> U64 {
        self.0.max_gas_per_predicate().into()
    }

    async fn max_message_data_length(&self) -> U64 {
        self.0.max_message_data_length().into()
    }
}

#[Object]
impl ScriptParameters {
    async fn version(&self) -> ScriptParametersVersion {
        match self.0 {
            fuel_tx::ScriptParameters::V1(_) => ScriptParametersVersion::V1,
        }
    }

    async fn max_script_length(&self) -> U64 {
        self.0.max_script_length().into()
    }

    async fn max_script_data_length(&self) -> U64 {
        self.0.max_script_data_length().into()
    }
}

#[Object]
impl ContractParameters {
    async fn version(&self) -> ContractParametersVersion {
        match self.0 {
            fuel_tx::ContractParameters::V1(_) => ContractParametersVersion::V1,
        }
    }

    async fn contract_max_size(&self) -> U64 {
        self.0.contract_max_size().into()
    }

    async fn max_storage_slots(&self) -> U64 {
        self.0.max_storage_slots().into()
    }
}

#[Object]
impl FeeParameters {
    async fn version(&self) -> FeeParametersVersion {
        match self.0 {
            fuel_tx::FeeParameters::V1(_) => FeeParametersVersion::V1,
        }
    }

    async fn gas_price_factor(&self) -> U64 {
        self.0.gas_price_factor().into()
    }

    async fn gas_per_byte(&self) -> U64 {
        self.0.gas_per_byte().into()
    }
}

#[Object]
impl GasCosts {
    async fn version(&self) -> GasCostsVersion {
        match self.0.deref() {
            GasCostsValues::V1(_) | GasCostsValues::V2(_) => GasCostsVersion::V1,
        }
    }

    async fn add(&self) -> U64 {
        self.0.add().into()
    }

    async fn addi(&self) -> U64 {
        self.0.addi().into()
    }

    async fn aloc(&self) -> U64 {
        self.0.aloc().base().into()
    }

    async fn and(&self) -> U64 {
        self.0.and().into()
    }

    async fn andi(&self) -> U64 {
        self.0.andi().into()
    }

    async fn bal(&self) -> U64 {
        self.0.bal().into()
    }

    async fn bhei(&self) -> U64 {
        self.0.bhei().into()
    }

    async fn bhsh(&self) -> U64 {
        self.0.bhsh().into()
    }

    async fn burn(&self) -> U64 {
        self.0.burn().into()
    }

    async fn cb(&self) -> U64 {
        self.0.cb().into()
    }

    async fn cfei(&self) -> U64 {
        self.0.cfei().into()
    }

    async fn cfsi(&self) -> U64 {
        self.0.cfsi().into()
    }

    async fn div(&self) -> U64 {
        self.0.div().into()
    }

    async fn divi(&self) -> U64 {
        self.0.divi().into()
    }

    async fn ecr1(&self) -> U64 {
        self.0.ecr1().into()
    }

    async fn eck1(&self) -> U64 {
        self.0.eck1().into()
    }

    async fn ed19(&self) -> U64 {
        self.0.ed19().into()
    }

    async fn eq(&self) -> U64 {
        self.0.eq_().into()
    }

    async fn exp(&self) -> U64 {
        self.0.exp().into()
    }

    async fn expi(&self) -> U64 {
        self.0.expi().into()
    }

    async fn flag(&self) -> U64 {
        self.0.flag().into()
    }

    async fn gm(&self) -> U64 {
        self.0.gm().into()
    }

    async fn gt(&self) -> U64 {
        self.0.gt().into()
    }

    async fn gtf(&self) -> U64 {
        self.0.gtf().into()
    }

    async fn ji(&self) -> U64 {
        self.0.ji().into()
    }

    async fn jmp(&self) -> U64 {
        self.0.jmp().into()
    }

    async fn jne(&self) -> U64 {
        self.0.jne().into()
    }

    async fn jnei(&self) -> U64 {
        self.0.jnei().into()
    }

    async fn jnzi(&self) -> U64 {
        self.0.jnzi().into()
    }

    async fn jmpf(&self) -> U64 {
        self.0.jmpf().into()
    }

    async fn jmpb(&self) -> U64 {
        self.0.jmpb().into()
    }

    async fn jnzf(&self) -> U64 {
        self.0.jnzf().into()
    }

    async fn jnzb(&self) -> U64 {
        self.0.jnzb().into()
    }

    async fn jnef(&self) -> U64 {
        self.0.jnef().into()
    }

    async fn jneb(&self) -> U64 {
        self.0.jneb().into()
    }

    async fn lb(&self) -> U64 {
        self.0.lb().into()
    }

    async fn log(&self) -> U64 {
        self.0.log().into()
    }

    async fn lt(&self) -> U64 {
        self.0.lt().into()
    }

    async fn lw(&self) -> U64 {
        self.0.lw().into()
    }

    async fn mint(&self) -> U64 {
        self.0.mint().into()
    }

    async fn mlog(&self) -> U64 {
        self.0.mlog().into()
    }

    async fn mod_op(&self) -> U64 {
        self.0.mod_op().into()
    }

    async fn modi(&self) -> U64 {
        self.0.modi().into()
    }

    async fn move_op(&self) -> U64 {
        self.0.move_op().into()
    }

    async fn movi(&self) -> U64 {
        self.0.movi().into()
    }

    async fn mroo(&self) -> U64 {
        self.0.mroo().into()
    }

    async fn mul(&self) -> U64 {
        self.0.mul().into()
    }

    async fn muli(&self) -> U64 {
        self.0.muli().into()
    }

    async fn mldv(&self) -> U64 {
        self.0.mldv().into()
    }

    async fn noop(&self) -> U64 {
        self.0.noop().into()
    }

    async fn not(&self) -> U64 {
        self.0.not().into()
    }

    async fn or(&self) -> U64 {
        self.0.or().into()
    }

    async fn ori(&self) -> U64 {
        self.0.ori().into()
    }

    async fn poph(&self) -> U64 {
        self.0.poph().into()
    }

    async fn popl(&self) -> U64 {
        self.0.popl().into()
    }

    async fn pshh(&self) -> U64 {
        self.0.pshh().into()
    }

    async fn pshl(&self) -> U64 {
        self.0.pshl().into()
    }

    async fn ret(&self) -> U64 {
        self.0.ret().into()
    }

    async fn rvrt(&self) -> U64 {
        self.0.rvrt().into()
    }

    async fn sb(&self) -> U64 {
        self.0.sb().into()
    }

    async fn sll(&self) -> U64 {
        self.0.sll().into()
    }

    async fn slli(&self) -> U64 {
        self.0.slli().into()
    }

    async fn srl(&self) -> U64 {
        self.0.srl().into()
    }

    async fn srli(&self) -> U64 {
        self.0.srli().into()
    }

    async fn srw(&self) -> U64 {
        self.0.srw().into()
    }

    async fn sub(&self) -> U64 {
        self.0.sub().into()
    }

    async fn subi(&self) -> U64 {
        self.0.subi().into()
    }

    async fn sw(&self) -> U64 {
        self.0.sw().into()
    }

    async fn sww(&self) -> U64 {
        self.0.sww().into()
    }

    async fn time(&self) -> U64 {
        self.0.time().into()
    }

    async fn tr(&self) -> U64 {
        self.0.tr().into()
    }

    async fn tro(&self) -> U64 {
        self.0.tro().into()
    }

    async fn wdcm(&self) -> U64 {
        self.0.wdcm().into()
    }

    async fn wqcm(&self) -> U64 {
        self.0.wqcm().into()
    }

    async fn wdop(&self) -> U64 {
        self.0.wdop().into()
    }

    async fn wqop(&self) -> U64 {
        self.0.wqop().into()
    }

    async fn wdml(&self) -> U64 {
        self.0.wdml().into()
    }

    async fn wqml(&self) -> U64 {
        self.0.wqml().into()
    }

    async fn wddv(&self) -> U64 {
        self.0.wddv().into()
    }

    async fn wqdv(&self) -> U64 {
        self.0.wqdv().into()
    }

    async fn wdmd(&self) -> U64 {
        self.0.wdmd().into()
    }

    async fn wqmd(&self) -> U64 {
        self.0.wqmd().into()
    }

    async fn wdam(&self) -> U64 {
        self.0.wdam().into()
    }

    async fn wqam(&self) -> U64 {
        self.0.wqam().into()
    }

    async fn wdmm(&self) -> U64 {
        self.0.wdmm().into()
    }

    async fn wqmm(&self) -> U64 {
        self.0.wqmm().into()
    }

    async fn xor(&self) -> U64 {
        self.0.xor().into()
    }

    async fn xori(&self) -> U64 {
        self.0.xori().into()
    }

    async fn aloc_dependent_cost(&self) -> DependentCost {
        self.0.aloc().into()
    }

    async fn call(&self) -> DependentCost {
        self.0.call().into()
    }

    async fn ccp(&self) -> DependentCost {
        self.0.ccp().into()
    }

    async fn croo(&self) -> DependentCost {
        self.0.croo().into()
    }

    async fn csiz(&self) -> DependentCost {
        self.0.csiz().into()
    }

    async fn k256(&self) -> DependentCost {
        self.0.k256().into()
    }

    async fn ldc(&self) -> DependentCost {
        self.0.ldc().into()
    }

    async fn logd(&self) -> DependentCost {
        self.0.logd().into()
    }

    async fn mcl(&self) -> DependentCost {
        self.0.mcl().into()
    }

    async fn mcli(&self) -> DependentCost {
        self.0.mcli().into()
    }

    async fn mcp(&self) -> DependentCost {
        self.0.mcp().into()
    }

    async fn mcpi(&self) -> DependentCost {
        self.0.mcpi().into()
    }

    async fn meq(&self) -> DependentCost {
        self.0.meq().into()
    }

    async fn retd(&self) -> DependentCost {
        self.0.retd().into()
    }

    async fn s256(&self) -> DependentCost {
        self.0.s256().into()
    }

    async fn scwq(&self) -> DependentCost {
        self.0.scwq().into()
    }

    async fn smo(&self) -> DependentCost {
        self.0.smo().into()
    }

    async fn srwq(&self) -> DependentCost {
        self.0.srwq().into()
    }

    async fn swwq(&self) -> DependentCost {
        self.0.swwq().into()
    }

    // Non-opcode prices

    async fn contract_root(&self) -> DependentCost {
        self.0.contract_root().into()
    }

    async fn state_root(&self) -> DependentCost {
        self.0.state_root().into()
    }

    async fn vm_initialization(&self) -> DependentCost {
        self.0.vm_initialization().into()
    }

    async fn new_storage_per_byte(&self) -> U64 {
        self.0.new_storage_per_byte().into()
    }
}

#[Object]
impl LightOperation {
    async fn base(&self) -> U64 {
        self.base.into()
    }

    async fn units_per_gas(&self) -> U64 {
        self.units_per_gas.into()
    }
}

#[Object]
impl HeavyOperation {
    async fn base(&self) -> U64 {
        self.base.into()
    }

    async fn gas_per_unit(&self) -> U64 {
        self.gas_per_unit.into()
    }
}

#[Object]
impl ChainInfo {
    async fn name(&self, ctx: &Context<'_>) -> async_graphql::Result<String> {
        let config: &Config = ctx.data_unchecked();
        Ok(config.chain_name.clone())
    }

    async fn latest_block(&self, ctx: &Context<'_>) -> async_graphql::Result<Block> {
        let query: &ReadView = ctx.data_unchecked();

        let latest_block = query.latest_block()?.into();
        Ok(latest_block)
    }

    async fn da_height(&self, ctx: &Context<'_>) -> U64 {
        let query: &ReadView = ctx.data_unchecked();

        let height = query
            .da_height()
            .expect("The blockchain always should have genesis block");

        height.0.into()
    }

    async fn consensus_parameters(
        &self,
        ctx: &Context<'_>,
    ) -> async_graphql::Result<ConsensusParameters> {
        let params = ctx
            .data_unchecked::<ConsensusProvider>()
            .latest_consensus_params();

        Ok(ConsensusParameters(params))
    }

    async fn gas_costs(&self, ctx: &Context<'_>) -> async_graphql::Result<GasCosts> {
        let params = ctx
            .data_unchecked::<ConsensusProvider>()
            .latest_consensus_params();

        Ok(GasCosts(params.gas_costs().clone()))
    }
}

#[derive(Default)]
pub struct ChainQuery;

#[Object]
impl ChainQuery {
    async fn chain(&self) -> ChainInfo {
        ChainInfo
    }
}