Skip to content

Commit

Permalink
save state
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Apr 12, 2024
1 parent ad4f82d commit b4035aa
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 136 deletions.
38 changes: 33 additions & 5 deletions kit/src/behaviors/creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -30,11 +33,27 @@ pub struct Creator<S: State> {

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config<P: PoolType> {
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<eAddress>,
}

impl<P: PoolType> PoolConfig for Config<P> {
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<P: PoolType> State for Config<P> {
type Data = Self;
}
Expand Down Expand Up @@ -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,
Expand Down
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::Creator,
deployer::Deployer,
pool::{
constant_sum::{ConstantSumInitData, ConstantSumPool},
constant_sum::{ConstantSumConfig, ConstantSumPool},
PoolType,
},
token_admin::TokenAdmin,
Expand Down Expand Up @@ -66,7 +66,7 @@ pub(crate) fn default_creator_config() -> Creator<creator::Config<ConstantSumPoo
swap_fee: 0.into(),
controller: eAddress::random(),
},
initial_allocation_data: ConstantSumInitData {
init_config: ConstantSumConfig {
name: "Test Pool".to_string(),
symbol: "TP".to_string(),
reserve_x: WAD,
Expand Down
2 changes: 1 addition & 1 deletion kit/src/behaviors/token_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ mod test {

use self::{
bindings::{constant_sum_solver::ConstantSumParams, usdc::USDC},
pool::constant_sum::{ConstantSumInitData, ConstantSumPool},
pool::constant_sum::{ConstantSumConfig, ConstantSumPool},
};
use super::*;
use crate::behaviors::behaviors::TokenAdmin;
Expand Down
85 changes: 20 additions & 65 deletions kit/src/pool/constant_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use bindings::{
constant_sum_solver::{ConstantSumParams, ConstantSumSolver},
shared_types::InitParams,
};
use ethers::etherscan::Client;
use tracing::{debug, info};

use self::behaviors::deployer::DeploymentData;
Expand All @@ -21,7 +20,7 @@ pub struct ConstantSumPool {

// Configuration for the pool
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ConstantSumInitData {
pub struct ConstantSumConfig {
pub name: String,
pub symbol: String,
pub reserve_x: eU256,
Expand All @@ -36,21 +35,10 @@ pub enum ConstantSumAllocationData {
GivenY(eU256),
}

// TODO: It's worth thinking about what this is since we have our own "Pool"
// struct 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,
// }

#[async_trait::async_trait]
impl PoolType for ConstantSumPool {
type InitConfig = ConstantSumConfig;
type PoolParameters = ConstantSumParams;
type InitializationData = ConstantSumInitData;
type StrategyContract = ConstantSum<ArbiterMiddleware>;
type SolverContract = ConstantSumSolver<ArbiterMiddleware>;
type AllocationData = ConstantSumAllocationData;
Expand All @@ -65,68 +53,35 @@ impl PoolType for ConstantSumPool {
)
}

async fn init_data(&self, init_data: Self::InitializationData) -> Result<Bytes> {
let init_bytes = self
.solver_contract
async fn get_init_bytes(
init_config: Self::InitConfig,
solver_contract: Self::SolverContract,
) -> Result<Bytes> {
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<ArbiterToken<ArbiterMiddleware>>,
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<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?;
debug!("Got init bytes {}", init_bytes);

let tokens: Vec<eAddress> = 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(
Expand Down
112 changes: 49 additions & 63 deletions kit/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -55,46 +58,35 @@ 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<P: PoolType>` which will hold onto actual instances of contracts
// (whereas this just holds config data).
type InitConfig: PoolConfig;
type PoolParameters: Clone
+ std::fmt::Debug
+ Serialize
+ for<'de> Deserialize<'de>
+ 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<ArbiterToken<ArbiterMiddleware>>,
strategy_contract: Self::StrategyContract,
solver_contract: Self::SolverContract,
dfmm: DFMM<ArbiterMiddleware>,
) -> Result<Pool<Self>>;

async fn init_data(&self, init_data: Self::InitializationData) -> Result<Bytes>;

async fn swap_data(&self, pool_id: eU256, swap: InputToken, amount_in: eU256) -> Result<Bytes>;
/// Change Parameters
async fn update_data(&self, new_data: Self::PoolParameters) -> Result<Bytes>;
Expand All @@ -109,6 +101,19 @@ pub trait PoolType: Sized + Clone + std::fmt::Debug + 'static {
deployment: &DeploymentData,
client: Arc<ArbiterMiddleware>,
) -> (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<Bytes>;

fn create_instance(
strategy_contract: Self::StrategyContract,
solver_contract: Self::SolverContract,
parameters: Self::PoolParameters,
) -> Self;
}

pub enum UpdateParameters<P: PoolType> {
Expand Down Expand Up @@ -137,51 +142,32 @@ pub struct Pool<P: PoolType> {
pub id: eU256,
pub dfmm: DFMM<ArbiterMiddleware>,
pub instance: P,
pub token_x: ArbiterToken<ArbiterMiddleware>,
pub token_y: ArbiterToken<ArbiterMiddleware>,
pub tokens: Vec<ArbiterToken<ArbiterMiddleware>>,
pub liquidity_token: ERC20<ArbiterMiddleware>,
}

impl<P: PoolType> Pool<P> {
// TODO: Finish this
// async fn create_pool(
// init_data: P::InitializationData,
// token_list: Vec<ArbiterToken<ArbiterMiddleware>>,
// strategy_contract: P::StrategyContract,
// solver_contract: P::SolverContract,
// dfmm: DFMM<ArbiterMiddleware>,
// instance: P,
// ) -> Result<Pool<P>> {
// // maybe we make a trait bound for the solver contract
// let init_bytes = solver_contract.init_data;

// let tokens: Vec<eAddress> = 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<ArbiterMiddleware>,
tokens: Vec<ArbiterToken<ArbiterMiddleware>>,
) -> Result<Self> {
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
Expand Down

0 comments on commit b4035aa

Please sign in to comment.