Skip to content

Commit

Permalink
implement fallbacks for perp_liq_base_or_positive_pnl
Browse files Browse the repository at this point in the history
  • Loading branch information
Lou-Kamades committed Apr 16, 2024
1 parent 22af31c commit 20eef53
Show file tree
Hide file tree
Showing 5 changed files with 381 additions and 6 deletions.
2 changes: 1 addition & 1 deletion programs/mango-v4/src/health/account_retriever.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ impl<'a, 'info> ScanningAccountRetriever<'a, 'info> {
let sol_oracle_index = ais[fallback_oracles_start..]
.iter()
.position(|o| o.key == &pyth_mainnet_sol_oracle::ID);

Ok(Self {
banks_and_oracles: ScannedBanksAndOracles {
banks: AccountInfoRefMut::borrow_slice(&ais[..n_banks])?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,16 @@ pub fn perp_liq_base_or_positive_pnl(

// Get oracle price for market. Price is validated inside
let oracle_ref = &AccountInfoRef::borrow(ctx.accounts.oracle.as_ref())?;
let fallback_opt = if perp_market.fallback_oracle != Pubkey::default() {
ctx.remaining_accounts
.iter()
.find(|a| a.key == &perp_market.fallback_oracle)
.map(|k| AccountInfoRef::borrow(k).unwrap())
} else {
None
};
let oracle_price = perp_market.oracle_price(
&OracleAccountInfos::from_reader(oracle_ref),
&OracleAccountInfos::from_reader_with_fallback(oracle_ref, fallback_opt.as_ref()),
None, // checked in health
)?;

Expand Down
3 changes: 1 addition & 2 deletions programs/mango-v4/tests/cases/test_liq_perps_bankruptcy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ async fn test_liq_perps_bankruptcy() -> Result<(), TransportError> {
/// Copy of the above test with an added fallback oracle + staleness instructions
async fn test_liq_perps_bankruptcy_stale_oracle() -> Result<(), TransportError> {
let mut test_builder = TestContextBuilder::new();
test_builder.test().set_compute_max_units(400_000); // PerpLiqNegativePnlOrBankruptcy takes a lot of CU
test_builder.test().set_compute_max_units(200_000); // PerpLiqNegativePnlOrBankruptcy takes a lot of CU
let context = test_builder.start_default().await;
let solana = &context.solana.clone();

Expand Down Expand Up @@ -836,7 +836,6 @@ async fn test_liq_perps_bankruptcy_stale_oracle() -> Result<(), TransportError>
is_writable: false,
is_signer: false,
};

assert!(send_tx_with_extra_accounts(
solana,
PerpLiqNegativePnlOrBankruptcyInstruction {
Expand Down
6 changes: 4 additions & 2 deletions programs/mango-v4/tests/cases/test_liq_perps_force_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ async fn test_liq_perps_force_cancel_stale_oracle() -> Result<(), TransportError
.is_err());

// can withdraw with fallback
send_tx_with_extra_accounts(
assert!(send_tx_with_extra_accounts(
solana,
TokenWithdrawInstruction {
amount: 1,
Expand All @@ -420,7 +420,9 @@ async fn test_liq_perps_force_cancel_stale_oracle() -> Result<(), TransportError
vec![fallback_oracle_meta.clone()],
)
.await
.unwrap();
.unwrap()
.result
.is_ok());

Ok(())
}
Loading

0 comments on commit 20eef53

Please sign in to comment.