use crate::binance::Binance;
use crate::gate::Gate;
use crate::http::{self, Http};
use crate::keys::Credentials;
use crate::revx::{Revx, RevxCredentials};
pub trait Market {
fn ticker(&self, exch: &str, pair: &str) -> Result<(f64, f64), String>;
fn closes(&self, exch: &str, symbol: &str, days: usize) -> Result<Vec<f64>, String>;
}
pub struct PublicMarket {
http: Http,
gate: Gate,
binance: Binance,
revx: Revx,
}
impl Default for PublicMarket {
fn default() -> Self {
Self::with_http(Http::default())
}
}
impl PublicMarket {
pub fn with_http(http: Http) -> Self {
let none = || Credentials {
key: String::new(),
secret: String::new(),
};
PublicMarket {
gate: Gate::with_http(none(), http.clone()),
binance: Binance::with_http(none(), http.clone()),
revx: Revx::with_http(
RevxCredentials {
key: String::new(),
pem_path: Default::default(),
},
http.clone(),
),
http,
}
}
fn get_list(&self, venue: &'static str, url: &str) -> Result<Vec<serde_json::Value>, String> {
let r = http::request(&self.http, venue, "GET", url, vec![], None)
.map_err(|e| e.to_string())?;
match (r.status, r.json.as_ref().and_then(|v| v.as_array())) {
(Some(200), Some(a)) => Ok(a.clone()),
_ => Err(format!("{} -> {}", url, r.status_str())),
}
}
}
fn num(v: Option<&serde_json::Value>) -> Result<f64, String> {
v.ok_or_else(|| "short candle".to_string())
.and_then(crate::pyfmt::to_float)
}
impl Market for PublicMarket {
fn ticker(&self, exch: &str, pair: &str) -> Result<(f64, f64), String> {
match exch {
"binance" => self.binance.ticker(pair),
"revx" => self.revx.ticker(pair),
_ => self.gate.ticker(pair),
}
.map_err(|e| e.to_string())
}
fn closes(&self, exch: &str, symbol: &str, days: usize) -> Result<Vec<f64>, String> {
if exch == "binance" {
let rows = self
.get_list(
"binance",
&format!(
"https://api.binance.com/api/v3/klines?symbol={symbol}&interval=1d&limit={days}"
),
)
.map_err(|_| format!("binance klines {symbol} -> error"))?;
rows.iter().map(|k| num(k.get(4))).collect()
} else {
let rows = self
.get_list(
"gate",
&format!(
"https://api.gateio.ws/api/v4/spot/candlesticks?currency_pair={symbol}&interval=1d&limit={days}"
),
)
.map_err(|_| format!("gate candlesticks {symbol} -> error"))?;
rows.iter().map(|k| num(k.get(2))).collect()
}
}
}