From ba8957685c667ac87360a7276992cc5c10bda3d5 Mon Sep 17 00:00:00 2001 From: Manuel Haug Date: Fri, 22 Dec 2023 22:45:05 +0100 Subject: [PATCH 1/3] add scan start block height config parameter --- core/src/oracle_config.rs | 2 ++ core/src/scans/registry.rs | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/core/src/oracle_config.rs b/core/src/oracle_config.rs index b66691e7..29d5c1e2 100644 --- a/core/src/oracle_config.rs +++ b/core/src/oracle_config.rs @@ -30,6 +30,7 @@ pub const DEFAULT_ORACLE_CONFIG_FILE_NAME: &str = "oracle_config.yaml"; pub struct OracleConfig { pub node_url: Url, pub base_fee: u64, + pub scan_start_height: u32, pub log_level: Option, pub core_api_port: u16, pub oracle_address: NetworkAddress, @@ -117,6 +118,7 @@ impl Default for OracleConfig { Self { oracle_address: address.clone(), core_api_port: 9010, + scan_start_height: 0, data_point_source_custom_script: None, base_fee: *tx_builder::SUGGESTED_TX_FEE().as_u64(), log_level: LevelFilter::Info.into(), diff --git a/core/src/scans/registry.rs b/core/src/scans/registry.rs index 50f03762..a99a71af 100644 --- a/core/src/scans/registry.rs +++ b/core/src/scans/registry.rs @@ -10,6 +10,7 @@ use crate::spec_token::PoolTokenId; use crate::spec_token::RefreshTokenId; use crate::spec_token::UpdateTokenId; +use crate::oracle_config::ORACLE_CONFIG; use ::serde::Deserialize; use ::serde::Serialize; use once_cell::sync; @@ -87,7 +88,7 @@ impl NodeScanRegistry { buyback_token_scan, }; registry.save_to_json_file(&get_scans_file_path())?; - node_api.rescan_from_height(0)?; + node_api.rescan_from_height(ORACLE_CONFIG.scan_start_height)?; Ok(registry) } @@ -116,7 +117,7 @@ impl NodeScanRegistry { } else { let buyback_token_scan = GenericTokenScan::register(node_api, &pool_config_buyback_token_id)?; - node_api.rescan_from_height(0)?; + node_api.rescan_from_height(ORACLE_CONFIG.scan_start_height)?; let new_registry = Self { buyback_token_scan: Some(buyback_token_scan), ..loaded_registry From 4985f3ed7231305eccfe50dbedc1a74ada92499b Mon Sep 17 00:00:00 2001 From: Manuel Haug Date: Sat, 23 Dec 2023 13:25:47 +0100 Subject: [PATCH 2/3] add new scan start height parameter to prepare update test --- core/src/cli_commands/prepare_update.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/cli_commands/prepare_update.rs b/core/src/cli_commands/prepare_update.rs index d782c984..de51fb2e 100644 --- a/core/src/cli_commands/prepare_update.rs +++ b/core/src/cli_commands/prepare_update.rs @@ -628,6 +628,7 @@ rescan_height: 141887 node_url: http://10.94.77.47:9052 node_api_key: hello base_fee: 1100000 +scan_start_height: 0 log_level: ~ core_api_port: 9010 oracle_address: 3Wy3BaCjGDWE3bjjZkNo3aWaMz3cYrePMFhchcKovY9uG9vhpAuW From 4ba544785071a30e5980883f4cd11db63055d476 Mon Sep 17 00:00:00 2001 From: Manuel Haug Date: Sat, 23 Dec 2023 13:26:12 +0100 Subject: [PATCH 3/3] 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::*;