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
use cynic::{http::SurfExt, MutationBuilder, Operation, QueryBuilder};
use fuel_vm::prelude::*;
use std::{
convert::TryInto,
io, net,
str::{self, FromStr},
};
pub mod schema;
pub mod types;
use schema::{
block::BlockByIdArgs,
coin::{Coin, CoinByIdArgs},
tx::{TxArg, TxIdArgs},
Bytes, HexString, HexString256, IdArg, MemoryArgs, RegisterArgs,
};
use crate::client::schema::ConversionError;
use crate::client::types::{TransactionResponse, TransactionStatus};
pub use schema::{PageDirection, PaginatedResult, PaginationRequest};
use std::io::ErrorKind;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FuelClient {
url: surf::Url,
}
impl FromStr for FuelClient {
type Err = net::AddrParseError;
fn from_str(str: &str) -> Result<Self, Self::Err> {
str.parse().map(|s: net::SocketAddr| s.into())
}
}
impl<S> From<S> for FuelClient
where
S: Into<net::SocketAddr>,
{
fn from(socket: S) -> Self {
let url = format!("http://{}/graphql", socket.into())
.as_str()
.parse()
.unwrap();
Self { url }
}
}
impl FuelClient {
pub fn new(url: impl AsRef<str>) -> Result<Self, net::AddrParseError> {
Self::from_str(url.as_ref())
}
async fn query<'a, R: 'a>(&self, q: Operation<'a, R>) -> io::Result<R> {
let response = surf::post(&self.url)
.run_graphql(q)
.await
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
match (response.data, response.errors) {
(Some(d), _) => Ok(d),
(_, Some(e)) => {
let e = e.into_iter().map(|e| e.message).fold(
String::from("Response errors"),
|mut s, e| {
s.push_str("; ");
s.push_str(e.as_str());
s
},
);
Err(io::Error::new(io::ErrorKind::Other, e))
}
_ => Err(io::Error::new(io::ErrorKind::Other, "Invalid response")),
}
}
pub async fn health(&self) -> io::Result<bool> {
let query = schema::Health::build(());
self.query(query).await.map(|r| r.health)
}
pub async fn dry_run(&self, tx: &Transaction) -> io::Result<Vec<Receipt>> {
let tx = tx.clone().to_bytes();
let query = schema::tx::DryRun::build(&TxArg {
tx: HexString(Bytes(tx)),
});
let receipts = self.query(query).await.map(|r| r.dry_run)?;
receipts
.into_iter()
.map(|receipt| receipt.try_into().map_err(Into::into))
.collect()
}
pub async fn submit(&self, tx: &Transaction) -> io::Result<HexString256> {
let tx = tx.clone().to_bytes();
let query = schema::tx::Submit::build(&TxArg {
tx: HexString(Bytes(tx)),
});
let id = self.query(query).await.map(|r| r.submit)?;
Ok(id)
}
pub async fn start_session(&self) -> io::Result<String> {
let query = schema::StartSession::build(&());
self.query(query)
.await
.map(|r| r.start_session.into_inner())
}
pub async fn end_session(&self, id: &str) -> io::Result<bool> {
let query = schema::EndSession::build(&IdArg { id: id.into() });
self.query(query).await.map(|r| r.end_session)
}
pub async fn reset(&self, id: &str) -> io::Result<bool> {
let query = schema::Reset::build(&IdArg { id: id.into() });
self.query(query).await.map(|r| r.reset)
}
pub async fn execute(&self, id: &str, op: &Opcode) -> io::Result<bool> {
let op = serde_json::to_string(op)?;
let query = schema::Execute::build(&schema::ExecuteArgs { id: id.into(), op });
self.query(query).await.map(|r| r.execute)
}
pub async fn register(&self, id: &str, register: RegisterId) -> io::Result<Word> {
let query = schema::Register::build(&RegisterArgs {
id: id.into(),
register: register.into(),
});
Ok(self.query(query).await?.register.0 as Word)
}
pub async fn memory(&self, id: &str, start: usize, size: usize) -> io::Result<Vec<u8>> {
let query = schema::Memory::build(&MemoryArgs {
id: id.into(),
start: start.into(),
size: size.into(),
});
let memory = self.query(query).await?.memory;
Ok(serde_json::from_str(memory.as_str())?)
}
pub async fn transaction(&self, id: &str) -> io::Result<Option<TransactionResponse>> {
let query = schema::tx::TransactionQuery::build(&TxIdArgs { id: id.parse()? });
let transaction = self.query(query).await?.transaction;
Ok(transaction.map(|tx| tx.try_into()).transpose()?)
}
pub async fn transaction_status(&self, id: &str) -> io::Result<TransactionStatus> {
let query = schema::tx::TransactionQuery::build(&TxIdArgs { id: id.parse()? });
let tx = self.query(query).await?.transaction.ok_or_else(|| {
io::Error::new(ErrorKind::NotFound, format!("transaction {} not found", id))
})?;
let status = tx
.status
.ok_or_else(|| {
io::Error::new(
ErrorKind::NotFound,
format!("status not found for transaction {}", id),
)
})?
.try_into()?;
Ok(status)
}
pub async fn transactions(
&self,
request: PaginationRequest<String>,
) -> io::Result<PaginatedResult<TransactionResponse, String>> {
let query = schema::tx::TransactionsQuery::build(&request.into());
let transactions = self.query(query).await?.transactions.try_into()?;
Ok(transactions)
}
pub async fn transactions_by_owner(
&self,
owner: &str,
request: PaginationRequest<String>,
) -> io::Result<PaginatedResult<TransactionResponse, String>> {
let owner: HexString256 = owner.parse()?;
let query = schema::tx::TransactionsByOwnerQuery::build(&(owner, request).into());
let transactions = self.query(query).await?.transactions_by_owner.try_into()?;
Ok(transactions)
}
pub async fn receipts(&self, id: &str) -> io::Result<Vec<fuel_tx::Receipt>> {
let query = schema::tx::TransactionQuery::build(&TxIdArgs { id: id.parse()? });
let tx = self.query(query).await?.transaction.ok_or_else(|| {
io::Error::new(ErrorKind::NotFound, format!("transaction {} not found", id))
})?;
let receipts: Result<Vec<fuel_tx::Receipt>, ConversionError> = tx
.receipts
.unwrap_or_default()
.into_iter()
.map(|r| r.try_into())
.collect();
Ok(receipts?)
}
pub async fn block(&self, id: &str) -> io::Result<Option<schema::block::Block>> {
let query = schema::block::BlockByIdQuery::build(&BlockByIdArgs { id: id.parse()? });
let block = self.query(query).await?.block;
Ok(block)
}
pub async fn blocks(
&self,
request: PaginationRequest<String>,
) -> io::Result<PaginatedResult<schema::block::Block, String>> {
let query = schema::block::BlocksQuery::build(&request.into());
let blocks = self.query(query).await?.blocks.into();
Ok(blocks)
}
pub async fn coin(&self, id: &str) -> io::Result<Option<Coin>> {
let query = schema::coin::CoinByIdQuery::build(CoinByIdArgs {
utxo_id: id.parse()?,
});
let coin = self.query(query).await?.coin;
Ok(coin)
}
pub async fn coins(
&self,
owner: &str,
color: Option<&str>,
request: PaginationRequest<String>,
) -> io::Result<PaginatedResult<schema::coin::Coin, String>> {
let owner: HexString256 = owner.parse()?;
let color: HexString256 = match color {
Some(color) => color.parse()?,
None => HexString256::default(),
};
let query = schema::coin::CoinsQuery::build(&(owner, color, request).into());
let coins = self.query(query).await?.coins.into();
Ok(coins)
}
}
#[cfg(any(test, feature = "test-helpers"))]
impl FuelClient {
pub async fn transparent_transaction(
&self,
id: &str,
) -> io::Result<Option<fuel_tx::Transaction>> {
let query = schema::tx::TransactionQuery::build(&TxIdArgs { id: id.parse()? });
let transaction = self.query(query).await?.transaction;
Ok(transaction.map(|tx| tx.try_into()).transpose()?)
}
}