Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointer width agnostic data type sizes #176

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub enum MangoInstruction {

/// DEPRECATED
AddToBasket {
market_index: usize,
market_index: u64,
},

/// DEPRECATED - use Withdraw with allow_borrow = true
Expand Down Expand Up @@ -250,7 +250,7 @@ pub enum MangoInstruction {
},

ConsumeEvents {
limit: usize,
limit: u64,
},

/// Cache perp markets
Expand Down Expand Up @@ -313,15 +313,15 @@ pub enum MangoInstruction {
///
/// Accounts expected (6):
SettlePnl {
market_index: usize,
market_index: u64,
},

/// DEPRECATED - no longer makes sense
/// Use this token's position and deposit to reduce borrows
///
/// Accounts expected by this instruction (5):
SettleBorrow {
token_index: usize,
token_index: u64,
quantity: u64,
},

Expand Down Expand Up @@ -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,
},

Expand Down Expand Up @@ -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,
},

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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,
Expand All @@ -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),
}
}
Expand Down Expand Up @@ -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),
}
}
Expand All @@ -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),
}
}
Expand Down Expand Up @@ -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 })
}
Expand All @@ -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 })
}
Expand Down Expand Up @@ -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<Instruction, ProgramError> {
let mut accounts = vec![
Expand Down
35 changes: 20 additions & 15 deletions program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -1649,7 +1649,7 @@ impl Processor {
check_eq!(&quote_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)?;
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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)?;
}
Expand Down Expand Up @@ -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)?;
}
Expand Down Expand Up @@ -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)?;
}
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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,
)
}
Expand All @@ -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, 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");
Expand Down
22 changes: 11 additions & 11 deletions program/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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)?;
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -839,7 +839,7 @@ impl HealthCache {
open_orders: &[Option<T>],
) -> 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],
Expand All @@ -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];

Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -1510,7 +1510,7 @@ impl MangoAccount {
packed_open_orders_ais: &'a [AccountInfo<'b>],
) -> MangoResult<Vec<Option<&'a AccountInfo<'b>>>> {
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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion program/tests/program_test/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions program/tests/program_test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()];
Expand Down