Skip to content

Commit

Permalink
program: add explanation for borrow (#772)
Browse files Browse the repository at this point in the history
* program: add explanation for borrow

* CHANGELOG
  • Loading branch information
crispheaney authored Dec 19, 2023
1 parent 337edac commit e025965
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- program: add borrow explanation to DepositRecords ([#772](https://github.com/drift-labs/protocol-v2/pull/772))
- sdk: OrderSubscriber has resync option ([#780](https://github.com/drift-labs/protocol-v2/pull/780))
- program: only consider recent last_active_slot in qualifies_for_withdraw_feen ([#756](https://github.com/drift-labs/protocol-v2/pull/756))
- program: amm can use reference price offset from oracle price based on clamped inventory and persist market premiums ([#681](https://github.com/drift-labs/protocol-v2/pull/681))
Expand Down
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
.get_spot_position(market_index)
.map_or(false, |pos| pos.is_borrow());
let deposit_explanation = if is_borrow {
DepositExplanation::Borrow
} else {
DepositExplanation::None
};

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 @@ -674,6 +674,10 @@ impl SpotPosition {

Ok([bid_simulation, ask_simulation])
}

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

#[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 e025965

Please sign in to comment.