Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit f8b6cfd

Browse files
authored
Merge pull request #10 from askibin/custody_fix
Fix collateral custody checks
2 parents 6c1bb34 + a53f667 commit f8b6cfd

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

programs/perpetuals/src/instructions/close_position.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct ClosePosition<'info> {
2323

2424
#[account(
2525
mut,
26-
constraint = receiving_account.mint == custody.mint,
26+
constraint = receiving_account.mint == collateral_custody.mint,
2727
has_one = owner
2828
)]
2929
pub receiving_account: Box<Account<'info, TokenAccount>>,

programs/perpetuals/src/instructions/liquidate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ pub struct Liquidate<'info> {
2323

2424
#[account(
2525
mut,
26-
constraint = receiving_account.mint == custody.mint,
26+
constraint = receiving_account.mint == collateral_custody.mint,
2727
constraint = receiving_account.owner == position.owner
2828
)]
2929
pub receiving_account: Box<Account<'info, TokenAccount>>,
3030

3131
#[account(
3232
mut,
33-
constraint = rewards_receiving_account.mint == custody.mint,
33+
constraint = rewards_receiving_account.mint == collateral_custody.mint,
3434
constraint = rewards_receiving_account.owner == signer.key()
3535
)]
3636
pub rewards_receiving_account: Box<Account<'info, TokenAccount>>,

programs/perpetuals/src/state/custody.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,14 @@ impl Custody {
408408
if stats.open_positions > 0 {
409409
Ok(Position {
410410
side,
411-
price: math::checked_as_u64(math::checked_div(
412-
stats.weighted_price,
413-
stats.total_quantity,
414-
)?)?,
411+
price: if stats.total_quantity > 0 {
412+
math::checked_as_u64(math::checked_div(
413+
stats.weighted_price,
414+
stats.total_quantity,
415+
)?)?
416+
} else {
417+
0
418+
},
415419
size_usd: stats.size_usd,
416420
unrealized_loss_usd: stats.cumulative_interest_usd,
417421
cumulative_interest_snapshot: stats.cumulative_interest_snapshot,

0 commit comments

Comments
 (0)