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
//! The gateway_balances command calculates the total balances issued by a
//! given account, optionally excluding amounts held by operational addresses.
//!
//! <https://xrpl.org/gateway_balances>

use crate::{Request, RetrieveLedgerSpec, ReturnLedgerSpec, WithLedgerSpec};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use xrpl_types::Amount;

#[derive(Default, Debug, Clone, Serialize)]
pub struct GatewayBalancesRequest {
    /// The address to check. This should be the issuing address.
    pub account: String,
    /// An operational address to exclude from the balances issued, or an array
    /// of such addresses.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hotwallet: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub strict: Option<bool>,
    #[serde(flatten)]
    pub ledger_spec: RetrieveLedgerSpec,
}

impl Request for GatewayBalancesRequest {
    type Response = GatewayBalancesResponse;

    fn method(&self) -> String {
        "gateway_balances".to_owned()
    }
}

impl WithLedgerSpec for GatewayBalancesRequest {
    fn as_ledger_spec(&self) -> &RetrieveLedgerSpec {
        &self.ledger_spec
    }

    fn as_ledger_spec_mut(&mut self) -> &mut RetrieveLedgerSpec {
        &mut self.ledger_spec
    }
}

impl GatewayBalancesRequest {
    pub fn new(account: &str) -> Self {
        Self {
            account: account.to_owned(),
            ..Default::default()
        }
    }

    pub fn strict(self, strict: bool) -> Self {
        Self {
            strict: Some(strict),
            ..self
        }
    }

    // #TODO more builder methods
}

#[derive(Debug, Deserialize)]
pub struct GatewayBalancesResponse {
    /// The address of the account that issued the balances.
    pub account: String,
    pub obligations: Option<HashMap<String, String>>,
    pub balances: Option<HashMap<String, Vec<Amount>>>,
    pub assets: Option<HashMap<String, Vec<Amount>>>,
    #[serde(flatten)]
    pub ledger_spec: ReturnLedgerSpec,
}