Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Kit Behaviors and Tests #130

Merged
merged 73 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
f0b5ebc
feat: LogNormalSolver
0xJepsen Apr 2, 2024
a19b93c
wip: save constant sum
0xJepsen Apr 2, 2024
34249eb
feat: constant sum
0xJepsen Apr 2, 2024
3d98aa4
Add `PairSolver` abstract contract to avoid custom allo/deallo delta …
kinrezC Apr 2, 2024
2a8ceec
Merge branch 'fix/spearbit-audit' into solvers
clemlak Apr 3, 2024
46cbea6
chore: natspec
0xJepsen Apr 3, 2024
793697c
Merge branch 'main' into solvers
0xJepsen Apr 3, 2024
16abc1f
chore: bindings
0xJepsen Apr 3, 2024
0f84472
feat: constant sum pool type
0xJepsen Apr 3, 2024
d6f5df7
feat: geometric mean pool type
0xJepsen Apr 3, 2024
d61cb51
feat: log normal pool type
0xJepsen Apr 3, 2024
e36f731
feat: n token geometric mean
0xJepsen Apr 3, 2024
2710bbc
feat: n token geometric mean
0xJepsen Apr 3, 2024
0db5c15
fmt
0xJepsen Apr 3, 2024
d47157f
return abi encoded allo/deallo data
kinrezC Apr 4, 2024
0056bad
feat: n-token bindings + pool types + tests
0xJepsen Apr 4, 2024
c002d1e
feat: n-token pooltype
0xJepsen Apr 4, 2024
b8c246f
feat: deploy n token geometric mean
0xJepsen Apr 4, 2024
9d051f2
feat: generic allocate and deallocate and update
0xJepsen Apr 5, 2024
6692071
feat: token admin
0xJepsen Apr 8, 2024
02a1117
feat: mint request
0xJepsen Apr 8, 2024
60e5e87
wip: allocation
0xJepsen Apr 8, 2024
89e0afd
wip: mint on startup
0xJepsen Apr 8, 2024
4305fbb
feat: super generic <A, P, E> type state Processor
0xJepsen Apr 9, 2024
9b74e54
wip: Save
0xJepsen Apr 10, 2024
3345892
fix: `Behaviors` enum with generic
Autoparallel Apr 10, 2024
0513bd4
fix: trait redundances and serde
Autoparallel Apr 10, 2024
3ceed49
clean up / constantsum
Autoparallel Apr 10, 2024
9675e36
a todo
Autoparallel Apr 10, 2024
764761f
wip: behavior
0xJepsen Apr 10, 2024
4fa7519
test runs but we hit a revert
Autoparallel Apr 10, 2024
62a3c5a
feat: token admin
0xJepsen Apr 11, 2024
858c207
debug: ruled out token aprovals and mints
0xJepsen Apr 11, 2024
3b076ca
chore: notes on create pool
0xJepsen Apr 11, 2024
1b23302
wip: debug minting
0xJepsen Apr 12, 2024
ad4f82d
clean up
Autoparallel Apr 12, 2024
b4035aa
save state
Autoparallel Apr 12, 2024
6cb4290
chore: some cleaning 🧼
0xJepsen Apr 12, 2024
37a5edb
test passes again
Autoparallel Apr 12, 2024
f3889ef
refactor: tests
Autoparallel Apr 12, 2024
5ca2a53
refactor: creator
Autoparallel Apr 12, 2024
499c7d6
clean up more
Autoparallel Apr 13, 2024
d854b02
chore: clippy is happy
0xJepsen Apr 15, 2024
d790215
feat: swap behavior startup
0xJepsen Apr 15, 2024
70dfdf0
test: geometric mean pool creator test
0xJepsen Apr 15, 2024
b5dbb2c
wip: swap trait and parameter clean up
0xJepsen Apr 16, 2024
c8f72ed
feat: updator
0xJepsen Apr 16, 2024
c877bae
feat: swap and updatoor are vibing
0xJepsen Apr 16, 2024
71707b2
creator integration test debuging
0xJepsen Apr 17, 2024
0be9b58
refactor / allocate builds
Autoparallel Apr 17, 2024
37e9086
chore: test compile with new names
0xJepsen Apr 17, 2024
014fed8
chore: deployer tests
0xJepsen Apr 17, 2024
45a5bfb
wip: save, going on walk
0xJepsen Apr 17, 2024
aaebac4
fix token test
Autoparallel Apr 17, 2024
bf2e9ff
chore: update test
0xJepsen Apr 17, 2024
20a1aea
chore: update test
0xJepsen Apr 17, 2024
0888561
wip: isolate bug
0xJepsen Apr 17, 2024
67ef8b7
Update update_integration.rs
0xJepsen Apr 17, 2024
74a6d5f
chore: message type match
0xJepsen Apr 18, 2024
d64e845
fix the loop
Autoparallel Apr 18, 2024
30af3d7
annoying stupid serde
Autoparallel Apr 18, 2024
074af3b
chore: onto great things (contract reverts)
0xJepsen Apr 18, 2024
eb8c24c
wip: save
0xJepsen Apr 18, 2024
313ae77
fixed updater startup
Autoparallel Apr 19, 2024
2a50abb
chore: Updater tests passing
0xJepsen Apr 19, 2024
ad00a7f
clean
0xJepsen Apr 19, 2024
75efd72
chore: Clean up + Starting swap integration tests
0xJepsen Apr 22, 2024
0305627
chore: Clean up + Starting swap integration tests
0xJepsen Apr 22, 2024
5eba09e
test: swap integration
0xJepsen Apr 22, 2024
8046db1
Update mod.rs
0xJepsen Apr 22, 2024
af348cf
chore: clippy
0xJepsen Apr 23, 2024
a3cae0c
Merge branch 'main' of github.com:primitivefinance/DFMM into allocate…
kinrezC Apr 23, 2024
e192da1
Merge branch 'allocate_behavior' of github.com:primitivefinance/DFMM …
kinrezC Apr 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
688 changes: 341 additions & 347 deletions Cargo.lock

Large diffs are not rendered by default.

5,586 changes: 0 additions & 5,586 deletions kit/Cargo.lock

This file was deleted.

10 changes: 5 additions & 5 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 = "0.10.2"
arbiter-engine = "0.3.0"
arbiter-macros = "0.1.2"
arbiter-bindings = "0.1.6"
arbiter-core = { git = "https://github.com/primitivefinance/arbiter.git", rev = "aff29d30" }
arbiter-engine = { git = "https://github.com/primitivefinance/arbiter.git", rev = "aff29d30" }
arbiter-macros = { git = "https://github.com/primitivefinance/arbiter.git", rev = "aff29d30" }
arbiter-bindings = { git = "https://github.com/primitivefinance/arbiter.git", rev = "aff29d30" }

# Ethereum
ethers = "2.0.13"
Expand All @@ -27,7 +27,7 @@ serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"

# Errors and tracing
anyhow = "1.0.80"
anyhow = "1.0.81"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"

Expand Down
6 changes: 6 additions & 0 deletions kit/configs/test.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
[[deployer]]
Deployer = {}

[[token_admin]]
TokenAdmin = { init_token_data = [
{ name = "US Dollar Coin", symbol = "USDC", decimals = 18 },
{ name = "Wrapped Ether", symbol = "WETH", decimals = 18 },
] }
13 changes: 7 additions & 6 deletions kit/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#[allow(unused)]
use dfmm_kit::behaviors::*;

#[arbiter_macros::main(
name = "DFMM Kit",
about = "Our entrypoint to working with the DFMM Kit.",
behaviors = Behaviors
)]
pub async fn main() {}
// #[arbiter_macros::main(
// name = "DFMM Kit",
// about = "Our entrypoint to working with the DFMM Kit.",
// behaviors = Behaviors
// )]
pub fn main() {}
83 changes: 83 additions & 0 deletions kit/src/behaviors/allocate/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use super::*;

pub trait AllocateType<E>: Debug + Serialize + Clone
where
E: Send + 'static,
{
// TODO: This should probably be how we do it, but this generic `P` gets
// annoying fn change_allocation_amount(&mut self, event: E) ->
// Option<P::AllocationData>;
fn change_allocation_amount(&mut self, event: E) -> Option<Vec<eI256>>;
fn get_stream(&self) -> Pin<Box<dyn Stream<Item = E> + Send + Sync>>;
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct Allocate<A, E, S>
where
A: AllocateType<E>,
E: Send + 'static,
S: State,
{
pub data: S::Data,
pub allocate_type: A,
_phantom_a: PhantomData<A>,
_phantom_e: PhantomData<E>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Config<P: PoolType> {
pub allocation_data: P::AllocationData,
}

pub struct Processing<P, E>
where
P: PoolType,
E: Send + 'static,
{
pub pool: Pool<P>,
pub client: Arc<ArbiterMiddleware>,
pub messager: Messager,
_phantom: PhantomData<E>,
}

impl<P: PoolType> State for Config<P> {
type Data = Self;
}

impl<P, E> State for Processing<P, E>
where
P: PoolType,
E: Send + 'static,
{
type Data = Self;
}

#[allow(unused_variables)]
#[async_trait::async_trait]
impl<A, P, E> Behavior<E> for Allocate<A, E, Config<P>>
where
A: AllocateType<E> + Debug + Send + Sync + 'static + for<'a> Deserialize<'a>,
P: PoolType + Debug + Send + Sync + 'static,
E: Debug + Send + Sync + 'static,
{
type Processor = Allocate<A, E, Processing<P, E>>;
async fn startup(
&mut self,
client: Arc<ArbiterMiddleware>,
messager: Messager,
) -> Result<Option<(Self::Processor, EventStream<E>)>> {
todo!();
}
}

#[async_trait::async_trait]
impl<A, P, E> Processor<E> for Allocate<A, E, Processing<P, E>>
where
A: AllocateType<E> + Debug + Send + Sync + 'static,
P: PoolType + Debug + Send + Sync + 'static,
E: Debug + Send + Sync + 'static,
{
async fn process(&mut self, _event: E) -> Result<ControlFlow> {
Ok(ControlFlow::Halt)
}
}
126 changes: 126 additions & 0 deletions kit/src/behaviors/creator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
use super::*;
use crate::behaviors::token::Response;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Create<S: State> {
pub token_admin: String,
pub data: S::Data,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config<P: PoolType> {
pub base_config: BaseConfig,
pub params: P::Parameters,
pub allocation_data: P::AllocationData,
pub token_list: Vec<String>,
}

impl<P: PoolType> State for Config<P> {
type Data = Self;
}

#[async_trait::async_trait]
impl<P> Behavior<()> for Create<Config<P>>
where
P: PoolType + Send + Sync + 'static,
P::StrategyContract: Send,
P::SolverContract: Send,
{
type Processor = ();
async fn startup(
&mut self,
client: Arc<ArbiterMiddleware>,
mut messager: Messager,
) -> Result<Option<(Self::Processor, EventStream<()>)>> {
// Receive the `DeploymentData` from the `Deployer` agent and use it to get the
// contracts.
debug!("Starting the creator");
let deployment_data = messager.get_next::<DeploymentData>().await?.data;

debug!("Creator: Received deployment data {:?}", deployment_data);
let (strategy_contract, solver_contract) =
P::get_contracts(&deployment_data, client.clone());
let dfmm = DFMM::new(deployment_data.dfmm, client.clone());

// Get the intended tokens for the pool and do approvals.
let mut tokens = Vec::new();
for tkn in self.data.token_list.drain(..) {
messager
.send(
To::Agent(self.token_admin.clone()),
TokenAdminQuery::AddressOf(tkn.clone()),
)
.await
.unwrap();
let token = ArbiterToken::new(
messager.get_next::<eAddress>().await.unwrap().data,
client.clone(),
);
messager
.send(
To::Agent(self.token_admin.clone()),
TokenAdminQuery::MintRequest(MintRequest {
token: tkn,
mint_to: client.address(),
mint_amount: 100_000_000_000,
}),
)
.await
.unwrap();
assert_eq!(
messager.get_next::<Response>().await.unwrap().data,
Response::Success
);
token
.approve(dfmm.address(), MAX)
.send()
.await
.unwrap()
.await
.unwrap();

tokens.push(token);
}
debug!(
"Setting Controller Address to self address: {:?}",
client.address()
);
if self.data.base_config.controller == eAddress::zero() {
self.data.base_config.controller = client.address();
}
let params = P::set_controller(self.data.params.clone(), client.address());

debug!("creating pool...");
let pool = Pool::<P>::new(
self.data.base_config.clone(),
params.clone(),
self.data.allocation_data.clone(),
strategy_contract,
solver_contract,
dfmm,
tokens,
)
.await?;

debug!("Pool created!\n {:#?}", pool);

let pool_creation = (
pool.id,
pool.tokens.iter().map(|t| t.address()).collect::<Vec<_>>(),
pool.liquidity_token.address(),
params,
self.data.allocation_data.clone(),
);
messager.send(To::All, pool_creation).await.unwrap();
Ok(None)
}
}

// TODO: We should be able to use this but it is currently hard to work with due
// to `serde::Deserialize`
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct PoolCreation<P: PoolType> {
pub id: eU256,
pub params: P::Parameters,
pub allocation_data: P::AllocationData,
}
108 changes: 108 additions & 0 deletions kit/src/behaviors/deploy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use arbiter_bindings::bindings::weth::WETH;
use bindings::{
constant_sum::ConstantSum, constant_sum_solver::ConstantSumSolver,
geometric_mean::GeometricMean, geometric_mean_solver::GeometricMeanSolver,
log_normal::LogNormal, log_normal_solver::LogNormalSolver,
};

use super::*;
use crate::bindings::{
n_token_geometric_mean::NTokenGeometricMean,
n_token_geometric_mean_solver::NTokenGeometricMeanSolver,
};

#[derive(Debug, Deserialize, Serialize)]
pub struct Deploy {}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DeploymentData {
pub weth: eAddress,
pub dfmm: eAddress,
pub geometric_mean: eAddress,
pub geometric_mean_solver: eAddress,
pub n_token_geometric_mean: eAddress,
pub n_token_geometric_mean_solver: eAddress,
pub log_normal: eAddress,
pub log_normal_solver: eAddress,
pub constant_sum: eAddress,
pub constant_sum_solver: eAddress,
}

#[async_trait::async_trait]
impl Behavior<()> for Deploy {
type Processor = ();
async fn startup(
&mut self,
client: Arc<ArbiterMiddleware>,
messager: Messager,
) -> Result<Option<(Self::Processor, EventStream<()>)>> {
let weth = WETH::deploy(client.clone(), ())?.send().await?;
trace!("WETH deployed at {:?}", weth.address());

let dfmm = DFMM::deploy(client.clone(), weth.address())?.send().await?;
trace!("DFMM deployed at {:?}", dfmm.address());

let geometric_mean = GeometricMean::deploy(client.clone(), dfmm.address())?
.send()
.await?;
trace!("GeometricMean deployed at {:?}", geometric_mean.address());

let geometric_mean_solver = GeometricMeanSolver::deploy(client.clone(), dfmm.address())?
.send()
.await?;
trace!(
"GeometricMeanSolver deployed at {:?}",
geometric_mean.address()
);

let log_normal = LogNormal::deploy(client.clone(), dfmm.address())?
.send()
.await?;
trace!("LogNormal deployed at {:?}", log_normal.address());

let log_normal_solver = LogNormalSolver::deploy(client.clone(), dfmm.address())?
.send()
.await?;
trace!(
"LogNormalSolver deployed at {:?}",
log_normal_solver.address()
);

let constant_sum = ConstantSum::deploy(client.clone(), dfmm.address())?
.send()
.await?;
trace!("ConstantSum deployed at {:?}", constant_sum.address());

let constant_sum_solver = ConstantSumSolver::deploy(client.clone(), dfmm.address())?
.send()
.await?;
trace!("ConstantSumSolver deployed at {:?}", constant_sum.address());

let n_token_geometric_mean = NTokenGeometricMean::deploy(client.clone(), dfmm.address())?
.send()
.await?;

let n_token_geometric_mean_solver =
NTokenGeometricMeanSolver::deploy(client.clone(), dfmm.address())?
.send()
.await?;

let deployment_data = DeploymentData {
weth: weth.address(),
dfmm: dfmm.address(),
geometric_mean: geometric_mean.address(),
geometric_mean_solver: geometric_mean_solver.address(),
n_token_geometric_mean: n_token_geometric_mean.address(),
n_token_geometric_mean_solver: n_token_geometric_mean_solver.address(),
log_normal: log_normal.address(),
log_normal_solver: log_normal_solver.address(),
constant_sum: constant_sum.address(),
constant_sum_solver: constant_sum_solver.address(),
};

debug!("Deployments completed: {:#?}", deployment_data);

messager.send(To::All, deployment_data).await?;
Ok(None)
}
}
Loading
Loading