文件 1 的 28:Address.sol
pragma solidity ^0.8.0;
library Address {
function isContract(address account) internal view returns (bool) {
uint256 size;
assembly {
size := extcodesize(account)
}
return size > 0;
}
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
function functionCallWithValue(
address target,
bytes memory data,
uint256 value
) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResult(success, returndata, errorMessage);
}
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResult(success, returndata, errorMessage);
}
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
require(isContract(target), "Address: delegate call to non-contract");
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResult(success, returndata, errorMessage);
}
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
文件 2 的 28:Clones.sol
pragma solidity ^0.8.0;
library Clones {
function clone(address implementation) internal returns (address instance) {
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
instance := create(0, ptr, 0x37)
}
require(instance != address(0), "ERC1167: create failed");
}
function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
instance := create2(0, ptr, 0x37, salt)
}
require(instance != address(0), "ERC1167: create2 failed");
}
function predictDeterministicAddress(
address implementation,
bytes32 salt,
address deployer
) internal pure returns (address predicted) {
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
mstore(add(ptr, 0x38), shl(0x60, deployer))
mstore(add(ptr, 0x4c), salt)
mstore(add(ptr, 0x6c), keccak256(ptr, 0x37))
predicted := keccak256(add(ptr, 0x37), 0x55)
}
}
function predictDeterministicAddress(address implementation, bytes32 salt)
internal
view
returns (address predicted)
{
return predictDeterministicAddress(implementation, salt, address(this));
}
}
文件 3 的 28:Context.sol
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
文件 4 的 28:ERC20.sol
pragma solidity ^0.8.0;
import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";
contract ERC20 is Context, IERC20, IERC20Metadata {
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}
function name() public view virtual override returns (string memory) {
return _name;
}
function symbol() public view virtual override returns (string memory) {
return _symbol;
}
function decimals() public view virtual override returns (uint8) {
return 18;
}
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) public view virtual override returns (uint256) {
return _balances[account];
}
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
function allowance(address owner, address spender) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}
function approve(address spender, uint256 amount) public virtual override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
function transferFrom(
address sender,
address recipient,
uint256 amount
) public virtual override returns (bool) {
_transfer(sender, recipient, amount);
uint256 currentAllowance = _allowances[sender][_msgSender()];
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
unchecked {
_approve(sender, _msgSender(), currentAllowance - amount);
}
return true;
}
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
return true;
}
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
uint256 currentAllowance = _allowances[_msgSender()][spender];
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
unchecked {
_approve(_msgSender(), spender, currentAllowance - subtractedValue);
}
return true;
}
function _transfer(
address sender,
address recipient,
uint256 amount
) internal virtual {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(sender, recipient, amount);
uint256 senderBalance = _balances[sender];
require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[sender] = senderBalance - amount;
}
_balances[recipient] += amount;
emit Transfer(sender, recipient, amount);
_afterTokenTransfer(sender, recipient, amount);
}
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply += amount;
_balances[account] += amount;
emit Transfer(address(0), account, amount);
_afterTokenTransfer(address(0), account, amount);
}
function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: burn from the zero address");
_beforeTokenTransfer(account, address(0), amount);
uint256 accountBalance = _balances[account];
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
unchecked {
_balances[account] = accountBalance - amount;
}
_totalSupply -= amount;
emit Transfer(account, address(0), amount);
_afterTokenTransfer(account, address(0), amount);
}
function _approve(
address owner,
address spender,
uint256 amount
) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
function _afterTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
}
文件 5 的 28:FullMath.sol
pragma solidity >=0.8.0;
library FullMath {
function mulDivFloor(
uint256 a,
uint256 b,
uint256 denominator
) internal pure returns (uint256 result) {
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, '0 denom');
assembly {
result := div(prod0, denominator)
}
return result;
}
require(denominator > prod1, 'denom <= prod1');
uint256 remainder;
assembly {
remainder := mulmod(a, b, denominator)
}
assembly {
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
uint256 twos = denominator & (~denominator + 1);
assembly {
denominator := div(denominator, twos)
}
assembly {
prod0 := div(prod0, twos)
}
assembly {
twos := add(div(sub(0, twos), twos), 1)
}
unchecked {
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;
}
function mulDivCeiling(
uint256 a,
uint256 b,
uint256 denominator
) internal pure returns (uint256 result) {
result = mulDivFloor(a, b, denominator);
if (mulmod(a, b, denominator) > 0) {
result++;
}
}
}
文件 6 的 28:IERC20.sol
pragma solidity ^0.8.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
文件 7 的 28:IERC20Metadata.sol
pragma solidity ^0.8.0;
import "../IERC20.sol";
interface IERC20Metadata is IERC20 {
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
}
文件 8 的 28:IFactory.sol
pragma solidity >=0.8.0;
interface IFactory {
event PoolCreated(
address indexed token0,
address indexed token1,
uint24 indexed swapFeeUnits,
int24 tickDistance,
address pool
);
event SwapFeeEnabled(uint24 indexed swapFeeUnits, int24 indexed tickDistance);
event VestingPeriodUpdated(uint32 vestingPeriod);
event ConfigMasterUpdated(address oldConfigMaster, address newConfigMaster);
event FeeConfigurationUpdated(address feeTo, uint24 governmentFeeUnits);
event WhitelistEnabled();
event WhitelistDisabled();
function vestingPeriod() external view returns (uint32);
function feeAmountTickDistance(uint24 swapFeeUnits) external view returns (int24);
function configMaster() external view returns (address);
function poolInitHash() external view returns (bytes32);
function feeConfiguration() external view returns (address _feeTo, uint24 _governmentFeeUnits);
function whitelistDisabled() external view returns (bool);
function getWhitelistedNFTManagers() external view returns (address[] memory);
function isWhitelistedNFTManager(address sender) external view returns (bool);
function getPool(
address tokenA,
address tokenB,
uint24 swapFeeUnits
) external view returns (address pool);
function parameters()
external
view
returns (
address factory,
address token0,
address token1,
uint24 swapFeeUnits,
int24 tickDistance
);
function createPool(
address tokenA,
address tokenB,
uint24 swapFeeUnits
) external returns (address pool);
function enableSwapFee(uint24 swapFeeUnits, int24 tickDistance) external;
function updateConfigMaster(address) external;
function updateVestingPeriod(uint32) external;
function updateFeeConfiguration(address feeTo, uint24 governmentFeeUnits) external;
function enableWhitelist() external;
function disableWhitelist() external;
}
文件 9 的 28:IFlashCallback.sol
pragma solidity >=0.8.0;
interface IFlashCallback {
function flashCallback(
uint256 feeQty0,
uint256 feeQty1,
bytes calldata data
) external;
}
文件 10 的 28:IMintCallback.sol
pragma solidity >=0.8.0;
interface IMintCallback {
function mintCallback(
uint256 deltaQty0,
uint256 deltaQty1,
bytes calldata data
) external;
}
文件 11 的 28:IPool.sol
pragma solidity >=0.8.0;
import {IPoolActions} from './pool/IPoolActions.sol';
import {IPoolEvents} from './pool/IPoolEvents.sol';
import {IPoolStorage} from './pool/IPoolStorage.sol';
interface IPool is IPoolActions, IPoolEvents, IPoolStorage {}
文件 12 的 28:IPoolActions.sol
pragma solidity >=0.8.0;
interface IPoolActions {
function unlockPool(uint160 initialSqrtP) external returns (uint256 qty0, uint256 qty1);
function mint(
address recipient,
int24 tickLower,
int24 tickUpper,
int24[2] calldata ticksPrevious,
uint128 qty,
bytes calldata data
)
external
returns (
uint256 qty0,
uint256 qty1,
uint256 feeGrowthInside
);
function burn(
int24 tickLower,
int24 tickUpper,
uint128 qty
)
external
returns (
uint256 qty0,
uint256 qty1,
uint256 feeGrowthInside
);
function burnRTokens(uint256 qty, bool isLogicalBurn)
external
returns (uint256 qty0, uint256 qty1);
function swap(
address recipient,
int256 swapQty,
bool isToken0,
uint160 limitSqrtP,
bytes calldata data
) external returns (int256 qty0, int256 qty1);
function flash(
address recipient,
uint256 qty0,
uint256 qty1,
bytes calldata data
) external;
}
文件 13 的 28:IPoolEvents.sol
pragma solidity >=0.8.0;
interface IPoolEvents {
event Initialize(uint160 sqrtP, int24 tick);
event Mint(
address sender,
address indexed owner,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 qty,
uint256 qty0,
uint256 qty1
);
event Burn(
address indexed owner,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 qty,
uint256 qty0,
uint256 qty1
);
event BurnRTokens(address indexed owner, uint256 qty, uint256 qty0, uint256 qty1);
event Swap(
address indexed sender,
address indexed recipient,
int256 deltaQty0,
int256 deltaQty1,
uint160 sqrtP,
uint128 liquidity,
int24 currentTick
);
event Flash(
address indexed sender,
address indexed recipient,
uint256 qty0,
uint256 qty1,
uint256 paid0,
uint256 paid1
);
}
文件 14 的 28:IPoolStorage.sol
pragma solidity >=0.8.0;
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {IFactory} from '../IFactory.sol';
interface IPoolStorage {
function factory() external view returns (IFactory);
function token0() external view returns (IERC20);
function token1() external view returns (IERC20);
function swapFeeUnits() external view returns (uint24);
function tickDistance() external view returns (int24);
function maxTickLiquidity() external view returns (uint128);
function ticks(int24 tick)
external
view
returns (
uint128 liquidityGross,
int128 liquidityNet,
uint256 feeGrowthOutside,
uint128 secondsPerLiquidityOutside
);
function initializedTicks(int24 tick) external view returns (int24 previous, int24 next);
function getPositions(
address owner,
int24 tickLower,
int24 tickUpper
) external view returns (uint128 liquidity, uint256 feeGrowthInsideLast);
function getPoolState()
external
view
returns (
uint160 sqrtP,
int24 currentTick,
int24 nearestCurrentTick,
bool locked
);
function getLiquidityState()
external
view
returns (
uint128 baseL,
uint128 reinvestL,
uint128 reinvestLLast
);
function getFeeGrowthGlobal() external view returns (uint256);
function getSecondsPerLiquidityData()
external
view
returns (uint128 secondsPerLiquidityGlobal, uint32 lastUpdateTime);
function getSecondsPerLiquidityInside(int24 tickLower, int24 tickUpper)
external
view
returns (uint128 secondsPerLiquidityInside);
}
文件 15 的 28:ISwapCallback.sol
pragma solidity >=0.8.0;
interface ISwapCallback {
function swapCallback(
int256 deltaQty0,
int256 deltaQty1,
bytes calldata data
) external;
}
文件 16 的 28:Linkedlist.sol
pragma solidity >=0.8.0;
library Linkedlist {
struct Data {
int24 previous;
int24 next;
}
function init(
mapping(int24 => Linkedlist.Data) storage self,
int24 lowestValue,
int24 highestValue
) internal {
(self[lowestValue].previous, self[lowestValue].next) = (lowestValue, highestValue);
(self[highestValue].previous, self[highestValue].next) = (lowestValue, highestValue);
}
function remove(mapping(int24 => Linkedlist.Data) storage self, int24 removedValue)
internal
returns (int24 lowerValue)
{
Data memory removedValueData = self[removedValue];
require(removedValueData.next != removedValueData.previous, 'remove non-existent value');
if (removedValueData.previous == removedValue) return removedValue;
lowerValue = removedValueData.previous;
if (removedValueData.next == removedValue) return lowerValue;
self[removedValueData.previous].next = removedValueData.next;
self[removedValueData.next].previous = removedValueData.previous;
delete self[removedValue];
}
function insert(
mapping(int24 => Linkedlist.Data) storage self,
int24 newValue,
int24 lowerValue,
int24 nextValue
) internal {
require(nextValue != self[lowerValue].previous, 'lower value is not initialized');
require(lowerValue < newValue && nextValue > newValue, 'invalid lower value');
self[newValue].next = nextValue;
self[newValue].previous = lowerValue;
self[nextValue].previous = newValue;
self[lowerValue].next = newValue;
}
}
文件 17 的 28:LiqDeltaMath.sol
pragma solidity >=0.8.0;
library LiqDeltaMath {
function applyLiquidityDelta(
uint128 liquidity,
uint128 liquidityDelta,
bool isAddLiquidity
) internal pure returns (uint128) {
return isAddLiquidity ? liquidity + liquidityDelta : liquidity - liquidityDelta;
}
}
文件 18 的 28:MathConstants.sol
pragma solidity >=0.8.0;
library MathConstants {
uint256 internal constant TWO_FEE_UNITS = 200_000;
uint256 internal constant TWO_POW_96 = 2**96;
uint128 internal constant MIN_LIQUIDITY = 100000;
uint8 internal constant RES_96 = 96;
uint24 internal constant BPS = 10000;
uint24 internal constant FEE_UNITS = 100000;
int24 internal constant MAX_TICK_DISTANCE = 480;
uint256 internal constant MAX_TICK_TRAVEL = 10;
}
文件 19 的 28:Pool.sol
pragma solidity 0.8.9;
import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import {ERC20} from '@openzeppelin/contracts/token/ERC20/ERC20.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {LiqDeltaMath} from './libraries/LiqDeltaMath.sol';
import {QtyDeltaMath} from './libraries/QtyDeltaMath.sol';
import {MathConstants as C} from './libraries/MathConstants.sol';
import {ReinvestmentMath} from './libraries/ReinvestmentMath.sol';
import {SwapMath} from './libraries/SwapMath.sol';
import {FullMath} from './libraries/FullMath.sol';
import {SafeCast} from './libraries/SafeCast.sol';
import {TickMath} from './libraries/TickMath.sol';
import {IPool} from './interfaces/IPool.sol';
import {IPoolActions} from './interfaces/pool/IPoolActions.sol';
import {IFactory} from './interfaces/IFactory.sol';
import {IMintCallback} from './interfaces/callback/IMintCallback.sol';
import {ISwapCallback} from './interfaces/callback/ISwapCallback.sol';
import {IFlashCallback} from './interfaces/callback/IFlashCallback.sol';
import {PoolTicksState} from './PoolTicksState.sol';
contract Pool is IPool, PoolTicksState, ERC20('KyberSwap v2 Reinvestment Token', 'KS2-RT') {
using SafeCast for uint256;
using SafeCast for int256;
using SafeERC20 for IERC20;
modifier lock() {
require(poolData.locked == false, 'locked');
poolData.locked = true;
_;
poolData.locked = false;
}
constructor() {}
function _poolBalToken0() private view returns (uint256) {
(bool success, bytes memory data) = address(token0).staticcall(
abi.encodeWithSelector(IERC20.balanceOf.selector, address(this))
);
require(success && data.length >= 32);
return abi.decode(data, (uint256));
}
function _poolBalToken1() private view returns (uint256) {
(bool success, bytes memory data) = address(token1).staticcall(
abi.encodeWithSelector(IERC20.balanceOf.selector, address(this))
);
require(success && data.length >= 32);
return abi.decode(data, (uint256));
}
function unlockPool(uint160 initialSqrtP)
external
override
returns (uint256 qty0, uint256 qty1)
{
require(poolData.sqrtP == 0, 'already inited');
int24 initialTick = TickMath.getTickAtSqrtRatio(initialSqrtP);
(qty0, qty1) = QtyDeltaMath.calcUnlockQtys(initialSqrtP);
require(qty0 <= _poolBalToken0(), 'lacking qty0');
require(qty1 <= _poolBalToken1(), 'lacking qty1');
_mint(address(this), C.MIN_LIQUIDITY);
_initPoolStorage(initialSqrtP, initialTick);
emit Initialize(initialSqrtP, initialTick);
}
function _tweakPosition(UpdatePositionData memory posData)
private
returns (
int256 qty0,
int256 qty1,
uint256 feeGrowthInsideLast
)
{
require(posData.tickLower < posData.tickUpper, 'invalid tick range');
require(TickMath.MIN_TICK <= posData.tickLower, 'invalid lower tick');
require(posData.tickUpper <= TickMath.MAX_TICK, 'invalid upper tick');
require(
posData.tickLower % tickDistance == 0 && posData.tickUpper % tickDistance == 0,
'tick not in distance'
);
uint160 sqrtP = poolData.sqrtP;
int24 currentTick = poolData.currentTick;
uint128 baseL = poolData.baseL;
uint128 reinvestL = poolData.reinvestL;
CumulativesData memory cumulatives;
cumulatives.feeGrowth = _syncFeeGrowth(baseL, reinvestL, poolData.feeGrowthGlobal, true);
cumulatives.secondsPerLiquidity = _syncSecondsPerLiquidity(
poolData.secondsPerLiquidityGlobal,
baseL
);
uint256 feesClaimable;
(feesClaimable, feeGrowthInsideLast) = _updatePosition(posData, currentTick, cumulatives);
if (feesClaimable != 0) _transfer(address(this), posData.owner, feesClaimable);
if (currentTick < posData.tickLower) {
return (
QtyDeltaMath.calcRequiredQty0(
TickMath.getSqrtRatioAtTick(posData.tickLower),
TickMath.getSqrtRatioAtTick(posData.tickUpper),
posData.liquidityDelta,
posData.isAddLiquidity
),
0,
feeGrowthInsideLast
);
}
if (currentTick >= posData.tickUpper) {
return (
0,
QtyDeltaMath.calcRequiredQty1(
TickMath.getSqrtRatioAtTick(posData.tickLower),
TickMath.getSqrtRatioAtTick(posData.tickUpper),
posData.liquidityDelta,
posData.isAddLiquidity
),
feeGrowthInsideLast
);
}
qty0 = QtyDeltaMath.calcRequiredQty0(
sqrtP,
TickMath.getSqrtRatioAtTick(posData.tickUpper),
posData.liquidityDelta,
posData.isAddLiquidity
);
qty1 = QtyDeltaMath.calcRequiredQty1(
TickMath.getSqrtRatioAtTick(posData.tickLower),
sqrtP,
posData.liquidityDelta,
posData.isAddLiquidity
);
poolData.baseL = LiqDeltaMath.applyLiquidityDelta(
baseL,
posData.liquidityDelta,
posData.isAddLiquidity
);
}
function mint(
address recipient,
int24 tickLower,
int24 tickUpper,
int24[2] calldata ticksPrevious,
uint128 qty,
bytes calldata data
)
external
override
lock
returns (
uint256 qty0,
uint256 qty1,
uint256 feeGrowthInsideLast
)
{
require(qty != 0, '0 qty');
require(factory.isWhitelistedNFTManager(msg.sender), 'forbidden');
int256 qty0Int;
int256 qty1Int;
(qty0Int, qty1Int, feeGrowthInsideLast) = _tweakPosition(
UpdatePositionData({
owner: recipient,
tickLower: tickLower,
tickUpper: tickUpper,
tickLowerPrevious: ticksPrevious[0],
tickUpperPrevious: ticksPrevious[1],
liquidityDelta: qty,
isAddLiquidity: true
})
);
qty0 = uint256(qty0Int);
qty1 = uint256(qty1Int);
uint256 balance0Before;
uint256 balance1Before;
if (qty0 > 0) balance0Before = _poolBalToken0();
if (qty1 > 0) balance1Before = _poolBalToken1();
IMintCallback(msg.sender).mintCallback(qty0, qty1, data);
if (qty0 > 0) require(balance0Before + qty0 <= _poolBalToken0(), 'lacking qty0');
if (qty1 > 0) require(balance1Before + qty1 <= _poolBalToken1(), 'lacking qty1');
emit Mint(msg.sender, recipient, tickLower, tickUpper, qty, qty0, qty1);
}
function burn(
int24 tickLower,
int24 tickUpper,
uint128 qty
)
external
override
lock
returns (
uint256 qty0,
uint256 qty1,
uint256 feeGrowthInsideLast
)
{
require(qty != 0, '0 qty');
int256 qty0Int;
int256 qty1Int;
(qty0Int, qty1Int, feeGrowthInsideLast) = _tweakPosition(
UpdatePositionData({
owner: msg.sender,
tickLower: tickLower,
tickUpper: tickUpper,
tickLowerPrevious: 0,
tickUpperPrevious: 0,
liquidityDelta: qty,
isAddLiquidity: false
})
);
if (qty0Int < 0) {
qty0 = qty0Int.revToUint256();
token0.safeTransfer(msg.sender, qty0);
}
if (qty1Int < 0) {
qty1 = qty1Int.revToUint256();
token1.safeTransfer(msg.sender, qty1);
}
emit Burn(msg.sender, tickLower, tickUpper, qty, qty0, qty1);
}
function burnRTokens(uint256 _qty, bool isLogicalBurn)
external
override
lock
returns (uint256 qty0, uint256 qty1)
{
if (isLogicalBurn) {
_burn(msg.sender, _qty);
emit BurnRTokens(msg.sender, _qty, 0, 0);
return (0, 0);
}
uint128 baseL = poolData.baseL;
uint128 reinvestL = poolData.reinvestL;
uint160 sqrtP = poolData.sqrtP;
_syncFeeGrowth(baseL, reinvestL, poolData.feeGrowthGlobal, false);
uint256 deltaL = FullMath.mulDivFloor(_qty, reinvestL, totalSupply());
reinvestL = reinvestL - deltaL.toUint128();
poolData.reinvestL = reinvestL;
poolData.reinvestLLast = reinvestL;
qty0 = QtyDeltaMath.getQty0FromBurnRTokens(sqrtP, deltaL);
qty1 = QtyDeltaMath.getQty1FromBurnRTokens(sqrtP, deltaL);
_burn(msg.sender, _qty);
if (qty0 > 0) token0.safeTransfer(msg.sender, qty0);
if (qty1 > 0) token1.safeTransfer(msg.sender, qty1);
emit BurnRTokens(msg.sender, _qty, qty0, qty1);
}
struct SwapData {
int256 specifiedAmount;
int256 returnedAmount;
uint160 sqrtP;
int24 currentTick;
int24 nextTick;
uint160 nextSqrtP;
bool isToken0;
bool isExactInput;
uint128 baseL;
uint128 reinvestL;
}
struct SwapCache {
uint256 rTotalSupply;
uint128 reinvestLLast;
uint256 feeGrowthGlobal;
uint128 secondsPerLiquidityGlobal;
address feeTo;
uint24 governmentFeeUnits;
uint256 governmentFee;
uint256 lpFee;
}
function swap(
address recipient,
int256 swapQty,
bool isToken0,
uint160 limitSqrtP,
bytes calldata data
) external override lock returns (int256 deltaQty0, int256 deltaQty1) {
require(swapQty != 0, '0 swapQty');
SwapData memory swapData;
swapData.specifiedAmount = swapQty;
swapData.isToken0 = isToken0;
swapData.isExactInput = swapData.specifiedAmount > 0;
bool willUpTick = (swapData.isExactInput != isToken0);
(
swapData.baseL,
swapData.reinvestL,
swapData.sqrtP,
swapData.currentTick,
swapData.nextTick
) = _getInitialSwapData(willUpTick);
if (willUpTick) {
require(
limitSqrtP > swapData.sqrtP && limitSqrtP < TickMath.MAX_SQRT_RATIO,
'bad limitSqrtP'
);
} else {
require(
limitSqrtP < swapData.sqrtP && limitSqrtP > TickMath.MIN_SQRT_RATIO,
'bad limitSqrtP'
);
}
SwapCache memory cache;
while (swapData.specifiedAmount != 0 && swapData.sqrtP != limitSqrtP) {
int24 tempNextTick = swapData.nextTick;
if (willUpTick && tempNextTick > C.MAX_TICK_DISTANCE + swapData.currentTick) {
tempNextTick = swapData.currentTick + C.MAX_TICK_DISTANCE;
} else if (!willUpTick && tempNextTick < swapData.currentTick - C.MAX_TICK_DISTANCE) {
tempNextTick = swapData.currentTick - C.MAX_TICK_DISTANCE;
}
swapData.nextSqrtP = TickMath.getSqrtRatioAtTick(tempNextTick);
{
uint160 targetSqrtP = swapData.nextSqrtP;
if (willUpTick == (swapData.nextSqrtP > limitSqrtP)) {
targetSqrtP = limitSqrtP;
}
int256 usedAmount;
int256 returnedAmount;
uint256 deltaL;
(usedAmount, returnedAmount, deltaL, swapData.sqrtP) = SwapMath.computeSwapStep(
swapData.baseL + swapData.reinvestL,
swapData.sqrtP,
targetSqrtP,
swapFeeUnits,
swapData.specifiedAmount,
swapData.isExactInput,
swapData.isToken0
);
swapData.specifiedAmount -= usedAmount;
swapData.returnedAmount += returnedAmount;
swapData.reinvestL += deltaL.toUint128();
}
if (swapData.sqrtP != swapData.nextSqrtP) {
swapData.currentTick = TickMath.getTickAtSqrtRatio(swapData.sqrtP);
break;
}
swapData.currentTick = willUpTick ? tempNextTick : tempNextTick - 1;
if (tempNextTick != swapData.nextTick) continue;
if (cache.rTotalSupply == 0) {
cache.rTotalSupply = totalSupply();
cache.reinvestLLast = poolData.reinvestLLast;
cache.feeGrowthGlobal = poolData.feeGrowthGlobal;
cache.secondsPerLiquidityGlobal = _syncSecondsPerLiquidity(
poolData.secondsPerLiquidityGlobal,
swapData.baseL
);
(cache.feeTo, cache.governmentFeeUnits) = factory.feeConfiguration();
}
uint256 rMintQty = ReinvestmentMath.calcrMintQty(
swapData.reinvestL,
cache.reinvestLLast,
swapData.baseL,
cache.rTotalSupply
);
if (rMintQty != 0) {
cache.rTotalSupply += rMintQty;
unchecked {
uint256 governmentFee = (rMintQty * cache.governmentFeeUnits) / C.FEE_UNITS;
cache.governmentFee += governmentFee;
uint256 lpFee = rMintQty - governmentFee;
cache.lpFee += lpFee;
cache.feeGrowthGlobal += FullMath.mulDivFloor(lpFee, C.TWO_POW_96, swapData.baseL);
}
}
cache.reinvestLLast = swapData.reinvestL;
(swapData.baseL, swapData.nextTick) = _updateLiquidityAndCrossTick(
swapData.nextTick,
swapData.baseL,
cache.feeGrowthGlobal,
cache.secondsPerLiquidityGlobal,
willUpTick
);
}
if (cache.rTotalSupply != 0) {
if (cache.governmentFee > 0) _mint(cache.feeTo, cache.governmentFee);
if (cache.lpFee > 0) _mint(address(this), cache.lpFee);
poolData.reinvestLLast = cache.reinvestLLast;
poolData.feeGrowthGlobal = cache.feeGrowthGlobal;
}
_updatePoolData(
swapData.baseL,
swapData.reinvestL,
swapData.sqrtP,
swapData.currentTick,
swapData.nextTick
);
(deltaQty0, deltaQty1) = isToken0
? (swapQty - swapData.specifiedAmount, swapData.returnedAmount)
: (swapData.returnedAmount, swapQty - swapData.specifiedAmount);
if (willUpTick) {
if (deltaQty0 < 0) token0.safeTransfer(recipient, deltaQty0.revToUint256());
uint256 balance1Before = _poolBalToken1();
ISwapCallback(msg.sender).swapCallback(deltaQty0, deltaQty1, data);
require(_poolBalToken1() >= balance1Before + uint256(deltaQty1), 'lacking deltaQty1');
} else {
if (deltaQty1 < 0) token1.safeTransfer(recipient, deltaQty1.revToUint256());
uint256 balance0Before = _poolBalToken0();
ISwapCallback(msg.sender).swapCallback(deltaQty0, deltaQty1, data);
require(_poolBalToken0() >= balance0Before + uint256(deltaQty0), 'lacking deltaQty0');
}
emit Swap(
msg.sender,
recipient,
deltaQty0,
deltaQty1,
swapData.sqrtP,
swapData.baseL,
swapData.currentTick
);
}
function flash(
address recipient,
uint256 qty0,
uint256 qty1,
bytes calldata data
) external override lock {
(address feeTo, ) = factory.feeConfiguration();
uint256 feeQty0;
uint256 feeQty1;
if (feeTo != address(0)) {
feeQty0 = (qty0 * swapFeeUnits) / C.FEE_UNITS;
feeQty1 = (qty1 * swapFeeUnits) / C.FEE_UNITS;
}
uint256 balance0Before = _poolBalToken0();
uint256 balance1Before = _poolBalToken1();
if (qty0 > 0) token0.safeTransfer(recipient, qty0);
if (qty1 > 0) token1.safeTransfer(recipient, qty1);
IFlashCallback(msg.sender).flashCallback(feeQty0, feeQty1, data);
uint256 balance0After = _poolBalToken0();
uint256 balance1After = _poolBalToken1();
require(balance0Before + feeQty0 <= balance0After, 'lacking feeQty0');
require(balance1Before + feeQty1 <= balance1After, 'lacking feeQty1');
uint256 paid0;
uint256 paid1;
unchecked {
paid0 = balance0After - balance0Before;
paid1 = balance1After - balance1Before;
}
if (paid0 > 0) token0.safeTransfer(feeTo, paid0);
if (paid1 > 0) token1.safeTransfer(feeTo, paid1);
emit Flash(msg.sender, recipient, qty0, qty1, paid0, paid1);
}
function _syncSecondsPerLiquidity(uint128 _secondsPerLiquidityGlobal, uint128 baseL)
internal
returns (uint128)
{
uint256 secondsElapsed = _blockTimestamp() - poolData.secondsPerLiquidityUpdateTime;
if (secondsElapsed > 0 && baseL > 0) {
_secondsPerLiquidityGlobal += uint128((secondsElapsed << C.RES_96) / baseL);
poolData.secondsPerLiquidityGlobal = _secondsPerLiquidityGlobal;
poolData.secondsPerLiquidityUpdateTime = _blockTimestamp();
}
return _secondsPerLiquidityGlobal;
}
function _syncFeeGrowth(
uint128 baseL,
uint128 reinvestL,
uint256 _feeGrowthGlobal,
bool updateReinvestLLast
) internal returns (uint256) {
uint256 rMintQty = ReinvestmentMath.calcrMintQty(
uint256(reinvestL),
uint256(poolData.reinvestLLast),
baseL,
totalSupply()
);
if (rMintQty != 0) {
rMintQty = _deductGovermentFee(rMintQty);
_mint(address(this), rMintQty);
unchecked {
_feeGrowthGlobal += FullMath.mulDivFloor(rMintQty, C.TWO_POW_96, baseL);
}
poolData.feeGrowthGlobal = _feeGrowthGlobal;
}
if (updateReinvestLLast) poolData.reinvestLLast = reinvestL;
return _feeGrowthGlobal;
}
function _deductGovermentFee(uint256 rMintQty) internal returns (uint256) {
(address feeTo, uint24 governmentFeeUnits) = factory.feeConfiguration();
if (governmentFeeUnits == 0) {
return rMintQty;
}
unchecked {
uint256 rGovtQty = (rMintQty * governmentFeeUnits) / C.FEE_UNITS;
if (rGovtQty != 0) {
_mint(feeTo, rGovtQty);
}
return rMintQty - rGovtQty;
}
}
}
文件 20 的 28:PoolStorage.sol
pragma solidity 0.8.9;
import {Clones} from '@openzeppelin/contracts/proxy/Clones.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {Linkedlist} from './libraries/Linkedlist.sol';
import {TickMath} from './libraries/TickMath.sol';
import {MathConstants as C} from './libraries/MathConstants.sol';
import {IFactory} from './interfaces/IFactory.sol';
import {IPoolStorage} from './interfaces/pool/IPoolStorage.sol';
abstract contract PoolStorage is IPoolStorage {
using Clones for address;
using Linkedlist for mapping(int24 => Linkedlist.Data);
address internal constant LIQUIDITY_LOCKUP_ADDRESS = 0xD444422222222222222222222222222222222222;
struct PoolData {
uint160 sqrtP;
int24 nearestCurrentTick;
int24 currentTick;
bool locked;
uint128 baseL;
uint128 reinvestL;
uint128 reinvestLLast;
uint256 feeGrowthGlobal;
uint128 secondsPerLiquidityGlobal;
uint32 secondsPerLiquidityUpdateTime;
}
struct TickData {
uint128 liquidityGross;
int128 liquidityNet;
uint256 feeGrowthOutside;
uint128 secondsPerLiquidityOutside;
}
struct Position {
uint128 liquidity;
uint256 feeGrowthInsideLast;
}
struct CumulativesData {
uint256 feeGrowth;
uint128 secondsPerLiquidity;
}
IFactory public immutable override factory;
IERC20 public immutable override token0;
IERC20 public immutable override token1;
uint128 public immutable override maxTickLiquidity;
uint24 public immutable override swapFeeUnits;
int24 public immutable override tickDistance;
mapping(int24 => TickData) public override ticks;
mapping(int24 => Linkedlist.Data) public override initializedTicks;
mapping(bytes32 => Position) internal positions;
PoolData internal poolData;
constructor() {
(
address _factory,
address _token0,
address _token1,
uint24 _swapFeeUnits,
int24 _tickDistance
) = IFactory(msg.sender).parameters();
factory = IFactory(_factory);
token0 = IERC20(_token0);
token1 = IERC20(_token1);
swapFeeUnits = _swapFeeUnits;
tickDistance = _tickDistance;
maxTickLiquidity = type(uint128).max / TickMath.getMaxNumberTicks(_tickDistance);
poolData.locked = true;
}
function _initPoolStorage(uint160 initialSqrtP, int24 initialTick) internal {
poolData.baseL = 0;
poolData.reinvestL = C.MIN_LIQUIDITY;
poolData.reinvestLLast = C.MIN_LIQUIDITY;
poolData.sqrtP = initialSqrtP;
poolData.currentTick = initialTick;
poolData.nearestCurrentTick = TickMath.MIN_TICK;
initializedTicks.init(TickMath.MIN_TICK, TickMath.MAX_TICK);
poolData.locked = false;
}
function getPositions(
address owner,
int24 tickLower,
int24 tickUpper
) external view override returns (uint128 liquidity, uint256 feeGrowthInsideLast) {
bytes32 key = _positionKey(owner, tickLower, tickUpper);
return (positions[key].liquidity, positions[key].feeGrowthInsideLast);
}
function getPoolState()
external
view
override
returns (
uint160 sqrtP,
int24 currentTick,
int24 nearestCurrentTick,
bool locked
)
{
sqrtP = poolData.sqrtP;
currentTick = poolData.currentTick;
nearestCurrentTick = poolData.nearestCurrentTick;
locked = poolData.locked;
}
function getLiquidityState()
external
view
override
returns (
uint128 baseL,
uint128 reinvestL,
uint128 reinvestLLast
)
{
baseL = poolData.baseL;
reinvestL = poolData.reinvestL;
reinvestLLast = poolData.reinvestLLast;
}
function getFeeGrowthGlobal() external view override returns (uint256) {
return poolData.feeGrowthGlobal;
}
function getSecondsPerLiquidityData()
external
view
override
returns (uint128 secondsPerLiquidityGlobal, uint32 lastUpdateTime)
{
secondsPerLiquidityGlobal = poolData.secondsPerLiquidityGlobal;
lastUpdateTime = poolData.secondsPerLiquidityUpdateTime;
}
function getSecondsPerLiquidityInside(int24 tickLower, int24 tickUpper)
external
view
override
returns (uint128 secondsPerLiquidityInside)
{
require(tickLower <= tickUpper, 'bad tick range');
int24 currentTick = poolData.currentTick;
uint128 secondsPerLiquidityGlobal = poolData.secondsPerLiquidityGlobal;
uint32 lastUpdateTime = poolData.secondsPerLiquidityUpdateTime;
uint128 lowerValue = ticks[tickLower].secondsPerLiquidityOutside;
uint128 upperValue = ticks[tickUpper].secondsPerLiquidityOutside;
unchecked {
if (currentTick < tickLower) {
secondsPerLiquidityInside = lowerValue - upperValue;
} else if (currentTick >= tickUpper) {
secondsPerLiquidityInside = upperValue - lowerValue;
} else {
secondsPerLiquidityInside = secondsPerLiquidityGlobal - (lowerValue + upperValue);
}
}
if (tickLower <= currentTick && currentTick < tickUpper) {
uint256 secondsElapsed = _blockTimestamp() - lastUpdateTime;
uint128 baseL = poolData.baseL;
if (secondsElapsed > 0 && baseL > 0) {
unchecked {
secondsPerLiquidityInside += uint128((secondsElapsed << 96) / baseL);
}
}
}
}
function _positionKey(
address owner,
int24 tickLower,
int24 tickUpper
) internal pure returns (bytes32) {
return keccak256(abi.encodePacked(owner, tickLower, tickUpper));
}
function _blockTimestamp() internal view virtual returns (uint32) {
return uint32(block.timestamp);
}
}
文件 21 的 28:PoolTicksState.sol
pragma solidity 0.8.9;
import {LiqDeltaMath} from './libraries/LiqDeltaMath.sol';
import {SafeCast} from './libraries/SafeCast.sol';
import {MathConstants} from './libraries/MathConstants.sol';
import {FullMath} from './libraries/FullMath.sol';
import {TickMath} from './libraries/TickMath.sol';
import {Linkedlist} from './libraries/Linkedlist.sol';
import {PoolStorage} from './PoolStorage.sol';
contract PoolTicksState is PoolStorage {
using SafeCast for int128;
using SafeCast for uint128;
using Linkedlist for mapping(int24 => Linkedlist.Data);
struct UpdatePositionData {
address owner;
int24 tickLower;
int24 tickUpper;
int24 tickLowerPrevious;
int24 tickUpperPrevious;
uint128 liquidityDelta;
bool isAddLiquidity;
}
function _updatePosition(
UpdatePositionData memory updateData,
int24 currentTick,
CumulativesData memory cumulatives
) internal returns (uint256 feesClaimable, uint256 feeGrowthInside) {
uint256 feeGrowthOutsideLowerTick = _updateTick(
updateData.tickLower,
currentTick,
updateData.tickLowerPrevious,
updateData.liquidityDelta,
updateData.isAddLiquidity,
cumulatives,
true
);
uint256 feeGrowthOutsideUpperTick = _updateTick(
updateData.tickUpper,
currentTick,
updateData.tickUpperPrevious,
updateData.liquidityDelta,
updateData.isAddLiquidity,
cumulatives,
false
);
unchecked {
if (currentTick < updateData.tickLower) {
feeGrowthInside = feeGrowthOutsideLowerTick - feeGrowthOutsideUpperTick;
} else if (currentTick >= updateData.tickUpper) {
feeGrowthInside = feeGrowthOutsideUpperTick - feeGrowthOutsideLowerTick;
} else {
feeGrowthInside =
cumulatives.feeGrowth -
feeGrowthOutsideLowerTick -
feeGrowthOutsideUpperTick;
}
}
feesClaimable = _updatePositionData(updateData, feeGrowthInside);
}
function _updateLiquidityAndCrossTick(
int24 nextTick,
uint128 currentLiquidity,
uint256 feeGrowthGlobal,
uint128 secondsPerLiquidityGlobal,
bool willUpTick
) internal returns (uint128 newLiquidity, int24 newNextTick) {
unchecked {
ticks[nextTick].feeGrowthOutside = feeGrowthGlobal - ticks[nextTick].feeGrowthOutside;
ticks[nextTick].secondsPerLiquidityOutside =
secondsPerLiquidityGlobal -
ticks[nextTick].secondsPerLiquidityOutside;
}
int128 liquidityNet = ticks[nextTick].liquidityNet;
if (willUpTick) {
newNextTick = initializedTicks[nextTick].next;
} else {
newNextTick = initializedTicks[nextTick].previous;
liquidityNet = -liquidityNet;
}
newLiquidity = LiqDeltaMath.applyLiquidityDelta(
currentLiquidity,
liquidityNet >= 0 ? uint128(liquidityNet) : liquidityNet.revToUint128(),
liquidityNet >= 0
);
}
function _updatePoolData(
uint128 baseL,
uint128 reinvestL,
uint160 sqrtP,
int24 currentTick,
int24 nextTick
) internal {
poolData.baseL = baseL;
poolData.reinvestL = reinvestL;
poolData.sqrtP = sqrtP;
poolData.currentTick = currentTick;
poolData.nearestCurrentTick = nextTick > currentTick
? initializedTicks[nextTick].previous
: nextTick;
}
function _getInitialSwapData(bool willUpTick)
internal
view
returns (
uint128 baseL,
uint128 reinvestL,
uint160 sqrtP,
int24 currentTick,
int24 nextTick
)
{
baseL = poolData.baseL;
reinvestL = poolData.reinvestL;
sqrtP = poolData.sqrtP;
currentTick = poolData.currentTick;
nextTick = poolData.nearestCurrentTick;
if (willUpTick) {
nextTick = initializedTicks[nextTick].next;
}
}
function _updatePositionData(UpdatePositionData memory _data, uint256 feeGrowthInside)
private
returns (uint256 feesClaimable)
{
bytes32 key = _positionKey(_data.owner, _data.tickLower, _data.tickUpper);
uint256 feeGrowth;
unchecked {
feeGrowth = feeGrowthInside - positions[key].feeGrowthInsideLast;
}
uint128 prevLiquidity = positions[key].liquidity;
feesClaimable = FullMath.mulDivFloor(feeGrowth, prevLiquidity, MathConstants.TWO_POW_96);
positions[key].liquidity = LiqDeltaMath.applyLiquidityDelta(
prevLiquidity,
_data.liquidityDelta,
_data.isAddLiquidity
);
positions[key].feeGrowthInsideLast = feeGrowthInside;
}
function _updateTick(
int24 tick,
int24 tickCurrent,
int24 tickPrevious,
uint128 liquidityDelta,
bool isAdd,
CumulativesData memory cumulatives,
bool isLower
) private returns (uint256 feeGrowthOutside) {
uint128 liquidityGrossBefore = ticks[tick].liquidityGross;
uint128 liquidityGrossAfter = LiqDeltaMath.applyLiquidityDelta(
liquidityGrossBefore,
liquidityDelta,
isAdd
);
require(liquidityGrossAfter <= maxTickLiquidity, '> max liquidity');
int128 signedLiquidityDelta = isAdd ? liquidityDelta.toInt128() : -(liquidityDelta.toInt128());
int128 liquidityNetAfter = isLower
? ticks[tick].liquidityNet + signedLiquidityDelta
: ticks[tick].liquidityNet - signedLiquidityDelta;
if (liquidityGrossBefore == 0) {
if (tick <= tickCurrent) {
ticks[tick].feeGrowthOutside = cumulatives.feeGrowth;
ticks[tick].secondsPerLiquidityOutside = cumulatives.secondsPerLiquidity;
}
}
ticks[tick].liquidityGross = liquidityGrossAfter;
ticks[tick].liquidityNet = liquidityNetAfter;
feeGrowthOutside = ticks[tick].feeGrowthOutside;
if (liquidityGrossBefore > 0 && liquidityGrossAfter == 0) {
delete ticks[tick];
}
if ((liquidityGrossBefore > 0) != (liquidityGrossAfter > 0)) {
_updateTickList(tick, tickPrevious, tickCurrent, isAdd);
}
}
function _updateTickList(
int24 tick,
int24 previousTick,
int24 currentTick,
bool isAdd
) internal {
if (isAdd) {
if (tick == TickMath.MIN_TICK || tick == TickMath.MAX_TICK) return;
int24 nextTick = initializedTicks[previousTick].next;
require(
nextTick != initializedTicks[previousTick].previous,
'previous tick has been removed'
);
uint256 iteration = 0;
while (nextTick <= tick && iteration < MathConstants.MAX_TICK_TRAVEL) {
previousTick = nextTick;
nextTick = initializedTicks[previousTick].next;
iteration++;
}
initializedTicks.insert(tick, previousTick, nextTick);
if (poolData.nearestCurrentTick < tick && tick <= currentTick) {
poolData.nearestCurrentTick = tick;
}
} else {
if (tick == poolData.nearestCurrentTick) {
poolData.nearestCurrentTick = initializedTicks.remove(tick);
} else {
initializedTicks.remove(tick);
}
}
}
}
文件 22 的 28:QtyDeltaMath.sol
pragma solidity >=0.8.0;
import {MathConstants as C} from './MathConstants.sol';
import {TickMath} from './TickMath.sol';
import {FullMath} from './FullMath.sol';
import {SafeCast} from './SafeCast.sol';
library QtyDeltaMath {
using SafeCast for uint256;
using SafeCast for int128;
function calcUnlockQtys(uint160 initialSqrtP)
internal
pure
returns (uint256 qty0, uint256 qty1)
{
qty0 = FullMath.mulDivCeiling(C.MIN_LIQUIDITY, C.TWO_POW_96, initialSqrtP);
qty1 = FullMath.mulDivCeiling(C.MIN_LIQUIDITY, initialSqrtP, C.TWO_POW_96);
}
function calcRequiredQty0(
uint160 lowerSqrtP,
uint160 upperSqrtP,
uint128 liquidity,
bool isAddLiquidity
) internal pure returns (int256) {
uint256 numerator1 = uint256(liquidity) << C.RES_96;
uint256 numerator2;
unchecked {
numerator2 = upperSqrtP - lowerSqrtP;
}
return
isAddLiquidity
? (divCeiling(FullMath.mulDivCeiling(numerator1, numerator2, upperSqrtP), lowerSqrtP))
.toInt256()
: (FullMath.mulDivFloor(numerator1, numerator2, upperSqrtP) / lowerSqrtP).revToInt256();
}
function calcRequiredQty1(
uint160 lowerSqrtP,
uint160 upperSqrtP,
uint128 liquidity,
bool isAddLiquidity
) internal pure returns (int256) {
unchecked {
return
isAddLiquidity
? (FullMath.mulDivCeiling(liquidity, upperSqrtP - lowerSqrtP, C.TWO_POW_96)).toInt256()
: (FullMath.mulDivFloor(liquidity, upperSqrtP - lowerSqrtP, C.TWO_POW_96)).revToInt256();
}
}
function getQty0FromBurnRTokens(uint160 sqrtP, uint256 liquidity)
internal
pure
returns (uint256)
{
return FullMath.mulDivFloor(liquidity, C.TWO_POW_96, sqrtP);
}
function getQty1FromBurnRTokens(uint160 sqrtP, uint256 liquidity)
internal
pure
returns (uint256)
{
return FullMath.mulDivFloor(liquidity, sqrtP, C.TWO_POW_96);
}
function divCeiling(uint256 x, uint256 y) internal pure returns (uint256 z) {
require(y > 0);
assembly {
z := add(div(x, y), gt(mod(x, y), 0))
}
}
}
文件 23 的 28:QuadMath.sol
pragma solidity >=0.8.0;
library QuadMath {
function getSmallerRootOfQuadEqn(
uint256 a,
uint256 b,
uint256 c
) internal pure returns (uint256 smallerRoot) {
smallerRoot = (b - sqrt(b * b - a * c)) / a;
}
function sqrt(uint256 y) internal pure returns (uint256 z) {
unchecked {
if (y > 3) {
z = y;
uint256 x = y / 2 + 1;
while (x < z) {
z = x;
x = (y / x + x) / 2;
}
} else if (y != 0) {
z = 1;
}
}
}
}
文件 24 的 28:ReinvestmentMath.sol
pragma solidity >=0.8.0;
import {MathConstants as C} from './MathConstants.sol';
import {FullMath} from './FullMath.sol';
library ReinvestmentMath {
function calcrMintQty(
uint256 reinvestL,
uint256 reinvestLLast,
uint128 baseL,
uint256 rTotalSupply
) internal pure returns (uint256 rMintQty) {
uint256 lpContribution = FullMath.mulDivFloor(
baseL,
reinvestL - reinvestLLast,
baseL + reinvestL
);
rMintQty = FullMath.mulDivFloor(rTotalSupply, lpContribution, reinvestLLast);
}
}
文件 25 的 28:SafeCast.sol
pragma solidity >=0.8.0;
library SafeCast {
function toUint32(uint256 y) internal pure returns (uint32 z) {
require((z = uint32(y)) == y);
}
function toInt128(uint128 y) internal pure returns (int128 z) {
require(y < 2**127);
z = int128(y);
}
function toUint128(uint256 y) internal pure returns (uint128 z) {
require((z = uint128(y)) == y);
}
function revToUint128(int128 y) internal pure returns (uint128 z) {
unchecked {
return type(uint128).max - uint128(y) + 1;
}
}
function toUint160(uint256 y) internal pure returns (uint160 z) {
require((z = uint160(y)) == y);
}
function toInt256(uint256 y) internal pure returns (int256 z) {
require(y < 2**255);
z = int256(y);
}
function revToInt256(uint256 y) internal pure returns (int256 z) {
require(y < 2**255);
z = -int256(y);
}
function revToUint256(int256 y) internal pure returns (uint256 z) {
unchecked {
return type(uint256).max - uint256(y) + 1;
}
}
}
文件 26 的 28:SafeERC20.sol
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../../../utils/Address.sol";
library SafeERC20 {
using Address for address;
function safeTransfer(
IERC20 token,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(
IERC20 token,
address from,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(
IERC20 token,
address spender,
uint256 value
) internal {
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
uint256 newAllowance = token.allowance(address(this), spender) + value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
uint256 newAllowance = oldAllowance - value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
文件 27 的 28:SwapMath.sol
pragma solidity >=0.8.0;
import {MathConstants as C} from './MathConstants.sol';
import {FullMath} from './FullMath.sol';
import {QuadMath} from './QuadMath.sol';
import {SafeCast} from './SafeCast.sol';
library SwapMath {
using SafeCast for uint256;
using SafeCast for int256;
function computeSwapStep(
uint256 liquidity,
uint160 currentSqrtP,
uint160 targetSqrtP,
uint256 feeInFeeUnits,
int256 specifiedAmount,
bool isExactInput,
bool isToken0
)
internal
pure
returns (
int256 usedAmount,
int256 returnedAmount,
uint256 deltaL,
uint160 nextSqrtP
)
{
if (currentSqrtP == targetSqrtP) return (0, 0, 0, currentSqrtP);
usedAmount = calcReachAmount(
liquidity,
currentSqrtP,
targetSqrtP,
feeInFeeUnits,
isExactInput,
isToken0
);
if (
(isExactInput && usedAmount >= specifiedAmount) ||
(!isExactInput && usedAmount <= specifiedAmount)
) {
usedAmount = specifiedAmount;
} else {
nextSqrtP = targetSqrtP;
}
uint256 absDelta = usedAmount >= 0 ? uint256(usedAmount) : usedAmount.revToUint256();
if (nextSqrtP == 0) {
deltaL = estimateIncrementalLiquidity(
absDelta,
liquidity,
currentSqrtP,
feeInFeeUnits,
isExactInput,
isToken0
);
nextSqrtP = calcFinalPrice(absDelta, liquidity, deltaL, currentSqrtP, isExactInput, isToken0)
.toUint160();
} else {
deltaL = calcIncrementalLiquidity(
absDelta,
liquidity,
currentSqrtP,
nextSqrtP,
isExactInput,
isToken0
);
}
returnedAmount = calcReturnedAmount(
liquidity,
currentSqrtP,
nextSqrtP,
deltaL,
isExactInput,
isToken0
);
}
function calcReachAmount(
uint256 liquidity,
uint256 currentSqrtP,
uint256 targetSqrtP,
uint256 feeInFeeUnits,
bool isExactInput,
bool isToken0
) internal pure returns (int256 reachAmount) {
uint256 absPriceDiff;
unchecked {
absPriceDiff = (currentSqrtP >= targetSqrtP)
? (currentSqrtP - targetSqrtP)
: (targetSqrtP - currentSqrtP);
}
if (isExactInput) {
if (isToken0) {
uint256 denominator = C.TWO_FEE_UNITS * targetSqrtP - feeInFeeUnits * currentSqrtP;
uint256 numerator = FullMath.mulDivFloor(
liquidity,
C.TWO_FEE_UNITS * absPriceDiff,
denominator
);
reachAmount = FullMath.mulDivFloor(numerator, C.TWO_POW_96, currentSqrtP).toInt256();
} else {
uint256 denominator = C.TWO_FEE_UNITS * currentSqrtP - feeInFeeUnits * targetSqrtP;
uint256 numerator = FullMath.mulDivFloor(
liquidity,
C.TWO_FEE_UNITS * absPriceDiff,
denominator
);
reachAmount = FullMath.mulDivFloor(numerator, currentSqrtP, C.TWO_POW_96).toInt256();
}
} else {
if (isToken0) {
uint256 denominator = C.TWO_FEE_UNITS * currentSqrtP - feeInFeeUnits * targetSqrtP;
uint256 numerator = denominator - feeInFeeUnits * currentSqrtP;
numerator = FullMath.mulDivFloor(liquidity << C.RES_96, numerator, denominator);
reachAmount = (FullMath.mulDivFloor(numerator, absPriceDiff, currentSqrtP) / targetSqrtP)
.revToInt256();
} else {
uint256 denominator = C.TWO_FEE_UNITS * targetSqrtP - feeInFeeUnits * currentSqrtP;
uint256 numerator = denominator - feeInFeeUnits * targetSqrtP;
numerator = FullMath.mulDivFloor(liquidity, numerator, denominator);
reachAmount = FullMath.mulDivFloor(numerator, absPriceDiff, C.TWO_POW_96).revToInt256();
}
}
}
function estimateIncrementalLiquidity(
uint256 absDelta,
uint256 liquidity,
uint160 currentSqrtP,
uint256 feeInFeeUnits,
bool isExactInput,
bool isToken0
) internal pure returns (uint256 deltaL) {
if (isExactInput) {
if (isToken0) {
deltaL = FullMath.mulDivFloor(
currentSqrtP,
absDelta * feeInFeeUnits,
C.TWO_FEE_UNITS << C.RES_96
);
} else {
deltaL = FullMath.mulDivFloor(
C.TWO_POW_96,
absDelta * feeInFeeUnits,
C.TWO_FEE_UNITS * currentSqrtP
);
}
} else {
uint256 a = feeInFeeUnits;
uint256 b = (C.FEE_UNITS - feeInFeeUnits) * liquidity;
uint256 c = feeInFeeUnits * liquidity * absDelta;
if (isToken0) {
b -= FullMath.mulDivFloor(C.FEE_UNITS * absDelta, currentSqrtP, C.TWO_POW_96);
c = FullMath.mulDivFloor(c, currentSqrtP, C.TWO_POW_96);
} else {
b -= FullMath.mulDivFloor(C.FEE_UNITS * absDelta, C.TWO_POW_96, currentSqrtP);
c = FullMath.mulDivFloor(c, C.TWO_POW_96, currentSqrtP);
}
deltaL = QuadMath.getSmallerRootOfQuadEqn(a, b, c);
}
}
function calcIncrementalLiquidity(
uint256 absDelta,
uint256 liquidity,
uint160 currentSqrtP,
uint160 nextSqrtP,
bool isExactInput,
bool isToken0
) internal pure returns (uint256 deltaL) {
if (isToken0) {
uint256 tmp1 = FullMath.mulDivFloor(liquidity, C.TWO_POW_96, currentSqrtP);
uint256 tmp2 = isExactInput ? tmp1 + absDelta : tmp1 - absDelta;
uint256 tmp3 = FullMath.mulDivFloor(nextSqrtP, tmp2, C.TWO_POW_96);
deltaL = (tmp3 > liquidity) ? tmp3 - liquidity : 0;
} else {
uint256 tmp1 = FullMath.mulDivFloor(liquidity, currentSqrtP, C.TWO_POW_96);
uint256 tmp2 = isExactInput ? tmp1 + absDelta : tmp1 - absDelta;
uint256 tmp3 = FullMath.mulDivFloor(tmp2, C.TWO_POW_96, nextSqrtP);
deltaL = (tmp3 > liquidity) ? tmp3 - liquidity : 0;
}
}
function calcFinalPrice(
uint256 absDelta,
uint256 liquidity,
uint256 deltaL,
uint160 currentSqrtP,
bool isExactInput,
bool isToken0
) internal pure returns (uint256) {
if (isToken0) {
uint256 tmp = FullMath.mulDivFloor(absDelta, currentSqrtP, C.TWO_POW_96);
if (isExactInput) {
return FullMath.mulDivCeiling(liquidity + deltaL, currentSqrtP, liquidity + tmp);
} else {
return FullMath.mulDivFloor(liquidity + deltaL, currentSqrtP, liquidity - tmp);
}
} else {
if (isExactInput) {
uint256 tmp = FullMath.mulDivFloor(absDelta, C.TWO_POW_96, currentSqrtP);
return FullMath.mulDivFloor(liquidity + tmp, currentSqrtP, liquidity + deltaL);
} else {
uint256 tmp = FullMath.mulDivFloor(absDelta, C.TWO_POW_96, currentSqrtP);
return FullMath.mulDivCeiling(liquidity - tmp, currentSqrtP, liquidity + deltaL);
}
}
}
function calcReturnedAmount(
uint256 liquidity,
uint160 currentSqrtP,
uint160 nextSqrtP,
uint256 deltaL,
bool isExactInput,
bool isToken0
) internal pure returns (int256 returnedAmount) {
if (isToken0) {
if (isExactInput) {
returnedAmount =
FullMath.mulDivCeiling(deltaL, nextSqrtP, C.TWO_POW_96).toInt256() +
FullMath.mulDivFloor(liquidity, currentSqrtP - nextSqrtP, C.TWO_POW_96).revToInt256();
} else {
returnedAmount =
FullMath.mulDivCeiling(deltaL, nextSqrtP, C.TWO_POW_96).toInt256() +
FullMath.mulDivCeiling(liquidity, nextSqrtP - currentSqrtP, C.TWO_POW_96).toInt256();
}
} else {
returnedAmount =
FullMath.mulDivCeiling(liquidity + deltaL, C.TWO_POW_96, nextSqrtP).toInt256() +
FullMath.mulDivFloor(liquidity, C.TWO_POW_96, currentSqrtP).revToInt256();
}
if (isExactInput && returnedAmount == 1) {
returnedAmount = 0;
}
}
}
文件 28 的 28:TickMath.sol
pragma solidity >=0.8.0;
library TickMath {
int24 internal constant MIN_TICK = -887272;
int24 internal constant MAX_TICK = -MIN_TICK;
uint160 internal constant MIN_SQRT_RATIO = 4295128739;
uint160 internal constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342;
function getSqrtRatioAtTick(int24 tick) internal pure returns (uint160 sqrtP) {
unchecked {
uint256 absTick = uint256(tick < 0 ? -int256(tick) : int256(tick));
require(absTick <= uint256(int256(MAX_TICK)), '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;
sqrtP = uint160((ratio >> 32) + (ratio % (1 << 32) == 0 ? 0 : 1));
}
}
function getTickAtSqrtRatio(uint160 sqrtP) internal pure returns (int24 tick) {
require(sqrtP >= MIN_SQRT_RATIO && sqrtP < MAX_SQRT_RATIO, 'R');
uint256 ratio = uint256(sqrtP) << 32;
uint256 r = ratio;
uint256 msb = 0;
unchecked {
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;
int24 tickLow = int24((log_sqrt10001 - 3402992956809132418596140100660247210) >> 128);
int24 tickHi = int24((log_sqrt10001 + 291339464771989622907027621153398088495) >> 128);
tick = tickLow == tickHi ? tickLow : getSqrtRatioAtTick(tickHi) <= sqrtP ? tickHi : tickLow;
}
}
function getMaxNumberTicks(int24 _tickDistance) internal pure returns (uint24 numTicks) {
return uint24(TickMath.MAX_TICK / _tickDistance) * 2;
}
}
{
"compilationTarget": {
"contracts/Pool.sol": "Pool"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "none"
},
"optimizer": {
"enabled": true,
"runs": 2000
},
"remappings": []
}
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"int24","name":"tickLower","type":"int24"},{"indexed":true,"internalType":"int24","name":"tickUpper","type":"int24"},{"indexed":false,"internalType":"uint128","name":"qty","type":"uint128"},{"indexed":false,"internalType":"uint256","name":"qty0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"qty1","type":"uint256"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":false,"internalType":"uint256","name":"qty","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"qty0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"qty1","type":"uint256"}],"name":"BurnRTokens","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"qty0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"qty1","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"paid0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"paid1","type":"uint256"}],"name":"Flash","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint160","name":"sqrtP","type":"uint160"},{"indexed":false,"internalType":"int24","name":"tick","type":"int24"}],"name":"Initialize","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"int24","name":"tickLower","type":"int24"},{"indexed":true,"internalType":"int24","name":"tickUpper","type":"int24"},{"indexed":false,"internalType":"uint128","name":"qty","type":"uint128"},{"indexed":false,"internalType":"uint256","name":"qty0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"qty1","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"int256","name":"deltaQty0","type":"int256"},{"indexed":false,"internalType":"int256","name":"deltaQty1","type":"int256"},{"indexed":false,"internalType":"uint160","name":"sqrtP","type":"uint160"},{"indexed":false,"internalType":"uint128","name":"liquidity","type":"uint128"},{"indexed":false,"internalType":"int24","name":"currentTick","type":"int24"}],"name":"Swap","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"},{"internalType":"uint128","name":"qty","type":"uint128"}],"name":"burn","outputs":[{"internalType":"uint256","name":"qty0","type":"uint256"},{"internalType":"uint256","name":"qty1","type":"uint256"},{"internalType":"uint256","name":"feeGrowthInsideLast","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_qty","type":"uint256"},{"internalType":"bool","name":"isLogicalBurn","type":"bool"}],"name":"burnRTokens","outputs":[{"internalType":"uint256","name":"qty0","type":"uint256"},{"internalType":"uint256","name":"qty1","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"contract IFactory","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"qty0","type":"uint256"},{"internalType":"uint256","name":"qty1","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"flash","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"getFeeGrowthGlobal","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getLiquidityState","outputs":[{"internalType":"uint128","name":"baseL","type":"uint128"},{"internalType":"uint128","name":"reinvestL","type":"uint128"},{"internalType":"uint128","name":"reinvestLLast","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPoolState","outputs":[{"internalType":"uint160","name":"sqrtP","type":"uint160"},{"internalType":"int24","name":"currentTick","type":"int24"},{"internalType":"int24","name":"nearestCurrentTick","type":"int24"},{"internalType":"bool","name":"locked","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"}],"name":"getPositions","outputs":[{"internalType":"uint128","name":"liquidity","type":"uint128"},{"internalType":"uint256","name":"feeGrowthInsideLast","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getSecondsPerLiquidityData","outputs":[{"internalType":"uint128","name":"secondsPerLiquidityGlobal","type":"uint128"},{"internalType":"uint32","name":"lastUpdateTime","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"}],"name":"getSecondsPerLiquidityInside","outputs":[{"internalType":"uint128","name":"secondsPerLiquidityInside","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"int24","name":"","type":"int24"}],"name":"initializedTicks","outputs":[{"internalType":"int24","name":"previous","type":"int24"},{"internalType":"int24","name":"next","type":"int24"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxTickLiquidity","outputs":[{"internalType":"uint128","name":"","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"},{"internalType":"int24[2]","name":"ticksPrevious","type":"int24[2]"},{"internalType":"uint128","name":"qty","type":"uint128"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"mint","outputs":[{"internalType":"uint256","name":"qty0","type":"uint256"},{"internalType":"uint256","name":"qty1","type":"uint256"},{"internalType":"uint256","name":"feeGrowthInsideLast","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"int256","name":"swapQty","type":"int256"},{"internalType":"bool","name":"isToken0","type":"bool"},{"internalType":"uint160","name":"limitSqrtP","type":"uint160"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"swap","outputs":[{"internalType":"int256","name":"deltaQty0","type":"int256"},{"internalType":"int256","name":"deltaQty1","type":"int256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"swapFeeUnits","outputs":[{"internalType":"uint24","name":"","type":"uint24"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"tickDistance","outputs":[{"internalType":"int24","name":"","type":"int24"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"int24","name":"","type":"int24"}],"name":"ticks","outputs":[{"internalType":"uint128","name":"liquidityGross","type":"uint128"},{"internalType":"int128","name":"liquidityNet","type":"int128"},{"internalType":"uint256","name":"feeGrowthOutside","type":"uint256"},{"internalType":"uint128","name":"secondsPerLiquidityOutside","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token0","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token1","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint160","name":"initialSqrtP","type":"uint160"}],"name":"unlockPool","outputs":[{"internalType":"uint256","name":"qty0","type":"uint256"},{"internalType":"uint256","name":"qty1","type":"uint256"}],"stateMutability":"nonpayable","type":"function"}]