zellic-audit
Initial commit
f998fcd
raw
history blame
57 kB
{
"language": "Solidity",
"sources": {
"contracts/banana/BuybackPool.sol": {
"content": "// SPDX-License-Identifier: GPL-2.0-or-later\npragma solidity ^0.8.0;\n\nimport \"../interfaces/IERC20.sol\";\nimport \"./interfaces/IBanana.sol\";\nimport \"./interfaces/IBananaDistributor.sol\";\nimport \"./interfaces/ITWAMM.sol\";\nimport \"./interfaces/ITWAMMPair.sol\";\nimport \"../utils/Ownable.sol\";\nimport \"../utils/AnalyticMath.sol\";\nimport \"../libraries/FullMath.sol\";\nimport \"../libraries/TransferHelper.sol\";\n\ncontract BuybackPool is Ownable, AnalyticMath {\n using FullMath for uint256;\n\n event BuybackExecuted(uint256 orderId, uint256 amountIn, uint256 buyingRate, uint256 burned);\n event WithdrawAndBurn(uint256 orderId, uint256 burnAmount);\n\n address public immutable banana;\n address public immutable usdc;\n address public immutable twamm;\n address public immutable bananaDistributor;\n address public keeper;\n\n uint256 public lastBuyingRate;\n uint256 public priceT1;\n uint256 public priceT2;\n uint256 public rewardT1;\n uint256 public rewardT2;\n uint256 public priceIndex = 100;\n uint256 public rewardIndex = 50;\n uint256 public secondsPerBlock = 12;\n uint256 public secondsOfEpoch;\n uint256 public lastOrderId = type(uint256).max;\n uint256 public lastExecuteTime;\n uint256 public endTime;\n\n bool public initialized;\n bool public isStop;\n\n constructor(\n address banana_,\n address usdc_,\n address twamm_,\n address bananaDistributor_,\n address keeper_,\n uint256 secondsOfEpoch_,\n uint256 initPrice,\n uint256 initReward,\n uint256 startTime,\n uint256 endTime_\n ) {\n owner = msg.sender;\n banana = banana_;\n usdc = usdc_;\n twamm = twamm_;\n bananaDistributor = bananaDistributor_;\n keeper = keeper_;\n secondsOfEpoch = secondsOfEpoch_;\n priceT2 = initPrice;\n rewardT2 = initReward;\n lastExecuteTime = startTime;\n endTime = endTime_;\n }\n\n // function initBuyingRate(uint256 amountIn) external onlyOwner {\n // require(!initialized, \"already initialized\");\n // initialized = true;\n // lastBuyingRate = amountIn / secondsOfEpoch;\n // }\n\n function updatePriceIndex(uint256 newPriceIndex) external onlyOwner {\n priceIndex = newPriceIndex;\n }\n\n function updateRewardIndex(uint256 newRewardIndex) external onlyOwner {\n rewardIndex = newRewardIndex;\n }\n\n function updateSecondsPerBlock(uint256 newSecondsPerBlock) external onlyOwner {\n secondsPerBlock = newSecondsPerBlock;\n }\n\n function updateSecondsOfEpoch(uint256 newSecondsOfEpoch) external onlyOwner {\n secondsOfEpoch = newSecondsOfEpoch;\n }\n\n function updateLastExecuteTime(uint256 newExecuteTime) external onlyOwner {\n lastExecuteTime = newExecuteTime;\n }\n\n function updateEndTime(uint256 endTime_) external onlyOwner {\n endTime = endTime_;\n }\n\n function updateStatus(bool isStop_) external onlyOwner {\n isStop = isStop_;\n }\n\n function updateKeeper(address keeper_) external onlyOwner {\n keeper = keeper_;\n }\n\n function resetPrice(uint256 newPriceT1, uint256 newPriceT2) external onlyOwner {\n priceT1 = newPriceT1;\n priceT2 = newPriceT2;\n }\n\n function resetReward(uint256 newRewardT1, uint256 newRewardT2) external onlyOwner {\n rewardT1 = newRewardT1;\n rewardT2 = newRewardT2;\n }\n\n function withdrawUsdc(address to) external onlyOwner {\n require(isStop, \"not stop\");\n uint256 usdcBalance = IERC20(usdc).balanceOf(address(this));\n TransferHelper.safeTransfer(usdc, to, usdcBalance);\n }\n\n function withdrawAndBurn(uint256 orderId) external onlyOwner {\n require(isStop, \"not stop\");\n address pair = ITWAMM(twamm).obtainPairAddress(usdc, banana);\n ITWAMMPair.Order memory order = ITWAMMPair(pair).getOrderDetails(orderId);\n require(block.number > order.expirationBlock, \"not reach withdrawable block\");\n ITWAMM(twamm).withdrawProceedsFromTermSwapTokenToToken(usdc, banana, orderId, block.timestamp);\n uint256 bananaBalance = IERC20(banana).balanceOf(address(this));\n require(bananaBalance > 0, \"nothing to burn\");\n IBanana(banana).burn(bananaBalance);\n emit WithdrawAndBurn(orderId, bananaBalance);\n }\n\n function execute() external {\n require(!isStop, \"is stop\");\n require(msg.sender == keeper, \"only keeper\");\n require(block.timestamp < endTime, \"end\");\n lastExecuteTime = lastExecuteTime + secondsOfEpoch;\n require(block.timestamp >= lastExecuteTime, \"not reach execute time\");\n require(lastExecuteTime + secondsOfEpoch > block.timestamp, \"over next epoch time\");\n\n uint256 burnAmount;\n if (lastOrderId != type(uint256).max) {\n address pair = ITWAMM(twamm).obtainPairAddress(usdc, banana);\n ITWAMMPair.Order memory order = ITWAMMPair(pair).getOrderDetails(lastOrderId);\n require(block.number > order.expirationBlock, \"not reach withdrawable block\");\n\n ITWAMM(twamm).withdrawProceedsFromTermSwapTokenToToken(usdc, banana, lastOrderId, block.timestamp);\n burnAmount = IERC20(banana).balanceOf(address(this));\n IBanana(banana).burn(burnAmount);\n }\n\n uint256 deltaTime = lastExecuteTime + secondsOfEpoch - block.timestamp;\n uint256 usdcBalance = IERC20(usdc).balanceOf(address(this));\n uint256 amountIn;\n if (!initialized) {\n amountIn = usdcBalance;\n lastBuyingRate = amountIn / deltaTime;\n initialized = true;\n } else {\n (uint256 pn, uint256 pd) = pow(priceT2, priceT1, priceIndex, 100);\n (uint256 rn, uint256 rd) = pow(rewardT1, rewardT2, rewardIndex, 100);\n lastBuyingRate = lastBuyingRate.mulDiv(pn, pd).mulDiv(rn, rd);\n amountIn = deltaTime * lastBuyingRate;\n if (amountIn > usdcBalance) {\n amountIn = usdcBalance;\n lastBuyingRate = amountIn / deltaTime;\n }\n }\n\n require(amountIn > 0, \"buying amount is 0\");\n IERC20(usdc).approve(twamm, amountIn);\n lastOrderId = ITWAMM(twamm).longTermSwapTokenToToken(\n usdc,\n banana,\n amountIn,\n deltaTime / (secondsPerBlock * 5),\n block.timestamp\n );\n\n uint256 lastReward = IBananaDistributor(bananaDistributor).lastReward();\n if (rewardT1 > 0) {\n rewardT2 = rewardT1;\n }\n rewardT1 = lastReward;\n\n (uint256 reserve0, uint256 reserve1) = ITWAMM(twamm).obtainReserves(usdc, banana);\n uint256 currentPrice = reserve0.mulDiv(1e30, reserve1); // reserve0/reserve1 * 10**(18+decimalsUSDC-decimalsBANA)\n if (priceT1 > 0) {\n priceT2 = priceT1;\n }\n priceT1 = currentPrice;\n\n emit BuybackExecuted(lastOrderId, amountIn, lastBuyingRate, burnAmount);\n }\n}\n"
},
"contracts/interfaces/IERC20.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\ninterface IERC20 {\n event Approval(address indexed owner, address indexed spender, uint256 value);\n event Transfer(address indexed from, address indexed to, uint256 value);\n\n function allowance(address owner, address spender) external view returns (uint256);\n\n function approve(address spender, uint256 value) external returns (bool);\n\n function transfer(address to, uint256 value) external returns (bool);\n\n function transferFrom(\n address from,\n address to,\n uint256 value\n ) external returns (bool);\n\n function totalSupply() external view returns (uint256);\n\n function balanceOf(address owner) external view returns (uint256);\n\n function name() external view returns (string memory);\n\n function symbol() external view returns (string memory);\n\n function decimals() external pure returns (uint8);\n}\n"
},
"contracts/banana/interfaces/IBanana.sol": {
"content": "// SPDX-License-Identifier: GPL-2.0-or-later\npragma solidity ^0.8.0;\n\nimport \"../../interfaces/IERC20.sol\";\n\ninterface IBanana is IERC20 {\n event RedeemTimeChanged(uint256 oldRedeemTime, uint256 newRedeemTime);\n event Redeem(address indexed user, uint256 burntAmount, uint256 apeXAmount);\n\n function apeXToken() external view returns (address);\n function redeemTime() external view returns (uint256);\n\n function mint(address to, uint256 apeXAmount) external returns (uint256);\n function burn(uint256 amount) external returns (bool);\n function burnFrom(address from, uint256 amount) external returns (bool);\n function redeem(uint256 amount) external returns (uint256);\n}"
},
"contracts/banana/interfaces/IBananaDistributor.sol": {
"content": "// SPDX-License-Identifier: GPL-2.0-or-later\npragma solidity ^0.8.0;\n\ninterface IBananaDistributor {\n function lastReward() external view returns (uint256);\n}"
},
"contracts/banana/interfaces/ITWAMM.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0-or-later\npragma solidity ^0.8.0;\n\ninterface ITWAMM {\n function factory() external view returns (address);\n\n function WETH() external view returns (address);\n\n function obtainReserves(address token0, address token1) external view returns (uint256 reserve0, uint256 reserve1);\n\n function obtainTotalSupply(address token0, address token1) external view returns (uint256);\n\n function obtainPairAddress(address token0, address token1) external view returns (address);\n\n function createPairWrapper(\n address token0,\n address token1,\n uint256 deadline\n ) external returns (address pair);\n\n function addInitialLiquidity(\n address token0,\n address token1,\n uint256 amount0,\n uint256 amount1,\n uint256 deadline\n ) external returns (uint256 lpTokenAmount);\n\n function addInitialLiquidityETH(\n address token,\n uint256 amountToken,\n uint256 amountETH,\n uint256 deadline\n ) external payable returns (uint256 lpTokenAmount);\n\n function addLiquidity(\n address token0,\n address token1,\n uint256 lpTokenAmount,\n uint256 amountIn0Max,\n uint256 amountIn1Max,\n uint256 deadline\n ) external returns (uint256 amountIn0, uint256 amountIn1);\n\n function addLiquidityETH(\n address token,\n uint256 lpTokenAmount,\n uint256 amountTokenInMax,\n uint256 amountETHInMax,\n uint256 deadline\n ) external payable returns (uint256 amountTokenIn, uint256 amountETHIn);\n\n function withdrawLiquidity(\n address token0,\n address token1,\n uint256 lpTokenAmount,\n uint256 amountOut0Min,\n uint256 amountOut1Min,\n uint256 deadline\n ) external returns (uint256 amountOut0, uint256 amountOut1);\n\n function withdrawLiquidityETH(\n address token,\n uint256 lpTokenAmount,\n uint256 amountTokenOutMin,\n uint256 amountETHOutMin,\n uint256 deadline\n ) external returns (uint256 amountTokenOut, uint256 amountETHOut);\n\n function instantSwapTokenToToken(\n address token0,\n address token1,\n uint256 amountIn,\n uint256 amountOutMin,\n uint256 deadline\n ) external returns (uint256 amountOut);\n\n function instantSwapTokenToETH(\n address token,\n uint256 amountTokenIn,\n uint256 amountETHOutMin,\n uint256 deadline\n ) external returns (uint256 amountETHOut);\n\n function instantSwapETHToToken(\n address token,\n uint256 amountETHIn,\n uint256 amountTokenOutMin,\n uint256 deadline\n ) external payable returns (uint256 amountTokenOut);\n\n function longTermSwapTokenToToken(\n address token0,\n address token1,\n uint256 amountIn,\n uint256 numberOfBlockIntervals,\n uint256 deadline\n ) external returns (uint256 orderId);\n\n function longTermSwapTokenToETH(\n address token,\n uint256 amountTokenIn,\n uint256 numberOfBlockIntervals,\n uint256 deadline\n ) external returns (uint256 orderId);\n\n function longTermSwapETHToToken(\n address token,\n uint256 amountETHIn,\n uint256 numberOfBlockIntervals,\n uint256 deadline\n ) external payable returns (uint256 orderId);\n\n function cancelTermSwapTokenToToken(\n address token0,\n address token1,\n uint256 orderId,\n uint256 deadline\n ) external returns (uint256 unsoldAmount, uint256 purchasedAmount);\n\n function cancelTermSwapTokenToETH(\n address token,\n uint256 orderId,\n uint256 deadline\n ) external returns (uint256 unsoldTokenAmount, uint256 purchasedETHAmount);\n\n function cancelTermSwapETHToToken(\n address token,\n uint256 orderId,\n uint256 deadline\n ) external returns (uint256 unsoldETHAmount, uint256 purchasedTokenAmount);\n\n function withdrawProceedsFromTermSwapTokenToToken(\n address token0,\n address token1,\n uint256 orderId,\n uint256 deadline\n ) external returns (uint256 proceeds);\n\n function withdrawProceedsFromTermSwapTokenToETH(\n address token,\n uint256 orderId,\n uint256 deadline\n ) external returns (uint256 proceedsETH);\n\n function withdrawProceedsFromTermSwapETHToToken(\n address token,\n uint256 orderId,\n uint256 deadline\n ) external returns (uint256 proceedsToken);\n\n function executeVirtualOrdersWrapper(address pair, uint256 blockNumber) external;\n}\n"
},
"contracts/banana/interfaces/ITWAMMPair.sol": {
"content": "// SPDX-License-Identifier: GPL-2.0-or-later\npragma solidity ^0.8.0;\n\ninterface ITWAMMPair {\n struct Order {\n uint256 id;\n uint256 expirationBlock;\n uint256 saleRate;\n address owner;\n address sellTokenId;\n address buyTokenId;\n }\n\n function getOrderDetails(uint256 orderId)\n external\n view\n returns (Order memory);\n}"
},
"contracts/utils/Ownable.sol": {
"content": "// SPDX-License-Identifier: GPL-2.0-or-later\npragma solidity ^0.8.0;\n\nabstract contract Ownable {\n address public owner;\n address public pendingOwner;\n\n event NewOwner(address indexed oldOwner, address indexed newOwner);\n event NewPendingOwner(address indexed oldPendingOwner, address indexed newPendingOwner);\n\n modifier onlyOwner() {\n require(msg.sender == owner, \"Ownable: REQUIRE_OWNER\");\n _;\n }\n\n function setPendingOwner(address newPendingOwner) external onlyOwner {\n require(pendingOwner != newPendingOwner, \"Ownable: ALREADY_SET\");\n emit NewPendingOwner(pendingOwner, newPendingOwner);\n pendingOwner = newPendingOwner;\n }\n\n function acceptOwner() external {\n require(msg.sender == pendingOwner, \"Ownable: REQUIRE_PENDING_OWNER\");\n address oldOwner = owner;\n address oldPendingOwner = pendingOwner;\n owner = pendingOwner;\n pendingOwner = address(0);\n emit NewOwner(oldOwner, owner);\n emit NewPendingOwner(oldPendingOwner, pendingOwner);\n }\n}\n"
},
"contracts/utils/AnalyticMath.sol": {
"content": "// SPDX-License-Identifier: GPL-2.0-or-later\npragma solidity ^0.8.0;\n\nimport \"../libraries/IntegralMath.sol\";\n\ncontract AnalyticMath {\n uint8 internal constant MIN_PRECISION = 32;\n uint8 internal constant MAX_PRECISION = 127;\n\n uint256 internal constant FIXED_1 = 1 << MAX_PRECISION;\n uint256 internal constant FIXED_2 = 2 << MAX_PRECISION;\n\n // Auto-generated via 'PrintLn2ScalingFactors.py'\n uint256 internal constant LN2_NUMERATOR = 0x3f80fe03f80fe03f80fe03f80fe03f8;\n uint256 internal constant LN2_DENOMINATOR = 0x5b9de1d10bf4103d647b0955897ba80;\n\n // Auto-generated via 'PrintOptimalThresholds.py'\n uint256 internal constant OPT_LOG_MAX_VAL = 0x15bf0a8b1457695355fb8ac404e7a79e4;\n uint256 internal constant OPT_EXP_MAX_VAL = 0x800000000000000000000000000000000;\n\n uint256[MAX_PRECISION + 1] private maxExpArray;\n\n /**\n * @dev Should be executed either during construction or after construction (if too large for the constructor)\n */\n constructor() {\n initMaxExpArray();\n }\n\n /**\n * @dev Compute (a / b) ^ (c / d)\n */\n function pow(uint256 a, uint256 b, uint256 c, uint256 d) internal view returns (uint256, uint256) { unchecked {\n if (a >= b)\n return mulDivExp(mulDivLog(FIXED_1, a, b), c, d);\n (uint256 q, uint256 p) = mulDivExp(mulDivLog(FIXED_1, b, a), c, d);\n return (p, q);\n }}\n\n /**\n * @dev Compute log(a / b)\n */\n function log(uint256 a, uint256 b) internal pure returns (uint256, uint256) { unchecked {\n require(a >= b, \"log: a < b\");\n return (mulDivLog(FIXED_1, a, b), FIXED_1);\n }}\n\n /**\n * @dev Compute e ^ (a / b)\n */\n function exp(uint256 a, uint256 b) internal view returns (uint256, uint256) { unchecked {\n return mulDivExp(FIXED_1, a, b);\n }}\n\n /**\n * @dev Compute log(x / FIXED_1) * FIXED_1\n */\n function fixedLog(uint256 x) internal pure returns (uint256) { unchecked {\n if (x < OPT_LOG_MAX_VAL) {\n return optimalLog(x);\n }\n else {\n return generalLog(x);\n }\n }}\n\n /**\n * @dev Compute e ^ (x / FIXED_1) * FIXED_1\n */\n function fixedExp(uint256 x) internal view returns (uint256, uint256) { unchecked {\n if (x < OPT_EXP_MAX_VAL) {\n return (optimalExp(x), 1 << MAX_PRECISION);\n }\n else {\n uint8 precision = findPosition(x);\n return (generalExp(x >> (MAX_PRECISION - precision), precision), 1 << precision);\n }\n }}\n\n /**\n * @dev Compute log(x / FIXED_1) * FIXED_1\n * This functions assumes that x >= FIXED_1, because the output would be negative otherwise\n */\n function generalLog(uint256 x) internal pure returns (uint256) { unchecked {\n uint256 res = 0;\n\n // if x >= 2, then we compute the integer part of log2(x), which is larger than 0\n if (x >= FIXED_2) {\n uint8 count = IntegralMath.floorLog2(x / FIXED_1);\n x >>= count; // now x < 2\n res = count * FIXED_1;\n }\n\n // if x > 1, then we compute the fraction part of log2(x), which is larger than 0\n if (x > FIXED_1) {\n for (uint8 i = MAX_PRECISION; i > 0; --i) {\n x = (x * x) / FIXED_1; // now 1 < x < 4\n if (x >= FIXED_2) {\n x >>= 1; // now 1 < x < 2\n res += 1 << (i - 1);\n }\n }\n }\n\n return res * LN2_NUMERATOR / LN2_DENOMINATOR;\n }}\n\n /**\n * @dev Approximate e ^ x as (x ^ 0) / 0! + (x ^ 1) / 1! + ... + (x ^ n) / n!\n * Auto-generated via 'PrintFunctionGeneralExp.py'\n * Detailed description:\n * - This function returns \"e ^ (x / 2 ^ precision) * 2 ^ precision\", that is, the result is upshifted for accuracy\n * - The global \"maxExpArray\" maps each \"precision\" to \"((maximumExponent + 1) << (MAX_PRECISION - precision)) - 1\"\n * - The maximum permitted value for \"x\" is therefore given by \"maxExpArray[precision] >> (MAX_PRECISION - precision)\"\n */\n function generalExp(uint256 x, uint8 precision) internal pure returns (uint256) { unchecked {\n uint256 xi = x;\n uint256 res = 0;\n\n xi = (xi * x) >> precision; res += xi * 0x3442c4e6074a82f1797f72ac0000000; // add x^02 * (33! / 02!)\n xi = (xi * x) >> precision; res += xi * 0x116b96f757c380fb287fd0e40000000; // add x^03 * (33! / 03!)\n xi = (xi * x) >> precision; res += xi * 0x045ae5bdd5f0e03eca1ff4390000000; // add x^04 * (33! / 04!)\n xi = (xi * x) >> precision; res += xi * 0x00defabf91302cd95b9ffda50000000; // add x^05 * (33! / 05!)\n xi = (xi * x) >> precision; res += xi * 0x002529ca9832b22439efff9b8000000; // add x^06 * (33! / 06!)\n xi = (xi * x) >> precision; res += xi * 0x00054f1cf12bd04e516b6da88000000; // add x^07 * (33! / 07!)\n xi = (xi * x) >> precision; res += xi * 0x0000a9e39e257a09ca2d6db51000000; // add x^08 * (33! / 08!)\n xi = (xi * x) >> precision; res += xi * 0x000012e066e7b839fa050c309000000; // add x^09 * (33! / 09!)\n xi = (xi * x) >> precision; res += xi * 0x000001e33d7d926c329a1ad1a800000; // add x^10 * (33! / 10!)\n xi = (xi * x) >> precision; res += xi * 0x0000002bee513bdb4a6b19b5f800000; // add x^11 * (33! / 11!)\n xi = (xi * x) >> precision; res += xi * 0x00000003a9316fa79b88eccf2a00000; // add x^12 * (33! / 12!)\n xi = (xi * x) >> precision; res += xi * 0x0000000048177ebe1fa812375200000; // add x^13 * (33! / 13!)\n xi = (xi * x) >> precision; res += xi * 0x0000000005263fe90242dcbacf00000; // add x^14 * (33! / 14!)\n xi = (xi * x) >> precision; res += xi * 0x000000000057e22099c030d94100000; // add x^15 * (33! / 15!)\n xi = (xi * x) >> precision; res += xi * 0x0000000000057e22099c030d9410000; // add x^16 * (33! / 16!)\n xi = (xi * x) >> precision; res += xi * 0x00000000000052b6b54569976310000; // add x^17 * (33! / 17!)\n xi = (xi * x) >> precision; res += xi * 0x00000000000004985f67696bf748000; // add x^18 * (33! / 18!)\n xi = (xi * x) >> precision; res += xi * 0x000000000000003dea12ea99e498000; // add x^19 * (33! / 19!)\n xi = (xi * x) >> precision; res += xi * 0x00000000000000031880f2214b6e000; // add x^20 * (33! / 20!)\n xi = (xi * x) >> precision; res += xi * 0x000000000000000025bcff56eb36000; // add x^21 * (33! / 21!)\n xi = (xi * x) >> precision; res += xi * 0x000000000000000001b722e10ab1000; // add x^22 * (33! / 22!)\n xi = (xi * x) >> precision; res += xi * 0x0000000000000000001317c70077000; // add x^23 * (33! / 23!)\n xi = (xi * x) >> precision; res += xi * 0x00000000000000000000cba84aafa00; // add x^24 * (33! / 24!)\n xi = (xi * x) >> precision; res += xi * 0x00000000000000000000082573a0a00; // add x^25 * (33! / 25!)\n xi = (xi * x) >> precision; res += xi * 0x00000000000000000000005035ad900; // add x^26 * (33! / 26!)\n xi = (xi * x) >> precision; res += xi * 0x000000000000000000000002f881b00; // add x^27 * (33! / 27!)\n xi = (xi * x) >> precision; res += xi * 0x0000000000000000000000001b29340; // add x^28 * (33! / 28!)\n xi = (xi * x) >> precision; res += xi * 0x00000000000000000000000000efc40; // add x^29 * (33! / 29!)\n xi = (xi * x) >> precision; res += xi * 0x0000000000000000000000000007fe0; // add x^30 * (33! / 30!)\n xi = (xi * x) >> precision; res += xi * 0x0000000000000000000000000000420; // add x^31 * (33! / 31!)\n xi = (xi * x) >> precision; res += xi * 0x0000000000000000000000000000021; // add x^32 * (33! / 32!)\n xi = (xi * x) >> precision; res += xi * 0x0000000000000000000000000000001; // add x^33 * (33! / 33!)\n\n return res / 0x688589cc0e9505e2f2fee5580000000 + x + (1 << precision); // divide by 33! and then add x^1 / 1! + x^0 / 0!\n }}\n\n /**\n * @dev Compute log(x / FIXED_1) * FIXED_1\n * Input range: FIXED_1 <= x <= OPT_LOG_MAX_VAL - 1\n * Auto-generated via 'PrintFunctionOptimalLog.py'\n * Detailed description:\n * - Rewrite the input as a product of natural exponents and a single residual r, such that 1 < r < 2\n * - The natural logarithm of each (pre-calculated) exponent is the degree of the exponent\n * - The natural logarithm of r is calculated via Taylor series for log(1 + x), where x = r - 1\n * - The natural logarithm of the input is calculated by summing up the intermediate results above\n * - For example: log(250) = log(e^4 * e^1 * e^0.5 * 1.021692859) = 4 + 1 + 0.5 + log(1 + 0.021692859)\n */\n function optimalLog(uint256 x) internal pure returns (uint256) { unchecked {\n uint256 res = 0;\n\n uint256 y;\n uint256 z;\n uint256 w;\n\n if (x >= 0xd3094c70f034de4b96ff7d5b6f99fcd9) {res += 0x40000000000000000000000000000000; x = x * FIXED_1 / 0xd3094c70f034de4b96ff7d5b6f99fcd9;} // add 1 / 2^1\n if (x >= 0xa45af1e1f40c333b3de1db4dd55f29a8) {res += 0x20000000000000000000000000000000; x = x * FIXED_1 / 0xa45af1e1f40c333b3de1db4dd55f29a8;} // add 1 / 2^2\n if (x >= 0x910b022db7ae67ce76b441c27035c6a2) {res += 0x10000000000000000000000000000000; x = x * FIXED_1 / 0x910b022db7ae67ce76b441c27035c6a2;} // add 1 / 2^3\n if (x >= 0x88415abbe9a76bead8d00cf112e4d4a9) {res += 0x08000000000000000000000000000000; x = x * FIXED_1 / 0x88415abbe9a76bead8d00cf112e4d4a9;} // add 1 / 2^4\n if (x >= 0x84102b00893f64c705e841d5d4064bd4) {res += 0x04000000000000000000000000000000; x = x * FIXED_1 / 0x84102b00893f64c705e841d5d4064bd4;} // add 1 / 2^5\n if (x >= 0x8204055aaef1c8bd5c3259f4822735a3) {res += 0x02000000000000000000000000000000; x = x * FIXED_1 / 0x8204055aaef1c8bd5c3259f4822735a3;} // add 1 / 2^6\n if (x >= 0x810100ab00222d861931c15e39b44e9a) {res += 0x01000000000000000000000000000000; x = x * FIXED_1 / 0x810100ab00222d861931c15e39b44e9a;} // add 1 / 2^7\n if (x >= 0x808040155aabbbe9451521693554f734) {res += 0x00800000000000000000000000000000; x = x * FIXED_1 / 0x808040155aabbbe9451521693554f734;} // add 1 / 2^8\n\n z = y = x - FIXED_1;\n w = y * y / FIXED_1;\n res += z * (0x100000000000000000000000000000000 - y) / 0x100000000000000000000000000000000; z = z * w / FIXED_1; // add y^01 / 01 - y^02 / 02\n res += z * (0x0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa - y) / 0x200000000000000000000000000000000; z = z * w / FIXED_1; // add y^03 / 03 - y^04 / 04\n res += z * (0x099999999999999999999999999999999 - y) / 0x300000000000000000000000000000000; z = z * w / FIXED_1; // add y^05 / 05 - y^06 / 06\n res += z * (0x092492492492492492492492492492492 - y) / 0x400000000000000000000000000000000; z = z * w / FIXED_1; // add y^07 / 07 - y^08 / 08\n res += z * (0x08e38e38e38e38e38e38e38e38e38e38e - y) / 0x500000000000000000000000000000000; z = z * w / FIXED_1; // add y^09 / 09 - y^10 / 10\n res += z * (0x08ba2e8ba2e8ba2e8ba2e8ba2e8ba2e8b - y) / 0x600000000000000000000000000000000; z = z * w / FIXED_1; // add y^11 / 11 - y^12 / 12\n res += z * (0x089d89d89d89d89d89d89d89d89d89d89 - y) / 0x700000000000000000000000000000000; z = z * w / FIXED_1; // add y^13 / 13 - y^14 / 14\n res += z * (0x088888888888888888888888888888888 - y) / 0x800000000000000000000000000000000; // add y^15 / 15 - y^16 / 16\n\n return res;\n }}\n\n /**\n * @dev Compute e ^ (x / FIXED_1) * FIXED_1\n * Input range: 0 <= x <= OPT_EXP_MAX_VAL - 1\n * Auto-generated via 'PrintFunctionOptimalExp.py'\n * Detailed description:\n * - Rewrite the input as a sum of binary exponents and a single residual r, as small as possible\n * - The exponentiation of each binary exponent is given (pre-calculated)\n * - The exponentiation of r is calculated via Taylor series for e^x, where x = r\n * - The exponentiation of the input is calculated by multiplying the intermediate results above\n * - For example: e^5.521692859 = e^(4 + 1 + 0.5 + 0.021692859) = e^4 * e^1 * e^0.5 * e^0.021692859\n */\n function optimalExp(uint256 x) internal pure returns (uint256) { unchecked {\n uint256 res = 0;\n\n uint256 y;\n uint256 z;\n\n z = y = x % 0x10000000000000000000000000000000; // get the input modulo 2^(-3)\n z = z * y / FIXED_1; res += z * 0x10e1b3be415a0000; // add y^02 * (20! / 02!)\n z = z * y / FIXED_1; res += z * 0x05a0913f6b1e0000; // add y^03 * (20! / 03!)\n z = z * y / FIXED_1; res += z * 0x0168244fdac78000; // add y^04 * (20! / 04!)\n z = z * y / FIXED_1; res += z * 0x004807432bc18000; // add y^05 * (20! / 05!)\n z = z * y / FIXED_1; res += z * 0x000c0135dca04000; // add y^06 * (20! / 06!)\n z = z * y / FIXED_1; res += z * 0x0001b707b1cdc000; // add y^07 * (20! / 07!)\n z = z * y / FIXED_1; res += z * 0x000036e0f639b800; // add y^08 * (20! / 08!)\n z = z * y / FIXED_1; res += z * 0x00000618fee9f800; // add y^09 * (20! / 09!)\n z = z * y / FIXED_1; res += z * 0x0000009c197dcc00; // add y^10 * (20! / 10!)\n z = z * y / FIXED_1; res += z * 0x0000000e30dce400; // add y^11 * (20! / 11!)\n z = z * y / FIXED_1; res += z * 0x000000012ebd1300; // add y^12 * (20! / 12!)\n z = z * y / FIXED_1; res += z * 0x0000000017499f00; // add y^13 * (20! / 13!)\n z = z * y / FIXED_1; res += z * 0x0000000001a9d480; // add y^14 * (20! / 14!)\n z = z * y / FIXED_1; res += z * 0x00000000001c6380; // add y^15 * (20! / 15!)\n z = z * y / FIXED_1; res += z * 0x000000000001c638; // add y^16 * (20! / 16!)\n z = z * y / FIXED_1; res += z * 0x0000000000001ab8; // add y^17 * (20! / 17!)\n z = z * y / FIXED_1; res += z * 0x000000000000017c; // add y^18 * (20! / 18!)\n z = z * y / FIXED_1; res += z * 0x0000000000000014; // add y^19 * (20! / 19!)\n z = z * y / FIXED_1; res += z * 0x0000000000000001; // add y^20 * (20! / 20!)\n res = res / 0x21c3677c82b40000 + y + FIXED_1; // divide by 20! and then add y^1 / 1! + y^0 / 0!\n\n if ((x & 0x010000000000000000000000000000000) != 0) res = res * 0x1c3d6a24ed82218787d624d3e5eba95f9 / 0x18ebef9eac820ae8682b9793ac6d1e776; // multiply by e^2^(-3)\n if ((x & 0x020000000000000000000000000000000) != 0) res = res * 0x18ebef9eac820ae8682b9793ac6d1e778 / 0x1368b2fc6f9609fe7aceb46aa619baed4; // multiply by e^2^(-2)\n if ((x & 0x040000000000000000000000000000000) != 0) res = res * 0x1368b2fc6f9609fe7aceb46aa619baed5 / 0x0bc5ab1b16779be3575bd8f0520a9f21f; // multiply by e^2^(-1)\n if ((x & 0x080000000000000000000000000000000) != 0) res = res * 0x0bc5ab1b16779be3575bd8f0520a9f21e / 0x0454aaa8efe072e7f6ddbab84b40a55c9; // multiply by e^2^(+0)\n if ((x & 0x100000000000000000000000000000000) != 0) res = res * 0x0454aaa8efe072e7f6ddbab84b40a55c5 / 0x00960aadc109e7a3bf4578099615711ea; // multiply by e^2^(+1)\n if ((x & 0x200000000000000000000000000000000) != 0) res = res * 0x00960aadc109e7a3bf4578099615711d7 / 0x0002bf84208204f5977f9a8cf01fdce3d; // multiply by e^2^(+2)\n if ((x & 0x400000000000000000000000000000000) != 0) res = res * 0x0002bf84208204f5977f9a8cf01fdc307 / 0x0000003c6ab775dd0b95b4cbee7e65d11; // multiply by e^2^(+3)\n\n return res;\n }}\n\n /**\n * @dev The global \"maxExpArray\" is sorted in descending order, and therefore the following statements are equivalent:\n * - This function finds the position of [the smallest value in \"maxExpArray\" larger than or equal to \"x\"]\n * - This function finds the highest position of [a value in \"maxExpArray\" larger than or equal to \"x\"]\n * This function supports the rational approximation of \"(a / b) ^ (c / d)\" via \"e ^ (log(a / b) * c / d)\".\n * The value of \"log(a / b)\" is represented with an integer slightly smaller than \"log(a / b) * 2 ^ precision\".\n * The larger \"precision\" is, the more accurately this value represents the real value.\n * However, the larger \"precision\" is, the more bits are required in order to store this value.\n * And the exponentiation function, which takes \"x\" and calculates \"e ^ x\", is limited to a maximum exponent (a maximum value of \"x\").\n * This maximum exponent depends on the \"precision\" used, and it is given by \"maxExpArray[precision] >> (MAX_PRECISION - precision)\".\n * Hence we need to determine the highest precision which can be used for the given input, before calling the exponentiation function.\n * This allows us to compute the result with maximum accuracy and without exceeding 256 bits in any of the intermediate computations.\n */\n function findPosition(uint256 x) internal view returns (uint8) { unchecked {\n uint8 lo = MIN_PRECISION;\n uint8 hi = MAX_PRECISION;\n\n while (lo + 1 < hi) {\n uint8 mid = (lo + hi) / 2;\n if (maxExpArray[mid] >= x)\n lo = mid;\n else\n hi = mid;\n }\n\n if (maxExpArray[hi] >= x)\n return hi;\n if (maxExpArray[lo] >= x)\n return lo;\n\n revert(\"findPosition: x > max\");\n }}\n\n /**\n * @dev Initialize internal data structure\n * Auto-generated via 'PrintMaxExpArray.py'\n */\n function initMaxExpArray() internal {\n // maxExpArray[ 0] = 0x6bffffffffffffffffffffffffffffffff;\n // maxExpArray[ 1] = 0x67ffffffffffffffffffffffffffffffff;\n // maxExpArray[ 2] = 0x637fffffffffffffffffffffffffffffff;\n // maxExpArray[ 3] = 0x5f6fffffffffffffffffffffffffffffff;\n // maxExpArray[ 4] = 0x5b77ffffffffffffffffffffffffffffff;\n // maxExpArray[ 5] = 0x57b3ffffffffffffffffffffffffffffff;\n // maxExpArray[ 6] = 0x5419ffffffffffffffffffffffffffffff;\n // maxExpArray[ 7] = 0x50a2ffffffffffffffffffffffffffffff;\n // maxExpArray[ 8] = 0x4d517fffffffffffffffffffffffffffff;\n // maxExpArray[ 9] = 0x4a233fffffffffffffffffffffffffffff;\n // maxExpArray[ 10] = 0x47165fffffffffffffffffffffffffffff;\n // maxExpArray[ 11] = 0x4429afffffffffffffffffffffffffffff;\n // maxExpArray[ 12] = 0x415bc7ffffffffffffffffffffffffffff;\n // maxExpArray[ 13] = 0x3eab73ffffffffffffffffffffffffffff;\n // maxExpArray[ 14] = 0x3c1771ffffffffffffffffffffffffffff;\n // maxExpArray[ 15] = 0x399e96ffffffffffffffffffffffffffff;\n // maxExpArray[ 16] = 0x373fc47fffffffffffffffffffffffffff;\n // maxExpArray[ 17] = 0x34f9e8ffffffffffffffffffffffffffff;\n // maxExpArray[ 18] = 0x32cbfd5fffffffffffffffffffffffffff;\n // maxExpArray[ 19] = 0x30b5057fffffffffffffffffffffffffff;\n // maxExpArray[ 20] = 0x2eb40f9fffffffffffffffffffffffffff;\n // maxExpArray[ 21] = 0x2cc8340fffffffffffffffffffffffffff;\n // maxExpArray[ 22] = 0x2af09481ffffffffffffffffffffffffff;\n // maxExpArray[ 23] = 0x292c5bddffffffffffffffffffffffffff;\n // maxExpArray[ 24] = 0x277abdcdffffffffffffffffffffffffff;\n // maxExpArray[ 25] = 0x25daf6657fffffffffffffffffffffffff;\n // maxExpArray[ 26] = 0x244c49c65fffffffffffffffffffffffff;\n // maxExpArray[ 27] = 0x22ce03cd5fffffffffffffffffffffffff;\n // maxExpArray[ 28] = 0x215f77c047ffffffffffffffffffffffff;\n // maxExpArray[ 29] = 0x1fffffffffffffffffffffffffffffffff;\n // maxExpArray[ 30] = 0x1eaefdbdabffffffffffffffffffffffff;\n // maxExpArray[ 31] = 0x1d6bd8b2ebffffffffffffffffffffffff;\n maxExpArray[ 32] = 0x1c35fedd14ffffffffffffffffffffffff;\n maxExpArray[ 33] = 0x1b0ce43b323fffffffffffffffffffffff;\n maxExpArray[ 34] = 0x19f0028ec1ffffffffffffffffffffffff;\n maxExpArray[ 35] = 0x18ded91f0e7fffffffffffffffffffffff;\n maxExpArray[ 36] = 0x17d8ec7f0417ffffffffffffffffffffff;\n maxExpArray[ 37] = 0x16ddc6556cdbffffffffffffffffffffff;\n maxExpArray[ 38] = 0x15ecf52776a1ffffffffffffffffffffff;\n maxExpArray[ 39] = 0x15060c256cb2ffffffffffffffffffffff;\n maxExpArray[ 40] = 0x1428a2f98d72ffffffffffffffffffffff;\n maxExpArray[ 41] = 0x13545598e5c23fffffffffffffffffffff;\n maxExpArray[ 42] = 0x1288c4161ce1dfffffffffffffffffffff;\n maxExpArray[ 43] = 0x11c592761c666fffffffffffffffffffff;\n maxExpArray[ 44] = 0x110a688680a757ffffffffffffffffffff;\n maxExpArray[ 45] = 0x1056f1b5bedf77ffffffffffffffffffff;\n maxExpArray[ 46] = 0x0faadceceeff8bffffffffffffffffffff;\n maxExpArray[ 47] = 0x0f05dc6b27edadffffffffffffffffffff;\n maxExpArray[ 48] = 0x0e67a5a25da4107fffffffffffffffffff;\n maxExpArray[ 49] = 0x0dcff115b14eedffffffffffffffffffff;\n maxExpArray[ 50] = 0x0d3e7a392431239fffffffffffffffffff;\n maxExpArray[ 51] = 0x0cb2ff529eb71e4fffffffffffffffffff;\n maxExpArray[ 52] = 0x0c2d415c3db974afffffffffffffffffff;\n maxExpArray[ 53] = 0x0bad03e7d883f69bffffffffffffffffff;\n maxExpArray[ 54] = 0x0b320d03b2c343d5ffffffffffffffffff;\n maxExpArray[ 55] = 0x0abc25204e02828dffffffffffffffffff;\n maxExpArray[ 56] = 0x0a4b16f74ee4bb207fffffffffffffffff;\n maxExpArray[ 57] = 0x09deaf736ac1f569ffffffffffffffffff;\n maxExpArray[ 58] = 0x0976bd9952c7aa957fffffffffffffffff;\n maxExpArray[ 59] = 0x09131271922eaa606fffffffffffffffff;\n maxExpArray[ 60] = 0x08b380f3558668c46fffffffffffffffff;\n maxExpArray[ 61] = 0x0857ddf0117efa215bffffffffffffffff;\n maxExpArray[ 62] = 0x07ffffffffffffffffffffffffffffffff;\n maxExpArray[ 63] = 0x07abbf6f6abb9d087fffffffffffffffff;\n maxExpArray[ 64] = 0x075af62cbac95f7dfa7fffffffffffffff;\n maxExpArray[ 65] = 0x070d7fb7452e187ac13fffffffffffffff;\n maxExpArray[ 66] = 0x06c3390ecc8af379295fffffffffffffff;\n maxExpArray[ 67] = 0x067c00a3b07ffc01fd6fffffffffffffff;\n maxExpArray[ 68] = 0x0637b647c39cbb9d3d27ffffffffffffff;\n maxExpArray[ 69] = 0x05f63b1fc104dbd39587ffffffffffffff;\n maxExpArray[ 70] = 0x05b771955b36e12f7235ffffffffffffff;\n maxExpArray[ 71] = 0x057b3d49dda84556d6f6ffffffffffffff;\n maxExpArray[ 72] = 0x054183095b2c8ececf30ffffffffffffff;\n maxExpArray[ 73] = 0x050a28be635ca2b888f77fffffffffffff;\n maxExpArray[ 74] = 0x04d5156639708c9db33c3fffffffffffff;\n maxExpArray[ 75] = 0x04a23105873875bd52dfdfffffffffffff;\n maxExpArray[ 76] = 0x0471649d87199aa990756fffffffffffff;\n maxExpArray[ 77] = 0x04429a21a029d4c1457cfbffffffffffff;\n maxExpArray[ 78] = 0x0415bc6d6fb7dd71af2cb3ffffffffffff;\n maxExpArray[ 79] = 0x03eab73b3bbfe282243ce1ffffffffffff;\n maxExpArray[ 80] = 0x03c1771ac9fb6b4c18e229ffffffffffff;\n maxExpArray[ 81] = 0x0399e96897690418f785257fffffffffff;\n maxExpArray[ 82] = 0x0373fc456c53bb779bf0ea9fffffffffff;\n maxExpArray[ 83] = 0x034f9e8e490c48e67e6ab8bfffffffffff;\n maxExpArray[ 84] = 0x032cbfd4a7adc790560b3337ffffffffff;\n maxExpArray[ 85] = 0x030b50570f6e5d2acca94613ffffffffff;\n maxExpArray[ 86] = 0x02eb40f9f620fda6b56c2861ffffffffff;\n maxExpArray[ 87] = 0x02cc8340ecb0d0f520a6af58ffffffffff;\n maxExpArray[ 88] = 0x02af09481380a0a35cf1ba02ffffffffff;\n maxExpArray[ 89] = 0x0292c5bdd3b92ec810287b1b3fffffffff;\n maxExpArray[ 90] = 0x0277abdcdab07d5a77ac6d6b9fffffffff;\n maxExpArray[ 91] = 0x025daf6654b1eaa55fd64df5efffffffff;\n maxExpArray[ 92] = 0x0244c49c648baa98192dce88b7ffffffff;\n maxExpArray[ 93] = 0x022ce03cd5619a311b2471268bffffffff;\n maxExpArray[ 94] = 0x0215f77c045fbe885654a44a0fffffffff;\n maxExpArray[ 95] = 0x01ffffffffffffffffffffffffffffffff;\n maxExpArray[ 96] = 0x01eaefdbdaaee7421fc4d3ede5ffffffff;\n maxExpArray[ 97] = 0x01d6bd8b2eb257df7e8ca57b09bfffffff;\n maxExpArray[ 98] = 0x01c35fedd14b861eb0443f7f133fffffff;\n maxExpArray[ 99] = 0x01b0ce43b322bcde4a56e8ada5afffffff;\n maxExpArray[100] = 0x019f0028ec1fff007f5a195a39dfffffff;\n maxExpArray[101] = 0x018ded91f0e72ee74f49b15ba527ffffff;\n maxExpArray[102] = 0x017d8ec7f04136f4e5615fd41a63ffffff;\n maxExpArray[103] = 0x016ddc6556cdb84bdc8d12d22e6fffffff;\n maxExpArray[104] = 0x015ecf52776a1155b5bd8395814f7fffff;\n maxExpArray[105] = 0x015060c256cb23b3b3cc3754cf40ffffff;\n maxExpArray[106] = 0x01428a2f98d728ae223ddab715be3fffff;\n maxExpArray[107] = 0x013545598e5c23276ccf0ede68034fffff;\n maxExpArray[108] = 0x01288c4161ce1d6f54b7f61081194fffff;\n maxExpArray[109] = 0x011c592761c666aa641d5a01a40f17ffff;\n maxExpArray[110] = 0x0110a688680a7530515f3e6e6cfdcdffff;\n maxExpArray[111] = 0x01056f1b5bedf75c6bcb2ce8aed428ffff;\n maxExpArray[112] = 0x00faadceceeff8a0890f3875f008277fff;\n maxExpArray[113] = 0x00f05dc6b27edad306388a600f6ba0bfff;\n maxExpArray[114] = 0x00e67a5a25da41063de1495d5b18cdbfff;\n maxExpArray[115] = 0x00dcff115b14eedde6fc3aa5353f2e4fff;\n maxExpArray[116] = 0x00d3e7a3924312399f9aae2e0f868f8fff;\n maxExpArray[117] = 0x00cb2ff529eb71e41582cccd5a1ee26fff;\n maxExpArray[118] = 0x00c2d415c3db974ab32a51840c0b67edff;\n maxExpArray[119] = 0x00bad03e7d883f69ad5b0a186184e06bff;\n maxExpArray[120] = 0x00b320d03b2c343d4829abd6075f0cc5ff;\n maxExpArray[121] = 0x00abc25204e02828d73c6e80bcdb1a95bf;\n maxExpArray[122] = 0x00a4b16f74ee4bb2040a1ec6c15fbbf2df;\n maxExpArray[123] = 0x009deaf736ac1f569deb1b5ae3f36c130f;\n maxExpArray[124] = 0x00976bd9952c7aa957f5937d790ef65037;\n maxExpArray[125] = 0x009131271922eaa6064b73a22d0bd4f2bf;\n maxExpArray[126] = 0x008b380f3558668c46c91c49a2f8e967b9;\n maxExpArray[127] = 0x00857ddf0117efa215952912839f6473e6;\n }\n\n // auxiliary function\n function mulDivLog(uint256 x, uint256 y, uint256 z) private pure returns (uint256) {\n return fixedLog(IntegralMath.mulDivF(x, y, z));\n }\n\n // auxiliary function\n function mulDivExp(uint256 x, uint256 y, uint256 z) private view returns (uint256, uint256) {\n return fixedExp(IntegralMath.mulDivF(x, y, z));\n }\n}\n"
},
"contracts/libraries/FullMath.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\n/// @title Contains 512-bit math functions\n/// @notice Facilitates multiplication and division that can have overflow of an intermediate value without any loss of precision\n/// @dev Handles \"phantom overflow\" i.e., allows multiplication and division where an intermediate value overflows 256 bits\nlibrary FullMath {\n /// @notice Calculates floor(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0\n /// @param a The multiplicand\n /// @param b The multiplier\n /// @param denominator The divisor\n /// @return result The 256-bit result\n /// @dev Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv\n function mulDiv(\n uint256 a,\n uint256 b,\n uint256 denominator\n ) internal pure returns (uint256 result) {\n // 512-bit multiply [prod1 prod0] = a * b\n // Compute the product mod 2**256 and mod 2**256 - 1\n // then use the Chinese Remainder Theorem to reconstruct\n // the 512 bit result. The result is stored in two 256\n // variables such that product = prod1 * 2**256 + prod0\n uint256 prod0; // Least significant 256 bits of the product\n uint256 prod1; // Most significant 256 bits of the product\n\n // todo unchecked\n unchecked {\n assembly {\n let mm := mulmod(a, b, not(0))\n prod0 := mul(a, b)\n prod1 := sub(sub(mm, prod0), lt(mm, prod0))\n }\n\n // Handle non-overflow cases, 256 by 256 division\n if (prod1 == 0) {\n require(denominator > 0);\n assembly {\n result := div(prod0, denominator)\n }\n return result;\n }\n\n // Make sure the result is less than 2**256.\n // Also prevents denominator == 0\n require(denominator > prod1);\n\n ///////////////////////////////////////////////\n // 512 by 256 division.\n ///////////////////////////////////////////////\n\n // Make division exact by subtracting the remainder from [prod1 prod0]\n // Compute remainder using mulmod\n uint256 remainder;\n assembly {\n remainder := mulmod(a, b, denominator)\n }\n // Subtract 256 bit number from 512 bit number\n assembly {\n prod1 := sub(prod1, gt(remainder, prod0))\n prod0 := sub(prod0, remainder)\n }\n\n // Factor powers of two out of denominator\n // Compute largest power of two divisor of denominator.\n // Always >= 1.\n uint256 twos = (~denominator + 1) & denominator;\n // Divide denominator by power of two\n assembly {\n denominator := div(denominator, twos)\n }\n\n // Divide [prod1 prod0] by the factors of two\n assembly {\n prod0 := div(prod0, twos)\n }\n // Shift in bits from prod1 into prod0. For this we need\n // to flip `twos` such that it is 2**256 / twos.\n // If twos is zero, then it becomes one\n assembly {\n twos := add(div(sub(0, twos), twos), 1)\n }\n\n prod0 |= prod1 * twos;\n\n // Invert denominator mod 2**256\n // Now that denominator is an odd number, it has an inverse\n // modulo 2**256 such that denominator * inv = 1 mod 2**256.\n // Compute the inverse by starting with a seed that is correct\n // correct for four bits. That is, denominator * inv = 1 mod 2**4\n uint256 inv = (3 * denominator) ^ 2;\n // Now use Newton-Raphson iteration to improve the precision.\n // Thanks to Hensel's lifting lemma, this also works in modular\n // arithmetic, doubling the correct bits in each step.\n\n inv *= 2 - denominator * inv; // inverse mod 2**8\n inv *= 2 - denominator * inv; // inverse mod 2**16\n inv *= 2 - denominator * inv; // inverse mod 2**32\n inv *= 2 - denominator * inv; // inverse mod 2**64\n inv *= 2 - denominator * inv; // inverse mod 2**128\n inv *= 2 - denominator * inv; // inverse mod 2**256\n\n // Because the division is now exact we can divide by multiplying\n // with the modular inverse of denominator. This will give us the\n // correct result modulo 2**256. Since the precoditions guarantee\n // that the outcome is less than 2**256, this is the final result.\n // We don't need to compute the high bits of the result and prod1\n // is no longer required.\n result = prod0 * inv;\n return result;\n }\n }\n\n /// @notice Calculates ceil(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0\n /// @param a The multiplicand\n /// @param b The multiplier\n /// @param denominator The divisor\n /// @return result The 256-bit result\n function mulDivRoundingUp(\n uint256 a,\n uint256 b,\n uint256 denominator\n ) internal pure returns (uint256 result) {\n result = mulDiv(a, b, denominator);\n if (mulmod(a, b, denominator) > 0) {\n require(result < type(uint256).max);\n result++;\n }\n }\n}\n"
},
"contracts/libraries/TransferHelper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\n// helper methods for interacting with ERC20 tokens and sending ETH that do not consistently return true/false\nlibrary TransferHelper {\n function safeApprove(\n address token,\n address to,\n uint256 value\n ) internal {\n // bytes4(keccak256(bytes('approve(address,uint256)')));\n (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));\n require(\n success && (data.length == 0 || abi.decode(data, (bool))),\n \"TransferHelper::safeApprove: approve failed\"\n );\n }\n\n function safeTransfer(\n address token,\n address to,\n uint256 value\n ) internal {\n // bytes4(keccak256(bytes('transfer(address,uint256)')));\n (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));\n require(\n success && (data.length == 0 || abi.decode(data, (bool))),\n \"TransferHelper::safeTransfer: transfer failed\"\n );\n }\n\n function safeTransferFrom(\n address token,\n address from,\n address to,\n uint256 value\n ) internal {\n // bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));\n (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));\n require(\n success && (data.length == 0 || abi.decode(data, (bool))),\n \"TransferHelper::transferFrom: transferFrom failed\"\n );\n }\n\n function safeTransferETH(address to, uint256 value) internal {\n (bool success, ) = to.call{value: value}(new bytes(0));\n require(success, \"TransferHelper::safeTransferETH: ETH transfer failed\");\n }\n}\n"
},
"contracts/libraries/IntegralMath.sol": {
"content": "// SPDX-License-Identifier: GPL-2.0-or-later\npragma solidity ^0.8.0;\n\nlibrary IntegralMath {\n uint256 constant MAX_VAL = type(uint256).max;\n\n // reverts on overflow\n function safeAdd(uint256 x, uint256 y) internal pure returns (uint256) {\n return x + y;\n }\n\n // does not revert on overflow\n function unsafeAdd(uint256 x, uint256 y) internal pure returns (uint256) { unchecked {\n return x + y;\n }}\n\n // does not revert on overflow\n function unsafeSub(uint256 x, uint256 y) internal pure returns (uint256) { unchecked {\n return x - y;\n }}\n\n // does not revert on overflow\n function unsafeMul(uint256 x, uint256 y) internal pure returns (uint256) { unchecked {\n return x * y;\n }}\n\n // does not overflow\n function mulModMax(uint256 x, uint256 y) internal pure returns (uint256) { unchecked {\n return mulmod(x, y, MAX_VAL);\n }}\n\n // does not overflow\n function mulMod(uint256 x, uint256 y, uint256 z) internal pure returns (uint256) { unchecked {\n return mulmod(x, y, z);\n }}\n /**\n * @dev Compute the largest integer smaller than or equal to the binary logarithm of `n`\n */\n function floorLog2(uint256 n) internal pure returns (uint8) { unchecked {\n uint8 res = 0;\n\n if (n < 256) {\n // at most 8 iterations\n while (n > 1) {\n n >>= 1;\n res += 1;\n }\n }\n else {\n // exactly 8 iterations\n for (uint8 s = 128; s > 0; s >>= 1) {\n if (n >= 1 << s) {\n n >>= s;\n res |= s;\n }\n }\n }\n\n return res;\n }}\n\n /**\n * @dev Compute the largest integer smaller than or equal to the square root of `n`\n */\n function floorSqrt(uint256 n) internal pure returns (uint256) { unchecked {\n if (n > 0) {\n uint256 x = n / 2 + 1;\n uint256 y = (x + n / x) / 2;\n while (x > y) {\n x = y;\n y = (x + n / x) / 2;\n }\n return x;\n }\n return 0;\n }}\n\n /**\n * @dev Compute the smallest integer larger than or equal to the square root of `n`\n */\n function ceilSqrt(uint256 n) internal pure returns (uint256) { unchecked {\n uint256 x = floorSqrt(n);\n return x ** 2 == n ? x : x + 1;\n }}\n\n /**\n * @dev Compute the largest integer smaller than or equal to the cubic root of `n`\n */\n function floorCbrt(uint256 n) internal pure returns (uint256) { unchecked {\n uint256 x = 0;\n for (uint256 y = 1 << 255; y > 0; y >>= 3) {\n x <<= 1;\n uint256 z = 3 * x * (x + 1) + 1;\n if (n / y >= z) {\n n -= y * z;\n x += 1;\n }\n }\n return x;\n }}\n\n /**\n * @dev Compute the smallest integer larger than or equal to the cubic root of `n`\n */\n function ceilCbrt(uint256 n) internal pure returns (uint256) { unchecked {\n uint256 x = floorCbrt(n);\n return x ** 3 == n ? x : x + 1;\n }}\n\n /**\n * @dev Compute the nearest integer to the quotient of `n` and `d` (or `n / d`)\n */\n function roundDiv(uint256 n, uint256 d) internal pure returns (uint256) { unchecked {\n return n / d + (n % d) / (d - d / 2);\n }}\n\n /**\n * @dev Compute the largest integer smaller than or equal to `x * y / z`\n */\n function mulDivF(uint256 x, uint256 y, uint256 z) internal pure returns (uint256) { unchecked {\n (uint256 xyh, uint256 xyl) = mul512(x, y);\n if (xyh == 0) { // `x * y < 2 ^ 256`\n return xyl / z;\n }\n if (xyh < z) { // `x * y / z < 2 ^ 256`\n uint256 m = mulMod(x, y, z); // `m = x * y % z`\n (uint256 nh, uint256 nl) = sub512(xyh, xyl, m); // `n = x * y - m` hence `n / z = floor(x * y / z)`\n if (nh == 0) { // `n < 2 ^ 256`\n return nl / z;\n }\n uint256 p = unsafeSub(0, z) & z; // `p` is the largest power of 2 which `z` is divisible by\n uint256 q = div512(nh, nl, p); // `n` is divisible by `p` because `n` is divisible by `z` and `z` is divisible by `p`\n uint256 r = inv256(z / p); // `z / p = 1 mod 2` hence `inverse(z / p) = 1 mod 2 ^ 256`\n return unsafeMul(q, r); // `q * r = (n / p) * inverse(z / p) = n / z`\n }\n revert(); // `x * y / z >= 2 ^ 256`\n }}\n\n /**\n * @dev Compute the smallest integer larger than or equal to `x * y / z`\n */\n function mulDivC(uint256 x, uint256 y, uint256 z) internal pure returns (uint256) { unchecked {\n uint256 w = mulDivF(x, y, z);\n if (mulMod(x, y, z) > 0)\n return safeAdd(w, 1);\n return w;\n }}\n\n /**\n * @dev Compute the value of `x * y`\n */\n function mul512(uint256 x, uint256 y) private pure returns (uint256, uint256) { unchecked {\n uint256 p = mulModMax(x, y);\n uint256 q = unsafeMul(x, y);\n if (p >= q)\n return (p - q, q);\n return (unsafeSub(p, q) - 1, q);\n }}\n\n /**\n * @dev Compute the value of `2 ^ 256 * xh + xl - y`, where `2 ^ 256 * xh + xl >= y`\n */\n function sub512(uint256 xh, uint256 xl, uint256 y) private pure returns (uint256, uint256) { unchecked {\n if (xl >= y)\n return (xh, xl - y);\n return (xh - 1, unsafeSub(xl, y));\n }}\n\n /**\n * @dev Compute the value of `(2 ^ 256 * xh + xl) / pow2n`, where `xl` is divisible by `pow2n`\n */\n function div512(uint256 xh, uint256 xl, uint256 pow2n) private pure returns (uint256) { unchecked {\n uint256 pow2nInv = unsafeAdd(unsafeSub(0, pow2n) / pow2n, 1); // `1 << (256 - n)`\n return unsafeMul(xh, pow2nInv) | (xl / pow2n); // `(xh << (256 - n)) | (xl >> n)`\n }}\n\n /**\n * @dev Compute the inverse of `d` modulo `2 ^ 256`, where `d` is congruent to `1` modulo `2`\n */\n function inv256(uint256 d) private pure returns (uint256) { unchecked {\n // approximate the root of `f(x) = 1 / x - d` using the newton–raphson convergence method\n uint256 x = 1;\n for (uint256 i = 0; i < 8; ++i)\n x = unsafeMul(x, unsafeSub(2, unsafeMul(x, d))); // `x = x * (2 - x * d) mod 2 ^ 256`\n return x;\n }}\n}\n"
}
},
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"evm.bytecode",
"evm.deployedBytecode",
"devdoc",
"userdoc",
"metadata",
"abi"
]
}
},
"metadata": {
"useLiteralContent": true
},
"libraries": {}
}
}