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
use crate::schema::{
    contract::Contract,
    scalars::{
        Address,
        AssetId,
        Bytes32,
        U64,
    },
};
use async_graphql::{
    Object,
    Union,
};
use fuel_core_types::{
    fuel_asm::Word,
    fuel_tx,
    fuel_types,
};

#[derive(Union)]
pub enum Output {
    Coin(CoinOutput),
    Contract(ContractOutput),
    Message(MessageOutput),
    Change(ChangeOutput),
    Variable(VariableOutput),
    ContractCreated(ContractCreated),
}

pub struct CoinOutput {
    to: fuel_types::Address,
    amount: Word,
    asset_id: fuel_types::AssetId,
}

#[Object]
impl CoinOutput {
    async fn to(&self) -> Address {
        self.to.into()
    }

    async fn amount(&self) -> U64 {
        self.amount.into()
    }

    async fn asset_id(&self) -> AssetId {
        self.asset_id.into()
    }
}

pub struct MessageOutput {
    amount: Word,
    recipient: fuel_types::Address,
}

#[Object]
impl MessageOutput {
    async fn recipient(&self) -> Address {
        self.recipient.into()
    }

    async fn amount(&self) -> U64 {
        self.amount.into()
    }
}

pub struct ChangeOutput(CoinOutput);

#[Object]
impl ChangeOutput {
    async fn to(&self) -> Address {
        self.0.to.into()
    }

    async fn amount(&self) -> U64 {
        self.0.amount.into()
    }

    async fn asset_id(&self) -> AssetId {
        self.0.asset_id.into()
    }
}

pub struct VariableOutput(CoinOutput);

#[Object]
impl VariableOutput {
    async fn to(&self) -> Address {
        self.0.to.into()
    }

    async fn amount(&self) -> U64 {
        self.0.amount.into()
    }

    async fn asset_id(&self) -> AssetId {
        self.0.asset_id.into()
    }
}

pub struct ContractOutput {
    input_index: u8,
    balance_root: fuel_types::Bytes32,
    state_root: fuel_types::Bytes32,
}

#[Object]
impl ContractOutput {
    async fn input_index(&self) -> u8 {
        self.input_index
    }

    async fn balance_root(&self) -> Bytes32 {
        self.balance_root.into()
    }

    async fn state_root(&self) -> Bytes32 {
        self.state_root.into()
    }
}

pub struct ContractCreated {
    contract_id: fuel_types::ContractId,
    state_root: fuel_types::Bytes32,
}

#[Object]
impl ContractCreated {
    async fn contract(&self) -> Contract {
        self.contract_id.into()
    }

    async fn state_root(&self) -> Bytes32 {
        self.state_root.into()
    }
}

impl From<&fuel_tx::Output> for Output {
    fn from(output: &fuel_tx::Output) -> Self {
        match output {
            fuel_tx::Output::Coin {
                to,
                amount,
                asset_id,
            } => Output::Coin(CoinOutput {
                to: *to,
                amount: *amount,
                asset_id: *asset_id,
            }),
            fuel_tx::Output::Contract {
                input_index,
                balance_root,
                state_root,
            } => Output::Contract(ContractOutput {
                input_index: *input_index,
                balance_root: *balance_root,
                state_root: *state_root,
            }),
            fuel_tx::Output::Message { recipient, amount } => {
                Output::Message(MessageOutput {
                    recipient: *recipient,
                    amount: *amount,
                })
            }
            fuel_tx::Output::Change {
                to,
                amount,
                asset_id,
            } => Output::Change(ChangeOutput(CoinOutput {
                to: *to,
                amount: *amount,
                asset_id: *asset_id,
            })),
            fuel_tx::Output::Variable {
                to,
                amount,
                asset_id,
            } => Output::Variable(VariableOutput(CoinOutput {
                to: *to,
                amount: *amount,
                asset_id: *asset_id,
            })),
            fuel_tx::Output::ContractCreated {
                contract_id,
                state_root,
            } => Output::ContractCreated(ContractCreated {
                contract_id: *contract_id,
                state_root: *state_root,
            }),
        }
    }
}