From b4035aa340579412ceaeabe990c94d435b535787 Mon Sep 17 00:00:00 2001 From: Colin Roberts Date: Fri, 12 Apr 2024 13:22:00 -0700 Subject: [PATCH] save state --- kit/src/behaviors/creator.rs | 38 +++++++++-- kit/src/behaviors/mod.rs | 4 +- kit/src/behaviors/token_admin.rs | 2 +- kit/src/pool/constant_sum.rs | 85 ++++++----------------- kit/src/pool/mod.rs | 112 ++++++++++++++----------------- 5 files changed, 105 insertions(+), 136 deletions(-) diff --git a/kit/src/behaviors/creator.rs b/kit/src/behaviors/creator.rs index 621a112c..a1b9446e 100644 --- a/kit/src/behaviors/creator.rs +++ b/kit/src/behaviors/creator.rs @@ -3,13 +3,16 @@ use bindings::dfmm::DFMM; use tracing::debug; +use self::pool::PoolConfig; + use super::*; use crate::{ behaviors::{ deployer::DeploymentData, token_admin::{Response, TokenAdminQuery}, }, - pool::PoolType, + bindings::idfmm::InitParams, + pool::{Pool, PoolType}, }; // Idea: Let's make a behavior that has two states: @@ -30,11 +33,27 @@ pub struct Creator { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Config { + pub name: String, + pub symbol: String, pub params: P::PoolParameters, - pub initial_allocation_data: P::InitializationData, + pub init_config: P::InitConfig, pub token_list: Vec, } +impl PoolConfig for Config

{ + fn get_init_params(&self) -> InitParams { + InitParams { + name: self.name.clone(), + symbol: self.symbol.clone(), + strategy: todo!(), + tokens: self.token_list.clone(), + data: todo!(), + fee_collector: todo!(), + controller_fee: todo!(), + } + } +} + impl State for Config

{ type Data = Self; } @@ -108,9 +127,18 @@ where .await? .await?; - let init_data = self.data.initial_allocation_data.clone(); - let pool = P::create_pool( - init_data, + let init_params = InitParams { + name: self.data.init_config.name, + symbol: todo!(), + strategy: todo!(), + tokens: todo!(), + data: todo!(), + fee_collector: todo!(), + controller_fee: todo!(), + }; + + let pool = Pool::new( + self.data.init_config, vec![token_x, token_y], strategy_contract, solver_contract, diff --git a/kit/src/behaviors/mod.rs b/kit/src/behaviors/mod.rs index 750944cf..7636a117 100644 --- a/kit/src/behaviors/mod.rs +++ b/kit/src/behaviors/mod.rs @@ -14,7 +14,7 @@ use self::{ creator::Creator, deployer::Deployer, pool::{ - constant_sum::{ConstantSumInitData, ConstantSumPool}, + constant_sum::{ConstantSumConfig, ConstantSumPool}, PoolType, }, token_admin::TokenAdmin, @@ -66,7 +66,7 @@ pub(crate) fn default_creator_config() -> Creator, -// 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, -// } - #[async_trait::async_trait] impl PoolType for ConstantSumPool { + type InitConfig = ConstantSumConfig; type PoolParameters = ConstantSumParams; - type InitializationData = ConstantSumInitData; type StrategyContract = ConstantSum; type SolverContract = ConstantSumSolver; type AllocationData = ConstantSumAllocationData; @@ -65,68 +53,35 @@ impl PoolType for ConstantSumPool { ) } - async fn init_data(&self, init_data: Self::InitializationData) -> Result { - let init_bytes = self - .solver_contract + async fn get_init_bytes( + init_config: Self::InitConfig, + solver_contract: Self::SolverContract, + ) -> Result { + let init_bytes = solver_contract .get_initial_pool_data( - init_data.reserve_x, - init_data.reserve_y, - init_data.params.clone(), + init_config.reserve_x, + init_config.reserve_y, + init_config.params.clone(), ) .call() .await?; Ok(init_bytes) } - async fn create_pool( - init_data: Self::InitializationData, - token_list: Vec>, + fn get_strategy_address(strategy_contract: Self::StrategyContract) -> eAddress { + strategy_contract.address() + } + + fn create_instance( strategy_contract: Self::StrategyContract, solver_contract: Self::SolverContract, - dfmm: DFMM, - ) -> Result> { - let init_bytes = solver_contract - .get_initial_pool_data( - init_data.reserve_x, - init_data.reserve_y, - init_data.params.clone(), - ) - .call() - .await?; - debug!("Got init bytes {}", init_bytes); - - let tokens: Vec = token_list.iter().map(|tok| tok.address()).collect(); - assert!(tokens.len() == 2); - assert!(tokens[0] != tokens[1]); - let init_params = InitParams { - name: init_data.name, - symbol: init_data.symbol, - strategy: strategy_contract.address(), - tokens, - data: init_bytes, - fee_collector: eAddress::zero(), - controller_fee: eU256::zero(), - }; - // let (id, _reserves, _total_liquidity) = - // dfmm.init(init_params.clone()).send().await?; - let thing = dfmm.init(init_params.clone()).send().await?.await?.unwrap(); - let thing1 = thing.status.unwrap(); - debug!("tx succeeded with status {}", thing1); - // debug!("got pool id {}", id); - - let instance = ConstantSumPool { + parameters: Self::PoolParameters, + ) -> Self { + Self { strategy_contract, solver_contract, - parameters: init_data.params, - }; - - Ok(Pool { - id: eU256::one(), - dfmm, - instance, - token_x: token_list[0].clone(), - token_y: token_list[1].clone(), - }) + parameters, + } } async fn swap_data( diff --git a/kit/src/pool/mod.rs b/kit/src/pool/mod.rs index c2309fae..810db160 100644 --- a/kit/src/pool/mod.rs +++ b/kit/src/pool/mod.rs @@ -8,11 +8,14 @@ use std::sync::Arc; use arbiter_core::middleware::ArbiterMiddleware; -use ethers::types::Bytes; +use ethers::{core::abi::AbiType, types::Bytes}; use serde::{Deserialize, Serialize}; use tracing::debug; -use self::behaviors::deployer::DeploymentData; +use self::{ + behaviors::deployer::DeploymentData, + bindings::{erc20::ERC20, shared_types}, +}; use super::*; use crate::bindings::{arbiter_token::ArbiterToken, dfmm::DFMM, shared_types::InitParams}; @@ -55,16 +58,23 @@ pub struct BaseParameters { // fn get_initial_pool_data .. // } +pub trait PoolConfig: + Clone + std::fmt::Debug + Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static +{ + fn get_init_params(&self) -> InitParams; +} + // 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 { +pub trait PoolType: 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` which will hold onto actual instances of contracts // (whereas this just holds config data). + type InitConfig: PoolConfig; type PoolParameters: Clone + std::fmt::Debug + Serialize @@ -72,29 +82,11 @@ pub trait PoolType: Sized + Clone + std::fmt::Debug + 'static { + Send + Sync + 'static; - 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( - init_data: Self::InitializationData, - token_list: Vec>, - strategy_contract: Self::StrategyContract, - solver_contract: Self::SolverContract, - dfmm: DFMM, - ) -> Result>; - - async fn init_data(&self, init_data: Self::InitializationData) -> Result; - async fn swap_data(&self, pool_id: eU256, swap: InputToken, amount_in: eU256) -> Result; /// Change Parameters async fn update_data(&self, new_data: Self::PoolParameters) -> Result; @@ -109,6 +101,19 @@ pub trait PoolType: Sized + Clone + std::fmt::Debug + 'static { deployment: &DeploymentData, client: Arc, ) -> (Self::StrategyContract, Self::SolverContract); + + fn get_strategy_address(strategy_contract: Self::StrategyContract) -> eAddress; + + async fn get_init_bytes( + init_config: Self::InitConfig, + solver_contract: Self::SolverContract, + ) -> Result; + + fn create_instance( + strategy_contract: Self::StrategyContract, + solver_contract: Self::SolverContract, + parameters: Self::PoolParameters, + ) -> Self; } pub enum UpdateParameters { @@ -137,51 +142,32 @@ pub struct Pool { pub id: eU256, pub dfmm: DFMM, pub instance: P, - pub token_x: ArbiterToken, - pub token_y: ArbiterToken, + pub tokens: Vec>, + pub liquidity_token: ERC20, } impl Pool

{ - // TODO: Finish this - // async fn create_pool( - // init_data: P::InitializationData, - // token_list: Vec>, - // strategy_contract: P::StrategyContract, - // solver_contract: P::SolverContract, - // dfmm: DFMM, - // instance: P, - // ) -> Result> { - // // maybe we make a trait bound for the solver contract - // let init_bytes = solver_contract.init_data; - - // let tokens: Vec = token_list.iter().map(|tok| - // tok.address()).collect(); assert!(tokens.len() == 2, "Token list must - // contain exactly two distinct tokens."); assert!(tokens[0] != - // tokens[1], "Token list contains duplicate tokens."); - - // // maybe we pass in name and symbol? - // let init_params = InitParams { - // name: init_data.name, - // symbol: init_data.symbol, - // strategy: strategy_contract.address(), - // tokens, - // data: init_bytes, - // fee_collector: eAddress::zero(), - // controller_fee: eU256::zero(), - // }; - - // let thing = dfmm.init(init_params.clone()).send().await?.await?.unwrap(); - // let thing1 = thing.status.unwrap(); - // debug!("tx succeeded with status {}", thing1); - - // Ok(Pool { - // id: eU256::one(), - // dfmm, - // instance, - // token_x: token_list[0].clone(), - // token_y: token_list[1].clone(), - // }) - // } + pub async fn new( + init_params: InitParams, + parameters: P::PoolParameters, + strategy_contract: P::StrategyContract, + solver_contract: P::SolverContract, + dfmm: DFMM, + tokens: Vec>, + ) -> Result { + let (id, _reserves, _total_liquidty) = dfmm.init(init_params.clone()).call().await?; + dfmm.init(init_params).send().await?.await?; + let pool: shared_types::Pool = dfmm.pools(id).call().await?; + let instance = P::create_instance(strategy_contract, solver_contract, parameters); + let client = dfmm.client(); + Ok(Self { + id, + dfmm, + instance, + tokens, + liquidity_token: ERC20::new(pool.liquidity_token, client), + }) + } /// Performs a swap on the pool. /// /// # Arguments