// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.8.15;
import { Owned } from "@solmate/auth/Owned.sol";
import { Pausable } from "@openzeppelin/contracts/security/Pausable.sol";
/// @title AccessControl
/// @notice Contain helper methods for accessibility of functions
abstract contract AccessControl is Owned, Pausable {
uint32 private _unlocked = 1;
mapping(address => bool) internal _operatorApproved;
modifier onlyOperator() {
require(_operatorApproved[msg.sender]);
_;
}
modifier nonReentrancy() {
require(_unlocked == 1);
_unlocked = 2;
_;
_unlocked = 1;
}
constructor(address _owner) Owned(_owner) { }
/// @notice Updates the status of given account as operator
/// @dev Must be called by the current governance
/// @param _operator Account to update status
function toggleOperator(address _operator) external onlyOwner {
_operatorApproved[_operator] = !_operatorApproved[_operator];
}
/// @notice Returns the status for a given operator that can execute operations
/// @param _operator Account to check status
function isOperator(address _operator) external view returns (bool) {
return _operatorApproved[_operator];
}
/// @dev Triggers stopped state.
function pause() external onlyOwner {
_pause();
}
/// @dev Returns to normal state.
function unpause() external onlyOwner {
_unpause();
}
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.8.15;
import { ICLTBase } from "./interfaces/ICLTBase.sol";
import { ICLTModules } from "./interfaces/ICLTModules.sol";
import { IGovernanceFeeHandler } from "./interfaces/IGovernanceFeeHandler.sol";
import { CLTPayments } from "./base/CLTPayments.sol";
import { AccessControl } from "./base/AccessControl.sol";
import { Position } from "./libraries/Position.sol";
import { Constants } from "./libraries/Constants.sol";
import { PoolActions } from "./libraries/PoolActions.sol";
import { UserPositions } from "./libraries/UserPositions.sol";
import { TransferHelper } from "./libraries/TransferHelper.sol";
import { LiquidityShares } from "./libraries/LiquidityShares.sol";
import { StrategyFeeShares } from "./libraries/StrategyFeeShares.sol";
import { ERC721 } from "@solmate/tokens/ERC721.sol";
import { FullMath } from "@uniswap/v3-core/contracts/libraries/FullMath.sol";
import { IUniswapV3Factory } from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Factory.sol";
/// @title A51 Finance Autonomus Liquidity Provision Base Contract
/// @author 0xMudassir
/// @notice The A51 ALP Base facilitates the liquidity strategies on concentrated AMM with dynamic adjustments based on
/// user preferences with the help of basic and advance liquidity modes
/// Holds the state for all strategies and it's users
contract CLTBase is ICLTBase, AccessControl, CLTPayments, ERC721 {
using Position for StrategyData;
using UserPositions for UserPositions.Data;
uint256 private _sharesId = 1;
uint256 private _strategyId = 1;
/// @notice The address of modes managment of strategy
address public immutable cltModules;
/// @notice The address of fee managment of strategy
address public immutable feeHandler;
/// @inheritdoc ICLTBase
mapping(bytes32 => StrategyData) public override strategies;
/// @inheritdoc ICLTBase
mapping(uint256 => UserPositions.Data) public override positions;
/// @notice The global fee growth as of last action on individual liquidity position in pool
/// @dev The uncollected fee earned by individual position is first collected by global account and then distributed
/// among the strategies having same ticks as of global account ticks according to the strategy fee growth & share
mapping(bytes32 => StrategyFeeShares.GlobalAccount) private strategyGlobalFees;
address private constant BLAST_POINTS = 0x2536FE9ab3F511540F2f9e2eC2A805005C3Dd800;
modifier isAuthorizedForToken(uint256 tokenId) {
_authorization(tokenId);
_;
}
constructor(
string memory _name,
string memory _symbol,
address _owner,
address _weth9,
address _feeHandler,
address _cltModules,
IUniswapV3Factory _factory
)
AccessControl(_owner)
ERC721(_name, _symbol)
CLTPayments(_factory, _weth9)
{
cltModules = _cltModules;
feeHandler = _feeHandler;
}
/// @inheritdoc ICLTBase
function createStrategy(
StrategyKey calldata key,
PositionActions calldata actions,
uint256 managementFee,
uint256 performanceFee,
bool isCompound,
bool isPrivate
)
external
payable
override
{
_validateModes(actions, managementFee, performanceFee);
bytes memory positionActionsHash = abi.encode(actions);
bytes32 strategyID = keccak256(abi.encode(_msgSender(), _strategyId++));
strategies[strategyID] = StrategyData({
key: key,
owner: _msgSender(),
actions: positionActionsHash,
actionStatus: "",
isCompound: isCompound,
isPrivate: isPrivate,
managementFee: managementFee,
performanceFee: performanceFee,
account: Account({
fee0: 0,
fee1: 0,
balance0: 0,
balance1: 0,
totalShares: 0,
uniswapLiquidity: 0,
feeGrowthInside0LastX128: 0,
feeGrowthInside1LastX128: 0,
feeGrowthOutside0LastX128: 0,
feeGrowthOutside1LastX128: 0
})
});
(, uint256 strategyCreationFeeAmount,,) = _getGovernanceFee(isPrivate);
if (strategyCreationFeeAmount > 0) TransferHelper.safeTransferETH(owner, strategyCreationFeeAmount);
refundETH();
emit StrategyCreated(strategyID);
}
/// @inheritdoc ICLTBase
function deposit(DepositParams calldata params)
external
payable
override
nonReentrancy
whenNotPaused
returns (uint256 tokenId, uint256 share, uint256 amount0, uint256 amount1)
{
_authorizationOfStrategy(params.strategyId);
uint256 feeGrowthInside0LastX128;
uint256 feeGrowthInside1LastX128;
(share, amount0, amount1, feeGrowthInside0LastX128, feeGrowthInside1LastX128) = _deposit(
params.strategyId, params.amount0Desired, params.amount1Desired, params.amount0Min, params.amount1Min
);
_mint(params.recipient, (tokenId = _sharesId++));
positions[tokenId] = UserPositions.Data({
strategyId: params.strategyId,
liquidityShare: share,
feeGrowthInside0LastX128: feeGrowthInside0LastX128,
feeGrowthInside1LastX128: feeGrowthInside1LastX128,
tokensOwed0: 0,
tokensOwed1: 0
});
emit Deposit(tokenId, params.recipient, share, amount0, amount1);
}
/// @inheritdoc ICLTBase
function updatePositionLiquidity(UpdatePositionParams calldata params)
external
payable
override
nonReentrancy
whenNotPaused
returns (uint256 share, uint256 amount0, uint256 amount1)
{
UserPositions.Data storage position = positions[params.tokenId];
bytes32 strategyId = position.strategyId;
_authorizationOfStrategy(strategyId);
uint256 feeGrowthInside0LastX128;
uint256 feeGrowthInside1LastX128;
(share, amount0, amount1, feeGrowthInside0LastX128, feeGrowthInside1LastX128) =
_deposit(strategyId, params.amount0Desired, params.amount1Desired, params.amount0Min, params.amount1Min);
if (!strategies[strategyId].isCompound) {
position.updateUserPosition(feeGrowthInside0LastX128, feeGrowthInside1LastX128);
}
position.liquidityShare += share;
emit PositionUpdated(params.tokenId, share, amount0, amount1);
}
/// @inheritdoc ICLTBase
function withdraw(WithdrawParams calldata params)
external
override
nonReentrancy
isAuthorizedForToken(params.tokenId)
returns (uint256 amount0, uint256 amount1)
{
UserPositions.Data storage position = positions[params.tokenId];
StrategyData storage strategy = strategies[position.strategyId];
StrategyFeeShares.GlobalAccount storage global = _updateGlobals(strategy, position.strategyId);
if (params.liquidity == 0) revert InvalidShare();
if (position.liquidityShare == 0) revert NoLiquidity();
if (position.liquidityShare < params.liquidity) revert InvalidShare();
// these vars used for multipurpose || strategist fee & contract balance
Account memory vars;
(vars.uniswapLiquidity, amount0, amount1,,) = PoolActions.burnUserLiquidity(
strategy.key,
strategy.account.uniswapLiquidity,
FullMath.mulDiv(params.liquidity, 1e18, strategy.account.totalShares)
);
if (!strategy.isCompound) {
(vars.fee0, vars.fee1) = position.claimFeeForNonCompounders(strategy);
} else {
(vars.fee0, vars.fee1) = position.claimFeeForCompounders(strategy);
}
// deduct any fees if required for strategist
IGovernanceFeeHandler.ProtocolFeeRegistry memory protocolFee;
(,, protocolFee.protcolFeeOnManagement, protocolFee.protcolFeeOnPerformance) =
_getGovernanceFee(strategy.isPrivate);
(vars.balance0, vars.balance1) = transferFee(
strategy.key,
protocolFee.protcolFeeOnPerformance,
strategy.performanceFee,
vars.fee0,
vars.fee1,
owner,
strategy.owner
);
vars.fee0 -= vars.balance0;
vars.fee1 -= vars.balance1;
(vars.balance0, vars.balance1) = transferFee(
strategy.key,
protocolFee.protcolFeeOnManagement,
strategy.managementFee,
amount0,
amount1,
owner,
strategy.owner
);
amount0 -= vars.balance0;
amount1 -= vars.balance1;
// should calculate correct amounts for both compounders & non-compounders
uint256 userShare0 = FullMath.mulDiv(strategy.account.balance0, params.liquidity, strategy.account.totalShares);
uint256 userShare1 = FullMath.mulDiv(strategy.account.balance1, params.liquidity, strategy.account.totalShares);
amount0 += userShare0 + vars.fee0;
amount1 += userShare1 + vars.fee1;
strategy.account.balance0 -= userShare0;
strategy.account.balance1 -= userShare1;
if (!strategy.isCompound) {
position.tokensOwed0 = 0;
position.tokensOwed1 = 0;
}
if (amount0 < params.amount0Min || amount1 < params.amount1Min) revert MinimumAmountsExceeded();
if (amount0 > 0) {
transferFunds(params.refundAsETH, params.recipient, strategy.key.pool.token0(), amount0);
}
if (amount1 > 0) {
transferFunds(params.refundAsETH, params.recipient, strategy.key.pool.token1(), amount1);
}
if (strategy.getHodlStatus() == false) global.totalLiquidity -= params.liquidity;
position.liquidityShare -= params.liquidity;
strategy.account.totalShares -= params.liquidity;
strategy.account.uniswapLiquidity -= vars.uniswapLiquidity;
emit Withdraw(params.tokenId, params.recipient, params.liquidity, amount0, amount1, vars.fee0, vars.fee1);
}
/// @inheritdoc ICLTBase
function claimPositionFee(ClaimFeesParams calldata params)
external
override
nonReentrancy
whenNotPaused
isAuthorizedForToken(params.tokenId)
{
UserPositions.Data storage position = positions[params.tokenId];
StrategyData storage strategy = strategies[position.strategyId];
_updateGlobals(strategy, position.strategyId);
if (strategy.isCompound) revert onlyNonCompounders();
if (position.liquidityShare == 0) revert NoLiquidity();
(uint128 tokensOwed0, uint128 tokensOwed1) = position.claimFeeForNonCompounders(strategy);
(,,, uint256 protcolFeeOnPerformance) = _getGovernanceFee(strategy.isPrivate);
(uint256 fee0, uint256 fee1) = transferFee(
strategy.key,
protcolFeeOnPerformance,
strategy.performanceFee,
tokensOwed0,
tokensOwed1,
owner,
strategy.owner
);
if (tokensOwed0 > 0) {
transferFunds(params.refundAsETH, params.recipient, strategy.key.pool.token0(), tokensOwed0 - fee0);
}
if (tokensOwed1 > 0) {
transferFunds(params.refundAsETH, params.recipient, strategy.key.pool.token1(), tokensOwed1 - fee1);
}
position.tokensOwed0 = 0;
position.tokensOwed1 = 0;
emit Collect(params.tokenId, params.recipient, fee0, fee1);
}
/// @inheritdoc ICLTBase
function shiftLiquidity(ShiftLiquidityParams calldata params) external override onlyOperator {
StrategyData storage strategy = strategies[params.strategyId];
StrategyFeeShares.GlobalAccount storage global = _updateGlobals(strategy, params.strategyId);
Account memory vars;
vars.uniswapLiquidity = strategy.account.uniswapLiquidity;
// only burn this strategy liquidity not other strategy with same ticks
(vars.balance0, vars.balance1,,) = PoolActions.burnLiquidity(strategy.key, vars.uniswapLiquidity);
// global liquidity will be less if strategy has activated exit mode
if (strategy.getHodlStatus() == false) {
global.totalLiquidity -= strategy.account.totalShares;
}
// returns protocol fees
(uint256 automationFee,,,) = _getGovernanceFee(strategy.isPrivate);
// deduct any fees if required for protocol
(vars.fee0, vars.fee1) =
transferFee(strategy.key, 0, automationFee, vars.balance0, vars.balance1, address(0), owner);
vars.balance0 -= vars.fee0;
vars.balance1 -= vars.fee1;
if (strategy.isCompound) {
vars.balance0 += strategy.account.fee0;
vars.balance1 += strategy.account.fee1;
emit FeeCompounded(params.strategyId, strategy.account.fee0, strategy.account.fee1);
}
// add unused assets for new liquidity
vars.balance0 += strategy.account.balance0;
vars.balance1 += strategy.account.balance1;
if (params.swapAmount != 0) {
(int256 amount0Swapped, int256 amount1Swapped) =
PoolActions.swapToken(params.key.pool, params.zeroForOne, params.swapAmount, params.sqrtPriceLimitX96);
(vars.balance0, vars.balance1) = PoolActions.amountsDirection(
params.zeroForOne,
vars.balance0,
vars.balance1,
uint256(amount0Swapped < 0 ? -amount0Swapped : amount0Swapped),
uint256(amount1Swapped < 0 ? -amount1Swapped : amount1Swapped)
);
}
uint128 liquidityDelta;
uint256 amount0Added;
uint256 amount1Added;
if (params.shouldMint) {
(liquidityDelta, amount0Added, amount1Added) =
PoolActions.mintLiquidity(params.key, vars.balance0, vars.balance1);
}
// update state { this state will be reflected to all users having this strategyID }
strategy.updateStrategy(
strategyGlobalFees,
params.key,
params.moduleStatus,
liquidityDelta,
vars.balance0 - amount0Added,
vars.balance1 - amount1Added
);
emit LiquidityShifted(params.strategyId, params.shouldMint, params.zeroForOne, params.swapAmount);
}
/// @notice updates the info of strategy
/// @dev The strategy can be update only by owner
function updateStrategyBase(
bytes32 strategyId,
address owner,
uint256 managementFee,
uint256 performanceFee,
PositionActions calldata actions
)
external
{
_validateModes(actions, managementFee, performanceFee);
StrategyData storage strategy = strategies[strategyId];
if (strategy.owner != _msgSender()) revert InvalidCaller();
if (owner == address(0)) revert OwnerCannotBeZeroAddress();
strategy.updateStrategyState(owner, managementFee, performanceFee, abi.encode(actions));
emit StrategyUpdated(strategyId);
}
function _deposit(
bytes32 strategyId,
uint256 amount0Desired,
uint256 amount1Desired,
uint256 amount0Min,
uint256 amount1Min
)
private
returns (
uint256 share,
uint256 amount0,
uint256 amount1,
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128
)
{
StrategyData storage strategy = strategies[strategyId];
StrategyFeeShares.GlobalAccount storage global = _updateGlobals(strategy, strategyId);
Account memory vars;
bool isExit = strategy.getHodlStatus();
// prevent user drains others
if (strategy.isCompound && isExit == false) {
(vars.uniswapLiquidity, vars.balance0, vars.balance1) = PoolActions.compoundFees(
strategy.key,
strategy.account.balance0 + strategy.account.fee0,
strategy.account.balance1 + strategy.account.fee1
);
if (vars.uniswapLiquidity > 0) {
strategy.updateForCompound(vars.uniswapLiquidity, vars.balance0, vars.balance1);
emit FeeCompounded(strategyId, vars.balance0, vars.balance1);
}
}
// shares should not include fee for non-compounders
(share, amount0, amount1) = LiquidityShares.computeLiquidityShare(strategy, amount0Desired, amount1Desired);
// liquidity frontrun checks here
if (share == 0) revert InvalidShare();
if (strategy.account.totalShares == 0) {
if (share < Constants.MIN_INITIAL_SHARES) revert InvalidShare();
}
if (amount0 < amount0Min || amount1 < amount1Min) revert MinimumAmountsExceeded();
pay(strategy.key.pool.token0(), _msgSender(), address(this), amount0);
pay(strategy.key.pool.token1(), _msgSender(), address(this), amount1);
// now contract balance has: new user asset + previous user unused assets + collected fee of strategy
if (isExit == false) {
(vars.uniswapLiquidity, vars.balance0, vars.balance1) =
PoolActions.mintLiquidity(strategy.key, amount0, amount1);
}
strategy.update(global, vars.uniswapLiquidity, share, amount0, amount1, vars.balance0, vars.balance1);
refundETH();
feeGrowthInside0LastX128 = strategy.account.feeGrowthInside0LastX128;
feeGrowthInside1LastX128 = strategy.account.feeGrowthInside1LastX128;
}
/// @notice Returns maximum amount of fees owed to a specific user position
/// @param tokenId The ID of the Unpilot NFT for which tokens will be collected
/// @return fee0 Amount of fees in token0
/// @return fee1 Amount of fees in token1
function getUserfee(uint256 tokenId) external returns (uint256 fee0, uint256 fee1) {
UserPositions.Data storage position = positions[tokenId];
StrategyData storage strategy = strategies[position.strategyId];
_updateGlobals(strategy, position.strategyId);
(fee0, fee1) = position.claimFeeForNonCompounders(strategy);
}
/// @dev Collects liquidity position fee and update global fee growth so earned fees are
/// updated. Should be called if total amounts needs to include up-to-date fees
function _updateGlobals(
StrategyData storage strategy,
bytes32 strategyId
)
private
returns (StrategyFeeShares.GlobalAccount storage global)
{
global = StrategyFeeShares.updateGlobalStrategyFees(strategyGlobalFees, strategy.key);
(uint256 earned0, uint256 earned1) = StrategyFeeShares.updateStrategyFees(strategy, global);
emit StrategyFee(strategyId, earned0, earned1);
}
/// @notice Returns the liquidity and fee earned by A51 strategy.
/// @param strategyId Hash of strategy ID
/// @return liquidity The currently liquidity available to the pool by strategy
/// @return fee0 The computed amount of token0 owed to the strategy as of the global update
/// @return fee1 The computed amount of token1 owed to the strategy as of the global update
function getStrategyReserves(bytes32 strategyId) external returns (uint128 liquidity, uint256 fee0, uint256 fee1) {
StrategyData storage strategy = strategies[strategyId];
_updateGlobals(strategy, strategyId);
(liquidity, fee0, fee1) = (strategy.account.uniswapLiquidity, strategy.account.fee0, strategy.account.fee1);
}
/// @notice Returns the protocol fee value
/// @param isPrivate Bool value weather strategy is private or public
function _getGovernanceFee(bool isPrivate)
private
view
returns (
uint256 lpAutomationFee,
uint256 strategyCreationFee,
uint256 protcolFeeOnManagement,
uint256 protcolFeeOnPerformance
)
{
return IGovernanceFeeHandler(feeHandler).getGovernanceFee(isPrivate);
}
/// @dev Common checks for valid inputs.
function _validateModes(PositionActions calldata actions, uint256 managementFee, uint256 performanceFee) private {
ICLTModules(cltModules).validateModes(actions, managementFee, performanceFee);
}
function _authorization(uint256 tokenID) private view {
require(ownerOf(tokenID) == _msgSender());
}
function _authorizationOfStrategy(bytes32 strategyId) private view {
if (strategies[strategyId].isPrivate) {
require(strategies[strategyId].owner == _msgSender());
}
}
function tokenURI(uint256 id) public view override returns (string memory) { }
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.8.15;
import { Constants } from "../libraries/Constants.sol";
import { TransferHelper } from "../libraries/TransferHelper.sol";
import { ICLTBase } from "../interfaces/ICLTBase.sol";
import { IWETH9 } from "../interfaces/external/IWETH9.sol";
import { ICLTPayments } from "../interfaces/ICLTPayments.sol";
import { IUniswapV3Factory } from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Factory.sol";
/// @title CLTPayments
/// @notice Contain helper methods for safe token transfers with custom logic
abstract contract CLTPayments is ICLTPayments {
address private immutable WETH9;
IUniswapV3Factory private immutable factory;
constructor(IUniswapV3Factory _factory, address _WETH9) {
factory = _factory;
WETH9 = _WETH9;
}
receive() external payable { }
fallback() external payable { }
/// @notice Pull in tokens from sender. Called to `msg.sender` after minting liquidity to a position from
/// IUniswapV3Pool#mint.
/// @dev In the implementation you must pay to the pool for the minted liquidity.
/// @param amount0Owed The amount of token0 due to the pool for the minted liquidity
/// @param amount1Owed The amount of token1 due to the pool for the minted liquidity
/// @param data Any data passed through by the caller via the IUniswapV3PoolActions#mint call
function uniswapV3MintCallback(uint256 amount0Owed, uint256 amount1Owed, bytes calldata data) external override {
MintCallbackData memory decodedData = abi.decode(data, (MintCallbackData));
_verifyCallBack(decodedData.token0, decodedData.token1, decodedData.fee);
if (amount0Owed > 0) {
TransferHelper.safeTransfer(decodedData.token0, msg.sender, amount0Owed);
}
if (amount1Owed > 0) {
TransferHelper.safeTransfer(decodedData.token1, msg.sender, amount1Owed);
}
}
/// @notice Called to `msg.sender` after minting swaping from IUniswapV3Pool#swap.
/// @dev In the implementation you must pay to the pool for swap.
/// @param amount0Delta The amount of token0 due to the pool for the swap
/// @param amount1Delta The amount of token1 due to the pool for the swap
/// @param data Any data passed through by the caller via the IUniswapV3PoolActions#swap call
function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata data) external override {
SwapCallbackData memory decoded = abi.decode(data, (SwapCallbackData));
_verifyCallBack(decoded.token0, decoded.token1, decoded.fee);
if (amount0Delta > 0) {
TransferHelper.safeTransfer(decoded.token0, msg.sender, uint256(amount0Delta));
}
if (amount1Delta > 0) {
TransferHelper.safeTransfer(decoded.token1, msg.sender, uint256(amount1Delta));
}
}
/// @param token The token to pay
/// @param payer The entity that must pay
/// @param recipient The entity that will receive payment
/// @param value The amount to pay
function pay(address token, address payer, address recipient, uint256 value) internal {
if (token == WETH9 && address(this).balance >= value) {
// pay with WETH9
IWETH9(WETH9).deposit{ value: value }(); // wrap only what is needed to pay
IWETH9(WETH9).transfer(recipient, value);
} else if (payer == address(this)) {
// pay with tokens already in the contract (for the exact input multihop case)
TransferHelper.safeTransfer(token, recipient, value);
} else {
// pull payment
TransferHelper.safeTransferFrom(token, payer, recipient, value);
}
}
/// @param refundAsETH Bool value to convert WETH amount into ETH and refund to the recipient held by strategy
/// @param recipient The entity that will receive payment
/// @param token The token to pay
/// @param amount The amount to pay
function transferFunds(bool refundAsETH, address recipient, address token, uint256 amount) internal {
if (refundAsETH && token == WETH9) {
IWETH9(WETH9).withdraw(amount);
TransferHelper.safeTransferETH(recipient, amount);
} else {
TransferHelper.safeTransfer(token, recipient, amount);
}
}
/// @notice Refunds any ETH balance held by this contract to the `msg.sender`
/// @dev Useful for bundling with mint or increase liquidity that uses ether, or exact output swaps
/// that use ether for the input amount
function refundETH() internal {
if (address(this).balance > 0) {
TransferHelper.safeTransferETH(msg.sender, address(this).balance);
}
}
/// @param key A51 strategy key details
/// @param protcolPercentage The value of percentage to deduct from strategist earned fee
/// @param percentage The value of percentage to deduct from liquidity or eanrned fee & transfer it to strategist
/// @param amount0 The amount of token0 from which the strategist fee will deduct
/// @param amount1 The amount of token1 from which the strategist fee will deduct
/// @param governance Address of protocol owner
/// @param strategyOwner Address of strategy owner
function transferFee(
ICLTBase.StrategyKey memory key,
uint256 protcolPercentage,
uint256 percentage,
uint256 amount0,
uint256 amount1,
address governance,
address strategyOwner
)
internal
returns (uint256 fee0, uint256 fee1)
{
if (percentage > 0) {
if (amount0 > 0) {
fee0 = (amount0 * percentage) / Constants.WAD;
uint256 protcolShare0 = (fee0 * protcolPercentage) / Constants.WAD;
TransferHelper.safeTransfer(key.pool.token0(), strategyOwner, fee0 - protcolShare0);
if (protcolShare0 > 0) TransferHelper.safeTransfer(key.pool.token0(), governance, protcolShare0);
}
if (amount1 > 0) {
fee1 = (amount1 * percentage) / Constants.WAD;
uint256 protcolShare1 = (fee1 * protcolPercentage) / Constants.WAD;
TransferHelper.safeTransfer(key.pool.token1(), strategyOwner, fee1 - protcolShare1);
if (protcolShare1 > 0) TransferHelper.safeTransfer(key.pool.token1(), governance, protcolShare1);
}
}
}
function _verifyCallBack(address token0, address token1, uint24 fee) private view {
require(msg.sender == factory.getPool(token0, token1, fee));
}
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
library Constants {
uint256 public constant WAD = 1e18;
uint256 public constant MIN_INITIAL_SHARES = 1e3;
uint256 public constant MAX_MANAGEMENT_FEE = 2e17;
uint256 public constant MAX_PERFORMANCE_FEE = 2e17;
uint256 public constant MAX_PROTCOL_MANAGEMENT_FEE = 2e17;
uint256 public constant MAX_PROTCOL_PERFORMANCE_FEE = 2e17;
uint256 public constant MAX_AUTOMATION_FEE = 2e17;
uint256 public constant MAX_STRATEGY_CREATION_FEE = 5e17;
uint128 public constant MAX_UINT128 = type(uint128).max;
// keccak256("MODE")
bytes32 public constant MODE = 0x25d202ee31c346b8c1099dc1a469d77ca5ac14ed43336c881902290b83e0a13a;
// keccak256("EXIT_STRATEGY")
bytes32 public constant EXIT_STRATEGY = 0xf36a697ed62dd2d982c1910275ee6172360bf72c4dc9f3b10f2d9c700666e227;
// keccak256("REBASE_STRATEGY")
bytes32 public constant REBASE_STRATEGY = 0x5eea0aea3d82798e316d046946dbce75c9d5995b956b9e60624a080c7f56f204;
// keccak256("LIQUIDITY_DISTRIBUTION")
bytes32 public constant LIQUIDITY_DISTRIBUTION = 0xeabe6f62bd74d002b0267a6aaacb5212bb162f4f87ee1c4a80ac0d2698f8a505;
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
pragma solidity ^0.8.0;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;
/// @notice Modern, minimalist, and gas efficient ERC-721 implementation.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC721.sol)
abstract contract ERC721 {
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event Transfer(address indexed from, address indexed to, uint256 indexed id);
event Approval(address indexed owner, address indexed spender, uint256 indexed id);
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
/*//////////////////////////////////////////////////////////////
METADATA STORAGE/LOGIC
//////////////////////////////////////////////////////////////*/
string public name;
string public symbol;
function tokenURI(uint256 id) public view virtual returns (string memory);
/*//////////////////////////////////////////////////////////////
ERC721 BALANCE/OWNER STORAGE
//////////////////////////////////////////////////////////////*/
mapping(uint256 => address) internal _ownerOf;
mapping(address => uint256) internal _balanceOf;
function ownerOf(uint256 id) public view virtual returns (address owner) {
require((owner = _ownerOf[id]) != address(0), "NOT_MINTED");
}
function balanceOf(address owner) public view virtual returns (uint256) {
require(owner != address(0), "ZERO_ADDRESS");
return _balanceOf[owner];
}
/*//////////////////////////////////////////////////////////////
ERC721 APPROVAL STORAGE
//////////////////////////////////////////////////////////////*/
mapping(uint256 => address) public getApproved;
mapping(address => mapping(address => bool)) public isApprovedForAll;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(string memory _name, string memory _symbol) {
name = _name;
symbol = _symbol;
}
/*//////////////////////////////////////////////////////////////
ERC721 LOGIC
//////////////////////////////////////////////////////////////*/
function approve(address spender, uint256 id) public virtual {
address owner = _ownerOf[id];
require(msg.sender == owner || isApprovedForAll[owner][msg.sender], "NOT_AUTHORIZED");
getApproved[id] = spender;
emit Approval(owner, spender, id);
}
function setApprovalForAll(address operator, bool approved) public virtual {
isApprovedForAll[msg.sender][operator] = approved;
emit ApprovalForAll(msg.sender, operator, approved);
}
function transferFrom(
address from,
address to,
uint256 id
) public virtual {
require(from == _ownerOf[id], "WRONG_FROM");
require(to != address(0), "INVALID_RECIPIENT");
require(
msg.sender == from || isApprovedForAll[from][msg.sender] || msg.sender == getApproved[id],
"NOT_AUTHORIZED"
);
// Underflow of the sender's balance is impossible because we check for
// ownership above and the recipient's balance can't realistically overflow.
unchecked {
_balanceOf[from]--;
_balanceOf[to]++;
}
_ownerOf[id] = to;
delete getApproved[id];
emit Transfer(from, to, id);
}
function safeTransferFrom(
address from,
address to,
uint256 id
) public virtual {
transferFrom(from, to, id);
require(
to.code.length == 0 ||
ERC721TokenReceiver(to).onERC721Received(msg.sender, from, id, "") ==
ERC721TokenReceiver.onERC721Received.selector,
"UNSAFE_RECIPIENT"
);
}
function safeTransferFrom(
address from,
address to,
uint256 id,
bytes calldata data
) public virtual {
transferFrom(from, to, id);
require(
to.code.length == 0 ||
ERC721TokenReceiver(to).onERC721Received(msg.sender, from, id, data) ==
ERC721TokenReceiver.onERC721Received.selector,
"UNSAFE_RECIPIENT"
);
}
/*//////////////////////////////////////////////////////////////
ERC165 LOGIC
//////////////////////////////////////////////////////////////*/
function supportsInterface(bytes4 interfaceId) public view virtual returns (bool) {
return
interfaceId == 0x01ffc9a7 || // ERC165 Interface ID for ERC165
interfaceId == 0x80ac58cd || // ERC165 Interface ID for ERC721
interfaceId == 0x5b5e139f; // ERC165 Interface ID for ERC721Metadata
}
/*//////////////////////////////////////////////////////////////
INTERNAL MINT/BURN LOGIC
//////////////////////////////////////////////////////////////*/
function _mint(address to, uint256 id) internal virtual {
require(to != address(0), "INVALID_RECIPIENT");
require(_ownerOf[id] == address(0), "ALREADY_MINTED");
// Counter overflow is incredibly unrealistic.
unchecked {
_balanceOf[to]++;
}
_ownerOf[id] = to;
emit Transfer(address(0), to, id);
}
function _burn(uint256 id) internal virtual {
address owner = _ownerOf[id];
require(owner != address(0), "NOT_MINTED");
// Ownership check above ensures no underflow.
unchecked {
_balanceOf[owner]--;
}
delete _ownerOf[id];
delete getApproved[id];
emit Transfer(owner, address(0), id);
}
/*//////////////////////////////////////////////////////////////
INTERNAL SAFE MINT LOGIC
//////////////////////////////////////////////////////////////*/
function _safeMint(address to, uint256 id) internal virtual {
_mint(to, id);
require(
to.code.length == 0 ||
ERC721TokenReceiver(to).onERC721Received(msg.sender, address(0), id, "") ==
ERC721TokenReceiver.onERC721Received.selector,
"UNSAFE_RECIPIENT"
);
}
function _safeMint(
address to,
uint256 id,
bytes memory data
) internal virtual {
_mint(to, id);
require(
to.code.length == 0 ||
ERC721TokenReceiver(to).onERC721Received(msg.sender, address(0), id, data) ==
ERC721TokenReceiver.onERC721Received.selector,
"UNSAFE_RECIPIENT"
);
}
}
/// @notice A generic interface for a contract which properly accepts ERC721 tokens.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC721.sol)
abstract contract ERC721TokenReceiver {
function onERC721Received(
address,
address,
uint256,
bytes calldata
) external virtual returns (bytes4) {
return ERC721TokenReceiver.onERC721Received.selector;
}
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.4.0;
/// @title FixedPoint128
/// @notice A library for handling binary fixed point numbers, see https://en.wikipedia.org/wiki/Q_(number_format)
library FixedPoint128 {
uint256 internal constant Q128 = 0x100000000000000000000000000000000;
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.4.0;
/// @title FixedPoint96
/// @notice A library for handling binary fixed point numbers, see https://en.wikipedia.org/wiki/Q_(number_format)
/// @dev Used in SqrtPriceMath.sol
library FixedPoint96 {
uint8 internal constant RESOLUTION = 96;
uint256 internal constant Q96 = 0x1000000000000000000000000;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @title Contains 512-bit math functions
/// @notice Facilitates multiplication and division that can have overflow of an intermediate value without any loss of precision
/// @dev Handles "phantom overflow" i.e., allows multiplication and division where an intermediate value overflows 256 bits
library FullMath {
/// @notice Calculates floor(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
/// @param a The multiplicand
/// @param b The multiplier
/// @param denominator The divisor
/// @return result The 256-bit result
/// @dev Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv
function mulDiv(
uint256 a,
uint256 b,
uint256 denominator
) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = a * b
// Compute the product mod 2**256 and mod 2**256 - 1
// then use the Chinese Remainder Theorem to reconstruct
// the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2**256 + prod0
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(a, b, not(0))
prod0 := mul(a, b)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division
if (prod1 == 0) {
require(denominator > 0);
assembly {
result := div(prod0, denominator)
}
return result;
}
// Make sure the result is less than 2**256.
// Also prevents denominator == 0
require(denominator > prod1);
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0]
// Compute remainder using mulmod
uint256 remainder;
assembly {
remainder := mulmod(a, b, denominator)
}
// Subtract 256 bit number from 512 bit number
assembly {
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator
// Compute largest power of two divisor of denominator.
// Always >= 1.
uint256 twos = (0 - denominator) & denominator;
// Divide denominator by power of two
assembly {
denominator := div(denominator, twos)
}
// Divide [prod1 prod0] by the factors of two
assembly {
prod0 := div(prod0, twos)
}
// Shift in bits from prod1 into prod0. For this we need
// to flip `twos` such that it is 2**256 / twos.
// If twos is zero, then it becomes one
assembly {
twos := add(div(sub(0, twos), twos), 1)
}
prod0 |= prod1 * twos;
// Invert denominator mod 2**256
// Now that denominator is an odd number, it has an inverse
// modulo 2**256 such that denominator * inv = 1 mod 2**256.
// Compute the inverse by starting with a seed that is correct
// correct for four bits. That is, denominator * inv = 1 mod 2**4
uint256 inv = (3 * denominator) ^ 2;
// Now use Newton-Raphson iteration to improve the precision.
// Thanks to Hensel's lifting lemma, this also works in modular
// arithmetic, doubling the correct bits in each step.
inv *= 2 - denominator * inv; // inverse mod 2**8
inv *= 2 - denominator * inv; // inverse mod 2**16
inv *= 2 - denominator * inv; // inverse mod 2**32
inv *= 2 - denominator * inv; // inverse mod 2**64
inv *= 2 - denominator * inv; // inverse mod 2**128
inv *= 2 - denominator * inv; // inverse mod 2**256
// Because the division is now exact we can divide by multiplying
// with the modular inverse of denominator. This will give us the
// correct result modulo 2**256. Since the precoditions guarantee
// that the outcome is less than 2**256, this is the final result.
// We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inv;
return result;
}
}
/// @notice Calculates ceil(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
/// @param a The multiplicand
/// @param b The multiplier
/// @param denominator The divisor
/// @return result The 256-bit result
function mulDivRoundingUp(
uint256 a,
uint256 b,
uint256 denominator
) internal pure returns (uint256 result) {
unchecked {
result = mulDiv(a, b, denominator);
if (mulmod(a, b, denominator) > 0) {
require(result < type(uint256).max);
result++;
}
}
}
}
//SPDX-License-Identifier: MIT
pragma solidity =0.8.15;
import { IUniswapV3Pool } from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
interface ICLTBase {
error NoLiquidity();
error InvalidInput();
error InvalidShare();
error InvalidCaller();
error onlyNonCompounders();
error TransactionTooAged();
error MinimumAmountsExceeded();
error OwnerCannotBeZeroAddress();
/// @param pool The Uniswap V3 pool
/// @param tickLower The lower tick of the A51's LP position
/// @param tickUpper The upper tick of the A51's LP position
struct StrategyKey {
IUniswapV3Pool pool;
int24 tickLower;
int24 tickUpper;
}
/// @param actionName Encoded name of whitelisted advance module
/// @param data input as encoded data for selected module
struct StrategyPayload {
bytes32 actionName;
bytes data;
}
/// @param mode ModuleId: one of four basic modes 1: left, 2: Right, 3: Both, 4: Static
/// @param exitStrategy Array of whitelistd ids for advance mode exit strategy selection
/// @param rebaseStrategy Array of whitelistd ids for advance mode rebase strategy selection
/// @param liquidityDistribution Array of whitelistd ids for advance mode liquidity distribution selection
struct PositionActions {
uint256 mode;
StrategyPayload[] exitStrategy;
StrategyPayload[] rebaseStrategy;
StrategyPayload[] liquidityDistribution;
}
/// @param fee0 Amount of fees0 collected by strategy
/// @param fee1 Amount of fees1 collected by strategy
/// @param balance0 Amount of token0 left in strategy that were not added in pool
/// @param balance1 Amount of token1 left in strategy that were not added in pool
/// @param totalShares Total no of shares minted for this A51's strategy
/// @param uniswapLiquidity Total no of liquidity added on AMM for this strategy
/// @param feeGrowthInside0LastX128 The fee growth of token0 collected per unit of liquidity for
/// the entire life of the A51's position
/// @param feeGrowthInside1LastX128 The fee growth of token1 collected per unit of liquidity for
/// the entire life of the A51's position
struct Account {
uint256 fee0;
uint256 fee1;
uint256 balance0;
uint256 balance1;
uint256 totalShares;
uint128 uniswapLiquidity;
uint256 feeGrowthInside0LastX128;
uint256 feeGrowthInside1LastX128;
uint256 feeGrowthOutside0LastX128;
uint256 feeGrowthOutside1LastX128;
}
/// @param key A51 position's key details
/// @param owner The address of the strategy owner
/// @param actions Ids of all modes selected by the strategist encoded together in a single hash
/// @param actionStatus The encoded data for each of the strategy to track any detail for futher actions
/// @param isCompound Bool weather the strategy has compunding activated or not
/// @param isPrivate Bool weather strategy is open for all users or not
/// @param managementFee The value of fee in percentage applied on strategy users liquidity by strategy owner
/// @param performanceFee The value of fee in percentage applied on strategy users earned fee by strategy owner
/// @param account Strategy accounts of balances and fee account details
struct StrategyData {
StrategyKey key;
address owner;
bytes actions;
bytes actionStatus;
bool isCompound;
bool isPrivate;
uint256 managementFee;
uint256 performanceFee;
Account account;
}
/// @notice Emitted when tokens are collected for a position NFT
/// @param tokenId The ID of the token for which underlying tokens were collected
/// @param recipient The address of the account that received the collected tokens
/// @param amount0Collected The amount of token0 owed to the position that was collected
/// @param amount1Collected The amount of token1 owed to the position that was collected
event Collect(uint256 tokenId, address recipient, uint256 amount0Collected, uint256 amount1Collected);
/// @notice Emitted when liquidity is minted for a given position
/// @param tokenId The ID of the token for which liquidity was increased
/// @param recipient Recipient of liquidity
/// @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 Deposit(
uint256 indexed tokenId, address indexed recipient, uint256 liquidity, uint256 amount0, uint256 amount1
);
/// @notice Emitted when a position's liquidity is removed
/// @param tokenId The ID of the token for which liquidity was decreased
/// @param recipient Recipient of liquidity
/// @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 Withdraw(
uint256 indexed tokenId,
address indexed recipient,
uint256 liquidity,
uint256 amount0,
uint256 amount1,
uint256 fee0,
uint256 fee1
);
/// @notice Emitted when strategy is created
/// @param strategyId The strategy's key is a hash of a preimage composed by the owner & token ID
event StrategyCreated(bytes32 indexed strategyId);
/// @notice Emitted when data of strategy is updated
/// @param strategyId Hash of strategy ID
event StrategyUpdated(bytes32 indexed strategyId);
/// @notice Emitted when fee of strategy is collected
/// @param strategyId Hash of strategy ID
/// @param fee0 Amount of fees0 collected by strategy
/// @param fee1 Amount of fees1 collected by strategy
event StrategyFee(bytes32 indexed strategyId, uint256 fee0, uint256 fee1);
/// @notice Emitted when liquidity is increased for a position NFT
/// @param tokenId The ID of the token for which liquidity was increased
/// @param share 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 PositionUpdated(uint256 indexed tokenId, uint256 share, uint256 amount0, uint256 amount1);
/// @notice Emitted when strategy position is updated or shifted
/// @param strategyId The strategy's key is a hash of a preimage composed by the owner & token ID
/// @param isLiquidityMinted Bool whether the new liquidity position is minted in pool or HODL in contract
/// @param zeroForOne Bool The direction of the swap, true for token0 to token1, false for token1 to token0
/// @param swapAmount The amount of the swap, which implicitly configures the swap as exact input (positive), or
/// exact output (negative)
event LiquidityShifted(bytes32 indexed strategyId, bool isLiquidityMinted, bool zeroForOne, int256 swapAmount);
/// @notice Emitted when collected fee of strategy is compounded
/// @param strategyId Hash of strategy ID
/// @param amount0 The amount of token0 that were compounded
/// @param amount1 The amount of token1 that were compounded
event FeeCompounded(bytes32 indexed strategyId, uint256 amount0, uint256 amount1);
/// @notice Creates new LP strategy on AMM
/// @dev Call this when the pool does exist and is initialized
/// List of whitelisted IDs could be fetched by the modules contract for each basic & advance mode.
/// If any ID is selected of any module it is mandatory to encode data for it then pass it to StrategyPayload.data
/// @param key The params necessary to select a position, encoded as `StrategyKey` in calldata
/// @param actions It is hash of all encoded data of whitelisted IDs which are being passed
/// @param managementFee The value of fee in percentage applied on strategy users liquidity by strategy owner
/// @param performanceFee The value of fee in percentage applied on strategy users earned fee by strategy owner
/// @param isCompound Bool weather the strategy should have compunding activated or not
/// @param isPrivate Bool weather strategy is open for all users or not
function createStrategy(
StrategyKey calldata key,
PositionActions calldata actions,
uint256 managementFee,
uint256 performanceFee,
bool isCompound,
bool isPrivate
)
external
payable;
/// @notice Returns the information about a strategy by the strategy's key
/// @param strategyId The strategy's key is a hash of a preimage composed by the owner & token ID
/// @return key A51 position's key details associated with this strategy
/// @return owner The address of the strategy owner
/// @return actions It is a hash of a preimage composed by all modes IDs selected by the strategist
/// @return actionStatus It is a hash of a additional data of strategy for further required actions
/// @return isCompound Bool weather the strategy has compunding activated or not
/// @return isPrivate Bool weather strategy is open for all users or not
/// @return managementFee The value of fee in percentage applied on strategy users liquidity by strategy owner
/// @return performanceFee The value of fee in percentage applied on strategy users earned fee by strategy owner
/// @return account Strategy values of balances and fee accounting details
function strategies(bytes32 strategyId)
external
returns (
StrategyKey memory key,
address owner,
bytes memory actions,
bytes memory actionStatus,
bool isCompound,
bool isPrivate,
uint256 managementFee,
uint256 performanceFee,
Account memory account
);
/// @notice Returns the position information associated with a given token ID.
/// @dev Throws if the token ID is not valid.
/// @param positionId The ID of the token that represents the position
/// @return strategyId strategy ID assigned to this token ID
/// @return liquidityShare Shares assigned to this token ID
/// @return feeGrowthInside0LastX128 The fee growth of token0 as of the last action on the individual position
/// @return feeGrowthInside1LastX128 The fee growth of token1 as of the last action on the individual position
/// @return tokensOwed0 The uncollected amount of token0 owed to the position as of the last computation
/// @return tokensOwed1 The uncollected amount of token1 owed to the position as of the last computation
function positions(uint256 positionId)
external
returns (
bytes32 strategyId,
uint256 liquidityShare,
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128,
uint128 tokensOwed0,
uint128 tokensOwed1
);
/// @param amount0Desired The desired amount of token0 to be spent,
/// @param amount1Desired The desired amount of token1 to be spent,
/// @param amount0Min The minimum amount of token0 to spend, which serves as a slippage check,
/// @param amount1Min The minimum amount of token1 to spend, which serves as a slippage check,
/// @param recipient account that should receive the shares in terms of A51's NFT
struct DepositParams {
bytes32 strategyId;
uint256 amount0Desired;
uint256 amount1Desired;
uint256 amount0Min;
uint256 amount1Min;
address recipient;
}
/// @notice Creates a new position wrapped in a A51 NFT
/// @param params The params necessary to mint a position, encoded as `MintParams` in calldata
/// @return tokenId The ID of the token that represents the minted position
/// @return liquidity The amount of liquidity for this position
/// @return amount0 The amount of token0
/// @return amount1 The amount of token1
function deposit(DepositParams calldata params)
external
payable
returns (uint256 tokenId, uint256 liquidity, uint256 amount0, uint256 amount1);
/// @param params tokenId The ID of the token for which liquidity is being increased
/// @param amount0Desired The desired amount of token0 to be spent,
/// @param amount1Desired The desired amount of token1 to be spent,
struct UpdatePositionParams {
uint256 tokenId;
uint256 amount0Desired;
uint256 amount1Desired;
uint256 amount0Min;
uint256 amount1Min;
}
/// @notice Increases the amount of liquidity in a position, with tokens paid by the `msg.sender`
/// @param params The params necessary to increase a position, encoded as `UpdatePositionParams` in calldata
/// @dev This method can be used by by both compounding & non-compounding strategy positions
/// @return share The new liquidity amount as a result of the increase
/// @return amount0 The amount of token0 to acheive resulting liquidity
/// @return amount1 The amount of token1 to acheive resulting liquidity
function updatePositionLiquidity(UpdatePositionParams calldata params)
external
payable
returns (uint256 share, uint256 amount0, uint256 amount1);
/// @param params tokenId The ID of the token for which liquidity is being decreased
/// @param liquidity amount The amount by which liquidity will be decreased,
/// @param recipient Recipient of tokens
/// @param refundAsETH whether to recieve in WETH or ETH (only valid for WETH/ALT pairs)
struct WithdrawParams {
uint256 tokenId;
uint256 liquidity;
address recipient;
bool refundAsETH;
uint256 amount0Min;
uint256 amount1Min;
}
/// @notice Decreases the amount of liquidity in a position and accounts it to the position
/// @param params The params necessary to decrease a position, encoded as `WithdrawParams` in calldata
/// @return amount0 Amount of token0 sent to recipient
/// @return amount1 Amount of token1 sent to recipient
function withdraw(WithdrawParams calldata params) external returns (uint256 amount0, uint256 amount1);
/// @param recipient Recipient of tokens
/// @param params tokenId The ID of the NFT for which tokens are being collected
/// @param refundAsETH whether to recieve in WETH or ETH (only valid for WETH/ALT pairs)
struct ClaimFeesParams {
address recipient;
uint256 tokenId;
bool refundAsETH;
}
/// @notice Collects up to a maximum amount of fees owed to a specific position to the recipient
/// @dev Only non-compounding strategy users can call this
/// @param params The params necessary to collect a position uncompounded fee, encoded as `ClaimFeesParams` in
/// calldata
function claimPositionFee(ClaimFeesParams calldata params) external;
/// @param key A51 new position's key with updated ticks
/// @param strategyId Id of A51's position for which ticks are being updated
/// @param shouldMint Bool weather liquidity should be added on AMM or hold in contract
/// @param zeroForOne The direction of the swap, true for token0 to token1, false for token1 to token0
/// @param swapAmount The amount of the swap, which implicitly configures the swap as exact input (positive), or
/// exact output (negative)
/// @param moduleStatus The encoded data for each of the strategy to track any detail for futher actions
/// @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this
/// value after the swap. If one for zero, the price cannot be greater than this value after the swap
/// @param data Any data to be passed through to the callback
struct ShiftLiquidityParams {
StrategyKey key;
bytes32 strategyId;
bool shouldMint;
bool zeroForOne;
int256 swapAmount;
bytes moduleStatus;
uint160 sqrtPriceLimitX96;
}
/// @notice Updates the strategy's liquidity accordingly w.r.t basic or advance module when it is activated
/// @dev Only called by the whitlisted bot or owner of strategy
/// @param params The params necessary to update a position, encoded as `ShiftLiquidityParams` in calldata
function shiftLiquidity(ShiftLiquidityParams calldata params) external;
}
//SPDX-License-Identifier: MIT
pragma solidity =0.8.15;
import { ICLTBase } from "./ICLTBase.sol";
interface ICLTModules {
error InvalidMode();
error InvalidStrategy();
error InvalidStrategyAction();
/// @notice Validates the strategy inputs
/// @param actions The ids of all actions selected for new strategy creation
/// @param managementFee The value of strategist management fee on strategy
/// @param performanceFee The value of strategist perofrmance fee on strategy
function validateModes(
ICLTBase.PositionActions calldata actions,
uint256 managementFee,
uint256 performanceFee
)
external;
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import { IUniswapV3MintCallback } from "@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3MintCallback.sol";
import { IUniswapV3SwapCallback } from "@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol";
/// @title Liquidity management functions
/// @notice Internal functions for safely managing liquidity in Uniswap V3
interface ICLTPayments is IUniswapV3MintCallback, IUniswapV3SwapCallback {
struct MintCallbackData {
address token0;
address token1;
uint24 fee;
address payer;
}
struct SwapCallbackData {
address token0;
address token1;
uint24 fee;
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 amount) external returns (bool);
}
//SPDX-License-Identifier: MIT
pragma solidity =0.8.15;
interface IGovernanceFeeHandler {
error StrategyFeeLimitExceed();
error ManagementFeeLimitExceed();
error PerformanceFeeLimitExceed();
error LPAutomationFeeLimitExceed();
/// @param lpAutomationFee The value of fee applied for automation of strategy
/// @param strategyCreationFee The value of fee applied for creation of new strategy
/// @param protcolFeeOnManagement The value of fee applied on strategiest earned fee on managment of strategy
/// @param protcolFeeOnPerformance The value of fee applied on strategiest earned fee on performance of strategy
struct ProtocolFeeRegistry {
uint256 lpAutomationFee;
uint256 strategyCreationFee;
uint256 protcolFeeOnManagement;
uint256 protcolFeeOnPerformance;
}
/// @notice Returns the protocol fee value
/// @param isPrivate Bool value weather strategy is private or public
function getGovernanceFee(bool isPrivate)
external
view
returns (
uint256 lpAutomationFee,
uint256 strategyCreationFee,
uint256 protcolFeeOnManagement,
uint256 protcolFeeOnPerformance
);
/// @notice Updates the protocol fee value for public strategy
function setPublicFeeRegistry(ProtocolFeeRegistry calldata newPublicStrategyFeeRegistry) external;
/// @notice Updates the protocol fee value for private strategy
function setPrivateFeeRegistry(ProtocolFeeRegistry calldata newPrivateStrategyFeeRegistry) external;
/// @notice Emitted when the protocol fee for public strategy has been updated
event PublicFeeRegistryUpdated(ProtocolFeeRegistry newRegistry);
/// @notice Emitted when the protocol fee for private strategy has been updated
event PrivateFeeRegistryUpdated(ProtocolFeeRegistry newRegistry);
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title The interface for the Uniswap V3 Factory
/// @notice The Uniswap V3 Factory facilitates creation of Uniswap V3 pools and control over the protocol fees
interface IUniswapV3Factory {
/// @notice Emitted when the owner of the factory is changed
/// @param oldOwner The owner before the owner was changed
/// @param newOwner The owner after the owner was changed
event OwnerChanged(address indexed oldOwner, address indexed newOwner);
/// @notice Emitted when a pool is created
/// @param token0 The first token of the pool by address sort order
/// @param token1 The second token of the pool by address sort order
/// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
/// @param tickSpacing The minimum number of ticks between initialized ticks
/// @param pool The address of the created pool
event PoolCreated(
address indexed token0,
address indexed token1,
uint24 indexed fee,
int24 tickSpacing,
address pool
);
/// @notice Emitted when a new fee amount is enabled for pool creation via the factory
/// @param fee The enabled fee, denominated in hundredths of a bip
/// @param tickSpacing The minimum number of ticks between initialized ticks for pools created with the given fee
event FeeAmountEnabled(uint24 indexed fee, int24 indexed tickSpacing);
/// @notice Returns the current owner of the factory
/// @dev Can be changed by the current owner via setOwner
/// @return The address of the factory owner
function owner() external view returns (address);
/// @notice Returns the tick spacing for a given fee amount, if enabled, or 0 if not enabled
/// @dev A fee amount can never be removed, so this value should be hard coded or cached in the calling context
/// @param fee The enabled fee, denominated in hundredths of a bip. Returns 0 in case of unenabled fee
/// @return The tick spacing
function feeAmountTickSpacing(uint24 fee) external view returns (int24);
/// @notice Returns the pool address for a given pair of tokens and a fee, or address 0 if it does not exist
/// @dev tokenA and tokenB may be passed in either token0/token1 or token1/token0 order
/// @param tokenA The contract address of either token0 or token1
/// @param tokenB The contract address of the other token
/// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
/// @return pool The pool address
function getPool(
address tokenA,
address tokenB,
uint24 fee
) external view returns (address pool);
/// @notice Creates a pool for the given two tokens and fee
/// @param tokenA One of the two tokens in the desired pool
/// @param tokenB The other of the two tokens in the desired pool
/// @param fee The desired fee for the pool
/// @dev tokenA and tokenB may be passed in either order: token0/token1 or token1/token0. tickSpacing is retrieved
/// from the fee. The call will revert if the pool already exists, the fee is invalid, or the token arguments
/// are invalid.
/// @return pool The address of the newly created pool
function createPool(
address tokenA,
address tokenB,
uint24 fee
) external returns (address pool);
/// @notice Updates the owner of the factory
/// @dev Must be called by the current owner
/// @param _owner The new owner of the factory
function setOwner(address _owner) external;
/// @notice Enables a fee amount with the given tickSpacing
/// @dev Fee amounts may never be removed once enabled
/// @param fee The fee amount to enable, denominated in hundredths of a bip (i.e. 1e-6)
/// @param tickSpacing The spacing between ticks to be enforced for all pools created with the given fee amount
function enableFeeAmount(uint24 fee, int24 tickSpacing) external;
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Callback for IUniswapV3PoolActions#mint
/// @notice Any contract that calls IUniswapV3PoolActions#mint must implement this interface
interface IUniswapV3MintCallback {
/// @notice Called to `msg.sender` after minting liquidity to a position from IUniswapV3Pool#mint.
/// @dev In the implementation you must pay the pool tokens owed for the minted liquidity.
/// The caller of this method must be checked to be a UniswapV3Pool deployed by the canonical UniswapV3Factory.
/// @param amount0Owed The amount of token0 due to the pool for the minted liquidity
/// @param amount1Owed The amount of token1 due to the pool for the minted liquidity
/// @param data Any data passed through by the caller via the IUniswapV3PoolActions#mint call
function uniswapV3MintCallback(
uint256 amount0Owed,
uint256 amount1Owed,
bytes calldata data
) external;
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import {IUniswapV3PoolImmutables} from './pool/IUniswapV3PoolImmutables.sol';
import {IUniswapV3PoolState} from './pool/IUniswapV3PoolState.sol';
import {IUniswapV3PoolDerivedState} from './pool/IUniswapV3PoolDerivedState.sol';
import {IUniswapV3PoolActions} from './pool/IUniswapV3PoolActions.sol';
import {IUniswapV3PoolOwnerActions} from './pool/IUniswapV3PoolOwnerActions.sol';
import {IUniswapV3PoolErrors} from './pool/IUniswapV3PoolErrors.sol';
import {IUniswapV3PoolEvents} from './pool/IUniswapV3PoolEvents.sol';
/// @title The interface for a Uniswap V3 Pool
/// @notice A Uniswap pool facilitates swapping and automated market making between any two assets that strictly conform
/// to the ERC20 specification
/// @dev The pool interface is broken up into many smaller pieces
interface IUniswapV3Pool is
IUniswapV3PoolImmutables,
IUniswapV3PoolState,
IUniswapV3PoolDerivedState,
IUniswapV3PoolActions,
IUniswapV3PoolOwnerActions,
IUniswapV3PoolErrors,
IUniswapV3PoolEvents
{
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Permissionless pool actions
/// @notice Contains pool methods that can be called by anyone
interface IUniswapV3PoolActions {
/// @notice Sets the initial price for the pool
/// @dev Price is represented as a sqrt(amountToken1/amountToken0) Q64.96 value
/// @param sqrtPriceX96 the initial sqrt price of the pool as a Q64.96
function initialize(uint160 sqrtPriceX96) external;
/// @notice Adds liquidity for the given recipient/tickLower/tickUpper position
/// @dev The caller of this method receives a callback in the form of IUniswapV3MintCallback#uniswapV3MintCallback
/// in which they must pay any token0 or token1 owed for the liquidity. The amount of token0/token1 due depends
/// on tickLower, tickUpper, the amount of liquidity, and the current price.
/// @param recipient The address for which the liquidity will be created
/// @param tickLower The lower tick of the position in which to add liquidity
/// @param tickUpper The upper tick of the position in which to add liquidity
/// @param amount The amount of liquidity to mint
/// @param data Any data that should be passed through to the callback
/// @return amount0 The amount of token0 that was paid to mint the given amount of liquidity. Matches the value in the callback
/// @return amount1 The amount of token1 that was paid to mint the given amount of liquidity. Matches the value in the callback
function mint(
address recipient,
int24 tickLower,
int24 tickUpper,
uint128 amount,
bytes calldata data
) external returns (uint256 amount0, uint256 amount1);
/// @notice Collects tokens owed to a position
/// @dev Does not recompute fees earned, which must be done either via mint or burn of any amount of liquidity.
/// Collect must be called by the position owner. To withdraw only token0 or only token1, amount0Requested or
/// amount1Requested may be set to zero. To withdraw all tokens owed, caller may pass any value greater than the
/// actual tokens owed, e.g. type(uint128).max. Tokens owed may be from accumulated swap fees or burned liquidity.
/// @param recipient The address which should receive the fees collected
/// @param tickLower The lower tick of the position for which to collect fees
/// @param tickUpper The upper tick of the position for which to collect fees
/// @param amount0Requested How much token0 should be withdrawn from the fees owed
/// @param amount1Requested How much token1 should be withdrawn from the fees owed
/// @return amount0 The amount of fees collected in token0
/// @return amount1 The amount of fees collected in token1
function collect(
address recipient,
int24 tickLower,
int24 tickUpper,
uint128 amount0Requested,
uint128 amount1Requested
) external returns (uint128 amount0, uint128 amount1);
/// @notice Burn liquidity from the sender and account tokens owed for the liquidity to the position
/// @dev Can be used to trigger a recalculation of fees owed to a position by calling with an amount of 0
/// @dev Fees must be collected separately via a call to #collect
/// @param tickLower The lower tick of the position for which to burn liquidity
/// @param tickUpper The upper tick of the position for which to burn liquidity
/// @param amount How much liquidity to burn
/// @return amount0 The amount of token0 sent to the recipient
/// @return amount1 The amount of token1 sent to the recipient
function burn(
int24 tickLower,
int24 tickUpper,
uint128 amount
) external returns (uint256 amount0, uint256 amount1);
/// @notice Swap token0 for token1, or token1 for token0
/// @dev The caller of this method receives a callback in the form of IUniswapV3SwapCallback#uniswapV3SwapCallback
/// @param recipient The address to receive the output of the swap
/// @param zeroForOne The direction of the swap, true for token0 to token1, false for token1 to token0
/// @param amountSpecified The amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative)
/// @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this
/// value after the swap. If one for zero, the price cannot be greater than this value after the swap
/// @param data Any data to be passed through to the callback
/// @return amount0 The delta of the balance of token0 of the pool, exact when negative, minimum when positive
/// @return amount1 The delta of the balance of token1 of the pool, exact when negative, minimum when positive
function swap(
address recipient,
bool zeroForOne,
int256 amountSpecified,
uint160 sqrtPriceLimitX96,
bytes calldata data
) external returns (int256 amount0, int256 amount1);
/// @notice Receive token0 and/or token1 and pay it back, plus a fee, in the callback
/// @dev The caller of this method receives a callback in the form of IUniswapV3FlashCallback#uniswapV3FlashCallback
/// @dev Can be used to donate underlying tokens pro-rata to currently in-range liquidity providers by calling
/// with 0 amount{0,1} and sending the donation amount(s) from the callback
/// @param recipient The address which will receive the token0 and token1 amounts
/// @param amount0 The amount of token0 to send
/// @param amount1 The amount of token1 to send
/// @param data Any data to be passed through to the callback
function flash(
address recipient,
uint256 amount0,
uint256 amount1,
bytes calldata data
) external;
/// @notice Increase the maximum number of price and liquidity observations that this pool will store
/// @dev This method is no-op if the pool already has an observationCardinalityNext greater than or equal to
/// the input observationCardinalityNext.
/// @param observationCardinalityNext The desired minimum number of observations for the pool to store
function increaseObservationCardinalityNext(uint16 observationCardinalityNext) external;
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Pool state that is not stored
/// @notice Contains view functions to provide information about the pool that is computed rather than stored on the
/// blockchain. The functions here may have variable gas costs.
interface IUniswapV3PoolDerivedState {
/// @notice Returns the cumulative tick and liquidity as of each timestamp `secondsAgo` from the current block timestamp
/// @dev To get a time weighted average tick or liquidity-in-range, you must call this with two values, one representing
/// the beginning of the period and another for the end of the period. E.g., to get the last hour time-weighted average tick,
/// you must call it with secondsAgos = [3600, 0].
/// @dev The time weighted average tick represents the geometric time weighted average price of the pool, in
/// log base sqrt(1.0001) of token1 / token0. The TickMath library can be used to go from a tick value to a ratio.
/// @param secondsAgos From how long ago each cumulative tick and liquidity value should be returned
/// @return tickCumulatives Cumulative tick values as of each `secondsAgos` from the current block timestamp
/// @return secondsPerLiquidityCumulativeX128s Cumulative seconds per liquidity-in-range value as of each `secondsAgos` from the current block
/// timestamp
function observe(uint32[] calldata secondsAgos)
external
view
returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s);
/// @notice Returns a snapshot of the tick cumulative, seconds per liquidity and seconds inside a tick range
/// @dev Snapshots must only be compared to other snapshots, taken over a period for which a position existed.
/// I.e., snapshots cannot be compared if a position is not held for the entire period between when the first
/// snapshot is taken and the second snapshot is taken.
/// @param tickLower The lower tick of the range
/// @param tickUpper The upper tick of the range
/// @return tickCumulativeInside The snapshot of the tick accumulator for the range
/// @return secondsPerLiquidityInsideX128 The snapshot of seconds per liquidity for the range
/// @return secondsInside The snapshot of seconds per liquidity for the range
function snapshotCumulativesInside(int24 tickLower, int24 tickUpper)
external
view
returns (
int56 tickCumulativeInside,
uint160 secondsPerLiquidityInsideX128,
uint32 secondsInside
);
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Errors emitted by a pool
/// @notice Contains all events emitted by the pool
interface IUniswapV3PoolErrors {
error LOK();
error TLU();
error TLM();
error TUM();
error AI();
error M0();
error M1();
error AS();
error IIA();
error L();
error F0();
error F1();
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Events emitted by a pool
/// @notice Contains all events emitted by the pool
interface IUniswapV3PoolEvents {
/// @notice Emitted exactly once by a pool when #initialize is first called on the pool
/// @dev Mint/Burn/Swap cannot be emitted by the pool before Initialize
/// @param sqrtPriceX96 The initial sqrt price of the pool, as a Q64.96
/// @param tick The initial tick of the pool, i.e. log base 1.0001 of the starting price of the pool
event Initialize(uint160 sqrtPriceX96, int24 tick);
/// @notice Emitted when liquidity is minted for a given position
/// @param sender The address that minted the liquidity
/// @param owner The owner of the position and recipient of any minted liquidity
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param amount The amount of liquidity minted to the position range
/// @param amount0 How much token0 was required for the minted liquidity
/// @param amount1 How much token1 was required for the minted liquidity
event Mint(
address sender,
address indexed owner,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 amount,
uint256 amount0,
uint256 amount1
);
/// @notice Emitted when fees are collected by the owner of a position
/// @dev Collect events may be emitted with zero amount0 and amount1 when the caller chooses not to collect fees
/// @param owner The owner of the position for which fees are collected
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param amount0 The amount of token0 fees collected
/// @param amount1 The amount of token1 fees collected
event Collect(
address indexed owner,
address recipient,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 amount0,
uint128 amount1
);
/// @notice Emitted when a position's liquidity is removed
/// @dev Does not withdraw any fees earned by the liquidity position, which must be withdrawn via #collect
/// @param owner The owner of the position for which liquidity is removed
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param amount The amount of liquidity to remove
/// @param amount0 The amount of token0 withdrawn
/// @param amount1 The amount of token1 withdrawn
event Burn(
address indexed owner,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 amount,
uint256 amount0,
uint256 amount1
);
/// @notice Emitted by the pool for any swaps between token0 and token1
/// @param sender The address that initiated the swap call, and that received the callback
/// @param recipient The address that received the output of the swap
/// @param amount0 The delta of the token0 balance of the pool
/// @param amount1 The delta of the token1 balance of the pool
/// @param sqrtPriceX96 The sqrt(price) of the pool after the swap, as a Q64.96
/// @param liquidity The liquidity of the pool after the swap
/// @param tick The log base 1.0001 of price of the pool after the swap
event Swap(
address indexed sender,
address indexed recipient,
int256 amount0,
int256 amount1,
uint160 sqrtPriceX96,
uint128 liquidity,
int24 tick
);
/// @notice Emitted by the pool for any flashes of token0/token1
/// @param sender The address that initiated the swap call, and that received the callback
/// @param recipient The address that received the tokens from flash
/// @param amount0 The amount of token0 that was flashed
/// @param amount1 The amount of token1 that was flashed
/// @param paid0 The amount of token0 paid for the flash, which can exceed the amount0 plus the fee
/// @param paid1 The amount of token1 paid for the flash, which can exceed the amount1 plus the fee
event Flash(
address indexed sender,
address indexed recipient,
uint256 amount0,
uint256 amount1,
uint256 paid0,
uint256 paid1
);
/// @notice Emitted by the pool for increases to the number of observations that can be stored
/// @dev observationCardinalityNext is not the observation cardinality until an observation is written at the index
/// just before a mint/swap/burn.
/// @param observationCardinalityNextOld The previous value of the next observation cardinality
/// @param observationCardinalityNextNew The updated value of the next observation cardinality
event IncreaseObservationCardinalityNext(
uint16 observationCardinalityNextOld,
uint16 observationCardinalityNextNew
);
/// @notice Emitted when the protocol fee is changed by the pool
/// @param feeProtocol0Old The previous value of the token0 protocol fee
/// @param feeProtocol1Old The previous value of the token1 protocol fee
/// @param feeProtocol0New The updated value of the token0 protocol fee
/// @param feeProtocol1New The updated value of the token1 protocol fee
event SetFeeProtocol(uint8 feeProtocol0Old, uint8 feeProtocol1Old, uint8 feeProtocol0New, uint8 feeProtocol1New);
/// @notice Emitted when the collected protocol fees are withdrawn by the factory owner
/// @param sender The address that collects the protocol fees
/// @param recipient The address that receives the collected protocol fees
/// @param amount0 The amount of token0 protocol fees that is withdrawn
/// @param amount0 The amount of token1 protocol fees that is withdrawn
event CollectProtocol(address indexed sender, address indexed recipient, uint128 amount0, uint128 amount1);
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Pool state that never changes
/// @notice These parameters are fixed for a pool forever, i.e., the methods will always return the same values
interface IUniswapV3PoolImmutables {
/// @notice The contract that deployed the pool, which must adhere to the IUniswapV3Factory interface
/// @return The contract address
function factory() external view returns (address);
/// @notice The first of the two tokens of the pool, sorted by address
/// @return The token contract address
function token0() external view returns (address);
/// @notice The second of the two tokens of the pool, sorted by address
/// @return The token contract address
function token1() external view returns (address);
/// @notice The pool's fee in hundredths of a bip, i.e. 1e-6
/// @return The fee
function fee() external view returns (uint24);
/// @notice The pool tick spacing
/// @dev Ticks can only be used at multiples of this value, minimum of 1 and always positive
/// e.g.: a tickSpacing of 3 means ticks can be initialized every 3rd tick, i.e., ..., -6, -3, 0, 3, 6, ...
/// This value is an int24 to avoid casting even though it is always positive.
/// @return The tick spacing
function tickSpacing() external view returns (int24);
/// @notice The maximum amount of position liquidity that can use any tick in the range
/// @dev This parameter is enforced per tick to prevent liquidity from overflowing a uint128 at any point, and
/// also prevents out-of-range liquidity from being used to prevent adding in-range liquidity to a pool
/// @return The max amount of liquidity per tick
function maxLiquidityPerTick() external view returns (uint128);
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Permissioned pool actions
/// @notice Contains pool methods that may only be called by the factory owner
interface IUniswapV3PoolOwnerActions {
/// @notice Set the denominator of the protocol's % share of the fees
/// @param feeProtocol0 new protocol fee for token0 of the pool
/// @param feeProtocol1 new protocol fee for token1 of the pool
function setFeeProtocol(uint8 feeProtocol0, uint8 feeProtocol1) external;
/// @notice Collect the protocol fee accrued to the pool
/// @param recipient The address to which collected protocol fees should be sent
/// @param amount0Requested The maximum amount of token0 to send, can be 0 to collect fees in only token1
/// @param amount1Requested The maximum amount of token1 to send, can be 0 to collect fees in only token0
/// @return amount0 The protocol fee collected in token0
/// @return amount1 The protocol fee collected in token1
function collectProtocol(
address recipient,
uint128 amount0Requested,
uint128 amount1Requested
) external returns (uint128 amount0, uint128 amount1);
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Pool state that can change
/// @notice These methods compose the pool's state, and can change with any frequency including multiple times
/// per transaction
interface IUniswapV3PoolState {
/// @notice The 0th storage slot in the pool stores many values, and is exposed as a single method to save gas
/// when accessed externally.
/// @return sqrtPriceX96 The current price of the pool as a sqrt(token1/token0) Q64.96 value
/// @return tick The current tick of the pool, i.e. according to the last tick transition that was run.
/// This value may not always be equal to SqrtTickMath.getTickAtSqrtRatio(sqrtPriceX96) if the price is on a tick
/// boundary.
/// @return observationIndex The index of the last oracle observation that was written,
/// @return observationCardinality The current maximum number of observations stored in the pool,
/// @return observationCardinalityNext The next maximum number of observations, to be updated when the observation.
/// @return feeProtocol The protocol fee for both tokens of the pool.
/// Encoded as two 4 bit values, where the protocol fee of token1 is shifted 4 bits and the protocol fee of token0
/// is the lower 4 bits. Used as the denominator of a fraction of the swap fee, e.g. 4 means 1/4th of the swap fee.
/// unlocked Whether the pool is currently locked to reentrancy
function slot0()
external
view
returns (
uint160 sqrtPriceX96,
int24 tick,
uint16 observationIndex,
uint16 observationCardinality,
uint16 observationCardinalityNext,
uint8 feeProtocol,
bool unlocked
);
/// @notice The fee growth as a Q128.128 fees of token0 collected per unit of liquidity for the entire life of the pool
/// @dev This value can overflow the uint256
function feeGrowthGlobal0X128() external view returns (uint256);
/// @notice The fee growth as a Q128.128 fees of token1 collected per unit of liquidity for the entire life of the pool
/// @dev This value can overflow the uint256
function feeGrowthGlobal1X128() external view returns (uint256);
/// @notice The amounts of token0 and token1 that are owed to the protocol
/// @dev Protocol fees will never exceed uint128 max in either token
function protocolFees() external view returns (uint128 token0, uint128 token1);
/// @notice The currently in range liquidity available to the pool
/// @dev This value has no relationship to the total liquidity across all ticks
/// @return The liquidity at the current price of the pool
function liquidity() external view returns (uint128);
/// @notice Look up information about a specific tick in the pool
/// @param tick The tick to look up
/// @return liquidityGross the total amount of position liquidity that uses the pool either as tick lower or
/// tick upper
/// @return liquidityNet how much liquidity changes when the pool price crosses the tick,
/// @return feeGrowthOutside0X128 the fee growth on the other side of the tick from the current tick in token0,
/// @return feeGrowthOutside1X128 the fee growth on the other side of the tick from the current tick in token1,
/// @return tickCumulativeOutside the cumulative tick value on the other side of the tick from the current tick
/// @return secondsPerLiquidityOutsideX128 the seconds spent per liquidity on the other side of the tick from the current tick,
/// @return secondsOutside the seconds spent on the other side of the tick from the current tick,
/// @return initialized Set to true if the tick is initialized, i.e. liquidityGross is greater than 0, otherwise equal to false.
/// Outside values can only be used if the tick is initialized, i.e. if liquidityGross is greater than 0.
/// In addition, these values are only relative and must be used only in comparison to previous snapshots for
/// a specific position.
function ticks(int24 tick)
external
view
returns (
uint128 liquidityGross,
int128 liquidityNet,
uint256 feeGrowthOutside0X128,
uint256 feeGrowthOutside1X128,
int56 tickCumulativeOutside,
uint160 secondsPerLiquidityOutsideX128,
uint32 secondsOutside,
bool initialized
);
/// @notice Returns 256 packed tick initialized boolean values. See TickBitmap for more information
function tickBitmap(int16 wordPosition) external view returns (uint256);
/// @notice Returns the information about a position by the position's key
/// @param key The position's key is a hash of a preimage composed by the owner, tickLower and tickUpper
/// @return liquidity The amount of liquidity in the position,
/// @return feeGrowthInside0LastX128 fee growth of token0 inside the tick range as of the last mint/burn/poke,
/// @return feeGrowthInside1LastX128 fee growth of token1 inside the tick range as of the last mint/burn/poke,
/// @return tokensOwed0 the computed amount of token0 owed to the position as of the last mint/burn/poke,
/// @return tokensOwed1 the computed amount of token1 owed to the position as of the last mint/burn/poke
function positions(bytes32 key)
external
view
returns (
uint128 liquidity,
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128,
uint128 tokensOwed0,
uint128 tokensOwed1
);
/// @notice Returns data about a specific observation index
/// @param index The element of the observations array to fetch
/// @dev You most likely want to use #observe() instead of this method to get an observation as of some amount of time
/// ago, rather than at a specific index in the array.
/// @return blockTimestamp The timestamp of the observation,
/// @return tickCumulative the tick multiplied by seconds elapsed for the life of the pool as of the observation timestamp,
/// @return secondsPerLiquidityCumulativeX128 the seconds per in range liquidity for the life of the pool as of the observation timestamp,
/// @return initialized whether the observation has been initialized and the values are safe to use
function observations(uint256 index)
external
view
returns (
uint32 blockTimestamp,
int56 tickCumulative,
uint160 secondsPerLiquidityCumulativeX128,
bool initialized
);
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Callback for IUniswapV3PoolActions#swap
/// @notice Any contract that calls IUniswapV3PoolActions#swap must implement this interface
interface IUniswapV3SwapCallback {
/// @notice Called to `msg.sender` after executing a swap via IUniswapV3Pool#swap.
/// @dev In the implementation you must pay the pool tokens owed for the swap.
/// The caller of this method must be checked to be a UniswapV3Pool deployed by the canonical UniswapV3Factory.
/// amount0Delta and amount1Delta can both be 0 if no tokens were swapped.
/// @param amount0Delta The amount of token0 that was sent (negative) or must be received (positive) by the pool by
/// the end of the swap. If positive, the callback must send that amount of token0 to the pool.
/// @param amount1Delta The amount of token1 that was sent (negative) or must be received (positive) by the pool by
/// the end of the swap. If positive, the callback must send that amount of token1 to the pool.
/// @param data Any data passed through by the caller via the IUniswapV3PoolActions#swap call
function uniswapV3SwapCallback(
int256 amount0Delta,
int256 amount1Delta,
bytes calldata data
) external;
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
/// @title Interface for WETH9
interface IWETH9 is IERC20 {
/// @notice Deposit ether to get wrapped ether
function deposit() external payable;
/// @notice Withdraw wrapped ether to get ether
function withdraw(uint256) external;
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import '@uniswap/v3-core/contracts/libraries/FullMath.sol';
import '@uniswap/v3-core/contracts/libraries/FixedPoint96.sol';
/// @title Liquidity amount functions
/// @notice Provides functions for computing liquidity amounts from token amounts and prices
library LiquidityAmounts {
/// @notice Downcasts uint256 to uint128
/// @param x The uint258 to be downcasted
/// @return y The passed value, downcasted to uint128
function toUint128(uint256 x) private pure returns (uint128 y) {
require((y = uint128(x)) == x);
}
/// @notice Computes the amount of liquidity received for a given amount of token0 and price range
/// @dev Calculates amount0 * (sqrt(upper) * sqrt(lower)) / (sqrt(upper) - sqrt(lower))
/// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
/// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
/// @param amount0 The amount0 being sent in
/// @return liquidity The amount of returned liquidity
function getLiquidityForAmount0(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint256 amount0
) internal pure returns (uint128 liquidity) {
if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
uint256 intermediate = FullMath.mulDiv(sqrtRatioAX96, sqrtRatioBX96, FixedPoint96.Q96);
unchecked {
return toUint128(FullMath.mulDiv(amount0, intermediate, sqrtRatioBX96 - sqrtRatioAX96));
}
}
/// @notice Computes the amount of liquidity received for a given amount of token1 and price range
/// @dev Calculates amount1 / (sqrt(upper) - sqrt(lower)).
/// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
/// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
/// @param amount1 The amount1 being sent in
/// @return liquidity The amount of returned liquidity
function getLiquidityForAmount1(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint256 amount1
) internal pure returns (uint128 liquidity) {
if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
unchecked {
return toUint128(FullMath.mulDiv(amount1, FixedPoint96.Q96, sqrtRatioBX96 - sqrtRatioAX96));
}
}
/// @notice Computes the maximum amount of liquidity received for a given amount of token0, token1, the current
/// pool prices and the prices at the tick boundaries
/// @param sqrtRatioX96 A sqrt price representing the current pool prices
/// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
/// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
/// @param amount0 The amount of token0 being sent in
/// @param amount1 The amount of token1 being sent in
/// @return liquidity The maximum amount of liquidity received
function getLiquidityForAmounts(
uint160 sqrtRatioX96,
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint256 amount0,
uint256 amount1
) internal pure returns (uint128 liquidity) {
if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
if (sqrtRatioX96 <= sqrtRatioAX96) {
liquidity = getLiquidityForAmount0(sqrtRatioAX96, sqrtRatioBX96, amount0);
} else if (sqrtRatioX96 < sqrtRatioBX96) {
uint128 liquidity0 = getLiquidityForAmount0(sqrtRatioX96, sqrtRatioBX96, amount0);
uint128 liquidity1 = getLiquidityForAmount1(sqrtRatioAX96, sqrtRatioX96, amount1);
liquidity = liquidity0 < liquidity1 ? liquidity0 : liquidity1;
} else {
liquidity = getLiquidityForAmount1(sqrtRatioAX96, sqrtRatioBX96, amount1);
}
}
/// @notice Computes the amount of token0 for a given amount of liquidity and a price range
/// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
/// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
/// @param liquidity The liquidity being valued
/// @return amount0 The amount of token0
function getAmount0ForLiquidity(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint128 liquidity
) internal pure returns (uint256 amount0) {
unchecked {
if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
return
FullMath.mulDiv(
uint256(liquidity) << FixedPoint96.RESOLUTION,
sqrtRatioBX96 - sqrtRatioAX96,
sqrtRatioBX96
) / sqrtRatioAX96;
}
}
/// @notice Computes the amount of token1 for a given amount of liquidity and a price range
/// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
/// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
/// @param liquidity The liquidity being valued
/// @return amount1 The amount of token1
function getAmount1ForLiquidity(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint128 liquidity
) internal pure returns (uint256 amount1) {
if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
unchecked {
return FullMath.mulDiv(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96);
}
}
/// @notice Computes the token0 and token1 value for a given amount of liquidity, the current
/// pool prices and the prices at the tick boundaries
/// @param sqrtRatioX96 A sqrt price representing the current pool prices
/// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
/// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
/// @param liquidity The liquidity being valued
/// @return amount0 The amount of token0
/// @return amount1 The amount of token1
function getAmountsForLiquidity(
uint160 sqrtRatioX96,
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint128 liquidity
) internal pure returns (uint256 amount0, uint256 amount1) {
if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
if (sqrtRatioX96 <= sqrtRatioAX96) {
amount0 = getAmount0ForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity);
} else if (sqrtRatioX96 < sqrtRatioBX96) {
amount0 = getAmount0ForLiquidity(sqrtRatioX96, sqrtRatioBX96, liquidity);
amount1 = getAmount1ForLiquidity(sqrtRatioAX96, sqrtRatioX96, liquidity);
} else {
amount1 = getAmount1ForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity);
}
}
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import { PoolActions } from "./PoolActions.sol";
import { ICLTBase } from "../interfaces/ICLTBase.sol";
import { FullMath } from "@uniswap/v3-core/contracts/libraries/FullMath.sol";
/// @title LiquidityShares
/// @notice Provides functions for computing liquidity amounts and shares for individual strategy
library LiquidityShares {
/// @notice Returns the token reserves for individual strategy position in pool
/// @param key A51 strategy key details
/// @param liquidity The amount of liquidity for this strategy
/// @return reserves0 The amount of token0 in the liquidity position
/// @return reserves1 The amount of token1 in the liquidity position
function getReserves(
ICLTBase.StrategyKey memory key,
uint128 liquidity
)
public
returns (uint256 reserves0, uint256 reserves1)
{
PoolActions.updatePosition(key);
// check only for this strategy uniswap liquidity
// earnable0 & earnable1 will always returns zero becuase fee already claimed in updateGlobal
if (liquidity > 0) {
(,,, uint256 earnable0, uint256 earnable1) = PoolActions.getPositionLiquidity(key);
(uint256 burnable0, uint256 burnable1) = PoolActions.getAmountsForLiquidity(key, liquidity);
reserves0 = burnable0 + earnable0;
reserves1 = burnable1 + earnable1;
}
}
/// @notice Mints shares to the recipient based on the amount of tokens recieved.
/// @param strategy The individual strategy position to mint shares for
/// @param amount0Max The desired amount of token0 to be spent
/// @param amount1Max The desired amount of token1 to be spent
/// @return shares The amount of share minted to the sender
/// @return amount0 The amount of token0 needs to transfered from sender to strategy
/// @return amount1 The amount of token1 needs to transfered from sender to strategy
function computeLiquidityShare(
ICLTBase.StrategyData storage strategy,
uint256 amount0Max,
uint256 amount1Max
)
external
returns (uint256 shares, uint256 amount0, uint256 amount1)
{
// check existing liquidity before the add
(uint256 reserve0, uint256 reserve1) = getReserves(strategy.key, strategy.account.uniswapLiquidity);
// includes unused balance
reserve0 += strategy.account.balance0;
reserve1 += strategy.account.balance1;
// If total supply > 0, strategy can't be empty
assert(strategy.account.totalShares == 0 || reserve0 != 0 || reserve1 != 0);
(shares, amount0, amount1) =
calculateShare(amount0Max, amount1Max, reserve0, reserve1, strategy.account.totalShares);
}
/// @dev Calculates the largest possible `amount0` and `amount1` such that
/// they're in the same proportion as total amounts, but not greater than
/// `amount0Desired` and `amount1Desired` respectively.
/// @param amount0Max The desired amount of token0 to be spent
/// @param amount1Max The desired amount of token1 to be spent
/// @param reserve0 The strategy total holdings of token0
/// @param reserve1 The strategy total holdings of token1
/// @param totalSupply Total amount of shares minted from current strategy
/// @return shares The amount of share minted to the sender
/// @return amount0 The amount of token0 needs to transfered from sender to strategy
/// @return amount1 The amount of token1 needs to transfered from sender to strategy
function calculateShare(
uint256 amount0Max,
uint256 amount1Max,
uint256 reserve0,
uint256 reserve1,
uint256 totalSupply
)
public
pure
returns (uint256 shares, uint256 amount0, uint256 amount1)
{
if (totalSupply == 0) {
// For first deposit, just use the amounts desired
amount0 = amount0Max;
amount1 = amount1Max;
shares = amount0 > amount1 ? amount0 : amount1; // max
} else if (reserve0 == 0) {
amount1 = amount1Max;
shares = FullMath.mulDiv(amount1, totalSupply, reserve1);
} else if (reserve1 == 0) {
amount0 = amount0Max;
shares = FullMath.mulDiv(amount0, totalSupply, reserve0);
} else {
amount0 = FullMath.mulDiv(amount1Max, reserve0, reserve1);
if (amount0 < amount0Max) {
amount1 = amount1Max;
shares = FullMath.mulDiv(amount1, totalSupply, reserve1);
} else {
amount0 = amount0Max;
amount1 = FullMath.mulDiv(amount0, reserve1, reserve0);
shares = FullMath.mulDiv(amount0, totalSupply, reserve0);
}
}
}
}
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;
/// @notice Simple single owner authorization mixin.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/auth/Owned.sol)
abstract contract Owned {
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event OwnershipTransferred(address indexed user, address indexed newOwner);
/*//////////////////////////////////////////////////////////////
OWNERSHIP STORAGE
//////////////////////////////////////////////////////////////*/
address public owner;
modifier onlyOwner() virtual {
require(msg.sender == owner, "UNAUTHORIZED");
_;
}
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(address _owner) {
owner = _owner;
emit OwnershipTransferred(address(0), _owner);
}
/*//////////////////////////////////////////////////////////////
OWNERSHIP LOGIC
//////////////////////////////////////////////////////////////*/
function transferOwnership(address newOwner) public virtual onlyOwner {
owner = newOwner;
emit OwnershipTransferred(msg.sender, newOwner);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (security/Pausable.sol)
pragma solidity ^0.8.0;
import "../utils/Context.sol";
/**
* @dev Contract module which allows children to implement an emergency stop
* mechanism that can be triggered by an authorized account.
*
* This module is used through inheritance. It will make available the
* modifiers `whenNotPaused` and `whenPaused`, which can be applied to
* the functions of your contract. Note that they will not be pausable by
* simply including this module, only once the modifiers are put in place.
*/
abstract contract Pausable is Context {
/**
* @dev Emitted when the pause is triggered by `account`.
*/
event Paused(address account);
/**
* @dev Emitted when the pause is lifted by `account`.
*/
event Unpaused(address account);
bool private _paused;
/**
* @dev Initializes the contract in unpaused state.
*/
constructor() {
_paused = false;
}
/**
* @dev Modifier to make a function callable only when the contract is not paused.
*
* Requirements:
*
* - The contract must not be paused.
*/
modifier whenNotPaused() {
_requireNotPaused();
_;
}
/**
* @dev Modifier to make a function callable only when the contract is paused.
*
* Requirements:
*
* - The contract must be paused.
*/
modifier whenPaused() {
_requirePaused();
_;
}
/**
* @dev Returns true if the contract is paused, and false otherwise.
*/
function paused() public view virtual returns (bool) {
return _paused;
}
/**
* @dev Throws if the contract is paused.
*/
function _requireNotPaused() internal view virtual {
require(!paused(), "Pausable: paused");
}
/**
* @dev Throws if the contract is not paused.
*/
function _requirePaused() internal view virtual {
require(paused(), "Pausable: not paused");
}
/**
* @dev Triggers stopped state.
*
* Requirements:
*
* - The contract must not be paused.
*/
function _pause() internal virtual whenNotPaused {
_paused = true;
emit Paused(_msgSender());
}
/**
* @dev Returns to normal state.
*
* Requirements:
*
* - The contract must be paused.
*/
function _unpause() internal virtual whenPaused {
_paused = false;
emit Unpaused(_msgSender());
}
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import { Constants } from "./Constants.sol";
import { ICLTBase } from "../interfaces/ICLTBase.sol";
import { SafeCastExtended } from "./SafeCastExtended.sol";
import { ICLTPayments } from "../interfaces/ICLTPayments.sol";
import { PositionKey } from "@uniswap/v3-periphery/contracts/libraries/PositionKey.sol";
import { LiquidityAmounts } from "@uniswap/v3-periphery/contracts/libraries/LiquidityAmounts.sol";
import { FullMath } from "@uniswap/v3-core/contracts/libraries/FullMath.sol";
import { TickMath } from "@uniswap/v3-core/contracts/libraries/TickMath.sol";
import { IUniswapV3Pool } from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import { SqrtPriceMath } from "@uniswap/v3-core/contracts/libraries/SqrtPriceMath.sol";
/// @title PoolActions
/// @notice Provides functions for computing and safely managing liquidity on AMM
library PoolActions {
using SafeCastExtended for int256;
using SafeCastExtended for uint256;
/// @notice Returns the liquidity for individual strategy position in pool
/// @param key A51 strategy key details
/// @return liquidity The amount of liquidity for this strategy
function updatePosition(ICLTBase.StrategyKey memory key) external returns (uint128 liquidity) {
(liquidity,,,,) = getPositionLiquidity(key);
if (liquidity > 0) {
key.pool.burn(key.tickLower, key.tickUpper, 0);
}
}
/// @notice Burn complete liquidity of strategy in a range from pool
/// @param key A51 strategy key details
/// @param strategyliquidity The amount of liquidity to burn for this strategy
/// @return amount0 The amount of token0 that was accounted for the decrease in liquidity
/// @return amount1 The amount of token1 that was accounted for the decrease in liquidity
/// @return fees0 The amount of fees collected in token0
/// @return fees1 The amount of fees collected in token1
function burnLiquidity(
ICLTBase.StrategyKey memory key,
uint128 strategyliquidity
)
external
returns (uint256 amount0, uint256 amount1, uint256 fees0, uint256 fees1)
{
// only use individual liquidity of strategy we need otherwise it will pull all strategies liquidity
if (strategyliquidity > 0) {
(amount0, amount1) = key.pool.burn(key.tickLower, key.tickUpper, strategyliquidity);
if (amount0 > 0 || amount1 > 0) {
(uint256 collect0, uint256 collect1) =
key.pool.collect(address(this), key.tickLower, key.tickUpper, type(uint128).max, type(uint128).max);
(fees0, fees1) = (collect0 - amount0, collect1 - amount1);
}
}
}
/// @notice Burn liquidity in share proportion to the strategy's totalSupply
/// @param strategyliquidity The total amount of liquidity for this strategy
/// @param userSharePercentage The value of user share in strategy in terms of percentage
/// @return liquidity The amount of liquidity decrease
/// @return amount0 The amount of token0 withdrawn to the recipient
/// @return amount1 The amount of token1 withdrawn to the recipient
/// @return fees0 The amount of fees collected in token0 to the recipient
/// @return fees1 The amount of fees collected in token1 to the recipient
function burnUserLiquidity(
ICLTBase.StrategyKey storage key,
uint128 strategyliquidity,
uint256 userSharePercentage
)
external
returns (uint128 liquidity, uint256 amount0, uint256 amount1, uint256 fees0, uint256 fees1)
{
if (strategyliquidity > 0) {
liquidity = (FullMath.mulDiv(uint256(strategyliquidity), userSharePercentage, 1e18)).toUint128();
(amount0, amount1) = key.pool.burn(key.tickLower, key.tickUpper, liquidity);
if (amount0 > 0 || amount1 > 0) {
(uint256 collect0, uint256 collect1) =
key.pool.collect(address(this), key.tickLower, key.tickUpper, type(uint128).max, type(uint128).max);
(fees0, fees1) = (collect0 - amount0, collect1 - amount1);
}
}
}
/// @notice Adds liquidity for the given strategy/tickLower/tickUpper position
/// @param amount0Desired The amount of token0 that was paid for the increase in liquidity
/// @param amount1Desired The amount of token1 that was paid for the increase in liquidity
/// @return liquidity The amount of liquidity minted for this strategy
/// @return amount0 The amount of token0 added
/// @return amount1 The amount of token1 added
function mintLiquidity(
ICLTBase.StrategyKey memory key,
uint256 amount0Desired,
uint256 amount1Desired
)
public
returns (uint128 liquidity, uint256 amount0, uint256 amount1)
{
liquidity = getLiquidityForAmounts(key, amount0Desired, amount1Desired);
(amount0, amount1) = key.pool.mint(
address(this),
key.tickLower,
key.tickUpper,
liquidity,
abi.encode(
ICLTPayments.MintCallbackData({
token0: key.pool.token0(),
token1: key.pool.token1(),
fee: key.pool.fee(),
payer: address(this)
})
)
);
}
/// @notice Swap token0 for token1, or token1 for token0
/// @param pool The address of the AMM Pool
/// @param zeroForOne The direction of swap
/// @param amountSpecified The amount of tokens to swap
/// @return amount0 The delta of the balance of token0 of the pool, exact when negative, minimum when positive
/// @return amount1 The delta of the balance of token1 of the pool, exact when negative, minimum when positive
function swapToken(
IUniswapV3Pool pool,
bool zeroForOne,
int256 amountSpecified,
uint160 sqrtPriceLimitX96
)
external
returns (int256 amount0, int256 amount1)
{
(amount0, amount1) = pool.swap(
address(this),
zeroForOne,
amountSpecified,
sqrtPriceLimitX96,
abi.encode(ICLTPayments.SwapCallbackData({ token0: pool.token0(), token1: pool.token1(), fee: pool.fee() }))
);
}
/// @notice Collects up to a maximum amount of fees owed to a specific position to the recipient
/// @param key A51 strategy key details
/// @param tokensOwed0 The maximum amount of token0 to collect,
/// @param tokensOwed1 The maximum amount of token1 to collect
/// @param recipient The account that should receive the tokens,
/// @return collect0 The amount of fees collected in token0
/// @return collect1 The amount of fees collected in token1
function collectPendingFees(
ICLTBase.StrategyKey memory key,
uint128 tokensOwed0,
uint128 tokensOwed1,
address recipient
)
public
returns (uint256 collect0, uint256 collect1)
{
(collect0, collect1) = key.pool.collect(recipient, key.tickLower, key.tickUpper, tokensOwed0, tokensOwed1);
}
/// @notice Claims the trading fees earned and uses it to add liquidity.
/// @param key A51 strategy key details
/// @param balance0 Amount of token0 left in strategy that were not added in pool
/// @param balance1 Amount of token1 left in strategy that were not added in pool
/// @return liquidity The new liquidity amount as a result of the increase
/// @return balance0AfterMint The amount of token0 not added to the liquidity position
/// @return balance1AfterMint The amount of token1 not added to the liquidity position
function compoundFees(
ICLTBase.StrategyKey memory key,
uint256 balance0,
uint256 balance1
)
external
returns (uint128 liquidity, uint256 balance0AfterMint, uint256 balance1AfterMint)
{
(uint256 collect0, uint256 collect1) =
collectPendingFees(key, Constants.MAX_UINT128, Constants.MAX_UINT128, address(this));
(uint256 total0, uint256 total1) = (collect0 + balance0, collect1 + balance1);
if (getLiquidityForAmounts(key, total0, total1) > 0) {
(liquidity, collect0, collect1) = mintLiquidity(key, total0, total1);
(balance0AfterMint, balance1AfterMint) = (total0 - collect0, total1 - collect1);
}
}
/// @notice Get the info of the given strategy position
/// @param key A51 strategy key details
/// @return liquidity The amount of liquidity of the position
/// @return feeGrowthInside0LastX128 The fee growth of token0 as of the last action on the individual position
/// @return feeGrowthInside1LastX128 The fee growth of token1 as of the last action on the individual position
/// @return tokensOwed0 Amount of token0 owed
/// @return tokensOwed1 Amount of token1 owed
function getPositionLiquidity(ICLTBase.StrategyKey memory key)
public
view
returns (
uint128 liquidity,
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128,
uint128 tokensOwed0,
uint128 tokensOwed1
)
{
bytes32 positionKey = PositionKey.compute(address(this), key.tickLower, key.tickUpper);
(liquidity, feeGrowthInside0LastX128, feeGrowthInside1LastX128, tokensOwed0, tokensOwed1) =
key.pool.positions(positionKey);
}
/// @notice Computes the maximum amount of liquidity received for a given amount of token0, token1, the current
/// pool prices and the prices at the tick boundaries
/// @param key A51 strategy key details
/// @param amount0 The amount of token0 being sent in
/// @param amount1 The amount of token1 being sent in
/// @return liquidity The maximum amount of liquidity received
function getLiquidityForAmounts(
ICLTBase.StrategyKey memory key,
uint256 amount0,
uint256 amount1
)
public
view
returns (uint128)
{
(uint160 sqrtRatioX96,,,,,,) = key.pool.slot0();
return LiquidityAmounts.getLiquidityForAmounts(
sqrtRatioX96,
TickMath.getSqrtRatioAtTick(key.tickLower),
TickMath.getSqrtRatioAtTick(key.tickUpper),
amount0,
amount1
);
}
/// @notice Computes the token0 and token1 value for a given amount of liquidity, the current
/// pool prices and the prices at the tick boundaries
/// @param key A51 strategy key details
/// @param liquidity The liquidity being valued
/// @return amount0 The amount of token0
/// @return amount1 The amount of token1
function getAmountsForLiquidity(
ICLTBase.StrategyKey memory key,
uint128 liquidity
)
public
view
returns (uint256 amount0, uint256 amount1)
{
(uint160 sqrtRatioX96, int24 tick,,,,,) = key.pool.slot0();
int256 amount0Delta;
int256 amount1Delta;
if (tick < key.tickLower) {
amount0Delta = SqrtPriceMath.getAmount0Delta(
TickMath.getSqrtRatioAtTick(key.tickLower),
TickMath.getSqrtRatioAtTick(key.tickUpper),
int256(uint256(liquidity)).toInt128()
);
} else if (tick < key.tickUpper) {
amount0Delta = SqrtPriceMath.getAmount0Delta(
sqrtRatioX96, TickMath.getSqrtRatioAtTick(key.tickUpper), int256(uint256(liquidity)).toInt128()
);
amount1Delta = SqrtPriceMath.getAmount1Delta(
TickMath.getSqrtRatioAtTick(key.tickLower), sqrtRatioX96, int256(uint256(liquidity)).toInt128()
);
} else {
amount1Delta = SqrtPriceMath.getAmount1Delta(
TickMath.getSqrtRatioAtTick(key.tickLower),
TickMath.getSqrtRatioAtTick(key.tickUpper),
int256(uint256(liquidity)).toInt128()
);
}
(amount0, amount1) = (uint256(amount0Delta), uint256(amount1Delta));
}
/// @notice Look up information about a specific pool
/// @param pool The address of the AMM Pool
/// @return sqrtRatioX96 The current price of the pool as a sqrt(token1/token0) Q64.96 value
/// @return tick The current tick of the pool, i.e. according to the last tick transition that was run
/// @return observationCardinality The current maximum number of observations stored in the pool
function getSqrtRatioX96AndTick(IUniswapV3Pool pool)
public
view
returns (uint160 sqrtRatioX96, int24 tick, uint16 observationCardinality)
{
(sqrtRatioX96, tick,, observationCardinality,,,) = pool.slot0();
}
/// @notice Computes the direction of tokens recieved after swap to merge in strategy reserves
/// @param zeroForOne The direction of swap
/// @param amount0Recieved The delta of the balance of token0 of the pool
/// @param amount1Recieved The delta of the balance of token1 of the pool
/// @param amount0 The amount of token0 in the strategy position
/// @param amount1 The amount of token1 in the strategy position
/// @return reserves0 The total amount of token0 in the strategy position
/// @return reserves1 The total amount of token1 in the strategy position
function amountsDirection(
bool zeroForOne,
uint256 amount0Recieved,
uint256 amount1Recieved,
uint256 amount0,
uint256 amount1
)
external
pure
returns (uint256 reserves0, uint256 reserves1)
{
(reserves0, reserves1) = zeroForOne
? (amount0Recieved - amount0, amount1Recieved + amount1)
: (amount0Recieved + amount0, amount1Recieved - amount1);
}
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import { ICLTBase } from "../interfaces/ICLTBase.sol";
import { StrategyFeeShares } from "../libraries/StrategyFeeShares.sol";
/// @title Position
/// @notice Positions store state for indivdual A51 strategy and manage th
library Position {
/// @notice updates the liquidity and balance of strategy
/// @param self The individual strategy position to update
/// @param global The individual global position
/// @param liquidityAdded A new amount of liquidity added on AMM
/// @param share The amount of shares minted by strategy
/// @param amount0Desired The amount of token0 that was paid to mint the given amount of shares
/// @param amount1Desired The amount of token1 that was paid to mint the given amount of shares
/// @param amount0Added The actual amount of token0 added on AMM
/// @param amount1Added The actual amount of token1 added on AMM
function update(
ICLTBase.StrategyData storage self,
StrategyFeeShares.GlobalAccount storage global,
uint128 liquidityAdded,
uint256 share,
uint256 amount0Desired,
uint256 amount1Desired,
uint256 amount0Added,
uint256 amount1Added
)
public
{
uint256 balance0 = amount0Desired - amount0Added;
uint256 balance1 = amount1Desired - amount1Added;
if (balance0 > 0 || balance1 > 0) {
self.account.balance0 += balance0;
self.account.balance1 += balance1;
}
if (share > 0) {
bool isExit = getHodlStatus(self);
self.account.totalShares += share;
self.account.uniswapLiquidity += liquidityAdded;
if (isExit == false) global.totalLiquidity += share; //if liquidity HODL it shouldn't added on dex liquidity
}
}
/// @notice updates the position of strategy after fee compound
/// @param self The individual strategy position to update
/// @param liquidityAdded A new amount of liquidity added on AMM
/// @param amount0Added The amount of token0 added to the liquidity position
/// @param amount1Added The amount of token1 added to the liquidity position
function updateForCompound(
ICLTBase.StrategyData storage self,
uint128 liquidityAdded,
uint256 amount0Added,
uint256 amount1Added
)
public
{
// fees amounts that are not added on AMM will be in held in contract balance
self.account.balance0 = amount0Added;
self.account.balance1 = amount1Added;
self.account.fee0 = 0;
self.account.fee1 = 0;
self.account.uniswapLiquidity += liquidityAdded;
}
/// @notice updates the strategy and mint new position on AMM
/// @param self The individual strategy position to update
/// @param global The mapping containing all global positions
/// @param key A51 strategy key details
/// @param status Additional data of strategy passed through by the modules contract
/// @param liquidity A new amount of liquidity added on AMM
/// @param balance0 Amount of token0 left that are not added on AMM
/// @param balance1 Amount of token1 left that are not added on AMM
function updateStrategy(
ICLTBase.StrategyData storage self,
mapping(bytes32 => StrategyFeeShares.GlobalAccount) storage global,
ICLTBase.StrategyKey memory key,
bytes memory status,
uint128 liquidity,
uint256 balance0,
uint256 balance1
)
public
{
StrategyFeeShares.GlobalAccount storage globalAccount =
global[keccak256(abi.encodePacked(key.pool, key.tickLower, key.tickUpper))];
self.key = key;
// remaining assets are held in contract
self.account.balance0 = balance0;
self.account.balance1 = balance1;
self.actionStatus = status;
self.account.uniswapLiquidity = liquidity;
bool isExit = getHodlStatus(self);
// if liquidity is on HODL it shouldn't recieve fee shares but calculations will remain for existing users
if (isExit == false) globalAccount.totalLiquidity += self.account.totalShares;
// fee should remain for non-compounding strategy existing users
if (self.isCompound) {
self.account.fee0 = 0;
self.account.fee1 = 0;
}
// assigning again feeGrowth here because if position ticks are changed then calculations will be messed
self.account.feeGrowthOutside0LastX128 = globalAccount.feeGrowthInside0LastX128;
self.account.feeGrowthOutside1LastX128 = globalAccount.feeGrowthInside1LastX128;
}
/// @notice updates the info of strategy
/// @param self The individual strategy position to update
/// @param newOwner The address of owner to update
/// @param managementFee The percentage of management fee to update
/// @param performanceFee The percentage of performance fee to update
/// @param newActions The ids of new modes to update
/// @dev The status of previous actions will be overwrite after update
function updateStrategyState(
ICLTBase.StrategyData storage self,
address newOwner,
uint256 managementFee,
uint256 performanceFee,
bytes memory newActions
)
public
{
self.actions = newActions;
if (self.owner != newOwner) self.owner = newOwner;
if (self.managementFee != managementFee) self.managementFee = managementFee;
if (self.performanceFee != performanceFee) self.performanceFee = performanceFee;
bool isExit = getHodlStatus(self);
if (isExit) {
self.actionStatus = abi.encode(0, isExit);
} else {
self.actionStatus = "";
}
}
function getHodlStatus(ICLTBase.StrategyData storage self) public view returns (bool isExit) {
if (self.actionStatus.length > 0) {
(, isExit) = abi.decode(self.actionStatus, (uint256, bool));
}
}
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
library PositionKey {
/// @dev Returns the key of the position in the core library
function compute(
address owner,
int24 tickLower,
int24 tickUpper
) internal pure returns (bytes32) {
return keccak256(abi.encodePacked(owner, tickLower, tickUpper));
}
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Safe casting methods
/// @notice Contains methods for safely casting between types
library SafeCast {
/// @notice Cast a uint256 to a uint160, revert on overflow
/// @param y The uint256 to be downcasted
/// @return z The downcasted integer, now type uint160
function toUint160(uint256 y) internal pure returns (uint160 z) {
require((z = uint160(y)) == y);
}
/// @notice Cast a int256 to a int128, revert on overflow or underflow
/// @param y The int256 to be downcasted
/// @return z The downcasted integer, now type int128
function toInt128(int256 y) internal pure returns (int128 z) {
require((z = int128(y)) == y);
}
/// @notice Cast a uint256 to a int256, revert on overflow
/// @param y The uint256 to be casted
/// @return z The casted integer, now type int256
function toInt256(uint256 y) internal pure returns (int256 z) {
require(y < 2**255);
z = int256(y);
}
}
// SPDX-License-Identifier: MIT
pragma solidity =0.8.15;
/**
* @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
* checks.
*
* Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
* easily result in undesired exploitation or bugs, since developers usually
* assume that overflows raise errors. `SafeCast` restores this intuition by
* reverting the transaction when such an operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*
* Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing
* all math on `uint256` and `int256` and then downcasting.
*/
library SafeCastExtended {
/**
* @dev Returns the downcasted uint128 from uint256, reverting on
* overflow (when the input is greater than largest uint128).
*
* Counterpart to Solidity's `uint128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*/
function toUint128(uint256 value) internal pure returns (uint128) {
require(value < 2 ** 128, "SafeCast: value doesn't fit in 128 bits");
return uint128(value);
}
/**
* @dev Returns the downcasted uint64 from uint256, reverting on
* overflow (when the input is greater than largest uint64).
*
* Counterpart to Solidity's `uint64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*/
function toUint64(uint256 value) internal pure returns (uint64) {
require(value < 2 ** 64, "SafeCast: value doesn't fit in 64 bits");
return uint64(value);
}
/**
* @dev Returns the downcasted uint32 from uint256, reverting on
* overflow (when the input is greater than largest uint32).
*
* Counterpart to Solidity's `uint32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*/
function toUint32(uint256 value) internal pure returns (uint32) {
require(value < 2 ** 32, "SafeCast: value doesn't fit in 32 bits");
return uint32(value);
}
/**
* @dev Returns the downcasted uint16 from uint256, reverting on
* overflow (when the input is greater than largest uint16).
*
* Counterpart to Solidity's `uint16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*/
function toUint16(uint256 value) internal pure returns (uint16) {
require(value < 2 ** 16, "SafeCast: value doesn't fit in 16 bits");
return uint16(value);
}
/**
* @dev Returns the downcasted uint8 from uint256, reverting on
* overflow (when the input is greater than largest uint8).
*
* Counterpart to Solidity's `uint8` operator.
*
* Requirements:
*
* - input must fit into 8 bits.
*/
function toUint8(uint256 value) internal pure returns (uint8) {
require(value < 2 ** 8, "SafeCast: value doesn't fit in 8 bits");
return uint8(value);
}
/**
* @dev Converts a signed int256 into an unsigned uint256.
*
* Requirements:
*
* - input must be greater than or equal to 0.
*/
function toUint256(int256 value) internal pure returns (uint256) {
require(value >= 0, "SafeCast: value must be positive");
return uint256(value);
}
/**
* @dev Returns the downcasted int128 from int256, reverting on
* overflow (when the input is less than smallest int128 or
* greater than largest int128).
*
* Counterpart to Solidity's `int128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*
* _Available since v3.1._
*/
function toInt128(int256 value) internal pure returns (int128) {
require(value >= -2 ** 127 && value < 2 ** 127, "SafeCast: value doesn't fit in 128 bits");
return int128(value);
}
/**
* @dev Returns the downcasted int64 from int256, reverting on
* overflow (when the input is less than smallest int64 or
* greater than largest int64).
*
* Counterpart to Solidity's `int64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*
* _Available since v3.1._
*/
function toInt64(int256 value) internal pure returns (int64) {
require(value >= -2 ** 63 && value < 2 ** 63, "SafeCast: value doesn't fit in 64 bits");
return int64(value);
}
/**
* @dev Returns the downcasted int32 from int256, reverting on
* overflow (when the input is less than smallest int32 or
* greater than largest int32).
*
* Counterpart to Solidity's `int32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*
* _Available since v3.1._
*/
function toInt32(int256 value) internal pure returns (int32) {
require(value >= -2 ** 31 && value < 2 ** 31, "SafeCast: value doesn't fit in 32 bits");
return int32(value);
}
/**
* @dev Returns the downcasted int16 from int256, reverting on
* overflow (when the input is less than smallest int16 or
* greater than largest int16).
*
* Counterpart to Solidity's `int16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*
* _Available since v3.1._
*/
function toInt16(int256 value) internal pure returns (int16) {
require(value >= -2 ** 15 && value < 2 ** 15, "SafeCast: value doesn't fit in 16 bits");
return int16(value);
}
/**
* @dev Returns the downcasted int8 from int256, reverting on
* overflow (when the input is less than smallest int8 or
* greater than largest int8).
*
* Counterpart to Solidity's `int8` operator.
*
* Requirements:
*
* - input must fit into 8 bits.
*
* _Available since v3.1._
*/
function toInt8(int256 value) internal pure returns (int8) {
require(value >= -2 ** 7 && value < 2 ** 7, "SafeCast: value doesn't fit in 8 bits");
return int8(value);
}
/**
* @dev Converts an unsigned uint256 into a signed int256.
*
* Requirements:
*
* - input must be less than or equal to maxInt256.
*/
function toInt256(uint256 value) internal pure returns (int256) {
require(value < 2 ** 255, "SafeCast: value doesn't fit in an int256");
return int256(value);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/math/SafeMath.sol)
pragma solidity ^0.8.0;
// CAUTION
// This version of SafeMath should only be used with Solidity 0.8 or later,
// because it relies on the compiler's built in overflow checks.
/**
* @dev Wrappers over Solidity's arithmetic operations.
*
* NOTE: `SafeMath` is generally not needed starting with Solidity 0.8, since the compiler
* now has built in overflow checking.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the subtraction of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b > a) return (false, 0);
return (true, a - b);
}
}
/**
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the division of two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b == 0) return (false, 0);
return (true, a / b);
}
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b == 0) return (false, 0);
return (true, a % b);
}
}
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
return a + b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return a - b;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
return a * b;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator.
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return a % b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {trySub}.
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
unchecked {
require(b <= a, errorMessage);
return a - b;
}
}
/**
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
unchecked {
require(b > 0, errorMessage);
return a / b;
}
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting with custom message when dividing by zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryMod}.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
unchecked {
require(b > 0, errorMessage);
return a % b;
}
}
}
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
import {SafeCast} from './SafeCast.sol';
import {FullMath} from './FullMath.sol';
import {UnsafeMath} from './UnsafeMath.sol';
import {FixedPoint96} from './FixedPoint96.sol';
/// @title Functions based on Q64.96 sqrt price and liquidity
/// @notice Contains the math that uses square root of price as a Q64.96 and liquidity to compute deltas
library SqrtPriceMath {
using SafeCast for uint256;
/// @notice Gets the next sqrt price given a delta of token0
/// @dev Always rounds up, because in the exact output case (increasing price) we need to move the price at least
/// far enough to get the desired output amount, and in the exact input case (decreasing price) we need to move the
/// price less in order to not send too much output.
/// The most precise formula for this is liquidity * sqrtPX96 / (liquidity +- amount * sqrtPX96),
/// if this is impossible because of overflow, we calculate liquidity / (liquidity / sqrtPX96 +- amount).
/// @param sqrtPX96 The starting price, i.e. before accounting for the token0 delta
/// @param liquidity The amount of usable liquidity
/// @param amount How much of token0 to add or remove from virtual reserves
/// @param add Whether to add or remove the amount of token0
/// @return The price after adding or removing amount, depending on add
function getNextSqrtPriceFromAmount0RoundingUp(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amount,
bool add
) internal pure returns (uint160) {
// we short circuit amount == 0 because the result is otherwise not guaranteed to equal the input price
if (amount == 0) return sqrtPX96;
uint256 numerator1 = uint256(liquidity) << FixedPoint96.RESOLUTION;
if (add) {
unchecked {
uint256 product;
if ((product = amount * sqrtPX96) / amount == sqrtPX96) {
uint256 denominator = numerator1 + product;
if (denominator >= numerator1)
// always fits in 160 bits
return uint160(FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator));
}
}
// denominator is checked for overflow
return uint160(UnsafeMath.divRoundingUp(numerator1, (numerator1 / sqrtPX96) + amount));
} else {
unchecked {
uint256 product;
// if the product overflows, we know the denominator underflows
// in addition, we must check that the denominator does not underflow
require((product = amount * sqrtPX96) / amount == sqrtPX96 && numerator1 > product);
uint256 denominator = numerator1 - product;
return FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator).toUint160();
}
}
}
/// @notice Gets the next sqrt price given a delta of token1
/// @dev Always rounds down, because in the exact output case (decreasing price) we need to move the price at least
/// far enough to get the desired output amount, and in the exact input case (increasing price) we need to move the
/// price less in order to not send too much output.
/// The formula we compute is within <1 wei of the lossless version: sqrtPX96 +- amount / liquidity
/// @param sqrtPX96 The starting price, i.e., before accounting for the token1 delta
/// @param liquidity The amount of usable liquidity
/// @param amount How much of token1 to add, or remove, from virtual reserves
/// @param add Whether to add, or remove, the amount of token1
/// @return The price after adding or removing `amount`
function getNextSqrtPriceFromAmount1RoundingDown(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amount,
bool add
) internal pure returns (uint160) {
// if we're adding (subtracting), rounding down requires rounding the quotient down (up)
// in both cases, avoid a mulDiv for most inputs
if (add) {
uint256 quotient = (
amount <= type(uint160).max
? (amount << FixedPoint96.RESOLUTION) / liquidity
: FullMath.mulDiv(amount, FixedPoint96.Q96, liquidity)
);
return (uint256(sqrtPX96) + quotient).toUint160();
} else {
uint256 quotient = (
amount <= type(uint160).max
? UnsafeMath.divRoundingUp(amount << FixedPoint96.RESOLUTION, liquidity)
: FullMath.mulDivRoundingUp(amount, FixedPoint96.Q96, liquidity)
);
require(sqrtPX96 > quotient);
// always fits 160 bits
unchecked {
return uint160(sqrtPX96 - quotient);
}
}
}
/// @notice Gets the next sqrt price given an input amount of token0 or token1
/// @dev Throws if price or liquidity are 0, or if the next price is out of bounds
/// @param sqrtPX96 The starting price, i.e., before accounting for the input amount
/// @param liquidity The amount of usable liquidity
/// @param amountIn How much of token0, or token1, is being swapped in
/// @param zeroForOne Whether the amount in is token0 or token1
/// @return sqrtQX96 The price after adding the input amount to token0 or token1
function getNextSqrtPriceFromInput(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amountIn,
bool zeroForOne
) internal pure returns (uint160 sqrtQX96) {
require(sqrtPX96 > 0);
require(liquidity > 0);
// round to make sure that we don't pass the target price
return
zeroForOne
? getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true)
: getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountIn, true);
}
/// @notice Gets the next sqrt price given an output amount of token0 or token1
/// @dev Throws if price or liquidity are 0 or the next price is out of bounds
/// @param sqrtPX96 The starting price before accounting for the output amount
/// @param liquidity The amount of usable liquidity
/// @param amountOut How much of token0, or token1, is being swapped out
/// @param zeroForOne Whether the amount out is token0 or token1
/// @return sqrtQX96 The price after removing the output amount of token0 or token1
function getNextSqrtPriceFromOutput(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amountOut,
bool zeroForOne
) internal pure returns (uint160 sqrtQX96) {
require(sqrtPX96 > 0);
require(liquidity > 0);
// round to make sure that we pass the target price
return
zeroForOne
? getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountOut, false)
: getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountOut, false);
}
/// @notice Gets the amount0 delta between two prices
/// @dev Calculates liquidity / sqrt(lower) - liquidity / sqrt(upper),
/// i.e. liquidity * (sqrt(upper) - sqrt(lower)) / (sqrt(upper) * sqrt(lower))
/// @param sqrtRatioAX96 A sqrt price
/// @param sqrtRatioBX96 Another sqrt price
/// @param liquidity The amount of usable liquidity
/// @param roundUp Whether to round the amount up or down
/// @return amount0 Amount of token0 required to cover a position of size liquidity between the two passed prices
function getAmount0Delta(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint128 liquidity,
bool roundUp
) internal pure returns (uint256 amount0) {
unchecked {
if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
uint256 numerator1 = uint256(liquidity) << FixedPoint96.RESOLUTION;
uint256 numerator2 = sqrtRatioBX96 - sqrtRatioAX96;
require(sqrtRatioAX96 > 0);
return
roundUp
? UnsafeMath.divRoundingUp(
FullMath.mulDivRoundingUp(numerator1, numerator2, sqrtRatioBX96),
sqrtRatioAX96
)
: FullMath.mulDiv(numerator1, numerator2, sqrtRatioBX96) / sqrtRatioAX96;
}
}
/// @notice Gets the amount1 delta between two prices
/// @dev Calculates liquidity * (sqrt(upper) - sqrt(lower))
/// @param sqrtRatioAX96 A sqrt price
/// @param sqrtRatioBX96 Another sqrt price
/// @param liquidity The amount of usable liquidity
/// @param roundUp Whether to round the amount up, or down
/// @return amount1 Amount of token1 required to cover a position of size liquidity between the two passed prices
function getAmount1Delta(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint128 liquidity,
bool roundUp
) internal pure returns (uint256 amount1) {
unchecked {
if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
return
roundUp
? FullMath.mulDivRoundingUp(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96)
: FullMath.mulDiv(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96);
}
}
/// @notice Helper that gets signed token0 delta
/// @param sqrtRatioAX96 A sqrt price
/// @param sqrtRatioBX96 Another sqrt price
/// @param liquidity The change in liquidity for which to compute the amount0 delta
/// @return amount0 Amount of token0 corresponding to the passed liquidityDelta between the two prices
function getAmount0Delta(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
int128 liquidity
) internal pure returns (int256 amount0) {
unchecked {
return
liquidity < 0
? -getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(-liquidity), false).toInt256()
: getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(liquidity), true).toInt256();
}
}
/// @notice Helper that gets signed token1 delta
/// @param sqrtRatioAX96 A sqrt price
/// @param sqrtRatioBX96 Another sqrt price
/// @param liquidity The change in liquidity for which to compute the amount1 delta
/// @return amount1 Amount of token1 corresponding to the passed liquidityDelta between the two prices
function getAmount1Delta(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
int128 liquidity
) internal pure returns (int256 amount1) {
unchecked {
return
liquidity < 0
? -getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(-liquidity), false).toInt256()
: getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(liquidity), true).toInt256();
}
}
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import { ICLTBase } from "../interfaces/ICLTBase.sol";
import { Constants } from "../libraries/Constants.sol";
import { PoolActions } from "../libraries/PoolActions.sol";
import { FixedPoint128 } from "../libraries/FixedPoint128.sol";
import { SafeMath } from "@openzeppelin/contracts/utils/math/SafeMath.sol";
import { FullMath } from "@uniswap/v3-core/contracts/libraries/FullMath.sol";
/// @title StrategyFeeShares
/// @notice StrategyFeeShares contains methods for tracking fees owed to the strategy w.r.t global fees
library StrategyFeeShares {
/// @param positionFee0 The uncollected amount of token0 owed to the global position as of the last computation
/// @param positionFee1 The uncollected amount of token1 owed to the global position as of the last computation
/// @param totalLiquidity The sum of liquidity of all strategies having global position ticks
/// @param feeGrowthInside0LastX128 The all-time fee growth in token0, per unit of liquidity, inside the position's
/// tick boundaries
/// @param feeGrowthInside1LastX128 The all-time fee growth in token1, per unit of liquidity, inside the position's
/// tick boundaries
struct GlobalAccount {
uint256 positionFee0;
uint256 positionFee1;
uint256 totalLiquidity;
uint256 feeGrowthInside0LastX128;
uint256 feeGrowthInside1LastX128;
}
/// @notice Collects total uncollected fee owed to the global position from AMM & updates all-time fee growth
/// @param self The individual global position to update
/// @param key A51 strategy key details
/// @return account The position info struct of the given global position
function updateGlobalStrategyFees(
mapping(bytes32 => GlobalAccount) storage self,
ICLTBase.StrategyKey memory key
)
external
returns (GlobalAccount storage account)
{
account = self[keccak256(abi.encodePacked(key.pool, key.tickLower, key.tickUpper))];
PoolActions.updatePosition(key);
if (account.totalLiquidity > 0) {
(uint256 fees0, uint256 fees1) =
PoolActions.collectPendingFees(key, Constants.MAX_UINT128, Constants.MAX_UINT128, address(this));
account.positionFee0 += fees0;
account.positionFee1 += fees1;
account.feeGrowthInside0LastX128 += FullMath.mulDiv(fees0, FixedPoint128.Q128, account.totalLiquidity);
account.feeGrowthInside1LastX128 += FullMath.mulDiv(fees1, FixedPoint128.Q128, account.totalLiquidity);
}
}
/// @notice Credits accumulated fees to a strategy from global position
/// @param self The individual strategy position to update
/// @param global The individual global position
/// @dev strategy will not recieve fee share from global position because it's liquidity is HODL in contract balance
/// during activation of exit mode
function updateStrategyFees(
ICLTBase.StrategyData storage self,
GlobalAccount storage global
)
external
returns (uint256 total0, uint256 total1)
{
(uint256 feeGrowthInside0LastX128, uint256 feeGrowthInside1LastX128) =
(global.feeGrowthInside0LastX128, global.feeGrowthInside1LastX128);
bool isExit;
if (self.actionStatus.length > 0) {
(, isExit) = abi.decode(self.actionStatus, (uint256, bool));
}
if (isExit == false) {
// calculate accumulated fees
total0 = uint128(
FullMath.mulDiv(
feeGrowthInside0LastX128 - self.account.feeGrowthOutside0LastX128,
self.account.totalShares,
FixedPoint128.Q128
)
);
total1 = uint128(
FullMath.mulDiv(
feeGrowthInside1LastX128 - self.account.feeGrowthOutside1LastX128,
self.account.totalShares,
FixedPoint128.Q128
)
);
}
// precesion loss expected here so rounding the value to zero to prevent underflow
(, global.positionFee0) = SafeMath.trySub(global.positionFee0, total0);
(, global.positionFee1) = SafeMath.trySub(global.positionFee1, total1);
// update the position
self.account.fee0 += total0;
self.account.fee1 += total1;
// assign fee growth from upper global of ticks
self.account.feeGrowthOutside0LastX128 = feeGrowthInside0LastX128;
self.account.feeGrowthOutside1LastX128 = feeGrowthInside1LastX128;
// increament fee growth for all the users inside strategy
if (self.account.totalShares > 0) {
self.account.feeGrowthInside0LastX128 +=
FullMath.mulDiv(total0, FixedPoint128.Q128, self.account.totalShares);
self.account.feeGrowthInside1LastX128 +=
FullMath.mulDiv(total1, FixedPoint128.Q128, self.account.totalShares);
}
}
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;
/// @title Math library for computing sqrt prices from ticks and vice versa
/// @notice Computes sqrt price for ticks of size 1.0001, i.e. sqrt(1.0001^tick) as fixed point Q64.96 numbers. Supports
/// prices between 2**-128 and 2**128
library TickMath {
error T();
error R();
/// @dev The minimum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**-128
int24 internal constant MIN_TICK = -887272;
/// @dev The maximum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**128
int24 internal constant MAX_TICK = -MIN_TICK;
/// @dev The minimum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MIN_TICK)
uint160 internal constant MIN_SQRT_RATIO = 4295128739;
/// @dev The maximum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MAX_TICK)
uint160 internal constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342;
/// @notice Calculates sqrt(1.0001^tick) * 2^96
/// @dev Throws if |tick| > max tick
/// @param tick The input tick for the above formula
/// @return sqrtPriceX96 A Fixed point Q64.96 number representing the sqrt of the ratio of the two assets (token1/token0)
/// at the given tick
function getSqrtRatioAtTick(int24 tick) internal pure returns (uint160 sqrtPriceX96) {
unchecked {
uint256 absTick = tick < 0 ? uint256(-int256(tick)) : uint256(int256(tick));
if (absTick > uint256(int256(MAX_TICK))) revert T();
uint256 ratio = absTick & 0x1 != 0
? 0xfffcb933bd6fad37aa2d162d1a594001
: 0x100000000000000000000000000000000;
if (absTick & 0x2 != 0) ratio = (ratio * 0xfff97272373d413259a46990580e213a) >> 128;
if (absTick & 0x4 != 0) ratio = (ratio * 0xfff2e50f5f656932ef12357cf3c7fdcc) >> 128;
if (absTick & 0x8 != 0) ratio = (ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0) >> 128;
if (absTick & 0x10 != 0) ratio = (ratio * 0xffcb9843d60f6159c9db58835c926644) >> 128;
if (absTick & 0x20 != 0) ratio = (ratio * 0xff973b41fa98c081472e6896dfb254c0) >> 128;
if (absTick & 0x40 != 0) ratio = (ratio * 0xff2ea16466c96a3843ec78b326b52861) >> 128;
if (absTick & 0x80 != 0) ratio = (ratio * 0xfe5dee046a99a2a811c461f1969c3053) >> 128;
if (absTick & 0x100 != 0) ratio = (ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4) >> 128;
if (absTick & 0x200 != 0) ratio = (ratio * 0xf987a7253ac413176f2b074cf7815e54) >> 128;
if (absTick & 0x400 != 0) ratio = (ratio * 0xf3392b0822b70005940c7a398e4b70f3) >> 128;
if (absTick & 0x800 != 0) ratio = (ratio * 0xe7159475a2c29b7443b29c7fa6e889d9) >> 128;
if (absTick & 0x1000 != 0) ratio = (ratio * 0xd097f3bdfd2022b8845ad8f792aa5825) >> 128;
if (absTick & 0x2000 != 0) ratio = (ratio * 0xa9f746462d870fdf8a65dc1f90e061e5) >> 128;
if (absTick & 0x4000 != 0) ratio = (ratio * 0x70d869a156d2a1b890bb3df62baf32f7) >> 128;
if (absTick & 0x8000 != 0) ratio = (ratio * 0x31be135f97d08fd981231505542fcfa6) >> 128;
if (absTick & 0x10000 != 0) ratio = (ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9) >> 128;
if (absTick & 0x20000 != 0) ratio = (ratio * 0x5d6af8dedb81196699c329225ee604) >> 128;
if (absTick & 0x40000 != 0) ratio = (ratio * 0x2216e584f5fa1ea926041bedfe98) >> 128;
if (absTick & 0x80000 != 0) ratio = (ratio * 0x48a170391f7dc42444e8fa2) >> 128;
if (tick > 0) ratio = type(uint256).max / ratio;
// this divides by 1<<32 rounding up to go from a Q128.128 to a Q128.96.
// we then downcast because we know the result always fits within 160 bits due to our tick input constraint
// we round up in the division so getTickAtSqrtRatio of the output price is always consistent
sqrtPriceX96 = uint160((ratio >> 32) + (ratio % (1 << 32) == 0 ? 0 : 1));
}
}
/// @notice Calculates the greatest tick value such that getRatioAtTick(tick) <= ratio
/// @dev Throws in case sqrtPriceX96 < MIN_SQRT_RATIO, as MIN_SQRT_RATIO is the lowest value getRatioAtTick may
/// ever return.
/// @param sqrtPriceX96 The sqrt ratio for which to compute the tick as a Q64.96
/// @return tick The greatest tick for which the ratio is less than or equal to the input ratio
function getTickAtSqrtRatio(uint160 sqrtPriceX96) internal pure returns (int24 tick) {
unchecked {
// second inequality must be < because the price can never reach the price at the max tick
if (!(sqrtPriceX96 >= MIN_SQRT_RATIO && sqrtPriceX96 < MAX_SQRT_RATIO)) revert R();
uint256 ratio = uint256(sqrtPriceX96) << 32;
uint256 r = ratio;
uint256 msb = 0;
assembly {
let f := shl(7, gt(r, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(6, gt(r, 0xFFFFFFFFFFFFFFFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(5, gt(r, 0xFFFFFFFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(4, gt(r, 0xFFFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(3, gt(r, 0xFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(2, gt(r, 0xF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(1, gt(r, 0x3))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := gt(r, 0x1)
msb := or(msb, f)
}
if (msb >= 128) r = ratio >> (msb - 127);
else r = ratio << (127 - msb);
int256 log_2 = (int256(msb) - 128) << 64;
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(63, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(62, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(61, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(60, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(59, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(58, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(57, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(56, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(55, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(54, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(53, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(52, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(51, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(50, f))
}
int256 log_sqrt10001 = log_2 * 255738958999603826347141; // 128.128 number
int24 tickLow = int24((log_sqrt10001 - 3402992956809132418596140100660247210) >> 128);
int24 tickHi = int24((log_sqrt10001 + 291339464771989622907027621153398088495) >> 128);
tick = tickLow == tickHi ? tickLow : getSqrtRatioAtTick(tickHi) <= sqrtPriceX96 ? tickHi : tickLow;
}
}
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.6.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
library TransferHelper {
/// @notice Transfers tokens from the targeted address to the given destination
/// @notice Errors with 'STF' if transfer fails
/// @param token The contract address of the token to be transferred
/// @param from The originating address from which the tokens will be transferred
/// @param to The destination address of the transfer
/// @param value The amount to be transferred
function safeTransferFrom(address token, address from, address to, uint256 value) external {
(bool success, bytes memory data) =
token.call(abi.encodeWithSelector(IERC20.transferFrom.selector, from, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), "STF");
}
/// @notice Transfers tokens from msg.sender to a recipient
/// @dev Errors with ST if transfer fails
/// @param token The contract address of the token which will be transferred
/// @param to The recipient of the transfer
/// @param value The value of the transfer
function safeTransfer(address token, address to, uint256 value) external {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(IERC20.transfer.selector, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), "ST");
}
/// @notice Approves the stipulated contract to spend the given allowance in the given token
/// @dev Errors with 'SA' if transfer fails
/// @param token The contract address of the token to be approved
/// @param to The target of the approval
/// @param value The amount of the given token the target will be allowed to spend
function safeApprove(address token, address to, uint256 value) external {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(IERC20.approve.selector, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), "SA");
}
/// @notice Transfers ETH to the recipient address
/// @dev Fails with `STE`
/// @param to The destination of the transfer
/// @param value The value to be transferred
function safeTransferETH(address to, uint256 value) external {
(bool success,) = to.call{ value: value }(new bytes(0));
require(success, "STE");
}
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Math functions that do not check inputs or outputs
/// @notice Contains methods that perform common math functions but do not do any overflow or underflow checks
library UnsafeMath {
/// @notice Returns ceil(x / y)
/// @dev division by 0 has unspecified behavior, and must be checked externally
/// @param x The dividend
/// @param y The divisor
/// @return z The quotient, ceil(x / y)
function divRoundingUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
assembly {
z := add(div(x, y), gt(mod(x, y), 0))
}
}
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import { ICLTBase } from "../interfaces/ICLTBase.sol";
import { SafeCastExtended } from "./SafeCastExtended.sol";
import { FixedPoint128 } from "../libraries/FixedPoint128.sol";
import { FullMath } from "@uniswap/v3-core/contracts/libraries/FullMath.sol";
import { SafeMath } from "@openzeppelin/contracts/utils/math/SafeMath.sol";
/// @title UserPositions
/// @notice UserPositions store additional state for tracking fees owed to the user compound or no compound strategy
library UserPositions {
using SafeCastExtended for uint256;
struct Data {
bytes32 strategyId;
uint256 liquidityShare;
uint256 feeGrowthInside0LastX128;
uint256 feeGrowthInside1LastX128;
uint128 tokensOwed0;
uint128 tokensOwed1;
}
/// @notice Collects up to a maximum amount of fees owed to a user from the strategy fees
/// @param self The individual user position to update
/// @param feeGrowthInside0LastX128 The all-time fee growth in token0, per unit of liquidity in strategy
/// @param feeGrowthInside1LastX128 The all-time fee growth in token1, per unit of liquidity in strategy
function updateUserPosition(
Data storage self,
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128
)
public
{
self.tokensOwed0 += FullMath.mulDiv(
feeGrowthInside0LastX128 - self.feeGrowthInside0LastX128, self.liquidityShare, FixedPoint128.Q128
).toUint128();
self.tokensOwed1 += FullMath.mulDiv(
feeGrowthInside1LastX128 - self.feeGrowthInside1LastX128, self.liquidityShare, FixedPoint128.Q128
).toUint128();
self.feeGrowthInside0LastX128 = feeGrowthInside0LastX128;
self.feeGrowthInside1LastX128 = feeGrowthInside1LastX128;
}
/// @notice Collects up to a maximum amount of fees owed to a user poistion in non-compounding strategy
/// @param self The individual user position to update
/// @param strategy The individual strategy position
/// @return total0 The amount of fees collected in token0
/// @return total1 The amount of fees collected in token1
function claimFeeForNonCompounders(
Data storage self,
ICLTBase.StrategyData storage strategy
)
public
returns (uint128 total0, uint128 total1)
{
(uint128 tokensOwed0, uint128 tokensOwed1) = (self.tokensOwed0, self.tokensOwed1);
(uint256 feeGrowthInside0LastX128, uint256 feeGrowthInside1LastX128) =
(strategy.account.feeGrowthInside0LastX128, strategy.account.feeGrowthInside1LastX128);
total0 = tokensOwed0
+ uint128(
FullMath.mulDiv(
feeGrowthInside0LastX128 - self.feeGrowthInside0LastX128, self.liquidityShare, FixedPoint128.Q128
)
);
total1 = tokensOwed1
+ uint128(
FullMath.mulDiv(
feeGrowthInside1LastX128 - self.feeGrowthInside1LastX128, self.liquidityShare, FixedPoint128.Q128
)
);
self.feeGrowthInside0LastX128 = feeGrowthInside0LastX128;
self.feeGrowthInside1LastX128 = feeGrowthInside1LastX128;
self.tokensOwed0 = total0;
self.tokensOwed1 = total1;
// precesion loss expected here so rounding the value to zero to prevent underflow
(, strategy.account.fee0) = SafeMath.trySub(strategy.account.fee0, total0);
(, strategy.account.fee1) = SafeMath.trySub(strategy.account.fee1, total1);
}
/// @notice Collects up to a maximum amount of fees owed to a user poistion in compounding strategy
/// @param self The individual user position to update
/// @param strategy The individual strategy position
/// @return fee0 The amount of fees collected in token0
/// @return fee1 The amount of fees collected in token1
function claimFeeForCompounders(
Data storage self,
ICLTBase.StrategyData storage strategy
)
public
returns (uint256 fee0, uint256 fee1)
{
fee0 = FullMath.mulDiv(strategy.account.fee0, self.liquidityShare, strategy.account.totalShares);
fee1 = FullMath.mulDiv(strategy.account.fee1, self.liquidityShare, strategy.account.totalShares);
(, strategy.account.fee0) = SafeMath.trySub(strategy.account.fee0, fee0);
(, strategy.account.fee1) = SafeMath.trySub(strategy.account.fee1, fee1);
}
}
{
"compilationTarget": {
"src/CLTBase.sol": "CLTBase"
},
"evmVersion": "london",
"libraries": {
"src/libraries/LiquidityShares.sol:LiquidityShares": "0xb0ca4bbb5c66276bea8b3349234ddeab1fbda050",
"src/libraries/PoolActions.sol:PoolActions": "0xcf47aa71ad8f734ce727f0d4ea357af7eb014f43",
"src/libraries/Position.sol:Position": "0xa8dc31c8c9f93db2e42a5472f580689794639576",
"src/libraries/StrategyFeeShares.sol:StrategyFeeShares": "0x4cc3305ee36ea4b8763aad4465a016aed944342e",
"src/libraries/TransferHelper.sol:TransferHelper": "0x525c80e91efe9222de3eae86af69a480fbced416",
"src/libraries/UserPositions.sol:UserPositions": "0xe626411bb9fa3e5c0d31e03996ac2e335cd315e4"
},
"metadata": {
"bytecodeHash": "none"
},
"optimizer": {
"enabled": true,
"runs": 4000
},
"remappings": [
":@openzeppelin/=lib/openzeppelin-contracts/",
":@solmate/=lib/solmate/src/",
":@uniswap/v3-core/=lib/v3-core/",
":@uniswap/v3-periphery/=lib/v3-periphery/",
":ds-test/=lib/forge-std/lib/ds-test/src/",
":erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
":forge-std/=lib/forge-std/src/",
":openzeppelin-contracts/=lib/openzeppelin-contracts/",
":openzeppelin/=lib/openzeppelin-contracts/contracts/",
":prb-test/=lib/prb-test/src/",
":solmate/=lib/solmate/src/",
":v3-core/=lib/v3-core/contracts/",
":v3-periphery/=lib/v3-periphery/contracts/"
]
}
[{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"address","name":"_owner","type":"address"},{"internalType":"address","name":"_weth9","type":"address"},{"internalType":"address","name":"_feeHandler","type":"address"},{"internalType":"address","name":"_cltModules","type":"address"},{"internalType":"contract IUniswapV3Factory","name":"_factory","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"InvalidCaller","type":"error"},{"inputs":[],"name":"InvalidInput","type":"error"},{"inputs":[],"name":"InvalidShare","type":"error"},{"inputs":[],"name":"MinimumAmountsExceeded","type":"error"},{"inputs":[],"name":"NoLiquidity","type":"error"},{"inputs":[],"name":"OwnerCannotBeZeroAddress","type":"error"},{"inputs":[],"name":"TransactionTooAged","type":"error"},{"inputs":[],"name":"onlyNonCompounders","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":true,"internalType":"uint256","name":"id","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"operator","type":"address"},{"indexed":false,"internalType":"bool","name":"approved","type":"bool"}],"name":"ApprovalForAll","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount0Collected","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1Collected","type":"uint256"}],"name":"Collect","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"liquidity","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"strategyId","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"}],"name":"FeeCompounded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"strategyId","type":"bytes32"},{"indexed":false,"internalType":"bool","name":"isLiquidityMinted","type":"bool"},{"indexed":false,"internalType":"bool","name":"zeroForOne","type":"bool"},{"indexed":false,"internalType":"int256","name":"swapAmount","type":"int256"}],"name":"LiquidityShifted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Paused","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"share","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"}],"name":"PositionUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"strategyId","type":"bytes32"}],"name":"StrategyCreated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"strategyId","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"fee0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"fee1","type":"uint256"}],"name":"StrategyFee","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"strategyId","type":"bytes32"}],"name":"StrategyUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"uint256","name":"id","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Unpaused","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"liquidity","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"fee0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"fee1","type":"uint256"}],"name":"Withdraw","type":"event"},{"stateMutability":"payable","type":"fallback"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"id","type":"uint256"}],"name":"approve","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"bool","name":"refundAsETH","type":"bool"}],"internalType":"struct ICLTBase.ClaimFeesParams","name":"params","type":"tuple"}],"name":"claimPositionFee","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"cltModules","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"contract IUniswapV3Pool","name":"pool","type":"address"},{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"}],"internalType":"struct ICLTBase.StrategyKey","name":"key","type":"tuple"},{"components":[{"internalType":"uint256","name":"mode","type":"uint256"},{"components":[{"internalType":"bytes32","name":"actionName","type":"bytes32"},{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct ICLTBase.StrategyPayload[]","name":"exitStrategy","type":"tuple[]"},{"components":[{"internalType":"bytes32","name":"actionName","type":"bytes32"},{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct ICLTBase.StrategyPayload[]","name":"rebaseStrategy","type":"tuple[]"},{"components":[{"internalType":"bytes32","name":"actionName","type":"bytes32"},{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct ICLTBase.StrategyPayload[]","name":"liquidityDistribution","type":"tuple[]"}],"internalType":"struct ICLTBase.PositionActions","name":"actions","type":"tuple"},{"internalType":"uint256","name":"managementFee","type":"uint256"},{"internalType":"uint256","name":"performanceFee","type":"uint256"},{"internalType":"bool","name":"isCompound","type":"bool"},{"internalType":"bool","name":"isPrivate","type":"bool"}],"name":"createStrategy","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"bytes32","name":"strategyId","type":"bytes32"},{"internalType":"uint256","name":"amount0Desired","type":"uint256"},{"internalType":"uint256","name":"amount1Desired","type":"uint256"},{"internalType":"uint256","name":"amount0Min","type":"uint256"},{"internalType":"uint256","name":"amount1Min","type":"uint256"},{"internalType":"address","name":"recipient","type":"address"}],"internalType":"struct ICLTBase.DepositParams","name":"params","type":"tuple"}],"name":"deposit","outputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint256","name":"share","type":"uint256"},{"internalType":"uint256","name":"amount0","type":"uint256"},{"internalType":"uint256","name":"amount1","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"feeHandler","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"getApproved","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"strategyId","type":"bytes32"}],"name":"getStrategyReserves","outputs":[{"internalType":"uint128","name":"liquidity","type":"uint128"},{"internalType":"uint256","name":"fee0","type":"uint256"},{"internalType":"uint256","name":"fee1","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"getUserfee","outputs":[{"internalType":"uint256","name":"fee0","type":"uint256"},{"internalType":"uint256","name":"fee1","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"isApprovedForAll","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_operator","type":"address"}],"name":"isOperator","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"id","type":"uint256"}],"name":"ownerOf","outputs":[{"internalType":"address","name":"owner","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"paused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"positions","outputs":[{"internalType":"bytes32","name":"strategyId","type":"bytes32"},{"internalType":"uint256","name":"liquidityShare","type":"uint256"},{"internalType":"uint256","name":"feeGrowthInside0LastX128","type":"uint256"},{"internalType":"uint256","name":"feeGrowthInside1LastX128","type":"uint256"},{"internalType":"uint128","name":"tokensOwed0","type":"uint128"},{"internalType":"uint128","name":"tokensOwed1","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"id","type":"uint256"}],"name":"safeTransferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"safeTransferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"operator","type":"address"},{"internalType":"bool","name":"approved","type":"bool"}],"name":"setApprovalForAll","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"components":[{"internalType":"contract IUniswapV3Pool","name":"pool","type":"address"},{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"}],"internalType":"struct ICLTBase.StrategyKey","name":"key","type":"tuple"},{"internalType":"bytes32","name":"strategyId","type":"bytes32"},{"internalType":"bool","name":"shouldMint","type":"bool"},{"internalType":"bool","name":"zeroForOne","type":"bool"},{"internalType":"int256","name":"swapAmount","type":"int256"},{"internalType":"bytes","name":"moduleStatus","type":"bytes"},{"internalType":"uint160","name":"sqrtPriceLimitX96","type":"uint160"}],"internalType":"struct ICLTBase.ShiftLiquidityParams","name":"params","type":"tuple"}],"name":"shiftLiquidity","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"strategies","outputs":[{"components":[{"internalType":"contract IUniswapV3Pool","name":"pool","type":"address"},{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"}],"internalType":"struct ICLTBase.StrategyKey","name":"key","type":"tuple"},{"internalType":"address","name":"owner","type":"address"},{"internalType":"bytes","name":"actions","type":"bytes"},{"internalType":"bytes","name":"actionStatus","type":"bytes"},{"internalType":"bool","name":"isCompound","type":"bool"},{"internalType":"bool","name":"isPrivate","type":"bool"},{"internalType":"uint256","name":"managementFee","type":"uint256"},{"internalType":"uint256","name":"performanceFee","type":"uint256"},{"components":[{"internalType":"uint256","name":"fee0","type":"uint256"},{"internalType":"uint256","name":"fee1","type":"uint256"},{"internalType":"uint256","name":"balance0","type":"uint256"},{"internalType":"uint256","name":"balance1","type":"uint256"},{"internalType":"uint256","name":"totalShares","type":"uint256"},{"internalType":"uint128","name":"uniswapLiquidity","type":"uint128"},{"internalType":"uint256","name":"feeGrowthInside0LastX128","type":"uint256"},{"internalType":"uint256","name":"feeGrowthInside1LastX128","type":"uint256"},{"internalType":"uint256","name":"feeGrowthOutside0LastX128","type":"uint256"},{"internalType":"uint256","name":"feeGrowthOutside1LastX128","type":"uint256"}],"internalType":"struct ICLTBase.Account","name":"account","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_operator","type":"address"}],"name":"toggleOperator","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"id","type":"uint256"}],"name":"tokenURI","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"id","type":"uint256"}],"name":"transferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount0Owed","type":"uint256"},{"internalType":"uint256","name":"amount1Owed","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"uniswapV3MintCallback","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"int256","name":"amount0Delta","type":"int256"},{"internalType":"int256","name":"amount1Delta","type":"int256"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"uniswapV3SwapCallback","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unpause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint256","name":"amount0Desired","type":"uint256"},{"internalType":"uint256","name":"amount1Desired","type":"uint256"},{"internalType":"uint256","name":"amount0Min","type":"uint256"},{"internalType":"uint256","name":"amount1Min","type":"uint256"}],"internalType":"struct ICLTBase.UpdatePositionParams","name":"params","type":"tuple"}],"name":"updatePositionLiquidity","outputs":[{"internalType":"uint256","name":"share","type":"uint256"},{"internalType":"uint256","name":"amount0","type":"uint256"},{"internalType":"uint256","name":"amount1","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"strategyId","type":"bytes32"},{"internalType":"address","name":"owner","type":"address"},{"internalType":"uint256","name":"managementFee","type":"uint256"},{"internalType":"uint256","name":"performanceFee","type":"uint256"},{"components":[{"internalType":"uint256","name":"mode","type":"uint256"},{"components":[{"internalType":"bytes32","name":"actionName","type":"bytes32"},{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct ICLTBase.StrategyPayload[]","name":"exitStrategy","type":"tuple[]"},{"components":[{"internalType":"bytes32","name":"actionName","type":"bytes32"},{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct ICLTBase.StrategyPayload[]","name":"rebaseStrategy","type":"tuple[]"},{"components":[{"internalType":"bytes32","name":"actionName","type":"bytes32"},{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct ICLTBase.StrategyPayload[]","name":"liquidityDistribution","type":"tuple[]"}],"internalType":"struct ICLTBase.PositionActions","name":"actions","type":"tuple"}],"name":"updateStrategyBase","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"bool","name":"refundAsETH","type":"bool"},{"internalType":"uint256","name":"amount0Min","type":"uint256"},{"internalType":"uint256","name":"amount1Min","type":"uint256"}],"internalType":"struct ICLTBase.WithdrawParams","name":"params","type":"tuple"}],"name":"withdraw","outputs":[{"internalType":"uint256","name":"amount0","type":"uint256"},{"internalType":"uint256","name":"amount1","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]