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
65 changes: 49 additions & 16 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions programs/drift/src/instructions/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,8 @@ pub fn handle_withdraw<'c: 'info, 'info>(

// reload the spot market vault balance so it's up-to-date
ctx.accounts.spot_market_vault.reload()?;
// Verify that the vault's on-chain token balance matches the protocol's internal ledger.
// If the vault has fewer tokens than expected, it indicates insolvency or a double-spend attempt, and aborts.
math::spot_withdraw::validate_spot_market_vault_amount(
&spot_market,
ctx.accounts.spot_market_vault.amount,
Expand Down
9 changes: 9 additions & 0 deletions programs/drift/src/math/casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ use crate::msg;
use std::convert::TryInto;
use std::panic::Location;

/// A helper trait for safe type casting in the Drift protocol.
///
/// Rust does not perform implicit type casting, and standard `as` casting
/// (e.g., `x as u64`) can cause silent truncation or overflows if the value
/// is too large or negative.
///
/// This trait utilizes `try_into()` to safely convert types. If the conversion
/// fails, it logs the exact file and line number of the failure using `#[track_caller]`
/// and returns a `CastingFailure` error instead of panicking.
pub trait Cast: Sized {
#[track_caller]
#[inline(always)]
Expand Down