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
use crate::{
    fuel_core_graphql_api::{
        service::Database,
        Config as GraphQLConfig,
    },
    query::{
        BlockQueryData,
        ChainQueryData,
    },
    schema::{
        block::Block,
        scalars::{
            U32,
            U64,
        },
    },
};
use async_graphql::{
    Context,
    Object,
};
use fuel_core_types::{
    fuel_tx,
    fuel_vm,
};

pub struct ChainInfo;

pub struct ConsensusParameters(fuel_tx::ConsensusParameters);

pub struct GasCosts(fuel_vm::GasCosts);

pub struct DependentCost(fuel_vm::DependentCost);

impl From<fuel_vm::DependentCost> for DependentCost {
    fn from(value: fuel_vm::DependentCost) -> Self {
        Self(value)
    }
}

#[Object]
impl ConsensusParameters {
    async fn contract_max_size(&self) -> U64 {
        self.0.contract_max_size.into()
    }

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

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

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

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

    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()
    }

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

    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 gas_price_factor(&self) -> U64 {
        self.0.gas_price_factor.into()
    }

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

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

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

#[Object]
impl GasCosts {
    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.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 croo(&self) -> U64 {
        self.0.croo.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 k256(&self) -> U64 {
        self.0.k256.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 mcpi(&self) -> U64 {
        self.0.mcpi.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 ret(&self) -> U64 {
        self.0.ret.into()
    }

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

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

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

    async fn scwq(&self) -> U64 {
        self.0.scwq.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 swwq(&self) -> U64 {
        self.0.swwq.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 call(&self) -> DependentCost {
        self.0.call.into()
    }

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

    async fn csiz(&self) -> DependentCost {
        self.0.csiz.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 meq(&self) -> DependentCost {
        self.0.meq.into()
    }

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

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

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

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

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

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

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

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

    async fn base_chain_height(&self, ctx: &Context<'_>) -> U32 {
        let query: &Database = ctx.data_unchecked();

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

        height.into()
    }

    async fn peer_count(&self) -> u16 {
        0
    }

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

        Ok(ConsensusParameters(config.transaction_parameters))
    }

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

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

#[derive(Default)]
pub struct ChainQuery;

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