Skip to content

Commit

Permalink
refactor current behaviors
Browse files Browse the repository at this point in the history
Some tests are now failing. Will work on these in the next commit.
  • Loading branch information
Autoparallel committed Apr 23, 2024
1 parent b7e3c47 commit d9cf8a4
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 182 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions kit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ keywords = ["ethereum", "smart-contracts", "automated market makers"]
readme = "../README.md"

[dependencies]
arbiter-core = { git = "https://github.com/primitivefinance/arbiter.git", rev = "aff29d30" }
arbiter-engine = { git = "https://github.com/primitivefinance/arbiter.git", rev = "aff29d30" }
arbiter-macros = { git = "https://github.com/primitivefinance/arbiter.git", rev = "aff29d30" }
arbiter-bindings = { git = "https://github.com/primitivefinance/arbiter.git", rev = "aff29d30" }
arbiter-core = { git = "https://github.com/primitivefinance/arbiter.git", rev = "360f4a7f" }
arbiter-engine = { git = "https://github.com/primitivefinance/arbiter.git", rev = "360f4a7f" }
arbiter-macros = { git = "https://github.com/primitivefinance/arbiter.git", rev = "360f4a7f" }
arbiter-bindings = { git = "https://github.com/primitivefinance/arbiter.git", rev = "360f4a7f" }

# Ethereum
ethers = "2.0.13"
Expand Down
21 changes: 6 additions & 15 deletions kit/src/behaviors/allocate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ where
// 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>>;
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand All @@ -24,11 +23,12 @@ where
_phantom_e: PhantomData<E>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, State)]
pub struct Config<P: PoolType> {
pub allocation_data: P::AllocationData,
}

#[derive(State)]
pub struct Processing<P, E>
where
P: PoolType,
Expand All @@ -40,18 +40,6 @@ where
_phantom: PhantomData<E>,
}

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

impl<P, E> State for Processing<P, E>
where
P: PoolType,
E: Send + 'static,
{
type Data = Self;
}

#[allow(unused_variables)]
#[async_trait::async_trait]
impl<A, P, E> Behavior<E> for Allocate<A, E, Config<P>>
Expand All @@ -65,7 +53,7 @@ where
&mut self,
client: Arc<ArbiterMiddleware>,
messager: Messager,
) -> Result<Option<(Self::Processor, EventStream<E>)>> {
) -> Result<Self::Processor> {
todo!();
}
}
Expand All @@ -77,6 +65,9 @@ where
P: PoolType + Debug + Send + Sync + 'static,
E: Debug + Send + Sync + 'static,
{
async fn get_stream(&mut self) -> Result<Option<EventStream<E>>> {
todo!("We have not implemented the 'get_stream' method yet for the 'Allocate' behavior.");
}
async fn process(&mut self, _event: E) -> Result<ControlFlow> {
Ok(ControlFlow::Halt)
}
Expand Down
10 changes: 3 additions & 7 deletions kit/src/behaviors/creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ pub struct Create<S: State> {
pub data: S::Data,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, State)]
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 Config<P> {
type Data = Self;
}

#[async_trait::async_trait]
impl<P> Behavior<()> for Create<Config<P>>
where
Expand All @@ -31,7 +27,7 @@ where
&mut self,
client: Arc<ArbiterMiddleware>,
mut messager: Messager,
) -> Result<Option<(Self::Processor, EventStream<()>)>> {
) -> Result<Self::Processor> {
// Receive the `DeploymentData` from the `Deployer` agent and use it to get the
// contracts.
debug!("Starting the creator");
Expand Down Expand Up @@ -112,7 +108,7 @@ where
self.data.allocation_data.clone(),
);
messager.send(To::All, pool_creation).await.unwrap();
Ok(None)
Ok(())
}
}

Expand Down
30 changes: 2 additions & 28 deletions kit/src/behaviors/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,58 +35,34 @@ impl Behavior<()> for Deploy {
&mut self,
client: Arc<ArbiterMiddleware>,
messager: Messager,
) -> Result<Option<(Self::Processor, EventStream<()>)>> {
) -> Result<Self::Processor> {
let weth = WETH::deploy(client.clone(), ())?.send().await?;
trace!("WETH deployed at {:?}", weth.address());

let dfmm = DFMM::deploy(client.clone(), weth.address())?.send().await?;
trace!("DFMM deployed at {:?}", dfmm.address());

let geometric_mean = GeometricMean::deploy(client.clone(), dfmm.address())?
.send()
.await?;
trace!("GeometricMean deployed at {:?}", geometric_mean.address());

let geometric_mean_solver = GeometricMeanSolver::deploy(client.clone(), dfmm.address())?
.send()
.await?;
trace!(
"GeometricMeanSolver deployed at {:?}",
geometric_mean.address()
);

let log_normal = LogNormal::deploy(client.clone(), dfmm.address())?
.send()
.await?;
trace!("LogNormal deployed at {:?}", log_normal.address());

let log_normal_solver = LogNormalSolver::deploy(client.clone(), dfmm.address())?
.send()
.await?;
trace!(
"LogNormalSolver deployed at {:?}",
log_normal_solver.address()
);

let constant_sum = ConstantSum::deploy(client.clone(), dfmm.address())?
.send()
.await?;
trace!("ConstantSum deployed at {:?}", constant_sum.address());

let constant_sum_solver = ConstantSumSolver::deploy(client.clone(), dfmm.address())?
.send()
.await?;
trace!("ConstantSumSolver deployed at {:?}", constant_sum.address());

let n_token_geometric_mean = NTokenGeometricMean::deploy(client.clone(), dfmm.address())?
.send()
.await?;

let n_token_geometric_mean_solver =
NTokenGeometricMeanSolver::deploy(client.clone(), dfmm.address())?
.send()
.await?;

let deployment_data = DeploymentData {
weth: weth.address(),
dfmm: dfmm.address(),
Expand All @@ -99,10 +75,8 @@ impl Behavior<()> for Deploy {
constant_sum: constant_sum.address(),
constant_sum_solver: constant_sum_solver.address(),
};

debug!("Deployments completed: {:#?}", deployment_data);

messager.send(To::All, deployment_data).await?;
Ok(None)
Ok(())
}
}
5 changes: 2 additions & 3 deletions kit/src/behaviors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::{boxed::Box, marker::PhantomData, pin::Pin, sync::Arc};
use std::{boxed::Box, marker::PhantomData, sync::Arc};

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

use self::{
Expand Down
70 changes: 28 additions & 42 deletions kit/src/behaviors/swap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ use crate::behaviors::token::Response;

pub trait SwapType<E>: Debug + Serialize + Clone {
fn compute_swap_amount(event: E) -> (eU256, InputToken);
// TODO: Put this on the processor in arbiter engine so that startups just
// return a proccess
fn get_stream(
&self,
_messager: Messager,
) -> Option<Pin<Box<dyn Stream<Item = E> + Send + Sync>>> {
None
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand All @@ -24,32 +16,21 @@ pub struct Swap<S: State, T: SwapType<E>, E> {
pub _phantom: PhantomData<E>,
}

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

#[derive(Debug, Clone)]
#[derive(Debug, Clone, State)]
pub struct Processing<P: PoolType> {
pub messager: Messager,
pub client: Arc<ArbiterMiddleware>,
pub pool: Pool<P>,
}

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

impl<P> State for Processing<P>
where
P: PoolType,
{
type Data = Self;
}

#[derive(Debug)]
struct SwapTodo<P: PoolType> {
deployment_data: Option<DeploymentData>,
Expand All @@ -64,20 +45,21 @@ struct SwapTodo<P: PoolType> {
}

#[async_trait::async_trait]
impl<P, T, E> Behavior<E> for Swap<Config<P>, T, E>
impl<P, T, E> Behavior<()> for Swap<Config<P>, T, E>
where
P: PoolType + Send + Sync + 'static,
P::StrategyContract: Send,
P::SolverContract: Send,
T: SwapType<E> + Send + Sync + 'static + for<'a> Deserialize<'a>,
E: Debug + Send + Sync + 'static,
{
type Processor = Swap<Processing<P>, T, E>;
// type Processor = Swap<Processing<P>, T, E>;
type Processor = ();
async fn startup(
&mut self,
client: Arc<ArbiterMiddleware>,
mut messager: Messager,
) -> Result<Option<(Self::Processor, EventStream<E>)>> {
) -> Result<Self::Processor> {
// Make a "TODO" list.
// This is the data I need to recieve to do my job
let mut todo: SwapTodo<P> = SwapTodo {
Expand Down Expand Up @@ -176,24 +158,25 @@ where
tokens,
liquidity_token: lp_token,
};

match self.swap_type.get_stream(messager.clone()) {
Some(stream) => {
let process = Self::Processor {
token_admin: self.token_admin.clone(),
update: self.update.clone(),
data: Processing {
messager,
client,
pool,
},
swap_type: self.swap_type.clone(),
_phantom: PhantomData::<E>,
};
Ok(Some((process, stream)))
}
None => Ok(None),
}
// TODO: We need to come back around and adjust this.
// match self.swap_type.get_stream(messager.clone()) {
// Some(stream) => {
// let process = Self::Processor {
// token_admin: self.token_admin.clone(),
// update: self.update.clone(),
// data: Processing {
// messager,
// client,
// pool,
// },
// swap_type: self.swap_type.clone(),
// _phantom: PhantomData::<E>,
// };
// Ok(Some((process, stream)))
// }
// None => Ok(None),
// }
Ok(())
}
}

Expand All @@ -204,6 +187,9 @@ where
T: SwapType<E> + Send + Sync + 'static,
E: Send + Sync + 'static,
{
async fn get_stream(&mut self) -> Result<Option<EventStream<E>>> {
todo!("We have not implemented the 'get_stream' method yet for the 'Swap' behavior.")
}
async fn process(&mut self, event: E) -> Result<ControlFlow> {
let (swap_amount, input) = T::compute_swap_amount(event);
self.data.pool.swap(swap_amount, input).await?;
Expand Down
Loading

0 comments on commit d9cf8a4

Please sign in to comment.