Skip to content

Commit 9ebf83b

Browse files
committed
calling safe function to get AMM state
1 parent 856e0b0 commit 9ebf83b

2 files changed

Lines changed: 25 additions & 20 deletions

File tree

src/pool.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use anyhow::Result;
22
use ethers::prelude::*;
33
use serde::Deserialize;
44
use std::sync::Arc;
5-
use log::info;
65

76
// Contract interface for Bulla pool
87
#[derive(Clone, Debug)]
@@ -11,13 +10,14 @@ pub struct BullaPool<M> {
1110
}
1211

1312
#[derive(Debug, Clone, Deserialize)]
14-
pub struct GlobalState {
15-
pub price: U256, // uint160
16-
pub tick: i32, // int24
13+
pub struct StateOfAMM {
14+
pub sqrt_price: U256, // uint160
15+
pub tick: i32, // int24
1716
pub last_fee: u16,
1817
pub plugin_config: u8,
19-
pub community_fee: u16,
20-
pub unlocked: bool,
18+
pub active_liquidity: U256, // uint128
19+
pub next_tick: i32, // int24
20+
pub previous_tick: i32, // int24
2121
}
2222

2323
impl<M: Middleware + 'static> BullaPool<M> {
@@ -32,25 +32,26 @@ impl<M: Middleware + 'static> BullaPool<M> {
3232
Self { contract }
3333
}
3434

35-
pub async fn get_global_state(&self) -> Result<GlobalState> {
36-
let state: (U256, i32, u16, u8, u16, bool) = self.contract
37-
.method("globalState", ())?
35+
pub async fn get_state_of_amm(&self) -> Result<StateOfAMM> {
36+
let state: (U256, i32, u16, u8, U256, i32, i32) = self.contract
37+
.method("safelyGetStateOfAMM", ())?
3838
.call()
3939
.await?;
4040

41-
Ok(GlobalState {
42-
price: state.0,
41+
Ok(StateOfAMM {
42+
sqrt_price: state.0,
4343
tick: state.1,
4444
last_fee: state.2,
4545
plugin_config: state.3,
46-
community_fee: state.4,
47-
unlocked: state.5,
46+
active_liquidity: state.4,
47+
next_tick: state.5,
48+
previous_tick: state.6,
4849
})
4950
}
5051

5152
pub async fn get_current_price(&self) -> Result<U256> {
52-
let state = self.get_global_state().await?;
53-
Ok(state.price)
53+
let state = self.get_state_of_amm().await?;
54+
Ok(state.sqrt_price)
5455
}
5556
}
5657

@@ -77,13 +78,13 @@ impl<P: JsonRpcClient + 'static> Pool<P> {
7778
pub fn set_provider(&mut self, provider: Provider<P>) {
7879
let contract = BullaPool::new(self.address, Arc::new(provider));
7980
self.contract = Some(contract);
80-
println!("Provider set for pool at {}", self.address);
81+
println!("Provider set for pool at {:?}", self.address);
8182
}
8283

8384
pub async fn get_current_price(&self, provider: &Provider<P>) -> Result<(f64, i32)> {
8485
if let Some(contract) = &self.contract {
85-
let state = contract.get_global_state().await?;
86-
let sqrt_price = state.price;
86+
let state = contract.get_state_of_amm().await?;
87+
let sqrt_price = state.sqrt_price;
8788
// Convert sqrt price (Q96.64) to actual price
8889
let price = (sqrt_price.as_u128() as f64 / (1u128 << 96) as f64).powi(2);
8990
// Adjust for token decimals (18 - 6 = 12 decimals difference)
@@ -100,4 +101,8 @@ impl<P: JsonRpcClient + 'static> Pool<P> {
100101
let upper_tick = current_tick + (half_ticks * self.tick_spacing);
101102
(lower_tick, upper_tick)
102103
}
104+
105+
pub fn address(&self) -> Address {
106+
self.address
107+
}
103108
}

src/price_tracker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ethers::prelude::*;
33
use std::sync::Arc;
44
use tokio::sync::RwLock;
55
use crate::pool::Pool;
6-
use log::{info, error};
6+
use log::error;
77

88
pub struct PriceTracker<P> {
99
pool: Arc<Pool<P>>,
@@ -25,7 +25,7 @@ impl<P: JsonRpcClient + 'static> PriceTracker<P> {
2525
}
2626

2727
pub async fn start_tracking(&self) -> Result<()> {
28-
println!("Starting price tracking for pool");
28+
println!("Starting price tracking for pool {:?}", self.pool.address());
2929

3030
loop {
3131
match self.update_price().await {

0 commit comments

Comments
 (0)