Verified contractPartialUNLICENSED
UniV3PathLib
Address0xbd0913dcaaf7a68738172e8582b03fb01202da71
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.34
- Same code
Only this contract
Details
- Compiler
- solc 0.8.34+commit.80d5c536
- Language
- solidity
- Optimization
- Yes, 120 runs
- EVM version
- cancun
- Via IR
- yes
- License
- UNLICENSED
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,434,708
- Creation transaction
- 0x88213b93cc…82eb4417f6
12 files
- project/contracts-uni-router/libraries/UniV3PathLib.solmain14.33 KB
- npm/@openzeppelin/[email protected]/utils/Panic.sol888 B
- npm/@openzeppelin/[email protected]/utils/math/Math.sol15.42 KB
- npm/@openzeppelin/[email protected]/utils/math/SafeCast.sol15.28 KB
- project/contracts-uni-router/libraries/uniswap/BitMath.sol2.07 KB
- project/contracts-uni-router/libraries/uniswap/FixedPoint96.sol207 B
- project/contracts-uni-router/libraries/uniswap/FullMath.sol502 B
- project/contracts-uni-router/libraries/uniswap/SqrtPriceMath.sol6 KB
- project/contracts-uni-router/libraries/uniswap/SwapMath.sol3.5 KB
- project/contracts-uni-router/libraries/uniswap/TickBitmap.sol2.33 KB
- project/contracts-uni-router/libraries/uniswap/UnsafeMath.sol279 B
- project/contracts/libraries/V3TickMath.sol2.97 KB
project/contracts-uni-router/libraries/UniV3PathLib.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.34;
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
import {V3TickMath} from "../../contracts/libraries/V3TickMath.sol";
import {FixedPoint96} from "./uniswap/FixedPoint96.sol";
import {SqrtPriceMath} from "./uniswap/SqrtPriceMath.sol";
import {SwapMath} from "./uniswap/SwapMath.sol";
import {TickBitmap} from "./uniswap/TickBitmap.sol";
library UniV3PathLib {
uint256 internal constant MAX_VENUES = 8;
uint256 internal constant MAX_TIERS = 14;
uint256 internal constant WORDS_PER_VENUE = 3;
uint256 internal constant FLAG_PAUSE_ALL = 1;
uint256 internal constant FLAG_PAUSE_PATH = 2;
uint256 internal constant FLAG_CHALLENGERS_OFF = 4;
uint256 internal constant FLAGS_SHIFT = 48;
uint256 internal constant TIER_COUNTS_SHIFT = 64;
uint256 internal constant MODE_EXACT_OUT = 0x100;
uint256 internal constant MODE_NEED_AMOUNT = 0x200;
uint256 internal constant MODE_QUOTE = 0x400;
uint160 internal constant MIN_LIMIT = 4295128740;
uint160 internal constant MAX_LIMIT = 1461446703485210103287273052203988822378723970341;
uint256 private constant PIPS = 1e6;
uint256 private constant BOUND = 0;
uint256 private constant FILLED = 1;
uint256 private constant DRY = 2;
bytes4 private constant SLOT0 = 0x3850c7bd;
bytes4 private constant LIQUIDITY = 0x1a686502;
bytes4 private constant TICK_SPACING = 0xd0c93a7c;
bytes4 private constant TICKS = 0xf30dba93;
struct Config {
uint256 cfg;
uint256[] words;
address deployer;
bytes32 initHash;
}
struct Pick {
address pool;
uint256 amount;
uint256 floor;
uint160 sqrtAfter;
uint256 crossed;
uint256 steps;
}
struct Walk {
address pool;
uint24 fee;
bool zf1;
bool exactOut;
uint160 limit;
uint160 sqrtP;
int24 tick;
int24 spacing;
uint128 liq;
uint256 rem;
uint256 acc;
uint256 steps;
uint256 crossed;
}
error HopUnfillable(uint256 hop);
error CallerPoolMissing(uint256 hop);
error PoolReadFailed(address pool);
error PathPairRepeated();
error InvalidPath();
error Paused();
function challengers(uint256 cfg) internal pure returns (uint256 k) {
if ((cfg >> FLAGS_SHIFT) & FLAG_CHALLENGERS_OFF == 0) k = uint8(cfg >> 8);
}
function requireLive(uint256 cfg) internal pure {
if ((cfg >> FLAGS_SHIFT) & (FLAG_PAUSE_ALL | FLAG_PAUSE_PATH) != 0) revert Paused();
}
function parse(bytes calldata path, bool reversed)
internal
pure
returns (address[] memory tokens, uint24[] memory fees)
{
uint256 len = path.length;
if (len < 43 || (len - 20) % 23 != 0) revert InvalidPath();
uint256 hops = (len - 20) / 23;
tokens = new address[](hops + 1);
fees = new uint24[](hops);
for (uint256 i; i <= hops; ++i) {
uint256 at = i * 23;
tokens[reversed ? hops - i : i] = address(bytes20(path[at:at + 20]));
if (i < hops) fees[reversed ? hops - 1 - i : i] = uint24(bytes3(path[at + 20:at + 23]));
}
for (uint256 i = 1; i < hops; ++i) {
for (uint256 j; j < i; ++j) {
if (
(tokens[i] == tokens[j] && tokens[i + 1] == tokens[j + 1])
|| (tokens[i] == tokens[j + 1] && tokens[i + 1] == tokens[j])
) revert PathPairRepeated();
}
}
}
function derive(address deployer, bytes32 initHash, address t0, address t1, uint24 fee)
internal
pure
returns (address pool)
{
pool = address(
uint160(uint256(keccak256(abi.encodePacked(hex"ff", deployer, keccak256(abi.encode(t0, t1, fee)), initHash))))
);
}
function callerPool(Config memory c, uint256 hop, address tokenIn, address tokenOut, uint24 fee)
internal
view
returns (address pool)
{
(address t0, address t1) = tokenIn < tokenOut ? (tokenIn, tokenOut) : (tokenOut, tokenIn);
pool = derive(c.deployer, c.initHash, t0, t1, fee);
if (pool.code.length == 0) revert CallerPoolMissing(hop);
}
function select(
uint256 cfg,
uint256[] memory words,
address deployer,
bytes32 initHash,
uint256 mode,
address tokenIn,
address tokenOut,
uint24 fee,
uint256 amount
)
public
view
returns (address pool, uint256 amountOut, uint256 floor, uint160 sqrtAfter, uint256 crossed, uint256 steps)
{
Pick memory p = _select(Config(cfg, words, deployer, initHash), mode, tokenIn, tokenOut, fee, amount);
return (p.pool, p.amount, p.floor, p.sqrtAfter, p.crossed, p.steps);
}
function selectLimited(
uint256 cfg,
address deployer,
bytes32 initHash,
address tokenIn,
address tokenOut,
uint24 fee,
uint256 amount,
bool exactOut,
uint160 limit
) public view returns (uint256 amountOut, uint160 sqrtAfter, uint256 crossed, uint256 steps) {
bool zf1 = tokenIn < tokenOut;
Config memory c = Config(cfg, new uint256[](0), deployer, initHash);
Walk memory w = _open(callerPool(c, 0, tokenIn, tokenOut, fee), fee, zf1, exactOut, amount, limit);
require(
zf1
? limit < w.sqrtP && limit > V3TickMath.MIN_SQRT_RATIO
: limit > w.sqrtP && limit < V3TickMath.MAX_SQRT_RATIO,
"SPL"
);
uint256 st = _walk(w, uint16(cfg >> 32));
_noProgress(w, amount);
if (st == BOUND) revert HopUnfillable(0);
return (w.acc, w.sqrtP, w.crossed, w.steps);
}
function _select(
Config memory c,
uint256 mode,
address tokenIn,
address tokenOut,
uint24 fee,
uint256 amount
) private view returns (Pick memory p) {
uint256 hop = uint8(mode);
bool exactOut = mode & MODE_EXACT_OUT != 0;
bool needAmount = mode & MODE_NEED_AMOUNT != 0;
bool zf1 = tokenIn < tokenOut;
p.pool = callerPool(c, hop, tokenIn, tokenOut, fee);
uint256 k = challengers(c.cfg);
address[] memory pools;
uint24[] memory tiers;
uint256 m;
if (k != 0) (pools, tiers, m) = _challengers(c, k, zf1 ? tokenIn : tokenOut, zf1 ? tokenOut : tokenIn, p.pool);
if (m == 0 && !needAmount) return p;
uint256 smax = uint16(c.cfg >> 32);
Walk memory w = _open(p.pool, fee, zf1, exactOut, amount, 0);
uint256 st = _walk(w, m == 0 ? smax : uint8(c.cfg >> 16));
if (st != FILLED) {
if (!needAmount) return p;
if (st == BOUND && m != 0) st = _walk(w, smax);
if (st != FILLED) {
if (mode & MODE_QUOTE != 0) _noProgress(w, amount);
revert HopUnfillable(hop);
}
_take(p, w);
p.steps = w.steps;
return p;
}
_take(p, w);
uint256 own = w.acc;
uint256 steps = w.steps;
uint256 sc = uint8(c.cfg >> 24);
for (uint256 j; j < m; ++j) {
Walk memory cw = _open(pools[j], tiers[j], zf1, exactOut, amount, 0);
uint256 cst = _walk(cw, sc);
steps += cw.steps;
if (cst != FILLED) continue;
if (exactOut ? cw.acc < p.amount : cw.acc > p.amount) {
_take(p, cw);
p.floor = exactOut ? amount : own;
}
}
p.steps = steps;
}
function _noProgress(Walk memory w, uint256 amount) private pure {
require(w.acc != 0 || w.rem != amount, "Unexpected error");
}
function _take(Pick memory p, Walk memory w) private pure {
p.pool = w.pool;
p.amount = w.acc;
p.sqrtAfter = w.sqrtP;
p.crossed = w.crossed;
}
function _challengers(Config memory c, uint256 k, address t0, address t1, address own)
private
view
returns (address[] memory pools, uint24[] memory tiers, uint256 m)
{
pools = new address[](k);
tiers = new uint24[](k);
uint256[] memory liqs = new uint256[](k);
uint256 n = uint8(c.cfg);
for (uint256 v; v < n; ++v) {
uint256 w0 = c.words[v * WORDS_PER_VENUE];
address deployer = address(uint160(w0));
bytes32 initHash = bytes32(c.words[v * WORDS_PER_VENUE + 1]);
uint256 w2 = c.words[v * WORDS_PER_VENUE + 2];
uint256 count = (c.cfg >> (TIER_COUNTS_SHIFT + 4 * v)) & 15;
for (uint256 t; t < count; ++t) {
uint24 tier = uint24(t < 4 ? w0 >> (160 + 24 * t) : w2 >> (24 * (t - 4)));
address pool = derive(deployer, initHash, t0, t1, tier);
if (pool == own || pool.code.length == 0) continue;
(uint256 liq,) = _read(pool, LIQUIDITY, 0, 0x20);
liq = uint128(liq);
if (liq == 0) continue;
uint256 j = m;
if (j == k) {
if (liq <= liqs[k - 1]) continue;
j = k - 1;
} else {
++m;
}
while (j != 0 && liq > liqs[j - 1]) {
pools[j] = pools[j - 1];
tiers[j] = tiers[j - 1];
liqs[j] = liqs[j - 1];
--j;
}
pools[j] = pool;
tiers[j] = tier;
liqs[j] = liq;
}
}
}
function _open(address pool, uint24 fee, bool zf1, bool exactOut, uint256 amount, uint160 limit)
private
view
returns (Walk memory w)
{
w.pool = pool;
w.fee = fee;
w.zf1 = zf1;
w.exactOut = exactOut;
w.rem = amount;
w.limit = limit != 0 ? limit : (zf1 ? MIN_LIMIT : MAX_LIMIT);
(uint256 price, uint256 tick) = _read(pool, SLOT0, 0, 0x40);
w.sqrtP = uint160(price);
w.tick = int24(int256(tick));
(uint256 liq,) = _read(pool, LIQUIDITY, 0, 0x20);
w.liq = uint128(liq);
(uint256 spacing,) = _read(pool, TICK_SPACING, 0, 0x20);
w.spacing = int24(int256(spacing));
}
function _walk(Walk memory w, uint256 maxSteps) private view returns (uint256) {
while (w.steps < maxSteps) {
++w.steps;
(int24 next, bool initialized) = TickBitmap.nextInitializedTickWithinOneWord(w.pool, w.tick, w.spacing, w.zf1);
if (next < -V3TickMath.MAX_TICK) next = -V3TickMath.MAX_TICK;
else if (next > V3TickMath.MAX_TICK) next = V3TickMath.MAX_TICK;
uint160 tickPrice = V3TickMath._getSqrtRatioAtTick(next);
uint160 target = (w.zf1 ? tickPrice < w.limit : tickPrice > w.limit) ? w.limit : tickPrice;
if (w.liq == 0) w.sqrtP = target;
else if (w.exactOut) w.sqrtP = _stepOut(w, target);
else w.sqrtP = _stepIn(w, target);
if (w.rem == 0) return w.acc != 0 ? FILLED : DRY;
if (w.sqrtP != tickPrice) return DRY;
if (initialized) {
(, uint256 net) = _read(w.pool, TICKS, int256(next), 0x40);
int128 liquidityNet = int128(int256(net));
unchecked {
if (w.zf1) liquidityNet = -liquidityNet;
}
w.liq = liquidityNet < 0 ? w.liq - uint128(-liquidityNet) : w.liq + uint128(liquidityNet);
++w.crossed;
}
w.tick = w.zf1 ? next - 1 : next;
}
return BOUND;
}
function _stepIn(Walk memory w, uint160 target) private pure returns (uint160 reached) {
uint256 amountIn;
uint256 amountOut;
uint256 feeAmount;
int256 remaining = w.rem > uint256(type(int256).max) ? type(int256).max : int256(w.rem);
(reached, amountIn, amountOut, feeAmount) = SwapMath.computeSwapStep(w.sqrtP, target, w.liq, remaining, w.fee);
w.rem -= amountIn + feeAmount;
w.acc += amountOut;
}
function _stepOut(Walk memory w, uint160 target) private pure returns (uint160 reached) {
uint160 price = w.sqrtP;
uint128 liq = w.liq;
uint256 maxOut = w.zf1
? SqrtPriceMath.getAmount1Delta(target, price, liq, false)
: SqrtPriceMath.getAmount0Delta(price, target, liq, false);
if (w.rem >= maxOut) {
reached = target;
} else if (w.zf1) {
reached = uint160(uint256(price) - Math.mulDiv(w.rem, FixedPoint96.Q96, liq, Math.Rounding.Ceil));
} else {
uint256 num = uint256(liq) << 96;
reached = uint160(Math.mulDiv(num, price, num - w.rem * uint256(price), Math.Rounding.Ceil));
}
uint256 amountIn = w.zf1
? SqrtPriceMath.getAmount0Delta(reached, price, liq, true)
: SqrtPriceMath.getAmount1Delta(price, reached, liq, true);
uint256 amountOut = reached == target
? maxOut
: (w.zf1
? SqrtPriceMath.getAmount1Delta(reached, price, liq, false)
: SqrtPriceMath.getAmount0Delta(price, reached, liq, false));
if (amountOut > w.rem) amountOut = w.rem;
w.rem -= amountOut;
w.acc += amountIn + Math.mulDiv(amountIn, w.fee, PIPS - w.fee, Math.Rounding.Ceil);
}
function _read(address pool, bytes4 selector, int256 arg, uint256 need)
private
view
returns (uint256 a, uint256 b)
{
bool ok;
assembly ("memory-safe") {
let m := mload(0x40)
mstore(m, selector)
mstore(add(m, 0x04), arg)
ok := staticcall(gas(), pool, m, 0x24, m, 0x40)
ok := and(ok, iszero(lt(returndatasize(), need)))
a := mload(m)
b := mload(add(m, 0x20))
}
if (!ok) revert PoolReadFailed(pool);
}
}
npm/@openzeppelin/[email protected]/utils/Panic.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
library Panic {
uint256 internal constant GENERIC = 0x00;
uint256 internal constant ASSERT = 0x01;
uint256 internal constant UNDER_OVERFLOW = 0x11;
uint256 internal constant DIVISION_BY_ZERO = 0x12;
uint256 internal constant ENUM_CONVERSION_ERROR = 0x21;
uint256 internal constant STORAGE_ENCODING_ERROR = 0x22;
uint256 internal constant EMPTY_ARRAY_POP = 0x31;
uint256 internal constant ARRAY_OUT_OF_BOUNDS = 0x32;
uint256 internal constant RESOURCE_ERROR = 0x41;
uint256 internal constant INVALID_INTERNAL_FUNCTION = 0x51;
function panic(uint256 code) internal pure {
assembly ("memory-safe") {
mstore(0x00, 0x4e487b71)
mstore(0x20, code)
revert(0x1c, 0x24)
}
}
}
npm/@openzeppelin/[email protected]/utils/math/Math.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {Panic} from "../Panic.sol";
import {SafeCast} from "./SafeCast.sol";
library Math {
enum Rounding {
Floor,
Ceil,
Trunc,
Expand
}
function add512(uint256 a, uint256 b) internal pure returns (uint256 high, uint256 low) {
assembly ("memory-safe") {
low := add(a, b)
high := lt(low, a)
}
}
function mul512(uint256 a, uint256 b) internal pure returns (uint256 high, uint256 low) {
assembly ("memory-safe") {
let mm := mulmod(a, b, not(0))
low := mul(a, b)
high := sub(sub(mm, low), lt(mm, low))
}
}
function tryAdd(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
uint256 c = a + b;
success = c >= a;
result = c * SafeCast.toUint(success);
}
}
function trySub(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
uint256 c = a - b;
success = c <= a;
result = c * SafeCast.toUint(success);
}
}
function tryMul(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
uint256 c = a * b;
assembly ("memory-safe") {
success := or(eq(div(c, a), b), iszero(a))
}
result = c * SafeCast.toUint(success);
}
}
function tryDiv(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
success = b > 0;
assembly ("memory-safe") {
result := div(a, b)
}
}
}
function tryMod(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
success = b > 0;
assembly ("memory-safe") {
result := mod(a, b)
}
}
}
function saturatingAdd(uint256 a, uint256 b) internal pure returns (uint256) {
(bool success, uint256 result) = tryAdd(a, b);
return ternary(success, result, type(uint256).max);
}
function saturatingSub(uint256 a, uint256 b) internal pure returns (uint256) {
(, uint256 result) = trySub(a, b);
return result;
}
function saturatingMul(uint256 a, uint256 b) internal pure returns (uint256) {
(bool success, uint256 result) = tryMul(a, b);
return ternary(success, result, type(uint256).max);
}
function ternary(bool condition, uint256 a, uint256 b) internal pure returns (uint256) {
unchecked {
return b ^ ((a ^ b) * SafeCast.toUint(condition));
}
}
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return ternary(a > b, a, b);
}
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return ternary(a < b, a, b);
}
function average(uint256 a, uint256 b) internal pure returns (uint256) {
unchecked {
return (a & b) + (a ^ b) / 2;
}
}
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
if (b == 0) {
Panic.panic(Panic.DIVISION_BY_ZERO);
}
unchecked {
return SafeCast.toUint(a > 0) * ((a - 1) / b + 1);
}
}
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
(uint256 high, uint256 low) = mul512(x, y);
if (high == 0) {
return low / denominator;
}
if (denominator <= high) {
Panic.panic(ternary(denominator == 0, Panic.DIVISION_BY_ZERO, Panic.UNDER_OVERFLOW));
}
uint256 remainder;
assembly ("memory-safe") {
remainder := mulmod(x, y, denominator)
high := sub(high, gt(remainder, low))
low := sub(low, remainder)
}
uint256 twos = denominator & (0 - denominator);
assembly ("memory-safe") {
denominator := div(denominator, twos)
low := div(low, twos)
twos := add(div(sub(0, twos), twos), 1)
}
low |= high * twos;
uint256 inverse = (3 * denominator) ^ 2;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
result = low * inverse;
return result;
}
}
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
return mulDiv(x, y, denominator) + SafeCast.toUint(unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0);
}
function mulShr(uint256 x, uint256 y, uint8 n) internal pure returns (uint256 result) {
unchecked {
(uint256 high, uint256 low) = mul512(x, y);
if (high >= 1 << n) {
Panic.panic(Panic.UNDER_OVERFLOW);
}
return (high << (256 - n)) | (low >> n);
}
}
function mulShr(uint256 x, uint256 y, uint8 n, Rounding rounding) internal pure returns (uint256) {
return mulShr(x, y, n) + SafeCast.toUint(unsignedRoundsUp(rounding) && mulmod(x, y, 1 << n) > 0);
}
function invMod(uint256 a, uint256 n) internal pure returns (uint256) {
unchecked {
if (n == 0) return 0;
uint256 remainder = a % n;
uint256 gcd = n;
int256 x = 0;
int256 y = 1;
while (remainder != 0) {
uint256 quotient = gcd / remainder;
(gcd, remainder) = (
remainder,
gcd - remainder * quotient
);
(x, y) = (
y,
x - y * int256(quotient)
);
}
if (gcd != 1) return 0;
return ternary(x < 0, n - uint256(-x), uint256(x));
}
}
function invModPrime(uint256 a, uint256 p) internal view returns (uint256) {
unchecked {
return Math.modExp(a, p - 2, p);
}
}
function modExp(uint256 b, uint256 e, uint256 m) internal view returns (uint256) {
(bool success, uint256 result) = tryModExp(b, e, m);
if (!success) {
Panic.panic(Panic.DIVISION_BY_ZERO);
}
return result;
}
function tryModExp(uint256 b, uint256 e, uint256 m) internal view returns (bool success, uint256 result) {
if (m == 0) return (false, 0);
assembly ("memory-safe") {
let ptr := mload(0x40)
mstore(ptr, 0x20)
mstore(add(ptr, 0x20), 0x20)
mstore(add(ptr, 0x40), 0x20)
mstore(add(ptr, 0x60), b)
mstore(add(ptr, 0x80), e)
mstore(add(ptr, 0xa0), m)
success := staticcall(gas(), 0x05, ptr, 0xc0, 0x00, 0x20)
result := mload(0x00)
}
}
function modExp(bytes memory b, bytes memory e, bytes memory m) internal view returns (bytes memory) {
(bool success, bytes memory result) = tryModExp(b, e, m);
if (!success) {
Panic.panic(Panic.DIVISION_BY_ZERO);
}
return result;
}
function tryModExp(
bytes memory b,
bytes memory e,
bytes memory m
) internal view returns (bool success, bytes memory result) {
if (_zeroBytes(m)) return (false, new bytes(0));
uint256 mLen = m.length;
result = abi.encodePacked(b.length, e.length, mLen, b, e, m);
assembly ("memory-safe") {
let dataPtr := add(result, 0x20)
success := staticcall(gas(), 0x05, dataPtr, mload(result), dataPtr, mLen)
mstore(result, mLen)
mstore(0x40, add(dataPtr, mLen))
}
}
function _zeroBytes(bytes memory buffer) private pure returns (bool) {
uint256 chunk;
for (uint256 i = 0; i < buffer.length; i += 0x20) {
assembly ("memory-safe") {
chunk := mload(add(add(buffer, 0x20), i))
}
if (chunk >> (8 * saturatingSub(i + 0x20, buffer.length)) != 0) {
return false;
}
}
return true;
}
function sqrt(uint256 a) internal pure returns (uint256) {
unchecked {
if (a <= 1) {
return a;
}
uint256 aa = a;
uint256 xn = 1;
if (aa >= (1 << 128)) {
aa >>= 128;
xn <<= 64;
}
if (aa >= (1 << 64)) {
aa >>= 64;
xn <<= 32;
}
if (aa >= (1 << 32)) {
aa >>= 32;
xn <<= 16;
}
if (aa >= (1 << 16)) {
aa >>= 16;
xn <<= 8;
}
if (aa >= (1 << 8)) {
aa >>= 8;
xn <<= 4;
}
if (aa >= (1 << 4)) {
aa >>= 4;
xn <<= 2;
}
if (aa >= (1 << 2)) {
xn <<= 1;
}
xn = (3 * xn) >> 1;
xn = (xn + a / xn) >> 1;
xn = (xn + a / xn) >> 1;
xn = (xn + a / xn) >> 1;
xn = (xn + a / xn) >> 1;
xn = (xn + a / xn) >> 1;
xn = (xn + a / xn) >> 1;
return xn - SafeCast.toUint(xn > a / xn);
}
}
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && result * result < a);
}
}
function log2(uint256 x) internal pure returns (uint256 r) {
r = SafeCast.toUint(x > 0xffffffffffffffffffffffffffffffff) << 7;
r |= SafeCast.toUint((x >> r) > 0xffffffffffffffff) << 6;
r |= SafeCast.toUint((x >> r) > 0xffffffff) << 5;
r |= SafeCast.toUint((x >> r) > 0xffff) << 4;
r |= SafeCast.toUint((x >> r) > 0xff) << 3;
r |= SafeCast.toUint((x >> r) > 0xf) << 2;
assembly ("memory-safe") {
r := or(r, byte(shr(r, x), 0x0000010102020202030303030303030300000000000000000000000000000000))
}
}
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << result < value);
}
}
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >= 10 ** 64) {
value /= 10 ** 64;
result += 64;
}
if (value >= 10 ** 32) {
value /= 10 ** 32;
result += 32;
}
if (value >= 10 ** 16) {
value /= 10 ** 16;
result += 16;
}
if (value >= 10 ** 8) {
value /= 10 ** 8;
result += 8;
}
if (value >= 10 ** 4) {
value /= 10 ** 4;
result += 4;
}
if (value >= 10 ** 2) {
value /= 10 ** 2;
result += 2;
}
if (value >= 10 ** 1) {
result += 1;
}
}
return result;
}
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 10 ** result < value);
}
}
function log256(uint256 x) internal pure returns (uint256 r) {
r = SafeCast.toUint(x > 0xffffffffffffffffffffffffffffffff) << 7;
r |= SafeCast.toUint((x >> r) > 0xffffffffffffffff) << 6;
r |= SafeCast.toUint((x >> r) > 0xffffffff) << 5;
r |= SafeCast.toUint((x >> r) > 0xffff) << 4;
return (r >> 3) | SafeCast.toUint((x >> r) > 0xff);
}
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << (result << 3) < value);
}
}
function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {
return uint8(rounding) % 2 == 1;
}
function clz(uint256 x) internal pure returns (uint256) {
return ternary(x == 0, 256, 255 - log2(x));
}
}
npm/@openzeppelin/[email protected]/utils/math/SafeCast.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
library SafeCast {
error SafeCastOverflowedUintDowncast(uint8 bits, uint256 value);
error SafeCastOverflowedIntToUint(int256 value);
error SafeCastOverflowedIntDowncast(uint8 bits, int256 value);
error SafeCastOverflowedUintToInt(uint256 value);
function toUint248(uint256 value) internal pure returns (uint248) {
if (value > type(uint248).max) {
revert SafeCastOverflowedUintDowncast(248, value);
}
return uint248(value);
}
function toUint240(uint256 value) internal pure returns (uint240) {
if (value > type(uint240).max) {
revert SafeCastOverflowedUintDowncast(240, value);
}
return uint240(value);
}
function toUint232(uint256 value) internal pure returns (uint232) {
if (value > type(uint232).max) {
revert SafeCastOverflowedUintDowncast(232, value);
}
return uint232(value);
}
function toUint224(uint256 value) internal pure returns (uint224) {
if (value > type(uint224).max) {
revert SafeCastOverflowedUintDowncast(224, value);
}
return uint224(value);
}
function toUint216(uint256 value) internal pure returns (uint216) {
if (value > type(uint216).max) {
revert SafeCastOverflowedUintDowncast(216, value);
}
return uint216(value);
}
function toUint208(uint256 value) internal pure returns (uint208) {
if (value > type(uint208).max) {
revert SafeCastOverflowedUintDowncast(208, value);
}
return uint208(value);
}
function toUint200(uint256 value) internal pure returns (uint200) {
if (value > type(uint200).max) {
revert SafeCastOverflowedUintDowncast(200, value);
}
return uint200(value);
}
function toUint192(uint256 value) internal pure returns (uint192) {
if (value > type(uint192).max) {
revert SafeCastOverflowedUintDowncast(192, value);
}
return uint192(value);
}
function toUint184(uint256 value) internal pure returns (uint184) {
if (value > type(uint184).max) {
revert SafeCastOverflowedUintDowncast(184, value);
}
return uint184(value);
}
function toUint176(uint256 value) internal pure returns (uint176) {
if (value > type(uint176).max) {
revert SafeCastOverflowedUintDowncast(176, value);
}
return uint176(value);
}
function toUint168(uint256 value) internal pure returns (uint168) {
if (value > type(uint168).max) {
revert SafeCastOverflowedUintDowncast(168, value);
}
return uint168(value);
}
function toUint160(uint256 value) internal pure returns (uint160) {
if (value > type(uint160).max) {
revert SafeCastOverflowedUintDowncast(160, value);
}
return uint160(value);
}
function toUint152(uint256 value) internal pure returns (uint152) {
if (value > type(uint152).max) {
revert SafeCastOverflowedUintDowncast(152, value);
}
return uint152(value);
}
function toUint144(uint256 value) internal pure returns (uint144) {
if (value > type(uint144).max) {
revert SafeCastOverflowedUintDowncast(144, value);
}
return uint144(value);
}
function toUint136(uint256 value) internal pure returns (uint136) {
if (value > type(uint136).max) {
revert SafeCastOverflowedUintDowncast(136, value);
}
return uint136(value);
}
function toUint128(uint256 value) internal pure returns (uint128) {
if (value > type(uint128).max) {
revert SafeCastOverflowedUintDowncast(128, value);
}
return uint128(value);
}
function toUint120(uint256 value) internal pure returns (uint120) {
if (value > type(uint120).max) {
revert SafeCastOverflowedUintDowncast(120, value);
}
return uint120(value);
}
function toUint112(uint256 value) internal pure returns (uint112) {
if (value > type(uint112).max) {
revert SafeCastOverflowedUintDowncast(112, value);
}
return uint112(value);
}
function toUint104(uint256 value) internal pure returns (uint104) {
if (value > type(uint104).max) {
revert SafeCastOverflowedUintDowncast(104, value);
}
return uint104(value);
}
function toUint96(uint256 value) internal pure returns (uint96) {
if (value > type(uint96).max) {
revert SafeCastOverflowedUintDowncast(96, value);
}
return uint96(value);
}
function toUint88(uint256 value) internal pure returns (uint88) {
if (value > type(uint88).max) {
revert SafeCastOverflowedUintDowncast(88, value);
}
return uint88(value);
}
function toUint80(uint256 value) internal pure returns (uint80) {
if (value > type(uint80).max) {
revert SafeCastOverflowedUintDowncast(80, value);
}
return uint80(value);
}
function toUint72(uint256 value) internal pure returns (uint72) {
if (value > type(uint72).max) {
revert SafeCastOverflowedUintDowncast(72, value);
}
return uint72(value);
}
function toUint64(uint256 value) internal pure returns (uint64) {
if (value > type(uint64).max) {
revert SafeCastOverflowedUintDowncast(64, value);
}
return uint64(value);
}
function toUint56(uint256 value) internal pure returns (uint56) {
if (value > type(uint56).max) {
revert SafeCastOverflowedUintDowncast(56, value);
}
return uint56(value);
}
function toUint48(uint256 value) internal pure returns (uint48) {
if (value > type(uint48).max) {
revert SafeCastOverflowedUintDowncast(48, value);
}
return uint48(value);
}
function toUint40(uint256 value) internal pure returns (uint40) {
if (value > type(uint40).max) {
revert SafeCastOverflowedUintDowncast(40, value);
}
return uint40(value);
}
function toUint32(uint256 value) internal pure returns (uint32) {
if (value > type(uint32).max) {
revert SafeCastOverflowedUintDowncast(32, value);
}
return uint32(value);
}
function toUint24(uint256 value) internal pure returns (uint24) {
if (value > type(uint24).max) {
revert SafeCastOverflowedUintDowncast(24, value);
}
return uint24(value);
}
function toUint16(uint256 value) internal pure returns (uint16) {
if (value > type(uint16).max) {
revert SafeCastOverflowedUintDowncast(16, value);
}
return uint16(value);
}
function toUint8(uint256 value) internal pure returns (uint8) {
if (value > type(uint8).max) {
revert SafeCastOverflowedUintDowncast(8, value);
}
return uint8(value);
}
function toUint256(int256 value) internal pure returns (uint256) {
if (value < 0) {
revert SafeCastOverflowedIntToUint(value);
}
return uint256(value);
}
function toInt248(int256 value) internal pure returns (int248 downcasted) {
downcasted = int248(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(248, value);
}
}
function toInt240(int256 value) internal pure returns (int240 downcasted) {
downcasted = int240(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(240, value);
}
}
function toInt232(int256 value) internal pure returns (int232 downcasted) {
downcasted = int232(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(232, value);
}
}
function toInt224(int256 value) internal pure returns (int224 downcasted) {
downcasted = int224(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(224, value);
}
}
function toInt216(int256 value) internal pure returns (int216 downcasted) {
downcasted = int216(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(216, value);
}
}
function toInt208(int256 value) internal pure returns (int208 downcasted) {
downcasted = int208(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(208, value);
}
}
function toInt200(int256 value) internal pure returns (int200 downcasted) {
downcasted = int200(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(200, value);
}
}
function toInt192(int256 value) internal pure returns (int192 downcasted) {
downcasted = int192(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(192, value);
}
}
function toInt184(int256 value) internal pure returns (int184 downcasted) {
downcasted = int184(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(184, value);
}
}
function toInt176(int256 value) internal pure returns (int176 downcasted) {
downcasted = int176(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(176, value);
}
}
function toInt168(int256 value) internal pure returns (int168 downcasted) {
downcasted = int168(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(168, value);
}
}
function toInt160(int256 value) internal pure returns (int160 downcasted) {
downcasted = int160(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(160, value);
}
}
function toInt152(int256 value) internal pure returns (int152 downcasted) {
downcasted = int152(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(152, value);
}
}
function toInt144(int256 value) internal pure returns (int144 downcasted) {
downcasted = int144(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(144, value);
}
}
function toInt136(int256 value) internal pure returns (int136 downcasted) {
downcasted = int136(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(136, value);
}
}
function toInt128(int256 value) internal pure returns (int128 downcasted) {
downcasted = int128(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(128, value);
}
}
function toInt120(int256 value) internal pure returns (int120 downcasted) {
downcasted = int120(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(120, value);
}
}
function toInt112(int256 value) internal pure returns (int112 downcasted) {
downcasted = int112(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(112, value);
}
}
function toInt104(int256 value) internal pure returns (int104 downcasted) {
downcasted = int104(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(104, value);
}
}
function toInt96(int256 value) internal pure returns (int96 downcasted) {
downcasted = int96(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(96, value);
}
}
function toInt88(int256 value) internal pure returns (int88 downcasted) {
downcasted = int88(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(88, value);
}
}
function toInt80(int256 value) internal pure returns (int80 downcasted) {
downcasted = int80(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(80, value);
}
}
function toInt72(int256 value) internal pure returns (int72 downcasted) {
downcasted = int72(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(72, value);
}
}
function toInt64(int256 value) internal pure returns (int64 downcasted) {
downcasted = int64(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(64, value);
}
}
function toInt56(int256 value) internal pure returns (int56 downcasted) {
downcasted = int56(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(56, value);
}
}
function toInt48(int256 value) internal pure returns (int48 downcasted) {
downcasted = int48(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(48, value);
}
}
function toInt40(int256 value) internal pure returns (int40 downcasted) {
downcasted = int40(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(40, value);
}
}
function toInt32(int256 value) internal pure returns (int32 downcasted) {
downcasted = int32(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(32, value);
}
}
function toInt24(int256 value) internal pure returns (int24 downcasted) {
downcasted = int24(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(24, value);
}
}
function toInt16(int256 value) internal pure returns (int16 downcasted) {
downcasted = int16(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(16, value);
}
}
function toInt8(int256 value) internal pure returns (int8 downcasted) {
downcasted = int8(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(8, value);
}
}
function toInt256(uint256 value) internal pure returns (int256) {
if (value > uint256(type(int256).max)) {
revert SafeCastOverflowedUintToInt(value);
}
return int256(value);
}
function toUint(bool b) internal pure returns (uint256 u) {
assembly ("memory-safe") {
u := iszero(iszero(b))
}
}
}
project/contracts-uni-router/libraries/uniswap/BitMath.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity 0.8.34;
library BitMath {
function mostSignificantBit(uint256 x) internal pure returns (uint8 r) {
require(x > 0);
unchecked {
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;
}
}
function leastSignificantBit(uint256 x) internal pure returns (uint8 r) {
require(x > 0);
unchecked {
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;
}
}
}
project/contracts-uni-router/libraries/uniswap/FixedPoint96.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity 0.8.34;
library FixedPoint96 {
uint8 internal constant RESOLUTION = 96;
uint256 internal constant Q96 = 0x1000000000000000000000000;
}
project/contracts-uni-router/libraries/uniswap/FullMath.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
library FullMath {
function mulDiv(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result) {
return Math.mulDiv(a, b, denominator);
}
function mulDivRoundingUp(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result) {
return Math.mulDiv(a, b, denominator, Math.Rounding.Ceil);
}
}
project/contracts-uni-router/libraries/uniswap/SqrtPriceMath.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.34;
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import {FullMath} from "./FullMath.sol";
import {UnsafeMath} from "./UnsafeMath.sol";
import {FixedPoint96} from "./FixedPoint96.sol";
library SqrtPriceMath {
using SafeCast for uint256;
function getNextSqrtPriceFromAmount0RoundingUp(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amount,
bool add
) internal pure returns (uint160) {
if (amount == 0) return sqrtPX96;
uint256 numerator1 = uint256(liquidity) << FixedPoint96.RESOLUTION;
if (add) {
unchecked {
uint256 product;
if ((product = amount * sqrtPX96) / amount == sqrtPX96) {
uint256 denominator = numerator1 + product;
if (denominator >= numerator1)
return uint160(FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator));
}
}
return uint160(UnsafeMath.divRoundingUp(numerator1, (numerator1 / sqrtPX96) + amount));
} else {
unchecked {
uint256 product;
require((product = amount * sqrtPX96) / amount == sqrtPX96 && numerator1 > product);
uint256 denominator = numerator1 - product;
return FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator).toUint160();
}
}
}
function getNextSqrtPriceFromAmount1RoundingDown(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amount,
bool add
) internal pure returns (uint160) {
if (add) {
uint256 quotient = (
amount <= type(uint160).max
? (amount << FixedPoint96.RESOLUTION) / liquidity
: FullMath.mulDiv(amount, FixedPoint96.Q96, liquidity)
);
return (uint256(sqrtPX96) + quotient).toUint160();
} else {
uint256 quotient = (
amount <= type(uint160).max
? UnsafeMath.divRoundingUp(amount << FixedPoint96.RESOLUTION, liquidity)
: FullMath.mulDivRoundingUp(amount, FixedPoint96.Q96, liquidity)
);
require(sqrtPX96 > quotient);
unchecked {
return uint160(sqrtPX96 - quotient);
}
}
}
function getNextSqrtPriceFromInput(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amountIn,
bool zeroForOne
) internal pure returns (uint160 sqrtQX96) {
require(sqrtPX96 > 0);
require(liquidity > 0);
return
zeroForOne
? getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true)
: getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountIn, true);
}
function getNextSqrtPriceFromOutput(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amountOut,
bool zeroForOne
) internal pure returns (uint160 sqrtQX96) {
require(sqrtPX96 > 0);
require(liquidity > 0);
return
zeroForOne
? getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountOut, false)
: getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountOut, false);
}
function getAmount0Delta(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint128 liquidity,
bool roundUp
) internal pure returns (uint256 amount0) {
unchecked {
if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
uint256 numerator1 = uint256(liquidity) << FixedPoint96.RESOLUTION;
uint256 numerator2 = sqrtRatioBX96 - sqrtRatioAX96;
require(sqrtRatioAX96 > 0);
return
roundUp
? UnsafeMath.divRoundingUp(
FullMath.mulDivRoundingUp(numerator1, numerator2, sqrtRatioBX96),
sqrtRatioAX96
)
: FullMath.mulDiv(numerator1, numerator2, sqrtRatioBX96) / sqrtRatioAX96;
}
}
function getAmount1Delta(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint128 liquidity,
bool roundUp
) internal pure returns (uint256 amount1) {
unchecked {
if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
return
roundUp
? FullMath.mulDivRoundingUp(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96)
: FullMath.mulDiv(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96);
}
}
function getAmount0Delta(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
int128 liquidity
) internal pure returns (int256 amount0) {
unchecked {
return
liquidity < 0
? -getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(-liquidity), false).toInt256()
: getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(liquidity), true).toInt256();
}
}
function getAmount1Delta(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
int128 liquidity
) internal pure returns (int256 amount1) {
unchecked {
return
liquidity < 0
? -getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(-liquidity), false).toInt256()
: getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(liquidity), true).toInt256();
}
}
}
project/contracts-uni-router/libraries/uniswap/SwapMath.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.34;
import {FullMath} from "./FullMath.sol";
import {SqrtPriceMath} from "./SqrtPriceMath.sol";
library SwapMath {
function computeSwapStep(
uint160 sqrtRatioCurrentX96,
uint160 sqrtRatioTargetX96,
uint128 liquidity,
int256 amountRemaining,
uint24 feePips
)
internal
pure
returns (
uint160 sqrtRatioNextX96,
uint256 amountIn,
uint256 amountOut,
uint256 feeAmount
)
{
unchecked {
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;
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);
}
if (!exactIn && amountOut > uint256(-amountRemaining)) {
amountOut = uint256(-amountRemaining);
}
if (exactIn && sqrtRatioNextX96 != sqrtRatioTargetX96) {
feeAmount = uint256(amountRemaining) - amountIn;
} else {
feeAmount = FullMath.mulDivRoundingUp(amountIn, feePips, 1e6 - feePips);
}
}
}
}
project/contracts-uni-router/libraries/uniswap/TickBitmap.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.34;
import {BitMath} from "./BitMath.sol";
library TickBitmap {
bytes4 private constant TICK_BITMAP = 0x5339c296;
error PoolReadFailed(address pool);
function position(int24 tick) private pure returns (int16 wordPos, uint8 bitPos) {
unchecked {
wordPos = int16(tick >> 8);
bitPos = uint8(int8(tick % 256));
}
}
function nextInitializedTickWithinOneWord(
address pool,
int24 tick,
int24 tickSpacing,
bool lte
) internal view returns (int24 next, bool initialized) {
unchecked {
int24 compressed = tick / tickSpacing;
if (tick < 0 && tick % tickSpacing != 0) compressed--;
if (lte) {
(int16 wordPos, uint8 bitPos) = position(compressed);
uint256 mask = (1 << bitPos) - 1 + (1 << bitPos);
uint256 masked = word(pool, wordPos) & mask;
initialized = masked != 0;
next = initialized
? (compressed - int24(uint24(bitPos - BitMath.mostSignificantBit(masked)))) * tickSpacing
: (compressed - int24(uint24(bitPos))) * tickSpacing;
} else {
(int16 wordPos, uint8 bitPos) = position(compressed + 1);
uint256 mask = ~((1 << bitPos) - 1);
uint256 masked = word(pool, wordPos) & mask;
initialized = masked != 0;
next = initialized
? (compressed + 1 + int24(uint24(BitMath.leastSignificantBit(masked) - bitPos))) * tickSpacing
: (compressed + 1 + int24(uint24(type(uint8).max - bitPos))) * tickSpacing;
}
}
}
function word(address pool, int16 wordPos) private view returns (uint256 value) {
bytes4 selector = TICK_BITMAP;
bool ok;
assembly ("memory-safe") {
let m := mload(0x40)
mstore(m, selector)
mstore(add(m, 0x04), signextend(1, wordPos))
ok := staticcall(gas(), pool, m, 0x24, m, 0x20)
ok := and(ok, gt(returndatasize(), 0x1f))
value := mload(m)
}
if (!ok) revert PoolReadFailed(pool);
}
}
project/contracts-uni-router/libraries/uniswap/UnsafeMath.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity 0.8.34;
library UnsafeMath {
function divRoundingUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
assembly {
z := add(div(x, y), gt(mod(x, y), 0))
}
}
}
project/contracts/libraries/V3TickMath.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
library V3TickMath {
uint160 internal constant MIN_SQRT_RATIO = 4295128739;
uint160 internal constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342;
int24 internal constant MAX_TICK = 887272;
error TickOutOfRange(int24 tick);
function _getSqrtRatioAtTick(int24 tick) internal pure returns (uint160 sqrtPriceX96) {
unchecked {
uint256 absTick = tick < 0 ? uint256(-int256(tick)) : uint256(int256(tick));
if (absTick > uint256(uint24(MAX_TICK))) revert TickOutOfRange(tick);
uint256 ratio =
absTick & 0x1 != 0 ? 0xfffcb933bd6fad37aa2d162d1a594001 : 0x100000000000000000000000000000000;
if (absTick & 0x2 != 0) ratio = (ratio * 0xfff97272373d413259a46990580e213a) >> 128;
if (absTick & 0x4 != 0) ratio = (ratio * 0xfff2e50f5f656932ef12357cf3c7fdcc) >> 128;
if (absTick & 0x8 != 0) ratio = (ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0) >> 128;
if (absTick & 0x10 != 0) ratio = (ratio * 0xffcb9843d60f6159c9db58835c926644) >> 128;
if (absTick & 0x20 != 0) ratio = (ratio * 0xff973b41fa98c081472e6896dfb254c0) >> 128;
if (absTick & 0x40 != 0) ratio = (ratio * 0xff2ea16466c96a3843ec78b326b52861) >> 128;
if (absTick & 0x80 != 0) ratio = (ratio * 0xfe5dee046a99a2a811c461f1969c3053) >> 128;
if (absTick & 0x100 != 0) ratio = (ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4) >> 128;
if (absTick & 0x200 != 0) ratio = (ratio * 0xf987a7253ac413176f2b074cf7815e54) >> 128;
if (absTick & 0x400 != 0) ratio = (ratio * 0xf3392b0822b70005940c7a398e4b70f3) >> 128;
if (absTick & 0x800 != 0) ratio = (ratio * 0xe7159475a2c29b7443b29c7fa6e889d9) >> 128;
if (absTick & 0x1000 != 0) ratio = (ratio * 0xd097f3bdfd2022b8845ad8f792aa5825) >> 128;
if (absTick & 0x2000 != 0) ratio = (ratio * 0xa9f746462d870fdf8a65dc1f90e061e5) >> 128;
if (absTick & 0x4000 != 0) ratio = (ratio * 0x70d869a156d2a1b890bb3df62baf32f7) >> 128;
if (absTick & 0x8000 != 0) ratio = (ratio * 0x31be135f97d08fd981231505542fcfa6) >> 128;
if (absTick & 0x10000 != 0) ratio = (ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9) >> 128;
if (absTick & 0x20000 != 0) ratio = (ratio * 0x5d6af8dedb81196699c329225ee604) >> 128;
if (absTick & 0x40000 != 0) ratio = (ratio * 0x2216e584f5fa1ea926041bedfe98) >> 128;
if (absTick & 0x80000 != 0) ratio = (ratio * 0x48a170391f7dc42444e8fa2) >> 128;
if (tick > 0) ratio = type(uint256).max / ratio;
sqrtPriceX96 = uint160((ratio >> 32) + (ratio % (1 << 32) == 0 ? 0 : 1));
}
}
function _tickSpanPriceLimit(int24, int24, bool zeroForOne) internal pure returns (uint160 priceLimit) {
priceLimit = zeroForOne ? MIN_SQRT_RATIO + 1 : MAX_SQRT_RATIO - 1;
}
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"bytecodeHash": "none"
},
"optimizer": {
"runs": 120,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"project/:@openzeppelin/contracts/=npm/@openzeppelin/[email protected]/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.