diff --git a/src/behaviors/deployer.rs b/src/behaviors/deployer.rs index 0d8faa5..72ceeda 100644 --- a/src/behaviors/deployer.rs +++ b/src/behaviors/deployer.rs @@ -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::*; @@ -40,37 +41,62 @@ impl Behavior<()> for Deployer { client: Arc, messager: Messager, ) -> Result>> { - 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, + name: &str, + symbol: &str, + ) -> Result> { + 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, + ) -> Result> { + 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( + &self, + factory: &UniswapV3Factory, + token_0: H160, + token_1: H160, + ) -> Result where M: ethers::providers::Middleware { + factory + .create_pool(token_0, token_1, 100) + .call() + .await + .map_err(|e| anyhow!("Failed to create pool: {}", e)) } }