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
pub mod account;
pub mod fee;
pub mod ledger;
pub mod submit;
pub mod channels;
pub mod tx;
pub mod subscribe;
use std::convert::{TryFrom, TryInto};
use std::num::ParseIntError;
use std::ops::Add;
use std::str::FromStr;
use rust_decimal::Decimal;
use serde;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Default, Clone)]
pub struct BigInt(pub u64);
impl From<u64> for BigInt {
fn from(v: u64) -> Self {
Self(v)
}
}
impl std::ops::Deref for BigInt {
type Target = u64;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Serialize for BigInt {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&format!("{}", self.0))
}
}
impl<'de> Deserialize<'de> for BigInt {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>,
{
deserializer.deserialize_str(BigIntVisitor)
}
}
struct BigIntVisitor;
impl<'de> serde::de::Visitor<'de> for BigIntVisitor {
type Value = BigInt;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("an unsigned integer")
}
fn visit_str<E>(self, value: &str) -> std::result::Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(BigInt(value.parse().map_err(|e| {
serde::de::Error::custom(format!("{:?}", e))
})?))
}
}
pub type Address = String;
pub type Marker = Value;
pub type H256 = String;
pub type RequestId = u64;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct LedgerInfo {
pub ledger_hash: Option<String>,
pub ledger_index: Option<u32>,
pub ledger_current_index: Option<i64>,
pub validated: Option<bool>,
}
fn from_str<'de, T, D>(deserializer: D) -> std::result::Result<Option<T>, D::Error>
where
T: FromStr,
T::Err: std::fmt::Display,
D: serde::de::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Ok(Some(T::from_str(&s).map_err(serde::de::Error::custom)?))
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct PaginationInfo {
pub limit: Option<i64>,
pub marker: Option<Marker>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Response<T> {
pub id: Option<RequestId>,
pub status: Option<String>,
pub r#type: Option<String>,
pub result: Result<T>,
pub warning: Option<String>,
pub warnings: Option<Vec<Value>>,
pub forwarded: Option<bool>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum Result<T> {
Ok(T),
Error(Value),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Error {
pub error: Option<String>,
}
#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct SignerList {
#[serde(rename = "SignerEntries")]
pub signer_entries: Vec<SignerEntry>,
#[serde(rename = "SignerQuorum")]
pub signer_quorum: u32,
}
#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct SignerEntry {
#[serde(rename = "Account")]
pub account: String,
#[serde(rename = "SignerWeight")]
pub signer_weight: u16,
}
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(untagged)]
pub enum CurrencyAmount {
XRP(BigInt),
IssuedCurrency(IssuedCurrencyAmount),
}
impl CurrencyAmount {
pub fn xrp(drops: u64) -> Self {
Self::XRP(BigInt(drops))
}
pub fn issued_currency(value: Decimal, currency: &str, issuer: &Address) -> Self {
Self::IssuedCurrency(IssuedCurrencyAmount {
value,
currency: currency.to_owned(),
issuer: issuer.to_owned(),
})
}
}
impl Default for CurrencyAmount {
fn default() -> Self {
Self::XRP(BigInt::default())
}
}
#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct IssuedCurrencyAmount {
pub value: Decimal,
pub currency: String,
pub issuer: Address,
}
#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct TransactionEntryRequest {
pub tx_hash: Option<String>,
pub ledger_index: Option<u64>,
pub ledger_hash: Option<String>,
}
#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct TransactionEntryResponse {
pub tx_json: Option<Value>,
pub ledger_index: Option<u64>,
pub ledger_hash: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(tag = "LedgerEntryType")]
pub enum LedgerEntry {
Unknown,
AccountRoot(AccountRoot),
Check(Check),
}
impl Default for LedgerEntry {
fn default() -> Self {
Self::Unknown
}
}
#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct AccountRoot {
pub account: Address,
pub balance: CurrencyAmount,
pub flags: u32,
pub owner_count: u32,
#[serde(rename = "PreviousTxnID")]
pub previous_txn_id: H256,
pub previous_txn_lgr_seq: u32,
pub sequence: u32,
pub account_txn_id: Option<H256>,
pub domain: Option<String>,
pub email_hash: Option<H256>,
pub message_key: Option<String>,
pub regular_key: Option<String>,
pub ticket_count: Option<u32>,
pub tick_size: Option<u8>,
pub transfer_rate: Option<u32>,
}
#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct Check {
pub account: Address,
pub destination: Address,
pub flags: u32,
}