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
use super::utils::http_get;
use crate::{error::Result, Fees, Market, MarketType, Precision, QuantityLimit};
use serde::{Deserialize, Serialize};
pub(crate) fn fetch_symbols(market_type: MarketType) -> Result<Vec<String>> {
match market_type {
MarketType::Spot => fetch_spot_symbols(),
_ => panic!("Unsupported market_type: {}", market_type),
}
}
pub(crate) fn fetch_markets(market_type: MarketType) -> Result<Vec<Market>> {
match market_type {
MarketType::Spot => fetch_spot_markets(),
_ => panic!("Unsupported market_type: {}", market_type),
}
}
#[derive(Serialize, Deserialize)]
struct SpotMarket {
id: String,
base_currency: String,
quote_currency: String,
base_min_size: String,
base_max_size: String,
quote_increment: String,
base_increment: String,
display_name: String,
min_market_funds: String,
max_market_funds: String,
margin_enabled: bool,
post_only: bool,
limit_only: bool,
cancel_only: bool,
trading_disabled: bool,
status: String,
status_message: String,
}
fn fetch_spot_markets_raw() -> Result<Vec<SpotMarket>> {
let txt = http_get("https://api.pro.coinbase.com/products", None)?;
let markets = serde_json::from_str::<Vec<SpotMarket>>(&txt)?;
Ok(markets)
}
fn fetch_spot_symbols() -> Result<Vec<String>> {
let symbols = fetch_spot_markets_raw()?
.into_iter()
.filter(|m| !m.trading_disabled && m.status == "online" && !m.cancel_only)
.map(|m| m.id)
.collect::<Vec<String>>();
Ok(symbols)
}
fn fetch_spot_markets() -> Result<Vec<Market>> {
let markets = fetch_spot_markets_raw()?
.into_iter()
.map(|m| {
let info = serde_json::to_value(&m)
.unwrap()
.as_object()
.unwrap()
.clone();
let pair = crypto_pair::normalize_pair(&m.id, "coinbase_pro").unwrap();
let (base, quote) = {
let v: Vec<&str> = pair.split('/').collect();
(v[0].to_string(), v[1].to_string())
};
Market {
exchange: "coinbase_pro".to_string(),
market_type: MarketType::Spot,
symbol: m.id,
base_id: m.base_currency,
quote_id: m.quote_currency,
settle_id: None,
base,
quote,
settle: None,
active: !m.trading_disabled && m.status == "online" && !m.cancel_only,
margin: m.margin_enabled,
fees: Fees {
maker: 0.005,
taker: 0.005,
},
precision: Precision {
tick_size: m.quote_increment.parse::<f64>().unwrap(),
lot_size: m.base_increment.parse::<f64>().unwrap(),
},
quantity_limit: Some(QuantityLimit {
min: m.base_min_size.parse::<f64>().unwrap(),
max: Some(m.base_max_size.parse::<f64>().unwrap()),
}),
contract_value: None,
delivery_date: None,
info,
}
})
.collect::<Vec<Market>>();
Ok(markets)
}