Skip to content

Nft knight branch #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/UniswapV3Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
56 changes: 56 additions & 0 deletions src/UniswapV3Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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),
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/interfaces/IUniswapV3Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,4 +81,7 @@ interface IUniswapV3Pool {
uint160 sqrtPriceLimitX96,
bytes calldata data
) external returns (int256, int256);

function getLiquidityByAddress(address owner) external view returns (LiquidityState[] memory);

}
21 changes: 21 additions & 0 deletions src/lib/Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
);
}
}