Skip to main content

fuel_core_client/client/schema/
balance.rs

1use crate::client::{
2    PageDirection,
3    PaginationRequest,
4    schema::{
5        Address,
6        AssetId,
7        PageInfo,
8        U64,
9        schema,
10    },
11};
12
13#[derive(cynic::QueryVariables, Debug, Clone)]
14pub struct BalanceArgs {
15    pub owner: Address,
16    pub asset_id: AssetId,
17}
18
19#[derive(cynic::QueryFragment, Clone, Debug)]
20#[cynic(
21    schema_path = "./assets/schema.sdl",
22    graphql_type = "Query",
23    variables = "BalanceArgs"
24)]
25pub struct BalanceQuery {
26    #[arguments(owner: $owner, assetId: $asset_id)]
27    pub balance: Balance,
28}
29
30#[derive(cynic::InputObject, Clone, Debug)]
31#[cynic(schema_path = "./assets/schema.sdl")]
32pub struct BalanceFilterInput {
33    /// Filter coins based on the `owner` field
34    pub owner: Address,
35}
36
37#[derive(cynic::QueryVariables, Debug, Clone)]
38pub struct BalancesConnectionArgs {
39    /// Filter coins based on a filter
40    filter: BalanceFilterInput,
41    /// Skip until coin id (forward pagination)
42    pub after: Option<String>,
43    /// Skip until coin id (backward pagination)
44    pub before: Option<String>,
45    /// Retrieve the first n coins in order (forward pagination)
46    pub first: Option<i32>,
47    /// Retrieve the last n coins in order (backward pagination).
48    /// Can't be used at the same time as `first`.
49    pub last: Option<i32>,
50}
51
52impl From<(Address, PaginationRequest<String>)> for BalancesConnectionArgs {
53    fn from(r: (Address, PaginationRequest<String>)) -> Self {
54        match r.1.direction {
55            PageDirection::Forward => BalancesConnectionArgs {
56                filter: BalanceFilterInput { owner: r.0 },
57                after: r.1.cursor,
58                before: None,
59                first: Some(r.1.results),
60                last: None,
61            },
62            PageDirection::Backward => BalancesConnectionArgs {
63                filter: BalanceFilterInput { owner: r.0 },
64                after: None,
65                before: r.1.cursor,
66                first: None,
67                last: Some(r.1.results),
68            },
69        }
70    }
71}
72
73#[derive(cynic::QueryFragment, Clone, Debug)]
74#[cynic(
75    schema_path = "./assets/schema.sdl",
76    graphql_type = "Query",
77    variables = "BalancesConnectionArgs"
78)]
79pub struct BalancesQuery {
80    #[arguments(filter: $filter, after: $after, before: $before, first: $first, last: $last)]
81    pub balances: BalanceConnection,
82}
83
84#[derive(cynic::QueryFragment, Clone, Debug)]
85#[cynic(schema_path = "./assets/schema.sdl")]
86pub struct BalanceConnection {
87    pub edges: Vec<BalanceEdge>,
88    pub page_info: PageInfo,
89}
90
91#[derive(cynic::QueryFragment, Clone, Debug)]
92#[cynic(schema_path = "./assets/schema.sdl")]
93pub struct BalanceEdge {
94    pub cursor: String,
95    pub node: Balance,
96}
97
98#[derive(cynic::QueryFragment, Clone, Debug)]
99#[cynic(schema_path = "./assets/schema.sdl")]
100pub struct Balance {
101    pub owner: Address,
102    pub amount: U64,
103    pub asset_id: AssetId,
104}
105
106#[cfg(test)]
107mod tests {
108    use super::*;
109
110    #[test]
111    fn balance_query_gql_output() {
112        use cynic::QueryBuilder;
113        let operation = BalanceQuery::build(BalanceArgs {
114            owner: Address::default(),
115            asset_id: AssetId::default(),
116        });
117        insta::assert_snapshot!(operation.query)
118    }
119
120    #[test]
121    fn balances_connection_query_gql_output() {
122        use cynic::QueryBuilder;
123        let operation = BalancesQuery::build(BalancesConnectionArgs {
124            filter: BalanceFilterInput {
125                owner: Address::default(),
126            },
127            after: None,
128            before: None,
129            first: None,
130            last: None,
131        });
132        insta::assert_snapshot!(operation.query)
133    }
134}