use super::types::{
HistoricalInterval, HistoricalPriceRequest, HistoricalPriceResponse, TokenAddress,
TokenPricesByAddressRequest, TokenPricesByAddressResponse, TokenPricesBySymbolResponse,
};
use crate::client::Client;
use crate::error::Result;
pub struct PricesApi<'a> {
client: &'a Client,
}
impl<'a> PricesApi<'a> {
pub(crate) fn new(client: &'a Client) -> Self {
Self { client }
}
pub async fn get_prices_by_symbol(
&self,
symbols: &[&str],
) -> Result<TokenPricesBySymbolResponse> {
let query: Vec<(&str, &str)> = symbols.iter().map(|s| ("symbols", *s)).collect();
self.client.prices_get("tokens/by-symbol", &query).await
}
pub async fn get_prices_by_address(
&self,
addresses: &[(&str, &str)],
) -> Result<TokenPricesByAddressResponse> {
let body = TokenPricesByAddressRequest {
addresses: addresses
.iter()
.map(|(network, address)| TokenAddress {
network: network.to_string(),
address: address.to_string(),
})
.collect(),
};
self.client.prices_post("tokens/by-address", &body).await
}
pub async fn get_historical_by_symbol(
&self,
symbol: &str,
start_time: &str,
end_time: &str,
interval: HistoricalInterval,
) -> Result<HistoricalPriceResponse> {
let body = HistoricalPriceRequest {
symbol: Some(symbol.to_string()),
network: None,
address: None,
start_time: start_time.to_string(),
end_time: end_time.to_string(),
interval: interval.as_str().to_string(),
};
self.client.prices_post("tokens/historical", &body).await
}
pub async fn get_historical_by_address(
&self,
network: &str,
address: &str,
start_time: &str,
end_time: &str,
interval: HistoricalInterval,
) -> Result<HistoricalPriceResponse> {
let body = HistoricalPriceRequest {
symbol: None,
network: Some(network.to_string()),
address: Some(address.to_string()),
start_time: start_time.to_string(),
end_time: end_time.to_string(),
interval: interval.as_str().to_string(),
};
self.client.prices_post("tokens/historical", &body).await
}
}