Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: using main macro #14

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ edition = "2021"
[dependencies]
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
arbiter-core = { version = "0.10.1" }
arbiter-macros = { version = "0.1.1" }
arbiter-engine = { version = "0.3.0" }
arbiter-bindings = "0.1.6"
arbiter-core = "0.10.3"
arbiter-macros = "0.1.3"
arbiter-engine = "0.3.1"
tokio = { version = "1.36.0", features = ["full"] }
ethers = { version = "2.0.10" }
ethers = "2.0.10"
serde = { version = "1.0.189", features = ["derive"] }
anyhow = { version = "1.0.79" }
anyhow = "1.0.79"
async-trait = "0.1.74"
clap = { version = "4.4.8", features = ["derive"] }
serde_json = "1.0.113"
2 changes: 1 addition & 1 deletion configs/example.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[[my_agent]]
Incrementer = { max_number_of_times = 5 }
Deployer = {}
132 changes: 68 additions & 64 deletions src/behaviors/deployer.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#![allow(dead_code)]
use std::sync::Arc;

use anyhow::{anyhow, Result};
use arbiter_bindings::bindings::{arbiter_token::ArbiterToken, liquid_exchange::LiquidExchange};
use arbiter_core::middleware::ArbiterMiddleware;
use arbiter_engine::{
machine::{Behavior, EventStream},
messager::{Messager, To},
};
use ethers::types::H160;
use tracing::debug;

use super::*;
use crate::bindings::{
liquid_exchange::LiquidExchange, token::ArbiterToken, uniswap_v3_factory::UniswapV3Factory,
};
use crate::bindings::uniswap_v3_factory::UniswapV3Factory;

#[derive(Debug, Deserialize, Serialize)]
pub struct DeploymentData {
Expand Down Expand Up @@ -41,7 +42,7 @@ impl DeploymentData {
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Deployer;
pub struct Deployer {}

#[async_trait::async_trait]
impl Behavior<()> for Deployer {
Expand All @@ -50,15 +51,24 @@ impl Behavior<()> for Deployer {
client: Arc<ArbiterMiddleware>,
messager: Messager,
) -> Result<Option<EventStream<()>>> {
let token_0 = self.deploy_token(&client, "Token 0", "0").await?;
let token_1 = self.deploy_token(&client, "Token 1", "1").await?;

let factory = self.deploy_factory(&client).await?;
let liquid_exchange = self.deploy_liquid_exchange(&client).await?;
println!("Prior to token deployment.");
let token_0 =
ArbiterToken::deploy(client.clone(), ("Token 0".to_owned(), "TKN".to_owned(), 18))?
.send()
.await?;
println!("Deployed token 0");
let token_1 = ArbiterToken::deploy(
client.clone(),
("Token 1".to_owned(), "TKN0".to_owned(), 18),
)?
.send()
.await?;
println!("Deployed tokens");
let factory = deploy_factory(&client).await?;
let liquid_exchange = deploy_liquid_exchange(&client).await?;

let pool = self
.create_pool(&factory, token_0.address(), token_1.address())
.await?;
let pool = create_pool(&factory, token_0.address(), token_1.address()).await?;
println!("Got here.");

let deployment_data = DeploymentData {
token_0: token_0.address(),
Expand All @@ -68,66 +78,60 @@ impl Behavior<()> for Deployer {
pool,
};

let _ = messager
println!("Deployment data: {:?}", deployment_data);
messager
.send(To::All, serde_json::to_string(&deployment_data)?)
.await;
.await?;
debug!("Sent deployment data: {:?}", deployment_data);

Ok(None)
}
}

impl Deployer {
async fn deploy_token(
&self,
client: &Arc<ArbiterMiddleware>,
name: &str,
symbol: &str,
) -> Result<ArbiterToken<ArbiterMiddleware>> {
ArbiterToken::deploy(
client.clone(),
(String::from(name), String::from(symbol), 18),
)
.map_err(|e| anyhow!("Failed to deploy token {}: {}", name, e))?
async fn deploy_token(
client: Arc<ArbiterMiddleware>,
name: &str,
symbol: &str,
) -> Result<ArbiterToken<ArbiterMiddleware>> {
println!("In here.");
let thing = ArbiterToken::deploy(
client.clone(),
(String::from(name), String::from(symbol), 18),
)?
.send()
.await;
println!("Thing: {:?}", thing);

Ok(thing?)
}

async fn deploy_factory(
client: &Arc<ArbiterMiddleware>,
) -> Result<UniswapV3Factory<ArbiterMiddleware>> {
UniswapV3Factory::deploy(client.clone(), ())
.map_err(|e| anyhow!("Failed to deploy factory: {}", e))?
.send()
.await
.map_err(|e| anyhow!("Failed to send token {}: {}", name, e))
}

async fn deploy_factory(
&self,
client: &Arc<ArbiterMiddleware>,
) -> Result<UniswapV3Factory<ArbiterMiddleware>> {
UniswapV3Factory::deploy(client.clone(), ())
.map_err(|e| anyhow!("Failed to deploy factory: {}", e))?
.send()
.await
.map_err(|e| anyhow!("Failed to send factory deployment: {}", e))
}
.map_err(|e| anyhow!("Failed to send factory deployment: {}", e))
}

async fn deploy_liquid_exchange(
&self,
client: &Arc<ArbiterMiddleware>,
) -> Result<LiquidExchange<ArbiterMiddleware>> {
LiquidExchange::deploy(client.clone(), ())
.map_err(|e| anyhow!("Failed to deploy liquid exchange: {}", e))?
.send()
.await
.map_err(|e| anyhow!("Failed to send liquid exchange: {}", e))
}
async fn deploy_liquid_exchange(
client: &Arc<ArbiterMiddleware>,
) -> Result<LiquidExchange<ArbiterMiddleware>> {
LiquidExchange::deploy(client.clone(), ())
.map_err(|e| anyhow!("Failed to deploy liquid exchange: {}", e))?
.send()
.await
.map_err(|e| anyhow!("Failed to send liquid exchange: {}", e))
}

async fn create_pool<M>(
&self,
factory: &UniswapV3Factory<M>,
token_0: H160,
token_1: H160,
) -> Result<H160>
where
M: ethers::providers::Middleware,
{
factory
.create_pool(token_0, token_1, 100)
.call()
.await
.map_err(|e| anyhow!("Failed to create pool: {}", e))
}
async fn create_pool<M>(factory: &UniswapV3Factory<M>, token_0: H160, token_1: H160) -> Result<H160>
where
M: ethers::providers::Middleware,
{
factory
.create_pool(token_0, token_1, 100)
.call()
.await
.map_err(|e| anyhow!("Failed to create pool: {}", e))
}
8 changes: 7 additions & 1 deletion src/behaviors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use arbiter_engine::machine::*;
use arbiter_macros::Behaviors;
use serde::{Deserialize, Serialize};

pub mod deployer;
use deployer::Deployer;

pub use deployer::Deployer;
#[derive(Behaviors, Debug, Serialize, Deserialize)]
pub enum Behaviors {
Deployer(Deployer),
}
34 changes: 22 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
use arbiter_engine::{agent::Agent, world::World};

pub mod behaviors;
pub mod bindings;

use crate::behaviors::Deployer;

#[tokio::main]
async fn main() {
let mut world = World::new("univ3");

let deployer = Agent::builder("deployer").with_behavior(Deployer);
use crate::behaviors::Behaviors;

world.add_agent(deployer);
let _ = world.run().await;
}
/// To run this example, you can do the following from the project root directory:
/// ```sh
/// cargo run simulate configs/example.toml
/// ```
/// If you would like to see more detailed logs, you can run the following:
/// ```sh
/// cargo run simulate configs/example.toml -vvv
/// ```
/// to get `debug` level logs (the default with no verbosity is `ERROR`).
///
/// By running
/// ```sh
/// cargo run
/// ```
/// you will get the `--help` message for the project.
#[arbiter_macros::main(
name = "Uniswap V3 Simulation",
about = "Simulating Uniswap V3 with Arbiter",
behaviors = Behaviors
)]
async fn main() {}
Loading