Skip to content

Commit 4ba5447

Browse files
committed
mock datasource api calls to prevent failing tests due to api rate limits
1 parent 4985f3e commit 4ba5447

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

core/src/datapoint_source/bitpanda.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::DataPointSourceError;
77
#[derive(Debug, Clone)]
88
pub struct BitPanda {}
99

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

38+
#[cfg(test)]
39+
pub async fn get_kgau_usd() -> Result<AssetsExchangeRate<KgAu, Usd>, DataPointSourceError> {
40+
// USD price of 1 gram of gold
41+
let p_float = 66.10;
42+
let usd_per_kgau = KgAu::from_gram(p_float);
43+
let rate = AssetsExchangeRate {
44+
per1: KgAu {},
45+
get: Usd {},
46+
rate: usd_per_kgau,
47+
};
48+
Ok(rate)
49+
}
50+
51+
#[cfg(not(test))]
3752
// Get USD/BTC. Can be used as a redundant source for ERG/BTC through ERG/USD and USD/BTC
3853
pub(crate) async fn get_btc_usd() -> Result<AssetsExchangeRate<Btc, Usd>, DataPointSourceError> {
3954
let url = "https://api.bitpanda.com/v1/ticker";
@@ -61,6 +76,18 @@ pub(crate) async fn get_btc_usd() -> Result<AssetsExchangeRate<Btc, Usd>, DataPo
6176
}
6277
}
6378

79+
#[cfg(test)]
80+
pub(crate) async fn get_btc_usd() -> Result<AssetsExchangeRate<Btc, Usd>, DataPointSourceError> {
81+
// USD price of BTC
82+
let usd_per_btc = 43827.02;
83+
let rate = AssetsExchangeRate {
84+
per1: Btc {},
85+
get: Usd {},
86+
rate: usd_per_btc,
87+
};
88+
Ok(rate)
89+
}
90+
6491
#[cfg(test)]
6592
mod tests {
6693
use super::*;

core/src/datapoint_source/coincap.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::DataPointSourceError;
77
#[derive(Debug, Clone)]
88
pub struct CoinCap;
99

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

38+
#[cfg(test)]
39+
pub async fn get_usd_nanoerg() -> Result<AssetsExchangeRate<Usd, NanoErg>, DataPointSourceError> {
40+
let p_float = 1.661_923_469_67;
41+
let nanoerg_per_usd = NanoErg::from_erg(1.0 / p_float);
42+
let rate = AssetsExchangeRate {
43+
per1: Usd {},
44+
get: NanoErg {},
45+
rate: nanoerg_per_usd,
46+
};
47+
Ok(rate)
48+
}
49+
50+
#[cfg(not(test))]
3751
// Get USD/BTC. Can be used as a redundant source for ERG/BTC through ERG/USD and USD/BTC
3852
pub async fn get_btc_usd() -> Result<AssetsExchangeRate<Btc, Usd>, DataPointSourceError> {
3953
// see https://coincap.io/assets/ergo
@@ -61,6 +75,17 @@ pub async fn get_btc_usd() -> Result<AssetsExchangeRate<Btc, Usd>, DataPointSour
6175
}
6276
}
6377

78+
#[cfg(test)]
79+
pub async fn get_btc_usd() -> Result<AssetsExchangeRate<Btc, Usd>, DataPointSourceError> {
80+
let usd_per_btc = 43_712.768_005_075_37;
81+
let rate = AssetsExchangeRate {
82+
per1: Btc {},
83+
get: Usd {},
84+
rate: usd_per_btc,
85+
};
86+
Ok(rate)
87+
}
88+
6489
#[cfg(test)]
6590
mod tests {
6691
use super::super::bitpanda;

core/src/datapoint_source/coingecko.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub async fn get_kgau_nanoerg() -> Result<AssetsExchangeRate<KgAu, NanoErg>, Dat
2929
}
3030
}
3131

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

54+
#[cfg(test)]
55+
pub async fn get_usd_nanoerg() -> Result<AssetsExchangeRate<Usd, NanoErg>, DataPointSourceError> {
56+
// Convert from price Erg/USD to nanoErgs per 1 USD
57+
let nanoerg_per_usd = NanoErg::from_erg(1.0 / 1.67);
58+
let rate = AssetsExchangeRate {
59+
per1: Usd {},
60+
get: NanoErg {},
61+
rate: nanoerg_per_usd,
62+
};
63+
Ok(rate)
64+
}
65+
66+
#[cfg(not(test))]
5367
pub async fn get_usd_lovelace() -> Result<AssetsExchangeRate<Usd, Lovelace>, DataPointSourceError> {
5468
let url = "https://api.coingecko.com/api/v3/simple/price?ids=cardano&vs_currencies=USD";
5569
let resp = reqwest::get(url).await?;
@@ -71,6 +85,19 @@ pub async fn get_usd_lovelace() -> Result<AssetsExchangeRate<Usd, Lovelace>, Dat
7185
}
7286
}
7387

88+
#[cfg(test)]
89+
pub async fn get_usd_lovelace() -> Result<AssetsExchangeRate<Usd, Lovelace>, DataPointSourceError> {
90+
// Convert from price Erg/USD to nanoErgs per 1 USD
91+
let lovelace_price = Lovelace::from_ada(1.0 / 0.606545);
92+
let rate = AssetsExchangeRate {
93+
per1: Usd {},
94+
get: Lovelace {},
95+
rate: lovelace_price,
96+
};
97+
Ok(rate)
98+
}
99+
100+
#[cfg(not(test))]
74101
pub async fn get_btc_nanoerg() -> Result<AssetsExchangeRate<Btc, NanoErg>, DataPointSourceError> {
75102
let url = "https://api.coingecko.com/api/v3/simple/price?ids=ergo&vs_currencies=BTC";
76103
let resp = reqwest::get(url).await?;
@@ -92,6 +119,18 @@ pub async fn get_btc_nanoerg() -> Result<AssetsExchangeRate<Btc, NanoErg>, DataP
92119
}
93120
}
94121

122+
#[cfg(test)]
123+
pub async fn get_btc_nanoerg() -> Result<AssetsExchangeRate<Btc, NanoErg>, DataPointSourceError> {
124+
// Convert from price BTC/ERG to nanoERG/BTC
125+
let erg_per_usd = NanoErg::from_erg(1.0 / 0.00003791);
126+
let rate = AssetsExchangeRate {
127+
per1: Btc {},
128+
get: NanoErg {},
129+
rate: erg_per_usd,
130+
};
131+
Ok(rate)
132+
}
133+
95134
#[cfg(test)]
96135
mod tests {
97136
use super::*;

0 commit comments

Comments
 (0)