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

Commit 7a238c8

Browse files
authored
Get rid of add/remove collateral fee (#27)
1 parent e84efae commit 7a238c8

File tree

4 files changed

+29
-70
lines changed

4 files changed

+29
-70
lines changed

programs/perpetuals/src/instructions/add_collateral.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ pub fn add_collateral(ctx: Context<AddCollateral>, params: &AddCollateralParams)
115115
let collateral_custody = ctx.accounts.collateral_custody.as_mut();
116116
let position = ctx.accounts.position.as_mut();
117117
let pool = ctx.accounts.pool.as_mut();
118-
let token_id = pool.get_token_id(&custody.key())?;
119118

120119
// compute position price
121120
let curtime = perpetuals.get_time()?;
@@ -155,20 +154,10 @@ pub fn add_collateral(ctx: Context<AddCollateral>, params: &AddCollateralParams)
155154
let min_collateral_price = collateral_token_price
156155
.get_min_price(&collateral_token_ema_price, collateral_custody.is_stable)?;
157156

158-
// compute fee
159-
let fee_amount = pool.get_add_liquidity_fee(
160-
token_id,
161-
params.collateral,
162-
collateral_custody,
163-
&collateral_token_ema_price,
164-
)?;
165-
msg!("Collected fee: {}", fee_amount);
166-
167157
// compute amount to transfer
168-
let transfer_amount = math::checked_add(params.collateral, fee_amount)?;
169158
let collateral_usd = min_collateral_price
170159
.get_asset_amount_usd(params.collateral, collateral_custody.decimals)?;
171-
msg!("Amount in: {}", transfer_amount);
160+
msg!("Amount in: {}", params.collateral);
172161
msg!("Collateral added in USD: {}", collateral_usd);
173162

174163
// update existing position
@@ -203,26 +192,14 @@ pub fn add_collateral(ctx: Context<AddCollateral>, params: &AddCollateralParams)
203192
.to_account_info(),
204193
ctx.accounts.owner.to_account_info(),
205194
ctx.accounts.token_program.to_account_info(),
206-
transfer_amount,
195+
params.collateral,
207196
)?;
208197

209198
// update custody stats
210199
msg!("Update custody stats");
211-
collateral_custody.collected_fees.open_position_usd = collateral_custody
212-
.collected_fees
213-
.open_position_usd
214-
.wrapping_add(
215-
collateral_token_ema_price
216-
.get_asset_amount_usd(fee_amount, collateral_custody.decimals)?,
217-
);
218-
219200
collateral_custody.assets.collateral =
220201
math::checked_add(collateral_custody.assets.collateral, params.collateral)?;
221202

222-
let protocol_fee = Pool::get_fee_amount(custody.fees.protocol_share, fee_amount)?;
223-
collateral_custody.assets.protocol_fees =
224-
math::checked_add(collateral_custody.assets.protocol_fees, protocol_fee)?;
225-
226203
// if custody and collateral_custody accounts are the same, ensure that data is in sync
227204
if position.side == Side::Long && !custody.is_virtual {
228205
*custody = collateral_custody.clone();

programs/perpetuals/src/instructions/remove_collateral.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ pub fn remove_collateral(
126126
return Err(ProgramError::InvalidArgument.into());
127127
}
128128
let pool = ctx.accounts.pool.as_mut();
129-
let token_id = pool.get_token_id(&custody.key())?;
130129

131130
// compute position price
132131
let curtime = perpetuals.get_time()?;
@@ -169,23 +168,13 @@ pub fn remove_collateral(
169168
collateral_token_ema_price
170169
};
171170

172-
// compute fee
171+
// compute amount to transfer
173172
let collateral = max_collateral_price
174173
.get_token_amount(params.collateral_usd, collateral_custody.decimals)?;
175-
let fee_amount = pool.get_remove_liquidity_fee(
176-
token_id,
177-
collateral,
178-
collateral_custody,
179-
&collateral_token_ema_price,
180-
)?;
181-
msg!("Collected fee: {}", fee_amount);
182-
183-
// compute amount to transfer
184174
if collateral > position.collateral_amount {
185175
return Err(ProgramError::InsufficientFunds.into());
186176
}
187-
let transfer_amount = math::checked_sub(collateral, fee_amount)?;
188-
msg!("Amount out: {}", transfer_amount);
177+
msg!("Amount out: {}", collateral);
189178

190179
// update existing position
191180
msg!("Update existing position");
@@ -219,26 +208,14 @@ pub fn remove_collateral(
219208
ctx.accounts.receiving_account.to_account_info(),
220209
ctx.accounts.transfer_authority.to_account_info(),
221210
ctx.accounts.token_program.to_account_info(),
222-
transfer_amount,
211+
collateral,
223212
)?;
224213

225214
// update custody stats
226215
msg!("Update custody stats");
227-
collateral_custody.collected_fees.open_position_usd = collateral_custody
228-
.collected_fees
229-
.open_position_usd
230-
.wrapping_add(
231-
collateral_token_ema_price
232-
.get_asset_amount_usd(fee_amount, collateral_custody.decimals)?,
233-
);
234-
235216
collateral_custody.assets.collateral =
236217
math::checked_sub(collateral_custody.assets.collateral, collateral)?;
237218

238-
let protocol_fee = Pool::get_fee_amount(custody.fees.protocol_share, fee_amount)?;
239-
collateral_custody.assets.protocol_fees =
240-
math::checked_add(collateral_custody.assets.protocol_fees, protocol_fee)?;
241-
242219
// if custody and collateral_custody accounts are the same, ensure that data is in sync
243220
if position.side == Side::Long && !custody.is_virtual {
244221
*custody = collateral_custody.clone();

programs/perpetuals/src/state/pool.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,12 @@ impl Pool {
690690
collateral_token_price
691691
.get_min_price(collateral_token_ema_price, collateral_custody.is_stable)?
692692
};
693-
let max_profit_usd = min_collateral_price
694-
.get_asset_amount_usd(position.locked_amount, collateral_custody.decimals)?;
693+
let max_profit_usd = if curtime <= position.open_time {
694+
0
695+
} else {
696+
min_collateral_price
697+
.get_asset_amount_usd(position.locked_amount, collateral_custody.decimals)?
698+
};
695699
Ok((
696700
std::cmp::min(max_profit_usd, cur_profit_usd),
697701
0u64,
@@ -1583,7 +1587,7 @@ mod test {
15831587
&token_price,
15841588
&token_ema_price,
15851589
&custody,
1586-
0,
1590+
1,
15871591
false
15881592
)
15891593
.unwrap()
@@ -1601,7 +1605,7 @@ mod test {
16011605
&token_price,
16021606
&token_ema_price,
16031607
&custody,
1604-
0,
1608+
1,
16051609
false
16061610
)
16071611
.unwrap()
@@ -1619,7 +1623,7 @@ mod test {
16191623
&token_price,
16201624
&token_ema_price,
16211625
&custody,
1622-
0,
1626+
1,
16231627
false
16241628
)
16251629
.unwrap()
@@ -1641,7 +1645,7 @@ mod test {
16411645
&token_price,
16421646
&token_ema_price,
16431647
&custody,
1644-
0
1648+
1
16451649
)
16461650
.unwrap()
16471651
);
@@ -1658,7 +1662,7 @@ mod test {
16581662
&token_price,
16591663
&token_ema_price,
16601664
&custody,
1661-
0
1665+
1
16621666
)
16631667
.unwrap()
16641668
);
@@ -1674,7 +1678,7 @@ mod test {
16741678
&token_price,
16751679
&token_ema_price,
16761680
&custody,
1677-
0
1681+
1
16781682
)
16791683
.unwrap()
16801684
);
@@ -1691,7 +1695,7 @@ mod test {
16911695
&token_price,
16921696
&token_ema_price,
16931697
&custody,
1694-
0
1698+
1
16951699
)
16961700
.unwrap()
16971701
);
@@ -1707,7 +1711,7 @@ mod test {
17071711
&token_price,
17081712
&token_ema_price,
17091713
&custody,
1710-
0
1714+
1
17111715
)
17121716
.unwrap()
17131717
);
@@ -1724,7 +1728,7 @@ mod test {
17241728
&token_price,
17251729
&token_ema_price,
17261730
&custody,
1727-
0
1731+
1
17281732
)
17291733
.unwrap()
17301734
);
@@ -1741,7 +1745,7 @@ mod test {
17411745
&token_price,
17421746
&token_ema_price,
17431747
&custody,
1744-
0
1748+
1
17451749
)
17461750
.unwrap()
17471751
);
@@ -1753,45 +1757,45 @@ mod test {
17531757

17541758
assert_eq!(
17551759
scale(21_250, Perpetuals::PRICE_DECIMALS),
1756-
pool.get_liquidation_price(&position, &token_price, &custody, &custody, 0)
1760+
pool.get_liquidation_price(&position, &token_price, &custody, &custody, 1)
17571761
.unwrap()
17581762
);
17591763

17601764
// lower price should lower liquidation price
17611765
position.price = scale(24_500, Perpetuals::PRICE_DECIMALS);
17621766
assert_eq!(
17631767
scale(20_825, Perpetuals::PRICE_DECIMALS),
1764-
pool.get_liquidation_price(&position, &token_price, &custody, &custody, 0)
1768+
pool.get_liquidation_price(&position, &token_price, &custody, &custody, 1)
17651769
.unwrap()
17661770
);
17671771

17681772
position.price = scale(20_000, Perpetuals::PRICE_DECIMALS);
17691773
assert_eq!(
17701774
scale(17_000, Perpetuals::PRICE_DECIMALS),
1771-
pool.get_liquidation_price(&position, &token_price, &custody, &custody, 0)
1775+
pool.get_liquidation_price(&position, &token_price, &custody, &custody, 1)
17721776
.unwrap()
17731777
);
17741778

17751779
// higher price should increase liquidation price
17761780
position.price = scale(26_000, Perpetuals::PRICE_DECIMALS);
17771781
assert_eq!(
17781782
scale(22_100, Perpetuals::PRICE_DECIMALS),
1779-
pool.get_liquidation_price(&position, &token_price, &custody, &custody, 0)
1783+
pool.get_liquidation_price(&position, &token_price, &custody, &custody, 1)
17801784
.unwrap()
17811785
);
17821786

17831787
position.price = scale(35_000, Perpetuals::PRICE_DECIMALS);
17841788
assert_eq!(
17851789
scale(29_750, Perpetuals::PRICE_DECIMALS),
1786-
pool.get_liquidation_price(&position, &token_price, &custody, &custody, 0)
1790+
pool.get_liquidation_price(&position, &token_price, &custody, &custody, 1)
17871791
.unwrap()
17881792
);
17891793

17901794
// dead price
17911795
position.price = scale(0, Perpetuals::PRICE_DECIMALS);
17921796
assert_eq!(
17931797
scale_f64(0.0, Perpetuals::PRICE_DECIMALS),
1794-
pool.get_liquidation_price(&position, &token_price, &custody, &custody, 0)
1798+
pool.get_liquidation_price(&position, &token_price, &custody, &custody, 1)
17951799
.unwrap()
17961800
);
17971801
}
@@ -1815,7 +1819,7 @@ mod test {
18151819
&token_price,
18161820
&token_ema_price,
18171821
&custody,
1818-
0,
1822+
1,
18191823
false
18201824
)
18211825
.unwrap()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ pub async fn max_user_profit() {
169169

170170
// Gains are limited to 0.25 * 5 = 1.25 ETH
171171
// True gains should be 2.5 ETH less fees (price did x2 on x5 leverage)
172+
// if this fails, make sure you run a non-test build, i.e. re-run "anchor build"
172173
assert_eq!(martin_eth_balance, utils::scale_f64(2.7, ETH_DECIMALS));
173174
}
174175
}

0 commit comments

Comments
 (0)