Skip to content

Commit

Permalink
Program: add a tier string in banks (not used program side)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnyser committed Aug 9, 2024
1 parent 69d8660 commit 37dfb6e
Show file tree
Hide file tree
Showing 8 changed files with 617 additions and 1,204 deletions.
27 changes: 25 additions & 2 deletions mango_v4.json
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,10 @@
{
"name": "collateralFeePerDay",
"type": "f32"
},
{
"name": "tier",
"type": "string"
}
]
},
Expand Down Expand Up @@ -1153,6 +1157,12 @@
"type": {
"option": "bool"
}
},
{
"name": "tierOpt",
"type": {
"option": "string"
}
}
]
},
Expand Down Expand Up @@ -7821,12 +7831,21 @@
],
"type": "f32"
},
{
"name": "tier",
"type": {
"array": [
"u8",
4
]
}
},
{
"name": "reserved",
"type": {
"array": [
"u8",
1900
1896
]
}
}
Expand Down Expand Up @@ -10143,9 +10162,13 @@
"type": {
"array": [
"u8",
119
111
]
}
},
{
"name": "forceAlign",
"type": "u64"
}
]
}
Expand Down
7 changes: 7 additions & 0 deletions programs/mango-v4/src/instructions/token_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub fn token_edit(
disable_asset_liquidation_opt: Option<bool>,
collateral_fee_per_day: Option<f32>,
force_withdraw_opt: Option<bool>,
tier_opt: Option<String>,
) -> Result<()> {
let group = ctx.accounts.group.load()?;

Expand Down Expand Up @@ -323,6 +324,12 @@ pub fn token_edit(
require_group_admin = true;
};

if let Some(tier) = tier_opt.as_ref() {
msg!("Tier: old - {:?}, new - {:?}", bank.tier, tier);
bank.tier = fill_from_str(&tier)?;
require_group_admin = true;
};

if let Some(force_close) = force_close_opt {
if force_close {
require!(bank.reduce_only > 0, MangoError::SomeError);
Expand Down
4 changes: 3 additions & 1 deletion programs/mango-v4/src/instructions/token_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub fn token_register(
platform_liquidation_fee: f32,
disable_asset_liquidation: bool,
collateral_fee_per_day: f32,
tier: String,
) -> Result<()> {
require_neq!(token_index, TokenIndex::MAX);

Expand Down Expand Up @@ -126,7 +127,8 @@ pub fn token_register(
collected_liquidation_fees: I80F48::ZERO,
collected_collateral_fees: I80F48::ZERO,
collateral_fee_per_day,
reserved: [0; 1900],
tier: fill_from_str(&tier)?,
reserved: [0; 1896],
};

let oracle_ref = &AccountInfoRef::borrow(ctx.accounts.oracle.as_ref())?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ pub fn token_register_trustless(
collected_liquidation_fees: I80F48::ZERO,
collected_collateral_fees: I80F48::ZERO,
collateral_fee_per_day: 0.0, // TODO
reserved: [0; 1900],
tier: fill_from_str("C")?,
reserved: [0; 1896],
};
let oracle_ref = &AccountInfoRef::borrow(ctx.accounts.oracle.as_ref())?;
if let Ok(oracle_price) = bank.oracle_price(&OracleAccountInfos::from_reader(oracle_ref), None)
Expand Down
4 changes: 4 additions & 0 deletions programs/mango-v4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ pub mod mango_v4 {
platform_liquidation_fee: f32,
disable_asset_liquidation: bool,
collateral_fee_per_day: f32,
tier: String,
) -> Result<()> {
#[cfg(feature = "enable-gpl")]
instructions::token_register(
Expand Down Expand Up @@ -202,6 +203,7 @@ pub mod mango_v4 {
platform_liquidation_fee,
disable_asset_liquidation,
collateral_fee_per_day,
tier,
)?;
Ok(())
}
Expand Down Expand Up @@ -260,6 +262,7 @@ pub mod mango_v4 {
disable_asset_liquidation_opt: Option<bool>,
collateral_fee_per_day_opt: Option<f32>,
force_withdraw_opt: Option<bool>,
tier_opt: Option<String>,
) -> Result<()> {
#[cfg(feature = "enable-gpl")]
instructions::token_edit(
Expand Down Expand Up @@ -305,6 +308,7 @@ pub mod mango_v4 {
disable_asset_liquidation_opt,
collateral_fee_per_day_opt,
force_withdraw_opt,
tier_opt,
)?;
Ok(())
}
Expand Down
17 changes: 14 additions & 3 deletions programs/mango-v4/src/state/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,11 @@ pub struct Bank {
/// The daily collateral fees rate for fully utilized collateral.
pub collateral_fee_per_day: f32,

#[derivative(Debug(format_with = "util::format_zero_terminated_utf8_bytes"))]
pub tier: [u8; 4],

#[derivative(Debug = "ignore")]
pub reserved: [u8; 1900],
pub reserved: [u8; 1896],
}
const_assert_eq!(
size_of::<Bank>(),
Expand Down Expand Up @@ -271,7 +274,8 @@ const_assert_eq!(
+ 8
+ 16 * 4
+ 4
+ 1900
+ 4
+ 1896
);
const_assert_eq!(size_of::<Bank>(), 3064);
const_assert_eq!(size_of::<Bank>() % 8, 0);
Expand Down Expand Up @@ -382,7 +386,8 @@ impl Bank {
zero_util_rate: existing_bank.zero_util_rate,
platform_liquidation_fee: existing_bank.platform_liquidation_fee,
collateral_fee_per_day: existing_bank.collateral_fee_per_day,
reserved: [0; 1900],
tier: existing_bank.tier,
reserved: [0; 1896],
}
}

Expand Down Expand Up @@ -440,6 +445,12 @@ impl Bank {
.trim_matches(char::from(0))
}

pub fn tier(&self) -> &str {
std::str::from_utf8(&self.tier)
.unwrap()
.trim_matches(char::from(0))
}

pub fn are_deposits_reduce_only(&self) -> bool {
self.reduce_only == 1
}
Expand Down
2 changes: 2 additions & 0 deletions programs/mango-v4/tests/program_test/mango_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,7 @@ impl ClientInstruction for TokenRegisterInstruction {
platform_liquidation_fee: self.platform_liquidation_fee,
disable_asset_liquidation: false,
collateral_fee_per_day: 0.0,
tier: "A".to_string(),
};

let bank = Pubkey::find_program_address(
Expand Down Expand Up @@ -1324,6 +1325,7 @@ pub fn token_edit_instruction_default() -> mango_v4::instruction::TokenEdit {
disable_asset_liquidation_opt: None,
collateral_fee_per_day_opt: None,
force_withdraw_opt: None,
tier_opt: None,
}
}

Expand Down
Loading

0 comments on commit 37dfb6e

Please sign in to comment.