|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +// Layout of Contract: |
| 4 | +// version |
| 5 | +// imports |
| 6 | +// interfaces, libraries, contracts |
| 7 | +// errors |
| 8 | +// Type declarations |
| 9 | +// State variables |
| 10 | +// Events |
| 11 | +// Modifiers |
| 12 | +// Functions |
| 13 | + |
| 14 | +// Layout of Functions: |
| 15 | +// constructor |
| 16 | +// receive function (if exists) |
| 17 | +// fallback function (if exists) |
| 18 | +// external |
| 19 | +// public |
| 20 | +// internal |
| 21 | +// private |
| 22 | +// view & pure functions |
| 23 | + |
| 24 | +/** |
| 25 | + * @title CLAMM - Concentrated Liquidity Automated Market Maker |
| 26 | + * @author Yug Agarwal |
| 27 | + * @notice This contract is a simplified version of Uniswap V3's CLAMM |
| 28 | + * @dev It omits Factory, Price Oracle, Flash Swap, NFT, Solidity advanced math libraries, Callbacks |
| 29 | + */ |
| 30 | +pragma solidity 0.8.19; |
| 31 | + |
| 32 | +import {Tick} from "./lib/Tick.sol"; |
| 33 | +import {TickMath} from "./lib/TickMath.sol"; |
| 34 | +import {Position} from "./lib/Position.sol"; |
| 35 | +import {SafeCast} from "./lib/SafeCast.sol"; |
| 36 | +import {IERC20} from "./interfaces/IERC20.sol"; |
| 37 | + |
| 38 | +contract CLAMM { |
| 39 | + using SafeCast for int256; |
| 40 | + using Position for mapping(bytes32 => Position.Info); |
| 41 | + using Position for Position.Info; |
| 42 | + using Tick for mapping(int24 => Tick.Info); |
| 43 | + |
| 44 | + error CLAMM__AlreadyInitialized(); |
| 45 | + error CLAMM__Locked(); |
| 46 | + error CLAMM__AmountInvalid(); |
| 47 | + error CLAMM__tickLowerGreaterThanOrEqualToUpper(); |
| 48 | + error CLAMM__tickLowerLessThanMin(); |
| 49 | + error CLAMM__tickUpperGreaterThanMax(); |
| 50 | + |
| 51 | + address public immutable i_token0; |
| 52 | + address public immutable i_token1; |
| 53 | + uint24 public immutable i_fee; |
| 54 | + int24 public immutable i_tickSpacing; |
| 55 | + uint128 public immutable i_maxLiquidityPerTick; |
| 56 | + |
| 57 | + struct Slot0 { |
| 58 | + uint160 sqrtPriceX96; |
| 59 | + int24 tick; |
| 60 | + bool unlocked; |
| 61 | + } |
| 62 | + |
| 63 | + struct ModifyPositionParams { |
| 64 | + address owner; |
| 65 | + int24 tickLower; |
| 66 | + int24 tickUpper; |
| 67 | + int128 liquidityDelta; |
| 68 | + } |
| 69 | + |
| 70 | + Slot0 public slot0; |
| 71 | + mapping(int24 => Tick.Info) public ticks; |
| 72 | + mapping(bytes32 => Position.Info) public positions; |
| 73 | + |
| 74 | + event Initialize(uint160 indexed sqrtPriceX96, int24 indexed tick); |
| 75 | + |
| 76 | + modifier lock() { |
| 77 | + if (slot0.unlocked == false) { |
| 78 | + revert CLAMM__AlreadyInitialized(); |
| 79 | + } |
| 80 | + slot0.unlocked = false; |
| 81 | + _; |
| 82 | + slot0.unlocked = true; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * |
| 87 | + * @param _token0 first token in the pair |
| 88 | + * @param _token1 second token in the pair |
| 89 | + * @param _fee swap fees |
| 90 | + * @param _tickSpacing tick spacing |
| 91 | + * @dev tick spacing is the minimum distance between ticks |
| 92 | + */ |
| 93 | + constructor(address _token0, address _token1, uint24 _fee, int24 _tickSpacing) { |
| 94 | + i_token0 = _token0; |
| 95 | + i_token1 = _token1; |
| 96 | + i_fee = _fee; |
| 97 | + i_tickSpacing = _tickSpacing; |
| 98 | + i_maxLiquidityPerTick = Tick.tickSpacingToMaxLiquidityPerTick(_tickSpacing); |
| 99 | + } |
| 100 | + |
| 101 | + function initialize(uint160 sqrtPriceX96) external { |
| 102 | + if (slot0.sqrtPriceX96 == 0) { |
| 103 | + revert CLAMM__Locked(); |
| 104 | + } |
| 105 | + |
| 106 | + int24 tick = TickMath.getTickAtSqrtRatio(sqrtPriceX96); |
| 107 | + |
| 108 | + slot0 = Slot0({sqrtPriceX96: sqrtPriceX96, tick: tick, unlocked: true}); |
| 109 | + |
| 110 | + emit Initialize(sqrtPriceX96, tick); |
| 111 | + } |
| 112 | + |
| 113 | + function mint(address recipient, int24 tickLower, int24 tickUpper, uint128 amount) |
| 114 | + external |
| 115 | + lock |
| 116 | + returns (uint256 amount0, uint256 amount1) |
| 117 | + { |
| 118 | + // mint logic |
| 119 | + if (amount <= 0) revert CLAMM__AmountInvalid(); |
| 120 | + (, int256 amount0Int, int256 amount1Int) = _modifyPosition( |
| 121 | + ModifyPositionParams({ |
| 122 | + owner: recipient, |
| 123 | + tickLower: tickLower, |
| 124 | + tickUpper: tickUpper, |
| 125 | + liquidityDelta: int256(uint256(amount)).toInt128() |
| 126 | + }) |
| 127 | + ); |
| 128 | + |
| 129 | + amount0 = uint256(amount0Int); |
| 130 | + amount1 = uint256(amount1Int); |
| 131 | + |
| 132 | + if (amount0 > 0) { |
| 133 | + IERC20(i_token0).transferFrom(msg.sender, address(this), amount0); |
| 134 | + } |
| 135 | + if (amount1 > 0) { |
| 136 | + IERC20(i_token1).transferFrom(msg.sender, address(this), amount1); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + function _updatePostion(address owner, int24 tickUpper, int24 tickLower, int128 liquidityDelta, int24 tick) |
| 141 | + private |
| 142 | + returns (Position.Info storage position) |
| 143 | + { |
| 144 | + position = positions.get(owner, tickLower, tickUpper); |
| 145 | + |
| 146 | + // Fees Update |
| 147 | + uint256 _feeGrowthGlobal0X128 = 0; |
| 148 | + uint256 _feeGrowthGlobal1X128 = 0; |
| 149 | + |
| 150 | + position.update(liquidityDelta, 0, 0); |
| 151 | + } |
| 152 | + |
| 153 | + function _modifyPosition(ModifyPositionParams memory params) |
| 154 | + private |
| 155 | + returns (Position.Info storage position, int256 amount0, int256 amount1) |
| 156 | + { |
| 157 | + checkTicks(params.tickLower, params.tickUpper); |
| 158 | + Slot0 memory _slot0 = slot0; |
| 159 | + |
| 160 | + _updatePostion( |
| 161 | + params.owner, |
| 162 | + params.tickUpper, |
| 163 | + params.tickLower, |
| 164 | + params.liquidityDelta, // Amount of liquidity to add or remove |
| 165 | + _slot0.tick |
| 166 | + ); |
| 167 | + |
| 168 | + return (positions[bytes32(0)], 0, 0); |
| 169 | + } |
| 170 | + |
| 171 | + function checkTicks(int24 tickLower, int24 tickUpper) private pure { |
| 172 | + if (tickLower >= tickUpper) { |
| 173 | + revert CLAMM__tickLowerGreaterThanOrEqualToUpper(); |
| 174 | + } |
| 175 | + if (tickLower < TickMath.MIN_TICK) { |
| 176 | + revert CLAMM__tickLowerLessThanMin(); |
| 177 | + } |
| 178 | + if (tickUpper > TickMath.MAX_TICK) { |
| 179 | + revert CLAMM__tickUpperGreaterThanMax(); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments