fuel_core_client/client/
schema.rs

1// this is the format cynic expects
2#[allow(clippy::module_inception)]
3pub mod schema {
4    cynic::use_schema!("./assets/schema.sdl");
5}
6
7use fuel_core_types::{
8    fuel_tx,
9    fuel_types::canonical,
10};
11use hex::FromHexError;
12use std::{
13    array::TryFromSliceError,
14    fmt::{
15        self,
16        Debug,
17    },
18    io::ErrorKind,
19    num::TryFromIntError,
20};
21use thiserror::Error;
22
23use crate::client::pagination::{
24    PageDirection,
25    PaginationRequest,
26};
27pub use primitives::*;
28
29pub mod assets;
30pub mod balance;
31pub mod blob;
32pub mod block;
33pub mod chain;
34pub mod coins;
35pub mod contract;
36pub mod da_compressed;
37pub mod message;
38pub mod node_info;
39pub mod storage_read_replay;
40pub mod upgrades;
41
42pub mod gas_price;
43pub mod primitives;
44pub mod tx;
45
46pub mod relayed_tx;
47pub mod storage;
48
49#[derive(cynic::QueryFragment, Clone, Debug)]
50#[cynic(schema_path = "./assets/schema.sdl", graphql_type = "Query")]
51pub struct Health {
52    pub health: bool,
53}
54
55#[derive(cynic::QueryFragment, Clone, Debug)]
56#[cynic(schema_path = "./assets/schema.sdl", graphql_type = "Mutation")]
57pub struct StartSession {
58    pub start_session: cynic::Id,
59}
60
61#[derive(cynic::QueryVariables)]
62pub struct IdArg {
63    pub id: cynic::Id,
64}
65
66#[derive(cynic::QueryFragment, Clone, Debug)]
67#[cynic(
68    schema_path = "./assets/schema.sdl",
69    graphql_type = "Mutation",
70    variables = "IdArg"
71)]
72pub struct EndSession {
73    #[arguments(id: $id)]
74    pub end_session: bool,
75}
76
77#[derive(cynic::QueryFragment, Clone, Debug)]
78#[cynic(
79    schema_path = "./assets/schema.sdl",
80    graphql_type = "Mutation",
81    variables = "IdArg"
82)]
83pub struct Reset {
84    #[arguments(id: $id)]
85    pub reset: bool,
86}
87
88#[derive(Debug, cynic::QueryVariables)]
89pub struct ExecuteArgs {
90    pub id: cynic::Id,
91    pub op: String,
92}
93
94#[derive(cynic::QueryFragment, Clone, Debug)]
95#[cynic(
96    schema_path = "./assets/schema.sdl",
97    graphql_type = "Mutation",
98    variables = "ExecuteArgs"
99)]
100pub struct Execute {
101    #[arguments(id: $id, op: $op)]
102    pub execute: bool,
103}
104
105#[derive(Debug, cynic::QueryVariables)]
106pub struct RegisterArgs {
107    pub id: cynic::Id,
108    pub register: U32,
109}
110
111#[derive(cynic::QueryFragment, Clone, Debug)]
112#[cynic(
113    schema_path = "./assets/schema.sdl",
114    graphql_type = "Query",
115    variables = "RegisterArgs"
116)]
117pub struct Register {
118    #[arguments(id: $id, register: $register)]
119    pub register: U64,
120}
121
122#[derive(Debug, cynic::QueryVariables)]
123pub struct MemoryArgs {
124    pub id: cynic::Id,
125    pub start: U32,
126    pub size: U32,
127}
128
129#[derive(cynic::QueryFragment, Clone, Debug)]
130#[cynic(
131    schema_path = "./assets/schema.sdl",
132    graphql_type = "Query",
133    variables = "MemoryArgs"
134)]
135pub struct Memory {
136    #[arguments(id: $id, start: $start, size: $size)]
137    pub memory: String,
138}
139
140#[derive(cynic::QueryVariables, Debug)]
141pub struct SetBreakpointArgs {
142    pub id: cynic::Id,
143    pub bp: Breakpoint,
144}
145
146#[derive(cynic::QueryFragment, Clone, Debug)]
147#[cynic(
148    schema_path = "./assets/schema.sdl",
149    graphql_type = "Mutation",
150    variables = "SetBreakpointArgs"
151)]
152pub struct SetBreakpoint {
153    #[arguments(id: $id, breakpoint: $bp)]
154    pub set_breakpoint: bool,
155}
156
157#[derive(cynic::InputObject, Debug)]
158#[cynic(schema_path = "./assets/schema.sdl")]
159pub struct Breakpoint {
160    pub contract: ContractId,
161    pub pc: U64,
162}
163
164#[derive(cynic::QueryVariables, Debug)]
165pub struct SetSingleSteppingArgs {
166    pub id: cynic::Id,
167    pub enable: bool,
168}
169
170#[derive(cynic::QueryFragment, Clone, Debug)]
171#[cynic(
172    schema_path = "./assets/schema.sdl",
173    graphql_type = "Mutation",
174    variables = "SetSingleSteppingArgs"
175)]
176pub struct SetSingleStepping {
177    #[arguments(id: $id, enable: $enable)]
178    pub set_single_stepping: bool,
179}
180
181#[derive(cynic::QueryVariables, Debug)]
182pub struct StartTxArgs {
183    pub id: cynic::Id,
184    pub tx: String,
185}
186
187#[derive(cynic::QueryFragment, Clone, Debug)]
188#[cynic(
189    schema_path = "./assets/schema.sdl",
190    graphql_type = "Mutation",
191    variables = "StartTxArgs"
192)]
193pub struct StartTx {
194    #[arguments(id: $id, txJson: $tx)]
195    pub start_tx: RunResult,
196}
197
198#[derive(cynic::QueryVariables, Debug)]
199pub struct ContinueTxArgs {
200    pub id: cynic::Id,
201}
202
203#[derive(cynic::QueryFragment, Clone, Debug)]
204#[cynic(
205    schema_path = "./assets/schema.sdl",
206    graphql_type = "Mutation",
207    variables = "ContinueTxArgs"
208)]
209pub struct ContinueTx {
210    #[arguments(id: $id)]
211    pub continue_tx: RunResult,
212}
213
214#[derive(cynic::QueryFragment, Clone, Debug)]
215#[cynic(schema_path = "./assets/schema.sdl")]
216pub struct RunResult {
217    pub breakpoint: Option<OutputBreakpoint>,
218    pub json_receipts: Vec<String>,
219}
220
221impl RunResult {
222    pub fn receipts(&self) -> impl Iterator<Item = fuel_tx::Receipt> + '_ {
223        self.json_receipts.iter().map(|r| {
224            serde_json::from_str::<fuel_tx::Receipt>(r)
225                .expect("Receipt deserialization failed, server/client version mismatch")
226        })
227    }
228}
229
230impl fmt::Display for RunResult {
231    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
232        let receipts: Vec<fuel_tx::Receipt> = self.receipts().collect();
233        f.debug_struct("RunResult")
234            .field("breakpoint", &self.breakpoint)
235            .field("receipts", &receipts)
236            .finish()
237    }
238}
239
240#[derive(cynic::QueryFragment, Clone, Debug)]
241#[cynic(schema_path = "./assets/schema.sdl")]
242pub struct OutputBreakpoint {
243    pub contract: ContractId,
244    pub pc: U64,
245}
246
247/// Generic graphql pagination query args
248#[derive(cynic::QueryVariables, Debug, Default)]
249pub struct ConnectionArgs {
250    /// Skip until cursor (forward pagination)
251    pub after: Option<String>,
252    /// Skip until cursor (backward pagination)
253    pub before: Option<String>,
254    /// Retrieve the first n items in order (forward pagination)
255    pub first: Option<i32>,
256    /// Retrieve the last n items in order (backward pagination).
257    /// Can't be used at the same time as `first`.
258    pub last: Option<i32>,
259}
260
261#[derive(cynic::QueryFragment, Clone, Debug)]
262#[cynic(schema_path = "./assets/schema.sdl")]
263pub struct PageInfo {
264    pub end_cursor: Option<String>,
265    pub has_next_page: bool,
266    pub has_previous_page: bool,
267    pub start_cursor: Option<String>,
268}
269
270impl<T: Into<String>> From<PaginationRequest<T>> for ConnectionArgs {
271    fn from(req: PaginationRequest<T>) -> Self {
272        match req.direction {
273            PageDirection::Forward => Self {
274                after: req.cursor.map(Into::into),
275                before: None,
276                first: Some(req.results),
277                last: None,
278            },
279            PageDirection::Backward => Self {
280                after: None,
281                before: req.cursor.map(Into::into),
282                first: None,
283                last: Some(req.results),
284            },
285        }
286    }
287}
288
289#[derive(Error, Debug)]
290#[non_exhaustive]
291pub enum ConversionError {
292    #[error("Field is required from the GraphQL response {0}")]
293    MissingField(String),
294    #[error("expected 0x prefix")]
295    HexStringPrefixError,
296    #[error("expected 32 bytes, received {0}")]
297    HexString256LengthError(usize),
298    #[error("hex parsing error {0}")]
299    HexDecodingError(FromHexError),
300    #[error("hex parsing error {0}")]
301    HexError(String),
302    #[error("failed integer conversion")]
303    IntegerConversion,
304    #[error("failed to deserialize transaction from bytes {0}")]
305    TransactionFromBytesError(canonical::Error),
306    #[error("failed to deserialize receipt from bytes {0}")]
307    ReceiptFromBytesError(canonical::Error),
308    #[error("failed to convert from bytes due to unexpected length")]
309    BytesLength,
310    #[error("Unknown variant of the {0} enum")]
311    UnknownVariant(&'static str),
312}
313
314impl From<FromHexError> for ConversionError {
315    fn from(hex_error: FromHexError) -> Self {
316        Self::HexDecodingError(hex_error)
317    }
318}
319
320impl From<ConversionError> for std::io::Error {
321    fn from(e: ConversionError) -> Self {
322        std::io::Error::new(ErrorKind::Other, e)
323    }
324}
325
326impl From<TryFromIntError> for ConversionError {
327    fn from(_: TryFromIntError) -> Self {
328        ConversionError::IntegerConversion
329    }
330}
331
332impl From<TryFromSliceError> for ConversionError {
333    fn from(_: TryFromSliceError) -> Self {
334        ConversionError::BytesLength
335    }
336}