diff --git a/kit/src/behaviors/swap/mod.rs b/kit/src/behaviors/swap/mod.rs index 023f64b7..0421d391 100644 --- a/kit/src/behaviors/swap/mod.rs +++ b/kit/src/behaviors/swap/mod.rs @@ -2,77 +2,54 @@ use self::{bindings::erc20::ERC20, pool::InputToken}; use super::*; use crate::behaviors::token::Response; -pub trait SwapType Deserialize<'a>>: Configurable { - fn compute_swap_amount(event: E) -> (eU256, InputToken); +pub trait SwapType { + fn compute_swap_amount(&self, event: E) -> (eU256, InputToken); } -#[derive(Deserialize)] -pub struct SwapOnce { - pub amount: eU256, - pub input: InputToken, -} - -impl Configurable for SwapOnce { - fn configure(data: SwapOnce) -> Self { - SwapOnce { - amount: data.amount, - input: data.input, - } - } -} - -impl SwapType 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 +#[derive(Clone, Debug, Serialize, Deserialize, State)] +pub struct Swap, E> where S: State, { pub data: S::Data, + pub swap_type: T, + pub _phantom: PhantomData, } // 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 +pub struct Config

where P: PoolType, - T: SwapType, - C: Configurable>, { pub token_admin: String, - swap_type_configuration: C, - _phantom_p: PhantomData

, - _phantom_e: PhantomData, - _phantom_t: PhantomData, + pub _phantom: PhantomData

, } #[derive(Debug, Clone, State)] -pub struct Processing +pub struct Processing

where P: PoolType, - T: SwapType, + // T: SwapType, + // E: Send + 'static, { pub messager: Messager, pub client: Arc, pub pool: Pool

, - pub swap_type: T, - _phantom: PhantomData, + // pub swap_type: T, + // _phantom: PhantomData, } #[async_trait::async_trait] -impl Behavior for Swap> +impl Behavior for Swap, T, E> where P: PoolType + Send + Sync, T: SwapType + Send, E: Send + 'static, { - type Processor = Swap>; + type Processor = Swap, T, E>; async fn startup( mut self, client: Arc, @@ -131,14 +108,13 @@ where }; let processor = Self::Processor { - token_admin: self.token_admin, data: Processing { messager, client, pool, }, swap_type: self.swap_type, - _phantom: PhantomData::, + _phantom: PhantomData, }; Ok(processor) @@ -146,7 +122,7 @@ where } #[async_trait::async_trait] -impl Processor for Swap> +impl Processor for Swap, T, E> where P: PoolType + Send + Sync, T: SwapType + Send, @@ -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 { - 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) } diff --git a/kit/src/behaviors/token.rs b/kit/src/behaviors/token.rs index ef3049d5..a35a00c2 100644 --- a/kit/src/behaviors/token.rs +++ b/kit/src/behaviors/token.rs @@ -1,7 +1,4 @@ use std::collections::HashMap; - -use ethers::utils::parse_ether; - use super::*; #[derive(Deserialize, Serialize, Clone, Debug)] diff --git a/kit/src/lib.rs b/kit/src/lib.rs index 9f327754..08611bf3 100644 --- a/kit/src/lib.rs +++ b/kit/src/lib.rs @@ -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}; diff --git a/kit/tests/common.rs b/kit/tests/common.rs index 1460f483..a6897cfb 100644 --- a/kit/tests/common.rs +++ b/kit/tests/common.rs @@ -83,24 +83,27 @@ pub fn spawn_constant_sum_updater(world: &mut World) { ) } -fn mock_swap_behavior() -> Swap, SwapOne, Message> { - let data = swap::Config::::default(); - - Swap::, SwapOne, Message> { +fn mock_swap_behavior() -> Swap::, SwapOnce, Message> { + let config = swap::Config:: { token_admin: TOKEN_ADMIN.to_owned(), - update: UPDATER.to_owned(), - data, - swap_type: SwapOne {}, + _phantom: PhantomData, + }; + Swap::, 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 for SwapOne { - fn compute_swap_amount(_event: Message) -> (eU256, dfmm_kit::pool::InputToken) { - (ethers::utils::parse_ether(1).unwrap(), InputToken::TokenY) +impl SwapType for SwapOnce { + fn compute_swap_amount(&self, _event: Message) -> (eU256, InputToken) { + (self.amount, self.input.clone()) } }