From 30dc4d0f9f6ff7fe50291ed0b1dd97b5d0317721 Mon Sep 17 00:00:00 2001 From: Colin Roberts Date: Wed, 24 Apr 2024 12:35:04 -0600 Subject: [PATCH] continuing refactor through swap --- Cargo.lock | 8 +-- kit/Cargo.toml | 8 +-- kit/src/behaviors/allocate/mod.rs | 2 +- kit/src/behaviors/creator.rs | 4 +- kit/src/behaviors/deploy.rs | 2 +- kit/src/behaviors/mod.rs | 5 ++ kit/src/behaviors/swap/mod.rs | 111 +++++++++++++++++------------- kit/src/behaviors/token.rs | 9 +-- kit/src/behaviors/update/mod.rs | 2 +- kit/src/pool/mod.rs | 1 + kit/tests/common.rs | 36 +++++----- kit/tests/creator_integration.rs | 5 +- kit/tests/update_integration.rs | 8 ++- 13 files changed, 113 insertions(+), 88 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a25c73d9..599d6696 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -185,7 +185,7 @@ checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" [[package]] name = "arbiter-bindings" version = "0.1.6" -source = "git+https://github.com/primitivefinance/arbiter.git?rev=360f4a7f#360f4a7f38667e761f8306ec140418eceab1bccd" +source = "git+https://github.com/primitivefinance/arbiter.git?rev=bd08b647#bd08b647d7d727f5631a5425402ba476f40a22bd" dependencies = [ "ethers", "serde", @@ -194,7 +194,7 @@ dependencies = [ [[package]] name = "arbiter-core" version = "0.10.3" -source = "git+https://github.com/primitivefinance/arbiter.git?rev=360f4a7f#360f4a7f38667e761f8306ec140418eceab1bccd" +source = "git+https://github.com/primitivefinance/arbiter.git?rev=bd08b647#bd08b647d7d727f5631a5425402ba476f40a22bd" dependencies = [ "arbiter-bindings", "async-stream", @@ -222,7 +222,7 @@ dependencies = [ [[package]] name = "arbiter-engine" version = "0.3.2" -source = "git+https://github.com/primitivefinance/arbiter.git?rev=360f4a7f#360f4a7f38667e761f8306ec140418eceab1bccd" +source = "git+https://github.com/primitivefinance/arbiter.git?rev=bd08b647#bd08b647d7d727f5631a5425402ba476f40a22bd" dependencies = [ "anyhow", "arbiter-bindings", @@ -246,7 +246,7 @@ dependencies = [ [[package]] name = "arbiter-macros" version = "0.1.3" -source = "git+https://github.com/primitivefinance/arbiter.git?rev=360f4a7f#360f4a7f38667e761f8306ec140418eceab1bccd" +source = "git+https://github.com/primitivefinance/arbiter.git?rev=bd08b647#bd08b647d7d727f5631a5425402ba476f40a22bd" dependencies = [ "quote", "syn 2.0.58", diff --git a/kit/Cargo.toml b/kit/Cargo.toml index 6dc656ed..6bb052d6 100644 --- a/kit/Cargo.toml +++ b/kit/Cargo.toml @@ -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 = "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" } +arbiter-core = { git = "https://github.com/primitivefinance/arbiter.git", rev = "bd08b647" } +arbiter-engine = { git = "https://github.com/primitivefinance/arbiter.git", rev = "bd08b647" } +arbiter-macros = { git = "https://github.com/primitivefinance/arbiter.git", rev = "bd08b647" } +arbiter-bindings = { git = "https://github.com/primitivefinance/arbiter.git", rev = "bd08b647" } # Ethereum ethers = "2.0.13" diff --git a/kit/src/behaviors/allocate/mod.rs b/kit/src/behaviors/allocate/mod.rs index c14b4c2c..0d305210 100644 --- a/kit/src/behaviors/allocate/mod.rs +++ b/kit/src/behaviors/allocate/mod.rs @@ -50,7 +50,7 @@ where { type Processor = Allocate>; async fn startup( - &mut self, + mut self, client: Arc, messager: Messager, ) -> Result { diff --git a/kit/src/behaviors/creator.rs b/kit/src/behaviors/creator.rs index 20c907f0..61f92b71 100644 --- a/kit/src/behaviors/creator.rs +++ b/kit/src/behaviors/creator.rs @@ -22,7 +22,7 @@ where { type Processor = (); async fn startup( - &mut self, + mut self, client: Arc, mut messager: Messager, ) -> Result { @@ -56,7 +56,7 @@ where TokenAdminQuery::MintRequest(MintRequest { token: tkn, mint_to: client.address(), - mint_amount: 100_000_000_000, + mint_amount: parse_ether(100)?, }), ) .await diff --git a/kit/src/behaviors/deploy.rs b/kit/src/behaviors/deploy.rs index cb4b0cd3..e410dbef 100644 --- a/kit/src/behaviors/deploy.rs +++ b/kit/src/behaviors/deploy.rs @@ -32,7 +32,7 @@ pub struct DeploymentData { impl Behavior<()> for Deploy { type Processor = (); async fn startup( - &mut self, + mut self, client: Arc, messager: Messager, ) -> Result { diff --git a/kit/src/behaviors/mod.rs b/kit/src/behaviors/mod.rs index 1594d2f1..d763c041 100644 --- a/kit/src/behaviors/mod.rs +++ b/kit/src/behaviors/mod.rs @@ -7,6 +7,7 @@ use arbiter_engine::{ #[allow(unused)] use arbiter_macros::{Behaviors, State}; use bindings::{arbiter_token::ArbiterToken, dfmm::DFMM}; +use ethers::utils::parse_ether; pub use token::{MintRequest, TokenAdminQuery}; use self::{ @@ -34,6 +35,10 @@ pub enum Behaviors { Swap(swap::Config

), } +pub trait Configurable Deserialize<'a>> { + fn configure(data: T) -> Self; +} + #[derive(Debug, Deserialize, Serialize)] #[serde(bound = "P: PoolType")] pub enum MessageTypes

diff --git a/kit/src/behaviors/swap/mod.rs b/kit/src/behaviors/swap/mod.rs index 3df32eb8..023f64b7 100644 --- a/kit/src/behaviors/swap/mod.rs +++ b/kit/src/behaviors/swap/mod.rs @@ -2,58 +2,79 @@ use self::{bindings::erc20::ERC20, pool::InputToken}; use super::*; use crate::behaviors::token::Response; -pub trait SwapType { +pub trait SwapType Deserialize<'a>>: Configurable { fn compute_swap_amount(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 +pub struct Swap where S: State, - T: SwapType, { - // to get tokens on start up - pub token_admin: String, - pub update: String, 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 { - phantom: PhantomData

, -} - -impl Default for Config

{ - fn default() -> Self { - Self { - phantom: PhantomData, - } - } +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, } #[derive(Debug, Clone, State)] -pub struct Processing { +pub struct Processing +where + P: PoolType, + T: SwapType, +{ pub messager: Messager, pub client: Arc, pub pool: Pool

, + pub swap_type: T, + _phantom: PhantomData, } #[async_trait::async_trait] -impl Behavior<()> for Swap, T, E> +impl Behavior for Swap> where - P: PoolType + Send, + P: PoolType + Send + Sync, T: SwapType + Send, - E: Send, + E: Send + 'static, { - // type Processor = Swap, T, E>; - type Processor = (); + type Processor = Swap>; async fn startup( - &mut self, + mut self, client: Arc, mut messager: Messager, ) -> Result { @@ -76,11 +97,11 @@ where let name = token.name().call().await?; messager .send( - To::Agent(self.token_admin.clone()), + To::Agent(self.data.token_admin.clone()), TokenAdminQuery::MintRequest(MintRequest { token: name, mint_to: client.address(), - mint_amount: 100_000_000_000, + mint_amount: parse_ether(100)?, }), ) .await @@ -101,37 +122,31 @@ where } // build pool for processor and stream - let _pool = Pool::

{ + let pool = Pool::

{ id: pool_creation.id, dfmm, instance: P::create_instance(strategy_contract, solver_contract, pool_creation.params), tokens, liquidity_token: ERC20::new(pool_creation.liquidity_token, client.clone()), }; - // 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::, - // }; - // Ok(Some((process, stream))) - // } - // None => Ok(None), - // } - Ok(()) + + let processor = Self::Processor { + token_admin: self.token_admin, + data: Processing { + messager, + client, + pool, + }, + swap_type: self.swap_type, + _phantom: PhantomData::, + }; + + Ok(processor) } } #[async_trait::async_trait] -impl Processor for Swap, T, E> +impl Processor for Swap> where P: PoolType + Send + Sync, T: SwapType + Send, diff --git a/kit/src/behaviors/token.rs b/kit/src/behaviors/token.rs index 390e9db9..ef3049d5 100644 --- a/kit/src/behaviors/token.rs +++ b/kit/src/behaviors/token.rs @@ -25,7 +25,7 @@ pub struct Processing { impl Behavior for TokenAdmin { type Processor = TokenAdmin; async fn startup( - &mut self, + mut self, client: Arc, messager: Messager, ) -> Result { @@ -120,10 +120,7 @@ impl TokenAdmin { async fn reply_mint_request(&self, mint_request: MintRequest, to: String) -> Result<()> { let token = &self.data.tokens.get(&mint_request.token).unwrap().1; token - .mint( - mint_request.mint_to, - parse_ether(mint_request.mint_amount).unwrap(), - ) + .mint(mint_request.mint_to, mint_request.mint_amount) .send() .await? .await?; @@ -163,7 +160,7 @@ pub struct MintRequest { pub mint_to: eAddress, /// The amount to mint. - pub mint_amount: u64, + pub mint_amount: eU256, } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] diff --git a/kit/src/behaviors/update/mod.rs b/kit/src/behaviors/update/mod.rs index 0f948bf1..e1744f57 100644 --- a/kit/src/behaviors/update/mod.rs +++ b/kit/src/behaviors/update/mod.rs @@ -34,7 +34,7 @@ where { type Processor = Update>; async fn startup( - &mut self, + mut self, client: Arc, mut messager: Messager, ) -> Result { diff --git a/kit/src/pool/mod.rs b/kit/src/pool/mod.rs index b7b5a303..d07fdc11 100644 --- a/kit/src/pool/mod.rs +++ b/kit/src/pool/mod.rs @@ -98,6 +98,7 @@ pub enum UpdateParameters { // Notes: // This is used in the `swap_data` function of the poolType trait to determine // which token to swap in. +#[derive(Deserialize, Serialize, Debug, Clone)] pub enum InputToken { TokenX, TokenY, diff --git a/kit/tests/common.rs b/kit/tests/common.rs index c87a30f1..1460f483 100644 --- a/kit/tests/common.rs +++ b/kit/tests/common.rs @@ -59,18 +59,6 @@ pub fn log(level: Level) { .unwrap(); } -pub fn spawn_constant_sum_swapper(world: &mut World) { - world.add_agent(Agent::builder(SWAPPER).with_behavior(mock_swap_behavior())) -} - -pub fn spawn_constant_sum_updater(world: &mut World) { - world.add_agent( - Agent::builder(UPDATER) - .with_behavior(mock_update_behavior()) - .with_behavior(mock_creator_behavior()), - ) -} - pub fn spawn_deployer(world: &mut World) { world.add_agent(Agent::builder(DEPLOYER).with_behavior(Deploy {})); } @@ -83,24 +71,36 @@ pub fn spawn_constant_sum_creator(world: &mut World) { world.add_agent(Agent::builder(CREATOR).with_behavior(mock_creator_behavior())); } -fn mock_swap_behavior() -> Swap, VanillaSwap, Message> { +pub fn spawn_constant_sum_swapper(world: &mut World) { + world.add_agent(Agent::builder(SWAPPER).with_behavior(mock_swap_behavior())) +} + +pub fn spawn_constant_sum_updater(world: &mut World) { + world.add_agent( + Agent::builder(UPDATER) + .with_behavior(mock_update_behavior()) + .with_behavior(mock_creator_behavior()), + ) +} + +fn mock_swap_behavior() -> Swap, SwapOne, Message> { let data = swap::Config::::default(); - Swap::, VanillaSwap, Message> { + Swap::, SwapOne, Message> { token_admin: TOKEN_ADMIN.to_owned(), update: UPDATER.to_owned(), data, - swap_type: VanillaSwap {}, + swap_type: SwapOne {}, _phantom: PhantomData, } } #[derive(Clone, Debug, Serialize, Deserialize)] -pub struct VanillaSwap {} +pub struct SwapOne {} -impl SwapType for VanillaSwap { +impl SwapType for SwapOne { fn compute_swap_amount(_event: Message) -> (eU256, dfmm_kit::pool::InputToken) { - (ethers::utils::parse_ether(0.5).unwrap(), InputToken::TokenY) + (ethers::utils::parse_ether(1).unwrap(), InputToken::TokenY) } } diff --git a/kit/tests/creator_integration.rs b/kit/tests/creator_integration.rs index bbe0b904..efffe350 100644 --- a/kit/tests/creator_integration.rs +++ b/kit/tests/creator_integration.rs @@ -1,4 +1,4 @@ -use std::time::Duration; +use std::{str::FromStr, time::Duration}; use tracing::info; include!("common.rs"); @@ -27,7 +27,8 @@ async fn run_creator_constant_sum() { let params = ConstantSumParams { price: WAD, swap_fee: ethers::utils::parse_ether(0.003).unwrap(), - controller: eAddress::zero(), + controller: eAddress::from_str("0x6965a885fde448e06b1cadd5bf15698c47cf4ab3") + .unwrap(), }; let allocation_data = ConstantSumAllocationData { reserve_x: RESERVE_X, diff --git a/kit/tests/update_integration.rs b/kit/tests/update_integration.rs index 3b753ef4..d7115689 100644 --- a/kit/tests/update_integration.rs +++ b/kit/tests/update_integration.rs @@ -6,6 +6,12 @@ use futures_util::StreamExt; use tracing::{info, warn}; include!("common.rs"); +// TODO: It is a bit odd in this test that when we see the `ConstantSumParams` +// we see that the controller is the 0 address. This is not actually correct +// though as it should be: "0x62007c61825f5052695f5de39baab5c23299876b". +// This is just due to the redundancy we have with the `ConstantSumParams` in +// and `BaseConfig` that we are currently using. + #[tokio::test(flavor = "multi_thread", worker_threads = 5)] async fn run_updater_constant_sum() { log(Level::DEBUG); @@ -42,7 +48,7 @@ async fn run_updater_constant_sum() { MessageTypes::Create(_) => continue, MessageTypes::TokenAdmin(_) => continue, MessageTypes::Update(params) => { - info!("successfully updated the params to {:?}", params); + info!("TEST SEES updated params: {:#?}", params); let mock_data = constant_sum_parameters_vec(); assert_eq!(params, mock_data[count]); if count >= 2 {