harb/src/LiquidityManager.sol

261 lines
10 KiB
Solidity
Raw Normal View History

2023-11-23 22:14:47 +01:00
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.20;
2023-11-25 22:06:38 +01:00
import {BloodX} from 'src/BloodX.sol';
import '@uniswap/v3-core/contracts/libraries/TickMath.sol';
import '@uniswap/v3-periphery/contracts/libraries/LiquidityAmounts.sol';
import '@uniswap/v3-periphery/contracts/libraries/PoolAddress.sol';
import '@uniswap/v3-periphery/contracts/libraries/PositionKey.sol';
import '@uniswap/v3-core/contracts/libraries/FixedPoint128.sol';
import '@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol';
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
2023-11-23 22:14:47 +01:00
/**
* @title LiquidityManager - A contract that supports the $bloodX ecosystem. It
* protects the communities liquidity while allowing a manager role to
* take strategic liqudity positions.
*/
2023-11-25 22:06:38 +01:00
contract LiquidityManager {
2023-11-23 22:14:47 +01:00
// default fee of 1%
uint24 constant FEE = uint24(10_000);
2023-11-25 22:06:38 +01:00
// the address of the Uniswap V3 factory
address public immutable factory;
// the address of WETH9
address public immutable WETH9;
2023-11-23 22:14:47 +01:00
struct AddLiquidityParams {
address token0;
address token1;
int24 tickLower;
int24 tickUpper;
uint256 amount0Desired;
uint256 amount1Desired;
uint256 amount0Min;
uint256 amount1Min;
uint256 deadline;
}
struct DecreaseLiquidityParams {
address token0;
address token1;
int24 tickLower;
int24 tickUpper;
uint128 liquidity;
uint256 amount0Min;
uint256 amount1Min;
uint256 deadline;
}
struct TokenPosition {
// the liquidity of the position
uint128 liquidity;
// the fee growth of the aggregate position as of the last action on the individual position
uint256 feeGrowthInside0LastX128;
uint256 feeGrowthInside1LastX128;
}
2023-11-25 22:06:38 +01:00
// uint256 = uint128 tokensOwed + uint128 ethOwed
mapping(address => uint256) private _feesOwed;
2023-11-25 19:47:37 +01:00
2023-11-23 22:14:47 +01:00
/// @dev The token ID position data
2023-11-25 22:06:38 +01:00
mapping(bytes26 => TokenPosition) private _positions;
modifier checkDeadline(uint256 deadline) {
require(block.timestamp <= deadline, 'Transaction too old');
_;
}
/// @notice Emitted when liquidity is increased for a position
/// @param liquidity The amount by which liquidity for the NFT position was increased
/// @param amount0 The amount of token0 that was paid for the increase in liquidity
/// @param amount1 The amount of token1 that was paid for the increase in liquidity
event IncreaseLiquidity(address indexed token, uint128 liquidity, uint256 amount0, uint256 amount1);
/// @notice Emitted when liquidity is decreased for a position
/// @param liquidity The amount by which liquidity for the NFT position was decreased
/// @param amount0 The amount of token0 that was accounted for the decrease in liquidity
/// @param amount1 The amount of token1 that was accounted for the decrease in liquidity
event DecreaseLiquidity(address indexed token, uint128 liquidity, uint256 amount0, uint256 amount1);
2023-11-23 22:14:47 +01:00
2023-11-25 20:59:15 +01:00
constructor(
address _factory,
address _WETH9
) {
2023-11-25 22:06:38 +01:00
factory = _factory;
WETH9 = _WETH9;
2023-11-23 22:14:47 +01:00
}
2023-11-25 22:06:38 +01:00
function getToken(address token0, address token1) internal view returns (bool token0isWeth, address token) {
token0isWeth = (token0 == WETH9);
require(token0isWeth || token1 == WETH9, "token error");
token = (token0isWeth) ? token1 : token0;
2023-11-23 22:14:47 +01:00
}
2023-11-25 19:47:37 +01:00
function getFeesOwed(address token) public view returns (uint128 tokensOwed, uint128 ethOwed) {
2023-11-25 22:06:38 +01:00
uint256 feesOwed = _feesOwed[token];
return (uint128(feesOwed >> 128), uint128(feesOwed));
2023-11-25 19:47:37 +01:00
}
function updateFeesOwed(bool token0isEth, address token, uint128 tokensOwed0, uint128 tokensOwed1) internal {
2023-11-25 22:06:38 +01:00
(uint128 tokensOwed, uint128 ethOwed) = getFeesOwed(token);
tokensOwed += token0isEth ? tokensOwed1 : tokensOwed0;
ethOwed += token0isEth ? tokensOwed0 : tokensOwed1;
_feesOwed[token] = uint256(tokensOwed) << 128 + ethOwed;
2023-11-25 19:47:37 +01:00
}
2023-11-25 22:06:38 +01:00
function posKey(address token, int24 tickLower, int24 tickUpper) internal pure returns (bytes26 _posKey) {
bytes memory _posKeyBytes = abi.encodePacked(token, tickLower, tickUpper);
assembly {
_posKey := mload(add(_posKeyBytes, 26))
}
2023-11-23 22:14:47 +01:00
}
2023-11-25 20:59:15 +01:00
function positions(address token, int24 tickLower, int24 tickUpper) external view
2023-11-23 22:14:47 +01:00
returns (
uint128 liquidity,
uint256 feeGrowthInside0LastX128,
2023-11-25 19:47:37 +01:00
uint256 feeGrowthInside1LastX128
2023-11-23 22:14:47 +01:00
)
{
2023-11-25 20:59:15 +01:00
TokenPosition memory position = _positions[posKey(token, tickLower, tickUpper)];
2023-11-23 22:14:47 +01:00
return (
position.liquidity,
position.feeGrowthInside0LastX128,
2023-11-25 19:47:37 +01:00
position.feeGrowthInside1LastX128
2023-11-23 22:14:47 +01:00
);
}
function uniswapV3MintCallback(
uint256 amount0Owed,
uint256 amount1Owed,
bytes calldata data
2023-11-25 22:06:38 +01:00
) external {
2023-11-23 22:14:47 +01:00
PoolAddress.PoolKey memory poolKey = abi.decode(data, (PoolAddress.PoolKey));
2023-11-25 22:06:38 +01:00
IUniswapV3Pool pool = IUniswapV3Pool(PoolAddress.computeAddress(factory, poolKey));
require(msg.sender == address(pool));
2023-11-23 22:14:47 +01:00
2023-11-25 22:06:38 +01:00
if (amount0Owed > 0) IERC20(poolKey.token0).transfer(msg.sender, amount0Owed);
if (amount1Owed > 0) IERC20(poolKey.token1).transfer(msg.sender, amount1Owed);
2023-11-23 22:14:47 +01:00
}
/// @notice Add liquidity to an initialized pool
2023-11-25 22:15:04 +01:00
function addLiquidity(AddLiquidityParams memory params) external checkDeadline(params.deadline) {
2023-11-23 22:14:47 +01:00
PoolAddress.PoolKey memory poolKey =
PoolAddress.PoolKey({token0: params.token0, token1: params.token1, fee: FEE});
2023-11-25 22:06:38 +01:00
IUniswapV3Pool pool = IUniswapV3Pool(PoolAddress.computeAddress(factory, poolKey));
2023-11-23 22:14:47 +01:00
// compute the liquidity amount
2023-11-25 22:15:04 +01:00
uint128 liquidity;
2023-11-23 22:14:47 +01:00
{
(uint160 sqrtPriceX96, , , , , , ) = pool.slot0();
uint160 sqrtRatioAX96 = TickMath.getSqrtRatioAtTick(params.tickLower);
uint160 sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(params.tickUpper);
liquidity = LiquidityAmounts.getLiquidityForAmounts(
sqrtPriceX96,
sqrtRatioAX96,
sqrtRatioBX96,
params.amount0Desired,
params.amount1Desired
);
}
2023-11-25 22:15:04 +01:00
(bool token0isWeth, address token) = getToken(params.token0, params.token1);
{
(uint256 amount0, uint256 amount1) = pool.mint(
address(this),
params.tickLower,
params.tickUpper,
liquidity,
abi.encode(poolKey)
);
2023-11-23 22:14:47 +01:00
2023-11-25 22:15:04 +01:00
require(amount0 >= params.amount0Min && amount1 >= params.amount1Min, 'Price slippage check');
emit IncreaseLiquidity(token, liquidity, amount0, amount1);
}
2023-11-23 22:14:47 +01:00
bytes32 positionKey = PositionKey.compute(address(this), params.tickLower, params.tickUpper);
// this is now updated to the current transaction
(, uint256 feeGrowthInside0LastX128, uint256 feeGrowthInside1LastX128, , ) = pool.positions(positionKey);
2023-11-25 22:06:38 +01:00
TokenPosition memory position = _positions[posKey(token, params.tickLower, params.tickUpper)];
2023-11-23 22:14:47 +01:00
if (liquidity == 0) {
// create entry
position = TokenPosition({
liquidity: liquidity,
feeGrowthInside0LastX128: feeGrowthInside0LastX128,
2023-11-25 19:47:37 +01:00
feeGrowthInside1LastX128: feeGrowthInside1LastX128
2023-11-23 22:14:47 +01:00
});
} else {
// update entry
2023-11-25 22:06:38 +01:00
updateFeesOwed(token0isWeth, token, uint128(
2023-11-23 22:14:47 +01:00
FullMath.mulDiv(
feeGrowthInside0LastX128 - position.feeGrowthInside0LastX128,
position.liquidity,
FixedPoint128.Q128
)
2023-11-25 19:47:37 +01:00
), uint128(
2023-11-23 22:14:47 +01:00
FullMath.mulDiv(
feeGrowthInside1LastX128 - position.feeGrowthInside1LastX128,
position.liquidity,
FixedPoint128.Q128
)
2023-11-25 19:47:37 +01:00
));
2023-11-23 22:14:47 +01:00
position.feeGrowthInside0LastX128 = feeGrowthInside0LastX128;
position.feeGrowthInside1LastX128 = feeGrowthInside1LastX128;
position.liquidity += liquidity;
}
}
function decreaseLiquidity(DecreaseLiquidityParams calldata params)
external
checkDeadline(params.deadline)
returns (uint256 amount0, uint256 amount1)
{
require(params.liquidity > 0);
2023-11-25 19:47:37 +01:00
(bool token0isWeth, address token) = getToken(params.token0, params.token1);
2023-11-25 22:06:38 +01:00
TokenPosition memory position = _positions[posKey(token, params.tickLower, params.tickUpper)];
2023-11-23 22:14:47 +01:00
uint128 positionLiquidity = position.liquidity;
require(positionLiquidity >= params.liquidity);
PoolAddress.PoolKey memory poolKey =
PoolAddress.PoolKey({token0: params.token0, token1: params.token1, fee: FEE});
2023-11-25 22:06:38 +01:00
IUniswapV3Pool pool = IUniswapV3Pool(PoolAddress.computeAddress(factory, poolKey));
2023-11-23 22:14:47 +01:00
(amount0, amount1) = pool.burn(params.tickLower, params.tickUpper, params.liquidity);
require(amount0 >= params.amount0Min && amount1 >= params.amount1Min, 'Price slippage check');
bytes32 positionKey = PositionKey.compute(address(this), params.tickLower, params.tickUpper);
// this is now updated to the current transaction
(, uint256 feeGrowthInside0LastX128, uint256 feeGrowthInside1LastX128, , ) = pool.positions(positionKey);
2023-11-25 22:06:38 +01:00
updateFeesOwed(token0isWeth, token, uint128(amount0) +
2023-11-23 22:14:47 +01:00
uint128(
FullMath.mulDiv(
feeGrowthInside0LastX128 - position.feeGrowthInside0LastX128,
positionLiquidity,
FixedPoint128.Q128
)
2023-11-25 19:47:37 +01:00
), uint128(amount1) +
2023-11-23 22:14:47 +01:00
uint128(
FullMath.mulDiv(
feeGrowthInside1LastX128 - position.feeGrowthInside1LastX128,
positionLiquidity,
FixedPoint128.Q128
)
2023-11-25 19:47:37 +01:00
));
2023-11-23 22:14:47 +01:00
position.feeGrowthInside0LastX128 = feeGrowthInside0LastX128;
position.feeGrowthInside1LastX128 = feeGrowthInside1LastX128;
// subtraction is safe because we checked positionLiquidity is gte params.liquidity
position.liquidity = positionLiquidity - params.liquidity;
2023-11-25 22:06:38 +01:00
emit DecreaseLiquidity(token, params.liquidity, amount0, amount1);
2023-11-23 22:14:47 +01:00
}
}