Skip to content

Commit

Permalink
feat: add a fetcher util contract for sqrtpricex96
Browse files Browse the repository at this point in the history
  • Loading branch information
ts0yu committed Jul 28, 2024
1 parent fb7d331 commit a9716b0
Show file tree
Hide file tree
Showing 4 changed files with 847 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@
[submodule "contracts/utils/lib/solmate"]
path = contracts/utils/lib/solmate
url = https://github.com/transmissions11/solmate
[submodule "lib/v4-core"]
path = lib/v4-core
url = https://github.com/uniswap/v4-core
[submodule "contracts/utils/lib/v4-core"]
path = contracts/utils/lib/v4-core
url = https://github.com/uniswap/v4-core
1 change: 1 addition & 0 deletions contracts/utils/lib/v4-core
Submodule v4-core added at 799dd2
40 changes: 40 additions & 0 deletions contracts/utils/src/Fetcher.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
pragma solidity ^0.8.10;

import {PoolId} from "v4-core/types/PoolId.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));

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

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)
}
}
}
Loading

0 comments on commit a9716b0

Please sign in to comment.