Skip to content

Commit

Permalink
feat: Pool struct and impls
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Feb 21, 2024
1 parent 2bbfdf9 commit 7b243fe
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ optimized-out

# Rust
kit/target
target
target

# VSCode
.vscode
2 changes: 1 addition & 1 deletion kit/src/behaviors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::Arc;

use anyhow::Result;
use arbiter_engine::{
machine::{Behavior, CreateStateMachine, Engine, EventStream, StateMachine},
messager::Messager,
Expand All @@ -9,6 +8,7 @@ use arbiter_macros::Behaviors;
use serde::{Deserialize, Serialize};

use self::deployer::Deployer;
use super::*;

pub mod deployer;

Expand Down
4 changes: 4 additions & 0 deletions kit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
pub mod behaviors;
pub mod bindings;
pub mod pool;

use anyhow::Result;
use ethers::types::U256 as eU256;
30 changes: 30 additions & 0 deletions kit/src/pool/constant_sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use bindings::{constant_sum::ConstantSum, constant_sum_solver::ConstantSumSolver};
use ethers::abi::Tokenize;

use super::*;

pub struct ConstantSumPool {
pub strategy_contract: ConstantSum<ArbiterMiddleware>,
pub solver_contract: ConstantSumSolver<ArbiterMiddleware>,
pub parameters: ConstantSumParameters,
}

pub struct ConstantSumParameters {
pub price: eU256,
pub swap_fee: eU256,
}

impl PoolType for ConstantSumPool {
type Parameters = ConstantSumParameters;
type StrategyContract = ConstantSum<ArbiterMiddleware>;
type SolverContract = ConstantSumSolver<ArbiterMiddleware>;

async fn swap_data(&self, pool_id: eU256, swap_x_in: bool, amount_in: eU256) -> Result<Bytes> {
let data = self
.solver_contract
.simulate_swap(pool_id, swap_x_in, amount_in)
.call()
.await?;
Ok(Bytes::from(ethers::abi::encode(&data.into_tokens())))
}
}
1 change: 1 addition & 0 deletions kit/src/pool/geometric_mean.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions kit/src/pool/log_normal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

43 changes: 43 additions & 0 deletions kit/src/pool/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use arbiter_core::middleware::ArbiterMiddleware;
use ethers::types::Bytes;

use self::bindings::dfmm::DFMM;
use super::*;
use crate::bindings::arbiter_token::ArbiterToken;

pub mod constant_sum;
pub mod geometric_mean;
pub mod log_normal;

pub trait PoolType {
type Parameters;
type StrategyContract;
type SolverContract;

#[allow(async_fn_in_trait)]
async fn swap_data(&self, pool_id: eU256, swap_x_in: bool, amount_in: eU256) -> Result<Bytes>;
}

pub struct Pool<P: PoolType> {
pub id: eU256,
pub dfmm: DFMM<ArbiterMiddleware>,
pub instance: P,
pub token_x: ArbiterToken<ArbiterMiddleware>,
pub token_y: ArbiterToken<ArbiterMiddleware>,
}

impl<P: PoolType> Pool<P> {
pub async fn swap(
&self,
amount_in: eU256,
token_in: &ArbiterToken<ArbiterMiddleware>,
) -> Result<()> {
let swap_x_in = token_in.address() == self.token_x.address();
let data = self
.instance
.swap_data(self.id, swap_x_in, amount_in)
.await?;
self.dfmm.swap(self.id, data).send().await?.await?;
Ok(())
}
}

0 comments on commit 7b243fe

Please sign in to comment.