Verified contractExactMIT
LiquidityManager
Address0x3ae711896a02754eec03aee3d6b5c2ea882e6ef9
- Balance
0 ETH
- Transactions
5
- Compiler
solc 0.8.20
- Same code
Only this contract
Details
- Compiler
- solc 0.8.20+commit.a1b79de6
- Language
- solidity
- Optimization
- Yes, 200 runs
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 23,863,253
- Creation transaction
- 0x95157df349…1d51da526c
LiquidityManager.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface AutomationCompatibleInterface {
function checkUpkeep(bytes calldata checkData) external view returns (bool upkeepNeeded, bytes memory performData);
function performUpkeep(bytes calldata performData) external;
}
interface IERC20 {
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 value) external returns (bool);
function transfer(address to, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
interface IWETH is IERC20 {
function deposit() external payable;
function withdraw(uint256) external;
}
interface IERC721Receiver {
function onERC721Received(address operator, address from, uint256 tokenId, bytes calldata data) external returns (bytes4);
}
interface IUniswapV3SwapCallback {
function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata data) external;
}
interface IUniswapV3Pool {
function slot0() external view returns (uint160 sqrtPriceX96, int24 tick, uint16 observationIndex, uint16 observationCardinality, uint16 observationCardinalityNext, uint8 feeProtocol, bool unlocked);
function liquidity() external view returns (uint128);
function tickSpacing() external view returns (int24);
function swap(address recipient, bool zeroForOne, int256 amountSpecified, uint160 sqrtPriceLimitX96, bytes calldata data) external returns (int256 amount0, int256 amount1);
}
interface INonfungiblePositionManager {
struct MintParams { address token0; address token1; uint24 fee; int24 tickLower; int24 tickUpper; uint256 amount0Desired; uint256 amount1Desired; uint256 amount0Min; uint256 amount1Min; address recipient; uint256 deadline; }
struct IncreaseLiquidityParams { uint256 tokenId; uint256 amount0Desired; uint256 amount1Desired; uint256 amount0Min; uint256 amount1Min; uint256 deadline; }
struct DecreaseLiquidityParams { uint256 tokenId; uint128 liquidity; uint256 amount0Min; uint256 amount1Min; uint256 deadline; }
struct CollectParams { uint256 tokenId; address recipient; uint128 amount0Max; uint128 amount1Max; }
function positions(uint256 tokenId) external view returns (uint96 nonce, address operator, address token0, address token1, uint24 fee, int24 tickLower, int24 tickUpper, uint128 liquidity, uint256 feeGrowthInside0LastX128, uint256 feeGrowthInside1LastX128, uint128 tokensOwed0, uint128 tokensOwed1);
function mint(MintParams calldata params) external payable returns (uint256 tokenId, uint128 liquidity, uint256 amount0, uint256 amount1);
function increaseLiquidity(IncreaseLiquidityParams calldata params) external payable returns (uint128 liquidity, uint256 amount0, uint256 amount1);
function decreaseLiquidity(DecreaseLiquidityParams calldata params) external payable returns (uint256 amount0, uint256 amount1);
function collect(CollectParams calldata params) external payable returns (uint256 amount0, uint256 amount1);
function burn(uint256 tokenId) external payable;
function safeTransferFrom(address from, address to, uint256 tokenId) external;
}
library FullMath {
function mulDiv(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
uint256 prod0;
uint256 prod1;
assembly {
let mm := mulmod(a, b, not(0))
prod0 := mul(a, b)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
if (prod1 == 0) {
require(denominator > 0, "DIV_ZERO");
return prod0 / denominator;
}
require(denominator > prod1, "OVERFLOW");
uint256 remainder;
assembly {
remainder := mulmod(a, b, denominator)
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
uint256 twos = denominator & (~denominator + 1);
assembly {
denominator := div(denominator, twos)
prod0 := div(prod0, twos)
twos := add(div(sub(0, twos), twos), 1)
}
prod0 |= prod1 * twos;
uint256 inv = 3 * denominator ^ 2;
inv *= 2 - denominator * inv;
inv *= 2 - denominator * inv;
inv *= 2 - denominator * inv;
inv *= 2 - denominator * inv;
inv *= 2 - denominator * inv;
inv *= 2 - denominator * inv;
result = prod0 * inv;
return result;
}
}
}
library TickMath {
int24 internal constant MIN_TICK = -887272;
int24 internal constant MAX_TICK = -MIN_TICK;
uint160 internal constant MIN_SQRT_RATIO = 4295128739 + 1;
uint160 internal constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342 - 1;
function getSqrtRatioAtTick(int24 tick) internal pure returns (uint160 sqrtPriceX96) {
unchecked {
require(tick >= MIN_TICK && tick <= MAX_TICK, "TICK_RANGE");
uint256 absTick = tick < 0 ? uint256(uint24(-tick)) : uint256(uint24(tick));
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;
sqrtPriceX96 = uint160((ratio >> 32) + (ratio % (1 << 32) == 0 ? 0 : 1));
}
}
}
library SafeERC20 {
function safeTransfer(IERC20 token, address to, uint256 value) internal { require(token.transfer(to, value), "TRANSFER_FAIL"); }
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal { require(token.transferFrom(from, to, value), "TRANSFER_FROM_FAIL"); }
function safeApprove(IERC20 token, address spender, uint256 value) internal { require(token.approve(spender, value), "APPROVE_FAIL"); }
}
abstract contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() { _status = _NOT_ENTERED; }
modifier nonReentrant() { require(_status != _ENTERED, "REENTRANCY"); _status = _ENTERED; _; _status = _NOT_ENTERED; }
}
abstract contract Ownable {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor() { _transferOwnership(msg.sender); }
modifier onlyOwner() { require(owner() == msg.sender, "NOT_OWNER"); _; }
function owner() public view returns (address) { return _owner; }
function transferOwnership(address newOwner) external onlyOwner { require(newOwner != address(0), "ZERO_OWNER"); _transferOwnership(newOwner); }
function _transferOwnership(address newOwner) internal { address oldOwner = _owner; _owner = newOwner; emit OwnershipTransferred(oldOwner, newOwner); }
}
contract LiquidityManager is Ownable, ReentrancyGuard, IERC721Receiver, IUniswapV3SwapCallback, AutomationCompatibleInterface {
using SafeERC20 for IERC20;
address public constant ENA = 0x57e114B691Db790C35207b2e685D4A43181e6061;
address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address public constant POOL = 0xc3Db44ADC1fCdFd5671f555236eae49f4A8EEa18;
address public constant POSITION_MANAGER = 0xC36442b4a4522E871399CD717aBDD847Ab11FE88;
uint24 public constant POOL_FEE = 3000;
int24 public constant TICK_SPACING = 60;
uint24 public constant SLIPPAGE_BPS = 50;
uint256 private constant BPS_DENOMINATOR = 10_000;
uint256 private constant DEADLINE_BUFFER = 15 minutes;
uint24 public constant PRICE_LIMIT_BPS = 50;
uint160 private constant MIN_SQRT_RATIO = 4295128739 + 1;
uint160 private constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342 - 1;
INonfungiblePositionManager private constant positionManager = INonfungiblePositionManager(POSITION_MANAGER);
IUniswapV3Pool private constant pool = IUniswapV3Pool(POOL);
IERC20 private constant token0 = IERC20(ENA);
IERC20 private constant token1 = IERC20(WETH);
uint256 public positionTokenId;
int24 public lastLowerTick;
int24 public lastUpperTick;
address public automation;
int24 public bandWidthTicks = 1800;
uint256 public lastCompoundTimestamp;
uint256 public constant COMPOUND_INTERVAL = 24 hours;
event Rebalanced(uint256 tokenId, int24 lowerTick, int24 upperTick, uint128 liquidityAdded);
event Compounded(uint256 tokenId, uint128 liquidityAdded);
event Deposited(uint256 amountENA, uint256 amountWETH);
event Withdrawn(uint256 shareBps, uint256 amountENA, uint256 amountWETH);
event AutomationUpdated(address indexed automation);
event EmergencyWithdrawn(uint256 amountENA, uint256 amountWETH);
event BandWidthUpdated(int24 width);
modifier hasPosition() { require(positionTokenId != 0, "NO_POSITION"); _; }
modifier onlyOwnerOrAutomation() { require(msg.sender == owner() || msg.sender == automation, "NOT_AUTHORIZED"); _; }
// --- Chainlink ---
function checkUpkeep(bytes calldata) external view override returns (bool upkeepNeeded, bytes memory performData) {
(bool needsRebalance, , , , ) = this.checkRebalance();
if (needsRebalance) return (true, abi.encode(uint8(1)));
bool canCompound = positionTokenId != 0 && (block.timestamp - lastCompoundTimestamp > COMPOUND_INTERVAL);
if (canCompound) return (true, abi.encode(uint8(2)));
return (false, bytes(""));
}
function performUpkeep(bytes calldata performData) external override {
uint8 action = abi.decode(performData, (uint8));
if (action == 1) {
(bool needsRebalance, , , , ) = this.checkRebalance();
if (needsRebalance) executeRebalance();
} else if (action == 2) {
bool canCompound = positionTokenId != 0 && (block.timestamp - lastCompoundTimestamp > COMPOUND_INTERVAL);
if (canCompound) autoCompound();
}
}
// --- Views ---
function checkRebalance() external view returns (bool needsRebalance, int24 currentTick, int24 lowerTick, int24 upperTick, uint128 liquidity) {
(, currentTick, , , , , ) = pool.slot0();
if (positionTokenId == 0) return (true, currentTick, 0, 0, 0);
(, , , , , lowerTick, upperTick, liquidity, , , , ) = positionManager.positions(positionTokenId);
needsRebalance = currentTick <= lowerTick || currentTick >= upperTick;
}
function getPosition() external view returns (uint256 tokenId, int24 lowerTick, int24 upperTick, uint128 liquidity) {
tokenId = positionTokenId;
if (tokenId == 0) return (0, 0, 0, 0);
(, , , , , lowerTick, upperTick, liquidity, , , , ) = positionManager.positions(tokenId);
}
// --- Admin ---
function setAutomation(address automation_) external onlyOwner {
automation = automation_;
emit AutomationUpdated(automation_);
}
function setBandWidthTicks(int24 newWidth) external onlyOwner {
require(newWidth > TICK_SPACING && newWidth % TICK_SPACING == 0 && newWidth <= 20_000, "INVALID_WIDTH");
bandWidthTicks = newWidth;
emit BandWidthUpdated(newWidth);
}
// --- Core ---
function executeRebalance() public onlyOwnerOrAutomation nonReentrant {
(, int24 currentTick, , , ) = _getSlot0();
_closePosition();
(int24 lowerTick, int24 upperTick) = _targetTicks(currentTick);
_mintWithBalances(lowerTick, upperTick);
}
function autoCompound() public onlyOwnerOrAutomation hasPosition nonReentrant {
lastCompoundTimestamp = block.timestamp;
_collect(positionTokenId);
_addToExisting();
}
function deposit(uint256 amountENA, uint256 amountWETH) external onlyOwner nonReentrant {
if (amountENA > 0) token0.safeTransferFrom(msg.sender, address(this), amountENA);
if (amountWETH > 0) token1.safeTransferFrom(msg.sender, address(this), amountWETH);
emit Deposited(amountENA, amountWETH);
if (positionTokenId != 0) _addToExisting();
}
function withdraw(uint256 shareBps) external onlyOwner nonReentrant {
require(shareBps <= BPS_DENOMINATOR, "BPS_OOB");
if (positionTokenId != 0 && shareBps > 0) {
(, , , , , , , uint128 liquidity, , , , ) = positionManager.positions(positionTokenId);
if (liquidity > 0) {
uint128 liquidityToWithdraw = uint128((uint256(liquidity) * shareBps) / BPS_DENOMINATOR);
if (liquidityToWithdraw > 0) {
positionManager.decreaseLiquidity(
INonfungiblePositionManager.DecreaseLiquidityParams({
tokenId: positionTokenId,
liquidity: liquidityToWithdraw,
amount0Min: 0,
amount1Min: 0,
deadline: block.timestamp + DEADLINE_BUFFER
})
);
_collect(positionTokenId);
}
}
}
_sendToOwner(shareBps);
}
function emergencyWithdraw() external onlyOwner nonReentrant {
_closePosition();
_sendToOwner(BPS_DENOMINATOR);
emit EmergencyWithdrawn(0, 0);
}
function rescueNFT() external onlyOwner nonReentrant {
uint256 tokenId = positionTokenId;
require(tokenId != 0, "NO_NFT");
positionTokenId = 0;
lastLowerTick = 0;
lastUpperTick = 0;
positionManager.safeTransferFrom(address(this), owner(), tokenId);
}
function rescueToken(address tokenAddress) external onlyOwner nonReentrant {
IERC20 token = IERC20(tokenAddress);
uint256 balance = token.balanceOf(address(this));
if (balance > 0) SafeERC20.safeTransfer(token, owner(), balance);
}
// --- Helpers ---
function _sendToOwner(uint256 shareBps) internal {
uint256 amount0Out = (token0.balanceOf(address(this)) * shareBps) / BPS_DENOMINATOR;
uint256 amount1Out = (token1.balanceOf(address(this)) * shareBps) / BPS_DENOMINATOR;
if (amount0Out > 0) token0.safeTransfer(owner(), amount0Out);
if (amount1Out > 0) token1.safeTransfer(owner(), amount1Out);
emit Withdrawn(shareBps, amount0Out, amount1Out);
}
function _getSlot0() internal view returns (uint160 sqrtPriceX96, int24 tick, uint128 liquidityVal, uint24 fee, int24 spacing) {
(sqrtPriceX96, tick, , , , , ) = pool.slot0();
liquidityVal = pool.liquidity();
fee = POOL_FEE;
spacing = TICK_SPACING;
}
function _closePosition() internal {
if (positionTokenId == 0) return;
(, , , , , , , uint128 liquidity, , , , ) = positionManager.positions(positionTokenId);
if (liquidity > 0) {
positionManager.decreaseLiquidity(
INonfungiblePositionManager.DecreaseLiquidityParams({
tokenId: positionTokenId,
liquidity: liquidity,
amount0Min: 0,
amount1Min: 0,
deadline: block.timestamp + DEADLINE_BUFFER
})
);
}
_collect(positionTokenId);
positionTokenId = 0;
lastLowerTick = 0;
lastUpperTick = 0;
}
function _addToExisting() internal {
(, , , , , int24 lowerTick, int24 upperTick, , , , , ) = positionManager.positions(positionTokenId);
lastLowerTick = lowerTick;
lastUpperTick = upperTick;
(uint160 sqrtPriceX96, , , , ) = _getSlot0();
_rebalanceHoldings(sqrtPriceX96);
uint256 bal0 = token0.balanceOf(address(this));
uint256 bal1 = token1.balanceOf(address(this));
if (bal0 == 0 && bal1 == 0) return;
_approveIfNeeded(token0, address(positionManager), bal0);
_approveIfNeeded(token1, address(positionManager), bal1);
positionManager.increaseLiquidity(
INonfungiblePositionManager.IncreaseLiquidityParams({
tokenId: positionTokenId,
amount0Desired: bal0,
amount1Desired: bal1,
amount0Min: _slippageAdjusted(bal0),
amount1Min: _slippageAdjusted(bal1),
deadline: block.timestamp + DEADLINE_BUFFER
})
);
emit Compounded(positionTokenId, 0);
}
function _mintWithBalances(int24 lowerTick, int24 upperTick) internal {
(uint160 sqrtPriceX96, , , , ) = _getSlot0();
lastLowerTick = lowerTick;
lastUpperTick = upperTick;
_rebalanceHoldings(sqrtPriceX96);
uint256 bal0 = token0.balanceOf(address(this));
uint256 bal1 = token1.balanceOf(address(this));
require(bal0 > 0 || bal1 > 0, "NO_FUNDS");
_approveIfNeeded(token0, address(positionManager), bal0);
_approveIfNeeded(token1, address(positionManager), bal1);
(uint256 newTokenId, uint128 liquidityMinted, uint256 amount0, uint256 amount1) = positionManager.mint(
INonfungiblePositionManager.MintParams({
token0: ENA,
token1: WETH,
fee: POOL_FEE,
tickLower: lowerTick,
tickUpper: upperTick,
amount0Desired: bal0,
amount1Desired: bal1,
amount0Min: _slippageAdjusted(bal0),
amount1Min: _slippageAdjusted(bal1),
recipient: address(this),
deadline: block.timestamp + DEADLINE_BUFFER
})
);
_approveIfNeeded(token0, address(positionManager), 0);
_approveIfNeeded(token1, address(positionManager), 0);
positionTokenId = newTokenId;
emit Rebalanced(newTokenId, lowerTick, upperTick, liquidityMinted);
emit Deposited(amount0, amount1);
}
function _collect(uint256 tokenId) internal {
positionManager.collect(
INonfungiblePositionManager.CollectParams({
tokenId: tokenId,
recipient: address(this),
amount0Max: type(uint128).max,
amount1Max: type(uint128).max
})
);
}
function _targetTicks(int24 currentTick) internal view returns (int24 lowerTick, int24 upperTick) {
int24 center = _floorToSpacing(currentTick);
int24 halfRange = bandWidthTicks / 2;
lowerTick = _floorToSpacing(center - halfRange);
upperTick = _floorToSpacing(center + halfRange);
if (upperTick <= lowerTick) upperTick = lowerTick + TICK_SPACING;
}
function _floorToSpacing(int24 tick) internal pure returns (int24) {
int24 remainder = tick % TICK_SPACING;
if (remainder == 0) return tick;
if (remainder > 0) return tick - remainder;
return tick - (remainder + TICK_SPACING);
}
function _slippageAdjusted(uint256 amount) internal pure returns (uint256) {
if (amount == 0) return 0;
return (amount * (BPS_DENOMINATOR - SLIPPAGE_BPS)) / BPS_DENOMINATOR;
}
function _approveIfNeeded(IERC20 token, address spender, uint256 amount) internal {
uint256 currentAllowance = token.allowance(address(this), spender);
if (currentAllowance < amount) {
if (currentAllowance != 0) SafeERC20.safeApprove(token, spender, 0);
SafeERC20.safeApprove(token, spender, amount);
}
}
function _rebalanceHoldings(uint160 sqrtPriceX96) internal {
uint256 bal0 = token0.balanceOf(address(this));
uint256 bal1 = token1.balanceOf(address(this));
if (bal0 == 0 && bal1 == 0) return;
uint160 sqrtLower = TickMath.getSqrtRatioAtTick(lastLowerTick);
uint160 sqrtUpper = TickMath.getSqrtRatioAtTick(lastUpperTick);
if (sqrtPriceX96 <= sqrtLower) {
if (bal1 > 0) _swapToken1For0(bal1, sqrtPriceX96);
return;
}
if (sqrtPriceX96 >= sqrtUpper) {
if (bal0 > 0) _swapToken0For1(bal0, sqrtPriceX96);
return;
}
uint256 priceQ96 = FullMath.mulDiv(uint256(sqrtPriceX96), uint256(sqrtPriceX96), 1 << 192);
if (priceQ96 == 0) return;
uint256 w0 = FullMath.mulDiv(uint256(sqrtUpper - sqrtPriceX96), 1e18, uint256(sqrtUpper));
w0 = FullMath.mulDiv(w0, 1, uint256(sqrtPriceX96));
uint256 w1 = uint256(sqrtPriceX96 - sqrtLower);
uint256 totalW = w0 + w1;
if (totalW == 0) return;
uint256 value0 = FullMath.mulDiv(bal0, priceQ96, 1);
uint256 totalValue = value0 + (bal1 << 96);
uint256 target0Value = FullMath.mulDiv(totalValue, w0, totalW);
if (value0 > target0Value && bal0 > 0) {
uint256 amount0 = FullMath.mulDiv(value0 - target0Value, 1, priceQ96);
if (amount0 > 0) _swapToken0For1(amount0, sqrtPriceX96);
} else if (value0 < target0Value && bal1 > 0) {
uint256 amount1 = (target0Value - value0) >> 96;
if (amount1 > 0) _swapToken1For0(amount1, sqrtPriceX96);
}
}
function _swapToken0For1(uint256 amountIn, uint160 sqrtPriceX96) internal returns (uint256 amountOut) {
if (amountIn == 0) return 0;
uint160 limit = _priceLimit(true, sqrtPriceX96);
(int256 amount0Delta, int256 amount1Delta) = pool.swap(address(this), true, int256(amountIn), limit, abi.encode(true));
amountOut = uint256(-amount1Delta);
require(amount0Delta <= int256(amountIn) && amountOut > 0, "SWAP0_FAIL");
}
function _swapToken1For0(uint256 amountIn, uint160 sqrtPriceX96) internal returns (uint256 amountOut) {
if (amountIn == 0) return 0;
uint160 limit = _priceLimit(false, sqrtPriceX96);
(int256 amount0Delta, int256 amount1Delta) = pool.swap(address(this), false, int256(amountIn), limit, abi.encode(false));
amountOut = uint256(-amount0Delta);
require(amount1Delta <= int256(amountIn) && amountOut > 0, "SWAP1_FAIL");
}
function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata) external override {
require(msg.sender == address(pool), "CALLER_NOT_POOL");
if (amount0Delta > 0) token0.safeTransfer(msg.sender, uint256(amount0Delta));
if (amount1Delta > 0) token1.safeTransfer(msg.sender, uint256(amount1Delta));
}
function _priceLimit(bool zeroForOne, uint160 sqrtPriceX96) internal pure returns (uint160 limit) {
if (zeroForOne) {
uint256 adjusted = (uint256(sqrtPriceX96) * (BPS_DENOMINATOR - PRICE_LIMIT_BPS)) / BPS_DENOMINATOR;
if (adjusted <= MIN_SQRT_RATIO) return MIN_SQRT_RATIO;
return uint160(adjusted);
} else {
uint256 adjusted = (uint256(sqrtPriceX96) * (BPS_DENOMINATOR + PRICE_LIMIT_BPS)) / BPS_DENOMINATOR;
if (adjusted >= MAX_SQRT_RATIO) return MAX_SQRT_RATIO;
return uint160(adjusted);
}
}
function onERC721Received(address, address, uint256, bytes calldata) external pure override returns (bytes4) {
return IERC721Receiver.onERC721Received.selector;
}
}
Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": true
},
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.