sui_graphql_client/query_types/
checkpoint.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use base64ct::Encoding;
5use sui_types::CheckpointSummary;
6
7use crate::error;
8use crate::error::Error;
9use crate::error::Kind;
10use crate::query_types::schema;
11use crate::query_types::Base64;
12use crate::query_types::PageInfo;
13
14// ===========================================================================
15// Checkpoint Queries
16// ===========================================================================
17
18#[derive(cynic::QueryFragment, Debug)]
19#[cynic(schema = "rpc", graphql_type = "Query", variables = "CheckpointArgs")]
20pub struct CheckpointQuery {
21    #[arguments(id: $id)]
22    pub checkpoint: Option<Checkpoint>,
23}
24
25#[derive(cynic::QueryFragment, Debug)]
26#[cynic(schema = "rpc", graphql_type = "Query", variables = "CheckpointArgs")]
27pub struct CheckpointTotalTxQuery {
28    #[arguments(id: $id)]
29    pub checkpoint: Option<CheckpointTotalTx>,
30}
31
32#[derive(cynic::QueryFragment, Debug)]
33#[cynic(schema = "rpc", graphql_type = "Checkpoint")]
34pub struct CheckpointTotalTx {
35    pub network_total_transactions: Option<u64>,
36}
37
38#[derive(cynic::QueryFragment, Debug)]
39#[cynic(schema = "rpc", graphql_type = "Query", variables = "CheckpointsArgs")]
40pub struct CheckpointsQuery {
41    pub checkpoints: CheckpointConnection,
42}
43
44#[derive(cynic::QueryFragment, Debug)]
45#[cynic(schema = "rpc", graphql_type = "CheckpointConnection")]
46pub struct CheckpointConnection {
47    pub nodes: Vec<Checkpoint>,
48    pub page_info: PageInfo,
49}
50
51#[derive(cynic::QueryVariables, Debug)]
52pub struct CheckpointsArgs<'a> {
53    pub first: Option<i32>,
54    pub after: Option<&'a str>,
55    pub last: Option<i32>,
56    pub before: Option<&'a str>,
57}
58
59// ===========================================================================
60// Checkpoint Query Args
61// ===========================================================================
62
63#[derive(cynic::QueryVariables, Debug)]
64pub struct CheckpointArgs {
65    pub id: CheckpointId,
66}
67
68#[derive(cynic::InputObject, Debug)]
69#[cynic(schema = "rpc", graphql_type = "CheckpointId")]
70pub struct CheckpointId {
71    pub digest: Option<String>,
72    pub sequence_number: Option<u64>,
73}
74// ===========================================================================
75// Checkpoint Types
76// ===========================================================================
77
78#[derive(cynic::QueryFragment, Debug)]
79#[cynic(schema = "rpc", graphql_type = "Checkpoint")]
80pub struct Checkpoint {
81    pub bcs: Option<Base64>,
82}
83
84impl TryInto<CheckpointSummary> for Checkpoint {
85    type Error = error::Error;
86
87    fn try_into(self) -> Result<CheckpointSummary, Error> {
88        let checkpoint = self
89            .bcs
90            .map(|x| base64ct::Base64::decode_vec(&x.0))
91            .transpose()?
92            .map(|bcs| {
93                bcs::from_bytes::<CheckpointSummary>(&bcs).map_err(|e| {
94                    Error::from_error(
95                        Kind::Other,
96                        format!("Failed to deserialize checkpoint summary: {}", e),
97                    )
98                })
99            })
100            .transpose()?;
101        checkpoint.ok_or_else(|| Error::from_error(Kind::Other, "Checkpoint summary is missing"))
102    }
103}