Skip to content

Commit

Permalink
feat: populate signal correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ts0yu authored Sep 14, 2024
1 parent a3be98b commit 4331dcb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 26 deletions.
46 changes: 42 additions & 4 deletions contracts/utils/src/ArenaController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,68 @@ import {PoolKey} from "v4-core/types/PoolKey.sol";
import {Currency} from "v4-core/types/Currency.sol";
import {IHooks} from "v4-core/interfaces/IHooks.sol";
import {IPoolManager} from "v4-core/interfaces/IPoolManager.sol";
import {LiquidExchange} from "./LiquidExchange.sol";
import {Fetcher} from "./Fetcher.sol";

contract ArenaController {
PoolManager immutable poolManager;
PoolModifyLiquidityTest immutable router;
LiquidExchange immutable lex;
Fetcher immutable fetcher;

ArenaToken immutable currency0;
ArenaToken immutable currency1;

PoolKey public poolKey;

constructor(uint256 fee) {
struct Signal {
int24 currentTick;
uint160 sqrtPriceX96;
address manager;
uint256 lexPrice;
}

constructor(uint256 fee, uint256 initialPrice) {
poolManager = new PoolManager(fee);
router = new PoolModifyLiquidityTest(poolManager);
fetcher = new Fetcher();

currency0 = new ArenaToken("currency0", "c0", 18);
currency1 = new ArenaToken("currency1", "c1", 18);

if (currency0 > currency1) {
(currency0, currency1) = (currency1, currency0);
}

lex = new LiquidExchange(address(currency0), address(currency1), initialPrice);

require(currency0.mint(address(this), type(uint256).max), "Minting currency0 to liquid exchange failed");
require(currency1.mint(address(this), type(uint256).max), "Minting currency1 to liquid exchange failed");
}

function constructSignal() public view returns (Signal memory) {
(uint160 sqrtPriceX96, int24 tick,,) = fetcher.getSlot0(poolManager, fetcher.toId(poolKey));

return Signal({
currentTick: tick,
sqrtPriceX96: sqrtPriceX96,
manager: address(poolManager),
lexPrice: lex.price()
});
}

function setPrice(uint256 price) public {
lex.setPrice(price);
}

function swapOnLex(address tokenIn, uint256 amountIn) public {
lex.swap(tokenIn, amountIn);
}

function setPool(uint24 poolFee, int24 tickSpacing, IHooks hooks, uint160 sqrtPriceX96, bytes memory hookData) public {
poolKey = PoolKey ({
function setPool(uint24 poolFee, int24 tickSpacing, IHooks hooks, uint160 sqrtPriceX96, bytes memory hookData)
public
{
poolKey = PoolKey({
currency0: Currency.wrap(address(currency0)),
currency1: Currency.wrap(address(currency1)),
fee: poolFee,
Expand Down Expand Up @@ -59,4 +97,4 @@ contract ArenaController {

router.modifyLiquidity(poolKey, params, "");
}
}
}
12 changes: 4 additions & 8 deletions contracts/utils/test/ModifyLiquidityTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract Test {
(currency0, currency1) = (currency1, currency0);
}

PoolKey memory poolKey = PoolKey ({
PoolKey memory poolKey = PoolKey({
currency0: Currency.wrap(address(currency0)),
currency1: Currency.wrap(address(currency1)),
fee: 4000,
Expand All @@ -37,13 +37,9 @@ contract Test {
// Represents a 1:1 ratio of assets in the pool.
poolManager.initialize(poolKey, 7922816251426433543950336, "");

IPoolManager.ModifyLiquidityParams memory params = IPoolManager.ModifyLiquidityParams ({
tickLower: -20,
tickUpper: 20,
liquidityDelta: 10000000000,
salt: ""
});
IPoolManager.ModifyLiquidityParams memory params =
IPoolManager.ModifyLiquidityParams({tickLower: -20, tickUpper: 20, liquidityDelta: 10000000000, salt: ""});

router.modifyLiquidity(poolKey, params, "");
}
}
}
27 changes: 17 additions & 10 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ impl<V> Arena<V> {
pub async fn run(&mut self, config: Config) -> Result<(), ArenaError> {
let admin_provider = self.providers[&0].clone();

let controller = ArenaController::deploy(admin_provider.clone(), config.fee).await?;
let controller =
ArenaController::deploy(admin_provider.clone(), config.fee, Uint::from(1)).await?;

controller
.setPool(
Expand All @@ -61,11 +62,13 @@ impl<V> Arena<V> {
for (idx, strategy) in self.strategies.iter_mut().enumerate() {
let strategy_provider = self.providers[&(idx + 1)].clone();

let signal = controller.constructSignal().call().await?._0;

let signal = Signal::new(
self.feed.current_value(),
signal.lexPrice,
None,
Signed::try_from(0).unwrap(),
Uint::from(0),
signal.currentTick,
signal.sqrtPriceX96,
);

strategy
Expand All @@ -78,22 +81,26 @@ impl<V> Arena<V> {
.await;
}

let signal = controller.constructSignal().call().await?._0;

let signal = Signal::new(
self.feed.current_value(),
signal.lexPrice,
None,
Signed::try_from(0).unwrap(),
Uint::from(0),
signal.currentTick,
signal.sqrtPriceX96,
);

self.arbitrageur.init(&signal, admin_provider.clone()).await;

for step in 0..config.steps {
for (idx, strategy) in self.strategies.iter_mut().enumerate() {
let signal = controller.constructSignal().call().await?._0;

let signal = Signal::new(
self.feed.current_value(),
signal.lexPrice,
Some(step),
Signed::try_from(0).unwrap(),
Uint::from(0),
signal.currentTick,
signal.sqrtPriceX96,
);

strategy
Expand Down
2 changes: 1 addition & 1 deletion src/artifacts/ArenaController.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ mod types {
#[derive(Debug, Clone, Default)]
pub struct Signal {
/// Current theoretical value of the pool.
pub current_value: f64,
pub lex_price: Uint<256, 4>,

/// Current step of the simulation.
pub step: Option<usize>,
Expand All @@ -219,13 +219,13 @@ pub struct Signal {
impl Signal {
/// Public constructor function for a new [`Signal`].
pub fn new(
current_value: f64,
lex_price: Uint<256, 4>,
step: Option<usize>,
tick: Signed<24, 1>,
sqrt_price_x96: Uint<160, 3>,
) -> Self {
Self {
current_value,
lex_price,
step,
tick,
sqrt_price_x96,
Expand Down

0 comments on commit 4331dcb

Please sign in to comment.