Skip to content

Commit

Permalink
fix: NSTR token + decimals (#29)
Browse files Browse the repository at this point in the history
* feat: add nostra pair

* fix: decimals
  • Loading branch information
EvolveArt authored Jun 20, 2024
1 parent 214593e commit 9f07c96
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub(crate) static COINGECKO_IDS: phf::Map<&'static str, &'static str> = phf_map!
"LORDS/USD" => "lords",
"STRK/USD" => "starknet",
"ZEND/USD" => "zklend-2",
"NSTR/USD" => "nostra",
};

lazy_static! {
Expand Down Expand Up @@ -119,7 +120,7 @@ lazy_static! {
}

#[allow(unused)]
pub const FEE_TOKEN_DECIMALS: i32 = 18;
pub const FEE_TOKEN_DECIMALS: u32 = 18;
#[allow(unused)]
pub const FEE_TOKEN_ADDRESS: &str =
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7";
Expand Down
12 changes: 11 additions & 1 deletion src/monitoring/on_off_deviation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,24 @@ pub async fn on_off_price_deviation(
.await
.map_err(|e| MonitoringError::OnChain(e.to_string()))?;

let decimals =
config
.decimals(data_type.clone())
.get(&pair_id)
.ok_or(MonitoringError::OnChain(format!(
"Failed to get decimals for pair {:?}",
pair_id
)))?;

let on_chain_price = data
.first()
.ok_or(MonitoringError::OnChain("No data".to_string()))?
.to_bigint()
.to_f64()
.ok_or(MonitoringError::Conversion(
"Failed to convert to f64".to_string(),
))?;
))?
/ 10u64.pow(*decimals as u32) as f64;

let (deviation, num_sources_aggregated) = match data_type {
DataType::Spot => {
Expand Down
5 changes: 3 additions & 2 deletions src/monitoring/publisher_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use starknet::{
providers::Provider,
};

use crate::constants::FEE_TOKEN_ADDRESS;
use crate::constants::{FEE_TOKEN_ADDRESS, FEE_TOKEN_DECIMALS};
use crate::{config::get_config, error::MonitoringError};

/// Returns the balance of a given publisher address
Expand Down Expand Up @@ -33,7 +33,8 @@ pub async fn publisher_balance(publisher_address: Felt) -> Result<f64, Monitorin
.to_f64()
.ok_or(MonitoringError::Conversion(
"Failed to convert to f64".to_string(),
))?;
))?
/ 10u32.pow(FEE_TOKEN_DECIMALS) as f64;

Ok(on_chain_balance)
}
11 changes: 10 additions & 1 deletion src/monitoring/source_deviation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,23 @@ pub async fn source_deviation<T: Entry>(
.await
.map_err(|e| MonitoringError::OnChain(e.to_string()))?;

let decimals = config
.decimals(query.data_type())
.get(query.pair_id())
.ok_or(MonitoringError::OnChain(format!(
"Failed to get decimals for pair {:?}",
query.pair_id()
)))?;

let on_chain_price = data
.first()
.ok_or(MonitoringError::OnChain("No data".to_string()))?
.to_bigint()
.to_f64()
.ok_or(MonitoringError::Conversion(
"Failed to convert to f64".to_string(),
))?;
))?
/ 10u32.pow(*decimals as u32) as f64;

let deviation = (normalized_price - on_chain_price) / on_chain_price;
let num_sources_aggregated = try_felt_to_u32(data.get(3).unwrap()).map_err(|e| {
Expand Down

0 comments on commit 9f07c96

Please sign in to comment.