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
use crate::database::transaction::OwnedTransactionIndexCursor;
use crate::model::fuel_block::BlockHeight;
use async_graphql::{
connection::CursorType, InputValueError, InputValueResult, Scalar, ScalarType, Value,
};
use fuel_tx::{Address, Bytes32, Color, ContractId, Salt, UtxoId};
use std::{
convert::TryInto,
fmt::{Display, Formatter},
ops::Deref,
str::FromStr,
};
#[derive(Copy, Clone, Debug, derive_more::Into, derive_more::From)]
pub struct U64(pub u64);
#[Scalar(name = "U64")]
impl ScalarType for U64 {
fn parse(value: Value) -> InputValueResult<Self> {
if let Value::String(value) = &value {
let num: u64 = value.parse().map_err(InputValueError::custom)?;
Ok(U64(num))
} else {
Err(InputValueError::expected_type(value))
}
}
fn to_value(&self) -> Value {
Value::String(self.0.to_string())
}
}
impl From<BlockHeight> for U64 {
fn from(h: BlockHeight) -> Self {
U64(h.to_usize() as u64)
}
}
impl From<U64> for usize {
fn from(u: U64) -> Self {
u.0 as usize
}
}
#[derive(Clone, Debug)]
pub struct SortedTxCursor {
pub block_height: BlockHeight,
pub tx_id: HexString256,
}
impl SortedTxCursor {
pub fn new(block_height: BlockHeight, tx_id: HexString256) -> Self {
Self {
block_height,
tx_id,
}
}
}
impl CursorType for SortedTxCursor {
type Error = String;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
let (block_height, tx_id) = s.split_once('#').ok_or("Incorrect format provided")?;
Ok(Self::new(
usize::from_str(block_height)
.map_err(|_| "Failed to decode block_height")?
.into(),
HexString256::decode_cursor(tx_id)?,
))
}
fn encode_cursor(&self) -> String {
format!("{}#{}", self.block_height, self.tx_id)
}
}
#[derive(Clone, Debug, derive_more::Into, derive_more::From)]
pub struct HexString(pub(crate) Vec<u8>);
#[Scalar(name = "HexString")]
impl ScalarType for HexString {
fn parse(value: Value) -> InputValueResult<Self> {
if let Value::String(value) = &value {
HexString::from_str(value.as_str()).map_err(Into::into)
} else {
Err(InputValueError::expected_type(value))
}
}
fn to_value(&self) -> Value {
Value::String(self.to_string())
}
}
impl Display for HexString {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let s = format!("0x{}", hex::encode(&self.0));
s.fmt(f)
}
}
impl CursorType for HexString {
type Error = String;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
Self::from_str(s)
}
fn encode_cursor(&self) -> String {
self.to_string()
}
}
impl From<HexString> for OwnedTransactionIndexCursor {
fn from(string: HexString) -> Self {
string.0.into()
}
}
impl From<OwnedTransactionIndexCursor> for HexString {
fn from(cursor: OwnedTransactionIndexCursor) -> Self {
HexString(cursor.into())
}
}
impl FromStr for HexString {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = s.strip_prefix("0x").ok_or("expected 0x prefix")?;
let bytes = hex::decode(value).map_err(|e| e.to_string())?;
Ok(HexString(bytes))
}
}
#[derive(Copy, Clone, Debug)]
pub struct HexString256(pub(crate) [u8; 32]);
#[Scalar(name = "HexString256")]
impl ScalarType for HexString256 {
fn parse(value: Value) -> InputValueResult<Self> {
if let Value::String(value) = &value {
HexString256::from_str(value.as_str()).map_err(Into::into)
} else {
Err(InputValueError::expected_type(value))
}
}
fn to_value(&self) -> Value {
Value::String(self.to_string())
}
}
impl FromStr for HexString256 {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = s.strip_prefix("0x").ok_or("expected 0x prefix")?;
let mut bytes = ((value.len() / 2)..32).map(|_| 0).collect::<Vec<u8>>();
bytes.extend(hex::decode(value).map_err(|e| e.to_string())?);
let bytes: [u8; 32] = bytes
.try_into()
.map_err(|e: Vec<u8>| format!("expected 32 bytes, received {}", e.len()))?;
Ok(Self(bytes))
}
}
impl Display for HexString256 {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let s = format!("0x{}", hex::encode(self.0));
s.fmt(f)
}
}
impl From<HexString256> for Bytes32 {
fn from(s: HexString256) -> Self {
Bytes32::new(s.0)
}
}
impl From<HexString256> for Address {
fn from(a: HexString256) -> Self {
Address::new(a.0)
}
}
impl From<Bytes32> for HexString256 {
fn from(b: Bytes32) -> Self {
HexString256(*b.deref())
}
}
impl From<ContractId> for HexString256 {
fn from(c: ContractId) -> Self {
HexString256(*c.deref())
}
}
impl From<Color> for HexString256 {
fn from(c: Color) -> Self {
HexString256(*c.deref())
}
}
impl From<Address> for HexString256 {
fn from(a: Address) -> Self {
HexString256(*a.deref())
}
}
impl From<Salt> for HexString256 {
fn from(s: Salt) -> Self {
HexString256(*s.deref())
}
}
impl CursorType for HexString256 {
type Error = String;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
Self::from_str(s)
}
fn encode_cursor(&self) -> String {
self.to_string()
}
}
#[derive(Clone, Copy, Debug)]
pub struct HexStringUtxoId(pub(crate) UtxoId);
#[Scalar(name = "HexStringUtxoId")]
impl ScalarType for HexStringUtxoId {
fn parse(value: Value) -> InputValueResult<Self> {
if let Value::String(value) = &value {
HexStringUtxoId::from_str(value.as_str()).map_err(Into::into)
} else {
Err(InputValueError::expected_type(value))
}
}
fn to_value(&self) -> Value {
Value::String(self.to_string())
}
}
impl FromStr for HexStringUtxoId {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
UtxoId::from_str(s).map(Self).map_err(str::to_owned)
}
}
impl Display for HexStringUtxoId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let s = format!("{:#x}", self.0);
s.fmt(f)
}
}
impl From<HexStringUtxoId> for UtxoId {
fn from(s: HexStringUtxoId) -> Self {
s.0
}
}
impl From<UtxoId> for HexStringUtxoId {
fn from(utxo_id: UtxoId) -> Self {
Self(utxo_id)
}
}
impl CursorType for HexStringUtxoId {
type Error = String;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
Self::from_str(s)
}
fn encode_cursor(&self) -> String {
self.to_string()
}
}