More Info
Private Name Tags
ContractCreator
Latest 25 from a total of 6,747 transactions
Transaction Hash |
Method
|
Block
|
From
|
To
|
|||||
---|---|---|---|---|---|---|---|---|---|
Create | 647060 | 25 mins ago | IN | 5 S | 0.0044211 | ||||
Sell | 645311 | 50 mins ago | IN | 0 S | 0.0002635 | ||||
Buy | 643839 | 1 hr ago | IN | 10 S | 0.00027831 | ||||
Buy | 642882 | 1 hr ago | IN | 3 S | 0.00028443 | ||||
Buy | 642719 | 1 hr ago | IN | 200 S | 0.00023805 | ||||
Buy | 642357 | 1 hr ago | IN | 50 S | 0.00026327 | ||||
Buy | 642299 | 1 hr ago | IN | 60 S | 0.00025515 | ||||
Buy | 641980 | 1 hr ago | IN | 100 S | 0.00028537 | ||||
Buy | 641687 | 1 hr ago | IN | 15 S | 0.00028678 | ||||
Sell | 626329 | 4 hrs ago | IN | 0 S | 0.00026915 | ||||
Buy | 625406 | 4 hrs ago | IN | 250 S | 0.00028974 | ||||
Sell | 614056 | 6 hrs ago | IN | 0 S | 0.0006053 | ||||
Sell | 611598 | 6 hrs ago | IN | 0 S | 0.00026962 | ||||
Buy | 609366 | 7 hrs ago | IN | 0.25 S | 0.00026233 | ||||
Buy | 605171 | 7 hrs ago | IN | 20 S | 0.00027925 | ||||
Buy | 602896 | 8 hrs ago | IN | 50 S | 0.00026185 | ||||
Buy | 601789 | 8 hrs ago | IN | 0.1 S | 0.00027925 | ||||
Buy | 601518 | 8 hrs ago | IN | 0.5 S | 0.00026233 | ||||
Buy | 601397 | 8 hrs ago | IN | 50 S | 0.00027535 | ||||
Buy | 601274 | 8 hrs ago | IN | 30 S | 0.00027878 | ||||
Buy | 598739 | 8 hrs ago | IN | 10 S | 0.0002882 | ||||
Buy | 598368 | 8 hrs ago | IN | 100 S | 0.00028255 | ||||
Buy | 598126 | 8 hrs ago | IN | 10 S | 0.00028443 | ||||
Buy | 598062 | 8 hrs ago | IN | 5 S | 0.00028867 | ||||
Create | 597832 | 8 hrs ago | IN | 20 S | 0.00202026 |
Latest 25 internal transactions (View All)
Parent Transaction Hash | Block | From | To | |||
---|---|---|---|---|---|---|
647060 | 25 mins ago | 0.05 S | ||||
647060 | 25 mins ago | Contract Creation | 0 S | |||
645311 | 50 mins ago | 160.91853902 S | ||||
645311 | 50 mins ago | 1.62543978 S | ||||
643839 | 1 hr ago | 0.1 S | ||||
642882 | 1 hr ago | 0.03 S | ||||
642719 | 1 hr ago | 2 S | ||||
642357 | 1 hr ago | 0.5 S | ||||
642299 | 1 hr ago | 0.6 S | ||||
641980 | 1 hr ago | 1 S | ||||
641687 | 1 hr ago | 0.15 S | ||||
626329 | 4 hrs ago | 25.86517129 S | ||||
626329 | 4 hrs ago | 0.26126435 S | ||||
625406 | 4 hrs ago | 2.5 S | ||||
614056 | 6 hrs ago | 54.24291733 S | ||||
614056 | 6 hrs ago | 0.54790825 S | ||||
611598 | 6 hrs ago | 10.26073147 S | ||||
611598 | 6 hrs ago | 0.10364375 S | ||||
609366 | 7 hrs ago | 0.0025 S | ||||
605171 | 7 hrs ago | 0.2 S | ||||
602896 | 8 hrs ago | 0.5 S | ||||
601789 | 8 hrs ago | 0.001 S | ||||
601518 | 8 hrs ago | 0.005 S | ||||
601397 | 8 hrs ago | 0.5 S | ||||
601274 | 8 hrs ago | 0.3 S |
Loading...
Loading
Contract Source Code Verified (Exact Match)
Contract Name:
BondingCurveManager
Compiler Version
v0.8.28+commit.7893614a
Optimization Enabled:
No with 200 runs
Other Settings:
paris EvmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import { BancorBondingCurve } from "./gate/BancorBondingCurve.sol"; import "./BondingCurveToken.sol"; import "./utils/owner/Ownable.sol"; import "./interfaces/IUniswapV2Router02.sol"; import "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; /** * @title BondingCurveManager * @dev Manages bonding curve tokens, allowing creation, buying, selling, and liquidity management. */ contract BondingCurveManager is Ownable, ReentrancyGuard { BancorBondingCurve private bancorFormula; IUniswapV2Router02 private uniRouter; struct TokenInfo { BondingCurveToken token; uint256 tokenbalance; uint256 ethBalance; bool isListed; } mapping(address => TokenInfo) public tokens; address[] public tokenList; uint256 private constant FEE_PERCENTAGE = 1e16; // 1% = 1e16 uint256 private LP_FEE_PERCENTAGE = 5e16; // 5% = 5e16 uint256 private MAX_POOL_BALANCE = 14500 ether; address private immutable LP_BURN_ADDR = 0x000000000000000000000000000000000000dEaD; address payable private feeRecipient; // Events event TokenCreated(address indexed tokenAddress, address indexed creator, string name, string symbol); event TokensBought(address indexed token, address indexed buyer, uint256 ethAmount, uint256 tokenAmount); event TokensSold(address indexed token, address indexed seller, uint256 tokenAmount, uint256 ethAmount); event LiquidityAdded(address indexed token, uint256 ethAmount, uint256 tokenAmount); // Custom Errors error TokenDoesNotExist(); error TokenAlreadyListed(); error ZeroEthSent(); error ZeroTokenAmount(); error FailedToSendEth(); error MaxPoolBalanceReached(); error InsufficientPoolbalance(); error TokenTransferFailed(); error InvalidRecipient(); error InvalidLpFeePercentage(); error PairCreationFailed(); /** * @dev Constructor initializes the contract with the Uniswap router, BancorFormula1 address, and fee recipient. * @param _uniRouter Address of the Uniswap V2 Router. * @param _bancorFormula Address of the deployed BancorFormula1 contract. * @param _feeRecipient Address to receive the fees. */ constructor( address _uniRouter, address _bancorFormula, address payable _feeRecipient ) { uniRouter = IUniswapV2Router02(_uniRouter); bancorFormula = BancorBondingCurve(_bancorFormula); feeRecipient = _feeRecipient; } /** * @notice Creates a new bonding curve token. * @param name The name of the new token. * @param symbol The symbol of the new token. */ function create(string calldata name, string calldata symbol) external payable nonReentrant { BondingCurveToken newToken = new BondingCurveToken(name, symbol); address tokenAddress = address(newToken); tokens[tokenAddress] = TokenInfo({ token: newToken, tokenbalance: 0, ethBalance: 0, isListed: false }); tokenList.push(tokenAddress); // Transfer the trading supply from the token to this manager contract newToken.transferTradingSupply(address(this)); // Verify that tokens were successfully transferred uint256 transferredAmount = newToken.balanceOf(address(this)); if (transferredAmount == 0) revert TokenTransferFailed(); tokens[tokenAddress].tokenbalance = transferredAmount; emit TokenCreated(tokenAddress, msg.sender, name, symbol); // If ETH is sent during token creation, buy tokens on behalf of the creator if (msg.value > 0) { buyTokenForCreator(tokenAddress, msg.value); } } /** * @notice Buys tokens for a specified token address. * @param tokenAddress The address of the token to buy. */ function buy(address tokenAddress) external payable nonReentrant { TokenInfo storage tokenInfo = tokens[tokenAddress]; BondingCurveToken token = tokenInfo.token; if (address(token) == address(0)) revert TokenDoesNotExist(); if (tokenInfo.isListed) revert TokenAlreadyListed(); if (msg.value == 0) revert ZeroEthSent(); uint256 currentEthBalance = tokenInfo.ethBalance; uint256 remainingEthToMax = MAX_POOL_BALANCE > currentEthBalance ? MAX_POOL_BALANCE - currentEthBalance : 0; if (remainingEthToMax == 0) revert MaxPoolBalanceReached(); uint256 availableTokens = tokenInfo.tokenbalance; uint256 totalSupply = token.TRADING_SUPPLY() - availableTokens; uint256 feeDenominator = 1e18 - FEE_PERCENTAGE; uint256 maxActualEthContribution = (remainingEthToMax * 1e18) / feeDenominator; uint256 actualEthContribution = msg.value > maxActualEthContribution ? maxActualEthContribution : msg.value; // Calculate fee and ETH to be used for purchasing tokens uint256 fee = calculateFee(actualEthContribution, FEE_PERCENTAGE); uint256 ethForTokens = actualEthContribution - fee; // Calculate the number of tokens the user can buy with ethForTokens uint256 tokensToTransfer = bancorFormula.computeMintingAmountFromPrice(currentEthBalance, totalSupply, ethForTokens); // If tokensToTransfer exceeds availableTokens, adjust tokensToTransfer without recalculating fee and ethForTokens if (tokensToTransfer > availableTokens) { tokensToTransfer = availableTokens; // Calculate ethForTokens based on tokensToTransfer without recalculating ethForTokens = bancorFormula.computePriceForMinting(currentEthBalance, totalSupply, tokensToTransfer); fee = calculateFee(ethForTokens, FEE_PERCENTAGE); actualEthContribution = ethForTokens + fee; } // Update balances tokenInfo.ethBalance = currentEthBalance + ethForTokens; tokenInfo.tokenbalance -= tokensToTransfer; // Transfer fee to feeRecipient if (fee > 0) { (bool feeSent, ) = feeRecipient.call{value: fee}(""); if (!feeSent) revert FailedToSendEth(); } // Transfer tokens to buyer if (!token.transfer(msg.sender, tokensToTransfer)) { revert TokenTransferFailed(); } // Refund excess ETH if any uint256 excessEth = msg.value > actualEthContribution ? msg.value - actualEthContribution : 0; if (excessEth > 0) { (bool sent, ) = msg.sender.call{value: excessEth}(""); if (!sent) revert FailedToSendEth(); } emit TokensBought(tokenAddress, msg.sender, ethForTokens, tokensToTransfer); // **New Liquidity Check Using Internal Function** if (shouldAddLiquidity(tokenInfo)) { _addLiquidity(tokenAddress); } } /** * @notice Sells tokens for a specified token address. * @param tokenAddress The address of the token to sell. * @param tokenAmount The amount of tokens to sell. */ function sell(address tokenAddress, uint256 tokenAmount) external nonReentrant { TokenInfo storage tokenInfo = tokens[tokenAddress]; BondingCurveToken token = tokenInfo.token; if (address(token) == address(0)) revert TokenDoesNotExist(); if (tokenInfo.isListed) revert TokenAlreadyListed(); if (tokenAmount == 0) revert ZeroTokenAmount(); uint256 currentEthBalance = tokenInfo.ethBalance; uint256 availableTokens = tokenInfo.tokenbalance; uint256 totalSupply = token.TRADING_SUPPLY() - availableTokens; uint256 ethToReturn = bancorFormula.computeRefundForBurning(currentEthBalance, totalSupply, tokenAmount); uint256 fee = calculateFee(ethToReturn, FEE_PERCENTAGE); uint256 ethAfterFee = ethToReturn - fee; if (currentEthBalance < ethToReturn) revert InsufficientPoolbalance(); unchecked { tokenInfo.ethBalance -= ethToReturn; tokenInfo.tokenbalance += tokenAmount; } if (fee > 0) { (bool feeSent, ) = feeRecipient.call{value: fee}(""); if (!feeSent) revert FailedToSendEth(); } if (!token.transferFrom(msg.sender, address(this), tokenAmount)) { revert TokenTransferFailed(); } (bool sent, ) = msg.sender.call{value: ethAfterFee}(""); if (!sent) revert FailedToSendEth(); emit TokensSold(tokenAddress, msg.sender, tokenAmount, ethAfterFee); } /** * @dev Buys tokens on behalf of the creator during token creation. * @param tokenAddress The address of the token. * @param ethAmount The amount of ETH sent. */ function buyTokenForCreator(address tokenAddress, uint256 ethAmount) internal { TokenInfo storage tokenInfo = tokens[tokenAddress]; BondingCurveToken token = tokenInfo.token; uint256 currentEthBalance = tokenInfo.ethBalance; uint256 availableTokens = tokenInfo.tokenbalance; uint256 totalSupply = token.TRADING_SUPPLY() - availableTokens; uint256 fee = calculateFee(ethAmount, FEE_PERCENTAGE); uint256 ethForTokens = ethAmount - fee; uint256 tokensToTransfer = bancorFormula.computeMintingAmountFromPrice(currentEthBalance, totalSupply, ethForTokens); // Ensure that the tokens purchased do not exceed of the trading supply if (tokensToTransfer > availableTokens) { tokensToTransfer = availableTokens; ethForTokens = bancorFormula.computePriceForMinting( currentEthBalance, totalSupply, tokensToTransfer ); fee = calculateFee(ethForTokens, FEE_PERCENTAGE); } // Update token eth balance/pool tokenInfo.ethBalance += ethForTokens; tokenInfo.tokenbalance -= tokensToTransfer; if (fee > 0) { (bool feeSent, ) = feeRecipient.call{value: fee}(""); if (!feeSent) revert FailedToSendEth(); } if (!token.transfer(msg.sender, tokensToTransfer)) { revert TokenTransferFailed(); } uint256 excessEth = ethAmount > ethForTokens + fee ? ethAmount - (ethForTokens + fee) : 0; if (excessEth > 0) { (bool sent, ) = msg.sender.call{value: excessEth}(""); if (!sent) revert FailedToSendEth(); } emit TokensBought(tokenAddress, msg.sender, ethForTokens, tokensToTransfer); } function _addLiquidity(address tokenAddress) internal { TokenInfo storage tokenInfo = tokens[tokenAddress]; BondingCurveToken token = tokenInfo.token; if (tokenInfo.isListed) revert TokenAlreadyListed(); uint256 totalEthBalance = tokenInfo.ethBalance; uint256 lpFee = calculateFee(totalEthBalance, LP_FEE_PERCENTAGE); uint256 ethForLiquidity = totalEthBalance - lpFee; token.transferLPSupply(address(this)); uint256 tokensForLiquidity = token.LP_SUPPLY() + tokenInfo.tokenbalance; // Update state variables before external calls to prevent re-entrancy tokenInfo.tokenbalance = 0; tokenInfo.ethBalance = 0; tokenInfo.isListed = true; // Transfer LP fee to the fee recipient if (lpFee > 0) { (bool feeSent, ) = feeRecipient.call{value: lpFee}(""); if (!feeSent) revert FailedToSendEth(); } token.approve(address(uniRouter), tokensForLiquidity); // Add liquidity to Uniswap (uint256 amountToken, uint256 amountETH, ) = uniRouter.addLiquidityETH{value: ethForLiquidity}( tokenAddress, tokensForLiquidity, 0, 0, LP_BURN_ADDR, block.timestamp ); token.renounceOwnership(); emit LiquidityAdded(tokenAddress, amountETH, amountToken); } /** * @dev Manually adds liquidity for a specified token. * Can only be called by the contract owner. * * @param tokenAddress The address of the token to add liquidity for. * To be used in situations where the bond owner wants to shutdown and * ensure tokens are safely moved to DEX. */ function addLP(address tokenAddress) external onlyOwner nonReentrant { TokenInfo storage tokenInfo = tokens[tokenAddress]; BondingCurveToken token = tokenInfo.token; if (address(token) == address(0)) revert TokenDoesNotExist(); if (tokenInfo.isListed) revert TokenAlreadyListed(); _addLiquidity(tokenAddress); } /** * @notice Sets the LP fee percentage. * @param _lpFeePercentage The new LP fee percentage in WAD (must not exceed 5%). */ function setLpFeePercentage(uint256 _lpFeePercentage) external onlyOwner { if (_lpFeePercentage > 5e16) revert InvalidLpFeePercentage(); LP_FEE_PERCENTAGE = _lpFeePercentage; } /** * @notice Retrieves the ETH balance for a specific token. * @param tokenAddress The address of the token. * @return The ETH balance of the token pool. */ function getTokenEthBalance(address tokenAddress) external view returns (uint256) { return tokens[tokenAddress].ethBalance; } /** * @notice Updates the fee recipient address. * @param _newRecipient The new address to receive fees. */ function setFeeRecipient(address payable _newRecipient) external onlyOwner { if (_newRecipient == address(0)) revert InvalidRecipient(); feeRecipient = _newRecipient; } function setUniRouter(address _uniRouter) external onlyOwner { uniRouter = IUniswapV2Router02(_uniRouter); } function setBancorFormula(address _bancorFormula) external onlyOwner { bancorFormula = BancorBondingCurve(_bancorFormula); } function setMaxPoolBalance(uint256 _maxPoolBalance) external onlyOwner { MAX_POOL_BALANCE = _maxPoolBalance; } /** * @dev Checks if liquidity should be added based on token balance or ETH balance. * @param tokenInfo The TokenInfo struct containing token details. * @return True if tokenbalance is zero or ethBalance is >= 99% of MAX_POOL_BALANCE, else false. */ function shouldAddLiquidity(TokenInfo storage tokenInfo) internal view returns (bool) { uint256 ninetyNinePercent = (MAX_POOL_BALANCE * 99) / 100; return (tokenInfo.tokenbalance == 0 || tokenInfo.ethBalance >= ninetyNinePercent); } /** * @notice Calculates the number of tokens that can be purchased for a given amount of ETH. * @param tokenAddress The address of the token. * @param ethAmount The amount of ETH to spend. * @return The number of tokens that can be purchased. */ function calculateCurvedBuyReturn(address tokenAddress, uint256 ethAmount) public view returns (uint256) { TokenInfo storage tokenInfo = tokens[tokenAddress]; BondingCurveToken token = tokenInfo.token; if (address(token) == address(0)) revert TokenDoesNotExist(); if (tokenInfo.isListed) revert TokenAlreadyListed(); if (ethAmount == 0) revert ZeroEthSent(); uint256 currentEthBalance = tokenInfo.ethBalance; uint256 fee = calculateFee(ethAmount, FEE_PERCENTAGE); uint256 ethForTokens = ethAmount - fee; uint256 availableTokens = tokenInfo.tokenbalance; uint256 totalSupply = token.TRADING_SUPPLY() - availableTokens; return bancorFormula.computeMintingAmountFromPrice( currentEthBalance, totalSupply, ethForTokens ); } /** * @notice Calculates the amount of ETH that will be returned for selling a given amount of tokens. * @param tokenAddress The address of the token. * @param tokenAmount The amount of tokens to sell. * @return The amount of ETH that will be returned after fees. */ function calculateCurvedSellReturn(address tokenAddress, uint256 tokenAmount) public view returns (uint256) { TokenInfo storage tokenInfo = tokens[tokenAddress]; BondingCurveToken token = tokenInfo.token; uint256 currentEthBalance = tokenInfo.ethBalance; if (address(token) == address(0)) revert TokenDoesNotExist(); if (tokenInfo.isListed) revert TokenAlreadyListed(); if (tokenAmount == 0) revert ZeroTokenAmount(); uint256 availableTokens = tokenInfo.tokenbalance; uint256 totalSupply = token.TRADING_SUPPLY() - availableTokens; uint256 ethToReturn = bancorFormula.computeRefundForBurning( currentEthBalance, totalSupply, tokenAmount ); uint256 fee = calculateFee(ethToReturn, FEE_PERCENTAGE); return ethToReturn - fee; } /** * @dev Calculates the current price of a token based on its supply. * @param tokenSupply The current supply of the token. * @return The current price of the token in ETH. */ function calculateCurrentPrice(uint256 currentEthBalance,uint256 tokenSupply) internal view returns (uint256) { uint256 tokenAmount = 1e18; uint256 ethAmount = bancorFormula.computePriceForMinting(currentEthBalance, tokenSupply, tokenAmount); uint256 fee = calculateFee(ethAmount, FEE_PERCENTAGE); return ethAmount - fee; } /** * @notice Retrieves the current price of a specific token. * @param tokenAddress The address of the token. * @return The current price of the token in ETH. */ function getCurrentTokenPrice(address tokenAddress) public view returns (uint256) { TokenInfo storage tokenInfo = tokens[tokenAddress]; BondingCurveToken token = tokenInfo.token; if (address(token) == address(0)) revert TokenDoesNotExist(); if (tokenInfo.isListed) revert TokenAlreadyListed(); uint256 currentEthBalance = tokenInfo.ethBalance; uint256 availableTokens = tokenInfo.tokenbalance; uint256 totalSupply = token.TRADING_SUPPLY() - availableTokens; return calculateCurrentPrice( currentEthBalance, totalSupply ); } /** * @notice Calculates the market capitalization of a specific token. * @param tokenAddress The address of the token. * @return The market capitalization of the token in ETH. */ function getMarketCap(address tokenAddress) public view returns (uint256) { TokenInfo storage tokenInfo = tokens[tokenAddress]; BondingCurveToken token = tokenInfo.token; if (address(token) == address(0)) revert TokenDoesNotExist(); if (tokenInfo.isListed) revert TokenAlreadyListed(); uint256 availableTokens = tokenInfo.tokenbalance; uint256 circulatingSupply = token.TRADING_SUPPLY() - availableTokens; uint256 currentPrice = getCurrentTokenPrice(tokenAddress); // Market cap: circulatingSupply * currentPrice uint256 marketCap = (circulatingSupply * currentPrice) / 1e18; return marketCap; } function calculateFee( uint256 _amount, uint256 _feePercent ) internal pure returns (uint256) { return (_amount * _feePercent) / 1e18; } /** * @notice Fallback function to accept ETH. */ receive() external payable {} }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (utils/math/Math.sol) pragma solidity ^0.8.20; import {Panic} from "../Panic.sol"; import {SafeCast} from "./SafeCast.sol"; /** * @dev Standard math utilities missing in the Solidity language. */ library Math { enum Rounding { Floor, // Toward negative infinity Ceil, // Toward positive infinity Trunc, // Toward zero Expand // Away from zero } /** * @dev Returns the addition of two unsigned integers, with an success flag (no overflow). */ function tryAdd(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) { unchecked { uint256 c = a + b; if (c < a) return (false, 0); return (true, c); } } /** * @dev Returns the subtraction of two unsigned integers, with an success flag (no overflow). */ function trySub(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) { unchecked { if (b > a) return (false, 0); return (true, a - b); } } /** * @dev Returns the multiplication of two unsigned integers, with an success flag (no overflow). */ function tryMul(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) { unchecked { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) return (true, 0); uint256 c = a * b; if (c / a != b) return (false, 0); return (true, c); } } /** * @dev Returns the division of two unsigned integers, with a success flag (no division by zero). */ function tryDiv(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) { unchecked { if (b == 0) return (false, 0); return (true, a / b); } } /** * @dev Returns the remainder of dividing two unsigned integers, with a success flag (no division by zero). */ function tryMod(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) { unchecked { if (b == 0) return (false, 0); return (true, a % b); } } /** * @dev Branchless ternary evaluation for `a ? b : c`. Gas costs are constant. * * IMPORTANT: This function may reduce bytecode size and consume less gas when used standalone. * However, the compiler may optimize Solidity ternary operations (i.e. `a ? b : c`) to only compute * one branch when needed, making this function more expensive. */ function ternary(bool condition, uint256 a, uint256 b) internal pure returns (uint256) { unchecked { // branchless ternary works because: // b ^ (a ^ b) == a // b ^ 0 == b return b ^ ((a ^ b) * SafeCast.toUint(condition)); } } /** * @dev Returns the largest of two numbers. */ function max(uint256 a, uint256 b) internal pure returns (uint256) { return ternary(a > b, a, b); } /** * @dev Returns the smallest of two numbers. */ function min(uint256 a, uint256 b) internal pure returns (uint256) { return ternary(a < b, a, b); } /** * @dev Returns the average of two numbers. The result is rounded towards * zero. */ function average(uint256 a, uint256 b) internal pure returns (uint256) { // (a + b) / 2 can overflow. return (a & b) + (a ^ b) / 2; } /** * @dev Returns the ceiling of the division of two numbers. * * This differs from standard division with `/` in that it rounds towards infinity instead * of rounding towards zero. */ function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) { if (b == 0) { // Guarantee the same behavior as in a regular Solidity division. Panic.panic(Panic.DIVISION_BY_ZERO); } // The following calculation ensures accurate ceiling division without overflow. // Since a is non-zero, (a - 1) / b will not overflow. // The largest possible result occurs when (a - 1) / b is type(uint256).max, // but the largest value we can obtain is type(uint256).max - 1, which happens // when a = type(uint256).max and b = 1. unchecked { return SafeCast.toUint(a > 0) * ((a - 1) / b + 1); } } /** * @dev Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or * denominator == 0. * * Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv) with further edits by * Uniswap Labs also under MIT license. */ function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) { unchecked { // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2²⁵⁶ and mod 2²⁵⁶ - 1, then use // the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256 // variables such that product = prod1 * 2²⁵⁶ + prod0. uint256 prod0 = x * y; // Least significant 256 bits of the product uint256 prod1; // Most significant 256 bits of the product assembly { let mm := mulmod(x, y, not(0)) prod1 := sub(sub(mm, prod0), lt(mm, prod0)) } // Handle non-overflow cases, 256 by 256 division. if (prod1 == 0) { // Solidity will revert if denominator == 0, unlike the div opcode on its own. // The surrounding unchecked block does not change this fact. // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic. return prod0 / denominator; } // Make sure the result is less than 2²⁵⁶. Also prevents denominator == 0. if (denominator <= prod1) { Panic.panic(ternary(denominator == 0, Panic.DIVISION_BY_ZERO, Panic.UNDER_OVERFLOW)); } /////////////////////////////////////////////// // 512 by 256 division. /////////////////////////////////////////////// // Make division exact by subtracting the remainder from [prod1 prod0]. uint256 remainder; assembly { // Compute remainder using mulmod. remainder := mulmod(x, y, denominator) // Subtract 256 bit number from 512 bit number. prod1 := sub(prod1, gt(remainder, prod0)) prod0 := sub(prod0, remainder) } // Factor powers of two out of denominator and compute largest power of two divisor of denominator. // Always >= 1. See https://cs.stackexchange.com/q/138556/92363. uint256 twos = denominator & (0 - denominator); assembly { // Divide denominator by twos. denominator := div(denominator, twos) // Divide [prod1 prod0] by twos. prod0 := div(prod0, twos) // Flip twos such that it is 2²⁵⁶ / twos. If twos is zero, then it becomes one. twos := add(div(sub(0, twos), twos), 1) } // Shift in bits from prod1 into prod0. prod0 |= prod1 * twos; // Invert denominator mod 2²⁵⁶. Now that denominator is an odd number, it has an inverse modulo 2²⁵⁶ such // that denominator * inv ≡ 1 mod 2²⁵⁶. Compute the inverse by starting with a seed that is correct for // four bits. That is, denominator * inv ≡ 1 mod 2⁴. uint256 inverse = (3 * denominator) ^ 2; // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also // works in modular arithmetic, doubling the correct bits in each step. inverse *= 2 - denominator * inverse; // inverse mod 2⁸ inverse *= 2 - denominator * inverse; // inverse mod 2¹⁶ inverse *= 2 - denominator * inverse; // inverse mod 2³² inverse *= 2 - denominator * inverse; // inverse mod 2⁶⁴ inverse *= 2 - denominator * inverse; // inverse mod 2¹²⁸ inverse *= 2 - denominator * inverse; // inverse mod 2²⁵⁶ // Because the division is now exact we can divide by multiplying with the modular inverse of denominator. // This will give us the correct result modulo 2²⁵⁶. Since the preconditions guarantee that the outcome is // less than 2²⁵⁶, this is the final result. We don't need to compute the high bits of the result and prod1 // is no longer required. result = prod0 * inverse; return result; } } /** * @dev Calculates x * y / denominator with full precision, following the selected rounding direction. */ 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); } /** * @dev Calculate the modular multiplicative inverse of a number in Z/nZ. * * If n is a prime, then Z/nZ is a field. In that case all elements are inversible, except 0. * If n is not a prime, then Z/nZ is not a field, and some elements might not be inversible. * * If the input value is not inversible, 0 is returned. * * NOTE: If you know for sure that n is (big) a prime, it may be cheaper to use Fermat's little theorem and get the * inverse using `Math.modExp(a, n - 2, n)`. See {invModPrime}. */ function invMod(uint256 a, uint256 n) internal pure returns (uint256) { unchecked { if (n == 0) return 0; // The inverse modulo is calculated using the Extended Euclidean Algorithm (iterative version) // Used to compute integers x and y such that: ax + ny = gcd(a, n). // When the gcd is 1, then the inverse of a modulo n exists and it's x. // ax + ny = 1 // ax = 1 + (-y)n // ax ≡ 1 (mod n) # x is the inverse of a modulo n // If the remainder is 0 the gcd is n right away. uint256 remainder = a % n; uint256 gcd = n; // Therefore the initial coefficients are: // ax + ny = gcd(a, n) = n // 0a + 1n = n int256 x = 0; int256 y = 1; while (remainder != 0) { uint256 quotient = gcd / remainder; (gcd, remainder) = ( // The old remainder is the next gcd to try. remainder, // Compute the next remainder. // Can't overflow given that (a % gcd) * (gcd // (a % gcd)) <= gcd // where gcd is at most n (capped to type(uint256).max) gcd - remainder * quotient ); (x, y) = ( // Increment the coefficient of a. y, // Decrement the coefficient of n. // Can overflow, but the result is casted to uint256 so that the // next value of y is "wrapped around" to a value between 0 and n - 1. x - y * int256(quotient) ); } if (gcd != 1) return 0; // No inverse exists. return ternary(x < 0, n - uint256(-x), uint256(x)); // Wrap the result if it's negative. } } /** * @dev Variant of {invMod}. More efficient, but only works if `p` is known to be a prime greater than `2`. * * From https://en.wikipedia.org/wiki/Fermat%27s_little_theorem[Fermat's little theorem], we know that if p is * prime, then `a**(p-1) ≡ 1 mod p`. As a consequence, we have `a * a**(p-2) ≡ 1 mod p`, which means that * `a**(p-2)` is the modular multiplicative inverse of a in Fp. * * NOTE: this function does NOT check that `p` is a prime greater than `2`. */ function invModPrime(uint256 a, uint256 p) internal view returns (uint256) { unchecked { return Math.modExp(a, p - 2, p); } } /** * @dev Returns the modular exponentiation of the specified base, exponent and modulus (b ** e % m) * * Requirements: * - modulus can't be zero * - underlying staticcall to precompile must succeed * * IMPORTANT: The result is only valid if the underlying call succeeds. When using this function, make * sure the chain you're using it on supports the precompiled contract for modular exponentiation * at address 0x05 as specified in https://eips.ethereum.org/EIPS/eip-198[EIP-198]. Otherwise, * the underlying function will succeed given the lack of a revert, but the result may be incorrectly * interpreted as 0. */ 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; } /** * @dev Returns the modular exponentiation of the specified base, exponent and modulus (b ** e % m). * It includes a success flag indicating if the operation succeeded. Operation will be marked as failed if trying * to operate modulo 0 or if the underlying precompile reverted. * * IMPORTANT: The result is only valid if the success flag is true. When using this function, make sure the chain * you're using it on supports the precompiled contract for modular exponentiation at address 0x05 as specified in * https://eips.ethereum.org/EIPS/eip-198[EIP-198]. Otherwise, the underlying function will succeed given the lack * of a revert, but the result may be incorrectly interpreted as 0. */ 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) // | Offset | Content | Content (Hex) | // |-----------|------------|--------------------------------------------------------------------| // | 0x00:0x1f | size of b | 0x0000000000000000000000000000000000000000000000000000000000000020 | // | 0x20:0x3f | size of e | 0x0000000000000000000000000000000000000000000000000000000000000020 | // | 0x40:0x5f | size of m | 0x0000000000000000000000000000000000000000000000000000000000000020 | // | 0x60:0x7f | value of b | 0x<.............................................................b> | // | 0x80:0x9f | value of e | 0x<.............................................................e> | // | 0xa0:0xbf | value of m | 0x<.............................................................m> | 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) // Given the result < m, it's guaranteed to fit in 32 bytes, // so we can use the memory scratch space located at offset 0. success := staticcall(gas(), 0x05, ptr, 0xc0, 0x00, 0x20) result := mload(0x00) } } /** * @dev Variant of {modExp} that supports inputs of arbitrary length. */ 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; } /** * @dev Variant of {tryModExp} that supports inputs of arbitrary length. */ 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; // Encode call args in result and move the free memory pointer result = abi.encodePacked(b.length, e.length, mLen, b, e, m); assembly ("memory-safe") { let dataPtr := add(result, 0x20) // Write result on top of args to avoid allocating extra memory. success := staticcall(gas(), 0x05, dataPtr, mload(result), dataPtr, mLen) // Overwrite the length. // result.length > returndatasize() is guaranteed because returndatasize() == m.length mstore(result, mLen) // Set the memory pointer after the returned data. mstore(0x40, add(dataPtr, mLen)) } } /** * @dev Returns whether the provided byte array is zero. */ function _zeroBytes(bytes memory byteArray) private pure returns (bool) { for (uint256 i = 0; i < byteArray.length; ++i) { if (byteArray[i] != 0) { return false; } } return true; } /** * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded * towards zero. * * This method is based on Newton's method for computing square roots; the algorithm is restricted to only * using integer operations. */ function sqrt(uint256 a) internal pure returns (uint256) { unchecked { // Take care of easy edge cases when a == 0 or a == 1 if (a <= 1) { return a; } // In this function, we use Newton's method to get a root of `f(x) := x² - a`. It involves building a // sequence x_n that converges toward sqrt(a). For each iteration x_n, we also define the error between // the current value as `ε_n = | x_n - sqrt(a) |`. // // For our first estimation, we consider `e` the smallest power of 2 which is bigger than the square root // of the target. (i.e. `2**(e-1) ≤ sqrt(a) < 2**e`). We know that `e ≤ 128` because `(2¹²⁸)² = 2²⁵⁶` is // bigger than any uint256. // // By noticing that // `2**(e-1) ≤ sqrt(a) < 2**e → (2**(e-1))² ≤ a < (2**e)² → 2**(2*e-2) ≤ a < 2**(2*e)` // we can deduce that `e - 1` is `log2(a) / 2`. We can thus compute `x_n = 2**(e-1)` using a method similar // to the msb function. 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; } // We now have x_n such that `x_n = 2**(e-1) ≤ sqrt(a) < 2**e = 2 * x_n`. This implies ε_n ≤ 2**(e-1). // // We can refine our estimation by noticing that the middle of that interval minimizes the error. // If we move x_n to equal 2**(e-1) + 2**(e-2), then we reduce the error to ε_n ≤ 2**(e-2). // This is going to be our x_0 (and ε_0) xn = (3 * xn) >> 1; // ε_0 := | x_0 - sqrt(a) | ≤ 2**(e-2) // From here, Newton's method give us: // x_{n+1} = (x_n + a / x_n) / 2 // // One should note that: // x_{n+1}² - a = ((x_n + a / x_n) / 2)² - a // = ((x_n² + a) / (2 * x_n))² - a // = (x_n⁴ + 2 * a * x_n² + a²) / (4 * x_n²) - a // = (x_n⁴ + 2 * a * x_n² + a² - 4 * a * x_n²) / (4 * x_n²) // = (x_n⁴ - 2 * a * x_n² + a²) / (4 * x_n²) // = (x_n² - a)² / (2 * x_n)² // = ((x_n² - a) / (2 * x_n))² // ≥ 0 // Which proves that for all n ≥ 1, sqrt(a) ≤ x_n // // This gives us the proof of quadratic convergence of the sequence: // ε_{n+1} = | x_{n+1} - sqrt(a) | // = | (x_n + a / x_n) / 2 - sqrt(a) | // = | (x_n² + a - 2*x_n*sqrt(a)) / (2 * x_n) | // = | (x_n - sqrt(a))² / (2 * x_n) | // = | ε_n² / (2 * x_n) | // = ε_n² / | (2 * x_n) | // // For the first iteration, we have a special case where x_0 is known: // ε_1 = ε_0² / | (2 * x_0) | // ≤ (2**(e-2))² / (2 * (2**(e-1) + 2**(e-2))) // ≤ 2**(2*e-4) / (3 * 2**(e-1)) // ≤ 2**(e-3) / 3 // ≤ 2**(e-3-log2(3)) // ≤ 2**(e-4.5) // // For the following iterations, we use the fact that, 2**(e-1) ≤ sqrt(a) ≤ x_n: // ε_{n+1} = ε_n² / | (2 * x_n) | // ≤ (2**(e-k))² / (2 * 2**(e-1)) // ≤ 2**(2*e-2*k) / 2**e // ≤ 2**(e-2*k) xn = (xn + a / xn) >> 1; // ε_1 := | x_1 - sqrt(a) | ≤ 2**(e-4.5) -- special case, see above xn = (xn + a / xn) >> 1; // ε_2 := | x_2 - sqrt(a) | ≤ 2**(e-9) -- general case with k = 4.5 xn = (xn + a / xn) >> 1; // ε_3 := | x_3 - sqrt(a) | ≤ 2**(e-18) -- general case with k = 9 xn = (xn + a / xn) >> 1; // ε_4 := | x_4 - sqrt(a) | ≤ 2**(e-36) -- general case with k = 18 xn = (xn + a / xn) >> 1; // ε_5 := | x_5 - sqrt(a) | ≤ 2**(e-72) -- general case with k = 36 xn = (xn + a / xn) >> 1; // ε_6 := | x_6 - sqrt(a) | ≤ 2**(e-144) -- general case with k = 72 // Because e ≤ 128 (as discussed during the first estimation phase), we know have reached a precision // ε_6 ≤ 2**(e-144) < 1. Given we're operating on integers, then we can ensure that xn is now either // sqrt(a) or sqrt(a) + 1. return xn - SafeCast.toUint(xn > a / xn); } } /** * @dev Calculates sqrt(a), following the selected rounding direction. */ function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) { unchecked { uint256 result = sqrt(a); return result + SafeCast.toUint(unsignedRoundsUp(rounding) && result * result < a); } } /** * @dev Return the log in base 2 of a positive value rounded towards zero. * Returns 0 if given 0. */ function log2(uint256 value) internal pure returns (uint256) { uint256 result = 0; uint256 exp; unchecked { exp = 128 * SafeCast.toUint(value > (1 << 128) - 1); value >>= exp; result += exp; exp = 64 * SafeCast.toUint(value > (1 << 64) - 1); value >>= exp; result += exp; exp = 32 * SafeCast.toUint(value > (1 << 32) - 1); value >>= exp; result += exp; exp = 16 * SafeCast.toUint(value > (1 << 16) - 1); value >>= exp; result += exp; exp = 8 * SafeCast.toUint(value > (1 << 8) - 1); value >>= exp; result += exp; exp = 4 * SafeCast.toUint(value > (1 << 4) - 1); value >>= exp; result += exp; exp = 2 * SafeCast.toUint(value > (1 << 2) - 1); value >>= exp; result += exp; result += SafeCast.toUint(value > 1); } return result; } /** * @dev Return the log in base 2, following the selected rounding direction, of a positive value. * Returns 0 if given 0. */ function log2(uint256 value, Rounding rounding) internal pure returns (uint256) { unchecked { uint256 result = log2(value); return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << result < value); } } /** * @dev Return the log in base 10 of a positive value rounded towards zero. * Returns 0 if given 0. */ 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; } /** * @dev Return the log in base 10, following the selected rounding direction, of a positive value. * Returns 0 if given 0. */ function log10(uint256 value, Rounding rounding) internal pure returns (uint256) { unchecked { uint256 result = log10(value); return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 10 ** result < value); } } /** * @dev Return the log in base 256 of a positive value rounded towards zero. * Returns 0 if given 0. * * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string. */ function log256(uint256 value) internal pure returns (uint256) { uint256 result = 0; uint256 isGt; unchecked { isGt = SafeCast.toUint(value > (1 << 128) - 1); value >>= isGt * 128; result += isGt * 16; isGt = SafeCast.toUint(value > (1 << 64) - 1); value >>= isGt * 64; result += isGt * 8; isGt = SafeCast.toUint(value > (1 << 32) - 1); value >>= isGt * 32; result += isGt * 4; isGt = SafeCast.toUint(value > (1 << 16) - 1); value >>= isGt * 16; result += isGt * 2; result += SafeCast.toUint(value > (1 << 8) - 1); } return result; } /** * @dev Return the log in base 256, following the selected rounding direction, of a positive value. * Returns 0 if given 0. */ 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); } } /** * @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers. */ function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) { return uint8(rounding) % 2 == 1; } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (utils/math/SafeCast.sol) // This file was procedurally generated from scripts/generate/templates/SafeCast.js. pragma solidity ^0.8.20; /** * @dev Wrappers over Solidity's uintXX/intXX/bool casting operators with added overflow * checks. * * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can * easily result in undesired exploitation or bugs, since developers usually * assume that overflows raise errors. `SafeCast` restores this intuition by * reverting the transaction when such an operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. */ library SafeCast { /** * @dev Value doesn't fit in an uint of `bits` size. */ error SafeCastOverflowedUintDowncast(uint8 bits, uint256 value); /** * @dev An int value doesn't fit in an uint of `bits` size. */ error SafeCastOverflowedIntToUint(int256 value); /** * @dev Value doesn't fit in an int of `bits` size. */ error SafeCastOverflowedIntDowncast(uint8 bits, int256 value); /** * @dev An uint value doesn't fit in an int of `bits` size. */ error SafeCastOverflowedUintToInt(uint256 value); /** * @dev Returns the downcasted uint248 from uint256, reverting on * overflow (when the input is greater than largest uint248). * * Counterpart to Solidity's `uint248` operator. * * Requirements: * * - input must fit into 248 bits */ function toUint248(uint256 value) internal pure returns (uint248) { if (value > type(uint248).max) { revert SafeCastOverflowedUintDowncast(248, value); } return uint248(value); } /** * @dev Returns the downcasted uint240 from uint256, reverting on * overflow (when the input is greater than largest uint240). * * Counterpart to Solidity's `uint240` operator. * * Requirements: * * - input must fit into 240 bits */ function toUint240(uint256 value) internal pure returns (uint240) { if (value > type(uint240).max) { revert SafeCastOverflowedUintDowncast(240, value); } return uint240(value); } /** * @dev Returns the downcasted uint232 from uint256, reverting on * overflow (when the input is greater than largest uint232). * * Counterpart to Solidity's `uint232` operator. * * Requirements: * * - input must fit into 232 bits */ function toUint232(uint256 value) internal pure returns (uint232) { if (value > type(uint232).max) { revert SafeCastOverflowedUintDowncast(232, value); } return uint232(value); } /** * @dev Returns the downcasted uint224 from uint256, reverting on * overflow (when the input is greater than largest uint224). * * Counterpart to Solidity's `uint224` operator. * * Requirements: * * - input must fit into 224 bits */ function toUint224(uint256 value) internal pure returns (uint224) { if (value > type(uint224).max) { revert SafeCastOverflowedUintDowncast(224, value); } return uint224(value); } /** * @dev Returns the downcasted uint216 from uint256, reverting on * overflow (when the input is greater than largest uint216). * * Counterpart to Solidity's `uint216` operator. * * Requirements: * * - input must fit into 216 bits */ function toUint216(uint256 value) internal pure returns (uint216) { if (value > type(uint216).max) { revert SafeCastOverflowedUintDowncast(216, value); } return uint216(value); } /** * @dev Returns the downcasted uint208 from uint256, reverting on * overflow (when the input is greater than largest uint208). * * Counterpart to Solidity's `uint208` operator. * * Requirements: * * - input must fit into 208 bits */ function toUint208(uint256 value) internal pure returns (uint208) { if (value > type(uint208).max) { revert SafeCastOverflowedUintDowncast(208, value); } return uint208(value); } /** * @dev Returns the downcasted uint200 from uint256, reverting on * overflow (when the input is greater than largest uint200). * * Counterpart to Solidity's `uint200` operator. * * Requirements: * * - input must fit into 200 bits */ function toUint200(uint256 value) internal pure returns (uint200) { if (value > type(uint200).max) { revert SafeCastOverflowedUintDowncast(200, value); } return uint200(value); } /** * @dev Returns the downcasted uint192 from uint256, reverting on * overflow (when the input is greater than largest uint192). * * Counterpart to Solidity's `uint192` operator. * * Requirements: * * - input must fit into 192 bits */ function toUint192(uint256 value) internal pure returns (uint192) { if (value > type(uint192).max) { revert SafeCastOverflowedUintDowncast(192, value); } return uint192(value); } /** * @dev Returns the downcasted uint184 from uint256, reverting on * overflow (when the input is greater than largest uint184). * * Counterpart to Solidity's `uint184` operator. * * Requirements: * * - input must fit into 184 bits */ function toUint184(uint256 value) internal pure returns (uint184) { if (value > type(uint184).max) { revert SafeCastOverflowedUintDowncast(184, value); } return uint184(value); } /** * @dev Returns the downcasted uint176 from uint256, reverting on * overflow (when the input is greater than largest uint176). * * Counterpart to Solidity's `uint176` operator. * * Requirements: * * - input must fit into 176 bits */ function toUint176(uint256 value) internal pure returns (uint176) { if (value > type(uint176).max) { revert SafeCastOverflowedUintDowncast(176, value); } return uint176(value); } /** * @dev Returns the downcasted uint168 from uint256, reverting on * overflow (when the input is greater than largest uint168). * * Counterpart to Solidity's `uint168` operator. * * Requirements: * * - input must fit into 168 bits */ function toUint168(uint256 value) internal pure returns (uint168) { if (value > type(uint168).max) { revert SafeCastOverflowedUintDowncast(168, value); } return uint168(value); } /** * @dev Returns the downcasted uint160 from uint256, reverting on * overflow (when the input is greater than largest uint160). * * Counterpart to Solidity's `uint160` operator. * * Requirements: * * - input must fit into 160 bits */ function toUint160(uint256 value) internal pure returns (uint160) { if (value > type(uint160).max) { revert SafeCastOverflowedUintDowncast(160, value); } return uint160(value); } /** * @dev Returns the downcasted uint152 from uint256, reverting on * overflow (when the input is greater than largest uint152). * * Counterpart to Solidity's `uint152` operator. * * Requirements: * * - input must fit into 152 bits */ function toUint152(uint256 value) internal pure returns (uint152) { if (value > type(uint152).max) { revert SafeCastOverflowedUintDowncast(152, value); } return uint152(value); } /** * @dev Returns the downcasted uint144 from uint256, reverting on * overflow (when the input is greater than largest uint144). * * Counterpart to Solidity's `uint144` operator. * * Requirements: * * - input must fit into 144 bits */ function toUint144(uint256 value) internal pure returns (uint144) { if (value > type(uint144).max) { revert SafeCastOverflowedUintDowncast(144, value); } return uint144(value); } /** * @dev Returns the downcasted uint136 from uint256, reverting on * overflow (when the input is greater than largest uint136). * * Counterpart to Solidity's `uint136` operator. * * Requirements: * * - input must fit into 136 bits */ function toUint136(uint256 value) internal pure returns (uint136) { if (value > type(uint136).max) { revert SafeCastOverflowedUintDowncast(136, value); } return uint136(value); } /** * @dev Returns the downcasted uint128 from uint256, reverting on * overflow (when the input is greater than largest uint128). * * Counterpart to Solidity's `uint128` operator. * * Requirements: * * - input must fit into 128 bits */ function toUint128(uint256 value) internal pure returns (uint128) { if (value > type(uint128).max) { revert SafeCastOverflowedUintDowncast(128, value); } return uint128(value); } /** * @dev Returns the downcasted uint120 from uint256, reverting on * overflow (when the input is greater than largest uint120). * * Counterpart to Solidity's `uint120` operator. * * Requirements: * * - input must fit into 120 bits */ function toUint120(uint256 value) internal pure returns (uint120) { if (value > type(uint120).max) { revert SafeCastOverflowedUintDowncast(120, value); } return uint120(value); } /** * @dev Returns the downcasted uint112 from uint256, reverting on * overflow (when the input is greater than largest uint112). * * Counterpart to Solidity's `uint112` operator. * * Requirements: * * - input must fit into 112 bits */ function toUint112(uint256 value) internal pure returns (uint112) { if (value > type(uint112).max) { revert SafeCastOverflowedUintDowncast(112, value); } return uint112(value); } /** * @dev Returns the downcasted uint104 from uint256, reverting on * overflow (when the input is greater than largest uint104). * * Counterpart to Solidity's `uint104` operator. * * Requirements: * * - input must fit into 104 bits */ function toUint104(uint256 value) internal pure returns (uint104) { if (value > type(uint104).max) { revert SafeCastOverflowedUintDowncast(104, value); } return uint104(value); } /** * @dev Returns the downcasted uint96 from uint256, reverting on * overflow (when the input is greater than largest uint96). * * Counterpart to Solidity's `uint96` operator. * * Requirements: * * - input must fit into 96 bits */ function toUint96(uint256 value) internal pure returns (uint96) { if (value > type(uint96).max) { revert SafeCastOverflowedUintDowncast(96, value); } return uint96(value); } /** * @dev Returns the downcasted uint88 from uint256, reverting on * overflow (when the input is greater than largest uint88). * * Counterpart to Solidity's `uint88` operator. * * Requirements: * * - input must fit into 88 bits */ function toUint88(uint256 value) internal pure returns (uint88) { if (value > type(uint88).max) { revert SafeCastOverflowedUintDowncast(88, value); } return uint88(value); } /** * @dev Returns the downcasted uint80 from uint256, reverting on * overflow (when the input is greater than largest uint80). * * Counterpart to Solidity's `uint80` operator. * * Requirements: * * - input must fit into 80 bits */ function toUint80(uint256 value) internal pure returns (uint80) { if (value > type(uint80).max) { revert SafeCastOverflowedUintDowncast(80, value); } return uint80(value); } /** * @dev Returns the downcasted uint72 from uint256, reverting on * overflow (when the input is greater than largest uint72). * * Counterpart to Solidity's `uint72` operator. * * Requirements: * * - input must fit into 72 bits */ function toUint72(uint256 value) internal pure returns (uint72) { if (value > type(uint72).max) { revert SafeCastOverflowedUintDowncast(72, value); } return uint72(value); } /** * @dev Returns the downcasted uint64 from uint256, reverting on * overflow (when the input is greater than largest uint64). * * Counterpart to Solidity's `uint64` operator. * * Requirements: * * - input must fit into 64 bits */ function toUint64(uint256 value) internal pure returns (uint64) { if (value > type(uint64).max) { revert SafeCastOverflowedUintDowncast(64, value); } return uint64(value); } /** * @dev Returns the downcasted uint56 from uint256, reverting on * overflow (when the input is greater than largest uint56). * * Counterpart to Solidity's `uint56` operator. * * Requirements: * * - input must fit into 56 bits */ function toUint56(uint256 value) internal pure returns (uint56) { if (value > type(uint56).max) { revert SafeCastOverflowedUintDowncast(56, value); } return uint56(value); } /** * @dev Returns the downcasted uint48 from uint256, reverting on * overflow (when the input is greater than largest uint48). * * Counterpart to Solidity's `uint48` operator. * * Requirements: * * - input must fit into 48 bits */ function toUint48(uint256 value) internal pure returns (uint48) { if (value > type(uint48).max) { revert SafeCastOverflowedUintDowncast(48, value); } return uint48(value); } /** * @dev Returns the downcasted uint40 from uint256, reverting on * overflow (when the input is greater than largest uint40). * * Counterpart to Solidity's `uint40` operator. * * Requirements: * * - input must fit into 40 bits */ function toUint40(uint256 value) internal pure returns (uint40) { if (value > type(uint40).max) { revert SafeCastOverflowedUintDowncast(40, value); } return uint40(value); } /** * @dev Returns the downcasted uint32 from uint256, reverting on * overflow (when the input is greater than largest uint32). * * Counterpart to Solidity's `uint32` operator. * * Requirements: * * - input must fit into 32 bits */ function toUint32(uint256 value) internal pure returns (uint32) { if (value > type(uint32).max) { revert SafeCastOverflowedUintDowncast(32, value); } return uint32(value); } /** * @dev Returns the downcasted uint24 from uint256, reverting on * overflow (when the input is greater than largest uint24). * * Counterpart to Solidity's `uint24` operator. * * Requirements: * * - input must fit into 24 bits */ function toUint24(uint256 value) internal pure returns (uint24) { if (value > type(uint24).max) { revert SafeCastOverflowedUintDowncast(24, value); } return uint24(value); } /** * @dev Returns the downcasted uint16 from uint256, reverting on * overflow (when the input is greater than largest uint16). * * Counterpart to Solidity's `uint16` operator. * * Requirements: * * - input must fit into 16 bits */ function toUint16(uint256 value) internal pure returns (uint16) { if (value > type(uint16).max) { revert SafeCastOverflowedUintDowncast(16, value); } return uint16(value); } /** * @dev Returns the downcasted uint8 from uint256, reverting on * overflow (when the input is greater than largest uint8). * * Counterpart to Solidity's `uint8` operator. * * Requirements: * * - input must fit into 8 bits */ function toUint8(uint256 value) internal pure returns (uint8) { if (value > type(uint8).max) { revert SafeCastOverflowedUintDowncast(8, value); } return uint8(value); } /** * @dev Converts a signed int256 into an unsigned uint256. * * Requirements: * * - input must be greater than or equal to 0. */ function toUint256(int256 value) internal pure returns (uint256) { if (value < 0) { revert SafeCastOverflowedIntToUint(value); } return uint256(value); } /** * @dev Returns the downcasted int248 from int256, reverting on * overflow (when the input is less than smallest int248 or * greater than largest int248). * * Counterpart to Solidity's `int248` operator. * * Requirements: * * - input must fit into 248 bits */ function toInt248(int256 value) internal pure returns (int248 downcasted) { downcasted = int248(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(248, value); } } /** * @dev Returns the downcasted int240 from int256, reverting on * overflow (when the input is less than smallest int240 or * greater than largest int240). * * Counterpart to Solidity's `int240` operator. * * Requirements: * * - input must fit into 240 bits */ function toInt240(int256 value) internal pure returns (int240 downcasted) { downcasted = int240(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(240, value); } } /** * @dev Returns the downcasted int232 from int256, reverting on * overflow (when the input is less than smallest int232 or * greater than largest int232). * * Counterpart to Solidity's `int232` operator. * * Requirements: * * - input must fit into 232 bits */ function toInt232(int256 value) internal pure returns (int232 downcasted) { downcasted = int232(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(232, value); } } /** * @dev Returns the downcasted int224 from int256, reverting on * overflow (when the input is less than smallest int224 or * greater than largest int224). * * Counterpart to Solidity's `int224` operator. * * Requirements: * * - input must fit into 224 bits */ function toInt224(int256 value) internal pure returns (int224 downcasted) { downcasted = int224(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(224, value); } } /** * @dev Returns the downcasted int216 from int256, reverting on * overflow (when the input is less than smallest int216 or * greater than largest int216). * * Counterpart to Solidity's `int216` operator. * * Requirements: * * - input must fit into 216 bits */ function toInt216(int256 value) internal pure returns (int216 downcasted) { downcasted = int216(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(216, value); } } /** * @dev Returns the downcasted int208 from int256, reverting on * overflow (when the input is less than smallest int208 or * greater than largest int208). * * Counterpart to Solidity's `int208` operator. * * Requirements: * * - input must fit into 208 bits */ function toInt208(int256 value) internal pure returns (int208 downcasted) { downcasted = int208(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(208, value); } } /** * @dev Returns the downcasted int200 from int256, reverting on * overflow (when the input is less than smallest int200 or * greater than largest int200). * * Counterpart to Solidity's `int200` operator. * * Requirements: * * - input must fit into 200 bits */ function toInt200(int256 value) internal pure returns (int200 downcasted) { downcasted = int200(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(200, value); } } /** * @dev Returns the downcasted int192 from int256, reverting on * overflow (when the input is less than smallest int192 or * greater than largest int192). * * Counterpart to Solidity's `int192` operator. * * Requirements: * * - input must fit into 192 bits */ function toInt192(int256 value) internal pure returns (int192 downcasted) { downcasted = int192(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(192, value); } } /** * @dev Returns the downcasted int184 from int256, reverting on * overflow (when the input is less than smallest int184 or * greater than largest int184). * * Counterpart to Solidity's `int184` operator. * * Requirements: * * - input must fit into 184 bits */ function toInt184(int256 value) internal pure returns (int184 downcasted) { downcasted = int184(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(184, value); } } /** * @dev Returns the downcasted int176 from int256, reverting on * overflow (when the input is less than smallest int176 or * greater than largest int176). * * Counterpart to Solidity's `int176` operator. * * Requirements: * * - input must fit into 176 bits */ function toInt176(int256 value) internal pure returns (int176 downcasted) { downcasted = int176(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(176, value); } } /** * @dev Returns the downcasted int168 from int256, reverting on * overflow (when the input is less than smallest int168 or * greater than largest int168). * * Counterpart to Solidity's `int168` operator. * * Requirements: * * - input must fit into 168 bits */ function toInt168(int256 value) internal pure returns (int168 downcasted) { downcasted = int168(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(168, value); } } /** * @dev Returns the downcasted int160 from int256, reverting on * overflow (when the input is less than smallest int160 or * greater than largest int160). * * Counterpart to Solidity's `int160` operator. * * Requirements: * * - input must fit into 160 bits */ function toInt160(int256 value) internal pure returns (int160 downcasted) { downcasted = int160(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(160, value); } } /** * @dev Returns the downcasted int152 from int256, reverting on * overflow (when the input is less than smallest int152 or * greater than largest int152). * * Counterpart to Solidity's `int152` operator. * * Requirements: * * - input must fit into 152 bits */ function toInt152(int256 value) internal pure returns (int152 downcasted) { downcasted = int152(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(152, value); } } /** * @dev Returns the downcasted int144 from int256, reverting on * overflow (when the input is less than smallest int144 or * greater than largest int144). * * Counterpart to Solidity's `int144` operator. * * Requirements: * * - input must fit into 144 bits */ function toInt144(int256 value) internal pure returns (int144 downcasted) { downcasted = int144(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(144, value); } } /** * @dev Returns the downcasted int136 from int256, reverting on * overflow (when the input is less than smallest int136 or * greater than largest int136). * * Counterpart to Solidity's `int136` operator. * * Requirements: * * - input must fit into 136 bits */ function toInt136(int256 value) internal pure returns (int136 downcasted) { downcasted = int136(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(136, value); } } /** * @dev Returns the downcasted int128 from int256, reverting on * overflow (when the input is less than smallest int128 or * greater than largest int128). * * Counterpart to Solidity's `int128` operator. * * Requirements: * * - input must fit into 128 bits */ function toInt128(int256 value) internal pure returns (int128 downcasted) { downcasted = int128(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(128, value); } } /** * @dev Returns the downcasted int120 from int256, reverting on * overflow (when the input is less than smallest int120 or * greater than largest int120). * * Counterpart to Solidity's `int120` operator. * * Requirements: * * - input must fit into 120 bits */ function toInt120(int256 value) internal pure returns (int120 downcasted) { downcasted = int120(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(120, value); } } /** * @dev Returns the downcasted int112 from int256, reverting on * overflow (when the input is less than smallest int112 or * greater than largest int112). * * Counterpart to Solidity's `int112` operator. * * Requirements: * * - input must fit into 112 bits */ function toInt112(int256 value) internal pure returns (int112 downcasted) { downcasted = int112(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(112, value); } } /** * @dev Returns the downcasted int104 from int256, reverting on * overflow (when the input is less than smallest int104 or * greater than largest int104). * * Counterpart to Solidity's `int104` operator. * * Requirements: * * - input must fit into 104 bits */ function toInt104(int256 value) internal pure returns (int104 downcasted) { downcasted = int104(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(104, value); } } /** * @dev Returns the downcasted int96 from int256, reverting on * overflow (when the input is less than smallest int96 or * greater than largest int96). * * Counterpart to Solidity's `int96` operator. * * Requirements: * * - input must fit into 96 bits */ function toInt96(int256 value) internal pure returns (int96 downcasted) { downcasted = int96(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(96, value); } } /** * @dev Returns the downcasted int88 from int256, reverting on * overflow (when the input is less than smallest int88 or * greater than largest int88). * * Counterpart to Solidity's `int88` operator. * * Requirements: * * - input must fit into 88 bits */ function toInt88(int256 value) internal pure returns (int88 downcasted) { downcasted = int88(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(88, value); } } /** * @dev Returns the downcasted int80 from int256, reverting on * overflow (when the input is less than smallest int80 or * greater than largest int80). * * Counterpart to Solidity's `int80` operator. * * Requirements: * * - input must fit into 80 bits */ function toInt80(int256 value) internal pure returns (int80 downcasted) { downcasted = int80(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(80, value); } } /** * @dev Returns the downcasted int72 from int256, reverting on * overflow (when the input is less than smallest int72 or * greater than largest int72). * * Counterpart to Solidity's `int72` operator. * * Requirements: * * - input must fit into 72 bits */ function toInt72(int256 value) internal pure returns (int72 downcasted) { downcasted = int72(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(72, value); } } /** * @dev Returns the downcasted int64 from int256, reverting on * overflow (when the input is less than smallest int64 or * greater than largest int64). * * Counterpart to Solidity's `int64` operator. * * Requirements: * * - input must fit into 64 bits */ function toInt64(int256 value) internal pure returns (int64 downcasted) { downcasted = int64(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(64, value); } } /** * @dev Returns the downcasted int56 from int256, reverting on * overflow (when the input is less than smallest int56 or * greater than largest int56). * * Counterpart to Solidity's `int56` operator. * * Requirements: * * - input must fit into 56 bits */ function toInt56(int256 value) internal pure returns (int56 downcasted) { downcasted = int56(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(56, value); } } /** * @dev Returns the downcasted int48 from int256, reverting on * overflow (when the input is less than smallest int48 or * greater than largest int48). * * Counterpart to Solidity's `int48` operator. * * Requirements: * * - input must fit into 48 bits */ function toInt48(int256 value) internal pure returns (int48 downcasted) { downcasted = int48(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(48, value); } } /** * @dev Returns the downcasted int40 from int256, reverting on * overflow (when the input is less than smallest int40 or * greater than largest int40). * * Counterpart to Solidity's `int40` operator. * * Requirements: * * - input must fit into 40 bits */ function toInt40(int256 value) internal pure returns (int40 downcasted) { downcasted = int40(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(40, value); } } /** * @dev Returns the downcasted int32 from int256, reverting on * overflow (when the input is less than smallest int32 or * greater than largest int32). * * Counterpart to Solidity's `int32` operator. * * Requirements: * * - input must fit into 32 bits */ function toInt32(int256 value) internal pure returns (int32 downcasted) { downcasted = int32(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(32, value); } } /** * @dev Returns the downcasted int24 from int256, reverting on * overflow (when the input is less than smallest int24 or * greater than largest int24). * * Counterpart to Solidity's `int24` operator. * * Requirements: * * - input must fit into 24 bits */ function toInt24(int256 value) internal pure returns (int24 downcasted) { downcasted = int24(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(24, value); } } /** * @dev Returns the downcasted int16 from int256, reverting on * overflow (when the input is less than smallest int16 or * greater than largest int16). * * Counterpart to Solidity's `int16` operator. * * Requirements: * * - input must fit into 16 bits */ function toInt16(int256 value) internal pure returns (int16 downcasted) { downcasted = int16(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(16, value); } } /** * @dev Returns the downcasted int8 from int256, reverting on * overflow (when the input is less than smallest int8 or * greater than largest int8). * * Counterpart to Solidity's `int8` operator. * * Requirements: * * - input must fit into 8 bits */ function toInt8(int256 value) internal pure returns (int8 downcasted) { downcasted = int8(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(8, value); } } /** * @dev Converts an unsigned uint256 into a signed int256. * * Requirements: * * - input must be less than or equal to maxInt256. */ function toInt256(uint256 value) internal pure returns (int256) { // Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive if (value > uint256(type(int256).max)) { revert SafeCastOverflowedUintToInt(value); } return int256(value); } /** * @dev Cast a boolean (false or true) to a uint256 (0 or 1) with no jump. */ function toUint(bool b) internal pure returns (uint256 u) { assembly ("memory-safe") { u := iszero(iszero(b)) } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (utils/Panic.sol) pragma solidity ^0.8.20; /** * @dev Helper library for emitting standardized panic codes. * * ```solidity * contract Example { * using Panic for uint256; * * // Use any of the declared internal constants * function foo() { Panic.GENERIC.panic(); } * * // Alternatively * function foo() { Panic.panic(Panic.GENERIC); } * } * ``` * * Follows the list from https://github.com/ethereum/solidity/blob/v0.8.24/libsolutil/ErrorCodes.h[libsolutil]. * * _Available since v5.1._ */ // slither-disable-next-line unused-state library Panic { /// @dev generic / unspecified error uint256 internal constant GENERIC = 0x00; /// @dev used by the assert() builtin uint256 internal constant ASSERT = 0x01; /// @dev arithmetic underflow or overflow uint256 internal constant UNDER_OVERFLOW = 0x11; /// @dev division or modulo by zero uint256 internal constant DIVISION_BY_ZERO = 0x12; /// @dev enum conversion error uint256 internal constant ENUM_CONVERSION_ERROR = 0x21; /// @dev invalid encoding in storage uint256 internal constant STORAGE_ENCODING_ERROR = 0x22; /// @dev empty array pop uint256 internal constant EMPTY_ARRAY_POP = 0x31; /// @dev array out of bounds access uint256 internal constant ARRAY_OUT_OF_BOUNDS = 0x32; /// @dev resource error (too large allocation or too large array) uint256 internal constant RESOURCE_ERROR = 0x41; /// @dev calling invalid internal function uint256 internal constant INVALID_INTERNAL_FUNCTION = 0x51; /// @dev Reverts with a panic code. Recommended to use with /// the internal constants with predefined codes. function panic(uint256 code) internal pure { assembly ("memory-safe") { mstore(0x00, 0x4e487b71) mstore(0x20, code) revert(0x1c, 0x24) } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (utils/ReentrancyGuard.sol) pragma solidity ^0.8.20; /** * @dev Contract module that helps prevent reentrant calls to a function. * * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier * available, which can be applied to functions to make sure there are no nested * (reentrant) calls to them. * * Note that because there is a single `nonReentrant` guard, functions marked as * `nonReentrant` may not call one another. This can be worked around by making * those functions `private`, and then adding `external` `nonReentrant` entry * points to them. * * TIP: If EIP-1153 (transient storage) is available on the chain you're deploying at, * consider using {ReentrancyGuardTransient} instead. * * TIP: If you would like to learn more about reentrancy and alternative ways * to protect against it, check out our blog post * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul]. */ abstract contract ReentrancyGuard { // Booleans are more expensive than uint256 or any type that takes up a full // word because each write operation emits an extra SLOAD to first read the // slot's contents, replace the bits taken up by the boolean, and then write // back. This is the compiler's defense against contract upgrades and // pointer aliasing, and it cannot be disabled. // The values being non-zero value makes deployment a bit more expensive, // but in exchange the refund on every call to nonReentrant will be lower in // amount. Since refunds are capped to a percentage of the total // transaction's gas, it is best to keep them low in cases like this one, to // increase the likelihood of the full refund coming into effect. uint256 private constant NOT_ENTERED = 1; uint256 private constant ENTERED = 2; uint256 private _status; /** * @dev Unauthorized reentrant call. */ error ReentrancyGuardReentrantCall(); constructor() { _status = NOT_ENTERED; } /** * @dev Prevents a contract from calling itself, directly or indirectly. * Calling a `nonReentrant` function from another `nonReentrant` * function is not supported. It is possible to prevent this from happening * by making the `nonReentrant` function external, and making it call a * `private` function that does the actual work. */ modifier nonReentrant() { _nonReentrantBefore(); _; _nonReentrantAfter(); } function _nonReentrantBefore() private { // On the first call to nonReentrant, _status will be NOT_ENTERED if (_status == ENTERED) { revert ReentrancyGuardReentrantCall(); } // Any calls to nonReentrant after this point will fail _status = ENTERED; } function _nonReentrantAfter() private { // By storing the original value once again, a refund is triggered (see // https://eips.ethereum.org/EIPS/eip-2200) _status = NOT_ENTERED; } /** * @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a * `nonReentrant` function in the call stack. */ function _reentrancyGuardEntered() internal view returns (bool) { return _status == ENTERED; } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "./utils/token/ERC20.sol"; import "./utils/owner/Ownable.sol"; import "./utils/token/extensions/ERC20Burnable.sol"; contract BondingCurveToken is ERC20, ERC20Burnable, Ownable { uint256 private constant TOTAL_SUPPLY = 1_000_000_000 * 10**18; uint256 public constant TRADING_SUPPLY = 800_000_000 * 10**18; uint256 public constant LP_SUPPLY = 200_000_000 * 10**18; constructor(string memory name, string memory symbol) ERC20(name, symbol) { // Mint total supply to the contract itself _mint(address(this), TOTAL_SUPPLY); } function transferTradingSupply(address manager) external onlyOwner { // Transfer trading supply to the bonding curve manager _transfer(address(this), manager, TRADING_SUPPLY); } function transferLPSupply(address manager) external onlyOwner { // Transfer LP supply to the bonding curve manager _transfer(address(this), manager, LP_SUPPLY); } function burnFrom(address account, uint256 amount) public virtual override { super.burnFrom(account, amount); } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import "./BancorFormula.sol"; import "prb-math/contracts/PRBMathSD59x18.sol"; import "prb-math/contracts/PRBMathUD60x18.sol"; // based on https://medium.com/relevant-community/bonding-curves-in-depth-intuition-parametrization-d3905a681e0a contract BancorBondingCurve is BancorFormula { using PRBMathSD59x18 for int256; using PRBMathUD60x18 for uint256; uint256 public immutable slope; uint32 public immutable reserveRatio; // reserveRatio = connectorWeight, but is scaled by MAX_WEIGHT (1000000) // also note that unscaled reserveRatio = 1 / (n+1), so a reserveRatio 1000000 means n=0, reserveRatio=2000000 means n=1, and so on // slope (denoted as m in the article) is only relevant when supply = 0. When supply is non-zero, the price for minting k tokens can be fully determined by current balance and supply constructor(uint256 _slope, uint32 _reserveRatio) { slope = _slope; reserveRatio = _reserveRatio; } // buy function /** * @notice Calculate the amount of collateral (ETH) required to mint a specific number of tokens. * @param b The current collateral balance in the bonding curve. * @param supply The current total supply of the token. * @param k The number of tokens to mint. * @return p The price (in collateral) required to mint `k` tokens. */ function computePriceForMinting(uint256 b, uint256 supply, uint256 k) public view returns (uint256 p) { if (supply == 0) { // Use custom calculation for zero supply uint256 r = uint256(reserveRatio); uint256 m = slope; return computeP(k, r, m); } // Use Bancor's sale return calculation when supply is non-zero return calculateSaleReturn(supply + k, b, reserveRatio, k); } /** * @notice Computes the number of tokens that can be minted for a given amount of collateral. * @param b The current collateral balance in the bonding curve. * @param supply The current total supply of the token. * @param p The amount of collateral provided. * @return k The number of tokens that can be minted with `p` collateral. */ function computeMintingAmountFromPrice(uint256 b, uint256 supply, uint256 p) public view returns (uint256 k) { if (supply == 0) { // uint256 result; // uint8 precision; // (result, precision) = power(p * MAX_WEIGHT, reserveRatio * slope, reserveRatio, MAX_WEIGHT); // return (result >> precision) * 1e18; // Custom formula when supply is zero: s = (p / (r * m))^r // Adjusted for integer math: s = (p * MAX_WEIGHT / (r * m))^(r / MAX_WEIGHT) uint256 baseNumerator = p * MAX_WEIGHT; // p * MAX_WEIGHT uint256 baseDenominator = uint256(reserveRatio) * slope; // r * m require(baseDenominator > 0, "Invalid base denominator"); uint32 expNumerator = reserveRatio; // r uint32 expDenominator = MAX_WEIGHT; // 1,000,000 /** * Compute s = (baseNumerator / baseDenominator)^(expNumerator / expDenominator) */ return computeS( int256(baseNumerator), int256(baseDenominator), int256(uint256(expNumerator)), int256(uint256(expDenominator)) ); } return calculatePurchaseReturn(supply, b, reserveRatio, p); } // sell function /** * @notice Computes the amount of collateral refunded when burning a specific number of tokens. * @param b The current collateral balance in the bonding curve. * @param supply The current total supply of the token. * @param k The number of tokens to burn. * @return p The amount of collateral refunded for burning `k` tokens. */ function computeRefundForBurning(uint256 b, uint256 supply, uint256 k) public view returns (uint256 p) { if (supply == k) { return b; } return calculateSaleReturn(supply, b, reserveRatio, k); } /** * @notice Computes the number of tokens that must be burned to receive a specific collateral refund. * @param b The current collateral balance in the bonding curve. * @param supply The current total supply of the token. * @param p The desired collateral refund. * @return k The number of tokens that must be burned to receive `p` collateral. */ function computeBurningAmountFromRefund(uint256 b, uint256 supply, uint256 p) public view returns (uint256 k) { if (b == p) { return supply; } return calculatePurchaseReturn(supply, b - p, reserveRatio, p); } // helpers for buys /* * @dev Computes the deposit amount using the formula p = (r * m) * s^(1/r) or p = (r * m) * sqrt[r]{s} . * @param _tokenAmount Amount of tokens desired (s). * @param _reserveRatio Reserve ratio (r). * @param _slope Slope parameter (m). * @return Amount of reserve tokens(ETH) needed (p). */ function computeP( uint256 s, uint256 r, uint256 m ) public pure returns (uint256 p) { // Calculate the exponentiation with high precision // s is scaled by 1e18, so s^(1/r) is also scaled by 1e18 // 1e6 = MAX_WEIGHT uint256 exponentiation = PRBMathUD60x18.pow(s, (1e18 * 1e6) / r); // (s)^(1/r) // Calculate the deposit amount with correct scaling // p = (r * m * s^(1/r)) / 1e24 // 1e6 (from reserveRatio) * 1e18 (fixed-point) = 1e24 p = (r * m * exponentiation) / 1e24; } //test compute - worked function computeS( int256 baseNumerator, int256 baseDenominator, int256 expNumerator, int256 expDenominator ) public pure returns (uint256 s) { // here i compute the base fraction int256 baseFraction = baseNumerator.div(baseDenominator); // then compute the exponent fraction int256 expFraction = expNumerator.div(expDenominator); // also compute the exponentiation int256 result = baseFraction.pow(expFraction); // result is non-negative require(result >= 0, "Result must be non-negative"); // Convert the result to uint256 s = uint256(result); } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import "./Power.sol"; import "@openzeppelin/contracts/utils/math/Math.sol"; /** * @title Bancor formula by Bancor * @dev Modified from the original by Slava Balasanov * https://github.com/bancorprotocol/contracts * Split Power.sol out from BancorFormula.sol and replace SafeMath formulas with zeppelin's SafeMath * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements; * and to You under the Apache License, Version 2.0. " */ contract BancorFormula is Power { string public constant version = "0.3.1"; uint32 public constant MAX_WEIGHT = 1000000; /** * @dev given a token supply, connector balance, weight and a deposit amount (in the connector token), * calculates the return for a given conversion (in the main token) * * Formula: * Return = _supply * ((1 + _depositAmount / _connectorBalance) ^ (_connectorWeight / 1000000) - 1) * * @param _supply token total supply * @param _connectorBalance total connector balance * @param _connectorWeight connector weight, represented in ppm, 1-1000000 * @param _depositAmount deposit amount, in connector token * * @return purchase return amount */ function calculatePurchaseReturn( uint256 _supply, uint256 _connectorBalance, uint32 _connectorWeight, uint256 _depositAmount ) internal view returns (uint256) { // validate input require(_supply > 0 && _connectorBalance > 0 && _connectorWeight > 0 && _connectorWeight <= MAX_WEIGHT); // special case for 0 deposit amount if (_depositAmount == 0) { return 0; } // special case if the weight = 100% if (_connectorWeight == MAX_WEIGHT) { return (_supply * _depositAmount) / _connectorBalance; } uint256 result; uint8 precision; uint256 baseN = _depositAmount + _connectorBalance; (result, precision) = power(baseN, _connectorBalance, _connectorWeight, MAX_WEIGHT); uint256 temp = (_supply * result) >> precision; return temp - _supply; } /** * @dev given a token supply, connector balance, weight and a sell amount (in the main token), * calculates the return for a given conversion (in the connector token) * * Formula: * Return = _connectorBalance * (1 - (1 - _sellAmount / _supply) ^ (1 / (_connectorWeight / 1000000))) * * @param _supply token total supply * @param _connectorBalance total connector * @param _connectorWeight constant connector Weight, represented in ppm, 1-1000000 * @param _sellAmount sell amount, in the token itself * * @return sale return amount */ function calculateSaleReturn(uint256 _supply, uint256 _connectorBalance, uint32 _connectorWeight, uint256 _sellAmount) internal view returns (uint256) { // validate input require(_supply > 0 && _connectorBalance > 0 && _connectorWeight > 0 && _connectorWeight <= MAX_WEIGHT && _sellAmount <= _supply); // special case for 0 sell amount if (_sellAmount == 0) { return 0; } // special case for selling the entire supply if (_sellAmount == _supply) { return _connectorBalance; } // special case if the weight = 100% if (_connectorWeight == MAX_WEIGHT) { return (_connectorBalance * _sellAmount) / _supply; } uint256 result; uint8 precision; uint256 baseD = _supply - _sellAmount; (result, precision) = power(_supply, baseD, MAX_WEIGHT, _connectorWeight); uint256 oldBalance = _connectorBalance * result; uint256 newBalance = _connectorBalance << precision; return (oldBalance - newBalance) / result; } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; // TODO: make this a library after constant uint256 array is supported /** * bancor formula by bancor * https://github.com/bancorprotocol/contracts * Modified from the original by Slava Balasanov * Split Power.sol out from BancorFormula.sol * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements; * and to You under the Apache License, Version 2.0. " */ contract Power { uint256 private constant ONE = 1; uint32 private constant MAX_WEIGHT = 1000000; uint8 private constant MIN_PRECISION = 32; uint8 private constant MAX_PRECISION = 127; /** The values below depend on MAX_PRECISION. If you choose to change it: Apply the same change in file 'PrintIntScalingFactors.py', run it and paste the results below. */ uint256 private constant FIXED_1 = 0x080000000000000000000000000000000; uint256 private constant FIXED_2 = 0x100000000000000000000000000000000; uint256 private constant MAX_NUM = 0x1ffffffffffffffffffffffffffffffff; /** The values below depend on MAX_PRECISION. If you choose to change it: Apply the same change in file 'PrintLn2ScalingFactors.py', run it and paste the results below. */ uint256 private constant LN2_MANTISSA = 0x2c5c85fdf473de6af278ece600fcbda; uint8 private constant LN2_EXPONENT = 122; /** The values below depend on MIN_PRECISION and MAX_PRECISION. If you choose to change either one of them: Apply the same change in file 'PrintFunctionBancorFormula.py', run it and paste the results below. */ uint256[128] private maxExpArray; // uint256[128] private constant maxExpArray = [ // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0, // 0x1c35fedd14ffffffffffffffffffffffff, // 0x1b0ce43b323fffffffffffffffffffffff, // 0x19f0028ec1ffffffffffffffffffffffff, // 0x18ded91f0e7fffffffffffffffffffffff, // 0x17d8ec7f0417ffffffffffffffffffffff, // 0x16ddc6556cdbffffffffffffffffffffff, // 0x15ecf52776a1ffffffffffffffffffffff, // 0x15060c256cb2ffffffffffffffffffffff, // 0x1428a2f98d72ffffffffffffffffffffff, // 0x13545598e5c23fffffffffffffffffffff, // 0x1288c4161ce1dfffffffffffffffffffff, // 0x11c592761c666fffffffffffffffffffff, // 0x110a688680a757ffffffffffffffffffff, // 0x1056f1b5bedf77ffffffffffffffffffff, // 0x0faadceceeff8bffffffffffffffffffff, // 0x0f05dc6b27edadffffffffffffffffffff, // 0x0e67a5a25da4107fffffffffffffffffff, // 0x0dcff115b14eedffffffffffffffffffff, // 0x0d3e7a392431239fffffffffffffffffff, // 0x0cb2ff529eb71e4fffffffffffffffffff, // 0x0c2d415c3db974afffffffffffffffffff, // 0x0bad03e7d883f69bffffffffffffffffff, // 0x0b320d03b2c343d5ffffffffffffffffff, // 0x0abc25204e02828dffffffffffffffffff, // 0x0a4b16f74ee4bb207fffffffffffffffff, // 0x09deaf736ac1f569ffffffffffffffffff, // 0x0976bd9952c7aa957fffffffffffffffff, // 0x09131271922eaa606fffffffffffffffff, // 0x08b380f3558668c46fffffffffffffffff, // 0x0857ddf0117efa215bffffffffffffffff, // 0x07ffffffffffffffffffffffffffffffff, // 0x07abbf6f6abb9d087fffffffffffffffff, // 0x075af62cbac95f7dfa7fffffffffffffff, // 0x070d7fb7452e187ac13fffffffffffffff, // 0x06c3390ecc8af379295fffffffffffffff, // 0x067c00a3b07ffc01fd6fffffffffffffff, // 0x0637b647c39cbb9d3d27ffffffffffffff, // 0x05f63b1fc104dbd39587ffffffffffffff, // 0x05b771955b36e12f7235ffffffffffffff, // 0x057b3d49dda84556d6f6ffffffffffffff, // 0x054183095b2c8ececf30ffffffffffffff, // 0x050a28be635ca2b888f77fffffffffffff, // 0x04d5156639708c9db33c3fffffffffffff, // 0x04a23105873875bd52dfdfffffffffffff, // 0x0471649d87199aa990756fffffffffffff, // 0x04429a21a029d4c1457cfbffffffffffff, // 0x0415bc6d6fb7dd71af2cb3ffffffffffff, // 0x03eab73b3bbfe282243ce1ffffffffffff, // 0x03c1771ac9fb6b4c18e229ffffffffffff, // 0x0399e96897690418f785257fffffffffff, // 0x0373fc456c53bb779bf0ea9fffffffffff, // 0x034f9e8e490c48e67e6ab8bfffffffffff, // 0x032cbfd4a7adc790560b3337ffffffffff, // 0x030b50570f6e5d2acca94613ffffffffff, // 0x02eb40f9f620fda6b56c2861ffffffffff, // 0x02cc8340ecb0d0f520a6af58ffffffffff, // 0x02af09481380a0a35cf1ba02ffffffffff, // 0x0292c5bdd3b92ec810287b1b3fffffffff, // 0x0277abdcdab07d5a77ac6d6b9fffffffff, // 0x025daf6654b1eaa55fd64df5efffffffff, // 0x0244c49c648baa98192dce88b7ffffffff, // 0x022ce03cd5619a311b2471268bffffffff, // 0x0215f77c045fbe885654a44a0fffffffff, // 0x01ffffffffffffffffffffffffffffffff, // 0x01eaefdbdaaee7421fc4d3ede5ffffffff, // 0x01d6bd8b2eb257df7e8ca57b09bfffffff, // 0x01c35fedd14b861eb0443f7f133fffffff, // 0x01b0ce43b322bcde4a56e8ada5afffffff, // 0x019f0028ec1fff007f5a195a39dfffffff, // 0x018ded91f0e72ee74f49b15ba527ffffff, // 0x017d8ec7f04136f4e5615fd41a63ffffff, // 0x016ddc6556cdb84bdc8d12d22e6fffffff, // 0x015ecf52776a1155b5bd8395814f7fffff, // 0x015060c256cb23b3b3cc3754cf40ffffff, // 0x01428a2f98d728ae223ddab715be3fffff, // 0x013545598e5c23276ccf0ede68034fffff, // 0x01288c4161ce1d6f54b7f61081194fffff, // 0x011c592761c666aa641d5a01a40f17ffff, // 0x0110a688680a7530515f3e6e6cfdcdffff, // 0x01056f1b5bedf75c6bcb2ce8aed428ffff, // 0x00faadceceeff8a0890f3875f008277fff, // 0x00f05dc6b27edad306388a600f6ba0bfff, // 0x00e67a5a25da41063de1495d5b18cdbfff, // 0x00dcff115b14eedde6fc3aa5353f2e4fff, // 0x00d3e7a3924312399f9aae2e0f868f8fff, // 0x00cb2ff529eb71e41582cccd5a1ee26fff, // 0x00c2d415c3db974ab32a51840c0b67edff, // 0x00bad03e7d883f69ad5b0a186184e06bff, // 0x00b320d03b2c343d4829abd6075f0cc5ff, // 0x00abc25204e02828d73c6e80bcdb1a95bf, // 0x00a4b16f74ee4bb2040a1ec6c15fbbf2df, // 0x009deaf736ac1f569deb1b5ae3f36c130f, // 0x00976bd9952c7aa957f5937d790ef65037, // 0x009131271922eaa6064b73a22d0bd4f2bf, // 0x008b380f3558668c46c91c49a2f8e967b9, // 0x00857ddf0117efa215952912839f6473e6 // ]; constructor() { // maxExpArray[ 0] = 0x6bffffffffffffffffffffffffffffffff; // maxExpArray[ 1] = 0x67ffffffffffffffffffffffffffffffff; // maxExpArray[ 2] = 0x637fffffffffffffffffffffffffffffff; // maxExpArray[ 3] = 0x5f6fffffffffffffffffffffffffffffff; // maxExpArray[ 4] = 0x5b77ffffffffffffffffffffffffffffff; // maxExpArray[ 5] = 0x57b3ffffffffffffffffffffffffffffff; // maxExpArray[ 6] = 0x5419ffffffffffffffffffffffffffffff; // maxExpArray[ 7] = 0x50a2ffffffffffffffffffffffffffffff; // maxExpArray[ 8] = 0x4d517fffffffffffffffffffffffffffff; // maxExpArray[ 9] = 0x4a233fffffffffffffffffffffffffffff; // maxExpArray[ 10] = 0x47165fffffffffffffffffffffffffffff; // maxExpArray[ 11] = 0x4429afffffffffffffffffffffffffffff; // maxExpArray[ 12] = 0x415bc7ffffffffffffffffffffffffffff; // maxExpArray[ 13] = 0x3eab73ffffffffffffffffffffffffffff; // maxExpArray[ 14] = 0x3c1771ffffffffffffffffffffffffffff; // maxExpArray[ 15] = 0x399e96ffffffffffffffffffffffffffff; // maxExpArray[ 16] = 0x373fc47fffffffffffffffffffffffffff; // maxExpArray[ 17] = 0x34f9e8ffffffffffffffffffffffffffff; // maxExpArray[ 18] = 0x32cbfd5fffffffffffffffffffffffffff; // maxExpArray[ 19] = 0x30b5057fffffffffffffffffffffffffff; // maxExpArray[ 20] = 0x2eb40f9fffffffffffffffffffffffffff; // maxExpArray[ 21] = 0x2cc8340fffffffffffffffffffffffffff; // maxExpArray[ 22] = 0x2af09481ffffffffffffffffffffffffff; // maxExpArray[ 23] = 0x292c5bddffffffffffffffffffffffffff; // maxExpArray[ 24] = 0x277abdcdffffffffffffffffffffffffff; // maxExpArray[ 25] = 0x25daf6657fffffffffffffffffffffffff; // maxExpArray[ 26] = 0x244c49c65fffffffffffffffffffffffff; // maxExpArray[ 27] = 0x22ce03cd5fffffffffffffffffffffffff; // maxExpArray[ 28] = 0x215f77c047ffffffffffffffffffffffff; // maxExpArray[ 29] = 0x1fffffffffffffffffffffffffffffffff; // maxExpArray[ 30] = 0x1eaefdbdabffffffffffffffffffffffff; // maxExpArray[ 31] = 0x1d6bd8b2ebffffffffffffffffffffffff; maxExpArray[32] = 0x1c35fedd14ffffffffffffffffffffffff; maxExpArray[33] = 0x1b0ce43b323fffffffffffffffffffffff; maxExpArray[34] = 0x19f0028ec1ffffffffffffffffffffffff; maxExpArray[35] = 0x18ded91f0e7fffffffffffffffffffffff; maxExpArray[36] = 0x17d8ec7f0417ffffffffffffffffffffff; maxExpArray[37] = 0x16ddc6556cdbffffffffffffffffffffff; maxExpArray[38] = 0x15ecf52776a1ffffffffffffffffffffff; maxExpArray[39] = 0x15060c256cb2ffffffffffffffffffffff; maxExpArray[40] = 0x1428a2f98d72ffffffffffffffffffffff; maxExpArray[41] = 0x13545598e5c23fffffffffffffffffffff; maxExpArray[42] = 0x1288c4161ce1dfffffffffffffffffffff; maxExpArray[43] = 0x11c592761c666fffffffffffffffffffff; maxExpArray[44] = 0x110a688680a757ffffffffffffffffffff; maxExpArray[45] = 0x1056f1b5bedf77ffffffffffffffffffff; maxExpArray[46] = 0x0faadceceeff8bffffffffffffffffffff; maxExpArray[47] = 0x0f05dc6b27edadffffffffffffffffffff; maxExpArray[48] = 0x0e67a5a25da4107fffffffffffffffffff; maxExpArray[49] = 0x0dcff115b14eedffffffffffffffffffff; maxExpArray[50] = 0x0d3e7a392431239fffffffffffffffffff; maxExpArray[51] = 0x0cb2ff529eb71e4fffffffffffffffffff; maxExpArray[52] = 0x0c2d415c3db974afffffffffffffffffff; maxExpArray[53] = 0x0bad03e7d883f69bffffffffffffffffff; maxExpArray[54] = 0x0b320d03b2c343d5ffffffffffffffffff; maxExpArray[55] = 0x0abc25204e02828dffffffffffffffffff; maxExpArray[56] = 0x0a4b16f74ee4bb207fffffffffffffffff; maxExpArray[57] = 0x09deaf736ac1f569ffffffffffffffffff; maxExpArray[58] = 0x0976bd9952c7aa957fffffffffffffffff; maxExpArray[59] = 0x09131271922eaa606fffffffffffffffff; maxExpArray[60] = 0x08b380f3558668c46fffffffffffffffff; maxExpArray[61] = 0x0857ddf0117efa215bffffffffffffffff; maxExpArray[62] = 0x07ffffffffffffffffffffffffffffffff; maxExpArray[63] = 0x07abbf6f6abb9d087fffffffffffffffff; maxExpArray[64] = 0x075af62cbac95f7dfa7fffffffffffffff; maxExpArray[65] = 0x070d7fb7452e187ac13fffffffffffffff; maxExpArray[66] = 0x06c3390ecc8af379295fffffffffffffff; maxExpArray[67] = 0x067c00a3b07ffc01fd6fffffffffffffff; maxExpArray[68] = 0x0637b647c39cbb9d3d27ffffffffffffff; maxExpArray[69] = 0x05f63b1fc104dbd39587ffffffffffffff; maxExpArray[70] = 0x05b771955b36e12f7235ffffffffffffff; maxExpArray[71] = 0x057b3d49dda84556d6f6ffffffffffffff; maxExpArray[72] = 0x054183095b2c8ececf30ffffffffffffff; maxExpArray[73] = 0x050a28be635ca2b888f77fffffffffffff; maxExpArray[74] = 0x04d5156639708c9db33c3fffffffffffff; maxExpArray[75] = 0x04a23105873875bd52dfdfffffffffffff; maxExpArray[76] = 0x0471649d87199aa990756fffffffffffff; maxExpArray[77] = 0x04429a21a029d4c1457cfbffffffffffff; maxExpArray[78] = 0x0415bc6d6fb7dd71af2cb3ffffffffffff; maxExpArray[79] = 0x03eab73b3bbfe282243ce1ffffffffffff; maxExpArray[80] = 0x03c1771ac9fb6b4c18e229ffffffffffff; maxExpArray[81] = 0x0399e96897690418f785257fffffffffff; maxExpArray[82] = 0x0373fc456c53bb779bf0ea9fffffffffff; maxExpArray[83] = 0x034f9e8e490c48e67e6ab8bfffffffffff; maxExpArray[84] = 0x032cbfd4a7adc790560b3337ffffffffff; maxExpArray[85] = 0x030b50570f6e5d2acca94613ffffffffff; maxExpArray[86] = 0x02eb40f9f620fda6b56c2861ffffffffff; maxExpArray[87] = 0x02cc8340ecb0d0f520a6af58ffffffffff; maxExpArray[88] = 0x02af09481380a0a35cf1ba02ffffffffff; maxExpArray[89] = 0x0292c5bdd3b92ec810287b1b3fffffffff; maxExpArray[90] = 0x0277abdcdab07d5a77ac6d6b9fffffffff; maxExpArray[91] = 0x025daf6654b1eaa55fd64df5efffffffff; maxExpArray[92] = 0x0244c49c648baa98192dce88b7ffffffff; maxExpArray[93] = 0x022ce03cd5619a311b2471268bffffffff; maxExpArray[94] = 0x0215f77c045fbe885654a44a0fffffffff; maxExpArray[95] = 0x01ffffffffffffffffffffffffffffffff; maxExpArray[96] = 0x01eaefdbdaaee7421fc4d3ede5ffffffff; maxExpArray[97] = 0x01d6bd8b2eb257df7e8ca57b09bfffffff; maxExpArray[98] = 0x01c35fedd14b861eb0443f7f133fffffff; maxExpArray[99] = 0x01b0ce43b322bcde4a56e8ada5afffffff; maxExpArray[100] = 0x019f0028ec1fff007f5a195a39dfffffff; maxExpArray[101] = 0x018ded91f0e72ee74f49b15ba527ffffff; maxExpArray[102] = 0x017d8ec7f04136f4e5615fd41a63ffffff; maxExpArray[103] = 0x016ddc6556cdb84bdc8d12d22e6fffffff; maxExpArray[104] = 0x015ecf52776a1155b5bd8395814f7fffff; maxExpArray[105] = 0x015060c256cb23b3b3cc3754cf40ffffff; maxExpArray[106] = 0x01428a2f98d728ae223ddab715be3fffff; maxExpArray[107] = 0x013545598e5c23276ccf0ede68034fffff; maxExpArray[108] = 0x01288c4161ce1d6f54b7f61081194fffff; maxExpArray[109] = 0x011c592761c666aa641d5a01a40f17ffff; maxExpArray[110] = 0x0110a688680a7530515f3e6e6cfdcdffff; maxExpArray[111] = 0x01056f1b5bedf75c6bcb2ce8aed428ffff; maxExpArray[112] = 0x00faadceceeff8a0890f3875f008277fff; maxExpArray[113] = 0x00f05dc6b27edad306388a600f6ba0bfff; maxExpArray[114] = 0x00e67a5a25da41063de1495d5b18cdbfff; maxExpArray[115] = 0x00dcff115b14eedde6fc3aa5353f2e4fff; maxExpArray[116] = 0x00d3e7a3924312399f9aae2e0f868f8fff; maxExpArray[117] = 0x00cb2ff529eb71e41582cccd5a1ee26fff; maxExpArray[118] = 0x00c2d415c3db974ab32a51840c0b67edff; maxExpArray[119] = 0x00bad03e7d883f69ad5b0a186184e06bff; maxExpArray[120] = 0x00b320d03b2c343d4829abd6075f0cc5ff; maxExpArray[121] = 0x00abc25204e02828d73c6e80bcdb1a95bf; maxExpArray[122] = 0x00a4b16f74ee4bb2040a1ec6c15fbbf2df; maxExpArray[123] = 0x009deaf736ac1f569deb1b5ae3f36c130f; maxExpArray[124] = 0x00976bd9952c7aa957f5937d790ef65037; maxExpArray[125] = 0x009131271922eaa6064b73a22d0bd4f2bf; maxExpArray[126] = 0x008b380f3558668c46c91c49a2f8e967b9; maxExpArray[127] = 0x00857ddf0117efa215952912839f6473e6; } /** Compute the largest integer smaller than or equal to the binary logarithm of the input. */ function floorLog2(uint256 _n) internal pure returns (uint8) { uint8 res = 0; uint256 n = _n; if (n < 256) { // At most 8 iterations while (n > 1) { n >>= 1; res += 1; } } else { // Exactly 8 iterations for (uint8 s = 128; s > 0; s >>= 1) { if (n >= (ONE << s)) { n >>= s; res |= s; } } } return res; } /** Return floor(ln(numerator / denominator) * 2 ^ MAX_PRECISION), where: - The numerator is a value between 1 and 2 ^ (256 - MAX_PRECISION) - 1 - The denominator is a value between 1 and 2 ^ (256 - MAX_PRECISION) - 1 - The output is a value between 0 and floor(ln(2 ^ (256 - MAX_PRECISION) - 1) * 2 ^ MAX_PRECISION) This functions assumes that the numerator is larger than or equal to the denominator, because the output would be negative otherwise. */ function ln(uint256 _numerator, uint256 _denominator) internal pure returns (uint256) { assert(_numerator <= MAX_NUM); uint256 res = 0; uint256 x = (_numerator * FIXED_1) / _denominator; // If x >= 2, then we compute the integer part of log2(x), which is larger than 0. if (x >= FIXED_2) { uint8 count = floorLog2(x / FIXED_1); x >>= count; // now x < 2 res = count * FIXED_1; } // If x > 1, then we compute the fraction part of log2(x), which is larger than 0. if (x > FIXED_1) { for (uint8 i = MAX_PRECISION; i > 0; --i) { x = (x * x) / FIXED_1; // now 1 < x < 4 if (x >= FIXED_2) { x >>= 1; // now 1 < x < 2 res += ONE << (i - 1); } } } return (res * LN2_MANTISSA) >> LN2_EXPONENT; } /** This function can be auto-generated by the script 'PrintFunctionFixedExp.py'. It approximates "e ^ x" via maclaurin summation: "(x^0)/0! + (x^1)/1! + ... + (x^n)/n!". It returns "e ^ (x / 2 ^ precision) * 2 ^ precision", that is, the result is upshifted for accuracy. The global "maxExpArray" maps each "precision" to "((maximumExponent + 1) << (MAX_PRECISION - precision)) - 1". The maximum permitted value for "x" is therefore given by "maxExpArray[precision] >> (MAX_PRECISION - precision)". */ function fixedExp(uint256 _x, uint8 _precision) internal pure returns (uint256) { uint256 xi = _x; uint256 res = 0; xi = (xi * _x) >> _precision; res += xi * 0x03442c4e6074a82f1797f72ac0000000; // add x^2 * (33! / 2!) xi = (xi * _x) >> _precision; res += xi * 0x0116b96f757c380fb287fd0e40000000; // add x^3 * (33! / 3!) xi = (xi * _x) >> _precision; res += xi * 0x0045ae5bdd5f0e03eca1ff4390000000; // add x^4 * (33! / 4!) xi = (xi * _x) >> _precision; res += xi * 0x000defabf91302cd95b9ffda50000000; // add x^5 * (33! / 5!) xi = (xi * _x) >> _precision; res += xi * 0x0002529ca9832b22439efff9b8000000; // add x^6 * (33! / 6!) xi = (xi * _x) >> _precision; res += xi * 0x000054f1cf12bd04e516b6da88000000; // add x^7 * (33! / 7!) xi = (xi * _x) >> _precision; res += xi * 0x00000a9e39e257a09ca2d6db51000000; // add x^8 * (33! / 8!) xi = (xi * _x) >> _precision; res += xi * 0x0000012e066e7b839fa050c309000000; // add x^9 * (33! / 9!) xi = (xi * _x) >> _precision; res += xi * 0x0000001e33d7d926c329a1ad1a800000; // add x^10 * (33! / 10!) xi = (xi * _x) >> _precision; res += xi * 0x00000002bee513bdb4a6b19b5f800000; // add x^11 * (33! / 11!) xi = (xi * _x) >> _precision; res += xi * 0x000000003a9316fa79b88eccf2a00000; // add x^12 * (33! / 12!) xi = (xi * _x) >> _precision; res += xi * 0x00000000048177ebe1fa812375200000; // add x^13 * (33! / 13!) xi = (xi * _x) >> _precision; res += xi * 0x00000000005263fe90242dcbacf00000; // add x^14 * (33! / 14!) xi = (xi * _x) >> _precision; res += xi * 0x0000000000057e22099c030d94100000; // add x^15 * (33! / 15!) xi = (xi * _x) >> _precision; res += xi * 0x00000000000057e22099c030d9410000; // add x^16 * (33! / 16!) xi = (xi * _x) >> _precision; res += xi * 0x000000000000052b6b54569976310000; // add x^17 * (33! / 17!) xi = (xi * _x) >> _precision; res += xi * 0x000000000000004985f67696bf748000; // add x^18 * (33! / 18!) xi = (xi * _x) >> _precision; res += xi * 0x0000000000000003dea12ea99e498000; // add x^19 * (33! / 19!) xi = (xi * _x) >> _precision; res += xi * 0x000000000000000031880f2214b6e000; // add x^20 * (33! / 20!) xi = (xi * _x) >> _precision; res += xi * 0x0000000000000000025bcff56eb36000; // add x^21 * (33! / 21!) xi = (xi * _x) >> _precision; res += xi * 0x0000000000000000001b722e10ab1000; // add x^22 * (33! / 22!) xi = (xi * _x) >> _precision; res += xi * 0x00000000000000000001317c70077000; // add x^23 * (33! / 23!) xi = (xi * _x) >> _precision; res += xi * 0x000000000000000000000cba84aafa00; // add x^24 * (33! / 24!) xi = (xi * _x) >> _precision; res += xi * 0x000000000000000000000082573a0a00; // add x^25 * (33! / 25!) xi = (xi * _x) >> _precision; res += xi * 0x000000000000000000000005035ad900; // add x^26 * (33! / 26!) xi = (xi * _x) >> _precision; res += xi * 0x0000000000000000000000002f881b00; // add x^27 * (33! / 27!) xi = (xi * _x) >> _precision; res += xi * 0x00000000000000000000000001b29340; // add x^28 * (33! / 28!) xi = (xi * _x) >> _precision; res += xi * 0x000000000000000000000000000efc40; // add x^29 * (33! / 29!) xi = (xi * _x) >> _precision; res += xi * 0x00000000000000000000000000007fe0; // add x^30 * (33! / 30!) xi = (xi * _x) >> _precision; res += xi * 0x00000000000000000000000000000420; // add x^31 * (33! / 31!) xi = (xi * _x) >> _precision; res += xi * 0x00000000000000000000000000000021; // add x^32 * (33! / 32!) xi = (xi * _x) >> _precision; res += xi * 0x00000000000000000000000000000001; // add x^33 * (33! / 33!) return res / 0x688589cc0e9505e2f2fee5580000000 + _x + (ONE << _precision); // divide by 33! and then add x^1 / 1! + x^0 / 0! } /** The global "maxExpArray" is sorted in descending order, and therefore the following statements are equivalent: - This function finds the position of [the smallest value in "maxExpArray" larger than or equal to "x"] - This function finds the highest position of [a value in "maxExpArray" larger than or equal to "x"] */ function findPositionInMaxExpArray(uint256 _x) internal view returns (uint8) { uint8 lo = MIN_PRECISION; uint8 hi = MAX_PRECISION; while (lo + 1 < hi) { uint8 mid = (lo + hi) / 2; if (maxExpArray[mid] >= _x) lo = mid; else hi = mid; } if (maxExpArray[hi] >= _x) return hi; if (maxExpArray[lo] >= _x) return lo; assert(false); return 0; } /** General Description: Determine a value of precision. Calculate an integer approximation of (_baseN / _baseD) ^ (_expN / _expD) * 2 ^ precision. Return the result along with the precision used. Detailed Description: Instead of calculating "base ^ exp", we calculate "e ^ (ln(base) * exp)". The value of "ln(base)" is represented with an integer slightly smaller than "ln(base) * 2 ^ precision". The larger "precision" is, the more accurately this value represents the real value. However, the larger "precision" is, the more bits are required in order to store this value. And the exponentiation function, which takes "x" and calculates "e ^ x", is limited to a maximum exponent (maximum value of "x"). This maximum exponent depends on the "precision" used, and it is given by "maxExpArray[precision] >> (MAX_PRECISION - precision)". Hence we need to determine the highest precision which can be used for the given input, before calling the exponentiation function. This allows us to compute "base ^ exp" with maximum accuracy and without exceeding 256 bits in any of the intermediate computations. */ function power(uint256 _baseN, uint256 _baseD, uint32 _expN, uint32 _expD) internal view returns (uint256, uint8) { uint256 lnBaseTimesExp = (ln(_baseN, _baseD) * _expN) / _expD; uint8 precision = findPositionInMaxExpArray(lnBaseTimesExp); return (fixedExp(lnBaseTimesExp >> (MAX_PRECISION - precision), precision), precision); } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; interface IUniswapV2Router02 { function addLiquidityETH( address token, uint amountTokenDesired, uint amountTokenMin, uint amountETHMin, address to, uint deadline ) external payable returns (uint amountToken, uint amountETH, uint liquidity); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable.sol) pragma solidity ^0.8.0; import "../token/extensions/Context.sol"; /** * @dev Contract module which provides a basic access control mechanism, where * there is an account (an owner) that can be granted exclusive access to * specific functions. * * By default, the owner account will be the one that deploys the contract. This * can later be changed with {transferOwnership}. * * This module is used through inheritance. It will make available the modifier * `onlyOwner`, which can be applied to your functions to restrict their use to * the owner. */ abstract contract Ownable is Context { address private _owner; event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); /** * @dev Initializes the contract setting the deployer as the initial owner. */ constructor() { _transferOwnership(_msgSender()); } /** * @dev Throws if called by any account other than the owner. */ modifier onlyOwner() { _checkOwner(); _; } /** * @dev Returns the address of the current owner. */ function owner() public view virtual returns (address) { return _owner; } /** * @dev Throws if the sender is not the owner. */ function _checkOwner() internal view virtual { require(owner() == _msgSender(), "Ownable: caller is not the owner"); } /** * @dev Leaves the contract without owner. It will not be possible to call * `onlyOwner` functions. Can only be called by the current owner. * * NOTE: Renouncing ownership will leave the contract without an owner, * thereby disabling any functionality that is only available to the owner. */ function renounceOwnership() public virtual onlyOwner { _transferOwnership(address(0)); } /** * @dev Transfers ownership of the contract to a new account (`newOwner`). * Can only be called by the current owner. */ function transferOwnership(address newOwner) public virtual onlyOwner { require(newOwner != address(0), "Ownable: new owner is the zero address"); _transferOwnership(newOwner); } /** * @dev Transfers ownership of the contract to a new account (`newOwner`). * Internal function without access restriction. */ function _transferOwnership(address newOwner) internal virtual { address oldOwner = _owner; _owner = newOwner; emit OwnershipTransferred(oldOwner, newOwner); } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/ERC20.sol) pragma solidity ^0.8.0; import "./IERC20.sol"; import "./extensions/IERC20Metadata.sol"; import "./extensions/Context.sol"; /** * @dev Implementation of the {IERC20} interface. * * This implementation is agnostic to the way tokens are created. This means * that a supply mechanism has to be added in a derived contract using {_mint}. * For a generic mechanism see {ERC20PresetMinterPauser}. * * TIP: For a detailed writeup see our guide * https://forum.openzeppelin.com/t/how-to-implement-erc20-supply-mechanisms/226[How * to implement supply mechanisms]. * * The default value of {decimals} is 18. To change this, you should override * this function so it returns a different value. * * We have followed general OpenZeppelin Contracts guidelines: functions revert * instead returning `false` on failure. This behavior is nonetheless * conventional and does not conflict with the expectations of ERC20 * applications. * * Additionally, an {Approval} event is emitted on calls to {transferFrom}. * This allows applications to reconstruct the allowance for all accounts just * by listening to said events. Other implementations of the EIP may not emit * these events, as it isn't required by the specification. * * Finally, the non-standard {decreaseAllowance} and {increaseAllowance} * functions have been added to mitigate the well-known issues around setting * allowances. See {IERC20-approve}. */ contract ERC20 is Context, IERC20, IERC20Metadata { mapping(address => uint256) private _balances; mapping(address => mapping(address => uint256)) private _allowances; uint256 private _totalSupply; string private _name; string private _symbol; /** * @dev Sets the values for {name} and {symbol}. * * All two of these values are immutable: they can only be set once during * construction. */ constructor(string memory name_, string memory symbol_) { _name = name_; _symbol = symbol_; } /** * @dev Returns the name of the token. */ function name() public view virtual override returns (string memory) { return _name; } /** * @dev Returns the symbol of the token, usually a shorter version of the * name. */ function symbol() public view virtual override returns (string memory) { return _symbol; } /** * @dev Returns the number of decimals used to get its user representation. * For example, if `decimals` equals `2`, a balance of `505` tokens should * be displayed to a user as `5.05` (`505 / 10 ** 2`). * * Tokens usually opt for a value of 18, imitating the relationship between * Ether and Wei. This is the default value returned by this function, unless * it's overridden. * * NOTE: This information is only used for _display_ purposes: it in * no way affects any of the arithmetic of the contract, including * {IERC20-balanceOf} and {IERC20-transfer}. */ function decimals() public view virtual override returns (uint8) { return 18; } /** * @dev See {IERC20-totalSupply}. */ function totalSupply() public view virtual override returns (uint256) { return _totalSupply; } /** * @dev See {IERC20-balanceOf}. */ function balanceOf(address account) public view virtual override returns (uint256) { return _balances[account]; } /** * @dev See {IERC20-transfer}. * * Requirements: * * - `to` cannot be the zero address. * - the caller must have a balance of at least `amount`. */ function transfer(address to, uint256 amount) public virtual override returns (bool) { address owner = _msgSender(); _transfer(owner, to, amount); return true; } /** * @dev See {IERC20-allowance}. */ function allowance(address owner, address spender) public view virtual override returns (uint256) { return _allowances[owner][spender]; } /** * @dev See {IERC20-approve}. * * NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on * `transferFrom`. This is semantically equivalent to an infinite approval. * * Requirements: * * - `spender` cannot be the zero address. */ function approve(address spender, uint256 amount) public virtual override returns (bool) { address owner = _msgSender(); _approve(owner, spender, amount); return true; } /** * @dev See {IERC20-transferFrom}. * * Emits an {Approval} event indicating the updated allowance. This is not * required by the EIP. See the note at the beginning of {ERC20}. * * NOTE: Does not update the allowance if the current allowance * is the maximum `uint256`. * * Requirements: * * - `from` and `to` cannot be the zero address. * - `from` must have a balance of at least `amount`. * - the caller must have allowance for ``from``'s tokens of at least * `amount`. */ function transferFrom(address from, address to, uint256 amount) public virtual override returns (bool) { address spender = _msgSender(); _spendAllowance(from, spender, amount); _transfer(from, to, amount); return true; } /** * @dev Atomically increases the allowance granted to `spender` by the caller. * * This is an alternative to {approve} that can be used as a mitigation for * problems described in {IERC20-approve}. * * Emits an {Approval} event indicating the updated allowance. * * Requirements: * * - `spender` cannot be the zero address. */ function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) { address owner = _msgSender(); _approve(owner, spender, allowance(owner, spender) + addedValue); return true; } /** * @dev Atomically decreases the allowance granted to `spender` by the caller. * * This is an alternative to {approve} that can be used as a mitigation for * problems described in {IERC20-approve}. * * Emits an {Approval} event indicating the updated allowance. * * Requirements: * * - `spender` cannot be the zero address. * - `spender` must have allowance for the caller of at least * `subtractedValue`. */ function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) { address owner = _msgSender(); uint256 currentAllowance = allowance(owner, spender); require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero"); unchecked { _approve(owner, spender, currentAllowance - subtractedValue); } return true; } /** * @dev Moves `amount` of tokens from `from` to `to`. * * This internal function is equivalent to {transfer}, and can be used to * e.g. implement automatic token fees, slashing mechanisms, etc. * * Emits a {Transfer} event. * * Requirements: * * - `from` cannot be the zero address. * - `to` cannot be the zero address. * - `from` must have a balance of at least `amount`. */ function _transfer(address from, address to, uint256 amount) internal virtual { require(from != address(0), "ERC20: transfer from the zero address"); require(to != address(0), "ERC20: transfer to the zero address"); _beforeTokenTransfer(from, to, amount); uint256 fromBalance = _balances[from]; require(fromBalance >= amount, "ERC20: transfer amount exceeds balance"); unchecked { _balances[from] = fromBalance - amount; // Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by // decrementing then incrementing. _balances[to] += amount; } emit Transfer(from, to, amount); _afterTokenTransfer(from, to, amount); } /** @dev Creates `amount` tokens and assigns them to `account`, increasing * the total supply. * * Emits a {Transfer} event with `from` set to the zero address. * * Requirements: * * - `account` cannot be the zero address. */ function _mint(address account, uint256 amount) internal virtual { require(account != address(0), "ERC20: mint to the zero address"); _beforeTokenTransfer(address(0), account, amount); _totalSupply += amount; unchecked { // Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above. _balances[account] += amount; } emit Transfer(address(0), account, amount); _afterTokenTransfer(address(0), account, amount); } /** * @dev Destroys `amount` tokens from `account`, reducing the * total supply. * * Emits a {Transfer} event with `to` set to the zero address. * * Requirements: * * - `account` cannot be the zero address. * - `account` must have at least `amount` tokens. */ function _burn(address account, uint256 amount) internal virtual { require(account != address(0), "ERC20: burn from the zero address"); _beforeTokenTransfer(account, address(0), amount); uint256 accountBalance = _balances[account]; require(accountBalance >= amount, "ERC20: burn amount exceeds balance"); unchecked { _balances[account] = accountBalance - amount; // Overflow not possible: amount <= accountBalance <= totalSupply. _totalSupply -= amount; } emit Transfer(account, address(0), amount); _afterTokenTransfer(account, address(0), amount); } /** * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens. * * This internal function is equivalent to `approve`, and can be used to * e.g. set automatic allowances for certain subsystems, etc. * * Emits an {Approval} event. * * Requirements: * * - `owner` cannot be the zero address. * - `spender` cannot be the zero address. */ function _approve(address owner, address spender, uint256 amount) internal virtual { require(owner != address(0), "ERC20: approve from the zero address"); require(spender != address(0), "ERC20: approve to the zero address"); _allowances[owner][spender] = amount; emit Approval(owner, spender, amount); } /** * @dev Updates `owner` s allowance for `spender` based on spent `amount`. * * Does not update the allowance amount in case of infinite allowance. * Revert if not enough allowance is available. * * Might emit an {Approval} event. */ function _spendAllowance(address owner, address spender, uint256 amount) internal virtual { uint256 currentAllowance = allowance(owner, spender); if (currentAllowance != type(uint256).max) { require(currentAllowance >= amount, "ERC20: insufficient allowance"); unchecked { _approve(owner, spender, currentAllowance - amount); } } } /** * @dev Hook that is called before any transfer of tokens. This includes * minting and burning. * * Calling conditions: * * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens * will be transferred to `to`. * - when `from` is zero, `amount` tokens will be minted for `to`. * - when `to` is zero, `amount` of ``from``'s tokens will be burned. * - `from` and `to` are never both zero. * * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. */ function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual {} /** * @dev Hook that is called after any transfer of tokens. This includes * minting and burning. * * Calling conditions: * * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens * has been transferred to `to`. * - when `from` is zero, `amount` tokens have been minted for `to`. * - when `to` is zero, `amount` of ``from``'s tokens have been burned. * - `from` and `to` are never both zero. * * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. */ function _afterTokenTransfer(address from, address to, uint256 amount) internal virtual {} }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.4) (utils/Context.sol) pragma solidity ^0.8.0; /** * @dev Provides information about the current execution context, including the * sender of the transaction and its data. While these are generally available * via msg.sender and msg.data, they should not be accessed in such a direct * manner, since when dealing with meta-transactions the account sending and * paying for execution may not be the actual sender (as far as an application * is concerned). * * This contract is only required for intermediate, library-like contracts. */ abstract contract Context { function _msgSender() internal view virtual returns (address) { return msg.sender; } function _msgData() internal view virtual returns (bytes calldata) { return msg.data; } function _contextSuffixLength() internal view virtual returns (uint256) { return 0; } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/extensions/ERC20Burnable.sol) pragma solidity ^0.8.0; import "../ERC20.sol"; import "./Context.sol"; /** * @dev Extension of {ERC20} that allows token holders to destroy both their own * tokens and those that they have an allowance for, in a way that can be * recognized off-chain (via event analysis). */ abstract contract ERC20Burnable is Context, ERC20 { /** * @dev Destroys `amount` tokens from the caller. * * See {ERC20-_burn}. */ function burn(uint256 amount) public virtual { _burn(_msgSender(), amount); } /** * @dev Destroys `amount` tokens from `account`, deducting from the caller's * allowance. * * See {ERC20-_burn} and {ERC20-allowance}. * * Requirements: * * - the caller must have allowance for ``accounts``'s tokens of at least * `amount`. */ function burnFrom(address account, uint256 amount) public virtual { _spendAllowance(account, _msgSender(), amount); _burn(account, amount); } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol) pragma solidity ^0.8.0; import "../IERC20.sol"; /** * @dev Interface for the optional metadata functions from the ERC20 standard. * * _Available since v4.1._ */ interface IERC20Metadata is IERC20 { /** * @dev Returns the name of the token. */ function name() external view returns (string memory); /** * @dev Returns the symbol of the token. */ function symbol() external view returns (string memory); /** * @dev Returns the decimals places of the token. */ function decimals() external view returns (uint8); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol) pragma solidity ^0.8.0; /** * @dev Interface of the ERC20 standard as defined in the EIP. */ interface IERC20 { /** * @dev Emitted when `value` tokens are moved from one account (`from`) to * another (`to`). * * Note that `value` may be zero. */ event Transfer(address indexed from, address indexed to, uint256 value); /** * @dev Emitted when the allowance of a `spender` for an `owner` is set by * a call to {approve}. `value` is the new allowance. */ event Approval(address indexed owner, address indexed spender, uint256 value); /** * @dev Returns the amount of tokens in existence. */ function totalSupply() external view returns (uint256); /** * @dev Returns the amount of tokens owned by `account`. */ function balanceOf(address account) external view returns (uint256); /** * @dev Moves `amount` tokens from the caller's account to `to`. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transfer(address to, uint256 amount) external returns (bool); /** * @dev Returns the remaining number of tokens that `spender` will be * allowed to spend on behalf of `owner` through {transferFrom}. This is * zero by default. * * This value changes when {approve} or {transferFrom} are called. */ function allowance(address owner, address spender) external view returns (uint256); /** * @dev Sets `amount` as the allowance of `spender` over the caller's tokens. * * Returns a boolean value indicating whether the operation succeeded. * * IMPORTANT: Beware that changing an allowance with this method brings the risk * that someone may use both the old and the new allowance by unfortunate * transaction ordering. One possible solution to mitigate this race * condition is to first reduce the spender's allowance to 0 and set the * desired value afterwards: * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 * * Emits an {Approval} event. */ function approve(address spender, uint256 amount) external returns (bool); /** * @dev Moves `amount` tokens from `from` to `to` using the * allowance mechanism. `amount` is then deducted from the caller's * allowance. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transferFrom(address from, address to, uint256 amount) external returns (bool); }
// SPDX-License-Identifier: Unlicense pragma solidity >=0.8.4; /// @notice Emitted when the result overflows uint256. error PRBMath__MulDivFixedPointOverflow(uint256 prod1); /// @notice Emitted when the result overflows uint256. error PRBMath__MulDivOverflow(uint256 prod1, uint256 denominator); /// @notice Emitted when one of the inputs is type(int256).min. error PRBMath__MulDivSignedInputTooSmall(); /// @notice Emitted when the intermediary absolute result overflows int256. error PRBMath__MulDivSignedOverflow(uint256 rAbs); /// @notice Emitted when the input is MIN_SD59x18. error PRBMathSD59x18__AbsInputTooSmall(); /// @notice Emitted when ceiling a number overflows SD59x18. error PRBMathSD59x18__CeilOverflow(int256 x); /// @notice Emitted when one of the inputs is MIN_SD59x18. error PRBMathSD59x18__DivInputTooSmall(); /// @notice Emitted when one of the intermediary unsigned results overflows SD59x18. error PRBMathSD59x18__DivOverflow(uint256 rAbs); /// @notice Emitted when the input is greater than 133.084258667509499441. error PRBMathSD59x18__ExpInputTooBig(int256 x); /// @notice Emitted when the input is greater than 192. error PRBMathSD59x18__Exp2InputTooBig(int256 x); /// @notice Emitted when flooring a number underflows SD59x18. error PRBMathSD59x18__FloorUnderflow(int256 x); /// @notice Emitted when converting a basic integer to the fixed-point format overflows SD59x18. error PRBMathSD59x18__FromIntOverflow(int256 x); /// @notice Emitted when converting a basic integer to the fixed-point format underflows SD59x18. error PRBMathSD59x18__FromIntUnderflow(int256 x); /// @notice Emitted when the product of the inputs is negative. error PRBMathSD59x18__GmNegativeProduct(int256 x, int256 y); /// @notice Emitted when multiplying the inputs overflows SD59x18. error PRBMathSD59x18__GmOverflow(int256 x, int256 y); /// @notice Emitted when the input is less than or equal to zero. error PRBMathSD59x18__LogInputTooSmall(int256 x); /// @notice Emitted when one of the inputs is MIN_SD59x18. error PRBMathSD59x18__MulInputTooSmall(); /// @notice Emitted when the intermediary absolute result overflows SD59x18. error PRBMathSD59x18__MulOverflow(uint256 rAbs); /// @notice Emitted when the intermediary absolute result overflows SD59x18. error PRBMathSD59x18__PowuOverflow(uint256 rAbs); /// @notice Emitted when the input is negative. error PRBMathSD59x18__SqrtNegativeInput(int256 x); /// @notice Emitted when the calculating the square root overflows SD59x18. error PRBMathSD59x18__SqrtOverflow(int256 x); /// @notice Emitted when addition overflows UD60x18. error PRBMathUD60x18__AddOverflow(uint256 x, uint256 y); /// @notice Emitted when ceiling a number overflows UD60x18. error PRBMathUD60x18__CeilOverflow(uint256 x); /// @notice Emitted when the input is greater than 133.084258667509499441. error PRBMathUD60x18__ExpInputTooBig(uint256 x); /// @notice Emitted when the input is greater than 192. error PRBMathUD60x18__Exp2InputTooBig(uint256 x); /// @notice Emitted when converting a basic integer to the fixed-point format format overflows UD60x18. error PRBMathUD60x18__FromUintOverflow(uint256 x); /// @notice Emitted when multiplying the inputs overflows UD60x18. error PRBMathUD60x18__GmOverflow(uint256 x, uint256 y); /// @notice Emitted when the input is less than 1. error PRBMathUD60x18__LogInputTooSmall(uint256 x); /// @notice Emitted when the calculating the square root overflows UD60x18. error PRBMathUD60x18__SqrtOverflow(uint256 x); /// @notice Emitted when subtraction underflows UD60x18. error PRBMathUD60x18__SubUnderflow(uint256 x, uint256 y); /// @dev Common mathematical functions used in both PRBMathSD59x18 and PRBMathUD60x18. Note that this shared library /// does not always assume the signed 59.18-decimal fixed-point or the unsigned 60.18-decimal fixed-point /// representation. When it does not, it is explicitly mentioned in the NatSpec documentation. library PRBMath { /// STRUCTS /// struct SD59x18 { int256 value; } struct UD60x18 { uint256 value; } /// STORAGE /// /// @dev How many trailing decimals can be represented. uint256 internal constant SCALE = 1e18; /// @dev Largest power of two divisor of SCALE. uint256 internal constant SCALE_LPOTD = 262144; /// @dev SCALE inverted mod 2^256. uint256 internal constant SCALE_INVERSE = 78156646155174841979727994598816262306175212592076161876661_508869554232690281; /// FUNCTIONS /// /// @notice Calculates the binary exponent of x using the binary fraction method. /// @dev Has to use 192.64-bit fixed-point numbers. /// See https://ethereum.stackexchange.com/a/96594/24693. /// @param x The exponent as an unsigned 192.64-bit fixed-point number. /// @return result The result as an unsigned 60.18-decimal fixed-point number. function exp2(uint256 x) internal pure returns (uint256 result) { unchecked { // Start from 0.5 in the 192.64-bit fixed-point format. result = 0x800000000000000000000000000000000000000000000000; // Multiply the result by root(2, 2^-i) when the bit at position i is 1. None of the intermediary results overflows // because the initial result is 2^191 and all magic factors are less than 2^65. if (x & 0x8000000000000000 > 0) { result = (result * 0x16A09E667F3BCC909) >> 64; } if (x & 0x4000000000000000 > 0) { result = (result * 0x1306FE0A31B7152DF) >> 64; } if (x & 0x2000000000000000 > 0) { result = (result * 0x1172B83C7D517ADCE) >> 64; } if (x & 0x1000000000000000 > 0) { result = (result * 0x10B5586CF9890F62A) >> 64; } if (x & 0x800000000000000 > 0) { result = (result * 0x1059B0D31585743AE) >> 64; } if (x & 0x400000000000000 > 0) { result = (result * 0x102C9A3E778060EE7) >> 64; } if (x & 0x200000000000000 > 0) { result = (result * 0x10163DA9FB33356D8) >> 64; } if (x & 0x100000000000000 > 0) { result = (result * 0x100B1AFA5ABCBED61) >> 64; } if (x & 0x80000000000000 > 0) { result = (result * 0x10058C86DA1C09EA2) >> 64; } if (x & 0x40000000000000 > 0) { result = (result * 0x1002C605E2E8CEC50) >> 64; } if (x & 0x20000000000000 > 0) { result = (result * 0x100162F3904051FA1) >> 64; } if (x & 0x10000000000000 > 0) { result = (result * 0x1000B175EFFDC76BA) >> 64; } if (x & 0x8000000000000 > 0) { result = (result * 0x100058BA01FB9F96D) >> 64; } if (x & 0x4000000000000 > 0) { result = (result * 0x10002C5CC37DA9492) >> 64; } if (x & 0x2000000000000 > 0) { result = (result * 0x1000162E525EE0547) >> 64; } if (x & 0x1000000000000 > 0) { result = (result * 0x10000B17255775C04) >> 64; } if (x & 0x800000000000 > 0) { result = (result * 0x1000058B91B5BC9AE) >> 64; } if (x & 0x400000000000 > 0) { result = (result * 0x100002C5C89D5EC6D) >> 64; } if (x & 0x200000000000 > 0) { result = (result * 0x10000162E43F4F831) >> 64; } if (x & 0x100000000000 > 0) { result = (result * 0x100000B1721BCFC9A) >> 64; } if (x & 0x80000000000 > 0) { result = (result * 0x10000058B90CF1E6E) >> 64; } if (x & 0x40000000000 > 0) { result = (result * 0x1000002C5C863B73F) >> 64; } if (x & 0x20000000000 > 0) { result = (result * 0x100000162E430E5A2) >> 64; } if (x & 0x10000000000 > 0) { result = (result * 0x1000000B172183551) >> 64; } if (x & 0x8000000000 > 0) { result = (result * 0x100000058B90C0B49) >> 64; } if (x & 0x4000000000 > 0) { result = (result * 0x10000002C5C8601CC) >> 64; } if (x & 0x2000000000 > 0) { result = (result * 0x1000000162E42FFF0) >> 64; } if (x & 0x1000000000 > 0) { result = (result * 0x10000000B17217FBB) >> 64; } if (x & 0x800000000 > 0) { result = (result * 0x1000000058B90BFCE) >> 64; } if (x & 0x400000000 > 0) { result = (result * 0x100000002C5C85FE3) >> 64; } if (x & 0x200000000 > 0) { result = (result * 0x10000000162E42FF1) >> 64; } if (x & 0x100000000 > 0) { result = (result * 0x100000000B17217F8) >> 64; } if (x & 0x80000000 > 0) { result = (result * 0x10000000058B90BFC) >> 64; } if (x & 0x40000000 > 0) { result = (result * 0x1000000002C5C85FE) >> 64; } if (x & 0x20000000 > 0) { result = (result * 0x100000000162E42FF) >> 64; } if (x & 0x10000000 > 0) { result = (result * 0x1000000000B17217F) >> 64; } if (x & 0x8000000 > 0) { result = (result * 0x100000000058B90C0) >> 64; } if (x & 0x4000000 > 0) { result = (result * 0x10000000002C5C860) >> 64; } if (x & 0x2000000 > 0) { result = (result * 0x1000000000162E430) >> 64; } if (x & 0x1000000 > 0) { result = (result * 0x10000000000B17218) >> 64; } if (x & 0x800000 > 0) { result = (result * 0x1000000000058B90C) >> 64; } if (x & 0x400000 > 0) { result = (result * 0x100000000002C5C86) >> 64; } if (x & 0x200000 > 0) { result = (result * 0x10000000000162E43) >> 64; } if (x & 0x100000 > 0) { result = (result * 0x100000000000B1721) >> 64; } if (x & 0x80000 > 0) { result = (result * 0x10000000000058B91) >> 64; } if (x & 0x40000 > 0) { result = (result * 0x1000000000002C5C8) >> 64; } if (x & 0x20000 > 0) { result = (result * 0x100000000000162E4) >> 64; } if (x & 0x10000 > 0) { result = (result * 0x1000000000000B172) >> 64; } if (x & 0x8000 > 0) { result = (result * 0x100000000000058B9) >> 64; } if (x & 0x4000 > 0) { result = (result * 0x10000000000002C5D) >> 64; } if (x & 0x2000 > 0) { result = (result * 0x1000000000000162E) >> 64; } if (x & 0x1000 > 0) { result = (result * 0x10000000000000B17) >> 64; } if (x & 0x800 > 0) { result = (result * 0x1000000000000058C) >> 64; } if (x & 0x400 > 0) { result = (result * 0x100000000000002C6) >> 64; } if (x & 0x200 > 0) { result = (result * 0x10000000000000163) >> 64; } if (x & 0x100 > 0) { result = (result * 0x100000000000000B1) >> 64; } if (x & 0x80 > 0) { result = (result * 0x10000000000000059) >> 64; } if (x & 0x40 > 0) { result = (result * 0x1000000000000002C) >> 64; } if (x & 0x20 > 0) { result = (result * 0x10000000000000016) >> 64; } if (x & 0x10 > 0) { result = (result * 0x1000000000000000B) >> 64; } if (x & 0x8 > 0) { result = (result * 0x10000000000000006) >> 64; } if (x & 0x4 > 0) { result = (result * 0x10000000000000003) >> 64; } if (x & 0x2 > 0) { result = (result * 0x10000000000000001) >> 64; } if (x & 0x1 > 0) { result = (result * 0x10000000000000001) >> 64; } // We're doing two things at the same time: // // 1. Multiply the result by 2^n + 1, where "2^n" is the integer part and the one is added to account for // the fact that we initially set the result to 0.5. This is accomplished by subtracting from 191 // rather than 192. // 2. Convert the result to the unsigned 60.18-decimal fixed-point format. // // This works because 2^(191-ip) = 2^ip / 2^191, where "ip" is the integer part "2^n". result *= SCALE; result >>= (191 - (x >> 64)); } } /// @notice Finds the zero-based index of the first one in the binary representation of x. /// @dev See the note on msb in the "Find First Set" Wikipedia article https://en.wikipedia.org/wiki/Find_first_set /// @param x The uint256 number for which to find the index of the most significant bit. /// @return msb The index of the most significant bit as an uint256. function mostSignificantBit(uint256 x) internal pure returns (uint256 msb) { if (x >= 2**128) { x >>= 128; msb += 128; } if (x >= 2**64) { x >>= 64; msb += 64; } if (x >= 2**32) { x >>= 32; msb += 32; } if (x >= 2**16) { x >>= 16; msb += 16; } if (x >= 2**8) { x >>= 8; msb += 8; } if (x >= 2**4) { x >>= 4; msb += 4; } if (x >= 2**2) { x >>= 2; msb += 2; } if (x >= 2**1) { // No need to shift x any more. msb += 1; } } /// @notice Calculates floor(x*y÷denominator) with full precision. /// /// @dev Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv. /// /// Requirements: /// - The denominator cannot be zero. /// - The result must fit within uint256. /// /// Caveats: /// - This function does not work with fixed-point numbers. /// /// @param x The multiplicand as an uint256. /// @param y The multiplier as an uint256. /// @param denominator The divisor as an uint256. /// @return result The result as an uint256. function mulDiv( uint256 x, uint256 y, uint256 denominator ) internal pure returns (uint256 result) { // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256 // variables such that product = prod1 * 2^256 + prod0. uint256 prod0; // Least significant 256 bits of the product uint256 prod1; // Most significant 256 bits of the product assembly { let mm := mulmod(x, y, not(0)) prod0 := mul(x, y) prod1 := sub(sub(mm, prod0), lt(mm, prod0)) } // Handle non-overflow cases, 256 by 256 division. if (prod1 == 0) { unchecked { result = prod0 / denominator; } return result; } // Make sure the result is less than 2^256. Also prevents denominator == 0. if (prod1 >= denominator) { revert PRBMath__MulDivOverflow(prod1, denominator); } /////////////////////////////////////////////// // 512 by 256 division. /////////////////////////////////////////////// // Make division exact by subtracting the remainder from [prod1 prod0]. uint256 remainder; assembly { // Compute remainder using mulmod. remainder := mulmod(x, y, denominator) // Subtract 256 bit number from 512 bit number. prod1 := sub(prod1, gt(remainder, prod0)) prod0 := sub(prod0, remainder) } // Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1. // See https://cs.stackexchange.com/q/138556/92363. unchecked { // Does not overflow because the denominator cannot be zero at this stage in the function. uint256 lpotdod = denominator & (~denominator + 1); assembly { // Divide denominator by lpotdod. denominator := div(denominator, lpotdod) // Divide [prod1 prod0] by lpotdod. prod0 := div(prod0, lpotdod) // Flip lpotdod such that it is 2^256 / lpotdod. If lpotdod is zero, then it becomes one. lpotdod := add(div(sub(0, lpotdod), lpotdod), 1) } // Shift in bits from prod1 into prod0. prod0 |= prod1 * lpotdod; // Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for // four bits. That is, denominator * inv = 1 mod 2^4. uint256 inverse = (3 * denominator) ^ 2; // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works // in modular arithmetic, doubling the correct bits in each step. inverse *= 2 - denominator * inverse; // inverse mod 2^8 inverse *= 2 - denominator * inverse; // inverse mod 2^16 inverse *= 2 - denominator * inverse; // inverse mod 2^32 inverse *= 2 - denominator * inverse; // inverse mod 2^64 inverse *= 2 - denominator * inverse; // inverse mod 2^128 inverse *= 2 - denominator * inverse; // inverse mod 2^256 // Because the division is now exact we can divide by multiplying with the modular inverse of denominator. // This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is // less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1 // is no longer required. result = prod0 * inverse; return result; } } /// @notice Calculates floor(x*y÷1e18) with full precision. /// /// @dev Variant of "mulDiv" with constant folding, i.e. in which the denominator is always 1e18. Before returning the /// final result, we add 1 if (x * y) % SCALE >= HALF_SCALE. Without this, 6.6e-19 would be truncated to 0 instead of /// being rounded to 1e-18. See "Listing 6" and text above it at https://accu.org/index.php/journals/1717. /// /// Requirements: /// - The result must fit within uint256. /// /// Caveats: /// - The body is purposely left uncommented; see the NatSpec comments in "PRBMath.mulDiv" to understand how this works. /// - It is assumed that the result can never be type(uint256).max when x and y solve the following two equations: /// 1. x * y = type(uint256).max * SCALE /// 2. (x * y) % SCALE >= SCALE / 2 /// /// @param x The multiplicand as an unsigned 60.18-decimal fixed-point number. /// @param y The multiplier as an unsigned 60.18-decimal fixed-point number. /// @return result The result as an unsigned 60.18-decimal fixed-point number. function mulDivFixedPoint(uint256 x, uint256 y) internal pure returns (uint256 result) { uint256 prod0; uint256 prod1; assembly { let mm := mulmod(x, y, not(0)) prod0 := mul(x, y) prod1 := sub(sub(mm, prod0), lt(mm, prod0)) } if (prod1 >= SCALE) { revert PRBMath__MulDivFixedPointOverflow(prod1); } uint256 remainder; uint256 roundUpUnit; assembly { remainder := mulmod(x, y, SCALE) roundUpUnit := gt(remainder, 499999999999999999) } if (prod1 == 0) { unchecked { result = (prod0 / SCALE) + roundUpUnit; return result; } } assembly { result := add( mul( or( div(sub(prod0, remainder), SCALE_LPOTD), mul(sub(prod1, gt(remainder, prod0)), add(div(sub(0, SCALE_LPOTD), SCALE_LPOTD), 1)) ), SCALE_INVERSE ), roundUpUnit ) } } /// @notice Calculates floor(x*y÷denominator) with full precision. /// /// @dev An extension of "mulDiv" for signed numbers. Works by computing the signs and the absolute values separately. /// /// Requirements: /// - None of the inputs can be type(int256).min. /// - The result must fit within int256. /// /// @param x The multiplicand as an int256. /// @param y The multiplier as an int256. /// @param denominator The divisor as an int256. /// @return result The result as an int256. function mulDivSigned( int256 x, int256 y, int256 denominator ) internal pure returns (int256 result) { if (x == type(int256).min || y == type(int256).min || denominator == type(int256).min) { revert PRBMath__MulDivSignedInputTooSmall(); } // Get hold of the absolute values of x, y and the denominator. uint256 ax; uint256 ay; uint256 ad; unchecked { ax = x < 0 ? uint256(-x) : uint256(x); ay = y < 0 ? uint256(-y) : uint256(y); ad = denominator < 0 ? uint256(-denominator) : uint256(denominator); } // Compute the absolute value of (x*y)÷denominator. The result must fit within int256. uint256 rAbs = mulDiv(ax, ay, ad); if (rAbs > uint256(type(int256).max)) { revert PRBMath__MulDivSignedOverflow(rAbs); } // Get the signs of x, y and the denominator. uint256 sx; uint256 sy; uint256 sd; assembly { sx := sgt(x, sub(0, 1)) sy := sgt(y, sub(0, 1)) sd := sgt(denominator, sub(0, 1)) } // XOR over sx, sy and sd. This is checking whether there are one or three negative signs in the inputs. // If yes, the result should be negative. result = sx ^ sy ^ sd == 0 ? -int256(rAbs) : int256(rAbs); } /// @notice Calculates the square root of x, rounding down. /// @dev Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method. /// /// Caveats: /// - This function does not work with fixed-point numbers. /// /// @param x The uint256 number for which to calculate the square root. /// @return result The result as an uint256. function sqrt(uint256 x) internal pure returns (uint256 result) { if (x == 0) { return 0; } // Set the initial guess to the least power of two that is greater than or equal to sqrt(x). uint256 xAux = uint256(x); result = 1; if (xAux >= 0x100000000000000000000000000000000) { xAux >>= 128; result <<= 64; } if (xAux >= 0x10000000000000000) { xAux >>= 64; result <<= 32; } if (xAux >= 0x100000000) { xAux >>= 32; result <<= 16; } if (xAux >= 0x10000) { xAux >>= 16; result <<= 8; } if (xAux >= 0x100) { xAux >>= 8; result <<= 4; } if (xAux >= 0x10) { xAux >>= 4; result <<= 2; } if (xAux >= 0x8) { result <<= 1; } // The operations can never overflow because the result is max 2^127 when it enters this block. unchecked { result = (result + x / result) >> 1; result = (result + x / result) >> 1; result = (result + x / result) >> 1; result = (result + x / result) >> 1; result = (result + x / result) >> 1; result = (result + x / result) >> 1; result = (result + x / result) >> 1; // Seven iterations should be enough uint256 roundedDownResult = x / result; return result >= roundedDownResult ? roundedDownResult : result; } } }
// SPDX-License-Identifier: Unlicense pragma solidity >=0.8.4; import "./PRBMath.sol"; /// @title PRBMathSD59x18 /// @author Paul Razvan Berg /// @notice Smart contract library for advanced fixed-point math that works with int256 numbers considered to have 18 /// trailing decimals. We call this number representation signed 59.18-decimal fixed-point, since the numbers can have /// a sign and there can be up to 59 digits in the integer part and up to 18 decimals in the fractional part. The numbers /// are bound by the minimum and the maximum values permitted by the Solidity type int256. library PRBMathSD59x18 { /// @dev log2(e) as a signed 59.18-decimal fixed-point number. int256 internal constant LOG2_E = 1_442695040888963407; /// @dev Half the SCALE number. int256 internal constant HALF_SCALE = 5e17; /// @dev The maximum value a signed 59.18-decimal fixed-point number can have. int256 internal constant MAX_SD59x18 = 57896044618658097711785492504343953926634992332820282019728_792003956564819967; /// @dev The maximum whole value a signed 59.18-decimal fixed-point number can have. int256 internal constant MAX_WHOLE_SD59x18 = 57896044618658097711785492504343953926634992332820282019728_000000000000000000; /// @dev The minimum value a signed 59.18-decimal fixed-point number can have. int256 internal constant MIN_SD59x18 = -57896044618658097711785492504343953926634992332820282019728_792003956564819968; /// @dev The minimum whole value a signed 59.18-decimal fixed-point number can have. int256 internal constant MIN_WHOLE_SD59x18 = -57896044618658097711785492504343953926634992332820282019728_000000000000000000; /// @dev How many trailing decimals can be represented. int256 internal constant SCALE = 1e18; /// INTERNAL FUNCTIONS /// /// @notice Calculate the absolute value of x. /// /// @dev Requirements: /// - x must be greater than MIN_SD59x18. /// /// @param x The number to calculate the absolute value for. /// @param result The absolute value of x. function abs(int256 x) internal pure returns (int256 result) { unchecked { if (x == MIN_SD59x18) { revert PRBMathSD59x18__AbsInputTooSmall(); } result = x < 0 ? -x : x; } } /// @notice Calculates the arithmetic average of x and y, rounding down. /// @param x The first operand as a signed 59.18-decimal fixed-point number. /// @param y The second operand as a signed 59.18-decimal fixed-point number. /// @return result The arithmetic average as a signed 59.18-decimal fixed-point number. function avg(int256 x, int256 y) internal pure returns (int256 result) { // The operations can never overflow. unchecked { int256 sum = (x >> 1) + (y >> 1); if (sum < 0) { // If at least one of x and y is odd, we add 1 to the result. This is because shifting negative numbers to the // right rounds down to infinity. assembly { result := add(sum, and(or(x, y), 1)) } } else { // If both x and y are odd, we add 1 to the result. This is because if both numbers are odd, the 0.5 // remainder gets truncated twice. result = sum + (x & y & 1); } } } /// @notice Yields the least greatest signed 59.18 decimal fixed-point number greater than or equal to x. /// /// @dev Optimized for fractional value inputs, because for every whole value there are (1e18 - 1) fractional counterparts. /// See https://en.wikipedia.org/wiki/Floor_and_ceiling_functions. /// /// Requirements: /// - x must be less than or equal to MAX_WHOLE_SD59x18. /// /// @param x The signed 59.18-decimal fixed-point number to ceil. /// @param result The least integer greater than or equal to x, as a signed 58.18-decimal fixed-point number. function ceil(int256 x) internal pure returns (int256 result) { if (x > MAX_WHOLE_SD59x18) { revert PRBMathSD59x18__CeilOverflow(x); } unchecked { int256 remainder = x % SCALE; if (remainder == 0) { result = x; } else { // Solidity uses C fmod style, which returns a modulus with the same sign as x. result = x - remainder; if (x > 0) { result += SCALE; } } } } /// @notice Divides two signed 59.18-decimal fixed-point numbers, returning a new signed 59.18-decimal fixed-point number. /// /// @dev Variant of "mulDiv" that works with signed numbers. Works by computing the signs and the absolute values separately. /// /// Requirements: /// - All from "PRBMath.mulDiv". /// - None of the inputs can be MIN_SD59x18. /// - The denominator cannot be zero. /// - The result must fit within int256. /// /// Caveats: /// - All from "PRBMath.mulDiv". /// /// @param x The numerator as a signed 59.18-decimal fixed-point number. /// @param y The denominator as a signed 59.18-decimal fixed-point number. /// @param result The quotient as a signed 59.18-decimal fixed-point number. function div(int256 x, int256 y) internal pure returns (int256 result) { if (x == MIN_SD59x18 || y == MIN_SD59x18) { revert PRBMathSD59x18__DivInputTooSmall(); } // Get hold of the absolute values of x and y. uint256 ax; uint256 ay; unchecked { ax = x < 0 ? uint256(-x) : uint256(x); ay = y < 0 ? uint256(-y) : uint256(y); } // Compute the absolute value of (x*SCALE)÷y. The result must fit within int256. uint256 rAbs = PRBMath.mulDiv(ax, uint256(SCALE), ay); if (rAbs > uint256(MAX_SD59x18)) { revert PRBMathSD59x18__DivOverflow(rAbs); } // Get the signs of x and y. uint256 sx; uint256 sy; assembly { sx := sgt(x, sub(0, 1)) sy := sgt(y, sub(0, 1)) } // XOR over sx and sy. This is basically checking whether the inputs have the same sign. If yes, the result // should be positive. Otherwise, it should be negative. result = sx ^ sy == 1 ? -int256(rAbs) : int256(rAbs); } /// @notice Returns Euler's number as a signed 59.18-decimal fixed-point number. /// @dev See https://en.wikipedia.org/wiki/E_(mathematical_constant). function e() internal pure returns (int256 result) { result = 2_718281828459045235; } /// @notice Calculates the natural exponent of x. /// /// @dev Based on the insight that e^x = 2^(x * log2(e)). /// /// Requirements: /// - All from "log2". /// - x must be less than 133.084258667509499441. /// /// Caveats: /// - All from "exp2". /// - For any x less than -41.446531673892822322, the result is zero. /// /// @param x The exponent as a signed 59.18-decimal fixed-point number. /// @return result The result as a signed 59.18-decimal fixed-point number. function exp(int256 x) internal pure returns (int256 result) { // Without this check, the value passed to "exp2" would be less than -59.794705707972522261. if (x < -41_446531673892822322) { return 0; } // Without this check, the value passed to "exp2" would be greater than 192. if (x >= 133_084258667509499441) { revert PRBMathSD59x18__ExpInputTooBig(x); } // Do the fixed-point multiplication inline to save gas. unchecked { int256 doubleScaleProduct = x * LOG2_E; result = exp2((doubleScaleProduct + HALF_SCALE) / SCALE); } } /// @notice Calculates the binary exponent of x using the binary fraction method. /// /// @dev See https://ethereum.stackexchange.com/q/79903/24693. /// /// Requirements: /// - x must be 192 or less. /// - The result must fit within MAX_SD59x18. /// /// Caveats: /// - For any x less than -59.794705707972522261, the result is zero. /// /// @param x The exponent as a signed 59.18-decimal fixed-point number. /// @return result The result as a signed 59.18-decimal fixed-point number. function exp2(int256 x) internal pure returns (int256 result) { // This works because 2^(-x) = 1/2^x. if (x < 0) { // 2^59.794705707972522262 is the maximum number whose inverse does not truncate down to zero. if (x < -59_794705707972522261) { return 0; } // Do the fixed-point inversion inline to save gas. The numerator is SCALE * SCALE. unchecked { result = 1e36 / exp2(-x); } } else { // 2^192 doesn't fit within the 192.64-bit format used internally in this function. if (x >= 192e18) { revert PRBMathSD59x18__Exp2InputTooBig(x); } unchecked { // Convert x to the 192.64-bit fixed-point format. uint256 x192x64 = (uint256(x) << 64) / uint256(SCALE); // Safe to convert the result to int256 directly because the maximum input allowed is 192. result = int256(PRBMath.exp2(x192x64)); } } } /// @notice Yields the greatest signed 59.18 decimal fixed-point number less than or equal to x. /// /// @dev Optimized for fractional value inputs, because for every whole value there are (1e18 - 1) fractional counterparts. /// See https://en.wikipedia.org/wiki/Floor_and_ceiling_functions. /// /// Requirements: /// - x must be greater than or equal to MIN_WHOLE_SD59x18. /// /// @param x The signed 59.18-decimal fixed-point number to floor. /// @param result The greatest integer less than or equal to x, as a signed 58.18-decimal fixed-point number. function floor(int256 x) internal pure returns (int256 result) { if (x < MIN_WHOLE_SD59x18) { revert PRBMathSD59x18__FloorUnderflow(x); } unchecked { int256 remainder = x % SCALE; if (remainder == 0) { result = x; } else { // Solidity uses C fmod style, which returns a modulus with the same sign as x. result = x - remainder; if (x < 0) { result -= SCALE; } } } } /// @notice Yields the excess beyond the floor of x for positive numbers and the part of the number to the right /// of the radix point for negative numbers. /// @dev Based on the odd function definition. https://en.wikipedia.org/wiki/Fractional_part /// @param x The signed 59.18-decimal fixed-point number to get the fractional part of. /// @param result The fractional part of x as a signed 59.18-decimal fixed-point number. function frac(int256 x) internal pure returns (int256 result) { unchecked { result = x % SCALE; } } /// @notice Converts a number from basic integer form to signed 59.18-decimal fixed-point representation. /// /// @dev Requirements: /// - x must be greater than or equal to MIN_SD59x18 divided by SCALE. /// - x must be less than or equal to MAX_SD59x18 divided by SCALE. /// /// @param x The basic integer to convert. /// @param result The same number in signed 59.18-decimal fixed-point representation. function fromInt(int256 x) internal pure returns (int256 result) { unchecked { if (x < MIN_SD59x18 / SCALE) { revert PRBMathSD59x18__FromIntUnderflow(x); } if (x > MAX_SD59x18 / SCALE) { revert PRBMathSD59x18__FromIntOverflow(x); } result = x * SCALE; } } /// @notice Calculates geometric mean of x and y, i.e. sqrt(x * y), rounding down. /// /// @dev Requirements: /// - x * y must fit within MAX_SD59x18, lest it overflows. /// - x * y cannot be negative. /// /// @param x The first operand as a signed 59.18-decimal fixed-point number. /// @param y The second operand as a signed 59.18-decimal fixed-point number. /// @return result The result as a signed 59.18-decimal fixed-point number. function gm(int256 x, int256 y) internal pure returns (int256 result) { if (x == 0) { return 0; } unchecked { // Checking for overflow this way is faster than letting Solidity do it. int256 xy = x * y; if (xy / x != y) { revert PRBMathSD59x18__GmOverflow(x, y); } // The product cannot be negative. if (xy < 0) { revert PRBMathSD59x18__GmNegativeProduct(x, y); } // We don't need to multiply by the SCALE here because the x*y product had already picked up a factor of SCALE // during multiplication. See the comments within the "sqrt" function. result = int256(PRBMath.sqrt(uint256(xy))); } } /// @notice Calculates 1 / x, rounding toward zero. /// /// @dev Requirements: /// - x cannot be zero. /// /// @param x The signed 59.18-decimal fixed-point number for which to calculate the inverse. /// @return result The inverse as a signed 59.18-decimal fixed-point number. function inv(int256 x) internal pure returns (int256 result) { unchecked { // 1e36 is SCALE * SCALE. result = 1e36 / x; } } /// @notice Calculates the natural logarithm of x. /// /// @dev Based on the insight that ln(x) = log2(x) / log2(e). /// /// Requirements: /// - All from "log2". /// /// Caveats: /// - All from "log2". /// - This doesn't return exactly 1 for 2718281828459045235, for that we would need more fine-grained precision. /// /// @param x The signed 59.18-decimal fixed-point number for which to calculate the natural logarithm. /// @return result The natural logarithm as a signed 59.18-decimal fixed-point number. function ln(int256 x) internal pure returns (int256 result) { // Do the fixed-point multiplication inline to save gas. This is overflow-safe because the maximum value that log2(x) // can return is 195205294292027477728. unchecked { result = (log2(x) * SCALE) / LOG2_E; } } /// @notice Calculates the common logarithm of x. /// /// @dev First checks if x is an exact power of ten and it stops if yes. If it's not, calculates the common /// logarithm based on the insight that log10(x) = log2(x) / log2(10). /// /// Requirements: /// - All from "log2". /// /// Caveats: /// - All from "log2". /// /// @param x The signed 59.18-decimal fixed-point number for which to calculate the common logarithm. /// @return result The common logarithm as a signed 59.18-decimal fixed-point number. function log10(int256 x) internal pure returns (int256 result) { if (x <= 0) { revert PRBMathSD59x18__LogInputTooSmall(x); } // Note that the "mul" in this block is the assembly mul operation, not the "mul" function defined in this contract. // prettier-ignore assembly { switch x case 1 { result := mul(SCALE, sub(0, 18)) } case 10 { result := mul(SCALE, sub(1, 18)) } case 100 { result := mul(SCALE, sub(2, 18)) } case 1000 { result := mul(SCALE, sub(3, 18)) } case 10000 { result := mul(SCALE, sub(4, 18)) } case 100000 { result := mul(SCALE, sub(5, 18)) } case 1000000 { result := mul(SCALE, sub(6, 18)) } case 10000000 { result := mul(SCALE, sub(7, 18)) } case 100000000 { result := mul(SCALE, sub(8, 18)) } case 1000000000 { result := mul(SCALE, sub(9, 18)) } case 10000000000 { result := mul(SCALE, sub(10, 18)) } case 100000000000 { result := mul(SCALE, sub(11, 18)) } case 1000000000000 { result := mul(SCALE, sub(12, 18)) } case 10000000000000 { result := mul(SCALE, sub(13, 18)) } case 100000000000000 { result := mul(SCALE, sub(14, 18)) } case 1000000000000000 { result := mul(SCALE, sub(15, 18)) } case 10000000000000000 { result := mul(SCALE, sub(16, 18)) } case 100000000000000000 { result := mul(SCALE, sub(17, 18)) } case 1000000000000000000 { result := 0 } case 10000000000000000000 { result := SCALE } case 100000000000000000000 { result := mul(SCALE, 2) } case 1000000000000000000000 { result := mul(SCALE, 3) } case 10000000000000000000000 { result := mul(SCALE, 4) } case 100000000000000000000000 { result := mul(SCALE, 5) } case 1000000000000000000000000 { result := mul(SCALE, 6) } case 10000000000000000000000000 { result := mul(SCALE, 7) } case 100000000000000000000000000 { result := mul(SCALE, 8) } case 1000000000000000000000000000 { result := mul(SCALE, 9) } case 10000000000000000000000000000 { result := mul(SCALE, 10) } case 100000000000000000000000000000 { result := mul(SCALE, 11) } case 1000000000000000000000000000000 { result := mul(SCALE, 12) } case 10000000000000000000000000000000 { result := mul(SCALE, 13) } case 100000000000000000000000000000000 { result := mul(SCALE, 14) } case 1000000000000000000000000000000000 { result := mul(SCALE, 15) } case 10000000000000000000000000000000000 { result := mul(SCALE, 16) } case 100000000000000000000000000000000000 { result := mul(SCALE, 17) } case 1000000000000000000000000000000000000 { result := mul(SCALE, 18) } case 10000000000000000000000000000000000000 { result := mul(SCALE, 19) } case 100000000000000000000000000000000000000 { result := mul(SCALE, 20) } case 1000000000000000000000000000000000000000 { result := mul(SCALE, 21) } case 10000000000000000000000000000000000000000 { result := mul(SCALE, 22) } case 100000000000000000000000000000000000000000 { result := mul(SCALE, 23) } case 1000000000000000000000000000000000000000000 { result := mul(SCALE, 24) } case 10000000000000000000000000000000000000000000 { result := mul(SCALE, 25) } case 100000000000000000000000000000000000000000000 { result := mul(SCALE, 26) } case 1000000000000000000000000000000000000000000000 { result := mul(SCALE, 27) } case 10000000000000000000000000000000000000000000000 { result := mul(SCALE, 28) } case 100000000000000000000000000000000000000000000000 { result := mul(SCALE, 29) } case 1000000000000000000000000000000000000000000000000 { result := mul(SCALE, 30) } case 10000000000000000000000000000000000000000000000000 { result := mul(SCALE, 31) } case 100000000000000000000000000000000000000000000000000 { result := mul(SCALE, 32) } case 1000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 33) } case 10000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 34) } case 100000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 35) } case 1000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 36) } case 10000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 37) } case 100000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 38) } case 1000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 39) } case 10000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 40) } case 100000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 41) } case 1000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 42) } case 10000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 43) } case 100000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 44) } case 1000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 45) } case 10000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 46) } case 100000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 47) } case 1000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 48) } case 10000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 49) } case 100000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 50) } case 1000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 51) } case 10000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 52) } case 100000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 53) } case 1000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 54) } case 10000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 55) } case 100000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 56) } case 1000000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 57) } case 10000000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 58) } default { result := MAX_SD59x18 } } if (result == MAX_SD59x18) { // Do the fixed-point division inline to save gas. The denominator is log2(10). unchecked { result = (log2(x) * SCALE) / 3_321928094887362347; } } } /// @notice Calculates the binary logarithm of x. /// /// @dev Based on the iterative approximation algorithm. /// https://en.wikipedia.org/wiki/Binary_logarithm#Iterative_approximation /// /// Requirements: /// - x must be greater than zero. /// /// Caveats: /// - The results are not perfectly accurate to the last decimal, due to the lossy precision of the iterative approximation. /// /// @param x The signed 59.18-decimal fixed-point number for which to calculate the binary logarithm. /// @return result The binary logarithm as a signed 59.18-decimal fixed-point number. function log2(int256 x) internal pure returns (int256 result) { if (x <= 0) { revert PRBMathSD59x18__LogInputTooSmall(x); } unchecked { // This works because log2(x) = -log2(1/x). int256 sign; if (x >= SCALE) { sign = 1; } else { sign = -1; // Do the fixed-point inversion inline to save gas. The numerator is SCALE * SCALE. assembly { x := div(1000000000000000000000000000000000000, x) } } // Calculate the integer part of the logarithm and add it to the result and finally calculate y = x * 2^(-n). uint256 n = PRBMath.mostSignificantBit(uint256(x / SCALE)); // The integer part of the logarithm as a signed 59.18-decimal fixed-point number. The operation can't overflow // because n is maximum 255, SCALE is 1e18 and sign is either 1 or -1. result = int256(n) * SCALE; // This is y = x * 2^(-n). int256 y = x >> n; // If y = 1, the fractional part is zero. if (y == SCALE) { return result * sign; } // Calculate the fractional part via the iterative approximation. // The "delta >>= 1" part is equivalent to "delta /= 2", but shifting bits is faster. for (int256 delta = int256(HALF_SCALE); delta > 0; delta >>= 1) { y = (y * y) / SCALE; // Is y^2 > 2 and so in the range [2,4)? if (y >= 2 * SCALE) { // Add the 2^(-m) factor to the logarithm. result += delta; // Corresponds to z/2 on Wikipedia. y >>= 1; } } result *= sign; } } /// @notice Multiplies two signed 59.18-decimal fixed-point numbers together, returning a new signed 59.18-decimal /// fixed-point number. /// /// @dev Variant of "mulDiv" that works with signed numbers and employs constant folding, i.e. the denominator is /// always 1e18. /// /// Requirements: /// - All from "PRBMath.mulDivFixedPoint". /// - None of the inputs can be MIN_SD59x18 /// - The result must fit within MAX_SD59x18. /// /// Caveats: /// - The body is purposely left uncommented; see the NatSpec comments in "PRBMath.mulDiv" to understand how this works. /// /// @param x The multiplicand as a signed 59.18-decimal fixed-point number. /// @param y The multiplier as a signed 59.18-decimal fixed-point number. /// @return result The product as a signed 59.18-decimal fixed-point number. function mul(int256 x, int256 y) internal pure returns (int256 result) { if (x == MIN_SD59x18 || y == MIN_SD59x18) { revert PRBMathSD59x18__MulInputTooSmall(); } unchecked { uint256 ax; uint256 ay; ax = x < 0 ? uint256(-x) : uint256(x); ay = y < 0 ? uint256(-y) : uint256(y); uint256 rAbs = PRBMath.mulDivFixedPoint(ax, ay); if (rAbs > uint256(MAX_SD59x18)) { revert PRBMathSD59x18__MulOverflow(rAbs); } uint256 sx; uint256 sy; assembly { sx := sgt(x, sub(0, 1)) sy := sgt(y, sub(0, 1)) } result = sx ^ sy == 1 ? -int256(rAbs) : int256(rAbs); } } /// @notice Returns PI as a signed 59.18-decimal fixed-point number. function pi() internal pure returns (int256 result) { result = 3_141592653589793238; } /// @notice Raises x to the power of y. /// /// @dev Based on the insight that x^y = 2^(log2(x) * y). /// /// Requirements: /// - All from "exp2", "log2" and "mul". /// - z cannot be zero. /// /// Caveats: /// - All from "exp2", "log2" and "mul". /// - Assumes 0^0 is 1. /// /// @param x Number to raise to given power y, as a signed 59.18-decimal fixed-point number. /// @param y Exponent to raise x to, as a signed 59.18-decimal fixed-point number. /// @return result x raised to power y, as a signed 59.18-decimal fixed-point number. function pow(int256 x, int256 y) internal pure returns (int256 result) { if (x == 0) { result = y == 0 ? SCALE : int256(0); } else { result = exp2(mul(log2(x), y)); } } /// @notice Raises x (signed 59.18-decimal fixed-point number) to the power of y (basic unsigned integer) using the /// famous algorithm "exponentiation by squaring". /// /// @dev See https://en.wikipedia.org/wiki/Exponentiation_by_squaring /// /// Requirements: /// - All from "abs" and "PRBMath.mulDivFixedPoint". /// - The result must fit within MAX_SD59x18. /// /// Caveats: /// - All from "PRBMath.mulDivFixedPoint". /// - Assumes 0^0 is 1. /// /// @param x The base as a signed 59.18-decimal fixed-point number. /// @param y The exponent as an uint256. /// @return result The result as a signed 59.18-decimal fixed-point number. function powu(int256 x, uint256 y) internal pure returns (int256 result) { uint256 xAbs = uint256(abs(x)); // Calculate the first iteration of the loop in advance. uint256 rAbs = y & 1 > 0 ? xAbs : uint256(SCALE); // Equivalent to "for(y /= 2; y > 0; y /= 2)" but faster. uint256 yAux = y; for (yAux >>= 1; yAux > 0; yAux >>= 1) { xAbs = PRBMath.mulDivFixedPoint(xAbs, xAbs); // Equivalent to "y % 2 == 1" but faster. if (yAux & 1 > 0) { rAbs = PRBMath.mulDivFixedPoint(rAbs, xAbs); } } // The result must fit within the 59.18-decimal fixed-point representation. if (rAbs > uint256(MAX_SD59x18)) { revert PRBMathSD59x18__PowuOverflow(rAbs); } // Is the base negative and the exponent an odd number? bool isNegative = x < 0 && y & 1 == 1; result = isNegative ? -int256(rAbs) : int256(rAbs); } /// @notice Returns 1 as a signed 59.18-decimal fixed-point number. function scale() internal pure returns (int256 result) { result = SCALE; } /// @notice Calculates the square root of x, rounding down. /// @dev Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method. /// /// Requirements: /// - x cannot be negative. /// - x must be less than MAX_SD59x18 / SCALE. /// /// @param x The signed 59.18-decimal fixed-point number for which to calculate the square root. /// @return result The result as a signed 59.18-decimal fixed-point . function sqrt(int256 x) internal pure returns (int256 result) { unchecked { if (x < 0) { revert PRBMathSD59x18__SqrtNegativeInput(x); } if (x > MAX_SD59x18 / SCALE) { revert PRBMathSD59x18__SqrtOverflow(x); } // Multiply x by the SCALE to account for the factor of SCALE that is picked up when multiplying two signed // 59.18-decimal fixed-point numbers together (in this case, those two numbers are both the square root). result = int256(PRBMath.sqrt(uint256(x * SCALE))); } } /// @notice Converts a signed 59.18-decimal fixed-point number to basic integer form, rounding down in the process. /// @param x The signed 59.18-decimal fixed-point number to convert. /// @return result The same number in basic integer form. function toInt(int256 x) internal pure returns (int256 result) { unchecked { result = x / SCALE; } } }
// SPDX-License-Identifier: Unlicense pragma solidity >=0.8.4; import "./PRBMath.sol"; /// @title PRBMathUD60x18 /// @author Paul Razvan Berg /// @notice Smart contract library for advanced fixed-point math that works with uint256 numbers considered to have 18 /// trailing decimals. We call this number representation unsigned 60.18-decimal fixed-point, since there can be up to 60 /// digits in the integer part and up to 18 decimals in the fractional part. The numbers are bound by the minimum and the /// maximum values permitted by the Solidity type uint256. library PRBMathUD60x18 { /// @dev Half the SCALE number. uint256 internal constant HALF_SCALE = 5e17; /// @dev log2(e) as an unsigned 60.18-decimal fixed-point number. uint256 internal constant LOG2_E = 1_442695040888963407; /// @dev The maximum value an unsigned 60.18-decimal fixed-point number can have. uint256 internal constant MAX_UD60x18 = 115792089237316195423570985008687907853269984665640564039457_584007913129639935; /// @dev The maximum whole value an unsigned 60.18-decimal fixed-point number can have. uint256 internal constant MAX_WHOLE_UD60x18 = 115792089237316195423570985008687907853269984665640564039457_000000000000000000; /// @dev How many trailing decimals can be represented. uint256 internal constant SCALE = 1e18; /// @notice Calculates the arithmetic average of x and y, rounding down. /// @param x The first operand as an unsigned 60.18-decimal fixed-point number. /// @param y The second operand as an unsigned 60.18-decimal fixed-point number. /// @return result The arithmetic average as an unsigned 60.18-decimal fixed-point number. function avg(uint256 x, uint256 y) internal pure returns (uint256 result) { // The operations can never overflow. unchecked { // The last operand checks if both x and y are odd and if that is the case, we add 1 to the result. We need // to do this because if both numbers are odd, the 0.5 remainder gets truncated twice. result = (x >> 1) + (y >> 1) + (x & y & 1); } } /// @notice Yields the least unsigned 60.18 decimal fixed-point number greater than or equal to x. /// /// @dev Optimized for fractional value inputs, because for every whole value there are (1e18 - 1) fractional counterparts. /// See https://en.wikipedia.org/wiki/Floor_and_ceiling_functions. /// /// Requirements: /// - x must be less than or equal to MAX_WHOLE_UD60x18. /// /// @param x The unsigned 60.18-decimal fixed-point number to ceil. /// @param result The least integer greater than or equal to x, as an unsigned 60.18-decimal fixed-point number. function ceil(uint256 x) internal pure returns (uint256 result) { if (x > MAX_WHOLE_UD60x18) { revert PRBMathUD60x18__CeilOverflow(x); } assembly { // Equivalent to "x % SCALE" but faster. let remainder := mod(x, SCALE) // Equivalent to "SCALE - remainder" but faster. let delta := sub(SCALE, remainder) // Equivalent to "x + delta * (remainder > 0 ? 1 : 0)" but faster. result := add(x, mul(delta, gt(remainder, 0))) } } /// @notice Divides two unsigned 60.18-decimal fixed-point numbers, returning a new unsigned 60.18-decimal fixed-point number. /// /// @dev Uses mulDiv to enable overflow-safe multiplication and division. /// /// Requirements: /// - The denominator cannot be zero. /// /// @param x The numerator as an unsigned 60.18-decimal fixed-point number. /// @param y The denominator as an unsigned 60.18-decimal fixed-point number. /// @param result The quotient as an unsigned 60.18-decimal fixed-point number. function div(uint256 x, uint256 y) internal pure returns (uint256 result) { result = PRBMath.mulDiv(x, SCALE, y); } /// @notice Returns Euler's number as an unsigned 60.18-decimal fixed-point number. /// @dev See https://en.wikipedia.org/wiki/E_(mathematical_constant). function e() internal pure returns (uint256 result) { result = 2_718281828459045235; } /// @notice Calculates the natural exponent of x. /// /// @dev Based on the insight that e^x = 2^(x * log2(e)). /// /// Requirements: /// - All from "log2". /// - x must be less than 133.084258667509499441. /// /// @param x The exponent as an unsigned 60.18-decimal fixed-point number. /// @return result The result as an unsigned 60.18-decimal fixed-point number. function exp(uint256 x) internal pure returns (uint256 result) { // Without this check, the value passed to "exp2" would be greater than 192. if (x >= 133_084258667509499441) { revert PRBMathUD60x18__ExpInputTooBig(x); } // Do the fixed-point multiplication inline to save gas. unchecked { uint256 doubleScaleProduct = x * LOG2_E; result = exp2((doubleScaleProduct + HALF_SCALE) / SCALE); } } /// @notice Calculates the binary exponent of x using the binary fraction method. /// /// @dev See https://ethereum.stackexchange.com/q/79903/24693. /// /// Requirements: /// - x must be 192 or less. /// - The result must fit within MAX_UD60x18. /// /// @param x The exponent as an unsigned 60.18-decimal fixed-point number. /// @return result The result as an unsigned 60.18-decimal fixed-point number. function exp2(uint256 x) internal pure returns (uint256 result) { // 2^192 doesn't fit within the 192.64-bit format used internally in this function. if (x >= 192e18) { revert PRBMathUD60x18__Exp2InputTooBig(x); } unchecked { // Convert x to the 192.64-bit fixed-point format. uint256 x192x64 = (x << 64) / SCALE; // Pass x to the PRBMath.exp2 function, which uses the 192.64-bit fixed-point number representation. result = PRBMath.exp2(x192x64); } } /// @notice Yields the greatest unsigned 60.18 decimal fixed-point number less than or equal to x. /// @dev Optimized for fractional value inputs, because for every whole value there are (1e18 - 1) fractional counterparts. /// See https://en.wikipedia.org/wiki/Floor_and_ceiling_functions. /// @param x The unsigned 60.18-decimal fixed-point number to floor. /// @param result The greatest integer less than or equal to x, as an unsigned 60.18-decimal fixed-point number. function floor(uint256 x) internal pure returns (uint256 result) { assembly { // Equivalent to "x % SCALE" but faster. let remainder := mod(x, SCALE) // Equivalent to "x - remainder * (remainder > 0 ? 1 : 0)" but faster. result := sub(x, mul(remainder, gt(remainder, 0))) } } /// @notice Yields the excess beyond the floor of x. /// @dev Based on the odd function definition https://en.wikipedia.org/wiki/Fractional_part. /// @param x The unsigned 60.18-decimal fixed-point number to get the fractional part of. /// @param result The fractional part of x as an unsigned 60.18-decimal fixed-point number. function frac(uint256 x) internal pure returns (uint256 result) { assembly { result := mod(x, SCALE) } } /// @notice Converts a number from basic integer form to unsigned 60.18-decimal fixed-point representation. /// /// @dev Requirements: /// - x must be less than or equal to MAX_UD60x18 divided by SCALE. /// /// @param x The basic integer to convert. /// @param result The same number in unsigned 60.18-decimal fixed-point representation. function fromUint(uint256 x) internal pure returns (uint256 result) { unchecked { if (x > MAX_UD60x18 / SCALE) { revert PRBMathUD60x18__FromUintOverflow(x); } result = x * SCALE; } } /// @notice Calculates geometric mean of x and y, i.e. sqrt(x * y), rounding down. /// /// @dev Requirements: /// - x * y must fit within MAX_UD60x18, lest it overflows. /// /// @param x The first operand as an unsigned 60.18-decimal fixed-point number. /// @param y The second operand as an unsigned 60.18-decimal fixed-point number. /// @return result The result as an unsigned 60.18-decimal fixed-point number. function gm(uint256 x, uint256 y) internal pure returns (uint256 result) { if (x == 0) { return 0; } unchecked { // Checking for overflow this way is faster than letting Solidity do it. uint256 xy = x * y; if (xy / x != y) { revert PRBMathUD60x18__GmOverflow(x, y); } // We don't need to multiply by the SCALE here because the x*y product had already picked up a factor of SCALE // during multiplication. See the comments within the "sqrt" function. result = PRBMath.sqrt(xy); } } /// @notice Calculates 1 / x, rounding toward zero. /// /// @dev Requirements: /// - x cannot be zero. /// /// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the inverse. /// @return result The inverse as an unsigned 60.18-decimal fixed-point number. function inv(uint256 x) internal pure returns (uint256 result) { unchecked { // 1e36 is SCALE * SCALE. result = 1e36 / x; } } /// @notice Calculates the natural logarithm of x. /// /// @dev Based on the insight that ln(x) = log2(x) / log2(e). /// /// Requirements: /// - All from "log2". /// /// Caveats: /// - All from "log2". /// - This doesn't return exactly 1 for 2.718281828459045235, for that we would need more fine-grained precision. /// /// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the natural logarithm. /// @return result The natural logarithm as an unsigned 60.18-decimal fixed-point number. function ln(uint256 x) internal pure returns (uint256 result) { // Do the fixed-point multiplication inline to save gas. This is overflow-safe because the maximum value that log2(x) // can return is 196205294292027477728. unchecked { result = (log2(x) * SCALE) / LOG2_E; } } /// @notice Calculates the common logarithm of x. /// /// @dev First checks if x is an exact power of ten and it stops if yes. If it's not, calculates the common /// logarithm based on the insight that log10(x) = log2(x) / log2(10). /// /// Requirements: /// - All from "log2". /// /// Caveats: /// - All from "log2". /// /// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the common logarithm. /// @return result The common logarithm as an unsigned 60.18-decimal fixed-point number. function log10(uint256 x) internal pure returns (uint256 result) { if (x < SCALE) { revert PRBMathUD60x18__LogInputTooSmall(x); } // Note that the "mul" in this block is the assembly multiplication operation, not the "mul" function defined // in this contract. // prettier-ignore assembly { switch x case 1 { result := mul(SCALE, sub(0, 18)) } case 10 { result := mul(SCALE, sub(1, 18)) } case 100 { result := mul(SCALE, sub(2, 18)) } case 1000 { result := mul(SCALE, sub(3, 18)) } case 10000 { result := mul(SCALE, sub(4, 18)) } case 100000 { result := mul(SCALE, sub(5, 18)) } case 1000000 { result := mul(SCALE, sub(6, 18)) } case 10000000 { result := mul(SCALE, sub(7, 18)) } case 100000000 { result := mul(SCALE, sub(8, 18)) } case 1000000000 { result := mul(SCALE, sub(9, 18)) } case 10000000000 { result := mul(SCALE, sub(10, 18)) } case 100000000000 { result := mul(SCALE, sub(11, 18)) } case 1000000000000 { result := mul(SCALE, sub(12, 18)) } case 10000000000000 { result := mul(SCALE, sub(13, 18)) } case 100000000000000 { result := mul(SCALE, sub(14, 18)) } case 1000000000000000 { result := mul(SCALE, sub(15, 18)) } case 10000000000000000 { result := mul(SCALE, sub(16, 18)) } case 100000000000000000 { result := mul(SCALE, sub(17, 18)) } case 1000000000000000000 { result := 0 } case 10000000000000000000 { result := SCALE } case 100000000000000000000 { result := mul(SCALE, 2) } case 1000000000000000000000 { result := mul(SCALE, 3) } case 10000000000000000000000 { result := mul(SCALE, 4) } case 100000000000000000000000 { result := mul(SCALE, 5) } case 1000000000000000000000000 { result := mul(SCALE, 6) } case 10000000000000000000000000 { result := mul(SCALE, 7) } case 100000000000000000000000000 { result := mul(SCALE, 8) } case 1000000000000000000000000000 { result := mul(SCALE, 9) } case 10000000000000000000000000000 { result := mul(SCALE, 10) } case 100000000000000000000000000000 { result := mul(SCALE, 11) } case 1000000000000000000000000000000 { result := mul(SCALE, 12) } case 10000000000000000000000000000000 { result := mul(SCALE, 13) } case 100000000000000000000000000000000 { result := mul(SCALE, 14) } case 1000000000000000000000000000000000 { result := mul(SCALE, 15) } case 10000000000000000000000000000000000 { result := mul(SCALE, 16) } case 100000000000000000000000000000000000 { result := mul(SCALE, 17) } case 1000000000000000000000000000000000000 { result := mul(SCALE, 18) } case 10000000000000000000000000000000000000 { result := mul(SCALE, 19) } case 100000000000000000000000000000000000000 { result := mul(SCALE, 20) } case 1000000000000000000000000000000000000000 { result := mul(SCALE, 21) } case 10000000000000000000000000000000000000000 { result := mul(SCALE, 22) } case 100000000000000000000000000000000000000000 { result := mul(SCALE, 23) } case 1000000000000000000000000000000000000000000 { result := mul(SCALE, 24) } case 10000000000000000000000000000000000000000000 { result := mul(SCALE, 25) } case 100000000000000000000000000000000000000000000 { result := mul(SCALE, 26) } case 1000000000000000000000000000000000000000000000 { result := mul(SCALE, 27) } case 10000000000000000000000000000000000000000000000 { result := mul(SCALE, 28) } case 100000000000000000000000000000000000000000000000 { result := mul(SCALE, 29) } case 1000000000000000000000000000000000000000000000000 { result := mul(SCALE, 30) } case 10000000000000000000000000000000000000000000000000 { result := mul(SCALE, 31) } case 100000000000000000000000000000000000000000000000000 { result := mul(SCALE, 32) } case 1000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 33) } case 10000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 34) } case 100000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 35) } case 1000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 36) } case 10000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 37) } case 100000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 38) } case 1000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 39) } case 10000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 40) } case 100000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 41) } case 1000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 42) } case 10000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 43) } case 100000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 44) } case 1000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 45) } case 10000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 46) } case 100000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 47) } case 1000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 48) } case 10000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 49) } case 100000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 50) } case 1000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 51) } case 10000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 52) } case 100000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 53) } case 1000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 54) } case 10000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 55) } case 100000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 56) } case 1000000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 57) } case 10000000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 58) } case 100000000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 59) } default { result := MAX_UD60x18 } } if (result == MAX_UD60x18) { // Do the fixed-point division inline to save gas. The denominator is log2(10). unchecked { result = (log2(x) * SCALE) / 3_321928094887362347; } } } /// @notice Calculates the binary logarithm of x. /// /// @dev Based on the iterative approximation algorithm. /// https://en.wikipedia.org/wiki/Binary_logarithm#Iterative_approximation /// /// Requirements: /// - x must be greater than or equal to SCALE, otherwise the result would be negative. /// /// Caveats: /// - The results are nor perfectly accurate to the last decimal, due to the lossy precision of the iterative approximation. /// /// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the binary logarithm. /// @return result The binary logarithm as an unsigned 60.18-decimal fixed-point number. function log2(uint256 x) internal pure returns (uint256 result) { if (x < SCALE) { revert PRBMathUD60x18__LogInputTooSmall(x); } unchecked { // Calculate the integer part of the logarithm and add it to the result and finally calculate y = x * 2^(-n). uint256 n = PRBMath.mostSignificantBit(x / SCALE); // The integer part of the logarithm as an unsigned 60.18-decimal fixed-point number. The operation can't overflow // because n is maximum 255 and SCALE is 1e18. result = n * SCALE; // This is y = x * 2^(-n). uint256 y = x >> n; // If y = 1, the fractional part is zero. if (y == SCALE) { return result; } // Calculate the fractional part via the iterative approximation. // The "delta >>= 1" part is equivalent to "delta /= 2", but shifting bits is faster. for (uint256 delta = HALF_SCALE; delta > 0; delta >>= 1) { y = (y * y) / SCALE; // Is y^2 > 2 and so in the range [2,4)? if (y >= 2 * SCALE) { // Add the 2^(-m) factor to the logarithm. result += delta; // Corresponds to z/2 on Wikipedia. y >>= 1; } } } } /// @notice Multiplies two unsigned 60.18-decimal fixed-point numbers together, returning a new unsigned 60.18-decimal /// fixed-point number. /// @dev See the documentation for the "PRBMath.mulDivFixedPoint" function. /// @param x The multiplicand as an unsigned 60.18-decimal fixed-point number. /// @param y The multiplier as an unsigned 60.18-decimal fixed-point number. /// @return result The product as an unsigned 60.18-decimal fixed-point number. function mul(uint256 x, uint256 y) internal pure returns (uint256 result) { result = PRBMath.mulDivFixedPoint(x, y); } /// @notice Returns PI as an unsigned 60.18-decimal fixed-point number. function pi() internal pure returns (uint256 result) { result = 3_141592653589793238; } /// @notice Raises x to the power of y. /// /// @dev Based on the insight that x^y = 2^(log2(x) * y). /// /// Requirements: /// - All from "exp2", "log2" and "mul". /// /// Caveats: /// - All from "exp2", "log2" and "mul". /// - Assumes 0^0 is 1. /// /// @param x Number to raise to given power y, as an unsigned 60.18-decimal fixed-point number. /// @param y Exponent to raise x to, as an unsigned 60.18-decimal fixed-point number. /// @return result x raised to power y, as an unsigned 60.18-decimal fixed-point number. function pow(uint256 x, uint256 y) internal pure returns (uint256 result) { if (x == 0) { result = y == 0 ? SCALE : uint256(0); } else { result = exp2(mul(log2(x), y)); } } /// @notice Raises x (unsigned 60.18-decimal fixed-point number) to the power of y (basic unsigned integer) using the /// famous algorithm "exponentiation by squaring". /// /// @dev See https://en.wikipedia.org/wiki/Exponentiation_by_squaring /// /// Requirements: /// - The result must fit within MAX_UD60x18. /// /// Caveats: /// - All from "mul". /// - Assumes 0^0 is 1. /// /// @param x The base as an unsigned 60.18-decimal fixed-point number. /// @param y The exponent as an uint256. /// @return result The result as an unsigned 60.18-decimal fixed-point number. function powu(uint256 x, uint256 y) internal pure returns (uint256 result) { // Calculate the first iteration of the loop in advance. result = y & 1 > 0 ? x : SCALE; // Equivalent to "for(y /= 2; y > 0; y /= 2)" but faster. for (y >>= 1; y > 0; y >>= 1) { x = PRBMath.mulDivFixedPoint(x, x); // Equivalent to "y % 2 == 1" but faster. if (y & 1 > 0) { result = PRBMath.mulDivFixedPoint(result, x); } } } /// @notice Returns 1 as an unsigned 60.18-decimal fixed-point number. function scale() internal pure returns (uint256 result) { result = SCALE; } /// @notice Calculates the square root of x, rounding down. /// @dev Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method. /// /// Requirements: /// - x must be less than MAX_UD60x18 / SCALE. /// /// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the square root. /// @return result The result as an unsigned 60.18-decimal fixed-point . function sqrt(uint256 x) internal pure returns (uint256 result) { unchecked { if (x > MAX_UD60x18 / SCALE) { revert PRBMathUD60x18__SqrtOverflow(x); } // Multiply x by the SCALE to account for the factor of SCALE that is picked up when multiplying two unsigned // 60.18-decimal fixed-point numbers together (in this case, those two numbers are both the square root). result = PRBMath.sqrt(x * SCALE); } } /// @notice Converts a unsigned 60.18-decimal fixed-point number to basic integer form, rounding down in the process. /// @param x The unsigned 60.18-decimal fixed-point number to convert. /// @return result The same number in basic integer form. function toUint(uint256 x) internal pure returns (uint256 result) { unchecked { result = x / SCALE; } } }
{ "evmVersion": "paris", "optimizer": { "enabled": false, "runs": 200 }, "outputSelection": { "*": { "*": [ "evm.bytecode", "evm.deployedBytecode", "devdoc", "userdoc", "metadata", "abi" ] } } }
Contract Security Audit
- No Contract Security Audit Submitted- Submit Audit Here
[{"inputs":[{"internalType":"address","name":"_uniRouter","type":"address"},{"internalType":"address","name":"_bancorFormula","type":"address"},{"internalType":"address payable","name":"_feeRecipient","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"FailedToSendEth","type":"error"},{"inputs":[],"name":"InsufficientPoolbalance","type":"error"},{"inputs":[],"name":"InvalidLpFeePercentage","type":"error"},{"inputs":[],"name":"InvalidRecipient","type":"error"},{"inputs":[],"name":"MaxPoolBalanceReached","type":"error"},{"inputs":[],"name":"PairCreationFailed","type":"error"},{"inputs":[],"name":"ReentrancyGuardReentrantCall","type":"error"},{"inputs":[],"name":"TokenAlreadyListed","type":"error"},{"inputs":[],"name":"TokenDoesNotExist","type":"error"},{"inputs":[],"name":"TokenTransferFailed","type":"error"},{"inputs":[],"name":"ZeroEthSent","type":"error"},{"inputs":[],"name":"ZeroTokenAmount","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"ethAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"tokenAmount","type":"uint256"}],"name":"LiquidityAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"tokenAddress","type":"address"},{"indexed":true,"internalType":"address","name":"creator","type":"address"},{"indexed":false,"internalType":"string","name":"name","type":"string"},{"indexed":false,"internalType":"string","name":"symbol","type":"string"}],"name":"TokenCreated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":true,"internalType":"address","name":"buyer","type":"address"},{"indexed":false,"internalType":"uint256","name":"ethAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"tokenAmount","type":"uint256"}],"name":"TokensBought","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":true,"internalType":"address","name":"seller","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"ethAmount","type":"uint256"}],"name":"TokensSold","type":"event"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"}],"name":"addLP","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"}],"name":"buy","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"},{"internalType":"uint256","name":"ethAmount","type":"uint256"}],"name":"calculateCurvedBuyReturn","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"},{"internalType":"uint256","name":"tokenAmount","type":"uint256"}],"name":"calculateCurvedSellReturn","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"symbol","type":"string"}],"name":"create","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"}],"name":"getCurrentTokenPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"}],"name":"getMarketCap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"}],"name":"getTokenEthBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"},{"internalType":"uint256","name":"tokenAmount","type":"uint256"}],"name":"sell","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_bancorFormula","type":"address"}],"name":"setBancorFormula","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address payable","name":"_newRecipient","type":"address"}],"name":"setFeeRecipient","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_lpFeePercentage","type":"uint256"}],"name":"setLpFeePercentage","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_maxPoolBalance","type":"uint256"}],"name":"setMaxPoolBalance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_uniRouter","type":"address"}],"name":"setUniRouter","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"tokenList","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"tokens","outputs":[{"internalType":"contract BondingCurveToken","name":"token","type":"address"},{"internalType":"uint256","name":"tokenbalance","type":"uint256"},{"internalType":"uint256","name":"ethBalance","type":"uint256"},{"internalType":"bool","name":"isListed","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]
Contract Creation Code
60a060405266b1a2bc2ec500006006556903120bec57b51c10000060075561dead73ffffffffffffffffffffffffffffffffffffffff1660809073ffffffffffffffffffffffffffffffffffffffff1681525034801561005e57600080fd5b5060405161603c38038061603c833981810160405281019061008091906102db565b61009c61009161016e60201b60201c565b61017660201b60201c565b6001808190555082600360006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555081600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555080600860006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050505061032e565b600033905090565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050816000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508173ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061026a8261023f565b9050919050565b61027a8161025f565b811461028557600080fd5b50565b60008151905061029781610271565b92915050565b60006102a88261023f565b9050919050565b6102b88161029d565b81146102c357600080fd5b50565b6000815190506102d5816102af565b92915050565b6000806000606084860312156102f4576102f361023a565b5b600061030286828701610288565b935050602061031386828701610288565b9250506040610324868287016102c6565b9150509250925092565b608051615cf36103496000396000612ed00152615cf36000f3fe6080604052600436106101185760003560e01c80638e72b23f116100a0578063e486033911610064578063e4860339146103bd578063e74b981b146103fd578063ee4e268714610426578063f088d5471461044f578063f2fde38b1461046b5761011f565b80638e72b23f146102b45780639ead7222146102dd578063be33f6bd1461031a578063d146d31d14610357578063ddf731f3146103945761011f565b80636c197ff5116100e75780636c197ff5146101f7578063715018a61461022057806376f728d814610237578063880af793146102605780638da5cb5b146102895761011f565b806309f72da314610124578063198e2b8a1461016157806331f3efba1461017d57806341528e72146101ba5761011f565b3661011f57005b600080fd5b34801561013057600080fd5b5061014b600480360381019061014691906130d0565b610494565b6040516101589190613116565b60405180910390f35b61017b60048036038101906101769190613196565b610656565b005b34801561018957600080fd5b506101a4600480360381019061019f9190613243565b6109f7565b6040516101b19190613116565b60405180910390f35b3480156101c657600080fd5b506101e160048036038101906101dc91906130d0565b610cb1565b6040516101ee9190613116565b60405180910390f35b34801561020357600080fd5b5061021e60048036038101906102199190613243565b610cfd565b005b34801561022c57600080fd5b506102356112b6565b005b34801561024357600080fd5b5061025e60048036038101906102599190613283565b6112ca565b005b34801561026c57600080fd5b5061028760048036038101906102829190613283565b6112dc565b005b34801561029557600080fd5b5061029e61132f565b6040516102ab91906132bf565b60405180910390f35b3480156102c057600080fd5b506102db60048036038101906102d691906130d0565b611358565b005b3480156102e957600080fd5b5061030460048036038101906102ff9190613283565b6113a4565b60405161031191906132bf565b60405180910390f35b34801561032657600080fd5b50610341600480360381019061033c9190613243565b6113e3565b60405161034e9190613116565b60405180910390f35b34801561036357600080fd5b5061037e600480360381019061037991906130d0565b61169d565b60405161038b9190613116565b60405180910390f35b3480156103a057600080fd5b506103bb60048036038101906103b691906130d0565b61187e565b005b3480156103c957600080fd5b506103e460048036038101906103df91906130d0565b6119bf565b6040516103f49493929190613354565b60405180910390f35b34801561040957600080fd5b50610424600480360381019061041f91906133d7565b611a1c565b005b34801561043257600080fd5b5061044d600480360381019061044891906130d0565b611ace565b005b610469600480360381019061046491906130d0565b611b1a565b005b34801561047757600080fd5b50610492600480360381019061048d91906130d0565b612268565b005b600080600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603610567576040517fceea21b600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8160030160009054906101000a900460ff16156105b0576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000826002015490506000836001015490506000818473ffffffffffffffffffffffffffffffffffffffff16634c0abcdb6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610610573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106349190613419565b61063e9190613475565b905061064a83826122eb565b95505050505050919050565b61065e6123c9565b6000848484846040516106709061305b565b61067d9493929190613507565b604051809103906000f080158015610699573d6000803e3d6000fd5b509050600081905060405180608001604052808373ffffffffffffffffffffffffffffffffffffffff168152602001600081526020016000815260200160001515815250600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008201518160000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550602082015181600101556040820151816002015560608201518160030160006101000a81548160ff0219169083151502179055509050506005819080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508173ffffffffffffffffffffffffffffffffffffffff1663aa60b06b306040518263ffffffff1660e01b815260040161083691906132bf565b600060405180830381600087803b15801561085057600080fd5b505af1158015610864573d6000803e3d6000fd5b5050505060008273ffffffffffffffffffffffffffffffffffffffff166370a08231306040518263ffffffff1660e01b81526004016108a391906132bf565b602060405180830381865afa1580156108c0573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906108e49190613419565b905060008103610920576040517f045c4b0200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b80600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600101819055503373ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff167fd5d05a8421149c74fd223cfc823befb883babf9bf0b0e4d6bf9c8fdb70e59bb4898989896040516109ca9493929190613507565b60405180910390a360003411156109e6576109e5823461240f565b5b5050506109f16129a6565b50505050565b600080600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600082600201549050600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603610ad3576040517fceea21b600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8260030160009054906101000a900460ff1615610b1c576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b60008503610b56576040517f82c4a8c900000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000836001015490506000818473ffffffffffffffffffffffffffffffffffffffff16634c0abcdb6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610bad573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610bd19190613419565b610bdb9190613475565b90506000600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636d2dfa4a85848b6040518463ffffffff1660e01b8152600401610c3e93929190613542565b602060405180830381865afa158015610c5b573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610c7f9190613419565b90506000610c9482662386f26fc100006129af565b90508082610ca29190613475565b97505050505050505092915050565b6000600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600201549050919050565b610d056123c9565b6000600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603610dd7576040517fceea21b600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8160030160009054906101000a900460ff1615610e20576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b60008303610e5a576040517f82c4a8c900000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000826002015490506000836001015490506000818473ffffffffffffffffffffffffffffffffffffffff16634c0abcdb6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610eba573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610ede9190613419565b610ee89190613475565b90506000600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636d2dfa4a85848a6040518463ffffffff1660e01b8152600401610f4b93929190613542565b602060405180830381865afa158015610f68573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610f8c9190613419565b90506000610fa182662386f26fc100006129af565b905060008183610fb19190613475565b905082861015610fed576040517ff8ffc0af00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b82886002016000828254039250508190555088886001016000828254019250508190555060008211156110e1576000600860009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1683604051611062906135aa565b60006040518083038185875af1925050503d806000811461109f576040519150601f19603f3d011682016040523d82523d6000602084013e6110a4565b606091505b50509050806110df576040517ffe8a94f400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505b8673ffffffffffffffffffffffffffffffffffffffff166323b872dd33308c6040518463ffffffff1660e01b815260040161111e939291906135bf565b6020604051808303816000875af115801561113d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111619190613622565b611197576040517f045c4b0200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b60003373ffffffffffffffffffffffffffffffffffffffff16826040516111bd906135aa565b60006040518083038185875af1925050503d80600081146111fa576040519150601f19603f3d011682016040523d82523d6000602084013e6111ff565b606091505b505090508061123a576040517ffe8a94f400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b3373ffffffffffffffffffffffffffffffffffffffff168b73ffffffffffffffffffffffffffffffffffffffff167f697c42d55a5e1fed3f464ec6f38b32546a0bd368dc8068b065c67566d73f32908c8560405161129992919061364f565b60405180910390a35050505050505050506112b26129a6565b5050565b6112be6129d8565b6112c86000612a56565b565b6112d26129d8565b8060078190555050565b6112e46129d8565b66b1a2bc2ec50000811115611325576040517fc99a8b3a00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8060068190555050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b6113606129d8565b80600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b600581815481106113b457600080fd5b906000526020600020016000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600080600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff16036114b6576040517fceea21b600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8160030160009054906101000a900460ff16156114ff576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b60008403611539576040517fa096c83800000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b600082600201549050600061155586662386f26fc100006129af565b9050600081876115659190613475565b90506000856001015490506000818673ffffffffffffffffffffffffffffffffffffffff16634c0abcdb6040518163ffffffff1660e01b8152600401602060405180830381865afa1580156115be573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906115e29190613419565b6115ec9190613475565b9050600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166388dc87438683866040518463ffffffff1660e01b815260040161164d93929190613542565b602060405180830381865afa15801561166a573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061168e9190613419565b97505050505050505092915050565b600080600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603611770576040517fceea21b600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8160030160009054906101000a900460ff16156117b9576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000826001015490506000818373ffffffffffffffffffffffffffffffffffffffff16634c0abcdb6040518163ffffffff1660e01b8152600401602060405180830381865afa158015611810573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906118349190613419565b61183e9190613475565b9050600061184b87610494565b90506000670de0b6b3a764000082846118649190613678565b61186e91906136e9565b9050809650505050505050919050565b6118866129d8565b61188e6123c9565b6000600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603611960576040517fceea21b600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8160030160009054906101000a900460ff16156119a9576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6119b283612b1a565b50506119bc6129a6565b50565b60046020528060005260406000206000915090508060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16908060010154908060020154908060030160009054906101000a900460ff16905084565b611a246129d8565b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603611a8a576040517f9c8d2cd200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b80600860006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b611ad66129d8565b80600360006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b611b226123c9565b6000600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603611bf4576040517fceea21b600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8160030160009054906101000a900460ff1615611c3d576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b60003403611c77576040517fa096c83800000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b60008260020154905060008160075411611c92576000611ca1565b81600754611ca09190613475565b5b905060008103611cdd576040517f9876e37400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000846001015490506000818573ffffffffffffffffffffffffffffffffffffffff16634c0abcdb6040518163ffffffff1660e01b8152600401602060405180830381865afa158015611d34573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611d589190613419565b611d629190613475565b90506000662386f26fc10000670de0b6b3a7640000611d819190613475565b9050600081670de0b6b3a764000086611d9a9190613678565b611da491906136e9565b90506000813411611db55734611db7565b815b90506000611dcc82662386f26fc100006129af565b905060008183611ddc9190613475565b90506000600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166388dc87438b89856040518463ffffffff1660e01b8152600401611e3f93929190613542565b602060405180830381865afa158015611e5c573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611e809190613419565b905087811115611f5157879050600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636a32926c8b89846040518463ffffffff1660e01b8152600401611eec93929190613542565b602060405180830381865afa158015611f09573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611f2d9190613419565b9150611f4082662386f26fc100006129af565b92508282611f4e919061371a565b93505b818a611f5d919061371a565b8c60020181905550808c6001016000828254611f799190613475565b925050819055506000831115612050576000600860009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1684604051611fd1906135aa565b60006040518083038185875af1925050503d806000811461200e576040519150601f19603f3d011682016040523d82523d6000602084013e612013565b606091505b505090508061204e576040517ffe8a94f400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505b8a73ffffffffffffffffffffffffffffffffffffffff1663a9059cbb33836040518363ffffffff1660e01b815260040161208b92919061374e565b6020604051808303816000875af11580156120aa573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906120ce9190613622565b612104576040517f045c4b0200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000843411612114576000612121565b84346121209190613475565b5b905060008111156121d15760003373ffffffffffffffffffffffffffffffffffffffff1682604051612152906135aa565b60006040518083038185875af1925050503d806000811461218f576040519150601f19603f3d011682016040523d82523d6000602084013e612194565b606091505b50509050806121cf576040517ffe8a94f400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505b3373ffffffffffffffffffffffffffffffffffffffff168e73ffffffffffffffffffffffffffffffffffffffff167fc244d500136edffae7025aaf7fbd7a07193ac544a1d0cc3132cf2a32452d340c858560405161223092919061364f565b60405180910390a36122418d613014565b156122505761224f8e612b1a565b5b505050505050505050505050506122656129a6565b50565b6122706129d8565b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff16036122df576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016122d6906137e9565b60405180910390fd5b6122e881612a56565b50565b600080670de0b6b3a764000090506000600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636a32926c8686856040518463ffffffff1660e01b815260040161235a93929190613542565b602060405180830381865afa158015612377573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061239b9190613419565b905060006123b082662386f26fc100006129af565b905080826123be9190613475565b935050505092915050565b600260015403612405576040517f3ee5aeb500000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6002600181905550565b6000600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690506000826002015490506000836001015490506000818473ffffffffffffffffffffffffffffffffffffffff16634c0abcdb6040518163ffffffff1660e01b8152600401602060405180830381865afa1580156124db573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906124ff9190613419565b6125099190613475565b9050600061251e87662386f26fc100006129af565b90506000818861252e9190613475565b90506000600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166388dc87438786856040518463ffffffff1660e01b815260040161259193929190613542565b602060405180830381865afa1580156125ae573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906125d29190613419565b90508481111561269557849050600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636a32926c8786846040518463ffffffff1660e01b815260040161263e93929190613542565b602060405180830381865afa15801561265b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061267f9190613419565b915061269282662386f26fc100006129af565b92505b818860020160008282546126a9919061371a565b92505081905550808860010160008282546126c49190613475565b92505081905550600083111561279b576000600860009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168460405161271c906135aa565b60006040518083038185875af1925050503d8060008114612759576040519150601f19603f3d011682016040523d82523d6000602084013e61275e565b606091505b5050905080612799576040517ffe8a94f400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505b8673ffffffffffffffffffffffffffffffffffffffff1663a9059cbb33836040518363ffffffff1660e01b81526004016127d692919061374e565b6020604051808303816000875af11580156127f5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906128199190613622565b61284f576040517f045c4b0200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000838361285d919061371a565b8a1161286a576000612882565b8383612876919061371a565b8a6128819190613475565b5b905060008111156129325760003373ffffffffffffffffffffffffffffffffffffffff16826040516128b3906135aa565b60006040518083038185875af1925050503d80600081146128f0576040519150601f19603f3d011682016040523d82523d6000602084013e6128f5565b606091505b5050905080612930576040517ffe8a94f400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505b3373ffffffffffffffffffffffffffffffffffffffff168b73ffffffffffffffffffffffffffffffffffffffff167fc244d500136edffae7025aaf7fbd7a07193ac544a1d0cc3132cf2a32452d340c858560405161299192919061364f565b60405180910390a35050505050505050505050565b60018081905550565b6000670de0b6b3a764000082846129c69190613678565b6129d091906136e9565b905092915050565b6129e0613053565b73ffffffffffffffffffffffffffffffffffffffff166129fe61132f565b73ffffffffffffffffffffffffffffffffffffffff1614612a54576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401612a4b90613855565b60405180910390fd5b565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050816000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508173ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35050565b6000600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690508160030160009054906101000a900460ff1615612bcf576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000826002015490506000612be6826006546129af565b905060008183612bf69190613475565b90508373ffffffffffffffffffffffffffffffffffffffff16634f6ef09a306040518263ffffffff1660e01b8152600401612c3191906132bf565b600060405180830381600087803b158015612c4b57600080fd5b505af1158015612c5f573d6000803e3d6000fd5b50505050600085600101548573ffffffffffffffffffffffffffffffffffffffff1663670171fd6040518163ffffffff1660e01b8152600401602060405180830381865afa158015612cb5573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612cd99190613419565b612ce3919061371a565b9050600086600101819055506000866002018190555060018660030160006101000a81548160ff0219169083151502179055506000831115612de6576000600860009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1684604051612d67906135aa565b60006040518083038185875af1925050503d8060008114612da4576040519150601f19603f3d011682016040523d82523d6000602084013e612da9565b606091505b5050905080612de4576040517ffe8a94f400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505b8473ffffffffffffffffffffffffffffffffffffffff1663095ea7b3600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16836040518363ffffffff1660e01b8152600401612e4392919061374e565b6020604051808303816000875af1158015612e62573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612e869190613622565b50600080600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663f305d719858b866000807f0000000000000000000000000000000000000000000000000000000000000000426040518863ffffffff1660e01b8152600401612f11969594939291906138b0565b60606040518083038185885af1158015612f2f573d6000803e3d6000fd5b50505050506040513d601f19601f82011682018060405250810190612f549190613911565b50915091508673ffffffffffffffffffffffffffffffffffffffff1663715018a66040518163ffffffff1660e01b8152600401600060405180830381600087803b158015612fa157600080fd5b505af1158015612fb5573d6000803e3d6000fd5b505050508873ffffffffffffffffffffffffffffffffffffffff167fac1d76749e5447b7b16f5ab61447e1bd502f3bb4807af3b28e620d1700a6ee45828460405161300192919061364f565b60405180910390a2505050505050505050565b600080606460636007546130289190613678565b61303291906136e9565b905060008360010154148061304b575080836002015410155b915050919050565b600033905090565b6123598061396583390190565b600080fd5b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061309d82613072565b9050919050565b6130ad81613092565b81146130b857600080fd5b50565b6000813590506130ca816130a4565b92915050565b6000602082840312156130e6576130e5613068565b5b60006130f4848285016130bb565b91505092915050565b6000819050919050565b613110816130fd565b82525050565b600060208201905061312b6000830184613107565b92915050565b600080fd5b600080fd5b600080fd5b60008083601f84011261315657613155613131565b5b8235905067ffffffffffffffff81111561317357613172613136565b5b60208301915083600182028301111561318f5761318e61313b565b5b9250929050565b600080600080604085870312156131b0576131af613068565b5b600085013567ffffffffffffffff8111156131ce576131cd61306d565b5b6131da87828801613140565b9450945050602085013567ffffffffffffffff8111156131fd576131fc61306d565b5b61320987828801613140565b925092505092959194509250565b613220816130fd565b811461322b57600080fd5b50565b60008135905061323d81613217565b92915050565b6000806040838503121561325a57613259613068565b5b6000613268858286016130bb565b92505060206132798582860161322e565b9150509250929050565b60006020828403121561329957613298613068565b5b60006132a78482850161322e565b91505092915050565b6132b981613092565b82525050565b60006020820190506132d460008301846132b0565b92915050565b6000819050919050565b60006132ff6132fa6132f584613072565b6132da565b613072565b9050919050565b6000613311826132e4565b9050919050565b600061332382613306565b9050919050565b61333381613318565b82525050565b60008115159050919050565b61334e81613339565b82525050565b6000608082019050613369600083018761332a565b6133766020830186613107565b6133836040830185613107565b6133906060830184613345565b95945050505050565b60006133a482613072565b9050919050565b6133b481613399565b81146133bf57600080fd5b50565b6000813590506133d1816133ab565b92915050565b6000602082840312156133ed576133ec613068565b5b60006133fb848285016133c2565b91505092915050565b60008151905061341381613217565b92915050565b60006020828403121561342f5761342e613068565b5b600061343d84828501613404565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000613480826130fd565b915061348b836130fd565b92508282039050818111156134a3576134a2613446565b5b92915050565b600082825260208201905092915050565b82818337600083830152505050565b6000601f19601f8301169050919050565b60006134e683856134a9565b93506134f38385846134ba565b6134fc836134c9565b840190509392505050565b600060408201905081810360008301526135228186886134da565b905081810360208301526135378184866134da565b905095945050505050565b60006060820190506135576000830186613107565b6135646020830185613107565b6135716040830184613107565b949350505050565b600081905092915050565b50565b6000613594600083613579565b915061359f82613584565b600082019050919050565b60006135b582613587565b9150819050919050565b60006060820190506135d460008301866132b0565b6135e160208301856132b0565b6135ee6040830184613107565b949350505050565b6135ff81613339565b811461360a57600080fd5b50565b60008151905061361c816135f6565b92915050565b60006020828403121561363857613637613068565b5b60006136468482850161360d565b91505092915050565b60006040820190506136646000830185613107565b6136716020830184613107565b9392505050565b6000613683826130fd565b915061368e836130fd565b925082820261369c816130fd565b915082820484148315176136b3576136b2613446565b5b5092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b60006136f4826130fd565b91506136ff836130fd565b92508261370f5761370e6136ba565b5b828204905092915050565b6000613725826130fd565b9150613730836130fd565b925082820190508082111561374857613747613446565b5b92915050565b600060408201905061376360008301856132b0565b6137706020830184613107565b9392505050565b7f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160008201527f6464726573730000000000000000000000000000000000000000000000000000602082015250565b60006137d36026836134a9565b91506137de82613777565b604082019050919050565b60006020820190508181036000830152613802816137c6565b9050919050565b7f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572600082015250565b600061383f6020836134a9565b915061384a82613809565b602082019050919050565b6000602082019050818103600083015261386e81613832565b9050919050565b6000819050919050565b600061389a61389561389084613875565b6132da565b6130fd565b9050919050565b6138aa8161387f565b82525050565b600060c0820190506138c560008301896132b0565b6138d26020830188613107565b6138df60408301876138a1565b6138ec60608301866138a1565b6138f960808301856132b0565b61390660a0830184613107565b979650505050505050565b60008060006060848603121561392a57613929613068565b5b600061393886828701613404565b935050602061394986828701613404565b925050604061395a86828701613404565b915050925092509256fe608060405234801561001057600080fd5b5060405161235938038061235983398181016040528101906100329190610444565b8181816003908161004391906106dd565b50806004908161005391906106dd565b50505061007261006761009560201b60201c565b61009d60201b60201c565b61008e306b033b2e3c9fd0803ce800000061016360201b60201c565b50506108b9565b600033905090565b6000600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905081600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508173ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35050565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036101d2576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101c99061080c565b60405180910390fd5b6101e4600083836102c560201b60201c565b80600260008282546101f6919061085b565b92505081905550806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516102a7919061089e565b60405180910390a36102c1600083836102ca60201b60201c565b5050565b505050565b505050565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b610336826102ed565b810181811067ffffffffffffffff82111715610355576103546102fe565b5b80604052505050565b60006103686102cf565b9050610374828261032d565b919050565b600067ffffffffffffffff821115610394576103936102fe565b5b61039d826102ed565b9050602081019050919050565b60005b838110156103c85780820151818401526020810190506103ad565b60008484015250505050565b60006103e76103e284610379565b61035e565b905082815260208101848484011115610403576104026102e8565b5b61040e8482856103aa565b509392505050565b600082601f83011261042b5761042a6102e3565b5b815161043b8482602086016103d4565b91505092915050565b6000806040838503121561045b5761045a6102d9565b5b600083015167ffffffffffffffff811115610479576104786102de565b5b61048585828601610416565b925050602083015167ffffffffffffffff8111156104a6576104a56102de565b5b6104b285828601610416565b9150509250929050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061050e57607f821691505b602082108103610521576105206104c7565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026105897fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8261054c565b610593868361054c565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b60006105da6105d56105d0846105ab565b6105b5565b6105ab565b9050919050565b6000819050919050565b6105f4836105bf565b610608610600826105e1565b848454610559565b825550505050565b600090565b61061d610610565b6106288184846105eb565b505050565b5b8181101561064c57610641600082610615565b60018101905061062e565b5050565b601f8211156106915761066281610527565b61066b8461053c565b8101602085101561067a578190505b61068e6106868561053c565b83018261062d565b50505b505050565b600082821c905092915050565b60006106b460001984600802610696565b1980831691505092915050565b60006106cd83836106a3565b9150826002028217905092915050565b6106e6826104bc565b67ffffffffffffffff8111156106ff576106fe6102fe565b5b61070982546104f6565b610714828285610650565b600060209050601f8311600181146107475760008415610735578287015190505b61073f85826106c1565b8655506107a7565b601f19841661075586610527565b60005b8281101561077d57848901518255600182019150602085019450602081019050610758565b8683101561079a5784890151610796601f8916826106a3565b8355505b6001600288020188555050505b505050505050565b600082825260208201905092915050565b7f45524332303a206d696e7420746f20746865207a65726f206164647265737300600082015250565b60006107f6601f836107af565b9150610801826107c0565b602082019050919050565b60006020820190508181036000830152610825816107e9565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610866826105ab565b9150610871836105ab565b92508282019050808211156108895761088861082c565b5b92915050565b610898816105ab565b82525050565b60006020820190506108b3600083018461088f565b92915050565b611a91806108c86000396000f3fe608060405234801561001057600080fd5b506004361061012c5760003560e01c806370a08231116100ad578063a457c2d711610071578063a457c2d714610321578063a9059cbb14610351578063aa60b06b14610381578063dd62ed3e1461039d578063f2fde38b146103cd5761012c565b806370a082311461028f578063715018a6146102bf57806379cc6790146102c95780638da5cb5b146102e557806395d89b41146103035761012c565b806339509351116100f457806339509351146101eb57806342966c681461021b5780634c0abcdb146102375780634f6ef09a14610255578063670171fd146102715761012c565b806306fdde0314610131578063095ea7b31461014f57806318160ddd1461017f57806323b872dd1461019d578063313ce567146101cd575b600080fd5b6101396103e9565b60405161014691906110f5565b60405180910390f35b610169600480360381019061016491906111b0565b61047b565b604051610176919061120b565b60405180910390f35b61018761049e565b6040516101949190611235565b60405180910390f35b6101b760048036038101906101b29190611250565b6104a8565b6040516101c4919061120b565b60405180910390f35b6101d56104d7565b6040516101e291906112bf565b60405180910390f35b610205600480360381019061020091906111b0565b6104e0565b604051610212919061120b565b60405180910390f35b610235600480360381019061023091906112da565b610517565b005b61023f61052b565b60405161024c9190611235565b60405180910390f35b61026f600480360381019061026a9190611307565b61053b565b005b61027961055c565b6040516102869190611235565b60405180910390f35b6102a960048036038101906102a49190611307565b61056b565b6040516102b69190611235565b60405180910390f35b6102c76105b3565b005b6102e360048036038101906102de91906111b0565b6105c7565b005b6102ed6105d5565b6040516102fa9190611343565b60405180910390f35b61030b6105ff565b60405161031891906110f5565b60405180910390f35b61033b600480360381019061033691906111b0565b610691565b604051610348919061120b565b60405180910390f35b61036b600480360381019061036691906111b0565b610708565b604051610378919061120b565b60405180910390f35b61039b60048036038101906103969190611307565b61072b565b005b6103b760048036038101906103b2919061135e565b61074d565b6040516103c49190611235565b60405180910390f35b6103e760048036038101906103e29190611307565b6107d4565b005b6060600380546103f8906113cd565b80601f0160208091040260200160405190810160405280929190818152602001828054610424906113cd565b80156104715780601f1061044657610100808354040283529160200191610471565b820191906000526020600020905b81548152906001019060200180831161045457829003601f168201915b5050505050905090565b600080610486610857565b905061049381858561085f565b600191505092915050565b6000600254905090565b6000806104b3610857565b90506104c0858285610a28565b6104cb858585610ab4565b60019150509392505050565b60006012905090565b6000806104eb610857565b905061050c8185856104fd858961074d565b610507919061142d565b61085f565b600191505092915050565b610528610522610857565b82610d2a565b50565b6b0295be96e64066972000000081565b610543610ef7565b61055930826aa56fa5b99019a5c8000000610ab4565b50565b6aa56fa5b99019a5c800000081565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b6105bb610ef7565b6105c56000610f75565b565b6105d1828261103b565b5050565b6000600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b60606004805461060e906113cd565b80601f016020809104026020016040519081016040528092919081815260200182805461063a906113cd565b80156106875780601f1061065c57610100808354040283529160200191610687565b820191906000526020600020905b81548152906001019060200180831161066a57829003601f168201915b5050505050905090565b60008061069c610857565b905060006106aa828661074d565b9050838110156106ef576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016106e6906114d3565b60405180910390fd5b6106fc828686840361085f565b60019250505092915050565b600080610713610857565b9050610720818585610ab4565b600191505092915050565b610733610ef7565b61074a30826b0295be96e640669720000000610ab4565b50565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b6107dc610ef7565b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff160361084b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161084290611565565b60405180910390fd5b61085481610f75565b50565b600033905090565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16036108ce576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016108c5906115f7565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160361093d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161093490611689565b60405180910390fd5b80600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92583604051610a1b9190611235565b60405180910390a3505050565b6000610a34848461074d565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8114610aae5781811015610aa0576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610a97906116f5565b60405180910390fd5b610aad848484840361085f565b5b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610b23576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610b1a90611787565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603610b92576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610b8990611819565b60405180910390fd5b610b9d83838361105b565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905081811015610c23576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610c1a906118ab565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610d119190611235565b60405180910390a3610d24848484611060565b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603610d99576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610d909061193d565b60405180910390fd5b610da58260008361105b565b60008060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905081811015610e2b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610e22906119cf565b60405180910390fd5b8181036000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555081600260008282540392505081905550600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610ede9190611235565b60405180910390a3610ef283600084611060565b505050565b610eff610857565b73ffffffffffffffffffffffffffffffffffffffff16610f1d6105d5565b73ffffffffffffffffffffffffffffffffffffffff1614610f73576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610f6a90611a3b565b60405180910390fd5b565b6000600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905081600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508173ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35050565b61104d82611047610857565b83610a28565b6110578282610d2a565b5050565b505050565b505050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561109f578082015181840152602081019050611084565b60008484015250505050565b6000601f19601f8301169050919050565b60006110c782611065565b6110d18185611070565b93506110e1818560208601611081565b6110ea816110ab565b840191505092915050565b6000602082019050818103600083015261110f81846110bc565b905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006111478261111c565b9050919050565b6111578161113c565b811461116257600080fd5b50565b6000813590506111748161114e565b92915050565b6000819050919050565b61118d8161117a565b811461119857600080fd5b50565b6000813590506111aa81611184565b92915050565b600080604083850312156111c7576111c6611117565b5b60006111d585828601611165565b92505060206111e68582860161119b565b9150509250929050565b60008115159050919050565b611205816111f0565b82525050565b600060208201905061122060008301846111fc565b92915050565b61122f8161117a565b82525050565b600060208201905061124a6000830184611226565b92915050565b60008060006060848603121561126957611268611117565b5b600061127786828701611165565b935050602061128886828701611165565b92505060406112998682870161119b565b9150509250925092565b600060ff82169050919050565b6112b9816112a3565b82525050565b60006020820190506112d460008301846112b0565b92915050565b6000602082840312156112f0576112ef611117565b5b60006112fe8482850161119b565b91505092915050565b60006020828403121561131d5761131c611117565b5b600061132b84828501611165565b91505092915050565b61133d8161113c565b82525050565b60006020820190506113586000830184611334565b92915050565b6000806040838503121561137557611374611117565b5b600061138385828601611165565b925050602061139485828601611165565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806113e557607f821691505b6020821081036113f8576113f761139e565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006114388261117a565b91506114438361117a565b925082820190508082111561145b5761145a6113fe565b5b92915050565b7f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f7760008201527f207a65726f000000000000000000000000000000000000000000000000000000602082015250565b60006114bd602583611070565b91506114c882611461565b604082019050919050565b600060208201905081810360008301526114ec816114b0565b9050919050565b7f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160008201527f6464726573730000000000000000000000000000000000000000000000000000602082015250565b600061154f602683611070565b915061155a826114f3565b604082019050919050565b6000602082019050818103600083015261157e81611542565b9050919050565b7f45524332303a20617070726f76652066726f6d20746865207a65726f2061646460008201527f7265737300000000000000000000000000000000000000000000000000000000602082015250565b60006115e1602483611070565b91506115ec82611585565b604082019050919050565b60006020820190508181036000830152611610816115d4565b9050919050565b7f45524332303a20617070726f766520746f20746865207a65726f20616464726560008201527f7373000000000000000000000000000000000000000000000000000000000000602082015250565b6000611673602283611070565b915061167e82611617565b604082019050919050565b600060208201905081810360008301526116a281611666565b9050919050565b7f45524332303a20696e73756666696369656e7420616c6c6f77616e6365000000600082015250565b60006116df601d83611070565b91506116ea826116a9565b602082019050919050565b6000602082019050818103600083015261170e816116d2565b9050919050565b7f45524332303a207472616e736665722066726f6d20746865207a65726f20616460008201527f6472657373000000000000000000000000000000000000000000000000000000602082015250565b6000611771602583611070565b915061177c82611715565b604082019050919050565b600060208201905081810360008301526117a081611764565b9050919050565b7f45524332303a207472616e7366657220746f20746865207a65726f206164647260008201527f6573730000000000000000000000000000000000000000000000000000000000602082015250565b6000611803602383611070565b915061180e826117a7565b604082019050919050565b60006020820190508181036000830152611832816117f6565b9050919050565b7f45524332303a207472616e7366657220616d6f756e742065786365656473206260008201527f616c616e63650000000000000000000000000000000000000000000000000000602082015250565b6000611895602683611070565b91506118a082611839565b604082019050919050565b600060208201905081810360008301526118c481611888565b9050919050565b7f45524332303a206275726e2066726f6d20746865207a65726f2061646472657360008201527f7300000000000000000000000000000000000000000000000000000000000000602082015250565b6000611927602183611070565b9150611932826118cb565b604082019050919050565b600060208201905081810360008301526119568161191a565b9050919050565b7f45524332303a206275726e20616d6f756e7420657863656564732062616c616e60008201527f6365000000000000000000000000000000000000000000000000000000000000602082015250565b60006119b9602283611070565b91506119c48261195d565b604082019050919050565b600060208201905081810360008301526119e8816119ac565b9050919050565b7f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572600082015250565b6000611a25602083611070565b9150611a30826119ef565b602082019050919050565b60006020820190508181036000830152611a5481611a18565b905091905056fea264697066735822122029fb9e3976dc34ea8c2335e8453d923920168f0fce0d170c0de7cdf7757ea4d564736f6c634300081c0033a264697066735822122003ef549fe9f08b32a444d1bcde97eff783a786f1092241eeb863fbfd63cd3dae64736f6c634300081c0033000000000000000000000000000000000000000000000000000000000000000000000000000000000000000042cc8bef111e05f478cdd37950f1c06b0348184a0000000000000000000000009c1b8deb6d89a795bc2e45f86a9af6e034744774
Deployed Bytecode
0x6080604052600436106101185760003560e01c80638e72b23f116100a0578063e486033911610064578063e4860339146103bd578063e74b981b146103fd578063ee4e268714610426578063f088d5471461044f578063f2fde38b1461046b5761011f565b80638e72b23f146102b45780639ead7222146102dd578063be33f6bd1461031a578063d146d31d14610357578063ddf731f3146103945761011f565b80636c197ff5116100e75780636c197ff5146101f7578063715018a61461022057806376f728d814610237578063880af793146102605780638da5cb5b146102895761011f565b806309f72da314610124578063198e2b8a1461016157806331f3efba1461017d57806341528e72146101ba5761011f565b3661011f57005b600080fd5b34801561013057600080fd5b5061014b600480360381019061014691906130d0565b610494565b6040516101589190613116565b60405180910390f35b61017b60048036038101906101769190613196565b610656565b005b34801561018957600080fd5b506101a4600480360381019061019f9190613243565b6109f7565b6040516101b19190613116565b60405180910390f35b3480156101c657600080fd5b506101e160048036038101906101dc91906130d0565b610cb1565b6040516101ee9190613116565b60405180910390f35b34801561020357600080fd5b5061021e60048036038101906102199190613243565b610cfd565b005b34801561022c57600080fd5b506102356112b6565b005b34801561024357600080fd5b5061025e60048036038101906102599190613283565b6112ca565b005b34801561026c57600080fd5b5061028760048036038101906102829190613283565b6112dc565b005b34801561029557600080fd5b5061029e61132f565b6040516102ab91906132bf565b60405180910390f35b3480156102c057600080fd5b506102db60048036038101906102d691906130d0565b611358565b005b3480156102e957600080fd5b5061030460048036038101906102ff9190613283565b6113a4565b60405161031191906132bf565b60405180910390f35b34801561032657600080fd5b50610341600480360381019061033c9190613243565b6113e3565b60405161034e9190613116565b60405180910390f35b34801561036357600080fd5b5061037e600480360381019061037991906130d0565b61169d565b60405161038b9190613116565b60405180910390f35b3480156103a057600080fd5b506103bb60048036038101906103b691906130d0565b61187e565b005b3480156103c957600080fd5b506103e460048036038101906103df91906130d0565b6119bf565b6040516103f49493929190613354565b60405180910390f35b34801561040957600080fd5b50610424600480360381019061041f91906133d7565b611a1c565b005b34801561043257600080fd5b5061044d600480360381019061044891906130d0565b611ace565b005b610469600480360381019061046491906130d0565b611b1a565b005b34801561047757600080fd5b50610492600480360381019061048d91906130d0565b612268565b005b600080600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603610567576040517fceea21b600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8160030160009054906101000a900460ff16156105b0576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000826002015490506000836001015490506000818473ffffffffffffffffffffffffffffffffffffffff16634c0abcdb6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610610573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106349190613419565b61063e9190613475565b905061064a83826122eb565b95505050505050919050565b61065e6123c9565b6000848484846040516106709061305b565b61067d9493929190613507565b604051809103906000f080158015610699573d6000803e3d6000fd5b509050600081905060405180608001604052808373ffffffffffffffffffffffffffffffffffffffff168152602001600081526020016000815260200160001515815250600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008201518160000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550602082015181600101556040820151816002015560608201518160030160006101000a81548160ff0219169083151502179055509050506005819080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508173ffffffffffffffffffffffffffffffffffffffff1663aa60b06b306040518263ffffffff1660e01b815260040161083691906132bf565b600060405180830381600087803b15801561085057600080fd5b505af1158015610864573d6000803e3d6000fd5b5050505060008273ffffffffffffffffffffffffffffffffffffffff166370a08231306040518263ffffffff1660e01b81526004016108a391906132bf565b602060405180830381865afa1580156108c0573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906108e49190613419565b905060008103610920576040517f045c4b0200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b80600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600101819055503373ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff167fd5d05a8421149c74fd223cfc823befb883babf9bf0b0e4d6bf9c8fdb70e59bb4898989896040516109ca9493929190613507565b60405180910390a360003411156109e6576109e5823461240f565b5b5050506109f16129a6565b50505050565b600080600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600082600201549050600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603610ad3576040517fceea21b600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8260030160009054906101000a900460ff1615610b1c576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b60008503610b56576040517f82c4a8c900000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000836001015490506000818473ffffffffffffffffffffffffffffffffffffffff16634c0abcdb6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610bad573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610bd19190613419565b610bdb9190613475565b90506000600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636d2dfa4a85848b6040518463ffffffff1660e01b8152600401610c3e93929190613542565b602060405180830381865afa158015610c5b573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610c7f9190613419565b90506000610c9482662386f26fc100006129af565b90508082610ca29190613475565b97505050505050505092915050565b6000600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600201549050919050565b610d056123c9565b6000600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603610dd7576040517fceea21b600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8160030160009054906101000a900460ff1615610e20576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b60008303610e5a576040517f82c4a8c900000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000826002015490506000836001015490506000818473ffffffffffffffffffffffffffffffffffffffff16634c0abcdb6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610eba573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610ede9190613419565b610ee89190613475565b90506000600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636d2dfa4a85848a6040518463ffffffff1660e01b8152600401610f4b93929190613542565b602060405180830381865afa158015610f68573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610f8c9190613419565b90506000610fa182662386f26fc100006129af565b905060008183610fb19190613475565b905082861015610fed576040517ff8ffc0af00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b82886002016000828254039250508190555088886001016000828254019250508190555060008211156110e1576000600860009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1683604051611062906135aa565b60006040518083038185875af1925050503d806000811461109f576040519150601f19603f3d011682016040523d82523d6000602084013e6110a4565b606091505b50509050806110df576040517ffe8a94f400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505b8673ffffffffffffffffffffffffffffffffffffffff166323b872dd33308c6040518463ffffffff1660e01b815260040161111e939291906135bf565b6020604051808303816000875af115801561113d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111619190613622565b611197576040517f045c4b0200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b60003373ffffffffffffffffffffffffffffffffffffffff16826040516111bd906135aa565b60006040518083038185875af1925050503d80600081146111fa576040519150601f19603f3d011682016040523d82523d6000602084013e6111ff565b606091505b505090508061123a576040517ffe8a94f400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b3373ffffffffffffffffffffffffffffffffffffffff168b73ffffffffffffffffffffffffffffffffffffffff167f697c42d55a5e1fed3f464ec6f38b32546a0bd368dc8068b065c67566d73f32908c8560405161129992919061364f565b60405180910390a35050505050505050506112b26129a6565b5050565b6112be6129d8565b6112c86000612a56565b565b6112d26129d8565b8060078190555050565b6112e46129d8565b66b1a2bc2ec50000811115611325576040517fc99a8b3a00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8060068190555050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b6113606129d8565b80600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b600581815481106113b457600080fd5b906000526020600020016000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600080600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff16036114b6576040517fceea21b600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8160030160009054906101000a900460ff16156114ff576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b60008403611539576040517fa096c83800000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b600082600201549050600061155586662386f26fc100006129af565b9050600081876115659190613475565b90506000856001015490506000818673ffffffffffffffffffffffffffffffffffffffff16634c0abcdb6040518163ffffffff1660e01b8152600401602060405180830381865afa1580156115be573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906115e29190613419565b6115ec9190613475565b9050600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166388dc87438683866040518463ffffffff1660e01b815260040161164d93929190613542565b602060405180830381865afa15801561166a573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061168e9190613419565b97505050505050505092915050565b600080600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603611770576040517fceea21b600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8160030160009054906101000a900460ff16156117b9576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000826001015490506000818373ffffffffffffffffffffffffffffffffffffffff16634c0abcdb6040518163ffffffff1660e01b8152600401602060405180830381865afa158015611810573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906118349190613419565b61183e9190613475565b9050600061184b87610494565b90506000670de0b6b3a764000082846118649190613678565b61186e91906136e9565b9050809650505050505050919050565b6118866129d8565b61188e6123c9565b6000600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603611960576040517fceea21b600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8160030160009054906101000a900460ff16156119a9576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6119b283612b1a565b50506119bc6129a6565b50565b60046020528060005260406000206000915090508060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16908060010154908060020154908060030160009054906101000a900460ff16905084565b611a246129d8565b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603611a8a576040517f9c8d2cd200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b80600860006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b611ad66129d8565b80600360006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b611b226123c9565b6000600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603611bf4576040517fceea21b600000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8160030160009054906101000a900460ff1615611c3d576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b60003403611c77576040517fa096c83800000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b60008260020154905060008160075411611c92576000611ca1565b81600754611ca09190613475565b5b905060008103611cdd576040517f9876e37400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000846001015490506000818573ffffffffffffffffffffffffffffffffffffffff16634c0abcdb6040518163ffffffff1660e01b8152600401602060405180830381865afa158015611d34573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611d589190613419565b611d629190613475565b90506000662386f26fc10000670de0b6b3a7640000611d819190613475565b9050600081670de0b6b3a764000086611d9a9190613678565b611da491906136e9565b90506000813411611db55734611db7565b815b90506000611dcc82662386f26fc100006129af565b905060008183611ddc9190613475565b90506000600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166388dc87438b89856040518463ffffffff1660e01b8152600401611e3f93929190613542565b602060405180830381865afa158015611e5c573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611e809190613419565b905087811115611f5157879050600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636a32926c8b89846040518463ffffffff1660e01b8152600401611eec93929190613542565b602060405180830381865afa158015611f09573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611f2d9190613419565b9150611f4082662386f26fc100006129af565b92508282611f4e919061371a565b93505b818a611f5d919061371a565b8c60020181905550808c6001016000828254611f799190613475565b925050819055506000831115612050576000600860009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1684604051611fd1906135aa565b60006040518083038185875af1925050503d806000811461200e576040519150601f19603f3d011682016040523d82523d6000602084013e612013565b606091505b505090508061204e576040517ffe8a94f400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505b8a73ffffffffffffffffffffffffffffffffffffffff1663a9059cbb33836040518363ffffffff1660e01b815260040161208b92919061374e565b6020604051808303816000875af11580156120aa573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906120ce9190613622565b612104576040517f045c4b0200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000843411612114576000612121565b84346121209190613475565b5b905060008111156121d15760003373ffffffffffffffffffffffffffffffffffffffff1682604051612152906135aa565b60006040518083038185875af1925050503d806000811461218f576040519150601f19603f3d011682016040523d82523d6000602084013e612194565b606091505b50509050806121cf576040517ffe8a94f400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505b3373ffffffffffffffffffffffffffffffffffffffff168e73ffffffffffffffffffffffffffffffffffffffff167fc244d500136edffae7025aaf7fbd7a07193ac544a1d0cc3132cf2a32452d340c858560405161223092919061364f565b60405180910390a36122418d613014565b156122505761224f8e612b1a565b5b505050505050505050505050506122656129a6565b50565b6122706129d8565b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff16036122df576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016122d6906137e9565b60405180910390fd5b6122e881612a56565b50565b600080670de0b6b3a764000090506000600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636a32926c8686856040518463ffffffff1660e01b815260040161235a93929190613542565b602060405180830381865afa158015612377573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061239b9190613419565b905060006123b082662386f26fc100006129af565b905080826123be9190613475565b935050505092915050565b600260015403612405576040517f3ee5aeb500000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6002600181905550565b6000600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690506000826002015490506000836001015490506000818473ffffffffffffffffffffffffffffffffffffffff16634c0abcdb6040518163ffffffff1660e01b8152600401602060405180830381865afa1580156124db573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906124ff9190613419565b6125099190613475565b9050600061251e87662386f26fc100006129af565b90506000818861252e9190613475565b90506000600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166388dc87438786856040518463ffffffff1660e01b815260040161259193929190613542565b602060405180830381865afa1580156125ae573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906125d29190613419565b90508481111561269557849050600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636a32926c8786846040518463ffffffff1660e01b815260040161263e93929190613542565b602060405180830381865afa15801561265b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061267f9190613419565b915061269282662386f26fc100006129af565b92505b818860020160008282546126a9919061371a565b92505081905550808860010160008282546126c49190613475565b92505081905550600083111561279b576000600860009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168460405161271c906135aa565b60006040518083038185875af1925050503d8060008114612759576040519150601f19603f3d011682016040523d82523d6000602084013e61275e565b606091505b5050905080612799576040517ffe8a94f400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505b8673ffffffffffffffffffffffffffffffffffffffff1663a9059cbb33836040518363ffffffff1660e01b81526004016127d692919061374e565b6020604051808303816000875af11580156127f5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906128199190613622565b61284f576040517f045c4b0200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000838361285d919061371a565b8a1161286a576000612882565b8383612876919061371a565b8a6128819190613475565b5b905060008111156129325760003373ffffffffffffffffffffffffffffffffffffffff16826040516128b3906135aa565b60006040518083038185875af1925050503d80600081146128f0576040519150601f19603f3d011682016040523d82523d6000602084013e6128f5565b606091505b5050905080612930576040517ffe8a94f400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505b3373ffffffffffffffffffffffffffffffffffffffff168b73ffffffffffffffffffffffffffffffffffffffff167fc244d500136edffae7025aaf7fbd7a07193ac544a1d0cc3132cf2a32452d340c858560405161299192919061364f565b60405180910390a35050505050505050505050565b60018081905550565b6000670de0b6b3a764000082846129c69190613678565b6129d091906136e9565b905092915050565b6129e0613053565b73ffffffffffffffffffffffffffffffffffffffff166129fe61132f565b73ffffffffffffffffffffffffffffffffffffffff1614612a54576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401612a4b90613855565b60405180910390fd5b565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050816000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508173ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35050565b6000600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690508160030160009054906101000a900460ff1615612bcf576040517fdeaabdc200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000826002015490506000612be6826006546129af565b905060008183612bf69190613475565b90508373ffffffffffffffffffffffffffffffffffffffff16634f6ef09a306040518263ffffffff1660e01b8152600401612c3191906132bf565b600060405180830381600087803b158015612c4b57600080fd5b505af1158015612c5f573d6000803e3d6000fd5b50505050600085600101548573ffffffffffffffffffffffffffffffffffffffff1663670171fd6040518163ffffffff1660e01b8152600401602060405180830381865afa158015612cb5573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612cd99190613419565b612ce3919061371a565b9050600086600101819055506000866002018190555060018660030160006101000a81548160ff0219169083151502179055506000831115612de6576000600860009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1684604051612d67906135aa565b60006040518083038185875af1925050503d8060008114612da4576040519150601f19603f3d011682016040523d82523d6000602084013e612da9565b606091505b5050905080612de4576040517ffe8a94f400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505b8473ffffffffffffffffffffffffffffffffffffffff1663095ea7b3600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16836040518363ffffffff1660e01b8152600401612e4392919061374e565b6020604051808303816000875af1158015612e62573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612e869190613622565b50600080600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663f305d719858b866000807f000000000000000000000000000000000000000000000000000000000000dead426040518863ffffffff1660e01b8152600401612f11969594939291906138b0565b60606040518083038185885af1158015612f2f573d6000803e3d6000fd5b50505050506040513d601f19601f82011682018060405250810190612f549190613911565b50915091508673ffffffffffffffffffffffffffffffffffffffff1663715018a66040518163ffffffff1660e01b8152600401600060405180830381600087803b158015612fa157600080fd5b505af1158015612fb5573d6000803e3d6000fd5b505050508873ffffffffffffffffffffffffffffffffffffffff167fac1d76749e5447b7b16f5ab61447e1bd502f3bb4807af3b28e620d1700a6ee45828460405161300192919061364f565b60405180910390a2505050505050505050565b600080606460636007546130289190613678565b61303291906136e9565b905060008360010154148061304b575080836002015410155b915050919050565b600033905090565b6123598061396583390190565b600080fd5b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061309d82613072565b9050919050565b6130ad81613092565b81146130b857600080fd5b50565b6000813590506130ca816130a4565b92915050565b6000602082840312156130e6576130e5613068565b5b60006130f4848285016130bb565b91505092915050565b6000819050919050565b613110816130fd565b82525050565b600060208201905061312b6000830184613107565b92915050565b600080fd5b600080fd5b600080fd5b60008083601f84011261315657613155613131565b5b8235905067ffffffffffffffff81111561317357613172613136565b5b60208301915083600182028301111561318f5761318e61313b565b5b9250929050565b600080600080604085870312156131b0576131af613068565b5b600085013567ffffffffffffffff8111156131ce576131cd61306d565b5b6131da87828801613140565b9450945050602085013567ffffffffffffffff8111156131fd576131fc61306d565b5b61320987828801613140565b925092505092959194509250565b613220816130fd565b811461322b57600080fd5b50565b60008135905061323d81613217565b92915050565b6000806040838503121561325a57613259613068565b5b6000613268858286016130bb565b92505060206132798582860161322e565b9150509250929050565b60006020828403121561329957613298613068565b5b60006132a78482850161322e565b91505092915050565b6132b981613092565b82525050565b60006020820190506132d460008301846132b0565b92915050565b6000819050919050565b60006132ff6132fa6132f584613072565b6132da565b613072565b9050919050565b6000613311826132e4565b9050919050565b600061332382613306565b9050919050565b61333381613318565b82525050565b60008115159050919050565b61334e81613339565b82525050565b6000608082019050613369600083018761332a565b6133766020830186613107565b6133836040830185613107565b6133906060830184613345565b95945050505050565b60006133a482613072565b9050919050565b6133b481613399565b81146133bf57600080fd5b50565b6000813590506133d1816133ab565b92915050565b6000602082840312156133ed576133ec613068565b5b60006133fb848285016133c2565b91505092915050565b60008151905061341381613217565b92915050565b60006020828403121561342f5761342e613068565b5b600061343d84828501613404565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000613480826130fd565b915061348b836130fd565b92508282039050818111156134a3576134a2613446565b5b92915050565b600082825260208201905092915050565b82818337600083830152505050565b6000601f19601f8301169050919050565b60006134e683856134a9565b93506134f38385846134ba565b6134fc836134c9565b840190509392505050565b600060408201905081810360008301526135228186886134da565b905081810360208301526135378184866134da565b905095945050505050565b60006060820190506135576000830186613107565b6135646020830185613107565b6135716040830184613107565b949350505050565b600081905092915050565b50565b6000613594600083613579565b915061359f82613584565b600082019050919050565b60006135b582613587565b9150819050919050565b60006060820190506135d460008301866132b0565b6135e160208301856132b0565b6135ee6040830184613107565b949350505050565b6135ff81613339565b811461360a57600080fd5b50565b60008151905061361c816135f6565b92915050565b60006020828403121561363857613637613068565b5b60006136468482850161360d565b91505092915050565b60006040820190506136646000830185613107565b6136716020830184613107565b9392505050565b6000613683826130fd565b915061368e836130fd565b925082820261369c816130fd565b915082820484148315176136b3576136b2613446565b5b5092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b60006136f4826130fd565b91506136ff836130fd565b92508261370f5761370e6136ba565b5b828204905092915050565b6000613725826130fd565b9150613730836130fd565b925082820190508082111561374857613747613446565b5b92915050565b600060408201905061376360008301856132b0565b6137706020830184613107565b9392505050565b7f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160008201527f6464726573730000000000000000000000000000000000000000000000000000602082015250565b60006137d36026836134a9565b91506137de82613777565b604082019050919050565b60006020820190508181036000830152613802816137c6565b9050919050565b7f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572600082015250565b600061383f6020836134a9565b915061384a82613809565b602082019050919050565b6000602082019050818103600083015261386e81613832565b9050919050565b6000819050919050565b600061389a61389561389084613875565b6132da565b6130fd565b9050919050565b6138aa8161387f565b82525050565b600060c0820190506138c560008301896132b0565b6138d26020830188613107565b6138df60408301876138a1565b6138ec60608301866138a1565b6138f960808301856132b0565b61390660a0830184613107565b979650505050505050565b60008060006060848603121561392a57613929613068565b5b600061393886828701613404565b935050602061394986828701613404565b925050604061395a86828701613404565b915050925092509256fe608060405234801561001057600080fd5b5060405161235938038061235983398181016040528101906100329190610444565b8181816003908161004391906106dd565b50806004908161005391906106dd565b50505061007261006761009560201b60201c565b61009d60201b60201c565b61008e306b033b2e3c9fd0803ce800000061016360201b60201c565b50506108b9565b600033905090565b6000600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905081600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508173ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35050565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036101d2576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101c99061080c565b60405180910390fd5b6101e4600083836102c560201b60201c565b80600260008282546101f6919061085b565b92505081905550806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516102a7919061089e565b60405180910390a36102c1600083836102ca60201b60201c565b5050565b505050565b505050565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b610336826102ed565b810181811067ffffffffffffffff82111715610355576103546102fe565b5b80604052505050565b60006103686102cf565b9050610374828261032d565b919050565b600067ffffffffffffffff821115610394576103936102fe565b5b61039d826102ed565b9050602081019050919050565b60005b838110156103c85780820151818401526020810190506103ad565b60008484015250505050565b60006103e76103e284610379565b61035e565b905082815260208101848484011115610403576104026102e8565b5b61040e8482856103aa565b509392505050565b600082601f83011261042b5761042a6102e3565b5b815161043b8482602086016103d4565b91505092915050565b6000806040838503121561045b5761045a6102d9565b5b600083015167ffffffffffffffff811115610479576104786102de565b5b61048585828601610416565b925050602083015167ffffffffffffffff8111156104a6576104a56102de565b5b6104b285828601610416565b9150509250929050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061050e57607f821691505b602082108103610521576105206104c7565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026105897fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8261054c565b610593868361054c565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b60006105da6105d56105d0846105ab565b6105b5565b6105ab565b9050919050565b6000819050919050565b6105f4836105bf565b610608610600826105e1565b848454610559565b825550505050565b600090565b61061d610610565b6106288184846105eb565b505050565b5b8181101561064c57610641600082610615565b60018101905061062e565b5050565b601f8211156106915761066281610527565b61066b8461053c565b8101602085101561067a578190505b61068e6106868561053c565b83018261062d565b50505b505050565b600082821c905092915050565b60006106b460001984600802610696565b1980831691505092915050565b60006106cd83836106a3565b9150826002028217905092915050565b6106e6826104bc565b67ffffffffffffffff8111156106ff576106fe6102fe565b5b61070982546104f6565b610714828285610650565b600060209050601f8311600181146107475760008415610735578287015190505b61073f85826106c1565b8655506107a7565b601f19841661075586610527565b60005b8281101561077d57848901518255600182019150602085019450602081019050610758565b8683101561079a5784890151610796601f8916826106a3565b8355505b6001600288020188555050505b505050505050565b600082825260208201905092915050565b7f45524332303a206d696e7420746f20746865207a65726f206164647265737300600082015250565b60006107f6601f836107af565b9150610801826107c0565b602082019050919050565b60006020820190508181036000830152610825816107e9565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610866826105ab565b9150610871836105ab565b92508282019050808211156108895761088861082c565b5b92915050565b610898816105ab565b82525050565b60006020820190506108b3600083018461088f565b92915050565b611a91806108c86000396000f3fe608060405234801561001057600080fd5b506004361061012c5760003560e01c806370a08231116100ad578063a457c2d711610071578063a457c2d714610321578063a9059cbb14610351578063aa60b06b14610381578063dd62ed3e1461039d578063f2fde38b146103cd5761012c565b806370a082311461028f578063715018a6146102bf57806379cc6790146102c95780638da5cb5b146102e557806395d89b41146103035761012c565b806339509351116100f457806339509351146101eb57806342966c681461021b5780634c0abcdb146102375780634f6ef09a14610255578063670171fd146102715761012c565b806306fdde0314610131578063095ea7b31461014f57806318160ddd1461017f57806323b872dd1461019d578063313ce567146101cd575b600080fd5b6101396103e9565b60405161014691906110f5565b60405180910390f35b610169600480360381019061016491906111b0565b61047b565b604051610176919061120b565b60405180910390f35b61018761049e565b6040516101949190611235565b60405180910390f35b6101b760048036038101906101b29190611250565b6104a8565b6040516101c4919061120b565b60405180910390f35b6101d56104d7565b6040516101e291906112bf565b60405180910390f35b610205600480360381019061020091906111b0565b6104e0565b604051610212919061120b565b60405180910390f35b610235600480360381019061023091906112da565b610517565b005b61023f61052b565b60405161024c9190611235565b60405180910390f35b61026f600480360381019061026a9190611307565b61053b565b005b61027961055c565b6040516102869190611235565b60405180910390f35b6102a960048036038101906102a49190611307565b61056b565b6040516102b69190611235565b60405180910390f35b6102c76105b3565b005b6102e360048036038101906102de91906111b0565b6105c7565b005b6102ed6105d5565b6040516102fa9190611343565b60405180910390f35b61030b6105ff565b60405161031891906110f5565b60405180910390f35b61033b600480360381019061033691906111b0565b610691565b604051610348919061120b565b60405180910390f35b61036b600480360381019061036691906111b0565b610708565b604051610378919061120b565b60405180910390f35b61039b60048036038101906103969190611307565b61072b565b005b6103b760048036038101906103b2919061135e565b61074d565b6040516103c49190611235565b60405180910390f35b6103e760048036038101906103e29190611307565b6107d4565b005b6060600380546103f8906113cd565b80601f0160208091040260200160405190810160405280929190818152602001828054610424906113cd565b80156104715780601f1061044657610100808354040283529160200191610471565b820191906000526020600020905b81548152906001019060200180831161045457829003601f168201915b5050505050905090565b600080610486610857565b905061049381858561085f565b600191505092915050565b6000600254905090565b6000806104b3610857565b90506104c0858285610a28565b6104cb858585610ab4565b60019150509392505050565b60006012905090565b6000806104eb610857565b905061050c8185856104fd858961074d565b610507919061142d565b61085f565b600191505092915050565b610528610522610857565b82610d2a565b50565b6b0295be96e64066972000000081565b610543610ef7565b61055930826aa56fa5b99019a5c8000000610ab4565b50565b6aa56fa5b99019a5c800000081565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b6105bb610ef7565b6105c56000610f75565b565b6105d1828261103b565b5050565b6000600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b60606004805461060e906113cd565b80601f016020809104026020016040519081016040528092919081815260200182805461063a906113cd565b80156106875780601f1061065c57610100808354040283529160200191610687565b820191906000526020600020905b81548152906001019060200180831161066a57829003601f168201915b5050505050905090565b60008061069c610857565b905060006106aa828661074d565b9050838110156106ef576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016106e6906114d3565b60405180910390fd5b6106fc828686840361085f565b60019250505092915050565b600080610713610857565b9050610720818585610ab4565b600191505092915050565b610733610ef7565b61074a30826b0295be96e640669720000000610ab4565b50565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b6107dc610ef7565b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff160361084b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161084290611565565b60405180910390fd5b61085481610f75565b50565b600033905090565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16036108ce576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016108c5906115f7565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160361093d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161093490611689565b60405180910390fd5b80600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92583604051610a1b9190611235565b60405180910390a3505050565b6000610a34848461074d565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8114610aae5781811015610aa0576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610a97906116f5565b60405180910390fd5b610aad848484840361085f565b5b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610b23576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610b1a90611787565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603610b92576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610b8990611819565b60405180910390fd5b610b9d83838361105b565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905081811015610c23576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610c1a906118ab565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610d119190611235565b60405180910390a3610d24848484611060565b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603610d99576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610d909061193d565b60405180910390fd5b610da58260008361105b565b60008060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905081811015610e2b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610e22906119cf565b60405180910390fd5b8181036000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555081600260008282540392505081905550600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610ede9190611235565b60405180910390a3610ef283600084611060565b505050565b610eff610857565b73ffffffffffffffffffffffffffffffffffffffff16610f1d6105d5565b73ffffffffffffffffffffffffffffffffffffffff1614610f73576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610f6a90611a3b565b60405180910390fd5b565b6000600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905081600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508173ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35050565b61104d82611047610857565b83610a28565b6110578282610d2a565b5050565b505050565b505050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561109f578082015181840152602081019050611084565b60008484015250505050565b6000601f19601f8301169050919050565b60006110c782611065565b6110d18185611070565b93506110e1818560208601611081565b6110ea816110ab565b840191505092915050565b6000602082019050818103600083015261110f81846110bc565b905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006111478261111c565b9050919050565b6111578161113c565b811461116257600080fd5b50565b6000813590506111748161114e565b92915050565b6000819050919050565b61118d8161117a565b811461119857600080fd5b50565b6000813590506111aa81611184565b92915050565b600080604083850312156111c7576111c6611117565b5b60006111d585828601611165565b92505060206111e68582860161119b565b9150509250929050565b60008115159050919050565b611205816111f0565b82525050565b600060208201905061122060008301846111fc565b92915050565b61122f8161117a565b82525050565b600060208201905061124a6000830184611226565b92915050565b60008060006060848603121561126957611268611117565b5b600061127786828701611165565b935050602061128886828701611165565b92505060406112998682870161119b565b9150509250925092565b600060ff82169050919050565b6112b9816112a3565b82525050565b60006020820190506112d460008301846112b0565b92915050565b6000602082840312156112f0576112ef611117565b5b60006112fe8482850161119b565b91505092915050565b60006020828403121561131d5761131c611117565b5b600061132b84828501611165565b91505092915050565b61133d8161113c565b82525050565b60006020820190506113586000830184611334565b92915050565b6000806040838503121561137557611374611117565b5b600061138385828601611165565b925050602061139485828601611165565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806113e557607f821691505b6020821081036113f8576113f761139e565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006114388261117a565b91506114438361117a565b925082820190508082111561145b5761145a6113fe565b5b92915050565b7f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f7760008201527f207a65726f000000000000000000000000000000000000000000000000000000602082015250565b60006114bd602583611070565b91506114c882611461565b604082019050919050565b600060208201905081810360008301526114ec816114b0565b9050919050565b7f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160008201527f6464726573730000000000000000000000000000000000000000000000000000602082015250565b600061154f602683611070565b915061155a826114f3565b604082019050919050565b6000602082019050818103600083015261157e81611542565b9050919050565b7f45524332303a20617070726f76652066726f6d20746865207a65726f2061646460008201527f7265737300000000000000000000000000000000000000000000000000000000602082015250565b60006115e1602483611070565b91506115ec82611585565b604082019050919050565b60006020820190508181036000830152611610816115d4565b9050919050565b7f45524332303a20617070726f766520746f20746865207a65726f20616464726560008201527f7373000000000000000000000000000000000000000000000000000000000000602082015250565b6000611673602283611070565b915061167e82611617565b604082019050919050565b600060208201905081810360008301526116a281611666565b9050919050565b7f45524332303a20696e73756666696369656e7420616c6c6f77616e6365000000600082015250565b60006116df601d83611070565b91506116ea826116a9565b602082019050919050565b6000602082019050818103600083015261170e816116d2565b9050919050565b7f45524332303a207472616e736665722066726f6d20746865207a65726f20616460008201527f6472657373000000000000000000000000000000000000000000000000000000602082015250565b6000611771602583611070565b915061177c82611715565b604082019050919050565b600060208201905081810360008301526117a081611764565b9050919050565b7f45524332303a207472616e7366657220746f20746865207a65726f206164647260008201527f6573730000000000000000000000000000000000000000000000000000000000602082015250565b6000611803602383611070565b915061180e826117a7565b604082019050919050565b60006020820190508181036000830152611832816117f6565b9050919050565b7f45524332303a207472616e7366657220616d6f756e742065786365656473206260008201527f616c616e63650000000000000000000000000000000000000000000000000000602082015250565b6000611895602683611070565b91506118a082611839565b604082019050919050565b600060208201905081810360008301526118c481611888565b9050919050565b7f45524332303a206275726e2066726f6d20746865207a65726f2061646472657360008201527f7300000000000000000000000000000000000000000000000000000000000000602082015250565b6000611927602183611070565b9150611932826118cb565b604082019050919050565b600060208201905081810360008301526119568161191a565b9050919050565b7f45524332303a206275726e20616d6f756e7420657863656564732062616c616e60008201527f6365000000000000000000000000000000000000000000000000000000000000602082015250565b60006119b9602283611070565b91506119c48261195d565b604082019050919050565b600060208201905081810360008301526119e8816119ac565b9050919050565b7f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572600082015250565b6000611a25602083611070565b9150611a30826119ef565b602082019050919050565b60006020820190508181036000830152611a5481611a18565b905091905056fea264697066735822122029fb9e3976dc34ea8c2335e8453d923920168f0fce0d170c0de7cdf7757ea4d564736f6c634300081c0033a264697066735822122003ef549fe9f08b32a444d1bcde97eff783a786f1092241eeb863fbfd63cd3dae64736f6c634300081c0033
Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000042cc8bef111e05f478cdd37950f1c06b0348184a0000000000000000000000009c1b8deb6d89a795bc2e45f86a9af6e034744774
-----Decoded View---------------
Arg [0] : _uniRouter (address): 0x0000000000000000000000000000000000000000
Arg [1] : _bancorFormula (address): 0x42cc8bef111E05f478CDd37950F1c06B0348184a
Arg [2] : _feeRecipient (address): 0x9C1B8Deb6d89A795Bc2e45F86A9Af6e034744774
-----Encoded View---------------
3 Constructor Arguments found :
Arg [0] : 0000000000000000000000000000000000000000000000000000000000000000
Arg [1] : 00000000000000000000000042cc8bef111e05f478cdd37950f1c06b0348184a
Arg [2] : 0000000000000000000000009c1b8deb6d89a795bc2e45f86a9af6e034744774
Loading...
Loading
Loading...
Loading
Multichain Portfolio | 30 Chains
Chain | Token | Portfolio % | Price | Amount | Value |
---|---|---|---|---|---|
FTM | 100.00% | $1.08 | 18 | $19.44 |
[ Download: CSV Export ]
[ Download: CSV Export ]
A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.