Skip to content

Commit

Permalink
chore: Updater tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJepsen committed Apr 19, 2024
1 parent 313ae77 commit 2a50abb
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 17 deletions.
10 changes: 10 additions & 0 deletions kit/src/behaviors/creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ where

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();
}
debug!("creating pool...");
// Create the pool.
let pool = Pool::<P>::new(
Expand Down Expand Up @@ -110,6 +117,9 @@ where
pool.id,
pool.tokens.iter().map(|t| t.address()).collect::<Vec<_>>(),
pool.liquidity_token.address(),
// TODO: This params will show the incorrect controller address
// Would be nice to have it be the correct one set on line 88
// so that the debug msg's are more helpful
self.data.params.clone(),
self.data.allocation_data.clone(),
);
Expand Down
16 changes: 12 additions & 4 deletions kit/src/behaviors/update.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::VecDeque;

use bindings::idfmm::IDFMM;
use tracing::warn;

Expand All @@ -15,15 +17,15 @@ pub struct Config<P: PoolType> {
pub base_config: BaseConfig,
pub allocation_data: P::AllocationData,
pub token_list: Vec<String>,
pub params: Vec<P::Parameters>,
pub params: VecDeque<P::Parameters>,
}

#[derive(Debug, Clone)]
pub struct Processing<P: PoolType> {
pub messager: Messager,
pub client: Arc<ArbiterMiddleware>,
pub pool: Pool<P>,
pub pool_params: Vec<P::Parameters>,
pub pool_params: VecDeque<P::Parameters>,
}

impl<P: PoolType> State for Config<P> {
Expand Down Expand Up @@ -64,6 +66,7 @@ where
mut messager: Messager,
) -> Result<Option<(Self::Processor, EventStream<Message>)>> {
// Make a "TODO" list.
// This is the data I need to recieve to do my job
let mut todo: Todo<P> = Todo {
deployment_data: None,
pool_creation: None,
Expand Down Expand Up @@ -152,9 +155,14 @@ where

match msg {
UpdatoorQuerry::UpdateMeDaddy => {
let params = self.data.pool_params.pop().unwrap();
let params = self.data.pool_params.pop_front().unwrap();
self.data.pool.update(params.clone()).await?;
let _ = self.data.messager.send(To::All, params).await?;
let _ = self
.data
.messager
.send(To::Agent(event.from), MessageTypes::Update::<P>(params))
.await?;
info!("Successfully updated!");
}

UpdatoorQuerry::NoOp => {
Expand Down
5 changes: 5 additions & 0 deletions kit/src/pool/constant_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ impl PoolType for ConstantSumPool {
Ok(init_bytes)
}

fn set_controller(mut params: Self::Parameters, controller: eAddress) -> Self::Parameters {
params.controller = controller;
params
}

fn get_strategy_address(strategy_contract: &Self::StrategyContract) -> eAddress {
strategy_contract.address()
}
Expand Down
18 changes: 16 additions & 2 deletions kit/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct BaseConfig {
pub name: String,
pub symbol: String,
pub swap_fee: eU256,
pub controller: eAddress,
pub controller_fee: eU256,
}

Expand Down Expand Up @@ -84,6 +85,8 @@ pub trait PoolType: Clone + Debug + 'static {
solver_contract: &Self::SolverContract,
) -> Result<Bytes>;

fn set_controller(params: Self::Parameters, controller: eAddress) -> Self::Parameters;

fn create_instance(
strategy_contract: Self::StrategyContract,
solver_contract: Self::SolverContract,
Expand Down Expand Up @@ -142,7 +145,17 @@ impl<P: PoolType> Pool<P> {
dfmm: DFMM<ArbiterMiddleware>,
tokens: Vec<ArbiterToken<ArbiterMiddleware>>,
) -> Result<Self> {
let data = P::get_init_data(params.clone(), &allocation_data, &solver_contract).await?;
// This seems a little hacky but might be the way forward untill we get the
// contract changes settled in the future we should see if we can work
// with Matt and Clement to remove the controller from the init params
let params_with_controller = P::set_controller(params, base_config.controller);

let data = P::get_init_data(
params_with_controller.clone(),
&allocation_data,
&solver_contract,
)
.await?;
debug!("Got init data {:?}", data);
let init_params = InitParams {
name: base_config.name,
Expand All @@ -157,7 +170,8 @@ impl<P: PoolType> Pool<P> {
let (id, _reserves, _total_liquidity) = dfmm.init(init_params.clone()).call().await?;
dfmm.init(init_params).send().await?.await?;
let pool: shared_types::Pool = dfmm.pools(id).call().await?;
let instance = P::create_instance(strategy_contract, solver_contract, params);
let instance =
P::create_instance(strategy_contract, solver_contract, params_with_controller);
let client = dfmm.client();

let pool = Self {
Expand Down
15 changes: 10 additions & 5 deletions kit/tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::VecDeque;

use arbiter_engine::{agent::Agent, world::World};
use dfmm_kit::{
behaviors::{
Expand Down Expand Up @@ -87,6 +89,7 @@ pub fn spawn_constant_sum_updater(world: &mut World) {
symbol: "TP".to_string(),
swap_fee: ethers::utils::parse_ether(0.003).unwrap(),
controller_fee: 0.into(),
controller: eAddress::zero(),
},
allocation_data: ConstantSumAllocationData {
reserve_x: RESERVE_X,
Expand All @@ -100,20 +103,21 @@ pub fn spawn_constant_sum_updater(world: &mut World) {
)
}

pub fn constant_sum_parameters() -> Vec<ConstantSumParams> {
let prices = vec![
pub fn constant_sum_parameters() -> VecDeque<ConstantSumParams> {
let prices: VecDeque<eU256> = vec![
PRICE,
ethers::utils::parse_ether(2).unwrap(),
ethers::utils::parse_ether(3).unwrap(),
];
let mut params = vec![];
]
.into();
let mut params = VecDeque::new();
for price in prices {
let parameter = ConstantSumParams {
price,
swap_fee: ethers::utils::parse_ether(0.003).unwrap(),
controller: eAddress::zero(),
};
params.push(parameter);
params.push_back(parameter);
}
params
}
Expand All @@ -133,6 +137,7 @@ fn creator() -> Create<creator::Config<ConstantSumPool>> {
symbol: "TP".to_string(),
swap_fee: ethers::utils::parse_ether(0.003).unwrap(),
controller_fee: 0.into(),
controller: eAddress::zero(),
},
allocation_data: ConstantSumAllocationData {
reserve_x: RESERVE_X,
Expand Down
15 changes: 9 additions & 6 deletions kit/tests/update_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::time::Duration;
use arbiter_engine::messager::To;
use dfmm_kit::behaviors::MessageTypes;
use futures_util::StreamExt;
use tracing::{debug, info, warn};
use tracing_subscriber::registry::Data;
use tracing::{info, warn};
include!("common.rs");

#[tokio::test(flavor = "multi_thread", worker_threads = 5)]
Expand All @@ -24,7 +23,7 @@ async fn run_updater_constant_sum() {
// receivers. TODO: This is a bit of a hack and we could honestly make
// the `World::run` better to handle this, but this works for now.
tokio::time::sleep(Duration::from_millis(2000)).await;
for _ in 0..2 {
for _ in 0..3 {
messager
.send(
To::Agent(UPDATER.to_owned()),
Expand All @@ -45,10 +44,14 @@ async fn run_updater_constant_sum() {
MessageTypes::Create(_) => continue,
MessageTypes::TokenAdmin(_) => continue,
MessageTypes::Update(params) => {
info!("successfully updated the params to {:?}", params);
let mock_data = constant_sum_parameters();
// assert_eq!(data, mock_data[0]);
warn!("deseriazlied into update respondse {:?}", params);
break;
assert_eq!(params, mock_data[count]);
if count >= 2 {
break;
} else {
count += 1;
}
}
}
}
Expand Down

0 comments on commit 2a50abb

Please sign in to comment.