Skip to content

Commit

Permalink
feat: refine Position::permit_batch_data and Pool::swap (#21)
Browse files Browse the repository at this point in the history
Updated `Position::permit_batch_data` to include enhanced type usage and improved structure alignment using `U48` and other primitives. Refactored `Pool::swap` logic for clarity by renaming and inverting the conditional helper method to `hook_impacts_swap`.
  • Loading branch information
shuhuiluo authored Dec 26, 2024
1 parent 29ad430 commit 3277390
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 43 deletions.
8 changes: 5 additions & 3 deletions src/entities/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl<TP: TickDataProvider> Pool<TP> {
amount_specified: I256,
sqrt_price_limit_x96: Option<U160>,
) -> Result<SwapState<TP::Index>, Error> {
if self.non_impactful_hook() {
if !self.hook_impacts_swap() {
Ok(v3_swap(
self.fee,
self.sqrt_price_x96,
Expand All @@ -305,8 +305,10 @@ impl<TP: TickDataProvider> Pool<TP> {
}
}

fn non_impactful_hook(&self) -> bool {
self.hooks == Address::ZERO
const fn hook_impacts_swap(&self) -> bool {
// could use this function to clear certain hooks that may have swap Permissions, but we
// know they don't interfere in the swap outcome
has_swap_permissions(self.hooks)
}
}

Expand Down
80 changes: 40 additions & 40 deletions src/entities/position.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::{tick_to_price, Error, Pool};
use alloy_primitives::{uint, U160, U256};
use crate::prelude::{tick_to_price, Error, Pool, *};
use alloy_primitives::{aliases::U48, uint, U160, U256};
use uniswap_sdk_core::prelude::*;
use uniswap_v3_sdk::prelude::*;

Expand Down Expand Up @@ -407,44 +407,44 @@ impl<TP: TickDataProvider> Position<TP> {
Ok(amounts)
}

// /// Returns the [`AllowanceTransferPermitBatch`] for adding liquidity to a position
// ///
// /// ## Arguments
// ///
// /// * `slippage_tolerance`: The amount by which the price can 'slip' before the transaction
// will /// revert
// /// * `spender`: The spender of the permit (should usually be the [`PositionManager`])
// /// * `nonce`: A valid permit2 nonce
// /// * `deadline`: The deadline for the permit
// #[inline]
// pub fn permit_batch_data(
// &mut self,
// slippage_tolerance: Percent,
// spender: String,
// nonce: U256,
// deadline: U256,
// ) -> Result<AllowanceTransferPermitBatch, Error> {
// let MintAmounts { amount0, amount1 } =
// self.mint_amounts_with_slippage(&slippage_tolerance)?;
// Ok(AllowanceTransferPermitBatch {
// details: vec![
// AllowanceTransferPermitDetails {
// token: self.pool.currency0.wrapped().address(),
// amount: amount0,
// expiration: deadline,
// nonce,
// },
// AllowanceTransferPermitDetails {
// token: self.pool.currency1.wrapped().address(),
// amount: amount1,
// expiration: deadline,
// nonce,
// },
// ],
// spender,
// sig_deadline: deadline,
// })
// }
/// Returns the [`AllowanceTransferPermitBatch`] for adding liquidity to a position
///
/// ## Arguments
///
/// * `slippage_tolerance`: The amount by which the price can 'slip' before the transaction will
/// revert
/// * `spender`: The spender of the permit (should usually be the [`PositionManager`])
/// * `nonce`: A valid permit2 nonce
/// * `deadline`: The deadline for the permit
#[inline]
pub fn permit_batch_data(
&mut self,
slippage_tolerance: &Percent,
spender: Address,
nonce: U256,
deadline: U256,
) -> Result<AllowanceTransferPermitBatch, Error> {
let MintAmounts { amount0, amount1 } =
self.mint_amounts_with_slippage(slippage_tolerance)?;
Ok(AllowanceTransferPermitBatch {
details: vec![
IAllowanceTransfer::PermitDetails {
token: self.pool.currency0.wrapped().address(),
amount: U160::from(amount0),
expiration: U48::from(deadline),
nonce: U48::from(nonce),
},
IAllowanceTransfer::PermitDetails {
token: self.pool.currency1.wrapped().address(),
amount: U160::from(amount1),
expiration: U48::from(deadline),
nonce: U48::from(nonce),
},
],
spender,
sigDeadline: deadline,
})
}

/// Computes the maximum amount of liquidity received for a given amount of token0, token1,
/// and the prices at the tick boundaries.
Expand Down

0 comments on commit 3277390

Please sign in to comment.