Skip to content

Commit

Permalink
fix: trait redundances and serde
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Apr 10, 2024
1 parent 3345892 commit 0513bd4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 46 deletions.
12 changes: 6 additions & 6 deletions kit/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use dfmm_kit::behaviors::*;

#[arbiter_macros::main(
name = "DFMM Kit",
about = "Our entrypoint to working with the DFMM Kit.",
behaviors = Behaviors
)]
pub async fn main() {}
// #[arbiter_macros::main(
// name = "DFMM Kit",
// about = "Our entrypoint to working with the DFMM Kit.",
// behaviors = Behaviors
// )]
pub fn main() {}
52 changes: 26 additions & 26 deletions kit/src/behaviors/creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct PoolCreator<S: State> {
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PoolConfig<P: PoolConfigurer> {
pub struct PoolConfig<P: PoolType> {
pub params: P::PoolParameters,
pub initial_allocation_data: P::InitialAllocationData,
pub token_list: Vec<eAddress>,
Expand All @@ -25,29 +25,29 @@ pub struct PoolProcessor<P: PoolType> {
pub pool: Pool<P>,
}

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

// #[async_trait::async_trait]
// impl<P, E> Processor<E> for PoolCreator<Processing<PoolProcessor<P>>>
// where
// P: PoolType + Send + Sync + 'static,
// E: Send + Sync + 'static,
// {
// async fn process(&mut self, event: E) -> Result<ControlFlow> {
// todo!()
// }
// }
#[async_trait::async_trait]
impl<P, E> Processor<E> for PoolCreator<Processing<PoolProcessor<P>>>
where
P: PoolType + Send + Sync + 'static,
E: Send + Sync + 'static,
{
async fn process(&mut self, event: E) -> Result<ControlFlow> {
todo!()
}
}
4 changes: 2 additions & 2 deletions kit/src/behaviors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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::log_normal::LogNormalParams,
pool::{PoolConfigurer, PoolType}, //token_admin::TokenAdmin,allocate::InitialAllocation,
};
use super::*;
Expand All @@ -25,7 +25,7 @@ pub mod deployer;
pub mod creator;

#[derive(Debug, Deserialize, Serialize)]
pub enum Behaviors<PC: PoolConfigurer> {
pub enum Behaviors<PC: PoolType> {
Creator(PoolCreator<Configuration<PoolConfig<PC>>>),
Deployer(Deployer),
}
Expand Down
6 changes: 4 additions & 2 deletions kit/src/pool/constant_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bindings::{constant_sum::ConstantSum, constant_sum_solver::ConstantSumSolver

use super::*;

#[derive(Clone, Debug)]
pub struct ConstantSumPool {
pub strategy_contract: ConstantSum<ArbiterMiddleware>,
pub solver_contract: ConstantSumSolver<ArbiterMiddleware>,
Expand All @@ -23,7 +24,8 @@ pub enum ConstantSumAllocationData {
GivenY(eU256),
}
impl PoolType for ConstantSumPool {
type Parameters = ConstantSumParameters;
type PoolParameters = ConstantSumParameters;
type InitialAllocationData = Bytes;
type StrategyContract = ConstantSum<ArbiterMiddleware>;
type SolverContract = ConstantSumSolver<ArbiterMiddleware>;
type AllocationData = ConstantSumAllocationData;
Expand Down Expand Up @@ -55,7 +57,7 @@ impl PoolType for ConstantSumPool {
}
}

async fn update_data(&self, parameters: Self::Parameters) -> Result<Bytes> {
async fn update_data(&self, parameters: Self::PoolParameters) -> Result<Bytes> {
let price_update_data = self
.solver_contract
.prepare_price_update(parameters.price)
Expand Down
35 changes: 25 additions & 10 deletions kit/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ use super::*;
use crate::bindings::arbiter_token::ArbiterToken;

pub mod constant_sum;
pub mod geometric_mean;
pub mod log_normal;
pub mod n_token_geometric_mean;
// pub mod geometric_mean;
// pub mod log_normal;
// pub mod n_token_geometric_mean;

// Notes:
// `InitData` is something that all pools need in order to be created. This consists of:
// 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: PoolConfigurer> {
pub struct InitData<P: PoolType> {
pub params: P::PoolParameters,
pub initial_allocation_data: P::InitialAllocationData,
pub base_config: BaseParameters,
Expand All @@ -37,6 +37,7 @@ 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).
Expand All @@ -60,8 +61,22 @@ pub trait PoolConfigurer: Clone + std::fmt::Debug + 'static {
// 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 {
type Parameters: PoolConfigurer;
pub trait PoolType: Sized + Clone + std::fmt::Debug + 'static {
// type Parameters: PoolConfigurer;
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;
type StrategyContract;
type SolverContract;
type AllocationData: Send + Sync + 'static;
Expand All @@ -70,7 +85,7 @@ pub trait PoolType: Sized {
async fn create_pool(
&self,
dfmm: DFMM<ArbiterMiddleware>,
init_data: InitData<Self::Parameters>,
init_data: InitData<Self>,
) -> Result<Pool<Self>> {
todo!()
// TODO: There is a blanket implementation that we can do here.
Expand All @@ -91,7 +106,7 @@ pub trait PoolType: Sized {
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::Parameters) -> Result<Bytes>;
async fn update_data(&self, new_data: Self::PoolParameters) -> Result<Bytes>;
/// Change Allocation Date
#[allow(async_fn_in_trait)]
async fn change_allocation_data(
Expand All @@ -102,7 +117,7 @@ pub trait PoolType: Sized {
}

pub enum UpdateParameters<P: PoolType> {
PoolParameters(P::Parameters),
PoolParameters(P::PoolParameters),
Controller(eAddress),
Fee(eU256),
}
Expand Down Expand Up @@ -203,7 +218,7 @@ impl<P: PoolType> Pool<P> {
///
/// Returns `Ok(())` if the update is successful, otherwise returns an
/// error.
pub async fn update(&self, new_data: P::Parameters) -> Result<()> {
pub async fn update(&self, new_data: P::PoolParameters) -> Result<()> {
let data = self.instance.update_data(new_data).await?;
self.dfmm.update(self.id, data).send().await?.await?;
Ok(())
Expand Down

0 comments on commit 0513bd4

Please sign in to comment.