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
use crate::rpc::*;
use crate::serde::ts_seconds;
use bigdecimal::BigDecimal;
use serde::*;

rpc_interface! {
    trait GolemPay {
        #[rpc_uri = "pay.incomes"]
        fn get_incomes_list(&self) -> Result<Vec<Income>>;

        //
        #[rpc_uri = "pay.payments"]
        fn get_payments_list(&self, #[kwarg] _num: Option<u32>, #[kwarg] _last_seconds: Option<u32>) -> Result<Vec<Payment>>;

        //
        // TODO: kwargs limit=1000, offset=0
        #[rpc_uri = "pay.deposit_payments"]
        fn get_deposit_payments_list(&self, #[kwarg] _limit : Option<usize>, #[kwarg] _offset : Option<usize>) -> Result<Option<Vec<DepositPayment>>>;

        #[rpc_uri = "pay.deposit_balance"]
        fn get_deposit_balance(&self) -> Result<Option<DepositBalance>>;

        #[rpc_uri = "pay.balance"]
        fn get_pay_balance(&self) -> Result<Balance>;

        #[rpc_uri = "pay.ident"]
        fn get_pay_ident(&self) -> Result<String>;
    }

    converter AsGolemPay as_golem_pay;
}

/// The status of a payment.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Ord, PartialOrd, PartialEq, Eq)]
#[repr(u8)]
#[serde(rename_all = "lowercase")]
pub enum PaymentStatus {
    /// Created but not introduced to the payment network.
    Awaiting = 1,

    /// Sent to the payment network.
    Sent = 2,

    /// Confirmed on the payment network.
    Confirmed = 3,

    /// Not confirmed on the payment network, but expected to be.
    Overdue = 4,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Income {
    pub subtask: String,
    pub payer: String,
    pub value: BigDecimal,
    // status
    pub status: PaymentStatus,
    pub transaction: Option<String>,
    #[serde(with = "ts_seconds")]
    pub created: chrono::DateTime<chrono::Utc>,
    #[serde(with = "ts_seconds")]
    pub modified: chrono::DateTime<chrono::Utc>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Payment {
    pub value: BigDecimal,
    pub fee: Option<BigDecimal>,
    pub subtask: String,
    pub payee: String,
    pub status: PaymentStatus,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DepositPayment {
    pub value: BigDecimal,
    pub status: PaymentStatus,
    pub fee: Option<BigDecimal>,
    pub transaction: String,
    pub created: chrono::DateTime<chrono::Utc>,
    pub modified: Option<chrono::DateTime<chrono::Utc>>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "lowercase")]
pub enum DepositStatus {
    Locked,
    Unlocking,
    Unlocked,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DepositBalance {
    pub status: DepositStatus,
    pub timelock: BigDecimal,
    #[serde(rename = "value")]
    pub balance: BigDecimal,
}

#[derive(Deserialize, Serialize)]
pub struct Balance {
    #[serde(default)]
    pub eth: BigDecimal,
    #[serde(default)]
    pub eth_lock: BigDecimal,
    #[serde(default)]
    pub av_gnt: BigDecimal,
    #[serde(default)]
    pub gnt_lock: BigDecimal,
    #[serde(default)]
    pub gnt_nonconverted: BigDecimal,
}

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

    #[test]
    fn test_parse_income() {
        let str = r#"{
            "subtask": "1e12a7e4-50a3-11e9-9521-1bac4bb5328e",
            "payer": "1bac4bb5328ec9fc70b336e7c720dbb0c9cd23b8782c8bfc66f2a946ab036b8b6b71775cb04ad08d606f652ba476d9692c77cbd135f534d68f54c12e3edc039a",
            "value": "33333333333333334",
            "status": "awaiting",
            "transaction": null,
            "created": 1553699819.286437,
            "modified": 1557242132.961853
        }"#;

        let income: Income = serde_json::from_str(str).unwrap();

        eprintln!("income = {:?}", income);
    }
}