switchboard_utils/exchanges/
binance.rs

1use crate::*;
2
3use futures_util::TryFutureExt;
4use serde::Deserialize;
5
6pub type GenericError = Box<dyn std::error::Error + Send + Sync>;
7
8#[allow(non_snake_case)]
9#[derive(Deserialize, Default, Clone, Debug)]
10pub struct BinanceTicker {
11    pub symbol: String,
12    pub price: Decimal,
13}
14
15pub struct BinanceApi {}
16
17impl BinanceApi {
18    pub async fn fetch_spot_prices(
19        symbol: Option<String>,
20    ) -> Result<Vec<BinanceTicker>, GenericError> {
21        let mut binance_spot: Vec<BinanceTicker> =
22            reqwest::get("https://www.binance.com/api/v3/ticker/price")
23                .and_then(|r| r.json())
24                .await?;
25        if let Some(symbol) = symbol {
26            binance_spot = binance_spot
27                .into_iter()
28                .filter(|x| x.symbol == symbol)
29                .collect()
30        }
31        Ok(binance_spot)
32    }
33}