Skip to content

Commit

Permalink
Merge branch 'main' into ben/inspector.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ts0yu authored Aug 28, 2024
2 parents 8f4e5b6 + 0751941 commit b02c8ae
Show file tree
Hide file tree
Showing 16 changed files with 628 additions and 49 deletions.
5 changes: 4 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
url = https://github.com/foundry-rs/forge-std
[submodule "contracts/utils/lib/solmate"]
path = contracts/utils/lib/solmate
url = https://github.com/tranmissions11/solmate
url = https://github.com/transmissions11/solmate
[submodule "contracts/utils/lib/v4-core"]
path = contracts/utils/lib/v4-core
url = https://github.com/uniswap/v4-core
143 changes: 142 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
[package]
name = "arena-core"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
repository = "https://github.com/arena-rs/arena"
description = "Framework for holistic economic modelling and simulation of Uniswap v4 strategies, hooks and pools."
license-file = "./LICENSE"
exclude = ["contracts", "cli"]

[lib]
name = "libarena"
path = "src/lib.rs"

[[bin]]
name = "arena"
path = "src/bin/cli.rs"

[dependencies]
alloy = { version = "0.2.1", features = ["full", "node-bindings", "json"] }
rug = "1.25.0"
rand = "0.8.5"
clap = { version = "4.5.16", features = ["derive"] }
plotly = "0.9.0"
rand_distr = "0.4.3"
async-trait = "0.1.81"
alloy-contract = "0.2.1"
alloy-sol-macro = "0.7.7"
alloy-sol-types = "0.7.7"
plotly = "0.9.0"
rand = "0.8.5"
rand_distr = "0.4.3"
tokio = { version = "1.39.2", features = ["macros"] }
csv = "1.1"
serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0"
serde_json = "1.0"
alloy = { version = "0.2.1", features = ["full", "node-bindings", "json"] }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The price of the simulation's underlying pool is set via a price process that im

## Usage

To use Arena, the Rust programming language must be installed on your machine. This is commonly achieved using `rustup`.
To use Arena, the Rust programming language alongside the Foundry framework must be installed on your machine. This is commonly achieved using [`rustup`](https://rustup.rs/), and [`foundryup`](https://book.getfoundry.sh/getting-started/installation)

Arena can be added to your library or binary with
```
Expand Down
1 change: 1 addition & 0 deletions contracts/utils/lib/v4-core
Submodule v4-core added at 799dd2
82 changes: 82 additions & 0 deletions contracts/utils/src/Fetcher.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
pragma solidity ^0.8.10;

import {PoolId} from "v4-core/types/PoolId.sol";
import {PoolKey} from "v4-core/types/PoolKey.sol";
import {IPoolManager} from "v4-core/interfaces/IPoolManager.sol";
import {Position} from "v4-core/libraries/Position.sol";

// This contract is a compact version of StateLibrary, which can be found below.
// https://github.com/Uniswap/v4-core/blob/799dd2cb980319a8d3b827b6a7aa59a606634553/src/libraries/StateLibrary.sol
contract Fetcher {
bytes32 public constant POOLS_SLOT = bytes32(uint256(6));
uint256 public constant TICKS_OFFSET = 4;

function _getPoolStateSlot(PoolId poolId) internal pure returns (bytes32) {
return keccak256(abi.encodePacked(PoolId.unwrap(poolId), POOLS_SLOT));
}

function toId(PoolKey memory poolKey) external pure returns (PoolId poolId) {
assembly ("memory-safe") {
poolId := keccak256(poolKey, mul(32, 5))
}
}

function getTickInfo(IPoolManager manager, PoolId poolId, int24 tick)
external
view
returns (
uint128 liquidityGross,
int128 liquidityNet,
uint256 feeGrowthOutside0X128,
uint256 feeGrowthOutside1X128
)
{
bytes32 slot = _getTickInfoSlot(poolId, tick);

// read all 3 words of the TickInfo struct
bytes32[] memory data = manager.extsload(slot, 3);
assembly ("memory-safe") {
let firstWord := mload(add(data, 32))
liquidityNet := sar(128, firstWord)
liquidityGross := and(firstWord, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
feeGrowthOutside0X128 := mload(add(data, 64))
feeGrowthOutside1X128 := mload(add(data, 96))
}
}

function getSlot0(IPoolManager manager, PoolId poolId)
external
view
returns (uint160 sqrtPriceX96, int24 tick, uint24 protocolFee, uint24 lpFee)
{
// slot key of Pool.State value: `pools[poolId]`
bytes32 stateSlot = _getPoolStateSlot(poolId);

bytes32 data = manager.extsload(stateSlot);

// 24 bits |24bits|24bits |24 bits|160 bits
// 0x000000 |000bb8|000000 |ffff75 |0000000000000000fe3aa841ba359daa0ea9eff7
// ---------- | fee |protocolfee | tick | sqrtPriceX96
assembly ("memory-safe") {
// bottom 160 bits of data
sqrtPriceX96 := and(data, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
// next 24 bits of data
tick := signextend(2, shr(160, data))
// next 24 bits of data
protocolFee := and(shr(184, data), 0xFFFFFF)
// last 24 bits of data
lpFee := and(shr(208, data), 0xFFFFFF)
}
}

function _getTickInfoSlot(PoolId poolId, int24 tick) internal pure returns (bytes32) {
// slot key of Pool.State value: `pools[poolId]`
bytes32 stateSlot = _getPoolStateSlot(poolId);

// Pool.State: `mapping(int24 => TickInfo) ticks`
bytes32 ticksMappingSlot = bytes32(uint256(stateSlot) + TICKS_OFFSET);

// slot key of the tick key: `pools[poolId].ticks[tick]
return keccak256(abi.encodePacked(int256(tick), ticksMappingSlot));
}
}
9 changes: 9 additions & 0 deletions src/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "src"
version = "0.1.0"
edition = "2021"

[dependencies]

[lib]
path = "lib.rs"
Loading

0 comments on commit b02c8ae

Please sign in to comment.