diff --git a/src/behaviors/deployer.rs b/src/behaviors/deployer.rs index d743abb..9fbee6c 100644 --- a/src/behaviors/deployer.rs +++ b/src/behaviors/deployer.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use anyhow::{anyhow, Result}; -use arbiter_bindings::bindings::{arbiter_token::ArbiterToken, liquid_exchange::LiquidExchange}; +use arbiter_bindings::bindings::liquid_exchange::LiquidExchange; use arbiter_core::middleware::ArbiterMiddleware; use arbiter_engine::{ machine::{Behavior, EventStream}, @@ -15,27 +15,15 @@ use crate::bindings::uniswap_v3_factory::UniswapV3Factory; #[derive(Debug, Deserialize, Serialize)] pub struct DeploymentData { - token_0: H160, - token_1: H160, factory: H160, liquid_exchange: H160, - pool: H160, } impl DeploymentData { - pub fn new( - token_0: H160, - token_1: H160, - factory: H160, - liquid_exchange: H160, - pool: H160, - ) -> Self { + pub fn new(factory: H160, liquid_exchange: H160) -> Self { Self { - token_0, - token_1, factory, liquid_exchange, - pool, } } } @@ -50,28 +38,12 @@ impl Behavior<()> for Deployer { client: Arc, messager: Messager, ) -> Result>> { - let token_0 = - ArbiterToken::deploy(client.clone(), ("Token 0".to_owned(), "TKN".to_owned(), 18))? - .send() - .await?; - let token_1 = ArbiterToken::deploy( - client.clone(), - ("Token 1".to_owned(), "TKN0".to_owned(), 18), - )? - .send() - .await?; - let factory = deploy_factory(&client).await?; let liquid_exchange = deploy_liquid_exchange(&client).await?; - let pool = create_pool(&factory, token_0.address(), token_1.address()).await?; - let deployment_data = DeploymentData { - token_0: token_0.address(), - token_1: token_1.address(), factory: factory.address(), liquid_exchange: liquid_exchange.address(), - pool, }; messager @@ -82,21 +54,6 @@ impl Behavior<()> for Deployer { } } -async fn deploy_token( - client: Arc, - name: &str, - symbol: &str, -) -> Result> { - let token = ArbiterToken::deploy( - client.clone(), - (String::from(name), String::from(symbol), 18_u8), - )? - .send() - .await?; - - Ok(token) -} - async fn deploy_factory( client: &Arc, ) -> Result> { @@ -116,14 +73,3 @@ async fn deploy_liquid_exchange( .await .map_err(|e| anyhow!("Failed to send liquid exchange: {}", e)) } - -async fn create_pool(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)) -} diff --git a/src/behaviors/mod.rs b/src/behaviors/mod.rs index c297646..f2e83bb 100644 --- a/src/behaviors/mod.rs +++ b/src/behaviors/mod.rs @@ -3,10 +3,16 @@ use arbiter_macros::Behaviors; use serde::{Deserialize, Serialize}; pub mod deployer; +pub mod pool_admin; pub mod token_admin; + use deployer::Deployer; +use pool_admin::PoolAdmin; +use token_admin::TokenAdmin; #[derive(Behaviors, Debug, Serialize, Deserialize)] pub enum Behaviors { Deployer(Deployer), + TokenAdmin(TokenAdmin), + PoolAdmin(PoolAdmin), } diff --git a/src/behaviors/pool_admin.rs b/src/behaviors/pool_admin.rs new file mode 100644 index 0000000..204bc30 --- /dev/null +++ b/src/behaviors/pool_admin.rs @@ -0,0 +1,73 @@ +use std::sync::Arc; + +use anyhow::{anyhow, Result}; +use arbiter_core::middleware::ArbiterMiddleware; +use arbiter_engine::messager::{Message, Messager}; +use ethers::types::H160; + +use super::*; +use crate::bindings::uniswap_v3_factory::UniswapV3Factory; + +#[derive(Debug, Serialize, Deserialize)] +pub struct PoolAdmin { + #[serde(skip)] + pub messager: Option, + + #[serde(skip)] + pub client: Option>, +} + +/// Used as an action to ask what tokens are available. +#[derive(Debug, Deserialize, Serialize)] +pub enum PoolAdminQuery { + /// Deploy request. + PoolCreation(PoolCreation), +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct PoolCreation { + factory: H160, + token_0: H160, + token_1: H160, + fee: u32, +} + +#[async_trait::async_trait] +impl Behavior for PoolAdmin { + async fn startup( + &mut self, + client: Arc, + messager: Messager, + ) -> Result>> { + self.client = Some(client.clone()); + self.messager = Some(messager.clone()); + + Ok(None) + } + + async fn process(&mut self, event: Message) -> Result { + let query: PoolAdminQuery = match serde_json::from_str(&event.data) { + Ok(query) => query, + Err(_) => { + eprintln!("Failed to deserialize the event data into a PoolAdminQuery"); + return Ok(ControlFlow::Continue); + } + }; + + match query { + PoolAdminQuery::PoolCreation(pool_creation) => { + let _ = UniswapV3Factory::new(pool_creation.factory, self.client.clone().unwrap()) + .create_pool( + pool_creation.token_0, + pool_creation.token_1, + pool_creation.fee, + ) + .call() + .await + .map_err(|e| anyhow!("Failed to create pool: {}", e)); + + Ok(ControlFlow::Continue) + } + } + } +} diff --git a/src/behaviors/token_admin.rs b/src/behaviors/token_admin.rs index 5e9cb9f..75c7b1f 100644 --- a/src/behaviors/token_admin.rs +++ b/src/behaviors/token_admin.rs @@ -12,9 +12,14 @@ use crate::bindings::token::ArbiterToken; pub struct TokenAdmin { #[serde(skip)] pub tokens: Option>>, + pub token_data: HashMap, + #[serde(skip)] pub messager: Option, + + #[serde(skip)] + pub client: Option>, } #[derive(Debug, Serialize, Deserialize)] @@ -30,8 +35,12 @@ pub struct TokenData { pub enum TokenAdminQuery { /// Get the address of the token. AddressOf(String), + /// Mint tokens. MintRequest(MintRequest), + + /// Deploy request. + DeployRequest(TokenData), } /// Used as an action to mint tokens. @@ -73,6 +82,7 @@ impl Behavior for TokenAdmin { } self.tokens = Some(deployed_tokens); + self.client = Some(client.clone()); let message_content = serde_json::to_string(&token_addresses)?; @@ -130,6 +140,20 @@ impl Behavior for TokenAdmin { } Ok(ControlFlow::Continue) } + TokenAdminQuery::DeployRequest(deploy_request) => { + ArbiterToken::deploy( + self.client.clone().unwrap(), + ( + deploy_request.name, + deploy_request.symbol, + deploy_request.decimals, + ), + )? + .send() + .await?; + + Ok(ControlFlow::Continue) + } } } } @@ -169,6 +193,7 @@ mod tests { h }, messager: Some(messager.clone()), + client: None, }; let agent = Agent::builder("token_admin_agent");