Skip to content

Commit

Permalink
Add get_tokens_owed
Browse files Browse the repository at this point in the history
  • Loading branch information
shuhuiluo committed Dec 29, 2023
1 parent 9699b3d commit 1c1f6d5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -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]);
30 changes: 30 additions & 0 deletions src/utils/position.rs
Original file line number Diff line number Diff line change
@@ -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));
}
}
Empty file.

0 comments on commit 1c1f6d5

Please sign in to comment.