fuel_core/schema/tx/
output.rs1use crate::schema::scalars::{
2 Address,
3 AssetId,
4 Bytes32,
5 ContractId,
6 U16,
7 U64,
8 UtxoId,
9};
10use async_graphql::{
11 Object,
12 Union,
13};
14use fuel_core_types::{
15 fuel_asm::Word,
16 fuel_tx,
17 fuel_tx::output,
18 fuel_types,
19};
20
21pub struct ResolvedOutput {
22 utxo_id: fuel_tx::UtxoId,
23 output: output::Output,
24}
25
26#[Object]
27impl ResolvedOutput {
28 async fn utxo_id(&self) -> UtxoId {
29 self.utxo_id.into()
30 }
31
32 async fn output(&self) -> Output {
33 (&self.output).into()
34 }
35}
36
37impl From<(fuel_tx::UtxoId, output::Output)> for ResolvedOutput {
38 fn from(value: (fuel_tx::UtxoId, output::Output)) -> Self {
39 let (utxo_id, output) = value;
40 ResolvedOutput { utxo_id, output }
41 }
42}
43
44#[derive(Union)]
45pub enum Output {
46 Coin(CoinOutput),
47 Contract(ContractOutput),
48 Change(ChangeOutput),
49 Variable(VariableOutput),
50 ContractCreated(ContractCreated),
51}
52
53pub struct CoinOutput {
54 to: fuel_types::Address,
55 amount: Word,
56 asset_id: fuel_types::AssetId,
57}
58
59#[Object]
60impl CoinOutput {
61 async fn to(&self) -> Address {
62 self.to.into()
63 }
64
65 async fn amount(&self) -> U64 {
66 self.amount.into()
67 }
68
69 async fn asset_id(&self) -> AssetId {
70 self.asset_id.into()
71 }
72}
73
74pub struct ChangeOutput(CoinOutput);
75
76#[Object]
77impl ChangeOutput {
78 async fn to(&self) -> Address {
79 self.0.to.into()
80 }
81
82 async fn amount(&self) -> U64 {
83 self.0.amount.into()
84 }
85
86 async fn asset_id(&self) -> AssetId {
87 self.0.asset_id.into()
88 }
89}
90
91pub struct VariableOutput(CoinOutput);
92
93#[Object]
94impl VariableOutput {
95 async fn to(&self) -> Address {
96 self.0.to.into()
97 }
98
99 async fn amount(&self) -> U64 {
100 self.0.amount.into()
101 }
102
103 async fn asset_id(&self) -> AssetId {
104 self.0.asset_id.into()
105 }
106}
107
108pub struct ContractOutput {
109 input_index: u16,
110 balance_root: fuel_types::Bytes32,
111 state_root: fuel_types::Bytes32,
112}
113
114#[Object]
115impl ContractOutput {
116 async fn input_index(&self) -> U16 {
117 self.input_index.into()
118 }
119
120 async fn balance_root(&self) -> Bytes32 {
121 self.balance_root.into()
122 }
123
124 async fn state_root(&self) -> Bytes32 {
125 self.state_root.into()
126 }
127}
128
129pub struct ContractCreated {
130 contract_id: fuel_types::ContractId,
131 state_root: fuel_types::Bytes32,
132}
133
134#[Object]
135impl ContractCreated {
136 async fn contract(&self) -> ContractId {
137 self.contract_id.into()
138 }
139
140 async fn state_root(&self) -> Bytes32 {
141 self.state_root.into()
142 }
143}
144
145impl From<&fuel_tx::Output> for Output {
146 fn from(output: &fuel_tx::Output) -> Self {
147 match output {
148 fuel_tx::Output::Coin {
149 to,
150 amount,
151 asset_id,
152 } => Output::Coin(CoinOutput {
153 to: *to,
154 amount: *amount,
155 asset_id: *asset_id,
156 }),
157 fuel_tx::Output::Contract(contract) => Output::Contract(contract.into()),
158 fuel_tx::Output::Change {
159 to,
160 amount,
161 asset_id,
162 } => Output::Change(ChangeOutput(CoinOutput {
163 to: *to,
164 amount: *amount,
165 asset_id: *asset_id,
166 })),
167 fuel_tx::Output::Variable {
168 to,
169 amount,
170 asset_id,
171 } => Output::Variable(VariableOutput(CoinOutput {
172 to: *to,
173 amount: *amount,
174 asset_id: *asset_id,
175 })),
176 fuel_tx::Output::ContractCreated {
177 contract_id,
178 state_root,
179 } => Output::ContractCreated(ContractCreated {
180 contract_id: *contract_id,
181 state_root: *state_root,
182 }),
183 }
184 }
185}
186
187impl From<&output::contract::Contract> for ContractOutput {
188 fn from(value: &output::contract::Contract) -> Self {
189 let output::contract::Contract {
190 input_index,
191 balance_root,
192 state_root,
193 } = value;
194 ContractOutput {
195 input_index: *input_index,
196 balance_root: *balance_root,
197 state_root: *state_root,
198 }
199 }
200}