Skip to content

Commit

Permalink
clean up init
Browse files Browse the repository at this point in the history
  • Loading branch information
kinrezC committed Oct 10, 2023
1 parent 723f192 commit 9325712
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 137 deletions.
77 changes: 77 additions & 0 deletions box-simulation/src/agents/liquidity_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::settings::params::SimulationConfig;
use arbiter_core::bindings::arbiter_token::ArbiterToken;
use ethers::utils::parse_ether;
use std::sync::Arc;

use super::*;

pub const INITIAL_PORTFOLIO_BALANCES: (u64, u64) = (100_000, 100_000);
pub struct LiquidityProvider {
pub client: Arc<RevmMiddleware>,
pub g3m: G3M<RevmMiddleware>,
pub arbx: ArbiterToken<RevmMiddleware>,
pub arby: ArbiterToken<RevmMiddleware>,
}

impl LiquidityProvider {
pub async fn new(label: &str, environment: &Environment, g3m_address: Address) -> Result<Self> {
let client = RevmMiddleware::new(environment, Some(label))?;
let g3m = G3M::new(g3m_address, client.clone());

let arbx = ArbiterToken::new(g3m.token_x().call().await?, client.clone());
let arby = ArbiterToken::new(g3m.token_y().call().await?, client.clone());

arbx.approve(g3m.address(), U256::MAX).send().await?;
arby.approve(g3m.address(), U256::MAX).send().await?;

Ok(Self {
client,
g3m,
arbx,
arby,
})
}

pub async fn add_liquidity(self, config: &SimulationConfig) -> Result<()> {
// Initial weight is set in the simulation config, but it can be overridden with setWeightX() function.
let initial_weight_0 =
parse_ether(config.portfolio_pool_parameters.weight_token_0).unwrap();
let initial_weight_1 = parse_ether(1)
.unwrap()
.checked_sub(initial_weight_0)
.unwrap();
// Using the initial weight, initial price, and initial reserve x, we can compute reserve y.
let initial_price = config.portfolio_pool_parameters.initial_price;
let initial_reserve_x = parse_ether(INITIAL_PORTFOLIO_BALANCES.0).unwrap();

// p = (x / w_x) / (y / w_y)
// y / w_y = (x / w_x) / p
// y = (x / w_x) / p * w_y
let one_ether = parse_ether(1).unwrap();
let initial_reserve_y = initial_reserve_x
.checked_mul(one_ether)
.unwrap()
.checked_div(initial_weight_0)
.unwrap()
.checked_mul(one_ether)
.unwrap()
.checked_div(parse_ether(initial_price).unwrap())
.unwrap()
.checked_mul(initial_weight_1)
.unwrap()
.checked_div(one_ether)
.unwrap();

// Get the parsed amounts for the portfolio deposit.
let amounts = (initial_reserve_x, initial_reserve_y);

// Call init pool to setup the portfolio
// Needs an amount of both tokens, the amounts can be anything but note that they affect the spot price.
self.g3m
.init_pool(amounts.0.into(), amounts.1.into())
.send()
.await?
.await?;
Ok(())
}
}
1 change: 1 addition & 0 deletions box-simulation/src/agents/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
pub mod arbitrageur;
pub mod atomic_arbitrage;
pub mod liquidity_provider;
pub mod rebalancer;
30 changes: 23 additions & 7 deletions box-simulation/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::time::Instant;

use agents::liquidity_provider::LiquidityProvider;
use agents::rebalancer::Rebalancer;
use anyhow::Result;
use arbiter_core::environment::builder::EnvironmentBuilder;
use arbiter_core::{
bindings::liquid_exchange::LiquidExchange, environment::Environment, middleware::RevmMiddleware,
};
use ethers::types::{Address, U256};
use tokio::task::JoinHandle;
use tracing::event;
use tracing_subscriber;

use bindings::{atomic_arbitrage::AtomicArbitrage, g3m::G3M};
Expand All @@ -21,6 +19,11 @@ mod utils;
/// The number 10^18.
pub const WAD: ethers::types::U256 = ethers::types::U256([10_u64.pow(18), 0, 0, 0]);

pub struct Agents {
pub liquidity_provider: LiquidityProvider,
pub rebalancer: Rebalancer,
}

#[tokio::main]
async fn main() -> Result<()> {
if std::env::var("RUST_LOG").is_err() {
Expand All @@ -35,9 +38,22 @@ async fn main() -> Result<()> {
let env = EnvironmentBuilder::new().build();
let contracts = setup::deploy::deploy_contracts(&env, &config).await?;

setup::init::init(&contracts, &config).await;

let init = setup::init::init(&contracts, &config).await?;
let lp = LiquidityProvider::new("lp", &env, contracts.exchanges.g3m.address()).await?;
let rebalancer = Rebalancer::new(
"rebalancer",
&env,
contracts.exchanges.lex.address(),
contracts.exchanges.g3m.address(),
0.15,
)
.await?;

let agents = Agents {
liquidity_provider: lp,
rebalancer,
};

let init = setup::init::init(&contracts, agents, &config).await?;

Ok(())
}
Loading

0 comments on commit 9325712

Please sign in to comment.