fuel_core_client/client/schema/
message.rs

1use super::{
2    BlockId,
3    Bytes32,
4    HexString,
5    PageInfo,
6    TransactionId,
7    block::Header,
8};
9use crate::client::{
10    pagination::{
11        PageDirection,
12        PaginationRequest,
13    },
14    schema::{
15        Address,
16        Nonce,
17        U32,
18        U64,
19        schema,
20    },
21};
22
23#[derive(cynic::QueryFragment, Clone, Debug)]
24#[cynic(schema_path = "./assets/schema.sdl")]
25pub struct Message {
26    pub amount: U64,
27    pub sender: Address,
28    pub recipient: Address,
29    pub nonce: Nonce,
30    pub data: HexString,
31    pub da_height: U64,
32}
33
34#[derive(cynic::QueryVariables, Debug)]
35pub struct NonceArgs {
36    pub nonce: Nonce,
37}
38
39#[derive(cynic::QueryFragment, Clone, Debug)]
40#[cynic(
41    schema_path = "./assets/schema.sdl",
42    graphql_type = "Query",
43    variables = "NonceArgs"
44)]
45pub struct MessageQuery {
46    #[arguments(nonce: $nonce)]
47    pub message: Option<Message>,
48}
49
50#[derive(cynic::QueryFragment, Clone, Debug)]
51#[cynic(schema_path = "./assets/schema.sdl")]
52pub struct MessageStatus {
53    pub(crate) state: MessageState,
54}
55
56#[derive(cynic::Enum, Clone, Copy, Debug)]
57#[cynic(schema_path = "./assets/schema.sdl")]
58pub enum MessageState {
59    Unspent,
60    Spent,
61    NotFound,
62}
63
64#[derive(cynic::QueryFragment, Clone, Debug)]
65#[cynic(
66    schema_path = "./assets/schema.sdl",
67    graphql_type = "Query",
68    variables = "OwnedMessagesConnectionArgs"
69)]
70pub struct OwnedMessageQuery {
71    #[arguments(owner: $owner, after: $after, before: $before, first: $first, last: $last)]
72    pub messages: MessageConnection,
73}
74
75#[derive(cynic::QueryFragment, Clone, Debug)]
76#[cynic(schema_path = "./assets/schema.sdl")]
77pub struct MessageConnection {
78    pub edges: Vec<MessageEdge>,
79    pub page_info: PageInfo,
80}
81
82#[derive(cynic::QueryFragment, Clone, Debug)]
83#[cynic(schema_path = "./assets/schema.sdl")]
84pub struct MessageEdge {
85    pub cursor: String,
86    pub node: Message,
87}
88
89#[derive(cynic::QueryVariables, Debug)]
90pub struct OwnedMessagesConnectionArgs {
91    /// Filter messages based on an owner
92    pub owner: Option<Address>,
93    /// Skip until coin id (forward pagination)
94    pub after: Option<String>,
95    /// Skip until coin id (backward pagination)
96    pub before: Option<String>,
97    /// Retrieve the first n coins in order (forward pagination)
98    pub first: Option<i32>,
99    /// Retrieve the last n coins in order (backward pagination).
100    /// Can't be used at the same time as `first`.
101    pub last: Option<i32>,
102}
103
104#[derive(cynic::QueryFragment, Clone, Debug)]
105#[cynic(
106    schema_path = "./assets/schema.sdl",
107    graphql_type = "Query",
108    variables = "MessageProofArgs"
109)]
110pub struct MessageProofQuery {
111    #[arguments(
112        transactionId: $transaction_id,
113        nonce: $nonce,
114        commitBlockId: $commit_block_id,
115        commitBlockHeight: $commit_block_height
116    )]
117    pub message_proof: MessageProof,
118}
119
120#[derive(cynic::QueryFragment, Clone, Debug)]
121#[cynic(schema_path = "./assets/schema.sdl")]
122pub struct MerkleProof {
123    /// The proof set of the message proof.
124    pub proof_set: Vec<Bytes32>,
125    /// The index that was used to produce this proof.
126    pub proof_index: U64,
127}
128
129#[derive(cynic::QueryFragment, Clone, Debug)]
130#[cynic(schema_path = "./assets/schema.sdl")]
131pub struct MessageProof {
132    /// Proof that message is contained within the provided block header.
133    pub message_proof: MerkleProof,
134    /// Proof that the provided block header is contained within the blockchain history.
135    pub block_proof: MerkleProof,
136    /// The previous fuel block header that contains the message. Message block height <
137    /// commit block height.
138    pub message_block_header: Header,
139    /// The consensus header associated with the finalized commit being used
140    /// as the root of the block proof.
141    pub commit_block_header: Header,
142
143    /// The messages sender address.
144    pub sender: Address,
145    /// The messages recipient address.
146    pub recipient: Address,
147    /// The nonce from the message.
148    pub nonce: Nonce,
149    /// The amount from the message.
150    pub amount: U64,
151    /// The data from the message.
152    pub data: HexString,
153}
154
155#[derive(cynic::QueryVariables, Debug)]
156pub struct MessageProofArgs {
157    /// Transaction id that contains the output message.
158    pub transaction_id: TransactionId,
159    /// The `Nonce` identifier of the output message that requires a proof.
160    pub nonce: Nonce,
161    /// The query supports either `commit_block_id`, or `commit_block_height` set on, not both.
162    /// The block id of the commitment block.
163    /// If it is `None`, the `commit_block_height` should be `Some`.
164    pub commit_block_id: Option<BlockId>,
165    /// The block height of the commitment block.
166    /// If it is `None`, the `commit_block_id` should be `Some`.
167    pub commit_block_height: Option<U32>,
168}
169
170#[derive(cynic::QueryFragment, Clone, Debug)]
171#[cynic(
172    schema_path = "./assets/schema.sdl",
173    graphql_type = "Query",
174    variables = "MessageStatusArgs"
175)]
176pub struct MessageStatusQuery {
177    #[arguments(nonce: $nonce)]
178    pub message_status: MessageStatus,
179}
180
181#[derive(cynic::QueryVariables, Debug)]
182pub struct MessageStatusArgs {
183    /// Nonce of the output message that requires a proof.
184    pub nonce: Nonce,
185}
186
187impl From<(Option<Address>, PaginationRequest<String>)> for OwnedMessagesConnectionArgs {
188    fn from(r: (Option<Address>, PaginationRequest<String>)) -> Self {
189        match r.1.direction {
190            PageDirection::Forward => OwnedMessagesConnectionArgs {
191                owner: r.0,
192                after: r.1.cursor,
193                before: None,
194                first: Some(r.1.results),
195                last: None,
196            },
197            PageDirection::Backward => OwnedMessagesConnectionArgs {
198                owner: r.0,
199                after: None,
200                before: r.1.cursor,
201                first: None,
202                last: Some(r.1.results),
203            },
204        }
205    }
206}
207
208#[cfg(test)]
209mod tests {
210    use super::*;
211
212    #[test]
213    fn owned_message_query_gql_output() {
214        use cynic::QueryBuilder;
215
216        let operation = OwnedMessageQuery::build(OwnedMessagesConnectionArgs {
217            owner: Some(Address::default()),
218            after: None,
219            before: None,
220            first: None,
221            last: None,
222        });
223
224        insta::assert_snapshot!(operation.query)
225    }
226}