bitcoin_price/
lib.rs

1//! # Bitcoin Price
2//!
3//! `bitcoin_price` is an easy way to get the current market data for bitcoin.
4
5use anyhow::Result;
6
7mod binance;
8mod crypto_dot_com;
9mod currencies;
10mod kraken;
11mod request;
12
13// From: https://benjaminbrandt.com/averages-in-rust/
14fn get_mean(list: &[f32]) -> f64 {
15    let sum: f32 = Iterator::sum(list.iter());
16    f64::from(sum) / (list.len() as f64)
17}
18
19pub fn get_coinbase_price() -> Result<f32> {
20    let coinbase_price = coinbase_bitcoin::get_price_data()?;
21    Ok(coinbase_price.spot)
22}
23
24pub fn get_coinbase_buy_price() -> Result<f32> {
25    let coinbase_price = coinbase_bitcoin::get_price_data()?;
26    Ok(coinbase_price.buy)
27}
28
29pub fn get_coinbase_sell_price() -> Result<f32> {
30    let coinbase_price = coinbase_bitcoin::get_price_data()?;
31    Ok(coinbase_price.sell)
32}
33
34pub fn get_kraken_price() -> Result<f32> {
35    kraken::get_spot_price()
36}
37
38pub fn get_binance_price() -> Result<f32> {
39    binance::get_latest_price()
40}
41
42pub fn get_crypto_dot_com_price() -> Result<f32> {
43    crypto_dot_com::get_price()
44}
45
46/// This gets the average spot price across multiple exchanges
47///
48/// # Example:
49/// ```
50/// let price = bitcoin_price::get_average_exachange_spot_price();
51/// assert_eq!(1,1);
52/// ```
53pub fn get_average_exchange_spot_price() -> f64 {
54    let mut list: Vec<f32> = Vec::new();
55
56    match coinbase_bitcoin::get_spot_price() {
57        Ok(price) => list.push(price),
58        Err(_err) => {}
59    }
60    match kraken::get_spot_price() {
61        Ok(price) => list.push(price),
62        Err(_err) => {}
63    }
64    match binance::get_latest_price() {
65        Ok(price) => list.push(price),
66        Err(_err) => {}
67    }
68    match crypto_dot_com::get_price() {
69        Ok(price) => list.push(price),
70        Err(_err) => {}
71    }
72
73    let average_price: f64 = get_mean(&list);
74    average_price
75}