Skip to content

Commit

Permalink
opt: rm oracle_price_feed key
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Oct 23, 2024
1 parent f5fb27a commit fcf77f6
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 36 deletions.
8 changes: 4 additions & 4 deletions lib/ain-ocean/src/api/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ async fn get_feed(

let mut oracle_price_feeds = Vec::new();

for ((token, currency, oracle_id, _), feed) in &price_feed_list {
for ((token, currency, oracle_id, txid), feed) in &price_feed_list {
if key.0.eq(token) && key.1.eq(currency) && key.2.eq(oracle_id) {
let amount = Decimal::from(feed.amount) / Decimal::from(COIN);
oracle_price_feeds.push(OraclePriceFeedResponse {
id: format!("{}-{}-{}-{}", token, currency, oracle_id, feed.txid),
id: format!("{}-{}-{}-{}", token, currency, oracle_id, txid),
key: format!("{}-{}-{}", token, currency, oracle_id),
sort: hex::encode(feed.block.height.to_string() + &feed.txid.to_string()),
sort: hex::encode(feed.block.height.to_string() + &txid.to_string()),
token: token.to_owned(),
currency: currency.to_owned(),
oracle_id: oracle_id.to_owned(),
txid: feed.txid,
txid: *txid,
time: feed.time,
amount: amount.normalize().to_string(),
block: feed.block.clone(),
Expand Down
5 changes: 2 additions & 3 deletions lib/ain-ocean/src/api/prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ async fn list_price_oracles(
)),
SortOrder::Descending,
)?
// .take(1)
.take_while(|item| match item {
Ok((k, _)) => k.0 == token && k.1 == currency && k.2 == oracle_id,
_ => true,
Expand All @@ -433,11 +432,11 @@ async fn list_price_oracles(
OraclePriceFeedResponse {
id: format!("{}-{}-{}-{}", token, currency, oracle_id, txid),
key: format!("{}-{}-{}", token, currency, oracle_id),
sort: hex::encode(f.block.height.to_string() + &f.txid.to_string()),
sort: hex::encode(f.block.height.to_string() + &txid.to_string()),
token: token.clone(),
currency: currency.clone(),
oracle_id,
txid: f.txid,
txid,
time: f.time,
amount: f.amount.to_string(),
block: f.block,
Expand Down
22 changes: 8 additions & 14 deletions lib/ain-ocean/src/indexer/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ fn map_price_aggregated(
) -> Result<Option<OraclePriceAggregated>> {
let (token, currency) = pair;
let oracle_repo = &services.oracle_token_currency;
let feed_repo = &services.oracle_price_feed;

let oracles = oracle_repo
.by_id
Expand Down Expand Up @@ -281,13 +280,14 @@ fn map_price_aggregated(
continue;
}

let feed_id = feed_repo.by_key.get(&(id))?;

let Some(feed_id) = feed_id else { continue };

let feed = feed_repo.by_id.get(&feed_id)?;
let feed = services
.oracle_price_feed
.by_id
.list(Some((id.0, id.1, id.2, Txid::from_byte_array([0xffu8; 32]))), SortOrder::Descending)?
.next()
.transpose()?;

let Some(feed) = feed else { continue };
let Some((_, feed)) = feed else { continue };

let time_diff = Decimal::from(feed.time) - Decimal::from(context.block.time);
if Decimal::abs(&time_diff) < dec!(3600) {
Expand Down Expand Up @@ -406,18 +406,13 @@ fn index_set_oracle_data_interval(

impl Index for SetOracleData {
fn index(self, services: &Arc<Services>, context: &Context) -> Result<()> {
let feed_repo = &services.oracle_price_feed;

let mut pairs = HashSet::new();
let feeds = map_price_feeds(&self, context);
for (id, feed) in &feeds {
let token = id.0.clone();
let currency = id.1.clone();
let oracle_id = id.2;
let key = (token.clone(), currency.clone(), oracle_id);
pairs.insert((token, currency));
feed_repo.by_key.put(&key, id)?;
feed_repo.by_id.put(id, feed)?;
services.oracle_price_feed.by_id.put(id, feed)?;
}

index_set_oracle_data(services, context, &pairs)?;
Expand Down Expand Up @@ -477,7 +472,6 @@ fn map_price_feeds(
amount: token_amount.amount,
block: ctx.block.clone(),
time: data.timestamp as i32,
txid: ctx.tx.txid,
};
feeds.push((id, oracle_price_feed));
}
Expand Down
2 changes: 0 additions & 2 deletions lib/ain-ocean/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ pub struct OracleService {
by_id: Oracle,
}
pub struct OraclePriceFeedService {
by_key: OraclePriceFeedKey,
by_id: OraclePriceFeed,
}
pub struct OraclePriceActiveService {
Expand Down Expand Up @@ -179,7 +178,6 @@ impl Services {
by_id: Oracle::new(Arc::clone(&store)),
},
oracle_price_feed: OraclePriceFeedService {
by_key: OraclePriceFeedKey::new(Arc::clone(&store)),
by_id: OraclePriceFeed::new(Arc::clone(&store)),
},
oracle_price_active: OraclePriceActiveService {
Expand Down
2 changes: 0 additions & 2 deletions lib/ain-ocean/src/model/oracle_price_feed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ use serde::{Deserialize, Serialize};

use super::BlockContext;
pub type OraclePriceFeedId = (String, String, Txid, Txid); // token-currency-oracle_id-txid
pub type OraclePriceFeedKey = (String, String, Txid); // token-currency-oracle_id

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceFeed {
pub txid: Txid,
pub time: i32,
pub amount: i64,
pub block: BlockContext,
Expand Down
12 changes: 1 addition & 11 deletions lib/ain-ocean/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,6 @@ define_table! {
}
}

define_table! {
#[derive(Debug)]
pub struct OraclePriceFeedKey {
key_type = model::OraclePriceFeedKey,
value_type = model::OraclePriceFeedId,
},
SecondaryIndex = OraclePriceFeed
}

define_table! {
#[derive(Debug)]
pub struct OracleTokenCurrency {
Expand Down Expand Up @@ -435,7 +426,7 @@ define_table! {
SecondaryIndex = VaultAuctionHistory
}

pub const COLUMN_NAMES: [&str; 31] = [
pub const COLUMN_NAMES: [&str; 30] = [
Block::NAME,
BlockByHeight::NAME,
MasternodeStats::NAME,
Expand All @@ -448,7 +439,6 @@ pub const COLUMN_NAMES: [&str; 31] = [
OraclePriceAggregated::NAME,
OraclePriceAggregatedInterval::NAME,
OraclePriceFeed::NAME,
OraclePriceFeedKey::NAME,
OracleTokenCurrency::NAME,
PoolSwapAggregated::NAME,
PoolSwapAggregatedKey::NAME,
Expand Down

0 comments on commit fcf77f6

Please sign in to comment.