Skip to content

Commit

Permalink
feat: abstract token deployment -> token_admin
Browse files Browse the repository at this point in the history
  • Loading branch information
ts0yu committed Feb 23, 2024
1 parent b7c6864 commit efcb897
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 56 deletions.
58 changes: 2 additions & 56 deletions src/behaviors/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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,
}
}
}
Expand All @@ -50,28 +38,12 @@ impl Behavior<()> for Deployer {
client: Arc<ArbiterMiddleware>,
messager: Messager,
) -> Result<Option<EventStream<()>>> {
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
Expand All @@ -82,21 +54,6 @@ impl Behavior<()> for Deployer {
}
}

async fn deploy_token(
client: Arc<ArbiterMiddleware>,
name: &str,
symbol: &str,
) -> Result<ArbiterToken<ArbiterMiddleware>> {
let token = ArbiterToken::deploy(
client.clone(),
(String::from(name), String::from(symbol), 18_u8),
)?
.send()
.await?;

Ok(token)
}

async fn deploy_factory(
client: &Arc<ArbiterMiddleware>,
) -> Result<UniswapV3Factory<ArbiterMiddleware>> {
Expand All @@ -116,14 +73,3 @@ async fn deploy_liquid_exchange(
.await
.map_err(|e| anyhow!("Failed to send liquid exchange: {}", e))
}

async fn create_pool<M>(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))
}
24 changes: 24 additions & 0 deletions src/behaviors/token_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ use crate::bindings::token::ArbiterToken;
pub struct TokenAdmin {
#[serde(skip)]
pub tokens: Option<HashMap<String, ArbiterToken<ArbiterMiddleware>>>,

pub token_data: HashMap<String, TokenData>,

#[serde(skip)]
pub messager: Option<Messager>,

#[serde(skip)]
pub client: Option<Arc<ArbiterMiddleware>>,
}

#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -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.
Expand Down Expand Up @@ -73,6 +82,7 @@ impl Behavior<Message> for TokenAdmin {
}

self.tokens = Some(deployed_tokens);
self.client = Some(client.clone());

let message_content = serde_json::to_string(&token_addresses)?;

Expand Down Expand Up @@ -130,6 +140,20 @@ impl Behavior<Message> 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)
}
}
}
}
Expand Down

0 comments on commit efcb897

Please sign in to comment.