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
use crate::client::SharedClient;
use crate::types::*;
use anyhow::Result;
use reqwest::Method;

/// Private API - Account
///
/// 自分のアカウントの残高や、各種情報を取得することができます。
///
/// <https://coincheck.com/ja/documents/exchange/api#account>
pub struct Account {
    client: SharedClient,
}

pub mod model {
    use crate::types::*;
    use chrono::{DateTime, Utc};
    use serde::{Deserialize, Serialize};
    use serde_with::{serde_as, DisplayFromStr};
    use std::collections::HashMap;

    /// 残高
    #[serde_as]
    #[derive(Debug, Serialize, Deserialize)]
    pub struct Balance {
        pub success: bool,
        #[serde_as(as = "DisplayFromStr")]
        pub jpy: PriceType,
        #[serde_as(as = "DisplayFromStr")]
        pub btc: PriceType,
        #[serde_as(as = "DisplayFromStr")]
        pub jpy_reserved: PriceType,
        #[serde_as(as = "DisplayFromStr")]
        pub btc_reserved: PriceType,
        #[serde_as(as = "DisplayFromStr")]
        pub jpy_lend_in_use: PriceType,
        #[serde_as(as = "DisplayFromStr")]
        pub btc_lend_in_use: PriceType,
        #[serde_as(as = "DisplayFromStr")]
        pub jpy_lent: PriceType,
        #[serde_as(as = "DisplayFromStr")]
        pub btc_lent: PriceType,
        #[serde_as(as = "DisplayFromStr")]
        pub jpy_debt: PriceType,
        #[serde_as(as = "DisplayFromStr")]
        pub btc_debt: PriceType,
    }

    /// 送金履歴
    #[derive(Debug, Serialize, Deserialize)]
    pub struct SendHistory {
        pub success: bool,
        pub sends: Vec<SendRecord>,
    }

    /// 送金履歴のレコード
    #[serde_as]
    #[derive(Debug, Serialize, Deserialize)]
    pub struct SendRecord {
        pub id: IdType,
        #[serde_as(as = "DisplayFromStr")]
        pub amount: PriceType,
        pub currency: String,
        #[serde_as(as = "DisplayFromStr")]
        pub fee: PriceType,
        pub address: String,
        pub created_at: DateTime<Utc>,
    }

    /// 受け取り履歴
    #[derive(Debug, Serialize, Deserialize)]
    pub struct DepositHistory {
        pub success: bool,
        pub deposits: Vec<DepositRecord>,
    }

    /// 受け取り履歴のレコード
    #[serde_as]
    #[derive(Debug, Serialize, Deserialize)]
    pub struct DepositRecord {
        pub id: IdType,
        #[serde_as(as = "DisplayFromStr")]
        pub amount: PriceType,
        pub currency: String,
        pub address: String,
        pub status: String,
        pub confirmed_at: String,
        pub created_at: DateTime<Utc>,
    }

    /// アカウント情報
    #[serde_as]
    #[derive(Debug, Serialize, Deserialize)]
    pub struct Account {
        pub success: bool,
        pub id: IdType,
        pub email: String,
        pub identity_status: String,
        pub bitcoin_address: String,
        #[serde_as(as = "DisplayFromStr")]
        pub taker_fee: PriceType,
        #[serde_as(as = "DisplayFromStr")]
        pub maker_fee: PriceType,
        pub exchange_fees: HashMap<String, Fee>,
    }

    /// 手数料
    #[serde_as]
    #[derive(Debug, Serialize, Deserialize)]
    pub struct Fee {
        #[serde_as(as = "DisplayFromStr")]
        pub taker_fee: PriceType,
        #[serde_as(as = "DisplayFromStr")]
        pub maker_fee: PriceType,
    }
}

impl Account {
    pub fn new(client: SharedClient) -> Self {
        Self { client }
    }

    const USE_AUTH: bool = true;

    /// 残高
    ///
    /// アカウントの残高を確認できます。
    /// jpy, btc には未決済の注文に利用している jpy_reserved, btc_reserved は含まれていません。
    ///
    /// <https://coincheck.com/ja/documents/exchange/api#account-balance>
    pub async fn balance(&mut self) -> Result<model::Balance> {
        self.client
            .borrow_mut()
            .request_and_get_json(Method::GET, "/api/accounts/balance", None, Self::USE_AUTH)
            .await
    }

    // TODO: implement ビットコインの送金 POST /api/send_money
    // https://coincheck.com/ja/documents/exchange/api#account-sendmoney

    /// 送金履歴
    ///
    /// ビットコインの送金履歴です。
    ///
    /// <https://coincheck.com/ja/documents/exchange/api#account-sends>
    pub async fn sends(&mut self) -> Result<model::SendHistory> {
        let mut params = Params::new();
        params.insert("currency", Currency::Btc.as_str());
        self.client
            .borrow_mut()
            .request_and_get_json(
                Method::GET,
                "/api/send_money",
                Some(&params),
                Self::USE_AUTH,
            )
            .await
    }

    /// 受け取り履歴
    ///
    /// ビットコインの受け取り履歴です。
    ///
    /// <https://coincheck.com/ja/documents/exchange/api#account-deposits>
    pub async fn deposits(&mut self) -> Result<model::DepositHistory> {
        let mut params = Params::new();
        params.insert("currency", Currency::Btc.as_str());
        self.client
            .borrow_mut()
            .request_and_get_json(
                Method::GET,
                "/api/deposit_money",
                Some(&params),
                Self::USE_AUTH,
            )
            .await
    }

    /// アカウント情報
    ///
    /// アカウントの情報を表示します。
    ///
    /// <https://coincheck.com/ja/documents/exchange/api#account-info>
    pub async fn info(&mut self) -> Result<model::Account> {
        self.client
            .borrow_mut()
            .request_and_get_json(Method::GET, "/api/accounts", None, Self::USE_AUTH)
            .await
    }
}

#[cfg(test)]
mod tests {
    use crate::Coincheck;

    #[tokio::test]
    #[serial_test::serial]
    async fn private_account_api() {
        let mut coincheck = Coincheck::new_with_env_keys();
        let api = &mut coincheck.private.account;

        assert!(api.balance().await.is_ok());
        assert!(api.sends().await.is_ok());
        assert!(api.deposits().await.is_ok());
        assert!(api.info().await.is_ok());
    }
}