diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 0e6b8d1..dadfcfd 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,20 +1,21 @@ +mod bit_math; mod compute_pool_address; -pub use compute_pool_address::compute_pool_address; - mod encode_sqrt_ratio_x96; -pub use encode_sqrt_ratio_x96::encode_sqrt_ratio_x96; - -mod nearest_usable_tick; -pub use nearest_usable_tick::nearest_usable_tick; - mod full_math; -pub use full_math::*; - mod liquidity_math; -pub use liquidity_math::add_delta; +mod nearest_usable_tick; +mod position; +mod price_tick_conversions; +mod tick_math; -mod bit_math; +use alloy_primitives::U256; pub use bit_math::*; - -mod tick_math; +pub use compute_pool_address::compute_pool_address; +pub use encode_sqrt_ratio_x96::encode_sqrt_ratio_x96; +pub use full_math::*; +pub use liquidity_math::add_delta; +pub use nearest_usable_tick::nearest_usable_tick; +pub use position::get_tokens_owed; pub use tick_math::*; + +pub const Q128: U256 = U256::from_limbs([0, 0, 1, 0]); diff --git a/src/utils/position.rs b/src/utils/position.rs new file mode 100644 index 0000000..1a90178 --- /dev/null +++ b/src/utils/position.rs @@ -0,0 +1,30 @@ +use super::Q128; +use alloy_primitives::U256; + +/// Computes the amount of fees owed to a position +pub fn get_tokens_owed( + fee_growth_inside_0_last_x128: U256, + fee_growth_inside_1_last_x128: U256, + liquidity: u128, + fee_growth_inside_0_x128: U256, + fee_growth_inside_1_x128: U256, +) -> (U256, U256) { + let liquidity = U256::from(liquidity); + let tokens_owed_0 = + (fee_growth_inside_0_x128 - fee_growth_inside_0_last_x128) * liquidity / Q128; + let tokens_owed_1 = + (fee_growth_inside_1_x128 - fee_growth_inside_1_last_x128) * liquidity / Q128; + (tokens_owed_0, tokens_owed_1) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_get_tokens_owed() { + let (tokens_owed_0, tokens_owed_1) = get_tokens_owed(U256::ZERO, U256::ZERO, 1, Q128, Q128); + assert_eq!(tokens_owed_0, U256::from(1)); + assert_eq!(tokens_owed_1, U256::from(1)); + } +} diff --git a/src/utils/price_tick_conversions.rs b/src/utils/price_tick_conversions.rs new file mode 100644 index 0000000..e69de29