Skip to content

Commit

Permalink
continuing refactor through swap
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Apr 24, 2024
1 parent c4c9245 commit 30dc4d0
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 88 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 = "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"
Expand Down
2 changes: 1 addition & 1 deletion kit/src/behaviors/allocate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where
{
type Processor = Allocate<A, E, Processing<P, E>>;
async fn startup(
&mut self,
mut self,
client: Arc<ArbiterMiddleware>,
messager: Messager,
) -> Result<Self::Processor> {
Expand Down
4 changes: 2 additions & 2 deletions kit/src/behaviors/creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
{
type Processor = ();
async fn startup(
&mut self,
mut self,
client: Arc<ArbiterMiddleware>,
mut messager: Messager,
) -> Result<Self::Processor> {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion kit/src/behaviors/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct DeploymentData {
impl Behavior<()> for Deploy {
type Processor = ();
async fn startup(
&mut self,
mut self,
client: Arc<ArbiterMiddleware>,
messager: Messager,
) -> Result<Self::Processor> {
Expand Down
5 changes: 5 additions & 0 deletions kit/src/behaviors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -34,6 +35,10 @@ pub enum Behaviors<P: PoolType> {
Swap(swap::Config<P>),
}

pub trait Configurable<T: for<'a> Deserialize<'a>> {
fn configure(data: T) -> Self;
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(bound = "P: PoolType")]
pub enum MessageTypes<P>
Expand Down
111 changes: 63 additions & 48 deletions kit/src/behaviors/swap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,79 @@ use self::{bindings::erc20::ERC20, pool::InputToken};
use super::*;
use crate::behaviors::token::Response;

pub trait SwapType<E> {
pub trait SwapType<E, T: for<'a> Deserialize<'a>>: Configurable<T> {
fn compute_swap_amount(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, T, E>
pub struct Swap<S>
where
S: State,
T: SwapType<E>,
{
// to get tokens on start up
pub token_admin: String,
pub update: String,
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: PoolType> {
phantom: PhantomData<P>,
}

impl<P: PoolType> Default for Config<P> {
fn default() -> Self {
Self {
phantom: PhantomData,
}
}
pub struct Config<P, T, E, C>
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>,
}

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

#[async_trait::async_trait]
impl<P, T, E> Behavior<()> for Swap<Config<P>, T, E>
impl<P, T, E> Behavior<E> for Swap<Config<P, T, E>>
where
P: PoolType + Send,
P: PoolType + Send + Sync,
T: SwapType<E> + Send,
E: Send,
E: Send + 'static,
{
// type Processor = Swap<Processing<P>, T, E>;
type Processor = ();
type Processor = Swap<Processing<P, T, E>>;
async fn startup(
&mut self,
mut self,
client: Arc<ArbiterMiddleware>,
mut messager: Messager,
) -> Result<Self::Processor> {
Expand All @@ -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
Expand All @@ -101,37 +122,31 @@ where
}

// build pool for processor and stream
let _pool = Pool::<P> {
let pool = Pool::<P> {
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::<E>,
// };
// 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::<E>,
};

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 Down
9 changes: 3 additions & 6 deletions kit/src/behaviors/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Processing {
impl Behavior<Message> for TokenAdmin<Config> {
type Processor = TokenAdmin<Processing>;
async fn startup(
&mut self,
mut self,
client: Arc<ArbiterMiddleware>,
messager: Messager,
) -> Result<Self::Processor> {
Expand Down Expand Up @@ -120,10 +120,7 @@ impl TokenAdmin<Processing> {
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?;
Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion kit/src/behaviors/update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where
{
type Processor = Update<Processing<P>>;
async fn startup(
&mut self,
mut self,
client: Arc<ArbiterMiddleware>,
mut messager: Messager,
) -> Result<Self::Processor> {
Expand Down
1 change: 1 addition & 0 deletions kit/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub enum UpdateParameters<P: PoolType> {
// 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,
Expand Down
36 changes: 18 additions & 18 deletions kit/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}));
}
Expand All @@ -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<swap::Config<ConstantSumPool>, 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<swap::Config<ConstantSumPool>, SwapOne, Message> {
let data = swap::Config::<ConstantSumPool>::default();

Swap::<swap::Config<ConstantSumPool>, VanillaSwap, Message> {
Swap::<swap::Config<ConstantSumPool>, 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<Message> for VanillaSwap {
impl SwapType<Message> 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)
}
}

Expand Down
Loading

0 comments on commit 30dc4d0

Please sign in to comment.