Skip to content

Commit

Permalink
filter_map as set
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Nov 28, 2024
1 parent ccae506 commit 583ca3f
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions lib/ain-ocean/src/indexer/loan_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,14 @@ pub fn index_active_price(services: &Arc<Services>, block: &BlockContext) -> Res
_ => 120,
};
if block.height % block_interval == 0 {
let mut set: HashSet<(Token, Currency)> = HashSet::new();
let pairs = services
.price_ticker
.by_id
.list(None, SortOrder::Descending)?
.flat_map(|item| {
let ((_, _, token, currency), _) = item?;
set.insert((token, currency));
Ok::<HashSet<(Token, Currency)>, Error>(set.clone())
.filter_map(|item| {
item.ok().map(|((_, _, token, currency), _)| (token, currency))
})
.next()
.unwrap_or(set);
.collect::<HashSet<(Token, Currency)>>();

for (token, currency) in pairs {
perform_active_price_tick(services, (token, currency), block)?;
Expand Down Expand Up @@ -143,26 +139,22 @@ pub fn invalidate_active_price(services: &Arc<Services>, block: &BlockContext) -
_ => 120,
};
if block.height % block_interval == 0 {
let mut set: HashSet<(Token, Currency)> = HashSet::new();
let pairs = services
.price_ticker
.by_id
.list(None, SortOrder::Descending)?
.flat_map(|item| {
let ((_, _, token, currency), _) = item?;
set.insert((token, currency));
Ok::<HashSet<(Token, Currency)>, Error>(set.clone())
.filter_map(|item| {
item.ok().map(|((_, _, token, currency), _)| (token, currency))
})
.next()
.unwrap_or(set);
.collect::<HashSet<(Token, Currency)>>();

// convert to vector to reverse the hashset is required
let mut vec = Vec::new();
let mut rev_pairs = Vec::new();
for pair in pairs {
vec.insert(0, pair);
rev_pairs.insert(0, pair);
}

for (token, currency) in vec {
for (token, currency) in rev_pairs {
services.oracle_price_active.by_id.delete(&(
token,
currency,
Expand Down

0 comments on commit 583ca3f

Please sign in to comment.