1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use crate::{
    client::schema::{
        contract::ContractIdFragment,
        schema,
        tx::{
            transparent_receipt::Receipt,
            TransactionStatus,
            TxIdArgs,
        },
        Address,
        AssetId,
        Bytes32,
        ConnectionArgs,
        ConversionError,
        HexString,
        MessageId,
        PageInfo,
        Salt,
        TransactionId,
        TxPointer,
        UtxoId,
        U64,
    },
    fuel_tx::field::ReceiptsRoot,
};
use core::convert::{
    TryFrom,
    TryInto,
};
use fuel_vm::fuel_tx::StorageSlot;
use itertools::Itertools;

/// Retrieves the transaction in opaque form
#[derive(cynic::QueryFragment, Debug)]
#[cynic(
    schema_path = "./assets/schema.sdl",
    graphql_type = "Query",
    variables = "TxIdArgs"
)]
pub struct TransactionQuery {
    #[arguments(id: $id)]
    pub transaction: Option<Transaction>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(
    schema_path = "./assets/schema.sdl",
    graphql_type = "Query",
    variables = "ConnectionArgs"
)]
pub struct TransactionsQuery {
    #[arguments(after: $after, before: $before, first: $first, last: $last)]
    pub transactions: TransactionConnection,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct TransactionConnection {
    pub edges: Vec<TransactionEdge>,
    pub page_info: PageInfo,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct TransactionEdge {
    pub cursor: String,
    pub node: Transaction,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct Transaction {
    pub gas_limit: Option<U64>,
    pub gas_price: Option<U64>,
    pub id: TransactionId,
    pub tx_pointer: Option<TxPointer>,
    pub input_asset_ids: Option<Vec<AssetId>>,
    pub input_contracts: Option<Vec<ContractIdFragment>>,
    pub inputs: Option<Vec<Input>>,
    pub is_script: bool,
    pub is_create: bool,
    pub is_mint: bool,
    pub outputs: Vec<Output>,
    pub maturity: Option<U64>,
    pub receipts_root: Option<Bytes32>,
    pub status: Option<TransactionStatus>,
    pub witnesses: Option<Vec<HexString>>,
    pub receipts: Option<Vec<Receipt>>,
    pub script: Option<HexString>,
    pub script_data: Option<HexString>,
    pub salt: Option<Salt>,
    pub storage_slots: Option<Vec<HexString>>,
    pub bytecode_witness_index: Option<i32>,
    pub bytecode_length: Option<U64>,
}

impl TryFrom<Transaction> for fuel_vm::prelude::Transaction {
    type Error = ConversionError;

    fn try_from(tx: Transaction) -> Result<Self, Self::Error> {
        let tx = if tx.is_script {
            let mut script = fuel_vm::prelude::Transaction::script(
                tx.gas_price
                    .ok_or_else(|| {
                        ConversionError::MissingField("gas_price".to_string())
                    })?
                    .into(),
                tx.gas_limit
                    .ok_or_else(|| {
                        ConversionError::MissingField("gas_limit".to_string())
                    })?
                    .into(),
                tx.maturity
                    .ok_or_else(|| ConversionError::MissingField("maturity".to_string()))?
                    .into(),
                tx.script
                    .ok_or_else(|| ConversionError::MissingField("script".to_string()))?
                    .into(),
                tx.script_data
                    .ok_or_else(|| {
                        ConversionError::MissingField("script_data".to_string())
                    })?
                    .into(),
                tx.inputs
                    .ok_or_else(|| ConversionError::MissingField("inputs".to_string()))?
                    .into_iter()
                    .map(TryInto::try_into)
                    .collect::<Result<Vec<::fuel_vm::fuel_tx::Input>, ConversionError>>(
                    )?,
                tx.outputs
                    .into_iter()
                    .map(TryInto::try_into)
                    .collect::<Result<Vec<::fuel_vm::fuel_tx::Output>, ConversionError>>(
                    )?,
                tx.witnesses
                    .ok_or_else(|| {
                        ConversionError::MissingField("witnesses".to_string())
                    })?
                    .into_iter()
                    .map(|w| w.0 .0.into())
                    .collect(),
            );
            *script.receipts_root_mut() = tx
                .receipts_root
                .ok_or_else(|| {
                    ConversionError::MissingField("receipts_root".to_string())
                })?
                .into();
            script.into()
        } else if tx.is_create {
            let create = fuel_vm::prelude::Transaction::create(
                tx.gas_price
                    .ok_or_else(|| {
                        ConversionError::MissingField("gas_price".to_string())
                    })?
                    .into(),
                tx.gas_limit
                    .ok_or_else(|| {
                        ConversionError::MissingField("gas_limit".to_string())
                    })?
                    .into(),
                tx.maturity
                    .ok_or_else(|| ConversionError::MissingField("maturity".to_string()))?
                    .into(),
                tx.bytecode_witness_index
                    .ok_or_else(|| {
                        ConversionError::MissingField(
                            "bytecode_witness_index".to_string(),
                        )
                    })?
                    .try_into()?,
                tx.salt
                    .ok_or_else(|| ConversionError::MissingField("salt".to_string()))?
                    .into(),
                tx.storage_slots
                    .ok_or_else(|| {
                        ConversionError::MissingField("storage_slots".to_string())
                    })?
                    .into_iter()
                    .map(|slot| {
                        if slot.0 .0.len() != 64 {
                            return Err(ConversionError::BytesLength)
                        }
                        let key = &slot.0 .0[0..32];
                        let value = &slot.0 .0[32..];
                        Ok(StorageSlot::new(
                            // unwrap is safe because length is checked
                            ::fuel_vm::fuel_types::Bytes32::try_from(key)
                                .map_err(|_| ConversionError::BytesLength)?,
                            ::fuel_vm::fuel_types::Bytes32::try_from(value)
                                .map_err(|_| ConversionError::BytesLength)?,
                        ))
                    })
                    .try_collect()?,
                tx.inputs
                    .ok_or_else(|| ConversionError::MissingField("inputs".to_string()))?
                    .into_iter()
                    .map(TryInto::try_into)
                    .collect::<Result<Vec<::fuel_vm::fuel_tx::Input>, ConversionError>>(
                    )?,
                tx.outputs
                    .into_iter()
                    .map(TryInto::try_into)
                    .collect::<Result<Vec<::fuel_vm::fuel_tx::Output>, ConversionError>>(
                    )?,
                tx.witnesses
                    .ok_or_else(|| {
                        ConversionError::MissingField("witnesses".to_string())
                    })?
                    .into_iter()
                    .map(|w| w.0 .0.into())
                    .collect(),
            );
            create.into()
        } else {
            let tx_pointer: fuel_vm::prelude::TxPointer = tx
                .tx_pointer
                .ok_or_else(|| ConversionError::MissingField("tx_pointer".to_string()))?
                .into();
            let mint = fuel_vm::prelude::Transaction::mint(
                tx_pointer,
                tx.outputs
                    .into_iter()
                    .map(TryInto::try_into)
                    .collect::<Result<Vec<::fuel_vm::fuel_tx::Output>, ConversionError>>(
                    )?,
            );
            mint.into()
        };

        // This `match` block is added here to enforce compilation error if a new variant
        // is added into the `fuel_tx::Transaction` enum.
        //
        // If you face a compilation error, please update the code above and add a new variant below.
        match tx {
            fuel_vm::prelude::Transaction::Script(_) => {}
            fuel_vm::prelude::Transaction::Create(_) => {}
            fuel_vm::prelude::Transaction::Mint(_) => {}
        };

        Ok(tx)
    }
}

#[derive(cynic::InlineFragments, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub enum Input {
    InputCoin(InputCoin),
    InputContract(InputContract),
    InputMessage(InputMessage),
    #[cynic(fallback)]
    Unknown,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct InputCoin {
    pub utxo_id: UtxoId,
    pub owner: Address,
    pub amount: U64,
    pub asset_id: AssetId,
    pub tx_pointer: TxPointer,
    pub witness_index: i32,
    pub maturity: U64,
    pub predicate: HexString,
    pub predicate_data: HexString,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct InputContract {
    pub utxo_id: UtxoId,
    pub balance_root: Bytes32,
    pub state_root: Bytes32,
    pub tx_pointer: TxPointer,
    pub contract: ContractIdFragment,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct InputMessage {
    message_id: MessageId,
    sender: Address,
    recipient: Address,
    amount: U64,
    nonce: U64,
    witness_index: i32,
    data: HexString,
    predicate: HexString,
    predicate_data: HexString,
}

impl TryFrom<Input> for ::fuel_vm::fuel_tx::Input {
    type Error = ConversionError;

    fn try_from(input: Input) -> Result<::fuel_vm::fuel_tx::Input, Self::Error> {
        Ok(match input {
            Input::InputCoin(coin) => {
                if coin.predicate.0 .0.is_empty() {
                    ::fuel_vm::fuel_tx::Input::CoinSigned {
                        utxo_id: coin.utxo_id.into(),
                        owner: coin.owner.into(),
                        amount: coin.amount.into(),
                        asset_id: coin.asset_id.into(),
                        tx_pointer: coin.tx_pointer.into(),
                        witness_index: coin.witness_index.try_into()?,
                        maturity: coin.maturity.into(),
                    }
                } else {
                    ::fuel_vm::fuel_tx::Input::CoinPredicate {
                        utxo_id: coin.utxo_id.into(),
                        owner: coin.owner.into(),
                        amount: coin.amount.into(),
                        asset_id: coin.asset_id.into(),
                        maturity: coin.maturity.into(),
                        tx_pointer: coin.tx_pointer.into(),
                        predicate: coin.predicate.into(),
                        predicate_data: coin.predicate_data.into(),
                    }
                }
            }
            Input::InputContract(contract) => ::fuel_vm::fuel_tx::Input::Contract {
                utxo_id: contract.utxo_id.into(),
                balance_root: contract.balance_root.into(),
                state_root: contract.state_root.into(),
                tx_pointer: contract.tx_pointer.into(),
                contract_id: contract.contract.id.into(),
            },
            Input::InputMessage(message) => {
                if message.predicate.0 .0.is_empty() {
                    ::fuel_vm::fuel_tx::Input::MessageSigned {
                        message_id: message.message_id.into(),
                        sender: message.sender.into(),
                        recipient: message.recipient.into(),
                        amount: message.amount.into(),
                        nonce: message.nonce.into(),
                        witness_index: message.witness_index.try_into()?,
                        data: message.data.into(),
                    }
                } else {
                    ::fuel_vm::fuel_tx::Input::MessagePredicate {
                        message_id: message.message_id.into(),
                        sender: message.sender.into(),
                        recipient: message.recipient.into(),
                        amount: message.amount.into(),
                        nonce: message.nonce.into(),
                        data: message.data.into(),
                        predicate: message.predicate.into(),
                        predicate_data: message.predicate_data.into(),
                    }
                }
            }
            Input::Unknown => return Err(Self::Error::UnknownVariant("Input")),
        })
    }
}

#[derive(cynic::InlineFragments, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub enum Output {
    CoinOutput(CoinOutput),
    ContractOutput(ContractOutput),
    MessageOutput(MessageOutput),
    ChangeOutput(ChangeOutput),
    VariableOutput(VariableOutput),
    ContractCreated(ContractCreated),
    #[cynic(fallback)]
    Unknown,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct CoinOutput {
    pub to: Address,
    pub amount: U64,
    pub asset_id: AssetId,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct MessageOutput {
    pub recipient: Address,
    pub amount: U64,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct ChangeOutput {
    pub to: Address,
    pub amount: U64,
    pub asset_id: AssetId,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct VariableOutput {
    pub to: Address,
    pub amount: U64,
    pub asset_id: AssetId,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct ContractOutput {
    pub input_index: i32,
    pub balance_root: Bytes32,
    pub state_root: Bytes32,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct ContractCreated {
    contract: ContractIdFragment,
    state_root: Bytes32,
}

impl TryFrom<Output> for ::fuel_vm::fuel_tx::Output {
    type Error = ConversionError;

    fn try_from(value: Output) -> Result<Self, Self::Error> {
        Ok(match value {
            Output::CoinOutput(coin) => Self::Coin {
                to: coin.to.into(),
                amount: coin.amount.into(),
                asset_id: coin.asset_id.into(),
            },
            Output::ContractOutput(contract) => Self::Contract {
                input_index: contract.input_index.try_into()?,
                balance_root: contract.balance_root.into(),
                state_root: contract.state_root.into(),
            },
            Output::MessageOutput(message) => Self::Message {
                recipient: message.recipient.into(),
                amount: message.amount.into(),
            },
            Output::ChangeOutput(change) => Self::Change {
                to: change.to.into(),
                amount: change.amount.into(),
                asset_id: change.asset_id.into(),
            },
            Output::VariableOutput(variable) => Self::Variable {
                to: variable.to.into(),
                amount: variable.amount.into(),
                asset_id: variable.asset_id.into(),
            },
            Output::ContractCreated(contract) => Self::ContractCreated {
                contract_id: contract.contract.id.into(),
                state_root: contract.state_root.into(),
            },
            Output::Unknown => return Err(Self::Error::UnknownVariant("Output")),
        })
    }
}