Skip to content

Commit

Permalink
feat: multi dfmm g3m and lognormal sim
Browse files Browse the repository at this point in the history
  • Loading branch information
kinrezC committed Jan 9, 2024
1 parent 1e0915a commit 3d2c19c
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 111 deletions.
15 changes: 8 additions & 7 deletions configs/v3/static.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ backtest = false
# The seed to use (optional)
seed = 5
# The number of steps in the process
num_steps = 1000
# num_steps = 1000
num_steps = 1825
# The number of distinct paths to use
num_paths = 1
# The initial price of the asset
Expand All @@ -32,9 +33,9 @@ t_0.fixed = 0.0
t_n.fixed = 1.0
## Parameters for the type of process to use.
# # The drift for GBM
process.GBM.drift.fixed = 0.0
process.GBM.drift.fixed = 0.1
# # The volatility for GBM
process.GBM.volatility.fixed = 0.1
process.GBM.volatility.fixed = 0.35
# process.OU.mean.fixed = 1.0
# The volatility for GBM
#process.OU.volatility.fixed = 0.1
Expand All @@ -44,9 +45,9 @@ process.GBM.volatility.fixed = 0.1
# Rmm portfolio manager params
[agent.portfolio_manager.VolatilityTargetingSubmitter]
sigma.fixed = 1.0
tau.fixed = 100000.0
tau.fixed = 1.0
strike_price.fixed = 1.0
fee.fixed = 5
fee.fixed = 30
## Volatility targeting specialty settings
# The frequency which weights are updated
# update_frequency = 450 # 1 update per 30 timestep day
Expand All @@ -56,9 +57,9 @@ specialty.VolatilityTargeting.update_frequency.fixed = 75 # 1 update per 5 times
# The target volatility
specialty.VolatilityTargeting.target_volatility.fixed = 0.15
# The sensitivity factor of the vol differential
specialty.VolatilityTargeting.sensitivity.fixed = 0.0
specialty.VolatilityTargeting.sensitivity.fixed = 0.0115
# Max strike update
specialty.VolatilityTargeting.max_strike_change.fixed = 0.0
specialty.VolatilityTargeting.max_strike_change.fixed = 0.06

# Liquidity provider settings
[agent.lp.LiquidityProvider]
Expand Down
2 changes: 1 addition & 1 deletion configs/v3/sweep.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ process.GBM.volatility.fixed = 0.35
# Rmm portfolio manager params
[agent.portfolio_manager.VolatilityTargetingSubmitter]
sigma.fixed = 1.0
tau.fixed = 100000.0
tau.fixed = 1.0
strike_price.fixed = 1.0
fee.fixed = 30
## Volatility targeting specialty settings
Expand Down
2 changes: 1 addition & 1 deletion crates/bindings/src/dfmm.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions crates/bindings/src/g3m.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions crates/bindings/src/log_normal.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions crates/bindings/src/log_normal_solver.rs

Large diffs are not rendered by default.

50 changes: 32 additions & 18 deletions crates/sim/src/agents/pm/arbitrageur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ impl Arbitrageur {
.await?;
let target_exchange_price_wad = from_ethers_u256(target_exchange_price_wad);
debug!("=== Start Loop ===");
info!("Price[LEX]: {:?}", format_ether(liquid_exchange_price_wad));
info!(
"Price[LOGNORM]: {:?}",
format_ether(target_exchange_price_wad)
);
// info!("Price[LEX]: {:?}", format_ether(liquid_exchange_price_wad));
// info!(
// "Price[LOGNORM]: {:?}",
// format_ether(target_exchange_price_wad)
// );

match liquid_exchange_price_wad {
_ if liquid_exchange_price_wad > target_exchange_price_wad => {
Expand Down Expand Up @@ -256,14 +256,14 @@ impl Agent for Arbitrageur {
let arby = ArbiterToken::new(arby, self.client.clone());
let arbx_balance = arbx.balance_of(self.client.address()).call().await?;
let arby_balance = arby.balance_of(self.client.address()).call().await?;
info!("arby_balance before: {:?}", arby_balance);
// info!("arby_balance before: {:?}", arby_balance);

match self.detect_arbitrage().await? {
Swap::RaiseExchangePrice(target_price) => {
info!(
"Signal[RAISE PRICE]: {:?}",
format_units(target_price, "ether")?
);
// info!(
// "Signal[RAISE PRICE]: {:?}",
// format_units(target_price, "ether")?
// );
let x_in = false;
let input = self.get_dy().await?.into_raw();

Expand Down Expand Up @@ -295,7 +295,8 @@ impl Agent for Arbitrageur {
if let RevmMiddlewareError::ExecutionRevert { gas_used, output } =
e.as_middleware_error().unwrap()
{
info!("Execution revert: {:?} Gas Used: {:?}", output, gas_used);
// info!("Execution revert: {:?} Gas Used: {:?}",
// output, gas_used);
}
}
}
Expand All @@ -305,15 +306,15 @@ impl Agent for Arbitrageur {
.get_ln_internal_price(ethers::types::U256::from(0))
.await?;
let internal_price = from_ethers_u256(internal_price);
info!("Price[LEX]: {:?}", format_ether(target_price));
info!("Price[LOGNORM]: {:?}", format_ether(internal_price));
// info!("Price[LEX]: {:?}", format_ether(target_price));
// info!("Price[LOGNORM]: {:?}", format_ether(internal_price));
debug!("=== End Loop ===");
}
Swap::LowerExchangePrice(target_price) => {
info!(
"Signal[LOWER PRICE] {:?}",
format_units(target_price, "ether")?
);
// info!(
// "Signal[LOWER PRICE] {:?}",
// format_units(target_price, "ether")?
// );

let x_in = true;
let liquid_exchange_price = self.liquid_exchange.price().call().await?;
Expand All @@ -324,6 +325,18 @@ impl Agent for Arbitrageur {

debug!("Optimal x input: {:?}", input);

let dfmm_balance_x = arbx
.balance_of(self.protocol_client.protocol.address())
.call()
.await?;
let dfmm_balance_y = arby
.balance_of(self.protocol_client.protocol.address())
.call()
.await?;

debug!("DFMM Balance X: {:?}", dfmm_balance_x);
debug!("DFMM Balance Y: {:?}", dfmm_balance_y);

let tx = self
.atomic_arbitrage
.lower_exchange_price(ethers::types::U256::from(0), input);
Expand Down Expand Up @@ -353,7 +366,8 @@ impl Agent for Arbitrageur {
if let RevmMiddlewareError::ExecutionRevert { gas_used, output } =
e.as_middleware_error().unwrap()
{
info!("Execution revert: {:?} Gas Used: {:?}", output, gas_used);
// info!("Execution revert: {:?} Gas Used: {:?}",
// output, gas_used);
}
}
}
Expand Down
83 changes: 26 additions & 57 deletions crates/sim/src/agents/pm/g3m_arbitrageur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ impl G3mArbitrageur {
.await?;
let target_exchange_price_wad = from_ethers_u256(target_exchange_price_wad);
debug!("=== Start Loop ===");
info!("Price[LEX]: {:?}", format_ether(liquid_exchange_price_wad));
info!("Price[G3M]: {:?}", format_ether(target_exchange_price_wad));
// info!("Price[LEX]: {:?}", format_ether(liquid_exchange_price_wad));
// info!("Price[G3M]: {:?}", format_ether(target_exchange_price_wad));

match liquid_exchange_price_wad {
_ if liquid_exchange_price_wad > target_exchange_price_wad => {
Expand Down Expand Up @@ -244,50 +244,27 @@ impl Agent for G3mArbitrageur {

match self.detect_arbitrage().await? {
Swap::RaiseExchangePrice(target_price) => {
info!(
"Signal[RAISE PRICE]: {:?}",
format_units(target_price, "ether")?
);
// info!(
// "Signal[RAISE PRICE]: {:?}",
// format_units(target_price, "ether")?
// );
let x_in = false;
let input = self.get_dy().await?.into_raw();
let mut input = self.get_dy().await?;
if (input < ethers::types::I256::from(0)) {
input = ethers::types::I256::from(0);
}
let input = input.into_raw();

debug!("Optimal y input: {:?}", input);

let tx = self
.atomic_arbitrage
.raise_exchange_price(ethers::types::U256::from(1), input);

let simulated_swap = self
.protocol_client
.g_solver
.simulate_swap(ethers::types::U256::from(1), false, input)
.call()
.await?;

let swap_constant = self
.protocol_client
.g_solver
.check_swap_constant(ethers::types::U256::from(1), simulated_swap.3)
.call()
.await?;
debug!("=============Swap constant: {:?}==========", swap_constant);

let output = tx.send().await;
let arbx_balance = arbx.balance_of(self.client.address()).call().await?;
let arby_balance = arby.balance_of(self.client.address()).call().await?;
debug!("arby_balance after: {:?}", arby_balance);
let (reserve_x, reserve_y, liquidity) = self
.protocol_client
.protocol
.get_reserves_and_liquidity(ethers::types::U256::from(1))
.call()
.await?;
let (wx, wy) = self
.protocol_client
.g_strategy
.get_params(ethers::types::U256::from(1))
.call()
.await?;

match output {
Ok(output) => {
Expand All @@ -297,7 +274,8 @@ impl Agent for G3mArbitrageur {
if let RevmMiddlewareError::ExecutionRevert { gas_used, output } =
e.as_middleware_error().unwrap()
{
info!("Execution revert: {:?} Gas Used: {:?}", output, gas_used);
// info!("Execution revert: {:?} Gas Used: {:?}",
// output, gas_used);
}
}
}
Expand All @@ -307,35 +285,25 @@ impl Agent for G3mArbitrageur {
.get_g_internal_price(ethers::types::U256::from(1))
.await?;
let internal_price = from_ethers_u256(internal_price);
info!("Price[LEX]: {:?}", format_ether(target_price));
info!("Price[G3M]: {:?}", format_ether(internal_price));
// info!("Price[LEX]: {:?}", format_ether(target_price));
// info!("Price[G3M]: {:?}", format_ether(internal_price));
debug!("=== End Loop ===");
}
Swap::LowerExchangePrice(target_price) => {
info!(
"Signal[LOWER PRICE] {:?}",
format_units(target_price, "ether")?
);
// info!(
// "Signal[LOWER PRICE] {:?}",
// format_units(target_price, "ether")?
// );

let x_in = true;
let liquid_exchange_price = self.liquid_exchange.price().call().await?;
let input = self.get_dx().await?.into_raw();
let mut input = self.get_dx().await?;
if (input < ethers::types::I256::from(0)) {
input = ethers::types::I256::from(0);
}
let input = input.into_raw();
let input = input * liquid_exchange_price / ethers::utils::parse_ether("1")?;

debug!("Optimal x input: {:?}", input);
let simulated_swap = self
.protocol_client
.g_solver
.simulate_swap(ethers::types::U256::from(1), true, input)
.call()
.await?;
let swap_constant = self
.protocol_client
.g_solver
.check_swap_constant(ethers::types::U256::from(1), simulated_swap.3)
.call()
.await?;
debug!("=============Swap constant: {:?}==========", swap_constant);

let tx = self
.atomic_arbitrage
Expand Down Expand Up @@ -370,7 +338,8 @@ impl Agent for G3mArbitrageur {
if let RevmMiddlewareError::ExecutionRevert { gas_used, output } =
e.as_middleware_error().unwrap()
{
info!("Execution revert: {:?} Gas Used: {:?}", output, gas_used);
// info!("Execution revert: {:?} Gas Used: {:?}",
// output, gas_used);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/sim/src/agents/pm/submitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ impl Agent for VolatilityTargetingSubmitter {

if self.g_data.portfolio_prices.is_empty() {
info!("portfolio_price: {}", g_portfolio_price);
self.ln_data.portfolio_prices.push((g_portfolio_price, 0));
self.ln_data.asset_prices.push((asset_price, 0));
self.g_data.portfolio_prices.push((g_portfolio_price, 0));
self.g_data.asset_prices.push((asset_price, 0));
}

if timestamp >= self.next_update_timestamp {
Expand Down Expand Up @@ -245,7 +245,7 @@ impl VolatilityTargetingSubmitter {
/ self.ln_data.asset_prices.first().unwrap().0
);
info!(
"Return[PORTFOLIO]: {}",
"Return[LN_PORTFOLIO]: {}",
(self.ln_data.portfolio_prices.last().unwrap().0
- self.ln_data.portfolio_prices.first().unwrap().0)
/ self.ln_data.portfolio_prices.first().unwrap().0
Expand Down Expand Up @@ -294,7 +294,7 @@ impl VolatilityTargetingSubmitter {
/ self.g_data.asset_prices.first().unwrap().0
);
info!(
"Return[PORTFOLIO]: {}",
"Return[G3M_PORTFOLIO]: {}",
(self.g_data.portfolio_prices.last().unwrap().0
- self.g_data.portfolio_prices.first().unwrap().0)
/ self.g_data.portfolio_prices.first().unwrap().0
Expand Down
8 changes: 4 additions & 4 deletions crates/sim/src/scenarios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ impl Scenario for DFMMScenario {
agents.add(lp);

// 4. Arbitrageur arbitrages between the DFMM and the Liquid Exchange.
// let arbitrageur =
// Arbitrageur::new(&environment, &token_admin, lex,
// protocol_client.clone()).await?; agents.add(arbitrageur.clone());
let arbitrageur =
Arbitrageur::new(&environment, &token_admin, lex, protocol_client.clone()).await?;
agents.add(arbitrageur.clone());

let g3m_arbitrageur =
G3mArbitrageur::new(&environment, &token_admin, lex, protocol_client.clone()).await?;
Expand All @@ -119,7 +119,7 @@ impl Scenario for DFMMScenario {
.add(market_events, "dfmm")
.add(token_admin.arbx.events(), "arbx")
.add(token_admin.arby.events(), "arby")
// .add(arbitrageur.atomic_arbitrage.events(), "ln_atomic_arbitrage")
.add(arbitrageur.atomic_arbitrage.events(), "ln_atomic_arbitrage")
.add(
g3m_arbitrageur.atomic_arbitrage.events(),
"g3m_atomic_arbitrage",
Expand Down
1 change: 0 additions & 1 deletion src/DFMM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ contract DFMM is ICore {
) {
uint256 growth = feeGrowth.mulWadDown(feeGrowthLast[msg.sender]);
balanceOf[msg.sender] = balanceOf[msg.sender].mulWadDown(growth);
console2.log("in here");
}

uint256 deltaX = reserveXWad - rx;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/StrategyLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ uint256 constant INFINITY_IS_NOT_REAL = type(uint256).max;
uint256 constant ZERO = 0;

/// @dev the swap constant should never fall outside of range [-EPSILON, EPSILON]
int256 constant EPSILON = 20;
int256 constant EPSILON = 30;

/// @dev Structure to hold reserve information
struct Reserves {
Expand Down
4 changes: 2 additions & 2 deletions src/solvers/LogNormalSolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ contract LogNormalSolver is IParams {
deltaL += 1;

endReserves.rx = startReserves.rx + amountIn;
endReserves.L = startReserves.L + deltaL;
endReserves.L = startComputedL + deltaL;

endReserves.ry =
getNextReserveY(poolId, endReserves.rx, endReserves.L);
Expand All @@ -170,7 +170,7 @@ contract LogNormalSolver is IParams {
deltaL += 1;

endReserves.ry = startReserves.ry + amountIn;
endReserves.L = startReserves.L + deltaL;
endReserves.L = startComputedL + deltaL;

endReserves.rx =
getNextReserveX(poolId, endReserves.ry, endReserves.L);
Expand Down
Loading

0 comments on commit 3d2c19c

Please sign in to comment.