Skip to content

Commit

Permalink
feat: Avoid purging prices when purging oracles (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass authored Jan 4, 2024
1 parent a79e51e commit 622ec12
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion programs/price-oracle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "price-oracle"
version = "0.2.0"
version = "0.2.1"
description = "Created with Anchor"
edition = "2021"

Expand Down
26 changes: 24 additions & 2 deletions programs/price-oracle/src/instructions/update_price_oracle_v0.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use crate::error::ErrorCode;
use crate::state::*;
use anchor_lang::prelude::*;
Expand Down Expand Up @@ -33,8 +35,28 @@ pub fn handler(ctx: Context<UpdatePriceOracleV0>, args: UpdatePriceOracleArgsV0)
ErrorCode::InvalidArgs
);
}
ctx.accounts.price_oracle.num_oracles = oracles.len().try_into().unwrap();
ctx.accounts.price_oracle.oracles = oracles;

let authorities: HashMap<String, &OracleV0> = ctx
.accounts
.price_oracle
.oracles
.iter()
.map(|oracle| (oracle.authority.to_string(), oracle))
.collect::<HashMap<_, _>>();
// If keeping an existing oracle, keep their price
let new_oracles = oracles
.into_iter()
.map(|oracle| {
if let Some(existing) = authorities.get(&oracle.authority.to_string()) {
(*existing).clone()
} else {
oracle
}
})
.collect::<Vec<_>>();

ctx.accounts.price_oracle.num_oracles = new_oracles.len().try_into().unwrap();
ctx.accounts.price_oracle.oracles = new_oracles;
ctx.accounts.price_oracle.current_price = None;
ctx.accounts.price_oracle.last_calculated_timestamp = None;
}
Expand Down

0 comments on commit 622ec12

Please sign in to comment.