Skip to content

Commit

Permalink
Merge pull request #26 from drinkcoffee/peter-pool-calcs
Browse files Browse the repository at this point in the history
Common pool calculations file
  • Loading branch information
drinkcoffee authored Nov 15, 2024
2 parents 0bbf11c + c7f13bc commit 859043b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
11 changes: 7 additions & 4 deletions bin/rb/src/commands/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use lib::prelude::*;
use tokens::erc20::Erc20;
use tokens::erc20_constants;
use uniswap_v3_sdk::prelude::FeeAmount;
use uniswapv3pool::pool_calcs::fee_to_float;
use uniswapv3pool::pool_constants;
use uniswapv3pool::univ3contract::UniswapV3PoolContract;
use uniswapv3pool::univ3sdk::UniswapV3PoolSdk;
Expand Down Expand Up @@ -159,7 +160,7 @@ pub async fn pool_list(args: ListArgs, provider: RootProvider) -> Result<()> {
FeeAmount::HIGH,
];

println!("Fee Pool Address Liquidity Rate Current Tick");
println!("Fee Pool Address Liquidity Current Tick Rate");
for fee in fees {
match UniswapV3PoolSdk::from_pool_key(
id,
Expand All @@ -176,9 +177,11 @@ pub async fn pool_list(args: ListArgs, provider: RootProvider) -> Result<()> {
pool.one_line_info().ok();
}
Err(_error) => {
let fee_num: usize = *fee as usize;
let fee_num = fee_num as f32;
let fee_num = fee_num / 10000.0;
let fee_num = fee_to_float(*fee);

// let fee_num: usize = *fee as usize;
// let fee_num = fee_num as f32;
// let fee_num = fee_num / 10000.0;
println!("{:<4}% No liquidity pool", fee_num);
}
};
Expand Down
1 change: 1 addition & 0 deletions crates/uniswapv3pool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod pool_calcs;
pub mod pool_constants;
pub mod univ3contract;
pub mod univ3sdk;
Expand Down
30 changes: 30 additions & 0 deletions crates/uniswapv3pool/src/pool_calcs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use eyre::{eyre, Result};
use uniswap_v3_sdk::prelude::FeeAmount;

pub fn fee_to_float(fee: FeeAmount) -> f32 {
let fee_num: usize = fee as usize;
let fee_num = fee_num as f32;
fee_num / 10000.0
}

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

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)
}
}
}
10 changes: 5 additions & 5 deletions crates/uniswapv3pool/src/univ3sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ impl UniswapV3PoolSdk {
}

pub fn one_line_info(&self) -> Result<()> {
let fee_num: usize = self.pool.fee as usize;
let fee_num = fee_num as f32;
let fee_num = fee_num / 10000.0;
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)?;
println!(
"{:<4}% {} {:<25} TODO {}",
"{:<4}% {} {:<25} {:<12} {}",
fee_num,
self.pool.address(None, None),
self.pool.liquidity,
self.pool.tick_current
self.pool.tick_current,
rate,
);
Ok(())
}
Expand Down

0 comments on commit 859043b

Please sign in to comment.