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

small refactor #7

Merged
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
80 changes: 53 additions & 27 deletions src/behaviors/deployer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::sync::Arc;

use anyhow::anyhow;
use anyhow::Result;
use arbiter_core::middleware::ArbiterMiddleware;
use arbiter_engine::{
machine::{Behavior, EventStream},
messager::{Messager, To},
};

use ethers::types::H160;

use super::*;
Expand Down Expand Up @@ -40,37 +41,62 @@ impl Behavior<()> for Deployer {
client: Arc<ArbiterMiddleware>,
messager: Messager,
) -> Result<Option<EventStream<()>>> {
let token_0 = ArbiterToken::deploy(
client.clone(),
(String::from("Token 0"), String::from("0"), 18),
)?
.send()
.await?;
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 pool = self.create_pool(&factory, token_0.address(), token_1.address()).await?;

let token_1 = ArbiterToken::deploy(
client.clone(),
(String::from("Token 1"), String::from("1"), 18),
)?
.send()
.await?;
let deployment_data = DeploymentData {
token_0: token_0.address(),
token_1: token_1.address(),
factory: factory.address(),
pool: pool,
};

let factory = UniswapV3Factory::deploy(client, ())?.send().await?;
messager.send(To::All, serde_json::to_string(&deployment_data)?).await;

let pool = factory
.create_pool(token_0.address(), token_1.address(), 100)
.call()
.await?;
Ok(None)
}
}

// Construct a data object that all other behaviours will recieve containing requisites for simulation.
let deployment_data = DeploymentData::new(
token_0.address(),
token_1.address(),
factory.address(),
pool,
);
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))?
.send()
.await
.map_err(|e| anyhow!("Failed to send token {}: {}", name, e))
}

messager.send(To::All, serde_json::to_string(&deployment_data)?).await;
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))
}

Ok(None)
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))
}
}
Loading