Skip to content

Commit

Permalink
clean up / constantsum
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Apr 10, 2024
1 parent 0513bd4 commit 3ceed49
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 73 deletions.
12 changes: 6 additions & 6 deletions kit/src/behaviors/creator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::pool::{Pool, PoolConfigurer, PoolType};
use crate::pool::{Pool, PoolType};
use arbiter_engine::machine::{Behavior, Configuration, Processing, Processor, State};

// Idea: Let's make a behavior that has two states:
Expand All @@ -17,7 +17,7 @@ pub struct PoolCreator<S: State> {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PoolConfig<P: PoolType> {
pub params: P::PoolParameters,
pub initial_allocation_data: P::InitialAllocationData,
pub initial_allocation_data: P::InitializationData,
pub token_list: Vec<eAddress>,
}

Expand All @@ -34,8 +34,8 @@ where
type Processor = PoolCreator<Processing<PoolProcessor<P>>>;
async fn startup(
&mut self,
client: Arc<ArbiterMiddleware>,
messager: Messager,
_client: Arc<ArbiterMiddleware>,
_messager: Messager,
) -> Result<Option<(Self::Processor, EventStream<E>)>> {
todo!()
}
Expand All @@ -47,7 +47,7 @@ where
P: PoolType + Send + Sync + 'static,
E: Send + Sync + 'static,
{
async fn process(&mut self, event: E) -> Result<ControlFlow> {
todo!()
async fn process(&mut self, _event: E) -> Result<ControlFlow> {
Ok(ControlFlow::Halt)
}
}
14 changes: 6 additions & 8 deletions kit/src/behaviors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ use std::sync::Arc;

use arbiter_bindings::bindings::arbiter_token::ArbiterToken;
use arbiter_engine::{
machine::{
Behavior, Configuration, ControlFlow, CreateStateMachine, Engine, EventStream, StateMachine,
},
messager::{Message, Messager, To},
machine::{Behavior, Configuration, ControlFlow, EventStream},
messager::{Messager, To},
};
use arbiter_macros::Behaviors;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde::{Deserialize, Serialize};

use self::{
creator::{PoolConfig, PoolCreator},
deployer::Deployer,
// bindings::idfmm::LogNormalParams, // TODO: We might want to just use these if we can.
// pool::log_normal::LogNormalParams,
pool::{PoolConfigurer, PoolType}, //token_admin::TokenAdmin,allocate::InitialAllocation,
pool::PoolType, //token_admin::TokenAdmin,allocate::InitialAllocation,
};
use super::*;

Expand All @@ -25,8 +23,8 @@ pub mod deployer;
pub mod creator;

#[derive(Debug, Deserialize, Serialize)]
pub enum Behaviors<PC: PoolType> {
Creator(PoolCreator<Configuration<PoolConfig<PC>>>),
pub enum Behaviors<P: PoolType> {
Creator(PoolCreator<Configuration<PoolConfig<P>>>),
Deployer(Deployer),
}

Expand Down
83 changes: 72 additions & 11 deletions kit/src/pool/constant_sum.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,96 @@
use bindings::{constant_sum::ConstantSum, constant_sum_solver::ConstantSumSolver};
use bindings::{
constant_sum::ConstantSum,
constant_sum_solver::{ConstantSumParams, ConstantSumSolver},
shared_types::InitParams,
};

use super::*;

#[derive(Clone, Debug)]
pub struct ConstantSumPool {
pub strategy_contract: ConstantSum<ArbiterMiddleware>,
pub solver_contract: ConstantSumSolver<ArbiterMiddleware>,
pub parameters: ConstantSumParameters,
pub parameters: ConstantSumParams,
}

// Configuration for the pool
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ConstantSumParameters {
pub price: eU256,
}

impl PoolConfigurer for ConstantSumParameters {
type PoolParameters = Self;
type InitialAllocationData = Bytes;
pub struct ConstantSumInitData {
pub name: String,
pub symbol: String,
pub reserve_x: eU256,
pub reserve_y: eU256,
pub token_x_name: String,
pub token_y_name: String,
pub params: ConstantSumParams,
}

pub enum ConstantSumAllocationData {
GivenX(eU256),
GivenY(eU256),
}

// pub struct Pool {
// pub strategy: ::ethers::core::types::Address,
// pub tokens: ::std::vec::Vec<::ethers::core::types::Address>,
// pub reserves: ::std::vec::Vec<::ethers::core::types::U256>,
// pub total_liquidity: ::ethers::core::types::U256,
// pub liquidity_token: ::ethers::core::types::Address,
// pub fee_collector: ::ethers::core::types::Address,
// pub controller_fee: ::ethers::core::types::U256,
// }

impl PoolType for ConstantSumPool {
type PoolParameters = ConstantSumParameters;
type InitialAllocationData = Bytes;
type PoolParameters = ConstantSumParams;
type InitializationData = ConstantSumInitData;
type StrategyContract = ConstantSum<ArbiterMiddleware>;
type SolverContract = ConstantSumSolver<ArbiterMiddleware>;
type AllocationData = ConstantSumAllocationData;

async fn create_pool(
&self,
init_data: Self::InitializationData,
token_list: Vec<ArbiterToken<ArbiterMiddleware>>,
strategy_contract: Self::StrategyContract,
solver_contract: Self::SolverContract,
dfmm: DFMM<ArbiterMiddleware>,
) -> Result<Pool<Self>> {
let init_bytes = solver_contract
.get_initial_pool_data(
init_data.reserve_x,
init_data.reserve_y,
init_data.params.clone(),
)
.call()
.await?;
let init_params = InitParams {
name: init_data.name,
symbol: init_data.symbol,
strategy: strategy_contract.address(),
tokens: token_list.iter().map(|tok| tok.address()).collect(),
data: init_bytes,
fee_collector: eAddress::zero(),
controller_fee: 0.into(),
};

let (id, _reserves, _total_liquidity) = dfmm.init(init_params.clone()).call().await?;
dfmm.init(init_params).send().await?;

let instance = ConstantSumPool {
strategy_contract,
solver_contract,
parameters: init_data.params,
};

Ok(Pool {
id,
dfmm,
instance,
token_x: token_list[0].clone(),
token_y: token_list[1].clone(),
})
}

async fn swap_data(
&self,
pool_id: eU256,
Expand Down
69 changes: 21 additions & 48 deletions kit/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

use arbiter_core::middleware::ArbiterMiddleware;
use ethers::types::Bytes;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde::{Deserialize, Serialize};

use self::bindings::dfmm::DFMM;
use super::*;
use crate::bindings::arbiter_token::ArbiterToken;
use crate::bindings::{arbiter_token::ArbiterToken, dfmm::DFMM};

pub mod constant_sum;
// pub mod geometric_mean;
Expand All @@ -21,12 +20,12 @@ pub mod constant_sum;
// 1. The parameters of the pool which, for example, are like the `mean` and `width` of the `LogNormal` pool. (Strategy specific since other pools might have different params like `ConstantSum` has `price`)
// 2. Initial allocation data, which consists of, for example, a `price` and an amount of `token_x` for the `LogNormal` pool. (Strategy specific since other pools like `ConstantSum` may not have the same needs)
// 3. Base configuration which ALL pools share as part of their parameterization which is the `swap_fee`, `controller` and the `controller_fee`. Every type of strategy needs these.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct InitData<P: PoolType> {
pub params: P::PoolParameters,
pub initial_allocation_data: P::InitialAllocationData,
pub base_config: BaseParameters,
}
// #[derive(Clone, Debug, Serialize, Deserialize)]
// pub struct InitData<P: PoolType> {
// pub params: P::PoolParameters,
// pub initial_allocation_data: P::InitialAllocationData,
// pub base_config: BaseParameters,
// }

// Notes:
// These are the things that all strategies need to have to be initialized (and potentially updated).
Expand All @@ -37,70 +36,44 @@ pub struct BaseParameters {
pub controller_fee: eU256,
}

// TODO: I don't think we need this anymore!
// Notes:
// 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).
pub trait PoolConfigurer: Clone + std::fmt::Debug + 'static {
type PoolParameters: Clone
+ std::fmt::Debug
+ Serialize
+ for<'de> Deserialize<'de>
+ Send
+ Sync
+ 'static;
type InitialAllocationData: Clone
+ std::fmt::Debug
+ Serialize
+ for<'de> Deserialize<'de>
+ Send
+ Sync
+ 'static;
}
// TODO: We could do something like this so we can have `create_pool` done generically
// pub trait StrategySolver {
// fn get_initial_pool_data ..
// }

// Notes:
// Everything from the above now gets collapsed (or inherited) as the associated `Parameters` type of the `PoolType`. E
// All the other types will be specific to each pool/strategy type since those will be specific contracts
pub trait PoolType: Sized + Clone + std::fmt::Debug + 'static {
// type Parameters: PoolConfigurer;
// 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).
type PoolParameters: Clone
+ std::fmt::Debug
+ Serialize
+ for<'de> Deserialize<'de>
+ Send
+ Sync
+ 'static;
type InitialAllocationData: Clone
type InitializationData: Clone
+ std::fmt::Debug
+ Serialize
+ for<'de> Deserialize<'de>
+ Send
+ Sync
+ 'static;
// ~~ These are the contracts that are used to interact with the pool. ~~
type StrategyContract;
type SolverContract;
type AllocationData: Send + Sync + 'static;

#[allow(async_fn_in_trait)]
async fn create_pool(
&self,
init_data: Self::InitializationData,
token_list: Vec<ArbiterToken<ArbiterMiddleware>>,
strategy_contract: Self::StrategyContract,
solver_contract: Self::SolverContract,
dfmm: DFMM<ArbiterMiddleware>,
init_data: InitData<Self>,
) -> Result<Pool<Self>> {
todo!()
// TODO: There is a blanket implementation that we can do here.
// we might be able to use the solver to encode this.
// THIS IS FROM THE BINDINGS FOR THE DFMM CONTRACT, WE NEED THE INPUT TO `dfmm.init(_)` to be of this type.
// pub struct InitParams {
// pub name: ::std::string::String,
// pub symbol: ::std::string::String,
// pub strategy: ::ethers::core::types::Address,
// pub tokens: ::std::vec::Vec<::ethers::core::types::Address>,
// pub data: ::ethers::core::types::Bytes,
// pub fee_collector: ::ethers::core::types::Address,
// pub controller_fee: ::ethers::core::types::U256,
// }
}
) -> Result<Pool<Self>>;

#[allow(async_fn_in_trait)]
async fn swap_data(&self, pool_id: eU256, swap: InputToken, amount_in: eU256) -> Result<Bytes>;
Expand Down

0 comments on commit 3ceed49

Please sign in to comment.