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
use chrono::prelude::*;
use resources::Amount;

/// A ledger represents the state of the Stellar universe at a given point in time. It contains the list of all the accounts and balances, all the orders in the distributed exchange, and any other data that persists.
/// The first ledger in the history of the network is called the genesis ledger.
#[derive(Debug, Deserialize, Clone)]
pub struct Ledger {
    id: String,
    paging_token: String,
    hash: String,
    sequence: u32,
    transaction_count: i64,
    operation_count: i64,
    closed_at: DateTime<Utc>,
    total_coins: Amount,
    fee_pool: Amount,
    base_fee_in_stroops: i64,
    base_reserve_in_stroops: i64,
    max_tx_set_size: u32,
    protocol_version: u32,
}

impl Ledger {
    /// The unique identifier for this ledger
    pub fn id(&self) -> &String {
        &self.id
    }
    /// A paging token suitable for use as the cursor parameter to ledger collection
    /// resources.
    pub fn paging_token(&self) -> &String {
        &self.paging_token
    }

    /// A hex-encoded SHA-256 hash of the ledger’s XDR-encoded form.
    pub fn hash(&self) -> &String {
        &self.hash
    }

    /// Sequence number of this ledger, suitable for use as the as the :id parameter for url templates that require a ledger number.
    pub fn sequence(&self) -> u32 {
        self.sequence
    }

    /// The number of transactions in this ledger.
    pub fn transaction_count(&self) -> i64 {
        self.transaction_count
    }

    /// The number of operations in this ledger.
    pub fn operation_count(&self) -> i64 {
        self.operation_count
    }

    /// An ISO 8601 formatted string of when this ledger was closed.
    pub fn closed_at(&self) -> DateTime<Utc> {
        self.closed_at
    }

    /// The total number of lumens in circulation.
    pub fn total_coins(&self) -> Amount {
        self.total_coins
    }

    /// The sum of all transaction fees (in lumens) since the last inflation operation. They are redistributed during inflation.
    pub fn fee_pool(&self) -> Amount {
        self.fee_pool
    }

    /// The fee the network charges per operation in a transaction.
    pub fn base_fee_in_stroops(&self) -> i64 {
        self.base_fee_in_stroops
    }

    /// The fee the network charges per operation in a transaction as an amount asset.
    pub fn base_fee_as_amount(&self) -> Amount {
        Amount::new(self.base_fee_in_stroops)
    }

    /// The reserve the network uses when calculating an account’s minimum balance.
    pub fn base_reserve_in_stroops(&self) -> i64 {
        self.base_reserve_in_stroops
    }

    /// The reserve the network uses when calculating an account’s minimum balance as an amount asset.
    pub fn base_reserve_as_amount(&self) -> Amount {
        Amount::new(self.base_reserve_in_stroops)
    }

    /// The maximum number of transactions validators have agreed to process in a given ledger.
    pub fn max_tx_set_size(&self) -> u32 {
        self.max_tx_set_size
    }

    /// The protocol version that the stellar network was running when this ledger was committed.
    pub fn protocol_version(&self) -> u32 {
        self.protocol_version
    }
}

#[cfg(test)]
mod ledger_tests {
    use super::*;
    use serde_json;

    fn ledger_json() -> &'static str {
        include_str!("../../fixtures/ledger.json")
    }

    #[test]
    fn it_parses_into_a_transaction() {
        let ledger: Ledger = serde_json::from_str(&ledger_json()).unwrap();
        assert_eq!(
            ledger.id(),
            "eee9e6e02899365ecae4c37e52db7d99e2d130baf4ec1856d311bb546df1d0ad"
        );
        assert_eq!(ledger.paging_token(), "300042120331264");
        assert_eq!(
            ledger.hash(),
            "eee9e6e02899365ecae4c37e52db7d99e2d130baf4ec1856d311bb546df1d0ad"
        );
        assert_eq!(ledger.sequence(), 69859);
        assert_eq!(ledger.transaction_count(), 0);
        assert_eq!(ledger.operation_count(), 0);
        assert_eq!(ledger.closed_at(), Utc.ymd(2017, 3, 23).and_hms(20, 13, 23));
        assert_eq!(ledger.total_coins(), Amount::new(1_000_000_000_000_000_000));
        assert_eq!(ledger.fee_pool(), Amount::new(18_000_080_200));
        assert_eq!(ledger.base_fee_in_stroops(), 100);
        assert_eq!(ledger.base_reserve_in_stroops(), 100000000);
        assert_eq!(ledger.max_tx_set_size(), 50);
        assert_eq!(ledger.protocol_version(), 4);
    }
}