use crate::*;
use serde::Deserialize;
#[derive(Clone, Deserialize, Debug)]
pub struct OraclePrice {
#[serde(deserialize_with = "Usd::deserialize")]
pub price: Usd,
pub block: u64,
}
#[derive(Clone, Deserialize, Debug)]
pub struct OraclePrediction {
#[serde(deserialize_with = "Usd::deserialize")]
pub price: Usd,
pub time: u64,
}
pub mod prices {
use super::*;
pub fn all(client: &Client) -> Stream<OraclePrice> {
client.fetch_stream("/oracle/prices", NO_QUERY)
}
pub async fn current(client: &Client) -> Result<OraclePrice> {
client.fetch("/oracle/prices/current", NO_QUERY).await
}
pub async fn at_block(client: &Client, block: u64) -> Result<OraclePrice> {
client
.fetch(&format!("/oracle/prices/{}", block), NO_QUERY)
.await
}
}
pub async fn predictions(client: &Client) -> Result<Vec<OraclePrediction>> {
client.fetch("/oracle/predictions", NO_QUERY).await
}
#[cfg(test)]
mod test {
use super::*;
use tokio::test;
#[test]
async fn all() {
let client = Client::default();
let prices =
oracle::prices::all(&client)
.take(10)
.fold(vec![], |mut acc, price| async move {
acc.push(price.unwrap().block);
acc
});
assert_eq!(prices.await.len(), 10);
}
#[test]
async fn current() {
let client = Client::default();
let price = oracle::prices::current(&client).await.expect("price");
assert!(price.block > 0);
}
#[test]
async fn at_block() {
let client = Client::default();
let price = oracle::prices::at_block(&client, 763816)
.await
.expect("price");
assert_eq!(price.price, Usd::from(733973329));
}
#[test]
async fn predictions() {
let client = Client::default();
let predictions = oracle::predictions(&client).await;
assert!(predictions.is_ok());
}
}