-
Notifications
You must be signed in to change notification settings - Fork 4
Rust reCLAMM pricing #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Rust reCLAMM pricing #153
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
97260ef
Initial Rust reCLAMM pricing. WIP as has failing tests.
johngrantuk 26e76d2
Fix swap_to_price math
brunoguerios a8cc70c
Add limit tests.
johngrantuk d0f904f
Merge branch 'main' into reclamm-pricing
johngrantuk 725bae6
Release v0.4.1.
johngrantuk 8c4be7d
refactor: Use scaled18 for target price.
johngrantuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| pub mod reclamm_data; | ||
| pub mod reclamm_math; | ||
| pub mod reclamm_pool; | ||
| pub mod reclamm_pricing; | ||
|
|
||
| pub use reclamm_data::*; | ||
| pub use reclamm_math::*; | ||
| pub use reclamm_pool::*; | ||
| pub use reclamm_pricing::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| use alloy_primitives::U256; | ||
|
|
||
| /// Result struct for swap to target price calculation | ||
| #[derive(Debug, Clone)] | ||
| pub struct SwapToTargetPriceResult { | ||
| pub token_in_index: usize, | ||
| pub token_out_index: usize, | ||
| pub amount_in_raw: U256, | ||
| pub amount_out_raw: U256, | ||
| } | ||
|
|
||
| /// Helper function to convert U256 to f64 | ||
| /// Handles large numbers by converting to string and parsing | ||
| fn u256_to_f64(value: &U256) -> f64 { | ||
| // For values that fit in u128, use direct conversion | ||
| if let Ok(val) = u128::try_from(*value) { | ||
| val as f64 | ||
| } else { | ||
| // For very large numbers, convert via string | ||
| // This may lose precision but is acceptable for our use case | ||
| value.to_string().parse::<f64>().unwrap_or(f64::MAX) | ||
| } | ||
| } | ||
|
|
||
| /// Calculate current ReCLAMM price | ||
| /// Returns price in e18 format: (balanceB + virtualB) / (balanceA + virtualA) | ||
| pub fn calculate_reclamm_price( | ||
| balances_live_scaled_18: &[U256], | ||
| current_virtual_balances: &[U256], | ||
| ) -> U256 { | ||
| // Convert to f64 | ||
| let balance_a = u256_to_f64(&balances_live_scaled_18[0]) / 1e18; | ||
| let balance_b = u256_to_f64(&balances_live_scaled_18[1]) / 1e18; | ||
| let virtual_a = u256_to_f64(¤t_virtual_balances[0]) / 1e18; | ||
| let virtual_b = u256_to_f64(¤t_virtual_balances[1]) / 1e18; | ||
|
|
||
| // Calculate price | ||
| let price = (balance_b + virtual_b) / (balance_a + virtual_a); | ||
|
|
||
| // Convert back to U256 (scaled to e18) | ||
| let price_scaled = (price * 1e18) as u128; | ||
| U256::from(price_scaled) | ||
| } | ||
|
|
||
| /// Calculate swap amounts needed to reach a target price in a ReCLAMM pool | ||
| /// | ||
| /// # Arguments | ||
| /// * `token_rates` - Token rates from rate providers [rateA, rateB] in e18 | ||
| /// * `balances_live_scaled_18` - Pool balances [balanceA, balanceB] in e18 | ||
| /// * `current_virtual_balances` - Virtual balances [virtualA, virtualB] in e18 | ||
| /// * `swap_fee_percentage` - Swap fee percentage in e18 | ||
| /// * `protocol_fee_percentage` - Protocol fee percentage in e18 | ||
| /// * `pool_creator_fee_percentage` - Pool creator fee percentage in e18 | ||
| /// * `decimals_a` - Decimals for token A | ||
| /// * `decimals_b` - Decimals for token B | ||
| /// * `target_price_scaled_18` - Target price in e18 format | ||
| /// | ||
| /// # Returns | ||
| /// Result containing token indices and raw amounts for the swap | ||
| #[allow(clippy::too_many_arguments)] | ||
| pub fn swap_reclamm_to_price( | ||
| token_rates: &[U256], | ||
| balances_live_scaled_18: &[U256], | ||
| current_virtual_balances: &[U256], | ||
| swap_fee_percentage: &U256, | ||
| _protocol_fee_percentage: &U256, | ||
| _pool_creator_fee_percentage: &U256, | ||
| decimals_a: u8, | ||
| decimals_b: u8, | ||
| target_price_scaled_18: &U256, | ||
| ) -> Result<SwapToTargetPriceResult, String> { | ||
| // Input validation | ||
| if balances_live_scaled_18.len() != 2 | ||
| || current_virtual_balances.len() != 2 | ||
| || token_rates.len() != 2 | ||
| { | ||
| return Err("Invalid input: arrays must have length 2".to_string()); | ||
| } | ||
|
|
||
| // Convert U256 inputs to f64 | ||
| let balance_a = u256_to_f64(&balances_live_scaled_18[0]) / 1e18; | ||
| let balance_b = u256_to_f64(&balances_live_scaled_18[1]) / 1e18; | ||
| let virtual_a = u256_to_f64(¤t_virtual_balances[0]) / 1e18; | ||
| let virtual_b = u256_to_f64(¤t_virtual_balances[1]) / 1e18; | ||
| let swap_fee = u256_to_f64(swap_fee_percentage) / 1e18; | ||
| // Note: protocol_fee and pool_creator_fee are not used in the calculation | ||
| // They affect fee distribution but not the swap amounts needed to reach target price | ||
| let rate_a = u256_to_f64(&token_rates[0]) / 1e18; | ||
| let rate_b = u256_to_f64(&token_rates[1]) / 1e18; | ||
| let target_price = u256_to_f64(target_price_scaled_18) / 1e18; | ||
|
|
||
| // Validate non-zero values | ||
| if balance_a + virtual_a == 0.0 || balance_b + virtual_b == 0.0 { | ||
| return Err("Invalid pool state: zero total balance".to_string()); | ||
| } | ||
| if rate_a == 0.0 || rate_b == 0.0 { | ||
| return Err("Invalid rates: zero rate".to_string()); | ||
| } | ||
| if target_price <= 0.0 { | ||
| return Err("Invalid target price: must be positive".to_string()); | ||
| } | ||
|
|
||
| // Calculate invariant and prices | ||
| let invariant = (balance_a + virtual_a) * (balance_b + virtual_b); | ||
| let current_price = (balance_b + virtual_b) / (balance_a + virtual_a); | ||
| let target_price_scaled = target_price * rate_a / rate_b; | ||
|
|
||
| if target_price_scaled > current_price { | ||
| // tokenB in, tokenA out | ||
| let amount_out_scaled = balance_a + virtual_a - (invariant / target_price_scaled).sqrt(); | ||
|
|
||
| // Calculate amount needed in the invariant math (after swap fee is removed) | ||
| let amount_in_scaled_net = | ||
| (invariant / (balance_a - amount_out_scaled + virtual_a)) - balance_b - virtual_b; | ||
|
|
||
| // Convert to gross amount (before swap fee) - this is what the user provides | ||
| let amount_in_scaled = amount_in_scaled_net / (1.0 - swap_fee); | ||
|
|
||
| // Validate amounts | ||
| if amount_out_scaled < 0.0 || amount_in_scaled < 0.0 { | ||
| return Err("Invalid calculation: negative amounts".to_string()); | ||
| } | ||
| if amount_out_scaled > balance_a { | ||
| return Err("Invalid calculation: amount out exceeds balance".to_string()); | ||
| } | ||
|
|
||
| Ok(SwapToTargetPriceResult { | ||
| token_in_index: 1, | ||
| token_out_index: 0, | ||
| amount_in_raw: U256::from( | ||
| (amount_in_scaled * 10f64.powi(decimals_b as i32) / rate_b).ceil() as u128, | ||
| ), | ||
| amount_out_raw: U256::from( | ||
| (amount_out_scaled * 10f64.powi(decimals_a as i32) / rate_a).floor() as u128, | ||
| ), | ||
| }) | ||
| } else { | ||
| // tokenA in, tokenB out | ||
| let amount_out_scaled = balance_b + virtual_b - (invariant * target_price_scaled).sqrt(); | ||
|
|
||
| // Calculate amount needed in the invariant math (after swap fee is removed) | ||
| let amount_in_scaled_net = | ||
| (invariant / (balance_b - amount_out_scaled + virtual_b)) - balance_a - virtual_a; | ||
|
|
||
| // Convert to gross amount (before swap fee) - this is what the user provides | ||
| let amount_in_scaled = amount_in_scaled_net / (1.0 - swap_fee); | ||
|
|
||
| // Validate amounts | ||
| if amount_out_scaled < 0.0 || amount_in_scaled < 0.0 { | ||
| return Err("Invalid calculation: negative amounts".to_string()); | ||
| } | ||
| if amount_out_scaled > balance_b { | ||
| return Err("Invalid calculation: amount out exceeds balance".to_string()); | ||
| } | ||
|
|
||
| Ok(SwapToTargetPriceResult { | ||
| token_in_index: 0, | ||
| token_out_index: 1, | ||
| amount_in_raw: U256::from( | ||
| (amount_in_scaled * 10f64.powi(decimals_a as i32) / rate_a).ceil() as u128, | ||
| ), | ||
| amount_out_raw: U256::from( | ||
| (amount_out_scaled * 10f64.powi(decimals_b as i32) / rate_b).floor() as u128, | ||
| ), | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.