Skip to content

Commit

Permalink
Fixed some typo
Browse files Browse the repository at this point in the history
Added benchmarking for setting pool fees
  • Loading branch information
syan095 committed Sep 27, 2023
1 parent b4d2e54 commit 312126f
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 163 deletions.
16 changes: 8 additions & 8 deletions state-chain/amm/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,15 @@ pub(super) fn tick_at_sqrt_price(sqrt_price: SqrtPriceQ64F96) -> Tick {

let (integer_log_2, mantissa) = {
let mut _bits_remaining = sqrt_price_q64f128;
let mut most_signifcant_bit = 0u8;
let mut most_significant_bit = 0u8;

// rustfmt chokes when formatting this macro.
// See: https://github.com/rust-lang/rustfmt/issues/5404
#[rustfmt::skip]
macro_rules! add_integer_bit {
($bit:literal, $lower_bits_mask:literal) => {
if _bits_remaining > U256::from($lower_bits_mask) {
most_signifcant_bit |= $bit;
most_significant_bit |= $bit;
_bits_remaining >>= $bit;
}
};
Expand All @@ -419,17 +419,17 @@ pub(super) fn tick_at_sqrt_price(sqrt_price: SqrtPriceQ64F96) -> Tick {
add_integer_bit!(1u8, 0x1u128);

(
// most_signifcant_bit is the log2 of sqrt_price_q64f128 as an integer. This
// converts most_signifcant_bit to the integer log2 of sqrt_price_q64f128 as an
// most_significant_bit is the log2 of sqrt_price_q64f128 as an integer. This
// converts most_significant_bit to the integer log2 of sqrt_price_q64f128 as an
// q64f128
((most_signifcant_bit as i16) + (-128i16)) as i8,
((most_significant_bit as i16) + (-128i16)) as i8,
// Calculate mantissa of sqrt_price_q64f128.
if most_signifcant_bit >= 128u8 {
if most_significant_bit >= 128u8 {
// The bits we possibly drop when right shifting don't contribute to the log2
// above the 14th fractional bit.
sqrt_price_q64f128 >> (most_signifcant_bit - 127u8)
sqrt_price_q64f128 >> (most_significant_bit - 127u8)
} else {
sqrt_price_q64f128 << (127u8 - most_signifcant_bit)
sqrt_price_q64f128 << (127u8 - most_significant_bit)
}
.as_u128(), // Conversion to u128 is safe as top 128 bits are always zero
)
Expand Down
14 changes: 7 additions & 7 deletions state-chain/amm/src/limit_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ impl FloatBetweenZeroAndOne {

let (y_floor, shift_remainder) = Self::right_shift_mod(y_shifted_floor, negative_exponent);

let y_floor = y_floor.try_into().unwrap(); // Unwrap safe as numerator <= demoninator and therefore y cannot be greater than x
let y_floor = y_floor.try_into().unwrap(); // Unwrap safe as numerator <= denominator and therefore y cannot be greater than x

(
y_floor,
if div_remainder.is_zero() && shift_remainder.is_zero() {
y_floor
} else {
y_floor + 1 // Safe as for there to be a remainder y_floor must be atleast 1 less than x
y_floor + 1 // Safe as for there to be a remainder y_floor must be at least 1 less than x
},
)
}
Expand Down Expand Up @@ -349,8 +349,8 @@ pub(super) struct FixedPool {
/// associated position but have some liquidity available, but this would likely be a very
/// small amount.
available: Amount,
/// This is the big product of all `1.0 - percent_used_by_swap` for all swaps that have occured
/// since this FixedPool instance was created and used liquidity from it.
/// This is the big product of all `1.0 - percent_used_by_swap` for all swaps that have
/// occurred since this FixedPool instance was created and used liquidity from it.
percent_remaining: FloatBetweenZeroAndOne,
}

Expand All @@ -377,7 +377,7 @@ impl<LiquidityProvider: Clone + Ord> PoolState<LiquidityProvider> {
///
/// This function never panics.
pub(super) fn new(fee_hundredth_pips: u32) -> Result<Self, NewError> {
(fee_hundredth_pips <= MAX_LP_FEE)
Self::validate_fees(fee_hundredth_pips)
.then_some(())
.ok_or(NewError::InvalidFeeAmount)?;

Expand Down Expand Up @@ -556,7 +556,7 @@ impl<LiquidityProvider: Clone + Ord> PoolState<LiquidityProvider> {
// bought_amount and fees than may exist in the pool
position.amount - remaining_amount_ceil,
// We under-estimate remaining liquidity so that lp's cannot burn more liquidity
// than truely exists in the pool
// than truly exists in the pool
if remaining_amount_floor.is_zero() {
None
} else {
Expand Down Expand Up @@ -742,7 +742,7 @@ impl<LiquidityProvider: Clone + Ord> PoolState<LiquidityProvider> {
}

/// Collects any earnings from the specified position. The SwapDirection determines which
/// direction of swaps the liquidity/position you're refering to is for.
/// direction of swaps the liquidity/position you're referring to is for.
///
/// This function never panics.
pub(super) fn collect<SD: SwapDirection>(
Expand Down
6 changes: 3 additions & 3 deletions state-chain/amm/src/range_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Position {

/*
Proof that `mul_div_floor` does not overflow:
Note position.liqiudity: u128
Note position.liquidity: u128
U512::one() << 128 > u128::MAX
*/
mul_div_floor(
Expand Down Expand Up @@ -148,7 +148,7 @@ pub struct PoolState<LiquidityProvider> {
/// This is the highest tick that represents a strictly lower price than the
/// current_sqrt_price. `current_tick` is the tick that when you swap ZeroToOne the
/// `current_sqrt_price` is moving towards (going down in literal value), and will cross when
/// `current_sqrt_price` reachs it. `current_tick + 1` is the tick the price is moving towards
/// `current_sqrt_price` reaches it. `current_tick + 1` is the tick the price is moving towards
/// (going up in literal value) when you swap OneToZero and will cross when
/// `current_sqrt_price` reaches it,
current_tick: Tick,
Expand Down Expand Up @@ -646,7 +646,7 @@ impl<LiquidityProvider: Clone + Ord> PoolState<LiquidityProvider> {
let (amounts_owed, current_liquidity_delta) =
self.inner_liquidity_to_amounts::<false>(burnt_liquidity, lower_tick, upper_tick);
// Will not underflow as current_liquidity_delta must have previously been added to
// current_liquidity for it to need to be substrated now
// current_liquidity for it to need to be subtracted now
self.current_liquidity -= current_liquidity_delta;

if lower_delta.liquidity_gross == 0 &&
Expand Down
47 changes: 47 additions & 0 deletions state-chain/pallets/cf-pools/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,53 @@ benchmarks! {
)
verify {}

set_pool_fees {
let caller = new_lp_account::<T>();
assert_ok!(Pallet::<T>::new_pool(T::EnsureGovernance::try_successful_origin().unwrap(), Asset::Eth, Asset::Usdc, 0, price_at_tick(0).unwrap()));
assert_ok!(T::LpBalance::try_credit_account(
&caller,
Asset::Eth,
1_000_000,
));
assert_ok!(T::LpBalance::try_credit_account(
&caller,
Asset::Usdc,
1_000_000,
));
assert_ok!(Pallet::<T>::set_limit_order(
RawOrigin::Signed(caller.clone()).into(),
Asset::Usdc,
Asset::Eth,
0,
Some(0),
10_000,
));
assert_ok!(Pallet::<T>::set_limit_order(
RawOrigin::Signed(caller.clone()).into(),
Asset::Eth,
Asset::Usdc,
1,
Some(0),
10_000,
));
assert_ok!(Pallet::<T>::swap_with_network_fee(STABLE_ASSET, Asset::Eth, 1_000));
let fee = 1_000;
let call = Call::<T>::set_pool_fees {
base_asset: Asset::Eth,
pair_asset: Asset::Usdc,
fee_hundredth_pips: fee,
};
}: { let _ = call.dispatch_bypass_filter(T::EnsureGovernance::try_successful_origin().unwrap()); }
verify {
assert_eq!(
Pallet::<T>::pool_info(Asset::Eth, STABLE_ASSET),
Some(PoolInfo {
limit_order_fee_hundredth_pips: fee,
range_order_fee_hundredth_pips: fee,
})
);
}

impl_benchmark_test_suite!(
Pallet,
crate::mock::new_test_ext(),
Expand Down
11 changes: 6 additions & 5 deletions state-chain/pallets/cf-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub mod pallet {

/// Represents an amount of liquidity, either as an exact amount, or through maximum and minimum
/// amounts of both assets. Internally those max/min are converted into exact liquidity amounts,
/// that is if the appropiate asset ratio can be achieved while maintaining the max/min bounds.
/// that is if the appropriate asset ratio can be achieved while maintaining the max/min bounds.
#[derive(
Copy,
Clone,
Expand All @@ -313,7 +313,7 @@ pub mod pallet {
Liquidity { liquidity: Liquidity },
}

/// Indicates if an LP wishs to increase or decreease the size of an order.
/// Indicates if an LP wishes to increase or decrease the size of an order.
#[derive(
Copy,
Clone,
Expand Down Expand Up @@ -796,7 +796,7 @@ pub mod pallet {
}

/// Optionally move the order to a different tick and then increase or decrease its amount
/// of liquidity. The appropiate assets will be debited or credited from your balance as
/// of liquidity. The appropriate assets will be debited or credited from your balance as
/// needed. If the order_id isn't being used at the moment you must specify a tick,
/// otherwise it will not know what tick you want the order to be over. Note limit order
/// order_id's are independent of range order order_id's. In addition to that, order_id's
Expand Down Expand Up @@ -872,7 +872,7 @@ pub mod pallet {
}

/// Optionally move the order to a different tick and then set its amount of liquidity. The
/// appropiate assets will be debited or credited from your balance as needed. If the
/// appropriate assets will be debited or credited from your balance as needed. If the
/// order_id isn't being used at the moment you must specify a tick, otherwise it will not
/// know what tick you want the order to be over. Note limit order order_id's are
/// independent of range order order_id's. In addition to that, order_id's for buy and sell
Expand Down Expand Up @@ -934,7 +934,8 @@ pub mod pallet {
})
}

/// Sets the Liquidirt Pool fees. The fee must be >= 50%.
/// Sets the Liquidity Pool fees. Also collect earned fees and bought amount for
/// all positions within the fee and accredit them to the liquidity provider.
/// Requires governance origin.
///
/// ## Events
Expand Down
Loading

0 comments on commit 312126f

Please sign in to comment.