Skip to content

Commit

Permalink
Merge pull request #27 from sergerad/serge/peter-pool-calcs
Browse files Browse the repository at this point in the history
Handle error, underflow and denominator
  • Loading branch information
drinkcoffee authored Nov 3, 2024
2 parents 4f2e1c3 + 710d9bf commit c7f13bc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
23 changes: 18 additions & 5 deletions crates/uniswapv3pool/src/pool_calcs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use eyre::{eyre, Result};
use uniswap_v3_sdk::prelude::FeeAmount;

pub fn fee_to_float(fee: FeeAmount) -> f32 {
Expand All @@ -6,12 +7,24 @@ pub fn fee_to_float(fee: FeeAmount) -> f32 {
fee_num / 10000.0
}

pub fn tick_to_exchange_rate(tick: i32, token_one_decimals: u64, token_two_decimals: u64) -> f64 {
pub fn tick_to_exchange_rate(
tick: i32,
token_one_decimals: u64,
token_two_decimals: u64,
) -> Result<f64> {
let tick = tick as f64;
let val: f64 = 1.0001;
let base: f64 = 1.0001;

if token_one_decimals != token_two_decimals {
println!("Exchange when token decimals not the same not yet supported");
let token_decimals_diff = token_one_decimals.checked_sub(token_two_decimals);
match token_decimals_diff {
None => Err(eyre!(
"Token decimals subtraction overflow: ({} - {})",
token_one_decimals,
token_two_decimals
)),
Some(diff) => {
let denominator = 10u64.pow(diff.try_into()?);
Ok(base.powf(tick) / denominator as f64)
}
}
val.powf(tick)
}
2 changes: 1 addition & 1 deletion crates/uniswapv3pool/src/univ3sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl UniswapV3PoolSdk {

pub fn one_line_info(&self) -> Result<()> {
let fee_num = crate::pool_calcs::fee_to_float(self.pool.fee);
let rate = crate::pool_calcs::tick_to_exchange_rate(self.pool.tick_current, 18, 18);
let rate = crate::pool_calcs::tick_to_exchange_rate(self.pool.tick_current, 18, 18)?;
println!(
"{:<4}% {} {:<25} {:<12} {}",
fee_num,
Expand Down

0 comments on commit c7f13bc

Please sign in to comment.