Skip to content

Commit

Permalink
feat: main runs again
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Apr 26, 2024
1 parent 5f4fb86 commit 147cafa
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 166 deletions.
353 changes: 237 additions & 116 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions kit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ arbiter-macros = { git = "https://github.com/primitivefinance/arbiter.git", rev
arbiter-bindings = { git = "https://github.com/primitivefinance/arbiter.git", rev = "d5bc1bb3" }

# Ethereum
ethers = "2.0.13"
ethers = "2.0.14"

# Async
tokio = { version = "1.0", features = ["full"] }
async-trait = "0.1.77"
tokio = { version = "1.37.0", features = ["full"] }
async-trait = "0.1.80"
futures-util = "0.3.30"

# Serde
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
serde = { version = "1.0.198", features = ["derive"] }
serde_json = "1.0.116"

# CLI
clap = { version = "4.5.4", features = ["derive"] }

# Errors and tracing
anyhow = "1.0.81"
anyhow = "1.0.82"
tracing = "0.1.40"

[dev-dependencies]
tracing-subscriber = "0.3.18"

[[bin]]
Expand Down
13 changes: 8 additions & 5 deletions kit/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#[allow(unused)]
use dfmm_kit::behaviors::*;

// #[arbiter_macros::main(
// name = "DFMM Kit",
// about = "Our entrypoint to working with the DFMM Kit.",
// behaviors = Behaviors
// )]
// To run this, you can do `cargo run --bin kit` to access the binary in debug
// mode. You can also `cargo install --path kit --bin kit --force` to install
// the binary and run it with `kit`. This will be the release profile.
#[arbiter_macros::main(
name = "DFMM Kit",
about = "Our entrypoint to working with the DFMM Kit.",
behaviors = Behaviors
)]
pub fn main() {}
24 changes: 18 additions & 6 deletions kit/src/behaviors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::{boxed::Box, marker::PhantomData, sync::Arc};

use arbiter_engine::{
machine::{Behavior, ControlFlow, EventStream, Processor, State},
machine::{
Behavior, ControlFlow, CreateStateMachine, Engine, EventStream, Processor, State,
StateMachine,
},
messager::{Message, Messager, To},
};
#[allow(unused)]
Expand All @@ -13,7 +16,8 @@ pub use token::{MintRequest, Response, TokenAdminQuery};
use self::{
create::Create,
deploy::{Deploy, DeploymentData},
pool::{PoolCreation, PoolType},
pool::{constant_sum::ConstantSumPool, PoolCreation, PoolType},
swap::Swap,
token::TokenAdmin,
};
use super::*;
Expand All @@ -27,12 +31,20 @@ pub mod swap;
pub mod token;
pub mod update;

#[derive(Debug, Deserialize, Serialize)]
pub enum Behaviors<P: PoolType> {
Create(Create<create::Config<P>>),
// TODO: This should never have generics in it (at least, at the moment until
// some crazy magic happens in `arbiter_engine`). The reason why is that
// `World::from_config<C>` will not work with this. It looks for a type
// parameter, and if you have generics it will need them to be fixed in place.
// However, this isn't so bad. Extending this enum is super simple and it does
// not break anything on top of it really (unlike having a `PoolType` as an
// enum). This is definitely not the most elegant thing though and I think this
// could be made much better with some TLC.
#[derive(Behaviors, Debug, Deserialize, Serialize)]
pub enum Behaviors {
Deployer(Deploy),
TokenAdmin(TokenAdmin<token::Config>),
Swap(swap::Config<P>),
CreateConstantSum(Create<create::Config<ConstantSumPool>>),
SwapConstantSum(Swap<swap::Config<ConstantSumPool>, swap::on_command::OnCommand, Message>),
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down
2 changes: 2 additions & 0 deletions kit/src/behaviors/swap/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::*;
use crate::pool::InputToken;

pub mod on_command;

// TODO: This could depend on the `PoolType` as the `AllocateType` does.
pub trait SwapType<E> {
fn compute_swap_amount(&self, event: E) -> Option<(eU256, InputToken)>;
Expand Down
23 changes: 23 additions & 0 deletions kit/src/behaviors/swap/on_command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::*;

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

impl SwapType<Message> for OnCommand {
fn compute_swap_amount(&self, event: Message) -> Option<(eU256, InputToken)> {
debug!("Inside compute swap amount for `SwapOnCommand`");
match serde_json::from_str::<ExecuteSwap>(&event.data) {
Ok(_) => {
debug!("Successfully deserialized message data for `SwapOnCommand`");
Some((self.amount, self.input))
}
Err(_) => None,
}
}
}

#[derive(Serialize, Deserialize, Clone)]
pub struct ExecuteSwap;
28 changes: 3 additions & 25 deletions kit/tests/agents/mock_agents/swap_agent.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,13 @@
use super::*;

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

impl SwapType<Message> for SwapOnCommand {
fn compute_swap_amount(&self, event: Message) -> Option<(eU256, InputToken)> {
debug!("Inside compute swap amount for `SwapOnCommand`");
match serde_json::from_str::<ExecuteSwap>(&event.data) {
Ok(_) => {
debug!("Successfully deserialized message data for `SwapOnCommand`");
Some((self.amount, self.input))
}
Err(_) => None,
}
}
}

#[derive(Serialize, Deserialize, Clone)]
pub struct ExecuteSwap;

pub fn mock_swap_behavior() -> Swap<swap::Config<ConstantSumPool>, SwapOnCommand, Message> {
pub fn mock_swap_behavior() -> Swap<swap::Config<ConstantSumPool>, OnCommand, Message> {
let config = swap::Config::<ConstantSumPool> {
token_admin: TOKEN_ADMIN.to_owned(),
_phantom: PhantomData,
};
Swap::<swap::Config<ConstantSumPool>, SwapOnCommand, Message> {
Swap::<swap::Config<ConstantSumPool>, OnCommand, Message> {
data: config,
swap_type: SwapOnCommand {
swap_type: OnCommand {
amount: parse_ether(0.5).unwrap(),
input: InputToken::TokenX,
},
Expand Down
7 changes: 5 additions & 2 deletions kit/tests/agents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use dfmm_kit::{
behaviors::{
create::{self, Create},
deploy::Deploy,
swap::{self, Swap, SwapType},
swap::{
self,
on_command::{ExecuteSwap, OnCommand},
Swap,
},
token::{self, TokenAdmin},
update::{self, Update},
},
Expand All @@ -21,7 +25,6 @@ use ethers::{
utils::parse_ether,
};
use futures_util::StreamExt;
use serde::{Deserialize, Serialize};
use tracing::{debug, info, Level};
use tracing_subscriber::FmtSubscriber;

Expand Down
5 changes: 1 addition & 4 deletions kit/tests/agents/swap_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ async fn run_swapper_constant_sum() {

// TODO: Send a specific message and see if we get the swap.
messager
.send(
To::Agent(SWAPPER.to_owned()),
mock_agents::swap_agent::ExecuteSwap,
)
.send(To::Agent(SWAPPER.to_owned()), ExecuteSwap)
.await
.unwrap();
debug!("message sent to swapper");
Expand Down

0 comments on commit 147cafa

Please sign in to comment.