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