-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
187 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use crate::datapoint_source::assets_exchange_rate::{AssetsExchangeRate, NanoErg}; | ||
use crate::datapoint_source::rsn_xag::Rsn; | ||
use crate::datapoint_source::DataPointSourceError; | ||
|
||
pub async fn get_rsn_nanoerg() -> Result<AssetsExchangeRate<NanoErg, Rsn>, DataPointSourceError> { | ||
let url = "https://api.spectrum.fi/v1/amm/pool/1b694b15467c62f0cd4525e368dbdea2329c713aa200b73df4a622e950551b40/stats"; | ||
let resp = reqwest::get(url).await?; | ||
let pool_json = json::parse(&resp.text().await?)?; | ||
let locked_erg = pool_json["lockedX"]["amount"].as_f64().ok_or_else(|| { | ||
DataPointSourceError::JsonMissingField { | ||
field: "lockedX.amount as f64".to_string(), | ||
json: pool_json.dump(), | ||
} | ||
})?; | ||
|
||
let locked_rsn = pool_json["lockedY"]["amount"].as_f64().ok_or_else(|| { | ||
DataPointSourceError::JsonMissingField { | ||
field: "lockedY.amount as f64".to_string(), | ||
json: pool_json.dump(), | ||
} | ||
})?; | ||
let price = Rsn::from_rsn(Rsn::from_rsn(locked_rsn) / NanoErg::from_erg(locked_erg)); | ||
let rate = AssetsExchangeRate { | ||
per1: NanoErg {}, | ||
get: Rsn {}, | ||
rate: price, | ||
}; | ||
Ok(rate) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_rsn_nanoerg_price() { | ||
let pair: AssetsExchangeRate<NanoErg, Rsn> = | ||
tokio_test::block_on(get_rsn_nanoerg()).unwrap(); | ||
assert!(pair.rate > 0.0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use futures::Future; | ||
use std::pin::Pin; | ||
|
||
use crate::datapoint_source::assets_exchange_rate::{convert_rate, Asset, AssetsExchangeRate}; | ||
use crate::datapoint_source::erg_xag::KgAg; | ||
use crate::datapoint_source::{bitpanda, coingecko, ergodex, DataPointSourceError}; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub struct Rsn {} | ||
|
||
impl Asset for Rsn {} | ||
|
||
impl Rsn { | ||
pub fn from_rsn(rsn: f64) -> f64 { | ||
rsn * 1_000.0 | ||
} | ||
} | ||
|
||
#[allow(clippy::type_complexity)] | ||
pub fn rsn_kgag_sources( | ||
) -> Vec<Pin<Box<dyn Future<Output = Result<AssetsExchangeRate<KgAg, Rsn>, DataPointSourceError>>>>> | ||
{ | ||
vec![ | ||
Box::pin(coingecko::get_kgag_rsn()), | ||
Box::pin(get_rsn_kgag_erg()), | ||
Box::pin(get_rsn_kgag_usd()), | ||
] | ||
} | ||
|
||
// Calculate RSN/KGAG through RSN/USD and KGAG/USD | ||
async fn get_rsn_kgag_usd() -> Result<AssetsExchangeRate<KgAg, Rsn>, DataPointSourceError> { | ||
Ok(convert_rate( | ||
coingecko::get_rsn_usd().await?, | ||
bitpanda::get_kgag_usd().await?, | ||
)) | ||
} | ||
|
||
// Calculate KGAG/RSN through KGAG/ERG and ERG/RSN | ||
async fn get_rsn_kgag_erg() -> Result<AssetsExchangeRate<KgAg, Rsn>, DataPointSourceError> { | ||
Ok(convert_rate( | ||
ergodex::get_rsn_nanoerg().await?, | ||
coingecko::get_kgag_nanoerg().await?, | ||
)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_kgag_rsn_combined() { | ||
let combined = tokio_test::block_on(get_rsn_kgag_usd()).unwrap(); | ||
let coingecko = tokio_test::block_on(coingecko::get_kgag_rsn()).unwrap(); | ||
let ergodex = tokio_test::block_on(get_rsn_kgag_erg()).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 ergodex_deviation_from_coingecko = | ||
(ergodex.rate - coingecko.rate).abs() / coingecko.rate; | ||
assert!( | ||
ergodex_deviation_from_coingecko < 0.05, | ||
"up to 5% deviation is allowed" | ||
); | ||
let deviation_from_ergodex = (ergodex.rate - combined.rate).abs() / combined.rate; | ||
assert!( | ||
deviation_from_ergodex < 0.05, | ||
"up to 5% deviation is allowed" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters