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

Commit 67478f1

Browse files
committed
Use position_price to compute size_usd in open_position
1 parent f8b6cfd commit 67478f1

File tree

4 files changed

+12
-16
lines changed

4 files changed

+12
-16
lines changed

programs/perpetuals/src/instructions/get_entry_price_and_fee.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,6 @@ pub fn get_entry_price_and_fee(
9292
custody.pricing.use_ema,
9393
)?;
9494

95-
let max_price = if token_price > token_ema_price {
96-
token_price
97-
} else {
98-
token_ema_price
99-
};
100-
10195
let collateral_token_price = OraclePrice::new_from_oracle(
10296
&ctx.accounts
10397
.collateral_custody_oracle_account
@@ -121,7 +115,11 @@ pub fn get_entry_price_and_fee(
121115

122116
let entry_price = pool.get_entry_price(&token_price, &token_ema_price, params.side, custody)?;
123117

124-
let size_usd = max_price.get_asset_amount_usd(params.size, custody.decimals)?;
118+
let position_oracle_price = OraclePrice {
119+
price: entry_price,
120+
exponent: -(Perpetuals::PRICE_DECIMALS as i32),
121+
};
122+
let size_usd = position_oracle_price.get_asset_amount_usd(params.size, custody.decimals)?;
125123
let collateral_usd = min_collateral_price
126124
.get_asset_amount_usd(params.collateral, collateral_custody.decimals)?;
127125

programs/perpetuals/src/instructions/open_position.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,6 @@ pub fn open_position(ctx: Context<OpenPosition>, params: &OpenPositionParams) ->
163163
custody.pricing.use_ema,
164164
)?;
165165

166-
let max_price = if token_price > token_ema_price {
167-
token_price
168-
} else {
169-
token_ema_price
170-
};
171-
172166
let collateral_token_price = OraclePrice::new_from_oracle(
173167
&ctx.accounts
174168
.collateral_custody_oracle_account
@@ -209,7 +203,11 @@ pub fn open_position(ctx: Context<OpenPosition>, params: &OpenPositionParams) ->
209203
}
210204

211205
// compute position parameters
212-
let size_usd = max_price.get_asset_amount_usd(params.size, custody.decimals)?;
206+
let position_oracle_price = OraclePrice {
207+
price: position_price,
208+
exponent: -(Perpetuals::PRICE_DECIMALS as i32),
209+
};
210+
let size_usd = position_oracle_price.get_asset_amount_usd(params.size, custody.decimals)?;
213211
let collateral_usd = min_collateral_price
214212
.get_asset_amount_usd(params.collateral, collateral_custody.decimals)?;
215213

programs/perpetuals/tests/anchor/basic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ describe("perpetuals", () => {
482482
updateTime: "0",
483483
side: { long: {} },
484484
price: "124230000",
485-
sizeUsd: "861000000",
485+
sizeUsd: "869610000",
486486
collateralUsd: "123000000",
487487
unrealizedProfitUsd: "0",
488488
unrealizedLossUsd: "0",

programs/perpetuals/tests/native/tests_suite/position/liquidate_position.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ pub async fn liquidate_position() {
185185

186186
assert_eq!(
187187
martin_eth_balance,
188-
utils::scale_f64(1.376624036, ETH_DECIMALS)
188+
utils::scale_f64(1.369834721, ETH_DECIMALS)
189189
);
190190
}
191191
}

0 commit comments

Comments
 (0)