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
use crate::database::{Database, KvStoreError};
use crate::model::coin::{Coin as CoinModel, CoinStatus};
use crate::schema::scalars::{HexString256, U64};
use crate::state::IterDirection;
use async_graphql::InputObject;
use async_graphql::{
connection::{query, Connection, Edge, EmptyFields},
Context, Object,
};
use fuel_storage::Storage;
use fuel_tx::{Address, UtxoId};
use itertools::Itertools;
use super::scalars::HexStringUtxoId;
pub struct Coin(UtxoId, CoinModel);
#[Object]
impl Coin {
async fn utxo_id(&self) -> HexStringUtxoId {
self.0.into()
}
async fn owner(&self) -> HexString256 {
self.1.owner.into()
}
async fn amount(&self) -> U64 {
self.1.amount.into()
}
async fn color(&self) -> HexString256 {
self.1.color.into()
}
async fn maturity(&self) -> U64 {
self.1.maturity.into()
}
async fn status(&self) -> CoinStatus {
self.1.status
}
async fn block_created(&self) -> U64 {
self.1.block_created.into()
}
}
#[derive(InputObject)]
struct CoinFilterInput {
owner: HexString256,
color: Option<HexString256>,
}
#[derive(Default)]
pub struct CoinQuery;
#[Object]
impl CoinQuery {
async fn coin(
&self,
ctx: &Context<'_>,
#[graphql(desc = "utxo_id of the coin")] utxo_id: HexStringUtxoId,
) -> async_graphql::Result<Option<Coin>> {
let utxo_id = utxo_id.0;
let db = ctx.data_unchecked::<Database>().clone();
let block = Storage::<UtxoId, CoinModel>::get(&db, &utxo_id)?
.map(|coin| Coin(utxo_id, coin.into_owned()));
Ok(block)
}
async fn coins(
&self,
ctx: &Context<'_>,
after: Option<String>,
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
filter: CoinFilterInput,
) -> async_graphql::Result<Connection<HexStringUtxoId, Coin, EmptyFields, EmptyFields>> {
let db = ctx.data_unchecked::<Database>();
query(
after,
before,
first,
last,
|after: Option<HexStringUtxoId>, before: Option<HexStringUtxoId>, first, last| async move {
let (records_to_fetch, direction) = if let Some(first) = first {
(first, IterDirection::Forward)
} else if let Some(last) = last {
(last, IterDirection::Reverse)
} else {
(0, IterDirection::Forward)
};
let after = after.map(UtxoId::from);
let before = before.map(UtxoId::from);
let start;
let end;
if direction == IterDirection::Forward {
start = after;
end = before;
} else {
start = before;
end = after;
}
let owner: Address = filter.owner.into();
let mut coin_ids = db.owned_coins(owner, start, Some(direction));
let mut started = None;
if start.is_some() {
started = coin_ids.next();
}
let coins = coin_ids
.take_while(|r| {
if let (Ok(t), Some(end)) = (r, end.as_ref()) {
if *t == *end {
return false;
}
}
true
})
.take(records_to_fetch);
let mut coins: Vec<UtxoId> = coins.try_collect()?;
if direction == IterDirection::Reverse {
coins.reverse();
}
let coins: Vec<Coin> = coins
.into_iter()
.map(|id| {
Storage::<UtxoId, CoinModel>::get(db, &id)
.transpose()
.ok_or(KvStoreError::NotFound)?
.map(|coin| Coin(id, coin.into_owned()))
})
.try_collect()?;
let mut coins = coins;
if let Some(color) = filter.color {
coins.retain(|coin| coin.1.color == color.0.into());
}
coins.retain(|coin| coin.1.status == CoinStatus::Unspent);
let mut connection =
Connection::new(started.is_some(), records_to_fetch <= coins.len());
connection.append(
coins
.into_iter()
.map(|item| Edge::new(HexStringUtxoId::from(item.0), item)),
);
Ok(connection)
},
)
.await
}
}