Skip to content

Commit d78d9de

Browse files
committed
fmt
1 parent 04ce24b commit d78d9de

File tree

3 files changed

+81
-43
lines changed

3 files changed

+81
-43
lines changed

programs/drift/src/instructions/lp_pool.rs

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
use anchor_lang::{prelude::*, Accounts, Key, Result};
2-
use anchor_spl::token_interface::{TokenAccount, TokenInterface, Mint};
2+
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};
33

44
use crate::error::ErrorCode;
5-
use crate::math::{oracle::{is_oracle_valid_for_action, DriftAction}, safe_math::SafeMath};
5+
use crate::math::{
6+
oracle::{is_oracle_valid_for_action, DriftAction},
7+
safe_math::SafeMath,
8+
};
69
use crate::msg;
710
use crate::state::spot_market::SpotBalanceType;
8-
use crate:: state::{
9-
lp_pool::{AmmConstituentDatum, AmmConstituentMappingFixed, LPPool, WeightValidationFlags, Constituent},
11+
use crate::state::{
12+
lp_pool::{
13+
AmmConstituentDatum, AmmConstituentMappingFixed, Constituent, LPPool, WeightValidationFlags,
14+
},
1015
perp_market_map::MarketSet,
1116
state::State,
1217
user::MarketType,
@@ -114,7 +119,6 @@ pub fn handle_lp_pool_swap<'c: 'info, 'info>(
114119
in_amount: u64,
115120
min_out_amount: u64,
116121
) -> Result<()> {
117-
118122
validate!(
119123
in_market_index != out_market_index,
120124
ErrorCode::InvalidSpotMarketAccount,
@@ -151,37 +155,47 @@ pub fn handle_lp_pool_swap<'c: 'info, 'info>(
151155

152156
let in_oracle_id = in_spot_market.oracle_id();
153157
let out_oracle_id = out_spot_market.oracle_id();
154-
158+
155159
let in_oracle = *oracle_map.get_price_data(&in_oracle_id)?;
156160
let out_oracle = *oracle_map.get_price_data(&out_oracle_id)?;
157161

158-
update_spot_market_cumulative_interest(
159-
&mut in_spot_market,
160-
Some(&in_oracle),
161-
now,
162-
)?;
163-
164-
update_spot_market_cumulative_interest(
165-
&mut out_spot_market,
166-
Some(&out_oracle),
167-
now,
168-
)?;
169-
170-
let in_constituent_balance = in_constituent.get_full_balance(in_constituent_token_account.amount)?;
171-
msg!("in_constituent: {}, in_constituent_balance: {}", in_constituent.constituent_index, in_constituent_balance);
172-
let out_constituent_balance = out_constituent.get_full_balance(out_constituent_token_account.amount)?;
173-
msg!("out_constituent: {}, out_constituent_balance: {}", out_constituent.constituent_index, out_constituent_balance);
174-
175-
let in_target_weight = constituent_target_weights.get_target_weight(in_constituent.constituent_index)?;
176-
let out_target_weight = constituent_target_weights.get_target_weight(out_constituent.constituent_index)?;
162+
update_spot_market_cumulative_interest(&mut in_spot_market, Some(&in_oracle), now)?;
163+
164+
update_spot_market_cumulative_interest(&mut out_spot_market, Some(&out_oracle), now)?;
165+
166+
let in_constituent_balance =
167+
in_constituent.get_full_balance(in_constituent_token_account.amount)?;
168+
msg!(
169+
"in_constituent: {}, in_constituent_balance: {}",
170+
in_constituent.constituent_index,
171+
in_constituent_balance
172+
);
173+
let out_constituent_balance =
174+
out_constituent.get_full_balance(out_constituent_token_account.amount)?;
175+
msg!(
176+
"out_constituent: {}, out_constituent_balance: {}",
177+
out_constituent.constituent_index,
178+
out_constituent_balance
179+
);
180+
181+
let in_target_weight =
182+
constituent_target_weights.get_target_weight(in_constituent.constituent_index)?;
183+
let out_target_weight =
184+
constituent_target_weights.get_target_weight(out_constituent.constituent_index)?;
177185

178186
let (in_amount, out_amount, in_fee, out_fee) = lp_pool.get_swap_amount(
179-
&in_oracle, &out_oracle,
180-
&in_constituent, &out_constituent,
181-
&in_spot_market, &out_spot_market,
182-
in_constituent_token_account.amount, out_constituent_token_account.amount,
183-
in_target_weight, out_target_weight,
184-
in_amount)?;
187+
&in_oracle,
188+
&out_oracle,
189+
&in_constituent,
190+
&out_constituent,
191+
&in_spot_market,
192+
&out_spot_market,
193+
in_constituent_token_account.amount,
194+
out_constituent_token_account.amount,
195+
in_target_weight,
196+
out_target_weight,
197+
in_amount,
198+
)?;
185199
let out_amount_net_fees = if out_fee > 0 {
186200
out_amount.safe_sub(out_fee.unsigned_abs() as u64)?
187201
} else {
@@ -191,16 +205,23 @@ pub fn handle_lp_pool_swap<'c: 'info, 'info>(
191205
validate!(
192206
out_amount_net_fees >= min_out_amount,
193207
ErrorCode::SlippageOutsideLimit,
194-
format!("Slippage outside limit: out_amount_net_fees({}) < min_out_amount({})", out_amount_net_fees, min_out_amount).as_str()
208+
format!(
209+
"Slippage outside limit: out_amount_net_fees({}) < min_out_amount({})",
210+
out_amount_net_fees, min_out_amount
211+
)
212+
.as_str()
195213
)?;
196214

197215
in_constituent.record_swap_fees(in_fee)?;
198216
out_constituent.record_swap_fees(out_fee)?;
199217

200-
201218
// interactions: CPIs
202-
203-
let (transfer_from_vault, transfer_from_constituent) = out_constituent.get_amount_from_vaults_to_withdraw(out_constituent_token_account.amount, out_amount_net_fees)?;
219+
220+
let (transfer_from_vault, transfer_from_constituent) = out_constituent
221+
.get_amount_from_vaults_to_withdraw(
222+
out_constituent_token_account.amount,
223+
out_amount_net_fees,
224+
)?;
204225

205226
// transfer in from user token account to token vault
206227
receive(
@@ -312,4 +333,4 @@ pub struct LPSwap<'info> {
312333
pub authority: Signer<'info>,
313334

314335
pub token_program: Interface<'info, TokenInterface>,
315-
}
336+
}

programs/drift/src/state/lp_pool.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,13 @@ impl LPPool {
134134
out_target_weight: i64,
135135
in_amount: u64,
136136
) -> DriftResult<(u64, u64, i64, i64)> {
137-
let swap_price = self.get_swap_price(in_spot_market, out_spot_market, in_oracle, out_oracle, in_amount)?;
137+
let swap_price = self.get_swap_price(
138+
in_spot_market,
139+
out_spot_market,
140+
in_oracle,
141+
out_oracle,
142+
in_amount,
143+
)?;
138144

139145
let in_fee = self.get_swap_fees(
140146
in_oracle,
@@ -153,7 +159,10 @@ impl LPPool {
153159
out_oracle,
154160
out_constituent,
155161
out_token_balance,
156-
out_amount.cast::<i64>()?.checked_neg().ok_or(ErrorCode::MathError.into())?,
162+
out_amount
163+
.cast::<i64>()?
164+
.checked_neg()
165+
.ok_or(ErrorCode::MathError.into())?,
157166
out_target_weight,
158167
)?;
159168

@@ -333,15 +342,23 @@ impl Constituent {
333342

334343
/// Calculates the amount to transfer to (SpotBalanceType::Deposit) or from (SpotBalanceType::Borrow)
335344
/// the constituent's SpotBalance to stay within limits.
336-
pub fn get_amount_to_rebalance_borrow_lend(&self, token_balance: u64, amount: u64) -> DriftResult<(SpotBalanceType, u64)> {
345+
pub fn get_amount_to_rebalance_borrow_lend(
346+
&self,
347+
token_balance: u64,
348+
amount: u64,
349+
) -> DriftResult<(SpotBalanceType, u64)> {
337350
// i.e. max +90% lending, 10% in token account
338351
// i.e. max -90% borrowed
339352
Ok((SpotBalanceType::Deposit, 0))
340353
}
341354

342355
/// Calculates the amount to withdraw from BorrowLend, and the Constituent TokenAccount. The TokenAccount
343356
/// will be exhausted before funds are pulled from BorrowLend.
344-
pub fn get_amount_from_vaults_to_withdraw(&self, constituent_token_balance: u64, amount_to_withdraw: u64) -> DriftResult<(u64, u64)> {
357+
pub fn get_amount_from_vaults_to_withdraw(
358+
&self,
359+
constituent_token_balance: u64,
360+
amount_to_withdraw: u64,
361+
) -> DriftResult<(u64, u64)> {
345362
if amount_to_withdraw <= constituent_token_balance {
346363
Ok((0, amount_to_withdraw))
347364
} else {
@@ -531,7 +548,7 @@ impl<'a> AccountZeroCopyMut<'a, WeightDatum, ConstituentTargetWeightsFixed> {
531548
// assumes PRICE_PRECISION = PERCENTAGE_PRECISION
532549
let target_weight = if aum > 0 {
533550
target_amount
534-
.saturating_mul(price)
551+
.saturating_mul(price)
535552
.saturating_div(aum as i128)
536553
} else {
537554
0

programs/drift/src/state/lp_pool/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mod tests {
7676
WeightValidationFlags::NONE,
7777
)
7878
.unwrap();
79-
79+
8080
assert_eq!(totalw, 0);
8181
assert_eq!(target_zc_mut.len(), 1);
8282
assert_eq!(target_zc_mut.get(0).weight, 0);
@@ -140,7 +140,7 @@ mod tests {
140140
WeightValidationFlags::NONE,
141141
)
142142
.unwrap();
143-
143+
144144
assert_eq!(totalw, 1000000);
145145

146146
assert_eq!(target_zc_mut.len(), 1);

0 commit comments

Comments
 (0)