Skip to content

Commit

Permalink
feat(refactor): arbitrageur trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ts0yu authored Aug 15, 2024
1 parent 47eda5c commit ab7ae89
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The price of the simulation's underlying pool is set via a price process that im
To use Arena, the Rust programming language must be installed on your machine. This is commonly achieved using `rustup`.

Arena can be added to your library or binary with
```rust
```
cargo add arena-core
```

Expand Down
37 changes: 27 additions & 10 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use alloy::{primitives::U256, providers::ProviderBuilder, signers::local::Privat
use super::*;
use crate::{
config::Config,
engine::{arbitrageur::Arbitrageur, inspector::Inspector},
feed::Feed,
inspector::Inspector,
strategy::Strategy,
types::{ArenaToken, LiquidExchange, PoolManager, PoolManager::PoolKey},
};
Expand All @@ -28,6 +28,9 @@ pub struct Arena<V> {
/// The inspector that is used to evaluate the performance of the strategies.
pub inspector: Box<dyn Inspector<V>>,

/// The arbitrageur that is used to peg the pool.
pub arbitrageur: Box<dyn Arbitrageur>,

providers: HashMap<usize, AnvilProvider>,
}

Expand Down Expand Up @@ -96,27 +99,30 @@ impl<V> Arena<V> {
}

for step in 0..config.steps {
let signal = Signal::new(
*pool_manager.address(),
self.pool.clone(),
self.feed.current_value(),
Some(step),
);

liquid_exchange
.setPrice(
alloy::primitives::utils::parse_ether(&self.feed.step().to_string())
.unwrap(),
alloy::primitives::utils::parse_ether(&self.feed.step().to_string()).unwrap(),
)
.send()
.await
.unwrap()
.watch()
.await
.unwrap();


self.arbitrageur.arbitrage(&signal, admin_provider.clone());

for (idx, strategy) in self.strategies.iter_mut().enumerate() {
strategy.process(
self.providers[&(idx + 1)].clone(),
Signal::new(
*pool_manager.address(),
self.pool.clone(),
self.feed.current_value(),
Some(step),
),
signal.clone(),
&mut self.inspector,
);
}
Expand All @@ -143,6 +149,9 @@ pub struct ArenaBuilder<V> {
/// [`Arena::inspector`]
pub inspector: Option<Box<dyn Inspector<V>>>,

/// [`Arena::arbitrageur`]
pub arbitrageur: Option<Box<dyn Arbitrageur>>,

providers: Option<HashMap<usize, AnvilProvider>>,
}

Expand All @@ -162,6 +171,7 @@ impl<V> ArenaBuilder<V> {
feed: None,
providers: None,
inspector: None,
arbitrageur: None,
}
}

Expand Down Expand Up @@ -201,6 +211,12 @@ impl<V> ArenaBuilder<V> {
self
}

/// Set the inspector that is used to evaluate the performance of the strategies.
pub fn with_arbitrageur(mut self, arbitrageur: Box<dyn Arbitrageur>) -> Self {
self.arbitrageur = Some(arbitrageur);
self
}

/// Build the [`Arena`] with the given configuration.
pub fn build(self) -> Arena<V> {
let mut providers = HashMap::new();
Expand All @@ -225,6 +241,7 @@ impl<V> ArenaBuilder<V> {
pool: self.pool,
feed: self.feed.unwrap(),
inspector: self.inspector.unwrap(),
arbitrageur: self.arbitrageur.unwrap(),
providers,
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/engine/arbitrageur.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::{AnvilProvider, Signal};

/// Generic trait allowing user defined arbitrage strategies.
pub trait Arbitrageur {
/// Perform an arbitrage based on a [`Signal`].
fn arbitrage(&self, signal: &Signal, provider: AnvilProvider);
}
File renamed without changes.
2 changes: 2 additions & 0 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod arbitrageur;
pub mod inspector;
16 changes: 11 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![warn(missing_docs)]
#[doc = include_str!("../README.md")]

/// Defines the main simulation runtime.
pub mod arena;
Expand All @@ -13,8 +12,7 @@ pub mod feed;
/// Defines the base strategy trait.
pub mod strategy;

/// Defines the [`Inspector`] trait.
pub mod inspector;
pub mod engine;

use alloy::{
network::{Ethereum, EthereumWallet},
Expand All @@ -27,7 +25,7 @@ use alloy::{
transports::http::{Client, Http},
};

use crate::{inspector::Inspector, types::PoolManager::PoolKey};
use crate::{engine::inspector::Inspector, types::PoolManager::PoolKey};

/// Provider type that includes all necessary fillers to execute transactions on an [`Anvil`] node.
pub type AnvilProvider = FillProvider<
Expand Down Expand Up @@ -105,8 +103,15 @@ mod tests {
feed::OrnsteinUhlenbeck,
strategy::Strategy,
};
use crate::engine::arbitrageur::Arbitrageur;

struct StrategyMock;
struct InspectorMock;
struct ArbitrageurMock;

impl Arbitrageur for ArbitrageurMock {
fn arbitrage(&self, _signal: &Signal, _provider: AnvilProvider) {}
}

impl Inspector<f64> for InspectorMock {
fn inspect(&self, _step: usize) -> Option<f64> {
Expand Down Expand Up @@ -143,8 +148,9 @@ mod tests {
.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 {}))
.with_arbitrageur(Box::new(ArbitrageurMock {}))
.build();

arena.run(Config::new(0)).await;
arena.run(Config::new(1)).await;
}
}

0 comments on commit ab7ae89

Please sign in to comment.