From efcb897ee8ee16f6a70ee71bd4e80012ed1b4b61 Mon Sep 17 00:00:00 2001 From: ts0yu <120932697+ts0yu@users.noreply.github.com> Date: Fri, 23 Feb 2024 20:06:25 +0000 Subject: [PATCH 1/5] feat: abstract token deployment -> token_admin --- src/behaviors/deployer.rs | 58 ++---------------------------------- src/behaviors/token_admin.rs | 24 +++++++++++++++ 2 files changed, 26 insertions(+), 56 deletions(-) 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/token_admin.rs b/src/behaviors/token_admin.rs index 5e9cb9f..3187d80 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) + } } } } From 14e18bb44c409241510d55006816830b63c08ca8 Mon Sep 17 00:00:00 2001 From: ts0yu <120932697+ts0yu@users.noreply.github.com> Date: Fri, 23 Feb 2024 20:21:27 +0000 Subject: [PATCH 2/5] feat: pool_admin behaviour --- src/behaviors/mod.rs | 6 ++++ src/behaviors/pool_admin.rs | 68 ++++++++++++++++++++++++++++++++++++ src/behaviors/token_admin.rs | 1 + 3 files changed, 75 insertions(+) create mode 100644 src/behaviors/pool_admin.rs diff --git a/src/behaviors/mod.rs b/src/behaviors/mod.rs index c297646..7a60ad9 100644 --- a/src/behaviors/mod.rs +++ b/src/behaviors/mod.rs @@ -4,9 +4,15 @@ use serde::{Deserialize, Serialize}; pub mod deployer; pub mod token_admin; +pub mod pool_admin; + use deployer::Deployer; +use token_admin::TokenAdmin; +use pool_admin::PoolAdmin; #[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..b3d2dc8 --- /dev/null +++ b/src/behaviors/pool_admin.rs @@ -0,0 +1,68 @@ +use std::{collections::HashMap, sync::Arc}; + +use anyhow::Result; +use arbiter_core::middleware::ArbiterMiddleware; +use arbiter_engine::messager::{Message, Messager, To}; +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), +} + +pub struct PoolCreation { + factory: &UniswapV3Factory, + token_0: H160, + token_1: H160, + fee: usize, +} + +#[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) => { + pool_creation.factory + .create_pool(pool_creation.token_0, pool_creation.token_1, pool_creation.fee.into()) + .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 3187d80..75c7b1f 100644 --- a/src/behaviors/token_admin.rs +++ b/src/behaviors/token_admin.rs @@ -193,6 +193,7 @@ mod tests { h }, messager: Some(messager.clone()), + client: None, }; let agent = Agent::builder("token_admin_agent"); From bd9adbe0769913c08ee7729374c195883a032b4f Mon Sep 17 00:00:00 2001 From: ts0yu <120932697+ts0yu@users.noreply.github.com> Date: Fri, 23 Feb 2024 20:31:09 +0000 Subject: [PATCH 3/5] feat: fix pool_admin --- src/behaviors/mod.rs | 4 ++-- src/behaviors/pool_admin.rs | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/behaviors/mod.rs b/src/behaviors/mod.rs index 7a60ad9..f2e83bb 100644 --- a/src/behaviors/mod.rs +++ b/src/behaviors/mod.rs @@ -3,12 +3,12 @@ use arbiter_macros::Behaviors; use serde::{Deserialize, Serialize}; pub mod deployer; -pub mod token_admin; pub mod pool_admin; +pub mod token_admin; use deployer::Deployer; -use token_admin::TokenAdmin; use pool_admin::PoolAdmin; +use token_admin::TokenAdmin; #[derive(Behaviors, Debug, Serialize, Deserialize)] pub enum Behaviors { diff --git a/src/behaviors/pool_admin.rs b/src/behaviors/pool_admin.rs index b3d2dc8..b5fa11b 100644 --- a/src/behaviors/pool_admin.rs +++ b/src/behaviors/pool_admin.rs @@ -1,6 +1,7 @@ use std::{collections::HashMap, sync::Arc}; use anyhow::Result; +use anyhow::anyhow; use arbiter_core::middleware::ArbiterMiddleware; use arbiter_engine::messager::{Message, Messager, To}; use ethers::types::H160; @@ -24,11 +25,12 @@ pub enum PoolAdminQuery { PoolCreation(PoolCreation), } +#[derive(Debug, Deserialize, Serialize)] pub struct PoolCreation { - factory: &UniswapV3Factory, - token_0: H160, + factory: H160, + token_0: H160, token_1: H160, - fee: usize, + fee: u32, } #[async_trait::async_trait] @@ -55,11 +57,16 @@ impl Behavior for PoolAdmin { match query { PoolAdminQuery::PoolCreation(pool_creation) => { - pool_creation.factory - .create_pool(pool_creation.token_0, pool_creation.token_1, pool_creation.fee.into()) + 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)) + .map_err(|e| anyhow!("Failed to create pool: {}", e)); Ok(ControlFlow::Continue) } From e1c473ad3f44db3e05367c0d72b79181fa07d238 Mon Sep 17 00:00:00 2001 From: ts0yu <120932697+ts0yu@users.noreply.github.com> Date: Fri, 23 Feb 2024 20:33:32 +0000 Subject: [PATCH 4/5] nit: fmt --- src/behaviors/pool_admin.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/behaviors/pool_admin.rs b/src/behaviors/pool_admin.rs index b5fa11b..6ba3938 100644 --- a/src/behaviors/pool_admin.rs +++ b/src/behaviors/pool_admin.rs @@ -1,7 +1,6 @@ use std::{collections::HashMap, sync::Arc}; -use anyhow::Result; -use anyhow::anyhow; +use anyhow::{anyhow, Result}; use arbiter_core::middleware::ArbiterMiddleware; use arbiter_engine::messager::{Message, Messager, To}; use ethers::types::H160; @@ -57,8 +56,7 @@ impl Behavior for PoolAdmin { match query { PoolAdminQuery::PoolCreation(pool_creation) => { - UniswapV3Factory::new(pool_creation - .factory, self.client.clone().unwrap()) + UniswapV3Factory::new(pool_creation.factory, self.client.clone().unwrap()) .create_pool( pool_creation.token_0, pool_creation.token_1, From d7030319b55cf38079752747048639f85a0d60a1 Mon Sep 17 00:00:00 2001 From: ts0yu <120932697+ts0yu@users.noreply.github.com> Date: Fri, 23 Feb 2024 20:45:03 +0000 Subject: [PATCH 5/5] nit: clippy --- src/behaviors/pool_admin.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/behaviors/pool_admin.rs b/src/behaviors/pool_admin.rs index 6ba3938..204bc30 100644 --- a/src/behaviors/pool_admin.rs +++ b/src/behaviors/pool_admin.rs @@ -1,8 +1,8 @@ -use std::{collections::HashMap, sync::Arc}; +use std::sync::Arc; use anyhow::{anyhow, Result}; use arbiter_core::middleware::ArbiterMiddleware; -use arbiter_engine::messager::{Message, Messager, To}; +use arbiter_engine::messager::{Message, Messager}; use ethers::types::H160; use super::*; @@ -56,7 +56,7 @@ impl Behavior for PoolAdmin { match query { PoolAdminQuery::PoolCreation(pool_creation) => { - UniswapV3Factory::new(pool_creation.factory, self.client.clone().unwrap()) + let _ = UniswapV3Factory::new(pool_creation.factory, self.client.clone().unwrap()) .create_pool( pool_creation.token_0, pool_creation.token_1,