Overview
BERA Balance
BERA Value
$0.00More Info
Private Name Tags
ContractCreator
Transaction Hash |
Method
|
Block
|
From
|
To
|
|||||
---|---|---|---|---|---|---|---|---|---|
Latest 1 internal transaction
Parent Transaction Hash | Block | From | To | |||
---|---|---|---|---|---|---|
4039964 | 21 hrs ago | Contract Creation | 0 BERA |
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 Source Code Verified (Exact Match)
Contract Name:
HoneyFactory
Compiler Version
v0.8.26+commit.8a97fa7a
Optimization Enabled:
Yes with 666 runs
Other Settings:
cancun EvmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.26; import { ERC20 } from "solady/src/tokens/ERC20.sol"; import { ERC4626 } from "solady/src/tokens/ERC4626.sol"; import { SafeTransferLib } from "solady/src/utils/SafeTransferLib.sol"; import { IPriceOracle } from "../extras/IPriceOracle.sol"; import { IHoneyFactory } from "./IHoneyFactory.sol"; import { Utils } from "../libraries/Utils.sol"; import { Honey } from "./Honey.sol"; import { VaultAdmin, CollateralVault } from "./VaultAdmin.sol"; /// @notice This is the factory contract for minting and redeeming Honey. /// @author Berachain Team contract HoneyFactory is IHoneyFactory, VaultAdmin { using Utils for bytes4; /// @dev The constant representing 100% of mint/redeem rate. uint256 private constant ONE_HUNDRED_PERCENT_RATE = 1e18; /// @dev The constant representing 98% of mint/redeem rate. uint256 private constant NINETY_EIGHT_PERCENT_RATE = 98e16; /// @dev The constant representing the default symmetrical offset for USD peg. uint256 private constant DEFAULT_PEG_OFFSET = 0.002e18; /// @dev The constant representing the default mint/redeem rate. uint256 private constant DEFAULT_MINT_REDEEM_RATE = 0.9995e18; /// @notice The constant representing the default minimum amount of shares to recapitalize. /// @dev It's set to 1 share. uint256 private constant DEFAULT_MIN_SHARES_TO_RECAPITALIZE = 1e18; /// @notice The constant representing the max peg offset allowed. /// @dev It's set to 2 cents. uint256 private constant MAX_PEG_OFFSET = 0.02e18; /// @notice The constant representing the max price feed delay tolerance in seconds allowed. uint256 private constant MAX_PRICE_FEED_DELAY_TOLERANCE = 120 seconds; /// @notice The Honey token contract. Honey public honey; /// @notice The rate of POLFeeCollector fees, 60.18-decimal fixed-point number representation /// @dev 1e18 will imply all the fees are collected by the POLFeeCollector /// @dev 0 will imply all fees goes to the feeReceiver uint256 public polFeeCollectorFeeRate; /// @notice Mint rate of Honey for each asset, 60.18-decimal fixed-point number representation mapping(address asset => uint256 rate) public mintRates; /// @notice Redemption rate of Honey for each asset, 60.18-decimal fixed-point number representation mapping(address asset => uint256 rate) public redeemRates; /// @notice WAD offset from 1e18 for USD peg mapping(address asset => uint256 lowerPegOffset) internal lowerPegOffsets; mapping(address asset => uint256 upperPegOffset) internal upperPegOffsets; /// @notice Premium rate applied upon liquidation mapping(address asset => uint256 rate) internal liquidationRates; /// @notice Whether the basket mode is forced regardless of the price oracle bool public forcedBasketMode; /// @notice Whether the liquidation is enabled bool public liquidationEnabled; /// @notice The price oracle IPriceOracle public priceOracle; /// @notice The max number of seconds of tolerated staleness /// @dev It's involved into deeming a collateral asset pegged or not uint256 public priceFeedMaxDelay; address public referenceCollateral; mapping(address asset => uint256 limit) public relativeCap; uint256 public globalCap; /// @notice The target balance for recapitalization of each asset mapping(address asset => uint256 targetBalance) public recapitalizeBalanceThreshold; /// @notice The minimum amount of shares that the user have to mint to recapitalize uint256 public minSharesToRecapitalize; /// @custom:oz-upgrades-unsafe-allow constructor constructor() { _disableInitializers(); } function initialize( address _governance, address _honey, address _polFeeCollector, address _feeReceiver, address _priceOracle, address _beacon ) external initializer { __VaultAdmin_init(_governance, _polFeeCollector, _feeReceiver, _beacon); if (_honey == address(0)) ZeroAddress.selector.revertWith(); if (_priceOracle == address(0)) ZeroAddress.selector.revertWith(); honey = Honey(_honey); // initialize with 100% of the mint/redeem fee to the polFeeCollector polFeeCollectorFeeRate = ONE_HUNDRED_PERCENT_RATE; // NOTE: based on the average block time of ~2 seconds. priceFeedMaxDelay = 10 seconds; minSharesToRecapitalize = DEFAULT_MIN_SHARES_TO_RECAPITALIZE; priceOracle = IPriceOracle(_priceOracle); globalCap = ONE_HUNDRED_PERCENT_RATE; liquidationEnabled = false; } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* MANAGER FUNCTIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @notice Set the mint rate of Honey for an asset. function setMintRate(address asset, uint256 mintRate) external { _checkRole(MANAGER_ROLE); // Revert if the mint rate is over 100% if (mintRate > ONE_HUNDRED_PERCENT_RATE) { OverOneHundredPercentRate.selector.revertWith(mintRate); } // Ensure manager cannot set the mint rate below 98% if (mintRate < NINETY_EIGHT_PERCENT_RATE) { UnderNinetyEightPercentRate.selector.revertWith(mintRate); } mintRates[asset] = mintRate; emit MintRateSet(asset, mintRate); } /// @notice Set the redemption rate of Honey for an asset. function setRedeemRate(address asset, uint256 redeemRate) external { _checkRole(MANAGER_ROLE); // Revert if the redeem rate is over 100% if (redeemRate > ONE_HUNDRED_PERCENT_RATE) { OverOneHundredPercentRate.selector.revertWith(redeemRate); } // Ensure manager cannot set the redeem rate below 98% if (redeemRate < NINETY_EIGHT_PERCENT_RATE) { UnderNinetyEightPercentRate.selector.revertWith(redeemRate); } redeemRates[asset] = redeemRate; emit RedeemRateSet(asset, redeemRate); } /// @notice Set the forced basket mode status. /// @dev Only Manager role can call this function. function setForcedBasketMode(bool forced) external { _checkRole(MANAGER_ROLE); forcedBasketMode = forced; emit BasketModeForced(forced); } /// @notice Set the max tolerated number of seconds for oracle staleness. /// @dev It's involved into deeming a collateral asset pegged or not. /// @dev Only Manager role can call this function. function setMaxFeedDelay(uint256 maxTolerance) external { _checkRole(MANAGER_ROLE); if (maxTolerance > MAX_PRICE_FEED_DELAY_TOLERANCE) { AmountOutOfRange.selector.revertWith(); } priceFeedMaxDelay = maxTolerance; emit MaxFeedDelaySet(maxTolerance); } /// @notice Set lower and upper depeg offset for an asset. /// @dev Only Manager role can call this function. function setDepegOffsets(address asset, uint256 lowerOffset, uint256 upperOffset) external { _checkRole(MANAGER_ROLE); _checkRegisteredAsset(asset); if (lowerOffset > MAX_PEG_OFFSET || upperOffset > MAX_PEG_OFFSET) { AmountOutOfRange.selector.revertWith(); } lowerPegOffsets[asset] = lowerOffset; upperPegOffsets[asset] = upperOffset; emit DepegOffsetsSet(asset, lowerOffset, upperOffset); } /// @notice Set the reference collateral for cap limits. function setReferenceCollateral(address asset) external { _checkRole(MANAGER_ROLE); _checkRegisteredAsset(asset); address old = referenceCollateral; referenceCollateral = asset; emit ReferenceCollateralSet(old, asset); } /// @notice Set the global cap limit. function setGlobalCap(uint256 limit) external { _checkRole(MANAGER_ROLE); // A change in the weights distribution that frontruns this transaction // may cause a DoS in the redeem of Honey uint256[] memory weights = _getWeights(true, false); uint256 max = 0; for (uint256 i = 0; i < registeredAssets.length; i++) { if (weights[i] > max) { max = weights[i]; } } if (limit < max) { CapCanCauseDenialOfService.selector.revertWith(); } globalCap = limit; emit GlobalCapSet(limit); } /// @notice Set the relative cap limit. function setRelativeCap(address asset, uint256 limit) external { _checkRole(MANAGER_ROLE); relativeCap[asset] = limit; emit RelativeCapSet(asset, limit); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* ADMIN FUNCTIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @notice Set the POLFeeCollector fee rate. function setPOLFeeCollectorFeeRate(uint256 _polFeeCollectorFeeRate) external { _checkRole(DEFAULT_ADMIN_ROLE); // Revert if the POLFeeCollector fee rate is over 100% if (_polFeeCollectorFeeRate > ONE_HUNDRED_PERCENT_RATE) { OverOneHundredPercentRate.selector.revertWith(_polFeeCollectorFeeRate); } polFeeCollectorFeeRate = _polFeeCollectorFeeRate; emit POLFeeCollectorFeeRateSet(_polFeeCollectorFeeRate); } /// @notice Set whether the liquidation is enabled or not. function setLiquidationEnabled(bool enabled) external { _checkRole(DEFAULT_ADMIN_ROLE); liquidationEnabled = enabled; emit LiquidationStatusSet(enabled); } /// @notice Set the liquidation rate for an asset /// @dev The `extraRate` is a premium, hence a 0.25 rate means a 1.25 premium factor. function setLiquidationRate(address asset, uint256 extraRate) external { _checkRole(DEFAULT_ADMIN_ROLE); _checkRegisteredAsset(asset); liquidationRates[asset] = extraRate; emit LiquidationRateSet(asset, extraRate); } /// @notice Set the minimum amount of shares that the user have to mint to recapitalize function setMinSharesToRecapitalize(uint256 minSharesAmount) external { _checkRole(DEFAULT_ADMIN_ROLE); if (minSharesAmount < DEFAULT_MIN_SHARES_TO_RECAPITALIZE) { AmountOutOfRange.selector.revertWith(minSharesAmount); } minSharesToRecapitalize = minSharesAmount; emit MinSharesToRecapitalizeSet(minSharesAmount); } /// @notice Set the target balance for recapitalization. /// @param asset The collateral asset to which the target applies. /// @param target The target amount with the token's decimals. function setRecapitalizeBalanceThreshold(address asset, uint256 target) external { _checkRole(DEFAULT_ADMIN_ROLE); recapitalizeBalanceThreshold[asset] = target; emit RecapitalizeBalanceThresholdSet(asset, target); } /// @notice Replace the price oracle. /// @param priceOracle_ The new price oracle to use. function setPriceOracle(address priceOracle_) external { _checkRole(DEFAULT_ADMIN_ROLE); if (priceOracle_ == address(0)) ZeroAddress.selector.revertWith(); priceOracle = IPriceOracle(priceOracle_); emit PriceOracleSet(priceOracle_); } /// @dev Create a new ERC4626 vault for a pair of asset - Honey and register it with VaultAdmin. /// @dev Reverts if the vault for the given asset is already registered. /// @dev Reverts if the asset is zero address during `vault.initialize`. /// @param asset The asset to create a vault for. /// @return vault The newly created vault. function createVault(address asset) external returns (ERC4626 vault) { if (numRegisteredAssets() == 0) { referenceCollateral = asset; } vault = _createVault(asset); relativeCap[asset] = ONE_HUNDRED_PERCENT_RATE; mintRates[asset] = DEFAULT_MINT_REDEEM_RATE; redeemRates[asset] = DEFAULT_MINT_REDEEM_RATE; // Check if oracle has the needed data and the asset is a stable one: lowerPegOffsets[asset] = MAX_PEG_OFFSET; upperPegOffsets[asset] = MAX_PEG_OFFSET; if (!isPegged(asset)) { NotPegged.selector.revertWith(asset); } // Restore the default value: lowerPegOffsets[asset] = DEFAULT_PEG_OFFSET; upperPegOffsets[asset] = DEFAULT_PEG_OFFSET; } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* USER FUNCTIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @inheritdoc IHoneyFactory function mint( address asset, uint256 amount, address receiver, bool expectBasketMode ) external whenNotPaused returns (uint256 honeyToMint) { _checkRegisteredAsset(asset); bool basketMode = isBasketModeEnabled(true); if (basketMode != expectBasketMode) { UnexpectedBasketModeStatus.selector.revertWith(); } if (!basketMode) { // Check if the asset is not market as bad collateral and if it is pegged. _checkGoodCollateralAsset(asset); if (!isPegged(asset)) { NotPegged.selector.revertWith(asset); } honeyToMint = _mint(asset, amount, receiver, false); if (!_isCappedGlobal(true, asset)) { ExceedGlobalCap.selector.revertWith(); } } else { uint256[] memory weights = _getWeights(false, true); // Here the assumption is that the callers knows about the basket mode and // has already approved all the needed (and previewed) amounts. // As we cannot trust the caller to have provided the right asset/amount tuples // without changing their distribution or without tricking it (e.g. by repeating some assets), // we take one of those amount as reference and compute the others accordingly. uint256 refAssetWeight = weights[_lookupRegistrationIndex(asset)]; if (refAssetWeight == 0) { ZeroWeight.selector.revertWith(asset); } uint8 decimals = ERC20(asset).decimals(); uint256 refAmount = Utils.changeDecimals(amount, decimals, 18); refAmount = refAmount * 1e18 / refAssetWeight; for (uint256 i = 0; i < registeredAssets.length; i++) { amount = refAmount * weights[i] / 1e18; amount = vaults[registeredAssets[i]].convertToAssets(amount); honeyToMint += _mint(registeredAssets[i], amount, receiver, true); } } } /// @inheritdoc IHoneyFactory function redeem( address asset, uint256 honeyAmount, address receiver, bool expectBasketMode ) external whenNotPaused returns (uint256[] memory redeemed) { _checkRegisteredAsset(asset); bool basketMode = isBasketModeEnabled(false); if (basketMode != expectBasketMode) { UnexpectedBasketModeStatus.selector.revertWith(); } if (!basketMode) { redeemed = new uint256[](registeredAssets.length); uint256 index = _lookupRegistrationIndex(asset); redeemed[index] = _redeem(asset, honeyAmount, receiver); // When the redeemed asset is the reference collateral // it might block other assets on mint due to its usage in the computation of the relative cap limits. // Because of this, we need to check the relative cap limits for all the other assets. if (asset == referenceCollateral) { for (uint256 i = 0; i < registeredAssets.length; i++) { if (registeredAssets[i] == asset) { continue; } if (!_isCappedRelative(registeredAssets[i])) { ExceedRelativeCap.selector.revertWith(); } } } if (!_isCappedGlobal(false, asset)) { ExceedGlobalCap.selector.revertWith(); } return redeemed; } uint256[] memory weights = _getWeights(false, true); redeemed = new uint256[](registeredAssets.length); for (uint256 i = 0; i < registeredAssets.length; i++) { uint256 amount = honeyAmount * weights[i] / 1e18; redeemed[i] = _redeem(registeredAssets[i], amount, receiver); } } /// @inheritdoc IHoneyFactory function liquidate( address badCollateral, address goodCollateral, uint256 goodAmount ) external whenNotPaused returns (uint256 badAmount) { _checkRegisteredAsset(badCollateral); _checkRegisteredAsset(goodCollateral); _checkGoodCollateralAsset(goodCollateral); if (!liquidationEnabled) { LiquidationDisabled.selector.revertWith(); } if (!isBadCollateralAsset[badCollateral]) { AssetIsNotBadCollateral.selector.revertWith(); } // If the bad collateral is the reference one, a liquidation may block other assets on mint // due to its usage in the computation of the relative cap limits. // As a conseguence, the reference asset must first be replaced. if (badCollateral == referenceCollateral) { LiquidationWithReferenceCollateral.selector.revertWith(); } uint256 goodShares = _approveAndDeposit(goodCollateral, goodAmount); uint256 priceBad = _getPrice(badCollateral); uint256 priceGood = _getPrice(goodCollateral); badAmount = (goodShares * priceGood / priceBad) * (1e18 + liquidationRates[badCollateral]) / 1e18; uint256 badShares = _getSharesWithoutFees(badCollateral); if (badAmount > badShares) { // x = bs / (pg / pb) * (1 + r) // optimized (for gas and rounding issues) as: // x = bs * (pb / pg) / (1 + r) uint256 goodSharesAdjusted = (badShares * priceBad / priceGood) * 1e18 / (1e18 + liquidationRates[badCollateral]); _redeemShares(goodCollateral, goodShares - goodSharesAdjusted, msg.sender); badAmount = badShares; } if (!_isCappedRelative(goodCollateral)) { ExceedRelativeCap.selector.revertWith(); } if (!_isCappedGlobal(false, badCollateral)) { ExceedGlobalCap.selector.revertWith(); } badAmount = _redeemShares(badCollateral, badAmount, msg.sender); if (badAmount == 0) { ZeroAmount.selector.revertWith(); } _checkInvariants(badCollateral); _checkInvariants(goodCollateral); emit Liquidated(badCollateral, goodCollateral, goodAmount, msg.sender); } /// @inheritdoc IHoneyFactory function recapitalize(address asset, uint256 amount) external whenNotPaused { _checkRegisteredAsset(asset); _checkGoodCollateralAsset(asset); uint256 targetBalance = recapitalizeBalanceThreshold[asset]; uint256 feeAssetBalance = vaults[asset].convertToAssets(collectedAssetFees[asset]); uint256 currentBalance = vaults[asset].totalAssets() - feeAssetBalance; if (currentBalance >= targetBalance) { RecapitalizeNotNeeded.selector.revertWith(asset); } if (!isPegged(asset)) { NotPegged.selector.revertWith(asset); } if (currentBalance + amount > targetBalance) { amount = targetBalance - currentBalance; } // Convert the amount to shares to avoid the need of decimals handling. uint256 shares = vaults[asset].convertToShares(amount); if (shares < minSharesToRecapitalize) { InsufficientRecapitalizeAmount.selector.revertWith(amount); } _approveAndDeposit(asset, amount); if (!_isCappedRelative(asset)) { ExceedRelativeCap.selector.revertWith(); } if (!_isCappedGlobal(true, asset)) { ExceedGlobalCap.selector.revertWith(); } _checkInvariants(asset); emit Recapitalized(asset, amount, msg.sender); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* GETTERS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @notice Get the status of the basket mode. /// @dev On mint, basket mode is enabled if all collaterals are either depegged or bad. /// @dev On redeem, basket mode is enabled if at least one asset is deppegged /// except for the collateral assets that have been fully liquidated. function isBasketModeEnabled(bool isMint) public view returns (bool basketMode) { if (forcedBasketMode) { return true; } for (uint256 i = 0; i < registeredAssets.length; i++) { bool isPegged_ = isPegged(registeredAssets[i]); if (isMint) { if (isPegged_ && !isBadCollateralAsset[registeredAssets[i]]) { // Basket mode should be disabled. It means there is a good collateral. return false; } } else if (!isPegged_) { // If the not pegged asset is a bad collateral and its vault doesn't have shares // we can ignore it because it means it has been fully liquidated. bool usedAsCollateral = _getSharesWithoutFees(registeredAssets[i]) > 0; if (!usedAsCollateral) { continue; } return true; } } // When is mint and there is no asset that disable basket mode, return true. // When is redeem and there is no asset that enable basket mode, return false. return isMint ? true : false; } /// @notice Get weights of all the registered assets except for the paused ones. /// @return w weights of all the registered assets. function getWeights() external view returns (uint256[] memory w) { w = _getWeights(false, true); } /// @notice Get if an asset is pegged or not. /// @param asset The asset to check. /// @return true if the asset is pegged. function isPegged(address asset) public view returns (bool) { if (!priceOracle.priceAvailable(asset)) { return false; } IPriceOracle.Data memory data = priceOracle.getPriceUnsafe(asset); if (data.publishTime < block.timestamp - priceFeedMaxDelay) { return false; } return (1e18 - lowerPegOffsets[asset] <= data.price) && (data.price <= 1e18 + upperPegOffsets[asset]); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* INTERNAL FUNCTIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ function _mint(address asset, uint256 amount, address receiver, bool basketMode) internal returns (uint256) { if (amount == 0) { return 0; } uint256 shares = _approveAndDeposit(asset, amount); // The factory mints the corresponding amount of Honey to the receiver // with the consideration of the static mint fee. (uint256 honeyToMint, uint256 feeReceiverFeeShares, uint256 polFeeCollectorFeeShares) = _getHoneyMintedFromShares(asset, shares); // Updates the fee accounts for the minted shares. _handleFees(asset, polFeeCollectorFeeShares, feeReceiverFeeShares); if (!basketMode && !_isCappedRelative(asset)) { ExceedRelativeCap.selector.revertWith(); } honey.mint(receiver, honeyToMint); _checkInvariants(asset); emit HoneyMinted(msg.sender, receiver, asset, amount, honeyToMint); return honeyToMint; } function _redeem(address asset, uint256 honeyAmount, address receiver) internal returns (uint256 redeemedAssets) { // The function reverts if the sender does not have enough Honey to burn or // the vault does not have enough assets to redeem. // The factory burns the corresponding amount of Honey of the sender // to get the shares and redeem them for assets from the vault. if (honeyAmount == 0) { return 0; } { (uint256 sharesForRedeem, uint256 feeReceiverFeeShares, uint256 polFeeCollectorFeeShares) = _getSharesRedeemedFromHoney(asset, honeyAmount); honey.burn(msg.sender, honeyAmount); // Updates the fee accounts for the redeemed shares. _handleFees(asset, polFeeCollectorFeeShares, feeReceiverFeeShares); // The factory redeems the corresponding amount of assets from Vault // and transfer the assets to the receiver. redeemedAssets = _redeemShares(asset, sharesForRedeem, receiver); } _checkInvariants(asset); emit HoneyRedeemed(msg.sender, receiver, asset, redeemedAssets, honeyAmount); } function _getHoneyMintedFromShares( address asset, uint256 shares ) internal view returns (uint256 honeyAmount, uint256 feeReceiverFeeShares, uint256 polFeeCollectorFeeShares) { uint256 mintRate = mintRates[asset]; honeyAmount = shares * mintRate / 1e18; uint256 feeShares = shares - honeyAmount; polFeeCollectorFeeShares = feeShares * polFeeCollectorFeeRate / 1e18; feeReceiverFeeShares = feeShares - polFeeCollectorFeeShares; } function _getSharesRedeemedFromHoney( address asset, uint256 honeyAmount ) internal view returns (uint256 shares, uint256 feeReceiverFeeShares, uint256 polFeeCollectorFeeShares) { uint256 redeemRate = redeemRates[asset]; shares = honeyAmount * redeemRate / 1e18; uint256 feeShares = honeyAmount - shares; // Distribute the fee to the polFeeCollector based on the polFeeCollectorFeeRate. polFeeCollectorFeeShares = feeShares * polFeeCollectorFeeRate / 1e18; // The remaining fee is distributed to the feeReceiver. feeReceiverFeeShares = feeShares - polFeeCollectorFeeShares; } function _getWeights( bool filterBadCollaterals, bool filterPausedCollateral ) internal view returns (uint256[] memory weights) { weights = new uint256[](registeredAssets.length); uint256 sum = 0; for (uint256 i = 0; i < registeredAssets.length; i++) { if (filterBadCollaterals && isBadCollateralAsset[registeredAssets[i]]) { continue; } // If vault is paused, the weight of the asset is 0. if (filterPausedCollateral && vaults[registeredAssets[i]].paused()) { continue; } // NOTE: vault shares are always in WAD, regardless of the asset's decimals weights[i] = _getSharesWithoutFees(registeredAssets[i]); sum += weights[i]; } if (sum == 0) { return weights; } for (uint256 i = 0; i < registeredAssets.length; i++) { weights[i] = weights[i] * 1e18 / sum; } } function _isCappedRelative(address asset) internal view returns (bool) { if (asset == referenceCollateral) { return true; } uint256 balance = _getSharesWithoutFees(asset); uint256 refBalance = _getSharesWithoutFees(referenceCollateral); if (refBalance == 0) { // If the balance of the asset is 0, it means that is capped // because the refence asset has also 0 balance. return (balance == 0) ? true : false; } uint256 weight = balance * 1e18 / refBalance; return weight <= relativeCap[asset]; } function _isCappedGlobal(bool isMint, address collateralAsset) internal view returns (bool) { uint256[] memory weights = _getWeights(true, false); for (uint256 i = 0; i < registeredAssets.length; i++) { // Upon mint, we don't care about the other collaterals, as their weight can // only decrease; also, shall we check them all, it's enough to have at least // one of them that exceeds their weight to prevent the current mint. if (isMint && registeredAssets[i] != collateralAsset) { continue; } // Upon redeem, we always allows the weight of the asset to be reduced, even if // its current weight is already over the globalCap (due to a lowered value), // as long as it doesn't cause the other collateral to exceeds the limit. if (!isMint && registeredAssets[i] == collateralAsset) { continue; } if (weights[i] > globalCap) { return false; } } return true; } function _getPrice(address asset) internal view returns (uint256) { IPriceOracle.Data memory data = priceOracle.getPriceNoOlderThan(asset, priceFeedMaxDelay); return data.price; } /// @dev Returns the amount of the shares of the factory for the given asset excluding the fees. function _getSharesWithoutFees(address asset) internal view returns (uint256) { return vaults[asset].balanceOf(address(this)) - collectedAssetFees[asset]; } function _approveAndDeposit(address asset, uint256 amount) internal returns (uint256 shares) { SafeTransferLib.safeTransferFrom(asset, msg.sender, address(this), amount); SafeTransferLib.safeApprove(asset, address(vaults[asset]), amount); shares = vaults[asset].deposit(amount, address(this)); } function _redeemShares(address asset, uint256 shares, address receiver) internal returns (uint256) { return vaults[asset].redeem(shares, receiver, address(this)); } /// @notice Handles the fee to distribute for a given asset to the PoL fee collector and the fee receiver. function _handleFees(address asset, uint256 polFeeCollectorFeeShares, uint256 feeReceiverFeeShares) internal { // The PoL fee collector's fee, if any, are transferred right away: if (polFeeCollectorFeeShares > 0) { _redeemShares(asset, polFeeCollectorFeeShares, polFeeCollector); } // The fee receiver's fee (shares), if any, are held until they are redeemed: if (feeReceiverFeeShares > 0) { collectedFees[feeReceiver][asset] += feeReceiverFeeShares; collectedAssetFees[asset] += feeReceiverFeeShares; } } /// @dev Check the invariant of the vault to ensure that assets are always sufficient to redeem. function _checkInvariants(address asset) internal view { ERC4626 vault = vaults[asset]; uint256 totalShares = vault.totalSupply(); (bool isCustodyVault, address custodyAddress) = CollateralVault(address(vault)).custodyInfo(); uint256 totalAssets = isCustodyVault ? ERC20(asset).balanceOf(custodyAddress) : ERC20(asset).balanceOf(address(vault)); if (vault.convertToAssets(totalShares) > totalAssets) { InsufficientAssets.selector.revertWith(totalAssets, totalShares); } // A user cannot redeem also the collected fees uint256 vaultShares = vault.balanceOf(address(this)); if (vaultShares < collectedAssetFees[asset]) { uint256 vaultAssets = vault.convertToAssets(vaultShares); InsufficientAssets.selector.revertWith(vaultAssets, vaultShares); } } }
// 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`. /// To enable, 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 false; } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.4; import {ERC20} from "./ERC20.sol"; import {FixedPointMathLib} from "../utils/FixedPointMathLib.sol"; import {SafeTransferLib} from "../utils/SafeTransferLib.sol"; /// @notice Simple ERC4626 tokenized Vault implementation. /// @author Solady (https://github.com/vectorized/solady/blob/main/src/tokens/ERC4626.sol) /// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/mixins/ERC4626.sol) /// @author Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC4626.sol) abstract contract ERC4626 is ERC20 { /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* CONSTANTS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev The default underlying decimals. uint8 internal constant _DEFAULT_UNDERLYING_DECIMALS = 18; /// @dev The default decimals offset. uint8 internal constant _DEFAULT_DECIMALS_OFFSET = 0; /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* CUSTOM ERRORS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Cannot deposit more than the max limit. error DepositMoreThanMax(); /// @dev Cannot mint more than the max limit. error MintMoreThanMax(); /// @dev Cannot withdraw more than the max limit. error WithdrawMoreThanMax(); /// @dev Cannot redeem more than the max limit. error RedeemMoreThanMax(); /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* EVENTS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Emitted during a mint call or deposit call. event Deposit(address indexed by, address indexed owner, uint256 assets, uint256 shares); /// @dev Emitted during a withdraw call or redeem call. event Withdraw( address indexed by, address indexed to, address indexed owner, uint256 assets, uint256 shares ); /// @dev `keccak256(bytes("Deposit(address,address,uint256,uint256)"))`. uint256 private constant _DEPOSIT_EVENT_SIGNATURE = 0xdcbc1c05240f31ff3ad067ef1ee35ce4997762752e3a095284754544f4c709d7; /// @dev `keccak256(bytes("Withdraw(address,address,address,uint256,uint256)"))`. uint256 private constant _WITHDRAW_EVENT_SIGNATURE = 0xfbde797d201c681b91056529119e0b02407c7bb96a4a2c75c01fc9667232c8db; /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* ERC4626 CONSTANTS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev To be overridden to return the address of the underlying asset. /// /// - MUST be an ERC20 token contract. /// - MUST NOT revert. function asset() public view virtual returns (address); /// @dev To be overridden to return the number of decimals of the underlying asset. /// Default: 18. /// /// - MUST NOT revert. function _underlyingDecimals() internal view virtual returns (uint8) { return _DEFAULT_UNDERLYING_DECIMALS; } /// @dev Override to return a non-zero value to make the inflation attack even more unfeasible. /// Only used when {_useVirtualShares} returns true. /// Default: 0. /// /// - MUST NOT revert. function _decimalsOffset() internal view virtual returns (uint8) { return _DEFAULT_DECIMALS_OFFSET; } /// @dev Returns whether virtual shares will be used to mitigate the inflation attack. /// See: https://github.com/OpenZeppelin/openzeppelin-contracts/issues/3706 /// Override to return true or false. /// Default: true. /// /// - MUST NOT revert. function _useVirtualShares() internal view virtual returns (bool) { return true; } /// @dev Returns the decimals places of the token. /// /// - MUST NOT revert. function decimals() public view virtual override(ERC20) returns (uint8) { if (!_useVirtualShares()) return _underlyingDecimals(); return _underlyingDecimals() + _decimalsOffset(); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* ASSET DECIMALS GETTER HELPER */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Helper function to get the decimals of the underlying asset. /// Useful for setting the return value of `_underlyingDecimals` during initialization. /// If the retrieval succeeds, `success` will be true, and `result` will hold the result. /// Otherwise, `success` will be false, and `result` will be zero. /// /// Example usage: /// ``` /// (bool success, uint8 result) = _tryGetAssetDecimals(underlying); /// _decimals = success ? result : _DEFAULT_UNDERLYING_DECIMALS; /// ``` function _tryGetAssetDecimals(address underlying) internal view returns (bool success, uint8 result) { /// @solidity memory-safe-assembly assembly { // Store the function selector of `decimals()`. mstore(0x00, 0x313ce567) // Arguments are evaluated last to first. success := and( // Returned value is less than 256, at left-padded to 32 bytes. and(lt(mload(0x00), 0x100), gt(returndatasize(), 0x1f)), // The staticcall succeeds. staticcall(gas(), underlying, 0x1c, 0x04, 0x00, 0x20) ) result := mul(mload(0x00), success) } } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* ACCOUNTING LOGIC */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Returns the total amount of the underlying asset managed by the Vault. /// /// - SHOULD include any compounding that occurs from the yield. /// - MUST be inclusive of any fees that are charged against assets in the Vault. /// - MUST NOT revert. function totalAssets() public view virtual returns (uint256 assets) { assets = SafeTransferLib.balanceOf(asset(), address(this)); } /// @dev Returns the amount of shares that the Vault will exchange for the amount of /// assets provided, in an ideal scenario where all conditions are met. /// /// - MUST NOT be inclusive of any fees that are charged against assets in the Vault. /// - MUST NOT show any variations depending on the caller. /// - MUST NOT reflect slippage or other on-chain conditions, during the actual exchange. /// - MUST NOT revert. /// /// Note: This calculation MAY NOT reflect the "per-user" price-per-share, and instead /// should reflect the "average-user's" price-per-share, i.e. what the average user should /// expect to see when exchanging to and from. function convertToShares(uint256 assets) public view virtual returns (uint256 shares) { if (!_useVirtualShares()) { uint256 supply = totalSupply(); return _eitherIsZero(assets, supply) ? _initialConvertToShares(assets) : FixedPointMathLib.fullMulDiv(assets, supply, totalAssets()); } uint256 o = _decimalsOffset(); if (o == uint256(0)) { return FixedPointMathLib.fullMulDiv(assets, totalSupply() + 1, _inc(totalAssets())); } return FixedPointMathLib.fullMulDiv(assets, totalSupply() + 10 ** o, _inc(totalAssets())); } /// @dev Returns the amount of assets that the Vault will exchange for the amount of /// shares provided, in an ideal scenario where all conditions are met. /// /// - MUST NOT be inclusive of any fees that are charged against assets in the Vault. /// - MUST NOT show any variations depending on the caller. /// - MUST NOT reflect slippage or other on-chain conditions, during the actual exchange. /// - MUST NOT revert. /// /// Note: This calculation MAY NOT reflect the "per-user" price-per-share, and instead /// should reflect the "average-user's" price-per-share, i.e. what the average user should /// expect to see when exchanging to and from. function convertToAssets(uint256 shares) public view virtual returns (uint256 assets) { if (!_useVirtualShares()) { uint256 supply = totalSupply(); return supply == uint256(0) ? _initialConvertToAssets(shares) : FixedPointMathLib.fullMulDiv(shares, totalAssets(), supply); } uint256 o = _decimalsOffset(); if (o == uint256(0)) { return FixedPointMathLib.fullMulDiv(shares, totalAssets() + 1, _inc(totalSupply())); } return FixedPointMathLib.fullMulDiv(shares, totalAssets() + 1, totalSupply() + 10 ** o); } /// @dev Allows an on-chain or off-chain user to simulate the effects of their deposit /// at the current block, given current on-chain conditions. /// /// - MUST return as close to and no more than the exact amount of Vault shares that /// will be minted in a deposit call in the same transaction, i.e. deposit should /// return the same or more shares as `previewDeposit` if call in the same transaction. /// - MUST NOT account for deposit limits like those returned from `maxDeposit` and should /// always act as if the deposit will be accepted, regardless of approvals, etc. /// - MUST be inclusive of deposit fees. Integrators should be aware of this. /// - MUST not revert. /// /// Note: Any unfavorable discrepancy between `convertToShares` and `previewDeposit` SHOULD /// be considered slippage in share price or some other type of condition, meaning /// the depositor will lose assets by depositing. function previewDeposit(uint256 assets) public view virtual returns (uint256 shares) { shares = convertToShares(assets); } /// @dev Allows an on-chain or off-chain user to simulate the effects of their mint /// at the current block, given current on-chain conditions. /// /// - MUST return as close to and no fewer than the exact amount of assets that /// will be deposited in a mint call in the same transaction, i.e. mint should /// return the same or fewer assets as `previewMint` if called in the same transaction. /// - MUST NOT account for mint limits like those returned from `maxMint` and should /// always act as if the mint will be accepted, regardless of approvals, etc. /// - MUST be inclusive of deposit fees. Integrators should be aware of this. /// - MUST not revert. /// /// Note: Any unfavorable discrepancy between `convertToAssets` and `previewMint` SHOULD /// be considered slippage in share price or some other type of condition, /// meaning the depositor will lose assets by minting. function previewMint(uint256 shares) public view virtual returns (uint256 assets) { if (!_useVirtualShares()) { uint256 supply = totalSupply(); return supply == uint256(0) ? _initialConvertToAssets(shares) : FixedPointMathLib.fullMulDivUp(shares, totalAssets(), supply); } uint256 o = _decimalsOffset(); if (o == uint256(0)) { return FixedPointMathLib.fullMulDivUp(shares, totalAssets() + 1, _inc(totalSupply())); } return FixedPointMathLib.fullMulDivUp(shares, totalAssets() + 1, totalSupply() + 10 ** o); } /// @dev Allows an on-chain or off-chain user to simulate the effects of their withdrawal /// at the current block, given the current on-chain conditions. /// /// - MUST return as close to and no fewer than the exact amount of Vault shares that /// will be burned in a withdraw call in the same transaction, i.e. withdraw should /// return the same or fewer shares as `previewWithdraw` if call in the same transaction. /// - MUST NOT account for withdrawal limits like those returned from `maxWithdraw` and should /// always act as if the withdrawal will be accepted, regardless of share balance, etc. /// - MUST be inclusive of withdrawal fees. Integrators should be aware of this. /// - MUST not revert. /// /// Note: Any unfavorable discrepancy between `convertToShares` and `previewWithdraw` SHOULD /// be considered slippage in share price or some other type of condition, /// meaning the depositor will lose assets by depositing. function previewWithdraw(uint256 assets) public view virtual returns (uint256 shares) { if (!_useVirtualShares()) { uint256 supply = totalSupply(); return _eitherIsZero(assets, supply) ? _initialConvertToShares(assets) : FixedPointMathLib.fullMulDivUp(assets, supply, totalAssets()); } uint256 o = _decimalsOffset(); if (o == uint256(0)) { return FixedPointMathLib.fullMulDivUp(assets, totalSupply() + 1, _inc(totalAssets())); } return FixedPointMathLib.fullMulDivUp(assets, totalSupply() + 10 ** o, _inc(totalAssets())); } /// @dev Allows an on-chain or off-chain user to simulate the effects of their redemption /// at the current block, given current on-chain conditions. /// /// - MUST return as close to and no more than the exact amount of assets that /// will be withdrawn in a redeem call in the same transaction, i.e. redeem should /// return the same or more assets as `previewRedeem` if called in the same transaction. /// - MUST NOT account for redemption limits like those returned from `maxRedeem` and should /// always act as if the redemption will be accepted, regardless of approvals, etc. /// - MUST be inclusive of withdrawal fees. Integrators should be aware of this. /// - MUST NOT revert. /// /// Note: Any unfavorable discrepancy between `convertToAssets` and `previewRedeem` SHOULD /// be considered slippage in share price or some other type of condition, /// meaning the depositor will lose assets by depositing. function previewRedeem(uint256 shares) public view virtual returns (uint256 assets) { assets = convertToAssets(shares); } /// @dev Private helper to return if either value is zero. function _eitherIsZero(uint256 a, uint256 b) private pure returns (bool result) { /// @solidity memory-safe-assembly assembly { result := or(iszero(a), iszero(b)) } } /// @dev Private helper to return `x + 1` without the overflow check. /// Used for computing the denominator input to `FixedPointMathLib.fullMulDiv(a, b, x + 1)`. /// When `x == type(uint256).max`, we get `x + 1 == 0` (mod 2**256 - 1), /// and `FixedPointMathLib.fullMulDiv` will revert as the denominator is zero. function _inc(uint256 x) private pure returns (uint256) { unchecked { return x + 1; } } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* DEPOSIT / WITHDRAWAL LIMIT LOGIC */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Returns the maximum amount of the underlying asset that can be deposited /// into the Vault for `to`, via a deposit call. /// /// - MUST return a limited value if `to` is subject to some deposit limit. /// - MUST return `2**256-1` if there is no maximum limit. /// - MUST NOT revert. function maxDeposit(address to) public view virtual returns (uint256 maxAssets) { to = to; // Silence unused variable warning. maxAssets = type(uint256).max; } /// @dev Returns the maximum amount of the Vault shares that can be minter for `to`, /// via a mint call. /// /// - MUST return a limited value if `to` is subject to some mint limit. /// - MUST return `2**256-1` if there is no maximum limit. /// - MUST NOT revert. function maxMint(address to) public view virtual returns (uint256 maxShares) { to = to; // Silence unused variable warning. maxShares = type(uint256).max; } /// @dev Returns the maximum amount of the underlying asset that can be withdrawn /// from the `owner`'s balance in the Vault, via a withdraw call. /// /// - MUST return a limited value if `owner` is subject to some withdrawal limit or timelock. /// - MUST NOT revert. function maxWithdraw(address owner) public view virtual returns (uint256 maxAssets) { maxAssets = convertToAssets(balanceOf(owner)); } /// @dev Returns the maximum amount of Vault shares that can be redeemed /// from the `owner`'s balance in the Vault, via a redeem call. /// /// - MUST return a limited value if `owner` is subject to some withdrawal limit or timelock. /// - MUST return `balanceOf(owner)` otherwise. /// - MUST NOT revert. function maxRedeem(address owner) public view virtual returns (uint256 maxShares) { maxShares = balanceOf(owner); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* DEPOSIT / WITHDRAWAL LOGIC */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Mints `shares` Vault shares to `to` by depositing exactly `assets` /// of underlying tokens. /// /// - MUST emit the {Deposit} event. /// - MAY support an additional flow in which the underlying tokens are owned by the Vault /// contract before the deposit execution, and are accounted for during deposit. /// - MUST revert if all of `assets` cannot be deposited, such as due to deposit limit, /// slippage, insufficient approval, etc. /// /// Note: Most implementations will require pre-approval of the Vault with the /// Vault's underlying `asset` token. function deposit(uint256 assets, address to) public virtual returns (uint256 shares) { if (assets > maxDeposit(to)) _revert(0xb3c61a83); // `DepositMoreThanMax()`. shares = previewDeposit(assets); _deposit(msg.sender, to, assets, shares); } /// @dev Mints exactly `shares` Vault shares to `to` by depositing `assets` /// of underlying tokens. /// /// - MUST emit the {Deposit} event. /// - MAY support an additional flow in which the underlying tokens are owned by the Vault /// contract before the mint execution, and are accounted for during mint. /// - MUST revert if all of `shares` cannot be deposited, such as due to deposit limit, /// slippage, insufficient approval, etc. /// /// Note: Most implementations will require pre-approval of the Vault with the /// Vault's underlying `asset` token. function mint(uint256 shares, address to) public virtual returns (uint256 assets) { if (shares > maxMint(to)) _revert(0x6a695959); // `MintMoreThanMax()`. assets = previewMint(shares); _deposit(msg.sender, to, assets, shares); } /// @dev Burns `shares` from `owner` and sends exactly `assets` of underlying tokens to `to`. /// /// - MUST emit the {Withdraw} event. /// - MAY support an additional flow in which the underlying tokens are owned by the Vault /// contract before the withdraw execution, and are accounted for during withdraw. /// - MUST revert if all of `assets` cannot be withdrawn, such as due to withdrawal limit, /// slippage, insufficient balance, etc. /// /// Note: Some implementations will require pre-requesting to the Vault before a withdrawal /// may be performed. Those methods should be performed separately. function withdraw(uint256 assets, address to, address owner) public virtual returns (uint256 shares) { if (assets > maxWithdraw(owner)) _revert(0x936941fc); // `WithdrawMoreThanMax()`. shares = previewWithdraw(assets); _withdraw(msg.sender, to, owner, assets, shares); } /// @dev Burns exactly `shares` from `owner` and sends `assets` of underlying tokens to `to`. /// /// - MUST emit the {Withdraw} event. /// - MAY support an additional flow in which the underlying tokens are owned by the Vault /// contract before the redeem execution, and are accounted for during redeem. /// - MUST revert if all of shares cannot be redeemed, such as due to withdrawal limit, /// slippage, insufficient balance, etc. /// /// Note: Some implementations will require pre-requesting to the Vault before a redeem /// may be performed. Those methods should be performed separately. function redeem(uint256 shares, address to, address owner) public virtual returns (uint256 assets) { if (shares > maxRedeem(owner)) _revert(0x4656425a); // `RedeemMoreThanMax()`. assets = previewRedeem(shares); _withdraw(msg.sender, to, owner, assets, shares); } /// @dev Internal helper for reverting efficiently. function _revert(uint256 s) private pure { /// @solidity memory-safe-assembly assembly { mstore(0x00, s) revert(0x1c, 0x04) } } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* INTERNAL HELPERS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev For deposits and mints. /// /// Emits a {Deposit} event. function _deposit(address by, address to, uint256 assets, uint256 shares) internal virtual { SafeTransferLib.safeTransferFrom(asset(), by, address(this), assets); _mint(to, shares); /// @solidity memory-safe-assembly assembly { // Emit the {Deposit} event. mstore(0x00, assets) mstore(0x20, shares) let m := shr(96, not(0)) log3(0x00, 0x40, _DEPOSIT_EVENT_SIGNATURE, and(m, by), and(m, to)) } _afterDeposit(assets, shares); } /// @dev For withdrawals and redemptions. /// /// Emits a {Withdraw} event. function _withdraw(address by, address to, address owner, uint256 assets, uint256 shares) internal virtual { if (by != owner) _spendAllowance(owner, by, shares); _beforeWithdraw(assets, shares); _burn(owner, shares); SafeTransferLib.safeTransfer(asset(), to, assets); /// @solidity memory-safe-assembly assembly { // Emit the {Withdraw} event. mstore(0x00, assets) mstore(0x20, shares) let m := shr(96, not(0)) log4(0x00, 0x40, _WITHDRAW_EVENT_SIGNATURE, and(m, by), and(m, to), and(m, owner)) } } /// @dev Internal conversion function (from assets to shares) to apply when the Vault is empty. /// Only used when {_useVirtualShares} returns false. /// /// Note: Make sure to keep this function consistent with {_initialConvertToAssets} /// when overriding it. function _initialConvertToShares(uint256 assets) internal view virtual returns (uint256 shares) { shares = assets; } /// @dev Internal conversion function (from shares to assets) to apply when the Vault is empty. /// Only used when {_useVirtualShares} returns false. /// /// Note: Make sure to keep this function consistent with {_initialConvertToShares} /// when overriding it. function _initialConvertToAssets(uint256 shares) internal view virtual returns (uint256 assets) { assets = shares; } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* HOOKS TO OVERRIDE */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Hook that is called before any withdrawal or redemption. function _beforeWithdraw(uint256 assets, uint256 shares) internal virtual {} /// @dev Hook that is called after any deposit or mint. function _afterDeposit(uint256 assets, uint256 shares) internal virtual {} }
// 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 Permit2 operation has failed. error Permit2Failed(); /// @dev The Permit2 amount must be less than `2**160 - 1`. error Permit2AmountOverflow(); /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* 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 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; /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* 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) } } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* 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 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), staticcall(gas(), token, 0x10, 0x24, add(m, 0x54), 0x20)) mstore(m, 0x8fcbaf0c000000000000000000000000) // `IDAIPermit.permit`. // `nonces` is already at `add(m, 0x54)`. // `1` 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) } } } }
// SPDX-License-Identifier: BUSL-1.1 pragma solidity ^0.8.26; /// @title Provide asset prices in USD with WAD precision /// @dev Generic interface that wraps the Pyth oracle interface IPriceOracle { /// @dev TBD whether to also return a confidence interval. struct Data { // Price with WAD precision uint256 price; // Unix timestamp describing when the price was published uint256 publishTime; } // Signature: 0xd92e233d error ZeroAddress(); error UnavailableData(address asset); /// @notice Returns the price in USD. /// @dev Reverts if the price has not been recently updated (implementation defined). /// @param asset The asset of which to fetch the price. /// @return data function getPrice(address asset) external view returns (Data memory data); /// @notice Returns the price without any sanity checks. /// @dev This function returns the most recent price update in this contract without any recency checks. /// This function is unsafe as the returned price update may be arbitrarily far in the past. /// /// Users of this function should check the `publishTime` to ensure that the returned price is /// sufficiently recent for their application. If you are considering using this function, it may be /// safer / easier to use either `getPrice` or `getPriceNoOlderThan`. /// @return data function getPriceUnsafe(address asset) external view returns (Data memory data); /// @notice Returns the price that is no older than `age` seconds of the current time. /// @dev This function is a sanity-checked version of `getPriceUnsafe` which is useful in /// applications that require a sufficiently-recent price. Reverts if the price wasn't updated sufficiently /// recently. /// @return data function getPriceNoOlderThan(address asset, uint256 age) external view returns (Data memory data); /// @notice Returns whether a price is available or not. function priceAvailable(address asset) external view returns (bool); }
// SPDX-License-Identifier: BUSL-1.1 pragma solidity ^0.8.26; /// @notice This is the interface of HoneyFactory. /// @author Berachain Team interface IHoneyFactory { /// @notice Emitted when a mint rate is set for an asset. event MintRateSet(address indexed asset, uint256 rate); /// @notice Emitted when a redemption rate is set for an asset. event RedeemRateSet(address indexed asset, uint256 rate); /// @notice Emitted when the POLFeeCollector fee rate is set. event POLFeeCollectorFeeRateSet(uint256 rate); /// @notice Emitted when honey is minted /// @param from The account that supplied assets for the minted honey. /// @param to The account that received the honey. /// @param asset The asset used to mint the honey. /// @param assetAmount The amount of assets supplied for minting the honey. /// @param mintAmount The amount of honey that was minted. event HoneyMinted( address indexed from, address indexed to, address indexed asset, uint256 assetAmount, uint256 mintAmount ); /// @notice Emitted when honey is redeemed /// @param from The account that redeemed the honey. /// @param to The account that received the assets. /// @param asset The asset for redeeming the honey. /// @param assetAmount The amount of assets received for redeeming the honey. /// @param redeemAmount The amount of honey that was redeemed. event HoneyRedeemed( address indexed from, address indexed to, address indexed asset, uint256 assetAmount, uint256 redeemAmount ); /// @notice Emitted when the basked mode is forced. /// @param forced The flag that represent the forced basket mode. event BasketModeForced(bool forced); /// @notice Emitted when the depeg offsets are changed. /// @param asset The asset that the depeg offsets are changed. /// @param lower The lower depeg offset. /// @param upper The upper depeg offset. event DepegOffsetsSet(address asset, uint256 lower, uint256 upper); /// @notice Emitted when the liquidation is enabled or disabled. /// @param enabled The flag that represent the liquidation status. event LiquidationStatusSet(bool enabled); /// @notice Emitted when the reference collateral is set. /// @param old The old reference collateral. /// @param asset The new reference collateral. event ReferenceCollateralSet(address old, address asset); /// @notice Emitted when the recapitalize balance threshold is set. /// @param asset The asset that the recapitalize balance threshold is set. /// @param target The target balance threshold. event RecapitalizeBalanceThresholdSet(address asset, uint256 target); /// @notice Emitted when the min shares to recapitalize is set. /// @param minShareAmount The min shares to recapitalize. event MinSharesToRecapitalizeSet(uint256 minShareAmount); /// @notice Emitted when the max feed delay is set. /// @param maxFeedDelay The max feed delay. event MaxFeedDelaySet(uint256 maxFeedDelay); /// @notice Emitted when the liquidation rate is set. /// @param asset The asset that the liquidation rate is set. /// @param rate The liquidation rate. event LiquidationRateSet(address asset, uint256 rate); /// @notice Emitted when the global cap is set. /// @param globalCap The global cap. event GlobalCapSet(uint256 globalCap); /// @notice Emitted when the relative cap is set. /// @param asset The asset that the relative cap is set. /// @param relativeCap The relative cap. event RelativeCapSet(address asset, uint256 relativeCap); /// @notice Emitted when the price oracle is replaced. /// @param oracle The address of the new price oracle. event PriceOracleSet(address oracle); /// @notice Emitted when the liquidate is performed. /// @param badAsset The bad asset that is liquidated. /// @param goodAsset The good asset that is provided. /// @param amount The amount of good asset provided. /// @param sender The account that performed the liquidation. event Liquidated(address badAsset, address goodAsset, uint256 amount, address sender); /// @notice Emitted when the collateral vault is recapitalized. /// @param asset The asset that is recapitalized. /// @param amount The amount of asset provided. /// @param sender The account that performed the recapitalization. event Recapitalized(address asset, uint256 amount, address sender); /// @notice Mint Honey by sending ERC20 to this contract. /// @dev Assest must be registered and must be a good collateral. /// @param amount The amount of ERC20 to mint with. /// @param receiver The address that will receive Honey. /// @param expectBasketMode The flag with which the client communicates its expectation of the basket mode /// status. /// @return The amount of Honey minted. /// @dev The expectBasketMode flag avoid behavioral issues that may happen when the basket mode status changes /// after the client signed its transaction. function mint(address asset, uint256 amount, address receiver, bool expectBasketMode) external returns (uint256); /// @notice Redeem assets by sending Honey in to burn. /// @param honeyAmount The amount of Honey to redeem. /// @param receiver The address that will receive assets. /// @param expectBasketMode The flag with which the client communicates its expectation of the basket mode /// status. /// @return The amount of assets redeemed. /// @dev The expectBasketMode flag avoid behavioral issues that may happen when the basket mode status changes /// after the client signed its transaction. function redeem( address asset, uint256 honeyAmount, address receiver, bool expectBasketMode ) external returns (uint256[] memory); /// @notice Liquidate a bad collateral asset. /// @param badCollateral The ERC20 asset to liquidate. /// @param goodCollateral The ERC20 asset to provide in place. /// @param goodAmount The amount provided. /// @return badAmount The amount obtained. function liquidate( address badCollateral, address goodCollateral, uint256 goodAmount ) external returns (uint256 badAmount); /// @notice Recapitalize a collateral vault. /// @param asset The ERC20 asset to recapitalize. /// @param amount The amount provided. function recapitalize(address asset, uint256 amount) external; }
// SPDX-License-Identifier: BUSL-1.1 // solhint-disable-next-line compiler-version pragma solidity 0.8.26; import { SafeTransferLib } from "solady/src/utils/SafeTransferLib.sol"; library Utils { using SafeTransferLib for address; /// @notice The gas limit for a transfer, used to prevent malicious token griefing. uint32 constant TRANSFER_GAS_LIMIT = 500_000; /// @notice Error for overflow when increasing allowance error IncreaseAllowanceOverflow(); /// @dev Reverts with the selector of a custom error in the scratch space. function revertWith(bytes4 selector) internal pure { assembly ("memory-safe") { mstore(0, selector) revert(0, 0x04) } } /// @dev Reverts for the reason encoding a silent revert, Error(string), or a custom error. function revertFor(bytes memory reason) internal pure { assembly ("memory-safe") { revert(add(reason, 0x20), mload(reason)) } } function revertWith(bytes4 selector, address addr) internal pure { assembly ("memory-safe") { mstore(0, selector) mstore(0x04, addr) revert(0, 0x24) // 4 (selector) + 32 (addr) } } function revertWith(bytes4 selector, uint256 amount) internal pure { assembly ("memory-safe") { mstore(0, selector) mstore(0x04, amount) revert(0, 0x24) // 4 (selector) + 32 (amount) } } function revertWith(bytes4 selector, uint256 amount1, uint256 amount2) internal pure { assembly ("memory-safe") { mstore(0, selector) mstore(0x04, amount1) mstore(0x24, amount2) revert(0, 0x44) // 4 (selector) + 32 (amount1) + 32 (amount2) } } function revertWith(bytes4 selector, address addr1, address addr2) internal pure { assembly ("memory-safe") { mstore(0, selector) mstore(0x04, addr1) mstore(0x24, addr2) revert(0, 0x44) // 4 (selector) + 32 (addr1) + 32 (addr2) } } /// @dev Increase the calling contract's allowance toward `spender` by `amount`. /// @dev Does not check if token exists. function safeIncreaseAllowance(address token, address spender, uint256 amount) internal { unchecked { uint256 oldAllowance = allowance(token, address(this), spender); uint256 newAllowance = oldAllowance + amount; if (newAllowance < oldAllowance) revertWith(IncreaseAllowanceOverflow.selector); token.safeApprove(spender, newAllowance); } } /// @dev Returns the amount of ERC20 `token` that `owner` has allowed `spender` to use. /// Returns zero if the `token` does not exist. function allowance(address token, address owner, address spender) internal view returns (uint256 amount) { assembly ("memory-safe") { mstore(0, 0xdd62ed3e00000000000000000000000000000000000000000000000000000000) // Store function selector of // `allowance(address,address)`. mstore(0x04, owner) // Store the `owner` argument. mstore(0x24, spender) // Store the `spender` argument. amount := mul( // The arguments of `mul` are evaluated from right to left. mload(0), and( // The arguments of `and` are evaluated from right to left. gt(returndatasize(), 0x1f), // At least 32 bytes returned. staticcall(gas(), token, 0, 0x44, 0, 0x20) ) ) mstore(0x24, 0) // clear the upper bits of free memory pointer. } } /// @dev Sends `amount` of ERC20 `token` from the current contract to `to`. /// Doesn't revert upon failure. function trySafeTransfer(address token, address to, uint256 amount) internal returns (bool success) { assembly ("memory-safe") { mstore(0x14, to) // Store the `to` argument. mstore(0x34, amount) // Store the `amount` argument. mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)` function selector. // Perform the transfer, returning success status. success := and( or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing. call(TRANSFER_GAS_LIMIT, token, 0, 0x10, 0x44, 0x00, 0x20) ) mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten. } } function changeDecimals(uint256 amount, uint8 from, uint8 to) internal pure returns (uint256) { if (from == to) { return amount; } if (from > to) { return amount / (10 ** (from - to)); } else { return amount * (10 ** (to - from)); } } }
// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.26; import { ERC20 } from "solady/src/tokens/ERC20.sol"; import { AccessControlUpgradeable } from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol"; import { UUPSUpgradeable } from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; import { Utils } from "../libraries/Utils.sol"; import { IHoneyErrors } from "./IHoneyErrors.sol"; /// @notice This is the ERC20 token representation of Berachain's native stablecoin, Honey. /// @author Berachain Team contract Honey is ERC20, AccessControlUpgradeable, UUPSUpgradeable, IHoneyErrors { using Utils for bytes4; string private constant NAME = "Honey"; string private constant SYMBOL = "HONEY"; /// @notice The factory contract that mints and burns Honey. address public factory; /// @custom:oz-upgrades-unsafe-allow constructor constructor() { _disableInitializers(); } function initialize(address _governance, address _factory) external initializer { __AccessControl_init(); __UUPSUpgradeable_init(); // Check for zero addresses. if (_factory == address(0)) ZeroAddress.selector.revertWith(); if (_governance == address(0)) ZeroAddress.selector.revertWith(); factory = _factory; _grantRole(DEFAULT_ADMIN_ROLE, _governance); } function _authorizeUpgrade(address) internal override onlyRole(DEFAULT_ADMIN_ROLE) { } modifier onlyFactory() { if (msg.sender != factory) NotFactory.selector.revertWith(); _; } /// @notice Mint Honey to the receiver. /// @dev Only the factory can call this function. /// @param to The receiver address. /// @param amount The amount of Honey to mint. function mint(address to, uint256 amount) external onlyFactory { _mint(to, amount); } /// @notice Burn Honey from an account. /// @dev Only the factory can call this function. /// @param from The account to burn Honey from. /// @param amount The amount of Honey to burn. function burn(address from, uint256 amount) external onlyFactory { _burn(from, amount); } function name() public pure override returns (string memory) { return NAME; } function symbol() public pure override returns (string memory) { return SYMBOL; } }
// SPDX-License-Identifier: BUSL-1.1 // To support named parameters in mapping types and custom operators for user-defined value types. pragma solidity 0.8.26; import { AccessControlUpgradeable } from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol"; import { PausableUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/PausableUpgradeable.sol"; import { UUPSUpgradeable } from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; import { LibClone } from "solady/src/utils/LibClone.sol"; import { IHoneyErrors } from "./IHoneyErrors.sol"; import { Utils } from "../libraries/Utils.sol"; import { CollateralVault } from "./CollateralVault.sol"; /// @notice This is the admin contract that manages the vaults and fees. /// @author Berachain Team abstract contract VaultAdmin is AccessControlUpgradeable, PausableUpgradeable, UUPSUpgradeable, IHoneyErrors { using Utils for bytes4; /// @notice Emitted when the fee receiver address is set. event FeeReceiverSet(address indexed feeReceiver); /// @notice Emitted when the POL Fee Collector address is set. event POLFeeCollectorSet(address indexed polFeeCollector); /// @notice Emitted when a new vault is created. event VaultCreated(address indexed vault, address indexed asset); /// @notice Emitted when collateral asset status is set. event CollateralAssetStatusSet(address indexed asset, bool isBadCollateral); /// @notice Emitted when a collected fee is withdrawn. event CollectedFeeWithdrawn(address indexed asset, address indexed receiver, uint256 shares, uint256 assets); /// @notice Emitted when a price feed is changed. event PriceFeedChanged(address indexed asset, bytes32 id); /// @notice The MANAGER role. bytes32 public constant MANAGER_ROLE = keccak256("MANAGER_ROLE"); /// @notice The PAUSER role. /// @dev This role is used to only pause the factory and vaults. bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE"); /// @notice The beacon address. address public beacon; /// @notice The address of the fee receiver. address public feeReceiver; /// @notice The address of the POL Fee Collector. address public polFeeCollector; /// @notice Array of registered assets. address[] public registeredAssets; /// @notice Mapping of assets to their corresponding vaults. mapping(address asset => CollateralVault vault) public vaults; /// @notice Mapping of bad collateral assets. mapping(address asset => bool badCollateral) public isBadCollateralAsset; /// @notice Mapping of receiver to asset to collected fee. /// @dev Stores the shares of fees corresponding to the receiver that are not yet redeemed. mapping(address receiver => mapping(address asset => uint256 collectedFee)) public collectedFees; /// @dev Stores the shares of fees for each asset that are not yet redeemed. mapping(address asset => uint256 collectedFee) internal collectedAssetFees; /// @dev This gap is used to prevent storage collisions. uint256[49] private __gap; /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* INITIALIZER */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Must be called by the initializer of the inheriting contract. /// @param _governance The address of the governance. /// @param _feeReceiver The address of the fee receiver. /// @param _polFeeCollector The address of the POL Fee Collector. function __VaultAdmin_init( address _governance, address _polFeeCollector, address _feeReceiver, address _beacon ) internal onlyInitializing { __AccessControl_init(); __Pausable_init(); __UUPSUpgradeable_init(); __VaultAdmin_init_unchained(_governance, _polFeeCollector, _feeReceiver, _beacon); // Allow the MANAGER role to manage the PAUSER role. // MANAGER role can grant and revoke access for the PAUSER role. _setRoleAdmin(PAUSER_ROLE, MANAGER_ROLE); } function __VaultAdmin_init_unchained( address _governance, address _polFeeCollector, address _feeReceiver, address _beacon ) internal onlyInitializing { if (_governance == address(0)) ZeroAddress.selector.revertWith(); if (_polFeeCollector == address(0)) ZeroAddress.selector.revertWith(); if (_feeReceiver == address(0)) ZeroAddress.selector.revertWith(); if (_beacon == address(0)) ZeroAddress.selector.revertWith(); beacon = _beacon; _grantRole(DEFAULT_ADMIN_ROLE, _governance); feeReceiver = _feeReceiver; polFeeCollector = _polFeeCollector; emit FeeReceiverSet(_feeReceiver); emit POLFeeCollectorSet(_polFeeCollector); } /// @notice Check if the asset is registered. function _checkRegisteredAsset(address asset) internal view { if (address(vaults[asset]) == address(0)) { AssetNotRegistered.selector.revertWith(asset); } } /// @notice Check if the asset is not a bad collateral. function _checkGoodCollateralAsset(address asset) internal view { if (isBadCollateralAsset[asset]) { AssetIsBadCollateral.selector.revertWith(asset); } } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* ADMIN FUNCTIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ function _authorizeUpgrade(address newImplementation) internal virtual override { // Silent warning newImplementation; _checkRole(DEFAULT_ADMIN_ROLE); } /// @notice Pause the contract. /// @dev Only the PAUSER role can call this function. function pause() external { _checkRole(PAUSER_ROLE); _pause(); } /// @notice Unpause the contract. /// @dev only the MANAGER role can call this function. function unpause() external { _checkRole(MANAGER_ROLE); _unpause(); } /// @notice Pause the vault for a given asset. /// @dev Only the PAUSER role can call this function. /// @dev Only registered assets can be paused. /// @param asset The address of the asset. function pauseVault(address asset) external { _checkRole(PAUSER_ROLE); _checkRegisteredAsset(asset); CollateralVault(address(vaults[asset])).pause(); } /// @notice Unpause the vault for a given asset. /// @dev Only the MANAGER role can call this function. /// @dev Only registered assets can be unpaused. /// @param asset The address of the asset. function unpauseVault(address asset) external { _checkRole(MANAGER_ROLE); _checkRegisteredAsset(asset); CollateralVault(address(vaults[asset])).unpause(); } /// @notice Set the custody info for the vault. /// @dev Only the default admin role can call this function. /// @dev Only registered assets can be set as custody vault. /// @dev Checks on zero address and input are done in the vault. /// @param asset The address of the asset. /// @param isCustodyVault Whether the vault is a custody vault. /// @param custodyAddress The address of the custody. function setCustodyInfo(address asset, bool isCustodyVault, address custodyAddress) external { _checkRole(DEFAULT_ADMIN_ROLE); _checkRegisteredAsset(asset); vaults[asset].setCustodyInfo(isCustodyVault, custodyAddress); } function _createVault(address asset) internal returns (CollateralVault) { _checkRole(DEFAULT_ADMIN_ROLE); // Revert if the vault for the given asset is already registered. if (address(vaults[asset]) != address(0)) { VaultAlreadyRegistered.selector.revertWith(asset); } // Register the asset. registeredAssets.push(asset); // Use solady library to deploy deterministic beacon proxy. // NOTE: bits not part of the encoding of the address type cannot be assumed to be zero bytes32 salt; assembly ("memory-safe") { mstore(0, shr(96, shl(96, asset))) salt := keccak256(0, 0x20) } CollateralVault vault = CollateralVault(LibClone.deployDeterministicERC1967BeaconProxy(beacon, salt)); vault.initialize(asset, address(this)); vaults[asset] = vault; emit VaultCreated(address(vault), address(asset)); return vault; } /// @notice Set the fee receiver address. /// @dev Only the default admin role can call this function. /// @dev Reverts if the fee receiver address is zero address. /// @param _feeReceiver The address of the fee receiver. function setFeeReceiver(address _feeReceiver) external { _checkRole(DEFAULT_ADMIN_ROLE); if (_feeReceiver == address(0)) ZeroAddress.selector.revertWith(); feeReceiver = _feeReceiver; emit FeeReceiverSet(_feeReceiver); } /// @notice Set the POL Fee Collector address. /// @dev Only the default admin role can call this function. /// @dev Reverts if the POL Fee Collector address is zero address. /// @param _polFeeCollector The address of the POL Fee Collector. function setPOLFeeCollector(address _polFeeCollector) external { _checkRole(DEFAULT_ADMIN_ROLE); if (_polFeeCollector == address(0)) ZeroAddress.selector.revertWith(); polFeeCollector = _polFeeCollector; emit POLFeeCollectorSet(_polFeeCollector); } /// @notice Set the bad collateral status of an asset. /// @dev Only the manager role can call this function. /// @dev Only registered assets can be set as bad collateral. /// @dev If set to true, minting will be disabled for the asset. /// @param asset The address of the asset. /// @param _isBadCollateral The status of the asset. function setCollateralAssetStatus(address asset, bool _isBadCollateral) external { _checkRole(MANAGER_ROLE); _checkRegisteredAsset(asset); isBadCollateralAsset[asset] = _isBadCollateral; emit CollateralAssetStatusSet(asset, _isBadCollateral); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* FEE RELATED */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @notice Withdraw all the collected fees for a `receiver`. /// @dev It loops over all the registered assets and withdraws the collected fees. /// @param receiver The address of the receiver. function withdrawAllFees(address receiver) external { uint256 numAssets = numRegisteredAssets(); for (uint256 i; i < numAssets;) { address asset = registeredAssets[i]; _withdrawCollectedFee(asset, receiver); unchecked { ++i; } } } /// @notice Withdraw the collected fees for a `receiver` for a specific `asset`. function withdrawFee(address asset, address receiver) external returns (uint256 assets) { _checkRegisteredAsset(asset); assets = _withdrawCollectedFee(asset, receiver); } /// @notice Internal function to withdraw the collected fees. /// @dev Redeems the shares from the vault and transfers the assets to the receiver. /// @param asset The address of the asset. /// @param receiver The address of the receiver. /// @return assets The amount of assets redeemed. function _withdrawCollectedFee(address asset, address receiver) internal returns (uint256 assets) { uint256 shares = collectedFees[receiver][asset]; if (vaults[asset].convertToAssets(shares) == 0) { return 0; } collectedFees[receiver][asset] = 0; // All the shares refer to the receiver and the assets are transferred to the receiver. // This subtraction is safe because of they are counted in the same function. collectedAssetFees[asset] -= shares; assets = vaults[asset].redeem(shares, receiver, address(this)); emit CollectedFeeWithdrawn(asset, receiver, shares, assets); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* GETTERS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @notice Get the length of `registeredAssets` array. function numRegisteredAssets() public view returns (uint256) { return registeredAssets.length; } function _lookupRegistrationIndex(address asset) internal view returns (uint256 index) { for (uint256 i = 0; i < registeredAssets.length; i++) { if (registeredAssets[i] == asset) { return i; } } } }
// 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 gt(x, div(not(0), y)) { if 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 { z := mul(x, y) // Equivalent to `require(y == 0 || x <= type(uint256).max / y)`. if iszero(eq(div(z, y), x)) { if y { mstore(0x00, 0xbac65e5b) // `MulWadFailed()`. revert(0x1c, 0x04) } } z := add(iszero(iszero(mod(z, WAD))), div(z, 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 && x <= type(uint256).max / WAD)`. if iszero(mul(y, lt(x, add(1, 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(mul(y, eq(sdiv(z, WAD), x))) { mstore(0x00, 0x5c43740d) // `SDivWadFailed()`. revert(0x1c, 0x04) } z := sdiv(z, 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 && x <= type(uint256).max / WAD)`. if iszero(mul(y, lt(x, add(1, 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 p) = (int256(WAD), 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)) } } /// @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) { unchecked { z = (uint256(x) + uint256(x >> 255)) ^ uint256(x >> 255); } } /// @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 := add(xor(sub(0, gt(x, y)), sub(y, x)), gt(x, y)) } } /// @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 := add(xor(sub(0, sgt(x, y)), sub(y, x)), sgt(x, y)) } } /// @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, begin, end) = (~t, ~begin, ~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, begin, end) = (~t, ~begin, ~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 - a), uint256(t - begin), uint256(end - begin))); return int256(uint256(a) - fullMulDiv(uint256(a - b), uint256(t - begin), uint256(end - begin))); } } /// @dev Returns if `x` is an even number. Some people may need this. function isEven(uint256 x) internal pure returns (bool) { return x & uint256(1) == uint256(0); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* 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: MIT // OpenZeppelin Contracts (last updated v5.0.0) (access/AccessControl.sol) pragma solidity ^0.8.20; import {IAccessControl} from "@openzeppelin/contracts/access/IAccessControl.sol"; import {ContextUpgradeable} from "../utils/ContextUpgradeable.sol"; import {ERC165Upgradeable} from "../utils/introspection/ERC165Upgradeable.sol"; import {Initializable} from "../proxy/utils/Initializable.sol"; /** * @dev Contract module that allows children to implement role-based access * control mechanisms. This is a lightweight version that doesn't allow enumerating role * members except through off-chain means by accessing the contract event logs. Some * applications may benefit from on-chain enumerability, for those cases see * {AccessControlEnumerable}. * * Roles are referred to by their `bytes32` identifier. These should be exposed * in the external API and be unique. The best way to achieve this is by * using `public constant` hash digests: * * ```solidity * bytes32 public constant MY_ROLE = keccak256("MY_ROLE"); * ``` * * Roles can be used to represent a set of permissions. To restrict access to a * function call, use {hasRole}: * * ```solidity * function foo() public { * require(hasRole(MY_ROLE, msg.sender)); * ... * } * ``` * * Roles can be granted and revoked dynamically via the {grantRole} and * {revokeRole} functions. Each role has an associated admin role, and only * accounts that have a role's admin role can call {grantRole} and {revokeRole}. * * By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means * that only accounts with this role will be able to grant or revoke other * roles. More complex role relationships can be created by using * {_setRoleAdmin}. * * WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to * grant and revoke this role. Extra precautions should be taken to secure * accounts that have been granted it. We recommend using {AccessControlDefaultAdminRules} * to enforce additional security measures for this role. */ abstract contract AccessControlUpgradeable is Initializable, ContextUpgradeable, IAccessControl, ERC165Upgradeable { struct RoleData { mapping(address account => bool) hasRole; bytes32 adminRole; } bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00; /// @custom:storage-location erc7201:openzeppelin.storage.AccessControl struct AccessControlStorage { mapping(bytes32 role => RoleData) _roles; } // keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.AccessControl")) - 1)) & ~bytes32(uint256(0xff)) bytes32 private constant AccessControlStorageLocation = 0x02dd7bc7dec4dceedda775e58dd541e08a116c6c53815c0bd028192f7b626800; function _getAccessControlStorage() private pure returns (AccessControlStorage storage $) { assembly { $.slot := AccessControlStorageLocation } } /** * @dev Modifier that checks that an account has a specific role. Reverts * with an {AccessControlUnauthorizedAccount} error including the required role. */ modifier onlyRole(bytes32 role) { _checkRole(role); _; } function __AccessControl_init() internal onlyInitializing { } function __AccessControl_init_unchained() internal onlyInitializing { } /** * @dev See {IERC165-supportsInterface}. */ function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) { return interfaceId == type(IAccessControl).interfaceId || super.supportsInterface(interfaceId); } /** * @dev Returns `true` if `account` has been granted `role`. */ function hasRole(bytes32 role, address account) public view virtual returns (bool) { AccessControlStorage storage $ = _getAccessControlStorage(); return $._roles[role].hasRole[account]; } /** * @dev Reverts with an {AccessControlUnauthorizedAccount} error if `_msgSender()` * is missing `role`. Overriding this function changes the behavior of the {onlyRole} modifier. */ function _checkRole(bytes32 role) internal view virtual { _checkRole(role, _msgSender()); } /** * @dev Reverts with an {AccessControlUnauthorizedAccount} error if `account` * is missing `role`. */ function _checkRole(bytes32 role, address account) internal view virtual { if (!hasRole(role, account)) { revert AccessControlUnauthorizedAccount(account, role); } } /** * @dev Returns the admin role that controls `role`. See {grantRole} and * {revokeRole}. * * To change a role's admin, use {_setRoleAdmin}. */ function getRoleAdmin(bytes32 role) public view virtual returns (bytes32) { AccessControlStorage storage $ = _getAccessControlStorage(); return $._roles[role].adminRole; } /** * @dev Grants `role` to `account`. * * If `account` had not been already granted `role`, emits a {RoleGranted} * event. * * Requirements: * * - the caller must have ``role``'s admin role. * * May emit a {RoleGranted} event. */ function grantRole(bytes32 role, address account) public virtual onlyRole(getRoleAdmin(role)) { _grantRole(role, account); } /** * @dev Revokes `role` from `account`. * * If `account` had been granted `role`, emits a {RoleRevoked} event. * * Requirements: * * - the caller must have ``role``'s admin role. * * May emit a {RoleRevoked} event. */ function revokeRole(bytes32 role, address account) public virtual onlyRole(getRoleAdmin(role)) { _revokeRole(role, account); } /** * @dev Revokes `role` from the calling account. * * Roles are often managed via {grantRole} and {revokeRole}: this function's * purpose is to provide a mechanism for accounts to lose their privileges * if they are compromised (such as when a trusted device is misplaced). * * If the calling account had been revoked `role`, emits a {RoleRevoked} * event. * * Requirements: * * - the caller must be `callerConfirmation`. * * May emit a {RoleRevoked} event. */ function renounceRole(bytes32 role, address callerConfirmation) public virtual { if (callerConfirmation != _msgSender()) { revert AccessControlBadConfirmation(); } _revokeRole(role, callerConfirmation); } /** * @dev Sets `adminRole` as ``role``'s admin role. * * Emits a {RoleAdminChanged} event. */ function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual { AccessControlStorage storage $ = _getAccessControlStorage(); bytes32 previousAdminRole = getRoleAdmin(role); $._roles[role].adminRole = adminRole; emit RoleAdminChanged(role, previousAdminRole, adminRole); } /** * @dev Attempts to grant `role` to `account` and returns a boolean indicating if `role` was granted. * * Internal function without access restriction. * * May emit a {RoleGranted} event. */ function _grantRole(bytes32 role, address account) internal virtual returns (bool) { AccessControlStorage storage $ = _getAccessControlStorage(); if (!hasRole(role, account)) { $._roles[role].hasRole[account] = true; emit RoleGranted(role, account, _msgSender()); return true; } else { return false; } } /** * @dev Attempts to revoke `role` to `account` and returns a boolean indicating if `role` was revoked. * * Internal function without access restriction. * * May emit a {RoleRevoked} event. */ function _revokeRole(bytes32 role, address account) internal virtual returns (bool) { AccessControlStorage storage $ = _getAccessControlStorage(); if (hasRole(role, account)) { $._roles[role].hasRole[account] = false; emit RoleRevoked(role, account, _msgSender()); return true; } else { return false; } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (proxy/utils/UUPSUpgradeable.sol) pragma solidity ^0.8.20; import {IERC1822Proxiable} from "@openzeppelin/contracts/interfaces/draft-IERC1822.sol"; import {ERC1967Utils} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Utils.sol"; import {Initializable} from "./Initializable.sol"; /** * @dev An upgradeability mechanism designed for UUPS proxies. The functions included here can perform an upgrade of an * {ERC1967Proxy}, when this contract is set as the implementation behind such a proxy. * * A security mechanism ensures that an upgrade does not turn off upgradeability accidentally, although this risk is * reinstated if the upgrade retains upgradeability but removes the security mechanism, e.g. by replacing * `UUPSUpgradeable` with a custom implementation of upgrades. * * The {_authorizeUpgrade} function must be overridden to include access restriction to the upgrade mechanism. */ abstract contract UUPSUpgradeable is Initializable, IERC1822Proxiable { /// @custom:oz-upgrades-unsafe-allow state-variable-immutable address private immutable __self = address(this); /** * @dev The version of the upgrade interface of the contract. If this getter is missing, both `upgradeTo(address)` * and `upgradeToAndCall(address,bytes)` are present, and `upgradeTo` must be used if no function should be called, * while `upgradeToAndCall` will invoke the `receive` function if the second argument is the empty byte string. * If the getter returns `"5.0.0"`, only `upgradeToAndCall(address,bytes)` is present, and the second argument must * be the empty byte string if no function should be called, making it impossible to invoke the `receive` function * during an upgrade. */ string public constant UPGRADE_INTERFACE_VERSION = "5.0.0"; /** * @dev The call is from an unauthorized context. */ error UUPSUnauthorizedCallContext(); /** * @dev The storage `slot` is unsupported as a UUID. */ error UUPSUnsupportedProxiableUUID(bytes32 slot); /** * @dev Check that the execution is being performed through a delegatecall call and that the execution context is * a proxy contract with an implementation (as defined in ERC-1967) pointing to self. This should only be the case * for UUPS and transparent proxies that are using the current contract as their implementation. Execution of a * function through ERC-1167 minimal proxies (clones) would not normally pass this test, but is not guaranteed to * fail. */ modifier onlyProxy() { _checkProxy(); _; } /** * @dev Check that the execution is not being performed through a delegate call. This allows a function to be * callable on the implementing contract but not through proxies. */ modifier notDelegated() { _checkNotDelegated(); _; } function __UUPSUpgradeable_init() internal onlyInitializing { } function __UUPSUpgradeable_init_unchained() internal onlyInitializing { } /** * @dev Implementation of the ERC-1822 {proxiableUUID} function. This returns the storage slot used by the * implementation. It is used to validate the implementation's compatibility when performing an upgrade. * * IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks * bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this * function revert if invoked through a proxy. This is guaranteed by the `notDelegated` modifier. */ function proxiableUUID() external view virtual notDelegated returns (bytes32) { return ERC1967Utils.IMPLEMENTATION_SLOT; } /** * @dev Upgrade the implementation of the proxy to `newImplementation`, and subsequently execute the function call * encoded in `data`. * * Calls {_authorizeUpgrade}. * * Emits an {Upgraded} event. * * @custom:oz-upgrades-unsafe-allow-reachable delegatecall */ function upgradeToAndCall(address newImplementation, bytes memory data) public payable virtual onlyProxy { _authorizeUpgrade(newImplementation); _upgradeToAndCallUUPS(newImplementation, data); } /** * @dev Reverts if the execution is not performed via delegatecall or the execution * context is not of a proxy with an ERC-1967 compliant implementation pointing to self. * See {_onlyProxy}. */ function _checkProxy() internal view virtual { if ( address(this) == __self || // Must be called through delegatecall ERC1967Utils.getImplementation() != __self // Must be called through an active proxy ) { revert UUPSUnauthorizedCallContext(); } } /** * @dev Reverts if the execution is performed via delegatecall. * See {notDelegated}. */ function _checkNotDelegated() internal view virtual { if (address(this) != __self) { // Must not be called through delegatecall revert UUPSUnauthorizedCallContext(); } } /** * @dev Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by * {upgradeToAndCall}. * * Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}. * * ```solidity * function _authorizeUpgrade(address) internal onlyOwner {} * ``` */ function _authorizeUpgrade(address newImplementation) internal virtual; /** * @dev Performs an implementation upgrade with a security check for UUPS proxies, and additional setup call. * * As a security check, {proxiableUUID} is invoked in the new implementation, and the return value * is expected to be the implementation slot in ERC-1967. * * Emits an {IERC1967-Upgraded} event. */ function _upgradeToAndCallUUPS(address newImplementation, bytes memory data) private { try IERC1822Proxiable(newImplementation).proxiableUUID() returns (bytes32 slot) { if (slot != ERC1967Utils.IMPLEMENTATION_SLOT) { revert UUPSUnsupportedProxiableUUID(slot); } ERC1967Utils.upgradeToAndCall(newImplementation, data); } catch { // The implementation is not UUPS revert ERC1967Utils.ERC1967InvalidImplementation(newImplementation); } } }
// SPDX-License-Identifier: BUSL-1.1 pragma solidity ^0.8.26; /// @notice Interface of Honey errors interface IHoneyErrors { // Signature: 0xd92e233d error ZeroAddress(); // Signature: 0x14799671 error MismatchedOwner(address owner, address expectedOwner); // Signature: 0x38bfcc16 error VaultAlreadyRegistered(address asset); // Signature: 0x1a2a9e87 error AssetNotRegistered(address asset); // Signature: 0x536dd9ef error UnauthorizedCaller(address caller, address expectedCaller); // Signature: 0xada46d16 error OverOneHundredPercentRate(uint256 rate); // Signature: 0x71fba9d0 error UnderNinetyEightPercentRate(uint256 rate); // Signature: 0x32cc7236 error NotFactory(); // Signature: 0xb97fded1 error InsufficientAssets(uint256 assets, uint256 shares); // Signature: 0x6ba2e418 error AssetIsBadCollateral(address asset); // Signature: 0x2595dbe7 error ExceedRelativeCap(); // Signature: 0x6dabf61e error ExceedGlobalCap(); // Signature: 0x07091331 error LiquidationDisabled(); // Signature: 0x867344a8 error AssetIsNotBadCollateral(address asset); // Signature: 0x0dc86ad3 error LiquidationWithReferenceCollateral(); // Signature: 0xda9f8b34 error VaultPaused(); // Signature: 0x168cecf7 error ZeroWeight(address asset); // Signature: 0x10e4809e error RecapitalizeNotNeeded(address asset); // Signature: 0x2dd78c7e error InsufficientRecapitalizeAmount(uint256 amount); // Signature: 0xc64200e9 error AmountOutOfRange(); // Signature: 0x2201a6c3 error NotPegged(address asset); // Signature: 0x1f2a2005 error ZeroAmount(); // Signature: 0x6ce14a8b error UnexpectedBasketModeStatus(); // Signature: 0x5419864a error CapCanCauseDenialOfService(); // Signature: 0xcd09f603 error InvalidCustodyInfoInput(); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/Pausable.sol) pragma solidity ^0.8.20; import {ContextUpgradeable} from "../utils/ContextUpgradeable.sol"; import {Initializable} from "../proxy/utils/Initializable.sol"; /** * @dev Contract module which allows children to implement an emergency stop * mechanism that can be triggered by an authorized account. * * This module is used through inheritance. It will make available the * modifiers `whenNotPaused` and `whenPaused`, which can be applied to * the functions of your contract. Note that they will not be pausable by * simply including this module, only once the modifiers are put in place. */ abstract contract PausableUpgradeable is Initializable, ContextUpgradeable { /// @custom:storage-location erc7201:openzeppelin.storage.Pausable struct PausableStorage { bool _paused; } // keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.Pausable")) - 1)) & ~bytes32(uint256(0xff)) bytes32 private constant PausableStorageLocation = 0xcd5ed15c6e187e77e9aee88184c21f4f2182ab5827cb3b7e07fbedcd63f03300; function _getPausableStorage() private pure returns (PausableStorage storage $) { assembly { $.slot := PausableStorageLocation } } /** * @dev Emitted when the pause is triggered by `account`. */ event Paused(address account); /** * @dev Emitted when the pause is lifted by `account`. */ event Unpaused(address account); /** * @dev The operation failed because the contract is paused. */ error EnforcedPause(); /** * @dev The operation failed because the contract is not paused. */ error ExpectedPause(); /** * @dev Initializes the contract in unpaused state. */ function __Pausable_init() internal onlyInitializing { __Pausable_init_unchained(); } function __Pausable_init_unchained() internal onlyInitializing { PausableStorage storage $ = _getPausableStorage(); $._paused = false; } /** * @dev Modifier to make a function callable only when the contract is not paused. * * Requirements: * * - The contract must not be paused. */ modifier whenNotPaused() { _requireNotPaused(); _; } /** * @dev Modifier to make a function callable only when the contract is paused. * * Requirements: * * - The contract must be paused. */ modifier whenPaused() { _requirePaused(); _; } /** * @dev Returns true if the contract is paused, and false otherwise. */ function paused() public view virtual returns (bool) { PausableStorage storage $ = _getPausableStorage(); return $._paused; } /** * @dev Throws if the contract is paused. */ function _requireNotPaused() internal view virtual { if (paused()) { revert EnforcedPause(); } } /** * @dev Throws if the contract is not paused. */ function _requirePaused() internal view virtual { if (!paused()) { revert ExpectedPause(); } } /** * @dev Triggers stopped state. * * Requirements: * * - The contract must not be paused. */ function _pause() internal virtual whenNotPaused { PausableStorage storage $ = _getPausableStorage(); $._paused = true; emit Paused(_msgSender()); } /** * @dev Returns to normal state. * * Requirements: * * - The contract must be paused. */ function _unpause() internal virtual whenPaused { PausableStorage storage $ = _getPausableStorage(); $._paused = false; emit Unpaused(_msgSender()); } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.4; /// @notice Minimal proxy library. /// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/LibClone.sol) /// @author Minimal proxy by 0age (https://github.com/0age) /// @author Clones with immutable args by wighawag, zefram.eth, Saw-mon & Natalie /// (https://github.com/Saw-mon-and-Natalie/clones-with-immutable-args) /// @author Minimal ERC1967 proxy by jtriley-eth (https://github.com/jtriley-eth/minimum-viable-proxy) /// /// @dev Minimal proxy: /// Although the sw0nt pattern saves 5 gas over the ERC1167 pattern during runtime, /// it is not supported out-of-the-box on Etherscan. Hence, we choose to use the 0age pattern, /// which saves 4 gas over the ERC1167 pattern during runtime, and has the smallest bytecode. /// - Automatically verified on Etherscan. /// /// @dev Minimal proxy (PUSH0 variant): /// This is a new minimal proxy that uses the PUSH0 opcode introduced during Shanghai. /// It is optimized first for minimal runtime gas, then for minimal bytecode. /// The PUSH0 clone functions are intentionally postfixed with a jarring "_PUSH0" as /// many EVM chains may not support the PUSH0 opcode in the early months after Shanghai. /// Please use with caution. /// - Automatically verified on Etherscan. /// /// @dev Clones with immutable args (CWIA): /// The implementation of CWIA here is does NOT append the immutable args into the calldata /// passed into delegatecall. It is simply an ERC1167 minimal proxy with the immutable arguments /// appended to the back of the runtime bytecode. /// - Uses the identity precompile (0x4) to copy args during deployment. /// /// @dev Minimal ERC1967 proxy: /// An minimal ERC1967 proxy, intended to be upgraded with UUPS. /// This is NOT the same as ERC1967Factory's transparent proxy, which includes admin logic. /// - Automatically verified on Etherscan. /// /// @dev Minimal ERC1967 proxy with immutable args: /// - Uses the identity precompile (0x4) to copy args during deployment. /// - Automatically verified on Etherscan. /// /// @dev ERC1967I proxy: /// An variant of the minimal ERC1967 proxy, with a special code path that activates /// if `calldatasize() == 1`. This code path skips the delegatecall and directly returns the /// `implementation` address. The returned implementation is guaranteed to be valid if the /// keccak256 of the proxy's code is equal to `ERC1967I_CODE_HASH`. /// /// @dev ERC1967I proxy with immutable args: /// An variant of the minimal ERC1967 proxy, with a special code path that activates /// if `calldatasize() == 1`. This code path skips the delegatecall and directly returns the /// - Uses the identity precompile (0x4) to copy args during deployment. /// /// @dev Minimal ERC1967 beacon proxy: /// A minimal beacon proxy, intended to be upgraded with an upgradable beacon. /// - Automatically verified on Etherscan. /// /// @dev Minimal ERC1967 beacon proxy with immutable args: /// - Uses the identity precompile (0x4) to copy args during deployment. /// - Automatically verified on Etherscan. /// /// @dev ERC1967I beacon proxy: /// An variant of the minimal ERC1967 beacon proxy, with a special code path that activates /// if `calldatasize() == 1`. This code path skips the delegatecall and directly returns the /// `implementation` address. The returned implementation is guaranteed to be valid if the /// keccak256 of the proxy's code is equal to `ERC1967I_CODE_HASH`. /// /// @dev ERC1967I proxy with immutable args: /// An variant of the minimal ERC1967 beacon proxy, with a special code path that activates /// if `calldatasize() == 1`. This code path skips the delegatecall and directly returns the /// - Uses the identity precompile (0x4) to copy args during deployment. library LibClone { /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* CONSTANTS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev The keccak256 of deployed code for the clone proxy, /// with the implementation set to `address(0)`. bytes32 internal constant CLONE_CODE_HASH = 0x48db2cfdb2853fce0b464f1f93a1996469459df3ab6c812106074c4106a1eb1f; /// @dev The keccak256 of deployed code for the PUSH0 proxy, /// with the implementation set to `address(0)`. bytes32 internal constant PUSH0_CLONE_CODE_HASH = 0x67bc6bde1b84d66e267c718ba44cf3928a615d29885537955cb43d44b3e789dc; /// @dev The keccak256 of deployed code for the ERC-1167 CWIA proxy, /// with the implementation set to `address(0)`. bytes32 internal constant CWIA_CODE_HASH = 0x3cf92464268225a4513da40a34d967354684c32cd0edd67b5f668dfe3550e940; /// @dev The keccak256 of the deployed code for the ERC1967 proxy. bytes32 internal constant ERC1967_CODE_HASH = 0xaaa52c8cc8a0e3fd27ce756cc6b4e70c51423e9b597b11f32d3e49f8b1fc890d; /// @dev The keccak256 of the deployed code for the ERC1967I proxy. bytes32 internal constant ERC1967I_CODE_HASH = 0xce700223c0d4cea4583409accfc45adac4a093b3519998a9cbbe1504dadba6f7; /// @dev The keccak256 of the deployed code for the ERC1967 beacon proxy. bytes32 internal constant ERC1967_BEACON_PROXY_CODE_HASH = 0x14044459af17bc4f0f5aa2f658cb692add77d1302c29fe2aebab005eea9d1162; /// @dev The keccak256 of the deployed code for the ERC1967 beacon proxy. bytes32 internal constant ERC1967I_BEACON_PROXY_CODE_HASH = 0xf8c46d2793d5aa984eb827aeaba4b63aedcab80119212fce827309788735519a; /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* CUSTOM ERRORS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Unable to deploy the clone. error DeploymentFailed(); /// @dev The salt must start with either the zero address or `by`. error SaltDoesNotStartWith(); /// @dev The ETH transfer has failed. error ETHTransferFailed(); /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* MINIMAL PROXY OPERATIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Deploys a clone of `implementation`. function clone(address implementation) internal returns (address instance) { instance = clone(0, implementation); } /// @dev Deploys a clone of `implementation`. /// Deposits `value` ETH during deployment. function clone(uint256 value, address implementation) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { /** * --------------------------------------------------------------------------+ * CREATION (9 bytes) | * --------------------------------------------------------------------------| * Opcode | Mnemonic | Stack | Memory | * --------------------------------------------------------------------------| * 60 runSize | PUSH1 runSize | r | | * 3d | RETURNDATASIZE | 0 r | | * 81 | DUP2 | r 0 r | | * 60 offset | PUSH1 offset | o r 0 r | | * 3d | RETURNDATASIZE | 0 o r 0 r | | * 39 | CODECOPY | 0 r | [0..runSize): runtime code | * f3 | RETURN | | [0..runSize): runtime code | * --------------------------------------------------------------------------| * RUNTIME (44 bytes) | * --------------------------------------------------------------------------| * Opcode | Mnemonic | Stack | Memory | * --------------------------------------------------------------------------| * | * ::: keep some values in stack ::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | 0 | | * 3d | RETURNDATASIZE | 0 0 | | * 3d | RETURNDATASIZE | 0 0 0 | | * 3d | RETURNDATASIZE | 0 0 0 0 | | * | * ::: copy calldata to memory ::::::::::::::::::::::::::::::::::::::::::::: | * 36 | CALLDATASIZE | cds 0 0 0 0 | | * 3d | RETURNDATASIZE | 0 cds 0 0 0 0 | | * 3d | RETURNDATASIZE | 0 0 cds 0 0 0 0 | | * 37 | CALLDATACOPY | 0 0 0 0 | [0..cds): calldata | * | * ::: delegate call to the implementation contract :::::::::::::::::::::::: | * 36 | CALLDATASIZE | cds 0 0 0 0 | [0..cds): calldata | * 3d | RETURNDATASIZE | 0 cds 0 0 0 0 | [0..cds): calldata | * 73 addr | PUSH20 addr | addr 0 cds 0 0 0 0 | [0..cds): calldata | * 5a | GAS | gas addr 0 cds 0 0 0 0 | [0..cds): calldata | * f4 | DELEGATECALL | success 0 0 | [0..cds): calldata | * | * ::: copy return data to memory :::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | rds success 0 0 | [0..cds): calldata | * 3d | RETURNDATASIZE | rds rds success 0 0 | [0..cds): calldata | * 93 | SWAP4 | 0 rds success 0 rds | [0..cds): calldata | * 80 | DUP1 | 0 0 rds success 0 rds | [0..cds): calldata | * 3e | RETURNDATACOPY | success 0 rds | [0..rds): returndata | * | * 60 0x2a | PUSH1 0x2a | 0x2a success 0 rds | [0..rds): returndata | * 57 | JUMPI | 0 rds | [0..rds): returndata | * | * ::: revert :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | * fd | REVERT | | [0..rds): returndata | * | * ::: return :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | * 5b | JUMPDEST | 0 rds | [0..rds): returndata | * f3 | RETURN | | [0..rds): returndata | * --------------------------------------------------------------------------+ */ mstore(0x21, 0x5af43d3d93803e602a57fd5bf3) mstore(0x14, implementation) mstore(0x00, 0x602c3d8160093d39f33d3d3d3d363d3d37363d73) instance := create(value, 0x0c, 0x35) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } mstore(0x21, 0) // Restore the overwritten part of the free memory pointer. } } /// @dev Deploys a deterministic clone of `implementation` with `salt`. function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) { instance = cloneDeterministic(0, implementation, salt); } /// @dev Deploys a deterministic clone of `implementation` with `salt`. /// Deposits `value` ETH during deployment. function cloneDeterministic(uint256 value, address implementation, bytes32 salt) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { mstore(0x21, 0x5af43d3d93803e602a57fd5bf3) mstore(0x14, implementation) mstore(0x00, 0x602c3d8160093d39f33d3d3d3d363d3d37363d73) instance := create2(value, 0x0c, 0x35, salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } mstore(0x21, 0) // Restore the overwritten part of the free memory pointer. } } /// @dev Returns the initialization code of the clone of `implementation`. function initCode(address implementation) internal pure returns (bytes memory c) { /// @solidity memory-safe-assembly assembly { c := mload(0x40) mstore(add(c, 0x40), 0x5af43d3d93803e602a57fd5bf30000000000000000000000) mstore(add(c, 0x28), implementation) mstore(add(c, 0x14), 0x602c3d8160093d39f33d3d3d3d363d3d37363d73) mstore(c, 0x35) // Store the length. mstore(0x40, add(c, 0x60)) // Allocate memory. } } /// @dev Returns the initialization code hash of the clone of `implementation`. function initCodeHash(address implementation) internal pure returns (bytes32 hash) { /// @solidity memory-safe-assembly assembly { mstore(0x21, 0x5af43d3d93803e602a57fd5bf3) mstore(0x14, implementation) mstore(0x00, 0x602c3d8160093d39f33d3d3d3d363d3d37363d73) hash := keccak256(0x0c, 0x35) mstore(0x21, 0) // Restore the overwritten part of the free memory pointer. } } /// @dev Returns the address of the clone of `implementation`, with `salt` by `deployer`. /// Note: The returned result has dirty upper 96 bits. Please clean if used in assembly. function predictDeterministicAddress(address implementation, bytes32 salt, address deployer) internal pure returns (address predicted) { bytes32 hash = initCodeHash(implementation); predicted = predictDeterministicAddress(hash, salt, deployer); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* MINIMAL PROXY OPERATIONS (PUSH0 VARIANT) */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Deploys a PUSH0 clone of `implementation`. function clone_PUSH0(address implementation) internal returns (address instance) { instance = clone_PUSH0(0, implementation); } /// @dev Deploys a PUSH0 clone of `implementation`. /// Deposits `value` ETH during deployment. function clone_PUSH0(uint256 value, address implementation) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { /** * --------------------------------------------------------------------------+ * CREATION (9 bytes) | * --------------------------------------------------------------------------| * Opcode | Mnemonic | Stack | Memory | * --------------------------------------------------------------------------| * 60 runSize | PUSH1 runSize | r | | * 5f | PUSH0 | 0 r | | * 81 | DUP2 | r 0 r | | * 60 offset | PUSH1 offset | o r 0 r | | * 5f | PUSH0 | 0 o r 0 r | | * 39 | CODECOPY | 0 r | [0..runSize): runtime code | * f3 | RETURN | | [0..runSize): runtime code | * --------------------------------------------------------------------------| * RUNTIME (45 bytes) | * --------------------------------------------------------------------------| * Opcode | Mnemonic | Stack | Memory | * --------------------------------------------------------------------------| * | * ::: keep some values in stack ::::::::::::::::::::::::::::::::::::::::::: | * 5f | PUSH0 | 0 | | * 5f | PUSH0 | 0 0 | | * | * ::: copy calldata to memory ::::::::::::::::::::::::::::::::::::::::::::: | * 36 | CALLDATASIZE | cds 0 0 | | * 5f | PUSH0 | 0 cds 0 0 | | * 5f | PUSH0 | 0 0 cds 0 0 | | * 37 | CALLDATACOPY | 0 0 | [0..cds): calldata | * | * ::: delegate call to the implementation contract :::::::::::::::::::::::: | * 36 | CALLDATASIZE | cds 0 0 | [0..cds): calldata | * 5f | PUSH0 | 0 cds 0 0 | [0..cds): calldata | * 73 addr | PUSH20 addr | addr 0 cds 0 0 | [0..cds): calldata | * 5a | GAS | gas addr 0 cds 0 0 | [0..cds): calldata | * f4 | DELEGATECALL | success | [0..cds): calldata | * | * ::: copy return data to memory :::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | rds success | [0..cds): calldata | * 5f | PUSH0 | 0 rds success | [0..cds): calldata | * 5f | PUSH0 | 0 0 rds success | [0..cds): calldata | * 3e | RETURNDATACOPY | success | [0..rds): returndata | * | * 60 0x29 | PUSH1 0x29 | 0x29 success | [0..rds): returndata | * 57 | JUMPI | | [0..rds): returndata | * | * ::: revert :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | rds | [0..rds): returndata | * 5f | PUSH0 | 0 rds | [0..rds): returndata | * fd | REVERT | | [0..rds): returndata | * | * ::: return :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | * 5b | JUMPDEST | | [0..rds): returndata | * 3d | RETURNDATASIZE | rds | [0..rds): returndata | * 5f | PUSH0 | 0 rds | [0..rds): returndata | * f3 | RETURN | | [0..rds): returndata | * --------------------------------------------------------------------------+ */ mstore(0x24, 0x5af43d5f5f3e6029573d5ffd5b3d5ff3) // 16 mstore(0x14, implementation) // 20 mstore(0x00, 0x602d5f8160095f39f35f5f365f5f37365f73) // 9 + 9 instance := create(value, 0x0e, 0x36) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } mstore(0x24, 0) // Restore the overwritten part of the free memory pointer. } } /// @dev Deploys a deterministic PUSH0 clone of `implementation` with `salt`. function cloneDeterministic_PUSH0(address implementation, bytes32 salt) internal returns (address instance) { instance = cloneDeterministic_PUSH0(0, implementation, salt); } /// @dev Deploys a deterministic PUSH0 clone of `implementation` with `salt`. /// Deposits `value` ETH during deployment. function cloneDeterministic_PUSH0(uint256 value, address implementation, bytes32 salt) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { mstore(0x24, 0x5af43d5f5f3e6029573d5ffd5b3d5ff3) // 16 mstore(0x14, implementation) // 20 mstore(0x00, 0x602d5f8160095f39f35f5f365f5f37365f73) // 9 + 9 instance := create2(value, 0x0e, 0x36, salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } mstore(0x24, 0) // Restore the overwritten part of the free memory pointer. } } /// @dev Returns the initialization code of the PUSH0 clone of `implementation`. function initCode_PUSH0(address implementation) internal pure returns (bytes memory c) { /// @solidity memory-safe-assembly assembly { c := mload(0x40) mstore(add(c, 0x40), 0x5af43d5f5f3e6029573d5ffd5b3d5ff300000000000000000000) // 16 mstore(add(c, 0x26), implementation) // 20 mstore(add(c, 0x12), 0x602d5f8160095f39f35f5f365f5f37365f73) // 9 + 9 mstore(c, 0x36) // Store the length. mstore(0x40, add(c, 0x60)) // Allocate memory. } } /// @dev Returns the initialization code hash of the PUSH0 clone of `implementation`. function initCodeHash_PUSH0(address implementation) internal pure returns (bytes32 hash) { /// @solidity memory-safe-assembly assembly { mstore(0x24, 0x5af43d5f5f3e6029573d5ffd5b3d5ff3) // 16 mstore(0x14, implementation) // 20 mstore(0x00, 0x602d5f8160095f39f35f5f365f5f37365f73) // 9 + 9 hash := keccak256(0x0e, 0x36) mstore(0x24, 0) // Restore the overwritten part of the free memory pointer. } } /// @dev Returns the address of the PUSH0 clone of `implementation`, with `salt` by `deployer`. /// Note: The returned result has dirty upper 96 bits. Please clean if used in assembly. function predictDeterministicAddress_PUSH0( address implementation, bytes32 salt, address deployer ) internal pure returns (address predicted) { bytes32 hash = initCodeHash_PUSH0(implementation); predicted = predictDeterministicAddress(hash, salt, deployer); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* CLONES WITH IMMUTABLE ARGS OPERATIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Deploys a clone of `implementation` with immutable arguments encoded in `args`. function clone(address implementation, bytes memory args) internal returns (address instance) { instance = clone(0, implementation, args); } /// @dev Deploys a clone of `implementation` with immutable arguments encoded in `args`. /// Deposits `value` ETH during deployment. function clone(uint256 value, address implementation, bytes memory args) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { /** * ---------------------------------------------------------------------------+ * CREATION (10 bytes) | * ---------------------------------------------------------------------------| * Opcode | Mnemonic | Stack | Memory | * ---------------------------------------------------------------------------| * 61 runSize | PUSH2 runSize | r | | * 3d | RETURNDATASIZE | 0 r | | * 81 | DUP2 | r 0 r | | * 60 offset | PUSH1 offset | o r 0 r | | * 3d | RETURNDATASIZE | 0 o r 0 r | | * 39 | CODECOPY | 0 r | [0..runSize): runtime code | * f3 | RETURN | | [0..runSize): runtime code | * ---------------------------------------------------------------------------| * RUNTIME (45 bytes + extraLength) | * ---------------------------------------------------------------------------| * Opcode | Mnemonic | Stack | Memory | * ---------------------------------------------------------------------------| * | * ::: copy calldata to memory :::::::::::::::::::::::::::::::::::::::::::::: | * 36 | CALLDATASIZE | cds | | * 3d | RETURNDATASIZE | 0 cds | | * 3d | RETURNDATASIZE | 0 0 cds | | * 37 | CALLDATACOPY | | [0..cds): calldata | * | * ::: delegate call to the implementation contract ::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | 0 | [0..cds): calldata | * 3d | RETURNDATASIZE | 0 0 | [0..cds): calldata | * 3d | RETURNDATASIZE | 0 0 0 | [0..cds): calldata | * 36 | CALLDATASIZE | cds 0 0 0 | [0..cds): calldata | * 3d | RETURNDATASIZE | 0 cds 0 0 0 0 | [0..cds): calldata | * 73 addr | PUSH20 addr | addr 0 cds 0 0 0 0 | [0..cds): calldata | * 5a | GAS | gas addr 0 cds 0 0 0 0 | [0..cds): calldata | * f4 | DELEGATECALL | success 0 0 | [0..cds): calldata | * | * ::: copy return data to memory ::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | rds success 0 | [0..cds): calldata | * 82 | DUP3 | 0 rds success 0 | [0..cds): calldata | * 80 | DUP1 | 0 0 rds success 0 | [0..cds): calldata | * 3e | RETURNDATACOPY | success 0 | [0..rds): returndata | * 90 | SWAP1 | 0 success | [0..rds): returndata | * 3d | RETURNDATASIZE | rds 0 success | [0..rds): returndata | * 91 | SWAP2 | success 0 rds | [0..rds): returndata | * | * 60 0x2b | PUSH1 0x2b | 0x2b success 0 rds | [0..rds): returndata | * 57 | JUMPI | 0 rds | [0..rds): returndata | * | * ::: revert ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | * fd | REVERT | | [0..rds): returndata | * | * ::: return ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | * 5b | JUMPDEST | 0 rds | [0..rds): returndata | * f3 | RETURN | | [0..rds): returndata | * ---------------------------------------------------------------------------+ */ let m := mload(0x40) let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x43), n)) mstore(add(m, 0x23), 0x5af43d82803e903d91602b57fd5bf3) mstore(add(m, 0x14), implementation) mstore(m, add(0xfe61002d3d81600a3d39f3363d3d373d3d3d363d73, shl(136, n))) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x2d = 0xffd2`. instance := create(value, add(m, add(0x0b, lt(n, 0xffd3))), add(n, 0x37)) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } } } /// @dev Deploys a deterministic clone of `implementation` /// with immutable arguments encoded in `args` and `salt`. function cloneDeterministic(address implementation, bytes memory args, bytes32 salt) internal returns (address instance) { instance = cloneDeterministic(0, implementation, args, salt); } /// @dev Deploys a deterministic clone of `implementation` /// with immutable arguments encoded in `args` and `salt`. function cloneDeterministic( uint256 value, address implementation, bytes memory args, bytes32 salt ) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x43), n)) mstore(add(m, 0x23), 0x5af43d82803e903d91602b57fd5bf3) mstore(add(m, 0x14), implementation) mstore(m, add(0xfe61002d3d81600a3d39f3363d3d373d3d3d363d73, shl(136, n))) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x2d = 0xffd2`. instance := create2(value, add(m, add(0x0b, lt(n, 0xffd3))), add(n, 0x37), salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } } } /// @dev Deploys a deterministic clone of `implementation` /// with immutable arguments encoded in `args` and `salt`. /// This method does not revert if the clone has already been deployed. function createDeterministicClone(address implementation, bytes memory args, bytes32 salt) internal returns (bool alreadyDeployed, address instance) { return createDeterministicClone(0, implementation, args, salt); } /// @dev Deploys a deterministic clone of `implementation` /// with immutable arguments encoded in `args` and `salt`. /// This method does not revert if the clone has already been deployed. function createDeterministicClone( uint256 value, address implementation, bytes memory args, bytes32 salt ) internal returns (bool alreadyDeployed, address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x43), n)) mstore(add(m, 0x23), 0x5af43d82803e903d91602b57fd5bf3) mstore(add(m, 0x14), implementation) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x2d = 0xffd2`. // forgefmt: disable-next-item mstore(add(m, gt(n, 0xffd2)), add(0xfe61002d3d81600a3d39f3363d3d373d3d3d363d73, shl(136, n))) // Compute and store the bytecode hash. mstore8(0x00, 0xff) // Write the prefix. mstore(0x35, keccak256(add(m, 0x0c), add(n, 0x37))) mstore(0x01, shl(96, address())) mstore(0x15, salt) instance := keccak256(0x00, 0x55) for {} 1 {} { if iszero(extcodesize(instance)) { instance := create2(value, add(m, 0x0c), add(n, 0x37), salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } break } alreadyDeployed := 1 if iszero(value) { break } if iszero(call(gas(), instance, value, codesize(), 0x00, codesize(), 0x00)) { mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`. revert(0x1c, 0x04) } break } mstore(0x35, 0) // Restore the overwritten part of the free memory pointer. } } /// @dev Returns the initialization code hash of the clone of `implementation` /// using immutable arguments encoded in `args`. function initCode(address implementation, bytes memory args) internal pure returns (bytes memory c) { /// @solidity memory-safe-assembly assembly { c := mload(0x40) let n := mload(args) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x2d = 0xffd2`. returndatacopy(returndatasize(), returndatasize(), gt(n, 0xffd2)) for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { mstore(add(add(c, 0x57), i), mload(add(add(args, 0x20), i))) } mstore(add(c, 0x37), 0x5af43d82803e903d91602b57fd5bf3) mstore(add(c, 0x28), implementation) mstore(add(c, 0x14), add(0x61002d3d81600a3d39f3363d3d373d3d3d363d73, shl(136, n))) mstore(c, add(0x37, n)) // Store the length. mstore(add(c, add(n, 0x57)), 0) // Zeroize the slot after the bytes. mstore(0x40, add(c, add(n, 0x77))) // Allocate memory. } } /// @dev Returns the initialization code hash of the clone of `implementation` /// using immutable arguments encoded in `args`. function initCodeHash(address implementation, bytes memory args) internal pure returns (bytes32 hash) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x2d = 0xffd2`. returndatacopy(returndatasize(), returndatasize(), gt(n, 0xffd2)) for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { mstore(add(add(m, 0x43), i), mload(add(add(args, 0x20), i))) } mstore(add(m, 0x23), 0x5af43d82803e903d91602b57fd5bf3) mstore(add(m, 0x14), implementation) mstore(m, add(0x61002d3d81600a3d39f3363d3d373d3d3d363d73, shl(136, n))) hash := keccak256(add(m, 0x0c), add(n, 0x37)) } } /// @dev Returns the address of the clone of /// `implementation` using immutable arguments encoded in `args`, with `salt`, by `deployer`. /// Note: The returned result has dirty upper 96 bits. Please clean if used in assembly. function predictDeterministicAddress( address implementation, bytes memory data, bytes32 salt, address deployer ) internal pure returns (address predicted) { bytes32 hash = initCodeHash(implementation, data); predicted = predictDeterministicAddress(hash, salt, deployer); } /// @dev Equivalent to `argsOnClone(instance, 0, 2 ** 256 - 1)`. function argsOnClone(address instance) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) mstore(args, and(0xffffffffff, sub(extcodesize(instance), 0x2d))) // Store the length. extcodecopy(instance, add(args, 0x20), 0x2d, add(mload(args), 0x20)) mstore(0x40, add(mload(args), add(args, 0x40))) // Allocate memory. } } /// @dev Equivalent to `argsOnClone(instance, start, 2 ** 256 - 1)`. function argsOnClone(address instance, uint256 start) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) let n := and(0xffffffffff, sub(extcodesize(instance), 0x2d)) extcodecopy(instance, add(args, 0x20), add(start, 0x2d), add(n, 0x20)) mstore(args, mul(sub(n, start), lt(start, n))) // Store the length. mstore(0x40, add(args, add(0x40, mload(args)))) // Allocate memory. } } /// @dev Returns a slice of the immutable arguments on `instance` from `start` to `end`. /// `start` and `end` will be clamped to the range `[0, args.length]`. /// The `instance` MUST be deployed via the clone with immutable args functions. /// Otherwise, the behavior is undefined. /// Out-of-gas reverts if `instance` does not have any code. function argsOnClone(address instance, uint256 start, uint256 end) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) if iszero(lt(end, 0xffff)) { end := 0xffff } let d := mul(sub(end, start), lt(start, end)) extcodecopy(instance, args, add(start, 0x0d), add(d, 0x20)) if iszero(and(0xff, mload(add(args, d)))) { let n := sub(extcodesize(instance), 0x2d) returndatacopy(returndatasize(), returndatasize(), shr(40, n)) d := mul(gt(n, start), sub(d, mul(gt(end, n), sub(end, n)))) } mstore(args, d) // Store the length. mstore(add(add(args, 0x20), d), 0) // Zeroize the slot after the bytes. mstore(0x40, add(add(args, 0x40), d)) // Allocate memory. } } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* MINIMAL ERC1967 PROXY OPERATIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ // Note: The ERC1967 proxy here is intended to be upgraded with UUPS. // This is NOT the same as ERC1967Factory's transparent proxy, which includes admin logic. /// @dev Deploys a minimal ERC1967 proxy with `implementation`. function deployERC1967(address implementation) internal returns (address instance) { instance = deployERC1967(0, implementation); } /// @dev Deploys a minimal ERC1967 proxy with `implementation`. /// Deposits `value` ETH during deployment. function deployERC1967(uint256 value, address implementation) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { /** * ---------------------------------------------------------------------------------+ * CREATION (34 bytes) | * ---------------------------------------------------------------------------------| * Opcode | Mnemonic | Stack | Memory | * ---------------------------------------------------------------------------------| * 60 runSize | PUSH1 runSize | r | | * 3d | RETURNDATASIZE | 0 r | | * 81 | DUP2 | r 0 r | | * 60 offset | PUSH1 offset | o r 0 r | | * 3d | RETURNDATASIZE | 0 o r 0 r | | * 39 | CODECOPY | 0 r | [0..runSize): runtime code | * 73 impl | PUSH20 impl | impl 0 r | [0..runSize): runtime code | * 60 slotPos | PUSH1 slotPos | slotPos impl 0 r | [0..runSize): runtime code | * 51 | MLOAD | slot impl 0 r | [0..runSize): runtime code | * 55 | SSTORE | 0 r | [0..runSize): runtime code | * f3 | RETURN | | [0..runSize): runtime code | * ---------------------------------------------------------------------------------| * RUNTIME (61 bytes) | * ---------------------------------------------------------------------------------| * Opcode | Mnemonic | Stack | Memory | * ---------------------------------------------------------------------------------| * | * ::: copy calldata to memory :::::::::::::::::::::::::::::::::::::::::::::::::::: | * 36 | CALLDATASIZE | cds | | * 3d | RETURNDATASIZE | 0 cds | | * 3d | RETURNDATASIZE | 0 0 cds | | * 37 | CALLDATACOPY | | [0..calldatasize): calldata | * | * ::: delegatecall to implementation ::::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | 0 | | * 3d | RETURNDATASIZE | 0 0 | | * 36 | CALLDATASIZE | cds 0 0 | [0..calldatasize): calldata | * 3d | RETURNDATASIZE | 0 cds 0 0 | [0..calldatasize): calldata | * 7f slot | PUSH32 slot | s 0 cds 0 0 | [0..calldatasize): calldata | * 54 | SLOAD | i 0 cds 0 0 | [0..calldatasize): calldata | * 5a | GAS | g i 0 cds 0 0 | [0..calldatasize): calldata | * f4 | DELEGATECALL | succ | [0..calldatasize): calldata | * | * ::: copy returndata to memory :::::::::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | rds succ | [0..calldatasize): calldata | * 60 0x00 | PUSH1 0x00 | 0 rds succ | [0..calldatasize): calldata | * 80 | DUP1 | 0 0 rds succ | [0..calldatasize): calldata | * 3e | RETURNDATACOPY | succ | [0..returndatasize): returndata | * | * ::: branch on delegatecall status :::::::::::::::::::::::::::::::::::::::::::::: | * 60 0x38 | PUSH1 0x38 | dest succ | [0..returndatasize): returndata | * 57 | JUMPI | | [0..returndatasize): returndata | * | * ::: delegatecall failed, revert :::::::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | rds | [0..returndatasize): returndata | * 60 0x00 | PUSH1 0x00 | 0 rds | [0..returndatasize): returndata | * fd | REVERT | | [0..returndatasize): returndata | * | * ::: delegatecall succeeded, return ::::::::::::::::::::::::::::::::::::::::::::: | * 5b | JUMPDEST | | [0..returndatasize): returndata | * 3d | RETURNDATASIZE | rds | [0..returndatasize): returndata | * 60 0x00 | PUSH1 0x00 | 0 rds | [0..returndatasize): returndata | * f3 | RETURN | | [0..returndatasize): returndata | * ---------------------------------------------------------------------------------+ */ let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0xcc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3) mstore(0x40, 0x5155f3363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076) mstore(0x20, 0x6009) mstore(0x1e, implementation) mstore(0x0a, 0x603d3d8160223d3973) instance := create(value, 0x21, 0x5f) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Deploys a deterministic minimal ERC1967 proxy with `implementation` and `salt`. function deployDeterministicERC1967(address implementation, bytes32 salt) internal returns (address instance) { instance = deployDeterministicERC1967(0, implementation, salt); } /// @dev Deploys a deterministic minimal ERC1967 proxy with `implementation` and `salt`. /// Deposits `value` ETH during deployment. function deployDeterministicERC1967(uint256 value, address implementation, bytes32 salt) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0xcc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3) mstore(0x40, 0x5155f3363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076) mstore(0x20, 0x6009) mstore(0x1e, implementation) mstore(0x0a, 0x603d3d8160223d3973) instance := create2(value, 0x21, 0x5f, salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Creates a deterministic minimal ERC1967 proxy with `implementation` and `salt`. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967(address implementation, bytes32 salt) internal returns (bool alreadyDeployed, address instance) { return createDeterministicERC1967(0, implementation, salt); } /// @dev Creates a deterministic minimal ERC1967 proxy with `implementation` and `salt`. /// Deposits `value` ETH during deployment. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967(uint256 value, address implementation, bytes32 salt) internal returns (bool alreadyDeployed, address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0xcc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3) mstore(0x40, 0x5155f3363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076) mstore(0x20, 0x6009) mstore(0x1e, implementation) mstore(0x0a, 0x603d3d8160223d3973) // Compute and store the bytecode hash. mstore(add(m, 0x35), keccak256(0x21, 0x5f)) mstore(m, shl(88, address())) mstore8(m, 0xff) // Write the prefix. mstore(add(m, 0x15), salt) instance := keccak256(m, 0x55) for {} 1 {} { if iszero(extcodesize(instance)) { instance := create2(value, 0x21, 0x5f, salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } break } alreadyDeployed := 1 if iszero(value) { break } if iszero(call(gas(), instance, value, codesize(), 0x00, codesize(), 0x00)) { mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`. revert(0x1c, 0x04) } break } mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Returns the initialization code of the minimal ERC1967 proxy of `implementation`. function initCodeERC1967(address implementation) internal pure returns (bytes memory c) { /// @solidity memory-safe-assembly assembly { c := mload(0x40) mstore(add(c, 0x60), 0x3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f300) mstore(add(c, 0x40), 0x55f3363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076cc) mstore(add(c, 0x20), or(shl(24, implementation), 0x600951)) mstore(add(c, 0x09), 0x603d3d8160223d3973) mstore(c, 0x5f) // Store the length. mstore(0x40, add(c, 0x80)) // Allocate memory. } } /// @dev Returns the initialization code hash of the minimal ERC1967 proxy of `implementation`. function initCodeHashERC1967(address implementation) internal pure returns (bytes32 hash) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0xcc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3) mstore(0x40, 0x5155f3363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076) mstore(0x20, 0x6009) mstore(0x1e, implementation) mstore(0x0a, 0x603d3d8160223d3973) hash := keccak256(0x21, 0x5f) mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Returns the address of the ERC1967 proxy of `implementation`, with `salt` by `deployer`. /// Note: The returned result has dirty upper 96 bits. Please clean if used in assembly. function predictDeterministicAddressERC1967( address implementation, bytes32 salt, address deployer ) internal pure returns (address predicted) { bytes32 hash = initCodeHashERC1967(implementation); predicted = predictDeterministicAddress(hash, salt, deployer); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* MINIMAL ERC1967 PROXY WITH IMMUTABLE ARGS OPERATIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Deploys a minimal ERC1967 proxy with `implementation` and `args`. function deployERC1967(address implementation, bytes memory args) internal returns (address instance) { instance = deployERC1967(0, implementation, args); } /// @dev Deploys a minimal ERC1967 proxy with `implementation` and `args`. /// Deposits `value` ETH during deployment. function deployERC1967(uint256 value, address implementation, bytes memory args) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x60), n)) mstore(add(m, 0x40), 0xcc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3) mstore(add(m, 0x20), 0x5155f3363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076) mstore(0x16, 0x6009) mstore(0x14, implementation) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x3d = 0xffc2`. mstore(gt(n, 0xffc2), add(0xfe61003d3d8160233d3973, shl(56, n))) mstore(m, mload(0x16)) instance := create(value, m, add(n, 0x60)) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } } } /// @dev Deploys a deterministic minimal ERC1967 proxy with `implementation`, `args` and `salt`. function deployDeterministicERC1967(address implementation, bytes memory args, bytes32 salt) internal returns (address instance) { instance = deployDeterministicERC1967(0, implementation, args, salt); } /// @dev Deploys a deterministic minimal ERC1967 proxy with `implementation`, `args` and `salt`. /// Deposits `value` ETH during deployment. function deployDeterministicERC1967( uint256 value, address implementation, bytes memory args, bytes32 salt ) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x60), n)) mstore(add(m, 0x40), 0xcc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3) mstore(add(m, 0x20), 0x5155f3363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076) mstore(0x16, 0x6009) mstore(0x14, implementation) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x3d = 0xffc2`. mstore(gt(n, 0xffc2), add(0xfe61003d3d8160233d3973, shl(56, n))) mstore(m, mload(0x16)) instance := create2(value, m, add(n, 0x60), salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } } } /// @dev Creates a deterministic minimal ERC1967 proxy with `implementation`, `args` and `salt`. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967(address implementation, bytes memory args, bytes32 salt) internal returns (bool alreadyDeployed, address instance) { return createDeterministicERC1967(0, implementation, args, salt); } /// @dev Creates a deterministic minimal ERC1967 proxy with `implementation`, `args` and `salt`. /// Deposits `value` ETH during deployment. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967( uint256 value, address implementation, bytes memory args, bytes32 salt ) internal returns (bool alreadyDeployed, address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x60), n)) mstore(add(m, 0x40), 0xcc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3) mstore(add(m, 0x20), 0x5155f3363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076) mstore(0x16, 0x6009) mstore(0x14, implementation) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x3d = 0xffc2`. mstore(gt(n, 0xffc2), add(0xfe61003d3d8160233d3973, shl(56, n))) mstore(m, mload(0x16)) // Compute and store the bytecode hash. mstore8(0x00, 0xff) // Write the prefix. mstore(0x35, keccak256(m, add(n, 0x60))) mstore(0x01, shl(96, address())) mstore(0x15, salt) instance := keccak256(0x00, 0x55) for {} 1 {} { if iszero(extcodesize(instance)) { instance := create2(value, m, add(n, 0x60), salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } break } alreadyDeployed := 1 if iszero(value) { break } if iszero(call(gas(), instance, value, codesize(), 0x00, codesize(), 0x00)) { mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`. revert(0x1c, 0x04) } break } mstore(0x35, 0) // Restore the overwritten part of the free memory pointer. } } /// @dev Returns the initialization code of the minimal ERC1967 proxy of `implementation` and `args`. function initCodeERC1967(address implementation, bytes memory args) internal pure returns (bytes memory c) { /// @solidity memory-safe-assembly assembly { c := mload(0x40) let n := mload(args) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x3d = 0xffc2`. returndatacopy(returndatasize(), returndatasize(), gt(n, 0xffc2)) for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { mstore(add(add(c, 0x80), i), mload(add(add(args, 0x20), i))) } mstore(add(c, 0x60), 0xcc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3) mstore(add(c, 0x40), 0x5155f3363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076) mstore(add(c, 0x20), 0x6009) mstore(add(c, 0x1e), implementation) mstore(add(c, 0x0a), add(0x61003d3d8160233d3973, shl(56, n))) mstore(c, add(n, 0x60)) // Store the length. mstore(add(c, add(n, 0x80)), 0) // Zeroize the slot after the bytes. mstore(0x40, add(c, add(n, 0xa0))) // Allocate memory. } } /// @dev Returns the initialization code hash of the minimal ERC1967 proxy of `implementation` and `args`. function initCodeHashERC1967(address implementation, bytes memory args) internal pure returns (bytes32 hash) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x3d = 0xffc2`. returndatacopy(returndatasize(), returndatasize(), gt(n, 0xffc2)) for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { mstore(add(add(m, 0x60), i), mload(add(add(args, 0x20), i))) } mstore(add(m, 0x40), 0xcc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3) mstore(add(m, 0x20), 0x5155f3363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076) mstore(0x16, 0x6009) mstore(0x14, implementation) mstore(0x00, add(0x61003d3d8160233d3973, shl(56, n))) mstore(m, mload(0x16)) hash := keccak256(m, add(n, 0x60)) } } /// @dev Returns the address of the ERC1967 proxy of `implementation`, `args`, with `salt` by `deployer`. /// Note: The returned result has dirty upper 96 bits. Please clean if used in assembly. function predictDeterministicAddressERC1967( address implementation, bytes memory args, bytes32 salt, address deployer ) internal pure returns (address predicted) { bytes32 hash = initCodeHashERC1967(implementation, args); predicted = predictDeterministicAddress(hash, salt, deployer); } /// @dev Equivalent to `argsOnERC1967(instance, start, 2 ** 256 - 1)`. function argsOnERC1967(address instance) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) mstore(args, and(0xffffffffff, sub(extcodesize(instance), 0x3d))) // Store the length. extcodecopy(instance, add(args, 0x20), 0x3d, add(mload(args), 0x20)) mstore(0x40, add(mload(args), add(args, 0x40))) // Allocate memory. } } /// @dev Equivalent to `argsOnERC1967(instance, start, 2 ** 256 - 1)`. function argsOnERC1967(address instance, uint256 start) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) let n := and(0xffffffffff, sub(extcodesize(instance), 0x3d)) extcodecopy(instance, add(args, 0x20), add(start, 0x3d), add(n, 0x20)) mstore(args, mul(sub(n, start), lt(start, n))) // Store the length. mstore(0x40, add(args, add(0x40, mload(args)))) // Allocate memory. } } /// @dev Returns a slice of the immutable arguments on `instance` from `start` to `end`. /// `start` and `end` will be clamped to the range `[0, args.length]`. /// The `instance` MUST be deployed via the ERC1967 with immutable args functions. /// Otherwise, the behavior is undefined. /// Out-of-gas reverts if `instance` does not have any code. function argsOnERC1967(address instance, uint256 start, uint256 end) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) if iszero(lt(end, 0xffff)) { end := 0xffff } let d := mul(sub(end, start), lt(start, end)) extcodecopy(instance, args, add(start, 0x1d), add(d, 0x20)) if iszero(and(0xff, mload(add(args, d)))) { let n := sub(extcodesize(instance), 0x3d) returndatacopy(returndatasize(), returndatasize(), shr(40, n)) d := mul(gt(n, start), sub(d, mul(gt(end, n), sub(end, n)))) } mstore(args, d) // Store the length. mstore(add(add(args, 0x20), d), 0) // Zeroize the slot after the bytes. mstore(0x40, add(add(args, 0x40), d)) // Allocate memory. } } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* ERC1967I PROXY OPERATIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ // Note: This proxy has a special code path that activates if `calldatasize() == 1`. // This code path skips the delegatecall and directly returns the `implementation` address. // The returned implementation is guaranteed to be valid if the keccak256 of the // proxy's code is equal to `ERC1967I_CODE_HASH`. /// @dev Deploys a ERC1967I proxy with `implementation`. function deployERC1967I(address implementation) internal returns (address instance) { instance = deployERC1967I(0, implementation); } /// @dev Deploys a ERC1967I proxy with `implementation`. /// Deposits `value` ETH during deployment. function deployERC1967I(uint256 value, address implementation) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { /** * ---------------------------------------------------------------------------------+ * CREATION (34 bytes) | * ---------------------------------------------------------------------------------| * Opcode | Mnemonic | Stack | Memory | * ---------------------------------------------------------------------------------| * 60 runSize | PUSH1 runSize | r | | * 3d | RETURNDATASIZE | 0 r | | * 81 | DUP2 | r 0 r | | * 60 offset | PUSH1 offset | o r 0 r | | * 3d | RETURNDATASIZE | 0 o r 0 r | | * 39 | CODECOPY | 0 r | [0..runSize): runtime code | * 73 impl | PUSH20 impl | impl 0 r | [0..runSize): runtime code | * 60 slotPos | PUSH1 slotPos | slotPos impl 0 r | [0..runSize): runtime code | * 51 | MLOAD | slot impl 0 r | [0..runSize): runtime code | * 55 | SSTORE | 0 r | [0..runSize): runtime code | * f3 | RETURN | | [0..runSize): runtime code | * ---------------------------------------------------------------------------------| * RUNTIME (82 bytes) | * ---------------------------------------------------------------------------------| * Opcode | Mnemonic | Stack | Memory | * ---------------------------------------------------------------------------------| * | * ::: check calldatasize ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | * 36 | CALLDATASIZE | cds | | * 58 | PC | 1 cds | | * 14 | EQ | eqs | | * 60 0x43 | PUSH1 0x43 | dest eqs | | * 57 | JUMPI | | | * | * ::: copy calldata to memory :::::::::::::::::::::::::::::::::::::::::::::::::::: | * 36 | CALLDATASIZE | cds | | * 3d | RETURNDATASIZE | 0 cds | | * 3d | RETURNDATASIZE | 0 0 cds | | * 37 | CALLDATACOPY | | [0..calldatasize): calldata | * | * ::: delegatecall to implementation ::::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | 0 | | * 3d | RETURNDATASIZE | 0 0 | | * 36 | CALLDATASIZE | cds 0 0 | [0..calldatasize): calldata | * 3d | RETURNDATASIZE | 0 cds 0 0 | [0..calldatasize): calldata | * 7f slot | PUSH32 slot | s 0 cds 0 0 | [0..calldatasize): calldata | * 54 | SLOAD | i 0 cds 0 0 | [0..calldatasize): calldata | * 5a | GAS | g i 0 cds 0 0 | [0..calldatasize): calldata | * f4 | DELEGATECALL | succ | [0..calldatasize): calldata | * | * ::: copy returndata to memory :::::::::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | rds succ | [0..calldatasize): calldata | * 60 0x00 | PUSH1 0x00 | 0 rds succ | [0..calldatasize): calldata | * 80 | DUP1 | 0 0 rds succ | [0..calldatasize): calldata | * 3e | RETURNDATACOPY | succ | [0..returndatasize): returndata | * | * ::: branch on delegatecall status :::::::::::::::::::::::::::::::::::::::::::::: | * 60 0x3E | PUSH1 0x3E | dest succ | [0..returndatasize): returndata | * 57 | JUMPI | | [0..returndatasize): returndata | * | * ::: delegatecall failed, revert :::::::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | rds | [0..returndatasize): returndata | * 60 0x00 | PUSH1 0x00 | 0 rds | [0..returndatasize): returndata | * fd | REVERT | | [0..returndatasize): returndata | * | * ::: delegatecall succeeded, return ::::::::::::::::::::::::::::::::::::::::::::: | * 5b | JUMPDEST | | [0..returndatasize): returndata | * 3d | RETURNDATASIZE | rds | [0..returndatasize): returndata | * 60 0x00 | PUSH1 0x00 | 0 rds | [0..returndatasize): returndata | * f3 | RETURN | | [0..returndatasize): returndata | * | * ::: implementation , return :::::::::::::::::::::::::::::::::::::::::::::::::::: | * 5b | JUMPDEST | | | * 60 0x20 | PUSH1 0x20 | 32 | | * 60 0x0F | PUSH1 0x0F | o 32 | | * 3d | RETURNDATASIZE | 0 o 32 | | * 39 | CODECOPY | | [0..32): implementation slot | * 3d | RETURNDATASIZE | 0 | [0..32): implementation slot | * 51 | MLOAD | slot | [0..32): implementation slot | * 54 | SLOAD | impl | [0..32): implementation slot | * 3d | RETURNDATASIZE | 0 impl | [0..32): implementation slot | * 52 | MSTORE | | [0..32): implementation address | * 59 | MSIZE | 32 | [0..32): implementation address | * 3d | RETURNDATASIZE | 0 32 | [0..32): implementation address | * f3 | RETURN | | [0..32): implementation address | * ---------------------------------------------------------------------------------+ */ let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0x3d6000803e603e573d6000fd5b3d6000f35b6020600f3d393d51543d52593df3) mstore(0x40, 0xa13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af4) mstore(0x20, 0x600f5155f3365814604357363d3d373d3d363d7f360894) mstore(0x09, or(shl(160, 0x60523d8160223d3973), shr(96, shl(96, implementation)))) instance := create(value, 0x0c, 0x74) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Deploys a deterministic ERC1967I proxy with `implementation` and `salt`. function deployDeterministicERC1967I(address implementation, bytes32 salt) internal returns (address instance) { instance = deployDeterministicERC1967I(0, implementation, salt); } /// @dev Deploys a deterministic ERC1967I proxy with `implementation` and `salt`. /// Deposits `value` ETH during deployment. function deployDeterministicERC1967I(uint256 value, address implementation, bytes32 salt) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0x3d6000803e603e573d6000fd5b3d6000f35b6020600f3d393d51543d52593df3) mstore(0x40, 0xa13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af4) mstore(0x20, 0x600f5155f3365814604357363d3d373d3d363d7f360894) mstore(0x09, or(shl(160, 0x60523d8160223d3973), shr(96, shl(96, implementation)))) instance := create2(value, 0x0c, 0x74, salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Creates a deterministic ERC1967I proxy with `implementation` and `salt`. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967I(address implementation, bytes32 salt) internal returns (bool alreadyDeployed, address instance) { return createDeterministicERC1967I(0, implementation, salt); } /// @dev Creates a deterministic ERC1967I proxy with `implementation` and `salt`. /// Deposits `value` ETH during deployment. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967I(uint256 value, address implementation, bytes32 salt) internal returns (bool alreadyDeployed, address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0x3d6000803e603e573d6000fd5b3d6000f35b6020600f3d393d51543d52593df3) mstore(0x40, 0xa13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af4) mstore(0x20, 0x600f5155f3365814604357363d3d373d3d363d7f360894) mstore(0x09, or(shl(160, 0x60523d8160223d3973), shr(96, shl(96, implementation)))) // Compute and store the bytecode hash. mstore(add(m, 0x35), keccak256(0x0c, 0x74)) mstore(m, shl(88, address())) mstore8(m, 0xff) // Write the prefix. mstore(add(m, 0x15), salt) instance := keccak256(m, 0x55) for {} 1 {} { if iszero(extcodesize(instance)) { instance := create2(value, 0x0c, 0x74, salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } break } alreadyDeployed := 1 if iszero(value) { break } if iszero(call(gas(), instance, value, codesize(), 0x00, codesize(), 0x00)) { mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`. revert(0x1c, 0x04) } break } mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Returns the initialization code of the ERC1967I proxy of `implementation`. function initCodeERC1967I(address implementation) internal pure returns (bytes memory c) { /// @solidity memory-safe-assembly assembly { c := mload(0x40) mstore(add(c, 0x74), 0x3d6000803e603e573d6000fd5b3d6000f35b6020600f3d393d51543d52593df3) mstore(add(c, 0x54), 0xa13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af4) mstore(add(c, 0x34), 0x600f5155f3365814604357363d3d373d3d363d7f360894) mstore(add(c, 0x1d), implementation) mstore(add(c, 0x09), 0x60523d8160223d3973) mstore(add(c, 0x94), 0) mstore(c, 0x74) // Store the length. mstore(0x40, add(c, 0xa0)) // Allocate memory. } } /// @dev Returns the initialization code hash of the ERC1967I proxy of `implementation`. function initCodeHashERC1967I(address implementation) internal pure returns (bytes32 hash) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0x3d6000803e603e573d6000fd5b3d6000f35b6020600f3d393d51543d52593df3) mstore(0x40, 0xa13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af4) mstore(0x20, 0x600f5155f3365814604357363d3d373d3d363d7f360894) mstore(0x09, or(shl(160, 0x60523d8160223d3973), shr(96, shl(96, implementation)))) hash := keccak256(0x0c, 0x74) mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Returns the address of the ERC1967I proxy of `implementation`, with `salt` by `deployer`. /// Note: The returned result has dirty upper 96 bits. Please clean if used in assembly. function predictDeterministicAddressERC1967I( address implementation, bytes32 salt, address deployer ) internal pure returns (address predicted) { bytes32 hash = initCodeHashERC1967I(implementation); predicted = predictDeterministicAddress(hash, salt, deployer); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* ERC1967I PROXY WITH IMMUTABLE ARGS OPERATIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Deploys a minimal ERC1967I proxy with `implementation` and `args`. function deployERC1967I(address implementation, bytes memory args) internal returns (address) { return deployERC1967I(0, implementation, args); } /// @dev Deploys a minimal ERC1967I proxy with `implementation` and `args`. /// Deposits `value` ETH during deployment. function deployERC1967I(uint256 value, address implementation, bytes memory args) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x8b), n)) mstore(add(m, 0x6b), 0x3d6000803e603e573d6000fd5b3d6000f35b6020600f3d393d51543d52593df3) mstore(add(m, 0x4b), 0xa13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af4) mstore(add(m, 0x2b), 0x600f5155f3365814604357363d3d373d3d363d7f360894) mstore(add(m, 0x14), implementation) mstore(m, add(0xfe6100523d8160233d3973, shl(56, n))) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x52 = 0xffad`. instance := create(value, add(m, add(0x15, lt(n, 0xffae))), add(0x75, n)) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } } } /// @dev Deploys a deterministic ERC1967I proxy with `implementation`, `args`, and `salt`. function deployDeterministicERC1967I(address implementation, bytes memory args, bytes32 salt) internal returns (address instance) { instance = deployDeterministicERC1967I(0, implementation, args, salt); } /// @dev Deploys a deterministic ERC1967I proxy with `implementation`,`args`, and `salt`. /// Deposits `value` ETH during deployment. function deployDeterministicERC1967I( uint256 value, address implementation, bytes memory args, bytes32 salt ) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x8b), n)) mstore(add(m, 0x6b), 0x3d6000803e603e573d6000fd5b3d6000f35b6020600f3d393d51543d52593df3) mstore(add(m, 0x4b), 0xa13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af4) mstore(add(m, 0x2b), 0x600f5155f3365814604357363d3d373d3d363d7f360894) mstore(add(m, 0x14), implementation) mstore(m, add(0xfe6100523d8160233d3973, shl(56, n))) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x52 = 0xffad`. instance := create2(value, add(m, add(0x15, lt(n, 0xffae))), add(0x75, n), salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } } } /// @dev Creates a deterministic ERC1967I proxy with `implementation`, `args` and `salt`. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967I(address implementation, bytes memory args, bytes32 salt) internal returns (bool alreadyDeployed, address instance) { return createDeterministicERC1967I(0, implementation, args, salt); } /// @dev Creates a deterministic ERC1967I proxy with `implementation`,`args` and `salt`. /// Deposits `value` ETH during deployment. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967I( uint256 value, address implementation, bytes memory args, bytes32 salt ) internal returns (bool alreadyDeployed, address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x75), n)) mstore(add(m, 0x55), 0x3d6000803e603e573d6000fd5b3d6000f35b6020600f3d393d51543d52593df3) mstore(add(m, 0x35), 0xa13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af4) mstore(add(m, 0x15), 0x5155f3365814604357363d3d373d3d363d7f360894) mstore(0x16, 0x600f) mstore(0x14, implementation) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x52 = 0xffad`. mstore(gt(n, 0xffad), add(0xfe6100523d8160233d3973, shl(56, n))) mstore(m, mload(0x16)) // Compute and store the bytecode hash. mstore8(0x00, 0xff) // Write the prefix. mstore(0x35, keccak256(m, add(n, 0x75))) mstore(0x01, shl(96, address())) mstore(0x15, salt) instance := keccak256(0x00, 0x55) for {} 1 {} { if iszero(extcodesize(instance)) { instance := create2(value, m, add(0x75, n), salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } break } alreadyDeployed := 1 if iszero(value) { break } if iszero(call(gas(), instance, value, codesize(), 0x00, codesize(), 0x00)) { mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`. revert(0x1c, 0x04) } break } mstore(0x35, 0) // Restore the overwritten part of the free memory pointer. } } /// @dev Returns the initialization code of the ERC1967I proxy of `implementation`and `args`. function initCodeERC1967I(address implementation, bytes memory args) internal pure returns (bytes memory c) { /// @solidity memory-safe-assembly assembly { c := mload(0x40) let n := mload(args) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x52 = 0xffad`. returndatacopy(returndatasize(), returndatasize(), gt(n, 0xffad)) for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { mstore(add(add(c, 0x95), i), mload(add(add(args, 0x20), i))) } mstore(add(c, 0x75), 0x3d6000803e603e573d6000fd5b3d6000f35b6020600f3d393d51543d52593df3) mstore(add(c, 0x55), 0xa13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af4) mstore(add(c, 0x35), 0x600f5155f3365814604357363d3d373d3d363d7f360894) mstore(add(c, 0x1e), implementation) mstore(add(c, 0x0a), add(0x6100523d8160233d3973, shl(56, n))) mstore(add(c, add(n, 0x95)), 0) mstore(c, add(0x75, n)) // Store the length. mstore(0x40, add(c, add(n, 0xb5))) // Allocate memory. } } /// @dev Returns the initialization code hash of the ERC1967I proxy of `implementation` and `args. function initCodeHashERC1967I(address implementation, bytes memory args) internal pure returns (bytes32 hash) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. let n := mload(args) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x52 = 0xffad`. returndatacopy(returndatasize(), returndatasize(), gt(n, 0xffad)) for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { mstore(add(add(m, 0x75), i), mload(add(add(args, 0x20), i))) } mstore(add(m, 0x55), 0x3d6000803e603e573d6000fd5b3d6000f35b6020600f3d393d51543d52593df3) mstore(add(m, 0x35), 0xa13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af4) mstore(add(m, 0x15), 0x5155f3365814604357363d3d373d3d363d7f360894) mstore(0x16, 0x600f) mstore(0x14, implementation) mstore(0x00, add(0x6100523d8160233d3973, shl(56, n))) mstore(m, mload(0x16)) hash := keccak256(m, add(0x75, n)) } } /// @dev Returns the address of the ERC1967I proxy of `implementation`, 'args` with `salt` by `deployer`. /// Note: The returned result has dirty upper 96 bits. Please clean if used in assembly. function predictDeterministicAddressERC1967I( address implementation, bytes memory args, bytes32 salt, address deployer ) internal pure returns (address predicted) { bytes32 hash = initCodeHashERC1967I(implementation, args); predicted = predictDeterministicAddress(hash, salt, deployer); } /// @dev Equivalent to `argsOnERC1967I(instance, start, 2 ** 256 - 1)`. function argsOnERC1967I(address instance) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) mstore(args, and(0xffffffffff, sub(extcodesize(instance), 0x52))) // Store the length. extcodecopy(instance, add(args, 0x20), 0x52, add(mload(args), 0x20)) mstore(0x40, add(mload(args), add(args, 0x40))) // Allocate memory. } } /// @dev Equivalent to `argsOnERC1967I(instance, start, 2 ** 256 - 1)`. function argsOnERC1967I(address instance, uint256 start) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) let n := and(0xffffffffff, sub(extcodesize(instance), 0x52)) extcodecopy(instance, add(args, 0x20), add(start, 0x52), add(n, 0x20)) mstore(args, mul(sub(n, start), lt(start, n))) // Store the length. mstore(0x40, add(mload(args), add(args, 0x40))) // Allocate memory. } } /// @dev Returns a slice of the immutable arguments on `instance` from `start` to `end`. /// `start` and `end` will be clamped to the range `[0, args.length]`. /// The `instance` MUST be deployed via the ERC1967 with immutable args functions. /// Otherwise, the behavior is undefined. /// Out-of-gas reverts if `instance` does not have any code. function argsOnERC1967I(address instance, uint256 start, uint256 end) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) if iszero(lt(end, 0xffff)) { end := 0xffff } let d := mul(sub(end, start), lt(start, end)) extcodecopy(instance, args, add(start, 0x32), add(d, 0x20)) if iszero(and(0xff, mload(add(args, d)))) { let n := sub(extcodesize(instance), 0x52) returndatacopy(returndatasize(), returndatasize(), shr(40, n)) d := mul(gt(n, start), sub(d, mul(gt(end, n), sub(end, n)))) } mstore(args, d) // Store the length. mstore(add(add(args, 0x20), d), 0) // Zeroize the slot after the bytes. mstore(0x40, add(add(args, 0x40), d)) // Allocate memory. } } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* ERC1967 BOOTSTRAP OPERATIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ // A bootstrap is a minimal UUPS implementation that allows an ERC1967 proxy // pointing to it to be upgraded. The ERC1967 proxy can then be deployed to a // deterministic address independent of the implementation: // ``` // address bootstrap = LibClone.erc1967Bootstrap(); // address instance = LibClone.deployDeterministicERC1967(0, bootstrap, salt); // LibClone.bootstrapERC1967(bootstrap, implementation); // ``` /// @dev Deploys the ERC1967 bootstrap if it has not been deployed. function erc1967Bootstrap() internal returns (address) { return erc1967Bootstrap(address(this)); } /// @dev Deploys the ERC1967 bootstrap if it has not been deployed. function erc1967Bootstrap(address authorizedUpgrader) internal returns (address bootstrap) { bytes memory c = initCodeERC1967Bootstrap(authorizedUpgrader); bootstrap = predictDeterministicAddress(keccak256(c), bytes32(0), address(this)); /// @solidity memory-safe-assembly assembly { if iszero(extcodesize(bootstrap)) { if iszero(create2(0, add(c, 0x20), mload(c), 0)) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } } } } /// @dev Replaces the implementation at `instance`. function bootstrapERC1967(address instance, address implementation) internal { /// @solidity memory-safe-assembly assembly { mstore(0x00, implementation) if iszero(call(gas(), instance, 0, 0x0c, 0x14, codesize(), 0x00)) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } } } /// @dev Replaces the implementation at `instance`, and then call it with `data`. function bootstrapERC1967AndCall(address instance, address implementation, bytes memory data) internal { /// @solidity memory-safe-assembly assembly { let n := mload(data) mstore(data, implementation) if iszero(call(gas(), instance, 0, add(data, 0x0c), add(n, 0x14), codesize(), 0x00)) { if iszero(returndatasize()) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } returndatacopy(mload(0x40), 0x00, returndatasize()) revert(mload(0x40), returndatasize()) } mstore(data, n) // Restore the length of `data`. } } /// @dev Returns the implementation address of the ERC1967 bootstrap for this contract. function predictDeterministicAddressERC1967Bootstrap() internal view returns (address) { return predictDeterministicAddressERC1967Bootstrap(address(this), address(this)); } /// @dev Returns the implementation address of the ERC1967 bootstrap for this contract. function predictDeterministicAddressERC1967Bootstrap( address authorizedUpgrader, address deployer ) internal pure returns (address) { bytes32 hash = initCodeHashERC1967Bootstrap(authorizedUpgrader); return predictDeterministicAddress(hash, bytes32(0), deployer); } /// @dev Returns the initialization code of the ERC1967 bootstrap. function initCodeERC1967Bootstrap(address authorizedUpgrader) internal pure returns (bytes memory c) { /// @solidity memory-safe-assembly assembly { c := mload(0x40) mstore(add(c, 0x80), 0x3d3560601c5af46047573d6000383e3d38fd0000000000000000000000000000) mstore(add(c, 0x60), 0xa920a3ca505d382bbc55601436116049575b005b363d3d373d3d601436036014) mstore(add(c, 0x40), 0x0338573d3560601c7f360894a13ba1a3210667c828492db98dca3e2076cc3735) mstore(add(c, 0x20), authorizedUpgrader) mstore(add(c, 0x0c), 0x606880600a3d393df3fe3373) mstore(c, 0x72) mstore(0x40, add(c, 0xa0)) } } /// @dev Returns the initialization code hash of the ERC1967 bootstrap. function initCodeHashERC1967Bootstrap(address authorizedUpgrader) internal pure returns (bytes32) { return keccak256(initCodeERC1967Bootstrap(authorizedUpgrader)); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* MINIMAL ERC1967 BEACON PROXY OPERATIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ // Note: If you use this proxy, you MUST make sure that the beacon is a // valid ERC1967 beacon. This means that the beacon must always return a valid // address upon a staticcall to `implementation()`, given sufficient gas. // For performance, the deployment operations and the proxy assumes that the // beacon is always valid and will NOT validate it. /// @dev Deploys a minimal ERC1967 beacon proxy. function deployERC1967BeaconProxy(address beacon) internal returns (address instance) { instance = deployERC1967BeaconProxy(0, beacon); } /// @dev Deploys a minimal ERC1967 beacon proxy. /// Deposits `value` ETH during deployment. function deployERC1967BeaconProxy(uint256 value, address beacon) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { /** * ---------------------------------------------------------------------------------+ * CREATION (34 bytes) | * ---------------------------------------------------------------------------------| * Opcode | Mnemonic | Stack | Memory | * ---------------------------------------------------------------------------------| * 60 runSize | PUSH1 runSize | r | | * 3d | RETURNDATASIZE | 0 r | | * 81 | DUP2 | r 0 r | | * 60 offset | PUSH1 offset | o r 0 r | | * 3d | RETURNDATASIZE | 0 o r 0 r | | * 39 | CODECOPY | 0 r | [0..runSize): runtime code | * 73 beac | PUSH20 beac | beac 0 r | [0..runSize): runtime code | * 60 slotPos | PUSH1 slotPos | slotPos beac 0 r | [0..runSize): runtime code | * 51 | MLOAD | slot beac 0 r | [0..runSize): runtime code | * 55 | SSTORE | 0 r | [0..runSize): runtime code | * f3 | RETURN | | [0..runSize): runtime code | * ---------------------------------------------------------------------------------| * RUNTIME (82 bytes) | * ---------------------------------------------------------------------------------| * Opcode | Mnemonic | Stack | Memory | * ---------------------------------------------------------------------------------| * | * ::: copy calldata to memory :::::::::::::::::::::::::::::::::::::::::::::::::::: | * 36 | CALLDATASIZE | cds | | * 3d | RETURNDATASIZE | 0 cds | | * 3d | RETURNDATASIZE | 0 0 cds | | * 37 | CALLDATACOPY | | [0..calldatasize): calldata | * | * ::: delegatecall to implementation ::::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | 0 | | * 3d | RETURNDATASIZE | 0 0 | | * 36 | CALLDATASIZE | cds 0 0 | [0..calldatasize): calldata | * 3d | RETURNDATASIZE | 0 cds 0 0 | [0..calldatasize): calldata | * | * ~~~~~~~ beacon staticcall sub procedure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | * 60 0x20 | PUSH1 0x20 | 32 | | * 36 | CALLDATASIZE | cds 32 | | * 60 0x04 | PUSH1 0x04 | 4 cds 32 | | * 36 | CALLDATASIZE | cds 4 cds 32 | | * 63 0x5c60da1b | PUSH4 0x5c60da1b | 0x5c60da1b cds 4 cds 32 | | * 60 0xe0 | PUSH1 0xe0 | 224 0x5c60da1b cds 4 cds 32 | | * 1b | SHL | sel cds 4 cds 32 | | * 36 | CALLDATASIZE | cds sel cds 4 cds 32 | | * 52 | MSTORE | cds 4 cds 32 | sel | * 7f slot | PUSH32 slot | s cds 4 cds 32 | sel | * 54 | SLOAD | beac cds 4 cds 32 | sel | * 5a | GAS | g beac cds 4 cds 32 | sel | * fa | STATICCALL | succ | impl | * 50 | POP | | impl | * 36 | CALLDATASIZE | cds | impl | * 51 | MLOAD | impl | impl | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | * 5a | GAS | g impl 0 cds 0 0 | [0..calldatasize): calldata | * f4 | DELEGATECALL | succ | [0..calldatasize): calldata | * | * ::: copy returndata to memory :::::::::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | rds succ | [0..calldatasize): calldata | * 60 0x00 | PUSH1 0x00 | 0 rds succ | [0..calldatasize): calldata | * 80 | DUP1 | 0 0 rds succ | [0..calldatasize): calldata | * 3e | RETURNDATACOPY | succ | [0..returndatasize): returndata | * | * ::: branch on delegatecall status :::::::::::::::::::::::::::::::::::::::::::::: | * 60 0x4d | PUSH1 0x4d | dest succ | [0..returndatasize): returndata | * 57 | JUMPI | | [0..returndatasize): returndata | * | * ::: delegatecall failed, revert :::::::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | rds | [0..returndatasize): returndata | * 60 0x00 | PUSH1 0x00 | 0 rds | [0..returndatasize): returndata | * fd | REVERT | | [0..returndatasize): returndata | * | * ::: delegatecall succeeded, return ::::::::::::::::::::::::::::::::::::::::::::: | * 5b | JUMPDEST | | [0..returndatasize): returndata | * 3d | RETURNDATASIZE | rds | [0..returndatasize): returndata | * 60 0x00 | PUSH1 0x00 | 0 rds | [0..returndatasize): returndata | * f3 | RETURN | | [0..returndatasize): returndata | * ---------------------------------------------------------------------------------+ */ let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0xb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f3) mstore(0x40, 0x1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6c) mstore(0x20, 0x60195155f3363d3d373d3d363d602036600436635c60da) mstore(0x09, or(shl(160, 0x60523d8160223d3973), shr(96, shl(96, beacon)))) instance := create(value, 0x0c, 0x74) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Deploys a deterministic minimal ERC1967 beacon proxy with `salt`. function deployDeterministicERC1967BeaconProxy(address beacon, bytes32 salt) internal returns (address instance) { instance = deployDeterministicERC1967BeaconProxy(0, beacon, salt); } /// @dev Deploys a deterministic minimal ERC1967 beacon proxy with `salt`. /// Deposits `value` ETH during deployment. function deployDeterministicERC1967BeaconProxy(uint256 value, address beacon, bytes32 salt) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0xb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f3) mstore(0x40, 0x1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6c) mstore(0x20, 0x60195155f3363d3d373d3d363d602036600436635c60da) mstore(0x09, or(shl(160, 0x60523d8160223d3973), shr(96, shl(96, beacon)))) instance := create2(value, 0x0c, 0x74, salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Creates a deterministic minimal ERC1967 beacon proxy with `salt`. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967BeaconProxy(address beacon, bytes32 salt) internal returns (bool alreadyDeployed, address instance) { return createDeterministicERC1967BeaconProxy(0, beacon, salt); } /// @dev Creates a deterministic minimal ERC1967 beacon proxy with `salt`. /// Deposits `value` ETH during deployment. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967BeaconProxy(uint256 value, address beacon, bytes32 salt) internal returns (bool alreadyDeployed, address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0xb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f3) mstore(0x40, 0x1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6c) mstore(0x20, 0x60195155f3363d3d373d3d363d602036600436635c60da) mstore(0x09, or(shl(160, 0x60523d8160223d3973), shr(96, shl(96, beacon)))) // Compute and store the bytecode hash. mstore(add(m, 0x35), keccak256(0x0c, 0x74)) mstore(m, shl(88, address())) mstore8(m, 0xff) // Write the prefix. mstore(add(m, 0x15), salt) instance := keccak256(m, 0x55) for {} 1 {} { if iszero(extcodesize(instance)) { instance := create2(value, 0x0c, 0x74, salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } break } alreadyDeployed := 1 if iszero(value) { break } if iszero(call(gas(), instance, value, codesize(), 0x00, codesize(), 0x00)) { mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`. revert(0x1c, 0x04) } break } mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Returns the initialization code of the minimal ERC1967 beacon proxy. function initCodeERC1967BeaconProxy(address beacon) internal pure returns (bytes memory c) { /// @solidity memory-safe-assembly assembly { c := mload(0x40) mstore(add(c, 0x74), 0xb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f3) mstore(add(c, 0x54), 0x1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6c) mstore(add(c, 0x34), 0x60195155f3363d3d373d3d363d602036600436635c60da) mstore(add(c, 0x1d), beacon) mstore(add(c, 0x09), 0x60523d8160223d3973) mstore(add(c, 0x94), 0) mstore(c, 0x74) // Store the length. mstore(0x40, add(c, 0xa0)) // Allocate memory. } } /// @dev Returns the initialization code hash of the minimal ERC1967 beacon proxy. function initCodeHashERC1967BeaconProxy(address beacon) internal pure returns (bytes32 hash) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0xb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f3) mstore(0x40, 0x1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6c) mstore(0x20, 0x60195155f3363d3d373d3d363d602036600436635c60da) mstore(0x09, or(shl(160, 0x60523d8160223d3973), shr(96, shl(96, beacon)))) hash := keccak256(0x0c, 0x74) mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Returns the address of the ERC1967 beacon proxy, with `salt` by `deployer`. /// Note: The returned result has dirty upper 96 bits. Please clean if used in assembly. function predictDeterministicAddressERC1967BeaconProxy( address beacon, bytes32 salt, address deployer ) internal pure returns (address predicted) { bytes32 hash = initCodeHashERC1967BeaconProxy(beacon); predicted = predictDeterministicAddress(hash, salt, deployer); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* ERC1967 BEACON PROXY WITH IMMUTABLE ARGS OPERATIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Deploys a minimal ERC1967 beacon proxy with `args`. function deployERC1967BeaconProxy(address beacon, bytes memory args) internal returns (address instance) { instance = deployERC1967BeaconProxy(0, beacon, args); } /// @dev Deploys a minimal ERC1967 beacon proxy with `args`. /// Deposits `value` ETH during deployment. function deployERC1967BeaconProxy(uint256 value, address beacon, bytes memory args) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x8b), n)) mstore(add(m, 0x6b), 0xb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f3) mstore(add(m, 0x4b), 0x1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6c) mstore(add(m, 0x2b), 0x60195155f3363d3d373d3d363d602036600436635c60da) mstore(add(m, 0x14), beacon) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x52 = 0xffad`. mstore(add(m, gt(n, 0xffad)), add(0xfe6100523d8160233d3973, shl(56, n))) instance := create(value, add(m, 0x16), add(n, 0x75)) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } } } /// @dev Deploys a deterministic minimal ERC1967 beacon proxy with `args` and `salt`. function deployDeterministicERC1967BeaconProxy(address beacon, bytes memory args, bytes32 salt) internal returns (address instance) { instance = deployDeterministicERC1967BeaconProxy(0, beacon, args, salt); } /// @dev Deploys a deterministic minimal ERC1967 beacon proxy with `args` and `salt`. /// Deposits `value` ETH during deployment. function deployDeterministicERC1967BeaconProxy( uint256 value, address beacon, bytes memory args, bytes32 salt ) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x8b), n)) mstore(add(m, 0x6b), 0xb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f3) mstore(add(m, 0x4b), 0x1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6c) mstore(add(m, 0x2b), 0x60195155f3363d3d373d3d363d602036600436635c60da) mstore(add(m, 0x14), beacon) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x52 = 0xffad`. mstore(add(m, gt(n, 0xffad)), add(0xfe6100523d8160233d3973, shl(56, n))) instance := create2(value, add(m, 0x16), add(n, 0x75), salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } } } /// @dev Creates a deterministic minimal ERC1967 beacon proxy with `args` and `salt`. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967BeaconProxy(address beacon, bytes memory args, bytes32 salt) internal returns (bool alreadyDeployed, address instance) { return createDeterministicERC1967BeaconProxy(0, beacon, args, salt); } /// @dev Creates a deterministic minimal ERC1967 beacon proxy with `args` and `salt`. /// Deposits `value` ETH during deployment. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967BeaconProxy( uint256 value, address beacon, bytes memory args, bytes32 salt ) internal returns (bool alreadyDeployed, address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x8b), n)) mstore(add(m, 0x6b), 0xb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f3) mstore(add(m, 0x4b), 0x1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6c) mstore(add(m, 0x2b), 0x60195155f3363d3d373d3d363d602036600436635c60da) mstore(add(m, 0x14), beacon) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x52 = 0xffad`. mstore(add(m, gt(n, 0xffad)), add(0xfe6100523d8160233d3973, shl(56, n))) // Compute and store the bytecode hash. mstore8(0x00, 0xff) // Write the prefix. mstore(0x35, keccak256(add(m, 0x16), add(n, 0x75))) mstore(0x01, shl(96, address())) mstore(0x15, salt) instance := keccak256(0x00, 0x55) for {} 1 {} { if iszero(extcodesize(instance)) { instance := create2(value, add(m, 0x16), add(n, 0x75), salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } break } alreadyDeployed := 1 if iszero(value) { break } if iszero(call(gas(), instance, value, codesize(), 0x00, codesize(), 0x00)) { mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`. revert(0x1c, 0x04) } break } mstore(0x35, 0) // Restore the overwritten part of the free memory pointer. } } /// @dev Returns the initialization code of the minimal ERC1967 beacon proxy. function initCodeERC1967BeaconProxy(address beacon, bytes memory args) internal pure returns (bytes memory c) { /// @solidity memory-safe-assembly assembly { c := mload(0x40) let n := mload(args) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x52 = 0xffad`. returndatacopy(returndatasize(), returndatasize(), gt(n, 0xffad)) for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { mstore(add(add(c, 0x95), i), mload(add(add(args, 0x20), i))) } mstore(add(c, 0x75), 0xb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f3) mstore(add(c, 0x55), 0x1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6c) mstore(add(c, 0x35), 0x60195155f3363d3d373d3d363d602036600436635c60da) mstore(add(c, 0x1e), beacon) mstore(add(c, 0x0a), add(0x6100523d8160233d3973, shl(56, n))) mstore(c, add(n, 0x75)) // Store the length. mstore(add(c, add(n, 0x95)), 0) // Zeroize the slot after the bytes. mstore(0x40, add(c, add(n, 0xb5))) // Allocate memory. } } /// @dev Returns the initialization code hash of the minimal ERC1967 beacon proxy with `args`. function initCodeHashERC1967BeaconProxy(address beacon, bytes memory args) internal pure returns (bytes32 hash) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x52 = 0xffad`. returndatacopy(returndatasize(), returndatasize(), gt(n, 0xffad)) for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { mstore(add(add(m, 0x8b), i), mload(add(add(args, 0x20), i))) } mstore(add(m, 0x6b), 0xb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f3) mstore(add(m, 0x4b), 0x1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6c) mstore(add(m, 0x2b), 0x60195155f3363d3d373d3d363d602036600436635c60da) mstore(add(m, 0x14), beacon) mstore(m, add(0x6100523d8160233d3973, shl(56, n))) hash := keccak256(add(m, 0x16), add(n, 0x75)) } } /// @dev Returns the address of the ERC1967 beacon proxy with `args`, with `salt` by `deployer`. /// Note: The returned result has dirty upper 96 bits. Please clean if used in assembly. function predictDeterministicAddressERC1967BeaconProxy( address beacon, bytes memory args, bytes32 salt, address deployer ) internal pure returns (address predicted) { bytes32 hash = initCodeHashERC1967BeaconProxy(beacon, args); predicted = predictDeterministicAddress(hash, salt, deployer); } /// @dev Equivalent to `argsOnERC1967BeaconProxy(instance, start, 2 ** 256 - 1)`. function argsOnERC1967BeaconProxy(address instance) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) mstore(args, and(0xffffffffff, sub(extcodesize(instance), 0x52))) // Store the length. extcodecopy(instance, add(args, 0x20), 0x52, add(mload(args), 0x20)) mstore(0x40, add(mload(args), add(args, 0x40))) // Allocate memory. } } /// @dev Equivalent to `argsOnERC1967BeaconProxy(instance, start, 2 ** 256 - 1)`. function argsOnERC1967BeaconProxy(address instance, uint256 start) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) let n := and(0xffffffffff, sub(extcodesize(instance), 0x52)) extcodecopy(instance, add(args, 0x20), add(start, 0x52), add(n, 0x20)) mstore(args, mul(sub(n, start), lt(start, n))) // Store the length. mstore(0x40, add(args, add(0x40, mload(args)))) // Allocate memory. } } /// @dev Returns a slice of the immutable arguments on `instance` from `start` to `end`. /// `start` and `end` will be clamped to the range `[0, args.length]`. /// The `instance` MUST be deployed via the ERC1967 beacon proxy with immutable args functions. /// Otherwise, the behavior is undefined. /// Out-of-gas reverts if `instance` does not have any code. function argsOnERC1967BeaconProxy(address instance, uint256 start, uint256 end) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) if iszero(lt(end, 0xffff)) { end := 0xffff } let d := mul(sub(end, start), lt(start, end)) extcodecopy(instance, args, add(start, 0x32), add(d, 0x20)) if iszero(and(0xff, mload(add(args, d)))) { let n := sub(extcodesize(instance), 0x52) returndatacopy(returndatasize(), returndatasize(), shr(40, n)) d := mul(gt(n, start), sub(d, mul(gt(end, n), sub(end, n)))) } mstore(args, d) // Store the length. mstore(add(add(args, 0x20), d), 0) // Zeroize the slot after the bytes. mstore(0x40, add(add(args, 0x40), d)) // Allocate memory. } } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* ERC1967I BEACON PROXY OPERATIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ // Note: This proxy has a special code path that activates if `calldatasize() == 1`. // This code path skips the delegatecall and directly returns the `implementation` address. // The returned implementation is guaranteed to be valid if the keccak256 of the // proxy's code is equal to `ERC1967_BEACON_PROXY_CODE_HASH`. // // If you use this proxy, you MUST make sure that the beacon is a // valid ERC1967 beacon. This means that the beacon must always return a valid // address upon a staticcall to `implementation()`, given sufficient gas. // For performance, the deployment operations and the proxy assumes that the // beacon is always valid and will NOT validate it. /// @dev Deploys a ERC1967I beacon proxy. function deployERC1967IBeaconProxy(address beacon) internal returns (address instance) { instance = deployERC1967IBeaconProxy(0, beacon); } /// @dev Deploys a ERC1967I beacon proxy. /// Deposits `value` ETH during deployment. function deployERC1967IBeaconProxy(uint256 value, address beacon) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { /** * ---------------------------------------------------------------------------------+ * CREATION (34 bytes) | * ---------------------------------------------------------------------------------| * Opcode | Mnemonic | Stack | Memory | * ---------------------------------------------------------------------------------| * 60 runSize | PUSH1 runSize | r | | * 3d | RETURNDATASIZE | 0 r | | * 81 | DUP2 | r 0 r | | * 60 offset | PUSH1 offset | o r 0 r | | * 3d | RETURNDATASIZE | 0 o r 0 r | | * 39 | CODECOPY | 0 r | [0..runSize): runtime code | * 73 beac | PUSH20 beac | beac 0 r | [0..runSize): runtime code | * 60 slotPos | PUSH1 slotPos | slotPos beac 0 r | [0..runSize): runtime code | * 51 | MLOAD | slot beac 0 r | [0..runSize): runtime code | * 55 | SSTORE | 0 r | [0..runSize): runtime code | * f3 | RETURN | | [0..runSize): runtime code | * ---------------------------------------------------------------------------------| * RUNTIME (87 bytes) | * ---------------------------------------------------------------------------------| * Opcode | Mnemonic | Stack | Memory | * ---------------------------------------------------------------------------------| * | * ::: copy calldata to memory :::::::::::::::::::::::::::::::::::::::::::::::::::: | * 36 | CALLDATASIZE | cds | | * 3d | RETURNDATASIZE | 0 cds | | * 3d | RETURNDATASIZE | 0 0 cds | | * 37 | CALLDATACOPY | | [0..calldatasize): calldata | * | * ::: delegatecall to implementation ::::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | 0 | | * 3d | RETURNDATASIZE | 0 0 | | * 36 | CALLDATASIZE | cds 0 0 | [0..calldatasize): calldata | * 3d | RETURNDATASIZE | 0 cds 0 0 | [0..calldatasize): calldata | * | * ~~~~~~~ beacon staticcall sub procedure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | * 60 0x20 | PUSH1 0x20 | 32 | | * 36 | CALLDATASIZE | cds 32 | | * 60 0x04 | PUSH1 0x04 | 4 cds 32 | | * 36 | CALLDATASIZE | cds 4 cds 32 | | * 63 0x5c60da1b | PUSH4 0x5c60da1b | 0x5c60da1b cds 4 cds 32 | | * 60 0xe0 | PUSH1 0xe0 | 224 0x5c60da1b cds 4 cds 32 | | * 1b | SHL | sel cds 4 cds 32 | | * 36 | CALLDATASIZE | cds sel cds 4 cds 32 | | * 52 | MSTORE | cds 4 cds 32 | sel | * 7f slot | PUSH32 slot | s cds 4 cds 32 | sel | * 54 | SLOAD | beac cds 4 cds 32 | sel | * 5a | GAS | g beac cds 4 cds 32 | sel | * fa | STATICCALL | succ | impl | * ~~~~~~ check calldatasize ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | * 36 | CALLDATASIZE | cds succ | | * 14 | EQ | | impl | * 60 0x52 | PUSH1 0x52 | | impl | * 57 | JUMPI | | impl | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | * 36 | CALLDATASIZE | cds | impl | * 51 | MLOAD | impl | impl | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | * 5a | GAS | g impl 0 cds 0 0 | [0..calldatasize): calldata | * f4 | DELEGATECALL | succ | [0..calldatasize): calldata | * | * ::: copy returndata to memory :::::::::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | rds succ | [0..calldatasize): calldata | * 60 0x00 | PUSH1 0x00 | 0 rds succ | [0..calldatasize): calldata | * 60 0x01 | PUSH1 0x01 | 1 0 rds succ | [0..calldatasize): calldata | * 3e | RETURNDATACOPY | succ | [1..returndatasize): returndata | * | * ::: branch on delegatecall status :::::::::::::::::::::::::::::::::::::::::::::: | * 60 0x52 | PUSH1 0x52 | dest succ | [1..returndatasize): returndata | * 57 | JUMPI | | [1..returndatasize): returndata | * | * ::: delegatecall failed, revert :::::::::::::::::::::::::::::::::::::::::::::::: | * 3d | RETURNDATASIZE | rds | [1..returndatasize): returndata | * 60 0x01 | PUSH1 0x01 | 1 rds | [1..returndatasize): returndata | * fd | REVERT | | [1..returndatasize): returndata | * | * ::: delegatecall succeeded, return ::::::::::::::::::::::::::::::::::::::::::::: | * 5b | JUMPDEST | | [1..returndatasize): returndata | * 3d | RETURNDATASIZE | rds | [1..returndatasize): returndata | * 60 0x01 | PUSH1 0x01 | 1 rds | [1..returndatasize): returndata | * f3 | RETURN | | [1..returndatasize): returndata | * ---------------------------------------------------------------------------------+ */ let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0x3d50545afa361460525736515af43d600060013e6052573d6001fd5b3d6001f3) mstore(0x40, 0x527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b3513) mstore(0x20, 0x60195155f3363d3d373d3d363d602036600436635c60da1b60e01b36) mstore(0x04, or(shl(160, 0x60573d8160223d3973), shr(96, shl(96, beacon)))) instance := create(value, 0x07, 0x79) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Deploys a deterministic ERC1967I beacon proxy with `salt`. function deployDeterministicERC1967IBeaconProxy(address beacon, bytes32 salt) internal returns (address instance) { instance = deployDeterministicERC1967IBeaconProxy(0, beacon, salt); } /// @dev Deploys a deterministic ERC1967I beacon proxy with `salt`. /// Deposits `value` ETH during deployment. function deployDeterministicERC1967IBeaconProxy(uint256 value, address beacon, bytes32 salt) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0x3d50545afa361460525736515af43d600060013e6052573d6001fd5b3d6001f3) mstore(0x40, 0x527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b3513) mstore(0x20, 0x60195155f3363d3d373d3d363d602036600436635c60da1b60e01b36) mstore(0x04, or(shl(160, 0x60573d8160223d3973), shr(96, shl(96, beacon)))) instance := create2(value, 0x07, 0x79, salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Creates a deterministic ERC1967I beacon proxy with `salt`. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967IBeaconProxy(address beacon, bytes32 salt) internal returns (bool alreadyDeployed, address instance) { return createDeterministicERC1967IBeaconProxy(0, beacon, salt); } /// @dev Creates a deterministic ERC1967I beacon proxy with `salt`. /// Deposits `value` ETH during deployment. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967IBeaconProxy(uint256 value, address beacon, bytes32 salt) internal returns (bool alreadyDeployed, address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0x3d50545afa361460525736515af43d600060013e6052573d6001fd5b3d6001f3) mstore(0x40, 0x527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b3513) mstore(0x20, 0x60195155f3363d3d373d3d363d602036600436635c60da1b60e01b36) mstore(0x04, or(shl(160, 0x60573d8160223d3973), shr(96, shl(96, beacon)))) // Compute and store the bytecode hash. mstore(add(m, 0x35), keccak256(0x07, 0x79)) mstore(m, shl(88, address())) mstore8(m, 0xff) // Write the prefix. mstore(add(m, 0x15), salt) instance := keccak256(m, 0x55) for {} 1 {} { if iszero(extcodesize(instance)) { instance := create2(value, 0x07, 0x79, salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } break } alreadyDeployed := 1 if iszero(value) { break } if iszero(call(gas(), instance, value, codesize(), 0x00, codesize(), 0x00)) { mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`. revert(0x1c, 0x04) } break } mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Returns the initialization code of the ERC1967I beacon proxy. function initCodeERC1967IBeaconProxy(address beacon) internal pure returns (bytes memory c) { /// @solidity memory-safe-assembly assembly { c := mload(0x40) mstore(add(c, 0x79), 0x3d50545afa361460525736515af43d600060013e6052573d6001fd5b3d6001f3) mstore(add(c, 0x59), 0x527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b3513) mstore(add(c, 0x39), 0x60195155f3363d3d373d3d363d602036600436635c60da1b60e01b36) mstore(add(c, 0x1d), beacon) mstore(add(c, 0x09), 0x60573d8160223d3973) mstore(add(c, 0x99), 0) mstore(c, 0x79) // Store the length. mstore(0x40, add(c, 0xa0)) // Allocate memory. } } /// @dev Returns the initialization code hash of the ERC1967I beacon proxy. function initCodeHashERC1967IBeaconProxy(address beacon) internal pure returns (bytes32 hash) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. mstore(0x60, 0x3d50545afa361460525736515af43d600060013e6052573d6001fd5b3d6001f3) mstore(0x40, 0x527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b3513) mstore(0x20, 0x60195155f3363d3d373d3d363d602036600436635c60da1b60e01b36) mstore(0x04, or(shl(160, 0x60573d8160223d3973), shr(96, shl(96, beacon)))) hash := keccak256(0x07, 0x79) mstore(0x40, m) // Restore the free memory pointer. mstore(0x60, 0) // Restore the zero slot. } } /// @dev Returns the address of the ERC1967I beacon proxy, with `salt` by `deployer`. /// Note: The returned result has dirty upper 96 bits. Please clean if used in assembly. function predictDeterministicAddressERC1967IBeaconProxy( address beacon, bytes32 salt, address deployer ) internal pure returns (address predicted) { bytes32 hash = initCodeHashERC1967IBeaconProxy(beacon); predicted = predictDeterministicAddress(hash, salt, deployer); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* ERC1967I BEACON PROXY WITH IMMUTABLE ARGS OPERATIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Deploys a ERC1967I beacon proxy with `args. function deployERC1967IBeaconProxy(address beacon, bytes memory args) internal returns (address instance) { instance = deployERC1967IBeaconProxy(0, beacon, args); } /// @dev Deploys a ERC1967I beacon proxy with `args. /// Deposits `value` ETH during deployment. function deployERC1967IBeaconProxy(uint256 value, address beacon, bytes memory args) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x90), n)) mstore(add(m, 0x70), 0x3d50545afa361460525736515af43d600060013e6052573d6001fd5b3d6001f3) mstore(add(m, 0x50), 0x527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b3513) mstore(add(m, 0x30), 0x60195155f3363d3d373d3d363d602036600436635c60da1b60e01b36) mstore(add(m, 0x14), beacon) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x57 = 0xffa8`. mstore(add(m, gt(n, 0xffa8)), add(0xfe6100573d8160233d3973, shl(56, n))) instance := create(value, add(m, 0x16), add(n, 0x7a)) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } } } /// @dev Deploys a deterministic ERC1967I beacon proxy with `args` and `salt`. function deployDeterministicERC1967IBeaconProxy(address beacon, bytes memory args, bytes32 salt) internal returns (address instance) { instance = deployDeterministicERC1967IBeaconProxy(0, beacon, args, salt); } /// @dev Deploys a deterministic ERC1967I beacon proxy with `args` and `salt`. /// Deposits `value` ETH during deployment. function deployDeterministicERC1967IBeaconProxy( uint256 value, address beacon, bytes memory args, bytes32 salt ) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) // Cache the free memory pointer. let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x90), n)) mstore(add(m, 0x70), 0x3d50545afa361460525736515af43d600060013e6052573d6001fd5b3d6001f3) mstore(add(m, 0x50), 0x527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b3513) mstore(add(m, 0x30), 0x60195155f3363d3d373d3d363d602036600436635c60da1b60e01b36) mstore(add(m, 0x14), beacon) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x57 = 0xffa8`. mstore(add(m, gt(n, 0xffa8)), add(0xfe6100573d8160233d3973, shl(56, n))) instance := create2(value, add(m, 0x16), add(n, 0x7a), salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } } } /// @dev Creates a deterministic ERC1967I beacon proxy with `args` and `salt`. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967IBeaconProxy(address beacon, bytes memory args, bytes32 salt) internal returns (bool alreadyDeployed, address instance) { return createDeterministicERC1967IBeaconProxy(0, beacon, args, salt); } /// @dev Creates a deterministic ERC1967I beacon proxy with `args` and `salt`. /// Deposits `value` ETH during deployment. /// Note: This method is intended for use in ERC4337 factories, /// which are expected to NOT revert if the proxy is already deployed. function createDeterministicERC1967IBeaconProxy( uint256 value, address beacon, bytes memory args, bytes32 salt ) internal returns (bool alreadyDeployed, address instance) { /// @solidity memory-safe-assembly assembly { let m := mload(0x40) let n := mload(args) pop(staticcall(gas(), 4, add(args, 0x20), n, add(m, 0x90), n)) mstore(add(m, 0x70), 0x3d50545afa361460525736515af43d600060013e6052573d6001fd5b3d6001f3) mstore(add(m, 0x50), 0x527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b3513) mstore(add(m, 0x30), 0x60195155f3363d3d373d3d363d602036600436635c60da1b60e01b36) mstore(add(m, 0x14), beacon) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x57 = 0xffa8`. mstore(add(m, gt(n, 0xffa8)), add(0xfe6100573d8160233d3973, shl(56, n))) // Compute and store the bytecode hash. mstore8(0x00, 0xff) // Write the prefix. mstore(0x35, keccak256(add(m, 0x16), add(n, 0x7a))) mstore(0x01, shl(96, address())) mstore(0x15, salt) instance := keccak256(0x00, 0x55) for {} 1 {} { if iszero(extcodesize(instance)) { instance := create2(value, add(m, 0x16), add(n, 0x7a), salt) if iszero(instance) { mstore(0x00, 0x30116425) // `DeploymentFailed()`. revert(0x1c, 0x04) } break } alreadyDeployed := 1 if iszero(value) { break } if iszero(call(gas(), instance, value, codesize(), 0x00, codesize(), 0x00)) { mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`. revert(0x1c, 0x04) } break } mstore(0x35, 0) // Restore the overwritten part of the free memory pointer. } } /// @dev Returns the initialization code of the ERC1967I beacon proxy with `args`. function initCodeERC1967IBeaconProxy(address beacon, bytes memory args) internal pure returns (bytes memory c) { /// @solidity memory-safe-assembly assembly { c := mload(0x40) let n := mload(args) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x57 = 0xffa8`. returndatacopy(returndatasize(), returndatasize(), gt(n, 0xffa8)) for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { mstore(add(add(c, 0x9a), i), mload(add(add(args, 0x20), i))) } mstore(add(c, 0x7a), 0x3d50545afa361460525736515af43d600060013e6052573d6001fd5b3d6001f3) mstore(add(c, 0x5a), 0x527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b3513) mstore(add(c, 0x3a), 0x60195155f3363d3d373d3d363d602036600436635c60da1b60e01b36) mstore(add(c, 0x1e), beacon) mstore(add(c, 0x0a), add(0x6100573d8160233d3973, shl(56, n))) mstore(add(c, add(n, 0x9a)), 0) mstore(c, add(n, 0x7a)) // Store the length. mstore(0x40, add(c, add(n, 0xba))) // Allocate memory. } } /// @dev Returns the initialization code hash of the ERC1967I beacon proxy with `args`. function initCodeHashERC1967IBeaconProxy(address beacon, bytes memory args) internal pure returns (bytes32 hash) { /// @solidity memory-safe-assembly assembly { let c := mload(0x40) // Cache the free memory pointer. let n := mload(args) // Do a out-of-gas revert if `n` is greater than `0xffff - 0x57 = 0xffa8`. returndatacopy(returndatasize(), returndatasize(), gt(n, 0xffa8)) for { let i := 0 } lt(i, n) { i := add(i, 0x20) } { mstore(add(add(c, 0x90), i), mload(add(add(args, 0x20), i))) } mstore(add(c, 0x70), 0x3d50545afa361460525736515af43d600060013e6052573d6001fd5b3d6001f3) mstore(add(c, 0x50), 0x527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b3513) mstore(add(c, 0x30), 0x60195155f3363d3d373d3d363d602036600436635c60da1b60e01b36) mstore(add(c, 0x14), beacon) mstore(c, add(0x6100573d8160233d3973, shl(56, n))) hash := keccak256(add(c, 0x16), add(n, 0x7a)) } } /// @dev Returns the address of the ERC1967I beacon proxy, with `args` and salt` by `deployer`. /// Note: The returned result has dirty upper 96 bits. Please clean if used in assembly. function predictDeterministicAddressERC1967IBeaconProxy( address beacon, bytes memory args, bytes32 salt, address deployer ) internal pure returns (address predicted) { bytes32 hash = initCodeHashERC1967IBeaconProxy(beacon, args); predicted = predictDeterministicAddress(hash, salt, deployer); } /// @dev Equivalent to `argsOnERC1967IBeaconProxy(instance, start, 2 ** 256 - 1)`. function argsOnERC1967IBeaconProxy(address instance) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) mstore(args, and(0xffffffffff, sub(extcodesize(instance), 0x57))) // Store the length. extcodecopy(instance, add(args, 0x20), 0x57, add(mload(args), 0x20)) mstore(0x40, add(mload(args), add(args, 0x40))) // Allocate memory. } } /// @dev Equivalent to `argsOnERC1967IBeaconProxy(instance, start, 2 ** 256 - 1)`. function argsOnERC1967IBeaconProxy(address instance, uint256 start) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) let n := and(0xffffffffff, sub(extcodesize(instance), 0x57)) extcodecopy(instance, add(args, 0x20), add(start, 0x57), add(n, 0x20)) mstore(args, mul(sub(n, start), lt(start, n))) // Store the length. mstore(0x40, add(args, add(0x40, mload(args)))) // Allocate memory. } } /// @dev Returns a slice of the immutable arguments on `instance` from `start` to `end`. /// `start` and `end` will be clamped to the range `[0, args.length]`. /// The `instance` MUST be deployed via the ERC1967I beacon proxy with immutable args functions. /// Otherwise, the behavior is undefined. /// Out-of-gas reverts if `instance` does not have any code. function argsOnERC1967IBeaconProxy(address instance, uint256 start, uint256 end) internal view returns (bytes memory args) { /// @solidity memory-safe-assembly assembly { args := mload(0x40) if iszero(lt(end, 0xffff)) { end := 0xffff } let d := mul(sub(end, start), lt(start, end)) extcodecopy(instance, args, add(start, 0x37), add(d, 0x20)) if iszero(and(0xff, mload(add(args, d)))) { let n := sub(extcodesize(instance), 0x57) returndatacopy(returndatasize(), returndatasize(), shr(40, n)) d := mul(gt(n, start), sub(d, mul(gt(end, n), sub(end, n)))) } mstore(args, d) // Store the length. mstore(add(add(args, 0x20), d), 0) // Zeroize the slot after the bytes. mstore(0x40, add(add(args, 0x40), d)) // Allocate memory. } } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* OTHER OPERATIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Returns `address(0)` if the implementation address cannot be determined. function implementationOf(address instance) internal view returns (address result) { /// @solidity memory-safe-assembly assembly { for { extcodecopy(instance, 0x00, 0x00, 0x57) } 1 {} { if mload(0x2d) { // ERC1967I and ERC1967IBeaconProxy detection. if or( eq(keccak256(0x00, 0x52), ERC1967I_CODE_HASH), eq(keccak256(0x00, 0x57), ERC1967I_BEACON_PROXY_CODE_HASH) ) { pop(staticcall(gas(), instance, 0x00, 0x01, 0x00, 0x20)) result := mload(0x0c) break } } // 0age clone detection. result := mload(0x0b) codecopy(0x0b, codesize(), 0x14) // Zeroize the 20 bytes for the address. if iszero(xor(keccak256(0x00, 0x2c), CLONE_CODE_HASH)) { break } mstore(0x0b, result) // Restore the zeroized memory. // CWIA detection. result := mload(0x0a) codecopy(0x0a, codesize(), 0x14) // Zeroize the 20 bytes for the address. if iszero(xor(keccak256(0x00, 0x2d), CWIA_CODE_HASH)) { break } mstore(0x0a, result) // Restore the zeroized memory. // PUSH0 clone detection. result := mload(0x09) codecopy(0x09, codesize(), 0x14) // Zeroize the 20 bytes for the address. result := shr(xor(keccak256(0x00, 0x2d), PUSH0_CLONE_CODE_HASH), result) break } result := shr(96, result) mstore(0x37, 0) // Restore the overwritten part of the free memory pointer. } } /// @dev Returns the address when a contract with initialization code hash, /// `hash`, is deployed with `salt`, by `deployer`. /// Note: The returned result has dirty upper 96 bits. Please clean if used in assembly. function predictDeterministicAddress(bytes32 hash, bytes32 salt, address deployer) internal pure returns (address predicted) { /// @solidity memory-safe-assembly assembly { // Compute and store the bytecode hash. mstore8(0x00, 0xff) // Write the prefix. mstore(0x35, hash) mstore(0x01, shl(96, deployer)) mstore(0x15, salt) predicted := keccak256(0x00, 0x55) mstore(0x35, 0) // Restore the overwritten part of the free memory pointer. } } /// @dev Requires that `salt` starts with either the zero address or `by`. function checkStartsWith(bytes32 salt, address by) internal pure { /// @solidity memory-safe-assembly assembly { // If the salt does not start with the zero address or `by`. if iszero(or(iszero(shr(96, salt)), eq(shr(96, shl(96, by)), shr(96, salt)))) { mstore(0x00, 0x0c4549ef) // `SaltDoesNotStartWith()`. revert(0x1c, 0x04) } } } /// @dev Returns the `bytes32` at `offset` in `args`, without any bounds checks. /// To load an address, you can use `address(bytes20(argLoad(args, offset)))`. function argLoad(bytes memory args, uint256 offset) internal pure returns (bytes32 result) { /// @solidity memory-safe-assembly assembly { result := mload(add(add(args, 0x20), offset)) } } }
// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.26; import { PausableUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/PausableUpgradeable.sol"; import { ERC20 } from "solady/src/tokens/ERC20.sol"; import { ERC4626 } from "solady/src/tokens/ERC4626.sol"; import { SafeTransferLib } from "solady/src/utils/SafeTransferLib.sol"; import { Utils } from "../libraries/Utils.sol"; import { IHoneyErrors } from "./IHoneyErrors.sol"; /// @notice This is the ERC4626 vault for the collateral assets to mint Honey. /// @author Berachain Team contract CollateralVault is ERC4626, PausableUpgradeable, IHoneyErrors { using Utils for bytes4; event CustodyInfoSet(bool indexed isCustodyVault, address indexed custodyAddress); ERC20 private _vaultAsset; string private _name; string private _symbol; /// @notice The address of the honey factory that created this vault. address public factory; /// @notice The custody info for the vault. CustodyInfo public custodyInfo; /// @notice The custody info struct. /// @param isCustodyVault Whether the vault is a custody vault. /// @param custodyAddress The address of the custody. 0 in case of no custody. struct CustodyInfo { bool isCustodyVault; address custodyAddress; } /// @custom:oz-upgrades-unsafe-allow constructor constructor() { _disableInitializers(); } function initialize(address asset_, address _factory) external initializer { __Pausable_init(); __CollateralVault_init(asset_, _factory); } function __CollateralVault_init(address asset_, address _factory) internal onlyInitializing { // Check for zero addresses. // No need to check for zero asset address, // _asset.name(), _asset.symbol() will revert with `EvmError` if asset is zero address. if (_factory == address(0)) ZeroAddress.selector.revertWith(); factory = _factory; ERC20 _asset = ERC20(asset_); _vaultAsset = _asset; _name = string.concat(_asset.name(), "Vault"); _symbol = string.concat(_asset.symbol(), "Vault"); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* MODIFIERS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @dev Throws if called by any account other than the factory. modifier onlyFactory() { _checkFactory(); _; } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* ADMIN FUNCTIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @notice Pauses the vault. /// @dev Only the factory can call this function. function pause() external onlyFactory { _pause(); } /// @notice Unpauses the vault. /// @dev Only the factory can call this function. function unpause() external onlyFactory { _unpause(); } /** * @dev A wrapper to ERC4626.deposit that only VaultAdmin can call. * @dev It is a protection against inflation attacks, * @dev in which only VaultAdmin can mint/burn shares. * @param assets The assets to deposit into the vault to receive shares. * @param receiver The address that will receive the shares. * @return shares The shares minted to deposit the assets. */ function deposit(uint256 assets, address receiver) public override onlyFactory whenNotPaused returns (uint256) { return super.deposit(assets, receiver); } /** * @dev A wrapper to ERC4626.mint that only VaultAdmin can call. * @dev It is a protection against inflation attacks, * @dev in which only VaultAdmin can mint/burn shares. * @param shares The exact shares to mint by depositing the assets. * @param receiver The address that will receive the shares. * @return assets The assets required to mint the shares. */ function mint(uint256 shares, address receiver) public override onlyFactory whenNotPaused returns (uint256) { return super.mint(shares, receiver); } /** * @notice Set the custody info for the vault. * @dev Only the factory can call this function. * @dev To change the custody vault, the factory must first remove the existing custody vault. * @param _isCustodyVault Whether the vault is a custody vault. * @param _custodyAddress The address of the custody. */ function setCustodyInfo(bool _isCustodyVault, address _custodyAddress) external onlyFactory { if (_isCustodyVault && _custodyAddress == address(0)) ZeroAddress.selector.revertWith(); CustodyInfo memory _custodyInfo = custodyInfo; // set custody info for new custody vault if (_isCustodyVault && _custodyInfo.custodyAddress == address(0)) { SafeTransferLib.safeTransfer(asset(), _custodyAddress, ERC20(asset()).balanceOf(address(this))); custodyInfo = CustodyInfo(_isCustodyVault, _custodyAddress); emit CustodyInfoSet(_isCustodyVault, _custodyAddress); return; } // allow to remove custody feature for existing custody vault if (!_isCustodyVault && _custodyInfo.custodyAddress == _custodyAddress) { SafeTransferLib.safeTransferFrom( asset(), _custodyAddress, address(this), ERC20(asset()).balanceOf(_custodyAddress) ); custodyInfo = CustodyInfo(_isCustodyVault, address(0)); emit CustodyInfoSet(_isCustodyVault, address(0)); return; } // revert if any other combination of inputs else { InvalidCustodyInfoInput.selector.revertWith(); } } /** * @dev A wrapper to ERC4626.withdraw that only VaultAdmin can call. * @dev It is a protection against inflation attacks, * @dev in which only VaultAdmin can mint/burn shares. * @param assets The exact assets to withdraw from the vault. * @param receiver The address that will receive the assets. * @param owner The address that will burn the shares. * @return shares The shares burned to withdraw the assets. */ function withdraw( uint256 assets, address receiver, address owner ) public override onlyFactory whenNotPaused returns (uint256) { return super.withdraw(assets, receiver, owner); } /** * @dev A wrapper to ERC4626.redeem that only VaultAdmin can call. * @dev It is a protection against inflation attacks, * @dev in which only VaultAdmin can mint/burn shares. * @param shares The shares to redeem for the assets. * @param receiver The address that will receive the assets. * @param owner The address that will burn the shares. * @return assets The assets redeemed from the vault. */ function redeem( uint256 shares, address receiver, address owner ) public override onlyFactory whenNotPaused returns (uint256) { return super.redeem(shares, receiver, owner); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* GETTERS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ function name() public view override returns (string memory) { return _name; } function symbol() public view override returns (string memory) { return _symbol; } function asset() public view virtual override returns (address) { return address(_vaultAsset); } function convertToAssets(uint256 shares) public view override returns (uint256) { return _convertToAssets(shares); } function convertToShares(uint256 assets) public view override returns (uint256) { return _convertToShares(assets); } /** * @dev An implementation of ERC4626.totalAssets to avoid inflation attacks, * @dev which returns the total assets that VaultAdmin transferred to the vault. */ function totalAssets() public view override returns (uint256) { // This is another layer of protection against inflation attacks. // ERC4626 uses this function to calculate the exchange rate. // Because only VaultAdmin can mint/burn shares via deposit/mint, // the total assets transferred by VaultAdmin into this vault // must be always equal to the total supply of the shares. // Therefore, the assets/shares exchange rate is always 1. // Attackers or users can transfer assets directly into this vault // (thus the total assets is always greater than or equal the total supply), // but that will not change the total assets returned by this // function, thus the exchange rate is not changed. // // We also need to consider the difference in decimals // between the vault and the asset to ensure that // 10^assetDecimals asset_wei ~ 10^vaultDecimals vault_wei. // asset_wei_totalAssets ~ vault_wei_totalSupply / 10**(vaultDecimals - assetDecimals) return _convertToAssets(totalSupply()); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* INTERNAL FUNCTIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ /// @inheritdoc ERC4626 function _initialConvertToShares(uint256 assets) internal view override returns (uint256 shares) { return _convertToShares(assets); } /// @inheritdoc ERC4626 function _initialConvertToAssets(uint256 shares) internal view override returns (uint256 assets) { return _convertToAssets(shares); } /** * @dev Convert the assets to shares with decimals in consideration, * but out of the ERC4626's logic to enforce the fixed exchange rate * between Honey, shares, and assets. * @param assets The assets to convert to shares. * @return shares The shares converted from the assets. */ function _convertToShares(uint256 assets) private view returns (uint256 shares) { uint8 vaultDecimals = decimals(); uint8 assetDecimals = _vaultAsset.decimals(); uint256 exponent; if (vaultDecimals >= assetDecimals) { unchecked { exponent = vaultDecimals - assetDecimals; } return assets * (10 ** exponent); } unchecked { exponent = assetDecimals - vaultDecimals; } return assets / (10 ** exponent); } /** * @dev Convert the assets to shares with decimals in consideration, * but out of the ERC4626's logic to enforce the fixed exchange rate * between Honey, shares, and assets. * @param shares The shares to convert to assets. * @return assets The assets converted from the shares. */ function _convertToAssets(uint256 shares) private view returns (uint256 assets) { uint8 vaultDecimals = decimals(); uint8 assetDecimals = _vaultAsset.decimals(); uint256 exponent; if (vaultDecimals >= assetDecimals) { unchecked { exponent = vaultDecimals - assetDecimals; } return shares / (10 ** exponent); } unchecked { exponent = assetDecimals - vaultDecimals; } return shares * (10 ** exponent); } /// @inheritdoc ERC4626 function _useVirtualShares() internal pure override returns (bool) { return false; } /// @inheritdoc PausableUpgradeable function _requireNotPaused() internal view override { if (paused()) VaultPaused.selector.revertWith(asset()); } function _checkFactory() internal view { if (msg.sender != factory) { NotFactory.selector.revertWith(); } } /// @dev This function is called after the deposit is made. /// @dev If the vault is a custody vault, it will transfer the assets to the custody. function _afterDeposit(uint256 assets, uint256) internal override { if (custodyInfo.isCustodyVault) { SafeTransferLib.safeTransfer(asset(), custodyInfo.custodyAddress, assets); } } /// @dev This function is called before the withdraw is made. /// @dev If the vault is a custody vault, it will pull the assets from the custody. function _beforeWithdraw(uint256 assets, uint256) internal override { if (custodyInfo.isCustodyVault) { SafeTransferLib.safeTransferFrom(asset(), custodyInfo.custodyAddress, address(this), assets); } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (access/IAccessControl.sol) pragma solidity ^0.8.20; /** * @dev External interface of AccessControl declared to support ERC-165 detection. */ interface IAccessControl { /** * @dev The `account` is missing a role. */ error AccessControlUnauthorizedAccount(address account, bytes32 neededRole); /** * @dev The caller of a function is not the expected one. * * NOTE: Don't confuse with {AccessControlUnauthorizedAccount}. */ error AccessControlBadConfirmation(); /** * @dev Emitted when `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole` * * `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite * {RoleAdminChanged} not being emitted signaling this. */ event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole); /** * @dev Emitted when `account` is granted `role`. * * `sender` is the account that originated the contract call. This account bears the admin role (for the granted role). * Expected in cases where the role was granted using the internal {AccessControl-_grantRole}. */ event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender); /** * @dev Emitted when `account` is revoked `role`. * * `sender` is the account that originated the contract call: * - if using `revokeRole`, it is the admin role bearer * - if using `renounceRole`, it is the role bearer (i.e. `account`) */ event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender); /** * @dev Returns `true` if `account` has been granted `role`. */ function hasRole(bytes32 role, address account) external view returns (bool); /** * @dev Returns the admin role that controls `role`. See {grantRole} and * {revokeRole}. * * To change a role's admin, use {AccessControl-_setRoleAdmin}. */ function getRoleAdmin(bytes32 role) external view returns (bytes32); /** * @dev Grants `role` to `account`. * * If `account` had not been already granted `role`, emits a {RoleGranted} * event. * * Requirements: * * - the caller must have ``role``'s admin role. */ function grantRole(bytes32 role, address account) external; /** * @dev Revokes `role` from `account`. * * If `account` had been granted `role`, emits a {RoleRevoked} event. * * Requirements: * * - the caller must have ``role``'s admin role. */ function revokeRole(bytes32 role, address account) external; /** * @dev Revokes `role` from the calling account. * * Roles are often managed via {grantRole} and {revokeRole}: this function's * purpose is to provide a mechanism for accounts to lose their privileges * if they are compromised (such as when a trusted device is misplaced). * * If the calling account had been granted `role`, emits a {RoleRevoked} * event. * * Requirements: * * - the caller must be `callerConfirmation`. */ function renounceRole(bytes32 role, address callerConfirmation) external; }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol) pragma solidity ^0.8.20; import {Initializable} from "../proxy/utils/Initializable.sol"; /** * @dev Provides information about the current execution context, including the * sender of the transaction and its data. While these are generally available * via msg.sender and msg.data, they should not be accessed in such a direct * manner, since when dealing with meta-transactions the account sending and * paying for execution may not be the actual sender (as far as an application * is concerned). * * This contract is only required for intermediate, library-like contracts. */ abstract contract ContextUpgradeable is Initializable { function __Context_init() internal onlyInitializing { } function __Context_init_unchained() internal onlyInitializing { } function _msgSender() internal view virtual returns (address) { return msg.sender; } function _msgData() internal view virtual returns (bytes calldata) { return msg.data; } function _contextSuffixLength() internal view virtual returns (uint256) { return 0; } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (utils/introspection/ERC165.sol) pragma solidity ^0.8.20; import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol"; import {Initializable} from "../../proxy/utils/Initializable.sol"; /** * @dev Implementation of the {IERC165} interface. * * Contracts that want to implement ERC-165 should inherit from this contract and override {supportsInterface} to check * for the additional interface id that will be supported. For example: * * ```solidity * function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) { * return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId); * } * ``` */ abstract contract ERC165Upgradeable is Initializable, IERC165 { function __ERC165_init() internal onlyInitializing { } function __ERC165_init_unchained() internal onlyInitializing { } /** * @dev See {IERC165-supportsInterface}. */ function supportsInterface(bytes4 interfaceId) public view virtual returns (bool) { return interfaceId == type(IERC165).interfaceId; } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (proxy/utils/Initializable.sol) pragma solidity ^0.8.20; /** * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed * behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect. * * The initialization functions use a version number. Once a version number is used, it is consumed and cannot be * reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in * case an upgrade adds a module that needs to be initialized. * * For example: * * [.hljs-theme-light.nopadding] * ```solidity * contract MyToken is ERC20Upgradeable { * function initialize() initializer public { * __ERC20_init("MyToken", "MTK"); * } * } * * contract MyTokenV2 is MyToken, ERC20PermitUpgradeable { * function initializeV2() reinitializer(2) public { * __ERC20Permit_init("MyToken"); * } * } * ``` * * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}. * * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity. * * [CAUTION] * ==== * Avoid leaving a contract uninitialized. * * An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation * contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke * the {_disableInitializers} function in the constructor to automatically lock it when it is deployed: * * [.hljs-theme-light.nopadding] * ``` * /// @custom:oz-upgrades-unsafe-allow constructor * constructor() { * _disableInitializers(); * } * ``` * ==== */ abstract contract Initializable { /** * @dev Storage of the initializable contract. * * It's implemented on a custom ERC-7201 namespace to reduce the risk of storage collisions * when using with upgradeable contracts. * * @custom:storage-location erc7201:openzeppelin.storage.Initializable */ struct InitializableStorage { /** * @dev Indicates that the contract has been initialized. */ uint64 _initialized; /** * @dev Indicates that the contract is in the process of being initialized. */ bool _initializing; } // keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.Initializable")) - 1)) & ~bytes32(uint256(0xff)) bytes32 private constant INITIALIZABLE_STORAGE = 0xf0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00; /** * @dev The contract is already initialized. */ error InvalidInitialization(); /** * @dev The contract is not initializing. */ error NotInitializing(); /** * @dev Triggered when the contract has been initialized or reinitialized. */ event Initialized(uint64 version); /** * @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope, * `onlyInitializing` functions can be used to initialize parent contracts. * * Similar to `reinitializer(1)`, except that in the context of a constructor an `initializer` may be invoked any * number of times. This behavior in the constructor can be useful during testing and is not expected to be used in * production. * * Emits an {Initialized} event. */ modifier initializer() { // solhint-disable-next-line var-name-mixedcase InitializableStorage storage $ = _getInitializableStorage(); // Cache values to avoid duplicated sloads bool isTopLevelCall = !$._initializing; uint64 initialized = $._initialized; // Allowed calls: // - initialSetup: the contract is not in the initializing state and no previous version was // initialized // - construction: the contract is initialized at version 1 (no reininitialization) and the // current contract is just being deployed bool initialSetup = initialized == 0 && isTopLevelCall; bool construction = initialized == 1 && address(this).code.length == 0; if (!initialSetup && !construction) { revert InvalidInitialization(); } $._initialized = 1; if (isTopLevelCall) { $._initializing = true; } _; if (isTopLevelCall) { $._initializing = false; emit Initialized(1); } } /** * @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the * contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be * used to initialize parent contracts. * * A reinitializer may be used after the original initialization step. This is essential to configure modules that * are added through upgrades and that require initialization. * * When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer` * cannot be nested. If one is invoked in the context of another, execution will revert. * * Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in * a contract, executing them in the right order is up to the developer or operator. * * WARNING: Setting the version to 2**64 - 1 will prevent any future reinitialization. * * Emits an {Initialized} event. */ modifier reinitializer(uint64 version) { // solhint-disable-next-line var-name-mixedcase InitializableStorage storage $ = _getInitializableStorage(); if ($._initializing || $._initialized >= version) { revert InvalidInitialization(); } $._initialized = version; $._initializing = true; _; $._initializing = false; emit Initialized(version); } /** * @dev Modifier to protect an initialization function so that it can only be invoked by functions with the * {initializer} and {reinitializer} modifiers, directly or indirectly. */ modifier onlyInitializing() { _checkInitializing(); _; } /** * @dev Reverts if the contract is not in an initializing state. See {onlyInitializing}. */ function _checkInitializing() internal view virtual { if (!_isInitializing()) { revert NotInitializing(); } } /** * @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call. * Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized * to any version. 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 executed. */ function _disableInitializers() internal virtual { // solhint-disable-next-line var-name-mixedcase InitializableStorage storage $ = _getInitializableStorage(); if ($._initializing) { revert InvalidInitialization(); } if ($._initialized != type(uint64).max) { $._initialized = type(uint64).max; emit Initialized(type(uint64).max); } } /** * @dev Returns the highest version that has been initialized. See {reinitializer}. */ function _getInitializedVersion() internal view returns (uint64) { return _getInitializableStorage()._initialized; } /** * @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}. */ function _isInitializing() internal view returns (bool) { return _getInitializableStorage()._initializing; } /** * @dev Returns a pointer to the storage namespace. */ // solhint-disable-next-line var-name-mixedcase function _getInitializableStorage() private pure returns (InitializableStorage storage $) { assembly { $.slot := INITIALIZABLE_STORAGE } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (interfaces/draft-IERC1822.sol) pragma solidity ^0.8.20; /** * @dev ERC-1822: Universal Upgradeable Proxy Standard (UUPS) documents a method for upgradeability through a simplified * proxy whose upgrades are fully controlled by the current implementation. */ interface IERC1822Proxiable { /** * @dev Returns the storage slot that the proxiable contract assumes is being used to store the implementation * address. * * IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks * bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this * function revert if invoked through a proxy. */ function proxiableUUID() external view returns (bytes32); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (proxy/ERC1967/ERC1967Utils.sol) pragma solidity ^0.8.21; import {IBeacon} from "../beacon/IBeacon.sol"; import {IERC1967} from "../../interfaces/IERC1967.sol"; import {Address} from "../../utils/Address.sol"; import {StorageSlot} from "../../utils/StorageSlot.sol"; /** * @dev This library provides getters and event emitting update functions for * https://eips.ethereum.org/EIPS/eip-1967[ERC-1967] slots. */ library ERC1967Utils { /** * @dev Storage slot with the address of the current implementation. * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1. */ // solhint-disable-next-line private-vars-leading-underscore bytes32 internal constant IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc; /** * @dev The `implementation` of the proxy is invalid. */ error ERC1967InvalidImplementation(address implementation); /** * @dev The `admin` of the proxy is invalid. */ error ERC1967InvalidAdmin(address admin); /** * @dev The `beacon` of the proxy is invalid. */ error ERC1967InvalidBeacon(address beacon); /** * @dev An upgrade function sees `msg.value > 0` that may be lost. */ error ERC1967NonPayable(); /** * @dev Returns the current implementation address. */ function getImplementation() internal view returns (address) { return StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value; } /** * @dev Stores a new address in the ERC-1967 implementation slot. */ function _setImplementation(address newImplementation) private { if (newImplementation.code.length == 0) { revert ERC1967InvalidImplementation(newImplementation); } StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value = newImplementation; } /** * @dev Performs implementation upgrade with additional setup call if data is nonempty. * This function is payable only if the setup call is performed, otherwise `msg.value` is rejected * to avoid stuck value in the contract. * * Emits an {IERC1967-Upgraded} event. */ function upgradeToAndCall(address newImplementation, bytes memory data) internal { _setImplementation(newImplementation); emit IERC1967.Upgraded(newImplementation); if (data.length > 0) { Address.functionDelegateCall(newImplementation, data); } else { _checkNonPayable(); } } /** * @dev Storage slot with the admin of the contract. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1. */ // solhint-disable-next-line private-vars-leading-underscore bytes32 internal constant ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103; /** * @dev Returns the current admin. * * TIP: To get this value clients can read directly from the storage slot shown below (specified by ERC-1967) using * the https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call. * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103` */ function getAdmin() internal view returns (address) { return StorageSlot.getAddressSlot(ADMIN_SLOT).value; } /** * @dev Stores a new address in the ERC-1967 admin slot. */ function _setAdmin(address newAdmin) private { if (newAdmin == address(0)) { revert ERC1967InvalidAdmin(address(0)); } StorageSlot.getAddressSlot(ADMIN_SLOT).value = newAdmin; } /** * @dev Changes the admin of the proxy. * * Emits an {IERC1967-AdminChanged} event. */ function changeAdmin(address newAdmin) internal { emit IERC1967.AdminChanged(getAdmin(), newAdmin); _setAdmin(newAdmin); } /** * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy. * This is the keccak-256 hash of "eip1967.proxy.beacon" subtracted by 1. */ // solhint-disable-next-line private-vars-leading-underscore bytes32 internal constant BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50; /** * @dev Returns the current beacon. */ function getBeacon() internal view returns (address) { return StorageSlot.getAddressSlot(BEACON_SLOT).value; } /** * @dev Stores a new beacon in the ERC-1967 beacon slot. */ function _setBeacon(address newBeacon) private { if (newBeacon.code.length == 0) { revert ERC1967InvalidBeacon(newBeacon); } StorageSlot.getAddressSlot(BEACON_SLOT).value = newBeacon; address beaconImplementation = IBeacon(newBeacon).implementation(); if (beaconImplementation.code.length == 0) { revert ERC1967InvalidImplementation(beaconImplementation); } } /** * @dev Change the beacon and trigger a setup call if data is nonempty. * This function is payable only if the setup call is performed, otherwise `msg.value` is rejected * to avoid stuck value in the contract. * * Emits an {IERC1967-BeaconUpgraded} event. * * CAUTION: Invoking this function has no effect on an instance of {BeaconProxy} since v5, since * it uses an immutable beacon without looking at the value of the ERC-1967 beacon slot for * efficiency. */ function upgradeBeaconToAndCall(address newBeacon, bytes memory data) internal { _setBeacon(newBeacon); emit IERC1967.BeaconUpgraded(newBeacon); if (data.length > 0) { Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data); } else { _checkNonPayable(); } } /** * @dev Reverts if `msg.value` is not zero. It can be used to avoid `msg.value` stuck in the contract * if an upgrade doesn't perform an initialization call. */ function _checkNonPayable() private { if (msg.value > 0) { revert ERC1967NonPayable(); } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (utils/introspection/IERC165.sol) pragma solidity ^0.8.20; /** * @dev Interface of the ERC-165 standard, as defined in the * https://eips.ethereum.org/EIPS/eip-165[ERC]. * * Implementers can declare support of contract interfaces, which can then be * queried by others ({ERC165Checker}). * * For an implementation, see {ERC165}. */ interface IERC165 { /** * @dev Returns true if this contract implements the interface defined by * `interfaceId`. See the corresponding * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[ERC section] * to learn more about how these ids are created. * * This function call must use less than 30 000 gas. */ function supportsInterface(bytes4 interfaceId) external view returns (bool); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (proxy/beacon/IBeacon.sol) pragma solidity ^0.8.20; /** * @dev This is the interface that {BeaconProxy} expects of its beacon. */ interface IBeacon { /** * @dev Must return an address that can be used as a delegate call target. * * {UpgradeableBeacon} will check that this address is a contract. */ function implementation() external view returns (address); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC1967.sol) pragma solidity ^0.8.20; /** * @dev ERC-1967: Proxy Storage Slots. This interface contains the events defined in the ERC. */ interface IERC1967 { /** * @dev Emitted when the implementation is upgraded. */ event Upgraded(address indexed implementation); /** * @dev Emitted when the admin account has changed. */ event AdminChanged(address previousAdmin, address newAdmin); /** * @dev Emitted when the beacon is changed. */ event BeaconUpgraded(address indexed beacon); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (utils/Address.sol) pragma solidity ^0.8.20; import {Errors} from "./Errors.sol"; /** * @dev Collection of functions related to the address type */ library Address { /** * @dev There's no code at `target` (it is not a contract). */ error AddressEmptyCode(address target); /** * @dev Replacement for Solidity's `transfer`: sends `amount` wei to * `recipient`, forwarding all available gas and reverting on errors. * * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost * of certain opcodes, possibly making contracts go over the 2300 gas limit * imposed by `transfer`, making them unable to receive funds via * `transfer`. {sendValue} removes this limitation. * * https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more]. * * IMPORTANT: because control is transferred to `recipient`, care must be * taken to not create reentrancy vulnerabilities. Consider using * {ReentrancyGuard} or the * https://solidity.readthedocs.io/en/v0.8.20/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern]. */ function sendValue(address payable recipient, uint256 amount) internal { if (address(this).balance < amount) { revert Errors.InsufficientBalance(address(this).balance, amount); } (bool success, ) = recipient.call{value: amount}(""); if (!success) { revert Errors.FailedCall(); } } /** * @dev Performs a Solidity function call using a low level `call`. A * plain `call` is an unsafe replacement for a function call: use this * function instead. * * If `target` reverts with a revert reason or custom error, it is bubbled * up by this function (like regular Solidity function calls). However, if * the call reverted with no returned reason, this function reverts with a * {Errors.FailedCall} error. * * Returns the raw returned data. To convert to the expected return value, * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`]. * * Requirements: * * - `target` must be a contract. * - calling `target` with `data` must not revert. */ function functionCall(address target, bytes memory data) internal returns (bytes memory) { return functionCallWithValue(target, data, 0); } /** * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], * but also transferring `value` wei to `target`. * * Requirements: * * - the calling contract must have an ETH balance of at least `value`. * - the called Solidity function must be `payable`. */ function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) { if (address(this).balance < value) { revert Errors.InsufficientBalance(address(this).balance, value); } (bool success, bytes memory returndata) = target.call{value: value}(data); return verifyCallResultFromTarget(target, success, returndata); } /** * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], * but performing a static call. */ function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) { (bool success, bytes memory returndata) = target.staticcall(data); return verifyCallResultFromTarget(target, success, returndata); } /** * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], * but performing a delegate call. */ function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) { (bool success, bytes memory returndata) = target.delegatecall(data); return verifyCallResultFromTarget(target, success, returndata); } /** * @dev Tool to verify that a low level call to smart-contract was successful, and reverts if the target * was not a contract or bubbling up the revert reason (falling back to {Errors.FailedCall}) in case * of an unsuccessful call. */ function verifyCallResultFromTarget( address target, bool success, bytes memory returndata ) internal view returns (bytes memory) { if (!success) { _revert(returndata); } else { // only check if target is a contract if the call was successful and the return data is empty // otherwise we already know that it was a contract if (returndata.length == 0 && target.code.length == 0) { revert AddressEmptyCode(target); } return returndata; } } /** * @dev Tool to verify that a low level call was successful, and reverts if it wasn't, either by bubbling the * revert reason or with a default {Errors.FailedCall} error. */ function verifyCallResult(bool success, bytes memory returndata) internal pure returns (bytes memory) { if (!success) { _revert(returndata); } else { return returndata; } } /** * @dev Reverts with returndata if present. Otherwise reverts with {Errors.FailedCall}. */ function _revert(bytes memory returndata) private pure { // Look for revert reason and bubble it up if present if (returndata.length > 0) { // The easiest way to bubble the revert reason is using memory via assembly assembly ("memory-safe") { let returndata_size := mload(returndata) revert(add(32, returndata), returndata_size) } } else { revert Errors.FailedCall(); } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (utils/StorageSlot.sol) // This file was procedurally generated from scripts/generate/templates/StorageSlot.js. pragma solidity ^0.8.20; /** * @dev Library for reading and writing primitive types to specific storage slots. * * Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts. * This library helps with reading and writing to such slots without the need for inline assembly. * * The functions in this library return Slot structs that contain a `value` member that can be used to read or write. * * Example usage to set ERC-1967 implementation slot: * ```solidity * contract ERC1967 { * // Define the slot. Alternatively, use the SlotDerivation library to derive the slot. * bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc; * * function _getImplementation() internal view returns (address) { * return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value; * } * * function _setImplementation(address newImplementation) internal { * require(newImplementation.code.length > 0); * StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation; * } * } * ``` * * TIP: Consider using this library along with {SlotDerivation}. */ library StorageSlot { struct AddressSlot { address value; } struct BooleanSlot { bool value; } struct Bytes32Slot { bytes32 value; } struct Uint256Slot { uint256 value; } struct Int256Slot { int256 value; } struct StringSlot { string value; } struct BytesSlot { bytes value; } /** * @dev Returns an `AddressSlot` with member `value` located at `slot`. */ function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) { assembly ("memory-safe") { r.slot := slot } } /** * @dev Returns a `BooleanSlot` with member `value` located at `slot`. */ function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) { assembly ("memory-safe") { r.slot := slot } } /** * @dev Returns a `Bytes32Slot` with member `value` located at `slot`. */ function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) { assembly ("memory-safe") { r.slot := slot } } /** * @dev Returns a `Uint256Slot` with member `value` located at `slot`. */ function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) { assembly ("memory-safe") { r.slot := slot } } /** * @dev Returns a `Int256Slot` with member `value` located at `slot`. */ function getInt256Slot(bytes32 slot) internal pure returns (Int256Slot storage r) { assembly ("memory-safe") { r.slot := slot } } /** * @dev Returns a `StringSlot` with member `value` located at `slot`. */ function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) { assembly ("memory-safe") { r.slot := slot } } /** * @dev Returns an `StringSlot` representation of the string storage pointer `store`. */ function getStringSlot(string storage store) internal pure returns (StringSlot storage r) { assembly ("memory-safe") { r.slot := store.slot } } /** * @dev Returns a `BytesSlot` with member `value` located at `slot`. */ function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) { assembly ("memory-safe") { r.slot := slot } } /** * @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`. */ function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) { assembly ("memory-safe") { r.slot := store.slot } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (utils/Errors.sol) pragma solidity ^0.8.20; /** * @dev Collection of common custom errors used in multiple contracts * * IMPORTANT: Backwards compatibility is not guaranteed in future versions of the library. * It is recommended to avoid relying on the error API for critical functionality. * * _Available since v5.1._ */ library Errors { /** * @dev The ETH balance of the account is not enough to perform the operation. */ error InsufficientBalance(uint256 balance, uint256 needed); /** * @dev A call to an address target failed. The target may have reverted. */ error FailedCall(); /** * @dev The deployment failed. */ error FailedDeployment(); /** * @dev A necessary precompile is missing. */ error MissingPrecompile(address); }
{ "remappings": [ "@openzeppelin/contracts/=node_modules/@openzeppelin/contracts/", "@openzeppelin/contracts-upgradeable/=node_modules/@openzeppelin/contracts-upgradeable/", "@openzeppelin-gov/=node_modules/@openzeppelin/contracts-upgradeable/governance/", "@openzeppelin-gov-ext/=node_modules/@openzeppelin/contracts-upgradeable/governance/extensions/", "@prb/math/=node_modules/@prb/math/", "@pythnetwork/=node_modules/@pythnetwork/pyth-sdk-solidity/", "forge-std/=lib/forge-std/src/", "solady/src/=lib/solady/src/", "solady/test/=lib/solady/test/", "transient-goodies/=lib/transient-goodies/src/", "@mock/=test/mock/", "@balancer-labs/=node_modules/@balancer-labs/", "ds-test/=lib/openzeppelin-foundry-upgrades/lib/solidity-stringutils/lib/ds-test/src/", "openzeppelin-foundry-upgrades/=lib/openzeppelin-foundry-upgrades/src/", "solidity-stringutils/=lib/openzeppelin-foundry-upgrades/lib/solidity-stringutils/" ], "optimizer": { "enabled": true, "runs": 666 }, "metadata": { "useLiteralContent": false, "bytecodeHash": "none", "appendCBOR": true }, "outputSelection": { "*": { "*": [ "evm.bytecode", "evm.deployedBytecode", "devdoc", "userdoc", "metadata", "abi" ] } }, "evmVersion": "cancun", "viaIR": true, "libraries": {} }
Contract Security Audit
- No Contract Security Audit Submitted- Submit Audit Here
Contract ABI
API[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"AccessControlBadConfirmation","type":"error"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"bytes32","name":"neededRole","type":"bytes32"}],"name":"AccessControlUnauthorizedAccount","type":"error"},{"inputs":[{"internalType":"address","name":"target","type":"address"}],"name":"AddressEmptyCode","type":"error"},{"inputs":[],"name":"AmountOutOfRange","type":"error"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"AssetIsBadCollateral","type":"error"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"AssetIsNotBadCollateral","type":"error"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"AssetNotRegistered","type":"error"},{"inputs":[],"name":"CapCanCauseDenialOfService","type":"error"},{"inputs":[{"internalType":"address","name":"implementation","type":"address"}],"name":"ERC1967InvalidImplementation","type":"error"},{"inputs":[],"name":"ERC1967NonPayable","type":"error"},{"inputs":[],"name":"EnforcedPause","type":"error"},{"inputs":[],"name":"ExceedGlobalCap","type":"error"},{"inputs":[],"name":"ExceedRelativeCap","type":"error"},{"inputs":[],"name":"ExpectedPause","type":"error"},{"inputs":[],"name":"FailedCall","type":"error"},{"inputs":[{"internalType":"uint256","name":"assets","type":"uint256"},{"internalType":"uint256","name":"shares","type":"uint256"}],"name":"InsufficientAssets","type":"error"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"InsufficientRecapitalizeAmount","type":"error"},{"inputs":[],"name":"InvalidCustodyInfoInput","type":"error"},{"inputs":[],"name":"InvalidInitialization","type":"error"},{"inputs":[],"name":"LiquidationDisabled","type":"error"},{"inputs":[],"name":"LiquidationWithReferenceCollateral","type":"error"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"expectedOwner","type":"address"}],"name":"MismatchedOwner","type":"error"},{"inputs":[],"name":"NotFactory","type":"error"},{"inputs":[],"name":"NotInitializing","type":"error"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"NotPegged","type":"error"},{"inputs":[{"internalType":"uint256","name":"rate","type":"uint256"}],"name":"OverOneHundredPercentRate","type":"error"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"RecapitalizeNotNeeded","type":"error"},{"inputs":[],"name":"UUPSUnauthorizedCallContext","type":"error"},{"inputs":[{"internalType":"bytes32","name":"slot","type":"bytes32"}],"name":"UUPSUnsupportedProxiableUUID","type":"error"},{"inputs":[{"internalType":"address","name":"caller","type":"address"},{"internalType":"address","name":"expectedCaller","type":"address"}],"name":"UnauthorizedCaller","type":"error"},{"inputs":[{"internalType":"uint256","name":"rate","type":"uint256"}],"name":"UnderNinetyEightPercentRate","type":"error"},{"inputs":[],"name":"UnexpectedBasketModeStatus","type":"error"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"VaultAlreadyRegistered","type":"error"},{"inputs":[],"name":"VaultPaused","type":"error"},{"inputs":[],"name":"ZeroAddress","type":"error"},{"inputs":[],"name":"ZeroAmount","type":"error"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"ZeroWeight","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bool","name":"forced","type":"bool"}],"name":"BasketModeForced","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"bool","name":"isBadCollateral","type":"bool"}],"name":"CollateralAssetStatusSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"asset","type":"address"},{"indexed":true,"internalType":"address","name":"receiver","type":"address"},{"indexed":false,"internalType":"uint256","name":"shares","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"assets","type":"uint256"}],"name":"CollectedFeeWithdrawn","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"uint256","name":"lower","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"upper","type":"uint256"}],"name":"DepegOffsetsSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"feeReceiver","type":"address"}],"name":"FeeReceiverSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"globalCap","type":"uint256"}],"name":"GlobalCapSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"uint256","name":"assetAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"mintAmount","type":"uint256"}],"name":"HoneyMinted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"uint256","name":"assetAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"redeemAmount","type":"uint256"}],"name":"HoneyRedeemed","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint64","name":"version","type":"uint64"}],"name":"Initialized","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"badAsset","type":"address"},{"indexed":false,"internalType":"address","name":"goodAsset","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"address","name":"sender","type":"address"}],"name":"Liquidated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"uint256","name":"rate","type":"uint256"}],"name":"LiquidationRateSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bool","name":"enabled","type":"bool"}],"name":"LiquidationStatusSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"maxFeedDelay","type":"uint256"}],"name":"MaxFeedDelaySet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"minShareAmount","type":"uint256"}],"name":"MinSharesToRecapitalizeSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"uint256","name":"rate","type":"uint256"}],"name":"MintRateSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"rate","type":"uint256"}],"name":"POLFeeCollectorFeeRateSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"polFeeCollector","type":"address"}],"name":"POLFeeCollectorSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Paused","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"bytes32","name":"id","type":"bytes32"}],"name":"PriceFeedChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"oracle","type":"address"}],"name":"PriceOracleSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"uint256","name":"target","type":"uint256"}],"name":"RecapitalizeBalanceThresholdSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"address","name":"sender","type":"address"}],"name":"Recapitalized","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"uint256","name":"rate","type":"uint256"}],"name":"RedeemRateSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"old","type":"address"},{"indexed":false,"internalType":"address","name":"asset","type":"address"}],"name":"ReferenceCollateralSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"uint256","name":"relativeCap","type":"uint256"}],"name":"RelativeCapSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"previousAdminRole","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"newAdminRole","type":"bytes32"}],"name":"RoleAdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleGranted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleRevoked","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Unpaused","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"implementation","type":"address"}],"name":"Upgraded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"vault","type":"address"},{"indexed":true,"internalType":"address","name":"asset","type":"address"}],"name":"VaultCreated","type":"event"},{"inputs":[],"name":"DEFAULT_ADMIN_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"MANAGER_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"PAUSER_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"UPGRADE_INTERFACE_VERSION","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"beacon","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"},{"internalType":"address","name":"asset","type":"address"}],"name":"collectedFees","outputs":[{"internalType":"uint256","name":"collectedFee","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"createVault","outputs":[{"internalType":"contract ERC4626","name":"vault","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"feeReceiver","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"forcedBasketMode","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"}],"name":"getRoleAdmin","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getWeights","outputs":[{"internalType":"uint256[]","name":"w","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"globalCap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"grantRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"hasRole","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"honey","outputs":[{"internalType":"contract Honey","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_governance","type":"address"},{"internalType":"address","name":"_honey","type":"address"},{"internalType":"address","name":"_polFeeCollector","type":"address"},{"internalType":"address","name":"_feeReceiver","type":"address"},{"internalType":"address","name":"_priceOracle","type":"address"},{"internalType":"address","name":"_beacon","type":"address"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"isBadCollateralAsset","outputs":[{"internalType":"bool","name":"badCollateral","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bool","name":"isMint","type":"bool"}],"name":"isBasketModeEnabled","outputs":[{"internalType":"bool","name":"basketMode","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"isPegged","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"badCollateral","type":"address"},{"internalType":"address","name":"goodCollateral","type":"address"},{"internalType":"uint256","name":"goodAmount","type":"uint256"}],"name":"liquidate","outputs":[{"internalType":"uint256","name":"badAmount","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"liquidationEnabled","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"minSharesToRecapitalize","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"receiver","type":"address"},{"internalType":"bool","name":"expectBasketMode","type":"bool"}],"name":"mint","outputs":[{"internalType":"uint256","name":"honeyToMint","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"mintRates","outputs":[{"internalType":"uint256","name":"rate","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"numRegisteredAssets","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"pauseVault","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"paused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"polFeeCollector","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"polFeeCollectorFeeRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"priceFeedMaxDelay","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"priceOracle","outputs":[{"internalType":"contract IPriceOracle","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"proxiableUUID","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"recapitalize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"recapitalizeBalanceThreshold","outputs":[{"internalType":"uint256","name":"targetBalance","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"},{"internalType":"uint256","name":"honeyAmount","type":"uint256"},{"internalType":"address","name":"receiver","type":"address"},{"internalType":"bool","name":"expectBasketMode","type":"bool"}],"name":"redeem","outputs":[{"internalType":"uint256[]","name":"redeemed","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"redeemRates","outputs":[{"internalType":"uint256","name":"rate","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"referenceCollateral","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"registeredAssets","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"relativeCap","outputs":[{"internalType":"uint256","name":"limit","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"callerConfirmation","type":"address"}],"name":"renounceRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"revokeRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"},{"internalType":"bool","name":"_isBadCollateral","type":"bool"}],"name":"setCollateralAssetStatus","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"},{"internalType":"bool","name":"isCustodyVault","type":"bool"},{"internalType":"address","name":"custodyAddress","type":"address"}],"name":"setCustodyInfo","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"},{"internalType":"uint256","name":"lowerOffset","type":"uint256"},{"internalType":"uint256","name":"upperOffset","type":"uint256"}],"name":"setDepegOffsets","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_feeReceiver","type":"address"}],"name":"setFeeReceiver","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bool","name":"forced","type":"bool"}],"name":"setForcedBasketMode","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"limit","type":"uint256"}],"name":"setGlobalCap","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bool","name":"enabled","type":"bool"}],"name":"setLiquidationEnabled","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"},{"internalType":"uint256","name":"extraRate","type":"uint256"}],"name":"setLiquidationRate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"maxTolerance","type":"uint256"}],"name":"setMaxFeedDelay","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"minSharesAmount","type":"uint256"}],"name":"setMinSharesToRecapitalize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"},{"internalType":"uint256","name":"mintRate","type":"uint256"}],"name":"setMintRate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_polFeeCollector","type":"address"}],"name":"setPOLFeeCollector","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_polFeeCollectorFeeRate","type":"uint256"}],"name":"setPOLFeeCollectorFeeRate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"priceOracle_","type":"address"}],"name":"setPriceOracle","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"},{"internalType":"uint256","name":"target","type":"uint256"}],"name":"setRecapitalizeBalanceThreshold","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"},{"internalType":"uint256","name":"redeemRate","type":"uint256"}],"name":"setRedeemRate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"setReferenceCollateral","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"},{"internalType":"uint256","name":"limit","type":"uint256"}],"name":"setRelativeCap","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"unpause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"unpauseVault","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":"address","name":"asset","type":"address"}],"name":"vaults","outputs":[{"internalType":"contract CollateralVault","name":"vault","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"}],"name":"withdrawAllFees","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"},{"internalType":"address","name":"receiver","type":"address"}],"name":"withdrawFee","outputs":[{"internalType":"uint256","name":"assets","type":"uint256"}],"stateMutability":"nonpayable","type":"function"}]
Contract Creation Code
60a080604052346100e857306080527ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005460ff8160401c166100d9576002600160401b03196001600160401b03821601610073575b6040516144af90816100ed8239608051818181611a810152611bcc0152f35b6001600160401b0319166001600160401b039081177ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005581527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d290602090a15f80610054565b63f92ee8a960e01b5f5260045ffd5b5f80fdfe60806040526004361015610011575f80fd5b5f803560e01c8062271801146128d057806301ffc9a71461287a5780630aa4f4cd146127f35780631224b28c1461273b5780631e17e57c146126fe5780631e704c63146126d757806322acb867146126bb578063248a9ca3146126835780632630c12f1461265957806326c01303146123a45780632cfb0e101461236c5780632efbab2c146122f65780632f2ff15d146122ab578063328ebaf71461200a57806336568abe14611fc557806336b2c4b214611f9e5780633f4ba83a14611f1e5780634af471be14611e935780634c17904214611e3c5780634c97f76614611dee5780634f1ef28614611b50578063503b9e5914611ae057806352d1902d14611a66578063530e784f146119af57806355c7aaff1461199157806359659e901461196b5780635c975abb1461193c5780635f0ccdc2146118d65780636fe7f983146118765780637b34b5d8146118535780638456cb59146117e057806391d148541461178957806393df00181461175157806399a2af75146117335780639ee61d5614611715578063a083bd3c146116d2578063a217fddf146116b6578063a3bc238514611635578063a622ee7c146115fb578063a71f6f5f1461154d578063ab6636ec146114e7578063aba1f54114611477578063abab398a146113ee578063ad1567c7146113b6578063ad3cb1cc1461135a578063ae574a9014611334578063b3f006741461130d578063b4bd6f4614611004578063bb85d15b14610fe6578063bc7c290214610fc2578063bdb912f314610f8a578063c0bd6f9e14610ef5578063c1a7b2af14610e6e578063c879c6d814610e30578063cc2a9a5b14610968578063d547741f14610914578063de4bc640146108e6578063e0cda5da146105fb578063e2a0253614610581578063e63ab1e914610546578063ebaa654514610528578063ec87621c146104ed578063ed596315146104b7578063efdcd9741461044d578063f0f15132146103d4578063f68f0c69146103ad5763f91d65af146102fa575f80fd5b346103aa5760403660031901126103aa57610313612993565b6024359061031f6130a8565b670de0b6b3a7640000821161039657670d99a8cec7e2000082106103825760206001600160a01b037f2efe6880df7ccb94a2aa093e54fd1f13f69ac2d50436d6a558a7c639697d1ea3921692838552603b8252806040862055604051908152a280f35b63071fba9d60e41b83526004829052602483fd5b6356d2368b60e11b83526004829052602483fd5b80fd5b50346103aa57806003193601126103aa5760206001600160a01b0360425416604051908152f35b50346103aa5760203660031901126103aa576001600160a01b036103f6612993565b6103fe613117565b16801561043e57806001600160a01b031960025416176002557fba4a603fbc52c5350922457bcd71f2287f1a67c8eabd5ec9a01f1dbd7ace4a7c8280a280f35b63d92e233d60e01b8252600482fd5b50346103aa5760203660031901126103aa576001600160a01b0361046f612993565b610477613117565b16801561043e57806001600160a01b031960015416176001557fbdf37c276f641820b141429d245add2552b4118c0866e5a78638e3de5ef18d9d8280a280f35b50346103aa576104e96104dd6104cc36612a2c565b926104d8929192613480565b612f14565b604051918291826129f3565b0390f35b50346103aa57806003193601126103aa5760206040517f241ecf16d79d0f8dbfb92cbc07fe17840425976cf0667f022fe9877caa831b088152f35b50346103aa57806003193601126103aa576020604154604051908152f35b50346103aa57806003193601126103aa5760206040517f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a8152f35b50346103aa5760403660031901126103aa577f2e04b29851c22c48f29b5bbcd5092f95d0907b7700d4aaa2341f95fc931e78706105bc612993565b602435906105c86130a8565b6001600160a01b031680845260436020908152604080862084905580519283529082019290925290819081015b0390a180f35b50346103aa5760403660031901126103aa57610615612993565b60243590610621613480565b819061062c81613222565b610635816134a7565b6001600160a01b038116928385526045602052604085205484865260046020526001600160a01b036040872054168587526007602052602060408820546024604051809481936303d1689d60e11b835260048301525afa80156108db5787906108a8575b600491508688528160205260206001600160a01b0360408a205416604051938480926278744560e21b82525afa801561089d578890610869575b6106dd9250612b43565b9181831015610855576106ef84612bac565b15610841576106ff829184612b36565b1161082e575b50508284526004602052602460206001600160a01b03604087205416604051928380926363737ac960e11b82528760048301525afa9081156108235785916107ed575b50604654116107d95761075b82826134d9565b506107658161378f565b156107ca576107738161388f565b156107bb57916060916107a67f1bb1edfe5e334d503d0dce0cd3185491ce674afad9ecfe52601c10c8c7c08d28946138fe565b6040519182526020820152336040820152a180f35b6336d5fb0f60e11b8452600484fd5b632595dbe760e01b8452600484fd5b6316ebc63f60e11b84526004829052602484fd5b90506020813d60201161081b575b8161080860209383612a73565b8101031261081757515f610748565b5f80fd5b3d91506107fb565b6040513d87823e3d90fd5b610839929350612b43565b905f80610705565b632201a6c360e01b87526004849052602487fd5b630872404f60e11b87526004849052602487fd5b506020823d602011610895575b8161088360209383612a73565b81010312610817576106dd91516106d3565b3d9150610876565b6040513d8a823e3d90fd5b506020813d6020116108d3575b816108c260209383612a73565b810103126108175760049051610699565b3d91506108b5565b6040513d89823e3d90fd5b50346103aa5760203660031901126103aa57602061090a6109056129e4565b612e04565b6040519015158152f35b50346103aa5760403660031901126103aa576109646004356109346129bf565b9061095f61095a825f525f80516020614463833981519152602052600160405f20015490565b6131d5565b613fe9565b5080f35b50346103aa5760c03660031901126103aa57610982612993565b61098a6129bf565b906109936129a9565b906064356001600160a01b038116809103610e2c57608435916001600160a01b03831691828403610e285760a435916001600160a01b038316809303610e24577ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00549560ff8760401c16159667ffffffffffffffff811680159081610e1c575b6001149081610e12575b159081610e09575b50610dfa5767ffffffffffffffff1981166001177ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005587610dbb575b50610a6a6143ca565b610a726143ca565b610a7a6143ca565b610a826143ca565b60ff195f8051602061448383398151915254165f8051602061448383398151915255610aac6143ca565b610ab46143ca565b6001600160a01b03821615610dac576001600160a01b0316928315610dac578215610dac578015610dac57885473ffffffffffffffffffffffffffffffffffffffff1916178855610b0490613c24565b50806001600160a01b03196001541617600155816001600160a01b031960025416176002557fbdf37c276f641820b141429d245add2552b4118c0866e5a78638e3de5ef18d9d8780a27fba4a603fbc52c5350922457bcd71f2287f1a67c8eabd5ec9a01f1dbd7ace4a7c8680a27f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a85525f805160206144638339815191526020526001600160a01b036001604087200154947f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a87525f805160206144638339815191526020527f241ecf16d79d0f8dbfb92cbc07fe17840425976cf0667f022fe9877caa831b0860016040892001557f241ecf16d79d0f8dbfb92cbc07fe17840425976cf0667f022fe9877caa831b08604051967f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a7fbd79b86ffe0ab8e8776151514217cd7cacd52c909f66475c3af44e129f0b00ff8a80a416908115610d9d5715610d8e576001600160a01b03196039541617603955670de0b6b3a7640000603a55600a604155670de0b6b3a76400006046557fffffffffffffffffffff000000000000000000000000000000000000000000ff75ffffffffffffffffffffffffffffffffffffffff000060405492670de0b6b3a764000060445560101b16911617604055610d12575080f35b60207fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d29168ff0000000000000000197ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a0054167ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005560018152a180f35b63d92e233d60e01b8552600485fd5b63d92e233d60e01b8652600486fd5b63d92e233d60e01b8952600489fd5b68ffffffffffffffffff191668010000000000000001177ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00555f610a61565b63f92ee8a960e01b8a5260048afd5b9050155f610a25565b303b159150610a1d565b899150610a13565b8780fd5b8680fd5b8480fd5b50346103aa5760403660031901126103aa576020610e66610e4f612993565b610e576129bf565b90610e6181613222565b614086565b604051908152f35b50346103aa5760403660031901126103aa57610e88612993565b7fd2f5128538548a810c4735697cdd8ea509b0d233102362e242063283126400b860206001600160a01b03610ebb6129d5565b93610ec46130a8565b610ecd81613222565b169283855260058252604085209015159060ff1981541660ff8316179055604051908152a280f35b50346103aa5760203660031901126103aa576001600160a01b03610f17612993565b610f1f613166565b610f2881613222565b1681526004602052806001600160a01b03604082205416803b15610f8757818091600460405180948193638456cb5960e01b83525af18015610f7c57610f6b5750f35b81610f7591612a73565b6103aa5780f35b6040513d84823e3d90fd5b50fd5b50346103aa5760203660031901126103aa5760406020916001600160a01b03610fb1612993565b168152604383522054604051908152f35b50346103aa5760203660031901126103aa57602061090a610fe1612993565b612bac565b50346103aa57806003193601126103aa576020600354604051908152f35b50346103aa5760203660031901126103aa5761101e612993565b9060035480156112ed575b611031613117565b6001600160a01b0383169081835260046020526001600160a01b036040842054166112d957680100000000000000008110156112c5578060016110779201600355612ac5565b81549060031b906001600160a01b0384831b921b191617905580825260208220926001600160a01b0383541693604051947fb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f36060527f1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6c6040527660195155f3363d3d373d3d363d602036600436635c60da6020527c60523d8160223d39730000000000000000000000000000000000000000176009526074600c85f580156112b8576001600160a01b039085604052846060521693843b156112b45763485cc95560e01b81526001600160a01b0382166004820152306024820152838160448183895af180156112a957908491611290575b50829052600460205260408320805473ffffffffffffffffffffffffffffffffffffffff19168517905581847f5d9c31ffa0fecffd7cf379989a3c7af252f0335e0d2a1320b55245912c781f538580a38183526043602052670de0b6b3a76400006040842055818352603b602052670ddeeff45500c0006040842055818352603c602052670ddeeff45500c0006040842055818352603d60205266470de4df8200006040842055818352603e60205266470de4df820000604084205561124e81612bac565b1561127e5760208466071afd498d000060408686808252603d865283838320558152603e85522055604051908152f35b632201a6c360e01b8352600452602482fd5b8161129a91612a73565b6112a557825f611189565b8280fd5b6040513d86823e3d90fd5b8380fd5b633011642584526004601cfd5b634e487b7160e01b83526041600452602483fd5b631c5fe60b60e11b83526004849052602483fd5b6001600160a01b0383166001600160a01b03196042541617604255611029565b50346103aa57806003193601126103aa5760206001600160a01b0360015416604051908152f35b50346103aa57806003193601126103aa57602060ff60405460081c166040519015158152f35b50346103aa57806003193601126103aa57604080516113798282612a73565b6005815260208101640352e302e360dc1b81528251938492602084525180928160208601528585015e828201840152601f01601f19168101030190f35b50346103aa5760203660031901126103aa5760406020916001600160a01b036113dd612993565b168152603b83522054604051908152f35b50346103aa5760403660031901126103aa57611408612993565b602435906114146130a8565b670de0b6b3a7640000821161039657670d99a8cec7e2000082106103825760206001600160a01b037f9ca91ac1ea5ea1d9cbec9ef13e20f58608deb72b1ec4c959e9deef3b844ef113921692838552603c8252806040862055604051908152a280f35b50346103aa5760203660031901126103aa57600435611494613117565b670de0b6b3a764000081106114d5576020817f8059a180b8ab782dfd848148b9824ec038f2b14545fa4f7d345afb4366b15bd792604655604051908152a180f35b63c64200e960e01b8252600452602490fd5b50346103aa5760203660031901126103aa576004356115046130a8565b6078811161153e576020817f8709833a517536698a10ba6401c14e6097aec681a3c756b82ee835e91f16bd1392604155604051908152a180f35b63c64200e960e01b8252600482fd5b50346103aa5760203660031901126103aa5760043561156a6130a8565b6115726133b0565b6003548391825b8281106115ca5750505081106115bb576020817fc11aa203f3c2e8b31d562bf3a9147fd82d486434a853f17239394afb0bd6bea392604455604051908152a180f35b632a0cc32560e11b8252600482fd5b836115d58284612b50565b51116115e4575b600101611579565b925060016115f28483612b50565b519390506115dc565b50346103aa5760203660031901126103aa576001600160a01b03604060209282611623612993565b16815260048452205416604051908152f35b50346103aa5760403660031901126103aa577fd4c474e9a2bd23d03ea76706eac0368ed31c4fc6c606a2b300c3a031be9ca796611670612993565b6024359061167c613117565b61168581613222565b6001600160a01b0316808452603f6020908152604080862084905580519283529082019290925290819081016105f5565b50346103aa57806003193601126103aa57602090604051908152f35b50346103aa5760203660031901126103aa57600435906003548210156103aa5760206001600160a01b0361170584612ac5565b90549060031b1c16604051908152f35b50346103aa57806003193601126103aa576020603a54604051908152f35b50346103aa57806003193601126103aa576020604454604051908152f35b50346103aa5760203660031901126103aa5760406020916001600160a01b03611778612993565b168152604583522054604051908152f35b50346103aa5760403660031901126103aa576001600160a01b0360406117ad6129bf565b9260043581525f805160206144638339815191526020522091165f52602052602060ff60405f2054166040519015158152f35b50346103aa57806003193601126103aa576117f9613166565b611801613480565b600160ff195f805160206144838339815191525416175f80516020614483833981519152557f62e78cea01bee320cd4e420270b5ea74000d11b0c9f74754ebdbfc544b05a2586020604051338152a180f35b50346103aa57806003193601126103aa57602060ff604054166040519015158152f35b50346103aa5760203660031901126103aa577f6878ba22919d474f18a1cf56ab9f49ca783696ab22be08ae3b69494c432ec2cc60206118b36129e4565b6118bb6130a8565b151560ff196040541660ff821617604055604051908152a180f35b50346103aa5760203660031901126103aa577fc8332d2a93ce8abef42d90f9a25582b80404fddf4ead3d8252bc63606fc33c6b60206119136129e4565b61191b613117565b151560405461ff008260081b169061ff00191617604055604051908152a180f35b50346103aa57806003193601126103aa57602060ff5f8051602061448383398151915254166040519015158152f35b50346103aa57806003193601126103aa576001600160a01b036020915416604051908152f35b50346103aa57806003193601126103aa576020604654604051908152f35b50346103aa5760203660031901126103aa576119c9612993565b6119d1613117565b6001600160a01b038116908115611a57577f6536690106168bdf4ba72c128a053d817999b1db90cae23f139b293bf862cb75916020917fffffffffffffffffffff0000000000000000000000000000000000000000ffff75ffffffffffffffffffffffffffffffffffffffff00006040549260101b16911617604055604051908152a180f35b63d92e233d60e01b8352600483fd5b50346103aa57806003193601126103aa576001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000163003611ad15760206040517f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc8152f35b63703e46dd60e11b8152600490fd5b50346103aa5760203660031901126103aa57600435611afd613117565b670de0b6b3a76400008111611b3e576020817fcc74da27ce2b4310d9585b17dba33f9102c597a23fcc53e11f22587eb44054ab92603a55604051908152a180f35b6356d2368b60e11b8252600452602490fd5b5060403660031901126103aa57611b65612993565b6024359067ffffffffffffffff82116112a557366023830112156112a55781600401359083611b9383612aa9565b93611ba16040519586612a73565b838552602085019336602482840101116112a557806024602093018637850101526001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016803014908115611db9575b50611daa57611c04613117565b6040516352d1902d60e01b81526001600160a01b0382169390602081600481885afa869181611d76575b50611c4757634c9c8ce360e01b86526004859052602486fd5b93847f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc879603611d645750823b15611d5257908185926001600160a01b03197f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5416177f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc557fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b8380a2805115611d37576109649382915190845af43d15611d2f573d91611d1383612aa9565b92611d216040519485612a73565b83523d85602085013e614408565b606091614408565b5050505034611d435780f35b63b398979f60e01b8152600490fd5b634c9c8ce360e01b8552600452602484fd5b632a87526960e21b8652600452602485fd5b9091506020813d602011611da2575b81611d9260209383612a73565b81010312610e285751905f611c2e565b3d9150611d85565b63703e46dd60e11b8452600484fd5b90506001600160a01b037f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc541614155f611bf7565b50346103aa5760403660031901126103aa576001600160a01b036040611e12612993565b9282611e1c6129bf565b9416815260066020522091165f52602052602060405f2054604051908152f35b50346103aa5760203660031901126103aa57611e56612993565b60035490825b828110611e67578380f35b80611e8c836001600160a01b03611e7f600195612ac5565b90549060031b1c16614086565b5001611e5c565b50346103aa5760203660031901126103aa577f2815e73179b9e71c24920f952681b17cd56ed8c3970196d013d9fb781fd0ff666001600160a01b03611ed6612993565b611ede6130a8565b611ee781613222565b6105f560425491838116841984161760425560405193849316839092916001600160a01b0360209181604085019616845216910152565b50346103aa57806003193601126103aa57611f376130a8565b5f805160206144838339815191525460ff811615611f8f5760ff19165f80516020614483833981519152557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa6020604051338152a180f35b638dfc202b60e01b8252600482fd5b50346103aa57806003193601126103aa5760206001600160a01b0360395416604051908152f35b50346103aa5760403660031901126103aa57611fdf6129bf565b336001600160a01b03821603611ffb5761096490600435613fe9565b63334bd91960e11b8252600482fd5b50346103aa5761201936612a2c565b612027949394929192613480565b839061203286613222565b61203a612d0c565b1590151581150361229c57156120aa5750612054846134a7565b61205d84612bac565b1561209657612076916120709185613f50565b9261388f565b156120875750602090604051908152f35b6336d5fb0f60e11b8152600490fd5b632201a6c360e01b83526004849052602483fd5b936120b693919361325b565b936120c96120c383613d74565b86612b50565b5191821561228a5760206001600160a01b039160046040518094819363313ce56760e01b8352165afa9081156112a957849161224c575b5061210a91613de3565b670de0b6b3a7640000810290808204670de0b6b3a76400001490151715612238579061213591612b18565b81945b60035486101561222a57670de0b6b3a764000061215f6121588888612b50565b5184612af1565b046001600160a01b0361217188612ac5565b90549060031b1c168452600460205260206001600160a01b03604086205416916024604051809481936303d1689d60e11b835260048301525afa9081156112a95790859185916121f4575b50916121e66121ec926001946001600160a01b036121d98c612ac5565b90549060031b1c16613e30565b90612b36565b950194612138565b9150506020813d8211612222575b8161220f60209383612a73565b81010312610817575184906121e66121bc565b3d9150612202565b935050505060209150610e66565b634e487b7160e01b83526011600452602483fd5b90506020813d602011612282575b8161226760209383612a73565b810103126112b4575160ff811681036112b45761210a612100565b3d915061225a565b63168cecf760e01b8452600452602483fd5b636ce14a8b60e01b8552600485fd5b50346103aa5760403660031901126103aa576109646004356122cb6129bf565b906122f161095a825f525f80516020614463833981519152602052600160405f20015490565b613cd3565b50346103aa5760203660031901126103aa576001600160a01b03612318612993565b6123206130a8565b61232981613222565b1681526004602052806001600160a01b03604082205416803b15610f8757818091600460405180948193631fa5d41d60e11b83525af18015610f7c57610f6b5750f35b50346103aa5760203660031901126103aa5760406020916001600160a01b03612393612993565b168152603c83522054604051908152f35b50346103aa5760603660031901126103aa576123be612993565b906123c76129bf565b604435906123d3613480565b6123dc84613222565b6123e581613222565b6123ee816134a7565b60ff60405460081c161561264a576001600160a01b03841693848452600560205260ff6040852054161561263b576001600160a01b0360425416851461262c5761243883836134d9565b61244182613630565b61244a84613630565b9161245e826124598584612af1565b612b18565b888852603f6020526040882054670de0b6b3a7640000019081670de0b6b3a76400001161261857670de0b6b3a76400009161249891612af1565b04926124a385613698565b9283851161256e575b505050506124b98361378f565b1561255f576124c782613817565b15612550576124d890339083613733565b9384156125415750937fbf8c93ffab42c7a8745e30d00eef080c6704962d26f0b42a0e9d9589b7417af1926001600160a01b036080936125196020986138fe565b612522816138fe565b60405193845216868301526040820152336060820152a1604051908152f35b631f2a200560e01b8152600490fd5b6336d5fb0f60e11b8552600485fd5b632595dbe760e01b8552600485fd5b8394506124599061257f9394612af1565b670de0b6b3a7640000810290808204670de0b6b3a7640000149015171561260457878752603f6020526040872054670de0b6b3a7640000019081670de0b6b3a7640000116125f057916125d86125de926125e694612b18565b90612b43565b339085613733565b505f8080806124ac565b634e487b7160e01b88526011600452602488fd5b634e487b7160e01b87526011600452602487fd5b634e487b7160e01b89526011600452602489fd5b630dc86ad360e01b8452600484fd5b6310ce689560e31b8452600484fd5b630709133160e01b8352600483fd5b50346103aa57806003193601126103aa5760206001600160a01b0360405460101c16604051908152f35b50346103aa5760203660031901126103aa576020610e666004355f525f80516020614463833981519152602052600160405f20015490565b50346103aa57806003193601126103aa576104e96104dd61325b565b50346103aa57806003193601126103aa5760206001600160a01b0360025416604051908152f35b50346103aa5760203660031901126103aa5760ff60406020926001600160a01b03612727612993565b168152600584522054166040519015158152f35b503461081757606036600319011261081757612755612993565b61275d6129d5565b6001600160a01b0361276d6129a9565b92612776613117565b61277f81613222565b165f5260046020526001600160a01b0360405f20541691823b156108175760445f92836001600160a01b0395604051968795869463110b006960e21b8652151560048601521660248401525af180156127e8576127da575080f35b6127e691505f90612a73565b005b6040513d5f823e3d90fd5b34610817576040366003190112610817577f4ace5cf0f475b119f5f67f29290bd198e42143b7934b17986fc5c06d00beae7a61282d612993565b60243590612839613117565b6001600160a01b0381165f5260456020528160405f205561287560405192839283602090939291936001600160a01b0360408201951681520152565b0390a1005b346108175760203660031901126108175760043563ffffffff60e01b811680910361081757602090637965db0b60e01b81149081156128bf575b506040519015158152f35b6301ffc9a760e01b149050826128b4565b34610817576060366003190112610817576128e9612993565b604435906024356128f86130a8565b61290182613222565b66470de4df82000081118015612983575b612974577fb360fe75f417722278b8ed353f0abcd3c5a6776faaa82a07b12047fa2ef3926e926001600160a01b036060931691825f52603d6020528060405f2055825f52603e6020528160405f205560405192835260208301526040820152a1005b63c64200e960e01b5f5260045ffd5b5066470de4df8200008311612912565b600435906001600160a01b038216820361081757565b604435906001600160a01b038216820361081757565b602435906001600160a01b038216820361081757565b60243590811515820361081757565b60043590811515820361081757565b60206040818301928281528451809452019201905f5b818110612a165750505090565b8251845260209384019390920191600101612a09565b6080906003190112610817576004356001600160a01b03811681036108175790602435906044356001600160a01b0381168103610817579060643580151581036108175790565b90601f8019910116810190811067ffffffffffffffff821117612a9557604052565b634e487b7160e01b5f52604160045260245ffd5b67ffffffffffffffff8111612a9557601f01601f191660200190565b600354811015612add5760035f5260205f2001905f90565b634e487b7160e01b5f52603260045260245ffd5b81810292918115918404141715612b0457565b634e487b7160e01b5f52601160045260245ffd5b8115612b22570490565b634e487b7160e01b5f52601260045260245ffd5b91908201809211612b0457565b91908203918211612b0457565b8051821015612add5760209160051b010190565b5190811515820361081757565b908160409103126108175760405190604082019082821067ffffffffffffffff831117612a9557602091604052805183520151602082015290565b6001600160a01b0360405460101c166001600160a01b0360405192635ab20f9d60e11b84521691826004820152602081602481855afa9081156127e8575f91612cd2575b5015612c9d57604060249181519283809263f989568d60e01b82528660048301525afa9081156127e8575f91612ca3575b506020810151612c3360415442612b43565b11612c9d57815f52603d60205260405f2054670de0b6b3a76400000390670de0b6b3a76400008211612b045751809111159182612c6f57505090565b9091505f52603e60205260405f2054670de0b6b3a7640000019081670de0b6b3a764000011612b0457111590565b50505f90565b612cc5915060403d604011612ccb575b612cbd8183612a73565b810190612b71565b5f612c21565b503d612cb3565b90506020813d602011612d04575b81612ced60209383612a73565b8101031261081757612cfe90612b64565b5f612bf0565b3d9150612ce0565b60ff60405416612d8b576003545f5b818110612d29575050600190565b612d4a6001600160a01b03612d3d83612ac5565b90549060031b1c16612bac565b80612d5c575b612c9d57600101612d1b565b506001600160a01b03612d6e82612ac5565b90549060031b1c165f52600560205260ff60405f20541615612d50565b600190565b60ff60405416612d8b576003545f5b818110612dac5750505f90565b612dc06001600160a01b03612d3d83612ac5565b15612dcf576001905b01612d9f565b612df06001600160a01b03612de383612ac5565b90549060031b1c16613698565b15612dfc575050600190565b600190612dc9565b60ff60405416612ec4576003545f5b818110612e2a57505015612e2657600190565b5f90565b612e3e6001600160a01b03612d3d83612ac5565b8315612e8e5780612e5f575b612e58576001905b01612e13565b5050505f90565b506001600160a01b03612e7182612ac5565b90549060031b1c165f52600560205260ff60405f20541615612e4a565b15612e9b57600190612e52565b612eaf6001600160a01b03612de383612ac5565b15612ebc57505050600190565b600190612e52565b50600190565b67ffffffffffffffff8111612a955760051b60200190565b90612eec82612eca565b612ef96040519182612a73565b8281528092612f0a601f1991612eca565b0190602036910137565b909392612f2082613222565b612f28612d90565b1590151581150361309957612fb35750612f4061325b565b92612f4c600354612ee2565b935f5b600354811015612fad5780612f9c85670de0b6b3a7640000612f7d612f7660019688612b50565b5188612af1565b046001600160a01b03612f8f85612ac5565b90549060031b1c16614239565b612fa68289612b50565b5201612f4f565b50505050565b909192612fdf612fd8612fc7600354612ee2565b94612fd185613d74565b9385614239565b9184612b50565b526001600160a01b03604254166001600160a01b03821690811461301f575b5061300890613817565b156130105790565b6336d5fb0f60e11b5f5260045ffd5b600354905f5b828110613033575050612ffe565b816001600160a01b0361304583612ac5565b90549060031b1c1614613091576130736001600160a01b0361306683612ac5565b90549060031b1c1661378f565b15613082576001905b01613025565b632595dbe760e01b5f5260045ffd5b60019061307c565b636ce14a8b60e01b5f5260045ffd5b335f9081527f06484cc59dc38e4f67c31122333a17ca81b3ca18cdf02bfc298072fa52b0316a602052604090205460ff16156130e057565b63e2517d3f60e01b5f52336004527f241ecf16d79d0f8dbfb92cbc07fe17840425976cf0667f022fe9877caa831b0860245260445ffd5b335f9081527fb7db2dd08fcb62d0c9e08c51941cae53c267786a0b75803fb7960902fc8ef97d602052604090205460ff161561314f57565b63e2517d3f60e01b5f52336004525f60245260445ffd5b335f9081527f75442b0a96088b5456bc4ed01394c96a4feec0f883c9494257d76b96ab1c9b6b602052604090205460ff161561319e57565b63e2517d3f60e01b5f52336004527f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a60245260445ffd5b805f525f8051602061446383398151915260205260405f206001600160a01b0333165f5260205260ff60405f2054161561320c5750565b63e2517d3f60e01b5f523360045260245260445ffd5b6001600160a01b0381165f5260046020526001600160a01b0360405f205416156132495750565b631a2a9e8760e01b5f5260045260245ffd5b6003549061326882612ee2565b915f905f5b8181106132d7575081156132d3575f5b81811061328957505050565b6132938186612b50565b5190670de0b6b3a7640000820291808304670de0b6b3a76400001490151715612b04576132c284600193612b18565b6132cc8288612b50565b520161327d565b5050565b916001600160a01b036132e984612ac5565b90549060031b1c165f526004602052600460206001600160a01b0360405f20541660405192838092635c975abb60e01b82525afa9081156127e8575f91613377575b5061336e5761336660019161334a6001600160a01b03612de387612ac5565b6133548689612b50565b5261335f8588612b50565b5190612b36565b925b0161326d565b91600190613368565b90506020813d82116133a8575b8161339160209383612a73565b81010312610817576133a290612b64565b5f61332b565b3d9150613384565b600354906133bd82612ee2565b915f905f5b818110613428575081156132d3575f5b8181106133de57505050565b6133e88186612b50565b5190670de0b6b3a7640000820291808304670de0b6b3a76400001490151715612b045761341784600193612b18565b6134218288612b50565b52016133d2565b916001600160a01b0361343a84612ac5565b90549060031b1c165f52600560205260ff60405f2054166134775761346f60019161334a6001600160a01b03612de387612ac5565b925b016133c2565b91600190613471565b60ff5f80516020614483833981519152541661349857565b63d93c066560e01b5f5260045ffd5b6001600160a01b0381165f52600560205260ff60405f2054166134c75750565b630d745c8360e31b5f5260045260245ffd5b60405182606052306040523360601b602c526f23b872dd000000000000000000000000600c5260205f6064601c82865af18060015f51141615613612575b505f6060526040526001600160a01b03811690815f5260046020526001600160a01b0360405f205416601452826034526f095ea7b30000000000000000000000005f5260205f6044601082855af1908160015f511416156135f4575b50505f916020918360345283526004825260446001600160a01b03604085205416916040519485938492636e553f6560e01b845260048401523060248401525af19081156127e8575f916135c5575090565b90506020813d6020116135ec575b816135e060209383612a73565b81010312610817575190565b3d91506135d3565b3b153d171015613605575f80613573565b633e3f8f735f526004601cfd5b3d833b15171015613623575f613517565b637939f4245f526004601cfd5b60408054604154825163c442b43960e01b81526001600160a01b0394851660048201526024810191909152928391604491839160101c165afa9081156127e8575f9161367b57505190565b613694915060403d604011612ccb57612cbd8183612a73565b5190565b6001600160a01b0360249116805f52600460205260206001600160a01b0360405f205416604051938480926370a0823160e01b82523060048301525afa9081156127e8575f916136fd575b6136fa92505f52600760205260405f205490612b43565b90565b90506020823d60201161372b575b8161371860209383612a73565b81010312610817576136fa9151906136e3565b3d915061370b565b6001600160a01b039081165f908152600460208181526040808420549051635d043b2960e11b81529283019590955294831660248201523060448201529392849260649284929091165af19081156127e8575f916135c5575090565b6001600160a01b03604254166001600160a01b0382169181831461380f576137b96137bf91613698565b91613698565b801561380157670de0b6b3a7640000820291808304670de0b6b3a76400001490151715612b04576137ef91612b18565b905f52604360205260405f2054101590565b5015159050612e2657600190565b505050600190565b61381f6133b0565b5f9060035492604454925b84811061383b575050505050600190565b6001600160a01b0361384c82612ac5565b90549060031b1c166001600160a01b03831614613887578361386e8285612b50565b511161387e576001905b0161382a565b50505050505f90565b600190613878565b6138976133b0565b5f9060035492604454925b8481106138b3575050505050600190565b6001600160a01b036138c482612ac5565b90549060031b1c166001600160a01b038316036138f657836138e68285612b50565b511161387e576001905b016138a2565b6001906138f0565b6001600160a01b0316805f5260046020526001600160a01b0360405f2054166040516318160ddd60e01b8152602081600481855afa9081156127e8575f91613bf2575b506040805163396a590d60e11b81529081600481865afa9081156127e8575f905f92613ba1575b5015613b3d576001600160a01b03604051916370a0823160e01b8352166004820152602081602481875afa9081156127e8575f91613b0b575b505b6040516303d1689d60e11b815260048101839052602081602481875afa80156127e85782915f91613ad6575b5011613ac15750506040516370a0823160e01b815230600482015291602083602481855afa9283156127e8575f93613a8d575b505f52600760205260405f20548210613a19575050565b6020602491604051928380926303d1689d60e11b82528660048301525afa9081156127e8575f91613a5b575b5063b97fded160e01b5f5260045260245260445ffd5b90506020813d602011613a85575b81613a7660209383612a73565b81010312610817575182613a45565b3d9150613a69565b9092506020813d602011613ab9575b81613aa960209383612a73565b810103126108175751915f613a02565b3d9150613a9c565b63b97fded160e01b5f5260045260245260445ffd5b9150506020813d602011613b03575b81613af260209383612a73565b81010312610817578190515f6139cf565b3d9150613ae5565b90506020813d602011613b35575b81613b2660209383612a73565b8101031261081757515f6139a1565b3d9150613b19565b506040516370a0823160e01b815260048101839052602081602481875afa9081156127e8575f91613b6f575b506139a3565b90506020813d602011613b99575b81613b8a60209383612a73565b8101031261081757515f613b69565b3d9150613b7d565b9150506040813d604011613bea575b81613bbd60409383612a73565b81010312610817576020613bd082612b64565b910151906001600160a01b0382168203610817575f613968565b3d9150613bb0565b90506020813d602011613c1c575b81613c0d60209383612a73565b8101031261081757515f613941565b3d9150613c00565b6001600160a01b0381165f9081527fb7db2dd08fcb62d0c9e08c51941cae53c267786a0b75803fb7960902fc8ef97d602052604090205460ff16613cce576001600160a01b03165f8181527fb7db2dd08fcb62d0c9e08c51941cae53c267786a0b75803fb7960902fc8ef97d60205260408120805460ff191660011790553391907f2f8788117e7eff1d82e926ec794901d17c78024a50270940304540a733656f0d8180a4600190565b505f90565b805f525f8051602061446383398151915260205260405f206001600160a01b0383165f5260205260ff60405f205416155f14612c9d57805f525f8051602061446383398151915260205260405f206001600160a01b0383165f5260205260405f20600160ff198254161790556001600160a01b03339216907f2f8788117e7eff1d82e926ec794901d17c78024a50270940304540a733656f0d5f80a4600190565b6003545f9291835b828110613d8857505050565b6001600160a01b03613d9982612ac5565b90549060031b1c166001600160a01b03831614613db857600101613d7c565b93505050565b9060ff8091169116039060ff8211612b0457565b60ff16604d8111612b0457600a0a90565b60ff821660128114613e2a5760121015613e1357613e0d613e0860126136fa94613dbe565b613dd2565b90612b18565b613e24613e086136fa936012613dbe565b90612af1565b50905090565b91908015612e5857613e4281846134d9565b926001600160a01b03811692835f52603b602052613ea2613e7b670de0b6b3a7640000613e7360405f205489612af1565b048097612b43565b613e9b670de0b6b3a7640000613e93603a5484612af1565b048092612b43565b908461434e565b6001600160a01b036039541691823b15610817576040516340c10f1960e01b81526001600160a01b038316600482015260248101879052925f908490818381604481015b03925af19081156127e8576001600160a01b0393613f0992613f40575b506138fe565b60405192835284602084015216907f5bf1ae7c4c81d8f51bd6f74471f2622d4952dfeca98ba4f9f0da428b2f84edf560403392a490565b5f613f4a91612a73565b5f613f03565b91908015612e5857613f6281846134d9565b926001600160a01b03811692835f52603b602052613f93613e7b670de0b6b3a7640000613e7360405f205489612af1565b613f9c8261378f565b15613082576001600160a01b036039541691823b15610817576040516340c10f1960e01b81526001600160a01b038316600482015260248101879052925f90849081838160448101613ee6565b805f525f8051602061446383398151915260205260405f206001600160a01b0383165f5260205260ff60405f2054165f14612c9d57805f525f8051602061446383398151915260205260405f206001600160a01b0383165f5260205260405f2060ff1981541690556001600160a01b03339216907ff6391f5c32d9c69d2a47ea670b442974b53935d1edc7fd64eb21e047a839171b5f80a4600190565b9190916001600160a01b03831690815f52600660205260405f206001600160a01b0382165f526020526001600160a01b0360405f2054911690815f526004602052602460206001600160a01b0360405f205416604051928380926303d1689d60e11b82528660048301525afa9081156127e8575f91614207575b50156141ff5760206141855f96858852600683526040882085895283528760408120558488526007835260408820614139858254612b43565b905584885260048084526040808a20549051635d043b2960e11b81529182018690526001600160a01b039283166024830152306044830152909889939190921691839182906064820190565b03925af19485156127e8575f956141ca575b5060407fd7df942c87bc524ccb17249639699aa79ede0bfadd2bea503ce279f993479150918682519182526020820152a3565b9094506020813d6020116141f7575b816141e660209383612a73565b810103126108175751936040614197565b3d91506141d9565b505f93505050565b90506020813d602011614231575b8161422260209383612a73565b8101031261081757515f614100565b3d9150614215565b9291908015614347576001600160a01b03841691825f52603c602052670de0b6b3a764000061426c60405f205484612af1565b04906142788284612b43565b614290670de0b6b3a7640000613e93603a5484612af1565b906001600160a01b0360395416803b1561081757604051632770a7eb60e21b815233600482015260248101879052905f908290604490829084905af19485156127e8576142f5614301946142fb9487946001600160a01b0399614337575b508c61434e565b89613733565b966138fe565b60405192868452602084015216907f86a824fd3cfb472a44138f5f76db0a4642f87a88cee775c39d3f865f4c03bdaa60403392a4565b5f61434191612a73565b5f6142ee565b505f925050565b90806143ac575b508161435f575050565b6001600160a01b039081600154165f52600660205260405f208282165f5260205260405f2061438f848254612b36565b9055165f5260076020526143a860405f20918254612b36565b9055565b6143c3906001600160a01b03600254169083613733565b505f614355565b60ff7ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005460401c16156143f957565b631afcd79f60e31b5f5260045ffd5b9061442c575080511561441d57805190602001fd5b63d6bda27560e01b5f5260045ffd5b81511580614459575b61443d575090565b6001600160a01b0390639996b31560e01b5f521660045260245ffd5b50803b1561443556fe02dd7bc7dec4dceedda775e58dd541e08a116c6c53815c0bd028192f7b626800cd5ed15c6e187e77e9aee88184c21f4f2182ab5827cb3b7e07fbedcd63f03300a164736f6c634300081a000a
Deployed Bytecode
0x60806040526004361015610011575f80fd5b5f803560e01c8062271801146128d057806301ffc9a71461287a5780630aa4f4cd146127f35780631224b28c1461273b5780631e17e57c146126fe5780631e704c63146126d757806322acb867146126bb578063248a9ca3146126835780632630c12f1461265957806326c01303146123a45780632cfb0e101461236c5780632efbab2c146122f65780632f2ff15d146122ab578063328ebaf71461200a57806336568abe14611fc557806336b2c4b214611f9e5780633f4ba83a14611f1e5780634af471be14611e935780634c17904214611e3c5780634c97f76614611dee5780634f1ef28614611b50578063503b9e5914611ae057806352d1902d14611a66578063530e784f146119af57806355c7aaff1461199157806359659e901461196b5780635c975abb1461193c5780635f0ccdc2146118d65780636fe7f983146118765780637b34b5d8146118535780638456cb59146117e057806391d148541461178957806393df00181461175157806399a2af75146117335780639ee61d5614611715578063a083bd3c146116d2578063a217fddf146116b6578063a3bc238514611635578063a622ee7c146115fb578063a71f6f5f1461154d578063ab6636ec146114e7578063aba1f54114611477578063abab398a146113ee578063ad1567c7146113b6578063ad3cb1cc1461135a578063ae574a9014611334578063b3f006741461130d578063b4bd6f4614611004578063bb85d15b14610fe6578063bc7c290214610fc2578063bdb912f314610f8a578063c0bd6f9e14610ef5578063c1a7b2af14610e6e578063c879c6d814610e30578063cc2a9a5b14610968578063d547741f14610914578063de4bc640146108e6578063e0cda5da146105fb578063e2a0253614610581578063e63ab1e914610546578063ebaa654514610528578063ec87621c146104ed578063ed596315146104b7578063efdcd9741461044d578063f0f15132146103d4578063f68f0c69146103ad5763f91d65af146102fa575f80fd5b346103aa5760403660031901126103aa57610313612993565b6024359061031f6130a8565b670de0b6b3a7640000821161039657670d99a8cec7e2000082106103825760206001600160a01b037f2efe6880df7ccb94a2aa093e54fd1f13f69ac2d50436d6a558a7c639697d1ea3921692838552603b8252806040862055604051908152a280f35b63071fba9d60e41b83526004829052602483fd5b6356d2368b60e11b83526004829052602483fd5b80fd5b50346103aa57806003193601126103aa5760206001600160a01b0360425416604051908152f35b50346103aa5760203660031901126103aa576001600160a01b036103f6612993565b6103fe613117565b16801561043e57806001600160a01b031960025416176002557fba4a603fbc52c5350922457bcd71f2287f1a67c8eabd5ec9a01f1dbd7ace4a7c8280a280f35b63d92e233d60e01b8252600482fd5b50346103aa5760203660031901126103aa576001600160a01b0361046f612993565b610477613117565b16801561043e57806001600160a01b031960015416176001557fbdf37c276f641820b141429d245add2552b4118c0866e5a78638e3de5ef18d9d8280a280f35b50346103aa576104e96104dd6104cc36612a2c565b926104d8929192613480565b612f14565b604051918291826129f3565b0390f35b50346103aa57806003193601126103aa5760206040517f241ecf16d79d0f8dbfb92cbc07fe17840425976cf0667f022fe9877caa831b088152f35b50346103aa57806003193601126103aa576020604154604051908152f35b50346103aa57806003193601126103aa5760206040517f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a8152f35b50346103aa5760403660031901126103aa577f2e04b29851c22c48f29b5bbcd5092f95d0907b7700d4aaa2341f95fc931e78706105bc612993565b602435906105c86130a8565b6001600160a01b031680845260436020908152604080862084905580519283529082019290925290819081015b0390a180f35b50346103aa5760403660031901126103aa57610615612993565b60243590610621613480565b819061062c81613222565b610635816134a7565b6001600160a01b038116928385526045602052604085205484865260046020526001600160a01b036040872054168587526007602052602060408820546024604051809481936303d1689d60e11b835260048301525afa80156108db5787906108a8575b600491508688528160205260206001600160a01b0360408a205416604051938480926278744560e21b82525afa801561089d578890610869575b6106dd9250612b43565b9181831015610855576106ef84612bac565b15610841576106ff829184612b36565b1161082e575b50508284526004602052602460206001600160a01b03604087205416604051928380926363737ac960e11b82528760048301525afa9081156108235785916107ed575b50604654116107d95761075b82826134d9565b506107658161378f565b156107ca576107738161388f565b156107bb57916060916107a67f1bb1edfe5e334d503d0dce0cd3185491ce674afad9ecfe52601c10c8c7c08d28946138fe565b6040519182526020820152336040820152a180f35b6336d5fb0f60e11b8452600484fd5b632595dbe760e01b8452600484fd5b6316ebc63f60e11b84526004829052602484fd5b90506020813d60201161081b575b8161080860209383612a73565b8101031261081757515f610748565b5f80fd5b3d91506107fb565b6040513d87823e3d90fd5b610839929350612b43565b905f80610705565b632201a6c360e01b87526004849052602487fd5b630872404f60e11b87526004849052602487fd5b506020823d602011610895575b8161088360209383612a73565b81010312610817576106dd91516106d3565b3d9150610876565b6040513d8a823e3d90fd5b506020813d6020116108d3575b816108c260209383612a73565b810103126108175760049051610699565b3d91506108b5565b6040513d89823e3d90fd5b50346103aa5760203660031901126103aa57602061090a6109056129e4565b612e04565b6040519015158152f35b50346103aa5760403660031901126103aa576109646004356109346129bf565b9061095f61095a825f525f80516020614463833981519152602052600160405f20015490565b6131d5565b613fe9565b5080f35b50346103aa5760c03660031901126103aa57610982612993565b61098a6129bf565b906109936129a9565b906064356001600160a01b038116809103610e2c57608435916001600160a01b03831691828403610e285760a435916001600160a01b038316809303610e24577ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00549560ff8760401c16159667ffffffffffffffff811680159081610e1c575b6001149081610e12575b159081610e09575b50610dfa5767ffffffffffffffff1981166001177ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005587610dbb575b50610a6a6143ca565b610a726143ca565b610a7a6143ca565b610a826143ca565b60ff195f8051602061448383398151915254165f8051602061448383398151915255610aac6143ca565b610ab46143ca565b6001600160a01b03821615610dac576001600160a01b0316928315610dac578215610dac578015610dac57885473ffffffffffffffffffffffffffffffffffffffff1916178855610b0490613c24565b50806001600160a01b03196001541617600155816001600160a01b031960025416176002557fbdf37c276f641820b141429d245add2552b4118c0866e5a78638e3de5ef18d9d8780a27fba4a603fbc52c5350922457bcd71f2287f1a67c8eabd5ec9a01f1dbd7ace4a7c8680a27f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a85525f805160206144638339815191526020526001600160a01b036001604087200154947f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a87525f805160206144638339815191526020527f241ecf16d79d0f8dbfb92cbc07fe17840425976cf0667f022fe9877caa831b0860016040892001557f241ecf16d79d0f8dbfb92cbc07fe17840425976cf0667f022fe9877caa831b08604051967f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a7fbd79b86ffe0ab8e8776151514217cd7cacd52c909f66475c3af44e129f0b00ff8a80a416908115610d9d5715610d8e576001600160a01b03196039541617603955670de0b6b3a7640000603a55600a604155670de0b6b3a76400006046557fffffffffffffffffffff000000000000000000000000000000000000000000ff75ffffffffffffffffffffffffffffffffffffffff000060405492670de0b6b3a764000060445560101b16911617604055610d12575080f35b60207fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d29168ff0000000000000000197ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a0054167ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005560018152a180f35b63d92e233d60e01b8552600485fd5b63d92e233d60e01b8652600486fd5b63d92e233d60e01b8952600489fd5b68ffffffffffffffffff191668010000000000000001177ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00555f610a61565b63f92ee8a960e01b8a5260048afd5b9050155f610a25565b303b159150610a1d565b899150610a13565b8780fd5b8680fd5b8480fd5b50346103aa5760403660031901126103aa576020610e66610e4f612993565b610e576129bf565b90610e6181613222565b614086565b604051908152f35b50346103aa5760403660031901126103aa57610e88612993565b7fd2f5128538548a810c4735697cdd8ea509b0d233102362e242063283126400b860206001600160a01b03610ebb6129d5565b93610ec46130a8565b610ecd81613222565b169283855260058252604085209015159060ff1981541660ff8316179055604051908152a280f35b50346103aa5760203660031901126103aa576001600160a01b03610f17612993565b610f1f613166565b610f2881613222565b1681526004602052806001600160a01b03604082205416803b15610f8757818091600460405180948193638456cb5960e01b83525af18015610f7c57610f6b5750f35b81610f7591612a73565b6103aa5780f35b6040513d84823e3d90fd5b50fd5b50346103aa5760203660031901126103aa5760406020916001600160a01b03610fb1612993565b168152604383522054604051908152f35b50346103aa5760203660031901126103aa57602061090a610fe1612993565b612bac565b50346103aa57806003193601126103aa576020600354604051908152f35b50346103aa5760203660031901126103aa5761101e612993565b9060035480156112ed575b611031613117565b6001600160a01b0383169081835260046020526001600160a01b036040842054166112d957680100000000000000008110156112c5578060016110779201600355612ac5565b81549060031b906001600160a01b0384831b921b191617905580825260208220926001600160a01b0383541693604051947fb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f36060527f1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6c6040527660195155f3363d3d373d3d363d602036600436635c60da6020527c60523d8160223d39730000000000000000000000000000000000000000176009526074600c85f580156112b8576001600160a01b039085604052846060521693843b156112b45763485cc95560e01b81526001600160a01b0382166004820152306024820152838160448183895af180156112a957908491611290575b50829052600460205260408320805473ffffffffffffffffffffffffffffffffffffffff19168517905581847f5d9c31ffa0fecffd7cf379989a3c7af252f0335e0d2a1320b55245912c781f538580a38183526043602052670de0b6b3a76400006040842055818352603b602052670ddeeff45500c0006040842055818352603c602052670ddeeff45500c0006040842055818352603d60205266470de4df8200006040842055818352603e60205266470de4df820000604084205561124e81612bac565b1561127e5760208466071afd498d000060408686808252603d865283838320558152603e85522055604051908152f35b632201a6c360e01b8352600452602482fd5b8161129a91612a73565b6112a557825f611189565b8280fd5b6040513d86823e3d90fd5b8380fd5b633011642584526004601cfd5b634e487b7160e01b83526041600452602483fd5b631c5fe60b60e11b83526004849052602483fd5b6001600160a01b0383166001600160a01b03196042541617604255611029565b50346103aa57806003193601126103aa5760206001600160a01b0360015416604051908152f35b50346103aa57806003193601126103aa57602060ff60405460081c166040519015158152f35b50346103aa57806003193601126103aa57604080516113798282612a73565b6005815260208101640352e302e360dc1b81528251938492602084525180928160208601528585015e828201840152601f01601f19168101030190f35b50346103aa5760203660031901126103aa5760406020916001600160a01b036113dd612993565b168152603b83522054604051908152f35b50346103aa5760403660031901126103aa57611408612993565b602435906114146130a8565b670de0b6b3a7640000821161039657670d99a8cec7e2000082106103825760206001600160a01b037f9ca91ac1ea5ea1d9cbec9ef13e20f58608deb72b1ec4c959e9deef3b844ef113921692838552603c8252806040862055604051908152a280f35b50346103aa5760203660031901126103aa57600435611494613117565b670de0b6b3a764000081106114d5576020817f8059a180b8ab782dfd848148b9824ec038f2b14545fa4f7d345afb4366b15bd792604655604051908152a180f35b63c64200e960e01b8252600452602490fd5b50346103aa5760203660031901126103aa576004356115046130a8565b6078811161153e576020817f8709833a517536698a10ba6401c14e6097aec681a3c756b82ee835e91f16bd1392604155604051908152a180f35b63c64200e960e01b8252600482fd5b50346103aa5760203660031901126103aa5760043561156a6130a8565b6115726133b0565b6003548391825b8281106115ca5750505081106115bb576020817fc11aa203f3c2e8b31d562bf3a9147fd82d486434a853f17239394afb0bd6bea392604455604051908152a180f35b632a0cc32560e11b8252600482fd5b836115d58284612b50565b51116115e4575b600101611579565b925060016115f28483612b50565b519390506115dc565b50346103aa5760203660031901126103aa576001600160a01b03604060209282611623612993565b16815260048452205416604051908152f35b50346103aa5760403660031901126103aa577fd4c474e9a2bd23d03ea76706eac0368ed31c4fc6c606a2b300c3a031be9ca796611670612993565b6024359061167c613117565b61168581613222565b6001600160a01b0316808452603f6020908152604080862084905580519283529082019290925290819081016105f5565b50346103aa57806003193601126103aa57602090604051908152f35b50346103aa5760203660031901126103aa57600435906003548210156103aa5760206001600160a01b0361170584612ac5565b90549060031b1c16604051908152f35b50346103aa57806003193601126103aa576020603a54604051908152f35b50346103aa57806003193601126103aa576020604454604051908152f35b50346103aa5760203660031901126103aa5760406020916001600160a01b03611778612993565b168152604583522054604051908152f35b50346103aa5760403660031901126103aa576001600160a01b0360406117ad6129bf565b9260043581525f805160206144638339815191526020522091165f52602052602060ff60405f2054166040519015158152f35b50346103aa57806003193601126103aa576117f9613166565b611801613480565b600160ff195f805160206144838339815191525416175f80516020614483833981519152557f62e78cea01bee320cd4e420270b5ea74000d11b0c9f74754ebdbfc544b05a2586020604051338152a180f35b50346103aa57806003193601126103aa57602060ff604054166040519015158152f35b50346103aa5760203660031901126103aa577f6878ba22919d474f18a1cf56ab9f49ca783696ab22be08ae3b69494c432ec2cc60206118b36129e4565b6118bb6130a8565b151560ff196040541660ff821617604055604051908152a180f35b50346103aa5760203660031901126103aa577fc8332d2a93ce8abef42d90f9a25582b80404fddf4ead3d8252bc63606fc33c6b60206119136129e4565b61191b613117565b151560405461ff008260081b169061ff00191617604055604051908152a180f35b50346103aa57806003193601126103aa57602060ff5f8051602061448383398151915254166040519015158152f35b50346103aa57806003193601126103aa576001600160a01b036020915416604051908152f35b50346103aa57806003193601126103aa576020604654604051908152f35b50346103aa5760203660031901126103aa576119c9612993565b6119d1613117565b6001600160a01b038116908115611a57577f6536690106168bdf4ba72c128a053d817999b1db90cae23f139b293bf862cb75916020917fffffffffffffffffffff0000000000000000000000000000000000000000ffff75ffffffffffffffffffffffffffffffffffffffff00006040549260101b16911617604055604051908152a180f35b63d92e233d60e01b8352600483fd5b50346103aa57806003193601126103aa576001600160a01b037f0000000000000000000000008fe54a1fb9d97ab6f02ad9e0931e9ab788f5d942163003611ad15760206040517f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc8152f35b63703e46dd60e11b8152600490fd5b50346103aa5760203660031901126103aa57600435611afd613117565b670de0b6b3a76400008111611b3e576020817fcc74da27ce2b4310d9585b17dba33f9102c597a23fcc53e11f22587eb44054ab92603a55604051908152a180f35b6356d2368b60e11b8252600452602490fd5b5060403660031901126103aa57611b65612993565b6024359067ffffffffffffffff82116112a557366023830112156112a55781600401359083611b9383612aa9565b93611ba16040519586612a73565b838552602085019336602482840101116112a557806024602093018637850101526001600160a01b037f0000000000000000000000008fe54a1fb9d97ab6f02ad9e0931e9ab788f5d94216803014908115611db9575b50611daa57611c04613117565b6040516352d1902d60e01b81526001600160a01b0382169390602081600481885afa869181611d76575b50611c4757634c9c8ce360e01b86526004859052602486fd5b93847f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc879603611d645750823b15611d5257908185926001600160a01b03197f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5416177f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc557fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b8380a2805115611d37576109649382915190845af43d15611d2f573d91611d1383612aa9565b92611d216040519485612a73565b83523d85602085013e614408565b606091614408565b5050505034611d435780f35b63b398979f60e01b8152600490fd5b634c9c8ce360e01b8552600452602484fd5b632a87526960e21b8652600452602485fd5b9091506020813d602011611da2575b81611d9260209383612a73565b81010312610e285751905f611c2e565b3d9150611d85565b63703e46dd60e11b8452600484fd5b90506001600160a01b037f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc541614155f611bf7565b50346103aa5760403660031901126103aa576001600160a01b036040611e12612993565b9282611e1c6129bf565b9416815260066020522091165f52602052602060405f2054604051908152f35b50346103aa5760203660031901126103aa57611e56612993565b60035490825b828110611e67578380f35b80611e8c836001600160a01b03611e7f600195612ac5565b90549060031b1c16614086565b5001611e5c565b50346103aa5760203660031901126103aa577f2815e73179b9e71c24920f952681b17cd56ed8c3970196d013d9fb781fd0ff666001600160a01b03611ed6612993565b611ede6130a8565b611ee781613222565b6105f560425491838116841984161760425560405193849316839092916001600160a01b0360209181604085019616845216910152565b50346103aa57806003193601126103aa57611f376130a8565b5f805160206144838339815191525460ff811615611f8f5760ff19165f80516020614483833981519152557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa6020604051338152a180f35b638dfc202b60e01b8252600482fd5b50346103aa57806003193601126103aa5760206001600160a01b0360395416604051908152f35b50346103aa5760403660031901126103aa57611fdf6129bf565b336001600160a01b03821603611ffb5761096490600435613fe9565b63334bd91960e11b8252600482fd5b50346103aa5761201936612a2c565b612027949394929192613480565b839061203286613222565b61203a612d0c565b1590151581150361229c57156120aa5750612054846134a7565b61205d84612bac565b1561209657612076916120709185613f50565b9261388f565b156120875750602090604051908152f35b6336d5fb0f60e11b8152600490fd5b632201a6c360e01b83526004849052602483fd5b936120b693919361325b565b936120c96120c383613d74565b86612b50565b5191821561228a5760206001600160a01b039160046040518094819363313ce56760e01b8352165afa9081156112a957849161224c575b5061210a91613de3565b670de0b6b3a7640000810290808204670de0b6b3a76400001490151715612238579061213591612b18565b81945b60035486101561222a57670de0b6b3a764000061215f6121588888612b50565b5184612af1565b046001600160a01b0361217188612ac5565b90549060031b1c168452600460205260206001600160a01b03604086205416916024604051809481936303d1689d60e11b835260048301525afa9081156112a95790859185916121f4575b50916121e66121ec926001946001600160a01b036121d98c612ac5565b90549060031b1c16613e30565b90612b36565b950194612138565b9150506020813d8211612222575b8161220f60209383612a73565b81010312610817575184906121e66121bc565b3d9150612202565b935050505060209150610e66565b634e487b7160e01b83526011600452602483fd5b90506020813d602011612282575b8161226760209383612a73565b810103126112b4575160ff811681036112b45761210a612100565b3d915061225a565b63168cecf760e01b8452600452602483fd5b636ce14a8b60e01b8552600485fd5b50346103aa5760403660031901126103aa576109646004356122cb6129bf565b906122f161095a825f525f80516020614463833981519152602052600160405f20015490565b613cd3565b50346103aa5760203660031901126103aa576001600160a01b03612318612993565b6123206130a8565b61232981613222565b1681526004602052806001600160a01b03604082205416803b15610f8757818091600460405180948193631fa5d41d60e11b83525af18015610f7c57610f6b5750f35b50346103aa5760203660031901126103aa5760406020916001600160a01b03612393612993565b168152603c83522054604051908152f35b50346103aa5760603660031901126103aa576123be612993565b906123c76129bf565b604435906123d3613480565b6123dc84613222565b6123e581613222565b6123ee816134a7565b60ff60405460081c161561264a576001600160a01b03841693848452600560205260ff6040852054161561263b576001600160a01b0360425416851461262c5761243883836134d9565b61244182613630565b61244a84613630565b9161245e826124598584612af1565b612b18565b888852603f6020526040882054670de0b6b3a7640000019081670de0b6b3a76400001161261857670de0b6b3a76400009161249891612af1565b04926124a385613698565b9283851161256e575b505050506124b98361378f565b1561255f576124c782613817565b15612550576124d890339083613733565b9384156125415750937fbf8c93ffab42c7a8745e30d00eef080c6704962d26f0b42a0e9d9589b7417af1926001600160a01b036080936125196020986138fe565b612522816138fe565b60405193845216868301526040820152336060820152a1604051908152f35b631f2a200560e01b8152600490fd5b6336d5fb0f60e11b8552600485fd5b632595dbe760e01b8552600485fd5b8394506124599061257f9394612af1565b670de0b6b3a7640000810290808204670de0b6b3a7640000149015171561260457878752603f6020526040872054670de0b6b3a7640000019081670de0b6b3a7640000116125f057916125d86125de926125e694612b18565b90612b43565b339085613733565b505f8080806124ac565b634e487b7160e01b88526011600452602488fd5b634e487b7160e01b87526011600452602487fd5b634e487b7160e01b89526011600452602489fd5b630dc86ad360e01b8452600484fd5b6310ce689560e31b8452600484fd5b630709133160e01b8352600483fd5b50346103aa57806003193601126103aa5760206001600160a01b0360405460101c16604051908152f35b50346103aa5760203660031901126103aa576020610e666004355f525f80516020614463833981519152602052600160405f20015490565b50346103aa57806003193601126103aa576104e96104dd61325b565b50346103aa57806003193601126103aa5760206001600160a01b0360025416604051908152f35b50346103aa5760203660031901126103aa5760ff60406020926001600160a01b03612727612993565b168152600584522054166040519015158152f35b503461081757606036600319011261081757612755612993565b61275d6129d5565b6001600160a01b0361276d6129a9565b92612776613117565b61277f81613222565b165f5260046020526001600160a01b0360405f20541691823b156108175760445f92836001600160a01b0395604051968795869463110b006960e21b8652151560048601521660248401525af180156127e8576127da575080f35b6127e691505f90612a73565b005b6040513d5f823e3d90fd5b34610817576040366003190112610817577f4ace5cf0f475b119f5f67f29290bd198e42143b7934b17986fc5c06d00beae7a61282d612993565b60243590612839613117565b6001600160a01b0381165f5260456020528160405f205561287560405192839283602090939291936001600160a01b0360408201951681520152565b0390a1005b346108175760203660031901126108175760043563ffffffff60e01b811680910361081757602090637965db0b60e01b81149081156128bf575b506040519015158152f35b6301ffc9a760e01b149050826128b4565b34610817576060366003190112610817576128e9612993565b604435906024356128f86130a8565b61290182613222565b66470de4df82000081118015612983575b612974577fb360fe75f417722278b8ed353f0abcd3c5a6776faaa82a07b12047fa2ef3926e926001600160a01b036060931691825f52603d6020528060405f2055825f52603e6020528160405f205560405192835260208301526040820152a1005b63c64200e960e01b5f5260045ffd5b5066470de4df8200008311612912565b600435906001600160a01b038216820361081757565b604435906001600160a01b038216820361081757565b602435906001600160a01b038216820361081757565b60243590811515820361081757565b60043590811515820361081757565b60206040818301928281528451809452019201905f5b818110612a165750505090565b8251845260209384019390920191600101612a09565b6080906003190112610817576004356001600160a01b03811681036108175790602435906044356001600160a01b0381168103610817579060643580151581036108175790565b90601f8019910116810190811067ffffffffffffffff821117612a9557604052565b634e487b7160e01b5f52604160045260245ffd5b67ffffffffffffffff8111612a9557601f01601f191660200190565b600354811015612add5760035f5260205f2001905f90565b634e487b7160e01b5f52603260045260245ffd5b81810292918115918404141715612b0457565b634e487b7160e01b5f52601160045260245ffd5b8115612b22570490565b634e487b7160e01b5f52601260045260245ffd5b91908201809211612b0457565b91908203918211612b0457565b8051821015612add5760209160051b010190565b5190811515820361081757565b908160409103126108175760405190604082019082821067ffffffffffffffff831117612a9557602091604052805183520151602082015290565b6001600160a01b0360405460101c166001600160a01b0360405192635ab20f9d60e11b84521691826004820152602081602481855afa9081156127e8575f91612cd2575b5015612c9d57604060249181519283809263f989568d60e01b82528660048301525afa9081156127e8575f91612ca3575b506020810151612c3360415442612b43565b11612c9d57815f52603d60205260405f2054670de0b6b3a76400000390670de0b6b3a76400008211612b045751809111159182612c6f57505090565b9091505f52603e60205260405f2054670de0b6b3a7640000019081670de0b6b3a764000011612b0457111590565b50505f90565b612cc5915060403d604011612ccb575b612cbd8183612a73565b810190612b71565b5f612c21565b503d612cb3565b90506020813d602011612d04575b81612ced60209383612a73565b8101031261081757612cfe90612b64565b5f612bf0565b3d9150612ce0565b60ff60405416612d8b576003545f5b818110612d29575050600190565b612d4a6001600160a01b03612d3d83612ac5565b90549060031b1c16612bac565b80612d5c575b612c9d57600101612d1b565b506001600160a01b03612d6e82612ac5565b90549060031b1c165f52600560205260ff60405f20541615612d50565b600190565b60ff60405416612d8b576003545f5b818110612dac5750505f90565b612dc06001600160a01b03612d3d83612ac5565b15612dcf576001905b01612d9f565b612df06001600160a01b03612de383612ac5565b90549060031b1c16613698565b15612dfc575050600190565b600190612dc9565b60ff60405416612ec4576003545f5b818110612e2a57505015612e2657600190565b5f90565b612e3e6001600160a01b03612d3d83612ac5565b8315612e8e5780612e5f575b612e58576001905b01612e13565b5050505f90565b506001600160a01b03612e7182612ac5565b90549060031b1c165f52600560205260ff60405f20541615612e4a565b15612e9b57600190612e52565b612eaf6001600160a01b03612de383612ac5565b15612ebc57505050600190565b600190612e52565b50600190565b67ffffffffffffffff8111612a955760051b60200190565b90612eec82612eca565b612ef96040519182612a73565b8281528092612f0a601f1991612eca565b0190602036910137565b909392612f2082613222565b612f28612d90565b1590151581150361309957612fb35750612f4061325b565b92612f4c600354612ee2565b935f5b600354811015612fad5780612f9c85670de0b6b3a7640000612f7d612f7660019688612b50565b5188612af1565b046001600160a01b03612f8f85612ac5565b90549060031b1c16614239565b612fa68289612b50565b5201612f4f565b50505050565b909192612fdf612fd8612fc7600354612ee2565b94612fd185613d74565b9385614239565b9184612b50565b526001600160a01b03604254166001600160a01b03821690811461301f575b5061300890613817565b156130105790565b6336d5fb0f60e11b5f5260045ffd5b600354905f5b828110613033575050612ffe565b816001600160a01b0361304583612ac5565b90549060031b1c1614613091576130736001600160a01b0361306683612ac5565b90549060031b1c1661378f565b15613082576001905b01613025565b632595dbe760e01b5f5260045ffd5b60019061307c565b636ce14a8b60e01b5f5260045ffd5b335f9081527f06484cc59dc38e4f67c31122333a17ca81b3ca18cdf02bfc298072fa52b0316a602052604090205460ff16156130e057565b63e2517d3f60e01b5f52336004527f241ecf16d79d0f8dbfb92cbc07fe17840425976cf0667f022fe9877caa831b0860245260445ffd5b335f9081527fb7db2dd08fcb62d0c9e08c51941cae53c267786a0b75803fb7960902fc8ef97d602052604090205460ff161561314f57565b63e2517d3f60e01b5f52336004525f60245260445ffd5b335f9081527f75442b0a96088b5456bc4ed01394c96a4feec0f883c9494257d76b96ab1c9b6b602052604090205460ff161561319e57565b63e2517d3f60e01b5f52336004527f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a60245260445ffd5b805f525f8051602061446383398151915260205260405f206001600160a01b0333165f5260205260ff60405f2054161561320c5750565b63e2517d3f60e01b5f523360045260245260445ffd5b6001600160a01b0381165f5260046020526001600160a01b0360405f205416156132495750565b631a2a9e8760e01b5f5260045260245ffd5b6003549061326882612ee2565b915f905f5b8181106132d7575081156132d3575f5b81811061328957505050565b6132938186612b50565b5190670de0b6b3a7640000820291808304670de0b6b3a76400001490151715612b04576132c284600193612b18565b6132cc8288612b50565b520161327d565b5050565b916001600160a01b036132e984612ac5565b90549060031b1c165f526004602052600460206001600160a01b0360405f20541660405192838092635c975abb60e01b82525afa9081156127e8575f91613377575b5061336e5761336660019161334a6001600160a01b03612de387612ac5565b6133548689612b50565b5261335f8588612b50565b5190612b36565b925b0161326d565b91600190613368565b90506020813d82116133a8575b8161339160209383612a73565b81010312610817576133a290612b64565b5f61332b565b3d9150613384565b600354906133bd82612ee2565b915f905f5b818110613428575081156132d3575f5b8181106133de57505050565b6133e88186612b50565b5190670de0b6b3a7640000820291808304670de0b6b3a76400001490151715612b045761341784600193612b18565b6134218288612b50565b52016133d2565b916001600160a01b0361343a84612ac5565b90549060031b1c165f52600560205260ff60405f2054166134775761346f60019161334a6001600160a01b03612de387612ac5565b925b016133c2565b91600190613471565b60ff5f80516020614483833981519152541661349857565b63d93c066560e01b5f5260045ffd5b6001600160a01b0381165f52600560205260ff60405f2054166134c75750565b630d745c8360e31b5f5260045260245ffd5b60405182606052306040523360601b602c526f23b872dd000000000000000000000000600c5260205f6064601c82865af18060015f51141615613612575b505f6060526040526001600160a01b03811690815f5260046020526001600160a01b0360405f205416601452826034526f095ea7b30000000000000000000000005f5260205f6044601082855af1908160015f511416156135f4575b50505f916020918360345283526004825260446001600160a01b03604085205416916040519485938492636e553f6560e01b845260048401523060248401525af19081156127e8575f916135c5575090565b90506020813d6020116135ec575b816135e060209383612a73565b81010312610817575190565b3d91506135d3565b3b153d171015613605575f80613573565b633e3f8f735f526004601cfd5b3d833b15171015613623575f613517565b637939f4245f526004601cfd5b60408054604154825163c442b43960e01b81526001600160a01b0394851660048201526024810191909152928391604491839160101c165afa9081156127e8575f9161367b57505190565b613694915060403d604011612ccb57612cbd8183612a73565b5190565b6001600160a01b0360249116805f52600460205260206001600160a01b0360405f205416604051938480926370a0823160e01b82523060048301525afa9081156127e8575f916136fd575b6136fa92505f52600760205260405f205490612b43565b90565b90506020823d60201161372b575b8161371860209383612a73565b81010312610817576136fa9151906136e3565b3d915061370b565b6001600160a01b039081165f908152600460208181526040808420549051635d043b2960e11b81529283019590955294831660248201523060448201529392849260649284929091165af19081156127e8575f916135c5575090565b6001600160a01b03604254166001600160a01b0382169181831461380f576137b96137bf91613698565b91613698565b801561380157670de0b6b3a7640000820291808304670de0b6b3a76400001490151715612b04576137ef91612b18565b905f52604360205260405f2054101590565b5015159050612e2657600190565b505050600190565b61381f6133b0565b5f9060035492604454925b84811061383b575050505050600190565b6001600160a01b0361384c82612ac5565b90549060031b1c166001600160a01b03831614613887578361386e8285612b50565b511161387e576001905b0161382a565b50505050505f90565b600190613878565b6138976133b0565b5f9060035492604454925b8481106138b3575050505050600190565b6001600160a01b036138c482612ac5565b90549060031b1c166001600160a01b038316036138f657836138e68285612b50565b511161387e576001905b016138a2565b6001906138f0565b6001600160a01b0316805f5260046020526001600160a01b0360405f2054166040516318160ddd60e01b8152602081600481855afa9081156127e8575f91613bf2575b506040805163396a590d60e11b81529081600481865afa9081156127e8575f905f92613ba1575b5015613b3d576001600160a01b03604051916370a0823160e01b8352166004820152602081602481875afa9081156127e8575f91613b0b575b505b6040516303d1689d60e11b815260048101839052602081602481875afa80156127e85782915f91613ad6575b5011613ac15750506040516370a0823160e01b815230600482015291602083602481855afa9283156127e8575f93613a8d575b505f52600760205260405f20548210613a19575050565b6020602491604051928380926303d1689d60e11b82528660048301525afa9081156127e8575f91613a5b575b5063b97fded160e01b5f5260045260245260445ffd5b90506020813d602011613a85575b81613a7660209383612a73565b81010312610817575182613a45565b3d9150613a69565b9092506020813d602011613ab9575b81613aa960209383612a73565b810103126108175751915f613a02565b3d9150613a9c565b63b97fded160e01b5f5260045260245260445ffd5b9150506020813d602011613b03575b81613af260209383612a73565b81010312610817578190515f6139cf565b3d9150613ae5565b90506020813d602011613b35575b81613b2660209383612a73565b8101031261081757515f6139a1565b3d9150613b19565b506040516370a0823160e01b815260048101839052602081602481875afa9081156127e8575f91613b6f575b506139a3565b90506020813d602011613b99575b81613b8a60209383612a73565b8101031261081757515f613b69565b3d9150613b7d565b9150506040813d604011613bea575b81613bbd60409383612a73565b81010312610817576020613bd082612b64565b910151906001600160a01b0382168203610817575f613968565b3d9150613bb0565b90506020813d602011613c1c575b81613c0d60209383612a73565b8101031261081757515f613941565b3d9150613c00565b6001600160a01b0381165f9081527fb7db2dd08fcb62d0c9e08c51941cae53c267786a0b75803fb7960902fc8ef97d602052604090205460ff16613cce576001600160a01b03165f8181527fb7db2dd08fcb62d0c9e08c51941cae53c267786a0b75803fb7960902fc8ef97d60205260408120805460ff191660011790553391907f2f8788117e7eff1d82e926ec794901d17c78024a50270940304540a733656f0d8180a4600190565b505f90565b805f525f8051602061446383398151915260205260405f206001600160a01b0383165f5260205260ff60405f205416155f14612c9d57805f525f8051602061446383398151915260205260405f206001600160a01b0383165f5260205260405f20600160ff198254161790556001600160a01b03339216907f2f8788117e7eff1d82e926ec794901d17c78024a50270940304540a733656f0d5f80a4600190565b6003545f9291835b828110613d8857505050565b6001600160a01b03613d9982612ac5565b90549060031b1c166001600160a01b03831614613db857600101613d7c565b93505050565b9060ff8091169116039060ff8211612b0457565b60ff16604d8111612b0457600a0a90565b60ff821660128114613e2a5760121015613e1357613e0d613e0860126136fa94613dbe565b613dd2565b90612b18565b613e24613e086136fa936012613dbe565b90612af1565b50905090565b91908015612e5857613e4281846134d9565b926001600160a01b03811692835f52603b602052613ea2613e7b670de0b6b3a7640000613e7360405f205489612af1565b048097612b43565b613e9b670de0b6b3a7640000613e93603a5484612af1565b048092612b43565b908461434e565b6001600160a01b036039541691823b15610817576040516340c10f1960e01b81526001600160a01b038316600482015260248101879052925f908490818381604481015b03925af19081156127e8576001600160a01b0393613f0992613f40575b506138fe565b60405192835284602084015216907f5bf1ae7c4c81d8f51bd6f74471f2622d4952dfeca98ba4f9f0da428b2f84edf560403392a490565b5f613f4a91612a73565b5f613f03565b91908015612e5857613f6281846134d9565b926001600160a01b03811692835f52603b602052613f93613e7b670de0b6b3a7640000613e7360405f205489612af1565b613f9c8261378f565b15613082576001600160a01b036039541691823b15610817576040516340c10f1960e01b81526001600160a01b038316600482015260248101879052925f90849081838160448101613ee6565b805f525f8051602061446383398151915260205260405f206001600160a01b0383165f5260205260ff60405f2054165f14612c9d57805f525f8051602061446383398151915260205260405f206001600160a01b0383165f5260205260405f2060ff1981541690556001600160a01b03339216907ff6391f5c32d9c69d2a47ea670b442974b53935d1edc7fd64eb21e047a839171b5f80a4600190565b9190916001600160a01b03831690815f52600660205260405f206001600160a01b0382165f526020526001600160a01b0360405f2054911690815f526004602052602460206001600160a01b0360405f205416604051928380926303d1689d60e11b82528660048301525afa9081156127e8575f91614207575b50156141ff5760206141855f96858852600683526040882085895283528760408120558488526007835260408820614139858254612b43565b905584885260048084526040808a20549051635d043b2960e11b81529182018690526001600160a01b039283166024830152306044830152909889939190921691839182906064820190565b03925af19485156127e8575f956141ca575b5060407fd7df942c87bc524ccb17249639699aa79ede0bfadd2bea503ce279f993479150918682519182526020820152a3565b9094506020813d6020116141f7575b816141e660209383612a73565b810103126108175751936040614197565b3d91506141d9565b505f93505050565b90506020813d602011614231575b8161422260209383612a73565b8101031261081757515f614100565b3d9150614215565b9291908015614347576001600160a01b03841691825f52603c602052670de0b6b3a764000061426c60405f205484612af1565b04906142788284612b43565b614290670de0b6b3a7640000613e93603a5484612af1565b906001600160a01b0360395416803b1561081757604051632770a7eb60e21b815233600482015260248101879052905f908290604490829084905af19485156127e8576142f5614301946142fb9487946001600160a01b0399614337575b508c61434e565b89613733565b966138fe565b60405192868452602084015216907f86a824fd3cfb472a44138f5f76db0a4642f87a88cee775c39d3f865f4c03bdaa60403392a4565b5f61434191612a73565b5f6142ee565b505f925050565b90806143ac575b508161435f575050565b6001600160a01b039081600154165f52600660205260405f208282165f5260205260405f2061438f848254612b36565b9055165f5260076020526143a860405f20918254612b36565b9055565b6143c3906001600160a01b03600254169083613733565b505f614355565b60ff7ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005460401c16156143f957565b631afcd79f60e31b5f5260045ffd5b9061442c575080511561441d57805190602001fd5b63d6bda27560e01b5f5260045ffd5b81511580614459575b61443d575090565b6001600160a01b0390639996b31560e01b5f521660045260245ffd5b50803b1561443556fe02dd7bc7dec4dceedda775e58dd541e08a116c6c53815c0bd028192f7b626800cd5ed15c6e187e77e9aee88184c21f4f2182ab5827cb3b7e07fbedcd63f03300a164736f6c634300081a000a
Loading...
Loading
Loading...
Loading
Multichain Portfolio | 34 Chains
Chain | Token | Portfolio % | Price | Amount | Value |
---|
Loading...
Loading
Loading...
Loading
[ Download: CSV Export ]
A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.