- Contract name:
- PancakeV3PoolDeployer
- Optimization enabled
- true
- Compiler version
- v0.7.6+commit.7338295f
- Optimization runs
- 400
- EVM Version
- istanbul
- Verified at
- 2024-07-20T04:26:26.453179Z
contracts/PancakeV3PoolDeployer.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity =0.7.6;
import './interfaces/IPancakeV3PoolDeployer.sol';
import './PancakeV3Pool.sol';
contract PancakeV3PoolDeployer is IPancakeV3PoolDeployer {
struct Parameters {
address factory;
address token0;
address token1;
uint24 fee;
int24 tickSpacing;
}
/// @inheritdoc IPancakeV3PoolDeployer
Parameters public override parameters;
address public factoryAddress;
/// @notice Emitted when factory address is set
event SetFactoryAddress(address indexed factory);
modifier onlyFactory() {
require(msg.sender == factoryAddress, "only factory can call deploy");
_;
}
function setFactoryAddress(address _factoryAddress) external {
require(factoryAddress == address(0), "already initialized");
factoryAddress = _factoryAddress;
emit SetFactoryAddress(_factoryAddress);
}
/// @dev Deploys a pool with the given parameters by transiently setting the parameters storage slot and then
/// clearing it after deploying the pool.
/// @param factory The contract address of the PancakeSwap V3 factory
/// @param token0 The first token of the pool by address sort order
/// @param token1 The second token of the pool by address sort order
/// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
/// @param tickSpacing The spacing between usable ticks
function deploy(
address factory,
address token0,
address token1,
uint24 fee,
int24 tickSpacing
) external override onlyFactory returns (address pool) {
parameters = Parameters({factory: factory, token0: token0, token1: token1, fee: fee, tickSpacing: tickSpacing});
pool = address(new PancakeV3Pool{salt: keccak256(abi.encode(token0, token1, fee))}());
delete parameters;
}
}
contracts/interfaces/IPancakeV3Pool.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import './pool/IPancakeV3PoolImmutables.sol';
import './pool/IPancakeV3PoolState.sol';
import './pool/IPancakeV3PoolDerivedState.sol';
import './pool/IPancakeV3PoolActions.sol';
import './pool/IPancakeV3PoolOwnerActions.sol';
import './pool/IPancakeV3PoolEvents.sol';
/// @title The interface for a PancakeSwap V3 Pool
/// @notice A PancakeSwap pool facilitates swapping and automated market making between any two assets that strictly conform
/// to the ERC20 specification
/// @dev The pool interface is broken up into many smaller pieces
interface IPancakeV3Pool is
IPancakeV3PoolImmutables,
IPancakeV3PoolState,
IPancakeV3PoolDerivedState,
IPancakeV3PoolActions,
IPancakeV3PoolOwnerActions,
IPancakeV3PoolEvents
{
}
@pancakeswap/v3-lm-pool/contracts/interfaces/IPancakeV3LmPool.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
interface IPancakeV3LmPool {
function accumulateReward(uint32 currTimestamp) external;
function crossLmTick(int24 tick, bool zeroForOne) external;
}
contracts/PancakeV3Pool.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity =0.7.6;
import './interfaces/IPancakeV3Pool.sol';
import './libraries/LowGasSafeMath.sol';
import './libraries/SafeCast.sol';
import './libraries/Tick.sol';
import './libraries/TickBitmap.sol';
import './libraries/Position.sol';
import './libraries/Oracle.sol';
import './libraries/FullMath.sol';
import './libraries/FixedPoint128.sol';
import './libraries/TransferHelper.sol';
import './libraries/TickMath.sol';
import './libraries/LiquidityMath.sol';
import './libraries/SqrtPriceMath.sol';
import './libraries/SwapMath.sol';
import './interfaces/IPancakeV3PoolDeployer.sol';
import './interfaces/IPancakeV3Factory.sol';
import './interfaces/IERC20Minimal.sol';
import './interfaces/callback/IPancakeV3MintCallback.sol';
import './interfaces/callback/IPancakeV3SwapCallback.sol';
import './interfaces/callback/IPancakeV3FlashCallback.sol';
import '@pancakeswap/v3-lm-pool/contracts/interfaces/IPancakeV3LmPool.sol';
contract PancakeV3Pool is IPancakeV3Pool {
using LowGasSafeMath for uint256;
using LowGasSafeMath for int256;
using SafeCast for uint256;
using SafeCast for int256;
using Tick for mapping(int24 => Tick.Info);
using TickBitmap for mapping(int16 => uint256);
using Position for mapping(bytes32 => Position.Info);
using Position for Position.Info;
using Oracle for Oracle.Observation[65535];
/// @inheritdoc IPancakeV3PoolImmutables
address public immutable override factory;
/// @inheritdoc IPancakeV3PoolImmutables
address public immutable override token0;
/// @inheritdoc IPancakeV3PoolImmutables
address public immutable override token1;
/// @inheritdoc IPancakeV3PoolImmutables
uint24 public immutable override fee;
/// @inheritdoc IPancakeV3PoolImmutables
int24 public immutable override tickSpacing;
/// @inheritdoc IPancakeV3PoolImmutables
uint128 public immutable override maxLiquidityPerTick;
uint32 internal constant PROTOCOL_FEE_SP = 65536;
uint256 internal constant PROTOCOL_FEE_DENOMINATOR = 10000;
struct Slot0 {
// the current price
uint160 sqrtPriceX96;
// the current tick
int24 tick;
// the most-recently updated index of the observations array
uint16 observationIndex;
// the current maximum number of observations that are being stored
uint16 observationCardinality;
// the next maximum number of observations to store, triggered in observations.write
uint16 observationCardinalityNext;
// the current protocol fee for token0 and token1,
// 2 uint32 values store in a uint32 variable (fee/PROTOCOL_FEE_DENOMINATOR)
uint32 feeProtocol;
// whether the pool is locked
bool unlocked;
}
/// @inheritdoc IPancakeV3PoolState
Slot0 public override slot0;
/// @inheritdoc IPancakeV3PoolState
uint256 public override feeGrowthGlobal0X128;
/// @inheritdoc IPancakeV3PoolState
uint256 public override feeGrowthGlobal1X128;
// accumulated protocol fees in token0/token1 units
struct ProtocolFees {
uint128 token0;
uint128 token1;
}
/// @inheritdoc IPancakeV3PoolState
ProtocolFees public override protocolFees;
/// @inheritdoc IPancakeV3PoolState
uint128 public override liquidity;
/// @inheritdoc IPancakeV3PoolState
mapping(int24 => Tick.Info) public override ticks;
/// @inheritdoc IPancakeV3PoolState
mapping(int16 => uint256) public override tickBitmap;
/// @inheritdoc IPancakeV3PoolState
mapping(bytes32 => Position.Info) public override positions;
/// @inheritdoc IPancakeV3PoolState
Oracle.Observation[65535] public override observations;
// liquidity mining
IPancakeV3LmPool public lmPool;
event SetLmPoolEvent(address addr);
/// @dev Mutually exclusive reentrancy protection into the pool to/from a method. This method also prevents entrance
/// to a function before the pool is initialized. The reentrancy guard is required throughout the contract because
/// we use balance checks to determine the payment status of interactions such as mint, swap and flash.
modifier lock() {
require(slot0.unlocked, 'LOK');
slot0.unlocked = false;
_;
slot0.unlocked = true;
}
/// @dev Prevents calling a function from anyone except the factory or its
/// owner
modifier onlyFactoryOrFactoryOwner() {
require(msg.sender == factory || msg.sender == IPancakeV3Factory(factory).owner());
_;
}
constructor() {
int24 _tickSpacing;
(factory, token0, token1, fee, _tickSpacing) = IPancakeV3PoolDeployer(msg.sender).parameters();
tickSpacing = _tickSpacing;
maxLiquidityPerTick = Tick.tickSpacingToMaxLiquidityPerTick(_tickSpacing);
}
/// @dev Common checks for valid tick inputs.
function checkTicks(int24 tickLower, int24 tickUpper) private pure {
require(tickLower < tickUpper, 'TLU');
require(tickLower >= TickMath.MIN_TICK, 'TLM');
require(tickUpper <= TickMath.MAX_TICK, 'TUM');
}
/// @dev Returns the block timestamp truncated to 32 bits, i.e. mod 2**32. This method is overridden in tests.
function _blockTimestamp() internal view virtual returns (uint32) {
return uint32(block.timestamp); // truncation is desired
}
/// @dev Get the pool's balance of token0
/// @dev This function is gas optimized to avoid a redundant extcodesize check in addition to the returndatasize
/// check
function balance0() private view returns (uint256) {
(bool success, bytes memory data) = token0.staticcall(
abi.encodeWithSelector(IERC20Minimal.balanceOf.selector, address(this))
);
require(success && data.length >= 32);
return abi.decode(data, (uint256));
}
/// @dev Get the pool's balance of token1
/// @dev This function is gas optimized to avoid a redundant extcodesize check in addition to the returndatasize
/// check
function balance1() private view returns (uint256) {
(bool success, bytes memory data) = token1.staticcall(
abi.encodeWithSelector(IERC20Minimal.balanceOf.selector, address(this))
);
require(success && data.length >= 32);
return abi.decode(data, (uint256));
}
/// @inheritdoc IPancakeV3PoolDerivedState
function snapshotCumulativesInside(int24 tickLower, int24 tickUpper)
external
view
override
returns (
int56 tickCumulativeInside,
uint160 secondsPerLiquidityInsideX128,
uint32 secondsInside
)
{
checkTicks(tickLower, tickUpper);
int56 tickCumulativeLower;
int56 tickCumulativeUpper;
uint160 secondsPerLiquidityOutsideLowerX128;
uint160 secondsPerLiquidityOutsideUpperX128;
uint32 secondsOutsideLower;
uint32 secondsOutsideUpper;
{
Tick.Info storage lower = ticks[tickLower];
Tick.Info storage upper = ticks[tickUpper];
bool initializedLower;
(tickCumulativeLower, secondsPerLiquidityOutsideLowerX128, secondsOutsideLower, initializedLower) = (
lower.tickCumulativeOutside,
lower.secondsPerLiquidityOutsideX128,
lower.secondsOutside,
lower.initialized
);
require(initializedLower);
bool initializedUpper;
(tickCumulativeUpper, secondsPerLiquidityOutsideUpperX128, secondsOutsideUpper, initializedUpper) = (
upper.tickCumulativeOutside,
upper.secondsPerLiquidityOutsideX128,
upper.secondsOutside,
upper.initialized
);
require(initializedUpper);
}
Slot0 memory _slot0 = slot0;
if (_slot0.tick < tickLower) {
return (
tickCumulativeLower - tickCumulativeUpper,
secondsPerLiquidityOutsideLowerX128 - secondsPerLiquidityOutsideUpperX128,
secondsOutsideLower - secondsOutsideUpper
);
} else if (_slot0.tick < tickUpper) {
uint32 time = _blockTimestamp();
(int56 tickCumulative, uint160 secondsPerLiquidityCumulativeX128) = observations.observeSingle(
time,
0,
_slot0.tick,
_slot0.observationIndex,
liquidity,
_slot0.observationCardinality
);
return (
tickCumulative - tickCumulativeLower - tickCumulativeUpper,
secondsPerLiquidityCumulativeX128 -
secondsPerLiquidityOutsideLowerX128 -
secondsPerLiquidityOutsideUpperX128,
time - secondsOutsideLower - secondsOutsideUpper
);
} else {
return (
tickCumulativeUpper - tickCumulativeLower,
secondsPerLiquidityOutsideUpperX128 - secondsPerLiquidityOutsideLowerX128,
secondsOutsideUpper - secondsOutsideLower
);
}
}
/// @inheritdoc IPancakeV3PoolDerivedState
function observe(uint32[] calldata secondsAgos)
external
view
override
returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s)
{
return
observations.observe(
_blockTimestamp(),
secondsAgos,
slot0.tick,
slot0.observationIndex,
liquidity,
slot0.observationCardinality
);
}
/// @inheritdoc IPancakeV3PoolActions
function increaseObservationCardinalityNext(uint16 observationCardinalityNext)
external
override
lock
{
uint16 observationCardinalityNextOld = slot0.observationCardinalityNext; // for the event
uint16 observationCardinalityNextNew = observations.grow(
observationCardinalityNextOld,
observationCardinalityNext
);
slot0.observationCardinalityNext = observationCardinalityNextNew;
if (observationCardinalityNextOld != observationCardinalityNextNew)
emit IncreaseObservationCardinalityNext(observationCardinalityNextOld, observationCardinalityNextNew);
}
/// @inheritdoc IPancakeV3PoolActions
/// @dev not locked because it initializes unlocked
function initialize(uint160 sqrtPriceX96) external override {
require(slot0.sqrtPriceX96 == 0, 'AI');
int24 tick = TickMath.getTickAtSqrtRatio(sqrtPriceX96);
(uint16 cardinality, uint16 cardinalityNext) = observations.initialize(_blockTimestamp());
slot0 = Slot0({
sqrtPriceX96: sqrtPriceX96,
tick: tick,
observationIndex: 0,
observationCardinality: cardinality,
observationCardinalityNext: cardinalityNext,
feeProtocol: 209718400, // default value for all pools, 3200:3200, store 2 uint32 inside
unlocked: true
});
if (fee == 100) {
slot0.feeProtocol = 216272100; // value for 3300:3300, store 2 uint32 inside
} else if (fee == 500) {
slot0.feeProtocol = 222825800; // value for 3400:3400, store 2 uint32 inside
} else if (fee == 2500) {
slot0.feeProtocol = 209718400; // value for 3200:3200, store 2 uint32 inside
} else if (fee == 10000) {
slot0.feeProtocol = 209718400; // value for 3200:3200, store 2 uint32 inside
}
emit Initialize(sqrtPriceX96, tick);
}
struct ModifyPositionParams {
// the address that owns the position
address owner;
// the lower and upper tick of the position
int24 tickLower;
int24 tickUpper;
// any change in liquidity
int128 liquidityDelta;
}
/// @dev Effect some changes to a position
/// @param params the position details and the change to the position's liquidity to effect
/// @return position a storage pointer referencing the position with the given owner and tick range
/// @return amount0 the amount of token0 owed to the pool, negative if the pool should pay the recipient
/// @return amount1 the amount of token1 owed to the pool, negative if the pool should pay the recipient
function _modifyPosition(ModifyPositionParams memory params)
private
returns (
Position.Info storage position,
int256 amount0,
int256 amount1
)
{
checkTicks(params.tickLower, params.tickUpper);
Slot0 memory _slot0 = slot0; // SLOAD for gas optimization
position = _updatePosition(
params.owner,
params.tickLower,
params.tickUpper,
params.liquidityDelta,
_slot0.tick
);
if (params.liquidityDelta != 0) {
if (_slot0.tick < params.tickLower) {
// current tick is below the passed range; liquidity can only become in range by crossing from left to
// right, when we'll need _more_ token0 (it's becoming more valuable) so user must provide it
amount0 = SqrtPriceMath.getAmount0Delta(
TickMath.getSqrtRatioAtTick(params.tickLower),
TickMath.getSqrtRatioAtTick(params.tickUpper),
params.liquidityDelta
);
} else if (_slot0.tick < params.tickUpper) {
// current tick is inside the passed range
uint128 liquidityBefore = liquidity; // SLOAD for gas optimization
// write an oracle entry
(slot0.observationIndex, slot0.observationCardinality) = observations.write(
_slot0.observationIndex,
_blockTimestamp(),
_slot0.tick,
liquidityBefore,
_slot0.observationCardinality,
_slot0.observationCardinalityNext
);
amount0 = SqrtPriceMath.getAmount0Delta(
_slot0.sqrtPriceX96,
TickMath.getSqrtRatioAtTick(params.tickUpper),
params.liquidityDelta
);
amount1 = SqrtPriceMath.getAmount1Delta(
TickMath.getSqrtRatioAtTick(params.tickLower),
_slot0.sqrtPriceX96,
params.liquidityDelta
);
liquidity = LiquidityMath.addDelta(liquidityBefore, params.liquidityDelta);
} else {
// current tick is above the passed range; liquidity can only become in range by crossing from right to
// left, when we'll need _more_ token1 (it's becoming more valuable) so user must provide it
amount1 = SqrtPriceMath.getAmount1Delta(
TickMath.getSqrtRatioAtTick(params.tickLower),
TickMath.getSqrtRatioAtTick(params.tickUpper),
params.liquidityDelta
);
}
}
}
/// @dev Gets and updates a position with the given liquidity delta
/// @param owner the owner of the position
/// @param tickLower the lower tick of the position's tick range
/// @param tickUpper the upper tick of the position's tick range
/// @param tick the current tick, passed to avoid sloads
function _updatePosition(
address owner,
int24 tickLower,
int24 tickUpper,
int128 liquidityDelta,
int24 tick
) private returns (Position.Info storage position) {
position = positions.get(owner, tickLower, tickUpper);
uint256 _feeGrowthGlobal0X128 = feeGrowthGlobal0X128; // SLOAD for gas optimization
uint256 _feeGrowthGlobal1X128 = feeGrowthGlobal1X128; // SLOAD for gas optimization
// if we need to update the ticks, do it
bool flippedLower;
bool flippedUpper;
if (liquidityDelta != 0) {
uint32 time = _blockTimestamp();
(int56 tickCumulative, uint160 secondsPerLiquidityCumulativeX128) = observations.observeSingle(
time,
0,
slot0.tick,
slot0.observationIndex,
liquidity,
slot0.observationCardinality
);
flippedLower = ticks.update(
tickLower,
tick,
liquidityDelta,
_feeGrowthGlobal0X128,
_feeGrowthGlobal1X128,
secondsPerLiquidityCumulativeX128,
tickCumulative,
time,
false,
maxLiquidityPerTick
);
flippedUpper = ticks.update(
tickUpper,
tick,
liquidityDelta,
_feeGrowthGlobal0X128,
_feeGrowthGlobal1X128,
secondsPerLiquidityCumulativeX128,
tickCumulative,
time,
true,
maxLiquidityPerTick
);
if (flippedLower) {
tickBitmap.flipTick(tickLower, tickSpacing);
}
if (flippedUpper) {
tickBitmap.flipTick(tickUpper, tickSpacing);
}
}
(uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) = ticks.getFeeGrowthInside(
tickLower,
tickUpper,
tick,
_feeGrowthGlobal0X128,
_feeGrowthGlobal1X128
);
position.update(liquidityDelta, feeGrowthInside0X128, feeGrowthInside1X128);
// clear any tick data that is no longer needed
if (liquidityDelta < 0) {
if (flippedLower) {
ticks.clear(tickLower);
}
if (flippedUpper) {
ticks.clear(tickUpper);
}
}
}
/// @inheritdoc IPancakeV3PoolActions
/// @dev noDelegateCall is applied indirectly via _modifyPosition
function mint(
address recipient,
int24 tickLower,
int24 tickUpper,
uint128 amount,
bytes calldata data
) external override lock returns (uint256 amount0, uint256 amount1) {
require(amount > 0);
(, int256 amount0Int, int256 amount1Int) = _modifyPosition(
ModifyPositionParams({
owner: recipient,
tickLower: tickLower,
tickUpper: tickUpper,
liquidityDelta: int256(amount).toInt128()
})
);
amount0 = uint256(amount0Int);
amount1 = uint256(amount1Int);
uint256 balance0Before;
uint256 balance1Before;
if (amount0 > 0) balance0Before = balance0();
if (amount1 > 0) balance1Before = balance1();
IPancakeV3MintCallback(msg.sender).pancakeV3MintCallback(amount0, amount1, data);
if (amount0 > 0) require(balance0Before.add(amount0) <= balance0(), 'M0');
if (amount1 > 0) require(balance1Before.add(amount1) <= balance1(), 'M1');
emit Mint(msg.sender, recipient, tickLower, tickUpper, amount, amount0, amount1);
}
/// @inheritdoc IPancakeV3PoolActions
function collect(
address recipient,
int24 tickLower,
int24 tickUpper,
uint128 amount0Requested,
uint128 amount1Requested
) external override lock returns (uint128 amount0, uint128 amount1) {
// we don't need to checkTicks here, because invalid positions will never have non-zero tokensOwed{0,1}
Position.Info storage position = positions.get(msg.sender, tickLower, tickUpper);
amount0 = amount0Requested > position.tokensOwed0 ? position.tokensOwed0 : amount0Requested;
amount1 = amount1Requested > position.tokensOwed1 ? position.tokensOwed1 : amount1Requested;
if (amount0 > 0) {
position.tokensOwed0 -= amount0;
TransferHelper.safeTransfer(token0, recipient, amount0);
}
if (amount1 > 0) {
position.tokensOwed1 -= amount1;
TransferHelper.safeTransfer(token1, recipient, amount1);
}
emit Collect(msg.sender, recipient, tickLower, tickUpper, amount0, amount1);
}
/// @inheritdoc IPancakeV3PoolActions
/// @dev noDelegateCall is applied indirectly via _modifyPosition
function burn(
int24 tickLower,
int24 tickUpper,
uint128 amount
) external override lock returns (uint256 amount0, uint256 amount1) {
(Position.Info storage position, int256 amount0Int, int256 amount1Int) = _modifyPosition(
ModifyPositionParams({
owner: msg.sender,
tickLower: tickLower,
tickUpper: tickUpper,
liquidityDelta: -int256(amount).toInt128()
})
);
amount0 = uint256(-amount0Int);
amount1 = uint256(-amount1Int);
if (amount0 > 0 || amount1 > 0) {
(position.tokensOwed0, position.tokensOwed1) = (
position.tokensOwed0 + uint128(amount0),
position.tokensOwed1 + uint128(amount1)
);
}
emit Burn(msg.sender, tickLower, tickUpper, amount, amount0, amount1);
}
struct SwapCache {
// the protocol fee for the input token
uint32 feeProtocol;
// liquidity at the beginning of the swap
uint128 liquidityStart;
// the timestamp of the current block
uint32 blockTimestamp;
// the current value of the tick accumulator, computed only if we cross an initialized tick
int56 tickCumulative;
// the current value of seconds per liquidity accumulator, computed only if we cross an initialized tick
uint160 secondsPerLiquidityCumulativeX128;
// whether we've computed and cached the above two accumulators
bool computedLatestObservation;
}
// the top level state of the swap, the results of which are recorded in storage at the end
struct SwapState {
// the amount remaining to be swapped in/out of the input/output asset
int256 amountSpecifiedRemaining;
// the amount already swapped out/in of the output/input asset
int256 amountCalculated;
// current sqrt(price)
uint160 sqrtPriceX96;
// the tick associated with the current price
int24 tick;
// the global fee growth of the input token
uint256 feeGrowthGlobalX128;
// amount of input token paid as protocol fee
uint128 protocolFee;
// the current liquidity in range
uint128 liquidity;
}
struct StepComputations {
// the price at the beginning of the step
uint160 sqrtPriceStartX96;
// the next tick to swap to from the current tick in the swap direction
int24 tickNext;
// whether tickNext is initialized or not
bool initialized;
// sqrt(price) for the next tick (1/0)
uint160 sqrtPriceNextX96;
// how much is being swapped in in this step
uint256 amountIn;
// how much is being swapped out
uint256 amountOut;
// how much fee is being paid in
uint256 feeAmount;
}
/// @inheritdoc IPancakeV3PoolActions
function swap(
address recipient,
bool zeroForOne,
int256 amountSpecified,
uint160 sqrtPriceLimitX96,
bytes calldata data
) external override returns (int256 amount0, int256 amount1) {
require(amountSpecified != 0, 'AS');
Slot0 memory slot0Start = slot0;
require(slot0Start.unlocked, 'LOK');
require(
zeroForOne
? sqrtPriceLimitX96 < slot0Start.sqrtPriceX96 && sqrtPriceLimitX96 > TickMath.MIN_SQRT_RATIO
: sqrtPriceLimitX96 > slot0Start.sqrtPriceX96 && sqrtPriceLimitX96 < TickMath.MAX_SQRT_RATIO,
'SPL'
);
slot0.unlocked = false;
SwapCache memory cache = SwapCache({
liquidityStart: liquidity,
blockTimestamp: _blockTimestamp(),
feeProtocol: zeroForOne ? (slot0Start.feeProtocol % PROTOCOL_FEE_SP) : (slot0Start.feeProtocol >> 16),
secondsPerLiquidityCumulativeX128: 0,
tickCumulative: 0,
computedLatestObservation: false
});
if (address(lmPool) != address(0)) {
lmPool.accumulateReward(cache.blockTimestamp);
}
bool exactInput = amountSpecified > 0;
SwapState memory state = SwapState({
amountSpecifiedRemaining: amountSpecified,
amountCalculated: 0,
sqrtPriceX96: slot0Start.sqrtPriceX96,
tick: slot0Start.tick,
feeGrowthGlobalX128: zeroForOne ? feeGrowthGlobal0X128 : feeGrowthGlobal1X128,
protocolFee: 0,
liquidity: cache.liquidityStart
});
// continue swapping as long as we haven't used the entire input/output and haven't reached the price limit
while (state.amountSpecifiedRemaining != 0 && state.sqrtPriceX96 != sqrtPriceLimitX96) {
StepComputations memory step;
step.sqrtPriceStartX96 = state.sqrtPriceX96;
(step.tickNext, step.initialized) = tickBitmap.nextInitializedTickWithinOneWord(
state.tick,
tickSpacing,
zeroForOne
);
// ensure that we do not overshoot the min/max tick, as the tick bitmap is not aware of these bounds
if (step.tickNext < TickMath.MIN_TICK) {
step.tickNext = TickMath.MIN_TICK;
} else if (step.tickNext > TickMath.MAX_TICK) {
step.tickNext = TickMath.MAX_TICK;
}
// get the price for the next tick
step.sqrtPriceNextX96 = TickMath.getSqrtRatioAtTick(step.tickNext);
// compute values to swap to the target tick, price limit, or point where input/output amount is exhausted
(state.sqrtPriceX96, step.amountIn, step.amountOut, step.feeAmount) = SwapMath.computeSwapStep(
state.sqrtPriceX96,
(zeroForOne ? step.sqrtPriceNextX96 < sqrtPriceLimitX96 : step.sqrtPriceNextX96 > sqrtPriceLimitX96)
? sqrtPriceLimitX96
: step.sqrtPriceNextX96,
state.liquidity,
state.amountSpecifiedRemaining,
fee
);
if (exactInput) {
state.amountSpecifiedRemaining -= (step.amountIn + step.feeAmount).toInt256();
state.amountCalculated = state.amountCalculated.sub(step.amountOut.toInt256());
} else {
state.amountSpecifiedRemaining += step.amountOut.toInt256();
state.amountCalculated = state.amountCalculated.add((step.amountIn + step.feeAmount).toInt256());
}
// if the protocol fee is on, calculate how much is owed, decrement feeAmount, and increment protocolFee
if (cache.feeProtocol > 0) {
uint256 delta = (step.feeAmount.mul(cache.feeProtocol)) / PROTOCOL_FEE_DENOMINATOR;
step.feeAmount -= delta;
state.protocolFee += uint128(delta);
}
// update global fee tracker
if (state.liquidity > 0)
state.feeGrowthGlobalX128 += FullMath.mulDiv(step.feeAmount, FixedPoint128.Q128, state.liquidity);
// shift tick if we reached the next price
if (state.sqrtPriceX96 == step.sqrtPriceNextX96) {
// if the tick is initialized, run the tick transition
if (step.initialized) {
// check for the placeholder value, which we replace with the actual value the first time the swap
// crosses an initialized tick
if (!cache.computedLatestObservation) {
(cache.tickCumulative, cache.secondsPerLiquidityCumulativeX128) = observations.observeSingle(
cache.blockTimestamp,
0,
slot0Start.tick,
slot0Start.observationIndex,
cache.liquidityStart,
slot0Start.observationCardinality
);
cache.computedLatestObservation = true;
}
if (address(lmPool) != address(0)) {
lmPool.crossLmTick(step.tickNext, zeroForOne);
}
int128 liquidityNet = ticks.cross(
step.tickNext,
(zeroForOne ? state.feeGrowthGlobalX128 : feeGrowthGlobal0X128),
(zeroForOne ? feeGrowthGlobal1X128 : state.feeGrowthGlobalX128),
cache.secondsPerLiquidityCumulativeX128,
cache.tickCumulative,
cache.blockTimestamp
);
// if we're moving leftward, we interpret liquidityNet as the opposite sign
// safe because liquidityNet cannot be type(int128).min
if (zeroForOne) liquidityNet = -liquidityNet;
state.liquidity = LiquidityMath.addDelta(state.liquidity, liquidityNet);
}
state.tick = zeroForOne ? step.tickNext - 1 : step.tickNext;
} else if (state.sqrtPriceX96 != step.sqrtPriceStartX96) {
// recompute unless we're on a lower tick boundary (i.e. already transitioned ticks), and haven't moved
state.tick = TickMath.getTickAtSqrtRatio(state.sqrtPriceX96);
}
}
// update tick and write an oracle entry if the tick change
if (state.tick != slot0Start.tick) {
(uint16 observationIndex, uint16 observationCardinality) = observations.write(
slot0Start.observationIndex,
cache.blockTimestamp,
slot0Start.tick,
cache.liquidityStart,
slot0Start.observationCardinality,
slot0Start.observationCardinalityNext
);
(slot0.sqrtPriceX96, slot0.tick, slot0.observationIndex, slot0.observationCardinality) = (
state.sqrtPriceX96,
state.tick,
observationIndex,
observationCardinality
);
} else {
// otherwise just update the price
slot0.sqrtPriceX96 = state.sqrtPriceX96;
}
// update liquidity if it changed
if (cache.liquidityStart != state.liquidity) liquidity = state.liquidity;
uint128 protocolFeesToken0 = 0;
uint128 protocolFeesToken1 = 0;
// update fee growth global and, if necessary, protocol fees
// overflow is acceptable, protocol has to withdraw before it hits type(uint128).max fees
if (zeroForOne) {
feeGrowthGlobal0X128 = state.feeGrowthGlobalX128;
if (state.protocolFee > 0) protocolFees.token0 += state.protocolFee;
protocolFeesToken0 = state.protocolFee;
} else {
feeGrowthGlobal1X128 = state.feeGrowthGlobalX128;
if (state.protocolFee > 0) protocolFees.token1 += state.protocolFee;
protocolFeesToken1 = state.protocolFee;
}
(amount0, amount1) = zeroForOne == exactInput
? (amountSpecified - state.amountSpecifiedRemaining, state.amountCalculated)
: (state.amountCalculated, amountSpecified - state.amountSpecifiedRemaining);
// do the transfers and collect payment
if (zeroForOne) {
if (amount1 < 0) TransferHelper.safeTransfer(token1, recipient, uint256(-amount1));
uint256 balance0Before = balance0();
IPancakeV3SwapCallback(msg.sender).pancakeV3SwapCallback(amount0, amount1, data);
require(balance0Before.add(uint256(amount0)) <= balance0(), 'IIA');
} else {
if (amount0 < 0) TransferHelper.safeTransfer(token0, recipient, uint256(-amount0));
uint256 balance1Before = balance1();
IPancakeV3SwapCallback(msg.sender).pancakeV3SwapCallback(amount0, amount1, data);
require(balance1Before.add(uint256(amount1)) <= balance1(), 'IIA');
}
emit Swap(msg.sender, recipient, amount0, amount1, state.sqrtPriceX96, state.liquidity, state.tick, protocolFeesToken0, protocolFeesToken1);
slot0.unlocked = true;
}
/// @inheritdoc IPancakeV3PoolActions
function flash(
address recipient,
uint256 amount0,
uint256 amount1,
bytes calldata data
) external override lock {
uint128 _liquidity = liquidity;
require(_liquidity > 0, 'L');
uint256 fee0 = FullMath.mulDivRoundingUp(amount0, fee, 1e6);
uint256 fee1 = FullMath.mulDivRoundingUp(amount1, fee, 1e6);
uint256 balance0Before = balance0();
uint256 balance1Before = balance1();
if (amount0 > 0) TransferHelper.safeTransfer(token0, recipient, amount0);
if (amount1 > 0) TransferHelper.safeTransfer(token1, recipient, amount1);
IPancakeV3FlashCallback(msg.sender).pancakeV3FlashCallback(fee0, fee1, data);
uint256 balance0After = balance0();
uint256 balance1After = balance1();
require(balance0Before.add(fee0) <= balance0After, 'F0');
require(balance1Before.add(fee1) <= balance1After, 'F1');
// sub is safe because we know balanceAfter is gt balanceBefore by at least fee
uint256 paid0 = balance0After - balance0Before;
uint256 paid1 = balance1After - balance1Before;
if (paid0 > 0) {
uint32 feeProtocol0 = slot0.feeProtocol % PROTOCOL_FEE_SP;
uint256 fees0 = feeProtocol0 == 0 ? 0 : (paid0 * feeProtocol0) / PROTOCOL_FEE_DENOMINATOR;
if (uint128(fees0) > 0) protocolFees.token0 += uint128(fees0);
feeGrowthGlobal0X128 += FullMath.mulDiv(paid0 - fees0, FixedPoint128.Q128, _liquidity);
}
if (paid1 > 0) {
uint32 feeProtocol1 = slot0.feeProtocol >> 16;
uint256 fees1 = feeProtocol1 == 0 ? 0 : (paid1 * feeProtocol1) / PROTOCOL_FEE_DENOMINATOR;
if (uint128(fees1) > 0) protocolFees.token1 += uint128(fees1);
feeGrowthGlobal1X128 += FullMath.mulDiv(paid1 - fees1, FixedPoint128.Q128, _liquidity);
}
emit Flash(msg.sender, recipient, amount0, amount1, paid0, paid1);
}
/// @inheritdoc IPancakeV3PoolOwnerActions
function setFeeProtocol(uint32 feeProtocol0, uint32 feeProtocol1) external override lock onlyFactoryOrFactoryOwner {
require(
(feeProtocol0 == 0 || (feeProtocol0 >= 1000 && feeProtocol0 <= 4000)) &&
(feeProtocol1 == 0 || (feeProtocol1 >= 1000 && feeProtocol1 <= 4000))
);
uint32 feeProtocolOld = slot0.feeProtocol;
slot0.feeProtocol = feeProtocol0 + (feeProtocol1 << 16);
emit SetFeeProtocol(feeProtocolOld % PROTOCOL_FEE_SP, feeProtocolOld >> 16, feeProtocol0, feeProtocol1);
}
/// @inheritdoc IPancakeV3PoolOwnerActions
function collectProtocol(
address recipient,
uint128 amount0Requested,
uint128 amount1Requested
) external override lock onlyFactoryOrFactoryOwner returns (uint128 amount0, uint128 amount1) {
amount0 = amount0Requested > protocolFees.token0 ? protocolFees.token0 : amount0Requested;
amount1 = amount1Requested > protocolFees.token1 ? protocolFees.token1 : amount1Requested;
if (amount0 > 0) {
if (amount0 == protocolFees.token0) amount0--; // ensure that the slot is not cleared, for gas savings
protocolFees.token0 -= amount0;
TransferHelper.safeTransfer(token0, recipient, amount0);
}
if (amount1 > 0) {
if (amount1 == protocolFees.token1) amount1--; // ensure that the slot is not cleared, for gas savings
protocolFees.token1 -= amount1;
TransferHelper.safeTransfer(token1, recipient, amount1);
}
emit CollectProtocol(msg.sender, recipient, amount0, amount1);
}
function setLmPool(address _lmPool) external override onlyFactoryOrFactoryOwner {
lmPool = IPancakeV3LmPool(_lmPool);
emit SetLmPoolEvent(address(_lmPool));
}
}
contracts/interfaces/IERC20Minimal.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Minimal ERC20 interface for PancakeSwap
/// @notice Contains a subset of the full ERC20 interface that is used in PancakeSwap V3
interface IERC20Minimal {
/// @notice Returns the balance of a token
/// @param account The account for which to look up the number of tokens it has, i.e. its balance
/// @return The number of tokens held by the account
function balanceOf(address account) external view returns (uint256);
/// @notice Transfers the amount of token from the `msg.sender` to the recipient
/// @param recipient The account that will receive the amount transferred
/// @param amount The number of tokens to send from the sender to the recipient
/// @return Returns true for a successful transfer, false for an unsuccessful transfer
function transfer(address recipient, uint256 amount) external returns (bool);
/// @notice Returns the current allowance given to a spender by an owner
/// @param owner The account of the token owner
/// @param spender The account of the token spender
/// @return The current allowance granted by `owner` to `spender`
function allowance(address owner, address spender) external view returns (uint256);
/// @notice Sets the allowance of a spender from the `msg.sender` to the value `amount`
/// @param spender The account which will be allowed to spend a given amount of the owners tokens
/// @param amount The amount of tokens allowed to be used by `spender`
/// @return Returns true for a successful approval, false for unsuccessful
function approve(address spender, uint256 amount) external returns (bool);
/// @notice Transfers `amount` tokens from `sender` to `recipient` up to the allowance given to the `msg.sender`
/// @param sender The account from which the transfer will be initiated
/// @param recipient The recipient of the transfer
/// @param amount The amount of the transfer
/// @return Returns true for a successful transfer, false for unsuccessful
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
/// @notice Event emitted when tokens are transferred from one address to another, either via `#transfer` or `#transferFrom`.
/// @param from The account from which the tokens were sent, i.e. the balance decreased
/// @param to The account to which the tokens were sent, i.e. the balance increased
/// @param value The amount of tokens that were transferred
event Transfer(address indexed from, address indexed to, uint256 value);
/// @notice Event emitted when the approval amount for the spender of a given owner's tokens changes.
/// @param owner The account that approved spending of its tokens
/// @param spender The account for which the spending allowance was modified
/// @param value The new allowance from the owner to the spender
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contracts/interfaces/IPancakeV3Factory.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title The interface for the PancakeSwap V3 Factory
/// @notice The PancakeSwap V3 Factory facilitates creation of PancakeSwap V3 pools and control over the protocol fees
interface IPancakeV3Factory {
struct TickSpacingExtraInfo {
bool whitelistRequested;
bool enabled;
}
/// @notice Emitted when the owner of the factory is changed
/// @param oldOwner The owner before the owner was changed
/// @param newOwner The owner after the owner was changed
event OwnerChanged(address indexed oldOwner, address indexed newOwner);
/// @notice Emitted when a pool is created
/// @param token0 The first token of the pool by address sort order
/// @param token1 The second token of the pool by address sort order
/// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
/// @param tickSpacing The minimum number of ticks between initialized ticks
/// @param pool The address of the created pool
event PoolCreated(
address indexed token0,
address indexed token1,
uint24 indexed fee,
int24 tickSpacing,
address pool
);
/// @notice Emitted when a new fee amount is enabled for pool creation via the factory
/// @param fee The enabled fee, denominated in hundredths of a bip
/// @param tickSpacing The minimum number of ticks between initialized ticks for pools created with the given fee
event FeeAmountEnabled(uint24 indexed fee, int24 indexed tickSpacing);
event FeeAmountExtraInfoUpdated(uint24 indexed fee, bool whitelistRequested, bool enabled);
event WhiteListAdded(address indexed user, bool verified);
/// @notice Emitted when LM pool deployer is set
event SetLmPoolDeployer(address indexed lmPoolDeployer);
/// @notice Returns the current owner of the factory
/// @dev Can be changed by the current owner via setOwner
/// @return The address of the factory owner
function owner() external view returns (address);
/// @notice Returns the tick spacing for a given fee amount, if enabled, or 0 if not enabled
/// @dev A fee amount can never be removed, so this value should be hard coded or cached in the calling context
/// @param fee The enabled fee, denominated in hundredths of a bip. Returns 0 in case of unenabled fee
/// @return The tick spacing
function feeAmountTickSpacing(uint24 fee) external view returns (int24);
/// @notice Returns the tick spacing extra info
/// @dev A fee amount can never be removed, so this value should be hard coded or cached in the calling context
/// @param fee The enabled fee, denominated in hundredths of a bip. Returns 0 in case of unenabled fee
/// @return whitelistRequested The flag whether should be created by white list users only
function feeAmountTickSpacingExtraInfo(uint24 fee) external view returns (bool whitelistRequested, bool enabled);
/// @notice Returns the pool address for a given pair of tokens and a fee, or address 0 if it does not exist
/// @dev tokenA and tokenB may be passed in either token0/token1 or token1/token0 order
/// @param tokenA The contract address of either token0 or token1
/// @param tokenB The contract address of the other token
/// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
/// @return pool The pool address
function getPool(
address tokenA,
address tokenB,
uint24 fee
) external view returns (address pool);
/// @notice Creates a pool for the given two tokens and fee
/// @param tokenA One of the two tokens in the desired pool
/// @param tokenB The other of the two tokens in the desired pool
/// @param fee The desired fee for the pool
/// @dev tokenA and tokenB may be passed in either order: token0/token1 or token1/token0. tickSpacing is retrieved
/// from the fee. The call will revert if the pool already exists, the fee is invalid, or the token arguments
/// are invalid.
/// @return pool The address of the newly created pool
function createPool(
address tokenA,
address tokenB,
uint24 fee
) external returns (address pool);
/// @notice Updates the owner of the factory
/// @dev Must be called by the current owner
/// @param _owner The new owner of the factory
function setOwner(address _owner) external;
/// @notice Enables a fee amount with the given tickSpacing
/// @dev Fee amounts may never be removed once enabled
/// @param fee The fee amount to enable, denominated in hundredths of a bip (i.e. 1e-6)
/// @param tickSpacing The spacing between ticks to be enforced for all pools created with the given fee amount
function enableFeeAmount(uint24 fee, int24 tickSpacing) external;
/// @notice Set an address into white list
/// @dev Address can be updated by owner with boolean value false
/// @param user The user address that add into white list
function setWhiteListAddress(address user, bool verified) external;
/// @notice Set a fee amount extra info
/// @dev Fee amounts can be updated by owner with extra info
/// @param whitelistRequested The flag whether should be created by owner only
/// @param enabled The flag is the fee is enabled or not
function setFeeAmountExtraInfo(
uint24 fee,
bool whitelistRequested,
bool enabled
) external;
function setLmPoolDeployer(address _lmPoolDeployer) external;
function setFeeProtocol(address pool, uint32 feeProtocol0, uint32 feeProtocol1) external;
function collectProtocol(
address pool,
address recipient,
uint128 amount0Requested,
uint128 amount1Requested
) external returns (uint128 amount0, uint128 amount1);
function setLmPool(address pool, address lmPool) external;
}
contracts/interfaces/IPancakeV3PoolDeployer.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title An interface for a contract that is capable of deploying PancakeSwap V3 Pools
/// @notice A contract that constructs a pool must implement this to pass arguments to the pool
/// @dev This is used to avoid having constructor arguments in the pool contract, which results in the init code hash
/// of the pool being constant allowing the CREATE2 address of the pool to be cheaply computed on-chain
interface IPancakeV3PoolDeployer {
/// @notice Get the parameters to be used in constructing the pool, set transiently during pool creation.
/// @dev Called by the pool constructor to fetch the parameters of the pool
/// Returns factory The factory address
/// Returns token0 The first token of the pool by address sort order
/// Returns token1 The second token of the pool by address sort order
/// Returns fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
/// Returns tickSpacing The minimum number of ticks between initialized ticks
function parameters()
external
view
returns (
address factory,
address token0,
address token1,
uint24 fee,
int24 tickSpacing
);
function deploy(
address factory,
address token0,
address token1,
uint24 fee,
int24 tickSpacing
) external returns (address pool);
}
contracts/interfaces/callback/IPancakeV3FlashCallback.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Callback for IPancakeV3PoolActions#flash
/// @notice Any contract that calls IPancakeV3PoolActions#flash must implement this interface
interface IPancakeV3FlashCallback {
/// @notice Called to `msg.sender` after transferring to the recipient from IPancakeV3Pool#flash.
/// @dev In the implementation you must repay the pool the tokens sent by flash plus the computed fee amounts.
/// The caller of this method must be checked to be a PancakeV3Pool deployed by the canonical PancakeV3Factory.
/// @param fee0 The fee amount in token0 due to the pool by the end of the flash
/// @param fee1 The fee amount in token1 due to the pool by the end of the flash
/// @param data Any data passed through by the caller via the IPancakeV3PoolActions#flash call
function pancakeV3FlashCallback(
uint256 fee0,
uint256 fee1,
bytes calldata data
) external;
}
contracts/interfaces/callback/IPancakeV3MintCallback.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Callback for IPancakeV3PoolActions#mint
/// @notice Any contract that calls IPancakeV3PoolActions#mint must implement this interface
interface IPancakeV3MintCallback {
/// @notice Called to `msg.sender` after minting liquidity to a position from IPancakeV3Pool#mint.
/// @dev In the implementation you must pay the pool tokens owed for the minted liquidity.
/// The caller of this method must be checked to be a PancakeV3Pool deployed by the canonical PancakeV3Factory.
/// @param amount0Owed The amount of token0 due to the pool for the minted liquidity
/// @param amount1Owed The amount of token1 due to the pool for the minted liquidity
/// @param data Any data passed through by the caller via the IPancakeV3PoolActions#mint call
function pancakeV3MintCallback(
uint256 amount0Owed,
uint256 amount1Owed,
bytes calldata data
) external;
}
contracts/interfaces/callback/IPancakeV3SwapCallback.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Callback for IPancakeV3PoolActions#swap
/// @notice Any contract that calls IPancakeV3PoolActions#swap must implement this interface
interface IPancakeV3SwapCallback {
/// @notice Called to `msg.sender` after executing a swap via IPancakeV3Pool#swap.
/// @dev In the implementation you must pay the pool tokens owed for the swap.
/// The caller of this method must be checked to be a PancakeV3Pool deployed by the canonical PancakeV3Factory.
/// amount0Delta and amount1Delta can both be 0 if no tokens were swapped.
/// @param amount0Delta The amount of token0 that was sent (negative) or must be received (positive) by the pool by
/// the end of the swap. If positive, the callback must send that amount of token0 to the pool.
/// @param amount1Delta The amount of token1 that was sent (negative) or must be received (positive) by the pool by
/// the end of the swap. If positive, the callback must send that amount of token1 to the pool.
/// @param data Any data passed through by the caller via the IPancakeV3PoolActions#swap call
function pancakeV3SwapCallback(
int256 amount0Delta,
int256 amount1Delta,
bytes calldata data
) external;
}
contracts/interfaces/pool/IPancakeV3PoolActions.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Permissionless pool actions
/// @notice Contains pool methods that can be called by anyone
interface IPancakeV3PoolActions {
/// @notice Sets the initial price for the pool
/// @dev Price is represented as a sqrt(amountToken1/amountToken0) Q64.96 value
/// @param sqrtPriceX96 the initial sqrt price of the pool as a Q64.96
function initialize(uint160 sqrtPriceX96) external;
/// @notice Adds liquidity for the given recipient/tickLower/tickUpper position
/// @dev The caller of this method receives a callback in the form of IPancakeV3MintCallback#pancakeV3MintCallback
/// in which they must pay any token0 or token1 owed for the liquidity. The amount of token0/token1 due depends
/// on tickLower, tickUpper, the amount of liquidity, and the current price.
/// @param recipient The address for which the liquidity will be created
/// @param tickLower The lower tick of the position in which to add liquidity
/// @param tickUpper The upper tick of the position in which to add liquidity
/// @param amount The amount of liquidity to mint
/// @param data Any data that should be passed through to the callback
/// @return amount0 The amount of token0 that was paid to mint the given amount of liquidity. Matches the value in the callback
/// @return amount1 The amount of token1 that was paid to mint the given amount of liquidity. Matches the value in the callback
function mint(
address recipient,
int24 tickLower,
int24 tickUpper,
uint128 amount,
bytes calldata data
) external returns (uint256 amount0, uint256 amount1);
/// @notice Collects tokens owed to a position
/// @dev Does not recompute fees earned, which must be done either via mint or burn of any amount of liquidity.
/// Collect must be called by the position owner. To withdraw only token0 or only token1, amount0Requested or
/// amount1Requested may be set to zero. To withdraw all tokens owed, caller may pass any value greater than the
/// actual tokens owed, e.g. type(uint128).max. Tokens owed may be from accumulated swap fees or burned liquidity.
/// @param recipient The address which should receive the fees collected
/// @param tickLower The lower tick of the position for which to collect fees
/// @param tickUpper The upper tick of the position for which to collect fees
/// @param amount0Requested How much token0 should be withdrawn from the fees owed
/// @param amount1Requested How much token1 should be withdrawn from the fees owed
/// @return amount0 The amount of fees collected in token0
/// @return amount1 The amount of fees collected in token1
function collect(
address recipient,
int24 tickLower,
int24 tickUpper,
uint128 amount0Requested,
uint128 amount1Requested
) external returns (uint128 amount0, uint128 amount1);
/// @notice Burn liquidity from the sender and account tokens owed for the liquidity to the position
/// @dev Can be used to trigger a recalculation of fees owed to a position by calling with an amount of 0
/// @dev Fees must be collected separately via a call to #collect
/// @param tickLower The lower tick of the position for which to burn liquidity
/// @param tickUpper The upper tick of the position for which to burn liquidity
/// @param amount How much liquidity to burn
/// @return amount0 The amount of token0 sent to the recipient
/// @return amount1 The amount of token1 sent to the recipient
function burn(
int24 tickLower,
int24 tickUpper,
uint128 amount
) external returns (uint256 amount0, uint256 amount1);
/// @notice Swap token0 for token1, or token1 for token0
/// @dev The caller of this method receives a callback in the form of IPancakeV3SwapCallback#pancakeV3SwapCallback
/// @param recipient The address to receive the output of the swap
/// @param zeroForOne The direction of the swap, true for token0 to token1, false for token1 to token0
/// @param amountSpecified The amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative)
/// @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this
/// value after the swap. If one for zero, the price cannot be greater than this value after the swap
/// @param data Any data to be passed through to the callback
/// @return amount0 The delta of the balance of token0 of the pool, exact when negative, minimum when positive
/// @return amount1 The delta of the balance of token1 of the pool, exact when negative, minimum when positive
function swap(
address recipient,
bool zeroForOne,
int256 amountSpecified,
uint160 sqrtPriceLimitX96,
bytes calldata data
) external returns (int256 amount0, int256 amount1);
/// @notice Receive token0 and/or token1 and pay it back, plus a fee, in the callback
/// @dev The caller of this method receives a callback in the form of IPancakeV3FlashCallback#pancakeV3FlashCallback
/// @dev Can be used to donate underlying tokens pro-rata to currently in-range liquidity providers by calling
/// with 0 amount{0,1} and sending the donation amount(s) from the callback
/// @param recipient The address which will receive the token0 and token1 amounts
/// @param amount0 The amount of token0 to send
/// @param amount1 The amount of token1 to send
/// @param data Any data to be passed through to the callback
function flash(
address recipient,
uint256 amount0,
uint256 amount1,
bytes calldata data
) external;
/// @notice Increase the maximum number of price and liquidity observations that this pool will store
/// @dev This method is no-op if the pool already has an observationCardinalityNext greater than or equal to
/// the input observationCardinalityNext.
/// @param observationCardinalityNext The desired minimum number of observations for the pool to store
function increaseObservationCardinalityNext(uint16 observationCardinalityNext) external;
}
contracts/interfaces/pool/IPancakeV3PoolDerivedState.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Pool state that is not stored
/// @notice Contains view functions to provide information about the pool that is computed rather than stored on the
/// blockchain. The functions here may have variable gas costs.
interface IPancakeV3PoolDerivedState {
/// @notice Returns the cumulative tick and liquidity as of each timestamp `secondsAgo` from the current block timestamp
/// @dev To get a time weighted average tick or liquidity-in-range, you must call this with two values, one representing
/// the beginning of the period and another for the end of the period. E.g., to get the last hour time-weighted average tick,
/// you must call it with secondsAgos = [3600, 0].
/// @dev The time weighted average tick represents the geometric time weighted average price of the pool, in
/// log base sqrt(1.0001) of token1 / token0. The TickMath library can be used to go from a tick value to a ratio.
/// @param secondsAgos From how long ago each cumulative tick and liquidity value should be returned
/// @return tickCumulatives Cumulative tick values as of each `secondsAgos` from the current block timestamp
/// @return secondsPerLiquidityCumulativeX128s Cumulative seconds per liquidity-in-range value as of each `secondsAgos` from the current block
/// timestamp
function observe(uint32[] calldata secondsAgos)
external
view
returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s);
/// @notice Returns a snapshot of the tick cumulative, seconds per liquidity and seconds inside a tick range
/// @dev Snapshots must only be compared to other snapshots, taken over a period for which a position existed.
/// I.e., snapshots cannot be compared if a position is not held for the entire period between when the first
/// snapshot is taken and the second snapshot is taken.
/// @param tickLower The lower tick of the range
/// @param tickUpper The upper tick of the range
/// @return tickCumulativeInside The snapshot of the tick accumulator for the range
/// @return secondsPerLiquidityInsideX128 The snapshot of seconds per liquidity for the range
/// @return secondsInside The snapshot of seconds per liquidity for the range
function snapshotCumulativesInside(int24 tickLower, int24 tickUpper)
external
view
returns (
int56 tickCumulativeInside,
uint160 secondsPerLiquidityInsideX128,
uint32 secondsInside
);
}
contracts/interfaces/pool/IPancakeV3PoolEvents.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Events emitted by a pool
/// @notice Contains all events emitted by the pool
interface IPancakeV3PoolEvents {
/// @notice Emitted exactly once by a pool when #initialize is first called on the pool
/// @dev Mint/Burn/Swap cannot be emitted by the pool before Initialize
/// @param sqrtPriceX96 The initial sqrt price of the pool, as a Q64.96
/// @param tick The initial tick of the pool, i.e. log base 1.0001 of the starting price of the pool
event Initialize(uint160 sqrtPriceX96, int24 tick);
/// @notice Emitted when liquidity is minted for a given position
/// @param sender The address that minted the liquidity
/// @param owner The owner of the position and recipient of any minted liquidity
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param amount The amount of liquidity minted to the position range
/// @param amount0 How much token0 was required for the minted liquidity
/// @param amount1 How much token1 was required for the minted liquidity
event Mint(
address sender,
address indexed owner,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 amount,
uint256 amount0,
uint256 amount1
);
/// @notice Emitted when fees are collected by the owner of a position
/// @dev Collect events may be emitted with zero amount0 and amount1 when the caller chooses not to collect fees
/// @param owner The owner of the position for which fees are collected
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param amount0 The amount of token0 fees collected
/// @param amount1 The amount of token1 fees collected
event Collect(
address indexed owner,
address recipient,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 amount0,
uint128 amount1
);
/// @notice Emitted when a position's liquidity is removed
/// @dev Does not withdraw any fees earned by the liquidity position, which must be withdrawn via #collect
/// @param owner The owner of the position for which liquidity is removed
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param amount The amount of liquidity to remove
/// @param amount0 The amount of token0 withdrawn
/// @param amount1 The amount of token1 withdrawn
event Burn(
address indexed owner,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 amount,
uint256 amount0,
uint256 amount1
);
/// @notice Emitted by the pool for any swaps between token0 and token1
/// @param sender The address that initiated the swap call, and that received the callback
/// @param recipient The address that received the output of the swap
/// @param amount0 The delta of the token0 balance of the pool
/// @param amount1 The delta of the token1 balance of the pool
/// @param sqrtPriceX96 The sqrt(price) of the pool after the swap, as a Q64.96
/// @param liquidity The liquidity of the pool after the swap
/// @param tick The log base 1.0001 of price of the pool after the swap
/// @param protocolFeesToken0 The protocol fee of token0 in the swap
/// @param protocolFeesToken1 The protocol fee of token1 in the swap
event Swap(
address indexed sender,
address indexed recipient,
int256 amount0,
int256 amount1,
uint160 sqrtPriceX96,
uint128 liquidity,
int24 tick,
uint128 protocolFeesToken0,
uint128 protocolFeesToken1
);
/// @notice Emitted by the pool for any flashes of token0/token1
/// @param sender The address that initiated the swap call, and that received the callback
/// @param recipient The address that received the tokens from flash
/// @param amount0 The amount of token0 that was flashed
/// @param amount1 The amount of token1 that was flashed
/// @param paid0 The amount of token0 paid for the flash, which can exceed the amount0 plus the fee
/// @param paid1 The amount of token1 paid for the flash, which can exceed the amount1 plus the fee
event Flash(
address indexed sender,
address indexed recipient,
uint256 amount0,
uint256 amount1,
uint256 paid0,
uint256 paid1
);
/// @notice Emitted by the pool for increases to the number of observations that can be stored
/// @dev observationCardinalityNext is not the observation cardinality until an observation is written at the index
/// just before a mint/swap/burn.
/// @param observationCardinalityNextOld The previous value of the next observation cardinality
/// @param observationCardinalityNextNew The updated value of the next observation cardinality
event IncreaseObservationCardinalityNext(
uint16 observationCardinalityNextOld,
uint16 observationCardinalityNextNew
);
/// @notice Emitted when the protocol fee is changed by the pool
/// @param feeProtocol0Old The previous value of the token0 protocol fee
/// @param feeProtocol1Old The previous value of the token1 protocol fee
/// @param feeProtocol0New The updated value of the token0 protocol fee
/// @param feeProtocol1New The updated value of the token1 protocol fee
event SetFeeProtocol(
uint32 feeProtocol0Old,
uint32 feeProtocol1Old,
uint32 feeProtocol0New,
uint32 feeProtocol1New
);
/// @notice Emitted when the collected protocol fees are withdrawn by the factory owner
/// @param sender The address that collects the protocol fees
/// @param recipient The address that receives the collected protocol fees
/// @param amount0 The amount of token0 protocol fees that is withdrawn
/// @param amount0 The amount of token1 protocol fees that is withdrawn
event CollectProtocol(address indexed sender, address indexed recipient, uint128 amount0, uint128 amount1);
}
contracts/interfaces/pool/IPancakeV3PoolImmutables.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Pool state that never changes
/// @notice These parameters are fixed for a pool forever, i.e., the methods will always return the same values
interface IPancakeV3PoolImmutables {
/// @notice The contract that deployed the pool, which must adhere to the IPancakeV3Factory interface
/// @return The contract address
function factory() external view returns (address);
/// @notice The first of the two tokens of the pool, sorted by address
/// @return The token contract address
function token0() external view returns (address);
/// @notice The second of the two tokens of the pool, sorted by address
/// @return The token contract address
function token1() external view returns (address);
/// @notice The pool's fee in hundredths of a bip, i.e. 1e-6
/// @return The fee
function fee() external view returns (uint24);
/// @notice The pool tick spacing
/// @dev Ticks can only be used at multiples of this value, minimum of 1 and always positive
/// e.g.: a tickSpacing of 3 means ticks can be initialized every 3rd tick, i.e., ..., -6, -3, 0, 3, 6, ...
/// This value is an int24 to avoid casting even though it is always positive.
/// @return The tick spacing
function tickSpacing() external view returns (int24);
/// @notice The maximum amount of position liquidity that can use any tick in the range
/// @dev This parameter is enforced per tick to prevent liquidity from overflowing a uint128 at any point, and
/// also prevents out-of-range liquidity from being used to prevent adding in-range liquidity to a pool
/// @return The max amount of liquidity per tick
function maxLiquidityPerTick() external view returns (uint128);
}
contracts/interfaces/pool/IPancakeV3PoolOwnerActions.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Permissioned pool actions
/// @notice Contains pool methods that may only be called by the factory owner
interface IPancakeV3PoolOwnerActions {
/// @notice Set the denominator of the protocol's % share of the fees
/// @param feeProtocol0 new protocol fee for token0 of the pool
/// @param feeProtocol1 new protocol fee for token1 of the pool
function setFeeProtocol(uint32 feeProtocol0, uint32 feeProtocol1) external;
/// @notice Collect the protocol fee accrued to the pool
/// @param recipient The address to which collected protocol fees should be sent
/// @param amount0Requested The maximum amount of token0 to send, can be 0 to collect fees in only token1
/// @param amount1Requested The maximum amount of token1 to send, can be 0 to collect fees in only token0
/// @return amount0 The protocol fee collected in token0
/// @return amount1 The protocol fee collected in token1
function collectProtocol(
address recipient,
uint128 amount0Requested,
uint128 amount1Requested
) external returns (uint128 amount0, uint128 amount1);
/// @notice Set the LM pool to enable liquidity mining
function setLmPool(address lmPool) external;
}
contracts/interfaces/pool/IPancakeV3PoolState.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Pool state that can change
/// @notice These methods compose the pool's state, and can change with any frequency including multiple times
/// per transaction
interface IPancakeV3PoolState {
/// @notice The 0th storage slot in the pool stores many values, and is exposed as a single method to save gas
/// when accessed externally.
/// @return sqrtPriceX96 The current price of the pool as a sqrt(token1/token0) Q64.96 value
/// tick The current tick of the pool, i.e. according to the last tick transition that was run.
/// This value may not always be equal to SqrtTickMath.getTickAtSqrtRatio(sqrtPriceX96) if the price is on a tick
/// boundary.
/// observationIndex The index of the last oracle observation that was written,
/// observationCardinality The current maximum number of observations stored in the pool,
/// observationCardinalityNext The next maximum number of observations, to be updated when the observation.
/// feeProtocol The protocol fee for both tokens of the pool.
/// Encoded as two 4 bit values, where the protocol fee of token1 is shifted 4 bits and the protocol fee of token0
/// is the lower 4 bits. Used as the denominator of a fraction of the swap fee, e.g. 4 means 1/4th of the swap fee.
/// unlocked Whether the pool is currently locked to reentrancy
function slot0()
external
view
returns (
uint160 sqrtPriceX96,
int24 tick,
uint16 observationIndex,
uint16 observationCardinality,
uint16 observationCardinalityNext,
uint32 feeProtocol,
bool unlocked
);
/// @notice The fee growth as a Q128.128 fees of token0 collected per unit of liquidity for the entire life of the pool
/// @dev This value can overflow the uint256
function feeGrowthGlobal0X128() external view returns (uint256);
/// @notice The fee growth as a Q128.128 fees of token1 collected per unit of liquidity for the entire life of the pool
/// @dev This value can overflow the uint256
function feeGrowthGlobal1X128() external view returns (uint256);
/// @notice The amounts of token0 and token1 that are owed to the protocol
/// @dev Protocol fees will never exceed uint128 max in either token
function protocolFees() external view returns (uint128 token0, uint128 token1);
/// @notice The currently in range liquidity available to the pool
/// @dev This value has no relationship to the total liquidity across all ticks
function liquidity() external view returns (uint128);
/// @notice Look up information about a specific tick in the pool
/// @param tick The tick to look up
/// @return liquidityGross the total amount of position liquidity that uses the pool either as tick lower or
/// tick upper,
/// liquidityNet how much liquidity changes when the pool price crosses the tick,
/// feeGrowthOutside0X128 the fee growth on the other side of the tick from the current tick in token0,
/// feeGrowthOutside1X128 the fee growth on the other side of the tick from the current tick in token1,
/// tickCumulativeOutside the cumulative tick value on the other side of the tick from the current tick
/// secondsPerLiquidityOutsideX128 the seconds spent per liquidity on the other side of the tick from the current tick,
/// secondsOutside the seconds spent on the other side of the tick from the current tick,
/// initialized Set to true if the tick is initialized, i.e. liquidityGross is greater than 0, otherwise equal to false.
/// Outside values can only be used if the tick is initialized, i.e. if liquidityGross is greater than 0.
/// In addition, these values are only relative and must be used only in comparison to previous snapshots for
/// a specific position.
function ticks(int24 tick)
external
view
returns (
uint128 liquidityGross,
int128 liquidityNet,
uint256 feeGrowthOutside0X128,
uint256 feeGrowthOutside1X128,
int56 tickCumulativeOutside,
uint160 secondsPerLiquidityOutsideX128,
uint32 secondsOutside,
bool initialized
);
/// @notice Returns 256 packed tick initialized boolean values. See TickBitmap for more information
function tickBitmap(int16 wordPosition) external view returns (uint256);
/// @notice Returns the information about a position by the position's key
/// @param key The position's key is a hash of a preimage composed by the owner, tickLower and tickUpper
/// @return _liquidity The amount of liquidity in the position,
/// Returns feeGrowthInside0LastX128 fee growth of token0 inside the tick range as of the last mint/burn/poke,
/// Returns feeGrowthInside1LastX128 fee growth of token1 inside the tick range as of the last mint/burn/poke,
/// Returns tokensOwed0 the computed amount of token0 owed to the position as of the last mint/burn/poke,
/// Returns tokensOwed1 the computed amount of token1 owed to the position as of the last mint/burn/poke
function positions(bytes32 key)
external
view
returns (
uint128 _liquidity,
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128,
uint128 tokensOwed0,
uint128 tokensOwed1
);
/// @notice Returns data about a specific observation index
/// @param index The element of the observations array to fetch
/// @dev You most likely want to use #observe() instead of this method to get an observation as of some amount of time
/// ago, rather than at a specific index in the array.
/// @return blockTimestamp The timestamp of the observation,
/// Returns tickCumulative the tick multiplied by seconds elapsed for the life of the pool as of the observation timestamp,
/// Returns secondsPerLiquidityCumulativeX128 the seconds per in range liquidity for the life of the pool as of the observation timestamp,
/// Returns initialized whether the observation has been initialized and the values are safe to use
function observations(uint256 index)
external
view
returns (
uint32 blockTimestamp,
int56 tickCumulative,
uint160 secondsPerLiquidityCumulativeX128,
bool initialized
);
}
contracts/libraries/BitMath.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title BitMath
/// @dev This library provides functionality for computing bit properties of an unsigned integer
library BitMath {
/// @notice Returns the index of the most significant bit of the number,
/// where the least significant bit is at index 0 and the most significant bit is at index 255
/// @dev The function satisfies the property:
/// x >= 2**mostSignificantBit(x) and x < 2**(mostSignificantBit(x)+1)
/// @param x the value for which to compute the most significant bit, must be greater than 0
/// @return r the index of the most significant bit
function mostSignificantBit(uint256 x) internal pure returns (uint8 r) {
require(x > 0);
if (x >= 0x100000000000000000000000000000000) {
x >>= 128;
r += 128;
}
if (x >= 0x10000000000000000) {
x >>= 64;
r += 64;
}
if (x >= 0x100000000) {
x >>= 32;
r += 32;
}
if (x >= 0x10000) {
x >>= 16;
r += 16;
}
if (x >= 0x100) {
x >>= 8;
r += 8;
}
if (x >= 0x10) {
x >>= 4;
r += 4;
}
if (x >= 0x4) {
x >>= 2;
r += 2;
}
if (x >= 0x2) r += 1;
}
/// @notice Returns the index of the least significant bit of the number,
/// where the least significant bit is at index 0 and the most significant bit is at index 255
/// @dev The function satisfies the property:
/// (x & 2**leastSignificantBit(x)) != 0 and (x & (2**(leastSignificantBit(x)) - 1)) == 0)
/// @param x the value for which to compute the least significant bit, must be greater than 0
/// @return r the index of the least significant bit
function leastSignificantBit(uint256 x) internal pure returns (uint8 r) {
require(x > 0);
r = 255;
if (x & type(uint128).max > 0) {
r -= 128;
} else {
x >>= 128;
}
if (x & type(uint64).max > 0) {
r -= 64;
} else {
x >>= 64;
}
if (x & type(uint32).max > 0) {
r -= 32;
} else {
x >>= 32;
}
if (x & type(uint16).max > 0) {
r -= 16;
} else {
x >>= 16;
}
if (x & type(uint8).max > 0) {
r -= 8;
} else {
x >>= 8;
}
if (x & 0xf > 0) {
r -= 4;
} else {
x >>= 4;
}
if (x & 0x3 > 0) {
r -= 2;
} else {
x >>= 2;
}
if (x & 0x1 > 0) r -= 1;
}
}
contracts/libraries/FixedPoint128.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.4.0;
/// @title FixedPoint128
/// @notice A library for handling binary fixed point numbers, see https://en.wikipedia.org/wiki/Q_(number_format)
library FixedPoint128 {
uint256 internal constant Q128 = 0x100000000000000000000000000000000;
}
contracts/libraries/FixedPoint96.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.4.0;
/// @title FixedPoint96
/// @notice A library for handling binary fixed point numbers, see https://en.wikipedia.org/wiki/Q_(number_format)
/// @dev Used in SqrtPriceMath.sol
library FixedPoint96 {
uint8 internal constant RESOLUTION = 96;
uint256 internal constant Q96 = 0x1000000000000000000000000;
}
contracts/libraries/FullMath.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.4.0 <0.8.0;
/// @title Contains 512-bit math functions
/// @notice Facilitates multiplication and division that can have overflow of an intermediate value without any loss of precision
/// @dev Handles "phantom overflow" i.e., allows multiplication and division where an intermediate value overflows 256 bits
library FullMath {
/// @notice Calculates floor(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
/// @param a The multiplicand
/// @param b The multiplier
/// @param denominator The divisor
/// @return result The 256-bit result
/// @dev Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv
function mulDiv(
uint256 a,
uint256 b,
uint256 denominator
) internal pure returns (uint256 result) {
// 512-bit multiply [prod1 prod0] = a * b
// Compute the product mod 2**256 and mod 2**256 - 1
// then use the Chinese Remainder Theorem to reconstruct
// the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2**256 + prod0
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(a, b, not(0))
prod0 := mul(a, b)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division
if (prod1 == 0) {
require(denominator > 0);
assembly {
result := div(prod0, denominator)
}
return result;
}
// Make sure the result is less than 2**256.
// Also prevents denominator == 0
require(denominator > prod1);
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0]
// Compute remainder using mulmod
uint256 remainder;
assembly {
remainder := mulmod(a, b, denominator)
}
// Subtract 256 bit number from 512 bit number
assembly {
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator
// Compute largest power of two divisor of denominator.
// Always >= 1.
uint256 twos = -denominator & denominator;
// Divide denominator by power of two
assembly {
denominator := div(denominator, twos)
}
// Divide [prod1 prod0] by the factors of two
assembly {
prod0 := div(prod0, twos)
}
// Shift in bits from prod1 into prod0. For this we need
// to flip `twos` such that it is 2**256 / twos.
// If twos is zero, then it becomes one
assembly {
twos := add(div(sub(0, twos), twos), 1)
}
prod0 |= prod1 * twos;
// Invert denominator mod 2**256
// Now that denominator is an odd number, it has an inverse
// modulo 2**256 such that denominator * inv = 1 mod 2**256.
// Compute the inverse by starting with a seed that is correct
// correct for four bits. That is, denominator * inv = 1 mod 2**4
uint256 inv = (3 * denominator) ^ 2;
// Now use Newton-Raphson iteration to improve the precision.
// Thanks to Hensel's lifting lemma, this also works in modular
// arithmetic, doubling the correct bits in each step.
inv *= 2 - denominator * inv; // inverse mod 2**8
inv *= 2 - denominator * inv; // inverse mod 2**16
inv *= 2 - denominator * inv; // inverse mod 2**32
inv *= 2 - denominator * inv; // inverse mod 2**64
inv *= 2 - denominator * inv; // inverse mod 2**128
inv *= 2 - denominator * inv; // inverse mod 2**256
// Because the division is now exact we can divide by multiplying
// with the modular inverse of denominator. This will give us the
// correct result modulo 2**256. Since the precoditions guarantee
// that the outcome is less than 2**256, this is the final result.
// We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inv;
return result;
}
/// @notice Calculates ceil(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
/// @param a The multiplicand
/// @param b The multiplier
/// @param denominator The divisor
/// @return result The 256-bit result
function mulDivRoundingUp(
uint256 a,
uint256 b,
uint256 denominator
) internal pure returns (uint256 result) {
result = mulDiv(a, b, denominator);
if (mulmod(a, b, denominator) > 0) {
require(result < type(uint256).max);
result++;
}
}
}
contracts/libraries/LiquidityMath.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Math library for liquidity
library LiquidityMath {
/// @notice Add a signed liquidity delta to liquidity and revert if it overflows or underflows
/// @param x The liquidity before change
/// @param y The delta by which liquidity should be changed
/// @return z The liquidity delta
function addDelta(uint128 x, int128 y) internal pure returns (uint128 z) {
if (y < 0) {
require((z = x - uint128(-y)) < x, 'LS');
} else {
require((z = x + uint128(y)) >= x, 'LA');
}
}
}
contracts/libraries/LowGasSafeMath.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.7.0;
/// @title Optimized overflow and underflow safe math operations
/// @notice Contains methods for doing math operations that revert on overflow or underflow for minimal gas cost
library LowGasSafeMath {
/// @notice Returns x + y, reverts if sum overflows uint256
/// @param x The augend
/// @param y The addend
/// @return z The sum of x and y
function add(uint256 x, uint256 y) internal pure returns (uint256 z) {
require((z = x + y) >= x);
}
/// @notice Returns x - y, reverts if underflows
/// @param x The minuend
/// @param y The subtrahend
/// @return z The difference of x and y
function sub(uint256 x, uint256 y) internal pure returns (uint256 z) {
require((z = x - y) <= x);
}
/// @notice Returns x * y, reverts if overflows
/// @param x The multiplicand
/// @param y The multiplier
/// @return z The product of x and y
function mul(uint256 x, uint256 y) internal pure returns (uint256 z) {
require(x == 0 || (z = x * y) / x == y);
}
/// @notice Returns x + y, reverts if overflows or underflows
/// @param x The augend
/// @param y The addend
/// @return z The sum of x and y
function add(int256 x, int256 y) internal pure returns (int256 z) {
require((z = x + y) >= x == (y >= 0));
}
/// @notice Returns x - y, reverts if overflows or underflows
/// @param x The minuend
/// @param y The subtrahend
/// @return z The difference of x and y
function sub(int256 x, int256 y) internal pure returns (int256 z) {
require((z = x - y) <= x == (y >= 0));
}
}
contracts/libraries/Oracle.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0 <0.8.0;
/// @title Oracle
/// @notice Provides price and liquidity data useful for a wide variety of system designs
/// @dev Instances of stored oracle data, "observations", are collected in the oracle array
/// Every pool is initialized with an oracle array length of 1. Anyone can pay the SSTOREs to increase the
/// maximum length of the oracle array. New slots will be added when the array is fully populated.
/// Observations are overwritten when the full length of the oracle array is populated.
/// The most recent observation is available, independent of the length of the oracle array, by passing 0 to observe()
library Oracle {
struct Observation {
// the block timestamp of the observation
uint32 blockTimestamp;
// the tick accumulator, i.e. tick * time elapsed since the pool was first initialized
int56 tickCumulative;
// the seconds per liquidity, i.e. seconds elapsed / max(1, liquidity) since the pool was first initialized
uint160 secondsPerLiquidityCumulativeX128;
// whether or not the observation is initialized
bool initialized;
}
/// @notice Transforms a previous observation into a new observation, given the passage of time and the current tick and liquidity values
/// @dev blockTimestamp _must_ be chronologically equal to or greater than last.blockTimestamp, safe for 0 or 1 overflows
/// @param last The specified observation to be transformed
/// @param blockTimestamp The timestamp of the new observation
/// @param tick The active tick at the time of the new observation
/// @param liquidity The total in-range liquidity at the time of the new observation
/// @return Observation The newly populated observation
function transform(
Observation memory last,
uint32 blockTimestamp,
int24 tick,
uint128 liquidity
) private pure returns (Observation memory) {
uint32 delta = blockTimestamp - last.blockTimestamp;
return
Observation({
blockTimestamp: blockTimestamp,
tickCumulative: last.tickCumulative + int56(tick) * delta,
secondsPerLiquidityCumulativeX128: last.secondsPerLiquidityCumulativeX128 +
((uint160(delta) << 128) / (liquidity > 0 ? liquidity : 1)),
initialized: true
});
}
/// @notice Initialize the oracle array by writing the first slot. Called once for the lifecycle of the observations array
/// @param self The stored oracle array
/// @param time The time of the oracle initialization, via block.timestamp truncated to uint32
/// @return cardinality The number of populated elements in the oracle array
/// @return cardinalityNext The new length of the oracle array, independent of population
function initialize(Observation[65535] storage self, uint32 time)
internal
returns (uint16 cardinality, uint16 cardinalityNext)
{
self[0] = Observation({
blockTimestamp: time,
tickCumulative: 0,
secondsPerLiquidityCumulativeX128: 0,
initialized: true
});
return (1, 1);
}
/// @notice Writes an oracle observation to the array
/// @dev Writable at most once per block. Index represents the most recently written element. cardinality and index must be tracked externally.
/// If the index is at the end of the allowable array length (according to cardinality), and the next cardinality
/// is greater than the current one, cardinality may be increased. This restriction is created to preserve ordering.
/// @param self The stored oracle array
/// @param index The index of the observation that was most recently written to the observations array
/// @param blockTimestamp The timestamp of the new observation
/// @param tick The active tick at the time of the new observation
/// @param liquidity The total in-range liquidity at the time of the new observation
/// @param cardinality The number of populated elements in the oracle array
/// @param cardinalityNext The new length of the oracle array, independent of population
/// @return indexUpdated The new index of the most recently written element in the oracle array
/// @return cardinalityUpdated The new cardinality of the oracle array
function write(
Observation[65535] storage self,
uint16 index,
uint32 blockTimestamp,
int24 tick,
uint128 liquidity,
uint16 cardinality,
uint16 cardinalityNext
) internal returns (uint16 indexUpdated, uint16 cardinalityUpdated) {
Observation memory last = self[index];
// early return if we've already written an observation this block
if (last.blockTimestamp == blockTimestamp) return (index, cardinality);
// if the conditions are right, we can bump the cardinality
if (cardinalityNext > cardinality && index == (cardinality - 1)) {
cardinalityUpdated = cardinalityNext;
} else {
cardinalityUpdated = cardinality;
}
indexUpdated = (index + 1) % cardinalityUpdated;
self[indexUpdated] = transform(last, blockTimestamp, tick, liquidity);
}
/// @notice Prepares the oracle array to store up to `next` observations
/// @param self The stored oracle array
/// @param current The current next cardinality of the oracle array
/// @param next The proposed next cardinality which will be populated in the oracle array
/// @return next The next cardinality which will be populated in the oracle array
function grow(
Observation[65535] storage self,
uint16 current,
uint16 next
) internal returns (uint16) {
require(current > 0, 'I');
// no-op if the passed next value isn't greater than the current next value
if (next <= current) return current;
// store in each slot to prevent fresh SSTOREs in swaps
// this data will not be used because the initialized boolean is still false
for (uint16 i = current; i < next; i++) self[i].blockTimestamp = 1;
return next;
}
/// @notice comparator for 32-bit timestamps
/// @dev safe for 0 or 1 overflows, a and b _must_ be chronologically before or equal to time
/// @param time A timestamp truncated to 32 bits
/// @param a A comparison timestamp from which to determine the relative position of `time`
/// @param b From which to determine the relative position of `time`
/// @return bool Whether `a` is chronologically <= `b`
function lte(
uint32 time,
uint32 a,
uint32 b
) private pure returns (bool) {
// if there hasn't been overflow, no need to adjust
if (a <= time && b <= time) return a <= b;
uint256 aAdjusted = a > time ? a : a + 2**32;
uint256 bAdjusted = b > time ? b : b + 2**32;
return aAdjusted <= bAdjusted;
}
/// @notice Fetches the observations beforeOrAt and atOrAfter a target, i.e. where [beforeOrAt, atOrAfter] is satisfied.
/// The result may be the same observation, or adjacent observations.
/// @dev The answer must be contained in the array, used when the target is located within the stored observation
/// boundaries: older than the most recent observation and younger, or the same age as, the oldest observation
/// @param self The stored oracle array
/// @param time The current block.timestamp
/// @param target The timestamp at which the reserved observation should be for
/// @param index The index of the observation that was most recently written to the observations array
/// @param cardinality The number of populated elements in the oracle array
/// @return beforeOrAt The observation recorded before, or at, the target
/// @return atOrAfter The observation recorded at, or after, the target
function binarySearch(
Observation[65535] storage self,
uint32 time,
uint32 target,
uint16 index,
uint16 cardinality
) private view returns (Observation memory beforeOrAt, Observation memory atOrAfter) {
uint256 l = (index + 1) % cardinality; // oldest observation
uint256 r = l + cardinality - 1; // newest observation
uint256 i;
while (true) {
i = (l + r) / 2;
beforeOrAt = self[i % cardinality];
// we've landed on an uninitialized tick, keep searching higher (more recently)
if (!beforeOrAt.initialized) {
l = i + 1;
continue;
}
atOrAfter = self[(i + 1) % cardinality];
bool targetAtOrAfter = lte(time, beforeOrAt.blockTimestamp, target);
// check if we've found the answer!
if (targetAtOrAfter && lte(time, target, atOrAfter.blockTimestamp)) break;
if (!targetAtOrAfter) r = i - 1;
else l = i + 1;
}
}
/// @notice Fetches the observations beforeOrAt and atOrAfter a given target, i.e. where [beforeOrAt, atOrAfter] is satisfied
/// @dev Assumes there is at least 1 initialized observation.
/// Used by observeSingle() to compute the counterfactual accumulator values as of a given block timestamp.
/// @param self The stored oracle array
/// @param time The current block.timestamp
/// @param target The timestamp at which the reserved observation should be for
/// @param tick The active tick at the time of the returned or simulated observation
/// @param index The index of the observation that was most recently written to the observations array
/// @param liquidity The total pool liquidity at the time of the call
/// @param cardinality The number of populated elements in the oracle array
/// @return beforeOrAt The observation which occurred at, or before, the given timestamp
/// @return atOrAfter The observation which occurred at, or after, the given timestamp
function getSurroundingObservations(
Observation[65535] storage self,
uint32 time,
uint32 target,
int24 tick,
uint16 index,
uint128 liquidity,
uint16 cardinality
) private view returns (Observation memory beforeOrAt, Observation memory atOrAfter) {
// optimistically set before to the newest observation
beforeOrAt = self[index];
// if the target is chronologically at or after the newest observation, we can early return
if (lte(time, beforeOrAt.blockTimestamp, target)) {
if (beforeOrAt.blockTimestamp == target) {
// if newest observation equals target, we're in the same block, so we can ignore atOrAfter
return (beforeOrAt, atOrAfter);
} else {
// otherwise, we need to transform
return (beforeOrAt, transform(beforeOrAt, target, tick, liquidity));
}
}
// now, set before to the oldest observation
beforeOrAt = self[(index + 1) % cardinality];
if (!beforeOrAt.initialized) beforeOrAt = self[0];
// ensure that the target is chronologically at or after the oldest observation
require(lte(time, beforeOrAt.blockTimestamp, target), 'OLD');
// if we've reached this point, we have to binary search
return binarySearch(self, time, target, index, cardinality);
}
/// @dev Reverts if an observation at or before the desired observation timestamp does not exist.
/// 0 may be passed as `secondsAgo' to return the current cumulative values.
/// If called with a timestamp falling between two observations, returns the counterfactual accumulator values
/// at exactly the timestamp between the two observations.
/// @param self The stored oracle array
/// @param time The current block timestamp
/// @param secondsAgo The amount of time to look back, in seconds, at which point to return an observation
/// @param tick The current tick
/// @param index The index of the observation that was most recently written to the observations array
/// @param liquidity The current in-range pool liquidity
/// @param cardinality The number of populated elements in the oracle array
/// @return tickCumulative The tick * time elapsed since the pool was first initialized, as of `secondsAgo`
/// @return secondsPerLiquidityCumulativeX128 The time elapsed / max(1, liquidity) since the pool was first initialized, as of `secondsAgo`
function observeSingle(
Observation[65535] storage self,
uint32 time,
uint32 secondsAgo,
int24 tick,
uint16 index,
uint128 liquidity,
uint16 cardinality
) internal view returns (int56 tickCumulative, uint160 secondsPerLiquidityCumulativeX128) {
if (secondsAgo == 0) {
Observation memory last = self[index];
if (last.blockTimestamp != time) last = transform(last, time, tick, liquidity);
return (last.tickCumulative, last.secondsPerLiquidityCumulativeX128);
}
uint32 target = time - secondsAgo;
(Observation memory beforeOrAt, Observation memory atOrAfter) = getSurroundingObservations(
self,
time,
target,
tick,
index,
liquidity,
cardinality
);
if (target == beforeOrAt.blockTimestamp) {
// we're at the left boundary
return (beforeOrAt.tickCumulative, beforeOrAt.secondsPerLiquidityCumulativeX128);
} else if (target == atOrAfter.blockTimestamp) {
// we're at the right boundary
return (atOrAfter.tickCumulative, atOrAfter.secondsPerLiquidityCumulativeX128);
} else {
// we're in the middle
uint32 observationTimeDelta = atOrAfter.blockTimestamp - beforeOrAt.blockTimestamp;
uint32 targetDelta = target - beforeOrAt.blockTimestamp;
return (
beforeOrAt.tickCumulative +
((atOrAfter.tickCumulative - beforeOrAt.tickCumulative) / observationTimeDelta) *
targetDelta,
beforeOrAt.secondsPerLiquidityCumulativeX128 +
uint160(
(uint256(
atOrAfter.secondsPerLiquidityCumulativeX128 - beforeOrAt.secondsPerLiquidityCumulativeX128
) * targetDelta) / observationTimeDelta
)
);
}
}
/// @notice Returns the accumulator values as of each time seconds ago from the given time in the array of `secondsAgos`
/// @dev Reverts if `secondsAgos` > oldest observation
/// @param self The stored oracle array
/// @param time The current block.timestamp
/// @param secondsAgos Each amount of time to look back, in seconds, at which point to return an observation
/// @param tick The current tick
/// @param index The index of the observation that was most recently written to the observations array
/// @param liquidity The current in-range pool liquidity
/// @param cardinality The number of populated elements in the oracle array
/// @return tickCumulatives The tick * time elapsed since the pool was first initialized, as of each `secondsAgo`
/// @return secondsPerLiquidityCumulativeX128s The cumulative seconds / max(1, liquidity) since the pool was first initialized, as of each `secondsAgo`
function observe(
Observation[65535] storage self,
uint32 time,
uint32[] memory secondsAgos,
int24 tick,
uint16 index,
uint128 liquidity,
uint16 cardinality
) internal view returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s) {
require(cardinality > 0, 'I');
tickCumulatives = new int56[](secondsAgos.length);
secondsPerLiquidityCumulativeX128s = new uint160[](secondsAgos.length);
for (uint256 i = 0; i < secondsAgos.length; i++) {
(tickCumulatives[i], secondsPerLiquidityCumulativeX128s[i]) = observeSingle(
self,
time,
secondsAgos[i],
tick,
index,
liquidity,
cardinality
);
}
}
}
contracts/libraries/Position.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0 <0.8.0;
import './FullMath.sol';
import './FixedPoint128.sol';
import './LiquidityMath.sol';
/// @title Position
/// @notice Positions represent an owner address' liquidity between a lower and upper tick boundary
/// @dev Positions store additional state for tracking fees owed to the position
library Position {
// info stored for each user's position
struct Info {
// the amount of liquidity owned by this position
uint128 liquidity;
// fee growth per unit of liquidity as of the last update to liquidity or fees owed
uint256 feeGrowthInside0LastX128;
uint256 feeGrowthInside1LastX128;
// the fees owed to the position owner in token0/token1
uint128 tokensOwed0;
uint128 tokensOwed1;
}
/// @notice Returns the Info struct of a position, given an owner and position boundaries
/// @param self The mapping containing all user positions
/// @param owner The address of the position owner
/// @param tickLower The lower tick boundary of the position
/// @param tickUpper The upper tick boundary of the position
/// @return position The position info struct of the given owners' position
function get(
mapping(bytes32 => Info) storage self,
address owner,
int24 tickLower,
int24 tickUpper
) internal view returns (Position.Info storage position) {
position = self[keccak256(abi.encodePacked(owner, tickLower, tickUpper))];
}
/// @notice Credits accumulated fees to a user's position
/// @param self The individual position to update
/// @param liquidityDelta The change in pool liquidity as a result of the position update
/// @param feeGrowthInside0X128 The all-time fee growth in token0, per unit of liquidity, inside the position's tick boundaries
/// @param feeGrowthInside1X128 The all-time fee growth in token1, per unit of liquidity, inside the position's tick boundaries
function update(
Info storage self,
int128 liquidityDelta,
uint256 feeGrowthInside0X128,
uint256 feeGrowthInside1X128
) internal {
Info memory _self = self;
uint128 liquidityNext;
if (liquidityDelta == 0) {
require(_self.liquidity > 0, 'NP'); // disallow pokes for 0 liquidity positions
liquidityNext = _self.liquidity;
} else {
liquidityNext = LiquidityMath.addDelta(_self.liquidity, liquidityDelta);
}
// calculate accumulated fees
uint128 tokensOwed0 = uint128(
FullMath.mulDiv(feeGrowthInside0X128 - _self.feeGrowthInside0LastX128, _self.liquidity, FixedPoint128.Q128)
);
uint128 tokensOwed1 = uint128(
FullMath.mulDiv(feeGrowthInside1X128 - _self.feeGrowthInside1LastX128, _self.liquidity, FixedPoint128.Q128)
);
// update the position
if (liquidityDelta != 0) self.liquidity = liquidityNext;
self.feeGrowthInside0LastX128 = feeGrowthInside0X128;
self.feeGrowthInside1LastX128 = feeGrowthInside1X128;
if (tokensOwed0 > 0 || tokensOwed1 > 0) {
// overflow is acceptable, have to withdraw before you hit type(uint128).max fees
self.tokensOwed0 += tokensOwed0;
self.tokensOwed1 += tokensOwed1;
}
}
}
contracts/libraries/SafeCast.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Safe casting methods
/// @notice Contains methods for safely casting between types
library SafeCast {
/// @notice Cast a uint256 to a uint160, revert on overflow
/// @param y The uint256 to be downcasted
/// @return z The downcasted integer, now type uint160
function toUint160(uint256 y) internal pure returns (uint160 z) {
require((z = uint160(y)) == y);
}
/// @notice Cast a int256 to a int128, revert on overflow or underflow
/// @param y The int256 to be downcasted
/// @return z The downcasted integer, now type int128
function toInt128(int256 y) internal pure returns (int128 z) {
require((z = int128(y)) == y);
}
/// @notice Cast a uint256 to a int256, revert on overflow
/// @param y The uint256 to be casted
/// @return z The casted integer, now type int256
function toInt256(uint256 y) internal pure returns (int256 z) {
require(y < 2**255);
z = int256(y);
}
}
contracts/libraries/SqrtPriceMath.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import './LowGasSafeMath.sol';
import './SafeCast.sol';
import './FullMath.sol';
import './UnsafeMath.sol';
import './FixedPoint96.sol';
/// @title Functions based on Q64.96 sqrt price and liquidity
/// @notice Contains the math that uses square root of price as a Q64.96 and liquidity to compute deltas
library SqrtPriceMath {
using LowGasSafeMath for uint256;
using SafeCast for uint256;
/// @notice Gets the next sqrt price given a delta of token0
/// @dev Always rounds up, because in the exact output case (increasing price) we need to move the price at least
/// far enough to get the desired output amount, and in the exact input case (decreasing price) we need to move the
/// price less in order to not send too much output.
/// The most precise formula for this is liquidity * sqrtPX96 / (liquidity +- amount * sqrtPX96),
/// if this is impossible because of overflow, we calculate liquidity / (liquidity / sqrtPX96 +- amount).
/// @param sqrtPX96 The starting price, i.e. before accounting for the token0 delta
/// @param liquidity The amount of usable liquidity
/// @param amount How much of token0 to add or remove from virtual reserves
/// @param add Whether to add or remove the amount of token0
/// @return The price after adding or removing amount, depending on add
function getNextSqrtPriceFromAmount0RoundingUp(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amount,
bool add
) internal pure returns (uint160) {
// we short circuit amount == 0 because the result is otherwise not guaranteed to equal the input price
if (amount == 0) return sqrtPX96;
uint256 numerator1 = uint256(liquidity) << FixedPoint96.RESOLUTION;
if (add) {
uint256 product;
if ((product = amount * sqrtPX96) / amount == sqrtPX96) {
uint256 denominator = numerator1 + product;
if (denominator >= numerator1)
// always fits in 160 bits
return uint160(FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator));
}
return uint160(UnsafeMath.divRoundingUp(numerator1, (numerator1 / sqrtPX96).add(amount)));
} else {
uint256 product;
// if the product overflows, we know the denominator underflows
// in addition, we must check that the denominator does not underflow
require((product = amount * sqrtPX96) / amount == sqrtPX96 && numerator1 > product);
uint256 denominator = numerator1 - product;
return FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator).toUint160();
}
}
/// @notice Gets the next sqrt price given a delta of token1
/// @dev Always rounds down, because in the exact output case (decreasing price) we need to move the price at least
/// far enough to get the desired output amount, and in the exact input case (increasing price) we need to move the
/// price less in order to not send too much output.
/// The formula we compute is within <1 wei of the lossless version: sqrtPX96 +- amount / liquidity
/// @param sqrtPX96 The starting price, i.e., before accounting for the token1 delta
/// @param liquidity The amount of usable liquidity
/// @param amount How much of token1 to add, or remove, from virtual reserves
/// @param add Whether to add, or remove, the amount of token1
/// @return The price after adding or removing `amount`
function getNextSqrtPriceFromAmount1RoundingDown(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amount,
bool add
) internal pure returns (uint160) {
// if we're adding (subtracting), rounding down requires rounding the quotient down (up)
// in both cases, avoid a mulDiv for most inputs
if (add) {
uint256 quotient = (
amount <= type(uint160).max
? (amount << FixedPoint96.RESOLUTION) / liquidity
: FullMath.mulDiv(amount, FixedPoint96.Q96, liquidity)
);
return uint256(sqrtPX96).add(quotient).toUint160();
} else {
uint256 quotient = (
amount <= type(uint160).max
? UnsafeMath.divRoundingUp(amount << FixedPoint96.RESOLUTION, liquidity)
: FullMath.mulDivRoundingUp(amount, FixedPoint96.Q96, liquidity)
);
require(sqrtPX96 > quotient);
// always fits 160 bits
return uint160(sqrtPX96 - quotient);
}
}
/// @notice Gets the next sqrt price given an input amount of token0 or token1
/// @dev Throws if price or liquidity are 0, or if the next price is out of bounds
/// @param sqrtPX96 The starting price, i.e., before accounting for the input amount
/// @param liquidity The amount of usable liquidity
/// @param amountIn How much of token0, or token1, is being swapped in
/// @param zeroForOne Whether the amount in is token0 or token1
/// @return sqrtQX96 The price after adding the input amount to token0 or token1
function getNextSqrtPriceFromInput(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amountIn,
bool zeroForOne
) internal pure returns (uint160 sqrtQX96) {
require(sqrtPX96 > 0);
require(liquidity > 0);
// round to make sure that we don't pass the target price
return
zeroForOne
? getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true)
: getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountIn, true);
}
/// @notice Gets the next sqrt price given an output amount of token0 or token1
/// @dev Throws if price or liquidity are 0 or the next price is out of bounds
/// @param sqrtPX96 The starting price before accounting for the output amount
/// @param liquidity The amount of usable liquidity
/// @param amountOut How much of token0, or token1, is being swapped out
/// @param zeroForOne Whether the amount out is token0 or token1
/// @return sqrtQX96 The price after removing the output amount of token0 or token1
function getNextSqrtPriceFromOutput(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amountOut,
bool zeroForOne
) internal pure returns (uint160 sqrtQX96) {
require(sqrtPX96 > 0);
require(liquidity > 0);
// round to make sure that we pass the target price
return
zeroForOne
? getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountOut, false)
: getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountOut, false);
}
/// @notice Gets the amount0 delta between two prices
/// @dev Calculates liquidity / sqrt(lower) - liquidity / sqrt(upper),
/// i.e. liquidity * (sqrt(upper) - sqrt(lower)) / (sqrt(upper) * sqrt(lower))
/// @param sqrtRatioAX96 A sqrt price
/// @param sqrtRatioBX96 Another sqrt price
/// @param liquidity The amount of usable liquidity
/// @param roundUp Whether to round the amount up or down
/// @return amount0 Amount of token0 required to cover a position of size liquidity between the two passed prices
function getAmount0Delta(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint128 liquidity,
bool roundUp
) internal pure returns (uint256 amount0) {
if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
uint256 numerator1 = uint256(liquidity) << FixedPoint96.RESOLUTION;
uint256 numerator2 = sqrtRatioBX96 - sqrtRatioAX96;
require(sqrtRatioAX96 > 0);
return
roundUp
? UnsafeMath.divRoundingUp(
FullMath.mulDivRoundingUp(numerator1, numerator2, sqrtRatioBX96),
sqrtRatioAX96
)
: FullMath.mulDiv(numerator1, numerator2, sqrtRatioBX96) / sqrtRatioAX96;
}
/// @notice Gets the amount1 delta between two prices
/// @dev Calculates liquidity * (sqrt(upper) - sqrt(lower))
/// @param sqrtRatioAX96 A sqrt price
/// @param sqrtRatioBX96 Another sqrt price
/// @param liquidity The amount of usable liquidity
/// @param roundUp Whether to round the amount up, or down
/// @return amount1 Amount of token1 required to cover a position of size liquidity between the two passed prices
function getAmount1Delta(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint128 liquidity,
bool roundUp
) internal pure returns (uint256 amount1) {
if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
return
roundUp
? FullMath.mulDivRoundingUp(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96)
: FullMath.mulDiv(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96);
}
/// @notice Helper that gets signed token0 delta
/// @param sqrtRatioAX96 A sqrt price
/// @param sqrtRatioBX96 Another sqrt price
/// @param liquidity The change in liquidity for which to compute the amount0 delta
/// @return amount0 Amount of token0 corresponding to the passed liquidityDelta between the two prices
function getAmount0Delta(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
int128 liquidity
) internal pure returns (int256 amount0) {
return
liquidity < 0
? -getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(-liquidity), false).toInt256()
: getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(liquidity), true).toInt256();
}
/// @notice Helper that gets signed token1 delta
/// @param sqrtRatioAX96 A sqrt price
/// @param sqrtRatioBX96 Another sqrt price
/// @param liquidity The change in liquidity for which to compute the amount1 delta
/// @return amount1 Amount of token1 corresponding to the passed liquidityDelta between the two prices
function getAmount1Delta(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
int128 liquidity
) internal pure returns (int256 amount1) {
return
liquidity < 0
? -getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(-liquidity), false).toInt256()
: getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(liquidity), true).toInt256();
}
}
contracts/libraries/SwapMath.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import './FullMath.sol';
import './SqrtPriceMath.sol';
/// @title Computes the result of a swap within ticks
/// @notice Contains methods for computing the result of a swap within a single tick price range, i.e., a single tick.
library SwapMath {
/// @notice Computes the result of swapping some amount in, or amount out, given the parameters of the swap
/// @dev The fee, plus the amount in, will never exceed the amount remaining if the swap's `amountSpecified` is positive
/// @param sqrtRatioCurrentX96 The current sqrt price of the pool
/// @param sqrtRatioTargetX96 The price that cannot be exceeded, from which the direction of the swap is inferred
/// @param liquidity The usable liquidity
/// @param amountRemaining How much input or output amount is remaining to be swapped in/out
/// @param feePips The fee taken from the input amount, expressed in hundredths of a bip
/// @return sqrtRatioNextX96 The price after swapping the amount in/out, not to exceed the price target
/// @return amountIn The amount to be swapped in, of either token0 or token1, based on the direction of the swap
/// @return amountOut The amount to be received, of either token0 or token1, based on the direction of the swap
/// @return feeAmount The amount of input that will be taken as a fee
function computeSwapStep(
uint160 sqrtRatioCurrentX96,
uint160 sqrtRatioTargetX96,
uint128 liquidity,
int256 amountRemaining,
uint24 feePips
)
internal
pure
returns (
uint160 sqrtRatioNextX96,
uint256 amountIn,
uint256 amountOut,
uint256 feeAmount
)
{
bool zeroForOne = sqrtRatioCurrentX96 >= sqrtRatioTargetX96;
bool exactIn = amountRemaining >= 0;
if (exactIn) {
uint256 amountRemainingLessFee = FullMath.mulDiv(uint256(amountRemaining), 1e6 - feePips, 1e6);
amountIn = zeroForOne
? SqrtPriceMath.getAmount0Delta(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, true)
: SqrtPriceMath.getAmount1Delta(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, true);
if (amountRemainingLessFee >= amountIn) sqrtRatioNextX96 = sqrtRatioTargetX96;
else
sqrtRatioNextX96 = SqrtPriceMath.getNextSqrtPriceFromInput(
sqrtRatioCurrentX96,
liquidity,
amountRemainingLessFee,
zeroForOne
);
} else {
amountOut = zeroForOne
? SqrtPriceMath.getAmount1Delta(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, false)
: SqrtPriceMath.getAmount0Delta(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, false);
if (uint256(-amountRemaining) >= amountOut) sqrtRatioNextX96 = sqrtRatioTargetX96;
else
sqrtRatioNextX96 = SqrtPriceMath.getNextSqrtPriceFromOutput(
sqrtRatioCurrentX96,
liquidity,
uint256(-amountRemaining),
zeroForOne
);
}
bool max = sqrtRatioTargetX96 == sqrtRatioNextX96;
// get the input/output amounts
if (zeroForOne) {
amountIn = max && exactIn
? amountIn
: SqrtPriceMath.getAmount0Delta(sqrtRatioNextX96, sqrtRatioCurrentX96, liquidity, true);
amountOut = max && !exactIn
? amountOut
: SqrtPriceMath.getAmount1Delta(sqrtRatioNextX96, sqrtRatioCurrentX96, liquidity, false);
} else {
amountIn = max && exactIn
? amountIn
: SqrtPriceMath.getAmount1Delta(sqrtRatioCurrentX96, sqrtRatioNextX96, liquidity, true);
amountOut = max && !exactIn
? amountOut
: SqrtPriceMath.getAmount0Delta(sqrtRatioCurrentX96, sqrtRatioNextX96, liquidity, false);
}
// cap the output amount to not exceed the remaining output amount
if (!exactIn && amountOut > uint256(-amountRemaining)) {
amountOut = uint256(-amountRemaining);
}
if (exactIn && sqrtRatioNextX96 != sqrtRatioTargetX96) {
// we didn't reach the target, so take the remainder of the maximum input as fee
feeAmount = uint256(amountRemaining) - amountIn;
} else {
feeAmount = FullMath.mulDivRoundingUp(amountIn, feePips, 1e6 - feePips);
}
}
}
contracts/libraries/Tick.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0 <0.8.0;
import './LowGasSafeMath.sol';
import './SafeCast.sol';
import './TickMath.sol';
import './LiquidityMath.sol';
/// @title Tick
/// @notice Contains functions for managing tick processes and relevant calculations
library Tick {
using LowGasSafeMath for int256;
using SafeCast for int256;
// info stored for each initialized individual tick
struct Info {
// the total position liquidity that references this tick
uint128 liquidityGross;
// amount of net liquidity added (subtracted) when tick is crossed from left to right (right to left),
int128 liquidityNet;
// fee growth per unit of liquidity on the _other_ side of this tick (relative to the current tick)
// only has relative meaning, not absolute — the value depends on when the tick is initialized
uint256 feeGrowthOutside0X128;
uint256 feeGrowthOutside1X128;
// the cumulative tick value on the other side of the tick
int56 tickCumulativeOutside;
// the seconds per unit of liquidity on the _other_ side of this tick (relative to the current tick)
// only has relative meaning, not absolute — the value depends on when the tick is initialized
uint160 secondsPerLiquidityOutsideX128;
// the seconds spent on the other side of the tick (relative to the current tick)
// only has relative meaning, not absolute — the value depends on when the tick is initialized
uint32 secondsOutside;
// true iff the tick is initialized, i.e. the value is exactly equivalent to the expression liquidityGross != 0
// these 8 bits are set to prevent fresh sstores when crossing newly initialized ticks
bool initialized;
}
/// @notice Derives max liquidity per tick from given tick spacing
/// @dev Executed within the pool constructor
/// @param tickSpacing The amount of required tick separation, realized in multiples of `tickSpacing`
/// e.g., a tickSpacing of 3 requires ticks to be initialized every 3rd tick i.e., ..., -6, -3, 0, 3, 6, ...
/// @return The max liquidity per tick
function tickSpacingToMaxLiquidityPerTick(int24 tickSpacing) internal pure returns (uint128) {
int24 minTick = (TickMath.MIN_TICK / tickSpacing) * tickSpacing;
int24 maxTick = (TickMath.MAX_TICK / tickSpacing) * tickSpacing;
uint24 numTicks = uint24((maxTick - minTick) / tickSpacing) + 1;
return type(uint128).max / numTicks;
}
/// @notice Retrieves fee growth data
/// @param self The mapping containing all tick information for initialized ticks
/// @param tickLower The lower tick boundary of the position
/// @param tickUpper The upper tick boundary of the position
/// @param tickCurrent The current tick
/// @param feeGrowthGlobal0X128 The all-time global fee growth, per unit of liquidity, in token0
/// @param feeGrowthGlobal1X128 The all-time global fee growth, per unit of liquidity, in token1
/// @return feeGrowthInside0X128 The all-time fee growth in token0, per unit of liquidity, inside the position's tick boundaries
/// @return feeGrowthInside1X128 The all-time fee growth in token1, per unit of liquidity, inside the position's tick boundaries
function getFeeGrowthInside(
mapping(int24 => Tick.Info) storage self,
int24 tickLower,
int24 tickUpper,
int24 tickCurrent,
uint256 feeGrowthGlobal0X128,
uint256 feeGrowthGlobal1X128
) internal view returns (uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) {
Info storage lower = self[tickLower];
Info storage upper = self[tickUpper];
// calculate fee growth below
uint256 feeGrowthBelow0X128;
uint256 feeGrowthBelow1X128;
if (tickCurrent >= tickLower) {
feeGrowthBelow0X128 = lower.feeGrowthOutside0X128;
feeGrowthBelow1X128 = lower.feeGrowthOutside1X128;
} else {
feeGrowthBelow0X128 = feeGrowthGlobal0X128 - lower.feeGrowthOutside0X128;
feeGrowthBelow1X128 = feeGrowthGlobal1X128 - lower.feeGrowthOutside1X128;
}
// calculate fee growth above
uint256 feeGrowthAbove0X128;
uint256 feeGrowthAbove1X128;
if (tickCurrent < tickUpper) {
feeGrowthAbove0X128 = upper.feeGrowthOutside0X128;
feeGrowthAbove1X128 = upper.feeGrowthOutside1X128;
} else {
feeGrowthAbove0X128 = feeGrowthGlobal0X128 - upper.feeGrowthOutside0X128;
feeGrowthAbove1X128 = feeGrowthGlobal1X128 - upper.feeGrowthOutside1X128;
}
feeGrowthInside0X128 = feeGrowthGlobal0X128 - feeGrowthBelow0X128 - feeGrowthAbove0X128;
feeGrowthInside1X128 = feeGrowthGlobal1X128 - feeGrowthBelow1X128 - feeGrowthAbove1X128;
}
/// @notice Updates a tick and returns true if the tick was flipped from initialized to uninitialized, or vice versa
/// @param self The mapping containing all tick information for initialized ticks
/// @param tick The tick that will be updated
/// @param tickCurrent The current tick
/// @param liquidityDelta A new amount of liquidity to be added (subtracted) when tick is crossed from left to right (right to left)
/// @param feeGrowthGlobal0X128 The all-time global fee growth, per unit of liquidity, in token0
/// @param feeGrowthGlobal1X128 The all-time global fee growth, per unit of liquidity, in token1
/// @param secondsPerLiquidityCumulativeX128 The all-time seconds per max(1, liquidity) of the pool
/// @param tickCumulative The tick * time elapsed since the pool was first initialized
/// @param time The current block timestamp cast to a uint32
/// @param upper true for updating a position's upper tick, or false for updating a position's lower tick
/// @param maxLiquidity The maximum liquidity allocation for a single tick
/// @return flipped Whether the tick was flipped from initialized to uninitialized, or vice versa
function update(
mapping(int24 => Tick.Info) storage self,
int24 tick,
int24 tickCurrent,
int128 liquidityDelta,
uint256 feeGrowthGlobal0X128,
uint256 feeGrowthGlobal1X128,
uint160 secondsPerLiquidityCumulativeX128,
int56 tickCumulative,
uint32 time,
bool upper,
uint128 maxLiquidity
) internal returns (bool flipped) {
Tick.Info storage info = self[tick];
uint128 liquidityGrossBefore = info.liquidityGross;
uint128 liquidityGrossAfter = LiquidityMath.addDelta(liquidityGrossBefore, liquidityDelta);
require(liquidityGrossAfter <= maxLiquidity, 'LO');
flipped = (liquidityGrossAfter == 0) != (liquidityGrossBefore == 0);
if (liquidityGrossBefore == 0) {
// by convention, we assume that all growth before a tick was initialized happened _below_ the tick
if (tick <= tickCurrent) {
info.feeGrowthOutside0X128 = feeGrowthGlobal0X128;
info.feeGrowthOutside1X128 = feeGrowthGlobal1X128;
info.secondsPerLiquidityOutsideX128 = secondsPerLiquidityCumulativeX128;
info.tickCumulativeOutside = tickCumulative;
info.secondsOutside = time;
}
info.initialized = true;
}
info.liquidityGross = liquidityGrossAfter;
// when the lower (upper) tick is crossed left to right (right to left), liquidity must be added (removed)
info.liquidityNet = upper
? int256(info.liquidityNet).sub(liquidityDelta).toInt128()
: int256(info.liquidityNet).add(liquidityDelta).toInt128();
}
/// @notice Clears tick data
/// @param self The mapping containing all initialized tick information for initialized ticks
/// @param tick The tick that will be cleared
function clear(mapping(int24 => Tick.Info) storage self, int24 tick) internal {
delete self[tick];
}
/// @notice Transitions to next tick as needed by price movement
/// @param self The mapping containing all tick information for initialized ticks
/// @param tick The destination tick of the transition
/// @param feeGrowthGlobal0X128 The all-time global fee growth, per unit of liquidity, in token0
/// @param feeGrowthGlobal1X128 The all-time global fee growth, per unit of liquidity, in token1
/// @param secondsPerLiquidityCumulativeX128 The current seconds per liquidity
/// @param tickCumulative The tick * time elapsed since the pool was first initialized
/// @param time The current block.timestamp
/// @return liquidityNet The amount of liquidity added (subtracted) when tick is crossed from left to right (right to left)
function cross(
mapping(int24 => Tick.Info) storage self,
int24 tick,
uint256 feeGrowthGlobal0X128,
uint256 feeGrowthGlobal1X128,
uint160 secondsPerLiquidityCumulativeX128,
int56 tickCumulative,
uint32 time
) internal returns (int128 liquidityNet) {
Tick.Info storage info = self[tick];
info.feeGrowthOutside0X128 = feeGrowthGlobal0X128 - info.feeGrowthOutside0X128;
info.feeGrowthOutside1X128 = feeGrowthGlobal1X128 - info.feeGrowthOutside1X128;
info.secondsPerLiquidityOutsideX128 = secondsPerLiquidityCumulativeX128 - info.secondsPerLiquidityOutsideX128;
info.tickCumulativeOutside = tickCumulative - info.tickCumulativeOutside;
info.secondsOutside = time - info.secondsOutside;
liquidityNet = info.liquidityNet;
}
}
contracts/libraries/TickBitmap.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import './BitMath.sol';
/// @title Packed tick initialized state library
/// @notice Stores a packed mapping of tick index to its initialized state
/// @dev The mapping uses int16 for keys since ticks are represented as int24 and there are 256 (2^8) values per word.
library TickBitmap {
/// @notice Computes the position in the mapping where the initialized bit for a tick lives
/// @param tick The tick for which to compute the position
/// @return wordPos The key in the mapping containing the word in which the bit is stored
/// @return bitPos The bit position in the word where the flag is stored
function position(int24 tick) private pure returns (int16 wordPos, uint8 bitPos) {
wordPos = int16(tick >> 8);
bitPos = uint8(tick % 256);
}
/// @notice Flips the initialized state for a given tick from false to true, or vice versa
/// @param self The mapping in which to flip the tick
/// @param tick The tick to flip
/// @param tickSpacing The spacing between usable ticks
function flipTick(
mapping(int16 => uint256) storage self,
int24 tick,
int24 tickSpacing
) internal {
require(tick % tickSpacing == 0); // ensure that the tick is spaced
(int16 wordPos, uint8 bitPos) = position(tick / tickSpacing);
uint256 mask = 1 << bitPos;
self[wordPos] ^= mask;
}
/// @notice Returns the next initialized tick contained in the same word (or adjacent word) as the tick that is either
/// to the left (less than or equal to) or right (greater than) of the given tick
/// @param self The mapping in which to compute the next initialized tick
/// @param tick The starting tick
/// @param tickSpacing The spacing between usable ticks
/// @param lte Whether to search for the next initialized tick to the left (less than or equal to the starting tick)
/// @return next The next initialized or uninitialized tick up to 256 ticks away from the current tick
/// @return initialized Whether the next tick is initialized, as the function only searches within up to 256 ticks
function nextInitializedTickWithinOneWord(
mapping(int16 => uint256) storage self,
int24 tick,
int24 tickSpacing,
bool lte
) internal view returns (int24 next, bool initialized) {
int24 compressed = tick / tickSpacing;
if (tick < 0 && tick % tickSpacing != 0) compressed--; // round towards negative infinity
if (lte) {
(int16 wordPos, uint8 bitPos) = position(compressed);
// all the 1s at or to the right of the current bitPos
uint256 mask = (1 << bitPos) - 1 + (1 << bitPos);
uint256 masked = self[wordPos] & mask;
// if there are no initialized ticks to the right of or at the current tick, return rightmost in the word
initialized = masked != 0;
// overflow/underflow is possible, but prevented externally by limiting both tickSpacing and tick
next = initialized
? (compressed - int24(bitPos - BitMath.mostSignificantBit(masked))) * tickSpacing
: (compressed - int24(bitPos)) * tickSpacing;
} else {
// start from the word of the next tick, since the current tick state doesn't matter
(int16 wordPos, uint8 bitPos) = position(compressed + 1);
// all the 1s at or to the left of the bitPos
uint256 mask = ~((1 << bitPos) - 1);
uint256 masked = self[wordPos] & mask;
// if there are no initialized ticks to the left of the current tick, return leftmost in the word
initialized = masked != 0;
// overflow/underflow is possible, but prevented externally by limiting both tickSpacing and tick
next = initialized
? (compressed + 1 + int24(BitMath.leastSignificantBit(masked) - bitPos)) * tickSpacing
: (compressed + 1 + int24(type(uint8).max - bitPos)) * tickSpacing;
}
}
}
contracts/libraries/TickMath.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0 <0.8.0;
/// @title Math library for computing sqrt prices from ticks and vice versa
/// @notice Computes sqrt price for ticks of size 1.0001, i.e. sqrt(1.0001^tick) as fixed point Q64.96 numbers. Supports
/// prices between 2**-128 and 2**128
library TickMath {
/// @dev The minimum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**-128
int24 internal constant MIN_TICK = -887272;
/// @dev The maximum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**128
int24 internal constant MAX_TICK = -MIN_TICK;
/// @dev The minimum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MIN_TICK)
uint160 internal constant MIN_SQRT_RATIO = 4295128739;
/// @dev The maximum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MAX_TICK)
uint160 internal constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342;
/// @notice Calculates sqrt(1.0001^tick) * 2^96
/// @dev Throws if |tick| > max tick
/// @param tick The input tick for the above formula
/// @return sqrtPriceX96 A Fixed point Q64.96 number representing the sqrt of the ratio of the two assets (token1/token0)
/// at the given tick
function getSqrtRatioAtTick(int24 tick) internal pure returns (uint160 sqrtPriceX96) {
uint256 absTick = tick < 0 ? uint256(-int256(tick)) : uint256(int256(tick));
require(absTick <= uint256(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;
// this divides by 1<<32 rounding up to go from a Q128.128 to a Q128.96.
// we then downcast because we know the result always fits within 160 bits due to our tick input constraint
// we round up in the division so getTickAtSqrtRatio of the output price is always consistent
sqrtPriceX96 = uint160((ratio >> 32) + (ratio % (1 << 32) == 0 ? 0 : 1));
}
/// @notice Calculates the greatest tick value such that getRatioAtTick(tick) <= ratio
/// @dev Throws in case sqrtPriceX96 < MIN_SQRT_RATIO, as MIN_SQRT_RATIO is the lowest value getRatioAtTick may
/// ever return.
/// @param sqrtPriceX96 The sqrt ratio for which to compute the tick as a Q64.96
/// @return tick The greatest tick for which the ratio is less than or equal to the input ratio
function getTickAtSqrtRatio(uint160 sqrtPriceX96) internal pure returns (int24 tick) {
// second inequality must be < because the price can never reach the price at the max tick
require(sqrtPriceX96 >= MIN_SQRT_RATIO && sqrtPriceX96 < MAX_SQRT_RATIO, 'R');
uint256 ratio = uint256(sqrtPriceX96) << 32;
uint256 r = ratio;
uint256 msb = 0;
assembly {
let f := shl(7, gt(r, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(6, gt(r, 0xFFFFFFFFFFFFFFFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(5, gt(r, 0xFFFFFFFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(4, gt(r, 0xFFFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(3, gt(r, 0xFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(2, gt(r, 0xF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(1, gt(r, 0x3))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := gt(r, 0x1)
msb := or(msb, f)
}
if (msb >= 128) r = ratio >> (msb - 127);
else r = ratio << (127 - msb);
int256 log_2 = (int256(msb) - 128) << 64;
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(63, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(62, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(61, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(60, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(59, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(58, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(57, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(56, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(55, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(54, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(53, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(52, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(51, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(50, f))
}
int256 log_sqrt10001 = log_2 * 255738958999603826347141; // 128.128 number
int24 tickLow = int24((log_sqrt10001 - 3402992956809132418596140100660247210) >> 128);
int24 tickHi = int24((log_sqrt10001 + 291339464771989622907027621153398088495) >> 128);
tick = tickLow == tickHi ? tickLow : getSqrtRatioAtTick(tickHi) <= sqrtPriceX96 ? tickHi : tickLow;
}
}
contracts/libraries/TransferHelper.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.6.0;
import '../interfaces/IERC20Minimal.sol';
/// @title TransferHelper
/// @notice Contains helper methods for interacting with ERC20 tokens that do not consistently return true/false
library TransferHelper {
/// @notice Transfers tokens from msg.sender to a recipient
/// @dev Calls transfer on token contract, errors with TF if transfer fails
/// @param token The contract address of the token which will be transferred
/// @param to The recipient of the transfer
/// @param value The value of the transfer
function safeTransfer(
address token,
address to,
uint256 value
) internal {
(bool success, bytes memory data) = token.call(
abi.encodeWithSelector(IERC20Minimal.transfer.selector, to, value)
);
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TF');
}
}
contracts/libraries/UnsafeMath.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Math functions that do not check inputs or outputs
/// @notice Contains methods that perform common math functions but do not do any overflow or underflow checks
library UnsafeMath {
/// @notice Returns ceil(x / y)
/// @dev division by 0 has unspecified behavior, and must be checked externally
/// @param x The dividend
/// @param y The divisor
/// @return z The quotient, ceil(x / y)
function divRoundingUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
assembly {
z := add(div(x, y), gt(mod(x, y), 0))
}
}
}
Compiler Settings
{"outputSelection":{"*":{"*":["abi","evm.bytecode","evm.deployedBytecode","evm.methodIdentifiers","metadata"],"":["ast"]}},"optimizer":{"runs":400,"enabled":true},"metadata":{"bytecodeHash":"none"},"libraries":{},"evmVersion":"istanbul"}
Contract ABI
[{"type":"event","name":"SetFactoryAddress","inputs":[{"type":"address","name":"factory","internalType":"address","indexed":true}],"anonymous":false},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"address","name":"pool","internalType":"address"}],"name":"deploy","inputs":[{"type":"address","name":"factory","internalType":"address"},{"type":"address","name":"token0","internalType":"address"},{"type":"address","name":"token1","internalType":"address"},{"type":"uint24","name":"fee","internalType":"uint24"},{"type":"int24","name":"tickSpacing","internalType":"int24"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"address","name":"","internalType":"address"}],"name":"factoryAddress","inputs":[]},{"type":"function","stateMutability":"view","outputs":[{"type":"address","name":"factory","internalType":"address"},{"type":"address","name":"token0","internalType":"address"},{"type":"address","name":"token1","internalType":"address"},{"type":"uint24","name":"fee","internalType":"uint24"},{"type":"int24","name":"tickSpacing","internalType":"int24"}],"name":"parameters","inputs":[]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"setFactoryAddress","inputs":[{"type":"address","name":"_factoryAddress","internalType":"address"}]}]
0x608060405234801561001057600080fd5b50615fec806100206000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c806383c17c55146100515780638903573014610079578063966dae0e146100c3578063fad5359f146100e7575b600080fd5b6100776004803603602081101561006757600080fd5b50356001600160a01b0316610131565b005b6100816101d9565b604080516001600160a01b0396871681529486166020860152929094168383015262ffffff16606083015260029290920b608082015290519081900360a00190f35b6100cb61020f565b604080516001600160a01b039092168252519081900360200190f35b6100cb600480360360a08110156100fd57600080fd5b506001600160a01b03813581169160208101358216916040820135169062ffffff606082013516906080013560020b61021e565b6003546001600160a01b03161561018f576040805162461bcd60e51b815260206004820152601360248201527f616c726561647920696e697469616c697a656400000000000000000000000000604482015290519081900360640190fd5b600380546001600160a01b0319166001600160a01b0383169081179091556040517fb4dd887347efc97a6ad35fc9824a2ac4c0a6a04344d89bf05f3308c854325a6490600090a250565b600054600154600280546001600160a01b03938416939283169281169162ffffff600160a01b83041691600160b81b9004900b85565b6003546001600160a01b031681565b6003546000906001600160a01b03163314610280576040805162461bcd60e51b815260206004820152601c60248201527f6f6e6c7920666163746f72792063616e2063616c6c206465706c6f7900000000604482015290519081900360640190fd5b6040805160a0810182526001600160a01b03888116808352888216602080850182905292891684860181905262ffffff898116606080880182905260028b810b6080998a01819052600080546001600160a01b03199081169099179055600180548916881790558154909716851762ffffff60a01b1916600160a01b84021762ffffff60b81b1916600160b81b97820b909416969096029290921790945586518086019390935282870191909152818101929092528451808203909201825290920192839052815191012090610355906103c4565b8190604051809103906000f5905080158015610375573d6000803e3d6000fd5b50600080546001600160a01b0319908116909155600180549091169055600280547fffffffffffff00000000000000000000000000000000000000000000000000001690559695505050505050565b615c0e806103d28339019056fe6101406040523480156200001257600080fd5b506000336001600160a01b031663890357306040518163ffffffff1660e01b815260040160a06040518083038186803b1580156200004f57600080fd5b505afa15801562000064573d6000803e3d6000fd5b505050506040513d60a08110156200007b57600080fd5b50805160208083015160408401516060808601516080968701516001600160e81b031960e892831b1660e0526001600160601b031993831b841660c05293821b831660a05294901b16909352600283810b900b90911b61010052909150620000ee90829062002f5e62000106821b17901c565b60801b6001600160801b031916610120525062000174565b60008082600281900b620d89e719816200011c57fe5b05029050600083600281900b620d89e8816200013457fe5b0502905060008460020b83830360020b816200014c57fe5b0560010190508062ffffff166001600160801b038016816200016a57fe5b0495945050505050565b60805160601c60a05160601c60c05160601c60e05160e81c6101005160e81c6101205160801c6159b26200025c600039806121735280614ecd5280614f04525080610cb15280612be95280614f385280614f6a525080610da05280611b475280611b7e5280612c315280612e005280612e425280612e895280612ed05250806113145280611c01528061206852806123f85280612c0d52806141fd52508061091452806114425280611bd05280612002528061237252806140b45250806121f6528061221f52806128d552806128fe5280612aa35280612ad05280612af952506159b26000f3fe608060405234801561001057600080fd5b50600436106101c45760003560e01c806370cf754a116100f9578063cc7e7fa211610097578063ddca3f4311610071578063ddca3f4314610842578063f305839914610862578063f30dba931461086a578063f637731d146108ec576101c4565b8063cc7e7fa2146107f5578063d0c93a7c1461081b578063d21220a71461083a576101c4565b8063a34123a7116100d3578063a34123a71461072d578063a38807f214610767578063b0d0d211146107c2578063c45a0155146107ed576101c4565b806370cf754a146105e157806385b66729146105e9578063883bdbfd14610626576101c4565b80633c8a7d8d116101665780634f1eb3d8116101405780634f1eb3d81461050f578063514ea4bf146105605780635339c296146105b9578063540d4918146105d9576101c4565b80633c8a7d8d146103cb578063461413191461046b578063490e6cbc14610485576101c4565b80631ad8b03b116101a25780631ad8b03b146102be578063252c09d7146102f557806332148f671461034c5780633850c7bd1461036f576101c4565b80630dfe1681146101c9578063128acb08146101ed5780631a6865021461029a575b600080fd5b6101d1610912565b604080516001600160a01b039092168252519081900360200190f35b610281600480360360a081101561020357600080fd5b6001600160a01b0382358116926020810135151592604082013592606083013516919081019060a081016080820135600160201b81111561024357600080fd5b82018360208201111561025557600080fd5b803590602001918460018302840111600160201b8311171561027657600080fd5b509092509050610936565b6040805192835260208301919091528051918290030190f35b6102a2611633565b604080516001600160801b039092168252519081900360200190f35b6102c6611642565b60405180836001600160801b03168152602001826001600160801b031681526020019250505060405180910390f35b6103126004803603602081101561030b57600080fd5b503561165c565b6040805163ffffffff909516855260069390930b60208501526001600160a01b039091168383015215156060830152519081900360800190f35b61036d6004803603602081101561036257600080fd5b503561ffff166116a1565b005b610377611793565b604080516001600160a01b03909816885260029690960b602088015261ffff9485168787015292841660608701529216608085015263ffffffff90911660a0840152151560c0830152519081900360e00190f35b610281600480360360a08110156103e157600080fd5b6001600160a01b03823516916020810135600290810b92604083013590910b916001600160801b036060820135169181019060a081016080820135600160201b81111561042d57600080fd5b82018360208201111561043f57600080fd5b803590602001918460018302840111600160201b8311171561046057600080fd5b5090925090506117e8565b610473611aa6565b60408051918252519081900360200190f35b61036d6004803603608081101561049b57600080fd5b6001600160a01b038235169160208101359160408201359190810190608081016060820135600160201b8111156104d157600080fd5b8201836020820111156104e357600080fd5b803590602001918460018302840111600160201b8311171561050457600080fd5b509092509050611aac565b6102c6600480360360a081101561052557600080fd5b506001600160a01b03813516906020810135600290810b91604081013590910b906001600160801b0360608201358116916080013516611ef3565b61057d6004803603602081101561057657600080fd5b5035612111565b604080516001600160801b0396871681526020810195909552848101939093529084166060840152909216608082015290519081900360a00190f35b610473600480360360208110156105cf57600080fd5b503560010b61214e565b6101d1612160565b6102a2612171565b6102c6600480360360608110156105ff57600080fd5b506001600160a01b03813516906001600160801b0360208201358116916040013516612195565b6106946004803603602081101561063c57600080fd5b810190602081018135600160201b81111561065657600080fd5b82018360208201111561066857600080fd5b803590602001918460208302840111600160201b8311171561068957600080fd5b509092509050612490565b604051808060200180602001838103835285818151815260200191508051906020019060200280838360005b838110156106d85781810151838201526020016106c0565b50505050905001838103825284818151815260200191508051906020019060200280838360005b838110156107175781810151838201526020016106ff565b5050505090500194505050505060405180910390f35b6102816004803603606081101561074357600080fd5b508035600290810b91602081013590910b90604001356001600160801b0316612515565b6107916004803603604081101561077d57600080fd5b508035600290810b9160200135900b612691565b6040805160069490940b84526001600160a01b03909216602084015263ffffffff1682820152519081900360600190f35b61036d600480360360408110156107d857600080fd5b5063ffffffff81358116916020013516612879565b6101d1612aa1565b61036d6004803603602081101561080b57600080fd5b50356001600160a01b0316612ac5565b610823612be7565b6040805160029290920b8252519081900360200190f35b6101d1612c0b565b61084a612c2f565b6040805162ffffff9092168252519081900360200190f35b610473612c53565b61088a6004803603602081101561088057600080fd5b503560020b612c59565b604080516001600160801b039099168952600f9790970b602089015287870195909552606087019390935260069190910b60808601526001600160a01b031660a085015263ffffffff1660c0840152151560e083015251908190036101000190f35b61036d6004803603602081101561090257600080fd5b50356001600160a01b0316612cc5565b7f000000000000000000000000000000000000000000000000000000000000000081565b60008085610970576040805162461bcd60e51b8152602060048201526002602482015261415360f01b604482015290519081900360640190fd5b6040805160e0810182526000546001600160a01b0381168252600160a01b8104600290810b810b900b602083015261ffff600160b81b8204811693830193909352600160c81b810483166060830152600160d81b9004909116608082015260015463ffffffff811660a083015260ff600160201b90910416151560c08201819052610a28576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b87610a735780600001516001600160a01b0316866001600160a01b0316118015610a6e575073fffd8963efd1fc6a506488495d951d5263988d266001600160a01b038716105b610aa5565b80600001516001600160a01b0316866001600160a01b0316108015610aa557506401000276a36001600160a01b038716115b610adc576040805162461bcd60e51b815260206004820152600360248201526214d41360ea1b604482015290519081900360640190fd5b6001805460ff60201b191690556040805160c08101909152600090808a610b115760108460a0015163ffffffff16901c610b1b565b60a084015161ffff165b63ffffffff1681526005546001600160801b03166020820152604001610b3f612fca565b63ffffffff1681526000602082018190526040820181905260609091015262010008549091506001600160a01b031615610be557620100085460408083015181516310a537f160e11b815263ffffffff909116600482015290516001600160a01b039092169163214a6fe29160248082019260009290919082900301818387803b158015610bcc57600080fd5b505af1158015610be0573d6000803e3d6000fd5b505050505b6000808913905060006040518060e001604052808b81526020016000815260200185600001516001600160a01b03168152602001856020015160020b81526020018c610c3357600354610c37565b6002545b815260200160006001600160801b0316815260200184602001516001600160801b031681525090505b805115801590610c865750886001600160a01b031681604001516001600160a01b031614155b156110f557610c93615942565b60408201516001600160a01b031681526060820151610cd6906007907f00000000000000000000000000000000000000000000000000000000000000008f612fce565b15156040830152600290810b810b60208301819052620d89e719910b1215610d0757620d89e7196020820152610d26565b6020810151620d89e860029190910b1315610d2657620d89e860208201525b610d338160200151613110565b6001600160a01b031660608201526040820151610dc4908d610d6d578b6001600160a01b031683606001516001600160a01b031611610d87565b8b6001600160a01b031683606001516001600160a01b0316105b610d95578260600151610d97565b8b5b60c085015185517f0000000000000000000000000000000000000000000000000000000000000000613441565b60c085015260a084015260808301526001600160a01b031660408301528215610e2657610dfa8160c00151826080015101613633565b825103825260a0810151610e1c90610e1190613633565b602084015190613649565b6020830152610e61565b610e338160a00151613633565b825101825260c08101516080820151610e5b91610e509101613633565b602084015190613665565b60208301525b835163ffffffff1615610ec1576000612710610e94866000015163ffffffff168460c0015161367b90919063ffffffff16565b81610e9b57fe5b60c0840180519290910491829003905260a0840180519091016001600160801b03169052505b60c08201516001600160801b031615610f0057610ef48160c00151600160801b8460c001516001600160801b031661369f565b60808301805190910190525b80606001516001600160a01b031682604001516001600160a01b031614156110b45780604001511561108b578360a00151610f8a57610f68846040015160008760200151886040015188602001518a60600151600961374f909695949392919063ffffffff16565b6001600160a01b03166080860152600690810b900b6060850152600160a08501525b62010008546001600160a01b03161561101557620100085460208201516040805163a498463360e01b815260029290920b60048301528e15156024830152516001600160a01b039092169163a49846339160448082019260009290919082900301818387803b158015610ffc57600080fd5b505af1158015611010573d6000803e3d6000fd5b505050505b600061106182602001518e61102c57600254611032565b84608001515b8f611041578560800151611045565b6003545b608089015160608a015160408b015160069594939291906138e1565b90508c1561106d576000035b61107b8360c001518261399f565b6001600160801b031660c0840152505b8b61109a5780602001516110a3565b60018160200151035b600290810b900b60608301526110ef565b80600001516001600160a01b031682604001516001600160a01b0316146110ef576110e28260400151613a55565b600290810b900b60608301525b50610c60565b836020015160020b816060015160020b146111c35760008061114386604001518660400151886020015188602001518a606001518b608001516009613d7d909695949392919063ffffffff16565b604085015160608601516000805461ffff60c81b1916600160c81b61ffff958616021761ffff60b81b1916600160b81b95909416949094029290921762ffffff60a01b1916600160a01b62ffffff60029490940b9390931692909202919091176001600160a01b0319166001600160a01b03909116179055506111e89050565b6040810151600080546001600160a01b0319166001600160a01b039092169190911790555b8060c001516001600160801b031683602001516001600160801b03161461122e5760c0810151600580546001600160801b0319166001600160801b039092169190911790555b6000808c1561128857608083015160025560a08301516001600160801b03161561127c5760a0830151600480546001600160801b031981166001600160801b03918216909301169190911790555b8260a0015191506112d5565b608083015160035560a08301516001600160801b0316156112ce5760a0830151600480546001600160801b03808216600160801b92839004821690940116029190911790555b5060a08201515b8315158d1515146112ee57602083015183518d036112fb565b82600001518c0383602001515b90985096508c1561143457600087121561133d5761133d7f00000000000000000000000000000000000000000000000000000000000000008f89600003613f18565b6000611347614066565b9050336001600160a01b03166323a69e758a8a8e8e6040518563ffffffff1660e01b815260040180858152602001848152602001806020018281038252848482818152602001925080828437600081840152601f19601f82011690508083019250505095505050505050600060405180830381600087803b1580156113cb57600080fd5b505af11580156113df573d6000803e3d6000fd5b505050506113eb614066565b6113f5828b61419f565b111561142e576040805162461bcd60e51b815260206004820152600360248201526249494160e81b604482015290519081900360640190fd5b5061155e565b600088121561146b5761146b7f00000000000000000000000000000000000000000000000000000000000000008f8a600003613f18565b60006114756141af565b9050336001600160a01b03166323a69e758a8a8e8e6040518563ffffffff1660e01b815260040180858152602001848152602001806020018281038252848482818152602001925080828437600081840152601f19601f82011690508083019250505095505050505050600060405180830381600087803b1580156114f957600080fd5b505af115801561150d573d6000803e3d6000fd5b505050506115196141af565b611523828a61419f565b111561155c576040805162461bcd60e51b815260206004820152600360248201526249494160e81b604482015290519081900360640190fd5b505b8d6001600160a01b0316336001600160a01b03167f19b47279256b2a23a1665c810c8d55a1758940ee09377d4f8d26497a3577dc838a8a87604001518860c001518960600151898960405180888152602001878152602001866001600160a01b03168152602001856001600160801b031681526020018460020b8152602001836001600160801b03168152602001826001600160801b0316815260200197505050505050505060405180910390a350506001805460ff60201b1916600160201b17905550939a92995091975050505050505050565b6005546001600160801b031681565b6004546001600160801b0380821691600160801b90041682565b60098161ffff811061166d57600080fd5b015463ffffffff81169150600160201b810460060b90600160581b81046001600160a01b031690600160f81b900460ff1684565b600154600160201b900460ff166116e5576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b6001805460ff60201b1916905560008054600160d81b900461ffff169061170e60098385614247565b6000805461ffff808416600160d81b810261ffff60d81b199093169290921790925591925083161461177b576040805161ffff80851682528316602082015281517fac49e518f90a358f652e4400164f05a5d8f7e35e7747279bc3a93dbf584e125a929181900390910190a15b50506001805460ff60201b1916600160201b17905550565b6000546001546001600160a01b03821691600160a01b810460020b9161ffff600160b81b8304811692600160c81b8104821692600160d81b9091049091169063ffffffff81169060ff600160201b9091041687565b6001546000908190600160201b900460ff16611831576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b6001805460ff60201b191690556001600160801b03851661185157600080fd5b60008061189f60405180608001604052808c6001600160a01b031681526020018b60020b81526020018a60020b81526020016118958a6001600160801b03166142ea565b600f0b90526142fb565b925092505081935080925060008060008611156118c1576118be614066565b91505b84156118d2576118cf6141af565b90505b336001600160a01b03166399eee9d087878b8b6040518563ffffffff1660e01b815260040180858152602001848152602001806020018281038252848482818152602001925080828437600081840152601f19601f82011690508083019250505095505050505050600060405180830381600087803b15801561195457600080fd5b505af1158015611968573d6000803e3d6000fd5b5050505060008611156119bf5761197d614066565b611987838861419f565b11156119bf576040805162461bcd60e51b815260206004820152600260248201526104d360f41b604482015290519081900360640190fd5b8415611a0f576119cd6141af565b6119d7828761419f565b1115611a0f576040805162461bcd60e51b81526020600482015260026024820152614d3160f01b604482015290519081900360640190fd5b8960020b8b60020b8d6001600160a01b03167f7a53080ba414158be7ec69b987b5fb7d07dee101fe85488f0853ae16239d0bde338d8b8b60405180856001600160a01b03168152602001846001600160801b0316815260200183815260200182815260200194505050505060405180910390a450506001805460ff60201b1916600160201b17905550919890975095505050505050565b60035481565b600154600160201b900460ff16611af0576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b6001805460ff60201b191690556005546001600160801b031680611b3f576040805162461bcd60e51b81526020600482015260016024820152601360fa1b604482015290519081900360640190fd5b6000611b74867f000000000000000000000000000000000000000000000000000000000000000062ffffff16620f4240614531565b90506000611bab867f000000000000000000000000000000000000000000000000000000000000000062ffffff16620f4240614531565b90506000611bb7614066565b90506000611bc36141af565b90508815611bf657611bf67f00000000000000000000000000000000000000000000000000000000000000008b8b613f18565b8715611c2757611c277f00000000000000000000000000000000000000000000000000000000000000008b8a613f18565b336001600160a01b031663a1d4833685858a8a6040518563ffffffff1660e01b815260040180858152602001848152602001806020018281038252848482818152602001925080828437600081840152601f19601f82011690508083019250505095505050505050600060405180830381600087803b158015611ca957600080fd5b505af1158015611cbd573d6000803e3d6000fd5b505050506000611ccb614066565b90506000611cd76141af565b905081611ce4858861419f565b1115611d1c576040805162461bcd60e51b8152602060048201526002602482015261046360f41b604482015290519081900360640190fd5b80611d27848761419f565b1115611d5f576040805162461bcd60e51b8152602060048201526002602482015261463160f01b604482015290519081900360640190fd5b8382038382038115611de85760015461ffff1660008115611d8c5761271063ffffffff8316850204611d8f565b60005b90506001600160801b03811615611dc257600480546001600160801b038082168401166001600160801b03199091161790555b611ddc818503600160801b8d6001600160801b031661369f565b60028054909101905550505b8015611e6d5760015460101c61ffff1660008115611e125761271063ffffffff8316840204611e15565b60005b90506001600160801b03811615611e4757600480546001600160801b03600160801b8083048216850182160291161790555b611e61818403600160801b8d6001600160801b031661369f565b60038054909101905550505b8d6001600160a01b0316336001600160a01b03167fbdbdb71d7860376ba52b25a5028beea23581364a40522f6bcfb86bb1f2dca6338f8f86866040518085815260200184815260200183815260200182815260200194505050505060405180910390a350506001805460ff60201b1916600160201b179055505050505050505050505050565b6001546000908190600160201b900460ff16611f3c576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b6001805460ff60201b191690556000611f58600833898961456b565b60038101549091506001600160801b0390811690861611611f795784611f88565b60038101546001600160801b03165b60038201549093506001600160801b03600160801b909104811690851611611fb05783611fc6565b6003810154600160801b90046001600160801b03165b91506001600160801b0383161561202b576003810180546001600160801b031981166001600160801b0391821686900382161790915561202b907f0000000000000000000000000000000000000000000000000000000000000000908a908616613f18565b6001600160801b03821615612091576003810180546001600160801b03600160801b808304821686900382160291811691909117909155612091907f0000000000000000000000000000000000000000000000000000000000000000908a908516613f18565b604080516001600160a01b038a1681526001600160801b0380861660208301528416818301529051600288810b92908a900b9133917f70935338e69775456a85ddef226c395fb668b63fa0115f5f20610b388e6ca9c0919081900360600190a4506001805460ff60201b1916600160201b17905590969095509350505050565b60086020526000908152604090208054600182015460028301546003909301546001600160801b0392831693919281811691600160801b90041685565b60076020526000908152604090205481565b62010008546001600160a01b031681565b7f000000000000000000000000000000000000000000000000000000000000000081565b6001546000908190600160201b900460ff166121de576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b6001805460ff60201b19169055336001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001614806122ae57507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316638da5cb5b6040518163ffffffff1660e01b815260040160206040518083038186803b15801561227657600080fd5b505afa15801561228a573d6000803e3d6000fd5b505050506040513d60208110156122a057600080fd5b50516001600160a01b031633145b6122b757600080fd5b6004546001600160801b03908116908516116122d357836122e0565b6004546001600160801b03165b6004549092506001600160801b03600160801b909104811690841611612306578261231a565b600454600160801b90046001600160801b03165b90506001600160801b0382161561239b576004546001600160801b038381169116141561234957600019909101905b600480546001600160801b031981166001600160801b0391821685900382161790915561239b907f00000000000000000000000000000000000000000000000000000000000000009087908516613f18565b6001600160801b03811615612421576004546001600160801b03828116600160801b9092041614156123cc57600019015b600480546001600160801b03600160801b808304821685900382160291811691909117909155612421907f00000000000000000000000000000000000000000000000000000000000000009087908416613f18565b604080516001600160801b0380851682528316602082015281516001600160a01b0388169233927f596b573906218d3411850b26a6b437d6c4522fdb43d2d2386263f86d50b8b151929081900390910190a36001805460ff60201b1916600160201b1790559094909350915050565b60608061250a61249e612fca565b858580806020026020016040519081016040528093929190818152602001838360200280828437600092018290525054600554600996959450600160a01b820460020b935061ffff600160b81b8304811693506001600160801b0390911691600160c81b9004166145cf565b915091509250929050565b6001546000908190600160201b900460ff1661255e576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b6001805460ff60201b1916905560408051608081018252338152600287810b602083015286900b91810191909152600090819081906125ba90606081016125ad6001600160801b038a166142ea565b600003600f0b90526142fb565b92509250925081600003945080600003935060008511806125db5750600084115b1561261a576003830180546001600160801b038082168089018216600160801b93849004831689019092169092029091176001600160801b0319161790555b604080516001600160801b0388168152602081018790528082018690529051600289810b92908b900b9133917f0c396cd989a39f4459b5fa1aed6a9a8dcdbc45908acfd67e028cd568da98982c919081900360600190a450506001805460ff60201b1916600160201b179055509094909350915050565b60008060006126a08585614729565b600285810b810b600090815260066020819052604080832088850b90940b8352822060038401549182900b93600160381b83046001600160a01b0316928492600160d81b820463ffffffff16928492909190600160f81b900460ff168061270657600080fd5b6003820154600681900b9850600160381b81046001600160a01b03169650600160d81b810463ffffffff169450600160f81b900460ff168061274757600080fd5b50506040805160e0810182526000546001600160a01b0381168252600160a01b8104600290810b810b810b6020840181905261ffff600160b81b8404811695850195909552600160c81b830485166060850152600160d81b909204909316608083015260015463ffffffff811660a084015260ff600160201b90910416151560c08301529093508e820b910b121590506127ef57509390940396509003935090039050612872565b8a60020b816020015160020b121561286357600061280b612fca565b6020830151604084015160055460608601519394506000938493612841936009938893879392916001600160801b03169061374f565b9a9003989098039b505094909603929092039650909103039250612872915050565b50949093039650039350900390505b9250925092565b600154600160201b900460ff166128bd576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b6001805460ff60201b19169055336001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016148061298d57507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316638da5cb5b6040518163ffffffff1660e01b815260040160206040518083038186803b15801561295557600080fd5b505afa158015612969573d6000803e3d6000fd5b505050506040513d602081101561297f57600080fd5b50516001600160a01b031633145b61299657600080fd5b63ffffffff821615806129c457506103e88263ffffffff16101580156129c45750610fa08263ffffffff1611155b80156129f9575063ffffffff811615806129f957506103e88163ffffffff16101580156129f95750610fa08163ffffffff1611155b612a0257600080fd5b6001805465ffffffff0000601084901b16840163ffffffff90811663ffffffff19831617909255167fb3159fed3ddfba67bae294599eafe2d0ec98c08bb38e0e5fb87d33154b6e05aa62010000826040805163ffffffff939092068316825261ffff601086901c16602083015286831682820152918516606082015290519081900360800190a150506001805460ff60201b1916600160201b17905550565b7f000000000000000000000000000000000000000000000000000000000000000081565b336001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000161480612b8857507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316638da5cb5b6040518163ffffffff1660e01b815260040160206040518083038186803b158015612b5057600080fd5b505afa158015612b64573d6000803e3d6000fd5b505050506040513d6020811015612b7a57600080fd5b50516001600160a01b031633145b612b9157600080fd5b6201000880546001600160a01b0383166001600160a01b0319909116811790915560408051918252517f29983690a85a11696ce8a357993744f8d5a74fde14653e517cc2f8608a7235e99181900360200190a150565b7f000000000000000000000000000000000000000000000000000000000000000081565b7f000000000000000000000000000000000000000000000000000000000000000081565b7f000000000000000000000000000000000000000000000000000000000000000081565b60025481565b60066020819052600091825260409091208054600182015460028301546003909301546001600160801b03831694600160801b909304600f0b93919281900b90600160381b81046001600160a01b031690600160d81b810463ffffffff1690600160f81b900460ff1688565b6000546001600160a01b031615612d08576040805162461bcd60e51b8152602060048201526002602482015261414960f01b604482015290519081900360640190fd5b6000612d1382613a55565b9050600080612d2b612d23612fca565b6009906147f2565b6040805160e0810182526001600160a01b038816808252600288810b60208401819052600094840185905261ffff8781166060860181905290871660808601819052630c800c8060a08701819052600160c090970187905287546001600160a01b03191690951762ffffff60a01b1916600160a01b62ffffff9490950b8416949094029390931763ffffffff60b81b1916600160c81b9091021761ffff60d81b1916600160d81b909202919091179093558154600160201b63ffffffff1990911690911760ff60201b191617905591935091507f00000000000000000000000000000000000000000000000000000000000000001660641415612e40576001805463ffffffff1916630ce40ce4179055612f11565b7f000000000000000000000000000000000000000000000000000000000000000062ffffff166101f41415612e87576001805463ffffffff1916630d480d48179055612f11565b7f000000000000000000000000000000000000000000000000000000000000000062ffffff166109c41415612ece576001805463ffffffff1916630c800c80179055612f11565b7f000000000000000000000000000000000000000000000000000000000000000062ffffff166127101415612f11576001805463ffffffff1916630c800c801790555b604080516001600160a01b0386168152600285900b602082015281517f98636036cb66a9c19a37435efc1e90142190214e8abeb821bdba3f2990dd4c95929181900390910190a150505050565b60008082600281900b620d89e71981612f7357fe5b05029050600083600281900b620d89e881612f8a57fe5b0502905060008460020b83830360020b81612fa157fe5b0560010190508062ffffff166001600160801b03801681612fbe57fe5b0493505050505b919050565b4290565b60008060008460020b8660020b81612fe257fe5b05905060008660020b12801561300957508460020b8660020b8161300257fe5b0760020b15155b1561301357600019015b8315613088576000806130258361483e565b600182810b810b600090815260208d9052604090205460ff83169190911b8001600019019081168015159750929450909250908561306a57888360ff1686030261307d565b8861307482614850565b840360ff168603025b965050505050613106565b6000806130978360010161483e565b91509150600060018260ff166001901b031990506000818b60008660010b60010b81526020019081526020016000205416905080600014159550856130e957888360ff0360ff168660010101026130ff565b88836130f4836148ef565b0360ff168660010101025b9650505050505b5094509492505050565b60008060008360020b12613127578260020b61312f565b8260020b6000035b9050620d89e881111561316d576040805162461bcd60e51b81526020600482015260016024820152601560fa1b604482015290519081900360640190fd5b60006001821661318157600160801b613193565b6ffffcb933bd6fad37aa2d162d1a5940015b70ffffffffffffffffffffffffffffffffff16905060028216156131c7576ffff97272373d413259a46990580e213a0260801c5b60048216156131e6576ffff2e50f5f656932ef12357cf3c7fdcc0260801c5b6008821615613205576fffe5caca7e10e4e61c3624eaa0941cd00260801c5b6010821615613224576fffcb9843d60f6159c9db58835c9266440260801c5b6020821615613243576fff973b41fa98c081472e6896dfb254c00260801c5b6040821615613262576fff2ea16466c96a3843ec78b326b528610260801c5b6080821615613281576ffe5dee046a99a2a811c461f1969c30530260801c5b6101008216156132a1576ffcbe86c7900a88aedcffc83b479aa3a40260801c5b6102008216156132c1576ff987a7253ac413176f2b074cf7815e540260801c5b6104008216156132e1576ff3392b0822b70005940c7a398e4b70f30260801c5b610800821615613301576fe7159475a2c29b7443b29c7fa6e889d90260801c5b611000821615613321576fd097f3bdfd2022b8845ad8f792aa58250260801c5b612000821615613341576fa9f746462d870fdf8a65dc1f90e061e50260801c5b614000821615613361576f70d869a156d2a1b890bb3df62baf32f70260801c5b618000821615613381576f31be135f97d08fd981231505542fcfa60260801c5b620100008216156133a2576f09aa508b5b7a84e1c677de54f3e99bc90260801c5b620200008216156133c2576e5d6af8dedb81196699c329225ee6040260801c5b620400008216156133e1576d2216e584f5fa1ea926041bedfe980260801c5b620800008216156133fe576b048a170391f7dc42444e8fa20260801c5b60008460020b131561341957806000198161341557fe5b0490505b600160201b81061561342c57600161342f565b60005b60ff16602082901c0192505050919050565b60008080806001600160a01b03808916908a1610158187128015906134c657600061347a8989620f42400362ffffff16620f424061369f565b9050826134935761348e8c8c8c60016149d9565b6134a0565b6134a08b8d8c6001614a54565b95508581106134b1578a96506134c0565b6134bd8c8b8386614b08565b96505b50613510565b816134dd576134d88b8b8b6000614a54565b6134ea565b6134ea8a8c8b60006149d9565b93508388600003106134fe57899550613510565b61350d8b8a8a60000385614b54565b95505b6001600160a01b038a81169087161482156135735780801561352f5750815b61354557613540878d8c6001614a54565b613547565b855b9550808015613554575081155b61356a57613565878d8c60006149d9565b61356c565b845b94506135bd565b80801561357d5750815b6135935761358e8c888c60016149d9565b613595565b855b95508080156135a2575081155b6135b8576135b38c888c6000614a54565b6135ba565b845b94505b811580156135cd57508860000385115b156135d9578860000394505b8180156135f857508a6001600160a01b0316876001600160a01b031614155b15613607578589039350613624565b613621868962ffffff168a620f42400362ffffff16614531565b93505b50505095509550955095915050565b6000600160ff1b821061364557600080fd5b5090565b8082038281131560008312151461365f57600080fd5b92915050565b8181018281121560008312151461365f57600080fd5b60008215806136965750508181028183828161369357fe5b04145b61365f57600080fd5b60008080600019858709868602925082811090839003039050806136d557600084116136ca57600080fd5b508290049050613748565b8084116136e157600080fd5b6000848688096000868103871696879004966002600389028118808a02820302808a02820302808a02820302808a02820302808a02820302808a02909103029181900381900460010186841190950394909402919094039290920491909117919091029150505b9392505050565b60008063ffffffff87166137f5576000898661ffff1661ffff811061377057fe5b60408051608081018252919092015463ffffffff808216808452600160201b8304600690810b810b900b6020850152600160581b83046001600160a01b031694840194909452600160f81b90910460ff16151560608301529092508a16146137e1576137de818a8988614ba0565b90505b8060200151816040015192509250506138d5565b86880360008061380a8c8c858c8c8c8c614c43565b91509150816000015163ffffffff168363ffffffff16141561383c5781602001518260400151945094505050506138d5565b805163ffffffff848116911614156138645780602001518160400151945094505050506138d5565b8151815160208085015190840151918390039286039163ffffffff80841692908516910360060b8161389257fe5b05028460200151018263ffffffff168263ffffffff1686604001518660400151036001600160a01b031602816138c457fe5b048560400151019650965050505050505b97509795505050505050565b600295860b860b60009081526020979097526040909620600181018054909503909455938301805490920390915560038201805463ffffffff600160d81b6001600160a01b03600160381b808504821690960316909402670100000000000000600160d81b031990921691909117600681810b90960390950b66ffffffffffffff1666ffffffffffffff199095169490941782810485169095039093160263ffffffff60d81b1990931692909217905554600160801b9004600f0b90565b60008082600f0b1215613a0457826001600160801b03168260000384039150816001600160801b0316106139ff576040805162461bcd60e51b81526020600482015260026024820152614c5360f01b604482015290519081900360640190fd5b61365f565b826001600160801b03168284019150816001600160801b0316101561365f576040805162461bcd60e51b81526020600482015260026024820152614c4160f01b604482015290519081900360640190fd5b60006401000276a36001600160a01b03831610801590613a91575073fffd8963efd1fc6a506488495d951d5263988d266001600160a01b038316105b613ac6576040805162461bcd60e51b81526020600482015260016024820152602960f91b604482015290519081900360640190fd5b77ffffffffffffffffffffffffffffffffffffffff00000000602083901b166001600160801b03811160071b81811c67ffffffffffffffff811160061b90811c63ffffffff811160051b90811c61ffff811160041b90811c60ff8111600390811b91821c600f811160021b90811c918211600190811b92831c97908811961790941790921717909117171760808110613b6757607f810383901c9150613b71565b80607f0383901b91505b908002607f81811c60ff83811c9190911c800280831c81831c1c800280841c81841c1c800280851c81851c1c800280861c81861c1c800280871c81871c1c800280881c81881c1c800280891c81891c1c8002808a1c818a1c1c8002808b1c818b1c1c8002808c1c818c1c1c8002808d1c818d1c1c8002808e1c9c81901c9c909c1c80029c8d901c9e9d607f198f0160401b60c09190911c678000000000000000161760c19b909b1c674000000000000000169a909a1760c29990991c672000000000000000169890981760c39790971c671000000000000000169690961760c49590951c670800000000000000169490941760c59390931c670400000000000000169290921760c69190911c670200000000000000161760c79190911c600160381b161760c89190911c6680000000000000161760c99190911c6640000000000000161760ca9190911c6620000000000000161760cb9190911c6610000000000000161760cc9190911c6608000000000000161760cd9190911c66040000000000001617693627a301d71055774c8581026f028f6481ab7f045a5af012a19d003aa9198101608090811d906fdb2df09e81959a81455e260799a0632f8301901d600281810b9083900b14613d6e57886001600160a01b0316613d5282613110565b6001600160a01b03161115613d675781613d69565b805b613d70565b815b9998505050505050505050565b6000806000898961ffff1661ffff8110613d9357fe5b60408051608081018252919092015463ffffffff808216808452600160201b8304600690810b810b900b6020850152600160581b83046001600160a01b031694840194909452600160f81b90910460ff161515606083015290925089161415613e0257888592509250506138d5565b8461ffff168461ffff16118015613e2357506001850361ffff168961ffff16145b15613e3057839150613e34565b8491505b8161ffff168960010161ffff1681613e4857fe5b069250613e5781898989614ba0565b8a8461ffff1661ffff8110613e6857fe5b825191018054602084015160408501516060909501511515600160f81b026001600160f81b036001600160a01b03909616600160581b027fff0000000000000000000000000000000000000000ffffffffffffffffffffff60069390930b66ffffffffffffff16600160201b026affffffffffffff000000001963ffffffff90971663ffffffff199095169490941795909516929092171692909217929092161790555097509795505050505050565b604080516001600160a01b038481166024830152604480830185905283518084039091018152606490920183526020820180516001600160e01b031663a9059cbb60e01b1781529251825160009485949389169392918291908083835b60208310613f945780518252601f199092019160209182019101613f75565b6001836020036101000a0380198251168184511680821785525050505050509050019150506000604051808303816000865af19150503d8060008114613ff6576040519150601f19603f3d011682016040523d82523d6000602084013e613ffb565b606091505b5091509150818015614029575080511580614029575080806020019051602081101561402657600080fd5b50515b61405f576040805162461bcd60e51b81526020600482015260026024820152612a2360f11b604482015290519081900360640190fd5b5050505050565b604080513060248083019190915282518083039091018152604490910182526020810180516001600160e01b03166370a0823160e01b17815291518151600093849384936001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001693919290918291908083835b602083106140ff5780518252601f1990920191602091820191016140e0565b6001836020036101000a038019825116818451168082178552505050505050905001915050600060405180830381855afa9150503d806000811461415f576040519150601f19603f3d011682016040523d82523d6000602084013e614164565b606091505b509150915081801561417857506020815110155b61418157600080fd5b80806020019051602081101561419657600080fd5b50519250505090565b8082018281101561365f57600080fd5b604080513060248083019190915282518083039091018152604490910182526020810180516001600160e01b03166370a0823160e01b17815291518151600093849384936001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000169391929091829190808383602083106140ff5780518252601f1990920191602091820191016140e0565b6000808361ffff1611614285576040805162461bcd60e51b81526020600482015260016024820152604960f81b604482015290519081900360640190fd5b8261ffff168261ffff161161429b575081613748565b825b8261ffff168161ffff1610156142e1576001858261ffff1661ffff81106142c057fe5b01805463ffffffff191663ffffffff9290921691909117905560010161429d565b50909392505050565b80600f81900b8114612fc557600080fd5b600080600061431284602001518560400151614729565b6040805160e0810182526000546001600160a01b0381168252600160a01b8104600290810b810b900b602080840182905261ffff600160b81b8404811685870152600160c81b84048116606080870191909152600160d81b90940416608085015260015463ffffffff811660a086015260ff600160201b90910416151560c0850152885190890151948901519289015193946143b49491939092909190614e3d565b93508460600151600f0b60001461452957846020015160020b816020015160020b1215614409576144026143eb8660200151613110565b6143f88760400151613110565b8760600151614ff2565b9250614529565b846040015160020b816020015160020b12156144ff5760055460408201516001600160801b039091169061445b9061443f612fca565b6020850151606086015160808701516009949392918791613d7d565b6000805461ffff60c81b1916600160c81b61ffff938416021761ffff60b81b1916600160b81b9390921692909202179055815160408701516144ab91906144a190613110565b8860600151614ff2565b93506144c96144bd8760200151613110565b83516060890151615036565b92506144d981876060015161399f565b600580546001600160801b0319166001600160801b039290921691909117905550614529565b61452661450f8660200151613110565b61451c8760400151613110565b8760600151615036565b91505b509193909250565b600061453e84848461369f565b90506000828061454a57fe5b848609111561374857600019811061456157600080fd5b6001019392505050565b6040805160609490941b6bffffffffffffffffffffffff1916602080860191909152600293840b60e890811b60348701529290930b90911b60378401528051808403601a018152603a90930181528251928201929092206000908152929052902090565b60608060008361ffff161161460f576040805162461bcd60e51b81526020600482015260016024820152604960f81b604482015290519081900360640190fd5b865167ffffffffffffffff8111801561462757600080fd5b50604051908082528060200260200182016040528015614651578160200160208202803683370190505b509150865167ffffffffffffffff8111801561466c57600080fd5b50604051908082528060200260200182016040528015614696578160200160208202803683370190505b50905060005b875181101561471c576146c78a8a8a84815181106146b657fe5b60200260200101518a8a8a8a61374f565b8483815181106146d357fe5b602002602001018484815181106146e657fe5b60200260200101826001600160a01b03166001600160a01b03168152508260060b60060b8152505050808060010191505061469c565b5097509795505050505050565b8060020b8260020b12614769576040805162461bcd60e51b8152602060048201526003602482015262544c5560e81b604482015290519081900360640190fd5b620d89e719600283900b12156147ac576040805162461bcd60e51b8152602060048201526003602482015262544c4d60e81b604482015290519081900360640190fd5b620d89e8600282900b13156147ee576040805162461bcd60e51b815260206004820152600360248201526254554d60e81b604482015290519081900360640190fd5b5050565b6040805160808101825263ffffffff9283168082526000602083018190529282019290925260016060909101819052835463ffffffff1916909117909116600160f81b17909155908190565b60020b600881901d9161010090910790565b600080821161485e57600080fd5b600160801b821061487157608091821c91015b68010000000000000000821061488957604091821c91015b600160201b821061489c57602091821c91015b6201000082106148ae57601091821c91015b61010082106148bf57600891821c91015b601082106148cf57600491821c91015b600482106148df57600291821c91015b60028210612fc557600101919050565b60008082116148fd57600080fd5b5060ff6001600160801b0382161561491857607f1901614920565b608082901c91505b67ffffffffffffffff82161561493957603f1901614941565b604082901c91505b63ffffffff82161561495657601f190161495e565b602082901c91505b61ffff82161561497157600f1901614979565b601082901c91505b60ff82161561498b5760071901614993565b600882901c91505b600f8216156149a557600319016149ad565b600482901c91505b60038216156149bf57600119016149c7565b600282901c91505b6001821615612fc55760001901919050565b6000836001600160a01b0316856001600160a01b031611156149f9579293925b81614a2657614a21836001600160801b03168686036001600160a01b0316600160601b61369f565b614a49565b614a49836001600160801b03168686036001600160a01b0316600160601b614531565b90505b949350505050565b6000836001600160a01b0316856001600160a01b03161115614a74579293925b6fffffffffffffffffffffffffffffffff60601b606084901b166001600160a01b038686038116908716614aa757600080fd5b83614ad757866001600160a01b0316614aca8383896001600160a01b031661369f565b81614ad157fe5b04614afd565b614afd614aee8383896001600160a01b0316614531565b886001600160a01b0316615065565b979650505050505050565b600080856001600160a01b031611614b1f57600080fd5b6000846001600160801b031611614b3557600080fd5b81614b4757614a218585856001615070565b614a498585856001615151565b600080856001600160a01b031611614b6b57600080fd5b6000846001600160801b031611614b8157600080fd5b81614b9357614a218585856000615151565b614a498585856000615070565b614ba861597e565b600085600001518503905060405180608001604052808663ffffffff1681526020018263ffffffff168660020b0288602001510160060b81526020016000856001600160801b031611614bfc576001614bfe565b845b6001600160801b031663ffffffff60801b608085901b1681614c1c57fe5b048860400151016001600160a01b0316815260200160011515815250915050949350505050565b614c4b61597e565b614c5361597e565b888561ffff1661ffff8110614c6457fe5b60408051608081018252919092015463ffffffff8116808352600160201b8204600690810b810b900b6020840152600160581b82046001600160a01b031693830193909352600160f81b900460ff16151560608201529250614cc89089908961523d565b15614d00578663ffffffff16826000015163ffffffff161415614cea576138d5565b81614cf783898988614ba0565b915091506138d5565b888361ffff168660010161ffff1681614d1557fe5b0661ffff1661ffff8110614d2557fe5b60408051608081018252929091015463ffffffff81168352600160201b8104600690810b810b900b60208401526001600160a01b03600160581b8204169183019190915260ff600160f81b90910416151560608201819052909250614dda57604080516080810182528a5463ffffffff81168252600160201b8104600690810b810b900b6020830152600160581b81046001600160a01b031692820192909252600160f81b90910460ff161515606082015291505b614de98883600001518961523d565b614e20576040805162461bcd60e51b815260206004820152600360248201526213d31160ea1b604482015290519081900360640190fd5b614e2d89898988876152fe565b9150915097509795505050505050565b6000614e4c600887878761456b565b60025460035491925090600080600f87900b15614f92576000614e6d612fca565b6000805460055492935090918291614eb79160099186918591600160a01b810460020b9161ffff600160b81b83048116926001600160801b0390921691600160c81b90041661374f565b9092509050614ef160068d8b8d8b8b87898b60007f000000000000000000000000000000000000000000000000000000000000000061549c565b9450614f2860068c8b8d8b8b87898b60017f000000000000000000000000000000000000000000000000000000000000000061549c565b93508415614f5c57614f5c60078d7f0000000000000000000000000000000000000000000000000000000000000000615659565b8315614f8e57614f8e60078c7f0000000000000000000000000000000000000000000000000000000000000000615659565b5050505b600080614fa460068c8c8b8a8a6156bf565b9092509050614fb5878a848461576b565b600089600f0b1215614fe3578315614fd257614fd260068c615900565b8215614fe357614fe360068b615900565b50505050505095945050505050565b60008082600f0b126150185761501361500e8585856001614a54565b613633565b614a4c565b61502b61500e8585856000036000614a54565b600003949350505050565b60008082600f0b126150525761501361500e85858560016149d9565b61502b61500e85858560000360006149d9565b808204910615150190565b600081156150e35760006001600160a01b038411156150a6576150a184600160601b876001600160801b031661369f565b6150be565b6001600160801b038516606085901b816150bc57fe5b045b90506150db6150d66001600160a01b0388168361419f565b61592c565b915050614a4c565b60006001600160a01b038411156151115761510c84600160601b876001600160801b0316614531565b615128565b615128606085901b6001600160801b038716615065565b905080866001600160a01b03161161513f57600080fd5b6001600160a01b038616039050614a4c565b60008261515f575083614a4c565b6fffffffffffffffffffffffffffffffff60601b606085901b1682156151f6576001600160a01b0386168481029085828161519657fe5b0414156151c7578181018281106151c5576151bb83896001600160a01b031683614531565b9350505050614a4c565b505b6151ed826151e8878a6001600160a01b031686816151e157fe5b049061419f565b615065565b92505050614a4c565b6001600160a01b0386168481029085828161520d57fe5b0414801561521a57508082115b61522357600080fd5b8082036151bb6150d6846001600160a01b038b1684614531565b60008363ffffffff168363ffffffff161115801561526757508363ffffffff168263ffffffff1611155b15615283578163ffffffff168363ffffffff1611159050613748565b60008463ffffffff168463ffffffff16116152aa578363ffffffff16600160201b016152b2565b8363ffffffff165b64ffffffffff16905060008563ffffffff168463ffffffff16116152e2578363ffffffff16600160201b016152ea565b8363ffffffff165b64ffffffffff169091111595945050505050565b61530661597e565b61530e61597e565b60008361ffff168560010161ffff168161532457fe5b0661ffff169050600060018561ffff16830103905060005b506002818301048961ffff8716828161535157fe5b0661ffff811061535d57fe5b60408051608081018252929091015463ffffffff81168352600160201b8104600690810b810b900b60208401526001600160a01b03600160581b8204169183019190915260ff600160f81b909104161515606082018190529095506153c75780600101925061533c565b898661ffff1682600101816153d857fe5b0661ffff81106153e457fe5b60408051608081018252929091015463ffffffff81168352600160201b8104600690810b810b900b60208401526001600160a01b03600160581b8204169183019190915260ff600160f81b9091041615156060820152855190945060009061544e908b908b61523d565b905080801561546757506154678a8a876000015161523d565b15615472575061548f565b8061548257600182039250615489565b8160010193505b5061533c565b5050509550959350505050565b60028a810b900b600090815260208c90526040812080546001600160801b0316826154c7828d61399f565b9050846001600160801b0316816001600160801b03161115615515576040805162461bcd60e51b81526020600482015260026024820152614c4f60f01b604482015290519081900360640190fd5b6001600160801b0382811615908216158114159450156155be578c60020b8e60020b136155a657600183018b9055600283018a9055600383018054670100000000000000600160d81b031916600160381b6001600160a01b038c16021766ffffffffffffff191666ffffffffffffff60068b900b161763ffffffff60d81b1916600160d81b63ffffffff8a16021790555b6003830180546001600160f81b0316600160f81b1790555b82546001600160801b0319166001600160801b03821617835585615607578254615602906155fd90600160801b9004600f90810b810b908f900b613665565b6142ea565b615628565b8254615628906155fd90600160801b9004600f90810b810b908f900b613649565b8354600f9190910b6001600160801b03908116600160801b0291161790925550909c9b505050505050505050505050565b8060020b8260020b8161566857fe5b0760020b1561567657600080fd5b6000806156918360020b8560020b8161568b57fe5b0561483e565b600191820b820b60009081526020979097526040909620805460ff9097169190911b90951890945550505050565b600285810b80820b60009081526020899052604080822088850b850b83529082209193849391929184918291908a900b1261570557505060018201546002830154615718565b8360010154880391508360020154870390505b6000808b60020b8b60020b121561573a5750506001830154600284015461574d565b84600101548a0391508460020154890390505b92909803979097039b96909503949094039850939650505050505050565b6040805160a08101825285546001600160801b0390811682526001870154602083015260028701549282019290925260038601548083166060830152600160801b900490911660808201526000600f85900b61580a5781516001600160801b0316615802576040805162461bcd60e51b815260206004820152600260248201526104e560f41b604482015290519081900360640190fd5b508051615819565b8151615816908661399f565b90505b600061583d8360200151860384600001516001600160801b0316600160801b61369f565b905060006158638460400151860385600001516001600160801b0316600160801b61369f565b905086600f0b60001461588a5787546001600160801b0319166001600160801b0384161788555b60018801869055600288018590556001600160801b0382161515806158b857506000816001600160801b0316115b156158f6576003880180546001600160801b031981166001600160801b039182168501821617808216600160801b9182900483168501909216021790555b5050505050505050565b600290810b810b6000908152602092909252604082208281556001810183905590810182905560030155565b806001600160a01b0381168114612fc557600080fd5b6040805160e081018252600080825260208201819052918101829052606081018290526080810182905260a0810182905260c081019190915290565b6040805160808101825260008082526020820181905291810182905260608101919091529056fea164736f6c6343000706000aa164736f6c6343000706000a
Deployed ByteCode
0x608060405234801561001057600080fd5b506004361061004c5760003560e01c806383c17c55146100515780638903573014610079578063966dae0e146100c3578063fad5359f146100e7575b600080fd5b6100776004803603602081101561006757600080fd5b50356001600160a01b0316610131565b005b6100816101d9565b604080516001600160a01b0396871681529486166020860152929094168383015262ffffff16606083015260029290920b608082015290519081900360a00190f35b6100cb61020f565b604080516001600160a01b039092168252519081900360200190f35b6100cb600480360360a08110156100fd57600080fd5b506001600160a01b03813581169160208101358216916040820135169062ffffff606082013516906080013560020b61021e565b6003546001600160a01b03161561018f576040805162461bcd60e51b815260206004820152601360248201527f616c726561647920696e697469616c697a656400000000000000000000000000604482015290519081900360640190fd5b600380546001600160a01b0319166001600160a01b0383169081179091556040517fb4dd887347efc97a6ad35fc9824a2ac4c0a6a04344d89bf05f3308c854325a6490600090a250565b600054600154600280546001600160a01b03938416939283169281169162ffffff600160a01b83041691600160b81b9004900b85565b6003546001600160a01b031681565b6003546000906001600160a01b03163314610280576040805162461bcd60e51b815260206004820152601c60248201527f6f6e6c7920666163746f72792063616e2063616c6c206465706c6f7900000000604482015290519081900360640190fd5b6040805160a0810182526001600160a01b03888116808352888216602080850182905292891684860181905262ffffff898116606080880182905260028b810b6080998a01819052600080546001600160a01b03199081169099179055600180548916881790558154909716851762ffffff60a01b1916600160a01b84021762ffffff60b81b1916600160b81b97820b909416969096029290921790945586518086019390935282870191909152818101929092528451808203909201825290920192839052815191012090610355906103c4565b8190604051809103906000f5905080158015610375573d6000803e3d6000fd5b50600080546001600160a01b0319908116909155600180549091169055600280547fffffffffffff00000000000000000000000000000000000000000000000000001690559695505050505050565b615c0e806103d28339019056fe6101406040523480156200001257600080fd5b506000336001600160a01b031663890357306040518163ffffffff1660e01b815260040160a06040518083038186803b1580156200004f57600080fd5b505afa15801562000064573d6000803e3d6000fd5b505050506040513d60a08110156200007b57600080fd5b50805160208083015160408401516060808601516080968701516001600160e81b031960e892831b1660e0526001600160601b031993831b841660c05293821b831660a05294901b16909352600283810b900b90911b61010052909150620000ee90829062002f5e62000106821b17901c565b60801b6001600160801b031916610120525062000174565b60008082600281900b620d89e719816200011c57fe5b05029050600083600281900b620d89e8816200013457fe5b0502905060008460020b83830360020b816200014c57fe5b0560010190508062ffffff166001600160801b038016816200016a57fe5b0495945050505050565b60805160601c60a05160601c60c05160601c60e05160e81c6101005160e81c6101205160801c6159b26200025c600039806121735280614ecd5280614f04525080610cb15280612be95280614f385280614f6a525080610da05280611b475280611b7e5280612c315280612e005280612e425280612e895280612ed05250806113145280611c01528061206852806123f85280612c0d52806141fd52508061091452806114425280611bd05280612002528061237252806140b45250806121f6528061221f52806128d552806128fe5280612aa35280612ad05280612af952506159b26000f3fe608060405234801561001057600080fd5b50600436106101c45760003560e01c806370cf754a116100f9578063cc7e7fa211610097578063ddca3f4311610071578063ddca3f4314610842578063f305839914610862578063f30dba931461086a578063f637731d146108ec576101c4565b8063cc7e7fa2146107f5578063d0c93a7c1461081b578063d21220a71461083a576101c4565b8063a34123a7116100d3578063a34123a71461072d578063a38807f214610767578063b0d0d211146107c2578063c45a0155146107ed576101c4565b806370cf754a146105e157806385b66729146105e9578063883bdbfd14610626576101c4565b80633c8a7d8d116101665780634f1eb3d8116101405780634f1eb3d81461050f578063514ea4bf146105605780635339c296146105b9578063540d4918146105d9576101c4565b80633c8a7d8d146103cb578063461413191461046b578063490e6cbc14610485576101c4565b80631ad8b03b116101a25780631ad8b03b146102be578063252c09d7146102f557806332148f671461034c5780633850c7bd1461036f576101c4565b80630dfe1681146101c9578063128acb08146101ed5780631a6865021461029a575b600080fd5b6101d1610912565b604080516001600160a01b039092168252519081900360200190f35b610281600480360360a081101561020357600080fd5b6001600160a01b0382358116926020810135151592604082013592606083013516919081019060a081016080820135600160201b81111561024357600080fd5b82018360208201111561025557600080fd5b803590602001918460018302840111600160201b8311171561027657600080fd5b509092509050610936565b6040805192835260208301919091528051918290030190f35b6102a2611633565b604080516001600160801b039092168252519081900360200190f35b6102c6611642565b60405180836001600160801b03168152602001826001600160801b031681526020019250505060405180910390f35b6103126004803603602081101561030b57600080fd5b503561165c565b6040805163ffffffff909516855260069390930b60208501526001600160a01b039091168383015215156060830152519081900360800190f35b61036d6004803603602081101561036257600080fd5b503561ffff166116a1565b005b610377611793565b604080516001600160a01b03909816885260029690960b602088015261ffff9485168787015292841660608701529216608085015263ffffffff90911660a0840152151560c0830152519081900360e00190f35b610281600480360360a08110156103e157600080fd5b6001600160a01b03823516916020810135600290810b92604083013590910b916001600160801b036060820135169181019060a081016080820135600160201b81111561042d57600080fd5b82018360208201111561043f57600080fd5b803590602001918460018302840111600160201b8311171561046057600080fd5b5090925090506117e8565b610473611aa6565b60408051918252519081900360200190f35b61036d6004803603608081101561049b57600080fd5b6001600160a01b038235169160208101359160408201359190810190608081016060820135600160201b8111156104d157600080fd5b8201836020820111156104e357600080fd5b803590602001918460018302840111600160201b8311171561050457600080fd5b509092509050611aac565b6102c6600480360360a081101561052557600080fd5b506001600160a01b03813516906020810135600290810b91604081013590910b906001600160801b0360608201358116916080013516611ef3565b61057d6004803603602081101561057657600080fd5b5035612111565b604080516001600160801b0396871681526020810195909552848101939093529084166060840152909216608082015290519081900360a00190f35b610473600480360360208110156105cf57600080fd5b503560010b61214e565b6101d1612160565b6102a2612171565b6102c6600480360360608110156105ff57600080fd5b506001600160a01b03813516906001600160801b0360208201358116916040013516612195565b6106946004803603602081101561063c57600080fd5b810190602081018135600160201b81111561065657600080fd5b82018360208201111561066857600080fd5b803590602001918460208302840111600160201b8311171561068957600080fd5b509092509050612490565b604051808060200180602001838103835285818151815260200191508051906020019060200280838360005b838110156106d85781810151838201526020016106c0565b50505050905001838103825284818151815260200191508051906020019060200280838360005b838110156107175781810151838201526020016106ff565b5050505090500194505050505060405180910390f35b6102816004803603606081101561074357600080fd5b508035600290810b91602081013590910b90604001356001600160801b0316612515565b6107916004803603604081101561077d57600080fd5b508035600290810b9160200135900b612691565b6040805160069490940b84526001600160a01b03909216602084015263ffffffff1682820152519081900360600190f35b61036d600480360360408110156107d857600080fd5b5063ffffffff81358116916020013516612879565b6101d1612aa1565b61036d6004803603602081101561080b57600080fd5b50356001600160a01b0316612ac5565b610823612be7565b6040805160029290920b8252519081900360200190f35b6101d1612c0b565b61084a612c2f565b6040805162ffffff9092168252519081900360200190f35b610473612c53565b61088a6004803603602081101561088057600080fd5b503560020b612c59565b604080516001600160801b039099168952600f9790970b602089015287870195909552606087019390935260069190910b60808601526001600160a01b031660a085015263ffffffff1660c0840152151560e083015251908190036101000190f35b61036d6004803603602081101561090257600080fd5b50356001600160a01b0316612cc5565b7f000000000000000000000000000000000000000000000000000000000000000081565b60008085610970576040805162461bcd60e51b8152602060048201526002602482015261415360f01b604482015290519081900360640190fd5b6040805160e0810182526000546001600160a01b0381168252600160a01b8104600290810b810b900b602083015261ffff600160b81b8204811693830193909352600160c81b810483166060830152600160d81b9004909116608082015260015463ffffffff811660a083015260ff600160201b90910416151560c08201819052610a28576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b87610a735780600001516001600160a01b0316866001600160a01b0316118015610a6e575073fffd8963efd1fc6a506488495d951d5263988d266001600160a01b038716105b610aa5565b80600001516001600160a01b0316866001600160a01b0316108015610aa557506401000276a36001600160a01b038716115b610adc576040805162461bcd60e51b815260206004820152600360248201526214d41360ea1b604482015290519081900360640190fd5b6001805460ff60201b191690556040805160c08101909152600090808a610b115760108460a0015163ffffffff16901c610b1b565b60a084015161ffff165b63ffffffff1681526005546001600160801b03166020820152604001610b3f612fca565b63ffffffff1681526000602082018190526040820181905260609091015262010008549091506001600160a01b031615610be557620100085460408083015181516310a537f160e11b815263ffffffff909116600482015290516001600160a01b039092169163214a6fe29160248082019260009290919082900301818387803b158015610bcc57600080fd5b505af1158015610be0573d6000803e3d6000fd5b505050505b6000808913905060006040518060e001604052808b81526020016000815260200185600001516001600160a01b03168152602001856020015160020b81526020018c610c3357600354610c37565b6002545b815260200160006001600160801b0316815260200184602001516001600160801b031681525090505b805115801590610c865750886001600160a01b031681604001516001600160a01b031614155b156110f557610c93615942565b60408201516001600160a01b031681526060820151610cd6906007907f00000000000000000000000000000000000000000000000000000000000000008f612fce565b15156040830152600290810b810b60208301819052620d89e719910b1215610d0757620d89e7196020820152610d26565b6020810151620d89e860029190910b1315610d2657620d89e860208201525b610d338160200151613110565b6001600160a01b031660608201526040820151610dc4908d610d6d578b6001600160a01b031683606001516001600160a01b031611610d87565b8b6001600160a01b031683606001516001600160a01b0316105b610d95578260600151610d97565b8b5b60c085015185517f0000000000000000000000000000000000000000000000000000000000000000613441565b60c085015260a084015260808301526001600160a01b031660408301528215610e2657610dfa8160c00151826080015101613633565b825103825260a0810151610e1c90610e1190613633565b602084015190613649565b6020830152610e61565b610e338160a00151613633565b825101825260c08101516080820151610e5b91610e509101613633565b602084015190613665565b60208301525b835163ffffffff1615610ec1576000612710610e94866000015163ffffffff168460c0015161367b90919063ffffffff16565b81610e9b57fe5b60c0840180519290910491829003905260a0840180519091016001600160801b03169052505b60c08201516001600160801b031615610f0057610ef48160c00151600160801b8460c001516001600160801b031661369f565b60808301805190910190525b80606001516001600160a01b031682604001516001600160a01b031614156110b45780604001511561108b578360a00151610f8a57610f68846040015160008760200151886040015188602001518a60600151600961374f909695949392919063ffffffff16565b6001600160a01b03166080860152600690810b900b6060850152600160a08501525b62010008546001600160a01b03161561101557620100085460208201516040805163a498463360e01b815260029290920b60048301528e15156024830152516001600160a01b039092169163a49846339160448082019260009290919082900301818387803b158015610ffc57600080fd5b505af1158015611010573d6000803e3d6000fd5b505050505b600061106182602001518e61102c57600254611032565b84608001515b8f611041578560800151611045565b6003545b608089015160608a015160408b015160069594939291906138e1565b90508c1561106d576000035b61107b8360c001518261399f565b6001600160801b031660c0840152505b8b61109a5780602001516110a3565b60018160200151035b600290810b900b60608301526110ef565b80600001516001600160a01b031682604001516001600160a01b0316146110ef576110e28260400151613a55565b600290810b900b60608301525b50610c60565b836020015160020b816060015160020b146111c35760008061114386604001518660400151886020015188602001518a606001518b608001516009613d7d909695949392919063ffffffff16565b604085015160608601516000805461ffff60c81b1916600160c81b61ffff958616021761ffff60b81b1916600160b81b95909416949094029290921762ffffff60a01b1916600160a01b62ffffff60029490940b9390931692909202919091176001600160a01b0319166001600160a01b03909116179055506111e89050565b6040810151600080546001600160a01b0319166001600160a01b039092169190911790555b8060c001516001600160801b031683602001516001600160801b03161461122e5760c0810151600580546001600160801b0319166001600160801b039092169190911790555b6000808c1561128857608083015160025560a08301516001600160801b03161561127c5760a0830151600480546001600160801b031981166001600160801b03918216909301169190911790555b8260a0015191506112d5565b608083015160035560a08301516001600160801b0316156112ce5760a0830151600480546001600160801b03808216600160801b92839004821690940116029190911790555b5060a08201515b8315158d1515146112ee57602083015183518d036112fb565b82600001518c0383602001515b90985096508c1561143457600087121561133d5761133d7f00000000000000000000000000000000000000000000000000000000000000008f89600003613f18565b6000611347614066565b9050336001600160a01b03166323a69e758a8a8e8e6040518563ffffffff1660e01b815260040180858152602001848152602001806020018281038252848482818152602001925080828437600081840152601f19601f82011690508083019250505095505050505050600060405180830381600087803b1580156113cb57600080fd5b505af11580156113df573d6000803e3d6000fd5b505050506113eb614066565b6113f5828b61419f565b111561142e576040805162461bcd60e51b815260206004820152600360248201526249494160e81b604482015290519081900360640190fd5b5061155e565b600088121561146b5761146b7f00000000000000000000000000000000000000000000000000000000000000008f8a600003613f18565b60006114756141af565b9050336001600160a01b03166323a69e758a8a8e8e6040518563ffffffff1660e01b815260040180858152602001848152602001806020018281038252848482818152602001925080828437600081840152601f19601f82011690508083019250505095505050505050600060405180830381600087803b1580156114f957600080fd5b505af115801561150d573d6000803e3d6000fd5b505050506115196141af565b611523828a61419f565b111561155c576040805162461bcd60e51b815260206004820152600360248201526249494160e81b604482015290519081900360640190fd5b505b8d6001600160a01b0316336001600160a01b03167f19b47279256b2a23a1665c810c8d55a1758940ee09377d4f8d26497a3577dc838a8a87604001518860c001518960600151898960405180888152602001878152602001866001600160a01b03168152602001856001600160801b031681526020018460020b8152602001836001600160801b03168152602001826001600160801b0316815260200197505050505050505060405180910390a350506001805460ff60201b1916600160201b17905550939a92995091975050505050505050565b6005546001600160801b031681565b6004546001600160801b0380821691600160801b90041682565b60098161ffff811061166d57600080fd5b015463ffffffff81169150600160201b810460060b90600160581b81046001600160a01b031690600160f81b900460ff1684565b600154600160201b900460ff166116e5576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b6001805460ff60201b1916905560008054600160d81b900461ffff169061170e60098385614247565b6000805461ffff808416600160d81b810261ffff60d81b199093169290921790925591925083161461177b576040805161ffff80851682528316602082015281517fac49e518f90a358f652e4400164f05a5d8f7e35e7747279bc3a93dbf584e125a929181900390910190a15b50506001805460ff60201b1916600160201b17905550565b6000546001546001600160a01b03821691600160a01b810460020b9161ffff600160b81b8304811692600160c81b8104821692600160d81b9091049091169063ffffffff81169060ff600160201b9091041687565b6001546000908190600160201b900460ff16611831576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b6001805460ff60201b191690556001600160801b03851661185157600080fd5b60008061189f60405180608001604052808c6001600160a01b031681526020018b60020b81526020018a60020b81526020016118958a6001600160801b03166142ea565b600f0b90526142fb565b925092505081935080925060008060008611156118c1576118be614066565b91505b84156118d2576118cf6141af565b90505b336001600160a01b03166399eee9d087878b8b6040518563ffffffff1660e01b815260040180858152602001848152602001806020018281038252848482818152602001925080828437600081840152601f19601f82011690508083019250505095505050505050600060405180830381600087803b15801561195457600080fd5b505af1158015611968573d6000803e3d6000fd5b5050505060008611156119bf5761197d614066565b611987838861419f565b11156119bf576040805162461bcd60e51b815260206004820152600260248201526104d360f41b604482015290519081900360640190fd5b8415611a0f576119cd6141af565b6119d7828761419f565b1115611a0f576040805162461bcd60e51b81526020600482015260026024820152614d3160f01b604482015290519081900360640190fd5b8960020b8b60020b8d6001600160a01b03167f7a53080ba414158be7ec69b987b5fb7d07dee101fe85488f0853ae16239d0bde338d8b8b60405180856001600160a01b03168152602001846001600160801b0316815260200183815260200182815260200194505050505060405180910390a450506001805460ff60201b1916600160201b17905550919890975095505050505050565b60035481565b600154600160201b900460ff16611af0576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b6001805460ff60201b191690556005546001600160801b031680611b3f576040805162461bcd60e51b81526020600482015260016024820152601360fa1b604482015290519081900360640190fd5b6000611b74867f000000000000000000000000000000000000000000000000000000000000000062ffffff16620f4240614531565b90506000611bab867f000000000000000000000000000000000000000000000000000000000000000062ffffff16620f4240614531565b90506000611bb7614066565b90506000611bc36141af565b90508815611bf657611bf67f00000000000000000000000000000000000000000000000000000000000000008b8b613f18565b8715611c2757611c277f00000000000000000000000000000000000000000000000000000000000000008b8a613f18565b336001600160a01b031663a1d4833685858a8a6040518563ffffffff1660e01b815260040180858152602001848152602001806020018281038252848482818152602001925080828437600081840152601f19601f82011690508083019250505095505050505050600060405180830381600087803b158015611ca957600080fd5b505af1158015611cbd573d6000803e3d6000fd5b505050506000611ccb614066565b90506000611cd76141af565b905081611ce4858861419f565b1115611d1c576040805162461bcd60e51b8152602060048201526002602482015261046360f41b604482015290519081900360640190fd5b80611d27848761419f565b1115611d5f576040805162461bcd60e51b8152602060048201526002602482015261463160f01b604482015290519081900360640190fd5b8382038382038115611de85760015461ffff1660008115611d8c5761271063ffffffff8316850204611d8f565b60005b90506001600160801b03811615611dc257600480546001600160801b038082168401166001600160801b03199091161790555b611ddc818503600160801b8d6001600160801b031661369f565b60028054909101905550505b8015611e6d5760015460101c61ffff1660008115611e125761271063ffffffff8316840204611e15565b60005b90506001600160801b03811615611e4757600480546001600160801b03600160801b8083048216850182160291161790555b611e61818403600160801b8d6001600160801b031661369f565b60038054909101905550505b8d6001600160a01b0316336001600160a01b03167fbdbdb71d7860376ba52b25a5028beea23581364a40522f6bcfb86bb1f2dca6338f8f86866040518085815260200184815260200183815260200182815260200194505050505060405180910390a350506001805460ff60201b1916600160201b179055505050505050505050505050565b6001546000908190600160201b900460ff16611f3c576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b6001805460ff60201b191690556000611f58600833898961456b565b60038101549091506001600160801b0390811690861611611f795784611f88565b60038101546001600160801b03165b60038201549093506001600160801b03600160801b909104811690851611611fb05783611fc6565b6003810154600160801b90046001600160801b03165b91506001600160801b0383161561202b576003810180546001600160801b031981166001600160801b0391821686900382161790915561202b907f0000000000000000000000000000000000000000000000000000000000000000908a908616613f18565b6001600160801b03821615612091576003810180546001600160801b03600160801b808304821686900382160291811691909117909155612091907f0000000000000000000000000000000000000000000000000000000000000000908a908516613f18565b604080516001600160a01b038a1681526001600160801b0380861660208301528416818301529051600288810b92908a900b9133917f70935338e69775456a85ddef226c395fb668b63fa0115f5f20610b388e6ca9c0919081900360600190a4506001805460ff60201b1916600160201b17905590969095509350505050565b60086020526000908152604090208054600182015460028301546003909301546001600160801b0392831693919281811691600160801b90041685565b60076020526000908152604090205481565b62010008546001600160a01b031681565b7f000000000000000000000000000000000000000000000000000000000000000081565b6001546000908190600160201b900460ff166121de576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b6001805460ff60201b19169055336001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001614806122ae57507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316638da5cb5b6040518163ffffffff1660e01b815260040160206040518083038186803b15801561227657600080fd5b505afa15801561228a573d6000803e3d6000fd5b505050506040513d60208110156122a057600080fd5b50516001600160a01b031633145b6122b757600080fd5b6004546001600160801b03908116908516116122d357836122e0565b6004546001600160801b03165b6004549092506001600160801b03600160801b909104811690841611612306578261231a565b600454600160801b90046001600160801b03165b90506001600160801b0382161561239b576004546001600160801b038381169116141561234957600019909101905b600480546001600160801b031981166001600160801b0391821685900382161790915561239b907f00000000000000000000000000000000000000000000000000000000000000009087908516613f18565b6001600160801b03811615612421576004546001600160801b03828116600160801b9092041614156123cc57600019015b600480546001600160801b03600160801b808304821685900382160291811691909117909155612421907f00000000000000000000000000000000000000000000000000000000000000009087908416613f18565b604080516001600160801b0380851682528316602082015281516001600160a01b0388169233927f596b573906218d3411850b26a6b437d6c4522fdb43d2d2386263f86d50b8b151929081900390910190a36001805460ff60201b1916600160201b1790559094909350915050565b60608061250a61249e612fca565b858580806020026020016040519081016040528093929190818152602001838360200280828437600092018290525054600554600996959450600160a01b820460020b935061ffff600160b81b8304811693506001600160801b0390911691600160c81b9004166145cf565b915091509250929050565b6001546000908190600160201b900460ff1661255e576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b6001805460ff60201b1916905560408051608081018252338152600287810b602083015286900b91810191909152600090819081906125ba90606081016125ad6001600160801b038a166142ea565b600003600f0b90526142fb565b92509250925081600003945080600003935060008511806125db5750600084115b1561261a576003830180546001600160801b038082168089018216600160801b93849004831689019092169092029091176001600160801b0319161790555b604080516001600160801b0388168152602081018790528082018690529051600289810b92908b900b9133917f0c396cd989a39f4459b5fa1aed6a9a8dcdbc45908acfd67e028cd568da98982c919081900360600190a450506001805460ff60201b1916600160201b179055509094909350915050565b60008060006126a08585614729565b600285810b810b600090815260066020819052604080832088850b90940b8352822060038401549182900b93600160381b83046001600160a01b0316928492600160d81b820463ffffffff16928492909190600160f81b900460ff168061270657600080fd5b6003820154600681900b9850600160381b81046001600160a01b03169650600160d81b810463ffffffff169450600160f81b900460ff168061274757600080fd5b50506040805160e0810182526000546001600160a01b0381168252600160a01b8104600290810b810b810b6020840181905261ffff600160b81b8404811695850195909552600160c81b830485166060850152600160d81b909204909316608083015260015463ffffffff811660a084015260ff600160201b90910416151560c08301529093508e820b910b121590506127ef57509390940396509003935090039050612872565b8a60020b816020015160020b121561286357600061280b612fca565b6020830151604084015160055460608601519394506000938493612841936009938893879392916001600160801b03169061374f565b9a9003989098039b505094909603929092039650909103039250612872915050565b50949093039650039350900390505b9250925092565b600154600160201b900460ff166128bd576040805162461bcd60e51b81526020600482015260036024820152624c4f4b60e81b604482015290519081900360640190fd5b6001805460ff60201b19169055336001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016148061298d57507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316638da5cb5b6040518163ffffffff1660e01b815260040160206040518083038186803b15801561295557600080fd5b505afa158015612969573d6000803e3d6000fd5b505050506040513d602081101561297f57600080fd5b50516001600160a01b031633145b61299657600080fd5b63ffffffff821615806129c457506103e88263ffffffff16101580156129c45750610fa08263ffffffff1611155b80156129f9575063ffffffff811615806129f957506103e88163ffffffff16101580156129f95750610fa08163ffffffff1611155b612a0257600080fd5b6001805465ffffffff0000601084901b16840163ffffffff90811663ffffffff19831617909255167fb3159fed3ddfba67bae294599eafe2d0ec98c08bb38e0e5fb87d33154b6e05aa62010000826040805163ffffffff939092068316825261ffff601086901c16602083015286831682820152918516606082015290519081900360800190a150506001805460ff60201b1916600160201b17905550565b7f000000000000000000000000000000000000000000000000000000000000000081565b336001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000161480612b8857507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316638da5cb5b6040518163ffffffff1660e01b815260040160206040518083038186803b158015612b5057600080fd5b505afa158015612b64573d6000803e3d6000fd5b505050506040513d6020811015612b7a57600080fd5b50516001600160a01b031633145b612b9157600080fd5b6201000880546001600160a01b0383166001600160a01b0319909116811790915560408051918252517f29983690a85a11696ce8a357993744f8d5a74fde14653e517cc2f8608a7235e99181900360200190a150565b7f000000000000000000000000000000000000000000000000000000000000000081565b7f000000000000000000000000000000000000000000000000000000000000000081565b7f000000000000000000000000000000000000000000000000000000000000000081565b60025481565b60066020819052600091825260409091208054600182015460028301546003909301546001600160801b03831694600160801b909304600f0b93919281900b90600160381b81046001600160a01b031690600160d81b810463ffffffff1690600160f81b900460ff1688565b6000546001600160a01b031615612d08576040805162461bcd60e51b8152602060048201526002602482015261414960f01b604482015290519081900360640190fd5b6000612d1382613a55565b9050600080612d2b612d23612fca565b6009906147f2565b6040805160e0810182526001600160a01b038816808252600288810b60208401819052600094840185905261ffff8781166060860181905290871660808601819052630c800c8060a08701819052600160c090970187905287546001600160a01b03191690951762ffffff60a01b1916600160a01b62ffffff9490950b8416949094029390931763ffffffff60b81b1916600160c81b9091021761ffff60d81b1916600160d81b909202919091179093558154600160201b63ffffffff1990911690911760ff60201b191617905591935091507f00000000000000000000000000000000000000000000000000000000000000001660641415612e40576001805463ffffffff1916630ce40ce4179055612f11565b7f000000000000000000000000000000000000000000000000000000000000000062ffffff166101f41415612e87576001805463ffffffff1916630d480d48179055612f11565b7f000000000000000000000000000000000000000000000000000000000000000062ffffff166109c41415612ece576001805463ffffffff1916630c800c80179055612f11565b7f000000000000000000000000000000000000000000000000000000000000000062ffffff166127101415612f11576001805463ffffffff1916630c800c801790555b604080516001600160a01b0386168152600285900b602082015281517f98636036cb66a9c19a37435efc1e90142190214e8abeb821bdba3f2990dd4c95929181900390910190a150505050565b60008082600281900b620d89e71981612f7357fe5b05029050600083600281900b620d89e881612f8a57fe5b0502905060008460020b83830360020b81612fa157fe5b0560010190508062ffffff166001600160801b03801681612fbe57fe5b0493505050505b919050565b4290565b60008060008460020b8660020b81612fe257fe5b05905060008660020b12801561300957508460020b8660020b8161300257fe5b0760020b15155b1561301357600019015b8315613088576000806130258361483e565b600182810b810b600090815260208d9052604090205460ff83169190911b8001600019019081168015159750929450909250908561306a57888360ff1686030261307d565b8861307482614850565b840360ff168603025b965050505050613106565b6000806130978360010161483e565b91509150600060018260ff166001901b031990506000818b60008660010b60010b81526020019081526020016000205416905080600014159550856130e957888360ff0360ff168660010101026130ff565b88836130f4836148ef565b0360ff168660010101025b9650505050505b5094509492505050565b60008060008360020b12613127578260020b61312f565b8260020b6000035b9050620d89e881111561316d576040805162461bcd60e51b81526020600482015260016024820152601560fa1b604482015290519081900360640190fd5b60006001821661318157600160801b613193565b6ffffcb933bd6fad37aa2d162d1a5940015b70ffffffffffffffffffffffffffffffffff16905060028216156131c7576ffff97272373d413259a46990580e213a0260801c5b60048216156131e6576ffff2e50f5f656932ef12357cf3c7fdcc0260801c5b6008821615613205576fffe5caca7e10e4e61c3624eaa0941cd00260801c5b6010821615613224576fffcb9843d60f6159c9db58835c9266440260801c5b6020821615613243576fff973b41fa98c081472e6896dfb254c00260801c5b6040821615613262576fff2ea16466c96a3843ec78b326b528610260801c5b6080821615613281576ffe5dee046a99a2a811c461f1969c30530260801c5b6101008216156132a1576ffcbe86c7900a88aedcffc83b479aa3a40260801c5b6102008216156132c1576ff987a7253ac413176f2b074cf7815e540260801c5b6104008216156132e1576ff3392b0822b70005940c7a398e4b70f30260801c5b610800821615613301576fe7159475a2c29b7443b29c7fa6e889d90260801c5b611000821615613321576fd097f3bdfd2022b8845ad8f792aa58250260801c5b612000821615613341576fa9f746462d870fdf8a65dc1f90e061e50260801c5b614000821615613361576f70d869a156d2a1b890bb3df62baf32f70260801c5b618000821615613381576f31be135f97d08fd981231505542fcfa60260801c5b620100008216156133a2576f09aa508b5b7a84e1c677de54f3e99bc90260801c5b620200008216156133c2576e5d6af8dedb81196699c329225ee6040260801c5b620400008216156133e1576d2216e584f5fa1ea926041bedfe980260801c5b620800008216156133fe576b048a170391f7dc42444e8fa20260801c5b60008460020b131561341957806000198161341557fe5b0490505b600160201b81061561342c57600161342f565b60005b60ff16602082901c0192505050919050565b60008080806001600160a01b03808916908a1610158187128015906134c657600061347a8989620f42400362ffffff16620f424061369f565b9050826134935761348e8c8c8c60016149d9565b6134a0565b6134a08b8d8c6001614a54565b95508581106134b1578a96506134c0565b6134bd8c8b8386614b08565b96505b50613510565b816134dd576134d88b8b8b6000614a54565b6134ea565b6134ea8a8c8b60006149d9565b93508388600003106134fe57899550613510565b61350d8b8a8a60000385614b54565b95505b6001600160a01b038a81169087161482156135735780801561352f5750815b61354557613540878d8c6001614a54565b613547565b855b9550808015613554575081155b61356a57613565878d8c60006149d9565b61356c565b845b94506135bd565b80801561357d5750815b6135935761358e8c888c60016149d9565b613595565b855b95508080156135a2575081155b6135b8576135b38c888c6000614a54565b6135ba565b845b94505b811580156135cd57508860000385115b156135d9578860000394505b8180156135f857508a6001600160a01b0316876001600160a01b031614155b15613607578589039350613624565b613621868962ffffff168a620f42400362ffffff16614531565b93505b50505095509550955095915050565b6000600160ff1b821061364557600080fd5b5090565b8082038281131560008312151461365f57600080fd5b92915050565b8181018281121560008312151461365f57600080fd5b60008215806136965750508181028183828161369357fe5b04145b61365f57600080fd5b60008080600019858709868602925082811090839003039050806136d557600084116136ca57600080fd5b508290049050613748565b8084116136e157600080fd5b6000848688096000868103871696879004966002600389028118808a02820302808a02820302808a02820302808a02820302808a02820302808a02909103029181900381900460010186841190950394909402919094039290920491909117919091029150505b9392505050565b60008063ffffffff87166137f5576000898661ffff1661ffff811061377057fe5b60408051608081018252919092015463ffffffff808216808452600160201b8304600690810b810b900b6020850152600160581b83046001600160a01b031694840194909452600160f81b90910460ff16151560608301529092508a16146137e1576137de818a8988614ba0565b90505b8060200151816040015192509250506138d5565b86880360008061380a8c8c858c8c8c8c614c43565b91509150816000015163ffffffff168363ffffffff16141561383c5781602001518260400151945094505050506138d5565b805163ffffffff848116911614156138645780602001518160400151945094505050506138d5565b8151815160208085015190840151918390039286039163ffffffff80841692908516910360060b8161389257fe5b05028460200151018263ffffffff168263ffffffff1686604001518660400151036001600160a01b031602816138c457fe5b048560400151019650965050505050505b97509795505050505050565b600295860b860b60009081526020979097526040909620600181018054909503909455938301805490920390915560038201805463ffffffff600160d81b6001600160a01b03600160381b808504821690960316909402670100000000000000600160d81b031990921691909117600681810b90960390950b66ffffffffffffff1666ffffffffffffff199095169490941782810485169095039093160263ffffffff60d81b1990931692909217905554600160801b9004600f0b90565b60008082600f0b1215613a0457826001600160801b03168260000384039150816001600160801b0316106139ff576040805162461bcd60e51b81526020600482015260026024820152614c5360f01b604482015290519081900360640190fd5b61365f565b826001600160801b03168284019150816001600160801b0316101561365f576040805162461bcd60e51b81526020600482015260026024820152614c4160f01b604482015290519081900360640190fd5b60006401000276a36001600160a01b03831610801590613a91575073fffd8963efd1fc6a506488495d951d5263988d266001600160a01b038316105b613ac6576040805162461bcd60e51b81526020600482015260016024820152602960f91b604482015290519081900360640190fd5b77ffffffffffffffffffffffffffffffffffffffff00000000602083901b166001600160801b03811160071b81811c67ffffffffffffffff811160061b90811c63ffffffff811160051b90811c61ffff811160041b90811c60ff8111600390811b91821c600f811160021b90811c918211600190811b92831c97908811961790941790921717909117171760808110613b6757607f810383901c9150613b71565b80607f0383901b91505b908002607f81811c60ff83811c9190911c800280831c81831c1c800280841c81841c1c800280851c81851c1c800280861c81861c1c800280871c81871c1c800280881c81881c1c800280891c81891c1c8002808a1c818a1c1c8002808b1c818b1c1c8002808c1c818c1c1c8002808d1c818d1c1c8002808e1c9c81901c9c909c1c80029c8d901c9e9d607f198f0160401b60c09190911c678000000000000000161760c19b909b1c674000000000000000169a909a1760c29990991c672000000000000000169890981760c39790971c671000000000000000169690961760c49590951c670800000000000000169490941760c59390931c670400000000000000169290921760c69190911c670200000000000000161760c79190911c600160381b161760c89190911c6680000000000000161760c99190911c6640000000000000161760ca9190911c6620000000000000161760cb9190911c6610000000000000161760cc9190911c6608000000000000161760cd9190911c66040000000000001617693627a301d71055774c8581026f028f6481ab7f045a5af012a19d003aa9198101608090811d906fdb2df09e81959a81455e260799a0632f8301901d600281810b9083900b14613d6e57886001600160a01b0316613d5282613110565b6001600160a01b03161115613d675781613d69565b805b613d70565b815b9998505050505050505050565b6000806000898961ffff1661ffff8110613d9357fe5b60408051608081018252919092015463ffffffff808216808452600160201b8304600690810b810b900b6020850152600160581b83046001600160a01b031694840194909452600160f81b90910460ff161515606083015290925089161415613e0257888592509250506138d5565b8461ffff168461ffff16118015613e2357506001850361ffff168961ffff16145b15613e3057839150613e34565b8491505b8161ffff168960010161ffff1681613e4857fe5b069250613e5781898989614ba0565b8a8461ffff1661ffff8110613e6857fe5b825191018054602084015160408501516060909501511515600160f81b026001600160f81b036001600160a01b03909616600160581b027fff0000000000000000000000000000000000000000ffffffffffffffffffffff60069390930b66ffffffffffffff16600160201b026affffffffffffff000000001963ffffffff90971663ffffffff199095169490941795909516929092171692909217929092161790555097509795505050505050565b604080516001600160a01b038481166024830152604480830185905283518084039091018152606490920183526020820180516001600160e01b031663a9059cbb60e01b1781529251825160009485949389169392918291908083835b60208310613f945780518252601f199092019160209182019101613f75565b6001836020036101000a0380198251168184511680821785525050505050509050019150506000604051808303816000865af19150503d8060008114613ff6576040519150601f19603f3d011682016040523d82523d6000602084013e613ffb565b606091505b5091509150818015614029575080511580614029575080806020019051602081101561402657600080fd5b50515b61405f576040805162461bcd60e51b81526020600482015260026024820152612a2360f11b604482015290519081900360640190fd5b5050505050565b604080513060248083019190915282518083039091018152604490910182526020810180516001600160e01b03166370a0823160e01b17815291518151600093849384936001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001693919290918291908083835b602083106140ff5780518252601f1990920191602091820191016140e0565b6001836020036101000a038019825116818451168082178552505050505050905001915050600060405180830381855afa9150503d806000811461415f576040519150601f19603f3d011682016040523d82523d6000602084013e614164565b606091505b509150915081801561417857506020815110155b61418157600080fd5b80806020019051602081101561419657600080fd5b50519250505090565b8082018281101561365f57600080fd5b604080513060248083019190915282518083039091018152604490910182526020810180516001600160e01b03166370a0823160e01b17815291518151600093849384936001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000169391929091829190808383602083106140ff5780518252601f1990920191602091820191016140e0565b6000808361ffff1611614285576040805162461bcd60e51b81526020600482015260016024820152604960f81b604482015290519081900360640190fd5b8261ffff168261ffff161161429b575081613748565b825b8261ffff168161ffff1610156142e1576001858261ffff1661ffff81106142c057fe5b01805463ffffffff191663ffffffff9290921691909117905560010161429d565b50909392505050565b80600f81900b8114612fc557600080fd5b600080600061431284602001518560400151614729565b6040805160e0810182526000546001600160a01b0381168252600160a01b8104600290810b810b900b602080840182905261ffff600160b81b8404811685870152600160c81b84048116606080870191909152600160d81b90940416608085015260015463ffffffff811660a086015260ff600160201b90910416151560c0850152885190890151948901519289015193946143b49491939092909190614e3d565b93508460600151600f0b60001461452957846020015160020b816020015160020b1215614409576144026143eb8660200151613110565b6143f88760400151613110565b8760600151614ff2565b9250614529565b846040015160020b816020015160020b12156144ff5760055460408201516001600160801b039091169061445b9061443f612fca565b6020850151606086015160808701516009949392918791613d7d565b6000805461ffff60c81b1916600160c81b61ffff938416021761ffff60b81b1916600160b81b9390921692909202179055815160408701516144ab91906144a190613110565b8860600151614ff2565b93506144c96144bd8760200151613110565b83516060890151615036565b92506144d981876060015161399f565b600580546001600160801b0319166001600160801b039290921691909117905550614529565b61452661450f8660200151613110565b61451c8760400151613110565b8760600151615036565b91505b509193909250565b600061453e84848461369f565b90506000828061454a57fe5b848609111561374857600019811061456157600080fd5b6001019392505050565b6040805160609490941b6bffffffffffffffffffffffff1916602080860191909152600293840b60e890811b60348701529290930b90911b60378401528051808403601a018152603a90930181528251928201929092206000908152929052902090565b60608060008361ffff161161460f576040805162461bcd60e51b81526020600482015260016024820152604960f81b604482015290519081900360640190fd5b865167ffffffffffffffff8111801561462757600080fd5b50604051908082528060200260200182016040528015614651578160200160208202803683370190505b509150865167ffffffffffffffff8111801561466c57600080fd5b50604051908082528060200260200182016040528015614696578160200160208202803683370190505b50905060005b875181101561471c576146c78a8a8a84815181106146b657fe5b60200260200101518a8a8a8a61374f565b8483815181106146d357fe5b602002602001018484815181106146e657fe5b60200260200101826001600160a01b03166001600160a01b03168152508260060b60060b8152505050808060010191505061469c565b5097509795505050505050565b8060020b8260020b12614769576040805162461bcd60e51b8152602060048201526003602482015262544c5560e81b604482015290519081900360640190fd5b620d89e719600283900b12156147ac576040805162461bcd60e51b8152602060048201526003602482015262544c4d60e81b604482015290519081900360640190fd5b620d89e8600282900b13156147ee576040805162461bcd60e51b815260206004820152600360248201526254554d60e81b604482015290519081900360640190fd5b5050565b6040805160808101825263ffffffff9283168082526000602083018190529282019290925260016060909101819052835463ffffffff1916909117909116600160f81b17909155908190565b60020b600881901d9161010090910790565b600080821161485e57600080fd5b600160801b821061487157608091821c91015b68010000000000000000821061488957604091821c91015b600160201b821061489c57602091821c91015b6201000082106148ae57601091821c91015b61010082106148bf57600891821c91015b601082106148cf57600491821c91015b600482106148df57600291821c91015b60028210612fc557600101919050565b60008082116148fd57600080fd5b5060ff6001600160801b0382161561491857607f1901614920565b608082901c91505b67ffffffffffffffff82161561493957603f1901614941565b604082901c91505b63ffffffff82161561495657601f190161495e565b602082901c91505b61ffff82161561497157600f1901614979565b601082901c91505b60ff82161561498b5760071901614993565b600882901c91505b600f8216156149a557600319016149ad565b600482901c91505b60038216156149bf57600119016149c7565b600282901c91505b6001821615612fc55760001901919050565b6000836001600160a01b0316856001600160a01b031611156149f9579293925b81614a2657614a21836001600160801b03168686036001600160a01b0316600160601b61369f565b614a49565b614a49836001600160801b03168686036001600160a01b0316600160601b614531565b90505b949350505050565b6000836001600160a01b0316856001600160a01b03161115614a74579293925b6fffffffffffffffffffffffffffffffff60601b606084901b166001600160a01b038686038116908716614aa757600080fd5b83614ad757866001600160a01b0316614aca8383896001600160a01b031661369f565b81614ad157fe5b04614afd565b614afd614aee8383896001600160a01b0316614531565b886001600160a01b0316615065565b979650505050505050565b600080856001600160a01b031611614b1f57600080fd5b6000846001600160801b031611614b3557600080fd5b81614b4757614a218585856001615070565b614a498585856001615151565b600080856001600160a01b031611614b6b57600080fd5b6000846001600160801b031611614b8157600080fd5b81614b9357614a218585856000615151565b614a498585856000615070565b614ba861597e565b600085600001518503905060405180608001604052808663ffffffff1681526020018263ffffffff168660020b0288602001510160060b81526020016000856001600160801b031611614bfc576001614bfe565b845b6001600160801b031663ffffffff60801b608085901b1681614c1c57fe5b048860400151016001600160a01b0316815260200160011515815250915050949350505050565b614c4b61597e565b614c5361597e565b888561ffff1661ffff8110614c6457fe5b60408051608081018252919092015463ffffffff8116808352600160201b8204600690810b810b900b6020840152600160581b82046001600160a01b031693830193909352600160f81b900460ff16151560608201529250614cc89089908961523d565b15614d00578663ffffffff16826000015163ffffffff161415614cea576138d5565b81614cf783898988614ba0565b915091506138d5565b888361ffff168660010161ffff1681614d1557fe5b0661ffff1661ffff8110614d2557fe5b60408051608081018252929091015463ffffffff81168352600160201b8104600690810b810b900b60208401526001600160a01b03600160581b8204169183019190915260ff600160f81b90910416151560608201819052909250614dda57604080516080810182528a5463ffffffff81168252600160201b8104600690810b810b900b6020830152600160581b81046001600160a01b031692820192909252600160f81b90910460ff161515606082015291505b614de98883600001518961523d565b614e20576040805162461bcd60e51b815260206004820152600360248201526213d31160ea1b604482015290519081900360640190fd5b614e2d89898988876152fe565b9150915097509795505050505050565b6000614e4c600887878761456b565b60025460035491925090600080600f87900b15614f92576000614e6d612fca565b6000805460055492935090918291614eb79160099186918591600160a01b810460020b9161ffff600160b81b83048116926001600160801b0390921691600160c81b90041661374f565b9092509050614ef160068d8b8d8b8b87898b60007f000000000000000000000000000000000000000000000000000000000000000061549c565b9450614f2860068c8b8d8b8b87898b60017f000000000000000000000000000000000000000000000000000000000000000061549c565b93508415614f5c57614f5c60078d7f0000000000000000000000000000000000000000000000000000000000000000615659565b8315614f8e57614f8e60078c7f0000000000000000000000000000000000000000000000000000000000000000615659565b5050505b600080614fa460068c8c8b8a8a6156bf565b9092509050614fb5878a848461576b565b600089600f0b1215614fe3578315614fd257614fd260068c615900565b8215614fe357614fe360068b615900565b50505050505095945050505050565b60008082600f0b126150185761501361500e8585856001614a54565b613633565b614a4c565b61502b61500e8585856000036000614a54565b600003949350505050565b60008082600f0b126150525761501361500e85858560016149d9565b61502b61500e85858560000360006149d9565b808204910615150190565b600081156150e35760006001600160a01b038411156150a6576150a184600160601b876001600160801b031661369f565b6150be565b6001600160801b038516606085901b816150bc57fe5b045b90506150db6150d66001600160a01b0388168361419f565b61592c565b915050614a4c565b60006001600160a01b038411156151115761510c84600160601b876001600160801b0316614531565b615128565b615128606085901b6001600160801b038716615065565b905080866001600160a01b03161161513f57600080fd5b6001600160a01b038616039050614a4c565b60008261515f575083614a4c565b6fffffffffffffffffffffffffffffffff60601b606085901b1682156151f6576001600160a01b0386168481029085828161519657fe5b0414156151c7578181018281106151c5576151bb83896001600160a01b031683614531565b9350505050614a4c565b505b6151ed826151e8878a6001600160a01b031686816151e157fe5b049061419f565b615065565b92505050614a4c565b6001600160a01b0386168481029085828161520d57fe5b0414801561521a57508082115b61522357600080fd5b8082036151bb6150d6846001600160a01b038b1684614531565b60008363ffffffff168363ffffffff161115801561526757508363ffffffff168263ffffffff1611155b15615283578163ffffffff168363ffffffff1611159050613748565b60008463ffffffff168463ffffffff16116152aa578363ffffffff16600160201b016152b2565b8363ffffffff165b64ffffffffff16905060008563ffffffff168463ffffffff16116152e2578363ffffffff16600160201b016152ea565b8363ffffffff165b64ffffffffff169091111595945050505050565b61530661597e565b61530e61597e565b60008361ffff168560010161ffff168161532457fe5b0661ffff169050600060018561ffff16830103905060005b506002818301048961ffff8716828161535157fe5b0661ffff811061535d57fe5b60408051608081018252929091015463ffffffff81168352600160201b8104600690810b810b900b60208401526001600160a01b03600160581b8204169183019190915260ff600160f81b909104161515606082018190529095506153c75780600101925061533c565b898661ffff1682600101816153d857fe5b0661ffff81106153e457fe5b60408051608081018252929091015463ffffffff81168352600160201b8104600690810b810b900b60208401526001600160a01b03600160581b8204169183019190915260ff600160f81b9091041615156060820152855190945060009061544e908b908b61523d565b905080801561546757506154678a8a876000015161523d565b15615472575061548f565b8061548257600182039250615489565b8160010193505b5061533c565b5050509550959350505050565b60028a810b900b600090815260208c90526040812080546001600160801b0316826154c7828d61399f565b9050846001600160801b0316816001600160801b03161115615515576040805162461bcd60e51b81526020600482015260026024820152614c4f60f01b604482015290519081900360640190fd5b6001600160801b0382811615908216158114159450156155be578c60020b8e60020b136155a657600183018b9055600283018a9055600383018054670100000000000000600160d81b031916600160381b6001600160a01b038c16021766ffffffffffffff191666ffffffffffffff60068b900b161763ffffffff60d81b1916600160d81b63ffffffff8a16021790555b6003830180546001600160f81b0316600160f81b1790555b82546001600160801b0319166001600160801b03821617835585615607578254615602906155fd90600160801b9004600f90810b810b908f900b613665565b6142ea565b615628565b8254615628906155fd90600160801b9004600f90810b810b908f900b613649565b8354600f9190910b6001600160801b03908116600160801b0291161790925550909c9b505050505050505050505050565b8060020b8260020b8161566857fe5b0760020b1561567657600080fd5b6000806156918360020b8560020b8161568b57fe5b0561483e565b600191820b820b60009081526020979097526040909620805460ff9097169190911b90951890945550505050565b600285810b80820b60009081526020899052604080822088850b850b83529082209193849391929184918291908a900b1261570557505060018201546002830154615718565b8360010154880391508360020154870390505b6000808b60020b8b60020b121561573a5750506001830154600284015461574d565b84600101548a0391508460020154890390505b92909803979097039b96909503949094039850939650505050505050565b6040805160a08101825285546001600160801b0390811682526001870154602083015260028701549282019290925260038601548083166060830152600160801b900490911660808201526000600f85900b61580a5781516001600160801b0316615802576040805162461bcd60e51b815260206004820152600260248201526104e560f41b604482015290519081900360640190fd5b508051615819565b8151615816908661399f565b90505b600061583d8360200151860384600001516001600160801b0316600160801b61369f565b905060006158638460400151860385600001516001600160801b0316600160801b61369f565b905086600f0b60001461588a5787546001600160801b0319166001600160801b0384161788555b60018801869055600288018590556001600160801b0382161515806158b857506000816001600160801b0316115b156158f6576003880180546001600160801b031981166001600160801b039182168501821617808216600160801b9182900483168501909216021790555b5050505050505050565b600290810b810b6000908152602092909252604082208281556001810183905590810182905560030155565b806001600160a01b0381168114612fc557600080fd5b6040805160e081018252600080825260208201819052918101829052606081018290526080810182905260a0810182905260c081019190915290565b6040805160808101825260008082526020820181905291810182905260608101919091529056fea164736f6c6343000706000aa164736f6c6343000706000a