Skip to content

Commit

Permalink
feat: swap and updatoor are vibing
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJepsen committed Apr 16, 2024
1 parent c8f72ed commit c877bae
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
27 changes: 18 additions & 9 deletions kit/src/behaviors/swap.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::{marker::PhantomData, pin::Pin};

use arbiter_core::events::stream_event;
use ethers::abi::Item;
use futures_util::{Stream, StreamExt};

use self::{
bindings::{dfmm::DFMM, erc20::ERC20},
creator::PoolCreation,
deployer::DeploymentData,
pool::{BaseConfig, InputToken, Pool},
updatoor::UpdatoorQuerry,
};
use super::*;
use crate::behaviors::token_admin::Response;
Expand All @@ -17,6 +17,7 @@ use crate::behaviors::token_admin::Response;
pub struct Swap<S: State, T: SwapType<E>, E> {
// to get tokens on start up
pub token_admin: String,
pub updatoor: String,
pub data: S::Data,
pub swap_type: T,
_phantom_e: PhantomData<E>,
Expand All @@ -30,7 +31,7 @@ pub struct SwapProcessing<P: PoolType> {
}

pub trait SwapType<E>: std::fmt::Debug + Serialize + Clone {
fn compute_swap_amount() -> (eU256, InputToken);
fn compute_swap_amount(event: E) -> (eU256, InputToken);
fn get_stream(&self) -> Pin<Box<dyn Stream<Item = E> + Send + Sync>>;
}

Expand Down Expand Up @@ -74,10 +75,10 @@ where
let (strategy_contract, solver_contract) =
P::get_contracts(&deployment_data, client.clone());
let dfmm = DFMM::new(deployment_data.dfmm, client.clone());
let mut init_event_steream = stream_event(dfmm.init_filter());
let mut init_event_stream = stream_event(dfmm.init_filter());

// Get the intended tokens for the pool and do approvals.
let mut tokens = Vec::new();
let mut tokens: Vec<ArbiterToken<ArbiterMiddleware>> = Vec::new();
for tkn in self.data.token_list.drain(..) {
messager
.send(
Expand Down Expand Up @@ -117,11 +118,20 @@ where
}

// Note: Would be nice to get one of these note both?
let init_event = init_event_steream.next().await.unwrap();
let init_event = init_event_stream.next().await.unwrap();
let pool_creation = messager.get_next::<PoolCreation<P>>().await?.data;
let lp_token = ERC20::new(init_event.lp_token, client.clone());
let instance = P::create_instance(strategy_contract, solver_contract, pool_creation.params);

// ask for first price udpate
messager
.send(
To::Agent(self.updatoor.to_owned()),
UpdatoorQuerry::UpdateMeDaddy,
)
.await?;

// build pool for processor and stream
let pool = Pool::<P> {
id: pool_creation.id,
dfmm,
Expand All @@ -132,6 +142,7 @@ where

let process = Self::Processor {
token_admin: self.token_admin.clone(),
updatoor: self.updatoor.clone(),
data: SwapProcessing {
messager,
client,
Expand All @@ -154,10 +165,8 @@ where
E: Send + Sync + 'static,
{
async fn process(&mut self, event: E) -> Result<ControlFlow> {
// todo, swap only on the right event trigger
let (swap_amount, input) = T::compute_swap_amount();
let swap = self.data.pool.swap(swap_amount, input).await?;

let (swap_amount, input) = T::compute_swap_amount(event);
self.data.pool.swap(swap_amount, input).await?;
Ok(ControlFlow::Continue)
}
}
23 changes: 14 additions & 9 deletions kit/src/behaviors/updatoor.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use std::marker::PhantomData;

use anyhow::Ok;
use arbiter_core::events::stream_event;
use bindings::dfmm::DFMM;
use ethers::contract::stream;
use futures_util::StreamExt;

use self::pool::BaseConfig;
use super::*;
use crate::{
behaviors::{creator::PoolCreation, deployer::DeploymentData, token_admin::Response},
behaviors::{creator::PoolCreation, deployer::DeploymentData},
bindings::erc20::ERC20,
pool::Pool,
};
Expand Down Expand Up @@ -75,7 +71,7 @@ where

// Get the intended tokens for the pool and do approvals.
let mut tokens: Vec<ArbiterToken<ArbiterMiddleware>> = Vec::new();
for tkn in self.data.token_list.drain(..) {
for _ in self.data.token_list.drain(..) {
let token = ArbiterToken::new(
messager.get_next::<eAddress>().await.unwrap().data,
client.clone(),
Expand Down Expand Up @@ -109,15 +105,19 @@ where
impl<P> Processor<Message> for Updatoor<ProcessingUpdates<P>>
where
P: PoolType + Send + Sync,
// E: std::fmt::Debug + Send + Sync + 'static,
{
async fn process(&mut self, event: Message) -> Result<ControlFlow> {
let msg: UpdatoorQuerry = serde_json::from_str(&event.data).unwrap_or(UpdatoorQuerry::NoOp);

match msg {
UpdatoorQuerry::Update => {
UpdatoorQuerry::UpdateMeDaddy => {
let params = self.data.pool_params.pop().unwrap();
self.data.pool.update(params).await?;
let _ = self
.data
.messager
.send(To::Agent(event.from), UpdatoorResponse::PriceUpdated)
.await?;
}

UpdatoorQuerry::NoOp => {
Expand All @@ -132,5 +132,10 @@ where
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum UpdatoorQuerry {
NoOp,
Update,
UpdateMeDaddy,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum UpdatoorResponse {
PriceUpdated,
}
3 changes: 1 addition & 2 deletions kit/src/pool/constant_sum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bindings::{constant_sum::ConstantSum, constant_sum_solver::ConstantSumSolver};

use self::bindings::constant_sum_solver::{self, ConstantSumParams};
use self::bindings::constant_sum_solver::ConstantSumParams;
use super::*;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -34,7 +34,6 @@ impl PoolType for ConstantSumPool {
}

async fn get_init_data(
base_config: &BaseConfig,
params: Self::Parameters,
allocation_data: &Self::AllocationData,
solver_contract: &Self::SolverContract,
Expand Down
2 changes: 0 additions & 2 deletions kit/src/pool/geometric_mean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use bindings::{
};
use ethers::types::Address;

use self::bindings::geometric_mean_solver;
use super::*;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -112,7 +111,6 @@ impl PoolType for GeometricMeanPool {
}

async fn get_init_data(
base_config: &BaseConfig,
params: Self::Parameters,
allocation_data: &Self::AllocationData,
solver_contract: &Self::SolverContract,
Expand Down
9 changes: 1 addition & 8 deletions kit/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ pub trait PoolType: Clone + std::fmt::Debug + 'static {
fn get_strategy_address(strategy_contract: &Self::StrategyContract) -> eAddress;

async fn get_init_data(
base_config: &BaseConfig,
params: Self::Parameters,
allocation_data: &Self::AllocationData,
solver_contract: &Self::SolverContract,
Expand Down Expand Up @@ -164,13 +163,7 @@ impl<P: PoolType> Pool<P> {
dfmm: DFMM<ArbiterMiddleware>,
tokens: Vec<ArbiterToken<ArbiterMiddleware>>,
) -> Result<Self> {
let data = P::get_init_data(
&base_config,
params.clone(),
&allocation_data,
&solver_contract,
)
.await?;
let data = P::get_init_data(params.clone(), &allocation_data, &solver_contract).await?;
debug!("Got init data {:?}", data);
let init_params = InitParams {
name: base_config.name,
Expand Down

0 comments on commit c877bae

Please sign in to comment.