Skip to content

Commit a0b2374

Browse files
committed
code refactoring
1 parent 7a5ea1f commit a0b2374

5 files changed

Lines changed: 14 additions & 17 deletions

File tree

contracts/script/LiqManager.s.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ contract LiqManagerScript is Script {
1717
vm.stopBroadcast();
1818
}
1919
}
20+
21+
// forge script script/LiqManager.s.sol:LiqManagerScript \
22+
// --rpc-url https://rpc.berachain.com \
23+
// --account DEV \
24+
// --broadcast

src/liquidity_provider.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl<P: JsonRpcClient + Clone + 'static> LiquidityProvider<P> {
2626
}
2727

2828
pub async fn provide_liquidity(&self, amount_a: U256, amount_b: U256) -> Result<()> {
29-
let (current_price, current_tick) = self.pool.get_adjusted_current_price_and_tick(&self.provider).await?;
29+
let (current_price, current_tick) = self.pool.get_adjusted_current_price_and_tick().await?;
3030
let (bottom_tick, top_tick) = self.pool.get_tick_range(current_tick, self.num_ticks).await;
3131

3232
info!("Providing liquidity at price {:.6}, tick range [{}, {}]",

src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ async fn main() -> Result<()> {
3939
let liquidity_manager: Address = liquidity_manager.parse()?;
4040

4141
// Create pool instance
42-
let pool = Pool::new(pool_address, token_a, token_b, tick_spacing, Arc::new(provider.clone())).await?;
43-
let mut pool = Arc::new(pool);
44-
Arc::get_mut(&mut pool).unwrap().set_provider(provider.clone());
42+
let pool = Arc::new(Pool::new(pool_address, Arc::new(provider.clone()), token_a, token_b, tick_spacing).await?);
4543

4644
// Create liquidity provider
4745
let liquidity_provider = Arc::new(LiquidityProvider::new(

src/pool.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,26 @@ pub struct Pool<P> {
1313
token_b_decimals: u8,
1414
tick_spacing: i32,
1515
contract: Option<BullaPool<Arc<Provider<P>>>>,
16-
provider: Option<Arc<Provider<P>>>,
1716
}
1817

1918
impl<P: JsonRpcClient + 'static> Pool<P> {
20-
pub async fn new(address: Address, token_a: Address, token_b: Address, tick_spacing: i32, provider: Arc<Provider<P>>) -> Result<Self> {
19+
pub async fn new(address: Address, provider: Arc<Provider<P>>, token_a: Address, token_b: Address, tick_spacing: i32) -> Result<Self> {
2120
let token_a_decimals = Self::get_decimals(provider.clone(), token_a).await?;
2221
let token_b_decimals = Self::get_decimals(provider.clone(), token_b).await?;
22+
let contract = BullaPool::new(address, provider.clone());
23+
println!("Provider set for pool at {:?}", address);
2324
Ok(Self {
2425
address,
2526
token_a,
2627
token_b,
2728
token_a_decimals,
2829
token_b_decimals,
2930
tick_spacing,
30-
contract: None,
31-
provider: Some(provider),
31+
contract: Some(contract),
3232
})
3333
}
3434

35-
pub fn set_provider(&mut self, provider: Provider<P>) {
36-
let contract = BullaPool::new(self.address, Arc::new(provider));
37-
self.contract = Some(contract);
38-
println!("Provider set for pool at {:?}", self.address);
39-
}
40-
41-
pub async fn get_adjusted_current_price_and_tick(&self, provider: &Provider<P>) -> Result<(f64, i32)> {
35+
pub async fn get_adjusted_current_price_and_tick(&self) -> Result<(f64, i32)> {
4236
if let Some(contract) = &self.contract {
4337
let state = contract.get_state_of_amm().await?;
4438
let sqrt_price = state.sqrt_price;
@@ -48,7 +42,7 @@ impl<P: JsonRpcClient + 'static> Pool<P> {
4842
let adjusted_price = price * (10.0_f64.powi(self.token_a_decimals as i32 - self.token_b_decimals as i32));
4943
Ok((adjusted_price, state.tick))
5044
} else {
51-
anyhow::bail!("Provider not set for pool")
45+
anyhow::bail!("Contract not set for pool")
5246
}
5347
}
5448

src/price_tracker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<P: JsonRpcClient + 'static> PriceTracker<P> {
4242
}
4343

4444
async fn update_price(&self) -> Result<(f64, i32, u64)> {
45-
let (price, tick) = self.pool.get_adjusted_current_price_and_tick(&self.provider).await?;
45+
let (price, tick) = self.pool.get_adjusted_current_price_and_tick().await?;
4646
let timestamp = std::time::SystemTime::now()
4747
.duration_since(std::time::UNIX_EPOCH)?
4848
.as_secs();

0 commit comments

Comments
 (0)