Skip to content

Commit

Permalink
feat: fully parametrize runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
ts0yu authored Sep 21, 2024
1 parent 0c08d8e commit 57219ae
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 41 deletions.
4 changes: 2 additions & 2 deletions contracts/utils/src/ArenaController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ contract ArenaController {
poolManager.initialize(poolKey, sqrtPriceX96, hookData);
}

function addLiquidity(int256 liquidityDelta, int24 tickLower, int24 tickUpper) public {
function addLiquidity(int256 liquidityDelta, int24 tickLower, int24 tickUpper, bytes memory hookData) public {
if (liquidityDelta > 0) {
require(currency0.mint(address(this), uint256(liquidityDelta)), "Minting currency0 failed");
require(currency1.mint(address(this), uint256(liquidityDelta)), "Minting currency1 failed");
Expand All @@ -159,6 +159,6 @@ contract ArenaController {
salt: ""
});

router.modifyLiquidity(poolKey, params, "");
router.modifyLiquidity(poolKey, params, hookData);
}
}
19 changes: 11 additions & 8 deletions src/arena.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;

use alloy::{
primitives::Uint,
providers::{Provider, ProviderBuilder, WalletProvider},
signers::local::PrivateKeySigner,
};
Expand Down Expand Up @@ -42,16 +41,20 @@ impl<V> Arena<V> {
pub async fn run(&mut self, config: Config) -> Result<(), ArenaError> {
let admin_provider = self.providers[&0].clone();

let controller =
ArenaController::deploy(admin_provider.clone(), config.fee, Uint::from(1)).await?;
let controller = ArenaController::deploy(
admin_provider.clone(),
config.manager_fee,
config.initial_price,
)
.await?;

controller
.setPool(
Uint::from(0),
Signed::try_from(2).unwrap(),
Address::default(),
Uint::from(79228162514264337593543950336_u128),
Bytes::new(),
config.pool_fee,
config.tick_spacing,
config.hooks,
config.sqrt_price_x96,
config.hook_data,
)
.send()
.await
Expand Down
2 changes: 1 addition & 1 deletion src/artifacts/ArenaController.json

Large diffs are not rendered by default.

45 changes: 42 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@
use alloy::primitives::U256;

use super::*;

/// Configuration for the simulation.
pub struct Config {
/// Number of steps to run the simulation for.
pub steps: usize,

/// Pool manager fee.
pub fee: U256,
pub manager_fee: U256,

/// Pool tick spacing.
pub tick_spacing: Signed<24, 1>,

/// Pool hook data.
pub hook_data: Bytes,

/// Pool sqrt price x96.
pub sqrt_price_x96: Uint<160, 3>,

/// Pool fee.
pub pool_fee: Uint<24, 1>,

/// Initial price.
pub initial_price: U256,

/// Pool hooks.
pub hooks: Address,
}

impl Config {
/// Public constructor function for a new [`Config`].
pub fn new(fee: U256, steps: usize) -> Self {
Config { steps, fee }
#[allow(clippy::too_many_arguments)]
pub fn new(
steps: usize,
manager_fee: U256,
tick_spacing: Signed<24, 1>,
hook_data: Bytes,
sqrt_price_x96: Uint<160, 3>,
pool_fee: Uint<24, 1>,
initial_price: U256,
hooks: Address,
) -> Self {
Self {
steps,
manager_fee,
tick_spacing,
hook_data,
sqrt_price_x96,
pool_fee,
initial_price,
hooks,
}
}
}
7 changes: 1 addition & 6 deletions src/engine/arbitrageur.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use async_trait::async_trait;

use super::*;
use crate::{
types::controller::ArenaController,
AnvilProvider, Signal,
};
use crate::{types::controller::ArenaController, AnvilProvider, Signal};

/// Generic trait allowing user defined arbitrage strategies.
#[async_trait]
Expand Down Expand Up @@ -45,8 +42,6 @@ impl Arbitrageur for FixedArbitrageur {
.watch()
.await
.unwrap();

println!("current: {}", signal.current_value);
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use alloy::{
};

use super::*;
use crate::{
error::ArenaError,
types::controller::ArenaController,
};
use crate::{error::ArenaError, types::controller::ArenaController};
/// Defines a trait for custom arbitrage strategies.
pub mod arbitrageur;

Expand All @@ -28,12 +25,13 @@ impl Engine {
liquidity_delta: I256,
tick_lower: Signed<24, 1>,
tick_upper: Signed<24, 1>,
hook_data: Bytes,
provider: AnvilProvider,
) -> Result<(), ArenaError> {
let controller = ArenaController::new(self.controller, provider.clone());

controller
.addLiquidity(liquidity_delta, tick_lower, tick_upper)
.addLiquidity(liquidity_delta, tick_lower, tick_upper, hook_data)
.nonce(
provider
.get_transaction_count(provider.default_signer_address())
Expand Down
33 changes: 17 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,15 @@ impl Signal {

#[cfg(test)]
mod tests {
use alloy::{
primitives::{Signed, Uint, I256},
};
use alloy::primitives::{Signed, Uint, I256};
use async_trait::async_trait;
use rug::{ops::Pow, Float};

use super::*;
use crate::{
arena::{Arena, ArenaBuilder},
config::Config,
engine::{
arbitrageur::{FixedArbitrageur},
inspector::EmptyInspector,
},
engine::{arbitrageur::FixedArbitrageur, inspector::EmptyInspector},
feed::OrnsteinUhlenbeck,
strategy::Strategy,
};
Expand All @@ -170,6 +165,7 @@ mod tests {
I256::try_from(10000000).unwrap(),
Signed::try_from(-887272).unwrap(),
Signed::try_from(887272).unwrap(),
Bytes::new(),
provider,
)
.await
Expand All @@ -178,16 +174,10 @@ mod tests {
async fn process(
&self,
_provider: AnvilProvider,
signal: Signal,
_signal: Signal,
_inspector: &mut Box<dyn Inspector<T>>,
_engine: Engine,
) {
let sqrt_price_x96 =
Float::with_val(53, Float::parse(signal.sqrt_price_x96.to_string()).unwrap());
let q96 = Float::with_val(53, 2).pow(96);
let price = Float::with_val(53, sqrt_price_x96 / q96).pow(2);

println!("price: {}", price);
}
}

Expand All @@ -199,11 +189,22 @@ mod tests {
.with_strategy(Box::new(StrategyMock))
.with_feed(Box::new(OrnsteinUhlenbeck::new(1.0, 0.1, 1.0, 0.1, 0.1)))
.with_inspector(Box::new(EmptyInspector {}))
.with_arbitrageur(Box::new(FixedArbitrageur { depth: Signed::try_from(10000).unwrap() }))
.with_arbitrageur(Box::new(FixedArbitrageur {
depth: Signed::try_from(10000).unwrap(),
}))
.build();

arena
.run(Config::new(Uint::from(5000), 10000))
.run(Config::new(
100,
Uint::from(0),
Signed::try_from(2).unwrap(),
Bytes::new(),
Uint::from(79228162514264337593543950336_u128),
Uint::from(0),
Uint::from(1),
Address::ZERO,
))
.await
.unwrap();
}
Expand Down

0 comments on commit 57219ae

Please sign in to comment.