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

Commit 5c03624

Browse files
committed
Limit max_payoff_mult for short positions
1 parent 2bf9971 commit 5c03624

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

programs/perpetuals/src/instructions/get_entry_price_and_fee.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ pub fn get_entry_price_and_fee(
126126
let locked_amount = if params.side == Side::Short || custody.is_virtual {
127127
custody.get_locked_amount(
128128
min_collateral_price.get_token_amount(size_usd, collateral_custody.decimals)?,
129+
params.side,
129130
)?
130131
} else {
131-
custody.get_locked_amount(params.size)?
132+
custody.get_locked_amount(params.size, params.side)?
132133
};
133134

134135
let position = Position {

programs/perpetuals/src/instructions/open_position.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,10 @@ pub fn open_position(ctx: Context<OpenPosition>, params: &OpenPositionParams) ->
214214
let locked_amount = if params.side == Side::Short || custody.is_virtual {
215215
custody.get_locked_amount(
216216
min_collateral_price.get_token_amount(size_usd, collateral_custody.decimals)?,
217+
params.side,
217218
)?
218219
} else {
219-
custody.get_locked_amount(params.size)?
220+
custody.get_locked_amount(params.size, params.side)?
220221
};
221222

222223
// compute fee

programs/perpetuals/src/state/custody.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,14 @@ impl Custody {
297297
Ok(())
298298
}
299299

300-
pub fn get_locked_amount(&self, size: u64) -> Result<u64> {
300+
pub fn get_locked_amount(&self, size: u64, side: Side) -> Result<u64> {
301+
let max_payoff_mult = if side == Side::Short {
302+
std::cmp::min(Perpetuals::BPS_POWER, self.pricing.max_payoff_mult as u128)
303+
} else {
304+
self.pricing.max_payoff_mult as u128
305+
};
301306
math::checked_as_u64(math::checked_div(
302-
math::checked_mul(size as u128, self.pricing.max_payoff_mult as u128)?,
307+
math::checked_mul(size as u128, max_payoff_mult)?,
303308
Perpetuals::BPS_POWER,
304309
)?)
305310
}

programs/perpetuals/src/state/pool.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ mod test {
12521252
pool.get_entry_fee(
12531253
custody.fees.open_position,
12541254
0,
1255-
custody.get_locked_amount(0).unwrap(),
1255+
custody.get_locked_amount(0, Side::Long).unwrap(),
12561256
&custody
12571257
)
12581258
.unwrap()
@@ -1263,7 +1263,7 @@ mod test {
12631263
pool.get_entry_fee(
12641264
custody.fees.open_position,
12651265
100_000,
1266-
custody.get_locked_amount(100_000).unwrap(),
1266+
custody.get_locked_amount(100_000, Side::Long).unwrap(),
12671267
&custody
12681268
)
12691269
.unwrap()
@@ -1274,7 +1274,7 @@ mod test {
12741274
pool.get_entry_fee(
12751275
custody.fees.open_position,
12761276
150_000,
1277-
custody.get_locked_amount(150_000).unwrap(),
1277+
custody.get_locked_amount(150_000, Side::Long).unwrap(),
12781278
&custody
12791279
)
12801280
.unwrap()
@@ -1285,7 +1285,7 @@ mod test {
12851285
pool.get_entry_fee(
12861286
custody.fees.open_position,
12871287
200_000,
1288-
custody.get_locked_amount(200_000).unwrap(),
1288+
custody.get_locked_amount(200_000, Side::Long).unwrap(),
12891289
&custody
12901290
)
12911291
.unwrap()
@@ -1296,7 +1296,7 @@ mod test {
12961296
pool.get_entry_fee(
12971297
custody.fees.open_position,
12981298
300_000,
1299-
custody.get_locked_amount(300_000).unwrap(),
1299+
custody.get_locked_amount(300_000, Side::Long).unwrap(),
13001300
&custody
13011301
)
13021302
.unwrap()
@@ -1311,7 +1311,7 @@ mod test {
13111311
pool.get_entry_fee(
13121312
custody.fees.open_position,
13131313
100_000,
1314-
custody.get_locked_amount(100_000).unwrap(),
1314+
custody.get_locked_amount(100_000, Side::Long).unwrap(),
13151315
&custody
13161316
)
13171317
.unwrap()
@@ -1322,7 +1322,7 @@ mod test {
13221322
pool.get_entry_fee(
13231323
custody.fees.open_position,
13241324
150_000,
1325-
custody.get_locked_amount(150_000).unwrap(),
1325+
custody.get_locked_amount(150_000, Side::Long).unwrap(),
13261326
&custody
13271327
)
13281328
.unwrap()
@@ -1333,7 +1333,7 @@ mod test {
13331333
pool.get_entry_fee(
13341334
custody.fees.open_position,
13351335
200_000,
1336-
custody.get_locked_amount(200_000).unwrap(),
1336+
custody.get_locked_amount(200_000, Side::Long).unwrap(),
13371337
&custody
13381338
)
13391339
.unwrap()
@@ -1344,7 +1344,7 @@ mod test {
13441344
pool.get_entry_fee(
13451345
custody.fees.open_position,
13461346
300_000,
1347-
custody.get_locked_amount(300_000).unwrap(),
1347+
custody.get_locked_amount(300_000, Side::Long).unwrap(),
13481348
&custody
13491349
)
13501350
.unwrap()
@@ -1357,7 +1357,7 @@ mod test {
13571357
pool.get_entry_fee(
13581358
custody.fees.open_position,
13591359
100_000,
1360-
custody.get_locked_amount(100_000).unwrap(),
1360+
custody.get_locked_amount(100_000, Side::Long).unwrap(),
13611361
&custody
13621362
)
13631363
.unwrap()
@@ -1368,7 +1368,7 @@ mod test {
13681368
pool.get_entry_fee(
13691369
custody.fees.open_position,
13701370
150_000,
1371-
custody.get_locked_amount(150_000).unwrap(),
1371+
custody.get_locked_amount(150_000, Side::Long).unwrap(),
13721372
&custody
13731373
)
13741374
.unwrap()
@@ -1379,7 +1379,7 @@ mod test {
13791379
pool.get_entry_fee(
13801380
custody.fees.open_position,
13811381
200_000,
1382-
custody.get_locked_amount(200_000).unwrap(),
1382+
custody.get_locked_amount(200_000, Side::Long).unwrap(),
13831383
&custody
13841384
)
13851385
.unwrap()
@@ -1390,7 +1390,7 @@ mod test {
13901390
pool.get_entry_fee(
13911391
custody.fees.open_position,
13921392
300_000,
1393-
custody.get_locked_amount(300_000).unwrap(),
1393+
custody.get_locked_amount(300_000, Side::Long).unwrap(),
13941394
&custody
13951395
)
13961396
.unwrap()
@@ -1404,7 +1404,7 @@ mod test {
14041404
pool.get_entry_fee(
14051405
custody.fees.open_position,
14061406
100_000,
1407-
custody.get_locked_amount(100_000).unwrap(),
1407+
custody.get_locked_amount(100_000, Side::Long).unwrap(),
14081408
&custody
14091409
)
14101410
.unwrap()
@@ -1415,7 +1415,7 @@ mod test {
14151415
pool.get_entry_fee(
14161416
custody.fees.open_position,
14171417
150_000,
1418-
custody.get_locked_amount(150_000).unwrap(),
1418+
custody.get_locked_amount(150_000, Side::Long).unwrap(),
14191419
&custody
14201420
)
14211421
.unwrap()
@@ -1426,7 +1426,7 @@ mod test {
14261426
pool.get_entry_fee(
14271427
custody.fees.open_position,
14281428
200_000,
1429-
custody.get_locked_amount(200_000).unwrap(),
1429+
custody.get_locked_amount(200_000, Side::Long).unwrap(),
14301430
&custody
14311431
)
14321432
.unwrap()
@@ -1437,7 +1437,7 @@ mod test {
14371437
pool.get_entry_fee(
14381438
custody.fees.open_position,
14391439
300_000,
1440-
custody.get_locked_amount(300_000).unwrap(),
1440+
custody.get_locked_amount(300_000, Side::Long).unwrap(),
14411441
&custody
14421442
)
14431443
.unwrap()

0 commit comments

Comments
 (0)