Skip to content

Commit

Permalink
feat(Refactor): add additional context to pool, type conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
ts0yu authored Aug 19, 2024
1 parent 69b1258 commit 479265f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "contracts/utils/lib/solmate"]
path = contracts/utils/lib/solmate
url = https://github.com/tranmissions11/solmate
[submodule "contracts/utils/lib/v4-core"]
path = contracts/utils/lib/v4-core
url = https://github.com/uniswap/v4-core
1 change: 1 addition & 0 deletions contracts/utils/lib/v4-core
Submodule v4-core added at 799dd2
33 changes: 29 additions & 4 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
engine::{arbitrageur::Arbitrageur, inspector::Inspector},
feed::Feed,
strategy::Strategy,
types::{ArenaToken, LiquidExchange, PoolManager, PoolManager::PoolKey},
types::{ArenaToken, Fetcher, LiquidExchange, PoolManager, PoolManager::PoolKey},
};

/// Represents an [`Arena`] that can be used to run a simulation and execute strategies.
Expand Down Expand Up @@ -43,6 +43,8 @@ impl<V> Arena<V> {
.await
.unwrap();

let fetcher = Fetcher::deploy(admin_provider.clone()).await.unwrap();

let currency_0 = ArenaToken::deploy(
admin_provider.clone(),
String::from("Currency 0"),
Expand Down Expand Up @@ -70,9 +72,9 @@ impl<V> Arena<V> {
.await
.unwrap();

if *currency_1.address() < *currency_0.address() {
self.pool.currency0 = *currency_1.address();
self.pool.currency1 = *currency_0.address();
if *currency_1.address() > *currency_0.address() {
(self.pool.currency0, self.pool.currency1) =
(*currency_0.address(), *currency_1.address());
}

pool_manager
Expand All @@ -84,26 +86,49 @@ impl<V> Arena<V> {
.call()
.await
.unwrap();
// .watch()
// .await
// .unwrap();

for (idx, strategy) in self.strategies.iter_mut().enumerate() {
let id = fetcher.toId(self.pool.clone().into()).call().await.unwrap();

let slot = fetcher
.getSlot0(*pool_manager.address(), id.poolId)
.call()
.await
.unwrap();

strategy.init(
self.providers[&(idx + 1)].clone(),
Signal::new(
*pool_manager.address(),
self.pool.clone(),
self.feed.current_value(),
None,
slot.tick,
slot.sqrtPriceX96,
),
&mut self.inspector,
);
}

for step in 0..config.steps {
let id = fetcher.toId(self.pool.clone().into()).call().await.unwrap();

let slot = fetcher
.getSlot0(*pool_manager.address(), id.poolId)
.call()
.await
.unwrap();

let signal = Signal::new(
*pool_manager.address(),
self.pool.clone(),
self.feed.current_value(),
Some(step),
slot.tick,
slot.sqrtPriceX96,
);

liquid_exchange
Expand Down
54 changes: 52 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub mod engine;
use alloy::{
network::{Ethereum, EthereumWallet},
node_bindings::{Anvil, AnvilInstance},
primitives::{Address, Bytes},
primitives::{Address, Bytes, U256},
providers::{
fillers::{ChainIdFiller, FillProvider, GasFiller, JoinFill, NonceFiller, WalletFiller},
Identity, RootProvider,
Expand All @@ -43,6 +43,10 @@ mod types {
#![allow(clippy::too_many_arguments)]
use alloy_sol_macro::sol;

use crate::types::{
Fetcher::PoolKey as FetcherPoolKey, PoolManager::PoolKey as ManagerPoolKey,
};

sol! {
#[sol(rpc)]
#[derive(Debug, Default)]
Expand All @@ -63,6 +67,37 @@ mod types {
ArenaToken,
"src/artifacts/ArenaToken.json"
}

sol! {
#[sol(rpc)]
#[derive(Debug)]
Fetcher,
"src/artifacts/Fetcher.json"
}

impl From<FetcherPoolKey> for ManagerPoolKey {
fn from(fetcher: FetcherPoolKey) -> Self {
ManagerPoolKey {
currency0: fetcher.currency0,
currency1: fetcher.currency1,
fee: fetcher.fee,
tickSpacing: fetcher.tickSpacing,
hooks: fetcher.hooks,
}
}
}

impl From<ManagerPoolKey> for FetcherPoolKey {
fn from(manager: ManagerPoolKey) -> Self {
FetcherPoolKey {
currency0: manager.currency0,
currency1: manager.currency1,
fee: manager.fee,
tickSpacing: manager.tickSpacing,
hooks: manager.hooks,
}
}
}
}

/// A signal that is passed to a [`Strategy`] to provide information about the current state of the pool.
Expand All @@ -79,16 +114,31 @@ pub struct Signal {

/// Current step of the simulation.
pub step: Option<usize>,

/// Current tick of the pool.
pub tick: i32,

/// Current price of the pool.
pub sqrt_price_x96: U256,
}

impl Signal {
/// Public constructor function for a new [`Signal`].
pub fn new(manager: Address, pool: PoolKey, current_value: f64, step: Option<usize>) -> Self {
pub fn new(
manager: Address,
pool: PoolKey,
current_value: f64,
step: Option<usize>,
tick: i32,
sqrt_price_x96: U256,
) -> Self {
Self {
manager,
pool,
current_value,
step,
tick,
sqrt_price_x96,
}
}
}
Expand Down

0 comments on commit 479265f

Please sign in to comment.