From ae937efc4b5ec2dd51fbb909b0db3236355d0f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Pov=C5=A1i=C4=8D?= Date: Tue, 19 Apr 2022 14:18:50 +0200 Subject: [PATCH 1/2] convert usize to fixed u64 --- program/src/instruction.rs | 36 +++++++++++++-------------- program/src/processor.rs | 30 +++++++++++----------- program/src/state.rs | 22 ++++++++-------- program/tests/program_test/cookies.rs | 2 +- program/tests/program_test/mod.rs | 4 +-- 5 files changed, 47 insertions(+), 47 deletions(-) diff --git a/program/src/instruction.rs b/program/src/instruction.rs index c9ce5913..4775bd58 100644 --- a/program/src/instruction.rs +++ b/program/src/instruction.rs @@ -109,7 +109,7 @@ pub enum MangoInstruction { /// DEPRECATED AddToBasket { - market_index: usize, + market_index: u64, }, /// DEPRECATED - use Withdraw with allow_borrow = true @@ -250,7 +250,7 @@ pub enum MangoInstruction { }, ConsumeEvents { - limit: usize, + limit: u64, }, /// Cache perp markets @@ -313,7 +313,7 @@ pub enum MangoInstruction { /// /// Accounts expected (6): SettlePnl { - market_index: usize, + market_index: u64, }, /// DEPRECATED - no longer makes sense @@ -321,7 +321,7 @@ pub enum MangoInstruction { /// /// Accounts expected by this instruction (5): SettleBorrow { - token_index: usize, + token_index: u64, quantity: u64, }, @@ -399,9 +399,9 @@ pub enum MangoInstruction { /// 7+MAX_PAIRS... `[]` liqor_open_orders_ais - Liqor open orders accs LiquidateTokenAndPerp { asset_type: AssetType, - asset_index: usize, + asset_index: u64, liab_type: AssetType, - liab_index: usize, + liab_index: u64, max_liab_transfer: I80F48, }, @@ -454,7 +454,7 @@ pub enum MangoInstruction { /// 12+... `[]` liqor_open_orders_ais - Liqor open orders accs ResolvePerpBankruptcy { // 30 - liab_index: usize, + liab_index: u64, max_liab_transfer: I80F48, }, @@ -1119,7 +1119,7 @@ impl MangoInstruction { } 5 => { let market_index = array_ref![data, 0, 8]; - MangoInstruction::AddToBasket { market_index: usize::from_le_bytes(*market_index) } + MangoInstruction::AddToBasket { market_index: u64::from_le_bytes(*market_index) } } 6 => { let quantity = array_ref![data, 0, 8]; @@ -1196,7 +1196,7 @@ impl MangoInstruction { } 15 => { let data_arr = array_ref![data, 0, 8]; - MangoInstruction::ConsumeEvents { limit: usize::from_le_bytes(*data_arr) } + MangoInstruction::ConsumeEvents { limit: u64::from_le_bytes(*data_arr) } } 16 => MangoInstruction::CachePerpMarkets, 17 => MangoInstruction::UpdateFunding, @@ -1222,14 +1222,14 @@ impl MangoInstruction { 22 => { let data_arr = array_ref![data, 0, 8]; - MangoInstruction::SettlePnl { market_index: usize::from_le_bytes(*data_arr) } + MangoInstruction::SettlePnl { market_index: u64::from_le_bytes(*data_arr) } } 23 => { let data = array_ref![data, 0, 16]; let (token_index, quantity) = array_refs![data, 8, 8]; MangoInstruction::SettleBorrow { - token_index: usize::from_le_bytes(*token_index), + token_index: u64::from_le_bytes(*token_index), quantity: u64::from_le_bytes(*quantity), } } @@ -1257,9 +1257,9 @@ impl MangoInstruction { MangoInstruction::LiquidateTokenAndPerp { asset_type: AssetType::try_from(u8::from_le_bytes(*asset_type)).unwrap(), - asset_index: usize::from_le_bytes(*asset_index), + asset_index: u64::from_le_bytes(*asset_index), liab_type: AssetType::try_from(u8::from_le_bytes(*liab_type)).unwrap(), - liab_index: usize::from_le_bytes(*liab_index), + liab_index: u64::from_le_bytes(*liab_index), max_liab_transfer: I80F48::from_le_bytes(*max_liab_transfer), } } @@ -1276,7 +1276,7 @@ impl MangoInstruction { let (liab_index, max_liab_transfer) = array_refs![data, 8, 16]; MangoInstruction::ResolvePerpBankruptcy { - liab_index: usize::from_le_bytes(*liab_index), + liab_index: u64::from_le_bytes(*liab_index), max_liab_transfer: I80F48::from_le_bytes(*max_liab_transfer), } } @@ -2314,7 +2314,7 @@ pub fn consume_events( mango_acc_pks.sort(); let mango_accounts = mango_acc_pks.into_iter().map(|pk| AccountMeta::new(*pk, false)); let accounts = fixed_accounts.into_iter().chain(mango_accounts).collect(); - let instr = MangoInstruction::ConsumeEvents { limit }; + let instr = MangoInstruction::ConsumeEvents { limit: limit as u64 }; let data = instr.pack(); Ok(Instruction { program_id: *program_id, accounts, data }) } @@ -2337,7 +2337,7 @@ pub fn settle_pnl( AccountMeta::new_readonly(*root_bank_pk, false), AccountMeta::new(*node_bank_pk, false), ]; - let instr = MangoInstruction::SettlePnl { market_index }; + let instr = MangoInstruction::SettlePnl { market_index: market_index as u64 }; let data = instr.pack(); Ok(Instruction { program_id: *program_id, accounts, data }) } @@ -2838,9 +2838,9 @@ pub fn liquidate_token_and_perp( liqee_open_orders_pks: &[Pubkey], liqor_open_orders_pks: &[Pubkey], asset_type: AssetType, - asset_index: usize, + asset_index: u64, liab_type: AssetType, - liab_index: usize, + liab_index: u64, max_liab_transfer: I80F48, ) -> Result { let mut accounts = vec![ diff --git a/program/src/processor.rs b/program/src/processor.rs index d5bc736d..9239c964 100644 --- a/program/src/processor.rs +++ b/program/src/processor.rs @@ -429,7 +429,7 @@ impl Processor { let market_index = mango_group.find_oracle_index(oracle_ai.key).ok_or(throw!())?; // This will catch the issue if oracle_ai.key == Pubkey::Default - check!(market_index < mango_group.num_oracles, MangoErrorCode::InvalidParam)?; + check!(market_index < mango_group.num_oracles as usize, MangoErrorCode::InvalidParam)?; // Make sure spot market at this index not already initialized check!( @@ -537,7 +537,7 @@ impl Processor { } } - let oracle_index = mango_group.num_oracles; + let oracle_index = mango_group.num_oracles as usize; mango_group.oracles[oracle_index] = *oracle_ai.key; mango_group.num_oracles += 1; @@ -622,7 +622,7 @@ impl Processor { let market_index = mango_group.find_oracle_index(oracle_ai.key).ok_or(throw!())?; // This will catch the issue if oracle_ai.key == Pubkey::Default - check!(market_index < mango_group.num_oracles, MangoErrorCode::InvalidParam)?; + check!(market_index < mango_group.num_oracles as usize, MangoErrorCode::InvalidParam)?; // Make sure perp market at this index not already initialized check!(mango_group.perp_markets[market_index].is_empty(), MangoErrorCode::InvalidParam)?; @@ -756,7 +756,7 @@ impl Processor { let market_index = mango_group.find_oracle_index(oracle_ai.key).ok_or(throw!())?; // This will catch the issue if oracle_ai.key == Pubkey::Default - check!(market_index < mango_group.num_oracles, MangoErrorCode::InvalidParam)?; + check!(market_index < mango_group.num_oracles as usize, MangoErrorCode::InvalidParam)?; // Make sure perp market at this index not already initialized check!(mango_group.perp_markets[market_index].is_empty(), MangoErrorCode::InvalidParam)?; @@ -1649,7 +1649,7 @@ impl Processor { check_eq!("e_node_bank.vault, quote_vault_ai.key, MangoErrorCode::InvalidVault)?; // Fix the margin basket incase there are empty ones; main benefit is freeing up basket space - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize { if mango_account.in_margin_basket[i] { let open_orders = load_open_orders(&open_orders_ais[i])?; mango_account.update_basket(i, &open_orders)?; @@ -1927,7 +1927,7 @@ impl Processor { let open_orders_accounts = load_open_orders_accounts(&open_orders_ais)?; // Fix the margin basket incase there are empty ones; main benefit is freeing up basket space - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize { if mango_account.in_margin_basket[i] { let open_orders = load_open_orders(open_orders_ais[i].unwrap())?; mango_account.update_basket(i, &open_orders)?; @@ -3458,7 +3458,7 @@ impl Processor { )?; // Make sure orders are cancelled for perps and check orders - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize { if liqee_active_assets.perps[i] { check!(liqee_ma.perp_accounts[i].has_no_open_orders(), MangoErrorCode::Default)?; } @@ -3676,7 +3676,7 @@ impl Processor { )?; // Make sure orders are cancelled for perps and check orders - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize { if liqee_active_assets.perps[i] { check!(liqee_ma.perp_accounts[i].has_no_open_orders(), MangoErrorCode::Default)?; } @@ -4000,7 +4000,7 @@ impl Processor { liqor_ma.perp_accounts[market_index].settle_funding(cache); // Make sure orders are cancelled for perps before liquidation - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize { if liqee_active_assets.perps[i] { check!(liqee_ma.perp_accounts[i].has_no_open_orders(), MangoErrorCode::Default)?; } @@ -5564,7 +5564,7 @@ impl Processor { let mut mango_account = MangoAccount::load_mut_checked(mango_account_ai, program_id, mango_group_ai.key)?; - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize { check_eq!( open_orders_ais[i].key, &mango_account.spot_open_orders[i], @@ -6349,7 +6349,7 @@ impl Processor { } MangoInstruction::ConsumeEvents { limit } => { msg!("Mango: ConsumeEvents limit={}", limit); - Self::consume_events(program_id, accounts, limit) + Self::consume_events(program_id, accounts, limit as usize) } MangoInstruction::CachePerpMarkets => { msg!("Mango: CachePerpMarkets"); @@ -6366,7 +6366,7 @@ impl Processor { } MangoInstruction::SettlePnl { market_index } => { msg!("Mango: SettlePnl"); - Self::settle_pnl(program_id, accounts, market_index) + Self::settle_pnl(program_id, accounts, market_index as usize) } MangoInstruction::SettleBorrow { .. } => { msg!("Mango: SettleBorrow DEPRECATED"); @@ -6396,9 +6396,9 @@ impl Processor { program_id, accounts, asset_type, - asset_index, + asset_index as usize, liab_type, - liab_index, + liab_index as usize, max_liab_transfer, ) } @@ -6412,7 +6412,7 @@ impl Processor { } MangoInstruction::ResolvePerpBankruptcy { liab_index, max_liab_transfer } => { msg!("Mango: ResolvePerpBankruptcy"); - Self::resolve_perp_bankruptcy(program_id, accounts, liab_index, max_liab_transfer) + Self::resolve_perp_bankruptcy(program_id, accounts, liab_index as usize, max_liab_transfer) } MangoInstruction::ResolveTokenBankruptcy { max_liab_transfer } => { msg!("Mango: ResolveTokenBankruptcy"); diff --git a/program/src/state.rs b/program/src/state.rs index c5f09669..90bc1534 100644 --- a/program/src/state.rs +++ b/program/src/state.rs @@ -191,7 +191,7 @@ impl PerpMarketInfo { #[repr(C)] pub struct MangoGroup { pub meta_data: MetaData, - pub num_oracles: usize, // incremented every time add_oracle is called + pub num_oracles: u64, // incremented every time add_oracle is called pub tokens: [TokenInfo; MAX_TOKENS], pub spot_markets: [SpotMarketInfo; MAX_PAIRS], @@ -703,7 +703,7 @@ impl MangoCache { active_assets: &UserActiveAssets, now_ts: u64, ) -> MangoResult<()> { - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize { if active_assets.spot[i] || active_assets.perps[i] { self.price_cache[i].check_valid(&mango_group, now_ts)?; } @@ -741,7 +741,7 @@ impl UserActiveAssets { ) -> Self { let mut spot = [false; MAX_PAIRS]; let mut perps = [false; MAX_PAIRS]; - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize{ spot[i] = !mango_group.spot_markets[i].is_empty() && (mango_account.in_margin_basket[i] || !mango_account.deposits[i].is_zero() @@ -805,7 +805,7 @@ impl HealthCache { open_orders_ais: &[AccountInfo; MAX_PAIRS], ) -> MangoResult<()> { self.quote = mango_account.get_net(&mango_cache.root_bank_cache[QUOTE_INDEX], QUOTE_INDEX); - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize{ if self.active_assets.spot[i] { self.spot[i] = mango_account.get_spot_val( &mango_cache.root_bank_cache[i], @@ -839,7 +839,7 @@ impl HealthCache { open_orders: &[Option], ) -> MangoResult<()> { self.quote = mango_account.get_net(&mango_cache.root_bank_cache[QUOTE_INDEX], QUOTE_INDEX); - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize{ if self.active_assets.spot[i] { self.spot[i] = mango_account.get_spot_val( &mango_cache.root_bank_cache[i], @@ -866,7 +866,7 @@ impl HealthCache { None => { // apply weights, cache result, return health let mut health = self.quote; - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize { let spot_market_info = &mango_group.spot_markets[i]; let perp_market_info = &mango_group.perp_markets[i]; @@ -924,7 +924,7 @@ impl HealthCache { } else { (self.quote, ZERO_I80F48) }; - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize { let spot_market_info = &mango_group.spot_markets[i]; let perp_market_info = &mango_group.perp_markets[i]; @@ -1446,7 +1446,7 @@ impl MangoAccount { return false; } - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize { if self.deposits[i] > DUST_THRESHOLD { return false; } @@ -1477,7 +1477,7 @@ impl MangoAccount { return false; } - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize { if self.borrows[i] > DUST_THRESHOLD { return false; } @@ -1510,7 +1510,7 @@ impl MangoAccount { packed_open_orders_ais: &'a [AccountInfo<'b>], ) -> MangoResult>>> { let mut unpacked = vec![None; MAX_PAIRS]; - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize { if self.in_margin_basket[i] { unpacked[i] = Some(self.checked_unpack_open_orders_single( mango_group, @@ -1526,7 +1526,7 @@ impl MangoAccount { mango_group: &MangoGroup, open_orders_ais: &[AccountInfo; MAX_PAIRS], ) -> MangoResult { - for i in 0..mango_group.num_oracles { + for i in 0..mango_group.num_oracles as usize { if self.in_margin_basket[i] { check_eq!( open_orders_ais[i].key, diff --git a/program/tests/program_test/cookies.rs b/program/tests/program_test/cookies.rs index a2ed3987..076ef0c2 100644 --- a/program/tests/program_test/cookies.rs +++ b/program/tests/program_test/cookies.rs @@ -218,7 +218,7 @@ impl MangoGroupCookie { test.cache_all_prices( mango_group, &self.address, - &mango_group.oracles[0..mango_group.num_oracles], + &mango_group.oracles[0..mango_group.num_oracles as usize], ) .await; diff --git a/program/tests/program_test/mod.rs b/program/tests/program_test/mod.rs index f3b9b8c9..5cf93971 100644 --- a/program/tests/program_test/mod.rs +++ b/program/tests/program_test/mod.rs @@ -1889,9 +1889,9 @@ impl MangoProgramTest { &liqee_mango_account.spot_open_orders, &liqor_mango_account.spot_open_orders, asset_type, - asset_index, + asset_index as u64, liab_type, - liab_index, + liab_index as u64, max_liab_transfer, ) .unwrap()]; From be0c8f064bd6b3d3857a41a53e3c9f450a5c2b11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Pov=C5=A1i=C4=8D?= Date: Tue, 19 Apr 2022 15:09:48 +0200 Subject: [PATCH 2/2] cargo fmt --- program/src/processor.rs | 7 ++++++- program/src/state.rs | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/program/src/processor.rs b/program/src/processor.rs index 9239c964..9160efe1 100644 --- a/program/src/processor.rs +++ b/program/src/processor.rs @@ -6412,7 +6412,12 @@ impl Processor { } MangoInstruction::ResolvePerpBankruptcy { liab_index, max_liab_transfer } => { msg!("Mango: ResolvePerpBankruptcy"); - Self::resolve_perp_bankruptcy(program_id, accounts, liab_index as usize, max_liab_transfer) + Self::resolve_perp_bankruptcy( + program_id, + accounts, + liab_index as usize, + max_liab_transfer, + ) } MangoInstruction::ResolveTokenBankruptcy { max_liab_transfer } => { msg!("Mango: ResolveTokenBankruptcy"); diff --git a/program/src/state.rs b/program/src/state.rs index 90bc1534..53ae7e7a 100644 --- a/program/src/state.rs +++ b/program/src/state.rs @@ -741,7 +741,7 @@ impl UserActiveAssets { ) -> Self { let mut spot = [false; MAX_PAIRS]; let mut perps = [false; MAX_PAIRS]; - for i in 0..mango_group.num_oracles as usize{ + for i in 0..mango_group.num_oracles as usize { spot[i] = !mango_group.spot_markets[i].is_empty() && (mango_account.in_margin_basket[i] || !mango_account.deposits[i].is_zero() @@ -805,7 +805,7 @@ impl HealthCache { open_orders_ais: &[AccountInfo; MAX_PAIRS], ) -> MangoResult<()> { self.quote = mango_account.get_net(&mango_cache.root_bank_cache[QUOTE_INDEX], QUOTE_INDEX); - for i in 0..mango_group.num_oracles as usize{ + for i in 0..mango_group.num_oracles as usize { if self.active_assets.spot[i] { self.spot[i] = mango_account.get_spot_val( &mango_cache.root_bank_cache[i], @@ -839,7 +839,7 @@ impl HealthCache { open_orders: &[Option], ) -> MangoResult<()> { self.quote = mango_account.get_net(&mango_cache.root_bank_cache[QUOTE_INDEX], QUOTE_INDEX); - for i in 0..mango_group.num_oracles as usize{ + for i in 0..mango_group.num_oracles as usize { if self.active_assets.spot[i] { self.spot[i] = mango_account.get_spot_val( &mango_cache.root_bank_cache[i],