helium_api/
oracle.rs

1use crate::{
2    models::{OraclePrediction, OraclePrice},
3    *,
4};
5
6pub mod prices {
7    use super::*;
8
9    /// Fetch all inferred oracle prices
10    pub fn all(client: &Client) -> Stream<OraclePrice> {
11        client.fetch_stream("/oracle/prices", NO_QUERY)
12    }
13
14    /// Get the current valid oracle price
15    pub async fn current(client: &Client) -> Result<OraclePrice> {
16        client.fetch("/oracle/prices/current", NO_QUERY).await
17    }
18
19    /// Get the oracle price that was valid at the given block
20    pub async fn at_block(client: &Client, block: u64) -> Result<OraclePrice> {
21        client
22            .fetch(&format!("/oracle/prices/{}", block), NO_QUERY)
23            .await
24    }
25}
26
27/// Fetches a list of oracle price predictions based on received oracle reports
28/// and the current oracle price.
29pub async fn predictions(client: &Client) -> Result<Vec<OraclePrediction>> {
30    client.fetch("/oracle/predictions", NO_QUERY).await
31}
32
33#[cfg(test)]
34mod test {
35    use super::*;
36    use models::Usd;
37    use tokio::test;
38
39    #[test]
40    async fn all() {
41        let client = get_test_client();
42        let prices = oracle::prices::all(&client)
43            .take(10)
44            .into_vec()
45            .await
46            .expect("oracle prices");
47        assert_eq!(prices.len(), 10);
48    }
49
50    #[test]
51    async fn current() {
52        let client = get_test_client();
53        let price = oracle::prices::current(&client).await.expect("price");
54        assert!(price.block > 0);
55    }
56
57    #[test]
58    async fn at_block() {
59        let client = get_test_client();
60        let price = oracle::prices::at_block(&client, 763816)
61            .await
62            .expect("price");
63        assert_eq!(price.price, Usd::from(733973329));
64    }
65
66    #[test]
67    async fn predictions() {
68        let client = get_test_client();
69        let predictions = oracle::predictions(&client).await;
70        // predictions may be an empty list
71        assert!(predictions.is_ok());
72    }
73}