Overview
S Balance
0 S
S Value
-More Info
Private Name Tags
ContractCreator
Latest 1 internal transaction
Parent Transaction Hash | Block | From | To | |||
---|---|---|---|---|---|---|
368135 | 8 days ago | Contract Creation | 0 S |
Loading...
Loading
Contract Source Code Verified (Exact Match)
Contract Name:
VaultAdmin
Compiler Version
v0.8.26+commit.8a97fa7a
Optimization Enabled:
Yes with 9999 runs
Other Settings:
cancun EvmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol"; import { IERC4626 } from "@openzeppelin/contracts/interfaces/IERC4626.sol"; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { IProtocolFeeController } from "@balancer-labs/v3-interfaces/contracts/vault/IProtocolFeeController.sol"; import { IAuthorizer } from "@balancer-labs/v3-interfaces/contracts/vault/IAuthorizer.sol"; import { IVaultAdmin } from "@balancer-labs/v3-interfaces/contracts/vault/IVaultAdmin.sol"; import { Rounding } from "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol"; import { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol"; import { PackedTokenBalance } from "@balancer-labs/v3-solidity-utils/contracts/helpers/PackedTokenBalance.sol"; import { EVMCallModeHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/EVMCallModeHelpers.sol"; import { Authentication } from "@balancer-labs/v3-solidity-utils/contracts/helpers/Authentication.sol"; import { FixedPoint } from "@balancer-labs/v3-solidity-utils/contracts/math/FixedPoint.sol"; import { VaultStateBits, VaultStateLib } from "./lib/VaultStateLib.sol"; import { PoolConfigLib, PoolConfigBits } from "./lib/PoolConfigLib.sol"; import { VaultExtensionsLib } from "./lib/VaultExtensionsLib.sol"; import { VaultCommon } from "./VaultCommon.sol"; import { VaultGuard } from "./VaultGuard.sol"; /** * @dev Bytecode extension for the Vault containing permissioned functions. Complementary to `VaultExtension`, * it has access to the same storage layout as the main vault. * * The functions in this contract are not meant to be called directly. They must only be called by the Vault * via delegate calls, so that any state modifications produced by this contract's code will actually target * the main Vault's state. * * The storage of this contract is in practice unused. */ contract VaultAdmin is IVaultAdmin, VaultCommon, Authentication, VaultGuard { using PackedTokenBalance for bytes32; using PoolConfigLib for PoolConfigBits; using VaultStateLib for VaultStateBits; using VaultExtensionsLib for IVault; using FixedPoint for uint256; using SafeERC20 for IERC20; using SafeCast for *; // Minimum BPT amount minted upon initialization. uint256 internal constant _BUFFER_MINIMUM_TOTAL_SUPPLY = 1e4; /// @dev Functions with this modifier can only be delegate-called by the Vault. modifier onlyVaultDelegateCall() { _vault.ensureVaultDelegateCall(); _; } /// @dev Functions with this modifier can only be called by the pool creator. modifier onlyProtocolFeeController() { if (msg.sender != address(_protocolFeeController)) { revert SenderNotAllowed(); } _; } /// @dev Validate aggregate percentage values. modifier withValidPercentage(uint256 aggregatePercentage) { if (aggregatePercentage > FixedPoint.ONE) { revert ProtocolFeesExceedTotalCollected(); } _; } constructor( IVault mainVault, uint32 pauseWindowDuration, uint32 bufferPeriodDuration, uint256 minTradeAmount, uint256 minWrapAmount ) Authentication(bytes32(uint256(uint160(address(mainVault))))) VaultGuard(mainVault) { if (pauseWindowDuration > _MAX_PAUSE_WINDOW_DURATION) { revert VaultPauseWindowDurationTooLarge(); } if (bufferPeriodDuration > _MAX_BUFFER_PERIOD_DURATION) { revert PauseBufferPeriodDurationTooLarge(); } // solhint-disable-next-line not-rely-on-time uint32 pauseWindowEndTime = (block.timestamp + pauseWindowDuration).toUint32(); _vaultPauseWindowEndTime = pauseWindowEndTime; _vaultBufferPeriodDuration = bufferPeriodDuration; _vaultBufferPeriodEndTime = pauseWindowEndTime + bufferPeriodDuration; _MINIMUM_TRADE_AMOUNT = minTradeAmount; _MINIMUM_WRAP_AMOUNT = minWrapAmount; } /******************************************************************************* Constants and immutables *******************************************************************************/ /// @inheritdoc IVaultAdmin function vault() external view returns (IVault) { return _vault; } /// @inheritdoc IVaultAdmin function getPauseWindowEndTime() external view returns (uint32) { return _vaultPauseWindowEndTime; } /// @inheritdoc IVaultAdmin function getBufferPeriodDuration() external view returns (uint32) { return _vaultBufferPeriodDuration; } /// @inheritdoc IVaultAdmin function getBufferPeriodEndTime() external view returns (uint32) { return _vaultBufferPeriodEndTime; } /// @inheritdoc IVaultAdmin function getMinimumPoolTokens() external pure returns (uint256) { return _MIN_TOKENS; } /// @inheritdoc IVaultAdmin function getMaximumPoolTokens() external pure returns (uint256) { return _MAX_TOKENS; } /// @inheritdoc IVaultAdmin function getPoolMinimumTotalSupply() external pure returns (uint256) { return _POOL_MINIMUM_TOTAL_SUPPLY; } /// @inheritdoc IVaultAdmin function getBufferMinimumTotalSupply() external pure returns (uint256) { return _BUFFER_MINIMUM_TOTAL_SUPPLY; } /// @inheritdoc IVaultAdmin function getMinimumTradeAmount() external view returns (uint256) { return _MINIMUM_TRADE_AMOUNT; } /// @inheritdoc IVaultAdmin function getMinimumWrapAmount() external view returns (uint256) { return _MINIMUM_WRAP_AMOUNT; } /******************************************************************************* Vault Pausing *******************************************************************************/ /// @inheritdoc IVaultAdmin function isVaultPaused() external view onlyVaultDelegateCall returns (bool) { return _isVaultPaused(); } /// @inheritdoc IVaultAdmin function getVaultPausedState() external view onlyVaultDelegateCall returns (bool, uint32, uint32) { return (_isVaultPaused(), _vaultPauseWindowEndTime, _vaultBufferPeriodEndTime); } /// @inheritdoc IVaultAdmin function pauseVault() external onlyVaultDelegateCall authenticate { _setVaultPaused(true); } /// @inheritdoc IVaultAdmin function unpauseVault() external onlyVaultDelegateCall authenticate { _setVaultPaused(false); } /** * @dev The contract can only be paused until the end of the Pause Window, and * unpaused until the end of the Buffer Period. */ function _setVaultPaused(bool pausing) internal { if (_isVaultPaused()) { if (pausing) { // Already paused, and we're trying to pause it again. revert VaultPaused(); } // The Vault can always be unpaused while it's paused. // When the buffer period expires, `_isVaultPaused` will return false, so we would be in the outside // else clause, where trying to unpause will revert unconditionally. } else { if (pausing) { // Not already paused; we can pause within the window. // solhint-disable-next-line not-rely-on-time if (block.timestamp >= _vaultPauseWindowEndTime) { revert VaultPauseWindowExpired(); } } else { // Not paused, and we're trying to unpause it. revert VaultNotPaused(); } } VaultStateBits vaultState = _vaultStateBits; vaultState = vaultState.setVaultPaused(pausing); _vaultStateBits = vaultState; emit VaultPausedStateChanged(pausing); } /******************************************************************************* Pool Pausing *******************************************************************************/ /// @inheritdoc IVaultAdmin function pausePool(address pool) external onlyVaultDelegateCall withRegisteredPool(pool) { _setPoolPaused(pool, true); } /// @inheritdoc IVaultAdmin function unpausePool(address pool) external onlyVaultDelegateCall withRegisteredPool(pool) { _setPoolPaused(pool, false); } function _setPoolPaused(address pool, bool pausing) internal { _ensureAuthenticatedByRole(pool, _poolRoleAccounts[pool].pauseManager); PoolConfigBits config = _poolConfigBits[pool]; if (_isPoolPaused(pool)) { if (pausing) { // Already paused, and we're trying to pause it again. revert PoolPaused(pool); } // The pool can always be unpaused while it's paused. // When the buffer period expires, `_isPoolPaused` will return false, so we would be in the outside // else clause, where trying to unpause will revert unconditionally. } else { if (pausing) { // Not already paused; we can pause within the window. // solhint-disable-next-line not-rely-on-time if (block.timestamp >= config.getPauseWindowEndTime()) { revert PoolPauseWindowExpired(pool); } } else { // Not paused, and we're trying to unpause it. revert PoolNotPaused(pool); } } // Update poolConfigBits. _poolConfigBits[pool] = config.setPoolPaused(pausing); emit PoolPausedStateChanged(pool, pausing); } /******************************************************************************* Fees *******************************************************************************/ /// @inheritdoc IVaultAdmin function setStaticSwapFeePercentage( address pool, uint256 swapFeePercentage ) external onlyVaultDelegateCall withRegisteredPool(pool) { _ensureAuthenticatedByExclusiveRole(pool, _poolRoleAccounts[pool].swapFeeManager); _ensureUnpaused(pool); _setStaticSwapFeePercentage(pool, swapFeePercentage); } /// @inheritdoc IVaultAdmin function collectAggregateFees( address pool ) public onlyVaultDelegateCall onlyWhenUnlocked onlyProtocolFeeController withRegisteredPool(pool) returns (uint256[] memory totalSwapFees, uint256[] memory totalYieldFees) { IERC20[] memory poolTokens = _vault.getPoolTokens(pool); uint256 numTokens = poolTokens.length; totalSwapFees = new uint256[](numTokens); totalYieldFees = new uint256[](numTokens); for (uint256 i = 0; i < poolTokens.length; ++i) { IERC20 token = poolTokens[i]; (totalSwapFees[i], totalYieldFees[i]) = _aggregateFeeAmounts[pool][token].fromPackedBalance(); if (totalSwapFees[i] > 0 || totalYieldFees[i] > 0) { // Supply credit for the total amount of fees. _aggregateFeeAmounts[pool][token] = 0; _supplyCredit(token, totalSwapFees[i] + totalYieldFees[i]); } } } /// @inheritdoc IVaultAdmin function updateAggregateSwapFeePercentage( address pool, uint256 newAggregateSwapFeePercentage ) external onlyVaultDelegateCall withRegisteredPool(pool) withValidPercentage(newAggregateSwapFeePercentage) onlyProtocolFeeController { _poolConfigBits[pool] = _poolConfigBits[pool].setAggregateSwapFeePercentage(newAggregateSwapFeePercentage); emit AggregateSwapFeePercentageChanged(pool, newAggregateSwapFeePercentage); } /// @inheritdoc IVaultAdmin function updateAggregateYieldFeePercentage( address pool, uint256 newAggregateYieldFeePercentage ) external onlyVaultDelegateCall withRegisteredPool(pool) withValidPercentage(newAggregateYieldFeePercentage) onlyProtocolFeeController { _poolConfigBits[pool] = _poolConfigBits[pool].setAggregateYieldFeePercentage(newAggregateYieldFeePercentage); emit AggregateYieldFeePercentageChanged(pool, newAggregateYieldFeePercentage); } /// @inheritdoc IVaultAdmin function setProtocolFeeController( IProtocolFeeController newProtocolFeeController ) external onlyVaultDelegateCall authenticate nonReentrant { _protocolFeeController = newProtocolFeeController; emit ProtocolFeeControllerChanged(newProtocolFeeController); } /******************************************************************************* Recovery Mode *******************************************************************************/ /// @inheritdoc IVaultAdmin function enableRecoveryMode(address pool) external onlyVaultDelegateCall withRegisteredPool(pool) { _ensurePoolNotInRecoveryMode(pool); // If the Vault or pool is pausable (and currently paused), this call is permissionless. if (_isPoolPaused(pool) == false && _isVaultPaused() == false) { // If not permissionless, authenticate with governance. _authenticateCaller(); } _setPoolRecoveryMode(pool, true); } /// @inheritdoc IVaultAdmin function disableRecoveryMode(address pool) external onlyVaultDelegateCall withRegisteredPool(pool) authenticate { _ensurePoolInRecoveryMode(pool); _setPoolRecoveryMode(pool, false); } /** * @dev Reverts if the pool is in recovery mode. * @param pool The pool */ function _ensurePoolNotInRecoveryMode(address pool) internal view { if (_isPoolInRecoveryMode(pool)) { revert PoolInRecoveryMode(pool); } } /** * @dev Change the recovery mode state of a pool, and emit an event. Assumes any validation (e.g., whether * the proposed state change is consistent) has already been done. * * @param pool The pool * @param recoveryMode The desired recovery mode state */ function _setPoolRecoveryMode(address pool, bool recoveryMode) internal { if (recoveryMode == false) { _syncPoolBalancesAfterRecoveryMode(pool); } // Update poolConfigBits. `_writePoolBalancesToStorage` updates *only* balances, not yield fees, which are // forfeited during Recovery Mode. To prevent yield fees from being charged, `_loadPoolData` must be called // while still in Recovery Mode, so updating the Recovery Mode bit must be done last, after the accounting. _poolConfigBits[pool] = _poolConfigBits[pool].setPoolInRecoveryMode(recoveryMode); emit PoolRecoveryModeStateChanged(pool, recoveryMode); } /** * @dev Raw and live balances will diverge as tokens are withdrawn during Recovery Mode. Live balances cannot * be updated in Recovery Mode, as this would require making external calls to update rates, which could fail. * When Recovery Mode is disabled, re-sync the balances. */ function _syncPoolBalancesAfterRecoveryMode(address pool) private nonReentrant { _writePoolBalancesToStorage(pool, _loadPoolData(pool, Rounding.ROUND_DOWN)); } /******************************************************************************* Query Functionality *******************************************************************************/ /// @inheritdoc IVaultAdmin function disableQuery() external onlyVaultDelegateCall authenticate { _disableQuery(); } /// @inheritdoc IVaultAdmin function disableQueryPermanently() external onlyVaultDelegateCall authenticate { _queriesDisabledPermanently = true; _disableQuery(); } function _disableQuery() internal { VaultStateBits vaultState = _vaultStateBits; vaultState = vaultState.setQueryDisabled(true); _vaultStateBits = vaultState; emit VaultQueriesDisabled(); } /// @inheritdoc IVaultAdmin function enableQuery() external onlyVaultDelegateCall authenticate { if (_queriesDisabledPermanently) { revert QueriesDisabledPermanently(); } VaultStateBits vaultState = _vaultStateBits; vaultState = vaultState.setQueryDisabled(false); _vaultStateBits = vaultState; emit VaultQueriesEnabled(); } /******************************************************************************* ERC4626 Buffers *******************************************************************************/ /// @inheritdoc IVaultAdmin function areBuffersPaused() external view onlyVaultDelegateCall returns (bool) { return _vaultStateBits.areBuffersPaused(); } /// @inheritdoc IVaultAdmin function pauseVaultBuffers() external onlyVaultDelegateCall authenticate { _setVaultBufferPauseState(true); } /// @inheritdoc IVaultAdmin function unpauseVaultBuffers() external onlyVaultDelegateCall authenticate { _setVaultBufferPauseState(false); } function _setVaultBufferPauseState(bool paused) private { VaultStateBits vaultState = _vaultStateBits; vaultState = vaultState.setBuffersPaused(paused); _vaultStateBits = vaultState; emit VaultBuffersPausedStateChanged(paused); } /// @inheritdoc IVaultAdmin function initializeBuffer( IERC4626 wrappedToken, uint256 amountUnderlyingRaw, uint256 amountWrappedRaw, uint256 minIssuedShares, address sharesOwner ) public onlyVaultDelegateCall onlyWhenUnlocked whenVaultBuffersAreNotPaused nonReentrant returns (uint256 issuedShares) { if (_bufferAssets[wrappedToken] != address(0)) { revert BufferAlreadyInitialized(wrappedToken); } address underlyingToken = wrappedToken.asset(); if (underlyingToken == address(0)) { // Should never happen, but a malicious wrapper could return the zero address and cause the buffer // initialization code to run more than once. revert InvalidUnderlyingToken(wrappedToken); } // Register asset of wrapper, so it cannot change. _bufferAssets[wrappedToken] = underlyingToken; // Take debt for initialization assets. _takeDebt(IERC20(underlyingToken), amountUnderlyingRaw); _takeDebt(wrappedToken, amountWrappedRaw); // Update buffer balances. bytes32 bufferBalances = PackedTokenBalance.toPackedBalance(amountUnderlyingRaw, amountWrappedRaw); _bufferTokenBalances[wrappedToken] = bufferBalances; // At initialization, the initial "BPT rate" is 1, so the `issuedShares` is simply the sum of the initial // buffer token balances, converted to underlying. We use `previewRedeem` to convert wrapped to underlying, // since `redeem` is an EXACT_IN operation that rounds down the result. issuedShares = wrappedToken.previewRedeem(amountWrappedRaw) + amountUnderlyingRaw; _ensureBufferMinimumTotalSupply(issuedShares); // Divide `issuedShares` between the zero address, which receives the minimum supply, and the account // depositing the tokens to initialize the buffer, which receives the balance. issuedShares -= _BUFFER_MINIMUM_TOTAL_SUPPLY; _mintMinimumBufferSupplyReserve(wrappedToken); _mintBufferShares(wrappedToken, sharesOwner, issuedShares); if (issuedShares < minIssuedShares) { revert IssuedSharesBelowMin(issuedShares, minIssuedShares); } emit LiquidityAddedToBuffer(wrappedToken, amountUnderlyingRaw, amountWrappedRaw, bufferBalances); } /// @inheritdoc IVaultAdmin function addLiquidityToBuffer( IERC4626 wrappedToken, uint256 maxAmountUnderlyingInRaw, uint256 maxAmountWrappedInRaw, uint256 exactSharesToIssue, address sharesOwner ) public onlyVaultDelegateCall onlyWhenUnlocked whenVaultBuffersAreNotPaused withInitializedBuffer(wrappedToken) nonReentrant returns (uint256 amountUnderlyingRaw, uint256 amountWrappedRaw) { // Check wrapped token asset correctness. address underlyingToken = wrappedToken.asset(); _ensureCorrectBufferAsset(wrappedToken, underlyingToken); bytes32 bufferBalances = _bufferTokenBalances[wrappedToken]; // To proportionally add liquidity to buffer, we need to calculate the buffer invariant ratio. It's calculated // as the amount of buffer shares the sender wants to issue (which in practice is the value that the sender // will add to the buffer, expressed in underlying token amounts), divided by the total shares of // the buffer. // Multiply the current buffer balance by the invariant ratio to calculate the amount of underlying and wrapped // tokens to add, keeping the proportion of the buffer. uint256 totalShares = _bufferTotalShares[wrappedToken]; amountUnderlyingRaw = bufferBalances.getBalanceRaw().mulDivUp(exactSharesToIssue, totalShares); amountWrappedRaw = bufferBalances.getBalanceDerived().mulDivUp(exactSharesToIssue, totalShares); if (amountUnderlyingRaw > maxAmountUnderlyingInRaw) { revert AmountInAboveMax(IERC20(underlyingToken), amountUnderlyingRaw, maxAmountUnderlyingInRaw); } if (amountWrappedRaw > maxAmountWrappedInRaw) { revert AmountInAboveMax(IERC20(wrappedToken), amountWrappedRaw, maxAmountWrappedInRaw); } // Take debt for assets going into the buffer (wrapped and underlying). _takeDebt(IERC20(underlyingToken), amountUnderlyingRaw); _takeDebt(wrappedToken, amountWrappedRaw); // Add the amountsIn to the current buffer balances. bufferBalances = PackedTokenBalance.toPackedBalance( bufferBalances.getBalanceRaw() + amountUnderlyingRaw, bufferBalances.getBalanceDerived() + amountWrappedRaw ); _bufferTokenBalances[wrappedToken] = bufferBalances; // Mint new shares to the owner. _mintBufferShares(wrappedToken, sharesOwner, exactSharesToIssue); emit LiquidityAddedToBuffer(wrappedToken, amountUnderlyingRaw, amountWrappedRaw, bufferBalances); } function _mintMinimumBufferSupplyReserve(IERC4626 wrappedToken) internal { _bufferTotalShares[wrappedToken] = _BUFFER_MINIMUM_TOTAL_SUPPLY; _bufferLpShares[wrappedToken][address(0)] = _BUFFER_MINIMUM_TOTAL_SUPPLY; emit BufferSharesMinted(wrappedToken, address(0), _BUFFER_MINIMUM_TOTAL_SUPPLY); } function _mintBufferShares(IERC4626 wrappedToken, address to, uint256 amount) internal { if (to == address(0)) { revert BufferSharesInvalidReceiver(); } uint256 newTotalSupply = _bufferTotalShares[wrappedToken] + amount; // This is called on buffer initialization - after the minimum reserve amount has been minted - and during // subsequent adds, when we're increasing it, so we do not really need to check it against the minimum. // We do it anyway out of an abundance of caution, and to preserve symmetry with `_burnBufferShares`. _ensureBufferMinimumTotalSupply(newTotalSupply); _bufferTotalShares[wrappedToken] = newTotalSupply; _bufferLpShares[wrappedToken][to] += amount; emit BufferSharesMinted(wrappedToken, to, amount); } /// @inheritdoc IVaultAdmin function removeLiquidityFromBuffer( IERC4626 wrappedToken, uint256 sharesToRemove, uint256 minAmountUnderlyingOutRaw, uint256 minAmountWrappedOutRaw ) external onlyVaultDelegateCall returns (uint256 removedUnderlyingBalanceRaw, uint256 removedWrappedBalanceRaw) { return abi.decode( _vault.unlock( abi.encodeCall( VaultAdmin.removeLiquidityFromBufferHook, (wrappedToken, sharesToRemove, minAmountUnderlyingOutRaw, minAmountWrappedOutRaw, msg.sender) ) ), (uint256, uint256) ); } /** * @dev Internal hook for `removeLiquidityFromBuffer`. Can only be called by the Vault itself via * `removeLiquidityFromBuffer`, which correctly forwards the real sender as the `sharesOwner`. * This function must be reentrant because it calls the nonReentrant function `sendTo`. However, * since `sendTo` is the only function that makes external calls, `removeLiquidityFromBufferHook` * cannot reenter the Vault. * * @param wrappedToken Address of the wrapped token that implements IERC4626 * @param sharesToRemove Amount of shares to remove from the buffer. Cannot be greater than sharesOwner's * total shares * @param minAmountUnderlyingOutRaw Minimum amount of underlying tokens to receive from the buffer. It is expressed * in underlying token native decimals * @param minAmountWrappedOutRaw Minimum amount of wrapped tokens to receive from the buffer. It is expressed in * wrapped token native decimals * @param sharesOwner Owner of the shares (`msg.sender` for `removeLiquidityFromBuffer` entrypoint) * @return removedUnderlyingBalanceRaw Amount of underlying tokens returned to the user * @return removedWrappedBalanceRaw Amount of wrapped tokens returned to the user */ function removeLiquidityFromBufferHook( IERC4626 wrappedToken, uint256 sharesToRemove, uint256 minAmountUnderlyingOutRaw, uint256 minAmountWrappedOutRaw, address sharesOwner ) external onlyVaultDelegateCall onlyVault onlyWhenUnlocked withInitializedBuffer(wrappedToken) returns (uint256 removedUnderlyingBalanceRaw, uint256 removedWrappedBalanceRaw) { if (_isQueryContext()) { // Increase `sharesOwner` balance to ensure that both the share amount check and the burn function succeed. _queryModeBufferSharesIncrease(wrappedToken, sharesOwner, sharesToRemove); } if (sharesToRemove > _bufferLpShares[wrappedToken][sharesOwner]) { revert NotEnoughBufferShares(); } bytes32 bufferBalances = _bufferTokenBalances[wrappedToken]; uint256 totalShares = _bufferTotalShares[wrappedToken]; removedUnderlyingBalanceRaw = (bufferBalances.getBalanceRaw() * sharesToRemove) / totalShares; removedWrappedBalanceRaw = (bufferBalances.getBalanceDerived() * sharesToRemove) / totalShares; // We get the underlying token stored internally as opposed to calling `asset()` in the wrapped token. // This is to avoid any kind of unnecessary external call; the underlying token is set during initialization // and can't change afterwards, so it is already validated at this point. There is no way to add liquidity // with an asset that differs from the one set during initialization. IERC20 underlyingToken = IERC20(_bufferAssets[wrappedToken]); if (removedUnderlyingBalanceRaw < minAmountUnderlyingOutRaw) { revert AmountInAboveMax(IERC20(underlyingToken), removedUnderlyingBalanceRaw, minAmountUnderlyingOutRaw); } if (removedWrappedBalanceRaw < minAmountWrappedOutRaw) { revert AmountInAboveMax(IERC20(wrappedToken), removedWrappedBalanceRaw, minAmountWrappedOutRaw); } _supplyCredit(underlyingToken, removedUnderlyingBalanceRaw); _supplyCredit(wrappedToken, removedWrappedBalanceRaw); bufferBalances = PackedTokenBalance.toPackedBalance( bufferBalances.getBalanceRaw() - removedUnderlyingBalanceRaw, bufferBalances.getBalanceDerived() - removedWrappedBalanceRaw ); _bufferTokenBalances[wrappedToken] = bufferBalances; // Ensures we cannot drop the supply below the minimum. _burnBufferShares(wrappedToken, sharesOwner, sharesToRemove); // This triggers an external call to itself; the Vault is acting as a Router in this case. // `sendTo` makes external calls (`transfer`) but is non-reentrant. if (removedUnderlyingBalanceRaw > 0) { _vault.sendTo(underlyingToken, sharesOwner, removedUnderlyingBalanceRaw); } if (removedWrappedBalanceRaw > 0) { _vault.sendTo(wrappedToken, sharesOwner, removedWrappedBalanceRaw); } emit LiquidityRemovedFromBuffer( wrappedToken, removedUnderlyingBalanceRaw, removedWrappedBalanceRaw, bufferBalances ); } function _burnBufferShares(IERC4626 wrappedToken, address from, uint256 amount) internal { if (from == address(0)) { revert BufferSharesInvalidOwner(); } uint256 newTotalSupply = _bufferTotalShares[wrappedToken] - amount; // Ensure that the buffer can never be drained below the minimum total supply. _ensureBufferMinimumTotalSupply(newTotalSupply); _bufferTotalShares[wrappedToken] = newTotalSupply; _bufferLpShares[wrappedToken][from] -= amount; emit BufferSharesBurned(wrappedToken, from, amount); } /// @dev For query mode usage only, inside `removeLiquidityFromBuffer`. function _queryModeBufferSharesIncrease(IERC4626 wrappedToken, address to, uint256 amount) internal { // Enforce that this can only be called in a read-only, query context. if (EVMCallModeHelpers.isStaticCall() == false) { revert EVMCallModeHelpers.NotStaticCall(); } // Increase `to` balance to ensure the burn function succeeds during query. _bufferLpShares[wrappedToken][to] += amount; } /// @inheritdoc IVaultAdmin function getBufferAsset( IERC4626 wrappedToken ) external view onlyVaultDelegateCall returns (address underlyingToken) { return _bufferAssets[wrappedToken]; } /// @inheritdoc IVaultAdmin function getBufferOwnerShares( IERC4626 token, address user ) external view onlyVaultDelegateCall returns (uint256 shares) { return _bufferLpShares[token][user]; } /// @inheritdoc IVaultAdmin function getBufferTotalShares(IERC4626 token) external view onlyVaultDelegateCall returns (uint256 shares) { return _bufferTotalShares[token]; } /// @inheritdoc IVaultAdmin function getBufferBalance(IERC4626 token) external view onlyVaultDelegateCall returns (uint256, uint256) { // The first balance is underlying, and the last is wrapped balance. return (_bufferTokenBalances[token].getBalanceRaw(), _bufferTokenBalances[token].getBalanceDerived()); } function _ensureBufferMinimumTotalSupply(uint256 newTotalSupply) private pure { if (newTotalSupply < _BUFFER_MINIMUM_TOTAL_SUPPLY) { revert BufferTotalSupplyTooLow(newTotalSupply); } } /******************************************************************************* Authentication *******************************************************************************/ /// @inheritdoc IVaultAdmin function setAuthorizer(IAuthorizer newAuthorizer) external onlyVaultDelegateCall authenticate { _authorizer = newAuthorizer; emit AuthorizerChanged(newAuthorizer); } /// @dev Authenticate by role; otherwise fall through and check governance. function _ensureAuthenticatedByRole(address pool, address roleAddress) private view { if (msg.sender == roleAddress) { return; } _ensureAuthenticated(pool); } /// @dev Authenticate exclusively by role; caller must match the `roleAddress`, if assigned. function _ensureAuthenticatedByExclusiveRole(address pool, address roleAddress) private view { if (roleAddress == address(0)) { // Defer to governance if no role assigned. _ensureAuthenticated(pool); } else if (msg.sender != roleAddress) { revert SenderNotAllowed(); } } /// @dev Delegate authentication to governance. function _ensureAuthenticated(address pool) private view { bytes32 actionId = getActionId(msg.sig); if (_canPerform(actionId, msg.sender, pool) == false) { revert SenderNotAllowed(); } } /// @dev Access control is delegated to the Authorizer. function _canPerform(bytes32 actionId, address user) internal view override returns (bool) { return _authorizer.canPerform(actionId, user, address(this)); } /// @dev Access control is delegated to the Authorizer. `where` refers to the target contract. function _canPerform(bytes32 actionId, address user, address where) internal view returns (bool) { return _authorizer.canPerform(actionId, user, where); } /******************************************************************************* Default handlers *******************************************************************************/ receive() external payable { revert CannotReceiveEth(); } // solhint-disable no-complex-fallback fallback() external payable { if (msg.value > 0) { revert CannotReceiveEth(); } revert("Not implemented"); } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; /// @notice Simple interface for permissioned calling of external functions. interface IAuthentication { /// @notice The sender does not have permission to call a function. error SenderNotAllowed(); /** * @notice Returns the action identifier associated with the external function described by `selector`. * @param selector The 4-byte selector of the permissioned function * @return actionId The computed actionId */ function getActionId(bytes4 selector) external view returns (bytes32 actionId); }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; /// @notice General interface for token exchange rates. interface IRateProvider { /** * @notice An 18 decimal fixed point number representing the exchange rate of one token to another related token. * @dev The meaning of this rate depends on the context. Note that there may be an error associated with a token * rate, and the caller might require a certain rounding direction to ensure correctness. This (legacy) interface * does not take a rounding direction or return an error, so great care must be taken when interpreting and using * rates in downstream computations. * * @return rate The current token rate */ function getRate() external view returns (uint256 rate); }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; /// @notice Interface to the Vault's permission system. interface IAuthorizer { /** * @notice Returns true if `account` can perform the action described by `actionId` in the contract `where`. * @param actionId Identifier for the action to be performed * @param account Account trying to perform the action * @param where Target contract for the action * @return success True if the action is permitted */ function canPerform(bytes32 actionId, address account, address where) external view returns (bool success); }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; interface IERC20MultiTokenErrors { /** * @notice The total supply of a pool token can't be lower than the absolute minimum. * @param totalSupply The total supply value that was below the minimum */ error PoolTotalSupplyTooLow(uint256 totalSupply); }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; // Explicitly import VaultTypes structs because we expect this interface to be heavily used by external developers. // Internally, when this list gets too long, we usually just do a simple import to keep things tidy. import { TokenConfig, LiquidityManagement, PoolSwapParams, AfterSwapParams, HookFlags, AddLiquidityKind, RemoveLiquidityKind, SwapKind } from "./VaultTypes.sol"; /** * @notice Interface for pool hooks. * @dev Hooks are functions invoked by the Vault at specific points in the flow of each operation. This guarantees that * they are called in the correct order, and with the correct arguments. To maintain this security, these functions * should only be called by the Vault. The recommended way to do this is to derive the hook contract from `BaseHooks`, * then use the `onlyVault` modifier from `VaultGuard`. (See the examples in /pool-hooks.) */ interface IHooks { /*************************************************************************** Register ***************************************************************************/ /** * @notice Hook executed when a pool is registered with a non-zero hooks contract. * @dev Returns true if registration was successful, and false to revert the pool registration. * Make sure this function is properly implemented (e.g. check the factory, and check that the * given pool is from the factory). The Vault address will be msg.sender. * * @param factory Address of the pool factory (contract deploying the pool) * @param pool Address of the pool * @param tokenConfig An array of descriptors for the tokens the pool will manage * @param liquidityManagement Liquidity management flags indicating which functions are enabled * @return success True if the hook allowed the registration, false otherwise */ function onRegister( address factory, address pool, TokenConfig[] memory tokenConfig, LiquidityManagement calldata liquidityManagement ) external returns (bool success); /** * @notice Return the set of hooks implemented by the contract. * @dev The Vault will only call hooks the pool says it supports, and of course only if a hooks contract is defined * (i.e., the `poolHooksContract` in `PoolRegistrationParams` is non-zero). * `onRegister` is the only "mandatory" hook. * * @return hookFlags Flags indicating which hooks the contract supports */ function getHookFlags() external view returns (HookFlags memory hookFlags); /*************************************************************************** Initialize ***************************************************************************/ /** * @notice Hook executed before pool initialization. * @dev Called if the `shouldCallBeforeInitialize` flag is set in the configuration. Hook contracts should use * the `onlyVault` modifier to guarantee this is only called by the Vault. * * @param exactAmountsIn Exact amounts of input tokens * @param userData Optional, arbitrary data sent with the encoded request * @return success True if the pool wishes to proceed with initialization */ function onBeforeInitialize(uint256[] memory exactAmountsIn, bytes memory userData) external returns (bool success); /** * @notice Hook to be executed after pool initialization. * @dev Called if the `shouldCallAfterInitialize` flag is set in the configuration. Hook contracts should use * the `onlyVault` modifier to guarantee this is only called by the Vault. * * @param exactAmountsIn Exact amounts of input tokens * @param bptAmountOut Amount of pool tokens minted during initialization * @param userData Optional, arbitrary data sent with the encoded request * @return success True if the pool accepts the initialization results */ function onAfterInitialize( uint256[] memory exactAmountsIn, uint256 bptAmountOut, bytes memory userData ) external returns (bool success); /*************************************************************************** Add Liquidity ***************************************************************************/ /** * @notice Hook to be executed before adding liquidity. * @dev Called if the `shouldCallBeforeAddLiquidity` flag is set in the configuration. Hook contracts should use * the `onlyVault` modifier to guarantee this is only called by the Vault. * * @param router The address (usually a router contract) that initiated an add liquidity operation on the Vault * @param pool Pool address, used to fetch pool information from the Vault (pool config, tokens, etc.) * @param kind The add liquidity operation type (e.g., proportional, custom) * @param maxAmountsInScaled18 Maximum amounts of input tokens * @param minBptAmountOut Minimum amount of output pool tokens * @param balancesScaled18 Current pool balances, sorted in token registration order * @param userData Optional, arbitrary data sent with the encoded request * @return success True if the pool wishes to proceed with settlement */ function onBeforeAddLiquidity( address router, address pool, AddLiquidityKind kind, uint256[] memory maxAmountsInScaled18, uint256 minBptAmountOut, uint256[] memory balancesScaled18, bytes memory userData ) external returns (bool success); /** * @notice Hook to be executed after adding liquidity. * @dev Called if the `shouldCallAfterAddLiquidity` flag is set in the configuration. The Vault will ignore * `hookAdjustedAmountsInRaw` unless `enableHookAdjustedAmounts` is true. Hook contracts should use the * `onlyVault` modifier to guarantee this is only called by the Vault. * * @param router The address (usually a router contract) that initiated an add liquidity operation on the Vault * @param pool Pool address, used to fetch pool information from the Vault (pool config, tokens, etc.) * @param kind The add liquidity operation type (e.g., proportional, custom) * @param amountsInScaled18 Actual amounts of tokens added, sorted in token registration order * @param amountsInRaw Actual amounts of tokens added, sorted in token registration order * @param bptAmountOut Amount of pool tokens minted * @param balancesScaled18 Current pool balances, sorted in token registration order * @param userData Additional (optional) data provided by the user * @return success True if the pool wishes to proceed with settlement * @return hookAdjustedAmountsInRaw New amountsInRaw, potentially modified by the hook */ function onAfterAddLiquidity( address router, address pool, AddLiquidityKind kind, uint256[] memory amountsInScaled18, uint256[] memory amountsInRaw, uint256 bptAmountOut, uint256[] memory balancesScaled18, bytes memory userData ) external returns (bool success, uint256[] memory hookAdjustedAmountsInRaw); /*************************************************************************** Remove Liquidity ***************************************************************************/ /** * @notice Hook to be executed before removing liquidity. * @dev Called if the `shouldCallBeforeRemoveLiquidity` flag is set in the configuration. Hook contracts should use * the `onlyVault` modifier to guarantee this is only called by the Vault. * * @param router The address (usually a router contract) that initiated a remove liquidity operation on the Vault * @param pool Pool address, used to fetch pool information from the Vault (pool config, tokens, etc.) * @param kind The type of remove liquidity operation (e.g., proportional, custom) * @param maxBptAmountIn Maximum amount of input pool tokens * @param minAmountsOutScaled18 Minimum output amounts, sorted in token registration order * @param balancesScaled18 Current pool balances, sorted in token registration order * @param userData Optional, arbitrary data sent with the encoded request * @return success True if the pool wishes to proceed with settlement */ function onBeforeRemoveLiquidity( address router, address pool, RemoveLiquidityKind kind, uint256 maxBptAmountIn, uint256[] memory minAmountsOutScaled18, uint256[] memory balancesScaled18, bytes memory userData ) external returns (bool success); /** * @notice Hook to be executed after removing liquidity. * @dev Called if the `shouldCallAfterRemoveLiquidity` flag is set in the configuration. The Vault will ignore * `hookAdjustedAmountsOutRaw` unless `enableHookAdjustedAmounts` is true. Hook contracts should use the * `onlyVault` modifier to guarantee this is only called by the Vault. * * @param router The address (usually a router contract) that initiated a remove liquidity operation on the Vault * @param pool Pool address, used to fetch pool information from the Vault (pool config, tokens, etc.) * @param kind The type of remove liquidity operation (e.g., proportional, custom) * @param bptAmountIn Amount of pool tokens to burn * @param amountsOutScaled18 Scaled amount of tokens to receive, sorted in token registration order * @param amountsOutRaw Actual amount of tokens to receive, sorted in token registration order * @param balancesScaled18 Current pool balances, sorted in token registration order * @param userData Additional (optional) data provided by the user * @return success True if the pool wishes to proceed with settlement * @return hookAdjustedAmountsOutRaw New amountsOutRaw, potentially modified by the hook */ function onAfterRemoveLiquidity( address router, address pool, RemoveLiquidityKind kind, uint256 bptAmountIn, uint256[] memory amountsOutScaled18, uint256[] memory amountsOutRaw, uint256[] memory balancesScaled18, bytes memory userData ) external returns (bool success, uint256[] memory hookAdjustedAmountsOutRaw); /*************************************************************************** Swap ***************************************************************************/ /** * @notice Called before a swap to give the Pool an opportunity to perform actions. * @dev Called if the `shouldCallBeforeSwap` flag is set in the configuration. Hook contracts should use the * `onlyVault` modifier to guarantee this is only called by the Vault. * * @param params Swap parameters (see PoolSwapParams for struct definition) * @param pool Pool address, used to get pool information from the Vault (poolData, token config, etc.) * @return success True if the pool wishes to proceed with settlement */ function onBeforeSwap(PoolSwapParams calldata params, address pool) external returns (bool success); /** * @notice Called after a swap to perform further actions once the balances have been updated by the swap. * @dev Called if the `shouldCallAfterSwap` flag is set in the configuration. The Vault will ignore * `hookAdjustedAmountCalculatedRaw` unless `enableHookAdjustedAmounts` is true. Hook contracts should * use the `onlyVault` modifier to guarantee this is only called by the Vault. * * @param params Swap parameters (see above for struct definition) * @return success True if the pool wishes to proceed with settlement * @return hookAdjustedAmountCalculatedRaw New amount calculated, potentially modified by the hook */ function onAfterSwap( AfterSwapParams calldata params ) external returns (bool success, uint256 hookAdjustedAmountCalculatedRaw); /** * @notice Called after `onBeforeSwap` and before the main swap operation, if the pool has dynamic fees. * @dev Called if the `shouldCallComputeDynamicSwapFee` flag is set in the configuration. Hook contracts should use * the `onlyVault` modifier to guarantee this is only called by the Vault. * * @param params Swap parameters (see PoolSwapParams for struct definition) * @param pool Pool address, used to get pool information from the Vault (poolData, token config, etc.) * @param staticSwapFeePercentage 18-decimal FP value of the static swap fee percentage, for reference * @return success True if the pool wishes to proceed with settlement * @return dynamicSwapFeePercentage Value of the swap fee percentage, as an 18-decimal FP value */ function onComputeDynamicSwapFeePercentage( PoolSwapParams calldata params, address pool, uint256 staticSwapFeePercentage ) external view returns (bool success, uint256 dynamicSwapFeePercentage); }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { IVault } from "./IVault.sol"; /// @notice Contract that handles protocol and pool creator fees for the Vault. interface IProtocolFeeController { /** * @notice Emitted when the protocol swap fee percentage is updated. * @param swapFeePercentage The updated protocol swap fee percentage */ event GlobalProtocolSwapFeePercentageChanged(uint256 swapFeePercentage); /** * @notice Emitted when the protocol yield fee percentage is updated. * @param yieldFeePercentage The updated protocol yield fee percentage */ event GlobalProtocolYieldFeePercentageChanged(uint256 yieldFeePercentage); /** * @notice Emitted when the protocol swap fee percentage is updated for a specific pool. * @param pool The pool whose protocol swap fee will be changed * @param swapFeePercentage The updated protocol swap fee percentage */ event ProtocolSwapFeePercentageChanged(address indexed pool, uint256 swapFeePercentage); /** * @notice Emitted when the protocol yield fee percentage is updated for a specific pool. * @param pool The pool whose protocol yield fee will be changed * @param yieldFeePercentage The updated protocol yield fee percentage */ event ProtocolYieldFeePercentageChanged(address indexed pool, uint256 yieldFeePercentage); /** * @notice Emitted when the pool creator swap fee percentage of a pool is updated. * @param pool The pool whose pool creator swap fee will be changed * @param poolCreatorSwapFeePercentage The new pool creator swap fee percentage for the pool */ event PoolCreatorSwapFeePercentageChanged(address indexed pool, uint256 poolCreatorSwapFeePercentage); /** * @notice Emitted when the pool creator yield fee percentage of a pool is updated. * @param pool The pool whose pool creator yield fee will be changed * @param poolCreatorYieldFeePercentage The new pool creator yield fee percentage for the pool */ event PoolCreatorYieldFeePercentageChanged(address indexed pool, uint256 poolCreatorYieldFeePercentage); /** * @notice Logs the collection of protocol swap fees in a specific token and amount. * @dev Note that since charging protocol fees (i.e., distributing tokens between pool and fee balances) occurs * in the Vault, but fee collection happens in the ProtocolFeeController, the swap fees reported here may encompass * multiple operations. * * @param pool The pool on which the swap fee was charged * @param token The token in which the swap fee was charged * @param amount The amount of the token collected in fees */ event ProtocolSwapFeeCollected(address indexed pool, IERC20 indexed token, uint256 amount); /** * @notice Logs the collection of protocol yield fees in a specific token and amount. * @dev Note that since charging protocol fees (i.e., distributing tokens between pool and fee balances) occurs * in the Vault, but fee collection happens in the ProtocolFeeController, the yield fees reported here may encompass * multiple operations. * * @param pool The pool on which the yield fee was charged * @param token The token in which the yield fee was charged * @param amount The amount of the token collected in fees */ event ProtocolYieldFeeCollected(address indexed pool, IERC20 indexed token, uint256 amount); /** * @notice Logs the withdrawal of protocol fees in a specific token and amount. * @param pool The pool from which protocol fees are being withdrawn * @param token The token being withdrawn * @param recipient The recipient of the funds * @param amount The amount of the fee token that was withdrawn */ event ProtocolFeesWithdrawn(address indexed pool, IERC20 indexed token, address indexed recipient, uint256 amount); /** * @notice Logs the withdrawal of pool creator fees in a specific token and amount. * @param pool The pool from which pool creator fees are being withdrawn * @param token The token being withdrawn * @param recipient The recipient of the funds (the pool creator if permissionless, or another account) * @param amount The amount of the fee token that was withdrawn */ event PoolCreatorFeesWithdrawn( address indexed pool, IERC20 indexed token, address indexed recipient, uint256 amount ); /** * @notice Error raised when the protocol swap fee percentage exceeds the maximum allowed value. * @dev Note that this is checked for both the global and pool-specific protocol swap fee percentages. */ error ProtocolSwapFeePercentageTooHigh(); /** * @notice Error raised when the protocol yield fee percentage exceeds the maximum allowed value. * @dev Note that this is checked for both the global and pool-specific protocol yield fee percentages. */ error ProtocolYieldFeePercentageTooHigh(); /** * @notice Error raised if there is no pool creator on a withdrawal attempt from the given pool. * @param pool The pool with no creator */ error PoolCreatorNotRegistered(address pool); /** * @notice Error raised if the wrong account attempts to withdraw pool creator fees. * @param caller The account attempting to withdraw pool creator fees * @param pool The pool the caller tried to withdraw from */ error CallerIsNotPoolCreator(address caller, address pool); /// @notice Error raised when the pool creator swap or yield fee percentage exceeds the maximum allowed value. error PoolCreatorFeePercentageTooHigh(); /** * @notice Get the address of the main Vault contract. * @return vault The Vault address */ function vault() external view returns (IVault); /** * @notice Collects aggregate fees from the Vault for a given pool. * @param pool The pool with aggregate fees */ function collectAggregateFees(address pool) external; /** * @notice Getter for the current global protocol swap fee. * @return protocolSwapFeePercentage The global protocol swap fee percentage */ function getGlobalProtocolSwapFeePercentage() external view returns (uint256 protocolSwapFeePercentage); /** * @notice Getter for the current global protocol yield fee. * @return protocolYieldFeePercentage The global protocol yield fee percentage */ function getGlobalProtocolYieldFeePercentage() external view returns (uint256 protocolYieldFeePercentage); /** * @notice Getter for the current protocol swap fee for a given pool. * @param pool The address of the pool * @return protocolSwapFeePercentage The global protocol swap fee percentage * @return isOverride True if the protocol fee has been overridden */ function getPoolProtocolSwapFeeInfo( address pool ) external view returns (uint256 protocolSwapFeePercentage, bool isOverride); /** * @notice Getter for the current protocol yield fee for a given pool. * @param pool The address of the pool * @return protocolYieldFeePercentage The global protocol yield fee percentage * @return isOverride True if the protocol fee has been overridden */ function getPoolProtocolYieldFeeInfo( address pool ) external view returns (uint256 protocolYieldFeePercentage, bool isOverride); /** * @notice Returns the amount of each pool token allocated to the protocol for withdrawal. * @dev Includes both swap and yield fees. * @param pool The address of the pool on which fees were collected * @return feeAmounts The total amounts of each token available for withdrawal, sorted in token registration order */ function getProtocolFeeAmounts(address pool) external view returns (uint256[] memory feeAmounts); /** * @notice Returns the amount of each pool token allocated to the pool creator for withdrawal. * @dev Includes both swap and yield fees. * @param pool The address of the pool on which fees were collected * @return feeAmounts The total amounts of each token available for withdrawal, sorted in token registration order */ function getPoolCreatorFeeAmounts(address pool) external view returns (uint256[] memory feeAmounts); /** * @notice Returns a calculated aggregate percentage from protocol and pool creator fee percentages. * @dev Not tied to any particular pool; this just performs the low-level "additive fee" calculation. Note that * pool creator fees are calculated based on creatorAndLpFees, and not in totalFees. Since aggregate fees are * stored in the Vault with 24-bit precision, this will truncate any values that require greater precision. * It is expected that pool creators will negotiate with the DAO and agree on reasonable values for these fee * components, but the truncation ensures it will not revert for any valid set of fee percentages. * * See example below: * * tokenOutAmount = 10000; poolSwapFeePct = 10%; protocolFeePct = 40%; creatorFeePct = 60% * totalFees = tokenOutAmount * poolSwapFeePct = 10000 * 10% = 1000 * protocolFees = totalFees * protocolFeePct = 1000 * 40% = 400 * creatorAndLpFees = totalFees - protocolFees = 1000 - 400 = 600 * creatorFees = creatorAndLpFees * creatorFeePct = 600 * 60% = 360 * lpFees (will stay in the pool) = creatorAndLpFees - creatorFees = 600 - 360 = 240 * * @param protocolFeePercentage The protocol portion of the aggregate fee percentage * @param poolCreatorFeePercentage The pool creator portion of the aggregate fee percentage * @return aggregateFeePercentage The computed aggregate percentage */ function computeAggregateFeePercentage( uint256 protocolFeePercentage, uint256 poolCreatorFeePercentage ) external pure returns (uint256 aggregateFeePercentage); /** * @notice Override the protocol swap fee percentage for a specific pool. * @dev This is a permissionless call, and will set the pool's fee to the current global fee, if it is different * from the current value, and the fee is not controlled by governance (i.e., has never been overridden). * * @param pool The pool for which we are setting the protocol swap fee */ function updateProtocolSwapFeePercentage(address pool) external; /** * @notice Override the protocol yield fee percentage for a specific pool. * @dev This is a permissionless call, and will set the pool's fee to the current global fee, if it is different * from the current value, and the fee is not controlled by governance (i.e., has never been overridden). * * @param pool The pool for which we are setting the protocol yield fee */ function updateProtocolYieldFeePercentage(address pool) external; /*************************************************************************** Permissioned Functions ***************************************************************************/ /** * @notice Add pool-specific entries to the protocol swap and yield percentages. * @dev This must be called from the Vault during pool registration. It will initialize the pool to the global * protocol fee percentage values (or 0, if the `protocolFeeExempt` flags is set), and return the initial aggregate * fee percentages, based on an initial pool creator fee of 0. * * @param pool The address of the pool being registered * @param poolCreator The address of the pool creator (or 0 if there won't be a pool creator fee) * @param protocolFeeExempt If true, the pool is initially exempt from protocol fees * @return aggregateSwapFeePercentage The initial aggregate swap fee percentage * @return aggregateYieldFeePercentage The initial aggregate yield fee percentage */ function registerPool( address pool, address poolCreator, bool protocolFeeExempt ) external returns (uint256 aggregateSwapFeePercentage, uint256 aggregateYieldFeePercentage); /** * @notice Set the global protocol swap fee percentage, used by standard pools. * @param newProtocolSwapFeePercentage The new protocol swap fee percentage */ function setGlobalProtocolSwapFeePercentage(uint256 newProtocolSwapFeePercentage) external; /** * @notice Set the global protocol yield fee percentage, used by standard pools. * @param newProtocolYieldFeePercentage The new protocol yield fee percentage */ function setGlobalProtocolYieldFeePercentage(uint256 newProtocolYieldFeePercentage) external; /** * @notice Override the protocol swap fee percentage for a specific pool. * @param pool The address of the pool for which we are setting the protocol swap fee * @param newProtocolSwapFeePercentage The new protocol swap fee percentage for the pool */ function setProtocolSwapFeePercentage(address pool, uint256 newProtocolSwapFeePercentage) external; /** * @notice Override the protocol yield fee percentage for a specific pool. * @param pool The address of the pool for which we are setting the protocol yield fee * @param newProtocolYieldFeePercentage The new protocol yield fee percentage for the pool */ function setProtocolYieldFeePercentage(address pool, uint256 newProtocolYieldFeePercentage) external; /** * @notice Assigns a new pool creator swap fee percentage to the specified pool. * @dev Fees are divided between the protocol, pool creator, and LPs. The pool creator percentage is applied to * the "net" amount after protocol fees, and divides the remainder between the pool creator and LPs. If the * pool creator fee is near 100%, almost none of the fee amount remains in the pool for LPs. * * @param pool The address of the pool for which the pool creator fee will be changed * @param poolCreatorSwapFeePercentage The new pool creator swap fee percentage to apply to the pool */ function setPoolCreatorSwapFeePercentage(address pool, uint256 poolCreatorSwapFeePercentage) external; /** * @notice Assigns a new pool creator yield fee percentage to the specified pool. * @dev Fees are divided between the protocol, pool creator, and LPs. The pool creator percentage is applied to * the "net" amount after protocol fees, and divides the remainder between the pool creator and LPs. If the * pool creator fee is near 100%, almost none of the fee amount remains in the pool for LPs. * * @param pool The address of the pool for which the pool creator fee will be changed * @param poolCreatorYieldFeePercentage The new pool creator yield fee percentage to apply to the pool */ function setPoolCreatorYieldFeePercentage(address pool, uint256 poolCreatorYieldFeePercentage) external; /** * @notice Withdraw collected protocol fees for a given pool (all tokens). This is a permissioned function. * @dev Sends swap and yield protocol fees to the recipient. * @param pool The pool on which fees were collected * @param recipient Address to send the tokens */ function withdrawProtocolFees(address pool, address recipient) external; /** * @notice Withdraw collected protocol fees for a given pool and a given token. This is a permissioned function. * @dev Sends swap and yield protocol fees to the recipient. * @param pool The pool on which fees were collected * @param recipient Address to send the tokens * @param token Token to withdraw */ function withdrawProtocolFeesForToken(address pool, address recipient, IERC20 token) external; /** * @notice Withdraw collected pool creator fees for a given pool. This is a permissioned function. * @dev Sends swap and yield pool creator fees to the recipient. * @param pool The pool on which fees were collected * @param recipient Address to send the tokens */ function withdrawPoolCreatorFees(address pool, address recipient) external; /** * @notice Withdraw collected pool creator fees for a given pool. * @dev Sends swap and yield pool creator fees to the registered poolCreator. Since this is a known and immutable * value, this function is permissionless. * * @param pool The pool on which fees were collected */ function withdrawPoolCreatorFees(address pool) external; }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; /** * @notice Return the minimum/maximum swap fee percentages for a pool. * @dev The Vault does not enforce bounds on swap fee percentages; `IBasePool` implements this interface to ensure * that new pool developers think about and set these bounds according to their specific pool type. * * A minimum swap fee might be necessary to ensure mathematical soundness (e.g., Weighted Pools, which use the power * function in the invariant). A maximum swap fee is general protection for users. With no limits at the Vault level, * a pool could specify a near 100% swap fee, effectively disabling trading. Though there are some use cases, such as * LVR/MEV strategies, where a very high fee makes sense. * * Note that the Vault does ensure that dynamic and aggregate fees are less than 100% to prevent attempting to allocate * more fees than were collected by the operation. The true `MAX_FEE_PERCENTAGE` is defined in VaultTypes.sol, and is * the highest value below 100% that satisfies the precision requirements. */ interface ISwapFeePercentageBounds { /// @return minimumSwapFeePercentage The minimum swap fee percentage for a pool function getMinimumSwapFeePercentage() external view returns (uint256 minimumSwapFeePercentage); /// @return maximumSwapFeePercentage The maximum swap fee percentage for a pool function getMaximumSwapFeePercentage() external view returns (uint256 maximumSwapFeePercentage); }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IAuthentication } from "../solidity-utils/helpers/IAuthentication.sol"; import { IVaultExtension } from "./IVaultExtension.sol"; import { IVaultErrors } from "./IVaultErrors.sol"; import { IVaultEvents } from "./IVaultEvents.sol"; import { IVaultAdmin } from "./IVaultAdmin.sol"; import { IVaultMain } from "./IVaultMain.sol"; /// @notice Composite interface for all Vault operations: swap, add/remove liquidity, and associated queries. interface IVault is IVaultMain, IVaultExtension, IVaultAdmin, IVaultErrors, IVaultEvents, IAuthentication { /// @return vault The main Vault address. function vault() external view override(IVaultAdmin, IVaultExtension) returns (IVault); }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC4626 } from "@openzeppelin/contracts/interfaces/IERC4626.sol"; import { IProtocolFeeController } from "./IProtocolFeeController.sol"; import { IAuthorizer } from "./IAuthorizer.sol"; import { IVault } from "./IVault.sol"; /** * @notice Interface for functions defined on the `VaultAdmin` contract. * @dev `VaultAdmin` is the Proxy extension of `VaultExtension`, and handles the least critical operations, * as two delegate calls add gas to each call. Most of the permissioned calls are here. */ interface IVaultAdmin { /******************************************************************************* Constants and immutables *******************************************************************************/ /** * @notice Returns the main Vault address. * @dev The main Vault contains the entrypoint and main liquidity operation implementations. * @return vault The address of the main Vault */ function vault() external view returns (IVault); /** * @notice Returns the Vault's pause window end time. * @dev This value is immutable, and represents the timestamp after which the Vault can no longer be paused * by governance. Balancer timestamps are 32 bits. * * @return pauseWindowEndTime The timestamp when the Vault's pause window ends */ function getPauseWindowEndTime() external view returns (uint32 pauseWindowEndTime); /** * @notice Returns the Vault's buffer period duration. * @dev This value is immutable. It represents the period during which, if paused, the Vault will remain paused. * This ensures there is time available to address whatever issue caused the Vault to be paused. Balancer * timestamps are 32 bits. * * @return bufferPeriodDuration The length of the buffer period in seconds */ function getBufferPeriodDuration() external view returns (uint32 bufferPeriodDuration); /** * @notice Returns the Vault's buffer period end time. * @dev This value is immutable. If already paused, the Vault can be unpaused until this timestamp. Balancer * timestamps are 32 bits. * * @return bufferPeriodEndTime The timestamp after which the Vault remains permanently unpaused */ function getBufferPeriodEndTime() external view returns (uint32 bufferPeriodEndTime); /** * @notice Get the minimum number of tokens in a pool. * @dev We expect the vast majority of pools to be 2-token. * @return minTokens The minimum token count of a pool */ function getMinimumPoolTokens() external pure returns (uint256 minTokens); /** * @notice Get the maximum number of tokens in a pool. * @return maxTokens The maximum token count of a pool */ function getMaximumPoolTokens() external pure returns (uint256 maxTokens); /** * @notice Get the minimum total supply of pool tokens (BPT) for an initialized pool. * @dev This prevents pools from being completely drained. When the pool is initialized, this minimum amount of BPT * is minted to the zero address. This is an 18-decimal floating point number; BPT are always 18 decimals. * * @return poolMinimumTotalSupply The minimum total supply a pool can have after initialization */ function getPoolMinimumTotalSupply() external pure returns (uint256 poolMinimumTotalSupply); /** * @notice Get the minimum total supply of an ERC4626 wrapped token buffer in the Vault. * @dev This prevents buffers from being completely drained. When the buffer is initialized, this minimum number * of shares is added to the shares resulting from the initial deposit. Buffer total supply accounting is internal * to the Vault, as buffers are not tokenized. * * @return bufferMinimumTotalSupply The minimum total supply a buffer can have after initialization */ function getBufferMinimumTotalSupply() external pure returns (uint256 bufferMinimumTotalSupply); /** * @notice Get the minimum trade amount in a pool operation. * @dev This limit is applied to the 18-decimal "upscaled" amount in any operation (swap, add/remove liquidity). * @return minimumTradeAmount The minimum trade amount as an 18-decimal floating point number */ function getMinimumTradeAmount() external view returns (uint256 minimumTradeAmount); /** * @notice Get the minimum wrap amount in a buffer operation. * @dev This limit is applied to the wrap operation amount, in native underlying token decimals. * @return minimumWrapAmount The minimum wrap amount in native underlying token decimals */ function getMinimumWrapAmount() external view returns (uint256 minimumWrapAmount); /******************************************************************************* Vault Pausing *******************************************************************************/ /** * @notice Indicates whether the Vault is paused. * @dev If the Vault is paused, all non-Recovery Mode state-changing operations on pools will revert. Note that * ERC4626 buffers and the Vault have separate and independent pausing mechanisms. Pausing the Vault does not * also pause buffers (though we anticipate they would likely be paused and unpaused together). Call * `areBuffersPaused` to check the pause state of the buffers. * * @return vaultPaused True if the Vault is paused */ function isVaultPaused() external view returns (bool vaultPaused); /** * @notice Returns the paused status, and end times of the Vault's pause window and buffer period. * @dev Balancer timestamps are 32 bits. * @return vaultPaused True if the Vault is paused * @return vaultPauseWindowEndTime The timestamp of the end of the Vault's pause window * @return vaultBufferPeriodEndTime The timestamp of the end of the Vault's buffer period */ function getVaultPausedState() external view returns (bool vaultPaused, uint32 vaultPauseWindowEndTime, uint32 vaultBufferPeriodEndTime); /** * @notice Pause the Vault: an emergency action which disables all operational state-changing functions on pools. * @dev This is a permissioned function that will only work during the Pause Window set during deployment. * Note that ERC4626 buffer operations have an independent pause mechanism, which is not affected by pausing * the Vault. Custom routers could still wrap/unwrap using buffers while the Vault is paused, unless buffers * are also paused (with `pauseVaultBuffers`). */ function pauseVault() external; /** * @notice Reverse a `pause` operation, and restore Vault pool operations to normal functionality. * @dev This is a permissioned function that will only work on a paused Vault within the Buffer Period set during * deployment. Note that the Vault will automatically unpause after the Buffer Period expires. As noted above, * ERC4626 buffers and Vault operations on pools are independent. Unpausing the Vault does not reverse * `pauseVaultBuffers`. If buffers were also paused, they will remain in that state until explicitly unpaused. */ function unpauseVault() external; /******************************************************************************* Pool Pausing *******************************************************************************/ /** * @notice Pause the Pool: an emergency action which disables all pool functions. * @dev This is a permissioned function that will only work during the Pause Window set during pool factory * deployment. * * @param pool The pool being paused */ function pausePool(address pool) external; /** * @notice Reverse a `pause` operation, and restore the Pool to normal functionality. * @dev This is a permissioned function that will only work on a paused Pool within the Buffer Period set during * deployment. Note that the Pool will automatically unpause after the Buffer Period expires. * * @param pool The pool being unpaused */ function unpausePool(address pool) external; /******************************************************************************* Fees *******************************************************************************/ /** * @notice Assigns a new static swap fee percentage to the specified pool. * @dev This is a permissioned function, disabled if the pool is paused. The swap fee percentage must be within * the bounds specified by the pool's implementation of `ISwapFeePercentageBounds`. * Emits the SwapFeePercentageChanged event. * * @param pool The address of the pool for which the static swap fee will be changed * @param swapFeePercentage The new swap fee percentage to apply to the pool */ function setStaticSwapFeePercentage(address pool, uint256 swapFeePercentage) external; /** * @notice Collects accumulated aggregate swap and yield fees for the specified pool. * @dev Fees are sent to the ProtocolFeeController address. * @param pool The pool on which all aggregate fees should be collected * @return swapFeeAmounts An array with the total swap fees collected, sorted in token registration order * @return yieldFeeAmounts An array with the total yield fees collected, sorted in token registration order */ function collectAggregateFees( address pool ) external returns (uint256[] memory swapFeeAmounts, uint256[] memory yieldFeeAmounts); /** * @notice Update an aggregate swap fee percentage. * @dev Can only be called by the current protocol fee controller. Called when governance overrides a protocol fee * for a specific pool, or to permissionlessly update a pool to a changed global protocol fee value (if the pool's * fee has not previously been set by governance). Ensures the aggregate percentage <= FixedPoint.ONE, and also * that the final value does not lose precision when stored in 24 bits (see `FEE_BITLENGTH` in VaultTypes.sol). * Emits an `AggregateSwapFeePercentageChanged` event. * * @param pool The pool whose swap fee percentage will be updated * @param newAggregateSwapFeePercentage The new aggregate swap fee percentage */ function updateAggregateSwapFeePercentage(address pool, uint256 newAggregateSwapFeePercentage) external; /** * @notice Update an aggregate yield fee percentage. * @dev Can only be called by the current protocol fee controller. Called when governance overrides a protocol fee * for a specific pool, or to permissionlessly update a pool to a changed global protocol fee value (if the pool's * fee has not previously been set by governance). Ensures the aggregate percentage <= FixedPoint.ONE, and also * that the final value does not lose precision when stored in 24 bits (see `FEE_BITLENGTH` in VaultTypes.sol). * Emits an `AggregateYieldFeePercentageChanged` event. * * @param pool The pool whose yield fee percentage will be updated * @param newAggregateYieldFeePercentage The new aggregate yield fee percentage */ function updateAggregateYieldFeePercentage(address pool, uint256 newAggregateYieldFeePercentage) external; /** * @notice Sets a new Protocol Fee Controller for the Vault. * @dev This is a permissioned call. Emits a `ProtocolFeeControllerChanged` event. * @param newProtocolFeeController The address of the new Protocol Fee Controller */ function setProtocolFeeController(IProtocolFeeController newProtocolFeeController) external; /******************************************************************************* Recovery Mode *******************************************************************************/ /** * @notice Enable recovery mode for a pool. * @dev This is a permissioned function. It enables a safe proportional withdrawal, with no external calls. * Since there are no external calls, ensuring that entering Recovery Mode cannot fail, we cannot compute and so * must forfeit any yield fees between the last operation and enabling Recovery Mode. For the same reason, live * balances cannot be updated while in Recovery Mode, as doing so might cause withdrawals to fail. * * @param pool The address of the pool */ function enableRecoveryMode(address pool) external; /** * @notice Disable recovery mode for a pool. * @dev This is a permissioned function. It re-syncs live balances (which could not be updated during * Recovery Mode), forfeiting any yield fees that accrued while enabled. It makes external calls, and could * potentially fail if there is an issue with any associated Rate Providers. * * @param pool The address of the pool */ function disableRecoveryMode(address pool) external; /******************************************************************************* Query Functionality *******************************************************************************/ /** * @notice Disables query functionality on the Vault. Can only be called by governance. * @dev The query functions rely on a specific EVM feature to detect static calls. Query operations are exempt from * settlement constraints, so it's critical that no state changes can occur. We retain the ability to disable * queries in the unlikely event that EVM changes violate its assumptions (perhaps on an L2). * This function can be acted upon as an emergency measure in ambiguous contexts where it's not 100% clear whether * disabling queries is completely necessary; queries can still be re-enabled after this call. */ function disableQuery() external; /** * @notice Disables query functionality permanently on the Vault. Can only be called by governance. * @dev Shall only be used when there is no doubt that queries pose a fundamental threat to the system. */ function disableQueryPermanently() external; /** * @notice Enables query functionality on the Vault. Can only be called by governance. * @dev Only works if queries are not permanently disabled. */ function enableQuery() external; /******************************************************************************* ERC4626 Buffers *******************************************************************************/ /** * @notice Indicates whether the Vault buffers are paused. * @dev When buffers are paused, all buffer operations (i.e., calls on the Router with `isBuffer` true) * will revert. Pausing buffers is reversible. Note that ERC4626 buffers and the Vault have separate and * independent pausing mechanisms. Pausing the Vault does not also pause buffers (though we anticipate they * would likely be paused and unpaused together). Call `isVaultPaused` to check the pause state of the Vault. * * @return buffersPaused True if the Vault buffers are paused */ function areBuffersPaused() external view returns (bool buffersPaused); /** * @notice Pauses native vault buffers globally. * @dev When buffers are paused, it's not possible to add liquidity or wrap/unwrap tokens using the Vault's * `erc4626BufferWrapOrUnwrap` primitive. However, it's still possible to remove liquidity. Currently it's not * possible to pause vault buffers individually. * * This is a permissioned call, and is reversible (see `unpauseVaultBuffers`). Note that the Vault has a separate * and independent pausing mechanism. It is possible to pause the Vault (i.e. pool operations), without affecting * buffers, and vice versa. */ function pauseVaultBuffers() external; /** * @notice Unpauses native vault buffers globally. * @dev When buffers are paused, it's not possible to add liquidity or wrap/unwrap tokens using the Vault's * `erc4626BufferWrapOrUnwrap` primitive. However, it's still possible to remove liquidity. As noted above, * ERC4626 buffers and Vault operations on pools are independent. Unpausing buffers does not reverse `pauseVault`. * If the Vault was also paused, it will remain in that state until explicitly unpaused. * * This is a permissioned call. */ function unpauseVaultBuffers() external; /** * @notice Initializes buffer for the given wrapped token. * @param wrappedToken Address of the wrapped token that implements IERC4626 * @param amountUnderlyingRaw Amount of underlying tokens that will be deposited into the buffer * @param amountWrappedRaw Amount of wrapped tokens that will be deposited into the buffer * @param minIssuedShares Minimum amount of shares to receive from the buffer, expressed in underlying token * native decimals * @param sharesOwner Address that will own the deposited liquidity. Only this address will be able to remove * liquidity from the buffer * @return issuedShares the amount of tokens sharesOwner has in the buffer, expressed in underlying token amounts. * (it is the BPT of an internal ERC4626 buffer). It is expressed in underlying token native decimals. */ function initializeBuffer( IERC4626 wrappedToken, uint256 amountUnderlyingRaw, uint256 amountWrappedRaw, uint256 minIssuedShares, address sharesOwner ) external returns (uint256 issuedShares); /** * @notice Adds liquidity to an internal ERC4626 buffer in the Vault, proportionally. * @dev The buffer needs to be initialized beforehand. * @param wrappedToken Address of the wrapped token that implements IERC4626 * @param maxAmountUnderlyingInRaw Maximum amount of underlying tokens to add to the buffer. It is expressed in * underlying token native decimals * @param maxAmountWrappedInRaw Maximum amount of wrapped tokens to add to the buffer. It is expressed in wrapped * token native decimals * @param exactSharesToIssue The value in underlying tokens that `sharesOwner` wants to add to the buffer, * in underlying token decimals * @param sharesOwner Address that will own the deposited liquidity. Only this address will be able to remove * liquidity from the buffer * @return amountUnderlyingRaw Amount of underlying tokens deposited into the buffer * @return amountWrappedRaw Amount of wrapped tokens deposited into the buffer */ function addLiquidityToBuffer( IERC4626 wrappedToken, uint256 maxAmountUnderlyingInRaw, uint256 maxAmountWrappedInRaw, uint256 exactSharesToIssue, address sharesOwner ) external returns (uint256 amountUnderlyingRaw, uint256 amountWrappedRaw); /** * @notice Removes liquidity from an internal ERC4626 buffer in the Vault. * @dev Only proportional exits are supported, and the sender has to be the owner of the shares. * This function unlocks the Vault just for this operation; it does not work with a Router as an entrypoint. * * Pre-conditions: * - The buffer needs to be initialized. * - sharesOwner is the original msg.sender, it needs to be checked in the Router. That's why * this call is authenticated; only routers approved by the DAO can remove the liquidity of a buffer. * - The buffer needs to have some liquidity and have its asset registered in `_bufferAssets` storage. * * @param wrappedToken Address of the wrapped token that implements IERC4626 * @param sharesToRemove Amount of shares to remove from the buffer. Cannot be greater than sharesOwner's * total shares. It is expressed in underlying token native decimals * @param minAmountUnderlyingOutRaw Minimum amount of underlying tokens to receive from the buffer. It is expressed * in underlying token native decimals * @param minAmountWrappedOutRaw Minimum amount of wrapped tokens to receive from the buffer. It is expressed in * wrapped token native decimals * @return removedUnderlyingBalanceRaw Amount of underlying tokens returned to the user * @return removedWrappedBalanceRaw Amount of wrapped tokens returned to the user */ function removeLiquidityFromBuffer( IERC4626 wrappedToken, uint256 sharesToRemove, uint256 minAmountUnderlyingOutRaw, uint256 minAmountWrappedOutRaw ) external returns (uint256 removedUnderlyingBalanceRaw, uint256 removedWrappedBalanceRaw); /** * @notice Returns the asset registered for a given wrapped token. * @dev The asset can never change after buffer initialization. * @param wrappedToken Address of the wrapped token that implements IERC4626 * @return underlyingToken Address of the underlying token registered for the wrapper; `address(0)` if the buffer * has not been initialized. */ function getBufferAsset(IERC4626 wrappedToken) external view returns (address underlyingToken); /** * @notice Returns the shares (internal buffer BPT) of a liquidity owner: a user that deposited assets * in the buffer. * * @param wrappedToken Address of the wrapped token that implements IERC4626 * @param liquidityOwner Address of the user that owns liquidity in the wrapped token's buffer * @return ownerShares Amount of shares allocated to the liquidity owner, in native underlying token decimals */ function getBufferOwnerShares( IERC4626 wrappedToken, address liquidityOwner ) external view returns (uint256 ownerShares); /** * @notice Returns the supply shares (internal buffer BPT) of the ERC4626 buffer. * @param wrappedToken Address of the wrapped token that implements IERC4626 * @return bufferShares Amount of supply shares of the buffer, in native underlying token decimals */ function getBufferTotalShares(IERC4626 wrappedToken) external view returns (uint256 bufferShares); /** * @notice Returns the amount of underlying and wrapped tokens deposited in the internal buffer of the Vault. * @dev All values are in native token decimals of the wrapped or underlying tokens. * @param wrappedToken Address of the wrapped token that implements IERC4626 * @return underlyingBalanceRaw Amount of underlying tokens deposited into the buffer, in native token decimals * @return wrappedBalanceRaw Amount of wrapped tokens deposited into the buffer, in native token decimals */ function getBufferBalance( IERC4626 wrappedToken ) external view returns (uint256 underlyingBalanceRaw, uint256 wrappedBalanceRaw); /******************************************************************************* Authentication *******************************************************************************/ /** * @notice Sets a new Authorizer for the Vault. * @dev This is a permissioned call. Emits an `AuthorizerChanged` event. * @param newAuthorizer The address of the new authorizer */ function setAuthorizer(IAuthorizer newAuthorizer) external; }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC4626 } from "@openzeppelin/contracts/interfaces/IERC4626.sol"; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; /// @notice Errors are declared inside an interface (namespace) to improve DX with Typechain. interface IVaultErrors { /******************************************************************************* Registration and Initialization *******************************************************************************/ /** * @notice A pool has already been registered. `registerPool` may only be called once. * @param pool The already registered pool */ error PoolAlreadyRegistered(address pool); /** * @notice A pool has already been initialized. `initialize` may only be called once. * @param pool The already initialized pool */ error PoolAlreadyInitialized(address pool); /** * @notice A pool has not been registered. * @param pool The unregistered pool */ error PoolNotRegistered(address pool); /** * @notice A referenced pool has not been initialized. * @param pool The uninitialized pool */ error PoolNotInitialized(address pool); /** * @notice A hook contract rejected a pool on registration. * @param poolHooksContract Address of the hook contract that rejected the pool registration * @param pool Address of the rejected pool * @param poolFactory Address of the pool factory */ error HookRegistrationFailed(address poolHooksContract, address pool, address poolFactory); /** * @notice A token was already registered (i.e., it is a duplicate in the pool). * @param token The duplicate token */ error TokenAlreadyRegistered(IERC20 token); /// @notice The token count is below the minimum allowed. error MinTokens(); /// @notice The token count is above the maximum allowed. error MaxTokens(); /// @notice Invalid tokens (e.g., zero) cannot be registered. error InvalidToken(); /// @notice The token type given in a TokenConfig during pool registration is invalid. error InvalidTokenType(); /// @notice The data in a TokenConfig struct is inconsistent or unsupported. error InvalidTokenConfiguration(); /// @notice Tokens with more than 18 decimals are not supported. error InvalidTokenDecimals(); /** * @notice The token list passed into an operation does not match the pool tokens in the pool. * @param pool Address of the pool * @param expectedToken The correct token at a given index in the pool * @param actualToken The actual token found at that index */ error TokensMismatch(address pool, address expectedToken, address actualToken); /******************************************************************************* Transient Accounting *******************************************************************************/ /// @notice A transient accounting operation completed with outstanding token deltas. error BalanceNotSettled(); /// @notice A user called a Vault function (swap, add/remove liquidity) outside the lock context. error VaultIsNotUnlocked(); /// @notice The pool has returned false to the beforeSwap hook, indicating the transaction should revert. error DynamicSwapFeeHookFailed(); /// @notice The pool has returned false to the beforeSwap hook, indicating the transaction should revert. error BeforeSwapHookFailed(); /// @notice The pool has returned false to the afterSwap hook, indicating the transaction should revert. error AfterSwapHookFailed(); /// @notice The pool has returned false to the beforeInitialize hook, indicating the transaction should revert. error BeforeInitializeHookFailed(); /// @notice The pool has returned false to the afterInitialize hook, indicating the transaction should revert. error AfterInitializeHookFailed(); /// @notice The pool has returned false to the beforeAddLiquidity hook, indicating the transaction should revert. error BeforeAddLiquidityHookFailed(); /// @notice The pool has returned false to the afterAddLiquidity hook, indicating the transaction should revert. error AfterAddLiquidityHookFailed(); /// @notice The pool has returned false to the beforeRemoveLiquidity hook, indicating the transaction should revert. error BeforeRemoveLiquidityHookFailed(); /// @notice The pool has returned false to the afterRemoveLiquidity hook, indicating the transaction should revert. error AfterRemoveLiquidityHookFailed(); /// @notice An unauthorized Router tried to call a permissioned function (i.e., using the Vault's token allowance). error RouterNotTrusted(); /******************************************************************************* Swaps *******************************************************************************/ /// @notice The user tried to swap zero tokens. error AmountGivenZero(); /// @notice The user attempted to swap a token for itself. error CannotSwapSameToken(); /** * @notice The user attempted to operate with a token that is not in the pool. * @param token The unregistered token */ error TokenNotRegistered(IERC20 token); /** * @notice An amount in or out has exceeded the limit specified in the swap request. * @param amount The total amount in or out * @param limit The amount of the limit that has been exceeded */ error SwapLimit(uint256 amount, uint256 limit); /** * @notice A hook adjusted amount in or out has exceeded the limit specified in the swap request. * @param amount The total amount in or out * @param limit The amount of the limit that has been exceeded */ error HookAdjustedSwapLimit(uint256 amount, uint256 limit); /// @notice The amount given or calculated for an operation is below the minimum limit. error TradeAmountTooSmall(); /******************************************************************************* Add Liquidity *******************************************************************************/ /// @notice Add liquidity kind not supported. error InvalidAddLiquidityKind(); /** * @notice A required amountIn exceeds the maximum limit specified for the operation. * @param tokenIn The incoming token * @param amountIn The total token amount in * @param maxAmountIn The amount of the limit that has been exceeded */ error AmountInAboveMax(IERC20 tokenIn, uint256 amountIn, uint256 maxAmountIn); /** * @notice A hook adjusted amountIn exceeds the maximum limit specified for the operation. * @param tokenIn The incoming token * @param amountIn The total token amount in * @param maxAmountIn The amount of the limit that has been exceeded */ error HookAdjustedAmountInAboveMax(IERC20 tokenIn, uint256 amountIn, uint256 maxAmountIn); /** * @notice The BPT amount received from adding liquidity is below the minimum specified for the operation. * @param amountOut The total BPT amount out * @param minAmountOut The amount of the limit that has been exceeded */ error BptAmountOutBelowMin(uint256 amountOut, uint256 minAmountOut); /// @notice Pool does not support adding liquidity with a customized input. error DoesNotSupportAddLiquidityCustom(); /// @notice Pool does not support adding liquidity through donation. error DoesNotSupportDonation(); /******************************************************************************* Remove Liquidity *******************************************************************************/ /// @notice Remove liquidity kind not supported. error InvalidRemoveLiquidityKind(); /** * @notice The actual amount out is below the minimum limit specified for the operation. * @param tokenOut The outgoing token * @param amountOut The total BPT amount out * @param minAmountOut The amount of the limit that has been exceeded */ error AmountOutBelowMin(IERC20 tokenOut, uint256 amountOut, uint256 minAmountOut); /** * @notice The hook adjusted amount out is below the minimum limit specified for the operation. * @param tokenOut The outgoing token * @param amountOut The total BPT amount out * @param minAmountOut The amount of the limit that has been exceeded */ error HookAdjustedAmountOutBelowMin(IERC20 tokenOut, uint256 amountOut, uint256 minAmountOut); /** * @notice The required BPT amount in exceeds the maximum limit specified for the operation. * @param amountIn The total BPT amount in * @param maxAmountIn The amount of the limit that has been exceeded */ error BptAmountInAboveMax(uint256 amountIn, uint256 maxAmountIn); /// @notice Pool does not support removing liquidity with a customized input. error DoesNotSupportRemoveLiquidityCustom(); /******************************************************************************* Fees *******************************************************************************/ /** * @notice Error raised when there is an overflow in the fee calculation. * @dev This occurs when the sum of the parts (aggregate swap or yield fee) is greater than the whole * (total swap or yield fee). Also validated when the protocol fee controller updates aggregate fee * percentages in the Vault. */ error ProtocolFeesExceedTotalCollected(); /** * @notice Error raised when the swap fee percentage is less than the minimum allowed value. * @dev The Vault itself does not impose a universal minimum. Rather, it validates against the * range specified by the `ISwapFeePercentageBounds` interface. and reverts with this error * if it is below the minimum value returned by the pool. * * Pools with dynamic fees do not check these limits. */ error SwapFeePercentageTooLow(); /** * @notice Error raised when the swap fee percentage is greater than the maximum allowed value. * @dev The Vault itself does not impose a universal minimum. Rather, it validates against the * range specified by the `ISwapFeePercentageBounds` interface. and reverts with this error * if it is above the maximum value returned by the pool. * * Pools with dynamic fees do not check these limits. */ error SwapFeePercentageTooHigh(); /** * @notice Primary fee percentages result in an aggregate fee that cannot be stored with the required precision. * @dev Primary fee percentages are 18-decimal values, stored here in 64 bits, and calculated with full 256-bit * precision. However, the resulting aggregate fees are stored in the Vault with 24-bit precision, which * corresponds to 0.00001% resolution (i.e., a fee can be 1%, 1.00001%, 1.00002%, but not 1.000005%). * Disallow setting fees such that there would be precision loss in the Vault, leading to a discrepancy between * the aggregate fee calculated here and that stored in the Vault. */ error FeePrecisionTooHigh(); /// @notice A given percentage is above the maximum (usually a value close to FixedPoint.ONE, or 1e18 wei). error PercentageAboveMax(); /******************************************************************************* Queries *******************************************************************************/ /// @notice A user tried to execute a query operation when they were disabled. error QueriesDisabled(); /// @notice An admin tried to re-enable queries, but they were disabled permanently. error QueriesDisabledPermanently(); /******************************************************************************* Recovery Mode *******************************************************************************/ /** * @notice Cannot enable recovery mode when already enabled. * @param pool The pool */ error PoolInRecoveryMode(address pool); /** * @notice Cannot disable recovery mode when not enabled. * @param pool The pool */ error PoolNotInRecoveryMode(address pool); /******************************************************************************* Authentication *******************************************************************************/ /** * @notice Error indicating the sender is not the Vault (e.g., someone is trying to call a permissioned function). * @param sender The account attempting to call a permissioned function */ error SenderIsNotVault(address sender); /******************************************************************************* Pausing *******************************************************************************/ /// @notice The caller specified a pause window period longer than the maximum. error VaultPauseWindowDurationTooLarge(); /// @notice The caller specified a buffer period longer than the maximum. error PauseBufferPeriodDurationTooLarge(); /// @notice A user tried to perform an operation while the Vault was paused. error VaultPaused(); /// @notice Governance tried to unpause the Vault when it was not paused. error VaultNotPaused(); /// @notice Governance tried to pause the Vault after the pause period expired. error VaultPauseWindowExpired(); /** * @notice A user tried to perform an operation involving a paused Pool. * @param pool The paused pool */ error PoolPaused(address pool); /** * @notice Governance tried to unpause the Pool when it was not paused. * @param pool The unpaused pool */ error PoolNotPaused(address pool); /** * @notice Governance tried to pause a Pool after the pause period expired. * @param pool The pool */ error PoolPauseWindowExpired(address pool); /******************************************************************************* ERC4626 token buffers *******************************************************************************/ /** * @notice The buffer for the given wrapped token was already initialized. * @param wrappedToken The wrapped token corresponding to the buffer */ error BufferAlreadyInitialized(IERC4626 wrappedToken); /** * @notice The buffer for the given wrapped token was not initialized. * @param wrappedToken The wrapped token corresponding to the buffer */ error BufferNotInitialized(IERC4626 wrappedToken); /// @notice The user is trying to remove more than their allocated shares from the buffer. error NotEnoughBufferShares(); /** * @notice The wrapped token asset does not match the underlying token. * @dev This should never happen, but a malicious wrapper contract might not return the correct address. * Legitimate wrapper contracts should make the asset a constant or immutable value. * * @param wrappedToken The wrapped token corresponding to the buffer * @param underlyingToken The underlying token returned by `asset` */ error WrongUnderlyingToken(IERC4626 wrappedToken, address underlyingToken); /** * @notice A wrapped token reported the zero address as its underlying token asset. * @dev This should never happen, but a malicious wrapper contract might do this (e.g., in an attempt to * re-initialize the buffer). * * @param wrappedToken The wrapped token corresponding to the buffer */ error InvalidUnderlyingToken(IERC4626 wrappedToken); /** * @notice The amount given to wrap/unwrap was too small, which can introduce rounding issues. * @param wrappedToken The wrapped token corresponding to the buffer */ error WrapAmountTooSmall(IERC4626 wrappedToken); /// @notice Buffer operation attempted while vault buffers are paused. error VaultBuffersArePaused(); /// @notice Buffer shares were minted to the zero address. error BufferSharesInvalidReceiver(); /// @notice Buffer shares were burned from the zero address. error BufferSharesInvalidOwner(); /** * @notice The total supply of a buffer can't be lower than the absolute minimum. * @param totalSupply The total supply value that was below the minimum */ error BufferTotalSupplyTooLow(uint256 totalSupply); /// @dev A wrap/unwrap operation consumed more or returned less underlying tokens than it should. error NotEnoughUnderlying(IERC4626 wrappedToken, uint256 expectedUnderlyingAmount, uint256 actualUnderlyingAmount); /// @dev A wrap/unwrap operation consumed more or returned less wrapped tokens than it should. error NotEnoughWrapped(IERC4626 wrappedToken, uint256 expectedWrappedAmount, uint256 actualWrappedAmount); /// @dev Shares issued during initialization are below the requested amount. error IssuedSharesBelowMin(uint256 issuedShares, uint256 minIssuedShares); /******************************************************************************* Miscellaneous *******************************************************************************/ /// @notice Pool does not support adding / removing liquidity with an unbalanced input. error DoesNotSupportUnbalancedLiquidity(); /// @notice The contract should not receive ETH. error CannotReceiveEth(); /** * @notice The `VaultExtension` contract was called by an account directly. * @dev It can only be called by the Vault via delegatecall. */ error NotVaultDelegateCall(); /// @notice The `VaultExtension` contract was configured with an incorrect Vault address. error WrongVaultExtensionDeployment(); /// @notice The `ProtocolFeeController` contract was configured with an incorrect Vault address. error WrongProtocolFeeControllerDeployment(); /// @notice The `VaultAdmin` contract was configured with an incorrect Vault address. error WrongVaultAdminDeployment(); /// @notice Quote reverted with a reserved error code. error QuoteResultSpoofed(); }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC4626 } from "@openzeppelin/contracts/interfaces/IERC4626.sol"; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { IProtocolFeeController } from "./IProtocolFeeController.sol"; import { IAuthorizer } from "./IAuthorizer.sol"; import { IHooks } from "./IHooks.sol"; import "./VaultTypes.sol"; /// @dev Events are declared inside an interface (namespace) to improve DX with Typechain. interface IVaultEvents { /** * @notice A Pool was registered by calling `registerPool`. * @param pool The pool being registered * @param factory The factory creating the pool * @param tokenConfig An array of descriptors for the tokens the pool will manage * @param swapFeePercentage The static swap fee of the pool * @param pauseWindowEndTime The pool's pause window end time * @param roleAccounts Addresses the Vault will allow to change certain pool settings * @param hooksConfig Flags indicating which hooks the pool supports and address of hooks contract * @param liquidityManagement Supported liquidity management hook flags */ event PoolRegistered( address indexed pool, address indexed factory, TokenConfig[] tokenConfig, uint256 swapFeePercentage, uint32 pauseWindowEndTime, PoolRoleAccounts roleAccounts, HooksConfig hooksConfig, LiquidityManagement liquidityManagement ); /** * @notice A Pool was initialized by calling `initialize`. * @param pool The pool being initialized */ event PoolInitialized(address indexed pool); /** * @notice A swap has occurred. * @param pool The pool with the tokens being swapped * @param tokenIn The token entering the Vault (balance increases) * @param tokenOut The token leaving the Vault (balance decreases) * @param amountIn Number of tokenIn tokens * @param amountOut Number of tokenOut tokens * @param swapFeePercentage Swap fee percentage applied (can differ if dynamic) * @param swapFeeAmount Swap fee amount paid */ event Swap( address indexed pool, IERC20 indexed tokenIn, IERC20 indexed tokenOut, uint256 amountIn, uint256 amountOut, uint256 swapFeePercentage, uint256 swapFeeAmount ); /** * @notice A wrap operation has occurred. * @param wrappedToken The wrapped token address * @param depositedUnderlying Number of underlying tokens deposited * @param mintedShares Number of shares (wrapped tokens) minted * @param bufferBalances The final buffer balances, packed in 128-bit words (underlying, wrapped) */ event Wrap( IERC4626 indexed wrappedToken, uint256 depositedUnderlying, uint256 mintedShares, bytes32 bufferBalances ); /** * @notice An unwrap operation has occurred. * @param wrappedToken The wrapped token address * @param burnedShares Number of shares (wrapped tokens) burned * @param withdrawnUnderlying Number of underlying tokens withdrawn * @param bufferBalances The final buffer balances, packed in 128-bit words (underlying, wrapped) */ event Unwrap( IERC4626 indexed wrappedToken, uint256 burnedShares, uint256 withdrawnUnderlying, bytes32 bufferBalances ); /** * @notice Liquidity has been added to a pool (including initialization). * @param pool The pool with liquidity added * @param liquidityProvider The user performing the operation * @param kind The add liquidity operation type (e.g., proportional, custom) * @param totalSupply The total supply of the pool after the operation * @param amountsAddedRaw The amount of each token that was added, sorted in token registration order * @param swapFeeAmountsRaw The total swap fees charged, sorted in token registration order */ event LiquidityAdded( address indexed pool, address indexed liquidityProvider, AddLiquidityKind indexed kind, uint256 totalSupply, uint256[] amountsAddedRaw, uint256[] swapFeeAmountsRaw ); /** * @notice Liquidity has been removed from a pool. * @param pool The pool with liquidity removed * @param liquidityProvider The user performing the operation * @param kind The remove liquidity operation type (e.g., proportional, custom) * @param totalSupply The total supply of the pool after the operation * @param amountsRemovedRaw The amount of each token that was removed, sorted in token registration order * @param swapFeeAmountsRaw The total swap fees charged, sorted in token registration order */ event LiquidityRemoved( address indexed pool, address indexed liquidityProvider, RemoveLiquidityKind indexed kind, uint256 totalSupply, uint256[] amountsRemovedRaw, uint256[] swapFeeAmountsRaw ); /** * @notice The Vault's pause status has changed. * @param paused True if the Vault was paused */ event VaultPausedStateChanged(bool paused); /// @notice `disableQuery` has been called on the Vault, disabling query functionality. event VaultQueriesDisabled(); /// @notice `enableQuery` has been called on the Vault, enabling query functionality. event VaultQueriesEnabled(); /** * @notice A Pool's pause status has changed. * @param pool The pool that was just paused or unpaused * @param paused True if the pool was paused */ event PoolPausedStateChanged(address indexed pool, bool paused); /** * @notice Emitted when the swap fee percentage of a pool is updated. * @param swapFeePercentage The new swap fee percentage for the pool */ event SwapFeePercentageChanged(address indexed pool, uint256 swapFeePercentage); /** * @notice Recovery mode has been enabled or disabled for a pool. * @param pool The pool * @param recoveryMode True if recovery mode was enabled */ event PoolRecoveryModeStateChanged(address indexed pool, bool recoveryMode); /** * @notice A protocol or pool creator fee has changed, causing an update to the aggregate swap fee. * @dev The `ProtocolFeeController` will emit an event with the underlying change. * @param pool The pool whose aggregate swap fee percentage changed * @param aggregateSwapFeePercentage The new aggregate swap fee percentage */ event AggregateSwapFeePercentageChanged(address indexed pool, uint256 aggregateSwapFeePercentage); /** * @notice A protocol or pool creator fee has changed, causing an update to the aggregate yield fee. * @dev The `ProtocolFeeController` will emit an event with the underlying change. * @param pool The pool whose aggregate yield fee percentage changed * @param aggregateYieldFeePercentage The new aggregate yield fee percentage */ event AggregateYieldFeePercentageChanged(address indexed pool, uint256 aggregateYieldFeePercentage); /** * @notice A new authorizer is set by `setAuthorizer`. * @param newAuthorizer The address of the new authorizer */ event AuthorizerChanged(IAuthorizer indexed newAuthorizer); /** * @notice A new protocol fee controller is set by `setProtocolFeeController`. * @param newProtocolFeeController The address of the new protocol fee controller */ event ProtocolFeeControllerChanged(IProtocolFeeController indexed newProtocolFeeController); /** * @notice Liquidity was added to an ERC4626 buffer corresponding to the given wrapped token. * @dev The underlying token can be derived from the wrapped token, so it's not included here. * * @param wrappedToken The wrapped token that identifies the buffer * @param amountUnderlying The amount of the underlying token that was deposited * @param amountWrapped The amount of the wrapped token that was deposited * @param bufferBalances The final buffer balances, packed in 128-bit words (underlying, wrapped) */ event LiquidityAddedToBuffer( IERC4626 indexed wrappedToken, uint256 amountUnderlying, uint256 amountWrapped, bytes32 bufferBalances ); /** * @notice Buffer shares were minted for an ERC4626 buffer corresponding to a given wrapped token. * @dev The shares are not tokenized like pool BPT, but accounted for in the Vault. `getBufferOwnerShares` * retrieves the current total shares for a given buffer and address, and `getBufferTotalShares` returns the * "totalSupply" of a buffer. * * @param wrappedToken The wrapped token that identifies the buffer * @param to The owner of the minted shares * @param issuedShares The amount of "internal BPT" shares created */ event BufferSharesMinted(IERC4626 indexed wrappedToken, address indexed to, uint256 issuedShares); /** * @notice Buffer shares were burned for an ERC4626 buffer corresponding to a given wrapped token. * @dev The shares are not tokenized like pool BPT, but accounted for in the Vault. `getBufferOwnerShares` * retrieves the current total shares for a given buffer and address, and `getBufferTotalShares` returns the * "totalSupply" of a buffer. * * @param wrappedToken The wrapped token that identifies the buffer * @param from The owner of the burned shares * @param burnedShares The amount of "internal BPT" shares burned */ event BufferSharesBurned(IERC4626 indexed wrappedToken, address indexed from, uint256 burnedShares); /** * @notice Liquidity was removed from an ERC4626 buffer. * @dev The underlying token can be derived from the wrapped token, so it's not included here. * @param wrappedToken The wrapped token that identifies the buffer * @param amountUnderlying The amount of the underlying token that was withdrawn * @param amountWrapped The amount of the wrapped token that was withdrawn * @param bufferBalances The final buffer balances, packed in 128-bit words (underlying, wrapped) */ event LiquidityRemovedFromBuffer( IERC4626 indexed wrappedToken, uint256 amountUnderlying, uint256 amountWrapped, bytes32 bufferBalances ); /** * @notice The Vault buffers pause status has changed. * @dev If buffers all paused, all buffer operations (i.e., all calls through the Router with `isBuffer` * set to true) will revert. * * @param paused True if the Vault buffers were paused */ event VaultBuffersPausedStateChanged(bool paused); /** * @notice Pools can use this event to emit event data from the Vault. * @param pool Pool address * @param eventKey Event key * @param eventData Encoded event data */ event VaultAuxiliary(address indexed pool, bytes32 indexed eventKey, bytes eventData); }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC4626 } from "@openzeppelin/contracts/interfaces/IERC4626.sol"; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { IAuthorizer } from "./IAuthorizer.sol"; import { IProtocolFeeController } from "./IProtocolFeeController.sol"; import { IVault } from "./IVault.sol"; import { IHooks } from "./IHooks.sol"; import "./VaultTypes.sol"; /** * @notice Interface for functions defined on the `VaultExtension` contract. * @dev `VaultExtension` handles less critical or frequently used functions, since delegate calls through * the Vault are more expensive than direct calls. The main Vault contains the core code for swaps and * liquidity operations. */ interface IVaultExtension { /******************************************************************************* Constants and immutables *******************************************************************************/ /** * @notice Returns the main Vault address. * @dev The main Vault contains the entrypoint and main liquidity operation implementations. * @return vault The address of the main Vault */ function vault() external view returns (IVault); /** * @notice Returns the VaultAdmin contract address. * @dev The VaultAdmin contract mostly implements permissioned functions. * @return vaultAdmin The address of the Vault admin */ function getVaultAdmin() external view returns (address vaultAdmin); /******************************************************************************* Transient Accounting *******************************************************************************/ /** * @notice Returns whether the Vault is unlocked (i.e., executing an operation). * @dev The Vault must be unlocked to perform state-changing liquidity operations. * @return unlocked True if the Vault is unlocked, false otherwise */ function isUnlocked() external view returns (bool unlocked); /** * @notice Returns the count of non-zero deltas. * @return nonzeroDeltaCount The current value of `_nonzeroDeltaCount` */ function getNonzeroDeltaCount() external view returns (uint256 nonzeroDeltaCount); /** * @notice Retrieves the token delta for a specific token. * @dev This function allows reading the value from the `_tokenDeltas` mapping. * @param token The token for which the delta is being fetched * @return tokenDelta The delta of the specified token */ function getTokenDelta(IERC20 token) external view returns (int256 tokenDelta); /** * @notice Retrieves the reserve (i.e., total Vault balance) of a given token. * @param token The token for which to retrieve the reserve * @return reserveAmount The amount of reserves for the given token */ function getReservesOf(IERC20 token) external view returns (uint256 reserveAmount); /** * @notice This flag is used to detect and tax "round-trip" interactions (adding and removing liquidity in the * same pool). * @dev Taxing remove liquidity proportional whenever liquidity was added in the same `unlock` call adds an extra * layer of security, discouraging operations that try to undo others for profit. Remove liquidity proportional * is the only standard way to exit a position without fees, and this flag is used to enable fees in that case. * It also discourages indirect swaps via unbalanced add and remove proportional, as they are expected to be worse * than a simple swap for every pool type. * * @param pool Address of the pool to check * @return liquidityAdded True if liquidity has been added to this pool in the current transaction * Note that there is no `sessionId` argument; it always returns the value for the current (i.e., latest) session. */ function getAddLiquidityCalledFlag(address pool) external view returns (bool liquidityAdded); /******************************************************************************* Pool Registration *******************************************************************************/ /** * @notice Registers a pool, associating it with its factory and the tokens it manages. * @dev A pool can opt-out of pausing by providing a zero value for the pause window, or allow pausing indefinitely * by providing a large value. (Pool pause windows are not limited by the Vault maximums.) The vault defines an * additional buffer period during which a paused pool will stay paused. After the buffer period passes, a paused * pool will automatically unpause. Balancer timestamps are 32 bits. * * A pool can opt out of Balancer governance pausing by providing a custom `pauseManager`. This might be a * multi-sig contract or an arbitrary smart contract with its own access controls, that forwards calls to * the Vault. * * If the zero address is provided for the `pauseManager`, permissions for pausing the pool will default to the * authorizer. * * @param pool The address of the pool being registered * @param tokenConfig An array of descriptors for the tokens the pool will manage * @param swapFeePercentage The initial static swap fee percentage of the pool * @param pauseWindowEndTime The timestamp after which it is no longer possible to pause the pool * @param protocolFeeExempt If true, the pool's initial aggregate fees will be set to 0 * @param roleAccounts Addresses the Vault will allow to change certain pool settings * @param poolHooksContract Contract that implements the hooks for the pool * @param liquidityManagement Liquidity management flags with implemented methods */ function registerPool( address pool, TokenConfig[] memory tokenConfig, uint256 swapFeePercentage, uint32 pauseWindowEndTime, bool protocolFeeExempt, PoolRoleAccounts calldata roleAccounts, address poolHooksContract, LiquidityManagement calldata liquidityManagement ) external; /** * @notice Checks whether a pool is registered. * @param pool Address of the pool to check * @return registered True if the pool is registered, false otherwise */ function isPoolRegistered(address pool) external view returns (bool registered); /** * @notice Initializes a registered pool by adding liquidity; mints BPT tokens for the first time in exchange. * @param pool Address of the pool to initialize * @param to Address that will receive the output BPT * @param tokens Tokens used to seed the pool (must match the registered tokens) * @param exactAmountsIn Exact amounts of input tokens * @param minBptAmountOut Minimum amount of output pool tokens * @param userData Additional (optional) data required for adding initial liquidity * @return bptAmountOut Output pool token amount */ function initialize( address pool, address to, IERC20[] memory tokens, uint256[] memory exactAmountsIn, uint256 minBptAmountOut, bytes memory userData ) external returns (uint256 bptAmountOut); /******************************************************************************* Pool Information *******************************************************************************/ /** * @notice Checks whether a pool is initialized. * @dev An initialized pool can be considered registered as well. * @param pool Address of the pool to check * @return initialized True if the pool is initialized, false otherwise */ function isPoolInitialized(address pool) external view returns (bool initialized); /** * @notice Gets the tokens registered to a pool. * @param pool Address of the pool * @return tokens List of tokens in the pool */ function getPoolTokens(address pool) external view returns (IERC20[] memory tokens); /** * @notice Gets pool token rates. * @dev This function performs external calls if tokens are yield-bearing. All returned arrays are in token * registration order. * * @param pool Address of the pool * @return decimalScalingFactors Conversion factor used to adjust for token decimals for uniform precision in * calculations. FP(1) for 18-decimal tokens * @return tokenRates 18-decimal FP values for rate tokens (e.g., yield-bearing), or FP(1) for standard tokens */ function getPoolTokenRates( address pool ) external view returns (uint256[] memory decimalScalingFactors, uint256[] memory tokenRates); /** * @notice Returns comprehensive pool data for the given pool. * @dev This contains the pool configuration (flags), tokens and token types, rates, scaling factors, and balances. * @param pool The address of the pool * @return poolData The `PoolData` result */ function getPoolData(address pool) external view returns (PoolData memory poolData); /** * @notice Gets the raw data for a pool: tokens, raw balances, scaling factors. * @param pool Address of the pool * @return tokens The pool tokens, sorted in registration order * @return tokenInfo Token info structs (type, rate provider, yield flag), sorted in token registration order * @return balancesRaw Current native decimal balances of the pool tokens, sorted in token registration order * @return lastBalancesLiveScaled18 Last saved live balances, sorted in token registration order */ function getPoolTokenInfo( address pool ) external view returns ( IERC20[] memory tokens, TokenInfo[] memory tokenInfo, uint256[] memory balancesRaw, uint256[] memory lastBalancesLiveScaled18 ); /** * @notice Gets current live balances of a given pool (fixed-point, 18 decimals), corresponding to its tokens in * registration order. * * @param pool Address of the pool * @return balancesLiveScaled18 Token balances after paying yield fees, applying decimal scaling and rates */ function getCurrentLiveBalances(address pool) external view returns (uint256[] memory balancesLiveScaled18); /** * @notice Gets the configuration parameters of a pool. * @dev The `PoolConfig` contains liquidity management and other state flags, fee percentages, the pause window. * @param pool Address of the pool * @return poolConfig The pool configuration as a `PoolConfig` struct */ function getPoolConfig(address pool) external view returns (PoolConfig memory poolConfig); /** * @notice Gets the hooks configuration parameters of a pool. * @dev The `HooksConfig` contains flags indicating which pool hooks are implemented. * @param pool Address of the pool * @return hooksConfig The hooks configuration as a `HooksConfig` struct */ function getHooksConfig(address pool) external view returns (HooksConfig memory hooksConfig); /** * @notice The current rate of a pool token (BPT) = invariant / totalSupply. * @param pool Address of the pool * @return rate BPT rate */ function getBptRate(address pool) external view returns (uint256 rate); /******************************************************************************* Balancer Pool Tokens *******************************************************************************/ /** * @notice Gets the total supply of a given ERC20 token. * @param token The token address * @return tokenTotalSupply Total supply of the token */ function totalSupply(address token) external view returns (uint256 tokenTotalSupply); /** * @notice Gets the balance of an account for a given ERC20 token. * @param token Address of the token * @param account Address of the account * @return tokenBalance Token balance of the account */ function balanceOf(address token, address account) external view returns (uint256 tokenBalance); /** * @notice Gets the allowance of a spender for a given ERC20 token and owner. * @param token Address of the token * @param owner Address of the owner * @param spender Address of the spender * @return tokenAllowance Amount of tokens the spender is allowed to spend */ function allowance(address token, address owner, address spender) external view returns (uint256 tokenAllowance); /** * @notice Approves a spender to spend pool tokens on behalf of sender. * @dev Notice that the pool token address is not included in the params. This function is exclusively called by * the pool contract, so msg.sender is used as the token address. * * @param owner Address of the owner * @param spender Address of the spender * @param amount Amount of tokens to approve * @return success True if successful, false otherwise */ function approve(address owner, address spender, uint256 amount) external returns (bool success); /******************************************************************************* Pool Pausing *******************************************************************************/ /** * @notice Indicates whether a pool is paused. * @dev If a pool is paused, all non-Recovery Mode state-changing operations will revert. * @param pool The pool to be checked * @return poolPaused True if the pool is paused */ function isPoolPaused(address pool) external view returns (bool poolPaused); /** * @notice Returns the paused status, and end times of the Pool's pause window and buffer period. * @dev Note that even when set to a paused state, the pool will automatically unpause at the end of * the buffer period. Balancer timestamps are 32 bits. * * @param pool The pool whose data is requested * @return poolPaused True if the Pool is paused * @return poolPauseWindowEndTime The timestamp of the end of the Pool's pause window * @return poolBufferPeriodEndTime The timestamp after which the Pool unpauses itself (if paused) * @return pauseManager The pause manager, or the zero address */ function getPoolPausedState( address pool ) external view returns (bool poolPaused, uint32 poolPauseWindowEndTime, uint32 poolBufferPeriodEndTime, address pauseManager); /******************************************************************************* ERC4626 Buffers *******************************************************************************/ /** * @notice Checks if the wrapped token has an initialized buffer in the Vault. * @dev An initialized buffer should have an asset registered in the Vault. * @param wrappedToken Address of the wrapped token that implements IERC4626 * @return isBufferInitialized True if the ERC4626 buffer is initialized */ function isERC4626BufferInitialized(IERC4626 wrappedToken) external view returns (bool isBufferInitialized); /** * @notice Gets the registered asset for a given buffer. * @dev To avoid malicious wrappers (e.g., that might potentially change their asset after deployment), routers * should never call `wrapper.asset()` directly, at least without checking it against the asset registered with * the Vault on initialization. * * @param wrappedToken The wrapped token specifying the buffer * @return asset The underlying asset of the wrapped token */ function getERC4626BufferAsset(IERC4626 wrappedToken) external view returns (address asset); /******************************************************************************* Fees *******************************************************************************/ /** * @notice Returns the accumulated swap fees (including aggregate fees) in `token` collected by the pool. * @param pool The address of the pool for which aggregate fees have been collected * @param token The address of the token in which fees have been accumulated * @return swapFeeAmount The total amount of fees accumulated in the specified token */ function getAggregateSwapFeeAmount(address pool, IERC20 token) external view returns (uint256 swapFeeAmount); /** * @notice Returns the accumulated yield fees (including aggregate fees) in `token` collected by the pool. * @param pool The address of the pool for which aggregate fees have been collected * @param token The address of the token in which fees have been accumulated * @return yieldFeeAmount The total amount of fees accumulated in the specified token */ function getAggregateYieldFeeAmount(address pool, IERC20 token) external view returns (uint256 yieldFeeAmount); /** * @notice Fetches the static swap fee percentage for a given pool. * @param pool The address of the pool whose static swap fee percentage is being queried * @return swapFeePercentage The current static swap fee percentage for the specified pool */ function getStaticSwapFeePercentage(address pool) external view returns (uint256 swapFeePercentage); /** * @notice Fetches the role accounts for a given pool (pause manager, swap manager, pool creator) * @param pool The address of the pool whose roles are being queried * @return roleAccounts A struct containing the role accounts for the pool (or 0 if unassigned) */ function getPoolRoleAccounts(address pool) external view returns (PoolRoleAccounts memory roleAccounts); /** * @notice Query the current dynamic swap fee percentage of a pool, given a set of swap parameters. * @dev Reverts if the hook doesn't return the success flag set to `true`. * @param pool The pool * @param swapParams The swap parameters used to compute the fee * @return dynamicSwapFeePercentage The dynamic swap fee percentage */ function computeDynamicSwapFeePercentage( address pool, PoolSwapParams memory swapParams ) external view returns (uint256 dynamicSwapFeePercentage); /** * @notice Returns the Protocol Fee Controller address. * @return protocolFeeController Address of the ProtocolFeeController */ function getProtocolFeeController() external view returns (IProtocolFeeController protocolFeeController); /******************************************************************************* Recovery Mode *******************************************************************************/ /** * @notice Checks whether a pool is in Recovery Mode. * @dev Recovery Mode enables a safe proportional withdrawal path, with no external calls. * @param pool Address of the pool to check * @return inRecoveryMode True if the pool is in Recovery Mode, false otherwise */ function isPoolInRecoveryMode(address pool) external view returns (bool inRecoveryMode); /** * @notice Remove liquidity from a pool specifying exact pool tokens in, with proportional token amounts out. * The request is implemented by the Vault without any interaction with the pool, ensuring that * it works the same for all pools, and cannot be disabled by a new pool type. * * @param pool Address of the pool * @param from Address of user to burn pool tokens from * @param exactBptAmountIn Input pool token amount * @param minAmountsOut Minimum amounts of tokens to be received, sorted in token registration order * @return amountsOut Actual calculated amounts of output tokens, sorted in token registration order */ function removeLiquidityRecovery( address pool, address from, uint256 exactBptAmountIn, uint256[] memory minAmountsOut ) external returns (uint256[] memory amountsOut); /******************************************************************************* Queries *******************************************************************************/ /** * @notice Performs a callback on msg.sender with arguments provided in `data`. * @dev Used to query a set of operations on the Vault. Only off-chain eth_call are allowed, * anything else will revert. * * Allows querying any operation on the Vault that has the `onlyWhenUnlocked` modifier. * * Allows the external calling of a function via the Vault contract to * access Vault's functions guarded by `onlyWhenUnlocked`. * `transient` modifier ensuring balances changes within the Vault are settled. * * @param data Contains function signature and args to be passed to the msg.sender * @return result Resulting data from the call */ function quote(bytes calldata data) external returns (bytes memory result); /** * @notice Performs a callback on msg.sender with arguments provided in `data`. * @dev Used to query a set of operations on the Vault. Only off-chain eth_call are allowed, * anything else will revert. * * Allows querying any operation on the Vault that has the `onlyWhenUnlocked` modifier. * * Allows the external calling of a function via the Vault contract to * access Vault's functions guarded by `onlyWhenUnlocked`. * `transient` modifier ensuring balances changes within the Vault are settled. * * This call always reverts, returning the result in the revert reason. * * @param data Contains function signature and args to be passed to the msg.sender */ function quoteAndRevert(bytes calldata data) external; /** * @notice Returns true if queries are disabled on the Vault. * @dev If true, queries might either be disabled temporarily or permanently. * @return queryDisabled True if query functionality is reversibly disabled */ function isQueryDisabled() external view returns (bool queryDisabled); /** * @notice Returns true if queries are disabled permanently; false if they are enabled. * @dev This is a one-way switch. Once queries are disabled permanently, they can never be re-enabled. * @return queryDisabledPermanently True if query functionality is permanently disabled */ function isQueryDisabledPermanently() external view returns (bool queryDisabledPermanently); /** * @notice Pools can use this event to emit event data from the Vault. * @param eventKey Event key * @param eventData Encoded event data */ function emitAuxiliaryEvent(bytes32 eventKey, bytes calldata eventData) external; /******************************************************************************* Authentication *******************************************************************************/ /** * @notice Returns the Authorizer address. * @dev The authorizer holds the permissions granted by governance. It is set on Vault deployment, * and can be changed through a permissioned call. * * @return authorizer Address of the authorizer contract */ function getAuthorizer() external view returns (IAuthorizer authorizer); }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "./VaultTypes.sol"; /** * @notice Interface for functions defined on the main Vault contract. * @dev These are generally "critical path" functions (swap, add/remove liquidity) that are in the main contract * for technical or performance reasons. */ interface IVaultMain { /******************************************************************************* Transient Accounting *******************************************************************************/ /** * @notice Creates a context for a sequence of operations (i.e., "unlocks" the Vault). * @dev Performs a callback on msg.sender with arguments provided in `data`. The Callback is `transient`, * meaning all balances for the caller have to be settled at the end. * * @param data Contains function signature and args to be passed to the msg.sender * @return result Resulting data from the call */ function unlock(bytes calldata data) external returns (bytes memory result); /** * @notice Settles deltas for a token; must be successful for the current lock to be released. * @dev Protects the caller against leftover dust in the Vault for the token being settled. The caller * should know in advance how many tokens were paid to the Vault, so it can provide it as a hint to discard any * excess in the Vault balance. * * If the given hint is equal to or higher than the difference in reserves, the difference in reserves is given as * credit to the caller. If it's higher, the caller sent fewer tokens than expected, so settlement would fail. * * If the given hint is lower than the difference in reserves, the hint is given as credit to the caller. * In this case, the excess would be absorbed by the Vault (and reflected correctly in the reserves), but would * not affect settlement. * * The credit supplied by the Vault can be calculated as `min(reserveDifference, amountHint)`, where the reserve * difference equals current balance of the token minus existing reserves of the token when the function is called. * * @param token Address of the token * @param amountHint Amount paid as reported by the caller * @return credit Credit received in return of the payment */ function settle(IERC20 token, uint256 amountHint) external returns (uint256 credit); /** * @notice Sends tokens to a recipient. * @dev There is no inverse operation for this function. Transfer funds to the Vault and call `settle` to cancel * debts. * * @param token Address of the token * @param to Recipient address * @param amount Amount of tokens to send */ function sendTo(IERC20 token, address to, uint256 amount) external; /*************************************************************************** Swaps ***************************************************************************/ /** * @notice Swaps tokens based on provided parameters. * @dev All parameters are given in raw token decimal encoding. * @param vaultSwapParams Parameters for the swap (see above for struct definition) * @return amountCalculatedRaw Calculated swap amount * @return amountInRaw Amount of input tokens for the swap * @return amountOutRaw Amount of output tokens from the swap */ function swap( VaultSwapParams memory vaultSwapParams ) external returns (uint256 amountCalculatedRaw, uint256 amountInRaw, uint256 amountOutRaw); /*************************************************************************** Add Liquidity ***************************************************************************/ /** * @notice Adds liquidity to a pool. * @dev Caution should be exercised when adding liquidity because the Vault has the capability * to transfer tokens from any user, given that it holds all allowances. * * @param params Parameters for the add liquidity (see above for struct definition) * @return amountsIn Actual amounts of input tokens * @return bptAmountOut Output pool token amount * @return returnData Arbitrary (optional) data with an encoded response from the pool */ function addLiquidity( AddLiquidityParams memory params ) external returns (uint256[] memory amountsIn, uint256 bptAmountOut, bytes memory returnData); /*************************************************************************** Remove Liquidity ***************************************************************************/ /** * @notice Removes liquidity from a pool. * @dev Trusted routers can burn pool tokens belonging to any user and require no prior approval from the user. * Untrusted routers require prior approval from the user. This is the only function allowed to call * _queryModeBalanceIncrease (and only in a query context). * * @param params Parameters for the remove liquidity (see above for struct definition) * @return bptAmountIn Actual amount of BPT burned * @return amountsOut Actual amounts of output tokens * @return returnData Arbitrary (optional) data with an encoded response from the pool */ function removeLiquidity( RemoveLiquidityParams memory params ) external returns (uint256 bptAmountIn, uint256[] memory amountsOut, bytes memory returnData); /******************************************************************************* Pool Information *******************************************************************************/ /** * @notice Gets the index of a token in a given pool. * @dev Reverts if the pool is not registered, or if the token does not belong to the pool. * @param pool Address of the pool * @param token Address of the token * @return tokenCount Number of tokens in the pool * @return index Index corresponding to the given token in the pool's token list */ function getPoolTokenCountAndIndexOfToken( address pool, IERC20 token ) external view returns (uint256 tokenCount, uint256 index); /******************************************************************************* Balancer Pool Tokens *******************************************************************************/ /** * @notice Transfers pool token from owner to a recipient. * @dev Notice that the pool token address is not included in the params. This function is exclusively called by * the pool contract, so msg.sender is used as the token address. * * @param owner Address of the owner * @param to Address of the recipient * @param amount Amount of tokens to transfer * @return success True if successful, false otherwise */ function transfer(address owner, address to, uint256 amount) external returns (bool); /** * @notice Transfers pool token from a sender to a recipient using an allowance. * @dev Notice that the pool token address is not included in the params. This function is exclusively called by * the pool contract, so msg.sender is used as the token address. * * @param spender Address allowed to perform the transfer * @param from Address of the sender * @param to Address of the recipient * @param amount Amount of tokens to transfer * @return success True if successful, false otherwise */ function transferFrom(address spender, address from, address to, uint256 amount) external returns (bool success); /******************************************************************************* ERC4626 Buffers *******************************************************************************/ /** * @notice Wraps/unwraps tokens based on the parameters provided. * @dev All parameters are given in raw token decimal encoding. It requires the buffer to be initialized, * and uses the internal wrapped token buffer when it has enough liquidity to avoid external calls. * * @param params Parameters for the wrap/unwrap operation (see struct definition) * @return amountCalculatedRaw Calculated swap amount * @return amountInRaw Amount of input tokens for the swap * @return amountOutRaw Amount of output tokens from the swap */ function erc4626BufferWrapOrUnwrap( BufferWrapOrUnwrapParams memory params ) external returns (uint256 amountCalculatedRaw, uint256 amountInRaw, uint256 amountOutRaw); /******************************************************************************* Miscellaneous *******************************************************************************/ /** * @notice Returns the VaultExtension contract address. * @dev Function is in the main Vault contract. The VaultExtension handles less critical or frequently used * functions, since delegate calls through the Vault are more expensive than direct calls. * * @return vaultExtension Address of the VaultExtension */ function getVaultExtension() external view returns (address vaultExtension); }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { IERC4626 } from "@openzeppelin/contracts/interfaces/IERC4626.sol"; import { IRateProvider } from "../solidity-utils/helpers/IRateProvider.sol"; /** * @notice Represents a pool's liquidity management configuration. * @param disableUnbalancedLiquidity If set, liquidity can only be added or removed proportionally * @param enableAddLiquidityCustom If set, the pool has implemented `onAddLiquidityCustom` * @param enableRemoveLiquidityCustom If set, the pool has implemented `onRemoveLiquidityCustom` * @param enableDonation If set, the pool will not revert if liquidity is added with AddLiquidityKind.DONATION */ struct LiquidityManagement { bool disableUnbalancedLiquidity; bool enableAddLiquidityCustom; bool enableRemoveLiquidityCustom; bool enableDonation; } // @notice Custom type to store the entire configuration of the pool. type PoolConfigBits is bytes32; /** * @notice Represents a pool's configuration (hooks configuration are separated in another struct). * @param liquidityManagement Flags related to adding/removing liquidity * @param staticSwapFeePercentage The pool's native swap fee * @param aggregateSwapFeePercentage The total swap fee charged, including protocol and pool creator components * @param aggregateYieldFeePercentage The total swap fee charged, including protocol and pool creator components * @param tokenDecimalDiffs Compressed storage of the token decimals of each pool token * @param pauseWindowEndTime Timestamp after which the pool cannot be paused * @param isPoolRegistered If true, the pool has been registered with the Vault * @param isPoolInitialized If true, the pool has been initialized with liquidity, and is available for trading * @param isPoolPaused If true, the pool has been paused (by governance or the pauseManager) * @param isPoolInRecoveryMode If true, the pool has been placed in recovery mode, enabling recovery mode withdrawals */ struct PoolConfig { LiquidityManagement liquidityManagement; uint256 staticSwapFeePercentage; uint256 aggregateSwapFeePercentage; uint256 aggregateYieldFeePercentage; uint40 tokenDecimalDiffs; uint32 pauseWindowEndTime; bool isPoolRegistered; bool isPoolInitialized; bool isPoolPaused; bool isPoolInRecoveryMode; } /** * @notice The flag portion of the `HooksConfig`. * @dev `enableHookAdjustedAmounts` must be true for all contracts that modify the `amountCalculated` * in after hooks. Otherwise, the Vault will ignore any "hookAdjusted" amounts. Setting any "shouldCall" * flags to true will cause the Vault to call the corresponding hook during operations. */ struct HookFlags { bool enableHookAdjustedAmounts; bool shouldCallBeforeInitialize; bool shouldCallAfterInitialize; bool shouldCallComputeDynamicSwapFee; bool shouldCallBeforeSwap; bool shouldCallAfterSwap; bool shouldCallBeforeAddLiquidity; bool shouldCallAfterAddLiquidity; bool shouldCallBeforeRemoveLiquidity; bool shouldCallAfterRemoveLiquidity; } /// @notice Represents a hook contract configuration for a pool (HookFlags + hooksContract address). struct HooksConfig { bool enableHookAdjustedAmounts; bool shouldCallBeforeInitialize; bool shouldCallAfterInitialize; bool shouldCallComputeDynamicSwapFee; bool shouldCallBeforeSwap; bool shouldCallAfterSwap; bool shouldCallBeforeAddLiquidity; bool shouldCallAfterAddLiquidity; bool shouldCallBeforeRemoveLiquidity; bool shouldCallAfterRemoveLiquidity; address hooksContract; } /** * @notice Represents temporary state used during a swap operation. * @param indexIn The zero-based index of tokenIn * @param indexOut The zero-based index of tokenOut * @param amountGivenScaled18 The amountGiven (i.e., tokenIn for ExactIn), adjusted for token decimals * @param swapFeePercentage The swap fee to be applied (might be static or dynamic) */ struct SwapState { uint256 indexIn; uint256 indexOut; uint256 amountGivenScaled18; uint256 swapFeePercentage; } /** * @notice Represents the Vault's configuration. * @param isQueryDisabled If set to true, disables query functionality of the Vault. Can be modified by governance * @param isVaultPaused If set to true, swaps and add/remove liquidity operations are halted * @param areBuffersPaused If set to true, the Vault wrap/unwrap primitives associated with buffers will be disabled */ struct VaultState { bool isQueryDisabled; bool isVaultPaused; bool areBuffersPaused; } /** * @notice Represents the accounts holding certain roles for a given pool. This is passed in on pool registration. * @param pauseManager Account empowered to pause/unpause the pool (note that governance can always pause a pool) * @param swapFeeManager Account empowered to set static swap fees for a pool (or 0 to delegate to governance) * @param poolCreator Account empowered to set the pool creator fee (or 0 if all fees go to the protocol and LPs) */ struct PoolRoleAccounts { address pauseManager; address swapFeeManager; address poolCreator; } /******************************************************************************* Tokens *******************************************************************************/ // Note that the following tokens are unsupported by the Vault. This list is not meant to be exhaustive, but covers // many common types of tokens that will not work with the Vault architecture. (See https://github.com/d-xo/weird-erc20 // for examples of token features that are problematic for many protocols.) // // * Rebasing tokens (e.g., aDAI). The Vault keeps track of token balances in its internal accounting; any token whose // balance changes asynchronously (i.e., outside a swap or liquidity operation), would get out-of-sync with this // internal accounting. This category would also include "airdrop" tokens, whose balances can change unexpectedly. // // * Double entrypoint (e.g., old Synthetix tokens, now fixed). These could likewise bypass internal accounting by // registering the token under one address, then accessing it through another. This is especially troublesome // in v3, with the introduction of ERC4626 buffers. // // * Fee on transfer (e.g., PAXG). The Vault issues credits and debits according to given and calculated token amounts, // and settlement assumes that the send/receive transfer functions transfer exactly the given number of tokens. // If this is not the case, transactions will not settle. Unlike with the other types, which are fundamentally // incompatible, it would be possible to design a Router to handle this - but we didn't try it. In any case, it's // not supported in the current Routers. // // * Tokens with more than 18 decimals (e.g., YAM-V2). The Vault handles token scaling: i.e., handling I/O for // amounts in native token decimals, but doing calculations with full 18-decimal precision. This requires reading // and storing the decimals for each token. Since virtually all tokens are 18 or fewer decimals, and we have limited // storage space, 18 was a reasonable maximum. Unlike the other types, this is enforceable by the Vault. Attempting // to register such tokens will revert with `InvalidTokenDecimals`. Of course, we must also be able to read the token // decimals, so the Vault only supports tokens that implement `IERC20Metadata.decimals`, and return a value less than // or equal to 18. // // * Token decimals are checked and stored only once, on registration. Valid tokens store their decimals as immutable // variables or constants. Malicious tokens that don't respect this basic property would not work anywhere in DeFi. // // These types of tokens are supported but discouraged, as they don't tend to play well with AMMs generally. // // * Very low-decimal tokens (e.g., GUSD). The Vault has been extensively tested with 6-decimal tokens (e.g., USDC), // but going much below that may lead to unanticipated effects due to precision loss, especially with smaller trade // values. // // * Revert on zero value approval/transfer. The Vault has been tested against these, but peripheral contracts, such // as hooks, might not have been designed with this in mind. // // * Other types from "weird-erc20," such as upgradeable, pausable, or tokens with blocklists. We have seen cases // where a token upgrade fails, "bricking" the token - and many operations on pools containing that token. Any // sort of "permissioned" token that can make transfers fail can cause operations on pools containing them to // revert. Even Recovery Mode cannot help then, as it does a proportional withdrawal of all tokens. If one of // them is bricked, the whole operation will revert. Since v3 does not have "internal balances" like v2, there // is no recourse. // // Of course, many tokens in common use have some of these "features" (especially centralized stable coins), so // we have to support them anyway. Working with common centralized tokens is a risk common to all of DeFi. /** * @notice Token types supported by the Vault. * @dev In general, pools may contain any combination of these tokens. * * STANDARD tokens (e.g., BAL, WETH) have no rate provider. * WITH_RATE tokens (e.g., wstETH) require a rate provider. These may be tokens like wstETH, which need to be wrapped * because the underlying stETH token is rebasing, and such tokens are unsupported by the Vault. They may also be * tokens like sEUR, which track an underlying asset, but are not yield-bearing. Finally, this encompasses * yield-bearing ERC4626 tokens, which can be used to facilitate swaps without requiring wrapping or unwrapping * in most cases. The `paysYieldFees` flag can be used to indicate whether a token is yield-bearing (e.g., waDAI), * not yield-bearing (e.g., sEUR), or yield-bearing but exempt from fees (e.g., in certain nested pools, where * yield fees are charged elsewhere). * * NB: STANDARD must always be the first enum element, so that newly initialized data structures default to Standard. */ enum TokenType { STANDARD, WITH_RATE } /** * @notice Encapsulate the data required for the Vault to support a token of the given type. * @dev For STANDARD tokens, the rate provider address must be 0, and paysYieldFees must be false. All WITH_RATE tokens * need a rate provider, and may or may not be yield-bearing. * * At registration time, it is useful to include the token address along with the token parameters in the structure * passed to `registerPool`, as the alternative would be parallel arrays, which would be error prone and require * validation checks. `TokenConfig` is only used for registration, and is never put into storage (see `TokenInfo`). * * @param token The token address * @param tokenType The token type (see the enum for supported types) * @param rateProvider The rate provider for a token (see further documentation above) * @param paysYieldFees Flag indicating whether yield fees should be charged on this token */ struct TokenConfig { IERC20 token; TokenType tokenType; IRateProvider rateProvider; bool paysYieldFees; } /** * @notice This data structure is stored in `_poolTokenInfo`, a nested mapping from pool -> (token -> TokenInfo). * @dev Since the token is already the key of the nested mapping, it would be redundant (and an extra SLOAD) to store * it again in the struct. When we construct PoolData, the tokens are separated into their own array. * * @param tokenType The token type (see the enum for supported types) * @param rateProvider The rate provider for a token (see further documentation above) * @param paysYieldFees Flag indicating whether yield fees should be charged on this token */ struct TokenInfo { TokenType tokenType; IRateProvider rateProvider; bool paysYieldFees; } /** * @notice Data structure used to represent the current pool state in memory * @param poolConfigBits Custom type to store the entire configuration of the pool. * @param tokens Pool tokens, sorted in token registration order * @param tokenInfo Configuration data for each token, sorted in token registration order * @param balancesRaw Token balances in native decimals * @param balancesLiveScaled18 Token balances after paying yield fees, applying decimal scaling and rates * @param tokenRates 18-decimal FP values for rate tokens (e.g., yield-bearing), or FP(1) for standard tokens * @param decimalScalingFactors Conversion factor used to adjust for token decimals for uniform precision in * calculations. It is 1e18 (FP 1) for 18-decimal tokens */ struct PoolData { PoolConfigBits poolConfigBits; IERC20[] tokens; TokenInfo[] tokenInfo; uint256[] balancesRaw; uint256[] balancesLiveScaled18; uint256[] tokenRates; uint256[] decimalScalingFactors; } enum Rounding { ROUND_UP, ROUND_DOWN } /******************************************************************************* Swaps *******************************************************************************/ enum SwapKind { EXACT_IN, EXACT_OUT } // There are two "SwapParams" structs defined below. `VaultSwapParams` corresponds to the external swap API defined // in the Router contracts, which uses explicit token addresses, the amount given and limit on the calculated amount // expressed in native token decimals, and optional user data passed in from the caller. // // `PoolSwapParams` passes some of this information through (kind, userData), but "translates" the parameters to fit // the internal swap API used by `IBasePool`. It scales amounts to full 18-decimal precision, adds the token balances, // converts the raw token addresses to indices, and adds the address of the Router originating the request. It does // not need the limit, since this is checked at the Router level. /** * @notice Data passed into primary Vault `swap` operations. * @param kind Type of swap (Exact In or Exact Out) * @param pool The pool with the tokens being swapped * @param tokenIn The token entering the Vault (balance increases) * @param tokenOut The token leaving the Vault (balance decreases) * @param amountGivenRaw Amount specified for tokenIn or tokenOut (depending on the type of swap) * @param limitRaw Minimum or maximum value of the calculated amount (depending on the type of swap) * @param userData Additional (optional) user data */ struct VaultSwapParams { SwapKind kind; address pool; IERC20 tokenIn; IERC20 tokenOut; uint256 amountGivenRaw; uint256 limitRaw; bytes userData; } /** * @notice Data for a swap operation, used by contracts implementing `IBasePool`. * @param kind Type of swap (exact in or exact out) * @param amountGivenScaled18 Amount given based on kind of the swap (e.g., tokenIn for EXACT_IN) * @param balancesScaled18 Current pool balances * @param indexIn Index of tokenIn * @param indexOut Index of tokenOut * @param router The address (usually a router contract) that initiated a swap operation on the Vault * @param userData Additional (optional) data required for the swap */ struct PoolSwapParams { SwapKind kind; uint256 amountGivenScaled18; uint256[] balancesScaled18; uint256 indexIn; uint256 indexOut; address router; bytes userData; } /** * @notice Data for the hook after a swap operation. * @param kind Type of swap (exact in or exact out) * @param tokenIn Token to be swapped from * @param tokenOut Token to be swapped to * @param amountInScaled18 Amount of tokenIn (entering the Vault) * @param amountOutScaled18 Amount of tokenOut (leaving the Vault) * @param tokenInBalanceScaled18 Updated (after swap) balance of tokenIn * @param tokenOutBalanceScaled18 Updated (after swap) balance of tokenOut * @param amountCalculatedScaled18 Token amount calculated by the swap * @param amountCalculatedRaw Token amount calculated by the swap * @param router The address (usually a router contract) that initiated a swap operation on the Vault * @param pool Pool address * @param userData Additional (optional) data required for the swap */ struct AfterSwapParams { SwapKind kind; IERC20 tokenIn; IERC20 tokenOut; uint256 amountInScaled18; uint256 amountOutScaled18; uint256 tokenInBalanceScaled18; uint256 tokenOutBalanceScaled18; uint256 amountCalculatedScaled18; uint256 amountCalculatedRaw; address router; address pool; bytes userData; } /******************************************************************************* Add liquidity *******************************************************************************/ enum AddLiquidityKind { PROPORTIONAL, UNBALANCED, SINGLE_TOKEN_EXACT_OUT, DONATION, CUSTOM } /** * @notice Data for an add liquidity operation. * @param pool Address of the pool * @param to Address of user to mint to * @param maxAmountsIn Maximum amounts of input tokens * @param minBptAmountOut Minimum amount of output pool tokens * @param kind Add liquidity kind * @param userData Optional user data */ struct AddLiquidityParams { address pool; address to; uint256[] maxAmountsIn; uint256 minBptAmountOut; AddLiquidityKind kind; bytes userData; } /******************************************************************************* Remove liquidity *******************************************************************************/ enum RemoveLiquidityKind { PROPORTIONAL, SINGLE_TOKEN_EXACT_IN, SINGLE_TOKEN_EXACT_OUT, CUSTOM } /** * @notice Data for an remove liquidity operation. * @param pool Address of the pool * @param from Address of user to burn from * @param maxBptAmountIn Maximum amount of input pool tokens * @param minAmountsOut Minimum amounts of output tokens * @param kind Remove liquidity kind * @param userData Optional user data */ struct RemoveLiquidityParams { address pool; address from; uint256 maxBptAmountIn; uint256[] minAmountsOut; RemoveLiquidityKind kind; bytes userData; } /******************************************************************************* Remove liquidity *******************************************************************************/ enum WrappingDirection { WRAP, UNWRAP } /** * @notice Data for a wrap/unwrap operation. * @param kind Type of swap (Exact In or Exact Out) * @param direction Direction of the wrapping operation (Wrap or Unwrap) * @param wrappedToken Wrapped token, compatible with interface ERC4626 * @param amountGivenRaw Amount specified for tokenIn or tokenOut (depends on the type of swap and wrapping direction) * @param limitRaw Minimum or maximum amount specified for the other token (depends on the type of swap and wrapping * direction) */ struct BufferWrapOrUnwrapParams { SwapKind kind; WrappingDirection direction; IERC4626 wrappedToken; uint256 amountGivenRaw; uint256 limitRaw; } // Protocol Fees are 24-bit values. We transform them by multiplying by 1e11, so that they can be set to any value // between 0% and 100% (step 0.00001%). Protocol and pool creator fees are set in the `ProtocolFeeController`, and // ensure both constituent and aggregate fees do not exceed this precision. uint256 constant FEE_BITLENGTH = 24; uint256 constant FEE_SCALING_FACTOR = 1e11; // Used to ensure the safety of fee-related math (e.g., pools or hooks don't set it greater than 100%). // This value should work for practical purposes and is well within the max precision requirements. uint256 constant MAX_FEE_PERCENTAGE = 99.9999e16; // 99.9999%
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IAuthentication } from "@balancer-labs/v3-interfaces/contracts/solidity-utils/helpers/IAuthentication.sol"; /** * @notice Building block for performing access control on external functions. * @dev This contract is used via the `authenticate` modifier (or the `_authenticateCaller` function), which can be * applied to external functions to make them only callable by authorized accounts. * * Derived contracts must implement the `_canPerform` function, which holds the actual access control logic. */ abstract contract Authentication is IAuthentication { bytes32 private immutable _actionIdDisambiguator; /** * @dev The main purpose of the `actionIdDisambiguator` is to prevent accidental function selector collisions in * multi-contract systems. * * There are two main uses for it: * - if the contract is a singleton, any unique identifier can be used to make the associated action identifiers * unique. The contract's own address is a good option. * - if the contract belongs to a family that shares action identifiers for the same functions, an identifier * shared by the entire family (and no other contract) should be used instead. */ constructor(bytes32 actionIdDisambiguator) { _actionIdDisambiguator = actionIdDisambiguator; } /// @dev Reverts unless the caller is allowed to call this function. Should only be applied to external functions. modifier authenticate() { _authenticateCaller(); _; } /// @dev Reverts unless the caller is allowed to call the entry point function. function _authenticateCaller() internal view { bytes32 actionId = getActionId(msg.sig); if (!_canPerform(actionId, msg.sender)) { revert SenderNotAllowed(); } } /// @inheritdoc IAuthentication function getActionId(bytes4 selector) public view override returns (bytes32) { // Each external function is dynamically assigned an action identifier as the hash of the disambiguator and the // function selector. Disambiguation is necessary to avoid potential collisions in the function selectors of // multiple contracts. return keccak256(abi.encodePacked(_actionIdDisambiguator, selector)); } /** * @dev Derived contracts must implement this function to perform the actual access control logic. * @param actionId The action identifier associated with an external function * @param user The account performing the action * @return success True if the action is permitted */ function _canPerform(bytes32 actionId, address user) internal view virtual returns (bool); }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; /// @notice Library of helper functions related to typecasting arrays. library CastingHelpers { /// @dev Returns a native array of addresses as an IERC20[] array. function asIERC20(address[] memory addresses) internal pure returns (IERC20[] memory tokens) { // solhint-disable-next-line no-inline-assembly assembly ("memory-safe") { tokens := addresses } } /// @dev Returns an IERC20[] array as an address[] array. function asAddress(IERC20[] memory tokens) internal pure returns (address[] memory addresses) { // solhint-disable-next-line no-inline-assembly assembly ("memory-safe") { addresses := tokens } } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; /// @notice Library used to check whether the current operation was initiated through a static call. library EVMCallModeHelpers { /// @notice A state-changing transaction was initiated in a context that only allows static calls. error NotStaticCall(); /** * @dev Detects whether the current transaction is a static call. * A static call is one where `tx.origin` equals 0x0 for most implementations. * See this tweet for a table on how transaction parameters are set on different platforms: * https://twitter.com/0xkarmacoma/status/1493380279309717505 * * Solidity eth_call reference docs are here: https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_call */ function isStaticCall() internal view returns (bool) { return tx.origin == address(0); // solhint-disable-previous-line avoid-tx-origin } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { CastingHelpers } from "./CastingHelpers.sol"; library InputHelpers { /// @notice Arrays passed to a function and intended to be parallel have different lengths. error InputLengthMismatch(); /** * @notice More than one non-zero value was given for a single token operation. * @dev Input arrays for single token add/remove liquidity operations are expected to have only one non-zero value, * corresponding to the token being added or removed. This error results if there are multiple non-zero entries. */ error MultipleNonZeroInputs(); /** * @notice No valid input was given for a single token operation. * @dev Input arrays for single token add/remove liquidity operations are expected to have one non-zero value, * corresponding to the token being added or removed. This error results if all entries are zero. */ error AllZeroInputs(); /** * @notice The tokens supplied to an array argument were not sorted in numerical order. * @dev Tokens are not sorted by address on registration. This is an optimization so that off-chain processes can * predict the token order without having to query the Vault. (It is also legacy v2 behavior.) */ error TokensNotSorted(); function ensureInputLengthMatch(uint256 a, uint256 b) internal pure { if (a != b) { revert InputLengthMismatch(); } } function ensureInputLengthMatch(uint256 a, uint256 b, uint256 c) internal pure { if (a != b || b != c) { revert InputLengthMismatch(); } } // Find the single non-zero input; revert if there is not exactly one such value. function getSingleInputIndex(uint256[] memory maxAmountsIn) internal pure returns (uint256 inputIndex) { uint256 length = maxAmountsIn.length; inputIndex = length; for (uint256 i = 0; i < length; ++i) { if (maxAmountsIn[i] != 0) { if (inputIndex != length) { revert MultipleNonZeroInputs(); } inputIndex = i; } } if (inputIndex >= length) { revert AllZeroInputs(); } return inputIndex; } /** * @dev Sort an array of tokens, mutating in place (and also returning them). * This assumes the tokens have been (or will be) validated elsewhere for length * and non-duplication. All this does is the sorting. * * A bubble sort should be gas- and bytecode-efficient enough for such small arrays. * Could have also done "manual" comparisons for each of the cases, but this is * about the same number of operations, and more concise. * * This is less efficient for larger token count (i.e., above 4), but such pools should * be rare. And in any case, sorting is only done on-chain in test code. */ function sortTokens(IERC20[] memory tokens) internal pure returns (IERC20[] memory) { for (uint256 i = 0; i < tokens.length - 1; ++i) { for (uint256 j = 0; j < tokens.length - i - 1; ++j) { if (tokens[j] > tokens[j + 1]) { // Swap if they're out of order. (tokens[j], tokens[j + 1]) = (tokens[j + 1], tokens[j]); } } } return tokens; } /// @dev Ensure an array of tokens is sorted. As above, does not validate length or uniqueness. function ensureSortedTokens(IERC20[] memory tokens) internal pure { IERC20 previous = tokens[0]; for (uint256 i = 1; i < tokens.length; ++i) { IERC20 current = tokens[i]; if (previous > current) { revert TokensNotSorted(); } previous = current; } } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; /** * @notice This library represents a data structure for packing a token's current raw and derived balances. A derived * balance can be the "last" live balance scaled18 of the raw token, or the balance of the wrapped version of the * token in a vault buffer, among others. * * @dev We could use a Solidity struct to pack balance values together in a single storage slot, but unfortunately * Solidity only allows for structs to live in either storage, calldata or memory. Because a memory struct still takes * up a slot in the stack (to store its memory location), and because the entire balance fits in a single stack slot * (two 128 bit values), using memory is strictly less gas performant. Therefore, we do manual packing and unpacking. * * We could also use custom types now, but given the simplicity here, and the existing EnumerableMap type, it seemed * easier to leave it as a bytes32. */ library PackedTokenBalance { // The 'rawBalance' portion of the balance is stored in the least significant 128 bits of a 256 bit word, while the // The 'derivedBalance' part uses the remaining 128 bits. uint256 private constant _MAX_BALANCE = 2 ** (128) - 1; /// @notice One of the balances is above the maximum value that can be stored. error BalanceOverflow(); function getBalanceRaw(bytes32 balance) internal pure returns (uint256) { return uint256(balance) & _MAX_BALANCE; } function getBalanceDerived(bytes32 balance) internal pure returns (uint256) { return uint256(balance >> 128) & _MAX_BALANCE; } /// @dev Sets only the raw balance of balances and returns the new bytes32 balance. function setBalanceRaw(bytes32 balance, uint256 newBalanceRaw) internal pure returns (bytes32) { return toPackedBalance(newBalanceRaw, getBalanceDerived(balance)); } /// @dev Sets only the derived balance of balances and returns the new bytes32 balance. function setBalanceDerived(bytes32 balance, uint256 newBalanceDerived) internal pure returns (bytes32) { return toPackedBalance(getBalanceRaw(balance), newBalanceDerived); } /// @dev Validates the size of `balanceRaw` and `balanceDerived`, then returns a packed balance bytes32. function toPackedBalance(uint256 balanceRaw, uint256 balanceDerived) internal pure returns (bytes32) { if (balanceRaw > _MAX_BALANCE || balanceDerived > _MAX_BALANCE) { revert BalanceOverflow(); } return _pack(balanceRaw, balanceDerived); } /// @dev Decode and fetch both balances. function fromPackedBalance(bytes32 balance) internal pure returns (uint256 balanceRaw, uint256 balanceDerived) { return (getBalanceRaw(balance), getBalanceDerived(balance)); } /// @dev Packs two uint128 values into a packed balance bytes32. It does not check balance sizes. function _pack(uint256 leastSignificant, uint256 mostSignificant) private pure returns (bytes32) { return bytes32((mostSignificant << 128) + leastSignificant); } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { FixedPoint } from "../math/FixedPoint.sol"; import { InputHelpers } from "./InputHelpers.sol"; /** * @notice Helper functions to apply/undo token decimal and rate adjustments, rounding in the direction indicated. * @dev To simplify Pool logic, all token balances and amounts are normalized to behave as if the token had * 18 decimals. When comparing DAI (18 decimals) and USDC (6 decimals), 1 USDC and 1 DAI would both be * represented as 1e18. This allows us to not consider differences in token decimals in the internal Pool * math, simplifying it greatly. * * The Vault does not support tokens with more than 18 decimals (see `_MAX_TOKEN_DECIMALS` in `VaultStorage`), * or tokens that do not implement `IERC20Metadata.decimals`. * * These helpers can also be used to scale amounts by other 18-decimal floating point values, such as rates. */ library ScalingHelpers { using FixedPoint for *; using ScalingHelpers for uint256; /*************************************************************************** Single Value Functions ***************************************************************************/ /** * @notice Applies `scalingFactor` and `tokenRate` to `amount`. * @dev This may result in a larger or equal value, depending on whether it needed scaling/rate adjustment or not. * The result is rounded down. * * @param amount Amount to be scaled up to 18 decimals * @param scalingFactor The token decimal scaling factor, `10^(18-tokenDecimals)` * @param tokenRate The token rate scaling factor * @return result The final 18-decimal precision result, rounded down */ function toScaled18ApplyRateRoundDown( uint256 amount, uint256 scalingFactor, uint256 tokenRate ) internal pure returns (uint256) { return (amount * scalingFactor).mulDown(tokenRate); } /** * @notice Applies `scalingFactor` and `tokenRate` to `amount`. * @dev This may result in a larger or equal value, depending on whether it needed scaling/rate adjustment or not. * The result is rounded up. * * @param amount Amount to be scaled up to 18 decimals * @param scalingFactor The token decimal scaling factor, `10^(18-tokenDecimals)` * @param tokenRate The token rate scaling factor * @return result The final 18-decimal precision result, rounded up */ function toScaled18ApplyRateRoundUp( uint256 amount, uint256 scalingFactor, uint256 tokenRate ) internal pure returns (uint256) { return (amount * scalingFactor).mulUp(tokenRate); } /** * @notice Reverses the `scalingFactor` and `tokenRate` applied to `amount`. * @dev This may result in a smaller or equal value, depending on whether it needed scaling/rate adjustment or not. * The result is rounded down. * * @param amount Amount to be scaled down to native token decimals * @param scalingFactor The token decimal scaling factor, `10^(18-tokenDecimals)` * @param tokenRate The token rate scaling factor * @return result The final native decimal result, rounded down */ function toRawUndoRateRoundDown( uint256 amount, uint256 scalingFactor, uint256 tokenRate ) internal pure returns (uint256) { // Do division last. Scaling factor is not a FP18, but a FP18 normalized by FP(1). // `scalingFactor * tokenRate` is a precise FP18, so there is no rounding direction here. return FixedPoint.divDown(amount, scalingFactor * tokenRate); } /** * @notice Reverses the `scalingFactor` and `tokenRate` applied to `amount`. * @dev This may result in a smaller or equal value, depending on whether it needed scaling/rate adjustment or not. * The result is rounded up. * * @param amount Amount to be scaled down to native token decimals * @param scalingFactor The token decimal scaling factor, `10^(18-tokenDecimals)` * @param tokenRate The token rate scaling factor * @return result The final native decimal result, rounded up */ function toRawUndoRateRoundUp( uint256 amount, uint256 scalingFactor, uint256 tokenRate ) internal pure returns (uint256) { // Do division last. Scaling factor is not a FP18, but a FP18 normalized by FP(1). // `scalingFactor * tokenRate` is a precise FP18, so there is no rounding direction here. return FixedPoint.divUp(amount, scalingFactor * tokenRate); } /*************************************************************************** Array Functions ***************************************************************************/ function copyToArray(uint256[] memory from, uint256[] memory to) internal pure { uint256 length = from.length; InputHelpers.ensureInputLengthMatch(length, to.length); // solhint-disable-next-line no-inline-assembly assembly ("memory-safe") { mcopy(add(to, 0x20), add(from, 0x20), mul(length, 0x20)) } } /** * @notice Same as `toScaled18ApplyRateRoundDown`, but for an entire array. * @dev This function does not return anything, but instead *mutates* the `amounts` array. * @param amounts Amounts to be scaled up to 18 decimals, sorted in token registration order * @param scalingFactors The token decimal scaling factors, sorted in token registration order * @param tokenRates The token rate scaling factors, sorted in token registration order */ function toScaled18ApplyRateRoundDownArray( uint256[] memory amounts, uint256[] memory scalingFactors, uint256[] memory tokenRates ) internal pure { uint256 length = amounts.length; InputHelpers.ensureInputLengthMatch(length, scalingFactors.length, tokenRates.length); for (uint256 i = 0; i < length; ++i) { amounts[i] = amounts[i].toScaled18ApplyRateRoundDown(scalingFactors[i], tokenRates[i]); } } /** * @notice Same as `toScaled18ApplyRateRoundDown`, but returns a new array, leaving the original intact. * @param amounts Amounts to be scaled up to 18 decimals, sorted in token registration order * @param scalingFactors The token decimal scaling factors, sorted in token registration order * @param tokenRates The token rate scaling factors, sorted in token registration order * @return results The final 18 decimal results, sorted in token registration order, rounded down */ function copyToScaled18ApplyRateRoundDownArray( uint256[] memory amounts, uint256[] memory scalingFactors, uint256[] memory tokenRates ) internal pure returns (uint256[] memory) { uint256 length = amounts.length; InputHelpers.ensureInputLengthMatch(length, scalingFactors.length, tokenRates.length); uint256[] memory amountsScaled18 = new uint256[](length); for (uint256 i = 0; i < length; ++i) { amountsScaled18[i] = amounts[i].toScaled18ApplyRateRoundDown(scalingFactors[i], tokenRates[i]); } return amountsScaled18; } /** * @notice Same as `toScaled18ApplyRateRoundUp`, but for an entire array. * @dev This function does not return anything, but instead *mutates* the `amounts` array. * @param amounts Amounts to be scaled up to 18 decimals, sorted in token registration order * @param scalingFactors The token decimal scaling factors, sorted in token registration order * @param tokenRates The token rate scaling factors, sorted in token registration order */ function toScaled18ApplyRateRoundUpArray( uint256[] memory amounts, uint256[] memory scalingFactors, uint256[] memory tokenRates ) internal pure { uint256 length = amounts.length; InputHelpers.ensureInputLengthMatch(length, scalingFactors.length, tokenRates.length); for (uint256 i = 0; i < length; ++i) { amounts[i] = amounts[i].toScaled18ApplyRateRoundUp(scalingFactors[i], tokenRates[i]); } } /** * @notice Same as `toScaled18ApplyRateRoundUp`, but returns a new array, leaving the original intact. * @param amounts Amounts to be scaled up to 18 decimals, sorted in token registration order * @param scalingFactors The token decimal scaling factors, sorted in token registration order * @param tokenRates The token rate scaling factors, sorted in token registration order * @return results The final 18 decimal results, sorted in token registration order, rounded up */ function copyToScaled18ApplyRateRoundUpArray( uint256[] memory amounts, uint256[] memory scalingFactors, uint256[] memory tokenRates ) internal pure returns (uint256[] memory) { uint256 length = amounts.length; InputHelpers.ensureInputLengthMatch(length, scalingFactors.length, tokenRates.length); uint256[] memory amountsScaled18 = new uint256[](length); for (uint256 i = 0; i < length; ++i) { amountsScaled18[i] = amounts[i].toScaled18ApplyRateRoundUp(scalingFactors[i], tokenRates[i]); } return amountsScaled18; } /** * @notice Rounds up a rate informed by a rate provider. * @dev Rates calculated by an external rate provider have rounding errors. Intuitively, a rate provider * rounds the rate down so the pool math is executed with conservative amounts. However, when upscaling or * downscaling the amount out, the rate should be rounded up to make sure the amounts scaled are conservative. * @param rate The original rate * @return roundedRate The final rate, with rounding applied */ function computeRateRoundUp(uint256 rate) internal pure returns (uint256) { uint256 roundedRate; // If rate is divisible by FixedPoint.ONE, roundedRate and rate will be equal. It means that rate has 18 zeros, // so there's no rounding issue and the rate should not be rounded up. unchecked { roundedRate = (rate / FixedPoint.ONE) * FixedPoint.ONE; } return roundedRate == rate ? rate : rate + 1; } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { StorageSlotExtension } from "../openzeppelin/StorageSlotExtension.sol"; import { SlotDerivation } from "../openzeppelin/SlotDerivation.sol"; type TokenDeltaMappingSlotType is bytes32; type AddressToUintMappingSlot is bytes32; type UintToAddressToBooleanMappingSlot is bytes32; type AddressArraySlotType is bytes32; /** * @notice Helper functions to read and write values from transient storage, including support for arrays and mappings. * @dev This is temporary, based on Open Zeppelin's partially released library. When the final version is published, we * should be able to remove our copies and import directly from OZ. When Solidity catches up and puts direct support * for transient storage in the language, we should be able to get rid of this altogether. * * This only works on networks where EIP-1153 is supported. */ library TransientStorageHelpers { using SlotDerivation for *; using StorageSlotExtension for *; /// @notice An index is out of bounds on an array operation (e.g., at). error TransientIndexOutOfBounds(); // Calculate the slot for a transient storage variable. function calculateSlot(string memory domain, string memory varName) internal pure returns (bytes32) { return keccak256( abi.encode(uint256(keccak256(abi.encodePacked("balancer-labs.v3.storage.", domain, ".", varName))) - 1) ) & ~bytes32(uint256(0xff)); } /*************************************************************************** Mappings ***************************************************************************/ function tGet(TokenDeltaMappingSlotType slot, IERC20 k1) internal view returns (int256) { return TokenDeltaMappingSlotType.unwrap(slot).deriveMapping(address(k1)).asInt256().tload(); } function tSet(TokenDeltaMappingSlotType slot, IERC20 k1, int256 value) internal { TokenDeltaMappingSlotType.unwrap(slot).deriveMapping(address(k1)).asInt256().tstore(value); } function tGet(AddressToUintMappingSlot slot, address key) internal view returns (uint256) { return AddressToUintMappingSlot.unwrap(slot).deriveMapping(key).asUint256().tload(); } function tSet(AddressToUintMappingSlot slot, address key, uint256 value) internal { AddressToUintMappingSlot.unwrap(slot).deriveMapping(key).asUint256().tstore(value); } function tGet( UintToAddressToBooleanMappingSlot slot, uint256 uintKey, address addressKey ) internal view returns (bool) { return UintToAddressToBooleanMappingSlot .unwrap(slot) .deriveMapping(uintKey) .deriveMapping(addressKey) .asBoolean() .tload(); } function tSet(UintToAddressToBooleanMappingSlot slot, uint256 uintKey, address addressKey, bool value) internal { UintToAddressToBooleanMappingSlot .unwrap(slot) .deriveMapping(uintKey) .deriveMapping(addressKey) .asBoolean() .tstore(value); } // Implement the common "+=" operation: map[key] += value. function tAdd(AddressToUintMappingSlot slot, address key, uint256 value) internal { AddressToUintMappingSlot.unwrap(slot).deriveMapping(key).asUint256().tstore(tGet(slot, key) + value); } function tSub(AddressToUintMappingSlot slot, address key, uint256 value) internal { AddressToUintMappingSlot.unwrap(slot).deriveMapping(key).asUint256().tstore(tGet(slot, key) - value); } /*************************************************************************** Arrays ***************************************************************************/ function tLength(AddressArraySlotType slot) internal view returns (uint256) { return AddressArraySlotType.unwrap(slot).asUint256().tload(); } function tAt(AddressArraySlotType slot, uint256 index) internal view returns (address) { _ensureIndexWithinBounds(slot, index); return AddressArraySlotType.unwrap(slot).deriveArray().offset(index).asAddress().tload(); } function tSet(AddressArraySlotType slot, uint256 index, address value) internal { _ensureIndexWithinBounds(slot, index); AddressArraySlotType.unwrap(slot).deriveArray().offset(index).asAddress().tstore(value); } function _ensureIndexWithinBounds(AddressArraySlotType slot, uint256 index) private view { uint256 length = AddressArraySlotType.unwrap(slot).asUint256().tload(); if (index >= length) { revert TransientIndexOutOfBounds(); } } function tUncheckedAt(AddressArraySlotType slot, uint256 index) internal view returns (address) { return AddressArraySlotType.unwrap(slot).deriveArray().offset(index).asAddress().tload(); } function tUncheckedSet(AddressArraySlotType slot, uint256 index, address value) internal { AddressArraySlotType.unwrap(slot).deriveArray().offset(index).asAddress().tstore(value); } function tPush(AddressArraySlotType slot, address value) internal { // Store the value at offset corresponding to the current length. uint256 length = AddressArraySlotType.unwrap(slot).asUint256().tload(); AddressArraySlotType.unwrap(slot).deriveArray().offset(length).asAddress().tstore(value); // Update current length to consider the new value. AddressArraySlotType.unwrap(slot).asUint256().tstore(length + 1); } function tPop(AddressArraySlotType slot) internal returns (address value) { uint256 lastElementIndex = AddressArraySlotType.unwrap(slot).asUint256().tload() - 1; // Update length to last element. When the index is 0, the slot that holds the length is cleared out. AddressArraySlotType.unwrap(slot).asUint256().tstore(lastElementIndex); StorageSlotExtension.AddressSlotType lastElementSlot = AddressArraySlotType .unwrap(slot) .deriveArray() .offset(lastElementIndex) .asAddress(); // Return last element. value = lastElementSlot.tload(); // Clear value in temporary storage. lastElementSlot.tstore(address(0)); } /*************************************************************************** Uint256 Values ***************************************************************************/ function tIncrement(StorageSlotExtension.Uint256SlotType slot) internal { slot.tstore(slot.tload() + 1); } function tDecrement(StorageSlotExtension.Uint256SlotType slot) internal { slot.tstore(slot.tload() - 1); } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { SignedMath } from "@openzeppelin/contracts/utils/math/SignedMath.sol"; import { Math } from "@openzeppelin/contracts/utils/math/Math.sol"; /** * @notice Library for encoding and decoding values stored inside a 256 bit word. * @dev Typically used to pack multiple values in a single slot, saving gas by performing fewer storage accesses. * * Each value is defined by its size and the least significant bit in the word, also known as offset. For example, two * 128 bit values may be encoded in a word by assigning one an offset of 0, and the other an offset of 128. * * We could use Solidity structs to pack values together in a single storage slot instead of relying on a custom and * error-prone library, but unfortunately Solidity only allows for structs to live in either storage, calldata or * memory. Because a memory struct uses not just memory but also a slot in the stack (to store its memory location), * using memory for word-sized values (i.e. of 256 bits or less) is strictly less gas performant, and doesn't even * prevent stack-too-deep issues. This is compounded by the fact that Balancer contracts typically are memory- * intensive, and the cost of accessing memory increases quadratically with the number of allocated words. Manual * packing and unpacking is therefore the preferred approach. */ library WordCodec { using Math for uint256; using SignedMath for int256; // solhint-disable no-inline-assembly /// @notice Function called with an invalid value. error CodecOverflow(); /// @notice Function called with an invalid bitLength or offset. error OutOfBounds(); /*************************************************************************** In-place Insertion ***************************************************************************/ /** * @dev Inserts an unsigned integer of bitLength, shifted by an offset, into a 256 bit word, * replacing the old value. Returns the new word. */ function insertUint( bytes32 word, uint256 value, uint256 offset, uint256 bitLength ) internal pure returns (bytes32 result) { _validateEncodingParams(value, offset, bitLength); // Equivalent to: // uint256 mask = (1 << bitLength) - 1; // bytes32 clearedWord = bytes32(uint256(word) & ~(mask << offset)); // result = clearedWord | bytes32(value << offset); assembly ("memory-safe") { let mask := sub(shl(bitLength, 1), 1) let clearedWord := and(word, not(shl(offset, mask))) result := or(clearedWord, shl(offset, value)) } } /** * @dev Inserts an address (160 bits), shifted by an offset, into a 256 bit word, * replacing the old value. Returns the new word. */ function insertAddress(bytes32 word, address value, uint256 offset) internal pure returns (bytes32 result) { uint256 addressBitLength = 160; _validateEncodingParams(uint256(uint160(value)), offset, addressBitLength); // Equivalent to: // uint256 mask = (1 << bitLength) - 1; // bytes32 clearedWord = bytes32(uint256(word) & ~(mask << offset)); // result = clearedWord | bytes32(value << offset); assembly ("memory-safe") { let mask := sub(shl(addressBitLength, 1), 1) let clearedWord := and(word, not(shl(offset, mask))) result := or(clearedWord, shl(offset, value)) } } /** * @dev Inserts a signed integer shifted by an offset into a 256 bit word, replacing the old value. Returns * the new word. * * Assumes `value` can be represented using `bitLength` bits. */ function insertInt(bytes32 word, int256 value, uint256 offset, uint256 bitLength) internal pure returns (bytes32) { _validateEncodingParams(value, offset, bitLength); uint256 mask = (1 << bitLength) - 1; bytes32 clearedWord = bytes32(uint256(word) & ~(mask << offset)); // Integer values need masking to remove the upper bits of negative values. return clearedWord | bytes32((uint256(value) & mask) << offset); } /*************************************************************************** Encoding ***************************************************************************/ /** * @dev Encodes an unsigned integer shifted by an offset. Ensures value fits within * `bitLength` bits. * * The return value can be ORed bitwise with other encoded values to form a 256 bit word. */ function encodeUint(uint256 value, uint256 offset, uint256 bitLength) internal pure returns (bytes32) { _validateEncodingParams(value, offset, bitLength); return bytes32(value << offset); } /** * @dev Encodes a signed integer shifted by an offset. * * The return value can be ORed bitwise with other encoded values to form a 256 bit word. */ function encodeInt(int256 value, uint256 offset, uint256 bitLength) internal pure returns (bytes32) { _validateEncodingParams(value, offset, bitLength); uint256 mask = (1 << bitLength) - 1; // Integer values need masking to remove the upper bits of negative values. return bytes32((uint256(value) & mask) << offset); } /*************************************************************************** Decoding ***************************************************************************/ /// @dev Decodes and returns an unsigned integer with `bitLength` bits, shifted by an offset, from a 256 bit word. function decodeUint(bytes32 word, uint256 offset, uint256 bitLength) internal pure returns (uint256 result) { // Equivalent to: // result = uint256(word >> offset) & ((1 << bitLength) - 1); assembly ("memory-safe") { result := and(shr(offset, word), sub(shl(bitLength, 1), 1)) } } /// @dev Decodes and returns a signed integer with `bitLength` bits, shifted by an offset, from a 256 bit word. function decodeInt(bytes32 word, uint256 offset, uint256 bitLength) internal pure returns (int256 result) { int256 maxInt = int256((1 << (bitLength - 1)) - 1); uint256 mask = (1 << bitLength) - 1; int256 value = int256(uint256(word >> offset) & mask); // In case the decoded value is greater than the max positive integer that can be represented with bitLength // bits, we know it was originally a negative integer. Therefore, we mask it to restore the sign in the 256 bit // representation. // // Equivalent to: // result = value > maxInt ? (value | int256(~mask)) : value; assembly ("memory-safe") { result := or(mul(gt(value, maxInt), not(mask)), value) } } /// @dev Decodes and returns an address (160 bits), shifted by an offset, from a 256 bit word. function decodeAddress(bytes32 word, uint256 offset) internal pure returns (address result) { // Equivalent to: // result = address(word >> offset) & ((1 << bitLength) - 1); assembly ("memory-safe") { result := and(shr(offset, word), sub(shl(160, 1), 1)) } } /*************************************************************************** Special Cases ***************************************************************************/ /// @dev Decodes and returns a boolean shifted by an offset from a 256 bit word. function decodeBool(bytes32 word, uint256 offset) internal pure returns (bool result) { // Equivalent to: // result = (uint256(word >> offset) & 1) == 1; assembly ("memory-safe") { result := and(shr(offset, word), 1) } } /** * @dev Inserts a boolean value shifted by an offset into a 256 bit word, replacing the old value. * Returns the new word. */ function insertBool(bytes32 word, bool value, uint256 offset) internal pure returns (bytes32 result) { // Equivalent to: // bytes32 clearedWord = bytes32(uint256(word) & ~(1 << offset)); // bytes32 referenceInsertBool = clearedWord | bytes32(uint256(value ? 1 : 0) << offset); assembly ("memory-safe") { let clearedWord := and(word, not(shl(offset, 1))) result := or(clearedWord, shl(offset, value)) } } /*************************************************************************** Helpers ***************************************************************************/ function _validateEncodingParams(uint256 value, uint256 offset, uint256 bitLength) private pure { if (offset >= 256) { revert OutOfBounds(); } // We never accept 256 bit values (which would make the codec pointless), and the larger the offset the smaller // the maximum bit length. if (!(bitLength >= 1 && bitLength <= Math.min(255, 256 - offset))) { revert OutOfBounds(); } // Testing unsigned values for size is straightforward: their upper bits must be cleared. if (value >> bitLength != 0) { revert CodecOverflow(); } } function _validateEncodingParams(int256 value, uint256 offset, uint256 bitLength) private pure { if (offset >= 256) { revert OutOfBounds(); } // We never accept 256 bit values (which would make the codec pointless), and the larger the offset the smaller // the maximum bit length. if (!(bitLength >= 1 && bitLength <= Math.min(255, 256 - offset))) { revert OutOfBounds(); } // Testing signed values for size is a bit more involved. if (value >= 0) { // For positive values, we can simply check that the upper bits are clear. Notice we remove one bit from the // length for the sign bit. if (value >> (bitLength - 1) != 0) { revert CodecOverflow(); } } else { // Negative values can receive the same treatment by making them positive, with the caveat that the range // for negative values in two's complement supports one more value than for the positive case. if ((value + 1).abs() >> (bitLength - 1) != 0) { revert CodecOverflow(); } } } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { LogExpMath } from "./LogExpMath.sol"; /// @notice Support 18-decimal fixed point arithmetic. All Vault calculations use this for high and uniform precision. library FixedPoint { /// @notice Attempted division by zero. error ZeroDivision(); // solhint-disable no-inline-assembly // solhint-disable private-vars-leading-underscore uint256 internal constant ONE = 1e18; // 18 decimal places uint256 internal constant TWO = 2 * ONE; uint256 internal constant FOUR = 4 * ONE; uint256 internal constant MAX_POW_RELATIVE_ERROR = 10000; // 10^(-14) function mulDown(uint256 a, uint256 b) internal pure returns (uint256) { // Multiplication overflow protection is provided by Solidity 0.8.x. uint256 product = a * b; return product / ONE; } function mulUp(uint256 a, uint256 b) internal pure returns (uint256 result) { // Multiplication overflow protection is provided by Solidity 0.8.x. uint256 product = a * b; // Equivalent to: // result = product == 0 ? 0 : ((product - 1) / FixedPoint.ONE) + 1 assembly ("memory-safe") { result := mul(iszero(iszero(product)), add(div(sub(product, 1), ONE), 1)) } } function divDown(uint256 a, uint256 b) internal pure returns (uint256) { // Solidity 0.8 reverts with a Panic code (0x11) if the multiplication overflows. uint256 aInflated = a * ONE; // Solidity 0.8 reverts with a "Division by Zero" Panic code (0x12) if b is zero return aInflated / b; } function divUp(uint256 a, uint256 b) internal pure returns (uint256 result) { return mulDivUp(a, ONE, b); } /// @dev Return (a * b) / c, rounding up. function mulDivUp(uint256 a, uint256 b, uint256 c) internal pure returns (uint256 result) { // This check is required because Yul's `div` doesn't revert on c==0. if (c == 0) { revert ZeroDivision(); } // Multiple overflow protection is done by Solidity 0.8.x. uint256 product = a * b; // The traditional divUp formula is: // divUp(x, y) := (x + y - 1) / y // To avoid intermediate overflow in the addition, we distribute the division and get: // divUp(x, y) := (x - 1) / y + 1 // Note that this requires x != 0, if x == 0 then the result is zero // // Equivalent to: // result = a == 0 ? 0 : (a * b - 1) / c + 1 assembly ("memory-safe") { result := mul(iszero(iszero(product)), add(div(sub(product, 1), c), 1)) } } /** * @dev Version of divUp when the input is raw (i.e., already "inflated"). For instance, * invariant * invariant (36 decimals) vs. invariant.mulDown(invariant) (18 decimal FP). * This can occur in calculations with many successive multiplications and divisions, and * we want to minimize the number of operations by avoiding unnecessary scaling by ONE. */ function divUpRaw(uint256 a, uint256 b) internal pure returns (uint256 result) { // This check is required because Yul's `div` doesn't revert on b==0. if (b == 0) { revert ZeroDivision(); } // Equivalent to: // result = a == 0 ? 0 : 1 + (a - 1) / b assembly ("memory-safe") { result := mul(iszero(iszero(a)), add(1, div(sub(a, 1), b))) } } /** * @dev Returns x^y, assuming both are fixed point numbers, rounding down. The result is guaranteed to not be above * the true value (that is, the error function expected - actual is always positive). */ function powDown(uint256 x, uint256 y) internal pure returns (uint256) { // Optimize for when y equals 1.0, 2.0 or 4.0, as those are very simple to implement and occur often in 50/50 // and 80/20 Weighted Pools if (y == ONE) { return x; } else if (y == TWO) { return mulDown(x, x); } else if (y == FOUR) { uint256 square = mulDown(x, x); return mulDown(square, square); } else { uint256 raw = LogExpMath.pow(x, y); uint256 maxError = mulUp(raw, MAX_POW_RELATIVE_ERROR) + 1; if (raw < maxError) { return 0; } else { unchecked { return raw - maxError; } } } } /** * @dev Returns x^y, assuming both are fixed point numbers, rounding up. The result is guaranteed to not be below * the true value (that is, the error function expected - actual is always negative). */ function powUp(uint256 x, uint256 y) internal pure returns (uint256) { // Optimize for when y equals 1.0, 2.0 or 4.0, as those are very simple to implement and occur often in 50/50 // and 80/20 Weighted Pools if (y == ONE) { return x; } else if (y == TWO) { return mulUp(x, x); } else if (y == FOUR) { uint256 square = mulUp(x, x); return mulUp(square, square); } else { uint256 raw = LogExpMath.pow(x, y); uint256 maxError = mulUp(raw, MAX_POW_RELATIVE_ERROR) + 1; return raw + maxError; } } /** * @dev Returns the complement of a value (1 - x), capped to 0 if x is larger than 1. * * Useful when computing the complement for values with some level of relative error, as it strips this error and * prevents intermediate negative values. */ function complement(uint256 x) internal pure returns (uint256 result) { // Equivalent to: // result = (x < ONE) ? (ONE - x) : 0 assembly ("memory-safe") { result := mul(lt(x, ONE), sub(ONE, x)) } } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; // solhint-disable /** * @dev Exponentiation and logarithm functions for 18 decimal fixed point numbers (both base and exponent/argument). * * Exponentiation and logarithm with arbitrary bases (x^y and log_x(y)) are implemented by conversion to natural * exponentiation and logarithm (where the base is Euler's number). * * All math operations are unchecked in order to save gas. * * @author Fernando Martinelli - @fernandomartinelli * @author Sergio Yuhjtman - @sergioyuhjtman * @author Daniel Fernandez - @dmf7z */ library LogExpMath { /// @notice This error is thrown when a base is not within an acceptable range. error BaseOutOfBounds(); /// @notice This error is thrown when a exponent is not within an acceptable range. error ExponentOutOfBounds(); /// @notice This error is thrown when the exponent * ln(base) is not within an acceptable range. error ProductOutOfBounds(); /// @notice This error is thrown when an exponent used in the exp function is not within an acceptable range. error InvalidExponent(); /// @notice This error is thrown when a variable or result is not within the acceptable bounds defined in the function. error OutOfBounds(); // All fixed point multiplications and divisions are inlined. This means we need to divide by ONE when multiplying // two numbers, and multiply by ONE when dividing them. // All arguments and return values are 18 decimal fixed point numbers. int256 constant ONE_18 = 1e18; // Internally, intermediate values are computed with higher precision as 20 decimal fixed point numbers, and in the // case of ln36, 36 decimals. int256 constant ONE_20 = 1e20; int256 constant ONE_36 = 1e36; // The domain of natural exponentiation is bound by the word size and number of decimals used. // // Because internally the result will be stored using 20 decimals, the largest possible result is // (2^255 - 1) / 10^20, which makes the largest exponent ln((2^255 - 1) / 10^20) = 130.700829182905140221. // The smallest possible result is 10^(-18), which makes largest negative argument // ln(10^(-18)) = -41.446531673892822312. // We use 130.0 and -41.0 to have some safety margin. int256 constant MAX_NATURAL_EXPONENT = 130e18; int256 constant MIN_NATURAL_EXPONENT = -41e18; // Bounds for ln_36's argument. Both ln(0.9) and ln(1.1) can be represented with 36 decimal places in a fixed point // 256 bit integer. int256 constant LN_36_LOWER_BOUND = ONE_18 - 1e17; int256 constant LN_36_UPPER_BOUND = ONE_18 + 1e17; uint256 constant MILD_EXPONENT_BOUND = 2 ** 254 / uint256(ONE_20); // 18 decimal constants int256 constant x0 = 128000000000000000000; // 2ˆ7 int256 constant a0 = 38877084059945950922200000000000000000000000000000000000; // eˆ(x0) (no decimals) int256 constant x1 = 64000000000000000000; // 2ˆ6 int256 constant a1 = 6235149080811616882910000000; // eˆ(x1) (no decimals) // 20 decimal constants int256 constant x2 = 3200000000000000000000; // 2ˆ5 int256 constant a2 = 7896296018268069516100000000000000; // eˆ(x2) int256 constant x3 = 1600000000000000000000; // 2ˆ4 int256 constant a3 = 888611052050787263676000000; // eˆ(x3) int256 constant x4 = 800000000000000000000; // 2ˆ3 int256 constant a4 = 298095798704172827474000; // eˆ(x4) int256 constant x5 = 400000000000000000000; // 2ˆ2 int256 constant a5 = 5459815003314423907810; // eˆ(x5) int256 constant x6 = 200000000000000000000; // 2ˆ1 int256 constant a6 = 738905609893065022723; // eˆ(x6) int256 constant x7 = 100000000000000000000; // 2ˆ0 int256 constant a7 = 271828182845904523536; // eˆ(x7) int256 constant x8 = 50000000000000000000; // 2ˆ-1 int256 constant a8 = 164872127070012814685; // eˆ(x8) int256 constant x9 = 25000000000000000000; // 2ˆ-2 int256 constant a9 = 128402541668774148407; // eˆ(x9) int256 constant x10 = 12500000000000000000; // 2ˆ-3 int256 constant a10 = 113314845306682631683; // eˆ(x10) int256 constant x11 = 6250000000000000000; // 2ˆ-4 int256 constant a11 = 106449445891785942956; // eˆ(x11) /** * @dev Exponentiation (x^y) with unsigned 18 decimal fixed point base and exponent. * * Reverts if ln(x) * y is smaller than `MIN_NATURAL_EXPONENT`, or larger than `MAX_NATURAL_EXPONENT`. */ function pow(uint256 x, uint256 y) internal pure returns (uint256) { if (y == 0) { // We solve the 0^0 indetermination by making it equal one. return uint256(ONE_18); } if (x == 0) { return 0; } // Instead of computing x^y directly, we instead rely on the properties of logarithms and exponentiation to // arrive at that result. In particular, exp(ln(x)) = x, and ln(x^y) = y * ln(x). This means // x^y = exp(y * ln(x)). // The ln function takes a signed value, so we need to make sure x fits in the signed 256 bit range. if (x >> 255 != 0) { revert BaseOutOfBounds(); } int256 x_int256 = int256(x); // We will compute y * ln(x) in a single step. Depending on the value of x, we can either use ln or ln_36. In // both cases, we leave the division by ONE_18 (due to fixed point multiplication) to the end. // This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 256 bit range. if (y >= MILD_EXPONENT_BOUND) { revert ExponentOutOfBounds(); } int256 y_int256 = int256(y); int256 logx_times_y; unchecked { if (LN_36_LOWER_BOUND < x_int256 && x_int256 < LN_36_UPPER_BOUND) { int256 ln_36_x = _ln_36(x_int256); // ln_36_x has 36 decimal places, so multiplying by y_int256 isn't as straightforward, since we can't just // bring y_int256 to 36 decimal places, as it might overflow. Instead, we perform two 18 decimal // multiplications and add the results: one with the first 18 decimals of ln_36_x, and one with the // (downscaled) last 18 decimals. logx_times_y = ((ln_36_x / ONE_18) * y_int256 + ((ln_36_x % ONE_18) * y_int256) / ONE_18); } else { logx_times_y = _ln(x_int256) * y_int256; } logx_times_y /= ONE_18; } // Finally, we compute exp(y * ln(x)) to arrive at x^y if (!(MIN_NATURAL_EXPONENT <= logx_times_y && logx_times_y <= MAX_NATURAL_EXPONENT)) { revert ProductOutOfBounds(); } return uint256(exp(logx_times_y)); } /** * @dev Natural exponentiation (e^x) with signed 18 decimal fixed point exponent. * * Reverts if `x` is smaller than MIN_NATURAL_EXPONENT, or larger than `MAX_NATURAL_EXPONENT`. */ function exp(int256 x) internal pure returns (int256) { if (!(x >= MIN_NATURAL_EXPONENT && x <= MAX_NATURAL_EXPONENT)) { revert InvalidExponent(); } // We avoid using recursion here because zkSync doesn't support it. bool negativeExponent = false; if (x < 0) { // We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it // fits in the signed 256 bit range (as it is larger than MIN_NATURAL_EXPONENT). In the negative // exponent case, compute e^x, then return 1 / result. unchecked { x = -x; } negativeExponent = true; } // First, we use the fact that e^(x+y) = e^x * e^y to decompose x into a sum of powers of two, which we call x_n, // where x_n == 2^(7 - n), and e^x_n = a_n has been precomputed. We choose the first x_n, x0, to equal 2^7 // because all larger powers are larger than MAX_NATURAL_EXPONENT, and therefore not present in the // decomposition. // At the end of this process we will have the product of all e^x_n = a_n that apply, and the remainder of this // decomposition, which will be lower than the smallest x_n. // exp(x) = k_0 * a_0 * k_1 * a_1 * ... + k_n * a_n * exp(remainder), where each k_n equals either 0 or 1. // We mutate x by subtracting x_n, making it the remainder of the decomposition. // The first two a_n (e^(2^7) and e^(2^6)) are too large if stored as 18 decimal numbers, and could cause // intermediate overflows. Instead we store them as plain integers, with 0 decimals. // Additionally, x0 + x1 is larger than MAX_NATURAL_EXPONENT, which means they will not both be present in the // decomposition. // For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct // it and compute the accumulated product. int256 firstAN; unchecked { if (x >= x0) { x -= x0; firstAN = a0; } else if (x >= x1) { x -= x1; firstAN = a1; } else { firstAN = 1; // One with no decimal places } // We now transform x into a 20 decimal fixed point number, to have enhanced precision when computing the // smaller terms. x *= 100; } // `product` is the accumulated product of all a_n (except a0 and a1), which starts at 20 decimal fixed point // one. Recall that fixed point multiplication requires dividing by ONE_20. int256 product = ONE_20; unchecked { if (x >= x2) { x -= x2; product = (product * a2) / ONE_20; } if (x >= x3) { x -= x3; product = (product * a3) / ONE_20; } if (x >= x4) { x -= x4; product = (product * a4) / ONE_20; } if (x >= x5) { x -= x5; product = (product * a5) / ONE_20; } if (x >= x6) { x -= x6; product = (product * a6) / ONE_20; } if (x >= x7) { x -= x7; product = (product * a7) / ONE_20; } if (x >= x8) { x -= x8; product = (product * a8) / ONE_20; } if (x >= x9) { x -= x9; product = (product * a9) / ONE_20; } } // x10 and x11 are unnecessary here since we have high enough precision already. // Now we need to compute e^x, where x is small (in particular, it is smaller than x9). We use the Taylor series // expansion for e^x: 1 + x + (x^2 / 2!) + (x^3 / 3!) + ... + (x^n / n!). int256 seriesSum = ONE_20; // The initial one in the sum, with 20 decimal places. int256 term; // Each term in the sum, where the nth term is (x^n / n!). // The first term is simply x. term = x; unchecked { seriesSum += term; // Each term (x^n / n!) equals the previous one times x, divided by n. Since x is a fixed point number, // multiplying by it requires dividing by ONE_20, but dividing by the non-fixed point n values does not. term = ((term * x) / ONE_20) / 2; seriesSum += term; term = ((term * x) / ONE_20) / 3; seriesSum += term; term = ((term * x) / ONE_20) / 4; seriesSum += term; term = ((term * x) / ONE_20) / 5; seriesSum += term; term = ((term * x) / ONE_20) / 6; seriesSum += term; term = ((term * x) / ONE_20) / 7; seriesSum += term; term = ((term * x) / ONE_20) / 8; seriesSum += term; term = ((term * x) / ONE_20) / 9; seriesSum += term; term = ((term * x) / ONE_20) / 10; seriesSum += term; term = ((term * x) / ONE_20) / 11; seriesSum += term; term = ((term * x) / ONE_20) / 12; seriesSum += term; // 12 Taylor terms are sufficient for 18 decimal precision. // We now have the first a_n (with no decimals), and the product of all other a_n present, and the Taylor // approximation of the exponentiation of the remainder (both with 20 decimals). All that remains is to multiply // all three (one 20 decimal fixed point multiplication, dividing by ONE_20, and one integer multiplication), // and then drop two digits to return an 18 decimal value. int256 result = (((product * seriesSum) / ONE_20) * firstAN) / 100; // We avoid using recursion here because zkSync doesn't support it. return negativeExponent ? (ONE_18 * ONE_18) / result : result; } } /// @dev Logarithm (log(arg, base), with signed 18 decimal fixed point base and argument. function log(int256 arg, int256 base) internal pure returns (int256) { // This performs a simple base change: log(arg, base) = ln(arg) / ln(base). // Both logBase and logArg are computed as 36 decimal fixed point numbers, either by using ln_36, or by // upscaling. int256 logBase; unchecked { if (LN_36_LOWER_BOUND < base && base < LN_36_UPPER_BOUND) { logBase = _ln_36(base); } else { logBase = _ln(base) * ONE_18; } } int256 logArg; unchecked { if (LN_36_LOWER_BOUND < arg && arg < LN_36_UPPER_BOUND) { logArg = _ln_36(arg); } else { logArg = _ln(arg) * ONE_18; } // When dividing, we multiply by ONE_18 to arrive at a result with 18 decimal places return (logArg * ONE_18) / logBase; } } /// @dev Natural logarithm (ln(a)) with signed 18 decimal fixed point argument. function ln(int256 a) internal pure returns (int256) { // The real natural logarithm is not defined for negative numbers or zero. if (a <= 0) { revert OutOfBounds(); } if (LN_36_LOWER_BOUND < a && a < LN_36_UPPER_BOUND) { unchecked { return _ln_36(a) / ONE_18; } } else { return _ln(a); } } /// @dev Internal natural logarithm (ln(a)) with signed 18 decimal fixed point argument. function _ln(int256 a) private pure returns (int256) { // We avoid using recursion here because zkSync doesn't support it. bool negativeExponent = false; if (a < ONE_18) { // Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). If a is less // than one, 1/a will be greater than one, so in this case we compute ln(1/a) and negate the final result. unchecked { a = (ONE_18 * ONE_18) / a; } negativeExponent = true; } // First, we use the fact that ln^(a * b) = ln(a) + ln(b) to decompose ln(a) into a sum of powers of two, which // we call x_n, where x_n == 2^(7 - n), which are the natural logarithm of precomputed quantities a_n (that is, // ln(a_n) = x_n). We choose the first x_n, x0, to equal 2^7 because the exponential of all larger powers cannot // be represented as 18 fixed point decimal numbers in 256 bits, and are therefore larger than a. // At the end of this process we will have the sum of all x_n = ln(a_n) that apply, and the remainder of this // decomposition, which will be lower than the smallest a_n. // ln(a) = k_0 * x_0 + k_1 * x_1 + ... + k_n * x_n + ln(remainder), where each k_n equals either 0 or 1. // We mutate a by subtracting a_n, making it the remainder of the decomposition. // For reasons related to how `exp` works, the first two a_n (e^(2^7) and e^(2^6)) are not stored as fixed point // numbers with 18 decimals, but instead as plain integers with 0 decimals, so we need to multiply them by // ONE_18 to convert them to fixed point. // For each a_n, we test if that term is present in the decomposition (if a is larger than it), and if so divide // by it and compute the accumulated sum. int256 sum = 0; unchecked { if (a >= a0 * ONE_18) { a /= a0; // Integer, not fixed point division sum += x0; } if (a >= a1 * ONE_18) { a /= a1; // Integer, not fixed point division sum += x1; } // All other a_n and x_n are stored as 20 digit fixed point numbers, so we convert the sum and a to this format. sum *= 100; a *= 100; // Because further a_n are 20 digit fixed point numbers, we multiply by ONE_20 when dividing by them. if (a >= a2) { a = (a * ONE_20) / a2; sum += x2; } if (a >= a3) { a = (a * ONE_20) / a3; sum += x3; } if (a >= a4) { a = (a * ONE_20) / a4; sum += x4; } if (a >= a5) { a = (a * ONE_20) / a5; sum += x5; } if (a >= a6) { a = (a * ONE_20) / a6; sum += x6; } if (a >= a7) { a = (a * ONE_20) / a7; sum += x7; } if (a >= a8) { a = (a * ONE_20) / a8; sum += x8; } if (a >= a9) { a = (a * ONE_20) / a9; sum += x9; } if (a >= a10) { a = (a * ONE_20) / a10; sum += x10; } if (a >= a11) { a = (a * ONE_20) / a11; sum += x11; } } // a is now a small number (smaller than a_11, which roughly equals 1.06). This means we can use a Taylor series // that converges rapidly for values of `a` close to one - the same one used in ln_36. // Let z = (a - 1) / (a + 1). // ln(a) = 2 * (z + z^3 / 3 + z^5 / 5 + z^7 / 7 + ... + z^(2 * n + 1) / (2 * n + 1)) // Recall that 20 digit fixed point division requires multiplying by ONE_20, and multiplication requires // division by ONE_20. unchecked { int256 z = ((a - ONE_20) * ONE_20) / (a + ONE_20); int256 z_squared = (z * z) / ONE_20; // num is the numerator of the series: the z^(2 * n + 1) term int256 num = z; // seriesSum holds the accumulated sum of each term in the series, starting with the initial z int256 seriesSum = num; // In each step, the numerator is multiplied by z^2 num = (num * z_squared) / ONE_20; seriesSum += num / 3; num = (num * z_squared) / ONE_20; seriesSum += num / 5; num = (num * z_squared) / ONE_20; seriesSum += num / 7; num = (num * z_squared) / ONE_20; seriesSum += num / 9; num = (num * z_squared) / ONE_20; seriesSum += num / 11; // 6 Taylor terms are sufficient for 36 decimal precision. // Finally, we multiply by 2 (non fixed point) to compute ln(remainder) seriesSum *= 2; // We now have the sum of all x_n present, and the Taylor approximation of the logarithm of the remainder (both // with 20 decimals). All that remains is to sum these two, and then drop two digits to return a 18 decimal // value. int256 result = (sum + seriesSum) / 100; // We avoid using recursion here because zkSync doesn't support it. return negativeExponent ? -result : result; } } /** * @dev Internal high precision (36 decimal places) natural logarithm (ln(x)) with signed 18 decimal fixed point argument, * for x close to one. * * Should only be used if x is between LN_36_LOWER_BOUND and LN_36_UPPER_BOUND. */ function _ln_36(int256 x) private pure returns (int256) { // Since ln(1) = 0, a value of x close to one will yield a very small result, which makes using 36 digits // worthwhile. // First, we transform x to a 36 digit fixed point value. unchecked { x *= ONE_18; // We will use the following Taylor expansion, which converges very rapidly. Let z = (x - 1) / (x + 1). // ln(x) = 2 * (z + z^3 / 3 + z^5 / 5 + z^7 / 7 + ... + z^(2 * n + 1) / (2 * n + 1)) // Recall that 36 digit fixed point division requires multiplying by ONE_36, and multiplication requires // division by ONE_36. int256 z = ((x - ONE_36) * ONE_36) / (x + ONE_36); int256 z_squared = (z * z) / ONE_36; // num is the numerator of the series: the z^(2 * n + 1) term int256 num = z; // seriesSum holds the accumulated sum of each term in the series, starting with the initial z int256 seriesSum = num; // In each step, the numerator is multiplied by z^2 num = (num * z_squared) / ONE_36; seriesSum += num / 3; num = (num * z_squared) / ONE_36; seriesSum += num / 5; num = (num * z_squared) / ONE_36; seriesSum += num / 7; num = (num * z_squared) / ONE_36; seriesSum += num / 9; num = (num * z_squared) / ONE_36; seriesSum += num / 11; num = (num * z_squared) / ONE_36; seriesSum += num / 13; num = (num * z_squared) / ONE_36; seriesSum += num / 15; // 8 Taylor terms are sufficient for 36 decimal precision. // All that remains is multiplying by 2 (non fixed point). return seriesSum * 2; } } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import { StorageSlotExtension } from "./StorageSlotExtension.sol"; /** * @notice Variant of {ReentrancyGuard} that uses transient storage. * @dev NOTE: This variant only works on networks where EIP-1153 is available. */ abstract contract ReentrancyGuardTransient { using StorageSlotExtension for *; // keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.ReentrancyGuard")) - 1)) & ~bytes32(uint256(0xff)) bytes32 private constant _REENTRANCY_GUARD_STORAGE = 0x9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00; /// @notice Unauthorized reentrant call. error ReentrancyGuardReentrantCall(); /** * @dev Prevents a contract from calling itself, directly or indirectly. * Calling a `nonReentrant` function from another `nonReentrant` * function is not supported. It is possible to prevent this from happening * by making the `nonReentrant` function external, and making it call a * `private` function that does the actual work. */ modifier nonReentrant() { _nonReentrantBefore(); _; _nonReentrantAfter(); } function _nonReentrantBefore() private { // On the first call to nonReentrant, _status will be NOT_ENTERED. if (_reentrancyGuardEntered()) { revert ReentrancyGuardReentrantCall(); } // Any calls to nonReentrant after this point will fail. _REENTRANCY_GUARD_STORAGE.asBoolean().tstore(true); } function _nonReentrantAfter() private { _REENTRANCY_GUARD_STORAGE.asBoolean().tstore(false); } /** * @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a * `nonReentrant` function in the call stack. */ function _reentrancyGuardEntered() internal view returns (bool) { return _REENTRANCY_GUARD_STORAGE.asBoolean().tload(); } }
// SPDX-License-Identifier: MIT // This file was procedurally generated from scripts/generate/templates/SlotDerivation.js. // Taken from https://raw.githubusercontent.com/Amxx/openzeppelin-contracts/ce497cb05ca05bb9aa2b86ec1d99e6454e7ab2e9/contracts/utils/SlotDerivation.sol pragma solidity ^0.8.20; /** * @notice Library for computing storage (and transient storage) locations from namespaces and deriving slots * corresponding to standard patterns. * @dev The derivation method for array and mapping matches the storage layout used by the solidity language/compiler. * * See https://docs.soliditylang.org/en/v0.8.20/internals/layout_in_storage.html#mappings-and-dynamic-arrays[Solidity docs for mappings and dynamic arrays.]. * * Example usage: * ```solidity * contract Example { * // Add the library methods * using StorageSlot for bytes32; * using SlotDerivation for bytes32; * * // Declare a namespace * string private constant _NAMESPACE = "<namespace>" // eg. OpenZeppelin.Slot * * function setValueInNamespace(uint256 key, address newValue) internal { * _NAMESPACE.erc7201Slot().deriveMapping(key).getAddressSlot().value = newValue; * } * * function getValueInNamespace(uint256 key) internal view returns (address) { * return _NAMESPACE.erc7201Slot().deriveMapping(key).getAddressSlot().value; * } * } * ``` * * TIP: Consider using this library along with {StorageSlot}. * * NOTE: This library provides a way to manipulate storage locations in a non-standard way. Tooling for checking * upgrade safety will ignore the slots accessed through this library. */ library SlotDerivation { /// @dev Derive an ERC-7201 slot from a string (namespace). function erc7201Slot(string memory namespace) internal pure returns (bytes32 slot) { /// @solidity memory-safe-assembly assembly { mstore(0x00, sub(keccak256(add(namespace, 0x20), mload(namespace)), 1)) slot := and(keccak256(0x00, 0x20), not(0xff)) } } /// @dev Add an offset to a slot to get the n-th element of a structure or an array. function offset(bytes32 slot, uint256 pos) internal pure returns (bytes32 result) { unchecked { return bytes32(uint256(slot) + pos); } } /// @dev Derive the location of the first element in an array from the slot where the length is stored. function deriveArray(bytes32 slot) internal pure returns (bytes32 result) { /// @solidity memory-safe-assembly assembly { mstore(0x00, slot) result := keccak256(0x00, 0x20) } } /// @dev Derive the location of a mapping element from the key. function deriveMapping(bytes32 slot, address key) internal pure returns (bytes32 result) { /// @solidity memory-safe-assembly assembly { mstore(0x00, key) mstore(0x20, slot) result := keccak256(0x00, 0x40) } } /// @dev Derive the location of a mapping element from the key. function deriveMapping(bytes32 slot, bool key) internal pure returns (bytes32 result) { /// @solidity memory-safe-assembly assembly { mstore(0x00, key) mstore(0x20, slot) result := keccak256(0x00, 0x40) } } /// @dev Derive the location of a mapping element from the key. function deriveMapping(bytes32 slot, bytes32 key) internal pure returns (bytes32 result) { /// @solidity memory-safe-assembly assembly { mstore(0x00, key) mstore(0x20, slot) result := keccak256(0x00, 0x40) } } /// @dev Derive the location of a mapping element from the key. function deriveMapping(bytes32 slot, uint256 key) internal pure returns (bytes32 result) { /// @solidity memory-safe-assembly assembly { mstore(0x00, key) mstore(0x20, slot) result := keccak256(0x00, 0x40) } } /// @dev Derive the location of a mapping element from the key. function deriveMapping(bytes32 slot, int256 key) internal pure returns (bytes32 result) { /// @solidity memory-safe-assembly assembly { mstore(0x00, key) mstore(0x20, slot) result := keccak256(0x00, 0x40) } } /// @dev Derive the location of a mapping element from the key. function deriveMapping(bytes32 slot, string memory key) internal pure returns (bytes32 result) { /// @solidity memory-safe-assembly assembly { let length := mload(key) let begin := add(key, 0x20) let end := add(begin, length) let cache := mload(end) mstore(end, slot) result := keccak256(begin, add(length, 0x20)) mstore(end, cache) } } /// @dev Derive the location of a mapping element from the key. function deriveMapping(bytes32 slot, bytes memory key) internal pure returns (bytes32 result) { /// @solidity memory-safe-assembly assembly { let length := mload(key) let begin := add(key, 0x20) let end := add(begin, length) let cache := mload(end) mstore(end, slot) result := keccak256(begin, add(length, 0x20)) mstore(end, cache) } } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; /** * @notice Library for reading and writing primitive types to specific storage slots. Based on OpenZeppelin; just adding support for int256. * @dev TIP: Consider using this library along with {SlotDerivation}. */ library StorageSlotExtension { struct Int256Slot { int256 value; } /// @dev Returns an `Int256Slot` with member `value` located at `slot`. function getInt256Slot(bytes32 slot) internal pure returns (Int256Slot storage r) { /// @solidity memory-safe-assembly assembly { r.slot := slot } } /// @dev Custom type that represents a slot holding an address. type AddressSlotType is bytes32; /// @dev Cast an arbitrary slot to a AddressSlotType. function asAddress(bytes32 slot) internal pure returns (AddressSlotType) { return AddressSlotType.wrap(slot); } /// @dev Custom type that represents a slot holding a boolean. type BooleanSlotType is bytes32; /// @dev Cast an arbitrary slot to a BooleanSlotType. function asBoolean(bytes32 slot) internal pure returns (BooleanSlotType) { return BooleanSlotType.wrap(slot); } /// @dev Custom type that represents a slot holding a bytes32. type Bytes32SlotType is bytes32; /// @dev Cast an arbitrary slot to a Bytes32SlotType. function asBytes32(bytes32 slot) internal pure returns (Bytes32SlotType) { return Bytes32SlotType.wrap(slot); } /// @dev Custom type that represents a slot holding a uint256. type Uint256SlotType is bytes32; /// @dev Cast an arbitrary slot to a Uint256SlotType. function asUint256(bytes32 slot) internal pure returns (Uint256SlotType) { return Uint256SlotType.wrap(slot); } /// @dev Custom type that represents a slot holding an int256. type Int256SlotType is bytes32; /// @dev Cast an arbitrary slot to an Int256SlotType. function asInt256(bytes32 slot) internal pure returns (Int256SlotType) { return Int256SlotType.wrap(slot); } /// @dev Load the value held at location `slot` in transient storage. function tload(AddressSlotType slot) internal view returns (address value) { /// @solidity memory-safe-assembly assembly { value := tload(slot) } } /// @dev Store `value` at location `slot` in transient storage. function tstore(AddressSlotType slot, address value) internal { /// @solidity memory-safe-assembly assembly { tstore(slot, value) } } /// @dev Load the value held at location `slot` in transient storage. function tload(BooleanSlotType slot) internal view returns (bool value) { /// @solidity memory-safe-assembly assembly { value := tload(slot) } } /// @dev Store `value` at location `slot` in transient storage. function tstore(BooleanSlotType slot, bool value) internal { /// @solidity memory-safe-assembly assembly { tstore(slot, value) } } /// @dev Load the value held at location `slot` in transient storage. function tload(Bytes32SlotType slot) internal view returns (bytes32 value) { /// @solidity memory-safe-assembly assembly { value := tload(slot) } } /// @dev Store `value` at location `slot` in transient storage. function tstore(Bytes32SlotType slot, bytes32 value) internal { /// @solidity memory-safe-assembly assembly { tstore(slot, value) } } /// @dev Load the value held at location `slot` in transient storage. function tload(Uint256SlotType slot) internal view returns (uint256 value) { /// @solidity memory-safe-assembly assembly { value := tload(slot) } } /// @dev Store `value` at location `slot` in transient storage. function tstore(Uint256SlotType slot, uint256 value) internal { /// @solidity memory-safe-assembly assembly { tstore(slot, value) } } /// @dev Load the value held at location `slot` in transient storage. function tload(Int256SlotType slot) internal view returns (int256 value) { /// @solidity memory-safe-assembly assembly { value := tload(slot) } } /// @dev Store `value` at location `slot` in transient storage. function tstore(Int256SlotType slot, int256 value) internal { /// @solidity memory-safe-assembly assembly { tstore(slot, value) } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (interfaces/draft-IERC6093.sol) pragma solidity ^0.8.20; /** * @dev Standard ERC20 Errors * Interface of the https://eips.ethereum.org/EIPS/eip-6093[ERC-6093] custom errors for ERC20 tokens. */ interface IERC20Errors { /** * @dev Indicates an error related to the current `balance` of a `sender`. Used in transfers. * @param sender Address whose tokens are being transferred. * @param balance Current balance for the interacting account. * @param needed Minimum amount required to perform a transfer. */ error ERC20InsufficientBalance(address sender, uint256 balance, uint256 needed); /** * @dev Indicates a failure with the token `sender`. Used in transfers. * @param sender Address whose tokens are being transferred. */ error ERC20InvalidSender(address sender); /** * @dev Indicates a failure with the token `receiver`. Used in transfers. * @param receiver Address to which tokens are being transferred. */ error ERC20InvalidReceiver(address receiver); /** * @dev Indicates a failure with the `spender`’s `allowance`. Used in transfers. * @param spender Address that may be allowed to operate on tokens without being their owner. * @param allowance Amount of tokens a `spender` is allowed to operate with. * @param needed Minimum amount required to perform a transfer. */ error ERC20InsufficientAllowance(address spender, uint256 allowance, uint256 needed); /** * @dev Indicates a failure with the `approver` of a token to be approved. Used in approvals. * @param approver Address initiating an approval operation. */ error ERC20InvalidApprover(address approver); /** * @dev Indicates a failure with the `spender` to be approved. Used in approvals. * @param spender Address that may be allowed to operate on tokens without being their owner. */ error ERC20InvalidSpender(address spender); } /** * @dev Standard ERC721 Errors * Interface of the https://eips.ethereum.org/EIPS/eip-6093[ERC-6093] custom errors for ERC721 tokens. */ interface IERC721Errors { /** * @dev Indicates that an address can't be an owner. For example, `address(0)` is a forbidden owner in EIP-20. * Used in balance queries. * @param owner Address of the current owner of a token. */ error ERC721InvalidOwner(address owner); /** * @dev Indicates a `tokenId` whose `owner` is the zero address. * @param tokenId Identifier number of a token. */ error ERC721NonexistentToken(uint256 tokenId); /** * @dev Indicates an error related to the ownership over a particular token. Used in transfers. * @param sender Address whose tokens are being transferred. * @param tokenId Identifier number of a token. * @param owner Address of the current owner of a token. */ error ERC721IncorrectOwner(address sender, uint256 tokenId, address owner); /** * @dev Indicates a failure with the token `sender`. Used in transfers. * @param sender Address whose tokens are being transferred. */ error ERC721InvalidSender(address sender); /** * @dev Indicates a failure with the token `receiver`. Used in transfers. * @param receiver Address to which tokens are being transferred. */ error ERC721InvalidReceiver(address receiver); /** * @dev Indicates a failure with the `operator`’s approval. Used in transfers. * @param operator Address that may be allowed to operate on tokens without being their owner. * @param tokenId Identifier number of a token. */ error ERC721InsufficientApproval(address operator, uint256 tokenId); /** * @dev Indicates a failure with the `approver` of a token to be approved. Used in approvals. * @param approver Address initiating an approval operation. */ error ERC721InvalidApprover(address approver); /** * @dev Indicates a failure with the `operator` to be approved. Used in approvals. * @param operator Address that may be allowed to operate on tokens without being their owner. */ error ERC721InvalidOperator(address operator); } /** * @dev Standard ERC1155 Errors * Interface of the https://eips.ethereum.org/EIPS/eip-6093[ERC-6093] custom errors for ERC1155 tokens. */ interface IERC1155Errors { /** * @dev Indicates an error related to the current `balance` of a `sender`. Used in transfers. * @param sender Address whose tokens are being transferred. * @param balance Current balance for the interacting account. * @param needed Minimum amount required to perform a transfer. * @param tokenId Identifier number of a token. */ error ERC1155InsufficientBalance(address sender, uint256 balance, uint256 needed, uint256 tokenId); /** * @dev Indicates a failure with the token `sender`. Used in transfers. * @param sender Address whose tokens are being transferred. */ error ERC1155InvalidSender(address sender); /** * @dev Indicates a failure with the token `receiver`. Used in transfers. * @param receiver Address to which tokens are being transferred. */ error ERC1155InvalidReceiver(address receiver); /** * @dev Indicates a failure with the `operator`’s approval. Used in transfers. * @param operator Address that may be allowed to operate on tokens without being their owner. * @param owner Address of the current owner of a token. */ error ERC1155MissingApprovalForAll(address operator, address owner); /** * @dev Indicates a failure with the `approver` of a token to be approved. Used in approvals. * @param approver Address initiating an approval operation. */ error ERC1155InvalidApprover(address approver); /** * @dev Indicates a failure with the `operator` to be approved. Used in approvals. * @param operator Address that may be allowed to operate on tokens without being their owner. */ error ERC1155InvalidOperator(address operator); /** * @dev Indicates an array length mismatch between ids and values in a safeBatchTransferFrom operation. * Used in batch transfers. * @param idsLength Length of the array of token identifiers * @param valuesLength Length of the array of token amounts */ error ERC1155InvalidArrayLength(uint256 idsLength, uint256 valuesLength); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC4626.sol) pragma solidity ^0.8.20; import {IERC20} from "../token/ERC20/IERC20.sol"; import {IERC20Metadata} from "../token/ERC20/extensions/IERC20Metadata.sol"; /** * @dev Interface of the ERC4626 "Tokenized Vault Standard", as defined in * https://eips.ethereum.org/EIPS/eip-4626[ERC-4626]. */ interface IERC4626 is IERC20, IERC20Metadata { event Deposit(address indexed sender, address indexed owner, uint256 assets, uint256 shares); event Withdraw( address indexed sender, address indexed receiver, address indexed owner, uint256 assets, uint256 shares ); /** * @dev Returns the address of the underlying token used for the Vault for accounting, depositing, and withdrawing. * * - MUST be an ERC-20 token contract. * - MUST NOT revert. */ function asset() external view returns (address assetTokenAddress); /** * @dev Returns the total amount of the underlying asset that is “managed” by Vault. * * - SHOULD include any compounding that occurs from yield. * - MUST be inclusive of any fees that are charged against assets in the Vault. * - MUST NOT revert. */ function totalAssets() external view returns (uint256 totalManagedAssets); /** * @dev Returns the amount of shares that the Vault would exchange for the amount of assets provided, in an ideal * scenario where all the 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, when performing 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, meaning what the average user should expect to see when exchanging to and * from. */ function convertToShares(uint256 assets) external view returns (uint256 shares); /** * @dev Returns the amount of assets that the Vault would exchange for the amount of shares provided, in an ideal * scenario where all the 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, when performing 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, meaning what the average user should expect to see when exchanging to and * from. */ function convertToAssets(uint256 shares) external view returns (uint256 assets); /** * @dev Returns the maximum amount of the underlying asset that can be deposited into the Vault for the receiver, * through a deposit call. * * - MUST return a limited value if receiver is subject to some deposit limit. * - MUST return 2 ** 256 - 1 if there is no limit on the maximum amount of assets that may be deposited. * - MUST NOT revert. */ function maxDeposit(address receiver) external view returns (uint256 maxAssets); /** * @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 would be minted in a deposit * call in the same transaction. I.e. deposit should return the same or more shares as previewDeposit if called * in the same transaction. * - MUST NOT account for deposit limits like those returned from maxDeposit and should always act as though the * deposit would be accepted, regardless if the user has enough tokens approved, etc. * - MUST be inclusive of deposit fees. Integrators should be aware of the existence of deposit fees. * - 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) external view returns (uint256 shares); /** * @dev Mints shares Vault shares to receiver by depositing exactly amount 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 (due to deposit limit being reached, slippage, the user not * approving enough underlying tokens to the Vault contract, etc). * * NOTE: most implementations will require pre-approval of the Vault with the Vault’s underlying asset token. */ function deposit(uint256 assets, address receiver) external returns (uint256 shares); /** * @dev Returns the maximum amount of the Vault shares that can be minted for the receiver, through a mint call. * - MUST return a limited value if receiver is subject to some mint limit. * - MUST return 2 ** 256 - 1 if there is no limit on the maximum amount of shares that may be minted. * - MUST NOT revert. */ function maxMint(address receiver) external view returns (uint256 maxShares); /** * @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 would 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 though the mint * would be accepted, regardless if the user has enough tokens approved, etc. * - MUST be inclusive of deposit fees. Integrators should be aware of the existence of deposit fees. * - 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) external view returns (uint256 assets); /** * @dev Mints exactly shares Vault shares to receiver by depositing amount 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 minted (due to deposit limit being reached, slippage, the user not * approving enough underlying tokens to the Vault contract, etc). * * NOTE: most implementations will require pre-approval of the Vault with the Vault’s underlying asset token. */ function mint(uint256 shares, address receiver) external returns (uint256 assets); /** * @dev Returns the maximum amount of the underlying asset that can be withdrawn from the owner balance in the * Vault, through 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) external view returns (uint256 maxAssets); /** * @dev Allows an on-chain or off-chain user to simulate the effects of their withdrawal at the current block, * given current on-chain conditions. * * - MUST return as close to and no fewer than the exact amount of Vault shares that would be burned in a withdraw * call in the same transaction. I.e. withdraw should return the same or fewer shares as previewWithdraw if * called * in the same transaction. * - MUST NOT account for withdrawal limits like those returned from maxWithdraw and should always act as though * the withdrawal would be accepted, regardless if the user has enough shares, etc. * - MUST be inclusive of withdrawal fees. Integrators should be aware of the existence of withdrawal fees. * - 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) external view returns (uint256 shares); /** * @dev Burns shares from owner and sends exactly assets of underlying tokens to receiver. * * - 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 (due to withdrawal limit being reached, slippage, the owner * not having enough shares, etc). * * Note that 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 receiver, address owner) external returns (uint256 shares); /** * @dev Returns the maximum amount of Vault shares that can be redeemed from the owner balance in the Vault, * through a redeem call. * * - MUST return a limited value if owner is subject to some withdrawal limit or timelock. * - MUST return balanceOf(owner) if owner is not subject to any withdrawal limit or timelock. * - MUST NOT revert. */ function maxRedeem(address owner) external view returns (uint256 maxShares); /** * @dev Allows an on-chain or off-chain user to simulate the effects of their redeemption at the current block, * given current on-chain conditions. * * - MUST return as close to and no more than the exact amount of assets that would 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 though the * redemption would be accepted, regardless if the user has enough shares, etc. * - MUST be inclusive of withdrawal fees. Integrators should be aware of the existence of withdrawal fees. * - 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 redeeming. */ function previewRedeem(uint256 shares) external view returns (uint256 assets); /** * @dev Burns exactly shares from owner and sends assets of underlying tokens to receiver. * * - 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 (due to withdrawal limit being reached, slippage, the owner * not having enough shares, etc). * * NOTE: some implementations will require pre-requesting to the Vault before a withdrawal may be performed. * Those methods should be performed separately. */ function redeem(uint256 shares, address receiver, address owner) external returns (uint256 assets); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC5267.sol) pragma solidity ^0.8.20; interface IERC5267 { /** * @dev MAY be emitted to signal that the domain could have changed. */ event EIP712DomainChanged(); /** * @dev returns the fields and values that describe the domain separator used by this contract for EIP-712 * signature. */ function eip712Domain() external view returns ( bytes1 fields, string memory name, string memory version, uint256 chainId, address verifyingContract, bytes32 salt, uint256[] memory extensions ); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/IERC20Metadata.sol) pragma solidity ^0.8.20; import {IERC20} from "../IERC20.sol"; /** * @dev Interface for the optional metadata functions from the ERC20 standard. */ interface IERC20Metadata is IERC20 { /** * @dev Returns the name of the token. */ function name() external view returns (string memory); /** * @dev Returns the symbol of the token. */ function symbol() external view returns (string memory); /** * @dev Returns the decimals places of the token. */ function decimals() external view returns (uint8); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/IERC20Permit.sol) pragma solidity ^0.8.20; /** * @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612]. * * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by * presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't * need to send a transaction, and thus is not required to hold Ether at all. * * ==== Security Considerations * * There are two important considerations concerning the use of `permit`. The first is that a valid permit signature * expresses an allowance, and it should not be assumed to convey additional meaning. In particular, it should not be * considered as an intention to spend the allowance in any specific way. The second is that because permits have * built-in replay protection and can be submitted by anyone, they can be frontrun. A protocol that uses permits should * take this into consideration and allow a `permit` call to fail. Combining these two aspects, a pattern that may be * generally recommended is: * * ```solidity * function doThingWithPermit(..., uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public { * try token.permit(msg.sender, address(this), value, deadline, v, r, s) {} catch {} * doThing(..., value); * } * * function doThing(..., uint256 value) public { * token.safeTransferFrom(msg.sender, address(this), value); * ... * } * ``` * * Observe that: 1) `msg.sender` is used as the owner, leaving no ambiguity as to the signer intent, and 2) the use of * `try/catch` allows the permit to fail and makes the code tolerant to frontrunning. (See also * {SafeERC20-safeTransferFrom}). * * Additionally, note that smart contract wallets (such as Argent or Safe) are not able to produce permit signatures, so * contracts should have entry points that don't rely on permit. */ interface IERC20Permit { /** * @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens, * given ``owner``'s signed approval. * * IMPORTANT: The same issues {IERC20-approve} has related to transaction * ordering also apply here. * * Emits an {Approval} event. * * Requirements: * * - `spender` cannot be the zero address. * - `deadline` must be a timestamp in the future. * - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner` * over the EIP712-formatted function arguments. * - the signature must use ``owner``'s current nonce (see {nonces}). * * For more information on the signature format, see the * https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP * section]. * * CAUTION: See Security Considerations above. */ function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /** * @dev Returns the current nonce for `owner`. This value must be * included whenever a signature is generated for {permit}. * * Every successful call to {permit} increases ``owner``'s nonce by one. This * prevents a signature from being used multiple times. */ function nonces(address owner) external view returns (uint256); /** * @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}. */ // solhint-disable-next-line func-name-mixedcase function DOMAIN_SEPARATOR() external view returns (bytes32); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/IERC20.sol) pragma solidity ^0.8.20; /** * @dev Interface of the ERC20 standard as defined in the EIP. */ interface IERC20 { /** * @dev Emitted when `value` tokens are moved from one account (`from`) to * another (`to`). * * Note that `value` may be zero. */ event Transfer(address indexed from, address indexed to, uint256 value); /** * @dev Emitted when the allowance of a `spender` for an `owner` is set by * a call to {approve}. `value` is the new allowance. */ event Approval(address indexed owner, address indexed spender, uint256 value); /** * @dev Returns the value of tokens in existence. */ function totalSupply() external view returns (uint256); /** * @dev Returns the value of tokens owned by `account`. */ function balanceOf(address account) external view returns (uint256); /** * @dev Moves a `value` amount of tokens from the caller's account to `to`. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transfer(address to, uint256 value) external returns (bool); /** * @dev Returns the remaining number of tokens that `spender` will be * allowed to spend on behalf of `owner` through {transferFrom}. This is * zero by default. * * This value changes when {approve} or {transferFrom} are called. */ function allowance(address owner, address spender) external view returns (uint256); /** * @dev Sets a `value` amount of tokens as the allowance of `spender` over the * caller's tokens. * * Returns a boolean value indicating whether the operation succeeded. * * IMPORTANT: Beware that changing an allowance with this method brings the risk * that someone may use both the old and the new allowance by unfortunate * transaction ordering. One possible solution to mitigate this race * condition is to first reduce the spender's allowance to 0 and set the * desired value afterwards: * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 * * Emits an {Approval} event. */ function approve(address spender, uint256 value) external returns (bool); /** * @dev Moves a `value` amount of tokens from `from` to `to` using the * allowance mechanism. `value` is then deducted from the caller's * allowance. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transferFrom(address from, address to, uint256 value) external returns (bool); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/utils/SafeERC20.sol) pragma solidity ^0.8.20; import {IERC20} from "../IERC20.sol"; import {IERC20Permit} from "../extensions/IERC20Permit.sol"; import {Address} from "../../../utils/Address.sol"; /** * @title SafeERC20 * @dev Wrappers around ERC20 operations that throw on failure (when the token * contract returns false). Tokens that return no value (and instead revert or * throw on failure) are also supported, non-reverting calls are assumed to be * successful. * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract, * which allows you to call the safe operations as `token.safeTransfer(...)`, etc. */ library SafeERC20 { using Address for address; /** * @dev An operation with an ERC20 token failed. */ error SafeERC20FailedOperation(address token); /** * @dev Indicates a failed `decreaseAllowance` request. */ error SafeERC20FailedDecreaseAllowance(address spender, uint256 currentAllowance, uint256 requestedDecrease); /** * @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value, * non-reverting calls are assumed to be successful. */ function safeTransfer(IERC20 token, address to, uint256 value) internal { _callOptionalReturn(token, abi.encodeCall(token.transfer, (to, value))); } /** * @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the * calling contract. If `token` returns no value, non-reverting calls are assumed to be successful. */ function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal { _callOptionalReturn(token, abi.encodeCall(token.transferFrom, (from, to, value))); } /** * @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value, * non-reverting calls are assumed to be successful. */ function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal { uint256 oldAllowance = token.allowance(address(this), spender); forceApprove(token, spender, oldAllowance + value); } /** * @dev Decrease the calling contract's allowance toward `spender` by `requestedDecrease`. If `token` returns no * value, non-reverting calls are assumed to be successful. */ function safeDecreaseAllowance(IERC20 token, address spender, uint256 requestedDecrease) internal { unchecked { uint256 currentAllowance = token.allowance(address(this), spender); if (currentAllowance < requestedDecrease) { revert SafeERC20FailedDecreaseAllowance(spender, currentAllowance, requestedDecrease); } forceApprove(token, spender, currentAllowance - requestedDecrease); } } /** * @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value, * non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval * to be set to zero before setting it to a non-zero value, such as USDT. */ function forceApprove(IERC20 token, address spender, uint256 value) internal { bytes memory approvalCall = abi.encodeCall(token.approve, (spender, value)); if (!_callOptionalReturnBool(token, approvalCall)) { _callOptionalReturn(token, abi.encodeCall(token.approve, (spender, 0))); _callOptionalReturn(token, approvalCall); } } /** * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement * on the return value: the return value is optional (but if data is returned, it must not be false). * @param token The token targeted by the call. * @param data The call data (encoded using abi.encode or one of its variants). */ function _callOptionalReturn(IERC20 token, bytes memory data) private { // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since // we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that // the target address contains contract code and also asserts for success in the low-level call. bytes memory returndata = address(token).functionCall(data); if (returndata.length != 0 && !abi.decode(returndata, (bool))) { revert SafeERC20FailedOperation(address(token)); } } /** * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement * on the return value: the return value is optional (but if data is returned, it must not be false). * @param token The token targeted by the call. * @param data The call data (encoded using abi.encode or one of its variants). * * This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead. */ function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) { // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since // we're implementing it ourselves. We cannot use {Address-functionCall} here since this should return false // and not revert is the subcall reverts. (bool success, bytes memory returndata) = address(token).call(data); return success && (returndata.length == 0 || abi.decode(returndata, (bool))) && address(token).code.length > 0; } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/Address.sol) pragma solidity ^0.8.20; /** * @dev Collection of functions related to the address type */ library Address { /** * @dev The ETH balance of the account is not enough to perform the operation. */ error AddressInsufficientBalance(address account); /** * @dev There's no code at `target` (it is not a contract). */ error AddressEmptyCode(address target); /** * @dev A call to an address target failed. The target may have reverted. */ error FailedInnerCall(); /** * @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 AddressInsufficientBalance(address(this)); } (bool success, ) = recipient.call{value: amount}(""); if (!success) { revert FailedInnerCall(); } } /** * @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 * {FailedInnerCall} 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 AddressInsufficientBalance(address(this)); } (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 {FailedInnerCall}) 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 {FailedInnerCall} 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 {FailedInnerCall}. */ 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 /// @solidity memory-safe-assembly assembly { let returndata_size := mload(returndata) revert(add(32, returndata), returndata_size) } } else { revert FailedInnerCall(); } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/cryptography/ECDSA.sol) pragma solidity ^0.8.20; /** * @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations. * * These functions can be used to verify that a message was signed by the holder * of the private keys of a given address. */ library ECDSA { enum RecoverError { NoError, InvalidSignature, InvalidSignatureLength, InvalidSignatureS } /** * @dev The signature derives the `address(0)`. */ error ECDSAInvalidSignature(); /** * @dev The signature has an invalid length. */ error ECDSAInvalidSignatureLength(uint256 length); /** * @dev The signature has an S value that is in the upper half order. */ error ECDSAInvalidSignatureS(bytes32 s); /** * @dev Returns the address that signed a hashed message (`hash`) with `signature` or an error. This will not * return address(0) without also returning an error description. Errors are documented using an enum (error type) * and a bytes32 providing additional information about the error. * * If no error is returned, then the address can be used for verification purposes. * * The `ecrecover` EVM precompile allows for malleable (non-unique) signatures: * this function rejects them by requiring the `s` value to be in the lower * half order, and the `v` value to be either 27 or 28. * * IMPORTANT: `hash` _must_ be the result of a hash operation for the * verification to be secure: it is possible to craft signatures that * recover to arbitrary addresses for non-hashed data. A safe way to ensure * this is by receiving a hash of the original message (which may otherwise * be too long), and then calling {MessageHashUtils-toEthSignedMessageHash} on it. * * Documentation for signature generation: * - with https://web3js.readthedocs.io/en/v1.3.4/web3-eth-accounts.html#sign[Web3.js] * - with https://docs.ethers.io/v5/api/signer/#Signer-signMessage[ethers] */ function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError, bytes32) { if (signature.length == 65) { bytes32 r; bytes32 s; uint8 v; // ecrecover takes the signature parameters, and the only way to get them // currently is to use assembly. /// @solidity memory-safe-assembly assembly { r := mload(add(signature, 0x20)) s := mload(add(signature, 0x40)) v := byte(0, mload(add(signature, 0x60))) } return tryRecover(hash, v, r, s); } else { return (address(0), RecoverError.InvalidSignatureLength, bytes32(signature.length)); } } /** * @dev Returns the address that signed a hashed message (`hash`) with * `signature`. This address can then be used for verification purposes. * * The `ecrecover` EVM precompile allows for malleable (non-unique) signatures: * this function rejects them by requiring the `s` value to be in the lower * half order, and the `v` value to be either 27 or 28. * * IMPORTANT: `hash` _must_ be the result of a hash operation for the * verification to be secure: it is possible to craft signatures that * recover to arbitrary addresses for non-hashed data. A safe way to ensure * this is by receiving a hash of the original message (which may otherwise * be too long), and then calling {MessageHashUtils-toEthSignedMessageHash} on it. */ function recover(bytes32 hash, bytes memory signature) internal pure returns (address) { (address recovered, RecoverError error, bytes32 errorArg) = tryRecover(hash, signature); _throwError(error, errorArg); return recovered; } /** * @dev Overload of {ECDSA-tryRecover} that receives the `r` and `vs` short-signature fields separately. * * See https://eips.ethereum.org/EIPS/eip-2098[EIP-2098 short signatures] */ function tryRecover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address, RecoverError, bytes32) { unchecked { bytes32 s = vs & bytes32(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); // We do not check for an overflow here since the shift operation results in 0 or 1. uint8 v = uint8((uint256(vs) >> 255) + 27); return tryRecover(hash, v, r, s); } } /** * @dev Overload of {ECDSA-recover} that receives the `r and `vs` short-signature fields separately. */ function recover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address) { (address recovered, RecoverError error, bytes32 errorArg) = tryRecover(hash, r, vs); _throwError(error, errorArg); return recovered; } /** * @dev Overload of {ECDSA-tryRecover} that receives the `v`, * `r` and `s` signature fields separately. */ function tryRecover( bytes32 hash, uint8 v, bytes32 r, bytes32 s ) internal pure returns (address, RecoverError, bytes32) { // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines // the valid range for s in (301): 0 < s < secp256k1n ÷ 2 + 1, and for v in (302): v ∈ {27, 28}. Most // signatures from current libraries generate a unique signature with an s-value in the lower half order. // // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept // these malleable signatures as well. if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) { return (address(0), RecoverError.InvalidSignatureS, s); } // If the signature is valid (and not malleable), return the signer address address signer = ecrecover(hash, v, r, s); if (signer == address(0)) { return (address(0), RecoverError.InvalidSignature, bytes32(0)); } return (signer, RecoverError.NoError, bytes32(0)); } /** * @dev Overload of {ECDSA-recover} that receives the `v`, * `r` and `s` signature fields separately. */ function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address) { (address recovered, RecoverError error, bytes32 errorArg) = tryRecover(hash, v, r, s); _throwError(error, errorArg); return recovered; } /** * @dev Optionally reverts with the corresponding custom error according to the `error` argument provided. */ function _throwError(RecoverError error, bytes32 errorArg) private pure { if (error == RecoverError.NoError) { return; // no error: do nothing } else if (error == RecoverError.InvalidSignature) { revert ECDSAInvalidSignature(); } else if (error == RecoverError.InvalidSignatureLength) { revert ECDSAInvalidSignatureLength(uint256(errorArg)); } else if (error == RecoverError.InvalidSignatureS) { revert ECDSAInvalidSignatureS(errorArg); } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/cryptography/EIP712.sol) pragma solidity ^0.8.20; import {MessageHashUtils} from "./MessageHashUtils.sol"; import {ShortStrings, ShortString} from "../ShortStrings.sol"; import {IERC5267} from "../../interfaces/IERC5267.sol"; /** * @dev https://eips.ethereum.org/EIPS/eip-712[EIP 712] is a standard for hashing and signing of typed structured data. * * The encoding scheme specified in the EIP requires a domain separator and a hash of the typed structured data, whose * encoding is very generic and therefore its implementation in Solidity is not feasible, thus this contract * does not implement the encoding itself. Protocols need to implement the type-specific encoding they need in order to * produce the hash of their typed data using a combination of `abi.encode` and `keccak256`. * * This contract implements the EIP 712 domain separator ({_domainSeparatorV4}) that is used as part of the encoding * scheme, and the final step of the encoding to obtain the message digest that is then signed via ECDSA * ({_hashTypedDataV4}). * * The implementation of the domain separator was designed to be as efficient as possible while still properly updating * the chain id to protect against replay attacks on an eventual fork of the chain. * * NOTE: This contract implements the version of the encoding known as "v4", as implemented by the JSON RPC method * https://docs.metamask.io/guide/signing-data.html[`eth_signTypedDataV4` in MetaMask]. * * NOTE: In the upgradeable version of this contract, the cached values will correspond to the address, and the domain * separator of the implementation contract. This will cause the {_domainSeparatorV4} function to always rebuild the * separator from the immutable values, which is cheaper than accessing a cached version in cold storage. * * @custom:oz-upgrades-unsafe-allow state-variable-immutable */ abstract contract EIP712 is IERC5267 { using ShortStrings for *; bytes32 private constant TYPE_HASH = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"); // Cache the domain separator as an immutable value, but also store the chain id that it corresponds to, in order to // invalidate the cached domain separator if the chain id changes. bytes32 private immutable _cachedDomainSeparator; uint256 private immutable _cachedChainId; address private immutable _cachedThis; bytes32 private immutable _hashedName; bytes32 private immutable _hashedVersion; ShortString private immutable _name; ShortString private immutable _version; string private _nameFallback; string private _versionFallback; /** * @dev Initializes the domain separator and parameter caches. * * The meaning of `name` and `version` is specified in * https://eips.ethereum.org/EIPS/eip-712#definition-of-domainseparator[EIP 712]: * * - `name`: the user readable name of the signing domain, i.e. the name of the DApp or the protocol. * - `version`: the current major version of the signing domain. * * NOTE: These parameters cannot be changed except through a xref:learn::upgrading-smart-contracts.adoc[smart * contract upgrade]. */ constructor(string memory name, string memory version) { _name = name.toShortStringWithFallback(_nameFallback); _version = version.toShortStringWithFallback(_versionFallback); _hashedName = keccak256(bytes(name)); _hashedVersion = keccak256(bytes(version)); _cachedChainId = block.chainid; _cachedDomainSeparator = _buildDomainSeparator(); _cachedThis = address(this); } /** * @dev Returns the domain separator for the current chain. */ function _domainSeparatorV4() internal view returns (bytes32) { if (address(this) == _cachedThis && block.chainid == _cachedChainId) { return _cachedDomainSeparator; } else { return _buildDomainSeparator(); } } function _buildDomainSeparator() private view returns (bytes32) { return keccak256(abi.encode(TYPE_HASH, _hashedName, _hashedVersion, block.chainid, address(this))); } /** * @dev Given an already https://eips.ethereum.org/EIPS/eip-712#definition-of-hashstruct[hashed struct], this * function returns the hash of the fully encoded EIP712 message for this domain. * * This hash can be used together with {ECDSA-recover} to obtain the signer of a message. For example: * * ```solidity * bytes32 digest = _hashTypedDataV4(keccak256(abi.encode( * keccak256("Mail(address to,string contents)"), * mailTo, * keccak256(bytes(mailContents)) * ))); * address signer = ECDSA.recover(digest, signature); * ``` */ function _hashTypedDataV4(bytes32 structHash) internal view virtual returns (bytes32) { return MessageHashUtils.toTypedDataHash(_domainSeparatorV4(), structHash); } /** * @dev See {IERC-5267}. */ function eip712Domain() public view virtual returns ( bytes1 fields, string memory name, string memory version, uint256 chainId, address verifyingContract, bytes32 salt, uint256[] memory extensions ) { return ( hex"0f", // 01111 _EIP712Name(), _EIP712Version(), block.chainid, address(this), bytes32(0), new uint256[](0) ); } /** * @dev The name parameter for the EIP712 domain. * * NOTE: By default this function reads _name which is an immutable value. * It only reads from storage if necessary (in case the value is too large to fit in a ShortString). */ // solhint-disable-next-line func-name-mixedcase function _EIP712Name() internal view returns (string memory) { return _name.toStringWithFallback(_nameFallback); } /** * @dev The version parameter for the EIP712 domain. * * NOTE: By default this function reads _version which is an immutable value. * It only reads from storage if necessary (in case the value is too large to fit in a ShortString). */ // solhint-disable-next-line func-name-mixedcase function _EIP712Version() internal view returns (string memory) { return _version.toStringWithFallback(_versionFallback); } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/cryptography/MessageHashUtils.sol) pragma solidity ^0.8.20; import {Strings} from "../Strings.sol"; /** * @dev Signature message hash utilities for producing digests to be consumed by {ECDSA} recovery or signing. * * The library provides methods for generating a hash of a message that conforms to the * https://eips.ethereum.org/EIPS/eip-191[EIP 191] and https://eips.ethereum.org/EIPS/eip-712[EIP 712] * specifications. */ library MessageHashUtils { /** * @dev Returns the keccak256 digest of an EIP-191 signed data with version * `0x45` (`personal_sign` messages). * * The digest is calculated by prefixing a bytes32 `messageHash` with * `"\x19Ethereum Signed Message:\n32"` and hashing the result. It corresponds with the * hash signed when using the https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`] JSON-RPC method. * * NOTE: The `messageHash` parameter is intended to be the result of hashing a raw message with * keccak256, although any bytes32 value can be safely used because the final digest will * be re-hashed. * * See {ECDSA-recover}. */ function toEthSignedMessageHash(bytes32 messageHash) internal pure returns (bytes32 digest) { /// @solidity memory-safe-assembly assembly { mstore(0x00, "\x19Ethereum Signed Message:\n32") // 32 is the bytes-length of messageHash mstore(0x1c, messageHash) // 0x1c (28) is the length of the prefix digest := keccak256(0x00, 0x3c) // 0x3c is the length of the prefix (0x1c) + messageHash (0x20) } } /** * @dev Returns the keccak256 digest of an EIP-191 signed data with version * `0x45` (`personal_sign` messages). * * The digest is calculated by prefixing an arbitrary `message` with * `"\x19Ethereum Signed Message:\n" + len(message)` and hashing the result. It corresponds with the * hash signed when using the https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`] JSON-RPC method. * * See {ECDSA-recover}. */ function toEthSignedMessageHash(bytes memory message) internal pure returns (bytes32) { return keccak256(bytes.concat("\x19Ethereum Signed Message:\n", bytes(Strings.toString(message.length)), message)); } /** * @dev Returns the keccak256 digest of an EIP-191 signed data with version * `0x00` (data with intended validator). * * The digest is calculated by prefixing an arbitrary `data` with `"\x19\x00"` and the intended * `validator` address. Then hashing the result. * * See {ECDSA-recover}. */ function toDataWithIntendedValidatorHash(address validator, bytes memory data) internal pure returns (bytes32) { return keccak256(abi.encodePacked(hex"19_00", validator, data)); } /** * @dev Returns the keccak256 digest of an EIP-712 typed data (EIP-191 version `0x01`). * * The digest is calculated from a `domainSeparator` and a `structHash`, by prefixing them with * `\x19\x01` and hashing the result. It corresponds to the hash signed by the * https://eips.ethereum.org/EIPS/eip-712[`eth_signTypedData`] JSON-RPC method as part of EIP-712. * * See {ECDSA-recover}. */ function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32 digest) { /// @solidity memory-safe-assembly assembly { let ptr := mload(0x40) mstore(ptr, hex"19_01") mstore(add(ptr, 0x02), domainSeparator) mstore(add(ptr, 0x22), structHash) digest := keccak256(ptr, 0x42) } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/introspection/ERC165.sol) pragma solidity ^0.8.20; import {IERC165} from "./IERC165.sol"; /** * @dev Implementation of the {IERC165} interface. * * Contracts that want to implement ERC165 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 ERC165 is IERC165 { /** * @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) (utils/introspection/IERC165.sol) pragma solidity ^0.8.20; /** * @dev Interface of the ERC165 standard, as defined in the * https://eips.ethereum.org/EIPS/eip-165[EIP]. * * 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[EIP 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) (utils/math/Math.sol) pragma solidity ^0.8.20; /** * @dev Standard math utilities missing in the Solidity language. */ library Math { /** * @dev Muldiv operation overflow. */ error MathOverflowedMulDiv(); enum Rounding { Floor, // Toward negative infinity Ceil, // Toward positive infinity Trunc, // Toward zero Expand // Away from zero } /** * @dev Returns the addition of two unsigned integers, with an overflow flag. */ function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) { unchecked { uint256 c = a + b; if (c < a) return (false, 0); return (true, c); } } /** * @dev Returns the subtraction of two unsigned integers, with an overflow flag. */ function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) { unchecked { if (b > a) return (false, 0); return (true, a - b); } } /** * @dev Returns the multiplication of two unsigned integers, with an overflow flag. */ function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) { unchecked { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) return (true, 0); uint256 c = a * b; if (c / a != b) return (false, 0); return (true, c); } } /** * @dev Returns the division of two unsigned integers, with a division by zero flag. */ function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) { unchecked { if (b == 0) return (false, 0); return (true, a / b); } } /** * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag. */ function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) { unchecked { if (b == 0) return (false, 0); return (true, a % b); } } /** * @dev Returns the largest of two numbers. */ function max(uint256 a, uint256 b) internal pure returns (uint256) { return a > b ? a : b; } /** * @dev Returns the smallest of two numbers. */ function min(uint256 a, uint256 b) internal pure returns (uint256) { return a < b ? a : b; } /** * @dev Returns the average of two numbers. The result is rounded towards * zero. */ function average(uint256 a, uint256 b) internal pure returns (uint256) { // (a + b) / 2 can overflow. return (a & b) + (a ^ b) / 2; } /** * @dev Returns the ceiling of the division of two numbers. * * This differs from standard division with `/` in that it rounds towards infinity instead * of rounding towards zero. */ function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) { if (b == 0) { // Guarantee the same behavior as in a regular Solidity division. return a / b; } // (a + b - 1) / b can overflow on addition, so we distribute. return a == 0 ? 0 : (a - 1) / b + 1; } /** * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or * denominator == 0. * @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv) with further edits by * Uniswap Labs also under MIT license. */ function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) { unchecked { // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256 // variables such that product = prod1 * 2^256 + prod0. uint256 prod0 = x * y; // Least significant 256 bits of the product uint256 prod1; // Most significant 256 bits of the product assembly { let mm := mulmod(x, y, not(0)) prod1 := sub(sub(mm, prod0), lt(mm, prod0)) } // Handle non-overflow cases, 256 by 256 division. if (prod1 == 0) { // Solidity will revert if denominator == 0, unlike the div opcode on its own. // The surrounding unchecked block does not change this fact. // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic. return prod0 / denominator; } // Make sure the result is less than 2^256. Also prevents denominator == 0. if (denominator <= prod1) { revert MathOverflowedMulDiv(); } /////////////////////////////////////////////// // 512 by 256 division. /////////////////////////////////////////////// // Make division exact by subtracting the remainder from [prod1 prod0]. uint256 remainder; assembly { // Compute remainder using mulmod. remainder := mulmod(x, y, denominator) // Subtract 256 bit number from 512 bit number. prod1 := sub(prod1, gt(remainder, prod0)) prod0 := sub(prod0, remainder) } // Factor powers of two out of denominator and compute largest power of two divisor of denominator. // Always >= 1. See https://cs.stackexchange.com/q/138556/92363. uint256 twos = denominator & (0 - denominator); assembly { // Divide denominator by twos. denominator := div(denominator, twos) // Divide [prod1 prod0] by twos. prod0 := div(prod0, twos) // Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one. twos := add(div(sub(0, twos), twos), 1) } // Shift in bits from prod1 into prod0. prod0 |= prod1 * twos; // Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for // four bits. That is, denominator * inv = 1 mod 2^4. uint256 inverse = (3 * denominator) ^ 2; // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also // works in modular arithmetic, doubling the correct bits in each step. inverse *= 2 - denominator * inverse; // inverse mod 2^8 inverse *= 2 - denominator * inverse; // inverse mod 2^16 inverse *= 2 - denominator * inverse; // inverse mod 2^32 inverse *= 2 - denominator * inverse; // inverse mod 2^64 inverse *= 2 - denominator * inverse; // inverse mod 2^128 inverse *= 2 - denominator * inverse; // inverse mod 2^256 // Because the division is now exact we can divide by multiplying with the modular inverse of denominator. // This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is // less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1 // is no longer required. result = prod0 * inverse; return result; } } /** * @notice Calculates x * y / denominator with full precision, following the selected rounding direction. */ function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) { uint256 result = mulDiv(x, y, denominator); if (unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0) { result += 1; } return result; } /** * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded * towards zero. * * Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11). */ function sqrt(uint256 a) internal pure returns (uint256) { if (a == 0) { return 0; } // For our first guess, we get the biggest power of 2 which is smaller than the square root of the target. // // We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have // `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`. // // This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)` // → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))` // → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)` // // Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit. uint256 result = 1 << (log2(a) >> 1); // At this point `result` is an estimation with one bit of precision. We know the true value is a uint128, // since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at // every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision // into the expected uint128 result. unchecked { result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; return min(result, a / result); } } /** * @notice Calculates sqrt(a), following the selected rounding direction. */ function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) { unchecked { uint256 result = sqrt(a); return result + (unsignedRoundsUp(rounding) && result * result < a ? 1 : 0); } } /** * @dev Return the log in base 2 of a positive value rounded towards zero. * Returns 0 if given 0. */ function log2(uint256 value) internal pure returns (uint256) { uint256 result = 0; unchecked { if (value >> 128 > 0) { value >>= 128; result += 128; } if (value >> 64 > 0) { value >>= 64; result += 64; } if (value >> 32 > 0) { value >>= 32; result += 32; } if (value >> 16 > 0) { value >>= 16; result += 16; } if (value >> 8 > 0) { value >>= 8; result += 8; } if (value >> 4 > 0) { value >>= 4; result += 4; } if (value >> 2 > 0) { value >>= 2; result += 2; } if (value >> 1 > 0) { result += 1; } } return result; } /** * @dev Return the log in base 2, following the selected rounding direction, of a positive value. * Returns 0 if given 0. */ function log2(uint256 value, Rounding rounding) internal pure returns (uint256) { unchecked { uint256 result = log2(value); return result + (unsignedRoundsUp(rounding) && 1 << result < value ? 1 : 0); } } /** * @dev Return the log in base 10 of a positive value rounded towards zero. * Returns 0 if given 0. */ function log10(uint256 value) internal pure returns (uint256) { uint256 result = 0; unchecked { if (value >= 10 ** 64) { value /= 10 ** 64; result += 64; } if (value >= 10 ** 32) { value /= 10 ** 32; result += 32; } if (value >= 10 ** 16) { value /= 10 ** 16; result += 16; } if (value >= 10 ** 8) { value /= 10 ** 8; result += 8; } if (value >= 10 ** 4) { value /= 10 ** 4; result += 4; } if (value >= 10 ** 2) { value /= 10 ** 2; result += 2; } if (value >= 10 ** 1) { result += 1; } } return result; } /** * @dev Return the log in base 10, following the selected rounding direction, of a positive value. * Returns 0 if given 0. */ function log10(uint256 value, Rounding rounding) internal pure returns (uint256) { unchecked { uint256 result = log10(value); return result + (unsignedRoundsUp(rounding) && 10 ** result < value ? 1 : 0); } } /** * @dev Return the log in base 256 of a positive value rounded towards zero. * Returns 0 if given 0. * * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string. */ function log256(uint256 value) internal pure returns (uint256) { uint256 result = 0; unchecked { if (value >> 128 > 0) { value >>= 128; result += 16; } if (value >> 64 > 0) { value >>= 64; result += 8; } if (value >> 32 > 0) { value >>= 32; result += 4; } if (value >> 16 > 0) { value >>= 16; result += 2; } if (value >> 8 > 0) { result += 1; } } return result; } /** * @dev Return the log in base 256, following the selected rounding direction, of a positive value. * Returns 0 if given 0. */ function log256(uint256 value, Rounding rounding) internal pure returns (uint256) { unchecked { uint256 result = log256(value); return result + (unsignedRoundsUp(rounding) && 1 << (result << 3) < value ? 1 : 0); } } /** * @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers. */ function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) { return uint8(rounding) % 2 == 1; } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/math/SafeCast.sol) // This file was procedurally generated from scripts/generate/templates/SafeCast.js. pragma solidity ^0.8.20; /** * @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow * checks. * * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can * easily result in undesired exploitation or bugs, since developers usually * assume that overflows raise errors. `SafeCast` restores this intuition by * reverting the transaction when such an operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. */ library SafeCast { /** * @dev Value doesn't fit in an uint of `bits` size. */ error SafeCastOverflowedUintDowncast(uint8 bits, uint256 value); /** * @dev An int value doesn't fit in an uint of `bits` size. */ error SafeCastOverflowedIntToUint(int256 value); /** * @dev Value doesn't fit in an int of `bits` size. */ error SafeCastOverflowedIntDowncast(uint8 bits, int256 value); /** * @dev An uint value doesn't fit in an int of `bits` size. */ error SafeCastOverflowedUintToInt(uint256 value); /** * @dev Returns the downcasted uint248 from uint256, reverting on * overflow (when the input is greater than largest uint248). * * Counterpart to Solidity's `uint248` operator. * * Requirements: * * - input must fit into 248 bits */ function toUint248(uint256 value) internal pure returns (uint248) { if (value > type(uint248).max) { revert SafeCastOverflowedUintDowncast(248, value); } return uint248(value); } /** * @dev Returns the downcasted uint240 from uint256, reverting on * overflow (when the input is greater than largest uint240). * * Counterpart to Solidity's `uint240` operator. * * Requirements: * * - input must fit into 240 bits */ function toUint240(uint256 value) internal pure returns (uint240) { if (value > type(uint240).max) { revert SafeCastOverflowedUintDowncast(240, value); } return uint240(value); } /** * @dev Returns the downcasted uint232 from uint256, reverting on * overflow (when the input is greater than largest uint232). * * Counterpart to Solidity's `uint232` operator. * * Requirements: * * - input must fit into 232 bits */ function toUint232(uint256 value) internal pure returns (uint232) { if (value > type(uint232).max) { revert SafeCastOverflowedUintDowncast(232, value); } return uint232(value); } /** * @dev Returns the downcasted uint224 from uint256, reverting on * overflow (when the input is greater than largest uint224). * * Counterpart to Solidity's `uint224` operator. * * Requirements: * * - input must fit into 224 bits */ function toUint224(uint256 value) internal pure returns (uint224) { if (value > type(uint224).max) { revert SafeCastOverflowedUintDowncast(224, value); } return uint224(value); } /** * @dev Returns the downcasted uint216 from uint256, reverting on * overflow (when the input is greater than largest uint216). * * Counterpart to Solidity's `uint216` operator. * * Requirements: * * - input must fit into 216 bits */ function toUint216(uint256 value) internal pure returns (uint216) { if (value > type(uint216).max) { revert SafeCastOverflowedUintDowncast(216, value); } return uint216(value); } /** * @dev Returns the downcasted uint208 from uint256, reverting on * overflow (when the input is greater than largest uint208). * * Counterpart to Solidity's `uint208` operator. * * Requirements: * * - input must fit into 208 bits */ function toUint208(uint256 value) internal pure returns (uint208) { if (value > type(uint208).max) { revert SafeCastOverflowedUintDowncast(208, value); } return uint208(value); } /** * @dev Returns the downcasted uint200 from uint256, reverting on * overflow (when the input is greater than largest uint200). * * Counterpart to Solidity's `uint200` operator. * * Requirements: * * - input must fit into 200 bits */ function toUint200(uint256 value) internal pure returns (uint200) { if (value > type(uint200).max) { revert SafeCastOverflowedUintDowncast(200, value); } return uint200(value); } /** * @dev Returns the downcasted uint192 from uint256, reverting on * overflow (when the input is greater than largest uint192). * * Counterpart to Solidity's `uint192` operator. * * Requirements: * * - input must fit into 192 bits */ function toUint192(uint256 value) internal pure returns (uint192) { if (value > type(uint192).max) { revert SafeCastOverflowedUintDowncast(192, value); } return uint192(value); } /** * @dev Returns the downcasted uint184 from uint256, reverting on * overflow (when the input is greater than largest uint184). * * Counterpart to Solidity's `uint184` operator. * * Requirements: * * - input must fit into 184 bits */ function toUint184(uint256 value) internal pure returns (uint184) { if (value > type(uint184).max) { revert SafeCastOverflowedUintDowncast(184, value); } return uint184(value); } /** * @dev Returns the downcasted uint176 from uint256, reverting on * overflow (when the input is greater than largest uint176). * * Counterpart to Solidity's `uint176` operator. * * Requirements: * * - input must fit into 176 bits */ function toUint176(uint256 value) internal pure returns (uint176) { if (value > type(uint176).max) { revert SafeCastOverflowedUintDowncast(176, value); } return uint176(value); } /** * @dev Returns the downcasted uint168 from uint256, reverting on * overflow (when the input is greater than largest uint168). * * Counterpart to Solidity's `uint168` operator. * * Requirements: * * - input must fit into 168 bits */ function toUint168(uint256 value) internal pure returns (uint168) { if (value > type(uint168).max) { revert SafeCastOverflowedUintDowncast(168, value); } return uint168(value); } /** * @dev Returns the downcasted uint160 from uint256, reverting on * overflow (when the input is greater than largest uint160). * * Counterpart to Solidity's `uint160` operator. * * Requirements: * * - input must fit into 160 bits */ function toUint160(uint256 value) internal pure returns (uint160) { if (value > type(uint160).max) { revert SafeCastOverflowedUintDowncast(160, value); } return uint160(value); } /** * @dev Returns the downcasted uint152 from uint256, reverting on * overflow (when the input is greater than largest uint152). * * Counterpart to Solidity's `uint152` operator. * * Requirements: * * - input must fit into 152 bits */ function toUint152(uint256 value) internal pure returns (uint152) { if (value > type(uint152).max) { revert SafeCastOverflowedUintDowncast(152, value); } return uint152(value); } /** * @dev Returns the downcasted uint144 from uint256, reverting on * overflow (when the input is greater than largest uint144). * * Counterpart to Solidity's `uint144` operator. * * Requirements: * * - input must fit into 144 bits */ function toUint144(uint256 value) internal pure returns (uint144) { if (value > type(uint144).max) { revert SafeCastOverflowedUintDowncast(144, value); } return uint144(value); } /** * @dev Returns the downcasted uint136 from uint256, reverting on * overflow (when the input is greater than largest uint136). * * Counterpart to Solidity's `uint136` operator. * * Requirements: * * - input must fit into 136 bits */ function toUint136(uint256 value) internal pure returns (uint136) { if (value > type(uint136).max) { revert SafeCastOverflowedUintDowncast(136, value); } return uint136(value); } /** * @dev Returns the downcasted uint128 from uint256, reverting on * overflow (when the input is greater than largest uint128). * * Counterpart to Solidity's `uint128` operator. * * Requirements: * * - input must fit into 128 bits */ function toUint128(uint256 value) internal pure returns (uint128) { if (value > type(uint128).max) { revert SafeCastOverflowedUintDowncast(128, value); } return uint128(value); } /** * @dev Returns the downcasted uint120 from uint256, reverting on * overflow (when the input is greater than largest uint120). * * Counterpart to Solidity's `uint120` operator. * * Requirements: * * - input must fit into 120 bits */ function toUint120(uint256 value) internal pure returns (uint120) { if (value > type(uint120).max) { revert SafeCastOverflowedUintDowncast(120, value); } return uint120(value); } /** * @dev Returns the downcasted uint112 from uint256, reverting on * overflow (when the input is greater than largest uint112). * * Counterpart to Solidity's `uint112` operator. * * Requirements: * * - input must fit into 112 bits */ function toUint112(uint256 value) internal pure returns (uint112) { if (value > type(uint112).max) { revert SafeCastOverflowedUintDowncast(112, value); } return uint112(value); } /** * @dev Returns the downcasted uint104 from uint256, reverting on * overflow (when the input is greater than largest uint104). * * Counterpart to Solidity's `uint104` operator. * * Requirements: * * - input must fit into 104 bits */ function toUint104(uint256 value) internal pure returns (uint104) { if (value > type(uint104).max) { revert SafeCastOverflowedUintDowncast(104, value); } return uint104(value); } /** * @dev Returns the downcasted uint96 from uint256, reverting on * overflow (when the input is greater than largest uint96). * * Counterpart to Solidity's `uint96` operator. * * Requirements: * * - input must fit into 96 bits */ function toUint96(uint256 value) internal pure returns (uint96) { if (value > type(uint96).max) { revert SafeCastOverflowedUintDowncast(96, value); } return uint96(value); } /** * @dev Returns the downcasted uint88 from uint256, reverting on * overflow (when the input is greater than largest uint88). * * Counterpart to Solidity's `uint88` operator. * * Requirements: * * - input must fit into 88 bits */ function toUint88(uint256 value) internal pure returns (uint88) { if (value > type(uint88).max) { revert SafeCastOverflowedUintDowncast(88, value); } return uint88(value); } /** * @dev Returns the downcasted uint80 from uint256, reverting on * overflow (when the input is greater than largest uint80). * * Counterpart to Solidity's `uint80` operator. * * Requirements: * * - input must fit into 80 bits */ function toUint80(uint256 value) internal pure returns (uint80) { if (value > type(uint80).max) { revert SafeCastOverflowedUintDowncast(80, value); } return uint80(value); } /** * @dev Returns the downcasted uint72 from uint256, reverting on * overflow (when the input is greater than largest uint72). * * Counterpart to Solidity's `uint72` operator. * * Requirements: * * - input must fit into 72 bits */ function toUint72(uint256 value) internal pure returns (uint72) { if (value > type(uint72).max) { revert SafeCastOverflowedUintDowncast(72, value); } return uint72(value); } /** * @dev Returns the downcasted uint64 from uint256, reverting on * overflow (when the input is greater than largest uint64). * * Counterpart to Solidity's `uint64` operator. * * Requirements: * * - input must fit into 64 bits */ function toUint64(uint256 value) internal pure returns (uint64) { if (value > type(uint64).max) { revert SafeCastOverflowedUintDowncast(64, value); } return uint64(value); } /** * @dev Returns the downcasted uint56 from uint256, reverting on * overflow (when the input is greater than largest uint56). * * Counterpart to Solidity's `uint56` operator. * * Requirements: * * - input must fit into 56 bits */ function toUint56(uint256 value) internal pure returns (uint56) { if (value > type(uint56).max) { revert SafeCastOverflowedUintDowncast(56, value); } return uint56(value); } /** * @dev Returns the downcasted uint48 from uint256, reverting on * overflow (when the input is greater than largest uint48). * * Counterpart to Solidity's `uint48` operator. * * Requirements: * * - input must fit into 48 bits */ function toUint48(uint256 value) internal pure returns (uint48) { if (value > type(uint48).max) { revert SafeCastOverflowedUintDowncast(48, value); } return uint48(value); } /** * @dev Returns the downcasted uint40 from uint256, reverting on * overflow (when the input is greater than largest uint40). * * Counterpart to Solidity's `uint40` operator. * * Requirements: * * - input must fit into 40 bits */ function toUint40(uint256 value) internal pure returns (uint40) { if (value > type(uint40).max) { revert SafeCastOverflowedUintDowncast(40, value); } return uint40(value); } /** * @dev Returns the downcasted uint32 from uint256, reverting on * overflow (when the input is greater than largest uint32). * * Counterpart to Solidity's `uint32` operator. * * Requirements: * * - input must fit into 32 bits */ function toUint32(uint256 value) internal pure returns (uint32) { if (value > type(uint32).max) { revert SafeCastOverflowedUintDowncast(32, value); } return uint32(value); } /** * @dev Returns the downcasted uint24 from uint256, reverting on * overflow (when the input is greater than largest uint24). * * Counterpart to Solidity's `uint24` operator. * * Requirements: * * - input must fit into 24 bits */ function toUint24(uint256 value) internal pure returns (uint24) { if (value > type(uint24).max) { revert SafeCastOverflowedUintDowncast(24, value); } return uint24(value); } /** * @dev Returns the downcasted uint16 from uint256, reverting on * overflow (when the input is greater than largest uint16). * * Counterpart to Solidity's `uint16` operator. * * Requirements: * * - input must fit into 16 bits */ function toUint16(uint256 value) internal pure returns (uint16) { if (value > type(uint16).max) { revert SafeCastOverflowedUintDowncast(16, value); } return uint16(value); } /** * @dev Returns the downcasted uint8 from uint256, reverting on * overflow (when the input is greater than largest uint8). * * Counterpart to Solidity's `uint8` operator. * * Requirements: * * - input must fit into 8 bits */ function toUint8(uint256 value) internal pure returns (uint8) { if (value > type(uint8).max) { revert SafeCastOverflowedUintDowncast(8, value); } return uint8(value); } /** * @dev Converts a signed int256 into an unsigned uint256. * * Requirements: * * - input must be greater than or equal to 0. */ function toUint256(int256 value) internal pure returns (uint256) { if (value < 0) { revert SafeCastOverflowedIntToUint(value); } return uint256(value); } /** * @dev Returns the downcasted int248 from int256, reverting on * overflow (when the input is less than smallest int248 or * greater than largest int248). * * Counterpart to Solidity's `int248` operator. * * Requirements: * * - input must fit into 248 bits */ function toInt248(int256 value) internal pure returns (int248 downcasted) { downcasted = int248(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(248, value); } } /** * @dev Returns the downcasted int240 from int256, reverting on * overflow (when the input is less than smallest int240 or * greater than largest int240). * * Counterpart to Solidity's `int240` operator. * * Requirements: * * - input must fit into 240 bits */ function toInt240(int256 value) internal pure returns (int240 downcasted) { downcasted = int240(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(240, value); } } /** * @dev Returns the downcasted int232 from int256, reverting on * overflow (when the input is less than smallest int232 or * greater than largest int232). * * Counterpart to Solidity's `int232` operator. * * Requirements: * * - input must fit into 232 bits */ function toInt232(int256 value) internal pure returns (int232 downcasted) { downcasted = int232(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(232, value); } } /** * @dev Returns the downcasted int224 from int256, reverting on * overflow (when the input is less than smallest int224 or * greater than largest int224). * * Counterpart to Solidity's `int224` operator. * * Requirements: * * - input must fit into 224 bits */ function toInt224(int256 value) internal pure returns (int224 downcasted) { downcasted = int224(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(224, value); } } /** * @dev Returns the downcasted int216 from int256, reverting on * overflow (when the input is less than smallest int216 or * greater than largest int216). * * Counterpart to Solidity's `int216` operator. * * Requirements: * * - input must fit into 216 bits */ function toInt216(int256 value) internal pure returns (int216 downcasted) { downcasted = int216(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(216, value); } } /** * @dev Returns the downcasted int208 from int256, reverting on * overflow (when the input is less than smallest int208 or * greater than largest int208). * * Counterpart to Solidity's `int208` operator. * * Requirements: * * - input must fit into 208 bits */ function toInt208(int256 value) internal pure returns (int208 downcasted) { downcasted = int208(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(208, value); } } /** * @dev Returns the downcasted int200 from int256, reverting on * overflow (when the input is less than smallest int200 or * greater than largest int200). * * Counterpart to Solidity's `int200` operator. * * Requirements: * * - input must fit into 200 bits */ function toInt200(int256 value) internal pure returns (int200 downcasted) { downcasted = int200(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(200, value); } } /** * @dev Returns the downcasted int192 from int256, reverting on * overflow (when the input is less than smallest int192 or * greater than largest int192). * * Counterpart to Solidity's `int192` operator. * * Requirements: * * - input must fit into 192 bits */ function toInt192(int256 value) internal pure returns (int192 downcasted) { downcasted = int192(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(192, value); } } /** * @dev Returns the downcasted int184 from int256, reverting on * overflow (when the input is less than smallest int184 or * greater than largest int184). * * Counterpart to Solidity's `int184` operator. * * Requirements: * * - input must fit into 184 bits */ function toInt184(int256 value) internal pure returns (int184 downcasted) { downcasted = int184(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(184, value); } } /** * @dev Returns the downcasted int176 from int256, reverting on * overflow (when the input is less than smallest int176 or * greater than largest int176). * * Counterpart to Solidity's `int176` operator. * * Requirements: * * - input must fit into 176 bits */ function toInt176(int256 value) internal pure returns (int176 downcasted) { downcasted = int176(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(176, value); } } /** * @dev Returns the downcasted int168 from int256, reverting on * overflow (when the input is less than smallest int168 or * greater than largest int168). * * Counterpart to Solidity's `int168` operator. * * Requirements: * * - input must fit into 168 bits */ function toInt168(int256 value) internal pure returns (int168 downcasted) { downcasted = int168(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(168, value); } } /** * @dev Returns the downcasted int160 from int256, reverting on * overflow (when the input is less than smallest int160 or * greater than largest int160). * * Counterpart to Solidity's `int160` operator. * * Requirements: * * - input must fit into 160 bits */ function toInt160(int256 value) internal pure returns (int160 downcasted) { downcasted = int160(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(160, value); } } /** * @dev Returns the downcasted int152 from int256, reverting on * overflow (when the input is less than smallest int152 or * greater than largest int152). * * Counterpart to Solidity's `int152` operator. * * Requirements: * * - input must fit into 152 bits */ function toInt152(int256 value) internal pure returns (int152 downcasted) { downcasted = int152(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(152, value); } } /** * @dev Returns the downcasted int144 from int256, reverting on * overflow (when the input is less than smallest int144 or * greater than largest int144). * * Counterpart to Solidity's `int144` operator. * * Requirements: * * - input must fit into 144 bits */ function toInt144(int256 value) internal pure returns (int144 downcasted) { downcasted = int144(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(144, value); } } /** * @dev Returns the downcasted int136 from int256, reverting on * overflow (when the input is less than smallest int136 or * greater than largest int136). * * Counterpart to Solidity's `int136` operator. * * Requirements: * * - input must fit into 136 bits */ function toInt136(int256 value) internal pure returns (int136 downcasted) { downcasted = int136(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(136, value); } } /** * @dev Returns the downcasted int128 from int256, reverting on * overflow (when the input is less than smallest int128 or * greater than largest int128). * * Counterpart to Solidity's `int128` operator. * * Requirements: * * - input must fit into 128 bits */ function toInt128(int256 value) internal pure returns (int128 downcasted) { downcasted = int128(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(128, value); } } /** * @dev Returns the downcasted int120 from int256, reverting on * overflow (when the input is less than smallest int120 or * greater than largest int120). * * Counterpart to Solidity's `int120` operator. * * Requirements: * * - input must fit into 120 bits */ function toInt120(int256 value) internal pure returns (int120 downcasted) { downcasted = int120(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(120, value); } } /** * @dev Returns the downcasted int112 from int256, reverting on * overflow (when the input is less than smallest int112 or * greater than largest int112). * * Counterpart to Solidity's `int112` operator. * * Requirements: * * - input must fit into 112 bits */ function toInt112(int256 value) internal pure returns (int112 downcasted) { downcasted = int112(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(112, value); } } /** * @dev Returns the downcasted int104 from int256, reverting on * overflow (when the input is less than smallest int104 or * greater than largest int104). * * Counterpart to Solidity's `int104` operator. * * Requirements: * * - input must fit into 104 bits */ function toInt104(int256 value) internal pure returns (int104 downcasted) { downcasted = int104(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(104, value); } } /** * @dev Returns the downcasted int96 from int256, reverting on * overflow (when the input is less than smallest int96 or * greater than largest int96). * * Counterpart to Solidity's `int96` operator. * * Requirements: * * - input must fit into 96 bits */ function toInt96(int256 value) internal pure returns (int96 downcasted) { downcasted = int96(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(96, value); } } /** * @dev Returns the downcasted int88 from int256, reverting on * overflow (when the input is less than smallest int88 or * greater than largest int88). * * Counterpart to Solidity's `int88` operator. * * Requirements: * * - input must fit into 88 bits */ function toInt88(int256 value) internal pure returns (int88 downcasted) { downcasted = int88(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(88, value); } } /** * @dev Returns the downcasted int80 from int256, reverting on * overflow (when the input is less than smallest int80 or * greater than largest int80). * * Counterpart to Solidity's `int80` operator. * * Requirements: * * - input must fit into 80 bits */ function toInt80(int256 value) internal pure returns (int80 downcasted) { downcasted = int80(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(80, value); } } /** * @dev Returns the downcasted int72 from int256, reverting on * overflow (when the input is less than smallest int72 or * greater than largest int72). * * Counterpart to Solidity's `int72` operator. * * Requirements: * * - input must fit into 72 bits */ function toInt72(int256 value) internal pure returns (int72 downcasted) { downcasted = int72(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(72, value); } } /** * @dev Returns the downcasted int64 from int256, reverting on * overflow (when the input is less than smallest int64 or * greater than largest int64). * * Counterpart to Solidity's `int64` operator. * * Requirements: * * - input must fit into 64 bits */ function toInt64(int256 value) internal pure returns (int64 downcasted) { downcasted = int64(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(64, value); } } /** * @dev Returns the downcasted int56 from int256, reverting on * overflow (when the input is less than smallest int56 or * greater than largest int56). * * Counterpart to Solidity's `int56` operator. * * Requirements: * * - input must fit into 56 bits */ function toInt56(int256 value) internal pure returns (int56 downcasted) { downcasted = int56(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(56, value); } } /** * @dev Returns the downcasted int48 from int256, reverting on * overflow (when the input is less than smallest int48 or * greater than largest int48). * * Counterpart to Solidity's `int48` operator. * * Requirements: * * - input must fit into 48 bits */ function toInt48(int256 value) internal pure returns (int48 downcasted) { downcasted = int48(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(48, value); } } /** * @dev Returns the downcasted int40 from int256, reverting on * overflow (when the input is less than smallest int40 or * greater than largest int40). * * Counterpart to Solidity's `int40` operator. * * Requirements: * * - input must fit into 40 bits */ function toInt40(int256 value) internal pure returns (int40 downcasted) { downcasted = int40(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(40, value); } } /** * @dev Returns the downcasted int32 from int256, reverting on * overflow (when the input is less than smallest int32 or * greater than largest int32). * * Counterpart to Solidity's `int32` operator. * * Requirements: * * - input must fit into 32 bits */ function toInt32(int256 value) internal pure returns (int32 downcasted) { downcasted = int32(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(32, value); } } /** * @dev Returns the downcasted int24 from int256, reverting on * overflow (when the input is less than smallest int24 or * greater than largest int24). * * Counterpart to Solidity's `int24` operator. * * Requirements: * * - input must fit into 24 bits */ function toInt24(int256 value) internal pure returns (int24 downcasted) { downcasted = int24(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(24, value); } } /** * @dev Returns the downcasted int16 from int256, reverting on * overflow (when the input is less than smallest int16 or * greater than largest int16). * * Counterpart to Solidity's `int16` operator. * * Requirements: * * - input must fit into 16 bits */ function toInt16(int256 value) internal pure returns (int16 downcasted) { downcasted = int16(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(16, value); } } /** * @dev Returns the downcasted int8 from int256, reverting on * overflow (when the input is less than smallest int8 or * greater than largest int8). * * Counterpart to Solidity's `int8` operator. * * Requirements: * * - input must fit into 8 bits */ function toInt8(int256 value) internal pure returns (int8 downcasted) { downcasted = int8(value); if (downcasted != value) { revert SafeCastOverflowedIntDowncast(8, value); } } /** * @dev Converts an unsigned uint256 into a signed int256. * * Requirements: * * - input must be less than or equal to maxInt256. */ function toInt256(uint256 value) internal pure returns (int256) { // Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive if (value > uint256(type(int256).max)) { revert SafeCastOverflowedUintToInt(value); } return int256(value); } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/math/SignedMath.sol) pragma solidity ^0.8.20; /** * @dev Standard signed math utilities missing in the Solidity language. */ library SignedMath { /** * @dev Returns the largest of two signed numbers. */ function max(int256 a, int256 b) internal pure returns (int256) { return a > b ? a : b; } /** * @dev Returns the smallest of two signed numbers. */ function min(int256 a, int256 b) internal pure returns (int256) { return a < b ? a : b; } /** * @dev Returns the average of two signed numbers without overflow. * The result is rounded towards zero. */ function average(int256 a, int256 b) internal pure returns (int256) { // Formula from the book "Hacker's Delight" int256 x = (a & b) + ((a ^ b) >> 1); return x + (int256(uint256(x) >> 255) & (a ^ b)); } /** * @dev Returns the absolute unsigned value of a signed value. */ function abs(int256 n) internal pure returns (uint256) { unchecked { // must be unchecked in order to support `n = type(int256).min` return uint256(n >= 0 ? n : -n); } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/Nonces.sol) pragma solidity ^0.8.20; /** * @dev Provides tracking nonces for addresses. Nonces will only increment. */ abstract contract Nonces { /** * @dev The nonce used for an `account` is not the expected current nonce. */ error InvalidAccountNonce(address account, uint256 currentNonce); mapping(address account => uint256) private _nonces; /** * @dev Returns the next unused nonce for an address. */ function nonces(address owner) public view virtual returns (uint256) { return _nonces[owner]; } /** * @dev Consumes a nonce. * * Returns the current value and increments nonce. */ function _useNonce(address owner) internal virtual returns (uint256) { // For each account, the nonce has an initial value of 0, can only be incremented by one, and cannot be // decremented or reset. This guarantees that the nonce never overflows. unchecked { // It is important to do x++ and not ++x here. return _nonces[owner]++; } } /** * @dev Same as {_useNonce} but checking that `nonce` is the next valid for `owner`. */ function _useCheckedNonce(address owner, uint256 nonce) internal virtual { uint256 current = _useNonce(owner); if (nonce != current) { revert InvalidAccountNonce(owner, current); } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/ShortStrings.sol) pragma solidity ^0.8.20; import {StorageSlot} from "./StorageSlot.sol"; // | string | 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | // | length | 0x BB | type ShortString is bytes32; /** * @dev This library provides functions to convert short memory strings * into a `ShortString` type that can be used as an immutable variable. * * Strings of arbitrary length can be optimized using this library if * they are short enough (up to 31 bytes) by packing them with their * length (1 byte) in a single EVM word (32 bytes). Additionally, a * fallback mechanism can be used for every other case. * * Usage example: * * ```solidity * contract Named { * using ShortStrings for *; * * ShortString private immutable _name; * string private _nameFallback; * * constructor(string memory contractName) { * _name = contractName.toShortStringWithFallback(_nameFallback); * } * * function name() external view returns (string memory) { * return _name.toStringWithFallback(_nameFallback); * } * } * ``` */ library ShortStrings { // Used as an identifier for strings longer than 31 bytes. bytes32 private constant FALLBACK_SENTINEL = 0x00000000000000000000000000000000000000000000000000000000000000FF; error StringTooLong(string str); error InvalidShortString(); /** * @dev Encode a string of at most 31 chars into a `ShortString`. * * This will trigger a `StringTooLong` error is the input string is too long. */ function toShortString(string memory str) internal pure returns (ShortString) { bytes memory bstr = bytes(str); if (bstr.length > 31) { revert StringTooLong(str); } return ShortString.wrap(bytes32(uint256(bytes32(bstr)) | bstr.length)); } /** * @dev Decode a `ShortString` back to a "normal" string. */ function toString(ShortString sstr) internal pure returns (string memory) { uint256 len = byteLength(sstr); // using `new string(len)` would work locally but is not memory safe. string memory str = new string(32); /// @solidity memory-safe-assembly assembly { mstore(str, len) mstore(add(str, 0x20), sstr) } return str; } /** * @dev Return the length of a `ShortString`. */ function byteLength(ShortString sstr) internal pure returns (uint256) { uint256 result = uint256(ShortString.unwrap(sstr)) & 0xFF; if (result > 31) { revert InvalidShortString(); } return result; } /** * @dev Encode a string into a `ShortString`, or write it to storage if it is too long. */ function toShortStringWithFallback(string memory value, string storage store) internal returns (ShortString) { if (bytes(value).length < 32) { return toShortString(value); } else { StorageSlot.getStringSlot(store).value = value; return ShortString.wrap(FALLBACK_SENTINEL); } } /** * @dev Decode a string that was encoded to `ShortString` or written to storage using {setWithFallback}. */ function toStringWithFallback(ShortString value, string storage store) internal pure returns (string memory) { if (ShortString.unwrap(value) != FALLBACK_SENTINEL) { return toString(value); } else { return store; } } /** * @dev Return the length of a string that was encoded to `ShortString` or written to storage using * {setWithFallback}. * * WARNING: This will return the "byte length" of the string. This may not reflect the actual length in terms of * actual characters as the UTF-8 encoding of a single character can span over multiple bytes. */ function byteLengthWithFallback(ShortString value, string storage store) internal view returns (uint256) { if (ShortString.unwrap(value) != FALLBACK_SENTINEL) { return byteLength(value); } else { return bytes(store).length; } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.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 ERC1967 implementation slot: * ```solidity * contract ERC1967 { * 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; * } * } * ``` */ library StorageSlot { struct AddressSlot { address value; } struct BooleanSlot { bool value; } struct Bytes32Slot { bytes32 value; } struct Uint256Slot { uint256 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) { /// @solidity memory-safe-assembly assembly { r.slot := slot } } /** * @dev Returns an `BooleanSlot` with member `value` located at `slot`. */ function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) { /// @solidity memory-safe-assembly assembly { r.slot := slot } } /** * @dev Returns an `Bytes32Slot` with member `value` located at `slot`. */ function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) { /// @solidity memory-safe-assembly assembly { r.slot := slot } } /** * @dev Returns an `Uint256Slot` with member `value` located at `slot`. */ function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) { /// @solidity memory-safe-assembly assembly { r.slot := slot } } /** * @dev Returns an `StringSlot` with member `value` located at `slot`. */ function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) { /// @solidity memory-safe-assembly assembly { 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) { /// @solidity memory-safe-assembly assembly { r.slot := store.slot } } /** * @dev Returns an `BytesSlot` with member `value` located at `slot`. */ function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) { /// @solidity memory-safe-assembly assembly { 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) { /// @solidity memory-safe-assembly assembly { r.slot := store.slot } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/Strings.sol) pragma solidity ^0.8.20; import {Math} from "./math/Math.sol"; import {SignedMath} from "./math/SignedMath.sol"; /** * @dev String operations. */ library Strings { bytes16 private constant HEX_DIGITS = "0123456789abcdef"; uint8 private constant ADDRESS_LENGTH = 20; /** * @dev The `value` string doesn't fit in the specified `length`. */ error StringsInsufficientHexLength(uint256 value, uint256 length); /** * @dev Converts a `uint256` to its ASCII `string` decimal representation. */ function toString(uint256 value) internal pure returns (string memory) { unchecked { uint256 length = Math.log10(value) + 1; string memory buffer = new string(length); uint256 ptr; /// @solidity memory-safe-assembly assembly { ptr := add(buffer, add(32, length)) } while (true) { ptr--; /// @solidity memory-safe-assembly assembly { mstore8(ptr, byte(mod(value, 10), HEX_DIGITS)) } value /= 10; if (value == 0) break; } return buffer; } } /** * @dev Converts a `int256` to its ASCII `string` decimal representation. */ function toStringSigned(int256 value) internal pure returns (string memory) { return string.concat(value < 0 ? "-" : "", toString(SignedMath.abs(value))); } /** * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation. */ function toHexString(uint256 value) internal pure returns (string memory) { unchecked { return toHexString(value, Math.log256(value) + 1); } } /** * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length. */ function toHexString(uint256 value, uint256 length) internal pure returns (string memory) { uint256 localValue = value; bytes memory buffer = new bytes(2 * length + 2); buffer[0] = "0"; buffer[1] = "x"; for (uint256 i = 2 * length + 1; i > 1; --i) { buffer[i] = HEX_DIGITS[localValue & 0xf]; localValue >>= 4; } if (localValue != 0) { revert StringsInsufficientHexLength(value, length); } return string(buffer); } /** * @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal * representation. */ function toHexString(address addr) internal pure returns (string memory) { return toHexString(uint256(uint160(addr)), ADDRESS_LENGTH); } /** * @dev Returns true if the two strings are equal. */ function equal(string memory a, string memory b) internal pure returns (bool) { return bytes(a).length == bytes(b).length && keccak256(bytes(a)) == keccak256(bytes(b)); } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol"; import { IERC20Permit } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Permit.sol"; import { ERC165 } from "@openzeppelin/contracts/utils/introspection/ERC165.sol"; import { EIP712 } from "@openzeppelin/contracts/utils/cryptography/EIP712.sol"; import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { Nonces } from "@openzeppelin/contracts/utils/Nonces.sol"; import { IRateProvider } from "@balancer-labs/v3-interfaces/contracts/solidity-utils/helpers/IRateProvider.sol"; import { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol"; import { VaultGuard } from "./VaultGuard.sol"; /** * @notice `BalancerPoolToken` is a fully ERC20-compatible token to be used as the base contract for Balancer Pools, * with all the data and implementation delegated to the ERC20Multitoken contract. * @dev Implementation of the ERC-20 Permit extension allowing approvals to be made via signatures, as defined in * https://eips.ethereum.org/EIPS/eip-2612[ERC-2612]. */ contract BalancerPoolToken is IERC20, IERC20Metadata, IERC20Permit, IRateProvider, EIP712, Nonces, ERC165, VaultGuard { bytes32 public constant PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); /** * @notice Operation failed due to an expired permit signature. * @param deadline The permit deadline that expired */ error ERC2612ExpiredSignature(uint256 deadline); /** * @notice Operation failed due to a non-matching signature. * @param signer The address corresponding to the signature provider * @param owner The address of the owner (expected value of the signature provider) */ error ERC2612InvalidSigner(address signer, address owner); // EIP712 also defines _name. string private _bptName; string private _bptSymbol; constructor(IVault vault_, string memory bptName, string memory bptSymbol) EIP712(bptName, "1") VaultGuard(vault_) { _bptName = bptName; _bptSymbol = bptSymbol; } /// @inheritdoc IERC20Metadata function name() external view returns (string memory) { return _bptName; } /// @inheritdoc IERC20Metadata function symbol() external view returns (string memory) { return _bptSymbol; } /// @inheritdoc IERC20Metadata function decimals() external pure returns (uint8) { // Always 18 decimals for BPT. return 18; } /// @inheritdoc IERC20 function totalSupply() public view returns (uint256) { return _vault.totalSupply(address(this)); } function getVault() public view returns (IVault) { return _vault; } /// @inheritdoc IERC20 function balanceOf(address account) external view returns (uint256) { return _vault.balanceOf(address(this), account); } /// @inheritdoc IERC20 function transfer(address to, uint256 amount) external returns (bool) { // Vault will perform the transfer and call emitTransfer to emit the event from this contract. _vault.transfer(msg.sender, to, amount); return true; } /// @inheritdoc IERC20 function allowance(address owner, address spender) external view returns (uint256) { return _vault.allowance(address(this), owner, spender); } /// @inheritdoc IERC20 function approve(address spender, uint256 amount) external returns (bool) { // Vault will perform the approval and call emitApproval to emit the event from this contract. _vault.approve(msg.sender, spender, amount); return true; } /// @inheritdoc IERC20 function transferFrom(address from, address to, uint256 amount) external returns (bool) { // Vault will perform the transfer and call emitTransfer to emit the event from this contract. _vault.transferFrom(msg.sender, from, to, amount); return true; } /** * Accounting is centralized in the MultiToken contract, and the actual transfers and approvals are done there. * Operations can be initiated from either the token contract or the MultiToken. * * To maintain compliance with the ERC-20 standard, and conform to the expectations of off-chain processes, * the MultiToken calls `emitTransfer` and `emitApproval` during those operations, so that the event is emitted * only from the token contract. These events are NOT defined in the MultiToken contract. */ /// @dev Emit the Transfer event. This function can only be called by the MultiToken. function emitTransfer(address from, address to, uint256 amount) external onlyVault { emit Transfer(from, to, amount); } /// @dev Emit the Approval event. This function can only be called by the MultiToken. function emitApproval(address owner, address spender, uint256 amount) external onlyVault { emit Approval(owner, spender, amount); } // @inheritdoc IERC20Permit function permit( address owner, address spender, uint256 amount, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) public virtual { // solhint-disable-next-line not-rely-on-time if (block.timestamp > deadline) { revert ERC2612ExpiredSignature(deadline); } bytes32 structHash = keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, amount, _useNonce(owner), deadline)); bytes32 hash = _hashTypedDataV4(structHash); address signer = ECDSA.recover(hash, v, r, s); if (signer != owner) { revert ERC2612InvalidSigner(signer, owner); } _vault.approve(owner, spender, amount); } // @inheritdoc IERC20Permit function nonces(address owner) public view virtual override(IERC20Permit, Nonces) returns (uint256) { return super.nonces(owner); } /// @notice Increment the sender's nonce to revoke any currently granted (but not yet executed) `permit`. function incrementNonce() external { _useNonce(msg.sender); } // @inheritdoc IERC20Permit // solhint-disable-next-line func-name-mixedcase function DOMAIN_SEPARATOR() external view virtual returns (bytes32) { return _domainSeparatorV4(); } /** * @notice Get the BPT rate, which is defined as: pool invariant/total supply. * @dev The VaultExtension contract defines a default implementation (`getBptRate`) to calculate the rate * of any given pool, which should be sufficient in nearly all cases. * * @return rate Rate of the pool's BPT */ function getRate() public view virtual returns (uint256) { return getVault().getBptRate(address(this)); } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { FEE_BITLENGTH } from "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol"; /** * @notice Helper functions to read and write the packed configuration flags stored in `_poolConfigBits`. * @dev Note that the entire configuration of each pool is stored in the `_poolConfigBits` mapping (one slot per pool). * This includes the data in the `PoolConfig` struct, plus the data in the `HookFlags` struct. The layout (i.e., * offsets for each data field) is specified here. * * There are two libraries for interpreting these data. `HooksConfigLib` parses fields related to hooks, while * `PoolConfigLib` contains helpers related to the non-hook-related flags, along with aggregate fee percentages * and other data associated with pools. */ library PoolConfigConst { // Bit offsets for main pool config settings. uint8 public constant POOL_REGISTERED_OFFSET = 0; uint8 public constant POOL_INITIALIZED_OFFSET = POOL_REGISTERED_OFFSET + 1; uint8 public constant POOL_PAUSED_OFFSET = POOL_INITIALIZED_OFFSET + 1; uint8 public constant POOL_RECOVERY_MODE_OFFSET = POOL_PAUSED_OFFSET + 1; // Bit offsets for liquidity operations. uint8 public constant UNBALANCED_LIQUIDITY_OFFSET = POOL_RECOVERY_MODE_OFFSET + 1; uint8 public constant ADD_LIQUIDITY_CUSTOM_OFFSET = UNBALANCED_LIQUIDITY_OFFSET + 1; uint8 public constant REMOVE_LIQUIDITY_CUSTOM_OFFSET = ADD_LIQUIDITY_CUSTOM_OFFSET + 1; uint8 public constant DONATION_OFFSET = REMOVE_LIQUIDITY_CUSTOM_OFFSET + 1; // Bit offsets for hooks config. uint8 public constant BEFORE_INITIALIZE_OFFSET = DONATION_OFFSET + 1; uint8 public constant ENABLE_HOOK_ADJUSTED_AMOUNTS_OFFSET = BEFORE_INITIALIZE_OFFSET + 1; uint8 public constant AFTER_INITIALIZE_OFFSET = ENABLE_HOOK_ADJUSTED_AMOUNTS_OFFSET + 1; uint8 public constant DYNAMIC_SWAP_FEE_OFFSET = AFTER_INITIALIZE_OFFSET + 1; uint8 public constant BEFORE_SWAP_OFFSET = DYNAMIC_SWAP_FEE_OFFSET + 1; uint8 public constant AFTER_SWAP_OFFSET = BEFORE_SWAP_OFFSET + 1; uint8 public constant BEFORE_ADD_LIQUIDITY_OFFSET = AFTER_SWAP_OFFSET + 1; uint8 public constant AFTER_ADD_LIQUIDITY_OFFSET = BEFORE_ADD_LIQUIDITY_OFFSET + 1; uint8 public constant BEFORE_REMOVE_LIQUIDITY_OFFSET = AFTER_ADD_LIQUIDITY_OFFSET + 1; uint8 public constant AFTER_REMOVE_LIQUIDITY_OFFSET = BEFORE_REMOVE_LIQUIDITY_OFFSET + 1; // Bit offsets for uint values. uint8 public constant STATIC_SWAP_FEE_OFFSET = AFTER_REMOVE_LIQUIDITY_OFFSET + 1; uint256 public constant AGGREGATE_SWAP_FEE_OFFSET = STATIC_SWAP_FEE_OFFSET + FEE_BITLENGTH; uint256 public constant AGGREGATE_YIELD_FEE_OFFSET = AGGREGATE_SWAP_FEE_OFFSET + FEE_BITLENGTH; uint256 public constant DECIMAL_SCALING_FACTORS_OFFSET = AGGREGATE_YIELD_FEE_OFFSET + FEE_BITLENGTH; uint256 public constant PAUSE_WINDOW_END_TIME_OFFSET = DECIMAL_SCALING_FACTORS_OFFSET + TOKEN_DECIMAL_DIFFS_BITLENGTH; // Uses a uint40 to pack the values: 8 tokens * 5 bits/token. // This maximum token count is also hard-coded in the Vault. uint8 public constant TOKEN_DECIMAL_DIFFS_BITLENGTH = 40; uint8 public constant DECIMAL_DIFF_BITLENGTH = 5; uint8 public constant TIMESTAMP_BITLENGTH = 32; }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IVaultErrors } from "@balancer-labs/v3-interfaces/contracts/vault/IVaultErrors.sol"; import "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol"; import { WordCodec } from "@balancer-labs/v3-solidity-utils/contracts/helpers/WordCodec.sol"; import { PoolConfigConst } from "./PoolConfigConst.sol"; /** * @notice Helper functions to read and write the packed hook configuration flags stored in `_poolConfigBits`. * @dev Note that the entire configuration of each pool is stored in the `_poolConfigBits` mapping (one slot * per pool). This includes the data in the `PoolConfig` struct, plus the data in the `HookFlags` struct. * The layout (i.e., offsets for each data field) is specified in `PoolConfigConst`. * * There are two libraries for interpreting these data. `HooksConfigLib` parses fields related to hooks, while * this one contains helpers related to the non-hook-related flags, along with aggregate fee percentages and * other data associated with pools. * * The `PoolData` struct contains the raw bitmap with the entire pool state (`PoolConfigBits`), plus the token * configuration, scaling factors, and dynamic information such as current balances and rates. */ library PoolConfigLib { using WordCodec for bytes32; using PoolConfigLib for PoolConfigBits; // Bit offsets for main pool config settings. function isPoolRegistered(PoolConfigBits config) internal pure returns (bool) { return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.POOL_REGISTERED_OFFSET); } function setPoolRegistered(PoolConfigBits config, bool value) internal pure returns (PoolConfigBits) { return PoolConfigBits.wrap( PoolConfigBits.unwrap(config).insertBool(value, PoolConfigConst.POOL_REGISTERED_OFFSET) ); } function isPoolInitialized(PoolConfigBits config) internal pure returns (bool) { return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.POOL_INITIALIZED_OFFSET); } function setPoolInitialized(PoolConfigBits config, bool value) internal pure returns (PoolConfigBits) { return PoolConfigBits.wrap( PoolConfigBits.unwrap(config).insertBool(value, PoolConfigConst.POOL_INITIALIZED_OFFSET) ); } function isPoolPaused(PoolConfigBits config) internal pure returns (bool) { return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.POOL_PAUSED_OFFSET); } function setPoolPaused(PoolConfigBits config, bool value) internal pure returns (PoolConfigBits) { return PoolConfigBits.wrap(PoolConfigBits.unwrap(config).insertBool(value, PoolConfigConst.POOL_PAUSED_OFFSET)); } function isPoolInRecoveryMode(PoolConfigBits config) internal pure returns (bool) { return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.POOL_RECOVERY_MODE_OFFSET); } function setPoolInRecoveryMode(PoolConfigBits config, bool value) internal pure returns (PoolConfigBits) { return PoolConfigBits.wrap( PoolConfigBits.unwrap(config).insertBool(value, PoolConfigConst.POOL_RECOVERY_MODE_OFFSET) ); } // Bit offsets for liquidity operations. function supportsUnbalancedLiquidity(PoolConfigBits config) internal pure returns (bool) { // NOTE: The unbalanced liquidity flag is default-on (false means it is supported). // This function returns the inverted value. return !PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.UNBALANCED_LIQUIDITY_OFFSET); } function requireUnbalancedLiquidityEnabled(PoolConfigBits config) internal pure { if (config.supportsUnbalancedLiquidity() == false) { revert IVaultErrors.DoesNotSupportUnbalancedLiquidity(); } } function setDisableUnbalancedLiquidity( PoolConfigBits config, bool disableUnbalancedLiquidity ) internal pure returns (PoolConfigBits) { return PoolConfigBits.wrap( PoolConfigBits.unwrap(config).insertBool( disableUnbalancedLiquidity, PoolConfigConst.UNBALANCED_LIQUIDITY_OFFSET ) ); } function supportsAddLiquidityCustom(PoolConfigBits config) internal pure returns (bool) { return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.ADD_LIQUIDITY_CUSTOM_OFFSET); } function requireAddLiquidityCustomEnabled(PoolConfigBits config) internal pure { if (config.supportsAddLiquidityCustom() == false) { revert IVaultErrors.DoesNotSupportAddLiquidityCustom(); } } function setAddLiquidityCustom( PoolConfigBits config, bool enableAddLiquidityCustom ) internal pure returns (PoolConfigBits) { return PoolConfigBits.wrap( PoolConfigBits.unwrap(config).insertBool( enableAddLiquidityCustom, PoolConfigConst.ADD_LIQUIDITY_CUSTOM_OFFSET ) ); } function supportsRemoveLiquidityCustom(PoolConfigBits config) internal pure returns (bool) { return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.REMOVE_LIQUIDITY_CUSTOM_OFFSET); } function requireRemoveLiquidityCustomEnabled(PoolConfigBits config) internal pure { if (config.supportsRemoveLiquidityCustom() == false) { revert IVaultErrors.DoesNotSupportRemoveLiquidityCustom(); } } function setRemoveLiquidityCustom( PoolConfigBits config, bool enableRemoveLiquidityCustom ) internal pure returns (PoolConfigBits) { return PoolConfigBits.wrap( PoolConfigBits.unwrap(config).insertBool( enableRemoveLiquidityCustom, PoolConfigConst.REMOVE_LIQUIDITY_CUSTOM_OFFSET ) ); } function supportsDonation(PoolConfigBits config) internal pure returns (bool) { return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.DONATION_OFFSET); } function setDonation(PoolConfigBits config, bool enableDonation) internal pure returns (PoolConfigBits) { return PoolConfigBits.wrap( PoolConfigBits.unwrap(config).insertBool(enableDonation, PoolConfigConst.DONATION_OFFSET) ); } function requireDonationEnabled(PoolConfigBits config) internal pure { if (config.supportsDonation() == false) { revert IVaultErrors.DoesNotSupportDonation(); } } // Bit offsets for uint values. function getStaticSwapFeePercentage(PoolConfigBits config) internal pure returns (uint256) { return PoolConfigBits.unwrap(config).decodeUint(PoolConfigConst.STATIC_SWAP_FEE_OFFSET, FEE_BITLENGTH) * FEE_SCALING_FACTOR; } function setStaticSwapFeePercentage(PoolConfigBits config, uint256 value) internal pure returns (PoolConfigBits) { // A 100% fee is not supported. In the ExactOut case, the Vault divides by the complement of the swap fee. // The max fee percentage is slightly below 100%. if (value > MAX_FEE_PERCENTAGE) { revert IVaultErrors.PercentageAboveMax(); } value /= FEE_SCALING_FACTOR; return PoolConfigBits.wrap( PoolConfigBits.unwrap(config).insertUint(value, PoolConfigConst.STATIC_SWAP_FEE_OFFSET, FEE_BITLENGTH) ); } function getAggregateSwapFeePercentage(PoolConfigBits config) internal pure returns (uint256) { return PoolConfigBits.unwrap(config).decodeUint(PoolConfigConst.AGGREGATE_SWAP_FEE_OFFSET, FEE_BITLENGTH) * FEE_SCALING_FACTOR; } function setAggregateSwapFeePercentage( PoolConfigBits config, uint256 value ) internal pure returns (PoolConfigBits) { if (value > MAX_FEE_PERCENTAGE) { revert IVaultErrors.PercentageAboveMax(); } value /= FEE_SCALING_FACTOR; return PoolConfigBits.wrap( PoolConfigBits.unwrap(config).insertUint( value, PoolConfigConst.AGGREGATE_SWAP_FEE_OFFSET, FEE_BITLENGTH ) ); } function getAggregateYieldFeePercentage(PoolConfigBits config) internal pure returns (uint256) { return PoolConfigBits.unwrap(config).decodeUint(PoolConfigConst.AGGREGATE_YIELD_FEE_OFFSET, FEE_BITLENGTH) * FEE_SCALING_FACTOR; } function setAggregateYieldFeePercentage( PoolConfigBits config, uint256 value ) internal pure returns (PoolConfigBits) { if (value > MAX_FEE_PERCENTAGE) { revert IVaultErrors.PercentageAboveMax(); } value /= FEE_SCALING_FACTOR; return PoolConfigBits.wrap( PoolConfigBits.unwrap(config).insertUint( value, PoolConfigConst.AGGREGATE_YIELD_FEE_OFFSET, FEE_BITLENGTH ) ); } function getTokenDecimalDiffs(PoolConfigBits config) internal pure returns (uint40) { return uint40( PoolConfigBits.unwrap(config).decodeUint( PoolConfigConst.DECIMAL_SCALING_FACTORS_OFFSET, PoolConfigConst.TOKEN_DECIMAL_DIFFS_BITLENGTH ) ); } function getDecimalScalingFactors( PoolConfigBits config, uint256 numTokens ) internal pure returns (uint256[] memory) { uint256[] memory scalingFactors = new uint256[](numTokens); bytes32 tokenDecimalDiffs = bytes32(uint256(config.getTokenDecimalDiffs())); for (uint256 i = 0; i < numTokens; ++i) { uint256 decimalDiff = tokenDecimalDiffs.decodeUint( i * PoolConfigConst.DECIMAL_DIFF_BITLENGTH, PoolConfigConst.DECIMAL_DIFF_BITLENGTH ); // This is a "raw" factor, not a fixed point number. It should be applied using raw math to raw amounts // instead of using FP multiplication. scalingFactors[i] = 10 ** decimalDiff; } return scalingFactors; } function setTokenDecimalDiffs(PoolConfigBits config, uint40 value) internal pure returns (PoolConfigBits) { return PoolConfigBits.wrap( PoolConfigBits.unwrap(config).insertUint( value, PoolConfigConst.DECIMAL_SCALING_FACTORS_OFFSET, PoolConfigConst.TOKEN_DECIMAL_DIFFS_BITLENGTH ) ); } function getPauseWindowEndTime(PoolConfigBits config) internal pure returns (uint32) { return uint32( PoolConfigBits.unwrap(config).decodeUint( PoolConfigConst.PAUSE_WINDOW_END_TIME_OFFSET, PoolConfigConst.TIMESTAMP_BITLENGTH ) ); } function setPauseWindowEndTime(PoolConfigBits config, uint32 value) internal pure returns (PoolConfigBits) { return PoolConfigBits.wrap( PoolConfigBits.unwrap(config).insertUint( value, PoolConfigConst.PAUSE_WINDOW_END_TIME_OFFSET, PoolConfigConst.TIMESTAMP_BITLENGTH ) ); } // Convert from an array of decimal differences, to the encoded 40-bit value (8 tokens * 5 bits/token). function toTokenDecimalDiffs(uint8[] memory tokenDecimalDiffs) internal pure returns (uint40) { bytes32 value; for (uint256 i = 0; i < tokenDecimalDiffs.length; ++i) { value = value.insertUint( tokenDecimalDiffs[i], i * PoolConfigConst.DECIMAL_DIFF_BITLENGTH, PoolConfigConst.DECIMAL_DIFF_BITLENGTH ); } return uint40(uint256(value)); } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { PoolData, TokenInfo, TokenType, Rounding } from "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol"; import { IVaultErrors } from "@balancer-labs/v3-interfaces/contracts/vault/IVaultErrors.sol"; import { FixedPoint } from "@balancer-labs/v3-solidity-utils/contracts/math/FixedPoint.sol"; import { ScalingHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/ScalingHelpers.sol"; import { PackedTokenBalance } from "@balancer-labs/v3-solidity-utils/contracts/helpers/PackedTokenBalance.sol"; import { PoolConfigBits, PoolConfigLib } from "./PoolConfigLib.sol"; /** * @notice Helper functions to read/write a `PoolData` struct. * @dev Note that the entire configuration of each pool is stored in the `_poolConfigBits` mapping (one slot per pool). * This includes the data in the `PoolConfig` struct, plus the data in the `HookFlags` struct. The layout (i.e., * offsets for each data field) is specified in `PoolConfigConst`. * * The `PoolData` struct contains the raw bitmap with the entire pool state (`PoolConfigBits`), plus the token * configuration, scaling factors, and dynamic information such as current balances and rates. */ library PoolDataLib { using PackedTokenBalance for bytes32; using FixedPoint for *; using ScalingHelpers for *; using PoolConfigLib for PoolConfigBits; function load( PoolData memory poolData, mapping(uint256 tokenIndex => bytes32 packedTokenBalance) storage poolTokenBalances, PoolConfigBits poolConfigBits, mapping(IERC20 poolToken => TokenInfo tokenInfo) storage poolTokenInfo, IERC20[] storage tokens, Rounding roundingDirection ) internal view { uint256 numTokens = tokens.length; poolData.poolConfigBits = poolConfigBits; poolData.tokens = tokens; poolData.tokenInfo = new TokenInfo[](numTokens); poolData.balancesRaw = new uint256[](numTokens); poolData.balancesLiveScaled18 = new uint256[](numTokens); poolData.decimalScalingFactors = PoolConfigLib.getDecimalScalingFactors(poolData.poolConfigBits, numTokens); poolData.tokenRates = new uint256[](numTokens); bool poolSubjectToYieldFees = poolData.poolConfigBits.isPoolInitialized() && poolData.poolConfigBits.getAggregateYieldFeePercentage() > 0 && poolData.poolConfigBits.isPoolInRecoveryMode() == false; for (uint256 i = 0; i < numTokens; ++i) { TokenInfo memory tokenInfo = poolTokenInfo[poolData.tokens[i]]; bytes32 packedBalance = poolTokenBalances[i]; poolData.tokenInfo[i] = tokenInfo; poolData.tokenRates[i] = getTokenRate(tokenInfo); updateRawAndLiveBalance(poolData, i, packedBalance.getBalanceRaw(), roundingDirection); // If there are no yield fees, we can save gas by skipping to the next token now. if (poolSubjectToYieldFees == false) { continue; } // `poolData` already has live balances computed from raw balances according to the token rates and the // given rounding direction. Charging a yield fee changes the raw balance, in which case the safest and // most numerically precise way to adjust the live balance is to simply repeat the scaling (hence the // second call below). // The Vault actually guarantees that a token with paysYieldFees set is a WITH_RATE token, so technically // we could just check the flag, but we don't want to introduce that dependency for a slight gas savings. bool tokenSubjectToYieldFees = tokenInfo.paysYieldFees && tokenInfo.tokenType == TokenType.WITH_RATE; // Do not charge yield fees before the pool is initialized, or in recovery mode. if (tokenSubjectToYieldFees) { uint256 aggregateYieldFeePercentage = poolData.poolConfigBits.getAggregateYieldFeePercentage(); uint256 balanceRaw = poolData.balancesRaw[i]; uint256 aggregateYieldFeeAmountRaw = _computeYieldFeesDue( poolData, packedBalance.getBalanceDerived(), i, aggregateYieldFeePercentage ); if (aggregateYieldFeeAmountRaw > 0) { updateRawAndLiveBalance(poolData, i, balanceRaw - aggregateYieldFeeAmountRaw, roundingDirection); } } } } function syncPoolBalancesAndFees( PoolData memory poolData, mapping(uint256 tokenIndex => bytes32 packedTokenBalance) storage poolTokenBalances, mapping(IERC20 token => bytes32 packedFeeAmounts) storage poolAggregateProtocolFeeAmounts ) internal { uint256 numTokens = poolData.balancesRaw.length; for (uint256 i = 0; i < numTokens; ++i) { IERC20 token = poolData.tokens[i]; bytes32 packedBalances = poolTokenBalances[i]; uint256 storedBalanceRaw = packedBalances.getBalanceRaw(); // `poolData` now has balances updated with yield fees. // If yield fees are not 0, then the stored balance is greater than the one in memory. if (storedBalanceRaw > poolData.balancesRaw[i]) { // Both Swap and Yield fees are stored together in a `PackedTokenBalance`. // We have designated "Derived" the derived half for Yield fee storage. bytes32 packedProtocolFeeAmounts = poolAggregateProtocolFeeAmounts[token]; poolAggregateProtocolFeeAmounts[token] = packedProtocolFeeAmounts.setBalanceDerived( packedProtocolFeeAmounts.getBalanceDerived() + (storedBalanceRaw - poolData.balancesRaw[i]) ); } poolTokenBalances[i] = PackedTokenBalance.toPackedBalance( poolData.balancesRaw[i], poolData.balancesLiveScaled18[i] ); } } /** * @dev This is typically called after a reentrant callback (e.g., a "before" liquidity operation callback), * to refresh the poolData struct with any balances (or rates) that might have changed. * * Preconditions: tokenConfig, balancesRaw, and decimalScalingFactors must be current in `poolData`. * Side effects: mutates tokenRates, balancesLiveScaled18 in `poolData`. */ function reloadBalancesAndRates( PoolData memory poolData, mapping(uint256 tokenIndex => bytes32 packedTokenBalance) storage poolTokenBalances, Rounding roundingDirection ) internal view { uint256 numTokens = poolData.tokens.length; // It's possible a reentrant hook changed the raw balances in Vault storage. // Update them before computing the live balances. bytes32 packedBalance; for (uint256 i = 0; i < numTokens; ++i) { poolData.tokenRates[i] = getTokenRate(poolData.tokenInfo[i]); packedBalance = poolTokenBalances[i]; // Note the order dependency. This requires up-to-date tokenRate for the token at index `i` in `poolData`. updateRawAndLiveBalance(poolData, i, packedBalance.getBalanceRaw(), roundingDirection); } } function getTokenRate(TokenInfo memory tokenInfo) internal view returns (uint256 rate) { TokenType tokenType = tokenInfo.tokenType; if (tokenType == TokenType.STANDARD) { rate = FixedPoint.ONE; } else if (tokenType == TokenType.WITH_RATE) { rate = tokenInfo.rateProvider.getRate(); } else { revert IVaultErrors.InvalidTokenConfiguration(); } } function updateRawAndLiveBalance( PoolData memory poolData, uint256 tokenIndex, uint256 newRawBalance, Rounding roundingDirection ) internal pure { poolData.balancesRaw[tokenIndex] = newRawBalance; function(uint256, uint256, uint256) internal pure returns (uint256) _upOrDown = roundingDirection == Rounding.ROUND_UP ? ScalingHelpers.toScaled18ApplyRateRoundUp : ScalingHelpers.toScaled18ApplyRateRoundDown; poolData.balancesLiveScaled18[tokenIndex] = _upOrDown( newRawBalance, poolData.decimalScalingFactors[tokenIndex], poolData.tokenRates[tokenIndex] ); } // solhint-disable-next-line private-vars-leading-underscore function _computeYieldFeesDue( PoolData memory poolData, uint256 lastLiveBalance, uint256 tokenIndex, uint256 aggregateYieldFeePercentage ) internal pure returns (uint256 aggregateYieldFeeAmountRaw) { uint256 currentLiveBalance = poolData.balancesLiveScaled18[tokenIndex]; // Do not charge fees if rates go down. If the rate were to go up, down, and back up again, protocol fees // would be charged multiple times on the "same" yield. For tokens subject to yield fees, this should not // happen, or at least be very rare. It can be addressed for known volatile rates by setting the yield fee // exempt flag on registration, or compensated off-chain if there is an incident with a normally // well-behaved rate provider. if (currentLiveBalance > lastLiveBalance) { unchecked { // Magnitudes are checked above, so it's safe to do unchecked math here. uint256 aggregateYieldFeeAmountScaled18 = (currentLiveBalance - lastLiveBalance).mulUp( aggregateYieldFeePercentage ); // A pool is subject to yield fees if poolSubjectToYieldFees is true, meaning that // `protocolYieldFeePercentage > 0`. So, we don't need to check this again in here, saving some gas. aggregateYieldFeeAmountRaw = aggregateYieldFeeAmountScaled18.toRawUndoRateRoundDown( poolData.decimalScalingFactors[tokenIndex], poolData.tokenRates[tokenIndex] ); } } } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol"; import { IVaultErrors } from "@balancer-labs/v3-interfaces/contracts/vault/IVaultErrors.sol"; /** * @notice Ensure functions in extension contracts can only be called through the main Vault. * @dev The Vault is composed of three contracts, using the Proxy pattern from OpenZeppelin. `ensureVaultDelegateCall` * can be called on the locally stored Vault address by modifiers in extension contracts to ensure that their functions * can only be called through the main Vault. Because the storage *layout* is shared (through inheritance of * `VaultStorage`), but each contract actually has its own storage, we need to make sure we are always calling in the * main Vault context, to avoid referencing storage in the extension contracts. */ library VaultExtensionsLib { function ensureVaultDelegateCall(IVault vault) internal view { // If this is a delegate call from the Vault, the address of the contract should be the Vault's, // not the extension. if (address(this) != address(vault)) { revert IVaultErrors.NotVaultDelegateCall(); } } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { WordCodec } from "@balancer-labs/v3-solidity-utils/contracts/helpers/WordCodec.sol"; // @notice Custom type to store the Vault configuration. type VaultStateBits is bytes32; /// @notice Helper functions for reading and writing the `VaultState` struct. library VaultStateLib { using WordCodec for bytes32; // Bit offsets for the Vault state flags. uint256 public constant QUERY_DISABLED_OFFSET = 0; uint256 public constant VAULT_PAUSED_OFFSET = QUERY_DISABLED_OFFSET + 1; uint256 public constant BUFFER_PAUSED_OFFSET = VAULT_PAUSED_OFFSET + 1; function isQueryDisabled(VaultStateBits config) internal pure returns (bool) { return VaultStateBits.unwrap(config).decodeBool(QUERY_DISABLED_OFFSET); } function setQueryDisabled(VaultStateBits config, bool value) internal pure returns (VaultStateBits) { return VaultStateBits.wrap(VaultStateBits.unwrap(config).insertBool(value, QUERY_DISABLED_OFFSET)); } function isVaultPaused(VaultStateBits config) internal pure returns (bool) { return VaultStateBits.unwrap(config).decodeBool(VAULT_PAUSED_OFFSET); } function setVaultPaused(VaultStateBits config, bool value) internal pure returns (VaultStateBits) { return VaultStateBits.wrap(VaultStateBits.unwrap(config).insertBool(value, VAULT_PAUSED_OFFSET)); } function areBuffersPaused(VaultStateBits config) internal pure returns (bool) { return VaultStateBits.unwrap(config).decodeBool(BUFFER_PAUSED_OFFSET); } function setBuffersPaused(VaultStateBits config, bool value) internal pure returns (VaultStateBits) { return VaultStateBits.wrap(VaultStateBits.unwrap(config).insertBool(value, BUFFER_PAUSED_OFFSET)); } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC20Errors } from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol"; import { IERC20MultiTokenErrors } from "@balancer-labs/v3-interfaces/contracts/vault/IERC20MultiTokenErrors.sol"; import { EVMCallModeHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/EVMCallModeHelpers.sol"; import { BalancerPoolToken } from "../BalancerPoolToken.sol"; /** * @notice Store Token data and handle accounting for pool tokens in the Vault. * @dev The ERC20MultiToken is an ERC20-focused multi-token implementation that is fully compatible with the ERC20 API * on the token side. It also allows for the minting and burning of tokens on the multi-token side. */ abstract contract ERC20MultiToken is IERC20Errors, IERC20MultiTokenErrors { // Minimum total supply amount. uint256 internal constant _POOL_MINIMUM_TOTAL_SUPPLY = 1e6; /** * @notice Pool tokens are moved from one account (`from`) to another (`to`). Note that `value` may be zero. * @param pool The pool token being transferred * @param from The token source * @param to The token destination * @param value The number of tokens */ event Transfer(address indexed pool, address indexed from, address indexed to, uint256 value); /** * @notice The allowance of a `spender` for an `owner` is set by a call to {approve}. `value` is the new allowance. * @param pool The pool token receiving the allowance * @param owner The token holder * @param spender The account being authorized to spend a given amount of the token * @param value The number of tokens spender is authorized to transfer from owner */ event Approval(address indexed pool, address indexed owner, address indexed spender, uint256 value); // Users' pool token (BPT) balances. mapping(address token => mapping(address owner => uint256 balance)) private _balances; // Users' pool token (BPT) allowances. mapping(address token => mapping(address owner => mapping(address spender => uint256 allowance))) private _allowances; // Total supply of all pool tokens (BPT). These are tokens minted and burned by the Vault. // The Vault balances of regular pool tokens are stored in `_reservesOf`. mapping(address token => uint256 totalSupply) private _totalSupplyOf; function _totalSupply(address pool) internal view returns (uint256) { return _totalSupplyOf[pool]; } function _balanceOf(address pool, address account) internal view returns (uint256) { return _balances[pool][account]; } function _allowance(address pool, address owner, address spender) internal view returns (uint256) { // Owner can spend anything without approval if (owner == spender) { return type(uint256).max; } else { return _allowances[pool][owner][spender]; } } /** * @dev DO NOT CALL THIS METHOD! * Only `removeLiquidity` in the Vault may call this - in a query context - to allow burning tokens the caller * does not have. */ function _queryModeBalanceIncrease(address pool, address to, uint256 amount) internal { // Enforce that this can only be called in a read-only, query context. if (EVMCallModeHelpers.isStaticCall() == false) { revert EVMCallModeHelpers.NotStaticCall(); } // Increase `to` balance to ensure the burn function succeeds during query. _balances[address(pool)][to] += amount; } function _mint(address pool, address to, uint256 amount) internal { if (to == address(0)) { revert ERC20InvalidReceiver(to); } uint256 newTotalSupply = _totalSupplyOf[pool] + amount; unchecked { // Overflow is not possible. balance + amount is at most totalSupply + amount, which is checked above. _balances[pool][to] += amount; } _ensurePoolMinimumTotalSupply(newTotalSupply); _totalSupplyOf[pool] = newTotalSupply; emit Transfer(pool, address(0), to, amount); // We also emit the "transfer" event on the pool token to ensure full compliance with the ERC20 standard. BalancerPoolToken(pool).emitTransfer(address(0), to, amount); } function _ensurePoolMinimumTotalSupply(uint256 newTotalSupply) internal pure { if (newTotalSupply < _POOL_MINIMUM_TOTAL_SUPPLY) { revert PoolTotalSupplyTooLow(newTotalSupply); } } function _mintMinimumSupplyReserve(address pool) internal { _totalSupplyOf[pool] += _POOL_MINIMUM_TOTAL_SUPPLY; unchecked { // Overflow is not possible. balance + amount is at most totalSupply + amount, which is checked above. _balances[pool][address(0)] += _POOL_MINIMUM_TOTAL_SUPPLY; } emit Transfer(pool, address(0), address(0), _POOL_MINIMUM_TOTAL_SUPPLY); // We also emit the "transfer" event on the pool token to ensure full compliance with the ERC20 standard. BalancerPoolToken(pool).emitTransfer(address(0), address(0), _POOL_MINIMUM_TOTAL_SUPPLY); } function _burn(address pool, address from, uint256 amount) internal { if (from == address(0)) { revert ERC20InvalidSender(from); } uint256 accountBalance = _balances[pool][from]; if (amount > accountBalance) { revert ERC20InsufficientBalance(from, accountBalance, amount); } unchecked { _balances[pool][from] = accountBalance - amount; } uint256 newTotalSupply = _totalSupplyOf[pool] - amount; _ensurePoolMinimumTotalSupply(newTotalSupply); _totalSupplyOf[pool] = newTotalSupply; // We also emit the "transfer" event on the pool token to ensure full compliance with the ERC20 standard. // If this function fails we keep going, as this is used in recovery mode. // Well-behaved pools will just emit an event here, so they should never fail. try BalancerPoolToken(pool).emitTransfer(from, address(0), amount) {} catch { // solhint-disable-previous-line no-empty-blocks } // Emit the internal event last to spend some gas after try / catch. emit Transfer(pool, from, address(0), amount); } function _transfer(address pool, address from, address to, uint256 amount) internal { if (from == address(0)) { revert ERC20InvalidSender(from); } if (to == address(0)) { revert ERC20InvalidReceiver(to); } uint256 fromBalance = _balances[pool][from]; if (amount > fromBalance) { revert ERC20InsufficientBalance(from, fromBalance, amount); } unchecked { _balances[pool][from] = fromBalance - amount; // Overflow is not possible. The sum of all balances is capped by totalSupply, and that sum is preserved by // decrementing then incrementing. _balances[pool][to] += amount; } emit Transfer(pool, from, to, amount); // We also emit the "transfer" event on the pool token to ensure full compliance with the ERC20 standard. BalancerPoolToken(pool).emitTransfer(from, to, amount); } function _approve(address pool, address owner, address spender, uint256 amount) internal { if (owner == address(0)) { revert ERC20InvalidApprover(owner); } if (spender == address(0)) { revert ERC20InvalidSpender(spender); } _allowances[pool][owner][spender] = amount; // We also emit the "approve" event on the pool token to ensure full compliance with the ERC20 standard. // If this function fails we keep going, as this is used in recovery mode. // Well-behaved pools will just emit an event here, so they should never fail. try BalancerPoolToken(pool).emitApproval(owner, spender, amount) {} catch { // solhint-disable-previous-line no-empty-blocks } // Emit the internal event last to spend some gas after try / catch. emit Approval(pool, owner, spender, amount); } function _spendAllowance(address pool, address owner, address spender, uint256 amount) internal { uint256 currentAllowance = _allowance(pool, owner, spender); if (currentAllowance != type(uint256).max) { if (amount > currentAllowance) { revert ERC20InsufficientAllowance(spender, currentAllowance, amount); } unchecked { _approve(pool, owner, spender, currentAllowance - amount); } } } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC4626 } from "@openzeppelin/contracts/interfaces/IERC4626.sol"; import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol"; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { ISwapFeePercentageBounds } from "@balancer-labs/v3-interfaces/contracts/vault/ISwapFeePercentageBounds.sol"; import { PoolData, Rounding } from "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol"; import { IVaultErrors } from "@balancer-labs/v3-interfaces/contracts/vault/IVaultErrors.sol"; import { IVaultEvents } from "@balancer-labs/v3-interfaces/contracts/vault/IVaultEvents.sol"; import { StorageSlotExtension } from "@balancer-labs/v3-solidity-utils/contracts/openzeppelin/StorageSlotExtension.sol"; import { EVMCallModeHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/EVMCallModeHelpers.sol"; import { PackedTokenBalance } from "@balancer-labs/v3-solidity-utils/contracts/helpers/PackedTokenBalance.sol"; import { ScalingHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/ScalingHelpers.sol"; import { ReentrancyGuardTransient } from "@balancer-labs/v3-solidity-utils/contracts/openzeppelin/ReentrancyGuardTransient.sol"; import { TransientStorageHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/TransientStorageHelpers.sol"; import { VaultStateBits, VaultStateLib } from "./lib/VaultStateLib.sol"; import { PoolConfigBits, PoolConfigLib } from "./lib/PoolConfigLib.sol"; import { ERC20MultiToken } from "./token/ERC20MultiToken.sol"; import { PoolDataLib } from "./lib/PoolDataLib.sol"; import { VaultStorage } from "./VaultStorage.sol"; /** * @notice Functions and modifiers shared between the main Vault and its extension contracts. * @dev This contract contains common utilities in the inheritance chain that require storage to work, * and will be required in both the main Vault and its extensions. */ abstract contract VaultCommon is IVaultEvents, IVaultErrors, VaultStorage, ReentrancyGuardTransient, ERC20MultiToken { using PoolConfigLib for PoolConfigBits; using VaultStateLib for VaultStateBits; using SafeCast for *; using TransientStorageHelpers for *; using StorageSlotExtension for *; using PoolDataLib for PoolData; /******************************************************************************* Transient Accounting *******************************************************************************/ /** * @dev This modifier ensures that the function it modifies can only be called * when a tab has been opened. */ modifier onlyWhenUnlocked() { _ensureUnlocked(); _; } function _ensureUnlocked() internal view { if (_isUnlocked().tload() == false) { revert VaultIsNotUnlocked(); } } /** * @notice Expose the state of the Vault's reentrancy guard. * @return True if the Vault is currently executing a nonReentrant function */ function reentrancyGuardEntered() public view returns (bool) { return _reentrancyGuardEntered(); } /** * @notice Records the `credit` for a given token. * @param token The ERC20 token for which the 'credit' will be accounted * @param credit The amount of `token` supplied to the Vault in favor of the caller */ function _supplyCredit(IERC20 token, uint256 credit) internal { _accountDelta(token, -credit.toInt256()); } /** * @notice Records the `debt` for a given token. * @param token The ERC20 token for which the `debt` will be accounted * @param debt The amount of `token` taken from the Vault in favor of the caller */ function _takeDebt(IERC20 token, uint256 debt) internal { _accountDelta(token, debt.toInt256()); } /** * @dev Accounts the delta for the given token. A positive delta represents debt, * while a negative delta represents surplus. * * @param token The ERC20 token for which the delta is being accounted * @param delta The difference in the token balance * Positive indicates a debit or a decrease in Vault's tokens, * negative indicates a credit or an increase in Vault's tokens. */ function _accountDelta(IERC20 token, int256 delta) internal { // If the delta is zero, there's nothing to account for. if (delta == 0) return; // Get the current recorded delta for this token. int256 current = _tokenDeltas().tGet(token); // Calculate the new delta after accounting for the change. int256 next = current + delta; if (next == 0) { // If the resultant delta becomes zero after this operation, // decrease the count of non-zero deltas. _nonZeroDeltaCount().tDecrement(); } else if (current == 0) { // If there was no previous delta (i.e., it was zero) and now we have one, // increase the count of non-zero deltas. _nonZeroDeltaCount().tIncrement(); } // Update the delta for this token. _tokenDeltas().tSet(token, next); } /******************************************************************************* Vault Pausing *******************************************************************************/ /// @dev Modifier to make a function callable only when the Vault is not paused. modifier whenVaultNotPaused() { _ensureVaultNotPaused(); _; } /// @dev Reverts if the Vault is paused. function _ensureVaultNotPaused() internal view { if (_isVaultPaused()) { revert VaultPaused(); } } /// @dev Reverts if the Vault or the given pool are paused. function _ensureUnpaused(address pool) internal view { _ensureVaultNotPaused(); _ensurePoolNotPaused(pool); } /** * @dev For gas efficiency, storage is only read before `_vaultBufferPeriodEndTime`. Once we're past that * timestamp, the expression short-circuits false, and the Vault is permanently unpaused. */ function _isVaultPaused() internal view returns (bool) { // solhint-disable-next-line not-rely-on-time return block.timestamp <= _vaultBufferPeriodEndTime && _vaultStateBits.isVaultPaused(); } /******************************************************************************* Pool Pausing *******************************************************************************/ /// @dev Reverts if the pool is paused. function _ensurePoolNotPaused(address pool) internal view { if (_isPoolPaused(pool)) { revert PoolPaused(pool); } } /// @dev Check both the flag and timestamp to determine whether the pool is paused. function _isPoolPaused(address pool) internal view returns (bool) { (bool paused, ) = _getPoolPausedState(pool); return paused; } /// @dev Lowest level routine that plucks only the minimum necessary parts from storage. function _getPoolPausedState(address pool) internal view returns (bool, uint32) { PoolConfigBits config = _poolConfigBits[pool]; bool isPoolPaused = config.isPoolPaused(); uint32 pauseWindowEndTime = config.getPauseWindowEndTime(); // Use the Vault's buffer period. // solhint-disable-next-line not-rely-on-time return (isPoolPaused && block.timestamp <= pauseWindowEndTime + _vaultBufferPeriodDuration, pauseWindowEndTime); } /******************************************************************************* Buffer Pausing *******************************************************************************/ /// @dev Modifier to make a function callable only when vault buffers are not paused. modifier whenVaultBuffersAreNotPaused() { _ensureVaultBuffersAreNotPaused(); _; } /// @dev Reverts if vault buffers are paused. function _ensureVaultBuffersAreNotPaused() internal view { if (_vaultStateBits.areBuffersPaused()) { revert VaultBuffersArePaused(); } } /******************************************************************************* Pool Registration and Initialization *******************************************************************************/ /// @dev Reverts unless `pool` is a registered Pool. modifier withRegisteredPool(address pool) { _ensureRegisteredPool(pool); _; } /// @dev Reverts unless `pool` is an initialized Pool. modifier withInitializedPool(address pool) { _ensureInitializedPool(pool); _; } function _ensureRegisteredPool(address pool) internal view { if (!_isPoolRegistered(pool)) { revert PoolNotRegistered(pool); } } /// @dev See `isPoolRegistered` function _isPoolRegistered(address pool) internal view returns (bool) { PoolConfigBits config = _poolConfigBits[pool]; return config.isPoolRegistered(); } function _ensureInitializedPool(address pool) internal view { if (!_isPoolInitialized(pool)) { revert PoolNotInitialized(pool); } } /// @dev See `isPoolInitialized` function _isPoolInitialized(address pool) internal view returns (bool) { PoolConfigBits config = _poolConfigBits[pool]; return config.isPoolInitialized(); } /******************************************************************************* Buffer Initialization & Validation *******************************************************************************/ modifier withInitializedBuffer(IERC4626 wrappedToken) { _ensureBufferInitialized(wrappedToken); _; } function _ensureBufferInitialized(IERC4626 wrappedToken) internal view { if (_bufferAssets[wrappedToken] == address(0)) { revert BufferNotInitialized(wrappedToken); } } /** * @dev This assumes `underlyingToken` is non-zero; should be called by functions that have already ensured the * buffer has been initialized (e.g., those protected by `withInitializedBuffer`). */ function _ensureCorrectBufferAsset(IERC4626 wrappedToken, address underlyingToken) internal view { if (_bufferAssets[wrappedToken] != underlyingToken) { // Asset was changed since the buffer was initialized. revert WrongUnderlyingToken(wrappedToken, underlyingToken); } } /******************************************************************************* Pool Information *******************************************************************************/ /** * @dev Packs and sets the raw and live balances of a Pool's tokens to the current values in poolData.balancesRaw * and poolData.liveBalances in the same storage slot. */ function _writePoolBalancesToStorage(address pool, PoolData memory poolData) internal { mapping(uint256 tokenIndex => bytes32 packedTokenBalance) storage poolBalances = _poolTokenBalances[pool]; for (uint256 i = 0; i < poolData.balancesRaw.length; ++i) { // We assume all newBalances are properly ordered. poolBalances[i] = PackedTokenBalance.toPackedBalance( poolData.balancesRaw[i], poolData.balancesLiveScaled18[i] ); } } /** * @dev Fill in PoolData, including paying protocol yield fees and computing final raw and live balances. * In normal operation, we update both balances and fees together. However, while Recovery Mode is enabled, * we cannot track yield fees, as that would involve making external calls that could fail and block withdrawals. * * Therefore, disabling Recovery Mode requires writing *only* the balances to storage, so we still need this * as a separate function. It is normally called by `_loadPoolDataUpdatingBalancesAndYieldFees`, but in the * Recovery Mode special case, it is called separately, with the result passed into `_writePoolBalancesToStorage`. */ function _loadPoolData(address pool, Rounding roundingDirection) internal view returns (PoolData memory poolData) { poolData.load( _poolTokenBalances[pool], _poolConfigBits[pool], _poolTokenInfo[pool], _poolTokens[pool], roundingDirection ); } /** * @dev Fill in PoolData, including paying protocol yield fees and computing final raw and live balances. * This function modifies protocol fees and balance storage. Out of an abundance of caution, since `_loadPoolData` * makes external calls, we are making anything that calls it and then modifies storage non-reentrant. * Side effects: updates `_aggregateFeeAmounts` and `_poolTokenBalances` in storage. */ function _loadPoolDataUpdatingBalancesAndYieldFees( address pool, Rounding roundingDirection ) internal nonReentrant returns (PoolData memory poolData) { // Initialize poolData with base information for subsequent calculations. poolData.load( _poolTokenBalances[pool], _poolConfigBits[pool], _poolTokenInfo[pool], _poolTokens[pool], roundingDirection ); PoolDataLib.syncPoolBalancesAndFees(poolData, _poolTokenBalances[pool], _aggregateFeeAmounts[pool]); } /** * @dev Updates the raw and live balance of a given token in poolData, scaling the given raw balance by both decimal * and token rates, and rounding the result in the given direction. Assumes scaling factors and rates are current * in PoolData. */ function _updateRawAndLiveTokenBalancesInPoolData( PoolData memory poolData, uint256 newRawBalance, Rounding roundingDirection, uint256 tokenIndex ) internal pure returns (uint256) { poolData.balancesRaw[tokenIndex] = newRawBalance; function(uint256, uint256, uint256) internal pure returns (uint256) _upOrDown = roundingDirection == Rounding.ROUND_UP ? ScalingHelpers.toScaled18ApplyRateRoundUp : ScalingHelpers.toScaled18ApplyRateRoundDown; poolData.balancesLiveScaled18[tokenIndex] = _upOrDown( newRawBalance, poolData.decimalScalingFactors[tokenIndex], poolData.tokenRates[tokenIndex] ); return _upOrDown(newRawBalance, poolData.decimalScalingFactors[tokenIndex], poolData.tokenRates[tokenIndex]); } function _setStaticSwapFeePercentage(address pool, uint256 swapFeePercentage) internal { // These cannot be called during pool construction. Pools must be deployed first, then registered. if (swapFeePercentage < ISwapFeePercentageBounds(pool).getMinimumSwapFeePercentage()) { revert SwapFeePercentageTooLow(); } if (swapFeePercentage > ISwapFeePercentageBounds(pool).getMaximumSwapFeePercentage()) { revert SwapFeePercentageTooHigh(); } // The library also checks that the percentage is <= FP(1), regardless of what the pool defines. _poolConfigBits[pool] = _poolConfigBits[pool].setStaticSwapFeePercentage(swapFeePercentage); emit SwapFeePercentageChanged(pool, swapFeePercentage); } /// @dev Find the index of a token in a token array. Reverts if not found. function _findTokenIndex(IERC20[] memory tokens, IERC20 token) internal pure returns (uint256) { for (uint256 i = 0; i < tokens.length; i++) { if (tokens[i] == token) { return i; } } revert TokenNotRegistered(token); } /******************************************************************************* Recovery Mode *******************************************************************************/ /// @dev Place on functions that may only be called when the associated pool is in recovery mode. modifier onlyInRecoveryMode(address pool) { _ensurePoolInRecoveryMode(pool); _; } /// @dev Reverts if the pool is not in recovery mode. function _ensurePoolInRecoveryMode(address pool) internal view { if (!_isPoolInRecoveryMode(pool)) { revert PoolNotInRecoveryMode(pool); } } /** * @notice Checks whether a pool is in recovery mode. * @param pool Address of the pool to check * @return inRecoveryMode True if the pool is in recovery mode, false otherwise */ function _isPoolInRecoveryMode(address pool) internal view returns (bool) { return _poolConfigBits[pool].isPoolInRecoveryMode(); } function _isQueryContext() internal view returns (bool) { return EVMCallModeHelpers.isStaticCall() && _vaultStateBits.isQueryDisabled() == false; } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IVaultErrors } from "@balancer-labs/v3-interfaces/contracts/vault/IVaultErrors.sol"; import { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol"; /// @notice Contract that shares the modifier `onlyVault`. contract VaultGuard { IVault internal immutable _vault; constructor(IVault vault) { _vault = vault; } modifier onlyVault() { _ensureOnlyVault(); _; } function _ensureOnlyVault() private view { if (msg.sender != address(_vault)) { revert IVaultErrors.SenderIsNotVault(msg.sender); } } }
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.24; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { IProtocolFeeController } from "@balancer-labs/v3-interfaces/contracts/vault/IProtocolFeeController.sol"; import { IVaultExtension } from "@balancer-labs/v3-interfaces/contracts/vault/IVaultExtension.sol"; import { IAuthorizer } from "@balancer-labs/v3-interfaces/contracts/vault/IAuthorizer.sol"; import { IHooks } from "@balancer-labs/v3-interfaces/contracts/vault/IHooks.sol"; import "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol"; import { StorageSlotExtension } from "@balancer-labs/v3-solidity-utils/contracts/openzeppelin/StorageSlotExtension.sol"; import { TransientStorageHelpers, TokenDeltaMappingSlotType, UintToAddressToBooleanMappingSlot } from "@balancer-labs/v3-solidity-utils/contracts/helpers/TransientStorageHelpers.sol"; import { VaultStateBits } from "./lib/VaultStateLib.sol"; import { PoolConfigBits } from "./lib/PoolConfigLib.sol"; // solhint-disable max-states-count /** * @notice Storage layout for the Vault. * @dev This contract has no code, but is inherited by all three Vault contracts. In order to ensure that *only* the * Vault contract's storage is actually used, calls to the extension contracts must be delegate calls made through the * main Vault. */ contract VaultStorage { using StorageSlotExtension for *; /*************************************************************************** Constants ***************************************************************************/ // Pools can have between two and eight tokens. uint256 internal constant _MIN_TOKENS = 2; // This maximum token count is also implicitly hard-coded in `PoolConfigLib` (through packing `tokenDecimalDiffs`). uint256 internal constant _MAX_TOKENS = 8; // Tokens with more than 18 decimals are not supported. Tokens must also implement `IERC20Metadata.decimals`. uint8 internal constant _MAX_TOKEN_DECIMALS = 18; // Maximum pause and buffer period durations. uint256 internal constant _MAX_PAUSE_WINDOW_DURATION = 365 days * 4; uint256 internal constant _MAX_BUFFER_PERIOD_DURATION = 180 days; // Minimum swap amount (applied to scaled18 values), enforced as a security measure to block potential // exploitation of rounding errors. // solhint-disable-next-line var-name-mixedcase uint256 internal immutable _MINIMUM_TRADE_AMOUNT; // Minimum given amount to wrap/unwrap (applied to native decimal values), to avoid rounding issues. // solhint-disable-next-line var-name-mixedcase uint256 internal immutable _MINIMUM_WRAP_AMOUNT; /*************************************************************************** Transient Storage Declarations ***************************************************************************/ // NOTE: If you use a constant, then it is simply replaced everywhere when this constant is used // by what is written after =. If you use immutable, the value is first calculated and // then replaced everywhere. That means that if a constant has executable variables, // they will be executed every time the constant is used. // solhint-disable var-name-mixedcase bytes32 private immutable _IS_UNLOCKED_SLOT = _calculateVaultStorageSlot("isUnlocked"); bytes32 private immutable _NON_ZERO_DELTA_COUNT_SLOT = _calculateVaultStorageSlot("nonZeroDeltaCount"); bytes32 private immutable _TOKEN_DELTAS_SLOT = _calculateVaultStorageSlot("tokenDeltas"); bytes32 private immutable _ADD_LIQUIDITY_CALLED_SLOT = _calculateVaultStorageSlot("addLiquidityCalled"); bytes32 private immutable _SESSION_ID_SLOT = _calculateVaultStorageSlot("sessionId"); // solhint-enable var-name-mixedcase /*************************************************************************** Pool State ***************************************************************************/ // Pool-specific configuration data (e.g., fees, pause window, configuration flags). mapping(address pool => PoolConfigBits poolConfig) internal _poolConfigBits; // Accounts assigned to specific roles; e.g., pauseManager, swapManager. mapping(address pool => PoolRoleAccounts roleAccounts) internal _poolRoleAccounts; // The hooks contracts associated with each pool. mapping(address pool => IHooks hooksContract) internal _hooksContracts; // The set of tokens associated with each pool. mapping(address pool => IERC20[] poolTokens) internal _poolTokens; // The token configuration of each Pool's tokens. mapping(address pool => mapping(IERC20 token => TokenInfo tokenInfo)) internal _poolTokenInfo; // Structure containing the current raw and "last live" scaled balances. Last live balances are used for // yield fee computation, and since these have rates applied, they are stored as scaled 18-decimal FP values. // Each value takes up half the storage slot (i.e., 128 bits). mapping(address pool => mapping(uint256 tokenIndex => bytes32 packedTokenBalance)) internal _poolTokenBalances; // Aggregate protocol swap/yield fees accumulated in the Vault for harvest. // Reusing PackedTokenBalance for the bytes32 values to save bytecode (despite differing semantics). // It's arbitrary which is which: we define raw = swap; derived = yield. mapping(address pool => mapping(IERC20 token => bytes32 packedFeeAmounts)) internal _aggregateFeeAmounts; /*************************************************************************** Vault State ***************************************************************************/ // The Pause Window and Buffer Period are timestamp-based: they should not be relied upon for sub-minute accuracy. uint32 internal immutable _vaultPauseWindowEndTime; uint32 internal immutable _vaultBufferPeriodEndTime; // Stored as a convenience, to avoid calculating it on every operation. uint32 internal immutable _vaultBufferPeriodDuration; // Bytes32 with pause flags for the Vault, buffers, and queries. VaultStateBits internal _vaultStateBits; /** * @dev Represents the total reserve of each ERC20 token. It should be always equal to `token.balanceOf(vault)`, * except during `unlock`. */ mapping(IERC20 token => uint256 vaultBalance) internal _reservesOf; /// @dev Flag that prevents re-enabling queries. bool internal _queriesDisabledPermanently; /*************************************************************************** Contract References ***************************************************************************/ // Upgradeable contract in charge of setting permissions. IAuthorizer internal _authorizer; // Contract that receives aggregate swap and yield fees. IProtocolFeeController internal _protocolFeeController; /*************************************************************************** ERC4626 Buffers ***************************************************************************/ // Any ERC4626 token can trade using a buffer, which is like a pool, but internal to the Vault. // The registry key is the wrapped token address, so there can only ever be one buffer per wrapped token. // This means they are permissionless, and have no registration function. // // Anyone can add liquidity to a buffer // A buffer will only ever have two tokens: wrapped and underlying. We pack the wrapped and underlying balances // into a single bytes32, interpreted with the `PackedTokenBalance` library. // ERC4626 token address -> PackedTokenBalance, which stores both the underlying and wrapped token balances. // Reusing PackedTokenBalance to save bytecode (despite differing semantics). // It's arbitrary which is which: we define raw = underlying token; derived = wrapped token. mapping(IERC4626 wrappedToken => bytes32 packedTokenBalance) internal _bufferTokenBalances; // The LP balances for buffers. LP balances are not tokenized (i.e., represented by ERC20 tokens like BPT), but // rather accounted for within the Vault. // Track the internal "BPT" shares of each buffer depositor. mapping(IERC4626 wrappedToken => mapping(address user => uint256 userShares)) internal _bufferLpShares; // Total LP shares. mapping(IERC4626 wrappedToken => uint256 totalShares) internal _bufferTotalShares; // Prevents a malicious ERC4626 from changing the asset after the buffer was initialized. mapping(IERC4626 wrappedToken => address underlyingToken) internal _bufferAssets; /*************************************************************************** Transient Storage Access ***************************************************************************/ function _isUnlocked() internal view returns (StorageSlotExtension.BooleanSlotType slot) { return _IS_UNLOCKED_SLOT.asBoolean(); } function _nonZeroDeltaCount() internal view returns (StorageSlotExtension.Uint256SlotType slot) { return _NON_ZERO_DELTA_COUNT_SLOT.asUint256(); } function _tokenDeltas() internal view returns (TokenDeltaMappingSlotType slot) { return TokenDeltaMappingSlotType.wrap(_TOKEN_DELTAS_SLOT); } function _addLiquidityCalled() internal view returns (UintToAddressToBooleanMappingSlot slot) { return UintToAddressToBooleanMappingSlot.wrap(_ADD_LIQUIDITY_CALLED_SLOT); } function _sessionIdSlot() internal view returns (StorageSlotExtension.Uint256SlotType slot) { return _SESSION_ID_SLOT.asUint256(); } function _calculateVaultStorageSlot(string memory key) private pure returns (bytes32) { return TransientStorageHelpers.calculateSlot(type(VaultStorage).name, key); } }
{ "viaIR": true, "evmVersion": "cancun", "optimizer": { "enabled": true, "runs": 9999, "details": { "yulDetails": { "optimizerSteps": "dhfoDgvulfnTUtnIf [ xa[r]EscLM cCTUtTOntnfDIul Lcul Vcul [j] Tpeul xa[rul] xa[r]cL gvif CTUca[r]LSsTFOtfDnca[r]Iulc ] jmul[jul] VcTOcul jmul : fDnTOcmu" } } }, "outputSelection": { "*": { "*": [ "evm.bytecode", "evm.deployedBytecode", "devdoc", "userdoc", "metadata", "abi" ] } }, "libraries": {} }
Contract Security Audit
- No Contract Security Audit Submitted- Submit Audit Here
[{"inputs":[{"internalType":"contract IVault","name":"mainVault","type":"address"},{"internalType":"uint32","name":"pauseWindowDuration","type":"uint32"},{"internalType":"uint32","name":"bufferPeriodDuration","type":"uint32"},{"internalType":"uint256","name":"minTradeAmount","type":"uint256"},{"internalType":"uint256","name":"minWrapAmount","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"AfterAddLiquidityHookFailed","type":"error"},{"inputs":[],"name":"AfterInitializeHookFailed","type":"error"},{"inputs":[],"name":"AfterRemoveLiquidityHookFailed","type":"error"},{"inputs":[],"name":"AfterSwapHookFailed","type":"error"},{"inputs":[],"name":"AmountGivenZero","type":"error"},{"inputs":[{"internalType":"contract IERC20","name":"tokenIn","type":"address"},{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"maxAmountIn","type":"uint256"}],"name":"AmountInAboveMax","type":"error"},{"inputs":[{"internalType":"contract IERC20","name":"tokenOut","type":"address"},{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"minAmountOut","type":"uint256"}],"name":"AmountOutBelowMin","type":"error"},{"inputs":[],"name":"BalanceNotSettled","type":"error"},{"inputs":[],"name":"BalanceOverflow","type":"error"},{"inputs":[],"name":"BeforeAddLiquidityHookFailed","type":"error"},{"inputs":[],"name":"BeforeInitializeHookFailed","type":"error"},{"inputs":[],"name":"BeforeRemoveLiquidityHookFailed","type":"error"},{"inputs":[],"name":"BeforeSwapHookFailed","type":"error"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"maxAmountIn","type":"uint256"}],"name":"BptAmountInAboveMax","type":"error"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"minAmountOut","type":"uint256"}],"name":"BptAmountOutBelowMin","type":"error"},{"inputs":[{"internalType":"contract IERC4626","name":"wrappedToken","type":"address"}],"name":"BufferAlreadyInitialized","type":"error"},{"inputs":[{"internalType":"contract IERC4626","name":"wrappedToken","type":"address"}],"name":"BufferNotInitialized","type":"error"},{"inputs":[],"name":"BufferSharesInvalidOwner","type":"error"},{"inputs":[],"name":"BufferSharesInvalidReceiver","type":"error"},{"inputs":[{"internalType":"uint256","name":"totalSupply","type":"uint256"}],"name":"BufferTotalSupplyTooLow","type":"error"},{"inputs":[],"name":"CannotReceiveEth","type":"error"},{"inputs":[],"name":"CannotSwapSameToken","type":"error"},{"inputs":[],"name":"CodecOverflow","type":"error"},{"inputs":[],"name":"DoesNotSupportAddLiquidityCustom","type":"error"},{"inputs":[],"name":"DoesNotSupportDonation","type":"error"},{"inputs":[],"name":"DoesNotSupportRemoveLiquidityCustom","type":"error"},{"inputs":[],"name":"DoesNotSupportUnbalancedLiquidity","type":"error"},{"inputs":[],"name":"DynamicSwapFeeHookFailed","type":"error"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"allowance","type":"uint256"},{"internalType":"uint256","name":"needed","type":"uint256"}],"name":"ERC20InsufficientAllowance","type":"error"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"uint256","name":"balance","type":"uint256"},{"internalType":"uint256","name":"needed","type":"uint256"}],"name":"ERC20InsufficientBalance","type":"error"},{"inputs":[{"internalType":"address","name":"approver","type":"address"}],"name":"ERC20InvalidApprover","type":"error"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"}],"name":"ERC20InvalidReceiver","type":"error"},{"inputs":[{"internalType":"address","name":"sender","type":"address"}],"name":"ERC20InvalidSender","type":"error"},{"inputs":[{"internalType":"address","name":"spender","type":"address"}],"name":"ERC20InvalidSpender","type":"error"},{"inputs":[],"name":"FeePrecisionTooHigh","type":"error"},{"inputs":[{"internalType":"contract IERC20","name":"tokenIn","type":"address"},{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"maxAmountIn","type":"uint256"}],"name":"HookAdjustedAmountInAboveMax","type":"error"},{"inputs":[{"internalType":"contract IERC20","name":"tokenOut","type":"address"},{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"minAmountOut","type":"uint256"}],"name":"HookAdjustedAmountOutBelowMin","type":"error"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"limit","type":"uint256"}],"name":"HookAdjustedSwapLimit","type":"error"},{"inputs":[{"internalType":"address","name":"poolHooksContract","type":"address"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"address","name":"poolFactory","type":"address"}],"name":"HookRegistrationFailed","type":"error"},{"inputs":[],"name":"InvalidAddLiquidityKind","type":"error"},{"inputs":[],"name":"InvalidRemoveLiquidityKind","type":"error"},{"inputs":[],"name":"InvalidToken","type":"error"},{"inputs":[],"name":"InvalidTokenConfiguration","type":"error"},{"inputs":[],"name":"InvalidTokenDecimals","type":"error"},{"inputs":[],"name":"InvalidTokenType","type":"error"},{"inputs":[{"internalType":"contract IERC4626","name":"wrappedToken","type":"address"}],"name":"InvalidUnderlyingToken","type":"error"},{"inputs":[{"internalType":"uint256","name":"issuedShares","type":"uint256"},{"internalType":"uint256","name":"minIssuedShares","type":"uint256"}],"name":"IssuedSharesBelowMin","type":"error"},{"inputs":[],"name":"MaxTokens","type":"error"},{"inputs":[],"name":"MinTokens","type":"error"},{"inputs":[],"name":"NotEnoughBufferShares","type":"error"},{"inputs":[{"internalType":"contract IERC4626","name":"wrappedToken","type":"address"},{"internalType":"uint256","name":"expectedUnderlyingAmount","type":"uint256"},{"internalType":"uint256","name":"actualUnderlyingAmount","type":"uint256"}],"name":"NotEnoughUnderlying","type":"error"},{"inputs":[{"internalType":"contract IERC4626","name":"wrappedToken","type":"address"},{"internalType":"uint256","name":"expectedWrappedAmount","type":"uint256"},{"internalType":"uint256","name":"actualWrappedAmount","type":"uint256"}],"name":"NotEnoughWrapped","type":"error"},{"inputs":[],"name":"NotStaticCall","type":"error"},{"inputs":[],"name":"NotVaultDelegateCall","type":"error"},{"inputs":[],"name":"OutOfBounds","type":"error"},{"inputs":[],"name":"PauseBufferPeriodDurationTooLarge","type":"error"},{"inputs":[],"name":"PercentageAboveMax","type":"error"},{"inputs":[{"internalType":"address","name":"pool","type":"address"}],"name":"PoolAlreadyInitialized","type":"error"},{"inputs":[{"internalType":"address","name":"pool","type":"address"}],"name":"PoolAlreadyRegistered","type":"error"},{"inputs":[{"internalType":"address","name":"pool","type":"address"}],"name":"PoolInRecoveryMode","type":"error"},{"inputs":[{"internalType":"address","name":"pool","type":"address"}],"name":"PoolNotInRecoveryMode","type":"error"},{"inputs":[{"internalType":"address","name":"pool","type":"address"}],"name":"PoolNotInitialized","type":"error"},{"inputs":[{"internalType":"address","name":"pool","type":"address"}],"name":"PoolNotPaused","type":"error"},{"inputs":[{"internalType":"address","name":"pool","type":"address"}],"name":"PoolNotRegistered","type":"error"},{"inputs":[{"internalType":"address","name":"pool","type":"address"}],"name":"PoolPauseWindowExpired","type":"error"},{"inputs":[{"internalType":"address","name":"pool","type":"address"}],"name":"PoolPaused","type":"error"},{"inputs":[{"internalType":"uint256","name":"totalSupply","type":"uint256"}],"name":"PoolTotalSupplyTooLow","type":"error"},{"inputs":[],"name":"ProtocolFeesExceedTotalCollected","type":"error"},{"inputs":[],"name":"QueriesDisabled","type":"error"},{"inputs":[],"name":"QueriesDisabledPermanently","type":"error"},{"inputs":[],"name":"QuoteResultSpoofed","type":"error"},{"inputs":[],"name":"ReentrancyGuardReentrantCall","type":"error"},{"inputs":[],"name":"RouterNotTrusted","type":"error"},{"inputs":[{"internalType":"uint8","name":"bits","type":"uint8"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"SafeCastOverflowedUintDowncast","type":"error"},{"inputs":[{"internalType":"uint256","name":"value","type":"uint256"}],"name":"SafeCastOverflowedUintToInt","type":"error"},{"inputs":[{"internalType":"address","name":"sender","type":"address"}],"name":"SenderIsNotVault","type":"error"},{"inputs":[],"name":"SenderNotAllowed","type":"error"},{"inputs":[],"name":"SwapFeePercentageTooHigh","type":"error"},{"inputs":[],"name":"SwapFeePercentageTooLow","type":"error"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"limit","type":"uint256"}],"name":"SwapLimit","type":"error"},{"inputs":[{"internalType":"contract IERC20","name":"token","type":"address"}],"name":"TokenAlreadyRegistered","type":"error"},{"inputs":[{"internalType":"contract IERC20","name":"token","type":"address"}],"name":"TokenNotRegistered","type":"error"},{"inputs":[{"internalType":"address","name":"pool","type":"address"},{"internalType":"address","name":"expectedToken","type":"address"},{"internalType":"address","name":"actualToken","type":"address"}],"name":"TokensMismatch","type":"error"},{"inputs":[],"name":"TradeAmountTooSmall","type":"error"},{"inputs":[],"name":"VaultBuffersArePaused","type":"error"},{"inputs":[],"name":"VaultIsNotUnlocked","type":"error"},{"inputs":[],"name":"VaultNotPaused","type":"error"},{"inputs":[],"name":"VaultPauseWindowDurationTooLarge","type":"error"},{"inputs":[],"name":"VaultPauseWindowExpired","type":"error"},{"inputs":[],"name":"VaultPaused","type":"error"},{"inputs":[{"internalType":"contract IERC4626","name":"wrappedToken","type":"address"}],"name":"WrapAmountTooSmall","type":"error"},{"inputs":[],"name":"WrongProtocolFeeControllerDeployment","type":"error"},{"inputs":[{"internalType":"contract IERC4626","name":"wrappedToken","type":"address"},{"internalType":"address","name":"underlyingToken","type":"address"}],"name":"WrongUnderlyingToken","type":"error"},{"inputs":[],"name":"WrongVaultAdminDeployment","type":"error"},{"inputs":[],"name":"WrongVaultExtensionDeployment","type":"error"},{"inputs":[],"name":"ZeroDivision","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pool","type":"address"},{"indexed":false,"internalType":"uint256","name":"aggregateSwapFeePercentage","type":"uint256"}],"name":"AggregateSwapFeePercentageChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pool","type":"address"},{"indexed":false,"internalType":"uint256","name":"aggregateYieldFeePercentage","type":"uint256"}],"name":"AggregateYieldFeePercentageChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pool","type":"address"},{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IAuthorizer","name":"newAuthorizer","type":"address"}],"name":"AuthorizerChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IERC4626","name":"wrappedToken","type":"address"},{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"uint256","name":"burnedShares","type":"uint256"}],"name":"BufferSharesBurned","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IERC4626","name":"wrappedToken","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"issuedShares","type":"uint256"}],"name":"BufferSharesMinted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pool","type":"address"},{"indexed":true,"internalType":"address","name":"liquidityProvider","type":"address"},{"indexed":true,"internalType":"enum AddLiquidityKind","name":"kind","type":"uint8"},{"indexed":false,"internalType":"uint256","name":"totalSupply","type":"uint256"},{"indexed":false,"internalType":"uint256[]","name":"amountsAddedRaw","type":"uint256[]"},{"indexed":false,"internalType":"uint256[]","name":"swapFeeAmountsRaw","type":"uint256[]"}],"name":"LiquidityAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IERC4626","name":"wrappedToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"amountUnderlying","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amountWrapped","type":"uint256"},{"indexed":false,"internalType":"bytes32","name":"bufferBalances","type":"bytes32"}],"name":"LiquidityAddedToBuffer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pool","type":"address"},{"indexed":true,"internalType":"address","name":"liquidityProvider","type":"address"},{"indexed":true,"internalType":"enum RemoveLiquidityKind","name":"kind","type":"uint8"},{"indexed":false,"internalType":"uint256","name":"totalSupply","type":"uint256"},{"indexed":false,"internalType":"uint256[]","name":"amountsRemovedRaw","type":"uint256[]"},{"indexed":false,"internalType":"uint256[]","name":"swapFeeAmountsRaw","type":"uint256[]"}],"name":"LiquidityRemoved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IERC4626","name":"wrappedToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"amountUnderlying","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amountWrapped","type":"uint256"},{"indexed":false,"internalType":"bytes32","name":"bufferBalances","type":"bytes32"}],"name":"LiquidityRemovedFromBuffer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pool","type":"address"}],"name":"PoolInitialized","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pool","type":"address"},{"indexed":false,"internalType":"bool","name":"paused","type":"bool"}],"name":"PoolPausedStateChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pool","type":"address"},{"indexed":false,"internalType":"bool","name":"recoveryMode","type":"bool"}],"name":"PoolRecoveryModeStateChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pool","type":"address"},{"indexed":true,"internalType":"address","name":"factory","type":"address"},{"components":[{"internalType":"contract IERC20","name":"token","type":"address"},{"internalType":"enum TokenType","name":"tokenType","type":"uint8"},{"internalType":"contract IRateProvider","name":"rateProvider","type":"address"},{"internalType":"bool","name":"paysYieldFees","type":"bool"}],"indexed":false,"internalType":"struct TokenConfig[]","name":"tokenConfig","type":"tuple[]"},{"indexed":false,"internalType":"uint256","name":"swapFeePercentage","type":"uint256"},{"indexed":false,"internalType":"uint32","name":"pauseWindowEndTime","type":"uint32"},{"components":[{"internalType":"address","name":"pauseManager","type":"address"},{"internalType":"address","name":"swapFeeManager","type":"address"},{"internalType":"address","name":"poolCreator","type":"address"}],"indexed":false,"internalType":"struct PoolRoleAccounts","name":"roleAccounts","type":"tuple"},{"components":[{"internalType":"bool","name":"enableHookAdjustedAmounts","type":"bool"},{"internalType":"bool","name":"shouldCallBeforeInitialize","type":"bool"},{"internalType":"bool","name":"shouldCallAfterInitialize","type":"bool"},{"internalType":"bool","name":"shouldCallComputeDynamicSwapFee","type":"bool"},{"internalType":"bool","name":"shouldCallBeforeSwap","type":"bool"},{"internalType":"bool","name":"shouldCallAfterSwap","type":"bool"},{"internalType":"bool","name":"shouldCallBeforeAddLiquidity","type":"bool"},{"internalType":"bool","name":"shouldCallAfterAddLiquidity","type":"bool"},{"internalType":"bool","name":"shouldCallBeforeRemoveLiquidity","type":"bool"},{"internalType":"bool","name":"shouldCallAfterRemoveLiquidity","type":"bool"},{"internalType":"address","name":"hooksContract","type":"address"}],"indexed":false,"internalType":"struct HooksConfig","name":"hooksConfig","type":"tuple"},{"components":[{"internalType":"bool","name":"disableUnbalancedLiquidity","type":"bool"},{"internalType":"bool","name":"enableAddLiquidityCustom","type":"bool"},{"internalType":"bool","name":"enableRemoveLiquidityCustom","type":"bool"},{"internalType":"bool","name":"enableDonation","type":"bool"}],"indexed":false,"internalType":"struct LiquidityManagement","name":"liquidityManagement","type":"tuple"}],"name":"PoolRegistered","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IProtocolFeeController","name":"newProtocolFeeController","type":"address"}],"name":"ProtocolFeeControllerChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pool","type":"address"},{"indexed":true,"internalType":"contract IERC20","name":"tokenIn","type":"address"},{"indexed":true,"internalType":"contract IERC20","name":"tokenOut","type":"address"},{"indexed":false,"internalType":"uint256","name":"amountIn","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amountOut","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"swapFeePercentage","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"swapFeeAmount","type":"uint256"}],"name":"Swap","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pool","type":"address"},{"indexed":false,"internalType":"uint256","name":"swapFeePercentage","type":"uint256"}],"name":"SwapFeePercentageChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pool","type":"address"},{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IERC4626","name":"wrappedToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"burnedShares","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"withdrawnUnderlying","type":"uint256"},{"indexed":false,"internalType":"bytes32","name":"bufferBalances","type":"bytes32"}],"name":"Unwrap","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pool","type":"address"},{"indexed":true,"internalType":"bytes32","name":"eventKey","type":"bytes32"},{"indexed":false,"internalType":"bytes","name":"eventData","type":"bytes"}],"name":"VaultAuxiliary","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bool","name":"paused","type":"bool"}],"name":"VaultBuffersPausedStateChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bool","name":"paused","type":"bool"}],"name":"VaultPausedStateChanged","type":"event"},{"anonymous":false,"inputs":[],"name":"VaultQueriesDisabled","type":"event"},{"anonymous":false,"inputs":[],"name":"VaultQueriesEnabled","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IERC4626","name":"wrappedToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"depositedUnderlying","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"mintedShares","type":"uint256"},{"indexed":false,"internalType":"bytes32","name":"bufferBalances","type":"bytes32"}],"name":"Wrap","type":"event"},{"stateMutability":"payable","type":"fallback"},{"inputs":[{"internalType":"contract IERC4626","name":"wrappedToken","type":"address"},{"internalType":"uint256","name":"maxAmountUnderlyingInRaw","type":"uint256"},{"internalType":"uint256","name":"maxAmountWrappedInRaw","type":"uint256"},{"internalType":"uint256","name":"exactSharesToIssue","type":"uint256"},{"internalType":"address","name":"sharesOwner","type":"address"}],"name":"addLiquidityToBuffer","outputs":[{"internalType":"uint256","name":"amountUnderlyingRaw","type":"uint256"},{"internalType":"uint256","name":"amountWrappedRaw","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"areBuffersPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"pool","type":"address"}],"name":"collectAggregateFees","outputs":[{"internalType":"uint256[]","name":"totalSwapFees","type":"uint256[]"},{"internalType":"uint256[]","name":"totalYieldFees","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"disableQuery","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"disableQueryPermanently","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"pool","type":"address"}],"name":"disableRecoveryMode","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"enableQuery","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"pool","type":"address"}],"name":"enableRecoveryMode","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"selector","type":"bytes4"}],"name":"getActionId","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC4626","name":"wrappedToken","type":"address"}],"name":"getBufferAsset","outputs":[{"internalType":"address","name":"underlyingToken","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC4626","name":"token","type":"address"}],"name":"getBufferBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getBufferMinimumTotalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"contract IERC4626","name":"token","type":"address"},{"internalType":"address","name":"user","type":"address"}],"name":"getBufferOwnerShares","outputs":[{"internalType":"uint256","name":"shares","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getBufferPeriodDuration","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getBufferPeriodEndTime","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC4626","name":"token","type":"address"}],"name":"getBufferTotalShares","outputs":[{"internalType":"uint256","name":"shares","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getMaximumPoolTokens","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getMinimumPoolTokens","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getMinimumTradeAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getMinimumWrapAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPauseWindowEndTime","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPoolMinimumTotalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getVaultPausedState","outputs":[{"internalType":"bool","name":"","type":"bool"},{"internalType":"uint32","name":"","type":"uint32"},{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC4626","name":"wrappedToken","type":"address"},{"internalType":"uint256","name":"amountUnderlyingRaw","type":"uint256"},{"internalType":"uint256","name":"amountWrappedRaw","type":"uint256"},{"internalType":"uint256","name":"minIssuedShares","type":"uint256"},{"internalType":"address","name":"sharesOwner","type":"address"}],"name":"initializeBuffer","outputs":[{"internalType":"uint256","name":"issuedShares","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"isVaultPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"pool","type":"address"}],"name":"pausePool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"pauseVault","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"pauseVaultBuffers","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"reentrancyGuardEntered","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC4626","name":"wrappedToken","type":"address"},{"internalType":"uint256","name":"sharesToRemove","type":"uint256"},{"internalType":"uint256","name":"minAmountUnderlyingOutRaw","type":"uint256"},{"internalType":"uint256","name":"minAmountWrappedOutRaw","type":"uint256"}],"name":"removeLiquidityFromBuffer","outputs":[{"internalType":"uint256","name":"removedUnderlyingBalanceRaw","type":"uint256"},{"internalType":"uint256","name":"removedWrappedBalanceRaw","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IERC4626","name":"wrappedToken","type":"address"},{"internalType":"uint256","name":"sharesToRemove","type":"uint256"},{"internalType":"uint256","name":"minAmountUnderlyingOutRaw","type":"uint256"},{"internalType":"uint256","name":"minAmountWrappedOutRaw","type":"uint256"},{"internalType":"address","name":"sharesOwner","type":"address"}],"name":"removeLiquidityFromBufferHook","outputs":[{"internalType":"uint256","name":"removedUnderlyingBalanceRaw","type":"uint256"},{"internalType":"uint256","name":"removedWrappedBalanceRaw","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IAuthorizer","name":"newAuthorizer","type":"address"}],"name":"setAuthorizer","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IProtocolFeeController","name":"newProtocolFeeController","type":"address"}],"name":"setProtocolFeeController","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"pool","type":"address"},{"internalType":"uint256","name":"swapFeePercentage","type":"uint256"}],"name":"setStaticSwapFeePercentage","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"pool","type":"address"}],"name":"unpausePool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unpauseVault","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unpauseVaultBuffers","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"pool","type":"address"},{"internalType":"uint256","name":"newAggregateSwapFeePercentage","type":"uint256"}],"name":"updateAggregateSwapFeePercentage","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"pool","type":"address"},{"internalType":"uint256","name":"newAggregateYieldFeePercentage","type":"uint256"}],"name":"updateAggregateYieldFeePercentage","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"vault","outputs":[{"internalType":"contract IVault","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]
Contract Creation Code
6102006040908082523461036b5760a081613fac8038038091610022828561039e565b83398101031261036b5780516001600160a01b03811680820361036b57602061004c8185016103c1565b6100578686016103c1565b91608060608701519601519561008c88516100718161036f565b600a8152691a5cd55b9b1bd8dad95960b21b848201526103d2565b60c0526100bf885161009d8161036f565b60118152701b9bdb96995c9bd1195b1d1850dbdd5b9d607a1b848201526103d2565b60e0526100ec88516100d08161036f565b600b81526a746f6b656e44656c74617360a81b848201526103d2565b9761010098895261012481516101018161036f565b6012815271185919131a5c5d5a591a5d1e50d85b1b195960721b858201526103d2565b9761012098895261015382516101398161036f565b60098152681cd95cdcda5bdb925960ba1b868201526103d2565b926101409384526101c09788526101e098895263ffffffff80961694630784ce00861161035c578688169562ed4e00871161034d5742019081421161032457878211610338575086166101608181526101a098895295019586116103245761018095865260805260a0525196613b1a98896104928a3960805189610728015260a05189612bb1015260c0518961347b015260e05189818161387701526138d70152518861382c0152518750505186505051858181611add01528181611ec60152611f3e0152518481816112ec01528181611f6501526133a201525183818161044101528181610e25015281816111fa01528181612b670152612d7201525182613185015251818181610281015281816102d401528181610328015281816104bd0152818161076401528181610a1601528181610b6501528181610c2801528181610cc301528181610d2101528181610f1101528181611330015281816119c201528181611a7501528181611bbd01528181611c1d01528181611f0001528181611ff40152818161206c015281816123f1015281816124ab015281816129bb01528181612a1201528181612bfa01528181612c9301528181612dab01528181612e7601528181612ed90152612f900152f35b634e487b7160e01b5f52601160045260245ffd5b6306dfcc6560e41b5f5260045260245260445ffd5b634f5277f760e11b5f5260045ffd5b63cc0e8fe560e01b5f5260045ffd5b5f80fd5b604081019081106001600160401b0382111761038a57604052565b634e487b7160e01b5f52604160045260245ffd5b601f909101601f19168101906001600160401b0382119082101761038a57604052565b519063ffffffff8216820361036b57565b604051906103df8261036f565b600c8252610460603a602084016b5661756c7453746f7261676560a01b81526020604051948592828401977f62616c616e6365722d6c6162732e76332e73746f726167652e000000000000008952518091603986015e830190601760f91b60398301528051928391018583015e015f8382015203601a81018452018261039e565b5190205f198101908111610324576040519060208201908152602082526104868261036f565b9051902060ff19169056fe6080604052600436101561009f575b361561007757346100775760646040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152600f60248201527f4e6f7420696d706c656d656e74656400000000000000000000000000000000006044820152fd5b7ff2238896000000000000000000000000000000000000000000000000000000005f5260045ffd5b5f803560e01c80630387587d14612f70578063058a628f14612eae578063098401f514612e615780630b7562be14612d9657806320c1fb7a14612d5657806326a8a99114612d3a5780632d77138914612c695780632e42f4d514612c4e5780634021fe0f14612bd457806353956aa214612b9a57806355aca1ec146129f457806355cba7fe146129a65780635dcacd64146124945780635e0b06f4146123ce578063653eb3b014612053578063821440f214611fde578063851c1bb314611f8c57806385c8c01514611eea5780638a8d123a14611ea95780638f4ab9ca14611bff5780639385e39a14611b855780639e0879c214611a5f578063a8175b2714611a43578063b9212b49146119ac578063bffb78b214611310578063cd51c12f146112cf578063d0965a6b146112b1578063d15126ba14610eef578063d2c725e014610eb0578063dc3f574e14610cfa578063de1a36a614610cad578063e085c5a814610c12578063e0d5560514610b4f578063e253670a146109f3578063e2a92b1a1461074b578063e2cb0ba014610710578063ebc7955c146104a1578063f21c38cd14610309578063f2784e07146102a85763fbfa77cf14610262575061000e565b346102a557806003193601126102a55760206040516001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000168152f35b80fd5b50346102a55760206003193601126102a55760406020916001600160a01b036102cf612fd3565b6102f87f0000000000000000000000000000000000000000000000000000000000000000613267565b168152600d83522054604051908152f35b50346102a55760206003193601126102a557610323612fd3565b61034c7f0000000000000000000000000000000000000000000000000000000000000000613267565b6103558161342e565b6103796001600160a01b039182811692835f52600160205260405f205416906137ab565b805f525f60205260405f205460018160021c1663ffffffff80836103a361039e605a90565b61313f565b1c168261043b575b50501561040f5760207f57e20448028297190122571be7cb6c1b1ef85730c673f7c72f533c8662419aa7917ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb5f91858352828452166040822055604051908152a280f35b507ffdcd6894000000000000000000000000000000000000000000000000000000005f5260045260245ffd5b908092507f0000000000000000000000000000000000000000000000000000000000000000160181811161047457164211155f806103ab565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b50346102a55760806003193601126102a5576104bb612fd3565b7f00000000000000000000000000000000000000000000000000000000000000006104e581613267565b6001600160a01b03906040519260209280848601927f5dcacd640000000000000000000000000000000000000000000000000000000084521660248601526024356044860152604435606486015260643560848601523360a486015260a4855260e085019067ffffffffffffffff92868310848411176106e357601f948389809460448b8496856040527f48c894910000000000000000000000000000000000000000000000000000000086528c60e4830152848251918261010485015282610124918286015e83830101527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09b018b168101030193165af19485156106d8578695610618575b5050505060408280518101031261061457604080935081830151920151908351928352820152f35b8280fd5b9091929394503d8087843e61062d81846130df565b810191858260e085019403126106d45751908382116106d457018160ff820112156106d05760e0810151916101009383116106a357908693929161067b8760405197601f86011601876130df565b82865283838301011161069f5785928291018386015e83010152905f8080806105ec565b8380fd5b6024877f4e487b710000000000000000000000000000000000000000000000000000000081526041600452fd5b8580fd5b8680fd5b6040513d88823e3d90fd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b50346102a557806003193601126102a55760206040517f00000000000000000000000000000000000000000000000000000000000000008152f35b50346102a55761075a36612fe9565b61078895919294957f0000000000000000000000000000000000000000000000000000000000000000613267565b610790613479565b6107986135b9565b6107a1866134ca565b6107a96133d9565b6001600160a01b0392838716916040517f38d52e0f0000000000000000000000000000000000000000000000000000000081526020958682600481885afa9182156106d85786926109c4575b50848652600e875280604087205416911680910361099457838552600b8652604085205497600d87526040862054916fffffffffffffffffffffffffffffffff8a169161085261084685888661372c565b9b60801c94878661372c565b99808c116109605750808a1161092c5750936108fd936108cd60409c946108c78c6108c18f6108df986108bc8e6108b37f75c4dc5f23640eeba7d404d9165f515fc3d9e23a5c8b6e2d09b4b9da56ff00a99f6108ad866137c7565b9061381c565b6108ad866137c7565b61314d565b9261314d565b90613557565b93878952600b8a52848d8a2055613627565b8851918291888a846040919493926060820195825260208201520152565b0390a27f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005d8351928352820152f35b876064918b897f8eda85e4000000000000000000000000000000000000000000000000000000008452600452602452604452fd5b886064918d857f8eda85e4000000000000000000000000000000000000000000000000000000008452600452602452604452fd5b84604491857f36b18d09000000000000000000000000000000000000000000000000000000008352600452602452fd5b6109e5919250873d89116109ec575b6109dd81836130df565b810190613120565b905f6107f5565b503d6109d3565b50346102a55760406003193601126102a557610a0d612fd3565b60243590610a3a7f0000000000000000000000000000000000000000000000000000000000000000613267565b610a438161342e565b670de0b6b3a76400008211610b27576001600160a01b039081600a54163303610aff571690818352826020526040832054670de0b5cad2bef0008211610ad7577f606eb97d83164bd6b200d638cd49c14c65d94d4f2c674cfd85e24e0e202c3ca591610ac2602092610ab3604290565b9064174876e800840490613950565b8486528583526040862055604051908152a280f35b6004847f746e5940000000000000000000000000000000000000000000000000000000008152fd5b6004847f23dada53000000000000000000000000000000000000000000000000000000008152fd5b6004837f4c69ac5d000000000000000000000000000000000000000000000000000000008152fd5b50346102a557806003193601126102a557610b897f0000000000000000000000000000000000000000000000000000000000000000613267565b610b916132a0565b60ff60095416610bea577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe600754166007557f91d7478835f2b5adc315f5aad920f4a7f0a02f7fddf3042d17b2c80168ea17f58180a180f35b807f069f8cbc0000000000000000000000000000000000000000000000000000000060049252fd5b50346102a557806003193601126102a557610c4c7f0000000000000000000000000000000000000000000000000000000000000000613267565b610c546132a0565b60047ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb60075416176007557f300c7ca619eb846386aa0a6e5916ac2a41406448b0a2e99ba9ccafeb899015a5602060405160018152a180f35b50346102a557806003193601126102a557610ce77f0000000000000000000000000000000000000000000000000000000000000000613267565b610cef6132a0565b610cf76136db565b80f35b50346102a55760206003193601126102a5576001600160a01b03610d1c612fd3565b610d457f0000000000000000000000000000000000000000000000000000000000000000613267565b610d4e8161342e565b16808252816020526001604083205460031c16610e855780825281602052604082205460018160021c169063ffffffff8091610d8b61039e605a90565b1c1682610e1f575b50501580610e10575b610e03575b805f525f60205260405f2060087ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff78254161790557fc2354cc2f78ea57777e55ddd43a7f22b112ce98868596880edaeb22b4f9c73a9602060405160018152a280f35b610e0b6132a0565b610da1565b50610e1961339b565b15610d9c565b908092507f00000000000000000000000000000000000000000000000000000000000000001601818111610e5857164211155f80610d93565b6024847f4e487b710000000000000000000000000000000000000000000000000000000081526011600452fd5b7f346d7607000000000000000000000000000000000000000000000000000000008252600452602490fd5b50346102a557806003193601126102a55760207f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005c6040519015158152f35b50346102a55760406003193601126102a557610f09612fd3565b602435610f357f0000000000000000000000000000000000000000000000000000000000000000613267565b610f3e8261342e565b6001600160a01b0380831692838552602091600183526001604087200154169081155f1461128257610f709150613997565b610f7861339b565b61125a57828452838152604084205460018160021c169063ffffffff8091610fa161039e605a90565b1c16826111f4575b50506111c8576040517fce20ece70000000000000000000000000000000000000000000000000000000081528181600481875afa90811561116857859161119b575b508210611173576040517f654cf15d0000000000000000000000000000000000000000000000000000000081528181600481875afa908115611168578591611137575b50821161110f57828452838152604084205491670de0b5cad2bef00081116110e75764174876e8008104928360181c6110bf577ffffffffffffffffffffffffffffffffffffffffffffffffffffffc000003ffff7f89d41522342fabac1471ca6073a5623e5caf367b03ca6e9a001478d0cf8be4a19486885287855260121b9116176040862055604051908152a280f35b7fe4337c05000000000000000000000000000000000000000000000000000000005f5260045ffd5b6004857f746e5940000000000000000000000000000000000000000000000000000000008152fd5b6004847f7f47834b000000000000000000000000000000000000000000000000000000008152fd5b90508181813d8311611161575b61114e81836130df565b8101031261115d57515f61102e565b5f80fd5b503d611144565b6040513d87823e3d90fd5b6004847fbfb20688000000000000000000000000000000000000000000000000000000008152fd5b90508181813d83116111c1575b6111b281836130df565b8101031261115d57515f610feb565b503d6111a8565b602484847fd971f597000000000000000000000000000000000000000000000000000000008252600452fd5b908092507f0000000000000000000000000000000000000000000000000000000000000000160181811161122d57164211155f80610fa9565b6024867f4e487b710000000000000000000000000000000000000000000000000000000081526011600452fd5b6004847fda9f8b34000000000000000000000000000000000000000000000000000000008152fd5b503314610f70576004847f23dada53000000000000000000000000000000000000000000000000000000008152fd5b50346102a557806003193601126102a5576020604051620f42408152f35b50346102a557806003193601126102a557602060405163ffffffff7f0000000000000000000000000000000000000000000000000000000000000000168152f35b50346102a55760206003193601126102a55761132a612fd3565b906113547f0000000000000000000000000000000000000000000000000000000000000000613267565b61135d8261342e565b6113656132a0565b6001600160a01b0382168152806020526001604082205460031c16156119785761138d6133d9565b6040519160e0830183811067ffffffffffffffff8211176106e3576040525f8352606060208401526060604084015260608084015260606080840152606060a0840152606060c08401526001600160a01b0381165f52600560205260405f205f60205260405f205490600460205260405f2090600360205260405f2080549387526040519060208286815201905f528160205f20915f5b8781106119555750611438925003826130df565b6020870152611446836131bf565b61145360405191826130df565b8381527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0611480856131bf565b015f5b81811061192c575050604087015261149a836131d7565b60608701526114a8836131d7565b6080870152855164ffffffffff6114be856131d7565b91605a1c165f5b8581106118f057505060c08701526114dc836131d7565b60a0870152855191600183811c1692836118dc575b50826118cc575b5f5b8481106115ee578787876001600160a01b0381165f52600560205260405f20905f5b60608501518051821015611562579061155061153a82600194613226565b516115498360808a0151613226565b5190613557565b815f528460205260405f20550161151c565b84835f7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005d6001600160a01b03165f8181526020818152604080832080547ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7169055519182527fc2354cc2f78ea57777e55ddd43a7f22b112ce98868596880edaeb22b4f9c73a991a280f35b6001600160a01b036116048260208b0151613226565b51165f528160205260405f20906040519161161e836130c3565b5460ff9081811661162e81613a2e565b84526001600160a01b038160081c16602085015260a81c1615156040830152805f528360205260405f2054916116748260408c01518361166e8383613226565b52613226565b50805161168081613a2e565b61168981613a2e565b806118055750670de0b6b3a76400005b6116a78360a08d0151613226565b526116c56fffffffffffffffffffffffffffffffff8416838c613a88565b85156117fb576040810151151590816117dd575b506116ea575b600191505b016114fa565b886116f58151613a65565b611703836060840151613226565b519360801c6117178460805f950151613226565b5181811161174e575b50505060019281611733575b50506116df565b61174791611740916130a2565b828b613a88565b5f8061172c565b611759935003613058565b91670de0b6b3a7640000926001847fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff83010401901515026117b88b6117b18560a06117a88260c0860151613226565b51930151613226565b5190613058565b8185810204851482151715610474576001946117d4920261306b565b90925f80611720565b60019150516117eb81613a2e565b6117f481613a2e565b145f6116d9565b50600191506116e4565b80611811600192613a2e565b036118a457600460206001600160a01b038184015116604051928380927f679aefce0000000000000000000000000000000000000000000000000000000082525afa908115611899575f91611867575b50611699565b90506020813d602011611891575b81611882602093836130df565b8101031261115d57515f611861565b3d9150611875565b6040513d5f823e3d90fd5b7fdf450632000000000000000000000000000000000000000000000000000000005f5260045ffd5b865160031c6001161592506114f8565b6118e7919350613a65565b1515915f6114f1565b60058102908082046005148115171561047457604d601f84841c161161047457601f836001931c16600a0a6119258286613226565b52016114c5565b60209060405161193b816130c3565b5f81525f838201525f604082015282828601015201611483565b91506001602081926001600160a01b038654168152019301910191839192611424565b6001600160a01b036024927fef029adf00000000000000000000000000000000000000000000000000000000835216600452fd5b50346102a557806003193601126102a5576119e67f0000000000000000000000000000000000000000000000000000000000000000613267565b6119ee6132a0565b7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb600754166007557f300c7ca619eb846386aa0a6e5916ac2a41406448b0a2e99ba9ccafeb899015a560206040515f8152a180f35b50346102a557806003193601126102a557602060405160028152f35b50346102a557806003193601126102a557611a997f0000000000000000000000000000000000000000000000000000000000000000613267565b611aa16132a0565b611aa961339b565b15611ad6577fda9f8b34000000000000000000000000000000000000000000000000000000005f5260045ffd5b63ffffffff7f000000000000000000000000000000000000000000000000000000000000000016421015611b5d5760027ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd60075416176007557fe0629fe656e45ad7fd63a24b899da368690024c07043b88e57aee5095b1d3d02602060405160018152a180f35b7f0e4460b7000000000000000000000000000000000000000000000000000000005f5260045ffd5b50346102a55760406003193601126102a557611b9f612fd3565b602435916001600160a01b0380841680940361115d57604092611be17f0000000000000000000000000000000000000000000000000000000000000000613267565b168152600c60205220905f52602052602060405f2054604051908152f35b50346102a557602080600319360112611ea557611c1a612fd3565b907f0000000000000000000000000000000000000000000000000000000000000000611c4581613267565b611c4d613479565b6001600160a01b039283600a54163303611e7d57938084602487611c71839961342e565b60405197889384927fca4f28030000000000000000000000000000000000000000000000000000000084521696876004840152165afa938415611e70578194611dd0575b50835192611ccb611cc5856131d7565b946131d7565b90825b8651811015611da2578088611ce56001938a613226565b511686865260069081855260408720815f5285526fffffffffffffffffffffffffffffffff60405f20548060801c611d1d868a613226565b5216611d29848b613226565b52611d34838a613226565b5115801590611d8f575b611d4b575b505001611cce565b611d8891888852855260408720815f5285528660405f2055611d82611d70848b613226565b51611d7b8589613226565b519061314d565b90613517565b5f80611d43565b50611d9a8387613226565b511515611d3e565b611dbf86611dcc8486604051948594604086526040860190613025565b9184830390850152613025565b0390f35b9093503d8085833e611de281836130df565b8101908381830312611e6c5780519067ffffffffffffffff82116106d057019080601f83011215611e6c578151611e18816131bf565b92611e2660405194856130df565b818452858085019260051b8201019283116106d4578501905b828210611e5057505050925f611cb5565b81518881168103611e68578152908501908501611e3f565b8780fd5b8480fd5b50604051903d90823e3d90fd5b6004857f23dada53000000000000000000000000000000000000000000000000000000008152fd5b5080fd5b50346102a557806003193601126102a557602060405163ffffffff7f0000000000000000000000000000000000000000000000000000000000000000168152f35b50346102a557806003193601126102a557611f247f0000000000000000000000000000000000000000000000000000000000000000613267565b6060611f2e61339b565b604051901515815263ffffffff807f00000000000000000000000000000000000000000000000000000000000000001660208301527f0000000000000000000000000000000000000000000000000000000000000000166040820152f35b50346102a55760206003193601126102a557600435907fffffffff00000000000000000000000000000000000000000000000000000000821682036102a5576020611fd68361315a565b604051908152f35b50346102a557806003193601126102a5576120187f0000000000000000000000000000000000000000000000000000000000000000613267565b6120206132a0565b60017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff006009541617600955610cf76136db565b50346102a55761206236612fe9565b94926120909291927f0000000000000000000000000000000000000000000000000000000000000000613267565b612098613479565b6120a06135b9565b6120a86133d9565b6001600160a01b039586861693848652602097600e8952806040882054166123a2576040517f38d52e0f00000000000000000000000000000000000000000000000000000000815289816004818a5afa90811561234e578891612385575b501680156123595761214e90868852600e8a5260408820817fffffffffffffffffffffffff00000000000000000000000000000000000000008254161790556108ad856137c7565b61216061215a856137c7565b8661381c565b61216a8484613557565b91858752600b89528260408820556040517f4cdad50600000000000000000000000000000000000000000000000000000000815285600482015289816024818a5afa801561234e578590899061231d575b6121c5925061314d565b976121cf896135f0565b7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd8f089019889116122f05761225291899189897fd66f031d33381c6408f0b32c884461e5de3df8808399b6f3a3d86b1368f8ec348e828452600d8152612710806040862055600c8252604085205f805282528060405f2055604051908152a3613627565b8087106122c05750604080519283526020830193909352918101919091527f75c4dc5f23640eeba7d404d9165f515fc3d9e23a5c8b6e2d09b4b9da56ff00a990606090a27f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005d604051908152f35b85604491887fda0cb07e000000000000000000000000000000000000000000000000000000008352600452602452fd5b6024887f4e487b710000000000000000000000000000000000000000000000000000000081526011600452fd5b50508981813d8311612347575b61233481836130df565b8101031261115d57846121c591516121bb565b503d61232a565b6040513d8a823e3d90fd5b602487877fd407f9c5000000000000000000000000000000000000000000000000000000008252600452fd5b61239c91508a3d8c116109ec576109dd81836130df565b5f612106565b602487877f1690fa40000000000000000000000000000000000000000000000000000000008252600452fd5b50346102a55760406003193601126102a5576123e8612fd3565b602435906124157f0000000000000000000000000000000000000000000000000000000000000000613267565b61241e8161342e565b670de0b6b3a76400008211610b27576001600160a01b039081600a54163303610aff571690818352826020526040832054670de0b5cad2bef0008211610ad75781610ac260209264174876e8007fe4d371097beea42453a37406e2aef4c04f3c548f84ac50e72578662c0dcd735495049061390e565b503461115d576124a336612fe9565b9490929193947f00000000000000000000000000000000000000000000000000000000000000006124d381613267565b6001600160a01b038116330361297a576124eb613479565b6124f4876134ca565b3215808061296d575b612905575b506001600160a01b0387165f52600c60205260405f206001600160a01b0383165f5260205260405f205484116128dd576001600160a01b0387165f52600b60205260405f205495600d60205260405f20549661258a61257c89612577896fffffffffffffffffffffffffffffffff8616613058565b61306b565b98612577888460801c613058565b966001600160a01b038a165f52600e6020526001600160a01b0360405f20541692808a106128aa575080881061286e5750612605906125c98984613517565b6125dc886001600160a01b038c16613517565b6108c7886125fc8b6fffffffffffffffffffffffffffffffff85166130a2565b9260801c6130a2565b946001600160a01b0389165f52600b6020528560405f20556001600160a01b03841615612846576001600160a01b0389165f52600d60205261264b8160405f20546130a2565b612654816135f0565b6001600160a01b038a165f52600d60205260405f2055600c60205260405f206001600160a01b0385165f5260205260405f206126918282546130a2565b90556040519081526001600160a01b038416907f4e09f7f7fc37ce2897800e2c2a9099565edb0a133d19d84a6871b3530af8846b60206001600160a01b038c1692a3866127c5575b5084612735575b5050604080518581526020810185905280820193909352946001600160a01b0316917f44d97b36e99b590b3d2875aad3b167b1d7fb1e063f3f1325a1eeac76caee51139150606090a282519182526020820152f35b6001600160a01b0381163b15610614576001600160a01b03606488858381958160405198899788967fae6393290000000000000000000000000000000000000000000000000000000088521660048701521660248501528a6044850152165af180156127ba576127a6575b806126e0565b6127b082916130af565b6102a557806127a0565b6040513d84823e3d90fd5b6001600160a01b0382163b1561115d57604051907fae63932900000000000000000000000000000000000000000000000000000000825260048201526001600160a01b03831660248201528660448201525f81606481836001600160a01b0387165af1801561189957156126d95761283e9193506130af565b5f915f6126d9565b7f586d06df000000000000000000000000000000000000000000000000000000005f5260045ffd5b876001600160a01b038b7f8eda85e4000000000000000000000000000000000000000000000000000000005f521660045260245260445260645ffd5b89847f8eda85e4000000000000000000000000000000000000000000000000000000005f5260045260245260445260645ffd5b7f98c5dbd6000000000000000000000000000000000000000000000000000000005f5260045ffd5b15612945576001600160a01b0387165f52600c60205260405f206001600160a01b0383165f5260205260405f2061293d85825461314d565b90555f612502565b7f67f84ab2000000000000000000000000000000000000000000000000000000005f5260045ffd5b50600160075416156124fd565b7f089676d5000000000000000000000000000000000000000000000000000000005f523360045260245ffd5b3461115d575f60031936011261115d576129df7f0000000000000000000000000000000000000000000000000000000000000000613267565b6020600160075460021c166040519015158152f35b3461115d57602060031936011261115d57612a0d612fd3565b612a367f0000000000000000000000000000000000000000000000000000000000000000613267565b612a3f8161342e565b612a636001600160a01b039182811692835f52600160205260405f205416906137ab565b805f525f60205260405f205460018160021c1663ffffffff908183612a8961039e605a90565b1c1681612b62575b5015612ac357827fd971f597000000000000000000000000000000000000000000000000000000005f5260045260245ffd5b81612ace605a61313f565b1c16421015612b36577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb600491835f525f602052161760405f20557f57e20448028297190122571be7cb6c1b1ef85730c673f7c72f533c8662419aa7602060405160018152a2005b507feb5a1217000000000000000000000000000000000000000000000000000000005f5260045260245ffd5b9050817f0000000000000000000000000000000000000000000000000000000000000000160181811161047457811642111584612a91565b3461115d575f60031936011261115d5760206040517f00000000000000000000000000000000000000000000000000000000000000008152f35b3461115d57602060031936011261115d576001600160a01b03612bf5612fd3565b612c1e7f0000000000000000000000000000000000000000000000000000000000000000613267565b165f52600b6020526040805f20548151906fffffffffffffffffffffffffffffffff8116825260801c6020820152f35b3461115d575f60031936011261115d57602060405160088152f35b3461115d57602060031936011261115d576004356001600160a01b03811680910361115d57612cb77f0000000000000000000000000000000000000000000000000000000000000000613267565b612cbf6132a0565b612cc76133d9565b807fffffffffffffffffffffffff0000000000000000000000000000000000000000600a541617600a557f280a60b1e63c1774d397d35cce80eb80e51408ead755fb446e6f744ce98e5df05f80a25f7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005d005b3461115d575f60031936011261115d5760206040516127108152f35b3461115d575f60031936011261115d57602060405163ffffffff7f0000000000000000000000000000000000000000000000000000000000000000168152f35b3461115d575f60031936011261115d57612dcf7f0000000000000000000000000000000000000000000000000000000000000000613267565b612dd76132a0565b612ddf61339b565b15612e39577fe0629fe656e45ad7fd63a24b899da368690024c07043b88e57aee5095b1d3d0260205f7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd60075416600755604051908152a1005b7ff7ff4dca000000000000000000000000000000000000000000000000000000005f5260045ffd5b3461115d575f60031936011261115d57612e9a7f0000000000000000000000000000000000000000000000000000000000000000613267565b6020612ea461339b565b6040519015158152f35b3461115d57602060031936011261115d576004356001600160a01b0381169081810361115d57612efd7f0000000000000000000000000000000000000000000000000000000000000000613267565b612f056132a0565b7fffffffffffffffffffffff0000000000000000000000000000000000000000ff74ffffffffffffffffffffffffffffffffffffffff006009549260081b169116176009557f94b979b6831a51293e2641426f97747feed46f17779fed9cd18d1ecefcfe92ef5f80a2005b3461115d57602060031936011261115d576020612f8b612fd3565b612fb47f0000000000000000000000000000000000000000000000000000000000000000613267565b6001600160a01b038091165f52600e825260405f205416604051908152f35b600435906001600160a01b038216820361115d57565b60031960a091011261115d576001600160a01b03600435818116810361115d5791602435916044359160643591608435908116810361115d5790565b9081518082526020808093019301915f5b828110613044575050505090565b835185529381019392810192600101613036565b8181029291811591840414171561047457565b8115613075570490565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601260045260245ffd5b9190820391821161047457565b67ffffffffffffffff81116106e357604052565b6060810190811067ffffffffffffffff8211176106e357604052565b90601f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0910116810190811067ffffffffffffffff8211176106e357604052565b9081602091031261115d57516001600160a01b038116810361115d5790565b906028820180921161047457565b9190820180921161047457565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060208201927f00000000000000000000000000000000000000000000000000000000000000008452166040820152602481526131b9816130c3565b51902090565b67ffffffffffffffff81116106e35760051b60200190565b906131e1826131bf565b6131ee60405191826130df565b8281527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe061321c82946131bf565b0190602036910137565b805182101561323a5760209160051b010190565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52603260045260245ffd5b6001600160a01b0316300361327857565b7f9fd25b36000000000000000000000000000000000000000000000000000000005f5260045ffd5b61332a60206132d17fffffffff000000000000000000000000000000000000000000000000000000005f351661315a565b6009546040517f9be2a88400000000000000000000000000000000000000000000000000000000815260048101929092523360248301523060448301529092839160081c6001600160a01b031690829081906064820190565b03915afa908115611899575f9161336c575b501561334457565b7f23dada53000000000000000000000000000000000000000000000000000000005f5260045ffd5b61338e915060203d602011613394575b61338681836130df565b810190613793565b5f61333c565b503d61337c565b63ffffffff7f000000000000000000000000000000000000000000000000000000000000000016421115806133cd5790565b506001600754811c1690565b7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00805c613406576001905d565b7f3ee5aeb5000000000000000000000000000000000000000000000000000000005f5260045ffd5b6001600160a01b0316805f525f602052600160405f2054161561344e5750565b7f9e51bd5c000000000000000000000000000000000000000000000000000000005f5260045260245ffd5b7f00000000000000000000000000000000000000000000000000000000000000005c156134a257565b7fc09ba736000000000000000000000000000000000000000000000000000000005f5260045ffd5b6001600160a01b0380911690815f52600e60205260405f205416156134ec5750565b7f85f41299000000000000000000000000000000000000000000000000000000005f5260045260245ffd5b90613521906137c7565b907f8000000000000000000000000000000000000000000000000000000000000000821461047457613555915f039061381c565b565b6fffffffffffffffffffffffffffffffff8082119081156135af575b50613587576135849160801b61314d565b90565b7f89560ca1000000000000000000000000000000000000000000000000000000005f5260045ffd5b905082115f613573565b600160075460021c166135c857565b7f0f27df09000000000000000000000000000000000000000000000000000000005f5260045ffd5b61271081106135fc5750565b7f34bdbfaa000000000000000000000000000000000000000000000000000000005f5260045260245ffd5b91906001600160a01b038091169283156136b3577fd66f031d33381c6408f0b32c884461e5de3df8808399b6f3a3d86b1368f8ec34916020911692835f52600d82526136778160405f205461314d565b613680816135f0565b845f52600d835260405f2055600c825260405f20855f52825260405f206136a882825461314d565b9055604051908152a3565b7fdbe6b10e000000000000000000000000000000000000000000000000000000005f5260045ffd5b60017ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe60075416176007557fbd204090fd387f08e3076528bf09b4fc99d8100d749eace96c06002d3fedc6255f80a1565b821561376b5760019161373e91613058565b917fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff830104019015150290565b7f0a0c22c7000000000000000000000000000000000000000000000000000000005f5260045ffd5b9081602091031261115d5751801515810361115d5790565b906001600160a01b031633146137c45761355590613997565b50565b7f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff81116137f15790565b7f24775e06000000000000000000000000000000000000000000000000000000005f5260045260245ffd5b811561390a576001600160a01b037f00000000000000000000000000000000000000000000000000000000000000009116805f528160205260405f205c8381019384125f821290801582169115161761047457836138d157507f0000000000000000000000000000000000000000000000000000000000000000805c907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8201918211610474575d5b5f5260205260405f205d565b6138c5577f0000000000000000000000000000000000000000000000000000000000000000805c9060018201809211610474575d6138c5565b5050565b908060181c6110bf57602a1b9062ffffff602a1b19161790565b7fb4120f14000000000000000000000000000000000000000000000000000000005f5260045ffd5b906101008084101561392857838103908111610474578060ff105f14613992575060ff5b601811613928578060181c6110bf5762ffffff90831b921b19161790565b613974565b602061332a916139c97fffffffff000000000000000000000000000000000000000000000000000000005f351661315a565b6001600160a01b0360095460081c16906040518095819482937f9be2a884000000000000000000000000000000000000000000000000000000008452339060048501916040919493606084019584526001600160a01b03809216602085015216910152565b60021115613a3857565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602160045260245ffd5b62ffffff9060421c1664174876e800908181029181830414901517156104745790565b91906080670de0b6b3a7640000613ad8613ae19480613aab8660608a0151613226565b52613ad3613abd8660c08a0151613226565b51613acc8760a08b0151613226565b5192613058565b613058565b04930151613226565b5256fea26469706673582212204cb355f6ec307339d854be3d30d9ab78b44dfeae01a9e21be08cbe08dce277b464736f6c634300081a0033000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba900000000000000000000000000000000000000000000000000000000076a70000000000000000000000000000000000000000000000000000000000000ed4e0000000000000000000000000000000000000000000000000000000000000f42400000000000000000000000000000000000000000000000000000000000002710
Deployed Bytecode
0x6080604052600436101561009f575b361561007757346100775760646040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152600f60248201527f4e6f7420696d706c656d656e74656400000000000000000000000000000000006044820152fd5b7ff2238896000000000000000000000000000000000000000000000000000000005f5260045ffd5b5f803560e01c80630387587d14612f70578063058a628f14612eae578063098401f514612e615780630b7562be14612d9657806320c1fb7a14612d5657806326a8a99114612d3a5780632d77138914612c695780632e42f4d514612c4e5780634021fe0f14612bd457806353956aa214612b9a57806355aca1ec146129f457806355cba7fe146129a65780635dcacd64146124945780635e0b06f4146123ce578063653eb3b014612053578063821440f214611fde578063851c1bb314611f8c57806385c8c01514611eea5780638a8d123a14611ea95780638f4ab9ca14611bff5780639385e39a14611b855780639e0879c214611a5f578063a8175b2714611a43578063b9212b49146119ac578063bffb78b214611310578063cd51c12f146112cf578063d0965a6b146112b1578063d15126ba14610eef578063d2c725e014610eb0578063dc3f574e14610cfa578063de1a36a614610cad578063e085c5a814610c12578063e0d5560514610b4f578063e253670a146109f3578063e2a92b1a1461074b578063e2cb0ba014610710578063ebc7955c146104a1578063f21c38cd14610309578063f2784e07146102a85763fbfa77cf14610262575061000e565b346102a557806003193601126102a55760206040516001600160a01b037f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9168152f35b80fd5b50346102a55760206003193601126102a55760406020916001600160a01b036102cf612fd3565b6102f87f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b168152600d83522054604051908152f35b50346102a55760206003193601126102a557610323612fd3565b61034c7f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b6103558161342e565b6103796001600160a01b039182811692835f52600160205260405f205416906137ab565b805f525f60205260405f205460018160021c1663ffffffff80836103a361039e605a90565b61313f565b1c168261043b575b50501561040f5760207f57e20448028297190122571be7cb6c1b1ef85730c673f7c72f533c8662419aa7917ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb5f91858352828452166040822055604051908152a280f35b507ffdcd6894000000000000000000000000000000000000000000000000000000005f5260045260245ffd5b908092507f0000000000000000000000000000000000000000000000000000000000ed4e00160181811161047457164211155f806103ab565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b50346102a55760806003193601126102a5576104bb612fd3565b7f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96104e581613267565b6001600160a01b03906040519260209280848601927f5dcacd640000000000000000000000000000000000000000000000000000000084521660248601526024356044860152604435606486015260643560848601523360a486015260a4855260e085019067ffffffffffffffff92868310848411176106e357601f948389809460448b8496856040527f48c894910000000000000000000000000000000000000000000000000000000086528c60e4830152848251918261010485015282610124918286015e83830101527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09b018b168101030193165af19485156106d8578695610618575b5050505060408280518101031261061457604080935081830151920151908351928352820152f35b8280fd5b9091929394503d8087843e61062d81846130df565b810191858260e085019403126106d45751908382116106d457018160ff820112156106d05760e0810151916101009383116106a357908693929161067b8760405197601f86011601876130df565b82865283838301011161069f5785928291018386015e83010152905f8080806105ec565b8380fd5b6024877f4e487b710000000000000000000000000000000000000000000000000000000081526041600452fd5b8580fd5b8680fd5b6040513d88823e3d90fd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b50346102a557806003193601126102a55760206040517f00000000000000000000000000000000000000000000000000000000000f42408152f35b50346102a55761075a36612fe9565b61078895919294957f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b610790613479565b6107986135b9565b6107a1866134ca565b6107a96133d9565b6001600160a01b0392838716916040517f38d52e0f0000000000000000000000000000000000000000000000000000000081526020958682600481885afa9182156106d85786926109c4575b50848652600e875280604087205416911680910361099457838552600b8652604085205497600d87526040862054916fffffffffffffffffffffffffffffffff8a169161085261084685888661372c565b9b60801c94878661372c565b99808c116109605750808a1161092c5750936108fd936108cd60409c946108c78c6108c18f6108df986108bc8e6108b37f75c4dc5f23640eeba7d404d9165f515fc3d9e23a5c8b6e2d09b4b9da56ff00a99f6108ad866137c7565b9061381c565b6108ad866137c7565b61314d565b9261314d565b90613557565b93878952600b8a52848d8a2055613627565b8851918291888a846040919493926060820195825260208201520152565b0390a27f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005d8351928352820152f35b876064918b897f8eda85e4000000000000000000000000000000000000000000000000000000008452600452602452604452fd5b886064918d857f8eda85e4000000000000000000000000000000000000000000000000000000008452600452602452604452fd5b84604491857f36b18d09000000000000000000000000000000000000000000000000000000008352600452602452fd5b6109e5919250873d89116109ec575b6109dd81836130df565b810190613120565b905f6107f5565b503d6109d3565b50346102a55760406003193601126102a557610a0d612fd3565b60243590610a3a7f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b610a438161342e565b670de0b6b3a76400008211610b27576001600160a01b039081600a54163303610aff571690818352826020526040832054670de0b5cad2bef0008211610ad7577f606eb97d83164bd6b200d638cd49c14c65d94d4f2c674cfd85e24e0e202c3ca591610ac2602092610ab3604290565b9064174876e800840490613950565b8486528583526040862055604051908152a280f35b6004847f746e5940000000000000000000000000000000000000000000000000000000008152fd5b6004847f23dada53000000000000000000000000000000000000000000000000000000008152fd5b6004837f4c69ac5d000000000000000000000000000000000000000000000000000000008152fd5b50346102a557806003193601126102a557610b897f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b610b916132a0565b60ff60095416610bea577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe600754166007557f91d7478835f2b5adc315f5aad920f4a7f0a02f7fddf3042d17b2c80168ea17f58180a180f35b807f069f8cbc0000000000000000000000000000000000000000000000000000000060049252fd5b50346102a557806003193601126102a557610c4c7f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b610c546132a0565b60047ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb60075416176007557f300c7ca619eb846386aa0a6e5916ac2a41406448b0a2e99ba9ccafeb899015a5602060405160018152a180f35b50346102a557806003193601126102a557610ce77f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b610cef6132a0565b610cf76136db565b80f35b50346102a55760206003193601126102a5576001600160a01b03610d1c612fd3565b610d457f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b610d4e8161342e565b16808252816020526001604083205460031c16610e855780825281602052604082205460018160021c169063ffffffff8091610d8b61039e605a90565b1c1682610e1f575b50501580610e10575b610e03575b805f525f60205260405f2060087ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff78254161790557fc2354cc2f78ea57777e55ddd43a7f22b112ce98868596880edaeb22b4f9c73a9602060405160018152a280f35b610e0b6132a0565b610da1565b50610e1961339b565b15610d9c565b908092507f0000000000000000000000000000000000000000000000000000000000ed4e001601818111610e5857164211155f80610d93565b6024847f4e487b710000000000000000000000000000000000000000000000000000000081526011600452fd5b7f346d7607000000000000000000000000000000000000000000000000000000008252600452602490fd5b50346102a557806003193601126102a55760207f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005c6040519015158152f35b50346102a55760406003193601126102a557610f09612fd3565b602435610f357f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b610f3e8261342e565b6001600160a01b0380831692838552602091600183526001604087200154169081155f1461128257610f709150613997565b610f7861339b565b61125a57828452838152604084205460018160021c169063ffffffff8091610fa161039e605a90565b1c16826111f4575b50506111c8576040517fce20ece70000000000000000000000000000000000000000000000000000000081528181600481875afa90811561116857859161119b575b508210611173576040517f654cf15d0000000000000000000000000000000000000000000000000000000081528181600481875afa908115611168578591611137575b50821161110f57828452838152604084205491670de0b5cad2bef00081116110e75764174876e8008104928360181c6110bf577ffffffffffffffffffffffffffffffffffffffffffffffffffffffc000003ffff7f89d41522342fabac1471ca6073a5623e5caf367b03ca6e9a001478d0cf8be4a19486885287855260121b9116176040862055604051908152a280f35b7fe4337c05000000000000000000000000000000000000000000000000000000005f5260045ffd5b6004857f746e5940000000000000000000000000000000000000000000000000000000008152fd5b6004847f7f47834b000000000000000000000000000000000000000000000000000000008152fd5b90508181813d8311611161575b61114e81836130df565b8101031261115d57515f61102e565b5f80fd5b503d611144565b6040513d87823e3d90fd5b6004847fbfb20688000000000000000000000000000000000000000000000000000000008152fd5b90508181813d83116111c1575b6111b281836130df565b8101031261115d57515f610feb565b503d6111a8565b602484847fd971f597000000000000000000000000000000000000000000000000000000008252600452fd5b908092507f0000000000000000000000000000000000000000000000000000000000ed4e00160181811161122d57164211155f80610fa9565b6024867f4e487b710000000000000000000000000000000000000000000000000000000081526011600452fd5b6004847fda9f8b34000000000000000000000000000000000000000000000000000000008152fd5b503314610f70576004847f23dada53000000000000000000000000000000000000000000000000000000008152fd5b50346102a557806003193601126102a5576020604051620f42408152f35b50346102a557806003193601126102a557602060405163ffffffff7f000000000000000000000000000000000000000000000000000000006fb3ee6a168152f35b50346102a55760206003193601126102a55761132a612fd3565b906113547f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b61135d8261342e565b6113656132a0565b6001600160a01b0382168152806020526001604082205460031c16156119785761138d6133d9565b6040519160e0830183811067ffffffffffffffff8211176106e3576040525f8352606060208401526060604084015260608084015260606080840152606060a0840152606060c08401526001600160a01b0381165f52600560205260405f205f60205260405f205490600460205260405f2090600360205260405f2080549387526040519060208286815201905f528160205f20915f5b8781106119555750611438925003826130df565b6020870152611446836131bf565b61145360405191826130df565b8381527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0611480856131bf565b015f5b81811061192c575050604087015261149a836131d7565b60608701526114a8836131d7565b6080870152855164ffffffffff6114be856131d7565b91605a1c165f5b8581106118f057505060c08701526114dc836131d7565b60a0870152855191600183811c1692836118dc575b50826118cc575b5f5b8481106115ee578787876001600160a01b0381165f52600560205260405f20905f5b60608501518051821015611562579061155061153a82600194613226565b516115498360808a0151613226565b5190613557565b815f528460205260405f20550161151c565b84835f7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005d6001600160a01b03165f8181526020818152604080832080547ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7169055519182527fc2354cc2f78ea57777e55ddd43a7f22b112ce98868596880edaeb22b4f9c73a991a280f35b6001600160a01b036116048260208b0151613226565b51165f528160205260405f20906040519161161e836130c3565b5460ff9081811661162e81613a2e565b84526001600160a01b038160081c16602085015260a81c1615156040830152805f528360205260405f2054916116748260408c01518361166e8383613226565b52613226565b50805161168081613a2e565b61168981613a2e565b806118055750670de0b6b3a76400005b6116a78360a08d0151613226565b526116c56fffffffffffffffffffffffffffffffff8416838c613a88565b85156117fb576040810151151590816117dd575b506116ea575b600191505b016114fa565b886116f58151613a65565b611703836060840151613226565b519360801c6117178460805f950151613226565b5181811161174e575b50505060019281611733575b50506116df565b61174791611740916130a2565b828b613a88565b5f8061172c565b611759935003613058565b91670de0b6b3a7640000926001847fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff83010401901515026117b88b6117b18560a06117a88260c0860151613226565b51930151613226565b5190613058565b8185810204851482151715610474576001946117d4920261306b565b90925f80611720565b60019150516117eb81613a2e565b6117f481613a2e565b145f6116d9565b50600191506116e4565b80611811600192613a2e565b036118a457600460206001600160a01b038184015116604051928380927f679aefce0000000000000000000000000000000000000000000000000000000082525afa908115611899575f91611867575b50611699565b90506020813d602011611891575b81611882602093836130df565b8101031261115d57515f611861565b3d9150611875565b6040513d5f823e3d90fd5b7fdf450632000000000000000000000000000000000000000000000000000000005f5260045ffd5b865160031c6001161592506114f8565b6118e7919350613a65565b1515915f6114f1565b60058102908082046005148115171561047457604d601f84841c161161047457601f836001931c16600a0a6119258286613226565b52016114c5565b60209060405161193b816130c3565b5f81525f838201525f604082015282828601015201611483565b91506001602081926001600160a01b038654168152019301910191839192611424565b6001600160a01b036024927fef029adf00000000000000000000000000000000000000000000000000000000835216600452fd5b50346102a557806003193601126102a5576119e67f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b6119ee6132a0565b7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb600754166007557f300c7ca619eb846386aa0a6e5916ac2a41406448b0a2e99ba9ccafeb899015a560206040515f8152a180f35b50346102a557806003193601126102a557602060405160028152f35b50346102a557806003193601126102a557611a997f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b611aa16132a0565b611aa961339b565b15611ad6577fda9f8b34000000000000000000000000000000000000000000000000000000005f5260045ffd5b63ffffffff7f000000000000000000000000000000000000000000000000000000006ec6a06a16421015611b5d5760027ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd60075416176007557fe0629fe656e45ad7fd63a24b899da368690024c07043b88e57aee5095b1d3d02602060405160018152a180f35b7f0e4460b7000000000000000000000000000000000000000000000000000000005f5260045ffd5b50346102a55760406003193601126102a557611b9f612fd3565b602435916001600160a01b0380841680940361115d57604092611be17f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b168152600c60205220905f52602052602060405f2054604051908152f35b50346102a557602080600319360112611ea557611c1a612fd3565b907f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9611c4581613267565b611c4d613479565b6001600160a01b039283600a54163303611e7d57938084602487611c71839961342e565b60405197889384927fca4f28030000000000000000000000000000000000000000000000000000000084521696876004840152165afa938415611e70578194611dd0575b50835192611ccb611cc5856131d7565b946131d7565b90825b8651811015611da2578088611ce56001938a613226565b511686865260069081855260408720815f5285526fffffffffffffffffffffffffffffffff60405f20548060801c611d1d868a613226565b5216611d29848b613226565b52611d34838a613226565b5115801590611d8f575b611d4b575b505001611cce565b611d8891888852855260408720815f5285528660405f2055611d82611d70848b613226565b51611d7b8589613226565b519061314d565b90613517565b5f80611d43565b50611d9a8387613226565b511515611d3e565b611dbf86611dcc8486604051948594604086526040860190613025565b9184830390850152613025565b0390f35b9093503d8085833e611de281836130df565b8101908381830312611e6c5780519067ffffffffffffffff82116106d057019080601f83011215611e6c578151611e18816131bf565b92611e2660405194856130df565b818452858085019260051b8201019283116106d4578501905b828210611e5057505050925f611cb5565b81518881168103611e68578152908501908501611e3f565b8780fd5b8480fd5b50604051903d90823e3d90fd5b6004857f23dada53000000000000000000000000000000000000000000000000000000008152fd5b5080fd5b50346102a557806003193601126102a557602060405163ffffffff7f000000000000000000000000000000000000000000000000000000006ec6a06a168152f35b50346102a557806003193601126102a557611f247f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b6060611f2e61339b565b604051901515815263ffffffff807f000000000000000000000000000000000000000000000000000000006ec6a06a1660208301527f000000000000000000000000000000000000000000000000000000006fb3ee6a166040820152f35b50346102a55760206003193601126102a557600435907fffffffff00000000000000000000000000000000000000000000000000000000821682036102a5576020611fd68361315a565b604051908152f35b50346102a557806003193601126102a5576120187f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b6120206132a0565b60017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff006009541617600955610cf76136db565b50346102a55761206236612fe9565b94926120909291927f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b612098613479565b6120a06135b9565b6120a86133d9565b6001600160a01b039586861693848652602097600e8952806040882054166123a2576040517f38d52e0f00000000000000000000000000000000000000000000000000000000815289816004818a5afa90811561234e578891612385575b501680156123595761214e90868852600e8a5260408820817fffffffffffffffffffffffff00000000000000000000000000000000000000008254161790556108ad856137c7565b61216061215a856137c7565b8661381c565b61216a8484613557565b91858752600b89528260408820556040517f4cdad50600000000000000000000000000000000000000000000000000000000815285600482015289816024818a5afa801561234e578590899061231d575b6121c5925061314d565b976121cf896135f0565b7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd8f089019889116122f05761225291899189897fd66f031d33381c6408f0b32c884461e5de3df8808399b6f3a3d86b1368f8ec348e828452600d8152612710806040862055600c8252604085205f805282528060405f2055604051908152a3613627565b8087106122c05750604080519283526020830193909352918101919091527f75c4dc5f23640eeba7d404d9165f515fc3d9e23a5c8b6e2d09b4b9da56ff00a990606090a27f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005d604051908152f35b85604491887fda0cb07e000000000000000000000000000000000000000000000000000000008352600452602452fd5b6024887f4e487b710000000000000000000000000000000000000000000000000000000081526011600452fd5b50508981813d8311612347575b61233481836130df565b8101031261115d57846121c591516121bb565b503d61232a565b6040513d8a823e3d90fd5b602487877fd407f9c5000000000000000000000000000000000000000000000000000000008252600452fd5b61239c91508a3d8c116109ec576109dd81836130df565b5f612106565b602487877f1690fa40000000000000000000000000000000000000000000000000000000008252600452fd5b50346102a55760406003193601126102a5576123e8612fd3565b602435906124157f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b61241e8161342e565b670de0b6b3a76400008211610b27576001600160a01b039081600a54163303610aff571690818352826020526040832054670de0b5cad2bef0008211610ad75781610ac260209264174876e8007fe4d371097beea42453a37406e2aef4c04f3c548f84ac50e72578662c0dcd735495049061390e565b503461115d576124a336612fe9565b9490929193947f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96124d381613267565b6001600160a01b038116330361297a576124eb613479565b6124f4876134ca565b3215808061296d575b612905575b506001600160a01b0387165f52600c60205260405f206001600160a01b0383165f5260205260405f205484116128dd576001600160a01b0387165f52600b60205260405f205495600d60205260405f20549661258a61257c89612577896fffffffffffffffffffffffffffffffff8616613058565b61306b565b98612577888460801c613058565b966001600160a01b038a165f52600e6020526001600160a01b0360405f20541692808a106128aa575080881061286e5750612605906125c98984613517565b6125dc886001600160a01b038c16613517565b6108c7886125fc8b6fffffffffffffffffffffffffffffffff85166130a2565b9260801c6130a2565b946001600160a01b0389165f52600b6020528560405f20556001600160a01b03841615612846576001600160a01b0389165f52600d60205261264b8160405f20546130a2565b612654816135f0565b6001600160a01b038a165f52600d60205260405f2055600c60205260405f206001600160a01b0385165f5260205260405f206126918282546130a2565b90556040519081526001600160a01b038416907f4e09f7f7fc37ce2897800e2c2a9099565edb0a133d19d84a6871b3530af8846b60206001600160a01b038c1692a3866127c5575b5084612735575b5050604080518581526020810185905280820193909352946001600160a01b0316917f44d97b36e99b590b3d2875aad3b167b1d7fb1e063f3f1325a1eeac76caee51139150606090a282519182526020820152f35b6001600160a01b0381163b15610614576001600160a01b03606488858381958160405198899788967fae6393290000000000000000000000000000000000000000000000000000000088521660048701521660248501528a6044850152165af180156127ba576127a6575b806126e0565b6127b082916130af565b6102a557806127a0565b6040513d84823e3d90fd5b6001600160a01b0382163b1561115d57604051907fae63932900000000000000000000000000000000000000000000000000000000825260048201526001600160a01b03831660248201528660448201525f81606481836001600160a01b0387165af1801561189957156126d95761283e9193506130af565b5f915f6126d9565b7f586d06df000000000000000000000000000000000000000000000000000000005f5260045ffd5b876001600160a01b038b7f8eda85e4000000000000000000000000000000000000000000000000000000005f521660045260245260445260645ffd5b89847f8eda85e4000000000000000000000000000000000000000000000000000000005f5260045260245260445260645ffd5b7f98c5dbd6000000000000000000000000000000000000000000000000000000005f5260045ffd5b15612945576001600160a01b0387165f52600c60205260405f206001600160a01b0383165f5260205260405f2061293d85825461314d565b90555f612502565b7f67f84ab2000000000000000000000000000000000000000000000000000000005f5260045ffd5b50600160075416156124fd565b7f089676d5000000000000000000000000000000000000000000000000000000005f523360045260245ffd5b3461115d575f60031936011261115d576129df7f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b6020600160075460021c166040519015158152f35b3461115d57602060031936011261115d57612a0d612fd3565b612a367f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b612a3f8161342e565b612a636001600160a01b039182811692835f52600160205260405f205416906137ab565b805f525f60205260405f205460018160021c1663ffffffff908183612a8961039e605a90565b1c1681612b62575b5015612ac357827fd971f597000000000000000000000000000000000000000000000000000000005f5260045260245ffd5b81612ace605a61313f565b1c16421015612b36577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb600491835f525f602052161760405f20557f57e20448028297190122571be7cb6c1b1ef85730c673f7c72f533c8662419aa7602060405160018152a2005b507feb5a1217000000000000000000000000000000000000000000000000000000005f5260045260245ffd5b9050817f0000000000000000000000000000000000000000000000000000000000ed4e00160181811161047457811642111584612a91565b3461115d575f60031936011261115d5760206040517f00000000000000000000000000000000000000000000000000000000000027108152f35b3461115d57602060031936011261115d576001600160a01b03612bf5612fd3565b612c1e7f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b165f52600b6020526040805f20548151906fffffffffffffffffffffffffffffffff8116825260801c6020820152f35b3461115d575f60031936011261115d57602060405160088152f35b3461115d57602060031936011261115d576004356001600160a01b03811680910361115d57612cb77f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b612cbf6132a0565b612cc76133d9565b807fffffffffffffffffffffffff0000000000000000000000000000000000000000600a541617600a557f280a60b1e63c1774d397d35cce80eb80e51408ead755fb446e6f744ce98e5df05f80a25f7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f005d005b3461115d575f60031936011261115d5760206040516127108152f35b3461115d575f60031936011261115d57602060405163ffffffff7f0000000000000000000000000000000000000000000000000000000000ed4e00168152f35b3461115d575f60031936011261115d57612dcf7f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b612dd76132a0565b612ddf61339b565b15612e39577fe0629fe656e45ad7fd63a24b899da368690024c07043b88e57aee5095b1d3d0260205f7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd60075416600755604051908152a1005b7ff7ff4dca000000000000000000000000000000000000000000000000000000005f5260045ffd5b3461115d575f60031936011261115d57612e9a7f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b6020612ea461339b565b6040519015158152f35b3461115d57602060031936011261115d576004356001600160a01b0381169081810361115d57612efd7f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b612f056132a0565b7fffffffffffffffffffffff0000000000000000000000000000000000000000ff74ffffffffffffffffffffffffffffffffffffffff006009549260081b169116176009557f94b979b6831a51293e2641426f97747feed46f17779fed9cd18d1ecefcfe92ef5f80a2005b3461115d57602060031936011261115d576020612f8b612fd3565b612fb47f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9613267565b6001600160a01b038091165f52600e825260405f205416604051908152f35b600435906001600160a01b038216820361115d57565b60031960a091011261115d576001600160a01b03600435818116810361115d5791602435916044359160643591608435908116810361115d5790565b9081518082526020808093019301915f5b828110613044575050505090565b835185529381019392810192600101613036565b8181029291811591840414171561047457565b8115613075570490565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601260045260245ffd5b9190820391821161047457565b67ffffffffffffffff81116106e357604052565b6060810190811067ffffffffffffffff8211176106e357604052565b90601f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0910116810190811067ffffffffffffffff8211176106e357604052565b9081602091031261115d57516001600160a01b038116810361115d5790565b906028820180921161047457565b9190820180921161047457565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060208201927f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba98452166040820152602481526131b9816130c3565b51902090565b67ffffffffffffffff81116106e35760051b60200190565b906131e1826131bf565b6131ee60405191826130df565b8281527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe061321c82946131bf565b0190602036910137565b805182101561323a5760209160051b010190565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52603260045260245ffd5b6001600160a01b0316300361327857565b7f9fd25b36000000000000000000000000000000000000000000000000000000005f5260045ffd5b61332a60206132d17fffffffff000000000000000000000000000000000000000000000000000000005f351661315a565b6009546040517f9be2a88400000000000000000000000000000000000000000000000000000000815260048101929092523360248301523060448301529092839160081c6001600160a01b031690829081906064820190565b03915afa908115611899575f9161336c575b501561334457565b7f23dada53000000000000000000000000000000000000000000000000000000005f5260045ffd5b61338e915060203d602011613394575b61338681836130df565b810190613793565b5f61333c565b503d61337c565b63ffffffff7f000000000000000000000000000000000000000000000000000000006fb3ee6a16421115806133cd5790565b506001600754811c1690565b7f9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00805c613406576001905d565b7f3ee5aeb5000000000000000000000000000000000000000000000000000000005f5260045ffd5b6001600160a01b0316805f525f602052600160405f2054161561344e5750565b7f9e51bd5c000000000000000000000000000000000000000000000000000000005f5260045260245ffd5b7f1369d017453f080f2416efe5ae39c8a4b4655ea0634227aaab0afdb9a9f93f005c156134a257565b7fc09ba736000000000000000000000000000000000000000000000000000000005f5260045ffd5b6001600160a01b0380911690815f52600e60205260405f205416156134ec5750565b7f85f41299000000000000000000000000000000000000000000000000000000005f5260045260245ffd5b90613521906137c7565b907f8000000000000000000000000000000000000000000000000000000000000000821461047457613555915f039061381c565b565b6fffffffffffffffffffffffffffffffff8082119081156135af575b50613587576135849160801b61314d565b90565b7f89560ca1000000000000000000000000000000000000000000000000000000005f5260045ffd5b905082115f613573565b600160075460021c166135c857565b7f0f27df09000000000000000000000000000000000000000000000000000000005f5260045ffd5b61271081106135fc5750565b7f34bdbfaa000000000000000000000000000000000000000000000000000000005f5260045260245ffd5b91906001600160a01b038091169283156136b3577fd66f031d33381c6408f0b32c884461e5de3df8808399b6f3a3d86b1368f8ec34916020911692835f52600d82526136778160405f205461314d565b613680816135f0565b845f52600d835260405f2055600c825260405f20855f52825260405f206136a882825461314d565b9055604051908152a3565b7fdbe6b10e000000000000000000000000000000000000000000000000000000005f5260045ffd5b60017ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe60075416176007557fbd204090fd387f08e3076528bf09b4fc99d8100d749eace96c06002d3fedc6255f80a1565b821561376b5760019161373e91613058565b917fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff830104019015150290565b7f0a0c22c7000000000000000000000000000000000000000000000000000000005f5260045ffd5b9081602091031261115d5751801515810361115d5790565b906001600160a01b031633146137c45761355590613997565b50565b7f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff81116137f15790565b7f24775e06000000000000000000000000000000000000000000000000000000005f5260045260245ffd5b811561390a576001600160a01b037ff74f46243717369ff9f20877dfc1ba8491e6be48bfe7acc5b65f5ac68f585c009116805f528160205260405f205c8381019384125f821290801582169115161761047457836138d157507fbcbf50c510014a975eac30806436734486f167c41af035c1645353d475d57100805c907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8201918211610474575d5b5f5260205260405f205d565b6138c5577fbcbf50c510014a975eac30806436734486f167c41af035c1645353d475d57100805c9060018201809211610474575d6138c5565b5050565b908060181c6110bf57602a1b9062ffffff602a1b19161790565b7fb4120f14000000000000000000000000000000000000000000000000000000005f5260045ffd5b906101008084101561392857838103908111610474578060ff105f14613992575060ff5b601811613928578060181c6110bf5762ffffff90831b921b19161790565b613974565b602061332a916139c97fffffffff000000000000000000000000000000000000000000000000000000005f351661315a565b6001600160a01b0360095460081c16906040518095819482937f9be2a884000000000000000000000000000000000000000000000000000000008452339060048501916040919493606084019584526001600160a01b03809216602085015216910152565b60021115613a3857565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602160045260245ffd5b62ffffff9060421c1664174876e800908181029181830414901517156104745790565b91906080670de0b6b3a7640000613ad8613ae19480613aab8660608a0151613226565b52613ad3613abd8660c08a0151613226565b51613acc8760a08b0151613226565b5192613058565b613058565b04930151613226565b5256fea26469706673582212204cb355f6ec307339d854be3d30d9ab78b44dfeae01a9e21be08cbe08dce277b464736f6c634300081a0033
Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)
000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba900000000000000000000000000000000000000000000000000000000076a70000000000000000000000000000000000000000000000000000000000000ed4e0000000000000000000000000000000000000000000000000000000000000f42400000000000000000000000000000000000000000000000000000000000002710
-----Decoded View---------------
Arg [0] : mainVault (address): 0xbA1333333333a1BA1108E8412f11850A5C319bA9
Arg [1] : pauseWindowDuration (uint32): 124416000
Arg [2] : bufferPeriodDuration (uint32): 15552000
Arg [3] : minTradeAmount (uint256): 1000000
Arg [4] : minWrapAmount (uint256): 10000
-----Encoded View---------------
5 Constructor Arguments found :
Arg [0] : 000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9
Arg [1] : 00000000000000000000000000000000000000000000000000000000076a7000
Arg [2] : 0000000000000000000000000000000000000000000000000000000000ed4e00
Arg [3] : 00000000000000000000000000000000000000000000000000000000000f4240
Arg [4] : 0000000000000000000000000000000000000000000000000000000000002710
Loading...
Loading
Loading...
Loading
Multichain Portfolio | 30 Chains
Chain | Token | Portfolio % | Price | Amount | Value |
---|
[ 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.