Skip to content

Commit

Permalink
feat: compiler happy
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJepsen committed Apr 24, 2024
1 parent 30dc4d0 commit 9a7d80f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 58 deletions.
60 changes: 18 additions & 42 deletions kit/src/behaviors/swap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,54 @@ use self::{bindings::erc20::ERC20, pool::InputToken};
use super::*;
use crate::behaviors::token::Response;

pub trait SwapType<E, T: for<'a> Deserialize<'a>>: Configurable<T> {
fn compute_swap_amount(event: E) -> (eU256, InputToken);
pub trait SwapType<E> {
fn compute_swap_amount(&self, event: E) -> (eU256, InputToken);
}

#[derive(Deserialize)]
pub struct SwapOnce {
pub amount: eU256,
pub input: InputToken,
}

impl Configurable<SwapOnce> for SwapOnce {
fn configure(data: SwapOnce) -> Self {
SwapOnce {
amount: data.amount,
input: data.input,
}
}
}

impl SwapType<Message, SwapOnce> for SwapOnce {
fn compute_swap_amount(_event: Message) -> (eU256, InputToken) {
(ethers::utils::parse_ether(1).unwrap(), InputToken::TokenY)
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Swap<S>
#[derive(Clone, Debug, Serialize, Deserialize, State)]
pub struct Swap<S, T: SwapType<E>, E>
where
S: State,
{
pub data: S::Data,
pub swap_type: T,
pub _phantom: PhantomData<E>,
}

// TODO: This needs to be configurable in some way to make the `SwapType` become
// transparent and useful.
// Should also get some data necessary for mint amounts and what not.
#[derive(Clone, Debug, Serialize, Deserialize, State)]
pub struct Config<P, T, E, C>
pub struct Config<P>
where
P: PoolType,
T: SwapType<E>,
C: Configurable<SwapType<E>>,
{
pub token_admin: String,
swap_type_configuration: C,
_phantom_p: PhantomData<P>,
_phantom_e: PhantomData<E>,
_phantom_t: PhantomData<T>,
pub _phantom: PhantomData<P>,
}

#[derive(Debug, Clone, State)]
pub struct Processing<P, T, E>
pub struct Processing<P>
where
P: PoolType,
T: SwapType<E>,
// T: SwapType<E>,
// E: Send + 'static,
{
pub messager: Messager,
pub client: Arc<ArbiterMiddleware>,
pub pool: Pool<P>,
pub swap_type: T,
_phantom: PhantomData<E>,
// pub swap_type: T,
// _phantom: PhantomData<E>,
}

#[async_trait::async_trait]
impl<P, T, E> Behavior<E> for Swap<Config<P, T, E>>
impl<P, T, E> Behavior<E> for Swap<Config<P>, T, E>
where
P: PoolType + Send + Sync,
T: SwapType<E> + Send,
E: Send + 'static,
{
type Processor = Swap<Processing<P, T, E>>;
type Processor = Swap<Processing<P>, T, E>;
async fn startup(
mut self,
client: Arc<ArbiterMiddleware>,
Expand Down Expand Up @@ -131,22 +108,21 @@ where
};

let processor = Self::Processor {
token_admin: self.token_admin,
data: Processing {
messager,
client,
pool,
},
swap_type: self.swap_type,
_phantom: PhantomData::<E>,
_phantom: PhantomData,
};

Ok(processor)
}
}

#[async_trait::async_trait]
impl<P, T, E> Processor<E> for Swap<Processing<P, T, E>>
impl<P, T, E> Processor<E> for Swap<Processing<P>, T, E>
where
P: PoolType + Send + Sync,
T: SwapType<E> + Send,
Expand All @@ -156,7 +132,7 @@ where
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);
let (swap_amount, input) = self.swap_type.compute_swap_amount(event);
self.data.pool.swap(swap_amount, input).await?;
Ok(ControlFlow::Continue)
}
Expand Down
3 changes: 0 additions & 3 deletions kit/src/behaviors/token.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::collections::HashMap;

use ethers::utils::parse_ether;

use super::*;

#[derive(Deserialize, Serialize, Clone, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion kit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pub use behaviors::token::TokenData;
use ethers::types::{Address as eAddress, I256 as eI256, U256 as eU256};
pub use pool::{BaseConfig, Pool, PoolType};
use serde::{Deserialize, Serialize};
use tracing::{debug, info, trace};
use tracing::{debug, info};
27 changes: 15 additions & 12 deletions kit/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,27 @@ pub fn spawn_constant_sum_updater(world: &mut World) {
)
}

fn mock_swap_behavior() -> Swap<swap::Config<ConstantSumPool>, SwapOne, Message> {
let data = swap::Config::<ConstantSumPool>::default();

Swap::<swap::Config<ConstantSumPool>, SwapOne, Message> {
fn mock_swap_behavior() -> Swap::<swap::Config::<ConstantSumPool>, SwapOnce, Message> {
let config = swap::Config::<ConstantSumPool> {
token_admin: TOKEN_ADMIN.to_owned(),
update: UPDATER.to_owned(),
data,
swap_type: SwapOne {},
_phantom: PhantomData,
};
Swap::<swap::Config::<ConstantSumPool>, SwapOnce, Message> {
data: config,
swap_type: SwapOnce { amount: ethers::utils::parse_ether(0.5).unwrap(), input: InputToken::TokenX },
_phantom: PhantomData,
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SwapOne {}
#[derive(Deserialize, Clone)]
pub struct SwapOnce {
pub amount: eU256,
pub input: InputToken,
}

impl SwapType<Message> for SwapOne {
fn compute_swap_amount(_event: Message) -> (eU256, dfmm_kit::pool::InputToken) {
(ethers::utils::parse_ether(1).unwrap(), InputToken::TokenY)
impl SwapType<Message> for SwapOnce {
fn compute_swap_amount(&self, _event: Message) -> (eU256, InputToken) {
(self.amount, self.input.clone())
}
}

Expand Down

0 comments on commit 9a7d80f

Please sign in to comment.