Verified contractExactMIT
EkuboPositionRegistry
Address0x999a76eaac2ff7c713ea0e0cdf93bd70e2ea54b3
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.19
- Same code
Only this contract
Details
- Compiler
- solc 0.8.19+commit.7dd6d404
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- paris
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,400,513
- Creation transaction
- 0x7878cc1825…9b7a374d35
contracts/connectors/ekubo/EkuboPositionRegistry.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import {
EkuboPoolKey,
IEkuboPositions
} from "contracts/interfaces/external/ekubo/IEkuboPositions.sol";
import { EkuboLib } from "contracts/connectors/ekubo/EkuboLib.sol";
/// @notice The Ekubo v3 range a Positions NFT holds: one (poolKey, ticks)
/// pair, in Ekubo's own int32 tick units.
struct EkuboPosition {
EkuboPoolKey poolKey;
int32 tickLower;
int32 tickUpper;
}
/// @title EkuboPositionRegistry
/// @notice Position metadata that Ekubo v3 Positions NFTs do not store.
/// @dev An Ekubo NFT carries no pool or range: a position is (id, poolKey,
/// tickLower, tickUpper), and one id may hold several of them. Its ids are
/// salt hashes, and the NFT is neither enumerable nor counted. The Sickle
/// interfaces, however, address a position by NFT id alone and detect mints
/// through a supply counter.
///
/// This registry fills that gap. It is called normally, never
/// delegatecalled: EkuboV3Connector, running inside a Sickle, calls
/// `record` right after minting, so `msg.sender` is the Sickle that owns
/// the NFT. Every record is checked against the protocol, not taken on the
/// caller's word: the caller must own the NFT, and Core must hold nonzero
/// liquidity for exactly that (id, poolKey, range).
///
/// Records are write-once, so an id never moves to another range. Keeping
/// them outside the connector lets a connector redeploy reuse them.
///
/// Only NFTs minted through the connector are recorded. An Ekubo NFT minted
/// elsewhere has no record, and the connector's views revert for it.
contract EkuboPositionRegistry {
error PositionAlreadyRecorded(uint256 tokenId);
error PositionNotRecorded(uint256 tokenId);
error PoolNotRegistered(bytes32 poolId);
error PoolNotInitialized(bytes32 poolId);
error NotPositionOwner(uint256 tokenId, address owner, address caller);
error PositionHasNoLiquidity(uint256 tokenId);
/// @notice A pool key became resolvable from its id.
event PoolRegistered(
bytes32 indexed poolId, address token0, address token1, bytes32 config
);
/// @notice An NFT was bound to its pool and range. `mintIndex` is the
/// value of `mintCount` after this record.
event PositionRecorded(
uint256 indexed tokenId,
address indexed owner,
bytes32 indexed poolId,
int32 tickLower,
int32 tickUpper,
uint256 mintIndex
);
IEkuboPositions public immutable positions;
address public immutable core;
/// @notice Number of positions recorded. Never decreases; the Sickle
/// strategies read it as the NFT supply.
uint256 public mintCount;
/// @notice The last NFT each owner recorded.
mapping(address => uint256) public lastTokenId;
/// @dev Presence is token1 != 0: Ekubo requires token0 < token1, so a
/// real key never has a zero token1, whereas config 0 is a valid
/// (full-range, fee-free, extensionless) pool.
mapping(uint256 => EkuboPosition) private _positions;
mapping(bytes32 => EkuboPoolKey) private _poolKeys;
constructor(
IEkuboPositions positions_,
address core_
) {
positions = positions_;
core = core_;
}
/// @notice Salt for the next connector mint. Each salt is used by at most
/// one recorded mint, so with Positions' id = keccak(minter, salt,
/// chainId, Positions) the id is unique and known before the mint.
function nextSalt() external view returns (bytes32) {
return bytes32(mintCount + 1);
}
/// @notice Bind `tokenId`, owned by the caller, to its pool and range.
function record(
uint256 tokenId,
EkuboPoolKey calldata poolKey,
int32 tickLower,
int32 tickUpper
) external returns (uint256 mintIndex) {
if (_positions[tokenId].poolKey.token1 != address(0)) {
revert PositionAlreadyRecorded(tokenId);
}
address owner = positions.ownerOf(tokenId);
if (owner != msg.sender) {
revert NotPositionOwner(tokenId, owner, msg.sender);
}
(uint128 liquidity,,,,) = positions.getPositionFeesAndLiquidity(
tokenId, poolKey, tickLower, tickUpper
);
if (liquidity == 0) {
revert PositionHasNoLiquidity(tokenId);
}
bytes32 id = _registerPool(poolKey);
_positions[tokenId] = EkuboPosition({
poolKey: poolKey, tickLower: tickLower, tickUpper: tickUpper
});
mintIndex = ++mintCount;
lastTokenId[msg.sender] = tokenId;
emit PositionRecorded(
tokenId, msg.sender, id, tickLower, tickUpper, mintIndex
);
}
/// @notice Make an initialized pool resolvable from its id. The id is the
/// key's hash, so anyone may register any initialized pool.
function registerPool(
EkuboPoolKey calldata poolKey
) external returns (bytes32) {
return _registerPool(poolKey);
}
function getPosition(
uint256 tokenId
) external view returns (EkuboPosition memory position) {
position = _positions[tokenId];
if (position.poolKey.token1 == address(0)) {
revert PositionNotRecorded(tokenId);
}
}
function isRecorded(
uint256 tokenId
) external view returns (bool) {
return _positions[tokenId].poolKey.token1 != address(0);
}
function getPoolKey(
bytes32 poolId
) external view returns (EkuboPoolKey memory poolKey) {
poolKey = _poolKeys[poolId];
if (poolKey.token1 == address(0)) {
revert PoolNotRegistered(poolId);
}
}
function _registerPool(
EkuboPoolKey memory poolKey
) private returns (bytes32 id) {
id = EkuboLib.poolId(poolKey);
if (_poolKeys[id].token1 != address(0)) {
return id;
}
if (!EkuboLib.isInitialized(core, id)) {
revert PoolNotInitialized(id);
}
_poolKeys[id] = poolKey;
emit PoolRegistered(id, poolKey.token0, poolKey.token1, poolKey.config);
}
}
contracts/connectors/ekubo/EkuboLib.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import {
EkuboPoolKey
} from "contracts/interfaces/external/ekubo/IEkuboPositions.sol";
/// @notice Ekubo v3 encodings shared by EkuboPositionRegistry and
/// EkuboV3Connector. Each helper restates one definition from
/// EkuboProtocol/evm-contracts (pinned at acf5e95, the source verified for
/// the deployed Core and Positions); the file cited is the authority.
library EkuboLib {
/// @dev `math/constants.sol` MIN_TICK / MAX_TICK. Ticks are powers of
/// 1.000001, unlike Uniswap's 1.0001.
int256 internal constant MIN_TICK = -88_722_835;
int256 internal constant MAX_TICK = 88_722_835;
/// @dev The smallest concentrated tick spacing whose scaled ticks fit the
/// Sickle structs' int24 fields: MAX_TICK / 11 = 8_065_712 <= 2^23 - 1,
/// while MAX_TICK / 10 = 8_872_283 does not fit.
uint32 internal constant MIN_SUPPORTED_TICK_SPACING = 11;
/// @dev `IExposedStorage.sload()`: Core reads raw 32-byte slots appended
/// to the selector and returns them unencoded (`base/ExposedStorage.sol`).
bytes4 internal constant SLOAD_SELECTOR = 0x380eb4e0;
/// @dev `libraries/CoreStorageLayout.sol` FPL_OFFSET: the pool's global
/// fees per liquidity live at poolId + FPL_OFFSET and the next slot.
uint256 internal constant FPL_OFFSET =
0xb09b03866d96933565a9435bfb511c8ac5b2be454285ca331201452704799f72;
/// @dev `types/sqrtRatio.sol` BIT_MASK: the two exponent bits of the
/// 96-bit float.
uint256 internal constant SQRT_RATIO_BIT_MASK = 0xc00000000000000000000000;
error CoreReadFailed();
error StableswapPoolNotSupported(bytes32 config);
error TickSpacingNotSupported(uint32 tickSpacing);
error TickOutOfRange(int256 tick);
error SqrtPriceX96Overflow(uint256 sqrtRatioFixed);
/// @dev `types/poolKey.sol` toPoolId: keccak256 of the 96-byte key.
function poolId(
EkuboPoolKey memory key
) internal pure returns (bytes32) {
return keccak256(abi.encode(key.token0, key.token1, key.config));
}
/// @dev `types/poolConfig.sol` fee(): bits 32-95, a fraction of 2^64.
function fee(
bytes32 config
) internal pure returns (uint64) {
return uint64(uint256(config) >> 32);
}
/// @dev Converts the 2^64-based pool fee to the Uniswap uint24 convention
/// (1e6 = 100%) that `INftLiquidityConnector.fee` reports. Rounds down.
function feeToUniswapUnits(
bytes32 config
) internal pure returns (uint24) {
return uint24((uint256(fee(config)) * 1e6) >> 64);
}
/// @dev `types/poolConfig.sol`: bit 31 set marks a concentrated pool and
/// bits 0-30 hold its tick spacing. Rejects stableswap/full-range configs
/// and spacings whose scaled ticks would overflow int24.
function supportedTickSpacing(
bytes32 config
) internal pure returns (uint32 spacing) {
if (uint256(config) & 0x80000000 == 0) {
revert StableswapPoolNotSupported(config);
}
spacing = uint32(uint256(config) & 0x7fffffff);
if (spacing < MIN_SUPPORTED_TICK_SPACING) {
revert TickSpacingNotSupported(spacing);
}
}
/// @dev A Sickle tick is an Ekubo tick in tick-spacing units:
/// ekuboTick = sickleTick * spacing. Ekubo validates alignment itself;
/// this checks the protocol bounds so int32 cannot wrap.
function toEkuboTick(
int24 sickleTick,
uint32 spacing
) internal pure returns (int32) {
int256 tick = int256(sickleTick) * int256(uint256(spacing));
if (tick < MIN_TICK || tick > MAX_TICK) {
revert TickOutOfRange(tick);
}
return int32(tick);
}
/// @dev floor(ekuboTick / spacing). For a position bound (always a
/// multiple of the spacing) this is exact. For the pool tick it keeps
/// range membership: with aligned bounds a * s <= t < b * s holds exactly
/// when a <= floor(t / s) < b. Fits int24 for every supported spacing.
function toSickleTick(
int32 ekuboTick,
uint32 spacing
) internal pure returns (int24) {
int256 s = int256(uint256(spacing));
int256 q = int256(ekuboTick) / s;
if (ekuboTick < 0 && int256(ekuboTick) % s != 0) {
q -= 1;
}
return int24(q);
}
/// @dev `types/sqrtRatio.sol` toFixed: the 96-bit float as a 64.128
/// fixed point number, i.e. sqrt(token1 / token0) * 2^128.
function sqrtRatioToFixed(
uint96 sqrtRatio
) internal pure returns (uint256) {
uint256 raw = uint256(sqrtRatio);
uint256 shift = 2 + ((raw & SQRT_RATIO_BIT_MASK) >> 89);
return (raw & ~SQRT_RATIO_BIT_MASK) << shift;
}
/// @dev Uniswap's sqrtPriceX96 is the 64.128 value shifted down by 32.
/// Near Ekubo's MAX_SQRT_RATIO it needs more than 160 bits; revert rather
/// than truncate a price bound into a wrong one.
function sqrtRatioToX96(
uint96 sqrtRatio
) internal pure returns (uint160) {
uint256 fixed128 = sqrtRatioToFixed(sqrtRatio);
uint256 x96 = fixed128 >> 32;
if (x96 > type(uint160).max) {
revert SqrtPriceX96Overflow(fixed128);
}
return uint160(x96);
}
/// @dev `math/fee.sol` computeFee: amount * fee / 2^64, rounded up. This
/// is how Positions takes SWAP_PROTOCOL_FEE_X64 from collected fees.
function computeFee(
uint128 amount,
uint64 feeX64
) internal pure returns (uint128) {
return uint128(
(uint256(amount) * uint256(feeX64) + 0xffffffffffffffff) >> 64
);
}
function sload(
address core,
uint256 slot
) internal view returns (bytes32 value) {
(bool ok, bytes memory data) =
core.staticcall(abi.encodePacked(SLOAD_SELECTOR, slot));
if (!ok || data.length != 32) revert CoreReadFailed();
value = bytes32(data);
}
/// @dev `types/poolState.sol`: sqrtRatio (96 bits) | tick (int32) |
/// liquidity (128 bits), stored at slot = poolId. Zero means the pool
/// was never initialized.
function poolState(
address core,
bytes32 id
) internal view returns (uint96 sqrtRatio, int32 tick, uint128 liquidity) {
uint256 state = uint256(sload(core, uint256(id)));
sqrtRatio = uint96(state >> 160);
tick = int32(uint32(state >> 128));
liquidity = uint128(state);
}
function isInitialized(
address core,
bytes32 id
) internal view returns (bool) {
return sload(core, uint256(id)) != bytes32(0);
}
/// @dev `libraries/CoreLib.sol` getPoolFeesPerLiquidity: Q128 fees per
/// unit of liquidity, Uniswap's feeGrowthGlobalX128 equivalent.
function feesPerLiquidity(
address core,
bytes32 id
) internal view returns (uint256 value0, uint256 value1) {
uint256 slot;
unchecked {
slot = uint256(id) + FPL_OFFSET;
value0 = uint256(sload(core, slot));
value1 = uint256(sload(core, slot + 1));
}
}
}
contracts/interfaces/external/ekubo/IEkuboPositions.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @notice Ekubo v3 pool key, as `src/types/poolKey.sol` defines it.
/// `config` packs extension (bits 96-255), fee (u64, bits 32-95) and the pool
/// type (bits 0-31: bit 31 set = concentrated, bits 0-30 = tick spacing).
/// Ekubo encodes native ETH as token0 = address(0).
struct EkuboPoolKey {
address token0;
address token1;
bytes32 config;
}
/// @notice The subset of Ekubo v3 `Positions` (`src/Positions.sol`,
/// `src/base/BasePositions.sol`, `src/base/BaseNonfungibleToken.sol`,
/// `src/base/PayableMulticallable.sol`) that the Sickle integration uses. The
/// signatures match the Etherscan-verified source of
/// 0x02D9876A21AF7545f8632C3af76eC90b5ad4b66D, which is the same address on
/// every Ekubo chain.
interface IEkuboPositions {
function mintAndDepositWithSalt(
bytes32 salt,
EkuboPoolKey memory poolKey,
int32 tickLower,
int32 tickUpper,
uint128 maxAmount0,
uint128 maxAmount1,
uint128 minLiquidity
)
external
payable
returns (
uint256 id,
uint128 liquidity,
uint128 amount0,
uint128 amount1
);
function deposit(
uint256 id,
EkuboPoolKey memory poolKey,
int32 tickLower,
int32 tickUpper,
uint128 maxAmount0,
uint128 maxAmount1,
uint128 minLiquidity
)
external
payable
returns (uint128 liquidity, uint128 amount0, uint128 amount1);
function withdraw(
uint256 id,
EkuboPoolKey memory poolKey,
int32 tickLower,
int32 tickUpper,
uint128 liquidity,
address recipient,
bool withFees
) external payable returns (uint128 amount0, uint128 amount1);
function collectFees(
uint256 id,
EkuboPoolKey memory poolKey,
int32 tickLower,
int32 tickUpper,
address recipient
) external payable returns (uint128 amount0, uint128 amount1);
function getPositionFeesAndLiquidity(
uint256 id,
EkuboPoolKey memory poolKey,
int32 tickLower,
int32 tickUpper
)
external
view
returns (
uint128 liquidity,
uint128 principal0,
uint128 principal1,
uint128 fees0,
uint128 fees1
);
function burn(
uint256 id
) external payable;
function saltToId(
address minter,
bytes32 salt
) external view returns (uint256 id);
function ownerOf(
uint256 id
) external view returns (address owner);
function multicall(
bytes[] calldata data
) external payable returns (bytes[] memory results);
function refundNativeToken() external payable;
/// @notice Share of collected swap fees Positions keeps, as a fraction of
/// 2^64 (immutable).
function SWAP_PROTOCOL_FEE_X64() external view returns (uint64);
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "paris",
"remappings": [
"solmate/=lib/solmate/src/",
"@openzeppelin/=lib/openzeppelin-contracts/",
"@morpho-blue/=lib/morpho-blue/src/",
"ds-test/=lib/solmate/lib/ds-test/src/",
"forge-std/=lib/forge-std/src/",
"morpho-blue/=lib/morpho-blue/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.