Source Code
Overview
MON Balance
MON Value
$0.00View more zero value Internal Transactions in Advanced View mode
Advanced mode:
Loading...
Loading
This contract may be a proxy contract. Click on More Options and select Is this a proxy? to confirm and enable the "Read as Proxy" & "Write as Proxy" tabs.
Contract Name:
KuruAMMVault
Compiler Version
v0.8.30+commit.73712a01
Optimization Enabled:
Yes with 1000 runs
Other Settings:
prague EvmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: BUSL-1.1
// ░█████░ ░█████▓ ███
// ███████ ▓███████▒ ███▒
// ███████ █████████▒ ███▒
// ███████ ██████████░ ███▒ ████ ████ ████ ▓████████ ░███░ ░███▓
// ░████▒██████▒░ ███▒ ████ ████ ████ ▓████████ ▒███▒ ░███▓
// ███▒ ▒████ ████ ████ ▓████ ▒███░ ░███▓
// ▓████▒███████▓ █████████ ████ ████ ▓███░ ▒███░ ░███▓
// ███████ ██████████▒ █████████▒ ████ ████ ▓███ ░███▒ ░███▒
// ███████ █████████▒ ███▒ ████▓ ▓███▒ ░████ ▓███ ████ ████░
// ███████ ▓███████▒ ███▒ ▒████ ███████████ ▓███ ░██████████▒
// ▒▓▓▓▓ ▒▓▓▓▓▒ ▓██ ███▒ ░█████░ ██▓ ▒████▒
pragma solidity ^0.8.20;
// ============ External Imports ============
import {ReentrancyGuardTransient} from "@openzeppelin/contracts/utils/ReentrancyGuardTransient.sol";
import {ERC20} from "solady/src/tokens/ERC20.sol";
import {Initializable} from "solady/src/utils/Initializable.sol";
import {UUPSUpgradeable} from "solady/src/utils/UUPSUpgradeable.sol";
import {SafeTransferLib} from "solady/src/utils/SafeTransferLib.sol";
// ============ Internal Imports ============
import {FixedPointMathLib} from "./libraries/FixedPointMathLib.sol";
import {KuruAMMVaultErrors} from "./libraries/Errors.sol";
import {IMarginAccount} from "./interfaces/IMarginAccount.sol";
import {IKuruAMMVault} from "./interfaces/IKuruAMMVault.sol";
import {IOrderBook} from "./interfaces/IOrderBook.sol";
contract KuruAMMVault is IKuruAMMVault, ERC20, Initializable, UUPSUpgradeable, ReentrancyGuardTransient {
using SafeTransferLib for address;
using FixedPointMathLib for uint256;
uint256 constant vaultPricePrecision = 10 ** 18;
uint256 constant MIN_LIQUIDITY = 10 ** 3;
uint256 constant BPS_MULTIPLIER = 10000;
uint256 constant DOUBLE_BPS_MULTIPLIER = 20000;
address public owner;
address public token1;
uint256 private token1Decimals;
address public token2;
uint256 private token2Decimals;
IMarginAccount public marginAccount;
IOrderBook public market;
MarketParams public marketParams;
uint96 public SPREAD_CONSTANT;
string internal _name;
constructor() {
_disableInitializers();
}
// ============ initializer =================
function initialize(
address _owner,
address token1_,
address token2_,
address _marginAccount,
address _market,
uint96 _spreadConstant
) public initializer {
owner = _owner;
token1 = token1_;
token1Decimals = token1_ != address(0) ? ERC20(token1_).decimals() : 18;
token2 = token2_;
token2Decimals = token2_ != address(0) ? ERC20(token2_).decimals() : 18;
marginAccount = IMarginAccount(_marginAccount);
market = IOrderBook(_market);
SPREAD_CONSTANT = _spreadConstant;
setMarketParams();
if (token1_ != address(0)) {
token1_.safeApprove(_marginAccount, type(uint256).max);
}
if (token2_ != address(0)) {
token2_.safeApprove(_marginAccount, type(uint256).max);
}
string memory token1Symbol;
string memory token2Symbol;
if (token1_ != address(0)) {
token1Symbol = ERC20(token1_).symbol();
} else {
token1Symbol = "MON";
}
if (token2_ != address(0)) {
token2Symbol = ERC20(token2_).symbol();
} else {
token2Symbol = "MON";
}
_name = string.concat(token1Symbol, "-", token2Symbol, "-", "KURU-AMM-VAULT");
}
// ============ auth =================
function transferOwnership(address _newOwner) external {
_checkOwner();
owner = _newOwner;
}
function _checkOwner() internal view {
require(msg.sender == owner, KuruAMMVaultErrors.Unauthorized());
}
function _authorizeUpgrade(address) internal view override {
_checkOwner();
}
function name() public view virtual override returns (string memory) {
return _name;
}
function symbol() public view virtual override returns (string memory) {
return _name;
}
/**
* @dev Fetches and stores the market params from the market contract.
*/
function setMarketParams() public {
(
marketParams.pricePrecision,
marketParams.sizePrecision,
marketParams.baseAssetAddress,
marketParams.baseAssetDecimals,
marketParams.quoteAssetAddress,
marketParams.quoteAssetDecimals,
marketParams.tickSize,
marketParams.minSize,
marketParams.maxSize,
marketParams.takerFeeBps,
marketParams.makerFeeBps
) = market.getMarketParams();
}
/**
* @dev Previews the amount of shares to be minted for a given deposit of token1 and token2.
*/
function previewDeposit(uint256 asset1, uint256 asset2) public view virtual returns (uint256) {
(, uint256 _price) = _returnNormalizedAmountAndPrice(true);
return _convertToShares(asset1, asset2, _price);
}
/**
* @dev Previews the amount of token1 and token2 to be received for a given amount of shares to be minted.
*/
function previewMint(uint256 shares) public view virtual returns (uint256, uint256) {
return _convertToAssets(shares, true);
}
/**
* @dev Previews the amount of token1 and token2 to be received for a given amount of shares to be burned.
*/
function previewWithdraw(uint256 shares) public view virtual returns (uint256, uint256) {
return _convertToAssets(shares, false);
}
/**
* @dev Returns the total assets of the vault in token1 and token2.
*/
function totalAssets() public view returns (uint256, uint256) {
return (marginAccount.getBalance(address(this), token1), marginAccount.getBalance(address(this), token2));
}
/**
* @dev Deposits token1 and token2 into the vault and mints shares to the receiver.
*/
function deposit(uint256 baseDeposit, uint256 quoteDeposit, uint256 minQuoteConsumed, address receiver)
public
payable
nonReentrant
returns (uint256)
{
require(
(token1 == address(0) || token2 == address(0))
? msg.value >= (token1 == address(0) ? baseDeposit : quoteDeposit)
: msg.value == 0,
KuruAMMVaultErrors.NativeAssetMismatch()
);
uint256 _sharesMinted;
(_sharesMinted, quoteDeposit) = _mintAndDeposit(baseDeposit, quoteDeposit, receiver);
require(quoteDeposit >= minQuoteConsumed, KuruAMMVaultErrors.InsufficientQuoteUsed());
return _sharesMinted;
}
/**
* @dev Internal function to handle the deposit logic and minting of shares.
*/
function _mintAndDeposit(uint256 baseDeposit, uint256 quoteDeposit, address receiver)
internal
returns (uint256, uint256)
{
(uint256 _baseAmount, uint256 _currentAskPrice) = _returnNormalizedAmountAndPrice(true);
uint256 _shares;
if (totalSupply() != 0) {
uint256 _expectedQuoteAmount = (
(
FixedPointMathLib.mulDivUp(baseDeposit, _currentAskPrice, vaultPricePrecision)
* 10 ** marketParams.quoteAssetDecimals
)
) / 10 ** marketParams.baseAssetDecimals;
require(_expectedQuoteAmount <= quoteDeposit, KuruAMMVaultErrors.InsufficientQuoteToken());
_shares = _convertToShares(baseDeposit, _expectedQuoteAmount, _currentAskPrice);
quoteDeposit = _expectedQuoteAmount;
_mint(receiver, _shares);
} else {
_shares = FixedPointMathLib.sqrt(baseDeposit * quoteDeposit);
_mint(address(marginAccount), MIN_LIQUIDITY);
_shares -= MIN_LIQUIDITY;
_mint(receiver, _shares);
_currentAskPrice = (
((quoteDeposit * 10 ** marketParams.baseAssetDecimals)) * vaultPricePrecision
/ 10 ** marketParams.quoteAssetDecimals
) / (baseDeposit);
require(_currentAskPrice > 0, KuruAMMVaultErrors.AskPriceZero());
}
require(_shares > 0, KuruAMMVaultErrors.InsufficientLiquidityMinted());
(uint96 _newAskSize, uint96 _newBidSize) = _getVaultSizesForBaseAmount(_baseAmount + baseDeposit);
require(_newAskSize != 0, KuruAMMVaultErrors.InvalidVaultSize());
require(_newBidSize != 0, KuruAMMVaultErrors.InvalidVaultSize());
market.updateVaultOrdSz(
_newAskSize,
_newBidSize,
_currentAskPrice,
FixedPointMathLib.mulDivRound(_currentAskPrice, BPS_MULTIPLIER, BPS_MULTIPLIER + SPREAD_CONSTANT),
false
);
address _token1 = token1;
address _token2 = token2;
_depositAmountsToMarginAccount(_token1, _token2, baseDeposit, quoteDeposit);
uint256 _nativeRefund =
_token1 == address(0) ? (msg.value - baseDeposit) : (_token2 == address(0) ? (msg.value - quoteDeposit) : 0);
if (_nativeRefund > 0) {
msg.sender.safeTransferETH(_nativeRefund);
}
emit KuruVaultDeposit(baseDeposit, quoteDeposit, _shares, receiver);
return (_shares, quoteDeposit);
}
function _depositAmountsToMarginAccount(address _token1, address _token2, uint256 baseDeposit, uint256 quoteDeposit)
internal
{
if (_token1 == address(0)) {
marginAccount.deposit{value: baseDeposit}(address(this), _token1, baseDeposit);
} else {
_token1.safeTransferFrom(msg.sender, address(this), baseDeposit);
marginAccount.deposit(address(this), _token1, baseDeposit);
}
if (_token2 == address(0)) {
marginAccount.deposit{value: quoteDeposit}(address(this), _token2, quoteDeposit);
} else {
_token2.safeTransferFrom(msg.sender, address(this), quoteDeposit);
marginAccount.deposit(address(this), _token2, quoteDeposit);
}
}
/**
* @dev Withdraws token1 and token2 from the vault by burning the specified amount of shares.
*/
function withdraw(uint256 _shares, address _receiver, address _owner)
public
nonReentrant
returns (uint256, uint256)
{
require(_shares <= balanceOf(_owner), KuruAMMVaultErrors.InsufficientBalance());
if (msg.sender != _owner) {
_spendAllowance(_owner, msg.sender, _shares);
}
return _burnAndWithdraw(_shares, _receiver, _owner);
}
/**
* @dev Internal function to handle the burning of shares and withdrawal logic.
*/
function _burnAndWithdraw(uint256 _shares, address _receiver, address _owner) internal returns (uint256, uint256) {
(
uint256 _baseOwedToUser,
uint256 _quoteOwedToUser,
uint96 _newAskSize,
uint96 _newBidSize,
bool _nullifyPartialFills
) = _convertToAssetsWithNewSize(_shares);
_burn(_owner, _shares);
// we pass 0,0 as the prices because prices are only set on the first deposit
// since we burn 10 ** 3 shares, supply of total shares never goes to 0
market.updateVaultOrdSz(_newAskSize, _newBidSize, 0, 0, _nullifyPartialFills);
_withdrawFromMarginAccount(_baseOwedToUser, _quoteOwedToUser, _receiver);
emit KuruVaultWithdraw(_baseOwedToUser, _quoteOwedToUser, _shares, _owner);
return (_baseOwedToUser, _quoteOwedToUser);
}
/**
* @dev Internal function to handle withdrawals from the margin account.
*/
function _withdrawFromMarginAccount(uint256 baseWithdraw, uint256 quoteWithdraw, address receiver) internal {
marginAccount.withdraw(baseWithdraw, token1);
marginAccount.withdraw(quoteWithdraw, token2);
_transferTokensToUser(baseWithdraw, token1, receiver);
_transferTokensToUser(quoteWithdraw, token2, receiver);
}
/**
* @dev Internal function to transfer tokens to the receiver.
*/
function _transferTokensToUser(uint256 amount, address token, address receiver) internal {
if (token == address(0)) {
receiver.safeTransferETH(amount);
} else {
token.safeTransfer(receiver, amount);
}
}
/**
* @dev Mints shares to the receiver in exchange for depositing token1 and token2.
*/
function mint(uint256 shares, address receiver) public payable returns (uint256, uint256) {
(uint256 baseDeposit, uint256 quoteDeposit) = previewMint(shares);
deposit(baseDeposit, quoteDeposit, quoteDeposit, receiver);
return (baseDeposit, quoteDeposit);
}
/**
* @dev Returns normalized base asset amount without profits
*/
function _returnNormalizedAmountAndPrice(bool roundUp) internal view returns (uint256, uint256) {
// @audit what is the right way to round this value?
// ideally, should be rounded up during deposits and rounded down during withdrawals
uint96 _normalizedBaseAmount = roundUp
? toU96(
FixedPointMathLib.mulDivUp(
market.vaultAskOrderSize(), DOUBLE_BPS_MULTIPLIER + SPREAD_CONSTANT, SPREAD_CONSTANT
)
)
: toU96(
FixedPointMathLib.mulDiv(
market.vaultAskOrderSize(), DOUBLE_BPS_MULTIPLIER + SPREAD_CONSTANT, SPREAD_CONSTANT
)
);
uint256 _price = market.vaultBestAsk();
return ((_normalizedBaseAmount * 10 ** marketParams.baseAssetDecimals) / marketParams.sizePrecision, _price);
}
/**
* @dev Internal function to convert asset amounts to shares.
*/
function _convertToShares(uint256 baseAmount, uint256 quoteAmount, uint256 _currentAskPrice)
internal
view
returns (uint256)
{
(uint256 _reserve1, uint256 _reserve2) = totalAssets();
(_reserve1, _reserve2) = _returnVirtuallyRebalancedTotalAssets(_reserve1, _reserve2, _currentAskPrice);
return FixedPointMathLib.min(
FixedPointMathLib.mulDiv(baseAmount, totalSupply(), _reserve1),
FixedPointMathLib.mulDiv(quoteAmount, totalSupply(), _reserve2)
);
}
function _convertToAssets(uint256 shares, bool isDeposit)
internal
view
returns (uint256 _baseAmount, uint256 _quoteAmount)
{
if (isDeposit) {
if (totalSupply() == 0) {
return (0, 0);
}
(uint256 _baseReserve, uint256 _quoteReserve) = totalAssets();
(, uint256 _currentAskPrice) = _returnNormalizedAmountAndPrice(true);
(_baseReserve, _quoteReserve) =
_returnVirtuallyRebalancedTotalAssets(_baseReserve, _quoteReserve, _currentAskPrice);
_baseAmount = FixedPointMathLib.mulDiv(shares, _baseReserve, totalSupply());
_quoteAmount = (
(
FixedPointMathLib.mulDivUp(_baseAmount, _currentAskPrice, vaultPricePrecision)
* 10 ** marketParams.quoteAssetDecimals
)
) / 10 ** marketParams.baseAssetDecimals;
return (_baseAmount, _quoteAmount);
} else {
(_baseAmount, _quoteAmount,,,) = _convertToAssetsWithNewSize(shares);
}
}
function _convertToAssetsWithNewSize(uint256 shares)
internal
view
returns (uint256, uint256, uint96, uint96, bool)
{
(uint256 _baseAmount,) = _returnNormalizedAmountAndPrice(false);
uint256 _baseAmountAfterRemoval = _baseAmount - FixedPointMathLib.mulDiv(shares, _baseAmount, totalSupply());
(uint96 _newAskSize, uint96 _newBidSize) = _getVaultSizesForBaseAmount(_baseAmountAfterRemoval);
(
,
uint256 _vaultBestBid,
uint96 _partiallyFilledBidSize,
uint256 _vaultBestAsk,
uint96 _partiallyFilledAskSize,
,
,
) = market.getVaultParams();
MarketParams memory _marketParams = marketParams;
(uint256 _reserveBase, uint256 _reserveQuote) = totalAssets();
if (_partiallyFilledAskSize >= _newAskSize || _partiallyFilledBidSize >= _newBidSize) {
int256 _baseOwedToVault = (
int256(uint256(_partiallyFilledAskSize)) - int256(uint256(_partiallyFilledBidSize))
) * int256(10 ** _marketParams.baseAssetDecimals) / int256(uint256(_marketParams.sizePrecision));
int256 _quoteOwedToVault = (
int256(FixedPointMathLib.mulDivUp(_partiallyFilledBidSize, _vaultBestBid, _marketParams.sizePrecision))
- int256(FixedPointMathLib.mulDiv(_partiallyFilledAskSize, _vaultBestAsk, _marketParams.sizePrecision))
) * int256(10 ** _marketParams.quoteAssetDecimals) / int256(vaultPricePrecision);
uint256 _baseOwedToUser;
uint256 _quoteOwedToUser;
if (_baseOwedToVault < 0) {
_reserveBase = _reserveBase - (uint256(-1 * _baseOwedToVault));
_baseOwedToUser =
FixedPointMathLib.mulDiv(shares, _reserveBase, totalSupply()) + uint256(-1 * _baseOwedToVault);
} else {
_reserveBase = _reserveBase + (uint256(_baseOwedToVault));
_baseOwedToUser =
FixedPointMathLib.mulDiv(shares, _reserveBase, totalSupply()) - uint256(_baseOwedToVault);
}
if (_quoteOwedToVault < 0) {
_reserveQuote = _reserveQuote - (uint256(-1 * _quoteOwedToVault));
_quoteOwedToUser =
FixedPointMathLib.mulDiv(shares, _reserveQuote, totalSupply()) + uint256(-1 * _quoteOwedToVault);
} else {
_reserveQuote = _reserveQuote + (uint256(_quoteOwedToVault));
_quoteOwedToUser =
FixedPointMathLib.mulDiv(shares, _reserveQuote, totalSupply()) - uint256(_quoteOwedToVault);
}
return (_baseOwedToUser, _quoteOwedToUser, _newAskSize, _newBidSize, true);
} else {
uint256 _baseOwedToUser = FixedPointMathLib.mulDiv(shares, _reserveBase, totalSupply());
uint256 _quoteOwedToUser = FixedPointMathLib.mulDiv(shares, _reserveQuote, totalSupply());
return (_baseOwedToUser, _quoteOwedToUser, _newAskSize, _newBidSize, false);
}
}
function _getVaultSizesForBaseAmount(uint256 _baseAmount) internal view returns (uint96, uint96) {
MarketParams memory _marketParams = marketParams;
uint96 _newAskSize = toU96(
(SPREAD_CONSTANT * _baseAmount * _marketParams.sizePrecision)
/ ((DOUBLE_BPS_MULTIPLIER + SPREAD_CONSTANT) * 10 ** _marketParams.baseAssetDecimals)
);
uint96 _newBidSize = toU96(
(SPREAD_CONSTANT * _baseAmount * _marketParams.sizePrecision)
/ (DOUBLE_BPS_MULTIPLIER * 10 ** _marketParams.baseAssetDecimals)
);
return (_newAskSize, _newBidSize);
}
function _returnVirtuallyRebalancedTotalAssets(uint256 _reserve1, uint256 _reserve2, uint256 _vaultPrice)
internal
view
returns (uint256, uint256)
{
MarketParams memory _marketParams = marketParams;
uint256 _halfTotalValuationInQuote = (
(_reserve1 * _vaultPrice * 10 ** _marketParams.quoteAssetDecimals)
/ (10 ** _marketParams.baseAssetDecimals * vaultPricePrecision) + _reserve2
) / 2;
uint256 _rebalancedBaseAsset = (
_halfTotalValuationInQuote * vaultPricePrecision * 10 ** _marketParams.baseAssetDecimals
) / (10 ** _marketParams.quoteAssetDecimals * _vaultPrice);
return (_rebalancedBaseAsset, _halfTotalValuationInQuote);
}
function toU96(uint256 _from) internal pure returns (uint96 _to) {
require((_to = uint96(_from)) == _from, KuruAMMVaultErrors.Uint96Overflow());
}
receive() external payable {}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.3.0) (utils/ReentrancyGuardTransient.sol)
pragma solidity ^0.8.24;
import {TransientSlot} from "./TransientSlot.sol";
/**
* @dev Variant of {ReentrancyGuard} that uses transient storage.
*
* NOTE: This variant only works on networks where EIP-1153 is available.
*
* _Available since v5.1._
*/
abstract contract ReentrancyGuardTransient {
using TransientSlot for *;
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.ReentrancyGuard")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant REENTRANCY_GUARD_STORAGE =
0x9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00;
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
/**
* @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, REENTRANCY_GUARD_STORAGE.asBoolean().tload() will be false
if (_reentrancyGuardEntered()) {
revert ReentrancyGuardReentrantCall();
}
// Any calls to nonReentrant after this point will fail
REENTRANCY_GUARD_STORAGE.asBoolean().tstore(true);
}
function _nonReentrantAfter() private {
REENTRANCY_GUARD_STORAGE.asBoolean().tstore(false);
}
/**
* @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 REENTRANCY_GUARD_STORAGE.asBoolean().tload();
}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Simple ERC20 + EIP-2612 implementation.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/tokens/ERC20.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC20.sol)
/// @author Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol)
///
/// @dev Note:
/// - The ERC20 standard allows minting and transferring to and from the zero address,
/// minting and transferring zero tokens, as well as self-approvals.
/// For performance, this implementation WILL NOT revert for such actions.
/// Please add any checks with overrides if desired.
/// - The `permit` function uses the ecrecover precompile (0x1).
///
/// If you are overriding:
/// - NEVER violate the ERC20 invariant:
/// the total sum of all balances must be equal to `totalSupply()`.
/// - Check that the overridden function is actually used in the function you want to
/// change the behavior of. Much of the code has been manually inlined for performance.
abstract contract ERC20 {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The total supply has overflowed.
error TotalSupplyOverflow();
/// @dev The allowance has overflowed.
error AllowanceOverflow();
/// @dev The allowance has underflowed.
error AllowanceUnderflow();
/// @dev Insufficient balance.
error InsufficientBalance();
/// @dev Insufficient allowance.
error InsufficientAllowance();
/// @dev The permit is invalid.
error InvalidPermit();
/// @dev The permit has expired.
error PermitExpired();
/// @dev The allowance of Permit2 is fixed at infinity.
error Permit2AllowanceIsFixedAtInfinity();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EVENTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Emitted when `amount` tokens is transferred from `from` to `to`.
event Transfer(address indexed from, address indexed to, uint256 amount);
/// @dev Emitted when `amount` tokens is approved by `owner` to be used by `spender`.
event Approval(address indexed owner, address indexed spender, uint256 amount);
/// @dev `keccak256(bytes("Transfer(address,address,uint256)"))`.
uint256 private constant _TRANSFER_EVENT_SIGNATURE =
0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef;
/// @dev `keccak256(bytes("Approval(address,address,uint256)"))`.
uint256 private constant _APPROVAL_EVENT_SIGNATURE =
0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STORAGE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The storage slot for the total supply.
uint256 private constant _TOTAL_SUPPLY_SLOT = 0x05345cdf77eb68f44c;
/// @dev The balance slot of `owner` is given by:
/// ```
/// mstore(0x0c, _BALANCE_SLOT_SEED)
/// mstore(0x00, owner)
/// let balanceSlot := keccak256(0x0c, 0x20)
/// ```
uint256 private constant _BALANCE_SLOT_SEED = 0x87a211a2;
/// @dev The allowance slot of (`owner`, `spender`) is given by:
/// ```
/// mstore(0x20, spender)
/// mstore(0x0c, _ALLOWANCE_SLOT_SEED)
/// mstore(0x00, owner)
/// let allowanceSlot := keccak256(0x0c, 0x34)
/// ```
uint256 private constant _ALLOWANCE_SLOT_SEED = 0x7f5e9f20;
/// @dev The nonce slot of `owner` is given by:
/// ```
/// mstore(0x0c, _NONCES_SLOT_SEED)
/// mstore(0x00, owner)
/// let nonceSlot := keccak256(0x0c, 0x20)
/// ```
uint256 private constant _NONCES_SLOT_SEED = 0x38377508;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev `(_NONCES_SLOT_SEED << 16) | 0x1901`.
uint256 private constant _NONCES_SLOT_SEED_WITH_SIGNATURE_PREFIX = 0x383775081901;
/// @dev `keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")`.
bytes32 private constant _DOMAIN_TYPEHASH =
0x8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f;
/// @dev `keccak256("1")`.
/// If you need to use a different version, override `_versionHash`.
bytes32 private constant _DEFAULT_VERSION_HASH =
0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6;
/// @dev `keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)")`.
bytes32 private constant _PERMIT_TYPEHASH =
0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9;
/// @dev The canonical Permit2 address.
/// For signature-based allowance granting for single transaction ERC20 `transferFrom`.
/// Enabled by default. To disable, override `_givePermit2InfiniteAllowance()`.
/// [Github](https://github.com/Uniswap/permit2)
/// [Etherscan](https://etherscan.io/address/0x000000000022D473030F116dDEE9F6B43aC78BA3)
address internal constant _PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ERC20 METADATA */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns the name of the token.
function name() public view virtual returns (string memory);
/// @dev Returns the symbol of the token.
function symbol() public view virtual returns (string memory);
/// @dev Returns the decimals places of the token.
function decimals() public view virtual returns (uint8) {
return 18;
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ERC20 */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns the amount of tokens in existence.
function totalSupply() public view virtual returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
result := sload(_TOTAL_SUPPLY_SLOT)
}
}
/// @dev Returns the amount of tokens owned by `owner`.
function balanceOf(address owner) public view virtual returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x0c, _BALANCE_SLOT_SEED)
mstore(0x00, owner)
result := sload(keccak256(0x0c, 0x20))
}
}
/// @dev Returns the amount of tokens that `spender` can spend on behalf of `owner`.
function allowance(address owner, address spender)
public
view
virtual
returns (uint256 result)
{
if (_givePermit2InfiniteAllowance()) {
if (spender == _PERMIT2) return type(uint256).max;
}
/// @solidity memory-safe-assembly
assembly {
mstore(0x20, spender)
mstore(0x0c, _ALLOWANCE_SLOT_SEED)
mstore(0x00, owner)
result := sload(keccak256(0x0c, 0x34))
}
}
/// @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
///
/// Emits a {Approval} event.
function approve(address spender, uint256 amount) public virtual returns (bool) {
if (_givePermit2InfiniteAllowance()) {
/// @solidity memory-safe-assembly
assembly {
// If `spender == _PERMIT2 && amount != type(uint256).max`.
if iszero(or(xor(shr(96, shl(96, spender)), _PERMIT2), iszero(not(amount)))) {
mstore(0x00, 0x3f68539a) // `Permit2AllowanceIsFixedAtInfinity()`.
revert(0x1c, 0x04)
}
}
}
/// @solidity memory-safe-assembly
assembly {
// Compute the allowance slot and store the amount.
mstore(0x20, spender)
mstore(0x0c, _ALLOWANCE_SLOT_SEED)
mstore(0x00, caller())
sstore(keccak256(0x0c, 0x34), amount)
// Emit the {Approval} event.
mstore(0x00, amount)
log3(0x00, 0x20, _APPROVAL_EVENT_SIGNATURE, caller(), shr(96, mload(0x2c)))
}
return true;
}
/// @dev Transfer `amount` tokens from the caller to `to`.
///
/// Requirements:
/// - `from` must at least have `amount`.
///
/// Emits a {Transfer} event.
function transfer(address to, uint256 amount) public virtual returns (bool) {
_beforeTokenTransfer(msg.sender, to, amount);
/// @solidity memory-safe-assembly
assembly {
// Compute the balance slot and load its value.
mstore(0x0c, _BALANCE_SLOT_SEED)
mstore(0x00, caller())
let fromBalanceSlot := keccak256(0x0c, 0x20)
let fromBalance := sload(fromBalanceSlot)
// Revert if insufficient balance.
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated balance.
sstore(fromBalanceSlot, sub(fromBalance, amount))
// Compute the balance slot of `to`.
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x20)
// Add and store the updated balance of `to`.
// Will not overflow because the sum of all user balances
// cannot exceed the maximum uint256 value.
sstore(toBalanceSlot, add(sload(toBalanceSlot), amount))
// Emit the {Transfer} event.
mstore(0x20, amount)
log3(0x20, 0x20, _TRANSFER_EVENT_SIGNATURE, caller(), shr(96, mload(0x0c)))
}
_afterTokenTransfer(msg.sender, to, amount);
return true;
}
/// @dev Transfers `amount` tokens from `from` to `to`.
///
/// Note: Does not update the allowance if it is the maximum uint256 value.
///
/// Requirements:
/// - `from` must at least have `amount`.
/// - The caller must have at least `amount` of allowance to transfer the tokens of `from`.
///
/// Emits a {Transfer} event.
function transferFrom(address from, address to, uint256 amount) public virtual returns (bool) {
_beforeTokenTransfer(from, to, amount);
// Code duplication is for zero-cost abstraction if possible.
if (_givePermit2InfiniteAllowance()) {
/// @solidity memory-safe-assembly
assembly {
let from_ := shl(96, from)
if iszero(eq(caller(), _PERMIT2)) {
// Compute the allowance slot and load its value.
mstore(0x20, caller())
mstore(0x0c, or(from_, _ALLOWANCE_SLOT_SEED))
let allowanceSlot := keccak256(0x0c, 0x34)
let allowance_ := sload(allowanceSlot)
// If the allowance is not the maximum uint256 value.
if not(allowance_) {
// Revert if the amount to be transferred exceeds the allowance.
if gt(amount, allowance_) {
mstore(0x00, 0x13be252b) // `InsufficientAllowance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated allowance.
sstore(allowanceSlot, sub(allowance_, amount))
}
}
// Compute the balance slot and load its value.
mstore(0x0c, or(from_, _BALANCE_SLOT_SEED))
let fromBalanceSlot := keccak256(0x0c, 0x20)
let fromBalance := sload(fromBalanceSlot)
// Revert if insufficient balance.
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated balance.
sstore(fromBalanceSlot, sub(fromBalance, amount))
// Compute the balance slot of `to`.
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x20)
// Add and store the updated balance of `to`.
// Will not overflow because the sum of all user balances
// cannot exceed the maximum uint256 value.
sstore(toBalanceSlot, add(sload(toBalanceSlot), amount))
// Emit the {Transfer} event.
mstore(0x20, amount)
log3(0x20, 0x20, _TRANSFER_EVENT_SIGNATURE, shr(96, from_), shr(96, mload(0x0c)))
}
} else {
/// @solidity memory-safe-assembly
assembly {
let from_ := shl(96, from)
// Compute the allowance slot and load its value.
mstore(0x20, caller())
mstore(0x0c, or(from_, _ALLOWANCE_SLOT_SEED))
let allowanceSlot := keccak256(0x0c, 0x34)
let allowance_ := sload(allowanceSlot)
// If the allowance is not the maximum uint256 value.
if not(allowance_) {
// Revert if the amount to be transferred exceeds the allowance.
if gt(amount, allowance_) {
mstore(0x00, 0x13be252b) // `InsufficientAllowance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated allowance.
sstore(allowanceSlot, sub(allowance_, amount))
}
// Compute the balance slot and load its value.
mstore(0x0c, or(from_, _BALANCE_SLOT_SEED))
let fromBalanceSlot := keccak256(0x0c, 0x20)
let fromBalance := sload(fromBalanceSlot)
// Revert if insufficient balance.
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated balance.
sstore(fromBalanceSlot, sub(fromBalance, amount))
// Compute the balance slot of `to`.
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x20)
// Add and store the updated balance of `to`.
// Will not overflow because the sum of all user balances
// cannot exceed the maximum uint256 value.
sstore(toBalanceSlot, add(sload(toBalanceSlot), amount))
// Emit the {Transfer} event.
mstore(0x20, amount)
log3(0x20, 0x20, _TRANSFER_EVENT_SIGNATURE, shr(96, from_), shr(96, mload(0x0c)))
}
}
_afterTokenTransfer(from, to, amount);
return true;
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EIP-2612 */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev For more performance, override to return the constant value
/// of `keccak256(bytes(name()))` if `name()` will never change.
function _constantNameHash() internal view virtual returns (bytes32 result) {}
/// @dev If you need a different value, override this function.
function _versionHash() internal view virtual returns (bytes32 result) {
result = _DEFAULT_VERSION_HASH;
}
/// @dev For inheriting contracts to increment the nonce.
function _incrementNonce(address owner) internal virtual {
/// @solidity memory-safe-assembly
assembly {
mstore(0x0c, _NONCES_SLOT_SEED)
mstore(0x00, owner)
let nonceSlot := keccak256(0x0c, 0x20)
sstore(nonceSlot, add(1, sload(nonceSlot)))
}
}
/// @dev Returns the current nonce for `owner`.
/// This value is used to compute the signature for EIP-2612 permit.
function nonces(address owner) public view virtual returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
// Compute the nonce slot and load its value.
mstore(0x0c, _NONCES_SLOT_SEED)
mstore(0x00, owner)
result := sload(keccak256(0x0c, 0x20))
}
}
/// @dev Sets `value` as the allowance of `spender` over the tokens of `owner`,
/// authorized by a signed approval by `owner`.
///
/// Emits a {Approval} event.
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public virtual {
if (_givePermit2InfiniteAllowance()) {
/// @solidity memory-safe-assembly
assembly {
// If `spender == _PERMIT2 && value != type(uint256).max`.
if iszero(or(xor(shr(96, shl(96, spender)), _PERMIT2), iszero(not(value)))) {
mstore(0x00, 0x3f68539a) // `Permit2AllowanceIsFixedAtInfinity()`.
revert(0x1c, 0x04)
}
}
}
bytes32 nameHash = _constantNameHash();
// We simply calculate it on-the-fly to allow for cases where the `name` may change.
if (nameHash == bytes32(0)) nameHash = keccak256(bytes(name()));
bytes32 versionHash = _versionHash();
/// @solidity memory-safe-assembly
assembly {
// Revert if the block timestamp is greater than `deadline`.
if gt(timestamp(), deadline) {
mstore(0x00, 0x1a15a3cc) // `PermitExpired()`.
revert(0x1c, 0x04)
}
let m := mload(0x40) // Grab the free memory pointer.
// Clean the upper 96 bits.
owner := shr(96, shl(96, owner))
spender := shr(96, shl(96, spender))
// Compute the nonce slot and load its value.
mstore(0x0e, _NONCES_SLOT_SEED_WITH_SIGNATURE_PREFIX)
mstore(0x00, owner)
let nonceSlot := keccak256(0x0c, 0x20)
let nonceValue := sload(nonceSlot)
// Prepare the domain separator.
mstore(m, _DOMAIN_TYPEHASH)
mstore(add(m, 0x20), nameHash)
mstore(add(m, 0x40), versionHash)
mstore(add(m, 0x60), chainid())
mstore(add(m, 0x80), address())
mstore(0x2e, keccak256(m, 0xa0))
// Prepare the struct hash.
mstore(m, _PERMIT_TYPEHASH)
mstore(add(m, 0x20), owner)
mstore(add(m, 0x40), spender)
mstore(add(m, 0x60), value)
mstore(add(m, 0x80), nonceValue)
mstore(add(m, 0xa0), deadline)
mstore(0x4e, keccak256(m, 0xc0))
// Prepare the ecrecover calldata.
mstore(0x00, keccak256(0x2c, 0x42))
mstore(0x20, and(0xff, v))
mstore(0x40, r)
mstore(0x60, s)
let t := staticcall(gas(), 1, 0x00, 0x80, 0x20, 0x20)
// If the ecrecover fails, the returndatasize will be 0x00,
// `owner` will be checked if it equals the hash at 0x00,
// which evaluates to false (i.e. 0), and we will revert.
// If the ecrecover succeeds, the returndatasize will be 0x20,
// `owner` will be compared against the returned address at 0x20.
if iszero(eq(mload(returndatasize()), owner)) {
mstore(0x00, 0xddafbaef) // `InvalidPermit()`.
revert(0x1c, 0x04)
}
// Increment and store the updated nonce.
sstore(nonceSlot, add(nonceValue, t)) // `t` is 1 if ecrecover succeeds.
// Compute the allowance slot and store the value.
// The `owner` is already at slot 0x20.
mstore(0x40, or(shl(160, _ALLOWANCE_SLOT_SEED), spender))
sstore(keccak256(0x2c, 0x34), value)
// Emit the {Approval} event.
log3(add(m, 0x60), 0x20, _APPROVAL_EVENT_SIGNATURE, owner, spender)
mstore(0x40, m) // Restore the free memory pointer.
mstore(0x60, 0) // Restore the zero pointer.
}
}
/// @dev Returns the EIP-712 domain separator for the EIP-2612 permit.
function DOMAIN_SEPARATOR() public view virtual returns (bytes32 result) {
bytes32 nameHash = _constantNameHash();
// We simply calculate it on-the-fly to allow for cases where the `name` may change.
if (nameHash == bytes32(0)) nameHash = keccak256(bytes(name()));
bytes32 versionHash = _versionHash();
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Grab the free memory pointer.
mstore(m, _DOMAIN_TYPEHASH)
mstore(add(m, 0x20), nameHash)
mstore(add(m, 0x40), versionHash)
mstore(add(m, 0x60), chainid())
mstore(add(m, 0x80), address())
result := keccak256(m, 0xa0)
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL MINT FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Mints `amount` tokens to `to`, increasing the total supply.
///
/// Emits a {Transfer} event.
function _mint(address to, uint256 amount) internal virtual {
_beforeTokenTransfer(address(0), to, amount);
/// @solidity memory-safe-assembly
assembly {
let totalSupplyBefore := sload(_TOTAL_SUPPLY_SLOT)
let totalSupplyAfter := add(totalSupplyBefore, amount)
// Revert if the total supply overflows.
if lt(totalSupplyAfter, totalSupplyBefore) {
mstore(0x00, 0xe5cfe957) // `TotalSupplyOverflow()`.
revert(0x1c, 0x04)
}
// Store the updated total supply.
sstore(_TOTAL_SUPPLY_SLOT, totalSupplyAfter)
// Compute the balance slot and load its value.
mstore(0x0c, _BALANCE_SLOT_SEED)
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x20)
// Add and store the updated balance.
sstore(toBalanceSlot, add(sload(toBalanceSlot), amount))
// Emit the {Transfer} event.
mstore(0x20, amount)
log3(0x20, 0x20, _TRANSFER_EVENT_SIGNATURE, 0, shr(96, mload(0x0c)))
}
_afterTokenTransfer(address(0), to, amount);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL BURN FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Burns `amount` tokens from `from`, reducing the total supply.
///
/// Emits a {Transfer} event.
function _burn(address from, uint256 amount) internal virtual {
_beforeTokenTransfer(from, address(0), amount);
/// @solidity memory-safe-assembly
assembly {
// Compute the balance slot and load its value.
mstore(0x0c, _BALANCE_SLOT_SEED)
mstore(0x00, from)
let fromBalanceSlot := keccak256(0x0c, 0x20)
let fromBalance := sload(fromBalanceSlot)
// Revert if insufficient balance.
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated balance.
sstore(fromBalanceSlot, sub(fromBalance, amount))
// Subtract and store the updated total supply.
sstore(_TOTAL_SUPPLY_SLOT, sub(sload(_TOTAL_SUPPLY_SLOT), amount))
// Emit the {Transfer} event.
mstore(0x00, amount)
log3(0x00, 0x20, _TRANSFER_EVENT_SIGNATURE, shr(96, shl(96, from)), 0)
}
_afterTokenTransfer(from, address(0), amount);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL TRANSFER FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Moves `amount` of tokens from `from` to `to`.
function _transfer(address from, address to, uint256 amount) internal virtual {
_beforeTokenTransfer(from, to, amount);
/// @solidity memory-safe-assembly
assembly {
let from_ := shl(96, from)
// Compute the balance slot and load its value.
mstore(0x0c, or(from_, _BALANCE_SLOT_SEED))
let fromBalanceSlot := keccak256(0x0c, 0x20)
let fromBalance := sload(fromBalanceSlot)
// Revert if insufficient balance.
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated balance.
sstore(fromBalanceSlot, sub(fromBalance, amount))
// Compute the balance slot of `to`.
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x20)
// Add and store the updated balance of `to`.
// Will not overflow because the sum of all user balances
// cannot exceed the maximum uint256 value.
sstore(toBalanceSlot, add(sload(toBalanceSlot), amount))
// Emit the {Transfer} event.
mstore(0x20, amount)
log3(0x20, 0x20, _TRANSFER_EVENT_SIGNATURE, shr(96, from_), shr(96, mload(0x0c)))
}
_afterTokenTransfer(from, to, amount);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL ALLOWANCE FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Updates the allowance of `owner` for `spender` based on spent `amount`.
function _spendAllowance(address owner, address spender, uint256 amount) internal virtual {
if (_givePermit2InfiniteAllowance()) {
if (spender == _PERMIT2) return; // Do nothing, as allowance is infinite.
}
/// @solidity memory-safe-assembly
assembly {
// Compute the allowance slot and load its value.
mstore(0x20, spender)
mstore(0x0c, _ALLOWANCE_SLOT_SEED)
mstore(0x00, owner)
let allowanceSlot := keccak256(0x0c, 0x34)
let allowance_ := sload(allowanceSlot)
// If the allowance is not the maximum uint256 value.
if not(allowance_) {
// Revert if the amount to be transferred exceeds the allowance.
if gt(amount, allowance_) {
mstore(0x00, 0x13be252b) // `InsufficientAllowance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated allowance.
sstore(allowanceSlot, sub(allowance_, amount))
}
}
}
/// @dev Sets `amount` as the allowance of `spender` over the tokens of `owner`.
///
/// Emits a {Approval} event.
function _approve(address owner, address spender, uint256 amount) internal virtual {
if (_givePermit2InfiniteAllowance()) {
/// @solidity memory-safe-assembly
assembly {
// If `spender == _PERMIT2 && amount != type(uint256).max`.
if iszero(or(xor(shr(96, shl(96, spender)), _PERMIT2), iszero(not(amount)))) {
mstore(0x00, 0x3f68539a) // `Permit2AllowanceIsFixedAtInfinity()`.
revert(0x1c, 0x04)
}
}
}
/// @solidity memory-safe-assembly
assembly {
let owner_ := shl(96, owner)
// Compute the allowance slot and store the amount.
mstore(0x20, spender)
mstore(0x0c, or(owner_, _ALLOWANCE_SLOT_SEED))
sstore(keccak256(0x0c, 0x34), amount)
// Emit the {Approval} event.
mstore(0x00, amount)
log3(0x00, 0x20, _APPROVAL_EVENT_SIGNATURE, shr(96, owner_), shr(96, mload(0x2c)))
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* HOOKS TO OVERRIDE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Hook that is called before any transfer of tokens.
/// This includes minting and burning.
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.
function _afterTokenTransfer(address from, address to, uint256 amount) internal virtual {}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* PERMIT2 */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns whether to fix the Permit2 contract's allowance at infinity.
///
/// This value should be kept constant after contract initialization,
/// or else the actual allowance values may not match with the {Approval} events.
/// For best performance, return a compile-time constant for zero-cost abstraction.
function _givePermit2InfiniteAllowance() internal view virtual returns (bool) {
return true;
}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Initializable mixin for the upgradeable contracts.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/Initializable.sol)
/// @author Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/proxy/utils/Initializable.sol)
abstract contract Initializable {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The contract is already initialized.
error InvalidInitialization();
/// @dev The contract is not initializing.
error NotInitializing();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EVENTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Triggered when the contract has been initialized.
event Initialized(uint64 version);
/// @dev `keccak256(bytes("Initialized(uint64)"))`.
bytes32 private constant _INITIALIZED_EVENT_SIGNATURE =
0xc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d2;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STORAGE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The default initializable slot is given by:
/// `bytes32(~uint256(uint32(bytes4(keccak256("_INITIALIZABLE_SLOT")))))`.
///
/// Bits Layout:
/// - [0] `initializing`
/// - [1..64] `initializedVersion`
bytes32 private constant _INITIALIZABLE_SLOT =
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffbf601132;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTRUCTOR */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
constructor() {
// Construction time check to ensure that `_initializableSlot()` is not
// overridden to zero. Will be optimized away if there is no revert.
require(_initializableSlot() != bytes32(0));
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Override to return a non-zero custom storage slot if required.
function _initializableSlot() internal pure virtual returns (bytes32) {
return _INITIALIZABLE_SLOT;
}
/// @dev Guards an initializer function so that it can be invoked at most once.
///
/// You can guard a function with `onlyInitializing` such that it can be called
/// through a function guarded with `initializer`.
///
/// This is similar to `reinitializer(1)`, except that in the context of a constructor,
/// an `initializer` guarded function can be invoked multiple times.
/// This can be useful during testing and is not expected to be used in production.
///
/// Emits an {Initialized} event.
modifier initializer() virtual {
bytes32 s = _initializableSlot();
/// @solidity memory-safe-assembly
assembly {
let i := sload(s)
// Set `initializing` to 1, `initializedVersion` to 1.
sstore(s, 3)
// If `!(initializing == 0 && initializedVersion == 0)`.
if i {
// If `!(address(this).code.length == 0 && initializedVersion == 1)`.
if iszero(lt(extcodesize(address()), eq(shr(1, i), 1))) {
mstore(0x00, 0xf92ee8a9) // `InvalidInitialization()`.
revert(0x1c, 0x04)
}
s := shl(shl(255, i), s) // Skip initializing if `initializing == 1`.
}
}
_;
/// @solidity memory-safe-assembly
assembly {
if s {
// Set `initializing` to 0, `initializedVersion` to 1.
sstore(s, 2)
// Emit the {Initialized} event.
mstore(0x20, 1)
log1(0x20, 0x20, _INITIALIZED_EVENT_SIGNATURE)
}
}
}
/// @dev Guards a reinitializer function so that it can be invoked at most once.
///
/// You can guard a function with `onlyInitializing` such that it can be called
/// through a function guarded with `reinitializer`.
///
/// Emits an {Initialized} event.
modifier reinitializer(uint64 version) virtual {
bytes32 s = _initializableSlot();
/// @solidity memory-safe-assembly
assembly {
// Clean upper bits, and shift left by 1 to make space for the initializing bit.
version := shl(1, and(version, 0xffffffffffffffff))
let i := sload(s)
// If `initializing == 1 || initializedVersion >= version`.
if iszero(lt(and(i, 1), lt(i, version))) {
mstore(0x00, 0xf92ee8a9) // `InvalidInitialization()`.
revert(0x1c, 0x04)
}
// Set `initializing` to 1, `initializedVersion` to `version`.
sstore(s, or(1, version))
}
_;
/// @solidity memory-safe-assembly
assembly {
// Set `initializing` to 0, `initializedVersion` to `version`.
sstore(s, version)
// Emit the {Initialized} event.
mstore(0x20, shr(1, version))
log1(0x20, 0x20, _INITIALIZED_EVENT_SIGNATURE)
}
}
/// @dev Guards a function such that it can only be called in the scope
/// of a function guarded with `initializer` or `reinitializer`.
modifier onlyInitializing() virtual {
_checkInitializing();
_;
}
/// @dev Reverts if the contract is not initializing.
function _checkInitializing() internal view virtual {
bytes32 s = _initializableSlot();
/// @solidity memory-safe-assembly
assembly {
if iszero(and(1, sload(s))) {
mstore(0x00, 0xd7e6bcf8) // `NotInitializing()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Locks any future initializations by setting the initialized version to `2**64 - 1`.
///
/// Calling this in the constructor will prevent the contract from being initialized
/// or reinitialized. It is recommended to use this to lock implementation contracts
/// that are designed to be called through proxies.
///
/// Emits an {Initialized} event the first time it is successfully called.
function _disableInitializers() internal virtual {
bytes32 s = _initializableSlot();
/// @solidity memory-safe-assembly
assembly {
let i := sload(s)
if and(i, 1) {
mstore(0x00, 0xf92ee8a9) // `InvalidInitialization()`.
revert(0x1c, 0x04)
}
let uint64max := 0xffffffffffffffff
if iszero(eq(shr(1, i), uint64max)) {
// Set `initializing` to 0, `initializedVersion` to `2**64 - 1`.
sstore(s, shl(1, uint64max))
// Emit the {Initialized} event.
mstore(0x20, uint64max)
log1(0x20, 0x20, _INITIALIZED_EVENT_SIGNATURE)
}
}
}
/// @dev Returns the highest version that has been initialized.
function _getInitializedVersion() internal view virtual returns (uint64 version) {
bytes32 s = _initializableSlot();
/// @solidity memory-safe-assembly
assembly {
version := shr(1, sload(s))
}
}
/// @dev Returns whether the contract is currently initializing.
function _isInitializing() internal view virtual returns (bool result) {
bytes32 s = _initializableSlot();
/// @solidity memory-safe-assembly
assembly {
result := and(1, sload(s))
}
}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import {CallContextChecker} from "./CallContextChecker.sol";
/// @notice UUPS proxy mixin.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/UUPSUpgradeable.sol)
/// @author Modified from OpenZeppelin
/// (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/proxy/utils/UUPSUpgradeable.sol)
///
/// @dev Note:
/// - This implementation is intended to be used with ERC1967 proxies.
/// See: `LibClone.deployERC1967` and related functions.
/// - This implementation is NOT compatible with legacy OpenZeppelin proxies
/// which do not store the implementation at `_ERC1967_IMPLEMENTATION_SLOT`.
abstract contract UUPSUpgradeable is CallContextChecker {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The upgrade failed.
error UpgradeFailed();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EVENTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Emitted when the proxy's implementation is upgraded.
event Upgraded(address indexed implementation);
/// @dev `keccak256(bytes("Upgraded(address)"))`.
uint256 private constant _UPGRADED_EVENT_SIGNATURE =
0xbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STORAGE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The ERC-1967 storage slot for the implementation in the proxy.
/// `uint256(keccak256("eip1967.proxy.implementation")) - 1`.
bytes32 internal constant _ERC1967_IMPLEMENTATION_SLOT =
0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* UUPS OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Please override this function to check if `msg.sender` is authorized
/// to upgrade the proxy to `newImplementation`, reverting if not.
/// ```
/// function _authorizeUpgrade(address) internal override onlyOwner {}
/// ```
function _authorizeUpgrade(address newImplementation) internal virtual;
/// @dev Returns the storage slot used by the implementation,
/// as specified in [ERC1822](https://eips.ethereum.org/EIPS/eip-1822).
///
/// Note: The `notDelegated` modifier prevents accidental upgrades to
/// an implementation that is a proxy contract.
function proxiableUUID() public view virtual notDelegated returns (bytes32) {
// This function must always return `_ERC1967_IMPLEMENTATION_SLOT` to comply with ERC1967.
return _ERC1967_IMPLEMENTATION_SLOT;
}
/// @dev Upgrades the proxy's implementation to `newImplementation`.
/// Emits a {Upgraded} event.
///
/// Note: Passing in empty `data` skips the delegatecall to `newImplementation`.
function upgradeToAndCall(address newImplementation, bytes calldata data)
public
payable
virtual
onlyProxy
{
_authorizeUpgrade(newImplementation);
/// @solidity memory-safe-assembly
assembly {
newImplementation := shr(96, shl(96, newImplementation)) // Clears upper 96 bits.
mstore(0x00, returndatasize())
mstore(0x01, 0x52d1902d) // `proxiableUUID()`.
let s := _ERC1967_IMPLEMENTATION_SLOT
// Check if `newImplementation` implements `proxiableUUID` correctly.
if iszero(eq(mload(staticcall(gas(), newImplementation, 0x1d, 0x04, 0x01, 0x20)), s)) {
mstore(0x01, 0x55299b49) // `UpgradeFailed()`.
revert(0x1d, 0x04)
}
// Emit the {Upgraded} event.
log2(codesize(), 0x00, _UPGRADED_EVENT_SIGNATURE, newImplementation)
sstore(s, newImplementation) // Updates the implementation.
// Perform a delegatecall to `newImplementation` if `data` is non-empty.
if data.length {
// Forwards the `data` to `newImplementation` via delegatecall.
let m := mload(0x40)
calldatacopy(m, data.offset, data.length)
if iszero(delegatecall(gas(), newImplementation, m, data.length, codesize(), 0x00))
{
// Bubble up the revert if the call reverts.
returndatacopy(m, 0x00, returndatasize())
revert(m, returndatasize())
}
}
}
}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Safe ETH and ERC20 transfer library that gracefully handles missing return values.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/SafeTransferLib.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/SafeTransferLib.sol)
/// @author Permit2 operations from (https://github.com/Uniswap/permit2/blob/main/src/libraries/Permit2Lib.sol)
///
/// @dev Note:
/// - For ETH transfers, please use `forceSafeTransferETH` for DoS protection.
library SafeTransferLib {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The ETH transfer has failed.
error ETHTransferFailed();
/// @dev The ERC20 `transferFrom` has failed.
error TransferFromFailed();
/// @dev The ERC20 `transfer` has failed.
error TransferFailed();
/// @dev The ERC20 `approve` has failed.
error ApproveFailed();
/// @dev The ERC20 `totalSupply` query has failed.
error TotalSupplyQueryFailed();
/// @dev The Permit2 operation has failed.
error Permit2Failed();
/// @dev The Permit2 amount must be less than `2**160 - 1`.
error Permit2AmountOverflow();
/// @dev The Permit2 approve operation has failed.
error Permit2ApproveFailed();
/// @dev The Permit2 lockdown operation has failed.
error Permit2LockdownFailed();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Suggested gas stipend for contract receiving ETH that disallows any storage writes.
uint256 internal constant GAS_STIPEND_NO_STORAGE_WRITES = 2300;
/// @dev Suggested gas stipend for contract receiving ETH to perform a few
/// storage reads and writes, but low enough to prevent griefing.
uint256 internal constant GAS_STIPEND_NO_GRIEF = 100000;
/// @dev The unique EIP-712 domain separator for the DAI token contract.
bytes32 internal constant DAI_DOMAIN_SEPARATOR =
0xdbb8cf42e1ecb028be3f3dbc922e1d878b963f411dc388ced501601c60f7c6f7;
/// @dev The address for the WETH9 contract on Ethereum mainnet.
address internal constant WETH9 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
/// @dev The canonical Permit2 address.
/// [Github](https://github.com/Uniswap/permit2)
/// [Etherscan](https://etherscan.io/address/0x000000000022D473030F116dDEE9F6B43aC78BA3)
address internal constant PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
/// @dev The canonical address of the `SELFDESTRUCT` ETH mover.
/// See: https://gist.github.com/Vectorized/1cb8ad4cf393b1378e08f23f79bd99fa
/// [Etherscan](https://etherscan.io/address/0x00000000000073c48c8055bD43D1A53799176f0D)
address internal constant ETH_MOVER = 0x00000000000073c48c8055bD43D1A53799176f0D;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ETH OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
// If the ETH transfer MUST succeed with a reasonable gas budget, use the force variants.
//
// The regular variants:
// - Forwards all remaining gas to the target.
// - Reverts if the target reverts.
// - Reverts if the current contract has insufficient balance.
//
// The force variants:
// - Forwards with an optional gas stipend
// (defaults to `GAS_STIPEND_NO_GRIEF`, which is sufficient for most cases).
// - If the target reverts, or if the gas stipend is exhausted,
// creates a temporary contract to force send the ETH via `SELFDESTRUCT`.
// Future compatible with `SENDALL`: https://eips.ethereum.org/EIPS/eip-4758.
// - Reverts if the current contract has insufficient balance.
//
// The try variants:
// - Forwards with a mandatory gas stipend.
// - Instead of reverting, returns whether the transfer succeeded.
/// @dev Sends `amount` (in wei) ETH to `to`.
function safeTransferETH(address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
if iszero(call(gas(), to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Sends all the ETH in the current contract to `to`.
function safeTransferAllETH(address to) internal {
/// @solidity memory-safe-assembly
assembly {
// Transfer all the ETH and check if it succeeded or not.
if iszero(call(gas(), to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Force sends `amount` (in wei) ETH to `to`, with a `gasStipend`.
function forceSafeTransferETH(address to, uint256 amount, uint256 gasStipend) internal {
/// @solidity memory-safe-assembly
assembly {
if lt(selfbalance(), amount) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
if iszero(call(gasStipend, to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(amount, 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Force sends all the ETH in the current contract to `to`, with a `gasStipend`.
function forceSafeTransferAllETH(address to, uint256 gasStipend) internal {
/// @solidity memory-safe-assembly
assembly {
if iszero(call(gasStipend, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(selfbalance(), 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Force sends `amount` (in wei) ETH to `to`, with `GAS_STIPEND_NO_GRIEF`.
function forceSafeTransferETH(address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
if lt(selfbalance(), amount) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
if iszero(call(GAS_STIPEND_NO_GRIEF, to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(amount, 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Force sends all the ETH in the current contract to `to`, with `GAS_STIPEND_NO_GRIEF`.
function forceSafeTransferAllETH(address to) internal {
/// @solidity memory-safe-assembly
assembly {
// forgefmt: disable-next-item
if iszero(call(GAS_STIPEND_NO_GRIEF, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(selfbalance(), 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Sends `amount` (in wei) ETH to `to`, with a `gasStipend`.
function trySafeTransferETH(address to, uint256 amount, uint256 gasStipend)
internal
returns (bool success)
{
/// @solidity memory-safe-assembly
assembly {
success := call(gasStipend, to, amount, codesize(), 0x00, codesize(), 0x00)
}
}
/// @dev Sends all the ETH in the current contract to `to`, with a `gasStipend`.
function trySafeTransferAllETH(address to, uint256 gasStipend)
internal
returns (bool success)
{
/// @solidity memory-safe-assembly
assembly {
success := call(gasStipend, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)
}
}
/// @dev Force transfers ETH to `to`, without triggering the fallback (if any).
/// This method attempts to use a separate contract to send via `SELFDESTRUCT`,
/// and upon failure, deploys a minimal vault to accrue the ETH.
function safeMoveETH(address to, uint256 amount) internal returns (address vault) {
/// @solidity memory-safe-assembly
assembly {
to := shr(96, shl(96, to)) // Clean upper 96 bits.
for { let mover := ETH_MOVER } iszero(eq(to, address())) {} {
let selfBalanceBefore := selfbalance()
if or(lt(selfBalanceBefore, amount), eq(to, mover)) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
if extcodesize(mover) {
let balanceBefore := balance(to) // Check via delta, in case `SELFDESTRUCT` is bricked.
mstore(0x00, to)
pop(call(gas(), mover, amount, 0x00, 0x20, codesize(), 0x00))
// If `address(to).balance >= amount + balanceBefore`, skip vault workflow.
if iszero(lt(balance(to), add(amount, balanceBefore))) { break }
// Just in case `SELFDESTRUCT` is changed to not revert and do nothing.
if lt(selfBalanceBefore, selfbalance()) { invalid() }
}
let m := mload(0x40)
// If the mover is missing or bricked, deploy a minimal vault
// that withdraws all ETH to `to` when being called only by `to`.
// forgefmt: disable-next-item
mstore(add(m, 0x20), 0x33146025575b600160005260206000f35b3d3d3d3d47335af1601a5760003dfd)
mstore(m, or(to, shl(160, 0x6035600b3d3960353df3fe73)))
// Compute and store the bytecode hash.
mstore8(0x00, 0xff) // Write the prefix.
mstore(0x35, keccak256(m, 0x40))
mstore(0x01, shl(96, address())) // Deployer.
mstore(0x15, 0) // Salt.
vault := keccak256(0x00, 0x55)
pop(call(gas(), vault, amount, codesize(), 0x00, codesize(), 0x00))
// The vault returns a single word on success. Failure reverts with empty data.
if iszero(returndatasize()) {
if iszero(create2(0, m, 0x40, 0)) { revert(codesize(), codesize()) } // For gas estimation.
}
mstore(0x40, m) // Restore the free memory pointer.
break
}
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ERC20 OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
/// Reverts upon failure.
///
/// The `from` account must have at least `amount` approved for
/// the current contract to manage.
function safeTransferFrom(address token, address from, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x60, amount) // Store the `amount` argument.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x23b872dd000000000000000000000000) // `transferFrom(address,address,uint256)`.
let success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
///
/// The `from` account must have at least `amount` approved for the current contract to manage.
function trySafeTransferFrom(address token, address from, address to, uint256 amount)
internal
returns (bool success)
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x60, amount) // Store the `amount` argument.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x23b872dd000000000000000000000000) // `transferFrom(address,address,uint256)`.
success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
success := lt(or(iszero(extcodesize(token)), returndatasize()), success)
}
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends all of ERC20 `token` from `from` to `to`.
/// Reverts upon failure.
///
/// The `from` account must have their entire balance approved for the current contract to manage.
function safeTransferAllFrom(address token, address from, address to)
internal
returns (uint256 amount)
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x70a08231000000000000000000000000) // `balanceOf(address)`.
// Read the balance, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x1c, 0x24, 0x60, 0x20)
)
) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
mstore(0x00, 0x23b872dd) // `transferFrom(address,address,uint256)`.
amount := mload(0x60) // The `amount` is already at 0x60. We'll need to return it.
// Perform the transfer, reverting upon failure.
let success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends `amount` of ERC20 `token` from the current contract to `to`.
/// Reverts upon failure.
function safeTransfer(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.
// Perform the transfer, reverting upon failure.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sends all of ERC20 `token` from the current contract to `to`.
/// Reverts upon failure.
function safeTransferAll(address token, address to) internal returns (uint256 amount) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, 0x70a08231) // Store the function selector of `balanceOf(address)`.
mstore(0x20, address()) // Store the address of the current contract.
// Read the balance, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x1c, 0x24, 0x34, 0x20)
)
) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
mstore(0x14, to) // Store the `to` argument.
amount := mload(0x34) // The `amount` is already at 0x34. We'll need to return it.
mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.
// Perform the transfer, reverting upon failure.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sets `amount` of ERC20 `token` for `to` to manage on behalf of the current contract.
/// Reverts upon failure.
function safeApprove(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x3e3f8f73) // `ApproveFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sets `amount` of ERC20 `token` for `to` to manage on behalf of the current contract.
/// If the initial attempt to approve fails, attempts to reset the approved amount to zero,
/// then retries the approval again (some tokens, e.g. USDT, requires this).
/// Reverts upon failure.
function safeApproveWithRetry(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
// Perform the approval, retrying upon failure.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x34, 0) // Store 0 for the `amount`.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
pop(call(gas(), token, 0, 0x10, 0x44, codesize(), 0x00)) // Reset the approval.
mstore(0x34, amount) // Store back the original `amount`.
// Retry the approval, reverting upon failure.
success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
// Check the `extcodesize` again just in case the token selfdestructs lol.
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x3e3f8f73) // `ApproveFailed()`.
revert(0x1c, 0x04)
}
}
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Returns the amount of ERC20 `token` owned by `account`.
/// Returns zero if the `token` does not exist.
function balanceOf(address token, address account) internal view returns (uint256 amount) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, account) // Store the `account` argument.
mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`.
amount :=
mul( // The arguments of `mul` are evaluated from right to left.
mload(0x20),
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20)
)
)
}
}
/// @dev Performs a `token.balanceOf(account)` check.
/// `implemented` denotes whether the `token` does not implement `balanceOf`.
/// `amount` is zero if the `token` does not implement `balanceOf`.
function checkBalanceOf(address token, address account)
internal
view
returns (bool implemented, uint256 amount)
{
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, account) // Store the `account` argument.
mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`.
implemented :=
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20)
)
amount := mul(mload(0x20), implemented)
}
}
/// @dev Returns the total supply of the `token`.
/// Reverts if the token does not exist or does not implement `totalSupply()`.
function totalSupply(address token) internal view returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, 0x18160ddd) // `totalSupply()`.
if iszero(
and(gt(returndatasize(), 0x1f), staticcall(gas(), token, 0x1c, 0x04, 0x00, 0x20))
) {
mstore(0x00, 0x54cd9435) // `TotalSupplyQueryFailed()`.
revert(0x1c, 0x04)
}
result := mload(0x00)
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
/// If the initial attempt fails, try to use Permit2 to transfer the token.
/// Reverts upon failure.
///
/// The `from` account must have at least `amount` approved for the current contract to manage.
function safeTransferFrom2(address token, address from, address to, uint256 amount) internal {
if (!trySafeTransferFrom(token, from, to, amount)) {
permit2TransferFrom(token, from, to, amount);
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to` via Permit2.
/// Reverts upon failure.
function permit2TransferFrom(address token, address from, address to, uint256 amount)
internal
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(add(m, 0x74), shr(96, shl(96, token)))
mstore(add(m, 0x54), amount)
mstore(add(m, 0x34), to)
mstore(add(m, 0x20), shl(96, from))
// `transferFrom(address,address,uint160,address)`.
mstore(m, 0x36c78516000000000000000000000000)
let p := PERMIT2
let exists := eq(chainid(), 1)
if iszero(exists) { exists := iszero(iszero(extcodesize(p))) }
if iszero(
and(
call(gas(), p, 0, add(m, 0x10), 0x84, codesize(), 0x00),
lt(iszero(extcodesize(token)), exists) // Token has code and Permit2 exists.
)
) {
mstore(0x00, 0x7939f4248757f0fd) // `TransferFromFailed()` or `Permit2AmountOverflow()`.
revert(add(0x18, shl(2, iszero(iszero(shr(160, amount))))), 0x04)
}
}
}
/// @dev Permit a user to spend a given amount of
/// another user's tokens via native EIP-2612 permit if possible, falling
/// back to Permit2 if native permit fails or is not implemented on the token.
function permit2(
address token,
address owner,
address spender,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
bool success;
/// @solidity memory-safe-assembly
assembly {
for {} shl(96, xor(token, WETH9)) {} {
mstore(0x00, 0x3644e515) // `DOMAIN_SEPARATOR()`.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
lt(iszero(mload(0x00)), eq(returndatasize(), 0x20)), // Returns 1 non-zero word.
// Gas stipend to limit gas burn for tokens that don't refund gas when
// an non-existing function is called. 5K should be enough for a SLOAD.
staticcall(5000, token, 0x1c, 0x04, 0x00, 0x20)
)
) { break }
// After here, we can be sure that token is a contract.
let m := mload(0x40)
mstore(add(m, 0x34), spender)
mstore(add(m, 0x20), shl(96, owner))
mstore(add(m, 0x74), deadline)
if eq(mload(0x00), DAI_DOMAIN_SEPARATOR) {
mstore(0x14, owner)
mstore(0x00, 0x7ecebe00000000000000000000000000) // `nonces(address)`.
mstore(
add(m, 0x94),
lt(iszero(amount), staticcall(gas(), token, 0x10, 0x24, add(m, 0x54), 0x20))
)
mstore(m, 0x8fcbaf0c000000000000000000000000) // `IDAIPermit.permit`.
// `nonces` is already at `add(m, 0x54)`.
// `amount != 0` is already stored at `add(m, 0x94)`.
mstore(add(m, 0xb4), and(0xff, v))
mstore(add(m, 0xd4), r)
mstore(add(m, 0xf4), s)
success := call(gas(), token, 0, add(m, 0x10), 0x104, codesize(), 0x00)
break
}
mstore(m, 0xd505accf000000000000000000000000) // `IERC20Permit.permit`.
mstore(add(m, 0x54), amount)
mstore(add(m, 0x94), and(0xff, v))
mstore(add(m, 0xb4), r)
mstore(add(m, 0xd4), s)
success := call(gas(), token, 0, add(m, 0x10), 0xe4, codesize(), 0x00)
break
}
}
if (!success) simplePermit2(token, owner, spender, amount, deadline, v, r, s);
}
/// @dev Simple permit on the Permit2 contract.
function simplePermit2(
address token,
address owner,
address spender,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(m, 0x927da105) // `allowance(address,address,address)`.
{
let addressMask := shr(96, not(0))
mstore(add(m, 0x20), and(addressMask, owner))
mstore(add(m, 0x40), and(addressMask, token))
mstore(add(m, 0x60), and(addressMask, spender))
mstore(add(m, 0xc0), and(addressMask, spender))
}
let p := mul(PERMIT2, iszero(shr(160, amount)))
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x5f), // Returns 3 words: `amount`, `expiration`, `nonce`.
staticcall(gas(), p, add(m, 0x1c), 0x64, add(m, 0x60), 0x60)
)
) {
mstore(0x00, 0x6b836e6b8757f0fd) // `Permit2Failed()` or `Permit2AmountOverflow()`.
revert(add(0x18, shl(2, iszero(p))), 0x04)
}
mstore(m, 0x2b67b570) // `Permit2.permit` (PermitSingle variant).
// `owner` is already `add(m, 0x20)`.
// `token` is already at `add(m, 0x40)`.
mstore(add(m, 0x60), amount)
mstore(add(m, 0x80), 0xffffffffffff) // `expiration = type(uint48).max`.
// `nonce` is already at `add(m, 0xa0)`.
// `spender` is already at `add(m, 0xc0)`.
mstore(add(m, 0xe0), deadline)
mstore(add(m, 0x100), 0x100) // `signature` offset.
mstore(add(m, 0x120), 0x41) // `signature` length.
mstore(add(m, 0x140), r)
mstore(add(m, 0x160), s)
mstore(add(m, 0x180), shl(248, v))
if iszero( // Revert if token does not have code, or if the call fails.
mul(extcodesize(token), call(gas(), p, 0, add(m, 0x1c), 0x184, codesize(), 0x00))) {
mstore(0x00, 0x6b836e6b) // `Permit2Failed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Approves `spender` to spend `amount` of `token` for `address(this)`.
function permit2Approve(address token, address spender, uint160 amount, uint48 expiration)
internal
{
/// @solidity memory-safe-assembly
assembly {
let addressMask := shr(96, not(0))
let m := mload(0x40)
mstore(m, 0x87517c45) // `approve(address,address,uint160,uint48)`.
mstore(add(m, 0x20), and(addressMask, token))
mstore(add(m, 0x40), and(addressMask, spender))
mstore(add(m, 0x60), and(addressMask, amount))
mstore(add(m, 0x80), and(0xffffffffffff, expiration))
if iszero(call(gas(), PERMIT2, 0, add(m, 0x1c), 0xa0, codesize(), 0x00)) {
mstore(0x00, 0x324f14ae) // `Permit2ApproveFailed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Revokes an approval for `token` and `spender` for `address(this)`.
function permit2Lockdown(address token, address spender) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(m, 0xcc53287f) // `Permit2.lockdown`.
mstore(add(m, 0x20), 0x20) // Offset of the `approvals`.
mstore(add(m, 0x40), 1) // `approvals.length`.
mstore(add(m, 0x60), shr(96, shl(96, token)))
mstore(add(m, 0x80), shr(96, shl(96, spender)))
if iszero(call(gas(), PERMIT2, 0, add(m, 0x1c), 0xa0, codesize(), 0x00)) {
mstore(0x00, 0x96b3de23) // `Permit2LockdownFailed()`.
revert(0x1c, 0x04)
}
}
}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Arithmetic library with operations for fixed-point numbers.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/FixedPointMathLib.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/FixedPointMathLib.sol)
library FixedPointMathLib {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The operation failed, as the output exceeds the maximum value of uint256.
error ExpOverflow();
/// @dev The operation failed, as the output exceeds the maximum value of uint256.
error FactorialOverflow();
/// @dev The operation failed, due to an overflow.
error RPowOverflow();
/// @dev The mantissa is too big to fit.
error MantissaOverflow();
/// @dev The operation failed, due to an multiplication overflow.
error MulWadFailed();
/// @dev The operation failed, due to an multiplication overflow.
error SMulWadFailed();
/// @dev The operation failed, either due to a multiplication overflow, or a division by a zero.
error DivWadFailed();
/// @dev The operation failed, either due to a multiplication overflow, or a division by a zero.
error SDivWadFailed();
/// @dev The operation failed, either due to a multiplication overflow, or a division by a zero.
error MulDivFailed();
/// @dev The division failed, as the denominator is zero.
error DivFailed();
/// @dev The full precision multiply-divide operation failed, either due
/// to the result being larger than 256 bits, or a division by a zero.
error FullMulDivFailed();
/// @dev The output is undefined, as the input is less-than-or-equal to zero.
error LnWadUndefined();
/// @dev The input outside the acceptable domain.
error OutOfDomain();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The scalar of ETH and most ERC20s.
uint256 internal constant WAD = 1e18;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* SIMPLIFIED FIXED POINT OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Equivalent to `(x * y) / WAD` rounded down.
function mulWad(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Equivalent to `require(y == 0 || x <= type(uint256).max / y)`.
if mul(y, gt(x, div(not(0), y))) {
mstore(0x00, 0xbac65e5b) // `MulWadFailed()`.
revert(0x1c, 0x04)
}
z := div(mul(x, y), WAD)
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded down.
function sMulWad(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(x, y)
// Equivalent to `require((x == 0 || z / x == y) && !(x == -1 && y == type(int256).min))`.
if iszero(gt(or(iszero(x), eq(sdiv(z, x), y)), lt(not(x), eq(y, shl(255, 1))))) {
mstore(0x00, 0xedcd4dd4) // `SMulWadFailed()`.
revert(0x1c, 0x04)
}
z := sdiv(z, WAD)
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded down, but without overflow checks.
function rawMulWad(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := div(mul(x, y), WAD)
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded down, but without overflow checks.
function rawSMulWad(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := sdiv(mul(x, y), WAD)
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded up.
function mulWadUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Equivalent to `require(y == 0 || x <= type(uint256).max / y)`.
if mul(y, gt(x, div(not(0), y))) {
mstore(0x00, 0xbac65e5b) // `MulWadFailed()`.
revert(0x1c, 0x04)
}
z := add(iszero(iszero(mod(mul(x, y), WAD))), div(mul(x, y), WAD))
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded up, but without overflow checks.
function rawMulWadUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := add(iszero(iszero(mod(mul(x, y), WAD))), div(mul(x, y), WAD))
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded down.
function divWad(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Equivalent to `require(y != 0 && (WAD == 0 || x <= type(uint256).max / WAD))`.
if iszero(mul(y, iszero(mul(WAD, gt(x, div(not(0), WAD)))))) {
mstore(0x00, 0x7c5f487d) // `DivWadFailed()`.
revert(0x1c, 0x04)
}
z := div(mul(x, WAD), y)
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded down.
function sDivWad(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(x, WAD)
// Equivalent to `require(y != 0 && ((x * WAD) / WAD == x))`.
if iszero(and(iszero(iszero(y)), eq(sdiv(z, WAD), x))) {
mstore(0x00, 0x5c43740d) // `SDivWadFailed()`.
revert(0x1c, 0x04)
}
z := sdiv(mul(x, WAD), y)
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded down, but without overflow and divide by zero checks.
function rawDivWad(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := div(mul(x, WAD), y)
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded down, but without overflow and divide by zero checks.
function rawSDivWad(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := sdiv(mul(x, WAD), y)
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded up.
function divWadUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Equivalent to `require(y != 0 && (WAD == 0 || x <= type(uint256).max / WAD))`.
if iszero(mul(y, iszero(mul(WAD, gt(x, div(not(0), WAD)))))) {
mstore(0x00, 0x7c5f487d) // `DivWadFailed()`.
revert(0x1c, 0x04)
}
z := add(iszero(iszero(mod(mul(x, WAD), y))), div(mul(x, WAD), y))
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded up, but without overflow and divide by zero checks.
function rawDivWadUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := add(iszero(iszero(mod(mul(x, WAD), y))), div(mul(x, WAD), y))
}
}
/// @dev Equivalent to `x` to the power of `y`.
/// because `x ** y = (e ** ln(x)) ** y = e ** (ln(x) * y)`.
/// Note: This function is an approximation.
function powWad(int256 x, int256 y) internal pure returns (int256) {
// Using `ln(x)` means `x` must be greater than 0.
return expWad((lnWad(x) * y) / int256(WAD));
}
/// @dev Returns `exp(x)`, denominated in `WAD`.
/// Credit to Remco Bloemen under MIT license: https://2π.com/22/exp-ln
/// Note: This function is an approximation. Monotonically increasing.
function expWad(int256 x) internal pure returns (int256 r) {
unchecked {
// When the result is less than 0.5 we return zero.
// This happens when `x <= (log(1e-18) * 1e18) ~ -4.15e19`.
if (x <= -41446531673892822313) return r;
/// @solidity memory-safe-assembly
assembly {
// When the result is greater than `(2**255 - 1) / 1e18` we can not represent it as
// an int. This happens when `x >= floor(log((2**255 - 1) / 1e18) * 1e18) ≈ 135`.
if iszero(slt(x, 135305999368893231589)) {
mstore(0x00, 0xa37bfec9) // `ExpOverflow()`.
revert(0x1c, 0x04)
}
}
// `x` is now in the range `(-42, 136) * 1e18`. Convert to `(-42, 136) * 2**96`
// for more intermediate precision and a binary basis. This base conversion
// is a multiplication by 1e18 / 2**96 = 5**18 / 2**78.
x = (x << 78) / 5 ** 18;
// Reduce range of x to (-½ ln 2, ½ ln 2) * 2**96 by factoring out powers
// of two such that exp(x) = exp(x') * 2**k, where k is an integer.
// Solving this gives k = round(x / log(2)) and x' = x - k * log(2).
int256 k = ((x << 96) / 54916777467707473351141471128 + 2 ** 95) >> 96;
x = x - k * 54916777467707473351141471128;
// `k` is in the range `[-61, 195]`.
// Evaluate using a (6, 7)-term rational approximation.
// `p` is made monic, we'll multiply by a scale factor later.
int256 y = x + 1346386616545796478920950773328;
y = ((y * x) >> 96) + 57155421227552351082224309758442;
int256 p = y + x - 94201549194550492254356042504812;
p = ((p * y) >> 96) + 28719021644029726153956944680412240;
p = p * x + (4385272521454847904659076985693276 << 96);
// We leave `p` in `2**192` basis so we don't need to scale it back up for the division.
int256 q = x - 2855989394907223263936484059900;
q = ((q * x) >> 96) + 50020603652535783019961831881945;
q = ((q * x) >> 96) - 533845033583426703283633433725380;
q = ((q * x) >> 96) + 3604857256930695427073651918091429;
q = ((q * x) >> 96) - 14423608567350463180887372962807573;
q = ((q * x) >> 96) + 26449188498355588339934803723976023;
/// @solidity memory-safe-assembly
assembly {
// Div in assembly because solidity adds a zero check despite the unchecked.
// The q polynomial won't have zeros in the domain as all its roots are complex.
// No scaling is necessary because p is already `2**96` too large.
r := sdiv(p, q)
}
// r should be in the range `(0.09, 0.25) * 2**96`.
// We now need to multiply r by:
// - The scale factor `s ≈ 6.031367120`.
// - The `2**k` factor from the range reduction.
// - The `1e18 / 2**96` factor for base conversion.
// We do this all at once, with an intermediate result in `2**213`
// basis, so the final right shift is always by a positive amount.
r = int256((uint256(r) * 3822833074963236453042738258902158003155416615667) >> uint256(195 - k));
}
}
/// @dev Returns `ln(x)`, denominated in `WAD`.
/// Credit to Remco Bloemen under MIT license: https://2π.com/22/exp-ln
/// Note: This function is an approximation. Monotonically increasing.
function lnWad(int256 x) internal pure returns (int256 r) {
/// @solidity memory-safe-assembly
assembly {
// We want to convert `x` from `10**18` fixed point to `2**96` fixed point.
// We do this by multiplying by `2**96 / 10**18`. But since
// `ln(x * C) = ln(x) + ln(C)`, we can simply do nothing here
// and add `ln(2**96 / 10**18)` at the end.
// Compute `k = log2(x) - 96`, `r = 159 - k = 255 - log2(x) = 255 ^ log2(x)`.
r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffff, shr(r, x))))
r := or(r, shl(3, lt(0xff, shr(r, x))))
// We place the check here for more optimal stack operations.
if iszero(sgt(x, 0)) {
mstore(0x00, 0x1615e638) // `LnWadUndefined()`.
revert(0x1c, 0x04)
}
// forgefmt: disable-next-item
r := xor(r, byte(and(0x1f, shr(shr(r, x), 0x8421084210842108cc6318c6db6d54be)),
0xf8f9f9faf9fdfafbf9fdfcfdfafbfcfef9fafdfafcfcfbfefafafcfbffffffff))
// Reduce range of x to (1, 2) * 2**96
// ln(2^k * x) = k * ln(2) + ln(x)
x := shr(159, shl(r, x))
// Evaluate using a (8, 8)-term rational approximation.
// `p` is made monic, we will multiply by a scale factor later.
// forgefmt: disable-next-item
let p := sub( // This heavily nested expression is to avoid stack-too-deep for via-ir.
sar(96, mul(add(43456485725739037958740375743393,
sar(96, mul(add(24828157081833163892658089445524,
sar(96, mul(add(3273285459638523848632254066296,
x), x))), x))), x)), 11111509109440967052023855526967)
p := sub(sar(96, mul(p, x)), 45023709667254063763336534515857)
p := sub(sar(96, mul(p, x)), 14706773417378608786704636184526)
p := sub(mul(p, x), shl(96, 795164235651350426258249787498))
// We leave `p` in `2**192` basis so we don't need to scale it back up for the division.
// `q` is monic by convention.
let q := add(5573035233440673466300451813936, x)
q := add(71694874799317883764090561454958, sar(96, mul(x, q)))
q := add(283447036172924575727196451306956, sar(96, mul(x, q)))
q := add(401686690394027663651624208769553, sar(96, mul(x, q)))
q := add(204048457590392012362485061816622, sar(96, mul(x, q)))
q := add(31853899698501571402653359427138, sar(96, mul(x, q)))
q := add(909429971244387300277376558375, sar(96, mul(x, q)))
// `p / q` is in the range `(0, 0.125) * 2**96`.
// Finalization, we need to:
// - Multiply by the scale factor `s = 5.549…`.
// - Add `ln(2**96 / 10**18)`.
// - Add `k * ln(2)`.
// - Multiply by `10**18 / 2**96 = 5**18 >> 78`.
// The q polynomial is known not to have zeros in the domain.
// No scaling required because p is already `2**96` too large.
p := sdiv(p, q)
// Multiply by the scaling factor: `s * 5**18 * 2**96`, base is now `5**18 * 2**192`.
p := mul(1677202110996718588342820967067443963516166, p)
// Add `ln(2) * k * 5**18 * 2**192`.
// forgefmt: disable-next-item
p := add(mul(16597577552685614221487285958193947469193820559219878177908093499208371, sub(159, r)), p)
// Add `ln(2**96 / 10**18) * 5**18 * 2**192`.
p := add(600920179829731861736702779321621459595472258049074101567377883020018308, p)
// Base conversion: mul `2**18 / 2**192`.
r := sar(174, p)
}
}
/// @dev Returns `W_0(x)`, denominated in `WAD`.
/// See: https://en.wikipedia.org/wiki/Lambert_W_function
/// a.k.a. Product log function. This is an approximation of the principal branch.
/// Note: This function is an approximation. Monotonically increasing.
function lambertW0Wad(int256 x) internal pure returns (int256 w) {
// forgefmt: disable-next-item
unchecked {
if ((w = x) <= -367879441171442322) revert OutOfDomain(); // `x` less than `-1/e`.
int256 wad = int256(WAD);
int256 p = x;
uint256 c; // Whether we need to avoid catastrophic cancellation.
uint256 i = 4; // Number of iterations.
if (w <= 0x1ffffffffffff) {
if (-0x4000000000000 <= w) {
i = 1; // Inputs near zero only take one step to converge.
} else if (w <= -0x3ffffffffffffff) {
i = 32; // Inputs near `-1/e` take very long to converge.
}
} else if (uint256(w >> 63) == uint256(0)) {
/// @solidity memory-safe-assembly
assembly {
// Inline log2 for more performance, since the range is small.
let v := shr(49, w)
let l := shl(3, lt(0xff, v))
l := add(or(l, byte(and(0x1f, shr(shr(l, v), 0x8421084210842108cc6318c6db6d54be)),
0x0706060506020504060203020504030106050205030304010505030400000000)), 49)
w := sdiv(shl(l, 7), byte(sub(l, 31), 0x0303030303030303040506080c13))
c := gt(l, 60)
i := add(2, add(gt(l, 53), c))
}
} else {
int256 ll = lnWad(w = lnWad(w));
/// @solidity memory-safe-assembly
assembly {
// `w = ln(x) - ln(ln(x)) + b * ln(ln(x)) / ln(x)`.
w := add(sdiv(mul(ll, 1023715080943847266), w), sub(w, ll))
i := add(3, iszero(shr(68, x)))
c := iszero(shr(143, x))
}
if (c == uint256(0)) {
do { // If `x` is big, use Newton's so that intermediate values won't overflow.
int256 e = expWad(w);
/// @solidity memory-safe-assembly
assembly {
let t := mul(w, div(e, wad))
w := sub(w, sdiv(sub(t, x), div(add(e, t), wad)))
}
if (p <= w) break;
p = w;
} while (--i != uint256(0));
/// @solidity memory-safe-assembly
assembly {
w := sub(w, sgt(w, 2))
}
return w;
}
}
do { // Otherwise, use Halley's for faster convergence.
int256 e = expWad(w);
/// @solidity memory-safe-assembly
assembly {
let t := add(w, wad)
let s := sub(mul(w, e), mul(x, wad))
w := sub(w, sdiv(mul(s, wad), sub(mul(e, t), sdiv(mul(add(t, wad), s), add(t, t)))))
}
if (p <= w) break;
p = w;
} while (--i != c);
/// @solidity memory-safe-assembly
assembly {
w := sub(w, sgt(w, 2))
}
// For certain ranges of `x`, we'll use the quadratic-rate recursive formula of
// R. Iacono and J.P. Boyd for the last iteration, to avoid catastrophic cancellation.
if (c == uint256(0)) return w;
int256 t = w | 1;
/// @solidity memory-safe-assembly
assembly {
x := sdiv(mul(x, wad), t)
}
x = (t * (wad + lnWad(x)));
/// @solidity memory-safe-assembly
assembly {
w := sdiv(x, add(wad, t))
}
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* GENERAL NUMBER UTILITIES */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Calculates `floor(x * y / d)` with full precision.
/// Throws if result overflows a uint256 or when `d` is zero.
/// Credit to Remco Bloemen under MIT license: https://2π.com/21/muldiv
function fullMulDiv(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
// 512-bit multiply `[p1 p0] = x * y`.
// Compute the product mod `2**256` and mod `2**256 - 1`
// then use the Chinese Remainder Theorem to reconstruct
// the 512 bit result. The result is stored in two 256
// variables such that `product = p1 * 2**256 + p0`.
// Temporarily use `result` as `p0` to save gas.
result := mul(x, y) // Lower 256 bits of `x * y`.
for {} 1 {} {
// If overflows.
if iszero(mul(or(iszero(x), eq(div(result, x), y)), d)) {
let mm := mulmod(x, y, not(0))
let p1 := sub(mm, add(result, lt(mm, result))) // Upper 256 bits of `x * y`.
/*------------------- 512 by 256 division --------------------*/
// Make division exact by subtracting the remainder from `[p1 p0]`.
let r := mulmod(x, y, d) // Compute remainder using mulmod.
let t := and(d, sub(0, d)) // The least significant bit of `d`. `t >= 1`.
// Make sure the result is less than `2**256`. Also prevents `d == 0`.
// Placing the check here seems to give more optimal stack operations.
if iszero(gt(d, p1)) {
mstore(0x00, 0xae47f702) // `FullMulDivFailed()`.
revert(0x1c, 0x04)
}
d := div(d, t) // Divide `d` by `t`, which is a power of two.
// Invert `d mod 2**256`
// Now that `d` is an odd number, it has an inverse
// modulo `2**256` such that `d * inv = 1 mod 2**256`.
// Compute the inverse by starting with a seed that is correct
// correct for four bits. That is, `d * inv = 1 mod 2**4`.
let inv := xor(2, mul(3, d))
// Now use Newton-Raphson iteration to improve the precision.
// Thanks to Hensel's lifting lemma, this also works in modular
// arithmetic, doubling the correct bits in each step.
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**8
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**16
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**32
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**64
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**128
result :=
mul(
// Divide [p1 p0] by the factors of two.
// Shift in bits from `p1` into `p0`. For this we need
// to flip `t` such that it is `2**256 / t`.
or(mul(sub(p1, gt(r, result)), add(div(sub(0, t), t), 1)), div(sub(result, r), t)),
mul(sub(2, mul(d, inv)), inv) // inverse mod 2**256
)
break
}
result := div(result, d)
break
}
}
}
/// @dev Calculates `floor(x * y / d)` with full precision.
/// Behavior is undefined if `d` is zero or the final result cannot fit in 256 bits.
/// Performs the full 512 bit calculation regardless.
function fullMulDivUnchecked(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
result := mul(x, y)
let mm := mulmod(x, y, not(0))
let p1 := sub(mm, add(result, lt(mm, result)))
let t := and(d, sub(0, d))
let r := mulmod(x, y, d)
d := div(d, t)
let inv := xor(2, mul(3, d))
inv := mul(inv, sub(2, mul(d, inv)))
inv := mul(inv, sub(2, mul(d, inv)))
inv := mul(inv, sub(2, mul(d, inv)))
inv := mul(inv, sub(2, mul(d, inv)))
inv := mul(inv, sub(2, mul(d, inv)))
result :=
mul(
or(mul(sub(p1, gt(r, result)), add(div(sub(0, t), t), 1)), div(sub(result, r), t)),
mul(sub(2, mul(d, inv)), inv)
)
}
}
/// @dev Calculates `floor(x * y / d)` with full precision, rounded up.
/// Throws if result overflows a uint256 or when `d` is zero.
/// Credit to Uniswap-v3-core under MIT license:
/// https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/FullMath.sol
function fullMulDivUp(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 result) {
result = fullMulDiv(x, y, d);
/// @solidity memory-safe-assembly
assembly {
if mulmod(x, y, d) {
result := add(result, 1)
if iszero(result) {
mstore(0x00, 0xae47f702) // `FullMulDivFailed()`.
revert(0x1c, 0x04)
}
}
}
}
/// @dev Returns `floor(x * y / d)`.
/// Reverts if `x * y` overflows, or `d` is zero.
function mulDiv(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(x, y)
// Equivalent to `require(d != 0 && (y == 0 || x <= type(uint256).max / y))`.
if iszero(mul(or(iszero(x), eq(div(z, x), y)), d)) {
mstore(0x00, 0xad251c27) // `MulDivFailed()`.
revert(0x1c, 0x04)
}
z := div(z, d)
}
}
/// @dev Returns `ceil(x * y / d)`.
/// Reverts if `x * y` overflows, or `d` is zero.
function mulDivUp(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(x, y)
// Equivalent to `require(d != 0 && (y == 0 || x <= type(uint256).max / y))`.
if iszero(mul(or(iszero(x), eq(div(z, x), y)), d)) {
mstore(0x00, 0xad251c27) // `MulDivFailed()`.
revert(0x1c, 0x04)
}
z := add(iszero(iszero(mod(z, d))), div(z, d))
}
}
function mulDivRound(uint256 value, uint256 multiplier, uint256 divisor) internal pure returns (uint256) {
return (value * multiplier + divisor / 2) / divisor;
}
/// @dev Returns `ceil(x / d)`.
/// Reverts if `d` is zero.
function divUp(uint256 x, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
if iszero(d) {
mstore(0x00, 0x65244e4e) // `DivFailed()`.
revert(0x1c, 0x04)
}
z := add(iszero(iszero(mod(x, d))), div(x, d))
}
}
/// @dev Returns `max(0, x - y)`.
function zeroFloorSub(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(gt(x, y), sub(x, y))
}
}
/// @dev Returns `condition ? x : y`, without branching.
function ternary(bool condition, uint256 x, uint256 y) internal pure returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
result := xor(x, mul(xor(x, y), iszero(condition)))
}
}
/// @dev Exponentiate `x` to `y` by squaring, denominated in base `b`.
/// Reverts if the computation overflows.
function rpow(uint256 x, uint256 y, uint256 b) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(b, iszero(y)) // `0 ** 0 = 1`. Otherwise, `0 ** n = 0`.
if x {
z := xor(b, mul(xor(b, x), and(y, 1))) // `z = isEven(y) ? scale : x`
let half := shr(1, b) // Divide `b` by 2.
// Divide `y` by 2 every iteration.
for { y := shr(1, y) } y { y := shr(1, y) } {
let xx := mul(x, x) // Store x squared.
let xxRound := add(xx, half) // Round to the nearest number.
// Revert if `xx + half` overflowed, or if `x ** 2` overflows.
if or(lt(xxRound, xx), shr(128, x)) {
mstore(0x00, 0x49f7642b) // `RPowOverflow()`.
revert(0x1c, 0x04)
}
x := div(xxRound, b) // Set `x` to scaled `xxRound`.
// If `y` is odd:
if and(y, 1) {
let zx := mul(z, x) // Compute `z * x`.
let zxRound := add(zx, half) // Round to the nearest number.
// If `z * x` overflowed or `zx + half` overflowed:
if or(xor(div(zx, x), z), lt(zxRound, zx)) {
// Revert if `x` is non-zero.
if x {
mstore(0x00, 0x49f7642b) // `RPowOverflow()`.
revert(0x1c, 0x04)
}
}
z := div(zxRound, b) // Return properly scaled `zxRound`.
}
}
}
}
}
/// @dev Returns the square root of `x`, rounded down.
function sqrt(uint256 x) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// `floor(sqrt(2**15)) = 181`. `sqrt(2**15) - 181 = 2.84`.
z := 181 // The "correct" value is 1, but this saves a multiplication later.
// This segment is to get a reasonable initial estimate for the Babylonian method. With a bad
// start, the correct # of bits increases ~linearly each iteration instead of ~quadratically.
// Let `y = x / 2**r`. We check `y >= 2**(k + 8)`
// but shift right by `k` bits to ensure that if `x >= 256`, then `y >= 256`.
let r := shl(7, lt(0xffffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffffff, shr(r, x))))
z := shl(shr(1, r), z)
// Goal was to get `z*z*y` within a small factor of `x`. More iterations could
// get y in a tighter range. Currently, we will have y in `[256, 256*(2**16))`.
// We ensured `y >= 256` so that the relative difference between `y` and `y+1` is small.
// That's not possible if `x < 256` but we can just verify those cases exhaustively.
// Now, `z*z*y <= x < z*z*(y+1)`, and `y <= 2**(16+8)`, and either `y >= 256`, or `x < 256`.
// Correctness can be checked exhaustively for `x < 256`, so we assume `y >= 256`.
// Then `z*sqrt(y)` is within `sqrt(257)/sqrt(256)` of `sqrt(x)`, or about 20bps.
// For `s` in the range `[1/256, 256]`, the estimate `f(s) = (181/1024) * (s+1)`
// is in the range `(1/2.84 * sqrt(s), 2.84 * sqrt(s))`,
// with largest error when `s = 1` and when `s = 256` or `1/256`.
// Since `y` is in `[256, 256*(2**16))`, let `a = y/65536`, so that `a` is in `[1/256, 256)`.
// Then we can estimate `sqrt(y)` using
// `sqrt(65536) * 181/1024 * (a + 1) = 181/4 * (y + 65536)/65536 = 181 * (y + 65536)/2**18`.
// There is no overflow risk here since `y < 2**136` after the first branch above.
z := shr(18, mul(z, add(shr(r, x), 65536))) // A `mul()` is saved from starting `z` at 181.
// Given the worst case multiplicative error of 2.84 above, 7 iterations should be enough.
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
// If `x+1` is a perfect square, the Babylonian method cycles between
// `floor(sqrt(x))` and `ceil(sqrt(x))`. This statement ensures we return floor.
// See: https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division
z := sub(z, lt(div(x, z), z))
}
}
/// @dev Returns the cube root of `x`, rounded down.
/// Credit to bout3fiddy and pcaversaccio under AGPLv3 license:
/// https://github.com/pcaversaccio/snekmate/blob/main/src/utils/Math.vy
/// Formally verified by xuwinnie:
/// https://github.com/vectorized/solady/blob/main/audits/xuwinnie-solady-cbrt-proof.pdf
function cbrt(uint256 x) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
let r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffff, shr(r, x))))
r := or(r, shl(3, lt(0xff, shr(r, x))))
// Makeshift lookup table to nudge the approximate log2 result.
z := div(shl(div(r, 3), shl(lt(0xf, shr(r, x)), 0xf)), xor(7, mod(r, 3)))
// Newton-Raphson's.
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
// Round down.
z := sub(z, lt(div(x, mul(z, z)), z))
}
}
/// @dev Returns the square root of `x`, denominated in `WAD`, rounded down.
function sqrtWad(uint256 x) internal pure returns (uint256 z) {
unchecked {
if (x <= type(uint256).max / 10 ** 18) return sqrt(x * 10 ** 18);
z = (1 + sqrt(x)) * 10 ** 9;
z = (fullMulDivUnchecked(x, 10 ** 18, z) + z) >> 1;
}
/// @solidity memory-safe-assembly
assembly {
z := sub(z, gt(999999999999999999, sub(mulmod(z, z, x), 1))) // Round down.
}
}
/// @dev Returns the cube root of `x`, denominated in `WAD`, rounded down.
/// Formally verified by xuwinnie:
/// https://github.com/vectorized/solady/blob/main/audits/xuwinnie-solady-cbrt-proof.pdf
function cbrtWad(uint256 x) internal pure returns (uint256 z) {
unchecked {
if (x <= type(uint256).max / 10 ** 36) return cbrt(x * 10 ** 36);
z = (1 + cbrt(x)) * 10 ** 12;
z = (fullMulDivUnchecked(x, 10 ** 36, z * z) + z + z) / 3;
}
/// @solidity memory-safe-assembly
assembly {
let p := x
for {} 1 {} {
if iszero(shr(229, p)) {
if iszero(shr(199, p)) {
p := mul(p, 100000000000000000) // 10 ** 17.
break
}
p := mul(p, 100000000) // 10 ** 8.
break
}
if iszero(shr(249, p)) { p := mul(p, 100) }
break
}
let t := mulmod(mul(z, z), z, p)
z := sub(z, gt(lt(t, shr(1, p)), iszero(t))) // Round down.
}
}
/// @dev Returns the factorial of `x`.
function factorial(uint256 x) internal pure returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
result := 1
if iszero(lt(x, 58)) {
mstore(0x00, 0xaba0f2a2) // `FactorialOverflow()`.
revert(0x1c, 0x04)
}
for {} x { x := sub(x, 1) } { result := mul(result, x) }
}
}
/// @dev Returns the log2 of `x`.
/// Equivalent to computing the index of the most significant bit (MSB) of `x`.
/// Returns 0 if `x` is zero.
function log2(uint256 x) internal pure returns (uint256 r) {
/// @solidity memory-safe-assembly
assembly {
r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffff, shr(r, x))))
r := or(r, shl(3, lt(0xff, shr(r, x))))
// forgefmt: disable-next-item
r := or(r, byte(and(0x1f, shr(shr(r, x), 0x8421084210842108cc6318c6db6d54be)),
0x0706060506020504060203020504030106050205030304010505030400000000))
}
}
/// @dev Returns the log2 of `x`, rounded up.
/// Returns 0 if `x` is zero.
function log2Up(uint256 x) internal pure returns (uint256 r) {
r = log2(x);
/// @solidity memory-safe-assembly
assembly {
r := add(r, lt(shl(r, 1), x))
}
}
/// @dev Returns the log10 of `x`.
/// Returns 0 if `x` is zero.
function log10(uint256 x) internal pure returns (uint256 r) {
/// @solidity memory-safe-assembly
assembly {
if iszero(lt(x, 100000000000000000000000000000000000000)) {
x := div(x, 100000000000000000000000000000000000000)
r := 38
}
if iszero(lt(x, 100000000000000000000)) {
x := div(x, 100000000000000000000)
r := add(r, 20)
}
if iszero(lt(x, 10000000000)) {
x := div(x, 10000000000)
r := add(r, 10)
}
if iszero(lt(x, 100000)) {
x := div(x, 100000)
r := add(r, 5)
}
r := add(r, add(gt(x, 9), add(gt(x, 99), add(gt(x, 999), gt(x, 9999)))))
}
}
/// @dev Returns the log10 of `x`, rounded up.
/// Returns 0 if `x` is zero.
function log10Up(uint256 x) internal pure returns (uint256 r) {
r = log10(x);
/// @solidity memory-safe-assembly
assembly {
r := add(r, lt(exp(10, r), x))
}
}
/// @dev Returns the log256 of `x`.
/// Returns 0 if `x` is zero.
function log256(uint256 x) internal pure returns (uint256 r) {
/// @solidity memory-safe-assembly
assembly {
r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffff, shr(r, x))))
r := or(shr(3, r), lt(0xff, shr(r, x)))
}
}
/// @dev Returns the log256 of `x`, rounded up.
/// Returns 0 if `x` is zero.
function log256Up(uint256 x) internal pure returns (uint256 r) {
r = log256(x);
/// @solidity memory-safe-assembly
assembly {
r := add(r, lt(shl(shl(3, r), 1), x))
}
}
/// @dev Returns the scientific notation format `mantissa * 10 ** exponent` of `x`.
/// Useful for compressing prices (e.g. using 25 bit mantissa and 7 bit exponent).
function sci(uint256 x) internal pure returns (uint256 mantissa, uint256 exponent) {
/// @solidity memory-safe-assembly
assembly {
mantissa := x
if mantissa {
if iszero(mod(mantissa, 1000000000000000000000000000000000)) {
mantissa := div(mantissa, 1000000000000000000000000000000000)
exponent := 33
}
if iszero(mod(mantissa, 10000000000000000000)) {
mantissa := div(mantissa, 10000000000000000000)
exponent := add(exponent, 19)
}
if iszero(mod(mantissa, 1000000000000)) {
mantissa := div(mantissa, 1000000000000)
exponent := add(exponent, 12)
}
if iszero(mod(mantissa, 1000000)) {
mantissa := div(mantissa, 1000000)
exponent := add(exponent, 6)
}
if iszero(mod(mantissa, 10000)) {
mantissa := div(mantissa, 10000)
exponent := add(exponent, 4)
}
if iszero(mod(mantissa, 100)) {
mantissa := div(mantissa, 100)
exponent := add(exponent, 2)
}
if iszero(mod(mantissa, 10)) {
mantissa := div(mantissa, 10)
exponent := add(exponent, 1)
}
}
}
}
/// @dev Convenience function for packing `x` into a smaller number using `sci`.
/// The `mantissa` will be in bits [7..255] (the upper 249 bits).
/// The `exponent` will be in bits [0..6] (the lower 7 bits).
/// Use `SafeCastLib` to safely ensure that the `packed` number is small
/// enough to fit in the desired unsigned integer type:
/// ```
/// uint32 packed = SafeCastLib.toUint32(FixedPointMathLib.packSci(777 ether));
/// ```
function packSci(uint256 x) internal pure returns (uint256 packed) {
(x, packed) = sci(x); // Reuse for `mantissa` and `exponent`.
/// @solidity memory-safe-assembly
assembly {
if shr(249, x) {
mstore(0x00, 0xce30380c) // `MantissaOverflow()`.
revert(0x1c, 0x04)
}
packed := or(shl(7, x), packed)
}
}
/// @dev Convenience function for unpacking a packed number from `packSci`.
function unpackSci(uint256 packed) internal pure returns (uint256 unpacked) {
unchecked {
unpacked = (packed >> 7) * 10 ** (packed & 0x7f);
}
}
/// @dev Returns the average of `x` and `y`. Rounds towards zero.
function avg(uint256 x, uint256 y) internal pure returns (uint256 z) {
unchecked {
z = (x & y) + ((x ^ y) >> 1);
}
}
/// @dev Returns the average of `x` and `y`. Rounds towards negative infinity.
function avg(int256 x, int256 y) internal pure returns (int256 z) {
unchecked {
z = (x >> 1) + (y >> 1) + (x & y & 1);
}
}
/// @dev Returns the absolute value of `x`.
function abs(int256 x) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(sar(255, x), add(sar(255, x), x))
}
}
/// @dev Returns the absolute distance between `x` and `y`.
function dist(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(mul(xor(sub(y, x), sub(x, y)), gt(x, y)), sub(y, x))
}
}
/// @dev Returns the absolute distance between `x` and `y`.
function dist(int256 x, int256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(mul(xor(sub(y, x), sub(x, y)), sgt(x, y)), sub(y, x))
}
}
/// @dev Returns the minimum of `x` and `y`.
function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), lt(y, x)))
}
}
/// @dev Returns the minimum of `x` and `y`.
function min(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), slt(y, x)))
}
}
/// @dev Returns the maximum of `x` and `y`.
function max(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), gt(y, x)))
}
}
/// @dev Returns the maximum of `x` and `y`.
function max(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), sgt(y, x)))
}
}
/// @dev Returns `x`, bounded to `minValue` and `maxValue`.
function clamp(uint256 x, uint256 minValue, uint256 maxValue) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, minValue), gt(minValue, x)))
z := xor(z, mul(xor(z, maxValue), lt(maxValue, z)))
}
}
/// @dev Returns `x`, bounded to `minValue` and `maxValue`.
function clamp(int256 x, int256 minValue, int256 maxValue) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, minValue), sgt(minValue, x)))
z := xor(z, mul(xor(z, maxValue), slt(maxValue, z)))
}
}
/// @dev Returns greatest common divisor of `x` and `y`.
function gcd(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
for { z := x } y {} {
let t := y
y := mod(z, y)
z := t
}
}
}
/// @dev Returns `a + (b - a) * (t - begin) / (end - begin)`,
/// with `t` clamped between `begin` and `end` (inclusive).
/// Agnostic to the order of (`a`, `b`) and (`end`, `begin`).
/// If `begins == end`, returns `t <= begin ? a : b`.
function lerp(uint256 a, uint256 b, uint256 t, uint256 begin, uint256 end) internal pure returns (uint256) {
if (begin > end) {
t = ~t;
begin = ~begin;
end = ~end;
}
if (t <= begin) return a;
if (t >= end) return b;
unchecked {
if (b >= a) return a + fullMulDiv(b - a, t - begin, end - begin);
return a - fullMulDiv(a - b, t - begin, end - begin);
}
}
/// @dev Returns `a + (b - a) * (t - begin) / (end - begin)`.
/// with `t` clamped between `begin` and `end` (inclusive).
/// Agnostic to the order of (`a`, `b`) and (`end`, `begin`).
/// If `begins == end`, returns `t <= begin ? a : b`.
function lerp(int256 a, int256 b, int256 t, int256 begin, int256 end) internal pure returns (int256) {
if (begin > end) {
t = int256(~uint256(t));
begin = int256(~uint256(begin));
end = int256(~uint256(end));
}
if (t <= begin) return a;
if (t >= end) return b;
// forgefmt: disable-next-item
unchecked {
if (b >= a) return int256(uint256(a) + fullMulDiv(uint256(b) - uint256(a),
uint256(t) - uint256(begin), uint256(end) - uint256(begin)));
return int256(uint256(a) - fullMulDiv(uint256(a) - uint256(b),
uint256(t) - uint256(begin), uint256(end) - uint256(begin)));
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* RAW NUMBER OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns `x + y`, without checking for overflow.
function rawAdd(uint256 x, uint256 y) internal pure returns (uint256 z) {
unchecked {
z = x + y;
}
}
/// @dev Returns `x + y`, without checking for overflow.
function rawAdd(int256 x, int256 y) internal pure returns (int256 z) {
unchecked {
z = x + y;
}
}
/// @dev Returns `x - y`, without checking for underflow.
function rawSub(uint256 x, uint256 y) internal pure returns (uint256 z) {
unchecked {
z = x - y;
}
}
/// @dev Returns `x - y`, without checking for underflow.
function rawSub(int256 x, int256 y) internal pure returns (int256 z) {
unchecked {
z = x - y;
}
}
/// @dev Returns `x * y`, without checking for overflow.
function rawMul(uint256 x, uint256 y) internal pure returns (uint256 z) {
unchecked {
z = x * y;
}
}
/// @dev Returns `x * y`, without checking for overflow.
function rawMul(int256 x, int256 y) internal pure returns (int256 z) {
unchecked {
z = x * y;
}
}
/// @dev Returns `x / y`, returning 0 if `y` is zero.
function rawDiv(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := div(x, y)
}
}
/// @dev Returns `x / y`, returning 0 if `y` is zero.
function rawSDiv(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := sdiv(x, y)
}
}
/// @dev Returns `x % y`, returning 0 if `y` is zero.
function rawMod(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mod(x, y)
}
}
/// @dev Returns `x % y`, returning 0 if `y` is zero.
function rawSMod(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := smod(x, y)
}
}
/// @dev Returns `(x + y) % d`, return 0 if `d` if zero.
function rawAddMod(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := addmod(x, y, d)
}
}
/// @dev Returns `(x * y) % d`, return 0 if `d` if zero.
function rawMulMod(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mulmod(x, y, d)
}
}
}//SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.20;
library OrderBookErrors {
/**
* @dev Thrown when a user is not the owner and tries to execute a privileged function
*/
error Unauthorized();
/**
* @dev Thrown when a market is paused and a user tries to execute an action or if the owner passes an already existing market state for toggling
*/
error MarketStateError();
/**
* @dev Thrown when maker fee passed to initializer is too high/invalid
*/
error MarketFeeError();
/**
* @dev Thrown when minSize = 0 or maxSize < minSize
*/
error MarketSizeError();
/**
* @dev Thrown when available size is rounded down to zero
*/
error VaultSizeZero();
/**
* @dev Thrown when Kuru AMM Vault spread passed to initializer is too high or too low or is not a multiple of 10
*/
error InvalidSpread();
/**
* @dev Thrown when the inputted price while adding an order is invalid
*/
error PriceError();
/**
* @dev Thrown when vault initialization price crosses the book
*/
error VaultInitializationPriceCrossesBook();
/**
* @dev Thrown when the size inputted is invalid, i.e, < minSize or > maxSize
*/
error SizeError();
/**
* @dev Thrown when price is not divisible by tick
*/
error TickSizeError();
/**
* @dev Thrown when a post only order gets filled
*/
error PostOnlyError();
/**
* @dev Thrown when a flip order matches with a price
*/
error ProvisionError();
/**
* @dev Thrown when a non-owner tries to execute a privileged function, i.e, if non owner tries to pause/unpause a market or
* if a user tries to cancel an order that they did not place
*/
error OnlyOwnerAllowedError();
/**
* @dev Thrown when wrong interface is called for a cancel
*/
error WrongOrderTypeCancel();
/**
* @dev Thrown when cancelOrder is called on an order which is already filled or cancelled
*/
error OrderAlreadyFilledOrCancelled();
/**
* @dev Thrown when length mismatch occurs between inputted arrays
*/
error LengthMismatch();
/**
* @dev Thrown when msg.value is insufficient in market orders
*/
error NativeAssetInsufficient();
/**
* @dev Thrown when msg.value is surplus in market orders
*/
error NativeAssetSurplus();
/**
* @dev Thrown when msg.value is greater than 0 when native assets are not required
*/
error NativeAssetNotRequired();
/**
* @dev Thrown when native asset transfer fails
*/
error NativeAssetTransferFail();
/**
* @dev Thrown when IOC orders do not get filled by the market
*/
error InsufficientLiquidity();
/**
* @dev Throws when slippage is exceeded in market orders
*/
error SlippageExceeded();
/**
* @dev Thrown when quote size in uint32 overflows
*/
error TooMuchSizeFilled();
/**
* @dev Thrown when safe transfer from fails
*/
error TransferFromFailed();
/**
* @dev Thrown when the call is not made by the vault
*/
error OnlyVaultAllowed();
/**
* @dev Thrown when safe cast to uint96 fails
*/
error Uint96Overflow();
/**
* @dev Thrown when safe cast to uint32 fails
*/
error Uint32Overflow();
}
library MarginAccountErrors {
/**
* @dev Thrown when a non-router tries to update markets
*/
error OnlyRouterAllowed();
/**
* @dev Thrown when a non-verified market tries to execute a market action
*/
error OnlyVerifiedMarketsAllowed();
/**
* @dev Thrown when a user has insufficient margin account balance
*/
error InsufficientBalance();
/**
* @dev Thrown when native asset transfer fails
*/
error NativeAssetTransferFail();
/**
* @dev Thrown when msg.value is not zero when native assets are not required
*/
error NativeAssetMismatch();
/**
* @dev Thrown when zero address is passed as a parameter
*/
error ZeroAddressNotAllowed();
/**
* @dev Thrown when protocol is paused
*/
error ProtocolPaused();
/**
* @dev Thrown when protocol state is not changed
*/
error ProtocolStateNotChanged();
/**
* @dev Thrown when fee collector is not set
*/
error FeeCollectorNotChanged();
}
library RouterErrors {
/**
* @dev Thrown when zero address is passed as a parameter
*/
error ZeroAddressNotAllowed();
/**
* @dev Thrown when base and quote asset addresses are the same
*/
error BaseAndQuoteAssetSame();
/**
* @dev Thrown when market type given and token addresses are not compatible
*/
error MarketTypeMismatch();
/**
* @dev Thrown when tick size is 0
*/
error InvalidTickSize();
/**
* @dev Thrown when size precision is not a power of 10
*/
error InvalidSizePrecision();
/**
* @dev Thrown when price precision is not a power of 10
*/
error InvalidPricePrecision();
/**
* @dev Thrown when no markets are passed as input
*/
error NoMarketsPassed();
/**
* @dev Thrown when the length of market addresses, isBuy, and nativeSend arrays are not the same
*/
error LengthMismatch();
/**
* @dev Thrown when the market is invalid
*/
error InvalidMarket();
/**
* @dev Thrown when the slippage exceeds the expected value
*/
error SlippageExceeded();
/**
* @dev Thrown when the native asset transfer fails
*/
error NativeAssetTransferFail();
/**
* @dev Thrown when safe cast to uint96 fails
*/
error Uint96Overflow();
/**
* @dev Thrown when there is no change in the implementation
*/
error ImplementationNotChanged();
}
library KuruAMMVaultErrors {
/**
* @dev Thrown when a user is not the owner and tries to execute a privileged function
*/
error Unauthorized();
/**
* @dev Thrown when native token passed as argument and msg.value does not match
*/
error NativeAssetMismatch();
/**
* @dev Thrown when first deposit ask price is 0
*/
error AskPriceZero();
/**
* @dev Thrown when vault sizes are 0
*/
error InvalidVaultSize();
/**
* @dev Thrown when amount of quote tokens passed is insufficient
*/
error InsufficientQuoteToken();
/**
* @dev Thrown when insufficient liquidity is minted
*/
error InsufficientLiquidityMinted();
/**
* @dev Thrown when balance of owner is too less
*/
error InsufficientBalance();
/**
* @dev Thrown when quote used in a deposit is too low
*/
error InsufficientQuoteUsed();
/**
* @dev Thrown when native asset transfer fails
*/
error NativeAssetTransferFail();
/**
* @dev Thrown when new size exceeds partially filled size
*/
error NewSizeExceedsPartiallyFilledSize();
/**
* @dev Thrown when the amounts withdrawn are negative
*/
error NegativeAmountWithdrawn();
/**
* @dev Thrown when safe cast to uint96 fails
*/
error Uint96Overflow();
}
library KuruForwarderErrors {
/**
* @dev Thrown when zero address is passed as a parameter
*/
error ZeroAddressNotAllowed();
/**
* @dev Thrown when the signature does not match the request
*/
error SignatureMismatch();
/**
* @dev Thrown when the interface is not allowed
*/
error InterfaceNotAllowed();
/**
* @dev Thrown when the execution fails
*/
error ExecutionFailed();
/**
* @dev Thrown when the nonce is already used
*/
error NonceAlreadyUsed();
/**
* @dev Thrown when the value is insufficient
*/
error InsufficientValue();
}// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.20;
interface IMarginAccount {
event ProtocolStateUpdated(bool newState);
event FeeCollectorUpdated(address newFeeCollector);
event Deposit(address owner, address token, uint256 amount);
event Withdrawal(address owner, address token, uint256 amount);
function updateMarkets(address _marketAddress) external;
function deposit(address _user, address _token, uint256 _amount) external payable;
function withdraw(uint256 _amount, address _token) external;
function debitUser(address _user, address _token, uint256 _amount) external;
function creditFee(address _assetA, uint256 _feeA, address _assetB, uint256 _feeB) external;
function creditUser(address _user, address _token, uint256 _amount, bool _useMargin) external;
function creditUsersEncoded(bytes calldata _encodedData) external;
function getBalance(address _user, address _token) external view returns (uint256);
}// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.20;
interface IKuruAMMVault {
struct MarketParams {
uint32 pricePrecision;
uint96 sizePrecision;
address baseAssetAddress;
uint256 baseAssetDecimals;
address quoteAssetAddress;
uint256 quoteAssetDecimals;
uint32 tickSize;
uint96 minSize;
uint96 maxSize;
uint256 takerFeeBps;
uint256 makerFeeBps;
}
event KuruVaultDeposit(uint256 amount1, uint256 amount2, uint256 shares, address userAddress);
event KuruVaultWithdraw(uint256 amount1, uint256 amount2, uint256 shares, address userAddress);
function initialize(
address _owner,
address token1_,
address token2_,
address _marginAccount,
address _market,
uint96 _spread
) external;
function setMarketParams() external;
function deposit(uint256 baseDeposit, uint256 quoteDeposit, uint256 minQuoteConsumed, address receiver)
external
payable
returns (uint256);
}// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.20;
interface IOrderBook {
enum OrderBookType {
NO_NATIVE,
NATIVE_IN_BASE,
NATIVE_IN_QUOTE
}
enum OrderStatus {
ACTIVE,
FILLED,
CANCELED
}
enum MarketState {
ACTIVE,
SOFT_PAUSED,
HARD_PAUSED
}
struct Order {
address ownerAddress;
uint96 size;
uint40 prev;
uint40 next;
uint40 flippedId;
uint32 price;
uint32 flippedPrice;
bool isBuy;
}
event MarketStateUpdated(MarketState previousState, MarketState newState);
/**
* @dev Emitted when a new order is created.
* @param orderId Unique identifier for the newly created order.
* @param owner Address of the user who created the order.
* @param price Price point of the order in the specified precision.
* @param size Size of the order in the specified precision.
* @param isBuy Boolean indicating if the order is a buy (true) or sell (false) order.
*/
event OrderCreated(uint40 orderId, address owner, uint96 size, uint32 price, bool isBuy);
/**
* @dev Emitted when a flip order is created
* @param orderId Unique identifier for the newly created order.
* @param flippedId Unique identifier for the flipped order.
* @param owner Address of the user who created the order.
* @param size Size of the order in the specified precision.
* @param price Price point of the order in the specified precision.
* @param flippedPrice Price point of the flipped order in the specified precision.
* @param isBuy Boolean indicating if the order is a buy (true) or sell (false) order.
*/
event FlipOrderCreated(
uint40 orderId, uint40 flippedId, address owner, uint96 size, uint32 price, uint32 flippedPrice, bool isBuy
);
/**
* @dev Emitted when a flip order is partially/completely filled and it results in a new order
* @param orderId Unique identifier for the newly created order.
* @param flippedId Unique identifier for the flipped order.
* @param owner Address of the user who created the order.
* @param size Size of the order in the specified precision.
* @param price Price point of the order in the specified precision.
* @param flippedPrice Price point of the flipped order in the specified precision.
* @param isBuy Boolean indicating if the order is a buy (true) or sell (false) order.
*/
event FlippedOrderCreated(
uint40 orderId, uint40 flippedId, address owner, uint96 size, uint32 price, uint32 flippedPrice, bool isBuy
);
/**
* @dev Emitted when a flip order is updated
* @param orderId Unique identifier for the order.
* @param size Size of the order in the specified precision.
*/
event FlipOrderUpdated(uint40 orderId, uint96 size);
/**
* @dev Emitted when one or more flip orders are canceled
* @param orderIds Array of order identifiers that were canceled.
* @param owner Address of the user who canceled the orders.
*/
event FlipOrdersCanceled(uint40[] orderIds, address owner);
/**
* @dev Emitted when one or more orders are completed or canceled.
* @param orderId Array of order identifiers that were completed or canceled.
*/
event OrdersCanceled(uint40[] orderId, address owner);
/**
* @dev Emitted for each cancel
*/
event OrderCanceled(uint40 orderId, address owner, uint32 price, uint96 size, bool isBuy);
/**
* @dev Emitted when the vault params are updated
* @param _vaultAskOrderSize Size of the vault ask order
* @param _vaultAskPartiallyFilledSize Size of the vault ask partially filled order
* @param _vaultBidOrderSize Size of the vault bid order
* @param _vaultBidPartiallyFilledSize Size of the vault bid partially filled order
* @param _askPrice The vault best ask price
* @param _bidPrice The vault best bid price
*/
event VaultParamsUpdated(
uint96 _vaultAskOrderSize,
uint96 _vaultAskPartiallyFilledSize,
uint96 _vaultBidOrderSize,
uint96 _vaultBidPartiallyFilledSize,
uint256 _askPrice,
uint256 _bidPrice
);
/**
*
* @dev Emitted when a trade goes through.
* @param orderId Order Id of the order that was filled.
* PS. All data regarding the original order can be found out from the order ID
* @param updatedSize New size of the order
* @param takerAddress Address of the taker.
* @param filledSize Size taken by the taker.
*/
event Trade(
uint40 orderId,
address makerAddress,
bool isBuy,
uint256 price,
uint96 updatedSize,
address takerAddress,
address txOrigin,
uint96 filledSize
);
function initialize(
address _factory,
OrderBookType _type,
address _baseAssetAddress,
uint256 _baseAssetDecimals,
address _quoteAssetAddress,
uint256 _quoteAssetDecimals,
address _marginAccountAddress,
uint96 _sizePrecision,
uint32 _pricePrecision,
uint32 _tickSize,
uint96 _minSize,
uint96 _maxSize,
uint256 _takerFeeBps,
uint256 _makerFeeBps,
address _kuruAmmVault,
uint96 _kuruAmmSpread,
address __trustedForwarder
) external;
function toggleMarket(MarketState _state) external;
function addBuyOrder(uint32 _price, uint96 size, bool _postOnly) external;
function addFlipBuyOrder(uint32 _price, uint32 _flippedPrice, uint96 _size, bool _provisionOrRevert) external;
function addSellOrder(uint32 _price, uint96 size, bool _postOnly) external;
function addFlipSellOrder(uint32 _price, uint32 _flippedPrice, uint96 _size, bool _provisionOrRevert) external;
function batchCancelOrders(uint40[] calldata _orderIds) external;
function batchCancelFlipOrders(uint40[] calldata _orderIds) external;
function batchUpdate(
uint32[] calldata buyPrices,
uint96[] calldata buySizes,
uint32[] calldata sellPrices,
uint96[] calldata sellSizes,
uint40[] calldata orderIdsToCancel,
bool postOnly
) external;
function placeAndExecuteMarketBuy(uint96 _quoteAmount, uint256 _minAmountOut, bool _isMargin, bool _isFillOrKill)
external
payable
returns (uint256);
function placeAndExecuteMarketSell(uint96 _size, uint256 _minAmountOut, bool _isMargin, bool _isFillOrKill)
external
payable
returns (uint256);
function bestBidAsk() external view returns (uint256, uint256);
function updateVaultOrdSz(
uint96 _vaultAskOrderSize,
uint96 _vaultBidOrderSize,
uint256 _askPrice,
uint256 _bidPrice,
bool _nullifyPartialFills
) external;
function getMarketParams()
external
view
returns (uint32, uint96, address, uint256, address, uint256, uint32, uint96, uint96, uint256, uint256);
function getVaultParams()
external
view
returns (address, uint256, uint96, uint256, uint96, uint96, uint96, uint96);
function vaultAskOrderSize() external view returns (uint96);
function vaultBestAsk() external view returns (uint256);
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.3.0) (utils/TransientSlot.sol)
// This file was procedurally generated from scripts/generate/templates/TransientSlot.js.
pragma solidity ^0.8.24;
/**
* @dev Library for reading and writing value-types to specific transient storage slots.
*
* Transient slots are often used to store temporary values that are removed after the current transaction.
* This library helps with reading and writing to such slots without the need for inline assembly.
*
* * Example reading and writing values using transient storage:
* ```solidity
* contract Lock {
* using TransientSlot for *;
*
* // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
* bytes32 internal constant _LOCK_SLOT = 0xf4678858b2b588224636b8522b729e7722d32fc491da849ed75b3fdf3c84f542;
*
* modifier locked() {
* require(!_LOCK_SLOT.asBoolean().tload());
*
* _LOCK_SLOT.asBoolean().tstore(true);
* _;
* _LOCK_SLOT.asBoolean().tstore(false);
* }
* }
* ```
*
* TIP: Consider using this library along with {SlotDerivation}.
*/
library TransientSlot {
/**
* @dev UDVT that represents a slot holding an address.
*/
type AddressSlot is bytes32;
/**
* @dev Cast an arbitrary slot to a AddressSlot.
*/
function asAddress(bytes32 slot) internal pure returns (AddressSlot) {
return AddressSlot.wrap(slot);
}
/**
* @dev UDVT that represents a slot holding a bool.
*/
type BooleanSlot is bytes32;
/**
* @dev Cast an arbitrary slot to a BooleanSlot.
*/
function asBoolean(bytes32 slot) internal pure returns (BooleanSlot) {
return BooleanSlot.wrap(slot);
}
/**
* @dev UDVT that represents a slot holding a bytes32.
*/
type Bytes32Slot is bytes32;
/**
* @dev Cast an arbitrary slot to a Bytes32Slot.
*/
function asBytes32(bytes32 slot) internal pure returns (Bytes32Slot) {
return Bytes32Slot.wrap(slot);
}
/**
* @dev UDVT that represents a slot holding a uint256.
*/
type Uint256Slot is bytes32;
/**
* @dev Cast an arbitrary slot to a Uint256Slot.
*/
function asUint256(bytes32 slot) internal pure returns (Uint256Slot) {
return Uint256Slot.wrap(slot);
}
/**
* @dev UDVT that represents a slot holding a int256.
*/
type Int256Slot is bytes32;
/**
* @dev Cast an arbitrary slot to a Int256Slot.
*/
function asInt256(bytes32 slot) internal pure returns (Int256Slot) {
return Int256Slot.wrap(slot);
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(AddressSlot slot) internal view returns (address value) {
assembly ("memory-safe") {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(AddressSlot slot, address value) internal {
assembly ("memory-safe") {
tstore(slot, value)
}
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(BooleanSlot slot) internal view returns (bool value) {
assembly ("memory-safe") {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(BooleanSlot slot, bool value) internal {
assembly ("memory-safe") {
tstore(slot, value)
}
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(Bytes32Slot slot) internal view returns (bytes32 value) {
assembly ("memory-safe") {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(Bytes32Slot slot, bytes32 value) internal {
assembly ("memory-safe") {
tstore(slot, value)
}
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(Uint256Slot slot) internal view returns (uint256 value) {
assembly ("memory-safe") {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(Uint256Slot slot, uint256 value) internal {
assembly ("memory-safe") {
tstore(slot, value)
}
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(Int256Slot slot) internal view returns (int256 value) {
assembly ("memory-safe") {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(Int256Slot slot, int256 value) internal {
assembly ("memory-safe") {
tstore(slot, value)
}
}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Call context checker mixin.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/CallContextChecker.sol)
contract CallContextChecker {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The call is from an unauthorized call context.
error UnauthorizedCallContext();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* IMMUTABLES */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev For checking if the context is a delegate call.
///
/// Note: To enable use cases with an immutable default implementation in the bytecode,
/// (see: ERC6551Proxy), we don't require that the proxy address must match the
/// value stored in the implementation slot, which may not be initialized.
uint256 private immutable __self = uint256(uint160(address(this)));
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CALL CONTEXT CHECKS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
// A proxy call can be either via a `delegatecall` to an implementation,
// or a 7702 call on an authority that points to a delegation.
/// @dev Returns whether the current call context is on a EIP7702 authority
/// (i.e. externally owned account).
function _onEIP7702Authority() internal view virtual returns (bool result) {
/// @solidity memory-safe-assembly
assembly {
extcodecopy(address(), 0x00, 0x00, 0x20)
// Note: Checking that it starts with hex"ef01" is the most general and futureproof.
// 7702 bytecode is `abi.encodePacked(hex"ef01", uint8(version), address(delegation))`.
result := eq(0xef01, shr(240, mload(0x00)))
}
}
/// @dev Returns the implementation of this contract.
function _selfImplementation() internal view virtual returns (address) {
return address(uint160(__self));
}
/// @dev Returns whether the current call context is on the implementation itself.
function _onImplementation() internal view virtual returns (bool) {
return __self == uint160(address(this));
}
/// @dev Requires that the current call context is performed via a EIP7702 authority.
function _checkOnlyEIP7702Authority() internal view virtual {
if (!_onEIP7702Authority()) _revertUnauthorizedCallContext();
}
/// @dev Requires that the current call context is performed via a proxy.
function _checkOnlyProxy() internal view virtual {
if (_onImplementation()) _revertUnauthorizedCallContext();
}
/// @dev Requires that the current call context is NOT performed via a proxy.
/// This is the opposite of `checkOnlyProxy`.
function _checkNotDelegated() internal view virtual {
if (!_onImplementation()) _revertUnauthorizedCallContext();
}
/// @dev Requires that the current call context is performed via a EIP7702 authority.
modifier onlyEIP7702Authority() virtual {
_checkOnlyEIP7702Authority();
_;
}
/// @dev Requires that the current call context is performed via a proxy.
modifier onlyProxy() virtual {
_checkOnlyProxy();
_;
}
/// @dev Requires that the current call context is NOT performed via a proxy.
/// This is the opposite of `onlyProxy`.
modifier notDelegated() virtual {
_checkNotDelegated();
_;
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* PRIVATE HELPERS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
function _revertUnauthorizedCallContext() private pure {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, 0x9f03a026) // `UnauthorizedCallContext()`.
revert(0x1c, 0x04)
}
}
}{
"remappings": [
"forge-std/=lib/forge-std/src/",
"@openzeppelin/=lib/openzeppelin-contracts/",
"@chainlink/=node_modules/@chainlink/",
"@eth-optimism/=node_modules/@eth-optimism/",
"ds-test/=lib/ds-test/src/",
"erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
"eth-gas-reporter/=node_modules/eth-gas-reporter/",
"halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
"hardhat/=node_modules/hardhat/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/",
"openzeppelin-solidity/=node_modules/openzeppelin-solidity/",
"solady/=node_modules/solady/"
],
"optimizer": {
"enabled": true,
"runs": 1000
},
"metadata": {
"useLiteralContent": false,
"bytecodeHash": "ipfs",
"appendCBOR": true
},
"outputSelection": {
"*": {
"*": [
"evm.bytecode",
"evm.deployedBytecode",
"devdoc",
"userdoc",
"metadata",
"abi"
]
}
},
"evmVersion": "prague",
"viaIR": true
}Contract Security Audit
- No Contract Security Audit Submitted- Submit Audit Here
Contract ABI
API[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"AllowanceOverflow","type":"error"},{"inputs":[],"name":"AllowanceUnderflow","type":"error"},{"inputs":[],"name":"AskPriceZero","type":"error"},{"inputs":[],"name":"InsufficientAllowance","type":"error"},{"inputs":[],"name":"InsufficientBalance","type":"error"},{"inputs":[],"name":"InsufficientBalance","type":"error"},{"inputs":[],"name":"InsufficientLiquidityMinted","type":"error"},{"inputs":[],"name":"InsufficientQuoteToken","type":"error"},{"inputs":[],"name":"InsufficientQuoteUsed","type":"error"},{"inputs":[],"name":"InvalidInitialization","type":"error"},{"inputs":[],"name":"InvalidPermit","type":"error"},{"inputs":[],"name":"InvalidVaultSize","type":"error"},{"inputs":[],"name":"NativeAssetMismatch","type":"error"},{"inputs":[],"name":"NotInitializing","type":"error"},{"inputs":[],"name":"Permit2AllowanceIsFixedAtInfinity","type":"error"},{"inputs":[],"name":"PermitExpired","type":"error"},{"inputs":[],"name":"ReentrancyGuardReentrantCall","type":"error"},{"inputs":[],"name":"TotalSupplyOverflow","type":"error"},{"inputs":[],"name":"Uint96Overflow","type":"error"},{"inputs":[],"name":"Unauthorized","type":"error"},{"inputs":[],"name":"UnauthorizedCallContext","type":"error"},{"inputs":[],"name":"UpgradeFailed","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint64","name":"version","type":"uint64"}],"name":"Initialized","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount2","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"shares","type":"uint256"},{"indexed":false,"internalType":"address","name":"userAddress","type":"address"}],"name":"KuruVaultDeposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount2","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"shares","type":"uint256"},{"indexed":false,"internalType":"address","name":"userAddress","type":"address"}],"name":"KuruVaultWithdraw","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"implementation","type":"address"}],"name":"Upgraded","type":"event"},{"inputs":[],"name":"DOMAIN_SEPARATOR","outputs":[{"internalType":"bytes32","name":"result","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"SPREAD_CONSTANT","outputs":[{"internalType":"uint96","name":"","type":"uint96"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"result","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"result","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"baseDeposit","type":"uint256"},{"internalType":"uint256","name":"quoteDeposit","type":"uint256"},{"internalType":"uint256","name":"minQuoteConsumed","type":"uint256"},{"internalType":"address","name":"receiver","type":"address"}],"name":"deposit","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"_owner","type":"address"},{"internalType":"address","name":"token1_","type":"address"},{"internalType":"address","name":"token2_","type":"address"},{"internalType":"address","name":"_marginAccount","type":"address"},{"internalType":"address","name":"_market","type":"address"},{"internalType":"uint96","name":"_spreadConstant","type":"uint96"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"marginAccount","outputs":[{"internalType":"contract IMarginAccount","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"market","outputs":[{"internalType":"contract IOrderBook","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"marketParams","outputs":[{"internalType":"uint32","name":"pricePrecision","type":"uint32"},{"internalType":"uint96","name":"sizePrecision","type":"uint96"},{"internalType":"address","name":"baseAssetAddress","type":"address"},{"internalType":"uint256","name":"baseAssetDecimals","type":"uint256"},{"internalType":"address","name":"quoteAssetAddress","type":"address"},{"internalType":"uint256","name":"quoteAssetDecimals","type":"uint256"},{"internalType":"uint32","name":"tickSize","type":"uint32"},{"internalType":"uint96","name":"minSize","type":"uint96"},{"internalType":"uint96","name":"maxSize","type":"uint96"},{"internalType":"uint256","name":"takerFeeBps","type":"uint256"},{"internalType":"uint256","name":"makerFeeBps","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"shares","type":"uint256"},{"internalType":"address","name":"receiver","type":"address"}],"name":"mint","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"nonces","outputs":[{"internalType":"uint256","name":"result","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"permit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"asset1","type":"uint256"},{"internalType":"uint256","name":"asset2","type":"uint256"}],"name":"previewDeposit","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"shares","type":"uint256"}],"name":"previewMint","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"shares","type":"uint256"}],"name":"previewWithdraw","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"proxiableUUID","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"setMarketParams","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token1","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token2","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalAssets","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"result","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newImplementation","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"upgradeToAndCall","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_shares","type":"uint256"},{"internalType":"address","name":"_receiver","type":"address"},{"internalType":"address","name":"_owner","type":"address"}],"name":"withdraw","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]Contract Creation Code
60a080604052346099573060805263409feecd195460018116608c5760011c6002600160401b0319016048575b612d98908161009e8239608051818181610fa601526110570152f35b6002600160411b0363409feecd19556001600160401b0360209081527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d29080a1602c565b63f92ee8a95f526004601cfd5b5f80fdfe6080604052600436101561001a575b3615610018575f80fd5b005b5f5f3560e01c806301e1d114146113f457806306fdde0314610dbe578063095ea7b3146113585780630a28a4771461132b57806318160ddd1461130657806323b872dd1461121057806325be124e146111ea578063313ce567146111cf5780633644e515146111475780634f1ef2861461100157806352d1902d14610f9357806370a0823114610f615780637660b15314610f495780637a7af70d14610f1e5780637b9e68f214610e7c5780637ecebe0014610e4a57806380f5560514610e245780638da5cb5b14610dff57806394bf804d14610dc357806395d89b4114610dbe578063a68ee88114610d98578063a9059cbb14610d16578063b3d7f6b914610cec578063b460af94146109ce578063d21220a7146109a7578063d246ce161461041b578063d505accf1461023d578063dd62ed3e14610210578063f2fde38b146101d3578063f742269d146101ac5763fad3cc4b1461017a575061000e565b60803660031901126101a95760206101a1610193611498565b604435602435600435611a29565b604051908152f35b80fd5b50346101a957806003193601126101a95760206001600160a01b0360055416604051908152f35b50346101a95760203660031901126101a9576001600160a01b036101f5611456565b6101fd6124c6565b166001600160a01b031982541617815580f35b50346101a95760403660031901126101a95760206101a161022f611456565b61023761146c565b906119ec565b50346101a95760e03660031901126101a957610257611456565b61025f61146c565b604435606435906084359260ff84168403610417576001600160a01b0316938119156e22d473030f116ddee9f6b43ac78ba38618171561040a576102a161160b565b60208151910120928042116103fd576001600160a01b0360405192169465383775081901600e5285885260c06020600c20938454937f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f8252602082019788528460408301987fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc68a528b606085019a468c528c608087019330855260a08820602e527f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c988525252888a525260a082015220604e526042602c20885260ff1660205260a43560405260c43560605260208060808960015afa90853d51036103f057019055777f5e9f20000000000000000000000000000000000000000084176040526034602c20557f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92590602090a380f35b63ddafbaef88526004601cfd5b631a15a3cc87526004601cfd5b633f68539a86526004601cfd5b8580fd5b50346101a95760c03660031901126101a957610435611456565b61043d61146c565b610445611482565b8361044e611498565b6084356001600160a01b0381168091036109a35760a435936001600160601b03851680950361099f5763409feecd199687548060038a55610966575b506001600160a01b03166001600160a01b03198554161784556001600160a01b03861692836001600160a01b0319600154161760015583151592835f1461095d5760405163313ce56760e01b8152602081600481895afa80156109525760ff918891610933575b505b166002556001600160a01b03831697886001600160a01b03196003541617600355881515978892835f1461092a5760405163313ce56760e01b815293506020846004818e5afa93841561091f578a9460ff918b916108f0575b505b166004556001600160a01b0385166001600160a01b031960055416176005556001600160a01b031960065416176006556001600160601b0319600f541617600f5582856105996116fb565b6108e0575b50506108d0575b5050156108c15781600491604051928380926395d89b4160e01b82525afa9081156108b657829161089c575b50915b1561088057600492604051938480926395d89b4160e01b82525afa801561087557600161067492602080600f9481978a91610853575b505b6040519784899551918291018487015e840190602d60f81b83830152805192839101602183015e0101602d60f81b838201527f4b5552552d414d4d2d5641554c54000000000000000000000000000000000000600282015203016010198101845201826114ae565b80519067ffffffffffffffff821161083f579082916106946010546115d3565b601f8111610792575b50602090601f831160011461070e5785919083610703575b50508160011b915f199060031b1c1916176010555b6106d2575080f35b6002905560016020527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d2602080a180f35b015190505f806106b5565b60108652601f1983169392917f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae67291875b868110610777575083600195961061075f575b505050811b016010556106ca565b01515f1960f88460031b161c191690555f8080610751565b8183015184558795506001909301926020928301920161073e565b9091925060108552601f830160051c7f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae672019060208410610817575b90601f859493920160051c7f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae67201905b818110610809575061069d565b8681558594506001016107fc565b7f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae67291506107cd565b602484634e487b7160e01b81526041600452fd5b61086f91503d808c833e61086781836114ae565b810190611979565b5f61060a565b6040513d86823e3d90fd5b5060209150600f600161067492848061089761193e565b61060c565b6108b091503d8084833e61086781836114ae565b5f6105d1565b6040513d84823e3d90fd5b506108ca61193e565b916105d4565b6108d99161246b565b5f806105a5565b6108e99161246b565b5f8261059e565b610912915060203d602011610918575b61090a81836114ae565b810190611925565b5f61054c565b503d610900565b6040513d8b823e3d90fd5b60ff601261054e565b61094c915060203d6020116109185761090a81836114ae565b5f6104f1565b6040513d89823e3d90fd5b60ff60126104f3565b90979192939450600181811c14303b10156109925760ff1b1b9587939291906001600160a01b0361048a565b63f92ee8a989526004601cfd5b8380fd5b8280fd5b50346101a957806003193601126101a95760206001600160a01b0360015416604051908152f35b5034610ca3576060366003190112610ca3576004356109eb61146c565b6109f3611482565b6109fb6123a6565b6387a211a2600c52805f526020600c20548311610cc4576001600160a01b03811692833303610cb4575b610a2e81612615565b90929693976387a211a2600c52865f526020600c2090815491828811610ca757875f93039055866805345cdf77eb68f44c54036805345cdf77eb68f44c558682527fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef602083a36001600160a01b0360065416803b15610ca3575f928360a4926001600160601b03948560405198899788966327688f0f60e01b8852166004870152166024850152826044850152826064850152151560848401525af18015610c9857610c83575b50856001600160a01b03600554166001600160a01b036001541690803b156109a357604051627b8a6760e11b8152600481018990526001600160a01b0392909216602483015282908290604490829084905af180156108b657610c6e575b506001600160a01b03600554166001600160a01b0360035416813b156109a357604051627b8a6760e11b8152600481018890526001600160a01b0391909116602482015291908290604490829084905af1801561095257610c59575b50917f77e17036448e366e2940534013d9c2770da17cc373dfd9b91c931cf85d80844e91610c0284610bf0604099966001600160a01b03600154168a612c8a565b6001600160a01b036003541687612c8a565b86518681526020810186905260408101919091526001600160a01b03919091166060820152608090a17f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005d82519182526020820152f35b610c648780926114ae565b610417575f610baf565b81610c78916114ae565b61041757855f610b53565b610c909196505f906114ae565b5f945f610af5565b6040513d5f823e3d90fd5b5f80fd5b63f4d678b85f526004601cfd5b610cbf81338461241a565b610a25565b7ff4d678b8000000000000000000000000000000000000000000000000000000005f5260045ffd5b34610ca3576020366003190112610ca3576040610d0a6004356121b7565b82519182526020820152f35b34610ca3576040366003190112610ca357610d2f611456565b602435906387a211a2600c52335f526020600c208054808411610ca75783900390555f526020600c20818154019055602052600c5160601c337fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef602080a3602060405160018152f35b34610ca3575f366003190112610ca35760206001600160601b03600f5416604051908152f35b61140e565b6040366003190112610ca3576040610dd961146c565b610df2610de76004356121b7565b919092828085611a29565b5082519182526020820152f35b34610ca3575f366003190112610ca35760206001600160a01b035f5416604051908152f35b34610ca3575f366003190112610ca35760206001600160a01b0360065416604051908152f35b34610ca3576020366003190112610ca357610e63611456565b6338377508600c525f52602080600c2054604051908152f35b34610ca3575f366003190112610ca3576101606007546001600160a01b03600854166009546001600160601b036001600160a01b03600a5416600b54600c5491600d5494600e5496856040519963ffffffff81168b5260201c1660208a015260408901526060880152608087015260a086015263ffffffff811660c0860152818160201c1660e086015260801c16610100840152610120830152610140820152f35b34610ca3576040366003190112610ca35760206101a1610f3c61221e565b9050602435600435612367565b34610ca3575f366003190112610ca3576100186116fb565b34610ca3576020366003190112610ca357610f7a611456565b6387a211a2600c525f52602080600c2054604051908152f35b34610ca3575f366003190112610ca357307f000000000000000000000000000000000000000000000000000000000000000003610ff45760206040517f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc8152f35b639f03a0265f526004601cfd5b6040366003190112610ca357611015611456565b6024359067ffffffffffffffff8211610ca35736602383011215610ca35781600401359067ffffffffffffffff8211610ca3573660248385010111610ca357307f000000000000000000000000000000000000000000000000000000000000000014610ff4576001600160a01b039061108c6124c6565b163d5f526352d1902d6001527f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc602060016004601d855afa510361113957807fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b5f80a281817f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5561111957005b815f926024604051950185378338925af41561113157005b3d5f823e3d90fd5b6355299b496001526004601dfd5b34610ca3575f366003190112610ca357602060a061116361160b565b828151910120604051907f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f8252838201527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6604082015246606082015230608082015220604051908152f35b34610ca3575f366003190112610ca357602060405160128152f35b34610ca3575f366003190112610ca35760206001600160a01b0360035416604051908152f35b34610ca3576060366003190112610ca357611229611456565b61123161146c565b604435908260601b6e22d473030f116ddee9f6b43ac78ba333036112bc575b6387a211a217600c526020600c208054808411610ca75783900390555f526020600c208181540190556020526001600160a01b03600c5160601c91167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef602080a3602060405160018152f35b33602052637f5e9f208117600c526034600c209081549182196112e2575b509050611250565b8285116112f957846387a211a293039055856112da565b6313be252b5f526004601cfd5b34610ca3575f366003190112610ca35760206805345cdf77eb68f44c54604051908152f35b34610ca3576020366003190112610ca3576040611349600435612615565b50505082519182526020820152f35b34610ca3576040366003190112610ca357611371611456565b602435908119156e22d473030f116ddee9f6b43ac78ba36001600160a01b0383161817156113e757602052637f5e9f20600c52335f52806034600c20555f52602c5160601c337f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92560205fa3602060405160018152f35b633f68539a5f526004601cfd5b34610ca3575f366003190112610ca3576040610d0a6114e4565b34610ca3575f366003190112610ca3576020604061142a61160b565b815192839181835280519182918282860152018484015e5f828201840152601f01601f19168101030190f35b600435906001600160a01b0382168203610ca357565b602435906001600160a01b0382168203610ca357565b604435906001600160a01b0382168203610ca357565b606435906001600160a01b0382168203610ca357565b90601f8019910116810190811067ffffffffffffffff8211176114d057604052565b634e487b7160e01b5f52604160045260245ffd5b60055460015460405163d4fac45d60e01b81523060048201526001600160a01b039182166024820152929116602083604481845afa928315610c98575f9361159e575b5060035460405163d4fac45d60e01b81523060048201526001600160a01b0390911660248201529290602090849060449082905afa928315610c98575f9361156e57509190565b9092506020813d602011611596575b8161158a602093836114ae565b81010312610ca3575190565b3d915061157d565b92506020833d6020116115cb575b816115b9602093836114ae565b81010312610ca3579151916020611527565b3d91506115ac565b90600182811c92168015611601575b60208310146115ed57565b634e487b7160e01b5f52602260045260245ffd5b91607f16916115e2565b604051905f826010549161161e836115d3565b80835292600181169081156116a35750600114611644575b611642925003836114ae565b565b5060105f90815290917f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae6725b81831061168757505090602061164292820101611636565b602091935080600191548385890101520191019091849261166f565b6020925061164294915060ff191682840152151560051b820101611636565b519063ffffffff82168203610ca357565b51906001600160601b0382168203610ca357565b51906001600160a01b0382168203610ca357565b60046101606001600160a01b0360065416604051928380927f90c9427c0000000000000000000000000000000000000000000000000000000082525afa8015610c98575f905f925f915f915f915f915f5f915f935f915f91611850575b506001600160a01b039897969492899694926fffffffffffffffffffffffff0000000092600e55600d557fffffffff000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffff0000000000000000000000000000000063ffffffff600c5493169560801b169116179160201b161717600c55600b55166001600160a01b0319600a541617600a55600955166001600160a01b031960085416176008557fffffffffffffffffffffffffffffffff000000000000000000000000000000006fffffffffffffffffffffffff0000000063ffffffff60075493169360201b1691161717600755565b9a5050505050505050505050610160813d821161191d575b8161187661016093836114ae565b81010312610ca357611887816116c2565b906001600160a01b0361189c602083016116d3565b916118a9604082016116e7565b93826060830151926118bd608082016116e7565b9260a0820151916118d060c082016116c2565b916fffffffffffffffffffffffff000000006118ee60e084016116d3565b6118fb61010085016116d3565b610120850151610140909501519b9c939b939a96975095909493929150611758565b3d9150611868565b90816020910312610ca3575160ff81168103610ca35790565b6040519061194d6040836114ae565b600382527f4d4f4e00000000000000000000000000000000000000000000000000000000006020830152565b602081830312610ca35780519067ffffffffffffffff8211610ca3570181601f82011215610ca35780519067ffffffffffffffff82116114d057604051926119cb601f8401601f1916602001856114ae565b82845260208383010111610ca357815f9260208093018386015e8301015290565b906e22d473030f116ddee9f6b43ac78ba36001600160a01b03821614611a2257602052637f5e9f20600c525f526034600c205490565b50505f1990565b5f9493919291611a376123a6565b6001600160a01b036001541615808015612165575b1561215d571561215757815b3410155b1561212f578391611a6b61221e565b6805345cdf77eb68f44c549194909115611f875750611ab2611aa1611a908385612ad6565b611a9b600b54612178565b90612186565b611aac600954612178565b90612199565b958611611f5f57611ac4818784612367565b9593611ad08785612b5e565b8615611f3757611af0611aeb846001600160601b0393612211565b612bc4565b9116908115611f0f576001600160601b03168015611f0f576001600160a01b03600654166001600160601b03600f5416612710018061271011611efb576127108502908582046127101486151715611efb57611b53611b58928260011c90612211565b612199565b92813b15610ca3575f60a492819560405197889687956327688f0f60e01b875260048701526024860152604485015260648401528160848401525af18015610c9857611ee6575b506001600160a01b0360015416906001600160a01b03600354169188811591825f14611e68576001600160a01b0360055416803b156109a357604051638340f54960e01b81523060048201526001600160a01b039290921660248301526044820185905282908290606490829088905af180156108b657611e53575b50505b8215908115611da257896001600160a01b0360055416803b15611d9e57604051638340f54960e01b81523060048201526001600160a01b03969096166024870152604486018890528190869060649082908b905af1948515611d91578795611d70575b505093611cf1917f51b403c6efd91871c21df6b993560fc987985297d57a4eafbacca48d0f73009b955b15611d565750611cbb8334612501565b80611d46575b50876040519485948592936001600160a01b03919594606093608086019786526020860152604085015216910152565b0390a110611d1e57917f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005d565b6004837fa725e26b000000000000000000000000000000000000000000000000000000008152fd5b611d509033612c6e565b5f611cc1565b15611d6a57611d658434612501565b611cbb565b89611cbb565b819293955090611d7f916114ae565b611d8d57918492895f611c81565b8880fd5b50604051903d90823e3d90fd5b5080fd5b611dae86303387612cfb565b896001600160a01b0360055416803b15611d9e57604051638340f54960e01b81523060048201526001600160a01b039690961660248701526044860188905281908690606490829084905af1948515611d91578795611e36575b505093611cf1917f51b403c6efd91871c21df6b993560fc987985297d57a4eafbacca48d0f73009b95611cab565b819293955090611e45916114ae565b611d8d57918492895f611e08565b81611e5d916114ae565b611d8d57885f611c1b565b611e7484303384612cfb565b6001600160a01b0360055416803b156109a357604051638340f54960e01b81523060048201526001600160a01b039290921660248301526044820185905282908290606490829084905af180156108b657611ed1575b5050611c1e565b81611edb916114ae565b611d8d57885f611eca565b611ef39197505f906114ae565b5f955f611b9f565b634e487b7160e01b5f52601160045260245ffd5b7fe9ab1f0b000000000000000000000000000000000000000000000000000000005f5260045ffd5b7fd226f9d4000000000000000000000000000000000000000000000000000000005f5260045ffd5b7fb9873846000000000000000000000000000000000000000000000000000000005f5260045ffd5b939050611f948683612186565b8070ffffffffffffffffffffffffffffffffff1060071b81811c68ffffffffffffffffff1060061b1781811c64ffffffffff1060051b1781811c62ffffff1060041b1760b56201000083831c019160011c1b0260121c8082040160011c8082040160011c8082040160011c8082040160011c8082040160011c8082040160011c8082040160011c80809204109003956001600160a01b03600554166805345cdf77eb68f44c546103e88101908110612122576805345cdf77eb68f44c556387a211a2600c525f526020600c206103e881540190556103e8602052600c5160601c5f7fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef602080a36103e7198701968711611efb576120c0906120b58886612b5e565b611a9b600954612178565b670de0b6b3a7640000810290808204670de0b6b3a76400001490151715611efb5782611b536120f492611aac600b54612178565b9081611ad0577f98de0cd0000000000000000000000000000000000000000000000000000000005f5260045ffd5b63e5cfe9575f526004601cfd5b7fcd41a9e3000000000000000000000000000000000000000000000000000000005f5260045ffd5b83611a58565b503415611a5c565b506001600160a01b036003541615611a4c565b604d8111611efb57600a0a90565b81810292918115918404141715611efb57565b81156121a3570490565b634e487b7160e01b5f52601260045260245ffd5b6805345cdf77eb68f44c5490811561220957611aa1611a906121ff6121f89594612206946121e36114e4565b6121ee99919961221e565b9a8b9291506129ff565b5090612ab3565b9485612ad6565b90565b50505f905f90565b91908201809211611efb57565b60049060206001600160a01b036006541660405193848092632889856360e11b82525afa918215610c98575f9261232b575b506001600160601b03600f54169182614e20019081614e2011611efb5760049361228b926001600160601b036122869316612b04565b612b22565b60206001600160a01b0360065416604051948580926311d2e12360e21b82525afa928315610c98575f936122f5575b506122dc6122f1916001600160601b036122d5600954612178565b9116612186565b6001600160601b0360075460201c1690612199565b9190565b9092506020813d602011612323575b81612311602093836114ae565b81010312610ca35751916122dc6122ba565b3d9150612304565b9091506020813d60201161235f575b81612347602093836114ae565b81010312610ca357612358906116d3565b905f612250565b3d915061233a565b6123959061238161239b939461237b6114e4565b906129ff565b92906805345cdf77eb68f44c548093612ab3565b93612ab3565b818110908218021890565b7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005c6123f25760017f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005d565b7f3ee5aeb5000000000000000000000000000000000000000000000000000000005f5260045ffd5b906e22d473030f116ddee9f6b43ac78ba36001600160a01b0382161461246657602052637f5e9f20600c525f526034600c20908154801961245a57505050565b8082116112f957039055565b505050565b906014525f196034526f095ea7b30000000000000000000000005f5260205f6044601082855af1908160015f511416156124a8575b50505f603452565b3b153d1710156124b9575f806124a0565b633e3f8f735f526004601cfd5b6001600160a01b035f541633036124d957565b7f82b42900000000000000000000000000000000000000000000000000000000005f5260045ffd5b91908203918211611efb57565b60405190610160820182811067ffffffffffffffff8211176114d057604052816001600160601b0360075463ffffffff8116835260201c1660208201526001600160a01b0360085416604082015260095460608201526001600160a01b03600a54166080820152600b5460a08201526001600160601b03600c5463ffffffff811660c0840152818160201c1660e084015260801c16610100820152600d54610120820152610140600e54910152565b81810392915f138015828513169184121617611efb57565b90815f0391600160ff1b8114600116611efb575f19830503611efb57565b81810292915f8212600160ff1b821416611efb578184051490151715611efb57565b600460206001600160a01b036006541660405192838092632889856360e11b82525afa908115610c98575f916129c5575b506001600160601b03600f54169081614e20019081614e2011611efb5760049261267c926001600160601b036122869316612ab3565b9260206001600160a01b0360065416604051938480926311d2e12360e21b82525afa918215610c98575f9261298d575b506126c96122dc6004956001600160601b036122d5600954612178565b91506126ee611aeb6805345cdf77eb68f44c54936126e8858288612ab3565b90612501565b9290936101006001600160a01b0360065416604051978880927f88bb4f600000000000000000000000000000000000000000000000000000000082525afa908115610c98575f965f975f915f946128f5575b506001600160601b039861275261250e565b9061275b6114e4565b9b909616906001600160601b038b1682108015906128d9575b156128b7576001600160601b0316916127a361279084846125bd565b61279d6060840151612178565b906125f3565b9460208201956001600160601b038751169081156121a357600160ff1b81145f19831416611efb578a976128096128179661279d956001600160601b03612800670de0b6b3a76400009b60a09861280f98059d8388511691612b04565b94511691612ab3565b906125bd565b920151612178565b05935f82121561289a5761283e61284a93612838612844936126e8866125d5565b86612ab3565b916125d5565b90612211565b965b5f83121561287d57612872936128449261286c61283e936126e8876125d5565b90612ab3565b935b93929190600190565b9261288f9161286c8461289496612211565b612501565b93612874565b6128b1926128ab8361288f93612211565b85612ab3565b9661284c565b5050505050956128cc836128d2949389612ab3565b96612ab3565b9291905f90565b506001600160601b038a166001600160601b0382161015612774565b935050509550610100813d8211612985575b8161291561010093836114ae565b81010312610ca3576001600160601b039561292f826116e7565b50602082015191612942604082016116d3565b60608201519361297a60e0612959608086016116d3565b9461296660a082016116d3565b5061297360c082016116d3565b50016116d3565b509093919398612740565b3d9150612907565b9391506020843d6020116129bd575b816129a9602093836114ae565b81010312610ca357925190926126c96126ac565b3d915061299c565b90506020813d6020116129f7575b816129e0602093836114ae565b81010312610ca3576129f1906116d3565b5f612646565b3d91506129d3565b92919092612a1582612a0f61250e565b92612186565b906060612a2a60a0830193611a9b8551612178565b910194612a378651612178565b91670de0b6b3a7640000830292808404670de0b6b3a76400001490151715611efb57612a6b92612a6691612199565b612211565b60011c93670de0b6b3a7640000850292858404670de0b6b3a76400001486151715611efb57612aae612aa76122f195611a9b611aac9551612178565b9351612178565b612186565b8181029181159183041417820215612ac9570490565b63ad251c275f526004601cfd5b8181029181159183041417670de0b6b3a76400000215612ac957670de0b6b3a7640000808204910615150190565b818102929181159184041417810215612ac957808204910615150190565b906001600160601b038216918203612b3657565b7fe233e012000000000000000000000000000000000000000000000000000000005f5260045ffd5b6805345cdf77eb68f44c54828101908110612122576805345cdf77eb68f44c556387a211a2600c525f526020600c20818154019055602052600c5160601c5f7fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef602080a3565b90612bcd61250e565b916001600160601b03600f541692612be58285612186565b91612bff60208301936001600160601b0385511690612186565b9285614e20019384614e2011611efb57612c40612c4993612c3a6122866001600160601b0394611aac6060612c509a019a611a9b8c51612178565b98612186565b91511690612186565b9151612178565b9081614e200291614e20830403611efb576122069161228691612199565b5f80809338935af115612c7d57565b63b12d13eb5f526004601cfd5b90916001600160a01b038316612ca4576116429250612c6e565b6014526034526fa9059cbb0000000000000000000000005f5260205f6044601082855af1908160015f51141615612cdd5750505f603452565b3b153d171015612cee575f806124a0565b6390b8ec185f526004601cfd5b916040519360605260405260601b602c526f23b872dd000000000000000000000000600c5260205f6064601c82855af1908160015f51141615612d44575b50505f606052604052565b3b153d171015612d55575f80612d39565b637939f4245f526004601cfdfea26469706673582212207c168ad73996dc9acb7aab7fca88f6d8156ba1bf388fb686630f74dc6407505c64736f6c634300081e0033
Deployed Bytecode
0x6080604052600436101561001a575b3615610018575f80fd5b005b5f5f3560e01c806301e1d114146113f457806306fdde0314610dbe578063095ea7b3146113585780630a28a4771461132b57806318160ddd1461130657806323b872dd1461121057806325be124e146111ea578063313ce567146111cf5780633644e515146111475780634f1ef2861461100157806352d1902d14610f9357806370a0823114610f615780637660b15314610f495780637a7af70d14610f1e5780637b9e68f214610e7c5780637ecebe0014610e4a57806380f5560514610e245780638da5cb5b14610dff57806394bf804d14610dc357806395d89b4114610dbe578063a68ee88114610d98578063a9059cbb14610d16578063b3d7f6b914610cec578063b460af94146109ce578063d21220a7146109a7578063d246ce161461041b578063d505accf1461023d578063dd62ed3e14610210578063f2fde38b146101d3578063f742269d146101ac5763fad3cc4b1461017a575061000e565b60803660031901126101a95760206101a1610193611498565b604435602435600435611a29565b604051908152f35b80fd5b50346101a957806003193601126101a95760206001600160a01b0360055416604051908152f35b50346101a95760203660031901126101a9576001600160a01b036101f5611456565b6101fd6124c6565b166001600160a01b031982541617815580f35b50346101a95760403660031901126101a95760206101a161022f611456565b61023761146c565b906119ec565b50346101a95760e03660031901126101a957610257611456565b61025f61146c565b604435606435906084359260ff84168403610417576001600160a01b0316938119156e22d473030f116ddee9f6b43ac78ba38618171561040a576102a161160b565b60208151910120928042116103fd576001600160a01b0360405192169465383775081901600e5285885260c06020600c20938454937f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f8252602082019788528460408301987fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc68a528b606085019a468c528c608087019330855260a08820602e527f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c988525252888a525260a082015220604e526042602c20885260ff1660205260a43560405260c43560605260208060808960015afa90853d51036103f057019055777f5e9f20000000000000000000000000000000000000000084176040526034602c20557f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92590602090a380f35b63ddafbaef88526004601cfd5b631a15a3cc87526004601cfd5b633f68539a86526004601cfd5b8580fd5b50346101a95760c03660031901126101a957610435611456565b61043d61146c565b610445611482565b8361044e611498565b6084356001600160a01b0381168091036109a35760a435936001600160601b03851680950361099f5763409feecd199687548060038a55610966575b506001600160a01b03166001600160a01b03198554161784556001600160a01b03861692836001600160a01b0319600154161760015583151592835f1461095d5760405163313ce56760e01b8152602081600481895afa80156109525760ff918891610933575b505b166002556001600160a01b03831697886001600160a01b03196003541617600355881515978892835f1461092a5760405163313ce56760e01b815293506020846004818e5afa93841561091f578a9460ff918b916108f0575b505b166004556001600160a01b0385166001600160a01b031960055416176005556001600160a01b031960065416176006556001600160601b0319600f541617600f5582856105996116fb565b6108e0575b50506108d0575b5050156108c15781600491604051928380926395d89b4160e01b82525afa9081156108b657829161089c575b50915b1561088057600492604051938480926395d89b4160e01b82525afa801561087557600161067492602080600f9481978a91610853575b505b6040519784899551918291018487015e840190602d60f81b83830152805192839101602183015e0101602d60f81b838201527f4b5552552d414d4d2d5641554c54000000000000000000000000000000000000600282015203016010198101845201826114ae565b80519067ffffffffffffffff821161083f579082916106946010546115d3565b601f8111610792575b50602090601f831160011461070e5785919083610703575b50508160011b915f199060031b1c1916176010555b6106d2575080f35b6002905560016020527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d2602080a180f35b015190505f806106b5565b60108652601f1983169392917f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae67291875b868110610777575083600195961061075f575b505050811b016010556106ca565b01515f1960f88460031b161c191690555f8080610751565b8183015184558795506001909301926020928301920161073e565b9091925060108552601f830160051c7f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae672019060208410610817575b90601f859493920160051c7f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae67201905b818110610809575061069d565b8681558594506001016107fc565b7f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae67291506107cd565b602484634e487b7160e01b81526041600452fd5b61086f91503d808c833e61086781836114ae565b810190611979565b5f61060a565b6040513d86823e3d90fd5b5060209150600f600161067492848061089761193e565b61060c565b6108b091503d8084833e61086781836114ae565b5f6105d1565b6040513d84823e3d90fd5b506108ca61193e565b916105d4565b6108d99161246b565b5f806105a5565b6108e99161246b565b5f8261059e565b610912915060203d602011610918575b61090a81836114ae565b810190611925565b5f61054c565b503d610900565b6040513d8b823e3d90fd5b60ff601261054e565b61094c915060203d6020116109185761090a81836114ae565b5f6104f1565b6040513d89823e3d90fd5b60ff60126104f3565b90979192939450600181811c14303b10156109925760ff1b1b9587939291906001600160a01b0361048a565b63f92ee8a989526004601cfd5b8380fd5b8280fd5b50346101a957806003193601126101a95760206001600160a01b0360015416604051908152f35b5034610ca3576060366003190112610ca3576004356109eb61146c565b6109f3611482565b6109fb6123a6565b6387a211a2600c52805f526020600c20548311610cc4576001600160a01b03811692833303610cb4575b610a2e81612615565b90929693976387a211a2600c52865f526020600c2090815491828811610ca757875f93039055866805345cdf77eb68f44c54036805345cdf77eb68f44c558682527fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef602083a36001600160a01b0360065416803b15610ca3575f928360a4926001600160601b03948560405198899788966327688f0f60e01b8852166004870152166024850152826044850152826064850152151560848401525af18015610c9857610c83575b50856001600160a01b03600554166001600160a01b036001541690803b156109a357604051627b8a6760e11b8152600481018990526001600160a01b0392909216602483015282908290604490829084905af180156108b657610c6e575b506001600160a01b03600554166001600160a01b0360035416813b156109a357604051627b8a6760e11b8152600481018890526001600160a01b0391909116602482015291908290604490829084905af1801561095257610c59575b50917f77e17036448e366e2940534013d9c2770da17cc373dfd9b91c931cf85d80844e91610c0284610bf0604099966001600160a01b03600154168a612c8a565b6001600160a01b036003541687612c8a565b86518681526020810186905260408101919091526001600160a01b03919091166060820152608090a17f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005d82519182526020820152f35b610c648780926114ae565b610417575f610baf565b81610c78916114ae565b61041757855f610b53565b610c909196505f906114ae565b5f945f610af5565b6040513d5f823e3d90fd5b5f80fd5b63f4d678b85f526004601cfd5b610cbf81338461241a565b610a25565b7ff4d678b8000000000000000000000000000000000000000000000000000000005f5260045ffd5b34610ca3576020366003190112610ca3576040610d0a6004356121b7565b82519182526020820152f35b34610ca3576040366003190112610ca357610d2f611456565b602435906387a211a2600c52335f526020600c208054808411610ca75783900390555f526020600c20818154019055602052600c5160601c337fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef602080a3602060405160018152f35b34610ca3575f366003190112610ca35760206001600160601b03600f5416604051908152f35b61140e565b6040366003190112610ca3576040610dd961146c565b610df2610de76004356121b7565b919092828085611a29565b5082519182526020820152f35b34610ca3575f366003190112610ca35760206001600160a01b035f5416604051908152f35b34610ca3575f366003190112610ca35760206001600160a01b0360065416604051908152f35b34610ca3576020366003190112610ca357610e63611456565b6338377508600c525f52602080600c2054604051908152f35b34610ca3575f366003190112610ca3576101606007546001600160a01b03600854166009546001600160601b036001600160a01b03600a5416600b54600c5491600d5494600e5496856040519963ffffffff81168b5260201c1660208a015260408901526060880152608087015260a086015263ffffffff811660c0860152818160201c1660e086015260801c16610100840152610120830152610140820152f35b34610ca3576040366003190112610ca35760206101a1610f3c61221e565b9050602435600435612367565b34610ca3575f366003190112610ca3576100186116fb565b34610ca3576020366003190112610ca357610f7a611456565b6387a211a2600c525f52602080600c2054604051908152f35b34610ca3575f366003190112610ca357307f000000000000000000000000dc2a82e321866c30d62077945e067172c5f970f403610ff45760206040517f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc8152f35b639f03a0265f526004601cfd5b6040366003190112610ca357611015611456565b6024359067ffffffffffffffff8211610ca35736602383011215610ca35781600401359067ffffffffffffffff8211610ca3573660248385010111610ca357307f000000000000000000000000dc2a82e321866c30d62077945e067172c5f970f414610ff4576001600160a01b039061108c6124c6565b163d5f526352d1902d6001527f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc602060016004601d855afa510361113957807fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b5f80a281817f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5561111957005b815f926024604051950185378338925af41561113157005b3d5f823e3d90fd5b6355299b496001526004601dfd5b34610ca3575f366003190112610ca357602060a061116361160b565b828151910120604051907f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f8252838201527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6604082015246606082015230608082015220604051908152f35b34610ca3575f366003190112610ca357602060405160128152f35b34610ca3575f366003190112610ca35760206001600160a01b0360035416604051908152f35b34610ca3576060366003190112610ca357611229611456565b61123161146c565b604435908260601b6e22d473030f116ddee9f6b43ac78ba333036112bc575b6387a211a217600c526020600c208054808411610ca75783900390555f526020600c208181540190556020526001600160a01b03600c5160601c91167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef602080a3602060405160018152f35b33602052637f5e9f208117600c526034600c209081549182196112e2575b509050611250565b8285116112f957846387a211a293039055856112da565b6313be252b5f526004601cfd5b34610ca3575f366003190112610ca35760206805345cdf77eb68f44c54604051908152f35b34610ca3576020366003190112610ca3576040611349600435612615565b50505082519182526020820152f35b34610ca3576040366003190112610ca357611371611456565b602435908119156e22d473030f116ddee9f6b43ac78ba36001600160a01b0383161817156113e757602052637f5e9f20600c52335f52806034600c20555f52602c5160601c337f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92560205fa3602060405160018152f35b633f68539a5f526004601cfd5b34610ca3575f366003190112610ca3576040610d0a6114e4565b34610ca3575f366003190112610ca3576020604061142a61160b565b815192839181835280519182918282860152018484015e5f828201840152601f01601f19168101030190f35b600435906001600160a01b0382168203610ca357565b602435906001600160a01b0382168203610ca357565b604435906001600160a01b0382168203610ca357565b606435906001600160a01b0382168203610ca357565b90601f8019910116810190811067ffffffffffffffff8211176114d057604052565b634e487b7160e01b5f52604160045260245ffd5b60055460015460405163d4fac45d60e01b81523060048201526001600160a01b039182166024820152929116602083604481845afa928315610c98575f9361159e575b5060035460405163d4fac45d60e01b81523060048201526001600160a01b0390911660248201529290602090849060449082905afa928315610c98575f9361156e57509190565b9092506020813d602011611596575b8161158a602093836114ae565b81010312610ca3575190565b3d915061157d565b92506020833d6020116115cb575b816115b9602093836114ae565b81010312610ca3579151916020611527565b3d91506115ac565b90600182811c92168015611601575b60208310146115ed57565b634e487b7160e01b5f52602260045260245ffd5b91607f16916115e2565b604051905f826010549161161e836115d3565b80835292600181169081156116a35750600114611644575b611642925003836114ae565b565b5060105f90815290917f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae6725b81831061168757505090602061164292820101611636565b602091935080600191548385890101520191019091849261166f565b6020925061164294915060ff191682840152151560051b820101611636565b519063ffffffff82168203610ca357565b51906001600160601b0382168203610ca357565b51906001600160a01b0382168203610ca357565b60046101606001600160a01b0360065416604051928380927f90c9427c0000000000000000000000000000000000000000000000000000000082525afa8015610c98575f905f925f915f915f915f915f5f915f935f915f91611850575b506001600160a01b039897969492899694926fffffffffffffffffffffffff0000000092600e55600d557fffffffff000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffff0000000000000000000000000000000063ffffffff600c5493169560801b169116179160201b161717600c55600b55166001600160a01b0319600a541617600a55600955166001600160a01b031960085416176008557fffffffffffffffffffffffffffffffff000000000000000000000000000000006fffffffffffffffffffffffff0000000063ffffffff60075493169360201b1691161717600755565b9a5050505050505050505050610160813d821161191d575b8161187661016093836114ae565b81010312610ca357611887816116c2565b906001600160a01b0361189c602083016116d3565b916118a9604082016116e7565b93826060830151926118bd608082016116e7565b9260a0820151916118d060c082016116c2565b916fffffffffffffffffffffffff000000006118ee60e084016116d3565b6118fb61010085016116d3565b610120850151610140909501519b9c939b939a96975095909493929150611758565b3d9150611868565b90816020910312610ca3575160ff81168103610ca35790565b6040519061194d6040836114ae565b600382527f4d4f4e00000000000000000000000000000000000000000000000000000000006020830152565b602081830312610ca35780519067ffffffffffffffff8211610ca3570181601f82011215610ca35780519067ffffffffffffffff82116114d057604051926119cb601f8401601f1916602001856114ae565b82845260208383010111610ca357815f9260208093018386015e8301015290565b906e22d473030f116ddee9f6b43ac78ba36001600160a01b03821614611a2257602052637f5e9f20600c525f526034600c205490565b50505f1990565b5f9493919291611a376123a6565b6001600160a01b036001541615808015612165575b1561215d571561215757815b3410155b1561212f578391611a6b61221e565b6805345cdf77eb68f44c549194909115611f875750611ab2611aa1611a908385612ad6565b611a9b600b54612178565b90612186565b611aac600954612178565b90612199565b958611611f5f57611ac4818784612367565b9593611ad08785612b5e565b8615611f3757611af0611aeb846001600160601b0393612211565b612bc4565b9116908115611f0f576001600160601b03168015611f0f576001600160a01b03600654166001600160601b03600f5416612710018061271011611efb576127108502908582046127101486151715611efb57611b53611b58928260011c90612211565b612199565b92813b15610ca3575f60a492819560405197889687956327688f0f60e01b875260048701526024860152604485015260648401528160848401525af18015610c9857611ee6575b506001600160a01b0360015416906001600160a01b03600354169188811591825f14611e68576001600160a01b0360055416803b156109a357604051638340f54960e01b81523060048201526001600160a01b039290921660248301526044820185905282908290606490829088905af180156108b657611e53575b50505b8215908115611da257896001600160a01b0360055416803b15611d9e57604051638340f54960e01b81523060048201526001600160a01b03969096166024870152604486018890528190869060649082908b905af1948515611d91578795611d70575b505093611cf1917f51b403c6efd91871c21df6b993560fc987985297d57a4eafbacca48d0f73009b955b15611d565750611cbb8334612501565b80611d46575b50876040519485948592936001600160a01b03919594606093608086019786526020860152604085015216910152565b0390a110611d1e57917f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005d565b6004837fa725e26b000000000000000000000000000000000000000000000000000000008152fd5b611d509033612c6e565b5f611cc1565b15611d6a57611d658434612501565b611cbb565b89611cbb565b819293955090611d7f916114ae565b611d8d57918492895f611c81565b8880fd5b50604051903d90823e3d90fd5b5080fd5b611dae86303387612cfb565b896001600160a01b0360055416803b15611d9e57604051638340f54960e01b81523060048201526001600160a01b039690961660248701526044860188905281908690606490829084905af1948515611d91578795611e36575b505093611cf1917f51b403c6efd91871c21df6b993560fc987985297d57a4eafbacca48d0f73009b95611cab565b819293955090611e45916114ae565b611d8d57918492895f611e08565b81611e5d916114ae565b611d8d57885f611c1b565b611e7484303384612cfb565b6001600160a01b0360055416803b156109a357604051638340f54960e01b81523060048201526001600160a01b039290921660248301526044820185905282908290606490829084905af180156108b657611ed1575b5050611c1e565b81611edb916114ae565b611d8d57885f611eca565b611ef39197505f906114ae565b5f955f611b9f565b634e487b7160e01b5f52601160045260245ffd5b7fe9ab1f0b000000000000000000000000000000000000000000000000000000005f5260045ffd5b7fd226f9d4000000000000000000000000000000000000000000000000000000005f5260045ffd5b7fb9873846000000000000000000000000000000000000000000000000000000005f5260045ffd5b939050611f948683612186565b8070ffffffffffffffffffffffffffffffffff1060071b81811c68ffffffffffffffffff1060061b1781811c64ffffffffff1060051b1781811c62ffffff1060041b1760b56201000083831c019160011c1b0260121c8082040160011c8082040160011c8082040160011c8082040160011c8082040160011c8082040160011c8082040160011c80809204109003956001600160a01b03600554166805345cdf77eb68f44c546103e88101908110612122576805345cdf77eb68f44c556387a211a2600c525f526020600c206103e881540190556103e8602052600c5160601c5f7fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef602080a36103e7198701968711611efb576120c0906120b58886612b5e565b611a9b600954612178565b670de0b6b3a7640000810290808204670de0b6b3a76400001490151715611efb5782611b536120f492611aac600b54612178565b9081611ad0577f98de0cd0000000000000000000000000000000000000000000000000000000005f5260045ffd5b63e5cfe9575f526004601cfd5b7fcd41a9e3000000000000000000000000000000000000000000000000000000005f5260045ffd5b83611a58565b503415611a5c565b506001600160a01b036003541615611a4c565b604d8111611efb57600a0a90565b81810292918115918404141715611efb57565b81156121a3570490565b634e487b7160e01b5f52601260045260245ffd5b6805345cdf77eb68f44c5490811561220957611aa1611a906121ff6121f89594612206946121e36114e4565b6121ee99919961221e565b9a8b9291506129ff565b5090612ab3565b9485612ad6565b90565b50505f905f90565b91908201809211611efb57565b60049060206001600160a01b036006541660405193848092632889856360e11b82525afa918215610c98575f9261232b575b506001600160601b03600f54169182614e20019081614e2011611efb5760049361228b926001600160601b036122869316612b04565b612b22565b60206001600160a01b0360065416604051948580926311d2e12360e21b82525afa928315610c98575f936122f5575b506122dc6122f1916001600160601b036122d5600954612178565b9116612186565b6001600160601b0360075460201c1690612199565b9190565b9092506020813d602011612323575b81612311602093836114ae565b81010312610ca35751916122dc6122ba565b3d9150612304565b9091506020813d60201161235f575b81612347602093836114ae565b81010312610ca357612358906116d3565b905f612250565b3d915061233a565b6123959061238161239b939461237b6114e4565b906129ff565b92906805345cdf77eb68f44c548093612ab3565b93612ab3565b818110908218021890565b7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005c6123f25760017f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005d565b7f3ee5aeb5000000000000000000000000000000000000000000000000000000005f5260045ffd5b906e22d473030f116ddee9f6b43ac78ba36001600160a01b0382161461246657602052637f5e9f20600c525f526034600c20908154801961245a57505050565b8082116112f957039055565b505050565b906014525f196034526f095ea7b30000000000000000000000005f5260205f6044601082855af1908160015f511416156124a8575b50505f603452565b3b153d1710156124b9575f806124a0565b633e3f8f735f526004601cfd5b6001600160a01b035f541633036124d957565b7f82b42900000000000000000000000000000000000000000000000000000000005f5260045ffd5b91908203918211611efb57565b60405190610160820182811067ffffffffffffffff8211176114d057604052816001600160601b0360075463ffffffff8116835260201c1660208201526001600160a01b0360085416604082015260095460608201526001600160a01b03600a54166080820152600b5460a08201526001600160601b03600c5463ffffffff811660c0840152818160201c1660e084015260801c16610100820152600d54610120820152610140600e54910152565b81810392915f138015828513169184121617611efb57565b90815f0391600160ff1b8114600116611efb575f19830503611efb57565b81810292915f8212600160ff1b821416611efb578184051490151715611efb57565b600460206001600160a01b036006541660405192838092632889856360e11b82525afa908115610c98575f916129c5575b506001600160601b03600f54169081614e20019081614e2011611efb5760049261267c926001600160601b036122869316612ab3565b9260206001600160a01b0360065416604051938480926311d2e12360e21b82525afa918215610c98575f9261298d575b506126c96122dc6004956001600160601b036122d5600954612178565b91506126ee611aeb6805345cdf77eb68f44c54936126e8858288612ab3565b90612501565b9290936101006001600160a01b0360065416604051978880927f88bb4f600000000000000000000000000000000000000000000000000000000082525afa908115610c98575f965f975f915f946128f5575b506001600160601b039861275261250e565b9061275b6114e4565b9b909616906001600160601b038b1682108015906128d9575b156128b7576001600160601b0316916127a361279084846125bd565b61279d6060840151612178565b906125f3565b9460208201956001600160601b038751169081156121a357600160ff1b81145f19831416611efb578a976128096128179661279d956001600160601b03612800670de0b6b3a76400009b60a09861280f98059d8388511691612b04565b94511691612ab3565b906125bd565b920151612178565b05935f82121561289a5761283e61284a93612838612844936126e8866125d5565b86612ab3565b916125d5565b90612211565b965b5f83121561287d57612872936128449261286c61283e936126e8876125d5565b90612ab3565b935b93929190600190565b9261288f9161286c8461289496612211565b612501565b93612874565b6128b1926128ab8361288f93612211565b85612ab3565b9661284c565b5050505050956128cc836128d2949389612ab3565b96612ab3565b9291905f90565b506001600160601b038a166001600160601b0382161015612774565b935050509550610100813d8211612985575b8161291561010093836114ae565b81010312610ca3576001600160601b039561292f826116e7565b50602082015191612942604082016116d3565b60608201519361297a60e0612959608086016116d3565b9461296660a082016116d3565b5061297360c082016116d3565b50016116d3565b509093919398612740565b3d9150612907565b9391506020843d6020116129bd575b816129a9602093836114ae565b81010312610ca357925190926126c96126ac565b3d915061299c565b90506020813d6020116129f7575b816129e0602093836114ae565b81010312610ca3576129f1906116d3565b5f612646565b3d91506129d3565b92919092612a1582612a0f61250e565b92612186565b906060612a2a60a0830193611a9b8551612178565b910194612a378651612178565b91670de0b6b3a7640000830292808404670de0b6b3a76400001490151715611efb57612a6b92612a6691612199565b612211565b60011c93670de0b6b3a7640000850292858404670de0b6b3a76400001486151715611efb57612aae612aa76122f195611a9b611aac9551612178565b9351612178565b612186565b8181029181159183041417820215612ac9570490565b63ad251c275f526004601cfd5b8181029181159183041417670de0b6b3a76400000215612ac957670de0b6b3a7640000808204910615150190565b818102929181159184041417810215612ac957808204910615150190565b906001600160601b038216918203612b3657565b7fe233e012000000000000000000000000000000000000000000000000000000005f5260045ffd5b6805345cdf77eb68f44c54828101908110612122576805345cdf77eb68f44c556387a211a2600c525f526020600c20818154019055602052600c5160601c5f7fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef602080a3565b90612bcd61250e565b916001600160601b03600f541692612be58285612186565b91612bff60208301936001600160601b0385511690612186565b9285614e20019384614e2011611efb57612c40612c4993612c3a6122866001600160601b0394611aac6060612c509a019a611a9b8c51612178565b98612186565b91511690612186565b9151612178565b9081614e200291614e20830403611efb576122069161228691612199565b5f80809338935af115612c7d57565b63b12d13eb5f526004601cfd5b90916001600160a01b038316612ca4576116429250612c6e565b6014526034526fa9059cbb0000000000000000000000005f5260205f6044601082855af1908160015f51141615612cdd5750505f603452565b3b153d171015612cee575f806124a0565b6390b8ec185f526004601cfd5b916040519360605260405260601b602c526f23b872dd000000000000000000000000600c5260205f6064601c82855af1908160015f51141615612d44575b50505f606052604052565b3b153d171015612d55575f80612d39565b637939f4245f526004601cfdfea26469706673582212207c168ad73996dc9acb7aab7fca88f6d8156ba1bf388fb686630f74dc6407505c64736f6c634300081e0033
Loading...
Loading
Loading...
Loading
Loading...
Loading
Net Worth in USD
$0.00
Net Worth in MON
Multichain Portfolio | 35 Chains
| Chain | Token | Portfolio % | Price | Amount | Value |
|---|
Loading...
Loading
Loading...
Loading
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.