From d41629b6e8c397669f5e6ddf260dc85635bb32c0 Mon Sep 17 00:00:00 2001 From: Chris Heaney Date: Sun, 17 Dec 2023 14:45:47 -0500 Subject: [PATCH 1/2] program: add explanation for borrow --- programs/drift/src/instructions/user.rs | 11 ++++++++++- programs/drift/src/state/events.rs | 1 + programs/drift/src/state/user.rs | 4 ++++ sdk/src/idl/drift.json | 3 +++ sdk/src/types.ts | 1 + 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/programs/drift/src/instructions/user.rs b/programs/drift/src/instructions/user.rs index 4704f0510..83e258f28 100644 --- a/programs/drift/src/instructions/user.rs +++ b/programs/drift/src/instructions/user.rs @@ -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, @@ -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); diff --git a/programs/drift/src/state/events.rs b/programs/drift/src/state/events.rs index 313c56635..b20195848 100644 --- a/programs/drift/src/state/events.rs +++ b/programs/drift/src/state/events.rs @@ -44,6 +44,7 @@ pub struct DepositRecord { pub enum DepositExplanation { None, Transfer, + Borrow, } impl Default for DepositExplanation { diff --git a/programs/drift/src/state/user.rs b/programs/drift/src/state/user.rs index 5c73e0319..39fd107a4 100644 --- a/programs/drift/src/state/user.rs +++ b/programs/drift/src/state/user.rs @@ -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 + } } #[zero_copy(unsafe)] diff --git a/sdk/src/idl/drift.json b/sdk/src/idl/drift.json index 767709818..ae9b859b5 100644 --- a/sdk/src/idl/drift.json +++ b/sdk/src/idl/drift.json @@ -8244,6 +8244,9 @@ }, { "name": "Transfer" + }, + { + "name": "Borrow" } ] } diff --git a/sdk/src/types.ts b/sdk/src/types.ts index 379d4be0e..415d45764 100644 --- a/sdk/src/types.ts +++ b/sdk/src/types.ts @@ -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 { From d776cdc7a7799821f8277fdb20b7632eeda19b22 Mon Sep 17 00:00:00 2001 From: Chris Heaney Date: Tue, 19 Dec 2023 14:00:42 -0500 Subject: [PATCH 2/2] CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 455c77b73..1fe27e9cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) - 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))