fuel_core_client/client/schema/
message.rs

1use super::{
2    block::Header,
3    BlockId,
4    Bytes32,
5    HexString,
6    PageInfo,
7    TransactionId,
8};
9use crate::client::{
10    pagination::{
11        PageDirection,
12        PaginationRequest,
13    },
14    schema::{
15        schema,
16        Address,
17        Nonce,
18        U32,
19        U64,
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
162    /// The query supports either `commit_block_id`, or `commit_block_height` set on, not both.
163
164    /// The block id of the commitment block.
165    /// If it is `None`, the `commit_block_height` should be `Some`.
166    pub commit_block_id: Option<BlockId>,
167    /// The block height of the commitment block.
168    /// If it is `None`, the `commit_block_id` should be `Some`.
169    pub commit_block_height: Option<U32>,
170}
171
172#[derive(cynic::QueryFragment, Clone, Debug)]
173#[cynic(
174    schema_path = "./assets/schema.sdl",
175    graphql_type = "Query",
176    variables = "MessageStatusArgs"
177)]
178pub struct MessageStatusQuery {
179    #[arguments(nonce: $nonce)]
180    pub message_status: MessageStatus,
181}
182
183#[derive(cynic::QueryVariables, Debug)]
184pub struct MessageStatusArgs {
185    /// Nonce of the output message that requires a proof.
186    pub nonce: Nonce,
187}
188
189impl From<(Option<Address>, PaginationRequest<String>)> for OwnedMessagesConnectionArgs {
190    fn from(r: (Option<Address>, PaginationRequest<String>)) -> Self {
191        match r.1.direction {
192            PageDirection::Forward => OwnedMessagesConnectionArgs {
193                owner: r.0,
194                after: r.1.cursor,
195                before: None,
196                first: Some(r.1.results),
197                last: None,
198            },
199            PageDirection::Backward => OwnedMessagesConnectionArgs {
200                owner: r.0,
201                after: None,
202                before: r.1.cursor,
203                first: None,
204                last: Some(r.1.results),
205            },
206        }
207    }
208}
209
210#[cfg(test)]
211mod tests {
212    use super::*;
213
214    #[test]
215    fn owned_message_query_gql_output() {
216        use cynic::QueryBuilder;
217
218        let operation = OwnedMessageQuery::build(OwnedMessagesConnectionArgs {
219            owner: Some(Address::default()),
220            after: None,
221            before: None,
222            first: None,
223            last: None,
224        });
225
226        insta::assert_snapshot!(operation.query)
227    }
228}