From 622ec12e18cde8b201018bb723c0d0df09cb4a33 Mon Sep 17 00:00:00 2001 From: Noah Prince <83885631+ChewingGlass@users.noreply.github.com> Date: Thu, 4 Jan 2024 09:14:21 -0500 Subject: [PATCH] feat: Avoid purging prices when purging oracles (#533) --- Cargo.lock | 2 +- programs/price-oracle/Cargo.toml | 2 +- .../instructions/update_price_oracle_v0.rs | 26 +++++++++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index adde2aa78..9015414eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2999,7 +2999,7 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "price-oracle" -version = "0.2.0" +version = "0.2.1" dependencies = [ "anchor-lang", "default-env", diff --git a/programs/price-oracle/Cargo.toml b/programs/price-oracle/Cargo.toml index 3b199c547..27c80755b 100644 --- a/programs/price-oracle/Cargo.toml +++ b/programs/price-oracle/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "price-oracle" -version = "0.2.0" +version = "0.2.1" description = "Created with Anchor" edition = "2021" diff --git a/programs/price-oracle/src/instructions/update_price_oracle_v0.rs b/programs/price-oracle/src/instructions/update_price_oracle_v0.rs index d9dc7c007..3745e7595 100644 --- a/programs/price-oracle/src/instructions/update_price_oracle_v0.rs +++ b/programs/price-oracle/src/instructions/update_price_oracle_v0.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use crate::error::ErrorCode; use crate::state::*; use anchor_lang::prelude::*; @@ -33,8 +35,28 @@ pub fn handler(ctx: Context, args: UpdatePriceOracleArgsV0) ErrorCode::InvalidArgs ); } - ctx.accounts.price_oracle.num_oracles = oracles.len().try_into().unwrap(); - ctx.accounts.price_oracle.oracles = oracles; + + let authorities: HashMap = ctx + .accounts + .price_oracle + .oracles + .iter() + .map(|oracle| (oracle.authority.to_string(), oracle)) + .collect::>(); + // 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::>(); + + 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; }