cxmr_api/
lib.rs

1//! Crypto-bank exchanges API primitives.
2
3#[macro_use]
4extern crate serde_derive;
5extern crate async_trait;
6extern crate serde;
7
8extern crate cxmr_currency;
9extern crate cxmr_exchanges;
10
11use std::sync::Arc;
12
13use async_trait::async_trait;
14
15use cxmr_currency::CurrencyPair;
16use cxmr_exchanges::{
17    AccountInfo, Exchange, ExchangeInfo, ExchangeOrder, MarketOrder, OrderExecution,
18};
19
20/// Exchange account configuration.
21#[derive(Deserialize, Serialize, Clone)]
22pub struct Account {
23    /// Human readable unique name of account.
24    pub name: String,
25    /// When false automatic trading is disabled.
26    pub enabled: bool,
27    /// Exchange identifier of account.
28    pub exchange: Exchange,
29    /// Exchange API credentials.
30    pub credentials: Credentials,
31}
32
33impl Account {
34    pub fn new(key: String, secret: String, exchange: Exchange) -> Self {
35        let mut name = key.clone();
36        name.truncate(3);
37        Account {
38            name: name,
39            enabled: false,
40            exchange: exchange,
41            credentials: Credentials::new(key, secret),
42        }
43    }
44}
45
46/// Exchange account credentials.
47#[derive(Deserialize, Serialize, Clone, Default)]
48pub struct Credentials {
49    pub key: String,
50    pub secret: String,
51}
52
53impl Credentials {
54    pub fn new(key: String, secret: String) -> Self {
55        Credentials {
56            key: key,
57            secret: secret,
58        }
59    }
60}
61
62/// Exchange API private client struct.
63#[async_trait]
64pub trait PrivateClient: Send + Sync {
65    type Error;
66
67    fn account(&self) -> &Account;
68
69    /// Returns static exchange identifier.
70    fn exchange(&self) -> &'static Exchange;
71
72    /// Requests account information.
73    async fn account_info(&self) -> Result<AccountInfo, Self::Error>;
74
75    /// Requests all open orders.
76    async fn open_orders(&self, pair: CurrencyPair) -> Result<Vec<MarketOrder>, Self::Error>;
77
78    /// Creates an order.
79    async fn create_order(&self, order: &ExchangeOrder) -> Result<OrderExecution, Self::Error>;
80
81    /// Renews key for user event data stream.
82    async fn user_data_stream(&self, key: Option<String>) -> Result<String, Self::Error>;
83}
84
85/// Exchange API public client struct.
86#[async_trait]
87pub trait PublicClient: Send + Sync {
88    type Error;
89
90    /// Returns static exchange identifier.
91    fn exchange(&self) -> &'static Exchange;
92
93    /// Requests exchange information.
94    async fn exchange_info(&self) -> Result<ExchangeInfo, Self::Error>;
95}
96
97/// Shared public client type alias.
98pub type SharedPublicClient<E> = Arc<dyn PublicClient<Error = E>>;
99
100/// Shared private client type alias.
101pub type SharedPrivateClient<E> = Arc<dyn PrivateClient<Error = E>>;