Skip to content

Commit

Permalink
feat: add support for custom fee tier (#107)
Browse files Browse the repository at this point in the history
Introduced a `CUSTOM` variant to `FeeAmount` enum to allow arbitrary fee values. Updated related functionality to handle custom fees and made necessary changes to dependent modules. Bumped version to 2.7.0 in Cargo.toml and README.md.
  • Loading branch information
shuhuiluo authored Nov 25, 2024
1 parent 355ea2d commit 00e0ebd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[package]
name = "uniswap-v3-sdk"
version = "2.6.0"
version = "2.7.0"
edition = "2021"
authors = ["Shuhui Luo <twitter.com/aureliano_law>"]
description = "Uniswap V3 SDK for Rust"
license = "MIT"
readme = "README.md"
repository = "https://github.com/shuhuiluo/uniswap-v3-sdk-rs"
categories = ["cryptography::cryptocurrencies", "finance", "no-std"]
keywords = ["uniswap-v3", "ethereum", "sdk"]
exclude = [".github", ".gitignore", "rustfmt.toml"]

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ It is feature-complete with unit tests matching the TypeScript SDK.
Add the following to your `Cargo.toml` file:

```toml
uniswap-v3-sdk = { version = "2.5.0", features = ["extensions", "std"] }
uniswap-v3-sdk = { version = "2.7.0", features = ["extensions", "std"] }
```

### Usage
Expand Down
19 changes: 15 additions & 4 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ pub enum FeeAmount {
LOW = 500,
MEDIUM = 3000,
HIGH = 10000,
CUSTOM(u32),
}

impl FeeAmount {
/// The default factory tick spacings by fee amount.
#[inline]
#[must_use]
pub const fn tick_spacing(&self) -> I24 {
pub fn tick_spacing(&self) -> I24 {
match self {
Self::LOWEST => I24::ONE,
Self::LOW_200 => I24::from_limbs([4]),
Expand All @@ -36,6 +37,7 @@ impl FeeAmount {
Self::LOW => I24::from_limbs([10]),
Self::MEDIUM => I24::from_limbs([60]),
Self::HIGH => I24::from_limbs([200]),
Self::CUSTOM(fee) => I24::from_limbs([(fee / 50) as u64]),
}
}
}
Expand All @@ -51,7 +53,7 @@ impl From<u32> for FeeAmount {
500 => Self::LOW,
3000 => Self::MEDIUM,
10000 => Self::HIGH,
_ => panic!("Invalid fee amount"),
fee => Self::CUSTOM(fee),
}
}
}
Expand All @@ -67,15 +69,24 @@ impl From<i32> for FeeAmount {
10 => Self::LOW,
60 => Self::MEDIUM,
200 => Self::HIGH,
_ => panic!("Invalid tick spacing"),
tick_spacing => Self::CUSTOM((tick_spacing * 50) as u32),
}
}
}

impl From<FeeAmount> for U24 {
#[inline]
fn from(fee: FeeAmount) -> Self {
Self::from_limbs([fee as u64])
Self::from_limbs([match fee {
FeeAmount::LOWEST => 100,
FeeAmount::LOW_200 => 200,
FeeAmount::LOW_300 => 300,
FeeAmount::LOW_400 => 400,
FeeAmount::LOW => 500,
FeeAmount::MEDIUM => 3000,
FeeAmount::HIGH => 10000,
FeeAmount::CUSTOM(fee) => fee as u64,
}])
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/entities/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ mod tests {
Lazy::new(|| encode_sqrt_ratio_x96(BigInt::from(10).pow(8), BigInt::from(10).pow(20)));
static POOL_TICK_CURRENT: Lazy<I24> =
Lazy::new(|| POOL_SQRT_RATIO_START.get_tick_at_sqrt_ratio().unwrap());
const TICK_SPACING: I24 = FeeAmount::LOW.tick_spacing();
const TICK_SPACING: I24 = I24::from_limbs([10]);

static DAI_USDC_POOL: Lazy<Pool> = Lazy::new(|| {
Pool::new(
Expand Down
5 changes: 3 additions & 2 deletions src/utils/compute_pool_address.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::constants::{FeeAmount, POOL_INIT_CODE_HASH};
use alloy_primitives::{b256, keccak256, Address, B256};
use alloy_primitives::{aliases::U24, b256, keccak256, Address, B256};
use alloy_sol_types::SolValue;
use uniswap_sdk_core::prelude::{
compute_zksync_create2_address::compute_zksync_create2_address, ChainId,
Expand Down Expand Up @@ -66,7 +66,8 @@ pub fn compute_pool_address(
} else {
(token_b, token_a)
};
let salt = keccak256((token_0, token_1, fee as i32).abi_encode());
let fee: U24 = fee.into();
let salt = keccak256((token_0, token_1, fee).abi_encode());
const ZKSYNC_CHAIN_ID: u64 = ChainId::ZKSYNC as u64;

// ZKSync uses a different create2 address computation
Expand Down

0 comments on commit 00e0ebd

Please sign in to comment.