Skip to content

Commit

Permalink
mock datasource api calls to prevent failing tests due to api rate li…
Browse files Browse the repository at this point in the history
…mits
  • Loading branch information
Alesfatalis committed Dec 23, 2023
1 parent 4985f3e commit 4ba5447
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
27 changes: 27 additions & 0 deletions core/src/datapoint_source/bitpanda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::DataPointSourceError;
#[derive(Debug, Clone)]
pub struct BitPanda {}

#[cfg(not(test))]
pub async fn get_kgau_usd() -> Result<AssetsExchangeRate<KgAu, Usd>, DataPointSourceError> {
let url = "https://api.bitpanda.com/v1/ticker";
let resp = reqwest::get(url).await?;
Expand Down Expand Up @@ -34,6 +35,20 @@ pub async fn get_kgau_usd() -> Result<AssetsExchangeRate<KgAu, Usd>, DataPointSo
}
}

#[cfg(test)]
pub async fn get_kgau_usd() -> Result<AssetsExchangeRate<KgAu, Usd>, 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<AssetsExchangeRate<Btc, Usd>, DataPointSourceError> {
let url = "https://api.bitpanda.com/v1/ticker";
Expand Down Expand Up @@ -61,6 +76,18 @@ pub(crate) async fn get_btc_usd() -> Result<AssetsExchangeRate<Btc, Usd>, DataPo
}
}

#[cfg(test)]
pub(crate) async fn get_btc_usd() -> Result<AssetsExchangeRate<Btc, Usd>, 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::*;
Expand Down
25 changes: 25 additions & 0 deletions core/src/datapoint_source/coincap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::DataPointSourceError;
#[derive(Debug, Clone)]
pub struct CoinCap;

#[cfg(not(test))]
pub async fn get_usd_nanoerg() -> Result<AssetsExchangeRate<Usd, NanoErg>, DataPointSourceError> {
// see https://coincap.io/assets/ergo
let url = "https://api.coincap.io/v2/assets/ergo";
Expand Down Expand Up @@ -34,6 +35,19 @@ pub async fn get_usd_nanoerg() -> Result<AssetsExchangeRate<Usd, NanoErg>, DataP
}
}

#[cfg(test)]
pub async fn get_usd_nanoerg() -> Result<AssetsExchangeRate<Usd, NanoErg>, 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<AssetsExchangeRate<Btc, Usd>, DataPointSourceError> {
// see https://coincap.io/assets/ergo
Expand Down Expand Up @@ -61,6 +75,17 @@ pub async fn get_btc_usd() -> Result<AssetsExchangeRate<Btc, Usd>, DataPointSour
}
}

#[cfg(test)]
pub async fn get_btc_usd() -> Result<AssetsExchangeRate<Btc, Usd>, 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;
Expand Down
39 changes: 39 additions & 0 deletions core/src/datapoint_source/coingecko.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub async fn get_kgau_nanoerg() -> Result<AssetsExchangeRate<KgAu, NanoErg>, Dat
}
}

#[cfg(not(test))]
pub async fn get_usd_nanoerg() -> Result<AssetsExchangeRate<Usd, NanoErg>, DataPointSourceError> {
let url = "https://api.coingecko.com/api/v3/simple/price?ids=ergo&vs_currencies=USD";
let resp = reqwest::get(url).await?;
Expand All @@ -50,6 +51,19 @@ pub async fn get_usd_nanoerg() -> Result<AssetsExchangeRate<Usd, NanoErg>, DataP
}
}

#[cfg(test)]
pub async fn get_usd_nanoerg() -> Result<AssetsExchangeRate<Usd, NanoErg>, 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<AssetsExchangeRate<Usd, Lovelace>, DataPointSourceError> {
let url = "https://api.coingecko.com/api/v3/simple/price?ids=cardano&vs_currencies=USD";
let resp = reqwest::get(url).await?;
Expand All @@ -71,6 +85,19 @@ pub async fn get_usd_lovelace() -> Result<AssetsExchangeRate<Usd, Lovelace>, Dat
}
}

#[cfg(test)]
pub async fn get_usd_lovelace() -> Result<AssetsExchangeRate<Usd, Lovelace>, 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<AssetsExchangeRate<Btc, NanoErg>, DataPointSourceError> {
let url = "https://api.coingecko.com/api/v3/simple/price?ids=ergo&vs_currencies=BTC";
let resp = reqwest::get(url).await?;
Expand All @@ -92,6 +119,18 @@ pub async fn get_btc_nanoerg() -> Result<AssetsExchangeRate<Btc, NanoErg>, DataP
}
}

#[cfg(test)]
pub async fn get_btc_nanoerg() -> Result<AssetsExchangeRate<Btc, NanoErg>, 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::*;
Expand Down

0 comments on commit 4ba5447

Please sign in to comment.