1pub mod account;
2pub mod fee;
3pub mod ledger;
4pub mod submit;
5pub mod channels;
6pub mod tx;
7pub mod subscribe;
8
9use std::convert::{TryFrom, TryInto};
10use std::num::ParseIntError;
11use std::ops::Add;
12use std::str::FromStr;
13
14use rust_decimal::Decimal;
15use serde;
16use serde::{Deserialize, Serialize};
17use serde_json::Value;
18use serde_with::skip_serializing_none;
19
20#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Default, Clone)]
21pub struct BigInt(pub u64);
22
23impl From<u64> for BigInt {
24 fn from(v: u64) -> Self {
25 Self(v)
26 }
27}
28
29impl std::ops::Deref for BigInt {
30 type Target = u64;
31
32 fn deref(&self) -> &Self::Target {
33 &self.0
34 }
35}
36
37impl Serialize for BigInt {
38 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
39 where
40 S: serde::Serializer,
41 {
42 serializer.serialize_str(&format!("{}", self.0))
43 }
44}
45
46impl<'de> Deserialize<'de> for BigInt {
47 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
48 where
49 D: serde::de::Deserializer<'de>,
50 {
51 deserializer.deserialize_str(BigIntVisitor)
52 }
53}
54
55struct BigIntVisitor;
56
57impl<'de> serde::de::Visitor<'de> for BigIntVisitor {
58 type Value = BigInt;
59
60 fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
61 formatter.write_str("an unsigned integer")
62 }
63
64 fn visit_str<E>(self, value: &str) -> std::result::Result<Self::Value, E>
65 where
66 E: serde::de::Error,
67 {
68 Ok(BigInt(value.parse().map_err(|e| {
69 serde::de::Error::custom(format!("{:?}", e))
70 })?))
71 }
72}
73
74pub type Address = String;
76
77pub type Marker = Value;
79
80pub type H256 = String;
81
82pub type RequestId = u64;
86
87#[skip_serializing_none]
88#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
89pub struct LedgerInfo {
90 pub ledger_hash: Option<String>,
92 pub ledger_index: Option<Integer>,
94 pub ledger_current_index: Option<i64>,
96 pub validated: Option<bool>,
98}
99
100#[derive(Debug, Clone, PartialEq, Eq, Default)]
101pub struct Integer(pub u32);
102
103impl Serialize for Integer {
104 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
105 where
106 S: serde::Serializer,
107 {
108 serializer.serialize_str(&format!("{}", self.0))
109 }
110}
111
112impl<'de> Deserialize<'de> for Integer {
113 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
114 where
115 D: serde::de::Deserializer<'de>,
116 {
117 let v = Value::deserialize(deserializer)?;
118 let n = v
119 .as_u64()
120 .or_else(|| v.as_str().and_then(|s| s.parse().ok()))
121 .ok_or_else(|| serde::de::Error::custom("non-integer"))?
122 .try_into()
123 .map_err(|_| serde::de::Error::custom("overflow"))?;
124 Ok(Integer(n))
125 }
126}
127
128#[skip_serializing_none]
129#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
130pub struct PaginationInfo {
131 pub limit: Option<i64>,
133 pub marker: Option<Marker>,
135}
136
137#[derive(Serialize, Deserialize, Debug, Clone)]
138pub struct JsonRPCResponse<T> {
139 pub result: JsonRPCResponseResult<T>,
140}
141
142#[derive(Serialize, Deserialize, Debug, Clone)]
143#[serde(tag = "status")]
144pub enum JsonRPCResponseResult<T> {
145 #[serde(rename = "success")]
146 Success(JsonRPCSuccessResponse<T>),
147 #[serde(rename = "error")]
148 Error(ErrorResponse),
149}
150
151
152#[derive(Serialize, Deserialize, Debug, Clone)]
153#[serde(tag = "status")]
154pub enum WebsocketResponse<T> {
155 #[serde(rename = "success")]
156 Success(WebsocketSuccessResponse<T>),
157 #[serde(rename = "error")]
158 Error(ErrorResponse),
159}
160
161impl<T> WebsocketResponse<T> {
162 pub fn get_id(&self) -> Option<u64> {
163 match self {
164 Self::Success(res) => Some(res.id.to_owned()),
165 Self::Error(res) => res.id.to_owned(),
166 }
167 }
168}
169
170#[derive(Serialize, Deserialize, Debug, Clone)]
171pub struct WebsocketSuccessResponse<T> {
172 pub id: RequestId,
174 pub r#type: Option<String>,
176 pub result: T,
178 pub warning: Option<String>,
180 pub warnings: Option<Vec<Value>>,
183 pub forwarded: Option<bool>,
185}
186
187#[derive(Serialize, Deserialize, Debug, Clone)]
188pub struct ErrorResponse {
189 pub id: Option<RequestId>,
190 pub r#type: Option<String>,
191 pub error: Option<String>,
192}
193
194
195#[derive(Serialize, Deserialize, Debug, Clone)]
196pub struct JsonRPCSuccessResponse<T> {
197 #[serde(flatten)]
199 pub result: T,
200 pub warning: Option<String>,
202 pub warnings: Option<Vec<Value>>,
205 pub forwarded: Option<bool>,
207}
208
209#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
210pub struct SignerList {
211 #[serde(rename = "SignerEntries")]
212 pub signer_entries: Vec<SignerEntry>,
213 #[serde(rename = "SignerQuorum")]
214 pub signer_quorum: u32,
215}
216
217#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
218pub struct SignerEntry {
219 #[serde(rename = "Account")]
220 pub account: String,
221 #[serde(rename = "SignerWeight")]
222 pub signer_weight: u16,
223}
224
225#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
226#[serde(untagged)]
227pub enum CurrencyAmount {
228 XRP(BigInt),
229 IssuedCurrency(IssuedCurrencyAmount),
230}
231
232impl CurrencyAmount {
233 pub fn xrp(drops: u64) -> Self {
234 Self::XRP(BigInt(drops))
235 }
236 pub fn issued_currency(value: Decimal, currency: &str, issuer: &Address) -> Self {
237 Self::IssuedCurrency(IssuedCurrencyAmount {
238 value,
239 currency: currency.to_owned(),
240 issuer: issuer.to_owned(),
241 })
242 }
243}
244
245impl Default for CurrencyAmount {
246 fn default() -> Self {
247 Self::XRP(BigInt::default())
248 }
249}
250
251#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
252pub struct IssuedCurrencyAmount {
253 pub value: Decimal,
254 pub currency: String,
255 pub issuer: Address,
256}
257
258#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
259pub struct TransactionEntryRequest {
260 pub tx_hash: Option<String>,
261 pub ledger_index: Option<u64>,
262 pub ledger_hash: Option<String>,
263}
264
265#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
266pub struct TransactionEntryResponse {
267 pub tx_json: Option<Value>,
268 pub ledger_index: Option<u64>,
269 pub ledger_hash: Option<String>,
270}
271
272#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
273#[serde(tag = "LedgerEntryType")]
274pub enum LedgerEntry {
275 Unknown,
276 AccountRoot(AccountRoot),
277 Check(Check),
278}
279
280impl Default for LedgerEntry {
281 fn default() -> Self {
282 Self::Unknown
283 }
284}
285
286#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
287#[serde(rename_all = "PascalCase")]
288pub struct AccountRoot {
289 pub account: Address,
291 pub balance: CurrencyAmount,
293 pub flags: u32,
295 pub owner_count: u32,
297 #[serde(rename = "PreviousTxnID")]
299 pub previous_txn_id: H256,
300 pub previous_txn_lgr_seq: u32,
302 pub sequence: u32,
304 pub account_txn_id: Option<H256>,
306 pub domain: Option<String>,
308 pub email_hash: Option<H256>,
310 pub message_key: Option<String>,
312 pub regular_key: Option<String>,
314 pub ticket_count: Option<u32>,
316 pub tick_size: Option<u8>,
318 pub transfer_rate: Option<u32>,
320}
321
322#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
323#[serde(rename_all = "PascalCase")]
324pub struct Check {
325 pub account: Address,
327 pub destination: Address,
329 pub flags: u32,
331}