From 4ba544785071a30e5980883f4cd11db63055d476 Mon Sep 17 00:00:00 2001 From: Manuel Haug Date: Sat, 23 Dec 2023 13:26:12 +0100 Subject: [PATCH] mock datasource api calls to prevent failing tests due to api rate limits --- core/src/datapoint_source/bitpanda.rs | 27 ++++++++++++++++++ core/src/datapoint_source/coincap.rs | 25 +++++++++++++++++ core/src/datapoint_source/coingecko.rs | 39 ++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/core/src/datapoint_source/bitpanda.rs b/core/src/datapoint_source/bitpanda.rs index ec6c6074..379e2d07 100644 --- a/core/src/datapoint_source/bitpanda.rs +++ b/core/src/datapoint_source/bitpanda.rs @@ -7,6 +7,7 @@ use super::DataPointSourceError; #[derive(Debug, Clone)] pub struct BitPanda {} +#[cfg(not(test))] pub async fn get_kgau_usd() -> Result, DataPointSourceError> { let url = "https://api.bitpanda.com/v1/ticker"; let resp = reqwest::get(url).await?; @@ -34,6 +35,20 @@ pub async fn get_kgau_usd() -> Result, DataPointSo } } +#[cfg(test)] +pub async fn get_kgau_usd() -> Result, DataPointSourceError> { + // USD price of 1 gram of gold + let p_float = 66.10; + let usd_per_kgau = KgAu::from_gram(p_float); + let rate = AssetsExchangeRate { + per1: KgAu {}, + get: Usd {}, + rate: usd_per_kgau, + }; + Ok(rate) +} + +#[cfg(not(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, DataPointSourceError> { let url = "https://api.bitpanda.com/v1/ticker"; @@ -61,6 +76,18 @@ pub(crate) async fn get_btc_usd() -> Result, DataPo } } +#[cfg(test)] +pub(crate) async fn get_btc_usd() -> Result, DataPointSourceError> { + // USD price of BTC + let usd_per_btc = 43827.02; + let rate = AssetsExchangeRate { + per1: Btc {}, + get: Usd {}, + rate: usd_per_btc, + }; + Ok(rate) +} + #[cfg(test)] mod tests { use super::*; diff --git a/core/src/datapoint_source/coincap.rs b/core/src/datapoint_source/coincap.rs index ec47a6f3..7f3191b8 100644 --- a/core/src/datapoint_source/coincap.rs +++ b/core/src/datapoint_source/coincap.rs @@ -7,6 +7,7 @@ use super::DataPointSourceError; #[derive(Debug, Clone)] pub struct CoinCap; +#[cfg(not(test))] pub async fn get_usd_nanoerg() -> Result, DataPointSourceError> { // see https://coincap.io/assets/ergo let url = "https://api.coincap.io/v2/assets/ergo"; @@ -34,6 +35,19 @@ pub async fn get_usd_nanoerg() -> Result, DataP } } +#[cfg(test)] +pub async fn get_usd_nanoerg() -> Result, DataPointSourceError> { + let p_float = 1.661_923_469_67; + let nanoerg_per_usd = NanoErg::from_erg(1.0 / p_float); + let rate = AssetsExchangeRate { + per1: Usd {}, + get: NanoErg {}, + rate: nanoerg_per_usd, + }; + Ok(rate) +} + +#[cfg(not(test))] // Get USD/BTC. Can be used as a redundant source for ERG/BTC through ERG/USD and USD/BTC pub async fn get_btc_usd() -> Result, DataPointSourceError> { // see https://coincap.io/assets/ergo @@ -61,6 +75,17 @@ pub async fn get_btc_usd() -> Result, DataPointSour } } +#[cfg(test)] +pub async fn get_btc_usd() -> Result, DataPointSourceError> { + let usd_per_btc = 43_712.768_005_075_37; + let rate = AssetsExchangeRate { + per1: Btc {}, + get: Usd {}, + rate: usd_per_btc, + }; + Ok(rate) +} + #[cfg(test)] mod tests { use super::super::bitpanda; diff --git a/core/src/datapoint_source/coingecko.rs b/core/src/datapoint_source/coingecko.rs index 33dca31e..6955b2d0 100644 --- a/core/src/datapoint_source/coingecko.rs +++ b/core/src/datapoint_source/coingecko.rs @@ -29,6 +29,7 @@ pub async fn get_kgau_nanoerg() -> Result, Dat } } +#[cfg(not(test))] pub async fn get_usd_nanoerg() -> Result, DataPointSourceError> { let url = "https://api.coingecko.com/api/v3/simple/price?ids=ergo&vs_currencies=USD"; let resp = reqwest::get(url).await?; @@ -50,6 +51,19 @@ pub async fn get_usd_nanoerg() -> Result, DataP } } +#[cfg(test)] +pub async fn get_usd_nanoerg() -> Result, DataPointSourceError> { + // Convert from price Erg/USD to nanoErgs per 1 USD + let nanoerg_per_usd = NanoErg::from_erg(1.0 / 1.67); + let rate = AssetsExchangeRate { + per1: Usd {}, + get: NanoErg {}, + rate: nanoerg_per_usd, + }; + Ok(rate) +} + +#[cfg(not(test))] pub async fn get_usd_lovelace() -> Result, DataPointSourceError> { let url = "https://api.coingecko.com/api/v3/simple/price?ids=cardano&vs_currencies=USD"; let resp = reqwest::get(url).await?; @@ -71,6 +85,19 @@ pub async fn get_usd_lovelace() -> Result, Dat } } +#[cfg(test)] +pub async fn get_usd_lovelace() -> Result, DataPointSourceError> { + // Convert from price Erg/USD to nanoErgs per 1 USD + let lovelace_price = Lovelace::from_ada(1.0 / 0.606545); + let rate = AssetsExchangeRate { + per1: Usd {}, + get: Lovelace {}, + rate: lovelace_price, + }; + Ok(rate) +} + +#[cfg(not(test))] pub async fn get_btc_nanoerg() -> Result, DataPointSourceError> { let url = "https://api.coingecko.com/api/v3/simple/price?ids=ergo&vs_currencies=BTC"; let resp = reqwest::get(url).await?; @@ -92,6 +119,18 @@ pub async fn get_btc_nanoerg() -> Result, DataP } } +#[cfg(test)] +pub async fn get_btc_nanoerg() -> Result, DataPointSourceError> { + // Convert from price BTC/ERG to nanoERG/BTC + let erg_per_usd = NanoErg::from_erg(1.0 / 0.00003791); + let rate = AssetsExchangeRate { + per1: Btc {}, + get: NanoErg {}, + rate: erg_per_usd, + }; + Ok(rate) +} + #[cfg(test)] mod tests { use super::*;