Skip to content

Commit

Permalink
program: add explanation for borrow
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Dec 17, 2023
1 parent 6b07123 commit d41629b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion programs/drift/src/instructions/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,15 @@ pub fn handle_withdraw(
let mut spot_market = spot_market_map.get_ref_mut(&market_index)?;
let oracle_price = oracle_map.get_price_data(&spot_market.oracle)?.price;

let is_borrow = user

Check warning on line 534 in programs/drift/src/instructions/user.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/user.rs#L534

Added line #L534 was not covered by tests
.get_spot_position(market_index)
.map_or(false, |pos| pos.is_borrow());
let deposit_explanation = if is_borrow {
DepositExplanation::Borrow

Check warning on line 538 in programs/drift/src/instructions/user.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/user.rs#L536-L538

Added lines #L536 - L538 were not covered by tests
} else {
DepositExplanation::None

Check warning on line 540 in programs/drift/src/instructions/user.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/user.rs#L540

Added line #L540 was not covered by tests
};

let deposit_record_id = get_then_update_id!(spot_market, next_deposit_record_id);
let deposit_record = DepositRecord {
ts: now,
Expand All @@ -547,7 +556,7 @@ pub fn handle_withdraw(
market_cumulative_borrow_interest: spot_market.cumulative_borrow_interest,
total_deposits_after: user.total_deposits,
total_withdraws_after: user.total_withdraws,
explanation: DepositExplanation::None,
explanation: deposit_explanation,
transfer_user: None,
};
emit!(deposit_record);
Expand Down
1 change: 1 addition & 0 deletions programs/drift/src/state/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct DepositRecord {
pub enum DepositExplanation {
None,
Transfer,
Borrow,
}

impl Default for DepositExplanation {
Expand Down
4 changes: 4 additions & 0 deletions programs/drift/src/state/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,10 @@ impl SpotPosition {

Ok([bid_simulation, ask_simulation])
}

pub fn is_borrow(&self) -> bool {
self.scaled_balance > 0 && self.balance_type == SpotBalanceType::Borrow

Check warning on line 675 in programs/drift/src/state/user.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/user.rs#L674-L675

Added lines #L674 - L675 were not covered by tests
}
}

#[zero_copy(unsafe)]
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/idl/drift.json
Original file line number Diff line number Diff line change
Expand Up @@ -8244,6 +8244,9 @@
},
{
"name": "Transfer"
},
{
"name": "Borrow"
}
]
}
Expand Down
1 change: 1 addition & 0 deletions sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export class SpotFulfillmentStatus {
export class DepositExplanation {
static readonly NONE = { none: {} };
static readonly TRANSFER = { transfer: {} };
static readonly BORROW = { borrow: {} };
}

export class SettlePnlExplanation {
Expand Down

0 comments on commit d41629b

Please sign in to comment.