Skip to content
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion programs/drift/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ enumflags2 = "0.6.4"
phoenix-v1 = { git = "https://github.com/jordy25519/phoenix-v1", branch = "master", features = ["no-entrypoint"] }
solana-security-txt = "1.1.0"
static_assertions = "1.1.0"
drift-macros = { git = "https://github.com/drift-labs/drift-macros.git", rev = "c57d87" }
drift-macros = { path = "../../../drift-macros" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this goes outside the repo root, will it still work in ci?

switchboard = { path = "../switchboard", features = ["no-entrypoint"] }
openbook-v2-light = { path = "../openbook_v2", features = ["no-entrypoint"] }
switchboard-on-demand = { path = "../switchboard-on-demand", features = ["no-entrypoint"] }
Expand Down
130 changes: 71 additions & 59 deletions programs/drift/src/controller/amm/tests.rs

Large diffs are not rendered by default.

37 changes: 20 additions & 17 deletions programs/drift/src/controller/insurance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,25 +220,26 @@ pub fn apply_rebase_to_insurance_fund_stake(
insurance_fund_stake: &mut InsuranceFundStake,
spot_market: &mut SpotMarket,
) -> DriftResult {
if spot_market.insurance_fund.shares_base != insurance_fund_stake.if_base {
if spot_market.insurance_fund.shares_base != insurance_fund_stake.if_base.as_u128() {
validate!(
spot_market.insurance_fund.shares_base > insurance_fund_stake.if_base,
spot_market.insurance_fund.shares_base > insurance_fund_stake.if_base.as_u128(),
ErrorCode::InvalidIFRebase,
"Rebase expo out of bounds"
)?;

let expo_diff = (spot_market.insurance_fund.shares_base - insurance_fund_stake.if_base)
.cast::<u32>()?;
let expo_diff = (spot_market.insurance_fund.shares_base
- insurance_fund_stake.if_base.as_u128())
.cast::<u32>()?;

let rebase_divisor = 10_u128.pow(expo_diff);

msg!(
"rebasing insurance fund stake: base: {} -> {} ",
insurance_fund_stake.if_base,
insurance_fund_stake.if_base.as_u128(),
spot_market.insurance_fund.shares_base,
);

insurance_fund_stake.if_base = spot_market.insurance_fund.shares_base;
insurance_fund_stake.if_base = spot_market.insurance_fund.shares_base.into();

let old_if_shares = insurance_fund_stake.unchecked_if_shares();
let new_if_shares = old_if_shares.safe_div(rebase_divisor)?;
Expand All @@ -252,7 +253,9 @@ pub fn apply_rebase_to_insurance_fund_stake(

insurance_fund_stake.last_withdraw_request_shares = insurance_fund_stake
.last_withdraw_request_shares
.safe_div(rebase_divisor)?;
.as_u128()
.safe_div(rebase_divisor)?
.into();
}

Ok(())
Expand All @@ -267,7 +270,7 @@ pub fn request_remove_insurance_fund_stake(
now: i64,
) -> DriftResult {
msg!("n_shares {}", n_shares);
insurance_fund_stake.last_withdraw_request_shares = n_shares;
insurance_fund_stake.last_withdraw_request_shares = n_shares.into();

apply_rebase_to_insurance_fund(insurance_vault_amount, spot_market)?;
apply_rebase_to_insurance_fund_stake(insurance_fund_stake, spot_market)?;
Expand All @@ -277,22 +280,22 @@ pub fn request_remove_insurance_fund_stake(
let user_if_shares_before = spot_market.insurance_fund.user_shares;

validate!(
insurance_fund_stake.last_withdraw_request_shares
insurance_fund_stake.last_withdraw_request_shares.as_u128()
<= insurance_fund_stake.checked_if_shares(spot_market)?,
ErrorCode::InvalidInsuranceUnstakeSize,
"last_withdraw_request_shares exceeds if_shares {} > {}",
insurance_fund_stake.last_withdraw_request_shares,
insurance_fund_stake.last_withdraw_request_shares.as_u128(),
insurance_fund_stake.checked_if_shares(spot_market)?
)?;

validate!(
insurance_fund_stake.if_base == spot_market.insurance_fund.shares_base,
insurance_fund_stake.if_base.as_u128() == spot_market.insurance_fund.shares_base,
ErrorCode::InvalidIFRebase,
"if stake base != spot market base"
)?;

insurance_fund_stake.last_withdraw_request_value = if_shares_to_vault_amount(
insurance_fund_stake.last_withdraw_request_shares,
insurance_fund_stake.last_withdraw_request_shares.as_u128(),
spot_market.insurance_fund.total_shares,
insurance_vault_amount,
)?
Expand Down Expand Up @@ -351,13 +354,13 @@ pub fn cancel_request_remove_insurance_fund_stake(
let user_if_shares_before = spot_market.insurance_fund.user_shares;

validate!(
insurance_fund_stake.if_base == spot_market.insurance_fund.shares_base,
insurance_fund_stake.if_base.as_u128() == spot_market.insurance_fund.shares_base,
ErrorCode::InvalidIFRebase,
"if stake base != spot market base"
)?;

validate!(
insurance_fund_stake.last_withdraw_request_shares != 0,
insurance_fund_stake.last_withdraw_request_shares.as_u128() != 0,
ErrorCode::InvalidIFUnstakeCancel,
"No withdraw request in progress"
)?;
Expand Down Expand Up @@ -403,7 +406,7 @@ pub fn cancel_request_remove_insurance_fund_stake(
user_if_shares_after: spot_market.insurance_fund.user_shares,
});

insurance_fund_stake.last_withdraw_request_shares = 0;
insurance_fund_stake.last_withdraw_request_shares = 0.into();
insurance_fund_stake.last_withdraw_request_value = 0;
insurance_fund_stake.last_withdraw_request_ts = now;

Expand Down Expand Up @@ -432,7 +435,7 @@ pub fn remove_insurance_fund_stake(
let total_if_shares_before = spot_market.insurance_fund.total_shares;
let user_if_shares_before = spot_market.insurance_fund.user_shares;

let n_shares = insurance_fund_stake.last_withdraw_request_shares;
let n_shares = insurance_fund_stake.last_withdraw_request_shares.as_u128();

validate!(
n_shares > 0,
Expand Down Expand Up @@ -469,7 +472,7 @@ pub fn remove_insurance_fund_stake(
spot_market.insurance_fund.user_shares.safe_sub(n_shares)?;

// reset insurance_fund_stake withdraw request info
insurance_fund_stake.last_withdraw_request_shares = 0;
insurance_fund_stake.last_withdraw_request_shares = 0.into();
insurance_fund_stake.last_withdraw_request_value = 0;
insurance_fund_stake.last_withdraw_request_ts = now;

Expand Down
Loading