Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add scan start block height config parameter #322

Merged
merged 3 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/cli_commands/prepare_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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
2 changes: 2 additions & 0 deletions core/src/oracle_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LevelFilter>,
pub core_api_port: u16,
pub oracle_address: NetworkAddress,
Expand Down Expand Up @@ -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(),
Expand Down
5 changes: 3 additions & 2 deletions core/src/scans/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
Loading