Skip to content

Commit

Permalink
Add combined ERG/BTC bitpanda+coincap source
Browse files Browse the repository at this point in the history
  • Loading branch information
SethDusek committed Jul 21, 2023
1 parent beb9ef7 commit d6115c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
3 changes: 0 additions & 3 deletions core/src/datapoint_source/bitpanda.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::assets_exchange_rate::AssetsExchangeRate;
#[cfg(test)]
use super::assets_exchange_rate::Btc;
use super::assets_exchange_rate::Usd;
use super::erg_xau::KgAu;
Expand Down Expand Up @@ -35,8 +34,6 @@ pub async fn get_kgau_usd() -> Result<AssetsExchangeRate<KgAu, Usd>, DataPointSo
}
}

// Currently only used for testing
#[cfg(test)]
// Get USD/BTC. Can be used as a redundant source for ERG/BTC through ERG/USD and USD/BTC
pub(crate) async fn get_btc_usd() -> Result<AssetsExchangeRate<Btc, Usd>, DataPointSourceError> {
let url = "https://api.bitpanda.com/v1/ticker";
Expand Down
23 changes: 22 additions & 1 deletion core/src/datapoint_source/erg_btc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use futures::Future;

use super::{
assets_exchange_rate::{convert_rate, AssetsExchangeRate, Btc, NanoErg},
coincap, coingecko, DataPointSourceError,
bitpanda, coincap, coingecko, DataPointSourceError,
};

#[allow(clippy::type_complexity)]
Expand All @@ -26,18 +26,39 @@ async fn get_btc_nanoerg_coincap() -> Result<AssetsExchangeRate<Btc, NanoErg>, D
))
}

async fn get_btc_nanoerg_bitpanda() -> Result<AssetsExchangeRate<Btc, NanoErg>, DataPointSourceError>
{
Ok(convert_rate(
coincap::get_usd_nanoerg().await?,
bitpanda::get_btc_usd().await?,
))
}

#[cfg(test)]
mod test {
use super::coingecko;
use super::get_btc_nanoerg_bitpanda;
use super::get_btc_nanoerg_coincap;
#[test]
fn test_btc_nanoerg_combined() {
let combined = tokio_test::block_on(get_btc_nanoerg_coincap()).unwrap();
let coingecko = tokio_test::block_on(coingecko::get_btc_nanoerg()).unwrap();
let bitpanda = tokio_test::block_on(get_btc_nanoerg_bitpanda()).unwrap();
let deviation_from_coingecko = (combined.rate - coingecko.rate).abs() / coingecko.rate;
assert!(
deviation_from_coingecko < 0.05,
"up to 5% deviation is allowed"
);
let bitpanda_deviation_from_coingecko =
(bitpanda.rate - coingecko.rate).abs() / coingecko.rate;
assert!(
bitpanda_deviation_from_coingecko < 0.05,
"up to 5% deviation is allowed"
);
let deviation_from_bitpanda = (bitpanda.rate - combined.rate).abs() / combined.rate;
assert!(
deviation_from_bitpanda < 0.05,
"up to 5% deviation is allowed"
);
}
}

0 comments on commit d6115c0

Please sign in to comment.