Skip to content

Commit

Permalink
feat(refactor): deploy tokens and liquid exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
ts0yu authored Aug 15, 2024
1 parent f6b8a01 commit be77e20
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 80 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "contracts/utils/lib/forge-std"]
path = contracts/utils/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "contracts/utils/lib/solmate"]
path = contracts/utils/lib/solmate
url = https://github.com/tranmissions11/solmate
1 change: 1 addition & 0 deletions contracts/utils/lib/solmate
Submodule solmate added at 97bdb2
19 changes: 0 additions & 19 deletions contracts/utils/script/Counter.s.sol

This file was deleted.

14 changes: 0 additions & 14 deletions contracts/utils/src/Counter.sol

This file was deleted.

24 changes: 0 additions & 24 deletions contracts/utils/test/Counter.t.sol

This file was deleted.

72 changes: 64 additions & 8 deletions src/arena.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::collections::HashMap;

use alloy::{providers::ProviderBuilder, signers::local::PrivateKeySigner};
use alloy::{primitives::U256, providers::ProviderBuilder, signers::local::PrivateKeySigner};

use super::*;
use crate::{
config::Config,
feed::Feed,
inspector::Inspector,
strategy::Strategy,
types::{PoolManager, PoolManager::PoolKey},
types::{ArenaToken, LiquidExchange, PoolManager, PoolManager::PoolKey},
};

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

let currency_0 = ArenaToken::deploy(
admin_provider.clone(),
String::from("Currency 0"),
String::from("C0"),
18,
)
.await
.unwrap();

let currency_1 = ArenaToken::deploy(
admin_provider.clone(),
String::from("Currency 1"),
String::from("C1"),
18,
)
.await
.unwrap();

let liquid_exchange = LiquidExchange::deploy(
admin_provider.clone(),
*currency_0.address(),
*currency_1.address(),
U256::from(1),
)
.await
.unwrap();

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

pool_manager
.initialize(
self.pool.clone(),
Expand All @@ -65,6 +97,18 @@ impl<V> Arena<V> {

for step in 0..config.steps {
for (idx, strategy) in self.strategies.iter_mut().enumerate() {
liquid_exchange
.setPrice(
alloy::primitives::utils::parse_ether(&self.feed.step().to_string())
.unwrap(),
)
.send()
.await
.unwrap()
.watch()
.await
.unwrap();

strategy.process(
self.providers[&(idx + 1)].clone(),
Signal::new(
Expand All @@ -91,7 +135,7 @@ pub struct ArenaBuilder<V> {
pub strategies: Vec<Box<dyn Strategy<V>>>,

/// [`Arena::pool`]
pub pool: Option<PoolKey>,
pub pool: PoolKey,

/// [`Arena::feed`]
pub feed: Option<Box<dyn Feed>>,
Expand All @@ -114,7 +158,7 @@ impl<V> ArenaBuilder<V> {
ArenaBuilder {
env: Anvil::default().spawn(),
strategies: Vec::new(),
pool: None,
pool: PoolKey::default(),
feed: None,
providers: None,
inspector: None,
Expand All @@ -127,9 +171,21 @@ impl<V> ArenaBuilder<V> {
self
}

/// Set the pool that the strategies are to be run against.
pub fn with_pool(mut self, pool: PoolKey) -> Self {
self.pool = Some(pool);
/// Set the pool fee.
pub fn with_fee(mut self, fee: u32) -> Self {
self.pool.fee = fee;
self
}

/// Set the pool tick spacing.
pub fn with_tick_spacing(mut self, tick_spacing: i32) -> Self {
self.pool.tickSpacing = tick_spacing;
self
}

/// Set the pool hooks.
pub fn with_hooks(mut self, hooks: Address) -> Self {
self.pool.hooks = hooks;
self
}

Expand Down Expand Up @@ -166,7 +222,7 @@ impl<V> ArenaBuilder<V> {
Arena {
env: self.env,
strategies: self.strategies,
pool: self.pool.unwrap(),
pool: self.pool,
feed: self.feed.unwrap(),
inspector: self.inspector.unwrap(),
providers,
Expand Down
67 changes: 52 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,26 @@ mod types {
#[allow(missing_docs)]
sol! {
#[sol(rpc)]
#[derive(Debug)]
#[derive(Debug, Default)]
PoolManager,
"contracts/v4-core/out/PoolManager.sol/PoolManager.json"
}

#[allow(missing_docs)]
sol! {
#[sol(rpc)]
#[derive(Debug)]
LiquidExchange,
"contracts/utils/out/LiquidExchange.sol/LiquidExchange.json"
}

#[allow(missing_docs)]
sol! {
#[sol(rpc)]
#[derive(Debug)]
ArenaToken,
"contracts/utils/out/ArenaToken.sol/ArenaToken.json"
}
}

/// A signal that is passed to a [`Strategy`] to provide information about the current state of the pool.
Expand Down Expand Up @@ -83,29 +99,50 @@ impl Signal {
#[cfg(test)]
mod tests {
use super::*;
use crate::{arena::ArenaBuilder, config::Config, feed::OrnsteinUhlenbeck, strategy::Strategy};

use crate::{
arena::{Arena, ArenaBuilder},
config::Config,
feed::OrnsteinUhlenbeck,
strategy::Strategy,
};
struct StrategyMock;
struct InspectorMock;

impl Strategy for StrategyMock {
fn init(&self, _provider: AnvilProvider, _signal: Signal) {}
fn process(&self, _provider: AnvilProvider, _signal: Signal) {}
impl Inspector<f64> for InspectorMock {
fn inspect(&self, _step: usize) -> Option<f64> {
None
}
fn log(&mut self, _value: f64) {}
fn save(&self) {}
}

impl<V> Strategy<V> for StrategyMock {
fn init(
&self,
_provider: AnvilProvider,
_signal: Signal,
_inspector: &mut Box<dyn Inspector<V>>,
) {
}
fn process(
&self,
_provider: AnvilProvider,
_signal: Signal,
_inspector: &mut Box<dyn Inspector<V>>,
) {
}
}

#[tokio::test]
async fn test_arena() {
let builder = ArenaBuilder::new();
let builder: ArenaBuilder<_> = ArenaBuilder::new();

let mut arena = builder
let mut arena: Arena<f64> = builder
.with_strategy(Box::new(StrategyMock {}))
.with_pool(PoolKey {
currency0: Address::default(),
currency1: Address::repeat_byte(1),
fee: 4000,
tickSpacing: 2,
hooks: Address::default(),
})
.with_fee(4000)
.with_tick_spacing(2)
.with_feed(Box::new(OrnsteinUhlenbeck::new(0.1, 0.1, 0.1, 0.1, 0.1)))
.with_inspector(Box::new(InspectorMock {}))
.build();

arena.run(Config::new(0)).await;
Expand Down

0 comments on commit be77e20

Please sign in to comment.