Skip to content

Commit

Permalink
Ocean model weightage as u32
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo committed Sep 24, 2024
1 parent 5ec692f commit d422a1b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 18 deletions.
5 changes: 3 additions & 2 deletions lib/ain-dftx/src/types/oracles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bitcoin::{hash_types::Txid, io, ScriptBuf};
use super::{
common::CompactVec,
price::{CurrencyPair, TokenPrice},
Weightage,
};

#[derive(ConsensusEncoding, Debug, PartialEq, Eq)]
Expand All @@ -21,14 +22,14 @@ pub struct RemoveOracle {
#[derive(ConsensusEncoding, Debug, PartialEq, Eq)]
pub struct AppointOracle {
pub script: ScriptBuf,
pub weightage: u8,
pub weightage: Weightage,
pub price_feeds: CompactVec<CurrencyPair>,
}

#[derive(ConsensusEncoding, Debug, PartialEq, Eq)]
pub struct UpdateOracle {
pub oracle_id: Txid,
pub script: ScriptBuf,
pub weightage: u8,
pub weightage: Weightage,
pub price_feeds: CompactVec<CurrencyPair>,
}
2 changes: 1 addition & 1 deletion lib/ain-ocean/src/api/prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct OraclePriceAggregatedResponse {
#[serde(rename_all = "camelCase")]
pub struct OraclePriceAggregatedAggregatedResponse {
pub amount: String,
pub weightage: Weightage,
pub weightage: u32,
pub oracles: OraclePriceActiveNextOracles,
}

Expand Down
17 changes: 6 additions & 11 deletions lib/ain-ocean/src/indexer/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ fn map_price_aggregated(

let mut aggregated_total = Decimal::zero();
let mut aggregated_count = 0;
let mut aggregated_weightage = 0u8;
let mut aggregated_weightage = 0u32;

let oracles_len = oracles.len();
for oracle in oracles {
Expand All @@ -424,12 +424,7 @@ fn map_price_aggregated(
let time_diff = Decimal::from(feed.time) - Decimal::from(context.block.time);
if Decimal::abs(&time_diff) < dec!(3600) {
aggregated_count += 1;
aggregated_weightage =
aggregated_weightage
.checked_add(oracle.weightage)
.context(OtherSnafu {
msg: "Error adding oracle weightage",
})?;
aggregated_weightage += oracle.weightage as u32;
log::trace!(
"SetOracleData weightage: {:?} * oracle_price.amount: {:?}",
aggregated_weightage,
Expand Down Expand Up @@ -785,8 +780,8 @@ pub fn invalidate_oracle_interval(
aggregated: OraclePriceAggregatedIntervalAggregated {
amount: aggregated_amount.to_string(),
weightage: aggregated_weightage
.to_u8()
.context(ToPrimitiveSnafu { msg: "to_u8" })?,
.to_u32()
.context(ToPrimitiveSnafu { msg: "to_u32" })?,
count,
oracles: OraclePriceAggregatedIntervalAggregatedOracles {
active: aggregated_active
Expand Down Expand Up @@ -847,8 +842,8 @@ fn forward_aggregate(
aggregated: OraclePriceAggregatedIntervalAggregated {
amount: aggregated_amount.to_string(),
weightage: aggregated_weightage
.to_u8()
.context(ToPrimitiveSnafu { msg: "to_u8" })?,
.to_u32()
.context(ToPrimitiveSnafu { msg: "to_u32" })?,
count,
oracles: OraclePriceAggregatedIntervalAggregatedOracles {
active: aggregated_active
Expand Down
3 changes: 1 addition & 2 deletions lib/ain-ocean/src/model/oracle_price_active.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use ain_dftx::Weightage;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

Expand All @@ -23,7 +22,7 @@ pub struct OraclePriceActive {
pub struct OraclePriceActiveNext {
#[serde(with = "rust_decimal::serde::str")]
pub amount: Decimal,
pub weightage: Weightage,
pub weightage: u32,
pub oracles: OraclePriceActiveNextOracles,
}

Expand Down
4 changes: 2 additions & 2 deletions lib/ain-ocean/src/model/oracle_price_aggregated_interval.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ain_dftx::{Currency, Token, Weightage};
use ain_dftx::{Currency, Token};
use serde::{Deserialize, Serialize};

use super::BlockContext;
Expand Down Expand Up @@ -32,7 +32,7 @@ pub struct OraclePriceAggregatedInterval {
#[serde(rename_all = "camelCase")]
pub struct OraclePriceAggregatedIntervalAggregated {
pub amount: String,
pub weightage: Weightage,
pub weightage: u32,
pub count: i32,
pub oracles: OraclePriceAggregatedIntervalAggregatedOracles,
}
Expand Down

0 comments on commit d422a1b

Please sign in to comment.