From 6288f2dd7df3b336c45aa1842f8f216aca3424a7 Mon Sep 17 00:00:00 2001 From: Lou-Kamades Date: Fri, 17 Nov 2023 13:08:25 -0600 Subject: [PATCH] add fallback oracles to bank and mintinfo --- programs/mango-v4/src/accounts_ix/token_edit.rs | 5 +++++ .../mango-v4/src/accounts_ix/token_register.rs | 3 +++ .../src/accounts_ix/token_register_trustless.rs | 3 +++ programs/mango-v4/src/instructions/token_edit.rs | 16 ++++++++++++++-- .../mango-v4/src/instructions/token_register.rs | 11 +++++++---- .../src/instructions/token_register_trustless.rs | 11 +++++++---- programs/mango-v4/src/lib.rs | 2 ++ programs/mango-v4/src/logs.rs | 11 +++++++++++ programs/mango-v4/src/state/bank.rs | 7 +++++-- programs/mango-v4/src/state/mint_info.rs | 4 +++- 10 files changed, 60 insertions(+), 13 deletions(-) diff --git a/programs/mango-v4/src/accounts_ix/token_edit.rs b/programs/mango-v4/src/accounts_ix/token_edit.rs index 5d20d59ca..d4e6efdf6 100644 --- a/programs/mango-v4/src/accounts_ix/token_edit.rs +++ b/programs/mango-v4/src/accounts_ix/token_edit.rs @@ -21,4 +21,9 @@ pub struct TokenEdit<'info> { /// /// CHECK: The oracle can be one of several different account types pub oracle: UncheckedAccount<'info>, + + /// The oracle account is optional and only used when reset_stable_price is set. + /// + /// CHECK: The oracle can be one of several different account types + pub fallback_oracle: UncheckedAccount<'info>, } diff --git a/programs/mango-v4/src/accounts_ix/token_register.rs b/programs/mango-v4/src/accounts_ix/token_register.rs index 6c51a7d13..1f91129e8 100644 --- a/programs/mango-v4/src/accounts_ix/token_register.rs +++ b/programs/mango-v4/src/accounts_ix/token_register.rs @@ -51,6 +51,9 @@ pub struct TokenRegister<'info> { /// CHECK: The oracle can be one of several different account types pub oracle: UncheckedAccount<'info>, + /// CHECK: The oracle can be one of several different account types + pub fallback_oracle: UncheckedAccount<'info>, + #[account(mut)] pub payer: Signer<'info>, diff --git a/programs/mango-v4/src/accounts_ix/token_register_trustless.rs b/programs/mango-v4/src/accounts_ix/token_register_trustless.rs index 0bd6130cf..755f02a14 100644 --- a/programs/mango-v4/src/accounts_ix/token_register_trustless.rs +++ b/programs/mango-v4/src/accounts_ix/token_register_trustless.rs @@ -51,6 +51,9 @@ pub struct TokenRegisterTrustless<'info> { /// CHECK: The oracle can be one of several different account types pub oracle: UncheckedAccount<'info>, + /// CHECK: The oracle can be one of several different account types + pub fallback_oracle: UncheckedAccount<'info>, + #[account(mut)] pub payer: Signer<'info>, diff --git a/programs/mango-v4/src/instructions/token_edit.rs b/programs/mango-v4/src/instructions/token_edit.rs index abab45d2d..80e3b28fa 100644 --- a/programs/mango-v4/src/instructions/token_edit.rs +++ b/programs/mango-v4/src/instructions/token_edit.rs @@ -8,7 +8,7 @@ use crate::error::MangoError; use crate::state::*; use crate::accounts_ix::*; -use crate::logs::{emit_stack, TokenMetaDataLog}; +use crate::logs::{emit_stack, TokenMetaDataLogV2}; use crate::util::fill_from_str; #[allow(unused_variables)] @@ -49,6 +49,7 @@ pub fn token_edit( maint_weight_shift_asset_target_opt: Option, maint_weight_shift_liab_target_opt: Option, maint_weight_shift_abort: bool, + fallback_oracle_opt: Option, ) -> Result<()> { let group = ctx.accounts.group.load()?; @@ -76,6 +77,16 @@ pub fn token_edit( mint_info.oracle = oracle; require_group_admin = true; } + if let Some(fallback_oracle) = fallback_oracle_opt { + msg!( + "Fallback oracle old {:?}, new {:?}", + bank.fallback_oracle, + fallback_oracle + ); + bank.fallback_oracle = fallback_oracle; + mint_info.fallback_oracle = fallback_oracle; + require_group_admin = true; + } if reset_stable_price { msg!("Stable price reset"); require_keys_eq!(bank.oracle, ctx.accounts.oracle.key()); @@ -456,12 +467,13 @@ pub fn token_edit( let bank = ctx.remaining_accounts.first().unwrap().load_mut::()?; bank.verify()?; - emit_stack(TokenMetaDataLog { + emit_stack!(TokenMetaDataLogV2 { mango_group: ctx.accounts.group.key(), mint: mint_info.mint.key(), token_index: bank.token_index, mint_decimals: bank.mint_decimals, oracle: mint_info.oracle.key(), + fallback_oracle: ctx.accounts.fallback_oracle.key(), mint_info: ctx.accounts.mint_info.key(), }); diff --git a/programs/mango-v4/src/instructions/token_register.rs b/programs/mango-v4/src/instructions/token_register.rs index 9ccda45ab..28858a023 100644 --- a/programs/mango-v4/src/instructions/token_register.rs +++ b/programs/mango-v4/src/instructions/token_register.rs @@ -6,7 +6,7 @@ use crate::error::*; use crate::state::*; use crate::util::fill_from_str; -use crate::logs::{emit_stack, TokenMetaDataLog}; +use crate::logs::{emit_stack, TokenMetaDataLogV2}; pub const INDEX_START: I80F48 = I80F48::from_bits(1_000_000 * I80F48::ONE.to_bits()); @@ -119,7 +119,8 @@ pub fn token_register( maint_weight_shift_duration_inv: I80F48::ZERO, maint_weight_shift_asset_target: I80F48::ZERO, maint_weight_shift_liab_target: I80F48::ZERO, - reserved: [0; 2008], + fallback_oracle: ctx.accounts.fallback_oracle.key(), + reserved: [0; 1976], }; if let Ok(oracle_price) = @@ -143,19 +144,21 @@ pub fn token_register( banks: Default::default(), vaults: Default::default(), oracle: ctx.accounts.oracle.key(), + fallback_oracle: ctx.accounts.fallback_oracle.key(), registration_time: Clock::get()?.unix_timestamp.try_into().unwrap(), - reserved: [0; 2560], + reserved: [0; 2528], }; mint_info.banks[0] = ctx.accounts.bank.key(); mint_info.vaults[0] = ctx.accounts.vault.key(); - emit_stack(TokenMetaDataLog { + emit_stack!(TokenMetaDataLogV2 { mango_group: ctx.accounts.group.key(), mint: ctx.accounts.mint.key(), token_index, mint_decimals: ctx.accounts.mint.decimals, oracle: ctx.accounts.oracle.key(), + fallback_oracle: ctx.accounts.fallback_oracle.key(), mint_info: ctx.accounts.mint_info.key(), }); diff --git a/programs/mango-v4/src/instructions/token_register_trustless.rs b/programs/mango-v4/src/instructions/token_register_trustless.rs index bb412ed34..77cab62db 100644 --- a/programs/mango-v4/src/instructions/token_register_trustless.rs +++ b/programs/mango-v4/src/instructions/token_register_trustless.rs @@ -7,7 +7,7 @@ use crate::instructions::INDEX_START; use crate::state::*; use crate::util::fill_from_str; -use crate::logs::{emit_stack, TokenMetaDataLog}; +use crate::logs::{emit_stack, TokenMetaDataLogV2}; use crate::accounts_ix::*; @@ -102,7 +102,8 @@ pub fn token_register_trustless( maint_weight_shift_duration_inv: I80F48::ZERO, maint_weight_shift_asset_target: I80F48::ZERO, maint_weight_shift_liab_target: I80F48::ZERO, - reserved: [0; 2008], + fallback_oracle: ctx.accounts.fallback_oracle.key(), + reserved: [0; 1976], }; if let Ok(oracle_price) = @@ -126,19 +127,21 @@ pub fn token_register_trustless( banks: Default::default(), vaults: Default::default(), oracle: ctx.accounts.oracle.key(), + fallback_oracle: ctx.accounts.fallback_oracle.key(), registration_time: Clock::get()?.unix_timestamp.try_into().unwrap(), - reserved: [0; 2560], + reserved: [0; 2528], }; mint_info.banks[0] = ctx.accounts.bank.key(); mint_info.vaults[0] = ctx.accounts.vault.key(); - emit_stack(TokenMetaDataLog { + emit_stack!(TokenMetaDataLogV2 { mango_group: ctx.accounts.group.key(), mint: ctx.accounts.mint.key(), token_index, mint_decimals: ctx.accounts.mint.decimals, oracle: ctx.accounts.oracle.key(), + fallback_oracle: ctx.accounts.fallback_oracle.key(), mint_info: ctx.accounts.mint_info.key(), }); diff --git a/programs/mango-v4/src/lib.rs b/programs/mango-v4/src/lib.rs index 477466497..8120d0823 100644 --- a/programs/mango-v4/src/lib.rs +++ b/programs/mango-v4/src/lib.rs @@ -234,6 +234,7 @@ pub mod mango_v4 { maint_weight_shift_asset_target_opt: Option, maint_weight_shift_liab_target_opt: Option, maint_weight_shift_abort: bool, + fallback_oracle_opt: Option, ) -> Result<()> { #[cfg(feature = "enable-gpl")] instructions::token_edit( @@ -272,6 +273,7 @@ pub mod mango_v4 { maint_weight_shift_asset_target_opt, maint_weight_shift_liab_target_opt, maint_weight_shift_abort, + fallback_oracle_opt, )?; Ok(()) } diff --git a/programs/mango-v4/src/logs.rs b/programs/mango-v4/src/logs.rs index 215da9bba..938abded0 100644 --- a/programs/mango-v4/src/logs.rs +++ b/programs/mango-v4/src/logs.rs @@ -450,6 +450,17 @@ pub struct TokenMetaDataLog { pub mint_info: Pubkey, } +#[event] +pub struct TokenMetaDataLogV2 { + pub mango_group: Pubkey, + pub mint: Pubkey, + pub token_index: u16, + pub mint_decimals: u8, + pub oracle: Pubkey, + pub fallback_oracle: Pubkey, + pub mint_info: Pubkey, +} + #[event] pub struct PerpMarketMetaDataLog { pub mango_group: Pubkey, diff --git a/programs/mango-v4/src/state/bank.rs b/programs/mango-v4/src/state/bank.rs index 17d250198..334c8944f 100644 --- a/programs/mango-v4/src/state/bank.rs +++ b/programs/mango-v4/src/state/bank.rs @@ -167,8 +167,10 @@ pub struct Bank { pub maint_weight_shift_asset_target: I80F48, pub maint_weight_shift_liab_target: I80F48, + pub fallback_oracle: Pubkey, + #[derivative(Debug = "ignore")] - pub reserved: [u8; 2008], + pub reserved: [u8; 1976], } const_assert_eq!( size_of::(), @@ -292,7 +294,8 @@ impl Bank { maint_weight_shift_duration_inv: existing_bank.maint_weight_shift_duration_inv, maint_weight_shift_asset_target: existing_bank.maint_weight_shift_asset_target, maint_weight_shift_liab_target: existing_bank.maint_weight_shift_liab_target, - reserved: [0; 2008], + fallback_oracle: existing_bank.oracle, // bongo + reserved: [0; 1976], } } diff --git a/programs/mango-v4/src/state/mint_info.rs b/programs/mango-v4/src/state/mint_info.rs index 73ae555a8..5f8cc6107 100644 --- a/programs/mango-v4/src/state/mint_info.rs +++ b/programs/mango-v4/src/state/mint_info.rs @@ -33,8 +33,10 @@ pub struct MintInfo { pub registration_time: u64, + pub fallback_oracle: Pubkey, + #[derivative(Debug = "ignore")] - pub reserved: [u8; 2560], + pub reserved: [u8; 2528], } const_assert_eq!( size_of::(),