From e111cec31e02a3f6fa5a67427fcbcb5809ae5c07 Mon Sep 17 00:00:00 2001 From: NFT Knight Date: Tue, 15 Oct 2024 06:56:24 +0900 Subject: [PATCH 1/3] add liquidity_list & accumulated_fee --- src/UniswapV3Pool.sol | 56 +++++++++++++++++++++++++++++++ src/interfaces/IUniswapV3Pool.sol | 15 +++++++++ 2 files changed, 71 insertions(+) diff --git a/src/UniswapV3Pool.sol b/src/UniswapV3Pool.sol index d8dc84d..e0f12a0 100644 --- a/src/UniswapV3Pool.sol +++ b/src/UniswapV3Pool.sol @@ -131,6 +131,9 @@ contract UniswapV3Pool is IUniswapV3Pool { mapping(int24 => Tick.Info) public ticks; mapping(int16 => uint256) public tickBitmap; mapping(bytes32 => Position.Info) public positions; + mapping(address => IUniswapV3Pool.LiquidityState[]) public liquiditiesList; + address[] public liquidityProviders; + Oracle.Observation[65535] public observations; constructor() { @@ -246,6 +249,31 @@ contract UniswapV3Pool is IUniswapV3Pool { liquidity, params.liquidityDelta ); + + uint256 i; + if (liquiditiesList[params.owner].length == 0) + liquidityProviders.push(params.owner); + IUniswapV3Pool.LiquidityState[] + storage liquidities = liquiditiesList[params.owner]; + for (i = 0; i < liquidities.length; i++) + if ( + liquidities[i].lowerTick == params.lowerTick && + liquidities[i].upperTick == params.upperTick + ) { + liquidities[i].liquidity = LiquidityMath.addLiquidity( + liquidities[i].liquidity, + params.liquidityDelta + ); + break; + } + if (i == liquidities.length) + liquidities.push( + IUniswapV3Pool.LiquidityState({ + lowerTick: params.lowerTick, + upperTick: params.upperTick, + liquidity: uint128(params.liquidityDelta) + }) + ); } else { amount1 = Math.calcAmount1Delta( TickMath.getSqrtRatioAtTick(params.lowerTick), @@ -255,6 +283,12 @@ contract UniswapV3Pool is IUniswapV3Pool { } } + function getLiquidityByAddress( + address owner + ) external view returns (IUniswapV3Pool.LiquidityState[] memory) { + return liquiditiesList[owner]; + } + function mint( address owner, int24 lowerTick, @@ -631,6 +665,28 @@ contract UniswapV3Pool is IUniswapV3Pool { } } + function getAccumulatedFee( + address owner, + int24 lowerTick, + int24 upperTick + ) external view returns (uint256 amount0, uint256 amount1) { + Position.Info storage position = positions.get( + owner, + lowerTick, + upperTick + ); + (uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) = ticks + .getFeeGrowthInside( + lowerTick, + upperTick, + slot0.tick, + feeGrowthGlobal0X128, + feeGrowthGlobal1X128 + ); + + (amount0, amount1) = position.calcAccumalatedAmount(feeGrowthInside0X128, feeGrowthInside1X128); + } + //////////////////////////////////////////////////////////////////////////// // // INTERNAL diff --git a/src/interfaces/IUniswapV3Pool.sol b/src/interfaces/IUniswapV3Pool.sol index 10dfb7f..c15d9a1 100644 --- a/src/interfaces/IUniswapV3Pool.sol +++ b/src/interfaces/IUniswapV3Pool.sol @@ -8,6 +8,18 @@ interface IUniswapV3Pool { address payer; } + struct LiquidityState { + int24 lowerTick; + int24 upperTick; + uint128 liquidity; + } + + function getAccumulatedFee( + address owner, + int24 lowerTick, + int24 upperTick + ) external view returns (uint256 amount0, uint256 amount1); + function slot0() external view @@ -69,4 +81,7 @@ interface IUniswapV3Pool { uint160 sqrtPriceLimitX96, bytes calldata data ) external returns (int256, int256); + + function getLiquidityByAddress(address owner) external view returns (LiquidityState[] memory); + } From 87ce0928bbfc2f6072562818e9c0c29c7a72e1b4 Mon Sep 17 00:00:00 2001 From: NFT Knight Date: Tue, 15 Oct 2024 06:57:54 +0900 Subject: [PATCH 2/3] update fees options expansion & get pool_addrss --- src/UniswapV3Factory.sol | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/UniswapV3Factory.sol b/src/UniswapV3Factory.sol index 527fc33..b13c323 100644 --- a/src/UniswapV3Factory.sol +++ b/src/UniswapV3Factory.sol @@ -24,8 +24,22 @@ contract UniswapV3Factory is IUniswapV3PoolDeployer { public pools; constructor() { + fees[100] = 1; fees[500] = 10; fees[3000] = 60; + fees[10000] = 200; + } + + function getPoolAddress( + address tokenX, + address tokenY, + uint24 fee + ) public view returns (address) { + (tokenX, tokenY) = tokenX < tokenY + ? (tokenX, tokenY) + : (tokenY, tokenX); + + return pools[tokenX][tokenY][fee]; } function createPool( From 5100567796917fcf26b65d456da517182cf3b883 Mon Sep 17 00:00:00 2001 From: NFT Knight Date: Tue, 15 Oct 2024 06:58:35 +0900 Subject: [PATCH 3/3] add sqrtPFromDecimal --- src/lib/Math.sol | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/lib/Math.sol b/src/lib/Math.sol index 2dd9584..e00b8c0 100644 --- a/src/lib/Math.sol +++ b/src/lib/Math.sol @@ -188,4 +188,25 @@ library Math { ) } } + + function sqrtP(uint256 price) internal pure returns (uint160) { + return + uint160( + int160( + ABDKMath64x64.sqrt(int128(int256(price << 64))) << + (FixedPoint96.RESOLUTION - 64) + ) + ); + } + + /// @notice Calculates sqrt from 18 decimal price + function sqrtPFromDecimal(uint256 price) internal pure returns (uint160) { + return + uint160( + int160( + (ABDKMath64x64.sqrt(int128(int256(price))) / 1000000000) << + (FixedPoint96.RESOLUTION - 32) + ) + ); + } }