Skip to content

Commit

Permalink
program: add-spot-borrow-insurance-limits
Browse files Browse the repository at this point in the history
  • Loading branch information
0xbigz committed Jun 6, 2024
1 parent 8f45a76 commit e2adcc4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 7 deletions.
48 changes: 45 additions & 3 deletions programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub fn handle_initialize_spot_market(
return Err(ErrorCode::InvalidInsuranceFundAuthority.into());
}

validate_borrow_rate(optimal_utilization, optimal_borrow_rate, max_borrow_rate)?;
validate_borrow_rate(optimal_utilization, optimal_borrow_rate, max_borrow_rate, 0)?;

Check warning on line 131 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L131

Added line #L131 was not covered by tests

let spot_market_index = get_then_update_id!(state, number_of_spot_markets);

Expand Down Expand Up @@ -278,7 +278,9 @@ pub fn handle_initialize_spot_market(
flash_loan_initial_token_amount: 0,
total_swap_fee: 0,
scale_initial_asset_weight_start,
padding: [0; 48],
max_token_borrows: 0,
min_borrow_rate: 0,
padding: [0; 36],

Check warning on line 283 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L283

Added line #L283 was not covered by tests
insurance_fund: InsuranceFund {
vault: *ctx.accounts.insurance_fund_vault.to_account_info().key,
unstaking_period: THIRTEEN_DAY,
Expand Down Expand Up @@ -2243,9 +2245,15 @@ pub fn handle_update_spot_market_borrow_rate(
optimal_utilization: u32,
optimal_borrow_rate: u32,
max_borrow_rate: u32,
min_borrow_rate: Option<u32>,
) -> Result<()> {
let spot_market = &mut load_mut!(ctx.accounts.spot_market)?;
validate_borrow_rate(optimal_utilization, optimal_borrow_rate, max_borrow_rate)?;
validate_borrow_rate(
optimal_utilization,
optimal_borrow_rate,
max_borrow_rate,
min_borrow_rate.unwrap_or(spot_market.min_borrow_rate),

Check warning on line 2255 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L2252-L2255

Added lines #L2252 - L2255 were not covered by tests
)?;

msg!(
"spot_market.optimal_utilization: {:?} -> {:?}",
Expand All @@ -2268,6 +2276,11 @@ pub fn handle_update_spot_market_borrow_rate(
spot_market.optimal_utilization = optimal_utilization;
spot_market.optimal_borrow_rate = optimal_borrow_rate;
spot_market.max_borrow_rate = max_borrow_rate;

if min_borrow_rate.is_some() {
spot_market.min_borrow_rate = min_borrow_rate.unwrap();

Check warning on line 2281 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L2280-L2281

Added lines #L2280 - L2281 were not covered by tests
}

Ok(())
}

Expand All @@ -2290,6 +2303,35 @@ pub fn handle_update_spot_market_max_token_deposits(
Ok(())
}

#[access_control(

Check warning on line 2306 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L2306

Added line #L2306 was not covered by tests
spot_market_valid(&ctx.accounts.spot_market)
)]
pub fn handle_update_spot_market_max_token_borrows(

Check warning on line 2309 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L2309

Added line #L2309 was not covered by tests
ctx: Context<AdminUpdateSpotMarket>,
max_token_borrows: u64,
) -> Result<()> {
let spot_market = &mut load_mut!(ctx.accounts.spot_market)?;

Check warning on line 2313 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L2313

Added line #L2313 was not covered by tests

msg!(

Check warning on line 2315 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L2315

Added line #L2315 was not covered by tests
"spot_market.max_token_borrows: {:?} -> {:?}",
spot_market.max_token_borrows,

Check warning on line 2317 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L2317

Added line #L2317 was not covered by tests
max_token_borrows
);

let current_spot_tokens_borrows: u64 = spot_market.get_borrows()?.cast()?;

Check warning on line 2321 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L2321

Added line #L2321 was not covered by tests

validate!(
current_spot_tokens_borrows <= max_token_borrows,
ErrorCode::InvalidSpotMarketInitialization,

Check warning on line 2325 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L2323-L2325

Added lines #L2323 - L2325 were not covered by tests
"spot borrows {} > max_token_borrows {}",
current_spot_tokens_borrows,
max_token_borrows
)?;

spot_market.max_token_borrows = max_token_borrows;
Ok(())

Check warning on line 2332 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L2331-L2332

Added lines #L2331 - L2332 were not covered by tests
}

#[access_control(
spot_market_valid(&ctx.accounts.spot_market)
)]
Expand Down
2 changes: 2 additions & 0 deletions programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,12 +874,14 @@ pub mod drift {
optimal_utilization: u32,
optimal_borrow_rate: u32,
max_borrow_rate: u32,
min_borrow_rate: Option<u32>,
) -> Result<()> {
handle_update_spot_market_borrow_rate(
ctx,
optimal_utilization,
optimal_borrow_rate,
max_borrow_rate,
min_borrow_rate,
)
}

Expand Down
3 changes: 2 additions & 1 deletion programs/drift/src/math/spot_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ pub fn calculate_accumulated_interest(
utilization
.safe_mul(borrow_rate_slope)?
.safe_div(SPOT_UTILIZATION_PRECISION)?
};
}
.max(spot_market.min_borrow_rate.cast()?);

let time_since_last_update = now
.cast::<u64>()
Expand Down
26 changes: 24 additions & 2 deletions programs/drift/src/state/spot_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,14 @@ pub struct SpotMarket {
/// disabled when 0
/// precision: QUOTE_PRECISION
pub scale_initial_asset_weight_start: u64,
pub padding: [u8; 48],
/// When to begin scaling down the initial asset weight
/// disabled when 0
/// precision: QUOTE_PRECISION
pub max_token_borrows: u64,
/// The min borrow rate for this market when the market regardless of utilization
/// precision: SPOT_RATE_PRECISION
pub min_borrow_rate: u32,
pub padding: [u8; 36],
}

impl Default for SpotMarket {
Expand Down Expand Up @@ -239,7 +246,9 @@ impl Default for SpotMarket {
flash_loan_initial_token_amount: 0,
total_swap_fee: 0,
scale_initial_asset_weight_start: 0,
padding: [0; 48],
max_token_borrows: 0,
min_borrow_rate: 0,
padding: [0; 36],
}
}
}
Expand Down Expand Up @@ -426,6 +435,19 @@ impl SpotMarket {
deposits,
)?;

if self.max_token_borrows > 0 {
let borrows = self.get_borrows()?;
let max_token_borrows = self.max_token_borrows.cast::<u128>()?;

Check warning on line 440 in programs/drift/src/state/spot_market.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/spot_market.rs#L439-L440

Added lines #L439 - L440 were not covered by tests

validate!(
max_token_borrows == 0 || borrows <= max_token_borrows,
ErrorCode::MaxDeposit,

Check warning on line 444 in programs/drift/src/state/spot_market.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/spot_market.rs#L442-L444

Added lines #L442 - L444 were not covered by tests
"max token amount ({}) < borrows ({})",
max_token_borrows,
borrows,

Check warning on line 447 in programs/drift/src/state/spot_market.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/spot_market.rs#L446-L447

Added lines #L446 - L447 were not covered by tests
)?;
}

Ok(())
}

Expand Down
11 changes: 10 additions & 1 deletion programs/drift/src/validation/spot_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub fn validate_borrow_rate(
optimal_utilization: u32,
optimal_borrow_rate: u32,
max_borrow_rate: u32,
min_borrow_rate: u32,
) -> DriftResult {
validate!(
optimal_utilization <= SPOT_UTILIZATION_PRECISION_U32,
Expand All @@ -18,10 +19,18 @@ pub fn validate_borrow_rate(
validate!(
optimal_borrow_rate <= max_borrow_rate,
ErrorCode::InvalidSpotMarketInitialization,
"For spot market, optimal borrow rate ({}) must be < max borrow rate ({})",
"For spot market, optimal borrow rate ({}) must be <= max borrow rate ({})",
optimal_borrow_rate,
max_borrow_rate
)?;

validate!(
optimal_borrow_rate >= min_borrow_rate,
ErrorCode::InvalidSpotMarketInitialization,

Check warning on line 29 in programs/drift/src/validation/spot_market.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/validation/spot_market.rs#L27-L29

Added lines #L27 - L29 were not covered by tests
"For spot market, optimal borrow rate ({}) must be >= min borrow rate ({})",
optimal_borrow_rate,
min_borrow_rate
)?;

Ok(())
}

0 comments on commit e2adcc4

Please sign in to comment.