Skip to content

Commit

Permalink
test runs but we hit a revert
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Apr 10, 2024
1 parent 764761f commit 4fa7519
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 54 deletions.
116 changes: 68 additions & 48 deletions kit/src/behaviors/creator.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use super::*;
use crate::{behaviors::deployer::DeploymentData, pool::{constant_sum::ConstantSumPool, Pool, PoolType}};
use crate::{
behaviors::deployer::DeploymentData,
pool::{Pool, PoolType},
};
use arbiter_engine::machine::{Behavior, Configuration, Processing, Processor, State};
use bindings::dfmm::DFMM;
use futures_util::StreamExt;
use bindings::{constant_sum::ConstantSum, constant_sum_solver::ConstantSumSolver, dfmm::DFMM};
use serde::de::DeserializeOwned;

// Idea: Let's make a behavior that has two states:
Expand All @@ -29,99 +32,116 @@ pub struct PoolProcessor<P: PoolType> {
}

#[async_trait::async_trait]
impl<P, E> Behavior<E> for PoolCreator<Configuration<PoolConfig<P>>>
impl<P> Behavior<()> for PoolCreator<Configuration<PoolConfig<P>>>
where
P: PoolType + Send + Sync + 'static,
E: Send + Sync + 'static + DeserializeOwned,
// E: Send + Sync + 'static + DeserializeOwned,
{
type Processor = PoolCreator<Processing<PoolProcessor<P>>>;
// type Processor = PoolCreator<Processing<PoolProcessor<P>>>;
type Processor = ();
async fn startup(
&mut self,
client: Arc<ArbiterMiddleware>,
messager: Messager,
) -> Result<Option<(Self::Processor, EventStream<E>)>> {

) -> Result<Option<(Self::Processor, EventStream<()>)>> {
let mut stream = messager.stream()?;
let res = stream.next().await.unwrap();
let data: String =
serde_json::from_str(&res.data).expect("Failed to
deserialize message data");
let parsed_data: DeploymentData =
serde_json::from_str(&data).expect("Failed to deserialize
token data");
let data: String = serde_json::from_str(&res.data).expect(
"Failed to
deserialize message data",
);
let parsed_data: DeploymentData = serde_json::from_str(&data).expect(
"Failed to deserialize
token data",
);

let token_x = ArbiterToken::new(parsed_data.token_x, client.clone());
let token_y = ArbiterToken::new(parsed_data.token_y, client);
let (strategy_contract, solver_contract) = P::get_contracts(&parsed_data, client);
let token_y = ArbiterToken::new(parsed_data.token_y, client.clone());
let (strategy_contract, solver_contract) = P::get_contracts(&parsed_data, client.clone());
let dfmm = DFMM::new(parsed_data.dfmm, client);
let init_data = self.data.initial_allocation_data.clone();
let pool = P::create_pool(self.data.initial_allocation_data, vec![token_x, token_y], strategy_contract, solver_contract, dfmm).await?;
let pool = P::create_pool(
init_data,
vec![token_x, token_y],
strategy_contract,
solver_contract,
dfmm,
)
.await?;
trace!("Pool created at {:?}", pool.id);
Ok(None)
}
}

#[async_trait::async_trait]
impl<P, E> Processor<E> for PoolCreator<Processing<PoolProcessor<P>>>
where
P: PoolType + Send + Sync + 'static,
E: Send + Sync + 'static + DeserializeOwned,
{
async fn process(&mut self, _event: E) -> Result<ControlFlow> {
Ok(ControlFlow::Halt)
}
}
// #[async_trait::async_trait]
// impl<P, E> Processor<E> for PoolCreator<Processing<PoolProcessor<P>>>
// where
// P: PoolType + Send + Sync + 'static,
// E: Send + Sync + 'static + DeserializeOwned,
// {
// async fn process(&mut self, _event: E) -> Result<ControlFlow> {
// Ok(ControlFlow::Halt)
// }
// }

mod test {
use std::str::FromStr;

use arbiter_engine::{agent::Agent, world::World};
use ethers::types::Address;
use futures_util::StreamExt;
use tracing::{level_filters::LevelFilter, Level};
use tracing_subscriber::FmtSubscriber;

use self::{
bindings::constant_sum_solver::ConstantSumParams,
pool::constant_sum::{ConstantSumInitData, ConstantSumPool},
};
use crate::behaviors::deployer::{Deployer, DeploymentData};
use crate::behaviors::Behaviors::Creator;
use self::{bindings::constant_sum_solver::ConstantSumParams, pool::constant_sum::{ConstantSumInitData, ConstantSumPool}};

use super::*;

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn deployer_behavior_test() {
let subscriber = FmtSubscriber::builder().finish();
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::DEBUG)
.pretty()
.finish();
tracing::subscriber::set_global_default(subscriber).unwrap();

let mut world = World::new("test");
let messager = world.messager.clone();

let agent = Agent::builder("token_admin_agent");
let creator = Agent::builder("pool_creator_agent");
world.add_agent(creator.with_behavior(PoolCreator::<Configuration<PoolConfig<ConstantSumPool>>>
{ data: PoolConfig {
params: ConstantSumParams {
price: 0.into(),
swap_fee: 0.into(),
controller: Address::zero(),
},
initial_allocation_data: ConstantSumInitData {
name: "Test Pool".to_string(),
symbol: "TP".to_string(),
reserve_x: 0.into(),
reserve_y: 0.into(),
token_x_name: "Token X".to_string(),
token_y_name: "Token Y".to_string(),
let pool_creator = PoolCreator::<Configuration<PoolConfig<ConstantSumPool>>> {
data: PoolConfig {
params: ConstantSumParams {
price: eU256::zero(),
swap_fee: eU256::zero(),
price: 0.into(),
swap_fee: 0.into(),
controller: Address::zero(),
},
initial_allocation_data: ConstantSumInitData {
name: "Test Pool".to_string(),
symbol: "TP".to_string(),
reserve_x: 0.into(),
reserve_y: 0.into(),
token_x_name: "Token X".to_string(),
token_y_name: "Token Y".to_string(),
params: ConstantSumParams {
price: eU256::zero(),
swap_fee: eU256::zero(),
controller: Address::zero(),
},
},
token_list: vec![Address::zero(), Address::zero()],
},
token_list: vec![Address::zero(), Address::zero()],
}
}));
};
world.add_agent(creator.with_behavior(pool_creator));

world.add_agent(agent.with_behavior(Deployer {}));

world.run().await.unwrap();
}
}
}
11 changes: 9 additions & 2 deletions kit/src/pool/constant_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,22 @@ pub enum ConstantSumAllocationData {
// pub controller_fee: ::ethers::core::types::U256,
// }

#[async_trait::async_trait]
impl PoolType for ConstantSumPool {
type PoolParameters = ConstantSumParams;
type InitializationData = ConstantSumInitData;
type StrategyContract = ConstantSum<ArbiterMiddleware>;
type SolverContract = ConstantSumSolver<ArbiterMiddleware>;
type AllocationData = ConstantSumAllocationData;

fn get_contracts(deployment: &DeploymentData, client: Arc<ArbiterMiddleware>) -> (Self::StrategyContract, Self::SolverContract) {
(ConstantSum::new(deployment.constant_sum, client.clone()), ConstantSumSolver::new(deployment.constant_sum_solver, client))
fn get_contracts(
deployment: &DeploymentData,
client: Arc<ArbiterMiddleware>,
) -> (Self::StrategyContract, Self::SolverContract) {
(
ConstantSum::new(deployment.constant_sum, client.clone()),
ConstantSumSolver::new(deployment.constant_sum_solver, client),
)
}

async fn create_pool(
Expand Down
9 changes: 5 additions & 4 deletions kit/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct BaseParameters {

// Notes:
// All the other types will be specific to each pool/strategy type since those will be specific contracts
#[async_trait::async_trait]
pub trait PoolType: Sized + Clone + std::fmt::Debug + 'static {
// This trait provides the interface for people to construct pools from a `Configuration` state since all of this should be `Serialize` and `Deserialize`.
// This stuff ultimately will be what's used to deploy a `Pool<P: PoolType>` which will hold onto actual instances of contracts (whereas this just holds config data).
Expand Down Expand Up @@ -78,20 +79,20 @@ pub trait PoolType: Sized + Clone + std::fmt::Debug + 'static {
dfmm: DFMM<ArbiterMiddleware>,
) -> Result<Pool<Self>>;

#[allow(async_fn_in_trait)]
async fn swap_data(&self, pool_id: eU256, swap: InputToken, amount_in: eU256) -> Result<Bytes>;
/// Change Parameters
#[allow(async_fn_in_trait)]
async fn update_data(&self, new_data: Self::PoolParameters) -> Result<Bytes>;
/// Change Allocation Date
#[allow(async_fn_in_trait)]
async fn change_allocation_data(
&self,
pool_id: eU256,
allocation_data: Self::AllocationData,
) -> Result<Bytes>;

fn get_contracts(deployment: &DeploymentData, client: Arc<ArbiterMiddleware>) -> (Self::StrategyContract, Self::SolverContract);
fn get_contracts(
deployment: &DeploymentData,
client: Arc<ArbiterMiddleware>,
) -> (Self::StrategyContract, Self::SolverContract);
}

pub enum UpdateParameters<P: PoolType> {
Expand Down

0 comments on commit 4fa7519

Please sign in to comment.