Skip to content
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

Common pool calculations file #26

Merged
merged 5 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to be a float?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the result can be 0.01, for instance. Though the current usage of the result is as a string, it is likely that the result will be used in calculations of the cost of doing a price change.

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 @@ -136,15 +136,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
Loading