Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

program: add explanation for borrow #772

Merged
merged 4 commits into from
Dec 19, 2023
Merged
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
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
Loading