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] 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) + } } } }