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
use crate::client::{
    schema::{
        schema,
        AssetId,
        ContractId,
        HexString,
        PageInfo,
        Salt,
        U64,
    },
    PageDirection,
    PaginatedResult,
    PaginationRequest,
};

#[derive(cynic::QueryVariables, Debug)]
pub struct ContractByIdArgs {
    pub id: ContractId,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(
    schema_path = "./assets/schema.sdl",
    graphql_type = "Query",
    variables = "ContractByIdArgs"
)]
pub struct ContractByIdQuery {
    #[arguments(id: $id)]
    pub contract: Option<Contract>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct ContractBalance {
    pub contract: ContractId,
    pub amount: U64,
    pub asset_id: AssetId,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct ContractBalanceQueryArgs {
    pub id: ContractId,
    pub asset: AssetId,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(
    schema_path = "./assets/schema.sdl",
    graphql_type = "Query",
    variables = "ContractBalanceQueryArgs"
)]
pub struct ContractBalanceQuery {
    #[arguments(contract: $id, asset: $asset)]
    pub contract_balance: ContractBalance,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct Contract {
    pub id: ContractId,
    pub bytecode: HexString,
    pub salt: Salt,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl", graphql_type = "Contract")]
pub struct ContractIdFragment {
    pub id: ContractId,
}

#[derive(cynic::InputObject, Clone, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct ContractBalanceFilterInput {
    /// Filter asset balances based on the `contract` field
    pub contract: ContractId,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct ContractBalancesConnectionArgs {
    /// Filter balances based on a filter
    filter: ContractBalanceFilterInput,
    /// Skip until asset id (forward pagination)
    pub after: Option<String>,
    /// Skip until asset id (backward pagination)
    pub before: Option<String>,
    /// Retrieve the first n asset balances in order (forward pagination)
    pub first: Option<i32>,
    /// Retrieve the last n asset balances in order (backward pagination).
    /// Can't be used at the same time as `first`.
    pub last: Option<i32>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct ContractBalanceEdge {
    pub cursor: String,
    pub node: ContractBalance,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct ContractBalanceConnection {
    pub edges: Vec<ContractBalanceEdge>,
    pub page_info: PageInfo,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(
    schema_path = "./assets/schema.sdl",
    graphql_type = "Query",
    variables = "ContractBalancesConnectionArgs"
)]
pub struct ContractBalancesQuery {
    #[arguments(filter: $filter, after: $after, before: $before, first: $first, last: $last)]
    pub contract_balances: ContractBalanceConnection,
}

impl From<ContractBalanceConnection> for PaginatedResult<ContractBalance, String> {
    fn from(conn: ContractBalanceConnection) -> Self {
        PaginatedResult {
            has_next_page: conn.page_info.has_next_page,
            has_previous_page: conn.page_info.has_previous_page,
            cursor: conn.page_info.end_cursor,
            results: conn.edges.into_iter().map(|e| e.node).collect(),
        }
    }
}

impl From<(ContractId, PaginationRequest<String>)> for ContractBalancesConnectionArgs {
    fn from(r: (ContractId, PaginationRequest<String>)) -> Self {
        match r.1.direction {
            PageDirection::Forward => ContractBalancesConnectionArgs {
                filter: ContractBalanceFilterInput { contract: r.0 },
                after: r.1.cursor,
                before: None,
                first: Some(r.1.results as i32),
                last: None,
            },
            PageDirection::Backward => ContractBalancesConnectionArgs {
                filter: ContractBalanceFilterInput { contract: r.0 },
                after: None,
                before: r.1.cursor,
                first: None,
                last: Some(r.1.results as i32),
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn contract_by_id_query_gql_output() {
        use cynic::QueryBuilder;
        let operation = ContractByIdQuery::build(ContractByIdArgs {
            id: ContractId::default(),
        });
        insta::assert_snapshot!(operation.query)
    }
}