Skip to content

Commit

Permalink
refactor / allocate builds
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Apr 17, 2024
1 parent 71707b2 commit 0be9b58
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 310 deletions.
261 changes: 75 additions & 186 deletions kit/src/behaviors/allocate.rs
Original file line number Diff line number Diff line change
@@ -1,194 +1,83 @@
// use std::marker::PhantomData;
use super::*;

// use arbiter_core::events::stream_event;
// use arbiter_engine::machine::{Configuration, ControlFlow, Processing, Processor, State};
trait AllocateType<E>: Debug + Serialize + Clone
where
E: Send + 'static,
{
// TODO: This should probably be how we do it, but this generic `P` gets
// annoying fn change_allocation_amount(&mut self, event: E) ->
// Option<P::AllocationData>;
fn change_allocation_amount(&mut self, event: E) -> Option<Vec<eI256>>;
fn get_stream(&self) -> Pin<Box<dyn Stream<Item = E> + Send + Sync>>;
}

// use self::{
// pool::PoolType,
// token_admin::{MintRequest, TokenAdminQuery},
// };
// use super::*;
#[derive(Clone, Debug, Serialize, Deserialize)]
struct Allocate<A, E, S>
where
A: AllocateType<E>,
E: Send + 'static,
S: State,
{
pub data: S::Data,
pub allocate_type: A,
_phantom_a: PhantomData<A>,
_phantom_e: PhantomData<E>,
}

// Notes:
// * The point of this function is to have the event piped to it from the
// behavior/processor and this just dictates how, based on some event, we want
// to change the allocation we have (or will have) in a pool.
// trait AllocationType<P, E>
// where
// P: PoolType,
// E: Send + 'static,
// {
// fn change_allocation_amount(&self, event: E) -> Option<P::AllocationData>;
// }
#[derive(Debug, Serialize, Deserialize)]
pub struct Config<P: PoolType> {
pub allocation_data: P::AllocationData,
}

// Notes:
// * The idea here is that the `ChangeAllocation` is generic over everything it
// needs to be generic over as far as how we'd want to interact with pools
// (i.e., `PoolType`).
// * It is also generic over the `AllocationType` so that we can implement the
// `Behavior`/`Processor` on this `ChangeAllocation` struct and have
// boilerplate here for how ANY `AllocationType` interacts with ANY
// `PoolType`.
// * In effect, a user ONLY has to implement the `AllocationType` trait
// themselves and decide how they want to do the allocations -- E.g.,
// `InitialAllocation` just chooses to allocate an amount to create a new
// pool. -- E.g., `RandomAllocation` would take in an event `E` like
// `BlockUpdate`, and based on this, randomly decides to allocate a random
// amount of liquidity.
// How this works (Type state pattern):
// By letting this be generic over `S: State`, we can have two different states
// that this struct exists in which helps us be able to create a version of this
// struct that can be read in from a config.toml (e.g., `S == Configuration`)
// while having a version of the struct that can do processing (e.g., `S ==
// Processing`). Having things this way makes it possible to have a
// parameterized `ChangeAllocation` that can then set itself up in the
// `Behavior::startup()` method so, for example, we don't have to have
// `Option<Arc<ArbiterMiddleware>>` or other fields that CANNOT BE
// DESERIALIZABLE!!! That's the trick.
// Example:
// I can read in from `config.toml` into `ChangeAllocation<A, P, E,
// Configuration>` since this should be Deserializable.
//
// Some more notes:
// #[derive(Debug, Serialize, Deserialize)]
// struct ChangeAllocation<A, P, E, S>
// where
// A: AllocationType<P, E>,
// P: PoolType,
// E: Send + 'static,
// S: State,
// {
// // APES LOL
// _phantom_a: PhantomData<A>,
// _phantom_p: PhantomData<P>,
// _phantom_e: PhantomData<E>,
// pub data: S::Data,
// }
pub struct Processing<P, E>
where
P: PoolType,
E: Send + 'static,
{
pub pool: Pool<P>,
pub client: Arc<ArbiterMiddleware>,
pub messager: Messager,
_phantom: PhantomData<E>,
}

// // This `ChangeAllocationStructData` will be the `D` in `S == Processing<D>`
// pub struct ChangeAllocationStructData<A, P, E>
// where
// A: AllocationType<P, E>,
// P: PoolType,
// E: Send + 'static,
// {
// pub client: Arc<ArbiterMiddleware>,
// pub pool: P,
// pub allocation_data: P::AllocationData,
// pub allocation_type: A,
// _phantom: PhantomData<E>,
// }
impl<P: PoolType> State for Config<P> {
type Data = Self;
}

// impl<A, P, E, S> AllocationType<P, E> for ChangeAllocation<A, P, E, S>
// where
// A: AllocationType<P, E>,
// P: PoolType,
// E: Send + 'static,
// S: State,
// {
// fn change_allocation_amount(&self, event: E) -> Option<P::AllocationData> {
// None
// }
// }
impl<P, E> State for Processing<P, E>
where
P: PoolType,
E: Send + 'static,
{
type Data = Self;
}

// #[derive(Debug)]
// pub struct InitialAllocation<P: PoolType> {
// /// The initial amount of token X.
// pub initial_x: eU256,
// /// The initial Price
// pub initial_price: eU256,
// /// Initial Parameters
// pub initial_parameters: P::AllocationData,
// /// The tokens to allocate
// pub tokens: (
// ArbiterToken<ArbiterMiddleware>,
// ArbiterToken<ArbiterMiddleware>,
// ),
// /// The tokens to request.
// pub token_data: TokenData,
// /// The agent ID to request tokens to.
// pub request_to: String,
// /// Client to have an address to receive token mint to and check balance
// // #[serde(skip)]
// pub client: Option<Arc<ArbiterMiddleware>>,
// /// The messaging layer for the token requester.
// // #[serde(skip)]
// pub messager: Option<Messager>,
// }
#[allow(unused_variables)]
#[async_trait::async_trait]
impl<A, P, E> Behavior<E> for Allocate<A, E, Config<P>>
where
A: AllocateType<E> + Debug + Send + Sync + 'static + for<'a> Deserialize<'a>,
P: PoolType + Debug + Send + Sync + 'static,
E: Debug + Send + Sync + 'static,
{
type Processor = Allocate<A, E, Processing<P, E>>;
async fn startup(
&mut self,
client: Arc<ArbiterMiddleware>,
messager: Messager,
) -> Result<Option<(Self::Processor, EventStream<E>)>> {
todo!();
}
}

// #[allow(unused_variables)]
// #[async_trait::async_trait]
// impl<A, P, E> Behavior<E> for ChangeAllocation<A, P, E, Configuration>
// where
// A: AllocationType<P, E> + std::fmt::Debug + Send + Sync + 'static,
// P: PoolType + std::fmt::Debug + Send + Sync + 'static,
// E: std::fmt::Debug + Send + Sync + 'static,
// {
// type Processor = ChangeAllocation<A, P, E, Processing<ChangeAllocationStructData<A, P, E>>>;
// async fn startup(
// &mut self,
// client: Arc<ArbiterMiddleware>,
// messager: Messager,
// ) -> Result<Option<(Self::Processor, EventStream<E>)>> {
// todo!();
// }
// }

// #[async_trait::async_trait]
// impl<A, P, E> Processor<E>
// for ChangeAllocation<A, P, E, Processing<ChangeAllocationStructData<A, P, E>>>
// where
// A: AllocationType<P, E>,
// P: PoolType,
// E: Send + 'static,
// {
// async fn process(&mut self, event: E) -> Result<ControlFlow> {
// Ok(ControlFlow::Halt)
// }
// }

// #[allow(unused_variables)]
// #[async_trait::async_trait]
// impl<P: PoolType, A: AllocationType<E>, E> Behavior<()> for
// ChangeAllocation<P, A, E> { type Processor = ();
// async fn startup(
// &mut self,
// client: Arc<ArbiterMiddleware>,
// messager: Messager,
// ) -> Result<Option<EventStream<()>>> {
// todo!();
// // PSEUDOCODE FOR HOW WE CAN WORK WITH THIS
// // // get the `event`
// // let event = ();
// // let amount = self.change_allocation_amount(event);

// // let allocation_data = self.pool.change_allocation_data(pool_id,
// amount); // self.pool.dfmm.update_allocation(allocation_data);

// // messager
// // .send(
// // To::Agent(self.request_to.clone()),
// // &TokenAdminQuery::AddressOf(self.token_data.name.clone()),
// // )
// // .await?;
// // let message = messager.get_next().await.unwrap();
// // let token_address =
// serde_json::from_str::<eAddress>(&message.data).unwrap(); // let
// token = ArbiterToken::new(token_address, client.clone()); //
// self.token_data.address = Some(token_address);

// // let mint_data = TokenAdminQuery::MintRequest(MintRequest {
// // token: self.token_data.name.clone(),
// // mint_to: client.address(),
// // mint_amount: 1,
// // });
// // messager
// // .send(To::Agent(self.request_to.clone()), mint_data)
// // .await?;

// // self.messager = Some(messager.clone());
// // self.client = Some(client.clone());
// // let transfer_stream = stream_event(token.transfer_filter());
// // Ok(Some(transfer_stream))
// }
// }
#[async_trait::async_trait]
impl<A, P, E> Processor<E> for Allocate<A, E, Processing<P, E>>
where
A: AllocateType<E> + Debug + Send + Sync + 'static,
P: PoolType + Debug + Send + Sync + 'static,
E: Debug + Send + Sync + 'static,
{
async fn process(&mut self, event: E) -> Result<ControlFlow> {
Ok(ControlFlow::Halt)
}
}
16 changes: 5 additions & 11 deletions kit/src/behaviors/creator.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
use bindings::dfmm::DFMM;

use self::pool::BaseConfig;
use super::*;
use crate::{
behaviors::{deployer::DeploymentData, token_admin::Response},
pool::Pool,
};
use crate::behaviors::{deploy::DeploymentData, token::Response};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Creator<S: State> {
pub struct Create<S: State> {
pub token_admin: String,
pub data: S::Data,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CreatorConfig<P: PoolType> {
pub struct Config<P: PoolType> {
pub base_config: BaseConfig,
pub params: P::Parameters,
pub allocation_data: P::AllocationData,
pub token_list: Vec<String>,
}

impl<P: PoolType> State for CreatorConfig<P> {
impl<P: PoolType> State for Config<P> {
type Data = Self;
}

#[async_trait::async_trait]
impl<P> Behavior<()> for Creator<CreatorConfig<P>>
impl<P> Behavior<()> for Create<Config<P>>
where
P: PoolType + Send + Sync + 'static,
P::StrategyContract: Send,
Expand Down
25 changes: 12 additions & 13 deletions kit/src/behaviors/deployer.rs → kit/src/behaviors/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
use arbiter_bindings::bindings::weth::WETH;
use bindings::{
constant_sum::ConstantSum, constant_sum_solver::ConstantSumSolver, dfmm::DFMM,
constant_sum::ConstantSum, constant_sum_solver::ConstantSumSolver,
geometric_mean::GeometricMean, geometric_mean_solver::GeometricMeanSolver,
log_normal::LogNormal, log_normal_solver::LogNormalSolver,
};
use ethers::types::Address;

use super::*;

#[derive(Debug, Deserialize, Serialize)]
pub struct Deployer {}
pub struct Deploy {}

#[derive(Debug, Deserialize, Serialize)]
pub struct DeploymentData {
pub weth: Address,
pub dfmm: Address,
pub geometric_mean: Address,
pub geometric_mean_solver: Address,
pub n_token_geometric_mean: Address,
pub log_normal: Address,
pub log_normal_solver: Address,
pub constant_sum: Address,
pub constant_sum_solver: Address,
pub weth: eAddress,
pub dfmm: eAddress,
pub geometric_mean: eAddress,
pub geometric_mean_solver: eAddress,
pub n_token_geometric_mean: eAddress,
pub log_normal: eAddress,
pub log_normal_solver: eAddress,
pub constant_sum: eAddress,
pub constant_sum_solver: eAddress,
}

#[async_trait::async_trait]
impl Behavior<()> for Deployer {
impl Behavior<()> for Deploy {
type Processor = ();
async fn startup(
&mut self,
Expand Down
24 changes: 13 additions & 11 deletions kit/src/behaviors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
use std::sync::Arc;
use std::{boxed::Box, marker::PhantomData, pin::Pin, sync::Arc};

use arbiter_core::events::stream_event;
use arbiter_engine::{
machine::{Behavior, ControlFlow, EventStream, Processor, State},
messager::{Message, Messager, To},
};
#[allow(unused)]
use arbiter_macros::Behaviors;
use bindings::arbiter_token::ArbiterToken;
pub use token_admin::{MintRequest, TokenAdminQuery};
use bindings::{arbiter_token::ArbiterToken, dfmm::DFMM};
use futures_util::{Stream, StreamExt};
pub use token::{MintRequest, TokenAdminQuery};

use self::{creator::Creator, deployer::Deployer, pool::PoolType, token_admin::TokenAdmin};
use self::{creator::Create, deploy::Deploy, pool::PoolType, token::TokenAdmin};
use super::*;

pub const MAX: eU256 = eU256::MAX;

// pub mod allocate;
pub mod allocate;
pub mod creator;
pub mod deployer;
pub mod deploy;
pub mod swap;
pub mod token_admin;
pub mod updatoor;
pub mod token;
pub mod update;

#[derive(Debug, Deserialize, Serialize)]
pub enum Behaviors<P: PoolType> {
Creator(Creator<creator::CreatorConfig<P>>),
Deployer(Deployer),
TokenAdmin(TokenAdmin<token_admin::Config>),
Create(Create<creator::Config<P>>),
Deployer(Deploy),
TokenAdmin(TokenAdmin<token::Config>),
}
Loading

0 comments on commit 0be9b58

Please sign in to comment.