Skip to content

Commit d6d6678

Browse files
committed
Add getLiquidityByAddress & getAccumulatedFee
1 parent 75ea0b9 commit d6d6678

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/UniswapV3Pool.sol

+56
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ contract UniswapV3Pool is IUniswapV3Pool {
131131
mapping(int24 => Tick.Info) public ticks;
132132
mapping(int16 => uint256) public tickBitmap;
133133
mapping(bytes32 => Position.Info) public positions;
134+
mapping(address => IUniswapV3Pool.LiquidityState[]) public liquiditiesList;
135+
address[] public liquidityProviders;
136+
134137
Oracle.Observation[65535] public observations;
135138

136139
constructor() {
@@ -246,6 +249,31 @@ contract UniswapV3Pool is IUniswapV3Pool {
246249
liquidity,
247250
params.liquidityDelta
248251
);
252+
253+
uint256 i;
254+
if (liquiditiesList[params.owner].length == 0)
255+
liquidityProviders.push(params.owner);
256+
IUniswapV3Pool.LiquidityState[]
257+
storage liquidities = liquiditiesList[params.owner];
258+
for (i = 0; i < liquidities.length; i++)
259+
if (
260+
liquidities[i].lowerTick == params.lowerTick &&
261+
liquidities[i].upperTick == params.upperTick
262+
) {
263+
liquidities[i].liquidity = LiquidityMath.addLiquidity(
264+
liquidities[i].liquidity,
265+
params.liquidityDelta
266+
);
267+
break;
268+
}
269+
if (i == liquidities.length)
270+
liquidities.push(
271+
IUniswapV3Pool.LiquidityState({
272+
lowerTick: params.lowerTick,
273+
upperTick: params.upperTick,
274+
liquidity: uint128(params.liquidityDelta)
275+
})
276+
);
249277
} else {
250278
amount1 = Math.calcAmount1Delta(
251279
TickMath.getSqrtRatioAtTick(params.lowerTick),
@@ -255,6 +283,12 @@ contract UniswapV3Pool is IUniswapV3Pool {
255283
}
256284
}
257285

286+
function getLiquidityByAddress(
287+
address owner
288+
) external view returns (IUniswapV3Pool.LiquidityState[] memory) {
289+
return liquiditiesList[owner];
290+
}
291+
258292
function mint(
259293
address owner,
260294
int24 lowerTick,
@@ -631,6 +665,28 @@ contract UniswapV3Pool is IUniswapV3Pool {
631665
}
632666
}
633667

668+
function getAccumulatedFee(
669+
address owner,
670+
int24 lowerTick,
671+
int24 upperTick
672+
) external view returns (uint256 amount0, uint256 amount1) {
673+
Position.Info storage position = positions.get(
674+
owner,
675+
lowerTick,
676+
upperTick
677+
);
678+
(uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) = ticks
679+
.getFeeGrowthInside(
680+
lowerTick,
681+
upperTick,
682+
slot0.tick,
683+
feeGrowthGlobal0X128,
684+
feeGrowthGlobal1X128
685+
);
686+
687+
(amount0, amount1) = position.calcAccumalatedAmount(feeGrowthInside0X128, feeGrowthInside1X128);
688+
}
689+
634690
////////////////////////////////////////////////////////////////////////////
635691
//
636692
// INTERNAL

src/interfaces/IUniswapV3Pool.sol

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ interface IUniswapV3Pool {
88
address payer;
99
}
1010

11+
struct LiquidityState {
12+
int24 lowerTick;
13+
int24 upperTick;
14+
uint128 liquidity;
15+
}
16+
17+
function getAccumulatedFee(
18+
address owner,
19+
int24 lowerTick,
20+
int24 upperTick
21+
) external view returns (uint256 amount0, uint256 amount1);
22+
1123
function slot0()
1224
external
1325
view
@@ -69,4 +81,7 @@ interface IUniswapV3Pool {
6981
uint160 sqrtPriceLimitX96,
7082
bytes calldata data
7183
) external returns (int256, int256);
84+
85+
function getLiquidityByAddress(address owner) external view returns (LiquidityState[] memory);
86+
7287
}

0 commit comments

Comments
 (0)