Skip to content

Commit

Permalink
Fixed liquid exchange deployment and double serializing issue (#32)
Browse files Browse the repository at this point in the history
* Fixed liquid exchange deployment and double serializing issue

* Fixed rustfmt
  • Loading branch information
pratraut authored Mar 12, 2024
1 parent a4d7acc commit 636c374
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 23 deletions.
2 changes: 1 addition & 1 deletion configs/stable_pool.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[[admin]]
Deployer = {}
Deployer = { liquid_exchange_parameters = { asset_token_parameters = { name = "Arbiter Token X", symbol = "ARBX", decimals = 18 }, quote_token_parameters = { name = "Arbiter Token Y", symbol = "ARBY", decimals = 18 }, initial_price = 1.0 }}

[[admin]]
PoolAdmin = {}
Expand Down
88 changes: 76 additions & 12 deletions src/behaviors/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
use std::sync::Arc;

use anyhow::{anyhow, Result};
use arbiter_bindings::bindings::liquid_exchange::LiquidExchange;
use arbiter_bindings::bindings::{arbiter_token::ArbiterToken, liquid_exchange::LiquidExchange};
use arbiter_core::middleware::ArbiterMiddleware;
use arbiter_engine::{
machine::{Behavior, EventStream},
messager::{Messager, To},
};
use ethers::types::H160;
use ethers::types::{H160, U256};
use tracing::info;

use self::token_admin::TokenData;
use super::*;
use crate::bindings::uniswap_v3_factory::UniswapV3Factory;

Expand All @@ -28,8 +30,17 @@ impl DeploymentData {
}
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct LiquidExchangeParameters {
pub asset_token_parameters: TokenData,
pub quote_token_parameters: TokenData,
pub initial_price: f64,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Deployer {}
pub struct Deployer {
pub liquid_exchange_parameters: LiquidExchangeParameters,
}

#[async_trait::async_trait]
impl Behavior<()> for Deployer {
Expand All @@ -39,16 +50,31 @@ impl Behavior<()> for Deployer {
messager: Messager,
) -> Result<Option<EventStream<()>>> {
let factory = deploy_factory(&client).await?;
let liquid_exchange = deploy_liquid_exchange(&client).await?;

let le_param = self.liquid_exchange_parameters.clone();
let liquid_exchange = deploy_liquid_exchange(
&client,
&le_param.asset_token_parameters,
&le_param.quote_token_parameters,
le_param.initial_price,
)
.await?;
info!("Factory deployed at {:?}", factory.address());
info!(
"Liquid exchange deployed at {:?}",
liquid_exchange.address()
);
info!(
"Liquid exchange initial price : {:?}",
liquid_exchange.price().call().await?
);

let deployment_data = DeploymentData {
factory: factory.address(),
liquid_exchange: liquid_exchange.address(),
};

messager
.send(To::All, serde_json::to_string(&deployment_data)?)
.await?;
messager.send(To::All, &deployment_data).await?;

Ok(None)
}
Expand All @@ -66,10 +92,48 @@ pub async fn deploy_factory(

pub async fn deploy_liquid_exchange(
client: &Arc<ArbiterMiddleware>,
arbx_param: &TokenData,
arby_param: &TokenData,
initial_price: f64,
) -> Result<LiquidExchange<ArbiterMiddleware>> {
LiquidExchange::deploy(client.clone(), ())
.map_err(|e| anyhow!("Failed to deploy liquid exchange: {}", e))?
.send()
.await
.map_err(|e| anyhow!("Failed to send liquid exchange: {}", e))
let initial_liquid_exchange_price = U256::from((initial_price * 10f64.powf(18.0)) as u64);

// Deploy an instance of the `ArbiterToken` contract using our predefined constants.
let arbx = ArbiterToken::deploy(
client.clone(),
(
arbx_param.name.clone(),
arbx_param.symbol.clone(),
arbx_param.decimals,
),
)?
.send()
.await?;
info!("Arbiter Token X contract deployed at {:?}", arbx.address());

// Deploy the second instance of the `ArbiterToken` contract using our predefined constants.
let arby = ArbiterToken::deploy(
client.clone(),
(
arby_param.name.clone(),
arby_param.symbol.clone(),
arby_param.decimals,
),
)?
.send()
.await?;
info!("Arbiter Token Y contract deployed at {:?}", arby.address());

LiquidExchange::deploy(
client.clone(),
(
arbx.address(),
arby.address(),
initial_liquid_exchange_price,
),
)
.map_err(|e| anyhow!("Failed to deploy liquid exchange: {}", e))?
.send()
.await
.map_err(|e| anyhow!("Failed to send liquid exchange: {}", e))
}
4 changes: 1 addition & 3 deletions src/behaviors/pool_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ mod tests {
fee: 100,
};

messager
.send(To::All, serde_json::to_string(&pool_creation_request)?)
.await?;
messager.send(To::All, &pool_creation_request).await?;

Ok(None)
}
Expand Down
21 changes: 14 additions & 7 deletions src/behaviors/token_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct TokenAdmin {
pub client: Option<Arc<ArbiterMiddleware>>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TokenData {
pub name: String,
pub symbol: String,
Expand Down Expand Up @@ -84,9 +84,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)?;

let _ = messager.send(To::All, &message_content).await;
let _ = messager.send(To::All, &token_addresses).await;

Ok(Some(messager.clone().stream().unwrap()))
}
Expand All @@ -103,8 +101,11 @@ impl Behavior<Message> for TokenAdmin {
match query {
TokenAdminQuery::AddressOf(token_name) => {
if let Some(token_data) = self.token_data.get(&token_name) {
let response = serde_json::to_string(&token_data.address)
.map_err(|_| anyhow::anyhow!("Failed to serialize token address"))?;
let response = if let Some(v) = token_data.address {
v
} else {
return Err(anyhow::anyhow!("Token address is not found."));
};
if let Some(messager) = &self.messager {
messager
.send(To::Agent(event.from.clone()), response)
Expand Down Expand Up @@ -141,7 +142,8 @@ impl Behavior<Message> for TokenAdmin {
Ok(ControlFlow::Continue)
}
TokenAdminQuery::DeployRequest(deploy_request) => {
ArbiterToken::deploy(
let req = deploy_request.clone();
let token = ArbiterToken::deploy(
self.client.clone().unwrap(),
(
deploy_request.name,
Expand All @@ -152,6 +154,11 @@ impl Behavior<Message> for TokenAdmin {
.send()
.await?;

self.token_data.insert(req.name.clone(), req.clone());
self.tokens
.as_mut()
.and_then(|token_obj| token_obj.insert(req.name, token));

Ok(ControlFlow::Continue)
}
}
Expand Down

0 comments on commit 636c374

Please sign in to comment.