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
//! # Coinchecker
//!
//! An unofficial Rust Library for the Coincheck REST API.
//!
//!
//! ## Disclaimer
//!
//! Use at your own risk. (自己責任で使ってください)
//!
//! - Some APIs not implemented
//! - Some APIs not well tested
//!
//!
//! ## APIs
//!
//! - [Public]
//! - [Private]
//!     - [Account]
//!     - [Order]
//!     - [WithdrawsJpy]
//!
//!
//! ## Usage
//!
//! If you use the private API, set the access key in the environment variable.
//! You can use the .env file.
//!
//! ```text:.env
//! # .env file
//! COINCHECK_ACCESS_KEY=hogehoge
//! COINCHECK_SECRET_KEY=fugafuga
//! ```
//!
//! Use like this.
//!
//! ```rust
//! use anyhow::Result;
//! use coinchecker::Coincheck;
//! use coinchecker::types::CoinPair;
//! use coinchecker::utils::quick_debug;
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//!     // Private and Public API
//!     let mut coincheck = Coincheck::new_with_env_keys();
//!     quick_debug(coincheck.public.trades(&CoinPair::BtcJpy)).await;
//!     quick_debug(coincheck.private.account.balance()).await;
//!
//!     // Public API only
//!     let mut coincheck = Coincheck::new_without_keys();
//!     quick_debug(coincheck.public.ticker()).await;
//!
//!     Ok(())
//! }
//! ```

pub mod client;
pub mod private;
pub mod public;
pub mod types;
pub mod utils;

use client::SharedClient;

use crate::client::Client;
use crate::private::account::Account;
use crate::private::order::Order;
use crate::private::withdraws_jpy::WithdrawsJpy;
use crate::private::Private;
use crate::public::Public;
use std::time::Instant;

/// A client for the Coincheck REST API.
pub struct Coincheck {
    pub public: Public,
    pub private: Private,
    client: SharedClient,
}

/// A client for the Coincheck REST API. Without API keys. Public API only.
pub struct CoincheckNoAuth {
    pub public: Public,
    client: SharedClient,
}

impl Coincheck {
    pub const ENV_ACCESS_KEY: &'static str = "COINCHECK_ACCESS_KEY";
    pub const ENV_SECRET_KEY: &'static str = "COINCHECK_SECRET_KEY";

    /// Create a new instance.
    pub fn new_with_keys(access_key: &str, secret_key: &str) -> Coincheck {
        let client = Client::shared_new(access_key.to_owned().into(), secret_key.to_owned().into());
        let public = Public::new(client.clone());

        let private = Private {
            order: Order::new(client.clone()),
            account: Account::new(client.clone()),
            withdraws_jpy: WithdrawsJpy::new(client.clone()),
        };

        Coincheck {
            public,
            private,
            client,
        }
    }

    /// Create a new instance. Use the authentication key from the environment variables (needs `COINCHECK_ACCESS_KEY` and `COINCHECK_SECRET_KEY`).
    pub fn new_with_env_keys() -> Coincheck {
        dotenv::dotenv().ok();

        let access_key = std::env::var(Self::ENV_ACCESS_KEY)
            .unwrap_or_else(|_| panic!("{} must be set", Self::ENV_ACCESS_KEY));
        let secret_key = std::env::var(Self::ENV_SECRET_KEY)
            .unwrap_or_else(|_| panic!("{} must be set", Self::ENV_SECRET_KEY));

        Coincheck::new_with_keys(&access_key, &secret_key)
    }

    /// Create a new instance without authentication keys. Only public APIs can be used.
    pub fn new_without_keys() -> CoincheckNoAuth {
        let client = Client::shared_new(None, None);
        let public = Public::new(client.clone());

        CoincheckNoAuth { public, client }
    }
}

trait GetLastRequestTime {
    /// Get the last requset time.
    fn last_request_time(&self) -> Instant;
}

impl GetLastRequestTime for Coincheck {
    fn last_request_time(&self) -> Instant {
        self.client.borrow().last_request_time
    }
}

impl GetLastRequestTime for CoincheckNoAuth {
    fn last_request_time(&self) -> Instant {
        self.client.borrow().last_request_time
    }
}

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

    #[test]
    fn create_coincheck_instance() {
        let _ = Coincheck::new_with_keys("hoge", "fuga");
        let _ = Coincheck::new_with_env_keys();
        let _ = Coincheck::new_without_keys();
    }
}