Skip to content

Commit

Permalink
feat: generic ito process, general clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ts0yu committed Jul 23, 2024
1 parent e0f11c3 commit bde79ab
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 115 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
alloy = { version = "0.1.3", features = ["full", "node-bindings", "sol-types", "contract"] }
alloy = { version = "0.1.3", features = ["full", "node-bindings", "sol-types", "contract", "transports", "transport-http"] }
tokio = { version = "1.36.0", features = ["full"] }
revm = "10.0.0"
octane = { git = "https://github.com/arena-rs/octane" }
Expand All @@ -18,6 +18,7 @@ async-trait = "0.1.81"
anyhow = "1.0.86"
tracing = "0.1.40"
env_logger = "0.11.3"
alloy-transport-http = "0.1"
serde_json = "1.0.120"
futures = "0.3.30"
rand = "0.8.5"
Expand Down
16 changes: 5 additions & 11 deletions src/arbitrageur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,12 @@ impl Behavior<Message> for Arbitrageur {
let mut stream = messager.clone().stream().unwrap();

while let Some(event) = stream.next().await {
let query: DeploymentResponse = match serde_json::from_str(&event.data) {
Ok(query) => query,
Err(_) => {
eprintln!("Failed to deserialize the event data into a DeploymentResponse");
continue;
if let Ok(query) = serde_json::from_str::<DeploymentResponse>(&event.data) {
match query {
DeploymentResponse::PoolManager(address) => self.deployment = Some(address),
DeploymentResponse::Pool(params) => self.pool = Some(params),
_ => {}
}
};

match query {
DeploymentResponse::PoolManager(address) => self.deployment = Some(address),
DeploymentResponse::Pool(params) => self.pool = Some(params),
_ => {}
}

if self.pool.is_some() && self.deployment.is_some() {
Expand Down
22 changes: 10 additions & 12 deletions src/deployer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use super::*;

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Deployer {
#[serde(skip)]
pub messager: Option<Messager>,

#[serde(skip)]
pub client: Option<Arc<AnvilProvider>>,
pub base: Base,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand Down Expand Up @@ -55,8 +51,8 @@ impl Behavior<Message> for Deployer {
)
.await?;

self.client = Some(client.clone());
self.messager = Some(messager.clone());
self.base.client = Some(client.clone());
self.base.messager = Some(messager.clone());

Ok(Some(messager.stream().unwrap()))
}
Expand All @@ -80,13 +76,14 @@ impl Behavior<Message> for Deployer {
decimals,
} => {
let token =
ArenaToken::deploy(self.client.clone().unwrap(), name, symbol, decimals)
ArenaToken::deploy(self.base.client.clone().unwrap(), name, symbol, decimals)
.await
.unwrap();

println!("Token deployed at address: {:?}", token.address());

self.messager
self.base
.messager
.clone()
.unwrap()
.send(To::All, DeploymentResponse::Token(*token.address()))
Expand All @@ -100,15 +97,16 @@ impl Behavior<Message> for Deployer {
initial_price,
} => {
let lex = LiquidExchange::deploy(
self.client.clone().unwrap(),
self.base.client.clone().unwrap(),
token_0,
token_1,
U256::from((initial_price * 10f64.powf(18.0)) as u64),
)
.await
.unwrap();

self.messager
self.base
.messager
.clone()
.unwrap()
.send(To::All, DeploymentResponse::LiquidExchange(*lex.address()))
Expand Down
25 changes: 16 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{fmt::Debug, sync::Arc};

use alloy::primitives::{Address, Bytes, Uint, U256};
use anyhow::Result;
Expand All @@ -23,8 +23,8 @@ use crate::{
pool_admin::PoolParams,
price_changer::{PriceChanger, PriceUpdate},
types::process::{OrnsteinUhlenbeck, StochasticProcess},
LiquidExchange::LiquidExchangeInstance,
};

pub mod arbitrageur;
pub mod bindings;
pub mod deployer;
Expand All @@ -33,6 +33,15 @@ pub mod pool_admin;
pub mod price_changer;
pub mod types;

#[derive(Debug, Default, Deserialize, Serialize, Clone)]
pub struct Base {
#[serde(skip)]
pub messager: Option<Messager>,

#[serde(skip)]
pub client: Option<Arc<AnvilProvider>>,
}

#[cfg(test)]
mod tests {
use octane::{agent::Agent, world::World};
Expand Down Expand Up @@ -172,20 +181,18 @@ mod tests {
client: None,
});

let deployer = Agent::builder("deployer").with_behavior(Deployer {
messager: None,
client: None,
});
let deployer = Agent::builder("deployer").with_behavior(Deployer::default());

let mock_deployer = Agent::builder("mock_deployer").with_behavior(MockOrchestrator {
client: None,
messager: None,
tokens: vec![],
});

let changer = Agent::builder("pricechanger").with_behavior(PriceChanger::new(
OrnsteinUhlenbeck::new(1.0, 0.15, 0.0, 0.3, 1.0 / 252.0),
));
let changer =
Agent::builder("pricechanger").with_behavior(PriceChanger::<OrnsteinUhlenbeck>::new(
OrnsteinUhlenbeck::new(1.0, 0.15, 0.0, 0.3, 1.0 / 252.0),
));

let mut world = World::new("id");

Expand Down
16 changes: 6 additions & 10 deletions src/liquidity_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use super::*;

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct LiquidityAdmin {
#[serde(skip)]
pub messager: Option<Messager>,

#[serde(skip)]
pub client: Option<Arc<AnvilProvider>>,
pub base: Base,

pub deployment: Option<Address>,
}
Expand All @@ -27,8 +23,8 @@ impl Behavior<Message> for LiquidityAdmin {
client: Arc<AnvilProvider>,
messager: Messager,
) -> Result<Option<EventStream<Message>>> {
self.client = Some(client.clone());
self.messager = Some(messager.clone());
self.base.client = Some(client.clone());
self.base.messager = Some(messager.clone());

let mut stream = messager.clone().stream().unwrap();

Expand All @@ -53,21 +49,21 @@ impl Behavior<Message> for LiquidityAdmin {
Err(_) => return Ok(ControlFlow::Continue),
};

ArenaToken::new(query.pool.currency0, self.client.clone().unwrap())
ArenaToken::new(query.pool.currency0, self.base.client.clone().unwrap())
.approve(self.deployment.unwrap(), Uint::MAX)
.send()
.await?
.watch()
.await?;

ArenaToken::new(query.pool.currency1, self.client.clone().unwrap())
ArenaToken::new(query.pool.currency1, self.base.client.clone().unwrap())
.approve(self.deployment.unwrap(), Uint::MAX)
.send()
.await?
.watch()
.await?;

PoolManager::new(self.deployment.unwrap(), self.client.clone().unwrap())
PoolManager::new(self.deployment.unwrap(), self.base.client.clone().unwrap())
.modifyLiquidity(query.pool, query.modification, Bytes::default())
.send()
.await?
Expand Down
18 changes: 6 additions & 12 deletions src/pool_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ use super::*;

#[derive(Debug, Serialize, Deserialize)]
pub struct PoolAdmin {
#[serde(skip)]
pub messager: Option<Messager>,

#[serde(skip)]
pub client: Option<Arc<AnvilProvider>>,
pub base: Base,

pub deployment: Option<Address>,
}
Expand All @@ -30,9 +26,6 @@ impl Behavior<Message> for PoolAdmin {
client: Arc<AnvilProvider>,
messager: Messager,
) -> Result<Option<EventStream<Message>>> {
self.client = Some(client.clone());
self.messager = Some(messager.clone());

let mut stream = messager.clone().stream().unwrap();

while let Some(event) = stream.next().await {
Expand All @@ -50,8 +43,8 @@ impl Behavior<Message> for PoolAdmin {
}
}

self.client = Some(client.clone());
self.messager = Some(messager.clone());
self.base.client = Some(client.clone());
self.base.messager = Some(messager.clone());

Ok(Some(messager.clone().stream().unwrap()))
}
Expand All @@ -71,7 +64,7 @@ impl Behavior<Message> for PoolAdmin {

// will never panic as is always Some
let pool_manager =
PoolManager::new(self.deployment.unwrap(), self.client.clone().unwrap());
PoolManager::new(self.deployment.unwrap(), self.base.client.clone().unwrap());

let tx = pool_manager.initialize(
pool_creation.key,
Expand All @@ -81,7 +74,8 @@ impl Behavior<Message> for PoolAdmin {

tx.send().await?.watch().await?;

self.messager
self.base
.messager
.clone()
.unwrap()
.send(To::All, DeploymentResponse::Pool(key))
Expand Down
Loading

0 comments on commit bde79ab

Please sign in to comment.