Feature Tip: Add private address tag to any address under My Name Tag !
Source Code
Overview
ETH Balance
0 ETH
Eth Value
$0.00| Transaction Hash |
Method
|
Block
|
From
|
To
|
|||||
|---|---|---|---|---|---|---|---|---|---|
Latest 1 internal transaction
Advanced mode:
| Parent Transaction Hash | Method | Block |
From
|
To
|
|||
|---|---|---|---|---|---|---|---|
| 0x61030060 | 22832234 | 118 days ago | Contract Creation | 0 ETH |
Cross-Chain Transactions
Loading...
Loading
Contract Source Code Verified (Exact Match)
Contract Name:
ReClammPool
Compiler Version
v0.8.27+commit.40a35a09
Optimization Enabled:
Yes with 700 runs
Other Settings:
cancun EvmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: GPL-3.0-or-later
// solhint-disable not-rely-on-time
pragma solidity ^0.8.24;
import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.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 "@balancer-labs/v3-interfaces/contracts/vault/IUnbalancedLiquidityInvariantRatioBounds.sol";
import { IVaultErrors } from "@balancer-labs/v3-interfaces/contracts/vault/IVaultErrors.sol";
import { IBasePool } from "@balancer-labs/v3-interfaces/contracts/vault/IBasePool.sol";
import { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol";
import { IHooks } from "@balancer-labs/v3-interfaces/contracts/vault/IHooks.sol";
import "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol";
import { BasePoolAuthentication } from "@balancer-labs/v3-pool-utils/contracts/BasePoolAuthentication.sol";
import { GradualValueChange } from "@balancer-labs/v3-pool-weighted/contracts/lib/GradualValueChange.sol";
import { ScalingHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/ScalingHelpers.sol";
import { FixedPoint } from "@balancer-labs/v3-solidity-utils/contracts/math/FixedPoint.sol";
import { BalancerPoolToken } from "@balancer-labs/v3-vault/contracts/BalancerPoolToken.sol";
import { Version } from "@balancer-labs/v3-solidity-utils/contracts/helpers/Version.sol";
import { PoolInfo } from "@balancer-labs/v3-pool-utils/contracts/PoolInfo.sol";
import { BaseHooks } from "@balancer-labs/v3-vault/contracts/BaseHooks.sol";
import { PriceRatioState, ReClammMath, a, b } from "./lib/ReClammMath.sol";
import {
ReClammPoolParams,
ReClammPoolDynamicData,
ReClammPoolImmutableData,
IReClammPool
} from "./interfaces/IReClammPool.sol";
contract ReClammPool is IReClammPool, BalancerPoolToken, PoolInfo, BasePoolAuthentication, Version, BaseHooks {
using FixedPoint for uint256;
using ScalingHelpers for uint256;
using SafeCast for *;
using ReClammMath for *;
// Fees are 18-decimal, floating point values, which will be stored in the Vault using 24 bits.
// This means they have 0.00001% resolution (i.e., any non-zero bits < 1e11 will cause precision loss).
// Minimum values help make the math well-behaved (i.e., the swap fee should overwhelm any rounding error).
// Maximum values protect users by preventing permissioned actors from setting excessively high swap fees.
uint256 private constant _MIN_SWAP_FEE_PERCENTAGE = 0.001e16; // 0.001%
uint256 internal constant _MAX_SWAP_FEE_PERCENTAGE = 10e16; // 10%
// The maximum pool centeredness allowed to consider the pool within the target range.
uint256 internal constant _MAX_CENTEREDNESS_MARGIN = 90e16; // 90%
// The daily price shift exponent is a percentage that defines the speed at which the virtual balances will change
// over the course of one day. A value of 100% (i.e, FP 1) means that the min and max prices will double (or halve)
// every day, until the pool price is within the range defined by the margin. This constant defines the maximum
// "price shift" velocity.
uint256 internal constant _MAX_DAILY_PRICE_SHIFT_EXPONENT = 100e16; // 100%
// Price ratio updates must have both a minimum duration and a maximum daily rate. For instance, an update rate of
// FP 2 means the ratio one day later must be at least half and at most double the rate at the start of the update.
uint256 internal constant _MIN_PRICE_RATIO_UPDATE_DURATION = 1 days;
uint256 internal immutable _MAX_DAILY_PRICE_RATIO_UPDATE_RATE;
// There is also a minimum delta, to keep the math well-behaved.
uint256 internal constant _MIN_PRICE_RATIO_DELTA = 1e6;
uint256 internal constant _MAX_TOKEN_DECIMALS = 18;
// This represents the maximum deviation from the ideal state (i.e., at target price and near centered) after
// initialization, to prevent arbitration losses.
uint256 internal constant _BALANCE_RATIO_AND_PRICE_TOLERANCE = 0.01e16; // 0.01%
// These immutables are only used during initialization, to set the virtual balances and price ratio in a more
// user-friendly manner.
uint256 private immutable _INITIAL_MIN_PRICE;
uint256 private immutable _INITIAL_MAX_PRICE;
uint256 private immutable _INITIAL_TARGET_PRICE;
uint256 private immutable _INITIAL_DAILY_PRICE_SHIFT_EXPONENT;
uint256 private immutable _INITIAL_CENTEREDNESS_MARGIN;
// ReClamm pools do not need to know the tokens on deployment. The factory deploys the pool, then registers it, at
// which point the Vault knows the tokens and rate providers. Finally, the user initializes the pool through the
// router, using the `computeInitialBalancesRaw` helper function to compute the correct initial raw balances.
//
// The twist here is that the pool may contain wrapped tokens (e.g., wstETH), and the initial prices given might be
// in terms of either the wrapped or the underlying token. If the price is that of the actual token being supplied
// (e.g., the wrapped token), the initialization helper should *not* apply the rate, and the flag should be false.
// If the price is given in terms of the underlying token, the initialization helper *should* apply the rate, so
// the flag should be true. Since the prices are stored on initialization, these flags are as well (vs. passing
// them in at initialization time, when they might be out-of-sync with the prices).
bool private immutable _TOKEN_A_PRICE_INCLUDES_RATE;
bool private immutable _TOKEN_B_PRICE_INCLUDES_RATE;
PriceRatioState internal _priceRatioState;
// Timestamp of the last user interaction.
uint32 internal _lastTimestamp;
// Internal representation of the speed at which the pool moves the virtual balances when outside the target range.
uint128 internal _dailyPriceShiftBase;
// Used to define the target price range of the pool (i.e., where the pool centeredness >= centeredness margin).
uint64 internal _centerednessMargin;
// The virtual balances at the time of the last user interaction.
uint128 internal _lastVirtualBalanceA;
uint128 internal _lastVirtualBalanceB;
// Protect functions that would otherwise be vulnerable to manipulation through transient liquidity.
modifier onlyWhenVaultIsLocked() {
_ensureVaultIsLocked();
_;
}
function _ensureVaultIsLocked() internal view {
if (_vault.isUnlocked()) {
revert VaultIsNotLocked();
}
}
modifier onlyWhenInitialized() {
_ensureVaultIsInitialized();
_;
}
function _ensureVaultIsInitialized() internal view {
if (_vault.isPoolInitialized(address(this)) == false) {
revert PoolNotInitialized();
}
}
modifier onlyWithinTargetRange() {
_ensurePoolWithinTargetRange();
_;
_ensurePoolWithinTargetRange();
}
constructor(
ReClammPoolParams memory params,
IVault vault
)
BalancerPoolToken(vault, params.name, params.symbol)
PoolInfo(vault)
BasePoolAuthentication(vault, msg.sender)
Version(params.version)
{
if (
params.initialMinPrice == 0 ||
params.initialMaxPrice == 0 ||
params.initialTargetPrice == 0 ||
params.initialTargetPrice < params.initialMinPrice ||
params.initialTargetPrice > params.initialMaxPrice ||
params.initialMinPrice >= params.initialMaxPrice
) {
// If any of these prices were 0, pool initialization would revert with a numerical error.
// For good measure, we also ensure the target is within the range.
revert InvalidInitialPrice();
}
// Initialize immutable params. These are only used during pool initialization.
_INITIAL_MIN_PRICE = params.initialMinPrice;
_INITIAL_MAX_PRICE = params.initialMaxPrice;
_INITIAL_TARGET_PRICE = params.initialTargetPrice;
_INITIAL_DAILY_PRICE_SHIFT_EXPONENT = params.dailyPriceShiftExponent;
_INITIAL_CENTEREDNESS_MARGIN = params.centerednessMargin;
_TOKEN_A_PRICE_INCLUDES_RATE = params.tokenAPriceIncludesRate;
_TOKEN_B_PRICE_INCLUDES_RATE = params.tokenBPriceIncludesRate;
// The maximum daily price ratio change rate is given by 2^_MAX_DAILY_PRICE_SHIFT_EXPONENT.
// This is somewhat arbitrary, but it makes sense to link these rates; i.e., we are setting the maximum speed
// of expansion or contraction to equal the maximum speed of the price shift. It is expressed as a multiple;
// i.e., 8e18 means it can change by 8x per day.
_MAX_DAILY_PRICE_RATIO_UPDATE_RATE = FixedPoint.powUp(2e18, _MAX_DAILY_PRICE_SHIFT_EXPONENT);
}
/********************************************************
Base Pool Functions
********************************************************/
/// @inheritdoc IBasePool
function computeInvariant(uint256[] memory balancesScaled18, Rounding rounding) public view returns (uint256) {
return
ReClammMath.computeInvariant(
balancesScaled18,
_lastVirtualBalanceA,
_lastVirtualBalanceB,
_dailyPriceShiftBase,
_lastTimestamp,
_centerednessMargin,
_priceRatioState,
rounding
);
}
/// @inheritdoc IBasePool
function computeBalance(uint256[] memory, uint256, uint256) external pure returns (uint256) {
// The pool does not allow unbalanced adds and removes, so this function does not need to be implemented.
revert NotImplemented();
}
/// @inheritdoc IBasePool
function onSwap(PoolSwapParams memory request) public virtual onlyVault returns (uint256 amountCalculatedScaled18) {
(uint256 currentVirtualBalanceA, uint256 currentVirtualBalanceB, bool changed) = _computeCurrentVirtualBalances(
request.balancesScaled18
);
if (changed) {
_setLastVirtualBalances(currentVirtualBalanceA, currentVirtualBalanceB);
}
_updateTimestamp();
// Calculate swap result.
if (request.kind == SwapKind.EXACT_IN) {
amountCalculatedScaled18 = ReClammMath.computeOutGivenIn(
request.balancesScaled18,
currentVirtualBalanceA,
currentVirtualBalanceB,
request.indexIn,
request.indexOut,
request.amountGivenScaled18
);
} else {
amountCalculatedScaled18 = ReClammMath.computeInGivenOut(
request.balancesScaled18,
currentVirtualBalanceA,
currentVirtualBalanceB,
request.indexIn,
request.indexOut,
request.amountGivenScaled18
);
}
}
/// @inheritdoc ISwapFeePercentageBounds
function getMinimumSwapFeePercentage() external pure returns (uint256) {
return _MIN_SWAP_FEE_PERCENTAGE;
}
/// @inheritdoc ISwapFeePercentageBounds
function getMaximumSwapFeePercentage() external pure returns (uint256) {
return _MAX_SWAP_FEE_PERCENTAGE;
}
/// @inheritdoc IUnbalancedLiquidityInvariantRatioBounds
function getMinimumInvariantRatio() external pure returns (uint256) {
// The invariant ratio bounds are required by `IBasePool`, but are unused in this pool type, as liquidity can
// only be added or removed proportionally.
return 0;
}
/// @inheritdoc IUnbalancedLiquidityInvariantRatioBounds
function getMaximumInvariantRatio() external pure returns (uint256) {
// The invariant ratio bounds are required by `IBasePool`, but are unused in this pool type, as liquidity can
// only be added or removed proportionally.
return 0;
}
/// @inheritdoc IRateProvider
function getRate() public pure override returns (uint256) {
revert ReClammPoolBptRateUnsupported();
}
/********************************************************
Hook Functions
********************************************************/
/// @inheritdoc IHooks
function getHookFlags() public pure override returns (HookFlags memory hookFlags) {
hookFlags.shouldCallBeforeInitialize = true;
hookFlags.shouldCallBeforeAddLiquidity = true;
hookFlags.shouldCallBeforeRemoveLiquidity = true;
}
/// @inheritdoc IHooks
function onRegister(
address,
address,
TokenConfig[] memory tokenConfig,
LiquidityManagement calldata liquidityManagement
) public pure override returns (bool) {
// This function is `pure`, so it does not need `onlyVault` protection.
return
tokenConfig.length == 2 &&
liquidityManagement.disableUnbalancedLiquidity &&
liquidityManagement.enableDonation == false;
}
struct InitializeLocals {
uint256 rateA;
uint256 rateB;
uint256 minPriceScaled18;
uint256 maxPriceScaled18;
uint256 targetPriceScaled18;
uint256[] theoreticalBalances;
uint256 theoreticalVirtualBalanceA;
uint256 theoreticalVirtualBalanceB;
uint256 priceRatio;
}
/// @inheritdoc IHooks
function onBeforeInitialize(
uint256[] memory balancesScaled18,
bytes memory
) public override onlyVault returns (bool) {
InitializeLocals memory locals;
(locals.rateA, locals.rateB) = _getTokenRates();
(
locals.minPriceScaled18,
locals.maxPriceScaled18,
locals.targetPriceScaled18
) = _getPriceSettingsAdjustedByRates(locals.rateA, locals.rateB);
(
locals.theoreticalBalances,
locals.theoreticalVirtualBalanceA,
locals.theoreticalVirtualBalanceB,
locals.priceRatio
) = ReClammMath.computeTheoreticalPriceRatioAndBalances(
locals.minPriceScaled18,
locals.maxPriceScaled18,
locals.targetPriceScaled18
);
_checkInitializationBalanceRatio(balancesScaled18, locals.theoreticalBalances);
uint256 scale = balancesScaled18[a].divDown(locals.theoreticalBalances[a]);
uint256 virtualBalanceA = locals.theoreticalVirtualBalanceA.mulDown(scale);
uint256 virtualBalanceB = locals.theoreticalVirtualBalanceB.mulDown(scale);
_checkInitializationPrices(
balancesScaled18,
locals.minPriceScaled18,
locals.maxPriceScaled18,
locals.targetPriceScaled18,
virtualBalanceA,
virtualBalanceB
);
_setLastVirtualBalances(virtualBalanceA, virtualBalanceB);
_startPriceRatioUpdate(locals.priceRatio, block.timestamp, block.timestamp);
// Set dynamic parameters.
_setDailyPriceShiftExponent(_INITIAL_DAILY_PRICE_SHIFT_EXPONENT);
_setCenterednessMargin(_INITIAL_CENTEREDNESS_MARGIN);
_updateTimestamp();
return true;
}
/// @inheritdoc IHooks
function onBeforeAddLiquidity(
address,
address pool,
AddLiquidityKind,
uint256[] memory,
uint256 exactBptAmountOut,
uint256[] memory balancesScaled18,
bytes memory
) public override onlyVault returns (bool) {
// This hook makes sure that the virtual balances are increased in the same proportion as the real balances
// after adding liquidity. This is needed to keep the pool centeredness and price ratio constant.
uint256 poolTotalSupply = _vault.totalSupply(pool);
uint256 newPoolTotalSupply = exactBptAmountOut + poolTotalSupply;
(uint256 currentVirtualBalanceA, uint256 currentVirtualBalanceB, ) = _computeCurrentVirtualBalances(
balancesScaled18
);
// When adding/removing liquidity, round down the virtual balances. This favors the vault in swap operations.
// The virtual balances are not used in proportional add/remove calculations.
currentVirtualBalanceA = (currentVirtualBalanceA * newPoolTotalSupply) / poolTotalSupply;
currentVirtualBalanceB = (currentVirtualBalanceB * newPoolTotalSupply) / poolTotalSupply;
_setLastVirtualBalances(currentVirtualBalanceA, currentVirtualBalanceB);
_updateTimestamp();
return true;
}
/// @inheritdoc IHooks
function onBeforeRemoveLiquidity(
address,
address pool,
RemoveLiquidityKind,
uint256 exactBptAmountIn,
uint256[] memory,
uint256[] memory balancesScaled18,
bytes memory
) public override onlyVault returns (bool) {
// This hook makes sure that the virtual balances are decreased in the same proportion as the real balances
// after removing liquidity. This is needed to keep the pool centeredness and price ratio constant.
uint256 poolTotalSupply = _vault.totalSupply(pool);
uint256 bptDelta = poolTotalSupply - exactBptAmountIn;
(uint256 currentVirtualBalanceA, uint256 currentVirtualBalanceB, ) = _computeCurrentVirtualBalances(
balancesScaled18
);
// When adding/removing liquidity, round down the virtual balances. This favors the vault in swap operations.
// The virtual balances are not used in proportional add/remove calculations.
currentVirtualBalanceA = (currentVirtualBalanceA * bptDelta) / poolTotalSupply;
currentVirtualBalanceB = (currentVirtualBalanceB * bptDelta) / poolTotalSupply;
_setLastVirtualBalances(currentVirtualBalanceA, currentVirtualBalanceB);
_updateTimestamp();
return true;
}
/********************************************************
Pool State Getters
********************************************************/
/// @inheritdoc IReClammPool
function computeInitialBalancesRaw(
IERC20 referenceToken,
uint256 referenceAmountInRaw
) external view returns (uint256[] memory initialBalancesRaw) {
IERC20[] memory tokens = _vault.getPoolTokens(address(this));
(uint256 referenceTokenIdx, uint256 otherTokenIdx) = tokens[a] == referenceToken ? (a, b) : (b, a);
if (referenceTokenIdx == b && referenceToken != tokens[b]) {
revert IVaultErrors.InvalidToken();
}
(uint256 rateA, uint256 rateB) = _getTokenRates();
uint256 balanceRatioScaled18 = _computeInitialBalanceRatioScaled18(rateA, rateB);
(uint256 rateReferenceToken, uint256 rateOtherToken) = tokens[a] == referenceToken
? (rateA, rateB)
: (rateB, rateA);
uint8 decimalsReferenceToken = IERC20Metadata(address(tokens[referenceTokenIdx])).decimals();
uint8 decimalsOtherToken = IERC20Metadata(address(tokens[otherTokenIdx])).decimals();
uint256 referenceAmountInScaled18 = referenceAmountInRaw.toScaled18ApplyRateRoundDown(
10 ** (_MAX_TOKEN_DECIMALS - decimalsReferenceToken),
rateReferenceToken
);
// Since the ratio is defined as b/a, multiply if we're given a, and divide if we're given b.
// If the theoretical virtual balances were a=50 and b=100, then the ratio would be 100/50 = 2.
// If we're given 100 a tokens, b = a * 2 = 200. If we're given 200 b tokens, a = b / 2 = 100.
initialBalancesRaw = new uint256[](2);
initialBalancesRaw[referenceTokenIdx] = referenceAmountInRaw;
function(uint256, uint256) pure returns (uint256) _mulOrDiv = referenceTokenIdx == a
? FixedPoint.mulDown
: FixedPoint.divDown;
initialBalancesRaw[otherTokenIdx] = _mulOrDiv(referenceAmountInScaled18, balanceRatioScaled18)
.toRawUndoRateRoundDown(10 ** (_MAX_TOKEN_DECIMALS - decimalsOtherToken), rateOtherToken);
}
/// @inheritdoc IReClammPool
function computeCurrentPriceRange() external view returns (uint256 minPrice, uint256 maxPrice) {
if (_vault.isPoolInitialized(address(this))) {
(, , , uint256[] memory balancesScaled18) = _vault.getPoolTokenInfo(address(this));
(uint256 virtualBalanceA, uint256 virtualBalanceB, ) = _computeCurrentVirtualBalances(balancesScaled18);
(minPrice, maxPrice) = ReClammMath.computePriceRange(balancesScaled18, virtualBalanceA, virtualBalanceB);
} else {
minPrice = _INITIAL_MIN_PRICE;
maxPrice = _INITIAL_MAX_PRICE;
}
}
/// @inheritdoc IReClammPool
function computeCurrentVirtualBalances()
external
view
returns (uint256 currentVirtualBalanceA, uint256 currentVirtualBalanceB, bool changed)
{
(, currentVirtualBalanceA, currentVirtualBalanceB, changed) = _getRealAndVirtualBalances();
}
/// @inheritdoc IReClammPool
function computeCurrentSpotPrice() external view returns (uint256) {
(
uint256[] memory balancesScaled18,
uint256 currentVirtualBalanceA,
uint256 currentVirtualBalanceB,
) = _getRealAndVirtualBalances();
return (balancesScaled18[b] + currentVirtualBalanceB).divDown(balancesScaled18[a] + currentVirtualBalanceA);
}
function _getRealAndVirtualBalances()
internal
view
returns (
uint256[] memory balancesScaled18,
uint256 currentVirtualBalanceA,
uint256 currentVirtualBalanceB,
bool changed
)
{
(, , , balancesScaled18) = _vault.getPoolTokenInfo(address(this));
(currentVirtualBalanceA, currentVirtualBalanceB, changed) = _computeCurrentVirtualBalances(balancesScaled18);
}
/// @inheritdoc IReClammPool
function getLastTimestamp() external view returns (uint32) {
return _lastTimestamp;
}
/// @inheritdoc IReClammPool
function getLastVirtualBalances() external view returns (uint256 virtualBalanceA, uint256 virtualBalanceB) {
return (_lastVirtualBalanceA, _lastVirtualBalanceB);
}
/// @inheritdoc IReClammPool
function getCenterednessMargin() external view returns (uint256) {
return _centerednessMargin;
}
/// @inheritdoc IReClammPool
function getDailyPriceShiftExponent() external view returns (uint256) {
return _dailyPriceShiftBase.toDailyPriceShiftExponent();
}
/// @inheritdoc IReClammPool
function getDailyPriceShiftBase() external view returns (uint256) {
return _dailyPriceShiftBase;
}
/// @inheritdoc IReClammPool
function getPriceRatioState() external view returns (PriceRatioState memory) {
return _priceRatioState;
}
/// @inheritdoc IReClammPool
function computeCurrentFourthRootPriceRatio() external view returns (uint256) {
return ReClammMath.fourthRootScaled18(_computeCurrentPriceRatio());
}
/// @inheritdoc IReClammPool
function computeCurrentPriceRatio() external view returns (uint256) {
return _computeCurrentPriceRatio();
}
/// @inheritdoc IReClammPool
function isPoolWithinTargetRange() external view returns (bool) {
return _isPoolWithinTargetRange();
}
/// @inheritdoc IReClammPool
function isPoolWithinTargetRangeUsingCurrentVirtualBalances()
external
view
returns (bool isWithinTargetRange, bool virtualBalancesChanged)
{
(, , , uint256[] memory balancesScaled18) = _vault.getPoolTokenInfo(address(this));
uint256 currentVirtualBalanceA;
uint256 currentVirtualBalanceB;
(currentVirtualBalanceA, currentVirtualBalanceB, virtualBalancesChanged) = _computeCurrentVirtualBalances(
balancesScaled18
);
isWithinTargetRange = ReClammMath.isPoolWithinTargetRange(
balancesScaled18,
currentVirtualBalanceA,
currentVirtualBalanceB,
_centerednessMargin
);
}
/// @inheritdoc IReClammPool
function computeCurrentPoolCenteredness() external view returns (uint256, bool) {
(, , , uint256[] memory currentBalancesScaled18) = _vault.getPoolTokenInfo(address(this));
return ReClammMath.computeCenteredness(currentBalancesScaled18, _lastVirtualBalanceA, _lastVirtualBalanceB);
}
/// @inheritdoc IReClammPool
function getReClammPoolDynamicData() external view returns (ReClammPoolDynamicData memory data) {
data.balancesLiveScaled18 = _vault.getCurrentLiveBalances(address(this));
(, data.tokenRates) = _vault.getPoolTokenRates(address(this));
data.staticSwapFeePercentage = _vault.getStaticSwapFeePercentage((address(this)));
data.totalSupply = totalSupply();
data.lastTimestamp = _lastTimestamp;
data.lastVirtualBalances = _getLastVirtualBalances();
data.dailyPriceShiftBase = _dailyPriceShiftBase;
data.dailyPriceShiftExponent = data.dailyPriceShiftBase.toDailyPriceShiftExponent();
data.centerednessMargin = _centerednessMargin;
PriceRatioState memory state = _priceRatioState;
data.startFourthRootPriceRatio = state.startFourthRootPriceRatio;
data.endFourthRootPriceRatio = state.endFourthRootPriceRatio;
data.priceRatioUpdateStartTime = state.priceRatioUpdateStartTime;
data.priceRatioUpdateEndTime = state.priceRatioUpdateEndTime;
PoolConfig memory poolConfig = _vault.getPoolConfig(address(this));
data.isPoolInitialized = poolConfig.isPoolInitialized;
data.isPoolPaused = poolConfig.isPoolPaused;
data.isPoolInRecoveryMode = poolConfig.isPoolInRecoveryMode;
// If the pool is not initialized, virtual balances will be zero and `_computeCurrentPriceRatio` would revert.
if (data.isPoolInitialized) {
data.currentPriceRatio = _computeCurrentPriceRatio();
data.currentFourthRootPriceRatio = ReClammMath.fourthRootScaled18(data.currentPriceRatio);
}
}
/// @inheritdoc IReClammPool
function getReClammPoolImmutableData() external view returns (ReClammPoolImmutableData memory data) {
// Base Pool
data.tokens = _vault.getPoolTokens(address(this));
(data.decimalScalingFactors, ) = _vault.getPoolTokenRates(address(this));
data.tokenAPriceIncludesRate = _TOKEN_A_PRICE_INCLUDES_RATE;
data.tokenBPriceIncludesRate = _TOKEN_B_PRICE_INCLUDES_RATE;
data.minSwapFeePercentage = _MIN_SWAP_FEE_PERCENTAGE;
data.maxSwapFeePercentage = _MAX_SWAP_FEE_PERCENTAGE;
// Initialization
data.initialMinPrice = _INITIAL_MIN_PRICE;
data.initialMaxPrice = _INITIAL_MAX_PRICE;
data.initialTargetPrice = _INITIAL_TARGET_PRICE;
data.initialDailyPriceShiftExponent = _INITIAL_DAILY_PRICE_SHIFT_EXPONENT;
data.initialCenterednessMargin = _INITIAL_CENTEREDNESS_MARGIN;
// Operating Limits
data.maxCenterednessMargin = _MAX_CENTEREDNESS_MARGIN;
data.maxDailyPriceShiftExponent = _MAX_DAILY_PRICE_SHIFT_EXPONENT;
data.maxDailyPriceRatioUpdateRate = _MAX_DAILY_PRICE_RATIO_UPDATE_RATE;
data.minPriceRatioUpdateDuration = _MIN_PRICE_RATIO_UPDATE_DURATION;
data.minPriceRatioDelta = _MIN_PRICE_RATIO_DELTA;
data.balanceRatioAndPriceTolerance = _BALANCE_RATIO_AND_PRICE_TOLERANCE;
}
/********************************************************
Pool State Setters
********************************************************/
/// @inheritdoc IReClammPool
function startPriceRatioUpdate(
uint256 endPriceRatio,
uint256 priceRatioUpdateStartTime,
uint256 priceRatioUpdateEndTime
)
external
onlyWhenInitialized
onlySwapFeeManagerOrGovernance(address(this))
returns (uint256 actualPriceRatioUpdateStartTime)
{
actualPriceRatioUpdateStartTime = GradualValueChange.resolveStartTime(
priceRatioUpdateStartTime,
priceRatioUpdateEndTime
);
uint256 updateDuration = priceRatioUpdateEndTime - actualPriceRatioUpdateStartTime;
// We've already validated that end time >= start time at this point.
if (updateDuration < _MIN_PRICE_RATIO_UPDATE_DURATION) {
revert PriceRatioUpdateDurationTooShort();
}
_updateVirtualBalances();
uint256 startPriceRatio = _startPriceRatioUpdate(
endPriceRatio,
actualPriceRatioUpdateStartTime,
priceRatioUpdateEndTime
);
uint256 priceRatioDelta;
unchecked {
priceRatioDelta = endPriceRatio >= startPriceRatio
? endPriceRatio - startPriceRatio
: startPriceRatio - endPriceRatio;
}
if (priceRatioDelta < _MIN_PRICE_RATIO_DELTA) {
revert PriceRatioDeltaBelowMin(priceRatioDelta);
}
// Compute the rate of change, as a multiple of the present value per day. For example, if the initial price
// range was 1,000 - 4,000, with a target price of 2,000, the raw ratio would be 4 (`startPriceRatio` ~ 1.414).
// If the new fourth root is 1.682, the new `endPriceRatio` would be 1.682^4 ~ 8. Note that since the
// centeredness remains constant, the new range would NOT be 1,000 - 8,000, but [C / sqrt(8), C * sqrt(8)],
// or about 707 - 5657.
//
// If the `updateDuration is 1 day, the time periods cancel, so `actualDailyPriceRatioUpdateRate` is simply
// given by: `endPriceRatio` / `startPriceRatio`; or 8 / 4 = 2: doubling once per day.
// All values are 18-decimal fixed point.
uint256 actualDailyPriceRatioUpdateRate = endPriceRatio > startPriceRatio
? FixedPoint.divUp(endPriceRatio * 1 days, startPriceRatio * updateDuration)
: FixedPoint.divUp(startPriceRatio * 1 days, endPriceRatio * updateDuration);
if (actualDailyPriceRatioUpdateRate > _MAX_DAILY_PRICE_RATIO_UPDATE_RATE) {
revert PriceRatioUpdateTooFast();
}
}
/// @inheritdoc IReClammPool
function stopPriceRatioUpdate() external onlyWhenInitialized onlySwapFeeManagerOrGovernance(address(this)) {
_updateVirtualBalances();
PriceRatioState memory priceRatioState = _priceRatioState;
if (priceRatioState.priceRatioUpdateEndTime < block.timestamp) {
revert PriceRatioNotUpdating();
}
uint256 currentPriceRatio = _computeCurrentPriceRatio();
_startPriceRatioUpdate(currentPriceRatio, block.timestamp, block.timestamp);
}
/// @inheritdoc IReClammPool
function setDailyPriceShiftExponent(
uint256 newDailyPriceShiftExponent
)
external
onlyWhenInitialized
onlyWhenVaultIsLocked
onlySwapFeeManagerOrGovernance(address(this))
returns (uint256)
{
// Update virtual balances before updating the daily price shift exponent.
return _setDailyPriceShiftExponentAndUpdateVirtualBalances(newDailyPriceShiftExponent);
}
/// @inheritdoc IReClammPool
function setCenterednessMargin(
uint256 newCenterednessMargin
)
external
onlyWhenInitialized
onlyWhenVaultIsLocked
onlyWithinTargetRange
onlySwapFeeManagerOrGovernance(address(this))
{
_setCenterednessMarginAndUpdateVirtualBalances(newCenterednessMargin);
}
/********************************************************
Internal Helpers
********************************************************/
function _computeCurrentVirtualBalances(
uint256[] memory balancesScaled18
) internal view returns (uint256 currentVirtualBalanceA, uint256 currentVirtualBalanceB, bool changed) {
(currentVirtualBalanceA, currentVirtualBalanceB, changed) = ReClammMath.computeCurrentVirtualBalances(
balancesScaled18,
_lastVirtualBalanceA,
_lastVirtualBalanceB,
_dailyPriceShiftBase,
_lastTimestamp,
_centerednessMargin,
_priceRatioState
);
}
function _setLastVirtualBalances(uint256 virtualBalanceA, uint256 virtualBalanceB) internal {
_lastVirtualBalanceA = virtualBalanceA.toUint128();
_lastVirtualBalanceB = virtualBalanceB.toUint128();
emit VirtualBalancesUpdated(virtualBalanceA, virtualBalanceB);
_vault.emitAuxiliaryEvent("VirtualBalancesUpdated", abi.encode(virtualBalanceA, virtualBalanceB));
}
function _startPriceRatioUpdate(
uint256 endPriceRatio,
uint256 priceRatioUpdateStartTime,
uint256 priceRatioUpdateEndTime
) internal returns (uint256 startPriceRatio) {
if (priceRatioUpdateStartTime > priceRatioUpdateEndTime || priceRatioUpdateStartTime < block.timestamp) {
revert InvalidStartTime();
}
PriceRatioState memory priceRatioState = _priceRatioState;
uint256 endFourthRootPriceRatio = ReClammMath.fourthRootScaled18(endPriceRatio);
uint256 startFourthRootPriceRatio;
if (_vault.isPoolInitialized(address(this))) {
startPriceRatio = _computeCurrentPriceRatio();
startFourthRootPriceRatio = ReClammMath.fourthRootScaled18(startPriceRatio);
} else {
startFourthRootPriceRatio = endFourthRootPriceRatio;
startPriceRatio = endPriceRatio;
}
priceRatioState.startFourthRootPriceRatio = startFourthRootPriceRatio.toUint96();
priceRatioState.endFourthRootPriceRatio = endFourthRootPriceRatio.toUint96();
priceRatioState.priceRatioUpdateStartTime = priceRatioUpdateStartTime.toUint32();
priceRatioState.priceRatioUpdateEndTime = priceRatioUpdateEndTime.toUint32();
_priceRatioState = priceRatioState;
emit PriceRatioStateUpdated(
startFourthRootPriceRatio,
endFourthRootPriceRatio,
priceRatioUpdateStartTime,
priceRatioUpdateEndTime
);
_vault.emitAuxiliaryEvent(
"PriceRatioStateUpdated",
abi.encode(
startFourthRootPriceRatio,
endFourthRootPriceRatio,
priceRatioUpdateStartTime,
priceRatioUpdateEndTime
)
);
}
/// Using the pool balances to update the virtual balances is dangerous with an unlocked vault, since the balances
/// are manipulable.
function _setDailyPriceShiftExponentAndUpdateVirtualBalances(
uint256 dailyPriceShiftExponent
) internal returns (uint256) {
// Update virtual balances with current daily price shift exponent.
_updateVirtualBalances();
// Update the price shift exponent.
return _setDailyPriceShiftExponent(dailyPriceShiftExponent);
}
function _setDailyPriceShiftExponent(uint256 dailyPriceShiftExponent) internal returns (uint256) {
if (dailyPriceShiftExponent > _MAX_DAILY_PRICE_SHIFT_EXPONENT) {
revert DailyPriceShiftExponentTooHigh();
}
uint256 dailyPriceShiftBase = dailyPriceShiftExponent.toDailyPriceShiftBase();
// There might be precision loss when adjusting to the internal representation, so we need to
// convert back to the external representation to emit the event.
dailyPriceShiftExponent = dailyPriceShiftBase.toDailyPriceShiftExponent();
_dailyPriceShiftBase = dailyPriceShiftBase.toUint128();
emit DailyPriceShiftExponentUpdated(dailyPriceShiftExponent, dailyPriceShiftBase);
_vault.emitAuxiliaryEvent(
"DailyPriceShiftExponentUpdated",
abi.encode(dailyPriceShiftExponent, dailyPriceShiftBase)
);
return dailyPriceShiftExponent;
}
/**
* @dev This function relies on the pool balance, which can be manipulated if the vault is unlocked. Also, the pool
* must be within the target range before and after the operation, or the pool owner could arb the pool.
*/
function _setCenterednessMarginAndUpdateVirtualBalances(uint256 centerednessMargin) internal {
// Update the virtual balances using the current daily price shift exponent.
_updateVirtualBalances();
_setCenterednessMargin(centerednessMargin);
}
/**
* @notice Sets the centeredness margin when the pool is created.
* @param centerednessMargin The new centerednessMargin value, which must be within the target range
*/
function _setCenterednessMargin(uint256 centerednessMargin) internal {
if (centerednessMargin > _MAX_CENTEREDNESS_MARGIN) {
revert InvalidCenterednessMargin();
}
// Straight cast is safe since the margin is validated above (and tests ensure the margins fit in uint64).
_centerednessMargin = uint64(centerednessMargin);
emit CenterednessMarginUpdated(centerednessMargin);
_vault.emitAuxiliaryEvent("CenterednessMarginUpdated", abi.encode(centerednessMargin));
}
function _updateVirtualBalances() internal {
(, , , uint256[] memory balancesScaled18) = _vault.getPoolTokenInfo(address(this));
(uint256 currentVirtualBalanceA, uint256 currentVirtualBalanceB, bool changed) = _computeCurrentVirtualBalances(
balancesScaled18
);
if (changed) {
_setLastVirtualBalances(currentVirtualBalanceA, currentVirtualBalanceB);
}
_updateTimestamp();
}
// Updates the last timestamp to the current timestamp.
function _updateTimestamp() internal {
uint32 lastTimestamp32 = block.timestamp.toUint32();
_lastTimestamp = lastTimestamp32;
emit LastTimestampUpdated(lastTimestamp32);
_vault.emitAuxiliaryEvent("LastTimestampUpdated", abi.encode(lastTimestamp32));
}
/**
* @notice Computes the fourth root of the current price ratio.
* @dev The function calculates the price ratio between tokens A and B using their real and virtual balances,
* then takes the fourth root of this ratio. The multiplication by FixedPoint.ONE before each sqrt operation
* is done to maintain precision in the fixed-point calculations.
*
* @return The fourth root of the current price ratio, maintaining precision through fixed-point arithmetic
*/
function _computeCurrentPriceRatio() internal view returns (uint256) {
(, , , uint256[] memory balancesScaled18) = _vault.getPoolTokenInfo(address(this));
(uint256 virtualBalanceA, uint256 virtualBalanceB, ) = _computeCurrentVirtualBalances(balancesScaled18);
return ReClammMath.computePriceRatio(balancesScaled18, virtualBalanceA, virtualBalanceB);
}
/// @dev This function relies on the pool balance, which can be manipulated if the vault is unlocked.
function _isPoolWithinTargetRange() internal view returns (bool) {
(, , , uint256[] memory balancesScaled18) = _vault.getPoolTokenInfo(address(this));
return
ReClammMath.isPoolWithinTargetRange(
balancesScaled18,
_lastVirtualBalanceA,
_lastVirtualBalanceB,
_centerednessMargin
);
}
/// @dev Checks that the current balance ratio is within the initialization balance ratio tolerance.
function _checkInitializationBalanceRatio(
uint256[] memory balancesScaled18,
uint256[] memory theoreticalBalances
) internal pure {
uint256 realBalanceRatio = balancesScaled18[b].divDown(balancesScaled18[a]);
uint256 theoreticalBalanceRatio = theoreticalBalances[b].divDown(theoreticalBalances[a]);
uint256 ratioLowerBound = theoreticalBalanceRatio.mulDown(FixedPoint.ONE - _BALANCE_RATIO_AND_PRICE_TOLERANCE);
uint256 ratioUpperBound = theoreticalBalanceRatio.mulDown(FixedPoint.ONE + _BALANCE_RATIO_AND_PRICE_TOLERANCE);
if (realBalanceRatio < ratioLowerBound || realBalanceRatio > ratioUpperBound) {
revert BalanceRatioExceedsTolerance();
}
}
/**
* @dev Checks that the current spot price is within the initialization tolerance of the price target, and that
* the total price range after initialization (i.e., with real balances) corresponds closely enough to the desired
* initial price range set on deployment.
*/
function _checkInitializationPrices(
uint256[] memory balancesScaled18,
uint256 minPrice,
uint256 maxPrice,
uint256 targetPrice,
uint256 virtualBalanceA,
uint256 virtualBalanceB
) internal pure {
// Compare current spot price with initialization target price.
uint256 spotPrice = (balancesScaled18[b] + virtualBalanceB).divDown(balancesScaled18[a] + virtualBalanceA);
_comparePrice(spotPrice, targetPrice);
uint256 currentInvariant = ReClammMath.computeInvariant(
balancesScaled18,
virtualBalanceA,
virtualBalanceB,
Rounding.ROUND_DOWN
);
// Compare current min price with initialization min price.
uint256 currentMinPrice = (virtualBalanceB * virtualBalanceB) / currentInvariant;
_comparePrice(currentMinPrice, minPrice);
// Compare current max price with initialization max price.
uint256 currentMaxPrice = _computeMaxPrice(currentInvariant, virtualBalanceA);
_comparePrice(currentMaxPrice, maxPrice);
}
function _comparePrice(uint256 currentPrice, uint256 initializationPrice) internal pure {
uint256 priceLowerBound = initializationPrice.mulDown(FixedPoint.ONE - _BALANCE_RATIO_AND_PRICE_TOLERANCE);
uint256 priceUpperBound = initializationPrice.mulDown(FixedPoint.ONE + _BALANCE_RATIO_AND_PRICE_TOLERANCE);
if (currentPrice < priceLowerBound || currentPrice > priceUpperBound) {
revert WrongInitializationPrices();
}
}
function _getLastVirtualBalances() internal view returns (uint256[] memory) {
uint256[] memory lastVirtualBalances = new uint256[](2);
lastVirtualBalances[a] = _lastVirtualBalanceA;
lastVirtualBalances[b] = _lastVirtualBalanceB;
return lastVirtualBalances;
}
function _ensurePoolWithinTargetRange() internal view {
if (_isPoolWithinTargetRange() == false) {
revert PoolOutsideTargetRange();
}
}
function _computeInitialBalanceRatioScaled18(uint256 rateA, uint256 rateB) internal view returns (uint256) {
(
uint256 minPriceScaled18,
uint256 maxPriceScaled18,
uint256 targetPriceScaled18
) = _getPriceSettingsAdjustedByRates(rateA, rateB);
(uint256[] memory theoreticalBalancesScaled18, , , ) = ReClammMath.computeTheoreticalPriceRatioAndBalances(
minPriceScaled18,
maxPriceScaled18,
targetPriceScaled18
);
return theoreticalBalancesScaled18[b].divDown(theoreticalBalancesScaled18[a]);
}
function _computeMaxPrice(uint256 currentInvariant, uint256 virtualBalanceA) internal pure returns (uint256) {
return currentInvariant.divDown(virtualBalanceA.mulDown(virtualBalanceA));
}
function _getTokenRates() internal view returns (uint256 rateA, uint256 rateB) {
(, TokenInfo[] memory tokenInfo, , ) = _vault.getPoolTokenInfo(address(this));
rateA = _getTokenRate(tokenInfo[a]);
rateB = _getTokenRate(tokenInfo[b]);
}
function _getTokenRate(TokenInfo memory tokenInfo) internal view returns (uint256) {
return tokenInfo.tokenType == TokenType.WITH_RATE ? tokenInfo.rateProvider.getRate() : FixedPoint.ONE;
}
function _getPriceSettingsAdjustedByRates(
uint256 rateA,
uint256 rateB
) internal view returns (uint256 minPrice, uint256 maxPrice, uint256 targetPrice) {
rateA = _TOKEN_A_PRICE_INCLUDES_RATE ? rateA : FixedPoint.ONE;
rateB = _TOKEN_B_PRICE_INCLUDES_RATE ? rateB : FixedPoint.ONE;
// Example: a pool waUSDC/waWETH, where the price is given in terms of the underlying tokens.
// Consider a USDC/ETH pool where the price is 2000. Token A is ETH (waWETH); token B is USDC (waUSDC).
// If waUSDC has a rate of 2 (1 waUSDC = 2 USDC), the price of waUSDC/ETH is 1000, which is
// obtained by dividing the price by the rate of waUSDC, which is token B.
// Now, if the rate of waWETH is 1.5 (1 waWETH = 1.5 ETH), waUSDC/waWETH = 1500, which is
// obtained by multiplying the price by the rate of waWETH, which is token A.
minPrice = (_INITIAL_MIN_PRICE * rateA) / rateB;
maxPrice = (_INITIAL_MAX_PRICE * rateA) / rateB;
targetPrice = (_INITIAL_TARGET_PRICE * rateA) / rateB;
}
}// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.24;
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { TokenInfo } from "../vault/VaultTypes.sol";
/**
* @notice Convenience interface for pools, to get easy access to information stored in the Vault.
* Intended mostly for off-chain requests; pools do not need to implement this to work properly.
*/
interface IPoolInfo {
/**
* @notice Gets the tokens registered in the pool.
* @return tokens List of tokens in the pool, sorted in registration order
*/
function getTokens() external view returns (IERC20[] memory tokens);
/**
* @notice Gets the raw data for the pool: tokens, token info, raw balances, and last live balances.
* @return tokens Pool tokens, sorted in token 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 getTokenInfo()
external
view
returns (
IERC20[] memory tokens,
TokenInfo[] memory tokenInfo,
uint256[] memory balancesRaw,
uint256[] memory lastBalancesLiveScaled18
);
/**
* @notice Gets the current live balances of the pool as fixed point, 18-decimal numbers.
* @dev Note that live balances will not necessarily be accurate if the pool is in Recovery Mode.
* Withdrawals in Recovery Mode do not make external calls (including those necessary for updating live balances),
* so if there are withdrawals, raw and live balances will be out of sync until Recovery Mode is disabled.
*
* @return balancesLiveScaled18 Token balances after paying yield fees, applying decimal scaling and rates
*/
function getCurrentLiveBalances() external view returns (uint256[] memory balancesLiveScaled18);
/**
* @notice Fetches the static swap fee percentage for the pool.
* @return staticSwapFeePercentage 18-decimal FP value of the static swap fee percentage
*/
function getStaticSwapFeePercentage() external view returns (uint256 staticSwapFeePercentage);
/**
* @notice Gets the aggregate swap and yield fee percentages for a pool.
* @dev These are determined by the current protocol and pool creator fees, set in the `ProtocolFeeController`.
* @return aggregateSwapFeePercentage The aggregate percentage fee applied to swaps
* @return aggregateYieldFeePercentage The aggregate percentage fee applied to yield
*/
function getAggregateFeePercentages()
external
view
returns (uint256 aggregateSwapFeePercentage, uint256 aggregateYieldFeePercentage);
}// 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 Simple interface to retrieve the version of a deployed contract.
interface IVersion {
/**
* @notice Return arbitrary text representing the version of a contract.
* @dev For standard Balancer contracts, returns a JSON representation of the contract version containing name,
* version number and task ID. See real examples in the deployment repo; local tests just use plain text strings.
*
* @return version The version string corresponding to the current deployed contract
*/
function version() external view returns (string memory);
}// 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;
import { IUnbalancedLiquidityInvariantRatioBounds } from "./IUnbalancedLiquidityInvariantRatioBounds.sol";
import { ISwapFeePercentageBounds } from "./ISwapFeePercentageBounds.sol";
import { PoolSwapParams, Rounding, SwapKind } from "./VaultTypes.sol";
/**
* @notice Base interface for a Balancer Pool.
* @dev All pool types should implement this interface. Note that it also requires implementation of:
* - `ISwapFeePercentageBounds` to specify the minimum and maximum swap fee percentages.
* - `IUnbalancedLiquidityInvariantRatioBounds` to specify how much the invariant can change during an unbalanced
* liquidity operation.
*/
interface IBasePool is ISwapFeePercentageBounds, IUnbalancedLiquidityInvariantRatioBounds {
/***************************************************************************
Invariant
***************************************************************************/
/**
* @notice Computes the pool's invariant.
* @dev This function computes the invariant based on current balances (and potentially other pool state).
* The rounding direction must be respected for the Vault to round in the pool's favor when calling this function.
* If the invariant computation involves no precision loss (e.g. simple sum of balances), the same result can be
* returned for both rounding directions.
*
* You can think of the invariant as a measure of the "value" of the pool, which is related to the total liquidity
* (i.e., the "BPT rate" is `invariant` / `totalSupply`). Two critical properties must hold:
*
* 1) The invariant should not change due to a swap. In practice, it can *increase* due to swap fees, which
* effectively add liquidity after the swap - but it should never decrease.
*
* 2) The invariant must be "linear"; i.e., increasing the balances proportionally must increase the invariant in
* the same proportion: inv(a * n, b * n, c * n) = inv(a, b, c) * n
*
* Property #1 is required to prevent "round trip" paths that drain value from the pool (and all LP shareholders).
* Intuitively, an accurate pricing algorithm ensures the user gets an equal value of token out given token in, so
* the total value should not change.
*
* Property #2 is essential for the "fungibility" of LP shares. If it did not hold, then different users depositing
* the same total value would get a different number of LP shares. In that case, LP shares would not be
* interchangeable, as they must be in a fair DEX.
*
* @param balancesLiveScaled18 Token balances after paying yield fees, applying decimal scaling and rates
* @param rounding Rounding direction to consider when computing the invariant
* @return invariant The calculated invariant of the pool, represented as a uint256
*/
function computeInvariant(
uint256[] memory balancesLiveScaled18,
Rounding rounding
) external view returns (uint256 invariant);
/**
* @notice Computes a new token balance, given the invariant growth ratio and all other balances.
* @dev Similar to V2's `_getTokenBalanceGivenInvariantAndAllOtherBalances` in StableMath.
* The pool must round up for the Vault to round in the protocol's favor when calling this function.
*
* @param balancesLiveScaled18 Token balances after paying yield fees, applying decimal scaling and rates
* @param tokenInIndex The index of the token we're computing the balance for, sorted in token registration order
* @param invariantRatio The ratio of the new invariant (after an operation) to the old
* @return newBalance The new balance of the selected token, after the operation
*/
function computeBalance(
uint256[] memory balancesLiveScaled18,
uint256 tokenInIndex,
uint256 invariantRatio
) external view returns (uint256 newBalance);
/***************************************************************************
Swaps
***************************************************************************/
/**
* @notice Execute a swap in the pool.
* @param params Swap parameters (see above for struct definition)
* @return amountCalculatedScaled18 Calculated amount for the swap operation
*/
function onSwap(PoolSwapParams calldata params) external returns (uint256 amountCalculatedScaled18);
}// 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 Emitted on pool registration with the initial aggregate swap fee percentage, for off-chain processes.
* @dev If the pool is registered as protocol fee exempt, this will be zero (until changed). Otherwise, it will
* equal the current global swap fee percentage.
*
* @param pool The pool being registered
* @param aggregateSwapFeePercentage The initial aggregate swap fee percentage
* @param isProtocolFeeExempt True if the pool is exempt from taking protocol fees initially
*/
event InitialPoolAggregateSwapFeePercentage(
address indexed pool,
uint256 aggregateSwapFeePercentage,
bool isProtocolFeeExempt
);
/**
* @notice Emitted on pool registration with the initial aggregate yield fee percentage, for off-chain processes.
* @dev If the pool is registered as protocol fee exempt, this will be zero (until changed). Otherwise, it will
* equal the current global yield fee percentage.
*
* @param pool The pool being registered
* @param aggregateYieldFeePercentage The initial aggregate yield fee percentage
* @param isProtocolFeeExempt True if the pool is exempt from taking protocol fees initially
*/
event InitialPoolAggregateYieldFeePercentage(
address indexed pool,
uint256 aggregateYieldFeePercentage,
bool isProtocolFeeExempt
);
/**
* @notice Emitted as a convenience during pool registration, more focused than the Vault's `PoolRegistered` event.
* @dev The `PoolRegistered` event includes the `roleAccounts` field, which also records the pool creator, but this
* simpler event is also provided for convenience. Though `InitialPoolAggregateSwapFeePercentage` and its yield fee
* counterpart also include the protocol fee exemption flag, we might as well include it here as well.
*
* @param pool The address of the pool being registered
* @param poolCreator The address of the pool creator (non-zero, or the event would not be emitted)
* @param protocolFeeExempt True if the pool is initially exempt from protocol fees
*/
event PoolRegisteredWithFeeController(address indexed pool, address indexed poolCreator, bool protocolFeeExempt);
/**
* @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 pool registration flag.
* @param pool The address of the pool
* @return isRegistered True if the pool configuration has been set (e.g., through `registerPool`)
*/
function isPoolRegistered(address pool) external view returns (bool);
/**
* @notice Getter for the current protocol swap fee for a given pool.
* @param pool The address of the pool
* @return protocolSwapFeePercentage The protocol swap fee percentage for the given pool
* @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 protocol yield fee percentage for the given pool
* @return isOverride True if the protocol fee has been overridden
*/
function getPoolProtocolYieldFeeInfo(
address pool
) external view returns (uint256 protocolYieldFeePercentage, bool isOverride);
/**
* @notice Getter for the current pool creator swap fee percentage for a given pool.
* @param pool The address of the pool
* @return poolCreatorSwapFeePercentage The pool creator swap fee component of the aggregate swap fee
*/
function getPoolCreatorSwapFeePercentage(address pool) external view returns (uint256);
/**
* @notice Getter for the current pool creator yield fee percentage for a given pool.
* @param pool The address of the pool
* @return poolCreatorSwapFeePercentage The pool creator yield fee component of the aggregate yield fee
*/
function getPoolCreatorYieldFeePercentage(address pool) external view returns (uint256);
/**
* @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;
/**
* @notice Return the minimum/maximum invariant ratios allowed during an unbalanced liquidity operation.
* @dev The Vault does not enforce any "baseline" bounds on invariant ratios, since such bounds are highly specific
* and dependent on the math of each pool type. Instead, the Vault reads invariant ratio bounds from the pools.
* `IBasePool` implements this interface to ensure that new pool developers think about and set these bounds according
* to their pool type's math.
*
* For instance, Balancer Weighted Pool math involves exponentiation (the `pow` function), which uses natural
* logarithms and a discrete Taylor series expansion to compute x^y values for the 18-decimal floating point numbers
* used in all Vault computations. See `LogExpMath` and `WeightedMath` for a derivation of the bounds for these pools.
*/
interface IUnbalancedLiquidityInvariantRatioBounds {
/// @return minimumInvariantRatio The minimum invariant ratio for a pool during unbalanced remove liquidity
function getMinimumInvariantRatio() external view returns (uint256 minimumInvariantRatio);
/// @return maximumInvariantRatio The maximum invariant ratio for a pool during unbalanced add liquidity
function getMaximumInvariantRatio() external view returns (uint256 maximumInvariantRatio);
}// 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 { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol";
import { CommonAuthentication } from "@balancer-labs/v3-vault/contracts/CommonAuthentication.sol";
/// @dev Base contract for performing access control on external functions within pools.
abstract contract BasePoolAuthentication is CommonAuthentication {
IVault private immutable _vault;
/**
* @dev Pools should use the pool factory as the disambiguator passed into the base Authentication contract.
* Otherwise, permissions would conflict if different pools reused function names.
*/
constructor(IVault vault, address factory) CommonAuthentication(vault, bytes32(uint256(uint160(factory)))) {
// solhint-disable-previous-line no-empty-blocks
}
}// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.24;
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { TokenInfo, PoolConfig } from "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol";
import { IPoolInfo } from "@balancer-labs/v3-interfaces/contracts/pool-utils/IPoolInfo.sol";
import { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol";
/**
* @notice Standard implementation of the `IPoolInfo` interface.
* @dev Balancer standard pools inherit from this optional interface to provide a standard off-chain interface for
* commonly requested data.
*/
contract PoolInfo is IPoolInfo {
IVault private immutable _vault;
constructor(IVault vault) {
_vault = vault;
}
/// @inheritdoc IPoolInfo
function getTokens() external view returns (IERC20[] memory tokens) {
return _vault.getPoolTokens(address(this));
}
/// @inheritdoc IPoolInfo
function getTokenInfo()
external
view
returns (
IERC20[] memory tokens,
TokenInfo[] memory tokenInfo,
uint256[] memory balancesRaw,
uint256[] memory lastBalancesLiveScaled18
)
{
return _vault.getPoolTokenInfo(address(this));
}
/// @inheritdoc IPoolInfo
function getCurrentLiveBalances() external view returns (uint256[] memory balancesLiveScaled18) {
return _vault.getCurrentLiveBalances(address(this));
}
/// @inheritdoc IPoolInfo
function getStaticSwapFeePercentage() external view returns (uint256) {
return _vault.getStaticSwapFeePercentage((address(this)));
}
/// @inheritdoc IPoolInfo
function getAggregateFeePercentages()
external
view
returns (uint256 aggregateSwapFeePercentage, uint256 aggregateYieldFeePercentage)
{
PoolConfig memory poolConfig = _vault.getPoolConfig(address(this));
aggregateSwapFeePercentage = poolConfig.aggregateSwapFeePercentage;
aggregateYieldFeePercentage = poolConfig.aggregateYieldFeePercentage;
}
}// SPDX-License-Identifier: GPL-3.0-or-later
import { FixedPoint } from "@balancer-labs/v3-solidity-utils/contracts/math/FixedPoint.sol";
import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
pragma solidity ^0.8.24;
// solhint-disable not-rely-on-time
library GradualValueChange {
/// @dev Indicates that the start time is after the end time
error InvalidStartTime(uint256 resolvedStartTime, uint256 endTime);
using FixedPoint for uint256;
function getInterpolatedValue(
uint256 startValue,
uint256 endValue,
uint256 startTime,
uint256 endTime
) internal view returns (uint256) {
uint256 pctProgress = calculateValueChangeProgress(startTime, endTime);
return interpolateValue(startValue, endValue, pctProgress);
}
function resolveStartTime(uint256 startTime, uint256 endTime) internal view returns (uint256 resolvedStartTime) {
// If the start time is in the past, "fast forward" to start now
// This avoids discontinuities in the value curve. Otherwise, if you set the start/end times with
// only 10% of the period in the future, the value would immediately jump 90%
resolvedStartTime = Math.max(block.timestamp, startTime);
if (resolvedStartTime > endTime) {
revert InvalidStartTime(resolvedStartTime, endTime);
}
}
function interpolateValue(
uint256 startValue,
uint256 endValue,
uint256 pctProgress
) internal pure returns (uint256) {
if (pctProgress >= FixedPoint.ONE || startValue == endValue) {
return endValue;
}
if (pctProgress == 0) {
return startValue;
}
unchecked {
if (startValue > endValue) {
uint256 delta = pctProgress.mulDown(startValue - endValue);
return startValue - delta;
} else {
uint256 delta = pctProgress.mulDown(endValue - startValue);
return startValue + delta;
}
}
}
/**
* @dev Returns a fixed-point number representing how far along the current value change is, where 0 means the
* change has not yet started, and FixedPoint.ONE means it has fully completed.
*/
function calculateValueChangeProgress(uint256 startTime, uint256 endTime) internal view returns (uint256) {
if (block.timestamp >= endTime) {
return FixedPoint.ONE;
} else if (block.timestamp <= startTime) {
return 0;
}
// No need for checked math as the magnitudes are verified above: endTime > block.timestamp > startTime
uint256 totalSeconds;
uint256 secondsElapsed;
unchecked {
totalSeconds = endTime - startTime;
secondsElapsed = block.timestamp - startTime;
}
// We don't need to consider zero division here as the code would never reach this point in that case.
// If startTime == endTime:
// - Progress = 1 if block.timestamp >= endTime (== startTime)
// - Progress = 0 if block.timestamp < startTime (== endTime)
return secondsElapsed.divDown(totalSeconds);
}
}// 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;
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 {
if (tokens.length < 2) {
return;
}
IERC20 previous = tokens[0];
for (uint256 i = 1; i < tokens.length; ++i) {
IERC20 current = tokens[i];
if (previous > current) {
revert TokensNotSorted();
}
previous = current;
}
}
/// @dev Ensure an array of amounts is sorted. As above, does not validate length or uniqueness.
function ensureSortedAmounts(uint256[] memory amounts) internal pure {
if (amounts.length < 2) {
return;
}
uint256 previous = amounts[0];
for (uint256 i = 1; i < amounts.length; ++i) {
uint256 current = amounts[i];
if (previous > current) {
revert TokensNotSorted();
}
previous = current;
}
}
}// 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 { IVersion } from "@balancer-labs/v3-interfaces/contracts/solidity-utils/helpers/IVersion.sol";
/**
* @notice Retrieves a contract's version from storage.
* @dev The version is set at deployment time and cannot be changed. It would be immutable, but immutable strings
* are not yet supported.
*
* Contracts like factories and pools should have versions. These typically take the form of JSON strings containing
* detailed information about the deployment. For instance:
*
* `{name: 'ChildChainGaugeFactory', version: 2, deployment: '20230316-child-chain-gauge-factory-v2'}`
*/
contract Version is IVersion {
string private _version;
constructor(string memory version_) {
_setVersion(version_);
}
/**
* @notice Getter for the version.
* @return version The stored contract version
*/
function version() external view returns (string memory) {
return _version;
}
/// @dev Internal setter that allows this contract to be used in proxies.
function _setVersion(string memory newVersion) internal {
_version = newVersion;
}
}// 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: 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 { IHooks } from "@balancer-labs/v3-interfaces/contracts/vault/IHooks.sol";
import {
AddLiquidityKind,
HookFlags,
LiquidityManagement,
RemoveLiquidityKind,
TokenConfig,
PoolSwapParams,
AfterSwapParams
} from "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol";
/**
* @notice Base for pool hooks contracts.
* @dev Hook contracts that only implement a subset of callbacks can inherit from here instead of IHooks,
* and only override what they need. `VaultGuard` allows use of the `onlyVault` modifier, which isn't used
* in this abstract contract, but should be used in real derived hook contracts.
*/
abstract contract BaseHooks is IHooks {
/// @inheritdoc IHooks
function onRegister(
address,
address,
TokenConfig[] memory,
LiquidityManagement calldata
) public virtual returns (bool) {
// By default, deny all factories. This method must be overwritten by the hook contract.
return false;
}
/// @inheritdoc IHooks
function getHookFlags() public view virtual returns (HookFlags memory);
/// @inheritdoc IHooks
function onBeforeInitialize(uint256[] memory, bytes memory) public virtual returns (bool) {
return false;
}
/// @inheritdoc IHooks
function onAfterInitialize(uint256[] memory, uint256, bytes memory) public virtual returns (bool) {
return false;
}
/// @inheritdoc IHooks
function onBeforeAddLiquidity(
address,
address,
AddLiquidityKind,
uint256[] memory,
uint256,
uint256[] memory,
bytes memory
) public virtual returns (bool) {
return false;
}
/// @inheritdoc IHooks
function onAfterAddLiquidity(
address,
address,
AddLiquidityKind,
uint256[] memory,
uint256[] memory amountsInRaw,
uint256,
uint256[] memory,
bytes memory
) public virtual returns (bool, uint256[] memory) {
return (false, amountsInRaw);
}
/// @inheritdoc IHooks
function onBeforeRemoveLiquidity(
address,
address,
RemoveLiquidityKind,
uint256,
uint256[] memory,
uint256[] memory,
bytes memory
) public virtual returns (bool) {
return false;
}
/// @inheritdoc IHooks
function onAfterRemoveLiquidity(
address,
address,
RemoveLiquidityKind,
uint256,
uint256[] memory,
uint256[] memory amountsOutRaw,
uint256[] memory,
bytes memory
) public virtual returns (bool, uint256[] memory) {
return (false, amountsOutRaw);
}
/// @inheritdoc IHooks
function onBeforeSwap(PoolSwapParams calldata, address) public virtual returns (bool) {
// return false to trigger an error if shouldCallBeforeSwap is true but this function is not overridden.
return false;
}
/// @inheritdoc IHooks
function onAfterSwap(AfterSwapParams calldata) public virtual returns (bool, uint256) {
// return false to trigger an error if shouldCallAfterSwap is true but this function is not overridden.
// The second argument is not used.
return (false, 0);
}
/// @inheritdoc IHooks
function onComputeDynamicSwapFeePercentage(
PoolSwapParams calldata,
address,
uint256
) public view virtual returns (bool, uint256) {
return (false, 0);
}
}// 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 { Authentication } from "@balancer-labs/v3-solidity-utils/contracts/helpers/Authentication.sol";
/// @dev Base contract for performing access control on external functions within pools.
abstract contract CommonAuthentication is Authentication {
/// @dev Vault cannot be address(0).
error VaultNotSet();
IVault private immutable _vault;
/// @notice Caller must be the swapFeeManager, if defined. Otherwise, default to governance.
modifier onlySwapFeeManagerOrGovernance(address pool) {
address roleAddress = _vault.getPoolRoleAccounts(pool).swapFeeManager;
_ensureAuthenticatedByExclusiveRole(pool, roleAddress);
_;
}
constructor(IVault vault, bytes32 actionIdDisambiguator) Authentication(actionIdDisambiguator) {
if (address(vault) == address(0)) {
revert VaultNotSet();
}
_vault = vault;
}
function _getVault() internal view returns (IVault) {
return _vault;
}
// Access control is delegated to the Authorizer in the `_canPerform` functions.
function _canPerform(bytes32 actionId, address user) internal view override returns (bool) {
return _vault.getAuthorizer().canPerform(actionId, user, address(this));
}
function _canPerform(bytes32 actionId, address account, address where) internal view returns (bool) {
return _vault.getAuthorizer().canPerform(actionId, account, where);
}
/// @dev Ensure the sender is the roleAccount, or default to governance if roleAccount is address(0).
function _ensureAuthenticatedByExclusiveRole(address where, address roleAccount) internal view {
if (roleAccount == address(0)) {
// Defer to governance if no role assigned.
if (_canPerform(getActionId(msg.sig), msg.sender, where) == false) {
revert SenderNotAllowed();
}
} else if (msg.sender != roleAccount) {
revert SenderNotAllowed();
}
}
/// @dev Ensure the sender is either the role manager, or is authorized by governance (non-exclusive).
function _ensureAuthenticatedByRole(address where, address roleAccount) internal view {
// If the sender is not the delegated manager for the role, defer to governance.
if (msg.sender != roleAccount) {
if (_canPerform(getActionId(msg.sig), msg.sender, where) == false) {
revert SenderNotAllowed();
}
}
// (else) if the sender is the delegated manager, proceed.
}
}// 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: MIT
// OpenZeppelin Contracts (last updated v5.1.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 ERC-4626 "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.1.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 ERC-20 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.1.0) (token/ERC20/extensions/IERC20Permit.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface 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].
*
* Adds the {permit} method, which can be used to change an account's ERC-20 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.1.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC-20 standard as defined in the ERC.
*/
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.1.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 recovered, RecoverError err, bytes32 errArg) {
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.
assembly ("memory-safe") {
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[ERC-2098 short signatures]
*/
function tryRecover(
bytes32 hash,
bytes32 r,
bytes32 vs
) internal pure returns (address recovered, RecoverError err, bytes32 errArg) {
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 recovered, RecoverError err, bytes32 errArg) {
// 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.1.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.1.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[ERC-191] and https://eips.ethereum.org/EIPS/eip-712[EIP 712]
* specifications.
*/
library MessageHashUtils {
/**
* @dev Returns the keccak256 digest of an ERC-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) {
assembly ("memory-safe") {
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 ERC-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 ERC-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 (ERC-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) {
assembly ("memory-safe") {
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.1.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 ERC-165 should inherit from this contract and override {supportsInterface} to check
* for the additional interface id that will be supported. For example:
*
* ```solidity
* function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
* return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId);
* }
* ```
*/
abstract contract 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.1.0) (utils/introspection/IERC165.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC-165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[ERC].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[ERC section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/math/Math.sol)
pragma solidity ^0.8.20;
import {Panic} from "../Panic.sol";
import {SafeCast} from "./SafeCast.sol";
/**
* @dev Standard math utilities missing in the Solidity language.
*/
library Math {
enum Rounding {
Floor, // Toward negative infinity
Ceil, // Toward positive infinity
Trunc, // Toward zero
Expand // Away from zero
}
/**
* @dev Returns the addition of two unsigned integers, with an success flag (no overflow).
*/
function tryAdd(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the subtraction of two unsigned integers, with an success flag (no overflow).
*/
function trySub(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
if (b > a) return (false, 0);
return (true, a - b);
}
}
/**
* @dev Returns the multiplication of two unsigned integers, with an success flag (no overflow).
*/
function tryMul(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the division of two unsigned integers, with a success flag (no division by zero).
*/
function tryDiv(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
if (b == 0) return (false, 0);
return (true, a / b);
}
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a success flag (no division by zero).
*/
function tryMod(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
if (b == 0) return (false, 0);
return (true, a % b);
}
}
/**
* @dev Branchless ternary evaluation for `a ? b : c`. Gas costs are constant.
*
* IMPORTANT: This function may reduce bytecode size and consume less gas when used standalone.
* However, the compiler may optimize Solidity ternary operations (i.e. `a ? b : c`) to only compute
* one branch when needed, making this function more expensive.
*/
function ternary(bool condition, uint256 a, uint256 b) internal pure returns (uint256) {
unchecked {
// branchless ternary works because:
// b ^ (a ^ b) == a
// b ^ 0 == b
return b ^ ((a ^ b) * SafeCast.toUint(condition));
}
}
/**
* @dev Returns the largest of two numbers.
*/
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return ternary(a > b, a, b);
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return ternary(a < b, a, b);
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow.
return (a & b) + (a ^ b) / 2;
}
/**
* @dev Returns the ceiling of the division of two numbers.
*
* This differs from standard division with `/` in that it rounds towards infinity instead
* of rounding towards zero.
*/
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
if (b == 0) {
// Guarantee the same behavior as in a regular Solidity division.
Panic.panic(Panic.DIVISION_BY_ZERO);
}
// The following calculation ensures accurate ceiling division without overflow.
// Since a is non-zero, (a - 1) / b will not overflow.
// The largest possible result occurs when (a - 1) / b is type(uint256).max,
// but the largest value we can obtain is type(uint256).max - 1, which happens
// when a = type(uint256).max and b = 1.
unchecked {
return SafeCast.toUint(a > 0) * ((a - 1) / b + 1);
}
}
/**
* @dev Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or
* denominator == 0.
*
* Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv) with further edits by
* Uniswap Labs also under MIT license.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2²⁵⁶ and mod 2²⁵⁶ - 1, then use
// the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2²⁵⁶ + prod0.
uint256 prod0 = x * y; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(x, y, not(0))
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division.
if (prod1 == 0) {
// Solidity will revert if denominator == 0, unlike the div opcode on its own.
// The surrounding unchecked block does not change this fact.
// See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
return prod0 / denominator;
}
// Make sure the result is less than 2²⁵⁶. Also prevents denominator == 0.
if (denominator <= prod1) {
Panic.panic(ternary(denominator == 0, Panic.DIVISION_BY_ZERO, Panic.UNDER_OVERFLOW));
}
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0].
uint256 remainder;
assembly {
// Compute remainder using mulmod.
remainder := mulmod(x, y, denominator)
// Subtract 256 bit number from 512 bit number.
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator and compute largest power of two divisor of denominator.
// Always >= 1. See https://cs.stackexchange.com/q/138556/92363.
uint256 twos = denominator & (0 - denominator);
assembly {
// Divide denominator by twos.
denominator := div(denominator, twos)
// Divide [prod1 prod0] by twos.
prod0 := div(prod0, twos)
// Flip twos such that it is 2²⁵⁶ / twos. If twos is zero, then it becomes one.
twos := add(div(sub(0, twos), twos), 1)
}
// Shift in bits from prod1 into prod0.
prod0 |= prod1 * twos;
// Invert denominator mod 2²⁵⁶. Now that denominator is an odd number, it has an inverse modulo 2²⁵⁶ such
// that denominator * inv ≡ 1 mod 2²⁵⁶. Compute the inverse by starting with a seed that is correct for
// four bits. That is, denominator * inv ≡ 1 mod 2⁴.
uint256 inverse = (3 * denominator) ^ 2;
// Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also
// works in modular arithmetic, doubling the correct bits in each step.
inverse *= 2 - denominator * inverse; // inverse mod 2⁸
inverse *= 2 - denominator * inverse; // inverse mod 2¹⁶
inverse *= 2 - denominator * inverse; // inverse mod 2³²
inverse *= 2 - denominator * inverse; // inverse mod 2⁶⁴
inverse *= 2 - denominator * inverse; // inverse mod 2¹²⁸
inverse *= 2 - denominator * inverse; // inverse mod 2²⁵⁶
// Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
// This will give us the correct result modulo 2²⁵⁶. Since the preconditions guarantee that the outcome is
// less than 2²⁵⁶, this is the final result. We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inverse;
return result;
}
}
/**
* @dev Calculates x * y / denominator with full precision, following the selected rounding direction.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
return mulDiv(x, y, denominator) + SafeCast.toUint(unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0);
}
/**
* @dev Calculate the modular multiplicative inverse of a number in Z/nZ.
*
* If n is a prime, then Z/nZ is a field. In that case all elements are inversible, except 0.
* If n is not a prime, then Z/nZ is not a field, and some elements might not be inversible.
*
* If the input value is not inversible, 0 is returned.
*
* NOTE: If you know for sure that n is (big) a prime, it may be cheaper to use Fermat's little theorem and get the
* inverse using `Math.modExp(a, n - 2, n)`. See {invModPrime}.
*/
function invMod(uint256 a, uint256 n) internal pure returns (uint256) {
unchecked {
if (n == 0) return 0;
// The inverse modulo is calculated using the Extended Euclidean Algorithm (iterative version)
// Used to compute integers x and y such that: ax + ny = gcd(a, n).
// When the gcd is 1, then the inverse of a modulo n exists and it's x.
// ax + ny = 1
// ax = 1 + (-y)n
// ax ≡ 1 (mod n) # x is the inverse of a modulo n
// If the remainder is 0 the gcd is n right away.
uint256 remainder = a % n;
uint256 gcd = n;
// Therefore the initial coefficients are:
// ax + ny = gcd(a, n) = n
// 0a + 1n = n
int256 x = 0;
int256 y = 1;
while (remainder != 0) {
uint256 quotient = gcd / remainder;
(gcd, remainder) = (
// The old remainder is the next gcd to try.
remainder,
// Compute the next remainder.
// Can't overflow given that (a % gcd) * (gcd // (a % gcd)) <= gcd
// where gcd is at most n (capped to type(uint256).max)
gcd - remainder * quotient
);
(x, y) = (
// Increment the coefficient of a.
y,
// Decrement the coefficient of n.
// Can overflow, but the result is casted to uint256 so that the
// next value of y is "wrapped around" to a value between 0 and n - 1.
x - y * int256(quotient)
);
}
if (gcd != 1) return 0; // No inverse exists.
return ternary(x < 0, n - uint256(-x), uint256(x)); // Wrap the result if it's negative.
}
}
/**
* @dev Variant of {invMod}. More efficient, but only works if `p` is known to be a prime greater than `2`.
*
* From https://en.wikipedia.org/wiki/Fermat%27s_little_theorem[Fermat's little theorem], we know that if p is
* prime, then `a**(p-1) ≡ 1 mod p`. As a consequence, we have `a * a**(p-2) ≡ 1 mod p`, which means that
* `a**(p-2)` is the modular multiplicative inverse of a in Fp.
*
* NOTE: this function does NOT check that `p` is a prime greater than `2`.
*/
function invModPrime(uint256 a, uint256 p) internal view returns (uint256) {
unchecked {
return Math.modExp(a, p - 2, p);
}
}
/**
* @dev Returns the modular exponentiation of the specified base, exponent and modulus (b ** e % m)
*
* Requirements:
* - modulus can't be zero
* - underlying staticcall to precompile must succeed
*
* IMPORTANT: The result is only valid if the underlying call succeeds. When using this function, make
* sure the chain you're using it on supports the precompiled contract for modular exponentiation
* at address 0x05 as specified in https://eips.ethereum.org/EIPS/eip-198[EIP-198]. Otherwise,
* the underlying function will succeed given the lack of a revert, but the result may be incorrectly
* interpreted as 0.
*/
function modExp(uint256 b, uint256 e, uint256 m) internal view returns (uint256) {
(bool success, uint256 result) = tryModExp(b, e, m);
if (!success) {
Panic.panic(Panic.DIVISION_BY_ZERO);
}
return result;
}
/**
* @dev Returns the modular exponentiation of the specified base, exponent and modulus (b ** e % m).
* It includes a success flag indicating if the operation succeeded. Operation will be marked as failed if trying
* to operate modulo 0 or if the underlying precompile reverted.
*
* IMPORTANT: The result is only valid if the success flag is true. When using this function, make sure the chain
* you're using it on supports the precompiled contract for modular exponentiation at address 0x05 as specified in
* https://eips.ethereum.org/EIPS/eip-198[EIP-198]. Otherwise, the underlying function will succeed given the lack
* of a revert, but the result may be incorrectly interpreted as 0.
*/
function tryModExp(uint256 b, uint256 e, uint256 m) internal view returns (bool success, uint256 result) {
if (m == 0) return (false, 0);
assembly ("memory-safe") {
let ptr := mload(0x40)
// | Offset | Content | Content (Hex) |
// |-----------|------------|--------------------------------------------------------------------|
// | 0x00:0x1f | size of b | 0x0000000000000000000000000000000000000000000000000000000000000020 |
// | 0x20:0x3f | size of e | 0x0000000000000000000000000000000000000000000000000000000000000020 |
// | 0x40:0x5f | size of m | 0x0000000000000000000000000000000000000000000000000000000000000020 |
// | 0x60:0x7f | value of b | 0x<.............................................................b> |
// | 0x80:0x9f | value of e | 0x<.............................................................e> |
// | 0xa0:0xbf | value of m | 0x<.............................................................m> |
mstore(ptr, 0x20)
mstore(add(ptr, 0x20), 0x20)
mstore(add(ptr, 0x40), 0x20)
mstore(add(ptr, 0x60), b)
mstore(add(ptr, 0x80), e)
mstore(add(ptr, 0xa0), m)
// Given the result < m, it's guaranteed to fit in 32 bytes,
// so we can use the memory scratch space located at offset 0.
success := staticcall(gas(), 0x05, ptr, 0xc0, 0x00, 0x20)
result := mload(0x00)
}
}
/**
* @dev Variant of {modExp} that supports inputs of arbitrary length.
*/
function modExp(bytes memory b, bytes memory e, bytes memory m) internal view returns (bytes memory) {
(bool success, bytes memory result) = tryModExp(b, e, m);
if (!success) {
Panic.panic(Panic.DIVISION_BY_ZERO);
}
return result;
}
/**
* @dev Variant of {tryModExp} that supports inputs of arbitrary length.
*/
function tryModExp(
bytes memory b,
bytes memory e,
bytes memory m
) internal view returns (bool success, bytes memory result) {
if (_zeroBytes(m)) return (false, new bytes(0));
uint256 mLen = m.length;
// Encode call args in result and move the free memory pointer
result = abi.encodePacked(b.length, e.length, mLen, b, e, m);
assembly ("memory-safe") {
let dataPtr := add(result, 0x20)
// Write result on top of args to avoid allocating extra memory.
success := staticcall(gas(), 0x05, dataPtr, mload(result), dataPtr, mLen)
// Overwrite the length.
// result.length > returndatasize() is guaranteed because returndatasize() == m.length
mstore(result, mLen)
// Set the memory pointer after the returned data.
mstore(0x40, add(dataPtr, mLen))
}
}
/**
* @dev Returns whether the provided byte array is zero.
*/
function _zeroBytes(bytes memory byteArray) private pure returns (bool) {
for (uint256 i = 0; i < byteArray.length; ++i) {
if (byteArray[i] != 0) {
return false;
}
}
return true;
}
/**
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded
* towards zero.
*
* This method is based on Newton's method for computing square roots; the algorithm is restricted to only
* using integer operations.
*/
function sqrt(uint256 a) internal pure returns (uint256) {
unchecked {
// Take care of easy edge cases when a == 0 or a == 1
if (a <= 1) {
return a;
}
// In this function, we use Newton's method to get a root of `f(x) := x² - a`. It involves building a
// sequence x_n that converges toward sqrt(a). For each iteration x_n, we also define the error between
// the current value as `ε_n = | x_n - sqrt(a) |`.
//
// For our first estimation, we consider `e` the smallest power of 2 which is bigger than the square root
// of the target. (i.e. `2**(e-1) ≤ sqrt(a) < 2**e`). We know that `e ≤ 128` because `(2¹²⁸)² = 2²⁵⁶` is
// bigger than any uint256.
//
// By noticing that
// `2**(e-1) ≤ sqrt(a) < 2**e → (2**(e-1))² ≤ a < (2**e)² → 2**(2*e-2) ≤ a < 2**(2*e)`
// we can deduce that `e - 1` is `log2(a) / 2`. We can thus compute `x_n = 2**(e-1)` using a method similar
// to the msb function.
uint256 aa = a;
uint256 xn = 1;
if (aa >= (1 << 128)) {
aa >>= 128;
xn <<= 64;
}
if (aa >= (1 << 64)) {
aa >>= 64;
xn <<= 32;
}
if (aa >= (1 << 32)) {
aa >>= 32;
xn <<= 16;
}
if (aa >= (1 << 16)) {
aa >>= 16;
xn <<= 8;
}
if (aa >= (1 << 8)) {
aa >>= 8;
xn <<= 4;
}
if (aa >= (1 << 4)) {
aa >>= 4;
xn <<= 2;
}
if (aa >= (1 << 2)) {
xn <<= 1;
}
// We now have x_n such that `x_n = 2**(e-1) ≤ sqrt(a) < 2**e = 2 * x_n`. This implies ε_n ≤ 2**(e-1).
//
// We can refine our estimation by noticing that the middle of that interval minimizes the error.
// If we move x_n to equal 2**(e-1) + 2**(e-2), then we reduce the error to ε_n ≤ 2**(e-2).
// This is going to be our x_0 (and ε_0)
xn = (3 * xn) >> 1; // ε_0 := | x_0 - sqrt(a) | ≤ 2**(e-2)
// From here, Newton's method give us:
// x_{n+1} = (x_n + a / x_n) / 2
//
// One should note that:
// x_{n+1}² - a = ((x_n + a / x_n) / 2)² - a
// = ((x_n² + a) / (2 * x_n))² - a
// = (x_n⁴ + 2 * a * x_n² + a²) / (4 * x_n²) - a
// = (x_n⁴ + 2 * a * x_n² + a² - 4 * a * x_n²) / (4 * x_n²)
// = (x_n⁴ - 2 * a * x_n² + a²) / (4 * x_n²)
// = (x_n² - a)² / (2 * x_n)²
// = ((x_n² - a) / (2 * x_n))²
// ≥ 0
// Which proves that for all n ≥ 1, sqrt(a) ≤ x_n
//
// This gives us the proof of quadratic convergence of the sequence:
// ε_{n+1} = | x_{n+1} - sqrt(a) |
// = | (x_n + a / x_n) / 2 - sqrt(a) |
// = | (x_n² + a - 2*x_n*sqrt(a)) / (2 * x_n) |
// = | (x_n - sqrt(a))² / (2 * x_n) |
// = | ε_n² / (2 * x_n) |
// = ε_n² / | (2 * x_n) |
//
// For the first iteration, we have a special case where x_0 is known:
// ε_1 = ε_0² / | (2 * x_0) |
// ≤ (2**(e-2))² / (2 * (2**(e-1) + 2**(e-2)))
// ≤ 2**(2*e-4) / (3 * 2**(e-1))
// ≤ 2**(e-3) / 3
// ≤ 2**(e-3-log2(3))
// ≤ 2**(e-4.5)
//
// For the following iterations, we use the fact that, 2**(e-1) ≤ sqrt(a) ≤ x_n:
// ε_{n+1} = ε_n² / | (2 * x_n) |
// ≤ (2**(e-k))² / (2 * 2**(e-1))
// ≤ 2**(2*e-2*k) / 2**e
// ≤ 2**(e-2*k)
xn = (xn + a / xn) >> 1; // ε_1 := | x_1 - sqrt(a) | ≤ 2**(e-4.5) -- special case, see above
xn = (xn + a / xn) >> 1; // ε_2 := | x_2 - sqrt(a) | ≤ 2**(e-9) -- general case with k = 4.5
xn = (xn + a / xn) >> 1; // ε_3 := | x_3 - sqrt(a) | ≤ 2**(e-18) -- general case with k = 9
xn = (xn + a / xn) >> 1; // ε_4 := | x_4 - sqrt(a) | ≤ 2**(e-36) -- general case with k = 18
xn = (xn + a / xn) >> 1; // ε_5 := | x_5 - sqrt(a) | ≤ 2**(e-72) -- general case with k = 36
xn = (xn + a / xn) >> 1; // ε_6 := | x_6 - sqrt(a) | ≤ 2**(e-144) -- general case with k = 72
// Because e ≤ 128 (as discussed during the first estimation phase), we know have reached a precision
// ε_6 ≤ 2**(e-144) < 1. Given we're operating on integers, then we can ensure that xn is now either
// sqrt(a) or sqrt(a) + 1.
return xn - SafeCast.toUint(xn > a / xn);
}
}
/**
* @dev Calculates sqrt(a), following the selected rounding direction.
*/
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && result * result < a);
}
}
/**
* @dev Return the log in base 2 of a positive value rounded towards zero.
* Returns 0 if given 0.
*/
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
uint256 exp;
unchecked {
exp = 128 * SafeCast.toUint(value > (1 << 128) - 1);
value >>= exp;
result += exp;
exp = 64 * SafeCast.toUint(value > (1 << 64) - 1);
value >>= exp;
result += exp;
exp = 32 * SafeCast.toUint(value > (1 << 32) - 1);
value >>= exp;
result += exp;
exp = 16 * SafeCast.toUint(value > (1 << 16) - 1);
value >>= exp;
result += exp;
exp = 8 * SafeCast.toUint(value > (1 << 8) - 1);
value >>= exp;
result += exp;
exp = 4 * SafeCast.toUint(value > (1 << 4) - 1);
value >>= exp;
result += exp;
exp = 2 * SafeCast.toUint(value > (1 << 2) - 1);
value >>= exp;
result += exp;
result += SafeCast.toUint(value > 1);
}
return result;
}
/**
* @dev Return the log in base 2, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << result < value);
}
}
/**
* @dev Return the log in base 10 of a positive value rounded towards zero.
* Returns 0 if given 0.
*/
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >= 10 ** 64) {
value /= 10 ** 64;
result += 64;
}
if (value >= 10 ** 32) {
value /= 10 ** 32;
result += 32;
}
if (value >= 10 ** 16) {
value /= 10 ** 16;
result += 16;
}
if (value >= 10 ** 8) {
value /= 10 ** 8;
result += 8;
}
if (value >= 10 ** 4) {
value /= 10 ** 4;
result += 4;
}
if (value >= 10 ** 2) {
value /= 10 ** 2;
result += 2;
}
if (value >= 10 ** 1) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 10, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 10 ** result < value);
}
}
/**
* @dev Return the log in base 256 of a positive value rounded towards zero.
* Returns 0 if given 0.
*
* Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
*/
function log256(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
uint256 isGt;
unchecked {
isGt = SafeCast.toUint(value > (1 << 128) - 1);
value >>= isGt * 128;
result += isGt * 16;
isGt = SafeCast.toUint(value > (1 << 64) - 1);
value >>= isGt * 64;
result += isGt * 8;
isGt = SafeCast.toUint(value > (1 << 32) - 1);
value >>= isGt * 32;
result += isGt * 4;
isGt = SafeCast.toUint(value > (1 << 16) - 1);
value >>= isGt * 16;
result += isGt * 2;
result += SafeCast.toUint(value > (1 << 8) - 1);
}
return result;
}
/**
* @dev Return the log in base 256, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << (result << 3) < value);
}
}
/**
* @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers.
*/
function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {
return uint8(rounding) % 2 == 1;
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/math/SafeCast.sol)
// This file was procedurally generated from scripts/generate/templates/SafeCast.js.
pragma solidity ^0.8.20;
/**
* @dev Wrappers over Solidity's uintXX/intXX/bool casting operators with added overflow
* checks.
*
* Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
* easily result in undesired exploitation or bugs, since developers usually
* assume that overflows raise errors. `SafeCast` restores this intuition by
* reverting the transaction when such an operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeCast {
/**
* @dev Value doesn't fit in an uint of `bits` size.
*/
error SafeCastOverflowedUintDowncast(uint8 bits, uint256 value);
/**
* @dev An int value doesn't fit in an uint of `bits` size.
*/
error SafeCastOverflowedIntToUint(int256 value);
/**
* @dev Value doesn't fit in an int of `bits` size.
*/
error SafeCastOverflowedIntDowncast(uint8 bits, int256 value);
/**
* @dev An uint value doesn't fit in an int of `bits` size.
*/
error SafeCastOverflowedUintToInt(uint256 value);
/**
* @dev Returns the downcasted uint248 from uint256, reverting on
* overflow (when the input is greater than largest uint248).
*
* Counterpart to Solidity's `uint248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*/
function toUint248(uint256 value) internal pure returns (uint248) {
if (value > type(uint248).max) {
revert SafeCastOverflowedUintDowncast(248, value);
}
return uint248(value);
}
/**
* @dev Returns the downcasted uint240 from uint256, reverting on
* overflow (when the input is greater than largest uint240).
*
* Counterpart to Solidity's `uint240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*/
function toUint240(uint256 value) internal pure returns (uint240) {
if (value > type(uint240).max) {
revert SafeCastOverflowedUintDowncast(240, value);
}
return uint240(value);
}
/**
* @dev Returns the downcasted uint232 from uint256, reverting on
* overflow (when the input is greater than largest uint232).
*
* Counterpart to Solidity's `uint232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*/
function toUint232(uint256 value) internal pure returns (uint232) {
if (value > type(uint232).max) {
revert SafeCastOverflowedUintDowncast(232, value);
}
return uint232(value);
}
/**
* @dev Returns the downcasted uint224 from uint256, reverting on
* overflow (when the input is greater than largest uint224).
*
* Counterpart to Solidity's `uint224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*/
function toUint224(uint256 value) internal pure returns (uint224) {
if (value > type(uint224).max) {
revert SafeCastOverflowedUintDowncast(224, value);
}
return uint224(value);
}
/**
* @dev Returns the downcasted uint216 from uint256, reverting on
* overflow (when the input is greater than largest uint216).
*
* Counterpart to Solidity's `uint216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*/
function toUint216(uint256 value) internal pure returns (uint216) {
if (value > type(uint216).max) {
revert SafeCastOverflowedUintDowncast(216, value);
}
return uint216(value);
}
/**
* @dev Returns the downcasted uint208 from uint256, reverting on
* overflow (when the input is greater than largest uint208).
*
* Counterpart to Solidity's `uint208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*/
function toUint208(uint256 value) internal pure returns (uint208) {
if (value > type(uint208).max) {
revert SafeCastOverflowedUintDowncast(208, value);
}
return uint208(value);
}
/**
* @dev Returns the downcasted uint200 from uint256, reverting on
* overflow (when the input is greater than largest uint200).
*
* Counterpart to Solidity's `uint200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*/
function toUint200(uint256 value) internal pure returns (uint200) {
if (value > type(uint200).max) {
revert SafeCastOverflowedUintDowncast(200, value);
}
return uint200(value);
}
/**
* @dev Returns the downcasted uint192 from uint256, reverting on
* overflow (when the input is greater than largest uint192).
*
* Counterpart to Solidity's `uint192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*/
function toUint192(uint256 value) internal pure returns (uint192) {
if (value > type(uint192).max) {
revert SafeCastOverflowedUintDowncast(192, value);
}
return uint192(value);
}
/**
* @dev Returns the downcasted uint184 from uint256, reverting on
* overflow (when the input is greater than largest uint184).
*
* Counterpart to Solidity's `uint184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*/
function toUint184(uint256 value) internal pure returns (uint184) {
if (value > type(uint184).max) {
revert SafeCastOverflowedUintDowncast(184, value);
}
return uint184(value);
}
/**
* @dev Returns the downcasted uint176 from uint256, reverting on
* overflow (when the input is greater than largest uint176).
*
* Counterpart to Solidity's `uint176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*/
function toUint176(uint256 value) internal pure returns (uint176) {
if (value > type(uint176).max) {
revert SafeCastOverflowedUintDowncast(176, value);
}
return uint176(value);
}
/**
* @dev Returns the downcasted uint168 from uint256, reverting on
* overflow (when the input is greater than largest uint168).
*
* Counterpart to Solidity's `uint168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*/
function toUint168(uint256 value) internal pure returns (uint168) {
if (value > type(uint168).max) {
revert SafeCastOverflowedUintDowncast(168, value);
}
return uint168(value);
}
/**
* @dev Returns the downcasted uint160 from uint256, reverting on
* overflow (when the input is greater than largest uint160).
*
* Counterpart to Solidity's `uint160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*/
function toUint160(uint256 value) internal pure returns (uint160) {
if (value > type(uint160).max) {
revert SafeCastOverflowedUintDowncast(160, value);
}
return uint160(value);
}
/**
* @dev Returns the downcasted uint152 from uint256, reverting on
* overflow (when the input is greater than largest uint152).
*
* Counterpart to Solidity's `uint152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*/
function toUint152(uint256 value) internal pure returns (uint152) {
if (value > type(uint152).max) {
revert SafeCastOverflowedUintDowncast(152, value);
}
return uint152(value);
}
/**
* @dev Returns the downcasted uint144 from uint256, reverting on
* overflow (when the input is greater than largest uint144).
*
* Counterpart to Solidity's `uint144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*/
function toUint144(uint256 value) internal pure returns (uint144) {
if (value > type(uint144).max) {
revert SafeCastOverflowedUintDowncast(144, value);
}
return uint144(value);
}
/**
* @dev Returns the downcasted uint136 from uint256, reverting on
* overflow (when the input is greater than largest uint136).
*
* Counterpart to Solidity's `uint136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*/
function toUint136(uint256 value) internal pure returns (uint136) {
if (value > type(uint136).max) {
revert SafeCastOverflowedUintDowncast(136, value);
}
return uint136(value);
}
/**
* @dev Returns the downcasted uint128 from uint256, reverting on
* overflow (when the input is greater than largest uint128).
*
* Counterpart to Solidity's `uint128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*/
function toUint128(uint256 value) internal pure returns (uint128) {
if (value > type(uint128).max) {
revert SafeCastOverflowedUintDowncast(128, value);
}
return uint128(value);
}
/**
* @dev Returns the downcasted uint120 from uint256, reverting on
* overflow (when the input is greater than largest uint120).
*
* Counterpart to Solidity's `uint120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*/
function toUint120(uint256 value) internal pure returns (uint120) {
if (value > type(uint120).max) {
revert SafeCastOverflowedUintDowncast(120, value);
}
return uint120(value);
}
/**
* @dev Returns the downcasted uint112 from uint256, reverting on
* overflow (when the input is greater than largest uint112).
*
* Counterpart to Solidity's `uint112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*/
function toUint112(uint256 value) internal pure returns (uint112) {
if (value > type(uint112).max) {
revert SafeCastOverflowedUintDowncast(112, value);
}
return uint112(value);
}
/**
* @dev Returns the downcasted uint104 from uint256, reverting on
* overflow (when the input is greater than largest uint104).
*
* Counterpart to Solidity's `uint104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*/
function toUint104(uint256 value) internal pure returns (uint104) {
if (value > type(uint104).max) {
revert SafeCastOverflowedUintDowncast(104, value);
}
return uint104(value);
}
/**
* @dev Returns the downcasted uint96 from uint256, reverting on
* overflow (when the input is greater than largest uint96).
*
* Counterpart to Solidity's `uint96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*/
function toUint96(uint256 value) internal pure returns (uint96) {
if (value > type(uint96).max) {
revert SafeCastOverflowedUintDowncast(96, value);
}
return uint96(value);
}
/**
* @dev Returns the downcasted uint88 from uint256, reverting on
* overflow (when the input is greater than largest uint88).
*
* Counterpart to Solidity's `uint88` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*/
function toUint88(uint256 value) internal pure returns (uint88) {
if (value > type(uint88).max) {
revert SafeCastOverflowedUintDowncast(88, value);
}
return uint88(value);
}
/**
* @dev Returns the downcasted uint80 from uint256, reverting on
* overflow (when the input is greater than largest uint80).
*
* Counterpart to Solidity's `uint80` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*/
function toUint80(uint256 value) internal pure returns (uint80) {
if (value > type(uint80).max) {
revert SafeCastOverflowedUintDowncast(80, value);
}
return uint80(value);
}
/**
* @dev Returns the downcasted uint72 from uint256, reverting on
* overflow (when the input is greater than largest uint72).
*
* Counterpart to Solidity's `uint72` operator.
*
* Requirements:
*
* - input must fit into 72 bits
*/
function toUint72(uint256 value) internal pure returns (uint72) {
if (value > type(uint72).max) {
revert SafeCastOverflowedUintDowncast(72, value);
}
return uint72(value);
}
/**
* @dev Returns the downcasted uint64 from uint256, reverting on
* overflow (when the input is greater than largest uint64).
*
* Counterpart to Solidity's `uint64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*/
function toUint64(uint256 value) internal pure returns (uint64) {
if (value > type(uint64).max) {
revert SafeCastOverflowedUintDowncast(64, value);
}
return uint64(value);
}
/**
* @dev Returns the downcasted uint56 from uint256, reverting on
* overflow (when the input is greater than largest uint56).
*
* Counterpart to Solidity's `uint56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*/
function toUint56(uint256 value) internal pure returns (uint56) {
if (value > type(uint56).max) {
revert SafeCastOverflowedUintDowncast(56, value);
}
return uint56(value);
}
/**
* @dev Returns the downcasted uint48 from uint256, reverting on
* overflow (when the input is greater than largest uint48).
*
* Counterpart to Solidity's `uint48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*/
function toUint48(uint256 value) internal pure returns (uint48) {
if (value > type(uint48).max) {
revert SafeCastOverflowedUintDowncast(48, value);
}
return uint48(value);
}
/**
* @dev Returns the downcasted uint40 from uint256, reverting on
* overflow (when the input is greater than largest uint40).
*
* Counterpart to Solidity's `uint40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*/
function toUint40(uint256 value) internal pure returns (uint40) {
if (value > type(uint40).max) {
revert SafeCastOverflowedUintDowncast(40, value);
}
return uint40(value);
}
/**
* @dev Returns the downcasted uint32 from uint256, reverting on
* overflow (when the input is greater than largest uint32).
*
* Counterpart to Solidity's `uint32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*/
function toUint32(uint256 value) internal pure returns (uint32) {
if (value > type(uint32).max) {
revert SafeCastOverflowedUintDowncast(32, value);
}
return uint32(value);
}
/**
* @dev Returns the downcasted uint24 from uint256, reverting on
* overflow (when the input is greater than largest uint24).
*
* Counterpart to Solidity's `uint24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*/
function toUint24(uint256 value) internal pure returns (uint24) {
if (value > type(uint24).max) {
revert SafeCastOverflowedUintDowncast(24, value);
}
return uint24(value);
}
/**
* @dev Returns the downcasted uint16 from uint256, reverting on
* overflow (when the input is greater than largest uint16).
*
* Counterpart to Solidity's `uint16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*/
function toUint16(uint256 value) internal pure returns (uint16) {
if (value > type(uint16).max) {
revert SafeCastOverflowedUintDowncast(16, value);
}
return uint16(value);
}
/**
* @dev Returns the downcasted uint8 from uint256, reverting on
* overflow (when the input is greater than largest uint8).
*
* Counterpart to Solidity's `uint8` operator.
*
* Requirements:
*
* - input must fit into 8 bits
*/
function toUint8(uint256 value) internal pure returns (uint8) {
if (value > type(uint8).max) {
revert SafeCastOverflowedUintDowncast(8, value);
}
return uint8(value);
}
/**
* @dev Converts a signed int256 into an unsigned uint256.
*
* Requirements:
*
* - input must be greater than or equal to 0.
*/
function toUint256(int256 value) internal pure returns (uint256) {
if (value < 0) {
revert SafeCastOverflowedIntToUint(value);
}
return uint256(value);
}
/**
* @dev Returns the downcasted int248 from int256, reverting on
* overflow (when the input is less than smallest int248 or
* greater than largest int248).
*
* Counterpart to Solidity's `int248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*/
function toInt248(int256 value) internal pure returns (int248 downcasted) {
downcasted = int248(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(248, value);
}
}
/**
* @dev Returns the downcasted int240 from int256, reverting on
* overflow (when the input is less than smallest int240 or
* greater than largest int240).
*
* Counterpart to Solidity's `int240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*/
function toInt240(int256 value) internal pure returns (int240 downcasted) {
downcasted = int240(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(240, value);
}
}
/**
* @dev Returns the downcasted int232 from int256, reverting on
* overflow (when the input is less than smallest int232 or
* greater than largest int232).
*
* Counterpart to Solidity's `int232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*/
function toInt232(int256 value) internal pure returns (int232 downcasted) {
downcasted = int232(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(232, value);
}
}
/**
* @dev Returns the downcasted int224 from int256, reverting on
* overflow (when the input is less than smallest int224 or
* greater than largest int224).
*
* Counterpart to Solidity's `int224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*/
function toInt224(int256 value) internal pure returns (int224 downcasted) {
downcasted = int224(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(224, value);
}
}
/**
* @dev Returns the downcasted int216 from int256, reverting on
* overflow (when the input is less than smallest int216 or
* greater than largest int216).
*
* Counterpart to Solidity's `int216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*/
function toInt216(int256 value) internal pure returns (int216 downcasted) {
downcasted = int216(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(216, value);
}
}
/**
* @dev Returns the downcasted int208 from int256, reverting on
* overflow (when the input is less than smallest int208 or
* greater than largest int208).
*
* Counterpart to Solidity's `int208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*/
function toInt208(int256 value) internal pure returns (int208 downcasted) {
downcasted = int208(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(208, value);
}
}
/**
* @dev Returns the downcasted int200 from int256, reverting on
* overflow (when the input is less than smallest int200 or
* greater than largest int200).
*
* Counterpart to Solidity's `int200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*/
function toInt200(int256 value) internal pure returns (int200 downcasted) {
downcasted = int200(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(200, value);
}
}
/**
* @dev Returns the downcasted int192 from int256, reverting on
* overflow (when the input is less than smallest int192 or
* greater than largest int192).
*
* Counterpart to Solidity's `int192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*/
function toInt192(int256 value) internal pure returns (int192 downcasted) {
downcasted = int192(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(192, value);
}
}
/**
* @dev Returns the downcasted int184 from int256, reverting on
* overflow (when the input is less than smallest int184 or
* greater than largest int184).
*
* Counterpart to Solidity's `int184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*/
function toInt184(int256 value) internal pure returns (int184 downcasted) {
downcasted = int184(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(184, value);
}
}
/**
* @dev Returns the downcasted int176 from int256, reverting on
* overflow (when the input is less than smallest int176 or
* greater than largest int176).
*
* Counterpart to Solidity's `int176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*/
function toInt176(int256 value) internal pure returns (int176 downcasted) {
downcasted = int176(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(176, value);
}
}
/**
* @dev Returns the downcasted int168 from int256, reverting on
* overflow (when the input is less than smallest int168 or
* greater than largest int168).
*
* Counterpart to Solidity's `int168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*/
function toInt168(int256 value) internal pure returns (int168 downcasted) {
downcasted = int168(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(168, value);
}
}
/**
* @dev Returns the downcasted int160 from int256, reverting on
* overflow (when the input is less than smallest int160 or
* greater than largest int160).
*
* Counterpart to Solidity's `int160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*/
function toInt160(int256 value) internal pure returns (int160 downcasted) {
downcasted = int160(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(160, value);
}
}
/**
* @dev Returns the downcasted int152 from int256, reverting on
* overflow (when the input is less than smallest int152 or
* greater than largest int152).
*
* Counterpart to Solidity's `int152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*/
function toInt152(int256 value) internal pure returns (int152 downcasted) {
downcasted = int152(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(152, value);
}
}
/**
* @dev Returns the downcasted int144 from int256, reverting on
* overflow (when the input is less than smallest int144 or
* greater than largest int144).
*
* Counterpart to Solidity's `int144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*/
function toInt144(int256 value) internal pure returns (int144 downcasted) {
downcasted = int144(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(144, value);
}
}
/**
* @dev Returns the downcasted int136 from int256, reverting on
* overflow (when the input is less than smallest int136 or
* greater than largest int136).
*
* Counterpart to Solidity's `int136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*/
function toInt136(int256 value) internal pure returns (int136 downcasted) {
downcasted = int136(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(136, value);
}
}
/**
* @dev Returns the downcasted int128 from int256, reverting on
* overflow (when the input is less than smallest int128 or
* greater than largest int128).
*
* Counterpart to Solidity's `int128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*/
function toInt128(int256 value) internal pure returns (int128 downcasted) {
downcasted = int128(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(128, value);
}
}
/**
* @dev Returns the downcasted int120 from int256, reverting on
* overflow (when the input is less than smallest int120 or
* greater than largest int120).
*
* Counterpart to Solidity's `int120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*/
function toInt120(int256 value) internal pure returns (int120 downcasted) {
downcasted = int120(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(120, value);
}
}
/**
* @dev Returns the downcasted int112 from int256, reverting on
* overflow (when the input is less than smallest int112 or
* greater than largest int112).
*
* Counterpart to Solidity's `int112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*/
function toInt112(int256 value) internal pure returns (int112 downcasted) {
downcasted = int112(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(112, value);
}
}
/**
* @dev Returns the downcasted int104 from int256, reverting on
* overflow (when the input is less than smallest int104 or
* greater than largest int104).
*
* Counterpart to Solidity's `int104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*/
function toInt104(int256 value) internal pure returns (int104 downcasted) {
downcasted = int104(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(104, value);
}
}
/**
* @dev Returns the downcasted int96 from int256, reverting on
* overflow (when the input is less than smallest int96 or
* greater than largest int96).
*
* Counterpart to Solidity's `int96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*/
function toInt96(int256 value) internal pure returns (int96 downcasted) {
downcasted = int96(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(96, value);
}
}
/**
* @dev Returns the downcasted int88 from int256, reverting on
* overflow (when the input is less than smallest int88 or
* greater than largest int88).
*
* Counterpart to Solidity's `int88` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*/
function toInt88(int256 value) internal pure returns (int88 downcasted) {
downcasted = int88(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(88, value);
}
}
/**
* @dev Returns the downcasted int80 from int256, reverting on
* overflow (when the input is less than smallest int80 or
* greater than largest int80).
*
* Counterpart to Solidity's `int80` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*/
function toInt80(int256 value) internal pure returns (int80 downcasted) {
downcasted = int80(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(80, value);
}
}
/**
* @dev Returns the downcasted int72 from int256, reverting on
* overflow (when the input is less than smallest int72 or
* greater than largest int72).
*
* Counterpart to Solidity's `int72` operator.
*
* Requirements:
*
* - input must fit into 72 bits
*/
function toInt72(int256 value) internal pure returns (int72 downcasted) {
downcasted = int72(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(72, value);
}
}
/**
* @dev Returns the downcasted int64 from int256, reverting on
* overflow (when the input is less than smallest int64 or
* greater than largest int64).
*
* Counterpart to Solidity's `int64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*/
function toInt64(int256 value) internal pure returns (int64 downcasted) {
downcasted = int64(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(64, value);
}
}
/**
* @dev Returns the downcasted int56 from int256, reverting on
* overflow (when the input is less than smallest int56 or
* greater than largest int56).
*
* Counterpart to Solidity's `int56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*/
function toInt56(int256 value) internal pure returns (int56 downcasted) {
downcasted = int56(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(56, value);
}
}
/**
* @dev Returns the downcasted int48 from int256, reverting on
* overflow (when the input is less than smallest int48 or
* greater than largest int48).
*
* Counterpart to Solidity's `int48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*/
function toInt48(int256 value) internal pure returns (int48 downcasted) {
downcasted = int48(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(48, value);
}
}
/**
* @dev Returns the downcasted int40 from int256, reverting on
* overflow (when the input is less than smallest int40 or
* greater than largest int40).
*
* Counterpart to Solidity's `int40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*/
function toInt40(int256 value) internal pure returns (int40 downcasted) {
downcasted = int40(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(40, value);
}
}
/**
* @dev Returns the downcasted int32 from int256, reverting on
* overflow (when the input is less than smallest int32 or
* greater than largest int32).
*
* Counterpart to Solidity's `int32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*/
function toInt32(int256 value) internal pure returns (int32 downcasted) {
downcasted = int32(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(32, value);
}
}
/**
* @dev Returns the downcasted int24 from int256, reverting on
* overflow (when the input is less than smallest int24 or
* greater than largest int24).
*
* Counterpart to Solidity's `int24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*/
function toInt24(int256 value) internal pure returns (int24 downcasted) {
downcasted = int24(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(24, value);
}
}
/**
* @dev Returns the downcasted int16 from int256, reverting on
* overflow (when the input is less than smallest int16 or
* greater than largest int16).
*
* Counterpart to Solidity's `int16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*/
function toInt16(int256 value) internal pure returns (int16 downcasted) {
downcasted = int16(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(16, value);
}
}
/**
* @dev Returns the downcasted int8 from int256, reverting on
* overflow (when the input is less than smallest int8 or
* greater than largest int8).
*
* Counterpart to Solidity's `int8` operator.
*
* Requirements:
*
* - input must fit into 8 bits
*/
function toInt8(int256 value) internal pure returns (int8 downcasted) {
downcasted = int8(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(8, value);
}
}
/**
* @dev Converts an unsigned uint256 into a signed int256.
*
* Requirements:
*
* - input must be less than or equal to maxInt256.
*/
function toInt256(uint256 value) internal pure returns (int256) {
// Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
if (value > uint256(type(int256).max)) {
revert SafeCastOverflowedUintToInt(value);
}
return int256(value);
}
/**
* @dev Cast a boolean (false or true) to a uint256 (0 or 1) with no jump.
*/
function toUint(bool b) internal pure returns (uint256 u) {
assembly ("memory-safe") {
u := iszero(iszero(b))
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/math/SignedMath.sol)
pragma solidity ^0.8.20;
import {SafeCast} from "./SafeCast.sol";
/**
* @dev Standard signed math utilities missing in the Solidity language.
*/
library SignedMath {
/**
* @dev Branchless ternary evaluation for `a ? b : c`. Gas costs are constant.
*
* IMPORTANT: This function may reduce bytecode size and consume less gas when used standalone.
* However, the compiler may optimize Solidity ternary operations (i.e. `a ? b : c`) to only compute
* one branch when needed, making this function more expensive.
*/
function ternary(bool condition, int256 a, int256 b) internal pure returns (int256) {
unchecked {
// branchless ternary works because:
// b ^ (a ^ b) == a
// b ^ 0 == b
return b ^ ((a ^ b) * int256(SafeCast.toUint(condition)));
}
}
/**
* @dev Returns the largest of two signed numbers.
*/
function max(int256 a, int256 b) internal pure returns (int256) {
return ternary(a > b, a, b);
}
/**
* @dev Returns the smallest of two signed numbers.
*/
function min(int256 a, int256 b) internal pure returns (int256) {
return ternary(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 {
// Formula from the "Bit Twiddling Hacks" by Sean Eron Anderson.
// Since `n` is a signed integer, the generated bytecode will use the SAR opcode to perform the right shift,
// taking advantage of the most significant (or "sign" bit) in two's complement representation.
// This opcode adds new most significant bits set to the value of the previous most significant bit. As a result,
// the mask will either be `bytes32(0)` (if n is positive) or `~bytes32(0)` (if n is negative).
int256 mask = n >> 255;
// A `bytes32(0)` mask leaves the input unchanged, while a `~bytes32(0)` mask complements it.
return uint256((n + mask) ^ mask);
}
}
}// 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.1.0) (utils/Panic.sol)
pragma solidity ^0.8.20;
/**
* @dev Helper library for emitting standardized panic codes.
*
* ```solidity
* contract Example {
* using Panic for uint256;
*
* // Use any of the declared internal constants
* function foo() { Panic.GENERIC.panic(); }
*
* // Alternatively
* function foo() { Panic.panic(Panic.GENERIC); }
* }
* ```
*
* Follows the list from https://github.com/ethereum/solidity/blob/v0.8.24/libsolutil/ErrorCodes.h[libsolutil].
*
* _Available since v5.1._
*/
// slither-disable-next-line unused-state
library Panic {
/// @dev generic / unspecified error
uint256 internal constant GENERIC = 0x00;
/// @dev used by the assert() builtin
uint256 internal constant ASSERT = 0x01;
/// @dev arithmetic underflow or overflow
uint256 internal constant UNDER_OVERFLOW = 0x11;
/// @dev division or modulo by zero
uint256 internal constant DIVISION_BY_ZERO = 0x12;
/// @dev enum conversion error
uint256 internal constant ENUM_CONVERSION_ERROR = 0x21;
/// @dev invalid encoding in storage
uint256 internal constant STORAGE_ENCODING_ERROR = 0x22;
/// @dev empty array pop
uint256 internal constant EMPTY_ARRAY_POP = 0x31;
/// @dev array out of bounds access
uint256 internal constant ARRAY_OUT_OF_BOUNDS = 0x32;
/// @dev resource error (too large allocation or too large array)
uint256 internal constant RESOURCE_ERROR = 0x41;
/// @dev calling invalid internal function
uint256 internal constant INVALID_INTERNAL_FUNCTION = 0x51;
/// @dev Reverts with a panic code. Recommended to use with
/// the internal constants with predefined codes.
function panic(uint256 code) internal pure {
assembly ("memory-safe") {
mstore(0x00, 0x4e487b71)
mstore(0x20, code)
revert(0x1c, 0x24)
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/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);
assembly ("memory-safe") {
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.1.0) (utils/StorageSlot.sol)
// This file was procedurally generated from scripts/generate/templates/StorageSlot.js.
pragma solidity ^0.8.20;
/**
* @dev Library for reading and writing primitive types to specific storage slots.
*
* Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
* This library helps with reading and writing to such slots without the need for inline assembly.
*
* The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
*
* Example usage to set ERC-1967 implementation slot:
* ```solidity
* contract ERC1967 {
* // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
* bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
*
* function _getImplementation() internal view returns (address) {
* return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
* }
*
* function _setImplementation(address newImplementation) internal {
* require(newImplementation.code.length > 0);
* StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
* }
* }
* ```
*
* TIP: Consider using this library along with {SlotDerivation}.
*/
library StorageSlot {
struct AddressSlot {
address value;
}
struct BooleanSlot {
bool value;
}
struct Bytes32Slot {
bytes32 value;
}
struct Uint256Slot {
uint256 value;
}
struct Int256Slot {
int256 value;
}
struct StringSlot {
string value;
}
struct BytesSlot {
bytes value;
}
/**
* @dev Returns an `AddressSlot` with member `value` located at `slot`.
*/
function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `BooleanSlot` with member `value` located at `slot`.
*/
function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `Bytes32Slot` with member `value` located at `slot`.
*/
function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `Uint256Slot` with member `value` located at `slot`.
*/
function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `Int256Slot` with member `value` located at `slot`.
*/
function getInt256Slot(bytes32 slot) internal pure returns (Int256Slot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns a `StringSlot` with member `value` located at `slot`.
*/
function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns an `StringSlot` representation of the string storage pointer `store`.
*/
function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
assembly ("memory-safe") {
r.slot := store.slot
}
}
/**
* @dev Returns a `BytesSlot` with member `value` located at `slot`.
*/
function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
assembly ("memory-safe") {
r.slot := slot
}
}
/**
* @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
*/
function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
assembly ("memory-safe") {
r.slot := store.slot
}
}
}// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.2.0) (utils/Strings.sol)
pragma solidity ^0.8.20;
import {Math} from "./math/Math.sol";
import {SafeCast} from "./math/SafeCast.sol";
import {SignedMath} from "./math/SignedMath.sol";
/**
* @dev String operations.
*/
library Strings {
using SafeCast for *;
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 The string being parsed contains characters that are not in scope of the given base.
*/
error StringsInvalidChar();
/**
* @dev The string being parsed is not a properly formatted address.
*/
error StringsInvalidAddressFormat();
/**
* @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;
assembly ("memory-safe") {
ptr := add(buffer, add(32, length))
}
while (true) {
ptr--;
assembly ("memory-safe") {
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 Converts an `address` with fixed length of 20 bytes to its checksummed ASCII `string` hexadecimal
* representation, according to EIP-55.
*/
function toChecksumHexString(address addr) internal pure returns (string memory) {
bytes memory buffer = bytes(toHexString(addr));
// hash the hex part of buffer (skip length + 2 bytes, length 40)
uint256 hashValue;
assembly ("memory-safe") {
hashValue := shr(96, keccak256(add(buffer, 0x22), 40))
}
for (uint256 i = 41; i > 1; --i) {
// possible values for buffer[i] are 48 (0) to 57 (9) and 97 (a) to 102 (f)
if (hashValue & 0xf > 7 && uint8(buffer[i]) > 96) {
// case shift by xoring with 0x20
buffer[i] ^= 0x20;
}
hashValue >>= 4;
}
return string(buffer);
}
/**
* @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));
}
/**
* @dev Parse a decimal string and returns the value as a `uint256`.
*
* Requirements:
* - The string must be formatted as `[0-9]*`
* - The result must fit into an `uint256` type
*/
function parseUint(string memory input) internal pure returns (uint256) {
return parseUint(input, 0, bytes(input).length);
}
/**
* @dev Variant of {parseUint} that parses a substring of `input` located between position `begin` (included) and
* `end` (excluded).
*
* Requirements:
* - The substring must be formatted as `[0-9]*`
* - The result must fit into an `uint256` type
*/
function parseUint(string memory input, uint256 begin, uint256 end) internal pure returns (uint256) {
(bool success, uint256 value) = tryParseUint(input, begin, end);
if (!success) revert StringsInvalidChar();
return value;
}
/**
* @dev Variant of {parseUint-string} that returns false if the parsing fails because of an invalid character.
*
* NOTE: This function will revert if the result does not fit in a `uint256`.
*/
function tryParseUint(string memory input) internal pure returns (bool success, uint256 value) {
return _tryParseUintUncheckedBounds(input, 0, bytes(input).length);
}
/**
* @dev Variant of {parseUint-string-uint256-uint256} that returns false if the parsing fails because of an invalid
* character.
*
* NOTE: This function will revert if the result does not fit in a `uint256`.
*/
function tryParseUint(
string memory input,
uint256 begin,
uint256 end
) internal pure returns (bool success, uint256 value) {
if (end > bytes(input).length || begin > end) return (false, 0);
return _tryParseUintUncheckedBounds(input, begin, end);
}
/**
* @dev Implementation of {tryParseUint} that does not check bounds. Caller should make sure that
* `begin <= end <= input.length`. Other inputs would result in undefined behavior.
*/
function _tryParseUintUncheckedBounds(
string memory input,
uint256 begin,
uint256 end
) private pure returns (bool success, uint256 value) {
bytes memory buffer = bytes(input);
uint256 result = 0;
for (uint256 i = begin; i < end; ++i) {
uint8 chr = _tryParseChr(bytes1(_unsafeReadBytesOffset(buffer, i)));
if (chr > 9) return (false, 0);
result *= 10;
result += chr;
}
return (true, result);
}
/**
* @dev Parse a decimal string and returns the value as a `int256`.
*
* Requirements:
* - The string must be formatted as `[-+]?[0-9]*`
* - The result must fit in an `int256` type.
*/
function parseInt(string memory input) internal pure returns (int256) {
return parseInt(input, 0, bytes(input).length);
}
/**
* @dev Variant of {parseInt-string} that parses a substring of `input` located between position `begin` (included) and
* `end` (excluded).
*
* Requirements:
* - The substring must be formatted as `[-+]?[0-9]*`
* - The result must fit in an `int256` type.
*/
function parseInt(string memory input, uint256 begin, uint256 end) internal pure returns (int256) {
(bool success, int256 value) = tryParseInt(input, begin, end);
if (!success) revert StringsInvalidChar();
return value;
}
/**
* @dev Variant of {parseInt-string} that returns false if the parsing fails because of an invalid character or if
* the result does not fit in a `int256`.
*
* NOTE: This function will revert if the absolute value of the result does not fit in a `uint256`.
*/
function tryParseInt(string memory input) internal pure returns (bool success, int256 value) {
return _tryParseIntUncheckedBounds(input, 0, bytes(input).length);
}
uint256 private constant ABS_MIN_INT256 = 2 ** 255;
/**
* @dev Variant of {parseInt-string-uint256-uint256} that returns false if the parsing fails because of an invalid
* character or if the result does not fit in a `int256`.
*
* NOTE: This function will revert if the absolute value of the result does not fit in a `uint256`.
*/
function tryParseInt(
string memory input,
uint256 begin,
uint256 end
) internal pure returns (bool success, int256 value) {
if (end > bytes(input).length || begin > end) return (false, 0);
return _tryParseIntUncheckedBounds(input, begin, end);
}
/**
* @dev Implementation of {tryParseInt} that does not check bounds. Caller should make sure that
* `begin <= end <= input.length`. Other inputs would result in undefined behavior.
*/
function _tryParseIntUncheckedBounds(
string memory input,
uint256 begin,
uint256 end
) private pure returns (bool success, int256 value) {
bytes memory buffer = bytes(input);
// Check presence of a negative sign.
bytes1 sign = begin == end ? bytes1(0) : bytes1(_unsafeReadBytesOffset(buffer, begin)); // don't do out-of-bound (possibly unsafe) read if sub-string is empty
bool positiveSign = sign == bytes1("+");
bool negativeSign = sign == bytes1("-");
uint256 offset = (positiveSign || negativeSign).toUint();
(bool absSuccess, uint256 absValue) = tryParseUint(input, begin + offset, end);
if (absSuccess && absValue < ABS_MIN_INT256) {
return (true, negativeSign ? -int256(absValue) : int256(absValue));
} else if (absSuccess && negativeSign && absValue == ABS_MIN_INT256) {
return (true, type(int256).min);
} else return (false, 0);
}
/**
* @dev Parse a hexadecimal string (with or without "0x" prefix), and returns the value as a `uint256`.
*
* Requirements:
* - The string must be formatted as `(0x)?[0-9a-fA-F]*`
* - The result must fit in an `uint256` type.
*/
function parseHexUint(string memory input) internal pure returns (uint256) {
return parseHexUint(input, 0, bytes(input).length);
}
/**
* @dev Variant of {parseHexUint} that parses a substring of `input` located between position `begin` (included) and
* `end` (excluded).
*
* Requirements:
* - The substring must be formatted as `(0x)?[0-9a-fA-F]*`
* - The result must fit in an `uint256` type.
*/
function parseHexUint(string memory input, uint256 begin, uint256 end) internal pure returns (uint256) {
(bool success, uint256 value) = tryParseHexUint(input, begin, end);
if (!success) revert StringsInvalidChar();
return value;
}
/**
* @dev Variant of {parseHexUint-string} that returns false if the parsing fails because of an invalid character.
*
* NOTE: This function will revert if the result does not fit in a `uint256`.
*/
function tryParseHexUint(string memory input) internal pure returns (bool success, uint256 value) {
return _tryParseHexUintUncheckedBounds(input, 0, bytes(input).length);
}
/**
* @dev Variant of {parseHexUint-string-uint256-uint256} that returns false if the parsing fails because of an
* invalid character.
*
* NOTE: This function will revert if the result does not fit in a `uint256`.
*/
function tryParseHexUint(
string memory input,
uint256 begin,
uint256 end
) internal pure returns (bool success, uint256 value) {
if (end > bytes(input).length || begin > end) return (false, 0);
return _tryParseHexUintUncheckedBounds(input, begin, end);
}
/**
* @dev Implementation of {tryParseHexUint} that does not check bounds. Caller should make sure that
* `begin <= end <= input.length`. Other inputs would result in undefined behavior.
*/
function _tryParseHexUintUncheckedBounds(
string memory input,
uint256 begin,
uint256 end
) private pure returns (bool success, uint256 value) {
bytes memory buffer = bytes(input);
// skip 0x prefix if present
bool hasPrefix = (end > begin + 1) && bytes2(_unsafeReadBytesOffset(buffer, begin)) == bytes2("0x"); // don't do out-of-bound (possibly unsafe) read if sub-string is empty
uint256 offset = hasPrefix.toUint() * 2;
uint256 result = 0;
for (uint256 i = begin + offset; i < end; ++i) {
uint8 chr = _tryParseChr(bytes1(_unsafeReadBytesOffset(buffer, i)));
if (chr > 15) return (false, 0);
result *= 16;
unchecked {
// Multiplying by 16 is equivalent to a shift of 4 bits (with additional overflow check).
// This guaratees that adding a value < 16 will not cause an overflow, hence the unchecked.
result += chr;
}
}
return (true, result);
}
/**
* @dev Parse a hexadecimal string (with or without "0x" prefix), and returns the value as an `address`.
*
* Requirements:
* - The string must be formatted as `(0x)?[0-9a-fA-F]{40}`
*/
function parseAddress(string memory input) internal pure returns (address) {
return parseAddress(input, 0, bytes(input).length);
}
/**
* @dev Variant of {parseAddress} that parses a substring of `input` located between position `begin` (included) and
* `end` (excluded).
*
* Requirements:
* - The substring must be formatted as `(0x)?[0-9a-fA-F]{40}`
*/
function parseAddress(string memory input, uint256 begin, uint256 end) internal pure returns (address) {
(bool success, address value) = tryParseAddress(input, begin, end);
if (!success) revert StringsInvalidAddressFormat();
return value;
}
/**
* @dev Variant of {parseAddress-string} that returns false if the parsing fails because the input is not a properly
* formatted address. See {parseAddress} requirements.
*/
function tryParseAddress(string memory input) internal pure returns (bool success, address value) {
return tryParseAddress(input, 0, bytes(input).length);
}
/**
* @dev Variant of {parseAddress-string-uint256-uint256} that returns false if the parsing fails because input is not a properly
* formatted address. See {parseAddress} requirements.
*/
function tryParseAddress(
string memory input,
uint256 begin,
uint256 end
) internal pure returns (bool success, address value) {
if (end > bytes(input).length || begin > end) return (false, address(0));
bool hasPrefix = (end > begin + 1) && bytes2(_unsafeReadBytesOffset(bytes(input), begin)) == bytes2("0x"); // don't do out-of-bound (possibly unsafe) read if sub-string is empty
uint256 expectedLength = 40 + hasPrefix.toUint() * 2;
// check that input is the correct length
if (end - begin == expectedLength) {
// length guarantees that this does not overflow, and value is at most type(uint160).max
(bool s, uint256 v) = _tryParseHexUintUncheckedBounds(input, begin, end);
return (s, address(uint160(v)));
} else {
return (false, address(0));
}
}
function _tryParseChr(bytes1 chr) private pure returns (uint8) {
uint8 value = uint8(chr);
// Try to parse `chr`:
// - Case 1: [0-9]
// - Case 2: [a-f]
// - Case 3: [A-F]
// - otherwise not supported
unchecked {
if (value > 47 && value < 58) value -= 48;
else if (value > 96 && value < 103) value -= 87;
else if (value > 64 && value < 71) value -= 55;
else return type(uint8).max;
}
return value;
}
/**
* @dev Reads a bytes32 from a bytes array without bounds checking.
*
* NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the
* assembly block as such would prevent some optimizations.
*/
function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) {
// This is not memory safe in the general case, but all calls to this private function are within bounds.
assembly ("memory-safe") {
value := mload(add(buffer, add(0x20, offset)))
}
}
}// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.24;
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IBasePool } from "@balancer-labs/v3-interfaces/contracts/vault/IBasePool.sol";
import { PriceRatioState } from "../lib/ReClammMath.sol";
/// @dev Struct with data for deploying a new ReClammPool.
struct ReClammPoolParams {
string name;
string symbol;
string version;
uint256 dailyPriceShiftExponent;
uint64 centerednessMargin;
uint256 initialMinPrice;
uint256 initialMaxPrice;
uint256 initialTargetPrice;
bool tokenAPriceIncludesRate;
bool tokenBPriceIncludesRate;
}
/**
* @notice ReClamm Pool data that cannot change after deployment.
* @dev Note that the initial prices are used only during pool initialization. After the initialization, the prices
* will shift according to price ratio and pool centeredness.
*
* @param tokens Pool tokens, sorted in token registration order
* @param decimalScalingFactors Adjust for token decimals to retain calculation precision. FP(1) for 18-decimal tokens
* @param tokenAPriceIncludesRate True if the prices incorporate a rate for token A
* @param tokenBPriceIncludesRate True if the prices incorporate a rate for token B
* @param minSwapFeePercentage The minimum allowed static swap fee percentage; mitigates precision loss due to rounding
* @param maxSwapFeePercentage The maximum allowed static swap fee percentage
* @param initialMinPrice The initial minimum price of token A in terms of token B (possibly applying rates)
* @param initialMaxPrice The initial maximum price of token A in terms of token B (possibly applying rates)
* @param initialTargetPrice The initial target price of token A in terms of token B (possibly applying rates)
* @param initialDailyPriceShiftExponent The initial daily price shift exponent
* @param initialCenterednessMargin The initial centeredness margin (threshold for initiating a range update)
* @param maxCenterednessMargin The maximum centeredness margin for the pool, as an 18-decimal FP percentage
* @param maxDailyPriceShiftExponent The maximum exponent for the pool's price shift, as an 18-decimal FP percentage
* @param maxDailyPriceRatioUpdateRate The maximum percentage the price range can expand/contract per day
* @param minPriceRatioUpdateDuration The minimum duration for the price ratio update, expressed in seconds
* @param minPriceRatioDelta The minimum absolute difference between current and new fourth root price ratio
* @param balanceRatioAndPriceTolerance The maximum amount initialized pool parameters can deviate from ideal values
*/
struct ReClammPoolImmutableData {
// Base Pool
IERC20[] tokens;
uint256[] decimalScalingFactors;
bool tokenAPriceIncludesRate;
bool tokenBPriceIncludesRate;
uint256 minSwapFeePercentage;
uint256 maxSwapFeePercentage;
// Initialization
uint256 initialMinPrice;
uint256 initialMaxPrice;
uint256 initialTargetPrice;
uint256 initialDailyPriceShiftExponent;
uint256 initialCenterednessMargin;
// Operating Limits
uint256 maxCenterednessMargin;
uint256 maxDailyPriceShiftExponent;
uint256 maxDailyPriceRatioUpdateRate;
uint256 minPriceRatioUpdateDuration;
uint256 minPriceRatioDelta;
uint256 balanceRatioAndPriceTolerance;
}
/**
* @notice Snapshot of current ReClamm Pool data that can change.
* @dev Note that live balances will not necessarily be accurate if the pool is in Recovery Mode. Withdrawals
* in Recovery Mode do not make external calls (including those necessary for updating live balances), so if
* there are withdrawals, raw and live balances will be out of sync until Recovery Mode is disabled.
*
* Base Pool:
* @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 staticSwapFeePercentage 18-decimal FP value of the static swap fee percentage
* @param totalSupply The current total supply of the pool tokens (BPT)
*
* ReClamm:
* @param lastTimestamp The timestamp of the last user interaction
* @param lastVirtualBalances The last virtual balances of the pool
* @param dailyPriceShiftExponent Virtual balances will change by 2^(dailyPriceShiftExponent) per day
* @param dailyPriceShiftBase Internal time constant used to update virtual balances (1 - tau)
* @param centerednessMargin The centeredness margin of the pool
* @param currentPriceRatio The current price ratio, an interpolation of the price ratio state
* @param currentFourthRootPriceRatio The current fourth root price ratio (stored in the price ratio state)
* @param startFourthRootPriceRatio The fourth root price ratio at the start of an update
* @param endFourthRootPriceRatio The fourth root price ratio at the end of an update
* @param priceRatioUpdateStartTime The timestamp when the update begins
* @param priceRatioUpdateEndTime The timestamp when the update ends
*
* Pool State:
* @param isPoolInitialized If false, the pool has not been seeded with initial liquidity, so operations will revert
* @param isPoolPaused If true, the pool is paused, and all non-recovery-mode state-changing operations will revert
* @param isPoolInRecoveryMode If true, Recovery Mode withdrawals are enabled, and live balances may be inaccurate
*/
struct ReClammPoolDynamicData {
// Base Pool
uint256[] balancesLiveScaled18;
uint256[] tokenRates;
uint256 staticSwapFeePercentage;
uint256 totalSupply;
// ReClamm
uint256 lastTimestamp;
uint256[] lastVirtualBalances;
uint256 dailyPriceShiftExponent;
uint256 dailyPriceShiftBase;
uint256 centerednessMargin;
uint256 currentPriceRatio;
uint256 currentFourthRootPriceRatio;
uint256 startFourthRootPriceRatio;
uint256 endFourthRootPriceRatio;
uint32 priceRatioUpdateStartTime;
uint32 priceRatioUpdateEndTime;
// Pool State
bool isPoolInitialized;
bool isPoolPaused;
bool isPoolInRecoveryMode;
}
interface IReClammPool is IBasePool {
/********************************************************
Events
********************************************************/
/**
* @notice The Price Ratio State was updated.
* @dev This event will be emitted on initialization, and when governance initiates a price ratio update.
* @param startFourthRootPriceRatio The fourth root price ratio at the start of an update
* @param endFourthRootPriceRatio The fourth root price ratio at the end of an update
* @param priceRatioUpdateStartTime The timestamp when the update begins
* @param priceRatioUpdateEndTime The timestamp when the update ends
*/
event PriceRatioStateUpdated(
uint256 startFourthRootPriceRatio,
uint256 endFourthRootPriceRatio,
uint256 priceRatioUpdateStartTime,
uint256 priceRatioUpdateEndTime
);
/**
* @notice The virtual balances were updated after a user interaction (swap or liquidity operation).
* @dev Unless the price range is changing, the virtual balances remain in proportion to the real balances.
* These balances will also be updated when the centeredness margin or daily price shift exponent is changed.
*
* @param virtualBalanceA Offset to the real balance reserves
* @param virtualBalanceB Offset to the real balance reserves
*/
event VirtualBalancesUpdated(uint256 virtualBalanceA, uint256 virtualBalanceB);
/**
* @notice The daily price shift exponent was updated.
* @dev This will be emitted on deployment, and when changed by governance or the swap manager.
* @param dailyPriceShiftExponent The new daily price shift exponent
* @param dailyPriceShiftBase Internal time constant used to update virtual balances (1 - tau)
*/
event DailyPriceShiftExponentUpdated(uint256 dailyPriceShiftExponent, uint256 dailyPriceShiftBase);
/**
* @notice The centeredness margin was updated.
* @dev This will be emitted on deployment, and when changed by governance or the swap manager.
* @param centerednessMargin The new centeredness margin
*/
event CenterednessMarginUpdated(uint256 centerednessMargin);
/**
* @notice The timestamp of the last user interaction.
* @dev This is emitted on every swap or liquidity operation.
* @param lastTimestamp The timestamp of the operation
*/
event LastTimestampUpdated(uint32 lastTimestamp);
/********************************************************
Errors
********************************************************/
/// @notice The function is not implemented.
error NotImplemented();
/// @notice The centeredness margin is outside the valid numerical range.
error InvalidCenterednessMargin();
/// @notice The vault is not locked, so the pool balances are manipulable.
error VaultIsNotLocked();
/// @notice The pool is outside the target price range before or after the operation.
error PoolOutsideTargetRange();
/// @notice The start time for the price ratio update is invalid (either in the past or after the given end time).
error InvalidStartTime();
/// @notice
error InvalidInitialPrice();
/// @notice The daily price shift exponent is too high.
error DailyPriceShiftExponentTooHigh();
/// @notice The difference between end time and start time is too short for the price ratio update.
error PriceRatioUpdateDurationTooShort();
/// @notice The rate of change exceeds the maximum daily price ratio rate.
error PriceRatioUpdateTooFast();
/// @dev The price ratio being set is too close to the current one.
error PriceRatioDeltaBelowMin(uint256 fourthRootPriceRatioDelta);
/// @dev An attempt was made to stop the price ratio update while no update was in progress.
error PriceRatioNotUpdating();
/**
* @notice `getRate` from `IRateProvider` was called on a ReClamm Pool.
* @dev ReClamm Pools should never be nested. This is because the invariant of the pool is only used to calculate
* swaps. When tracking the market price or shrinking or expanding the liquidity concentration, the invariant can
* can decrease or increase independent of the balances, which makes the BPT rate meaningless.
*/
error ReClammPoolBptRateUnsupported();
/// @dev Function called before initializing the pool.
error PoolNotInitialized();
/**
* @notice The initial balances of the ReClamm Pool must respect the initialization ratio bounds.
* @dev On pool creation, a theoretical balance ratio is computed from the min, max, and target prices. During
* initialization, the actual balance ratio is compared to this theoretical value, and must fall within a fixed,
* symmetrical tolerance range, or initialization reverts. If it were outside this range, the initial price would
* diverge too far from the target price, and the pool would be vulnerable to arbitrage.
*/
error BalanceRatioExceedsTolerance();
/// @notice The current price interval or spot price is outside the initialization price range.
error WrongInitializationPrices();
/********************************************************
Pool State Getters
********************************************************/
/**
* @notice Compute the initialization amounts, given a reference token and amount.
* @dev Convenience function to compute the initial funding amount for the second token, given the first. It
* returns the amount of tokens in raw amounts, which can be used as-is to initialize the pool using a standard
* router.
*
* @param referenceToken The token whose amount is known
* @param referenceAmountInRaw The amount of the reference token to be used for initialization, in raw amounts
* @return initialBalancesRaw Initialization raw balances sorted in token registration order, including the given
* amount and a calculated raw amount for the other token
*/
function computeInitialBalancesRaw(
IERC20 referenceToken,
uint256 referenceAmountInRaw
) external view returns (uint256[] memory initialBalancesRaw);
/**
* @notice Computes the current total price range.
* @dev Prices represent the value of token A denominated in token B (i.e., how many B tokens equal the value of
* one A token).
*
* The "target" range is then defined as a subset of this total price range, with the margin trimmed symmetrically
* from each side. The pool endeavors to adjust this range as necessary to keep the current market price within it.
*
* The computation involves the current live balances (though it should not be sensitive to them), so manipulating
* the result of this function is theoretically possible while the Vault is unlocked. Ensure that the Vault is
* locked before calling this function if this side effect is undesired (does not apply to off-chain calls).
*
* @return minPrice The lower limit of the current total price range
* @return maxPrice The upper limit of the current total price range
*/
function computeCurrentPriceRange() external view returns (uint256 minPrice, uint256 maxPrice);
/**
* @notice Computes the current virtual balances and a flag indicating whether they have changed.
* @dev The current virtual balances are calculated based on the last virtual balances. If the pool is within the
* target range and the price ratio is not updating, the virtual balances will not change. If the pool is outside
* the target range, or the price ratio is updating, this function will calculate the new virtual balances based on
* the timestamp of the last user interaction. Note that virtual balances are always scaled18 values.
*
* Current virtual balances might change as a result of an operation, manipulating the value to some degree.
* Ensure that the vault is locked before calling this function if this side effect is undesired.
*
* @return currentVirtualBalanceA The current virtual balance of token A
* @return currentVirtualBalanceB The current virtual balance of token B
* @return changed Whether the current virtual balances are different from `lastVirtualBalances`
*/
function computeCurrentVirtualBalances()
external
view
returns (uint256 currentVirtualBalanceA, uint256 currentVirtualBalanceB, bool changed);
/**
* @notice Computes the current target price. This is the ratio of the total (i.e., real + virtual) balances (B/A).
* @dev Given the nature of the internal pool maths (particularly when virtual balances are shifting), it is not
* recommended to use this pool as a price oracle.
* @return currentTargetPrice Target price at the current pool state (real and virtual balances)
*/
function computeCurrentSpotPrice() external view returns (uint256 currentTargetPrice);
/**
* @notice Getter for the timestamp of the last user interaction.
* @return lastTimestamp The timestamp of the operation
*/
function getLastTimestamp() external view returns (uint32 lastTimestamp);
/**
* @notice Getter for the last virtual balances.
* @return lastVirtualBalanceA The last virtual balance of token A
* @return lastVirtualBalanceB The last virtual balance of token B
*/
function getLastVirtualBalances() external view returns (uint256 lastVirtualBalanceA, uint256 lastVirtualBalanceB);
/**
* @notice Returns the centeredness margin.
* @dev The centeredness margin defines how closely an unbalanced pool can approach the limits of the total price
* range and still be considered within the target range. The margin is symmetrical. If it's 20%, the target
* range is defined as >= 20% above the lower bound and <= 20% below the upper bound.
*
* @return centerednessMargin The current centeredness margin
*/
function getCenterednessMargin() external view returns (uint256 centerednessMargin);
/**
* @notice Returns the daily price shift exponent as an 18-decimal FP.
* @dev At 100% (FixedPoint.ONE), the price range doubles (or halves) within a day.
* @return dailyPriceShiftExponent The daily price shift exponent
*/
function getDailyPriceShiftExponent() external view returns (uint256 dailyPriceShiftExponent);
/**
* @notice Returns the internal time constant representation for the daily price shift exponent (tau).
* @dev Equals dailyPriceShiftExponent / _PRICE_SHIFT_EXPONENT_INTERNAL_ADJUSTMENT.
* @return dailyPriceShiftBase The internal representation for the daily price shift exponent
*/
function getDailyPriceShiftBase() external view returns (uint256 dailyPriceShiftBase);
/**
* @notice Returns the current price ratio state.
* @dev This includes start and end values for the fourth root price ratio, and start and end times for the update.
* @return priceRatioState The current price ratio state
*/
function getPriceRatioState() external view returns (PriceRatioState memory priceRatioState);
/**
* @notice Computes the current fourth root of price ratio.
* @dev The price ratio is the ratio of the max price to the min price, according to current real and virtual
* balances. This function returns its fourth root.
*
* @return currentFourthRootPriceRatio The current fourth root of price ratio
*/
function computeCurrentFourthRootPriceRatio() external view returns (uint256 currentFourthRootPriceRatio);
/**
* @notice Computes the current price ratio.
* @dev The price ratio is the ratio of the max price to the min price, according to current real and virtual
* balances.
*
* @return currentPriceRatio The current price ratio
*/
function computeCurrentPriceRatio() external view returns (uint256 currentPriceRatio);
/**
* @notice Compute whether the pool is within the target price range.
* @dev The pool is considered to be in the target range when the centeredness is greater than or equal to the
* centeredness margin (i.e., the price is within the subset of the total price range defined by the centeredness
* margin).
*
* Note that this function reports the state *after* the last operation. It is not very meaningful during or
* outside an operation, as the current or next operation could change it. If this is unlikely (e.g., for high-
* liquidity pools with high centeredness and small swaps), it may nonetheless be useful for some applications,
* such as off-chain indicators.
*
* The state depends on the current balances and centeredness margin, and it uses the *last* virtual balances in
* the calculation. This is fine because the real balances can only change during an operation, and the margin can
* only change through the permissioned setter - both of which update the virtual balances. So it is not possible
* for the current and last virtual balances to get out-of-sync.
*
* The range calculation is affected by the current live balances, so manipulating the result of this function
* is possible while the Vault is unlocked. Ensure that the Vault is locked before calling this function if this
* side effect is undesired (does not apply to off-chain calls).
*
* @return isWithinTargetRange True if pool centeredness is greater than or equal to the centeredness margin
*/
function isPoolWithinTargetRange() external view returns (bool isWithinTargetRange);
/**
* @notice Compute whether the pool is within the target price range, recomputing the virtual balances.
* @dev The pool is considered to be in the target range when the centeredness is greater than the centeredness
* margin (i.e., the price is within the subset of the total price range defined by the centeredness margin.)
*
* This function is identical to `isPoolWithinTargetRange` above, except that it recomputes and uses the current
* instead of the last virtual balances. As noted above, these should normally give the same result.
*
* @return isWithinTargetRange True if pool centeredness is greater than the centeredness margin
* @return virtualBalancesChanged True if the current virtual balances would not match the last virtual balances
*/
function isPoolWithinTargetRangeUsingCurrentVirtualBalances()
external
view
returns (bool isWithinTargetRange, bool virtualBalancesChanged);
/**
* @notice Compute the current pool centeredness (a measure of how unbalanced the pool is).
* @dev A value of 0 means the pool is at the edge of the price range (i.e., one of the real balances is zero).
* A value of FixedPoint.ONE means the balances (and market price) are exactly in the middle of the range.
*
* The centeredness margin is affected by the current live balances, so manipulating the result of this function
* is possible while the Vault is unlocked. Ensure that the Vault is locked before calling this function if this
* side effect is undesired (does not apply to off-chain calls).
*
* @return poolCenteredness The current centeredness margin (as a 18-decimal FP value)
* @return isPoolAboveCenter True if the pool is above the center, false otherwise
*/
function computeCurrentPoolCenteredness() external view returns (uint256 poolCenteredness, bool isPoolAboveCenter);
/**
* @notice Get dynamic pool data relevant to swap/add/remove calculations.
* @return data A struct containing all dynamic ReClamm pool parameters
*/
function getReClammPoolDynamicData() external view returns (ReClammPoolDynamicData memory data);
/**
* @notice Get immutable pool data relevant to swap/add/remove calculations.
* @return data A struct containing all immutable ReClamm pool parameters
*/
function getReClammPoolImmutableData() external view returns (ReClammPoolImmutableData memory data);
/********************************************************
Pool State Setters
********************************************************/
/**
* @notice Initiates a price ratio update by setting a new ending price ratio and time interval.
* @dev The price ratio is calculated by interpolating between the start and end times. The start price ratio will
* be set to the current fourth root price ratio of the pool. This is a permissioned function.
*
* @param endPriceRatio The new ending value of the price ratio, as a floating point value (e.g., 8 = 8e18)
* @param priceRatioUpdateStartTime The timestamp when the price ratio update will start
* @param priceRatioUpdateEndTime The timestamp when the price ratio update will end
* @return actualPriceRatioUpdateStartTime The actual start time for the price ratio update (min: block.timestamp).
*/
function startPriceRatioUpdate(
uint256 endPriceRatio,
uint256 priceRatioUpdateStartTime,
uint256 priceRatioUpdateEndTime
) external returns (uint256 actualPriceRatioUpdateStartTime);
/**
* @notice Stops an ongoing price ratio update.
* @dev The price ratio is calculated by interpolating between the start and end times. The new end price ratio
* will be set to the current one at the current timestamp, effectively pausing the update.
* This is a permissioned function.
*/
function stopPriceRatioUpdate() external;
/**
* @notice Updates the daily price shift exponent, as a 18-decimal FP percentage.
* @dev This function is considered a user interaction, and therefore recalculates the virtual balances and sets
* the last timestamp. This is a permissioned function.
*
* A percentage of 100% will make the price range double (or halve) within a day.
* A percentage of 200% will make the price range quadruple (or quartered) within a day.
*
* More generically, the new price range will be either
* Range_old * 2^(newDailyPriceShiftExponent / 100), or
* Range_old / 2^(newDailyPriceShiftExponent / 100)
*
* @param newDailyPriceShiftExponent The new daily price shift exponent
* @return actualNewDailyPriceShiftExponent The actual new daily price shift exponent, after accounting for
* precision loss incurred when dealing with the internal representation of the exponent
*/
function setDailyPriceShiftExponent(
uint256 newDailyPriceShiftExponent
) external returns (uint256 actualNewDailyPriceShiftExponent);
/**
* @notice Set the centeredness margin.
* @dev This function is considered a user action, so it will update the last timestamp and virtual balances.
* This is a permissioned function.
*
* @param newCenterednessMargin The new centeredness margin
*/
function setCenterednessMargin(uint256 newCenterednessMargin) external;
}// SPDX-License-Identifier: GPL-3.0-or-later
// solhint-disable not-rely-on-time
pragma solidity ^0.8.24;
import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
import { Rounding } from "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol";
import { FixedPoint } from "@balancer-labs/v3-solidity-utils/contracts/math/FixedPoint.sol";
struct PriceRatioState {
uint96 startFourthRootPriceRatio;
uint96 endFourthRootPriceRatio;
uint32 priceRatioUpdateStartTime;
uint32 priceRatioUpdateEndTime;
}
// ReClamm pools are always 2-token pools, and the documentation assigns the first token (in sorted order) the
// subscript `a`, and the second token `b`. Define these here to make the code more readable and self-documenting.
uint256 constant a = 0;
uint256 constant b = 1;
library ReClammMath {
using FixedPoint for uint256;
using SafeCast for *;
using ReClammMath for bool;
/// @notice The swap result is greater than the real balance of the token (i.e., the balance would drop below zero).
error AmountOutGreaterThanBalance();
// When a pool is outside the target range, we start adjusting the price range by altering the virtual balances,
// which affects the price. At a DailyPriceShiftExponent of 100%, we want to be able to change the price by a factor
// of two: either doubling or halving it over the course of a day (86,400 seconds). The virtual balances must
// change at the same rate. Therefore, if we want to double it in a day:
//
// 1. `V_next = 2*V_current`
// 2. In the equation `V_next = V_current * (1 - tau)^(n+1)`, isolate tau.
// 3. Replace `V_next` with `2*V_current` and `n` with `86400` to get `tau = 1 - pow(2, 1/(86400+1))`.
// 4. Since `tau = dailyPriceShiftExponent/x`, then `x = dailyPriceShiftExponent/tau`.
// Since dailyPriceShiftExponent = 100%, then `x = 100%/(1 - pow(2, 1/(86400+1)))`, which is 124649.
//
// This constant shall be used to scale the dailyPriceShiftExponent, which is a percentage, to the actual value of
// tau that will be used in the formula.
uint256 private constant _PRICE_SHIFT_EXPONENT_INTERNAL_ADJUSTMENT = 124649;
// We need to use a random number to calculate the initial virtual and real balances. This number will be scaled
// later, during initialization, according to the actual liquidity added. Choosing a large number will maintain
// precision when the pool is initialized with large amounts.
uint256 private constant _INITIALIZATION_MAX_BALANCE_A = 1e6 * 1e18;
/**
* @notice Get the current virtual balances and compute the invariant of the pool using constant product.
* @param balancesScaled18 Current pool balances, sorted in token registration order
* @param lastVirtualBalanceA The last virtual balance of token A
* @param lastVirtualBalanceB The last virtual balance of token B
* @param dailyPriceShiftBase Internal time constant used to update virtual balances (1 - tau)
* @param lastTimestamp The timestamp of the last user interaction with the pool
* @param centerednessMargin A symmetrical measure of how closely an unbalanced pool can approach the limits of the
* price range before it is considered outside the target range
* @param priceRatioState A struct containing start and end price ratios and a time interval
* @param rounding Rounding direction to consider when computing the invariant
* @return invariant The invariant of the pool
*/
function computeInvariant(
uint256[] memory balancesScaled18,
uint256 lastVirtualBalanceA,
uint256 lastVirtualBalanceB,
uint256 dailyPriceShiftBase,
uint32 lastTimestamp,
uint64 centerednessMargin,
PriceRatioState storage priceRatioState,
Rounding rounding
) internal view returns (uint256 invariant) {
(uint256 virtualBalanceA, uint256 virtualBalanceB, ) = computeCurrentVirtualBalances(
balancesScaled18,
lastVirtualBalanceA,
lastVirtualBalanceB,
dailyPriceShiftBase,
lastTimestamp,
centerednessMargin,
priceRatioState
);
return computeInvariant(balancesScaled18, virtualBalanceA, virtualBalanceB, rounding);
}
/**
* @notice Compute the invariant of the pool using constant product.
* @dev Note that the invariant is computed as (x+a)(y+b), without a square root. This is because the calculations
* of virtual balance updates are easier with this invariant. Unlike most other pools, the ReClamm invariant will
* change over time, if the pool is outside the target range, or the price ratio is updating, so these pools are
* not composable. Therefore, the BPT value is meaningless.
*
* Consequently, liquidity can only be added or removed proportionally, as these operations do not depend on the
* invariant. Therefore, it does not matter that the relationship between the invariant and liquidity is non-
* linear; the invariant is only used to calculate swaps.
*
* @param balancesScaled18 Current pool balances, sorted in token registration order
* @param virtualBalanceA The last virtual balance of token A
* @param virtualBalanceB The last virtual balance of token B
* @param rounding Rounding direction to consider when computing the invariant
* @return invariant The invariant of the pool
*/
function computeInvariant(
uint256[] memory balancesScaled18,
uint256 virtualBalanceA,
uint256 virtualBalanceB,
Rounding rounding
) internal pure returns (uint256) {
function(uint256, uint256) pure returns (uint256) _mulUpOrDown = rounding == Rounding.ROUND_DOWN
? FixedPoint.mulDown
: FixedPoint.mulUp;
return _mulUpOrDown((balancesScaled18[a] + virtualBalanceA), (balancesScaled18[b] + virtualBalanceB));
}
/**
* @notice Compute the `amountOut` of tokenOut in a swap, given the current balances and virtual balances.
* @param balancesScaled18 Current pool balances, sorted in token registration order
* @param virtualBalanceAе The last virtual balance of token A
* @param virtualBalanceB The last virtual balance of token B
* @param tokenInIndex Index of the token being swapped in
* @param tokenOutIndex Index of the token being swapped out
* @param amountInScaled18 The exact amount of `tokenIn` (i.e., the amount given in an ExactIn swap)
* @return amountOutScaled18 The calculated amount of `tokenOut` returned in an ExactIn swap
*/
function computeOutGivenIn(
uint256[] memory balancesScaled18,
uint256 virtualBalanceA,
uint256 virtualBalanceB,
uint256 tokenInIndex,
uint256 tokenOutIndex,
uint256 amountInScaled18
) internal pure returns (uint256 amountOutScaled18) {
// `amountOutScaled18 = currentTotalTokenOutPoolBalance - newTotalTokenOutPoolBalance`,
// where `currentTotalTokenOutPoolBalance = balancesScaled18[tokenOutIndex] + virtualBalanceTokenOut`
// and `newTotalTokenOutPoolBalance = invariant / (currentTotalTokenInPoolBalance + amountInScaled18)`.
// In other words,
// +--------------------------------------------------+
// | L |
// | Ao = Bo + Vo - --------------------- |
// | (Bi + Vi + Ai) |
// +--------------------------------------------------+
// Simplify by:
// - replacing `L = (Bo + Vo) (Bi + Vi)`, and
// - multiplying `(Bo + Vo)` by `(Bi + Vi + Ai) / (Bi + Vi + Ai)`:
// +--------------------------------------------------+
// | (Bo + Vo) Ai |
// | Ao = ------------------------------ |
// | (Bi + Vi + Ai) |
// +--------------------------------------------------+
// | Where: |
// | Ao = Amount out |
// | Bo = Balance token out |
// | Vo = Virtual balance token out |
// | Ai = Amount in |
// | Bi = Balance token in |
// | Vi = Virtual balance token in |
// +--------------------------------------------------+
(uint256 virtualBalanceTokenIn, uint256 virtualBalanceTokenOut) = tokenInIndex == a
? (virtualBalanceA, virtualBalanceB)
: (virtualBalanceB, virtualBalanceA);
amountOutScaled18 =
((balancesScaled18[tokenOutIndex] + virtualBalanceTokenOut) * amountInScaled18) /
(balancesScaled18[tokenInIndex] + virtualBalanceTokenIn + amountInScaled18);
if (amountOutScaled18 > balancesScaled18[tokenOutIndex]) {
// Amount out cannot be greater than the real balance of the token in the pool.
revert AmountOutGreaterThanBalance();
}
}
/**
* @notice Compute the `amountIn` of tokenIn in a swap, given the current balances and virtual balances.
* @param balancesScaled18 Current pool balances, sorted in token registration order
* @param virtualBalanceA The last virtual balances of token A
* @param virtualBalanceB The last virtual balances of token B
* @param tokenInIndex Index of the token being swapped in
* @param tokenOutIndex Index of the token being swapped out
* @param amountOutScaled18 The exact amount of `tokenOut` (i.e., the amount given in an ExactOut swap)
* @return amountInScaled18 The calculated amount of `tokenIn` returned in an ExactOut swap
*/
function computeInGivenOut(
uint256[] memory balancesScaled18,
uint256 virtualBalanceA,
uint256 virtualBalanceB,
uint256 tokenInIndex,
uint256 tokenOutIndex,
uint256 amountOutScaled18
) internal pure returns (uint256 amountInScaled18) {
// `amountInScaled18 = newTotalTokenOutPoolBalance - currentTotalTokenInPoolBalance`,
// where `newTotalTokenOutPoolBalance = invariant / (currentTotalTokenOutPoolBalance - amountOutScaled18)`
// and `currentTotalTokenInPoolBalance = balancesScaled18[tokenInIndex] + virtualBalanceTokenIn`.
// In other words,
// +--------------------------------------------------+
// | L |
// | Ai = --------------------- - (Bi + Vi) |
// | (Bo + Vo - Ao) |
// +--------------------------------------------------+
// Simplify by:
// - replacing `L = (Bo + Vo) (Bi + Vi)`, and
// - multiplying `(Bi + Vi)` by `(Bo + Vo - Ao) / (Bo + Vo - Ao)`:
// +--------------------------------------------------+
// | (Bi + Vi) Ao |
// | Ai = ------------------------------ |
// | (Bo + Vo - Ao) |
// +--------------------------------------------------+
// | Where: |
// | Ao = Amount out |
// | Bo = Balance token out |
// | Vo = Virtual balance token out |
// | Ai = Amount in |
// | Bi = Balance token in |
// | Vi = Virtual balance token in |
// +--------------------------------------------------+
if (amountOutScaled18 > balancesScaled18[tokenOutIndex]) {
// Amount out cannot be greater than the real balance of the token in the pool.
revert AmountOutGreaterThanBalance();
}
(uint256 virtualBalanceTokenIn, uint256 virtualBalanceTokenOut) = tokenInIndex == a
? (virtualBalanceA, virtualBalanceB)
: (virtualBalanceB, virtualBalanceA);
// Round up to favor the vault (i.e. request larger amount in from the user).
amountInScaled18 = FixedPoint.mulDivUp(
balancesScaled18[tokenInIndex] + virtualBalanceTokenIn,
amountOutScaled18,
balancesScaled18[tokenOutIndex] + virtualBalanceTokenOut - amountOutScaled18
);
}
/**
* @notice Computes the theoretical initial state of a ReClamm pool based on its price parameters.
* @dev This function calculates three key components needed to initialize a ReClamm pool:
* 1. Initial real token balances - Using a reference value (_INITIALIZATION_MAX_BALANCE_A) that will be
* scaled later during actual pool initialization based on the actual tokens provided
* 2. Initial virtual balances - Additional balances used to control the pool's price range
* 3. Price ratio - The ratio between the pool's minimum and maximum price boundaries
*
* Note: The actual balances used in pool initialization will be proportionally scaled versions
* of these theoretical values, maintaining the same ratios but adjusted to the actual amount of
* liquidity provided.
*
* Price is defined as (balanceB + virtualBalanceB) / (balanceA + virtualBalanceA),
* where A and B are the pool tokens, sorted by address (A is the token with the lowest address).
* For example, if the pool is ETH/USDC, and USDC has an address that is smaller than ETH, this price will
* be defined as ETH/USDC (meaning, how much ETH is required to buy 1 USDC).
*
* @param minPriceScaled18 The minimum price limit of the pool
* @param maxPriceScaled18 The maximum price limit of the pool
* @param targetPriceScaled18 The desired initial price point within the total price range (i.e., the midpoint)
* @return realBalancesScaled18 Array of theoretical initial token balances [tokenA, tokenB]
* @return virtualBalanceAScaled18 The theoretical initial virtual balance of token A [virtualA]
* @return virtualBalanceBScaled18 The theoretical initial virtual balance of token B [virtualB]
* @return priceRatio The ratio of the max price to the min price
*/
function computeTheoreticalPriceRatioAndBalances(
uint256 minPriceScaled18,
uint256 maxPriceScaled18,
uint256 targetPriceScaled18
)
internal
pure
returns (
uint256[] memory realBalancesScaled18,
uint256 virtualBalanceAScaled18,
uint256 virtualBalanceBScaled18,
uint256 priceRatio
)
{
priceRatio = maxPriceScaled18.divDown(minPriceScaled18);
// In the formulas below, Ra_max is a random number that defines the maximum real balance of token A, and
// consequently a random initial liquidity. We will scale all balances according to the actual amount of
// liquidity provided during initialization.
uint256 sqrtPriceRatio = sqrtScaled18(priceRatio);
// Va = Ra_max / (sqrtPriceRatio - 1)
virtualBalanceAScaled18 = _INITIALIZATION_MAX_BALANCE_A.divDown(sqrtPriceRatio - FixedPoint.ONE);
// Vb = minPrice * (Va + Ra_max)
virtualBalanceBScaled18 = minPriceScaled18.mulDown(virtualBalanceAScaled18 + _INITIALIZATION_MAX_BALANCE_A);
realBalancesScaled18 = new uint256[](2);
// Rb = sqrt(targetPrice * Vb * (Ra_max + Va)) - Vb
realBalancesScaled18[b] =
sqrtScaled18(
targetPriceScaled18.mulUp(virtualBalanceBScaled18).mulUp(
_INITIALIZATION_MAX_BALANCE_A + virtualBalanceAScaled18
)
) -
virtualBalanceBScaled18;
// Ra = (Rb + Vb - (Va * targetPrice)) / targetPrice
realBalancesScaled18[a] = (realBalancesScaled18[b] +
virtualBalanceBScaled18 -
virtualBalanceAScaled18.mulDown(targetPriceScaled18)).divDown(targetPriceScaled18);
}
/**
* @notice Calculate the current virtual balances of the pool.
* @dev If the pool is within the target range, or the price ratio is not updating, the virtual balances do not
* change, and we return lastVirtualBalances. Otherwise, follow these three steps:
*
* 1. Calculate the current fourth root of price ratio.
* 2. Shrink/Expand the price interval considering the current fourth root of price ratio (if the price ratio
* is updating).
* 3. Track the market price by moving the price interval (if the pool is outside the target range).
*
* Note: Virtual balances will be rounded down so that the swap result favors the Vault.
*
* @param balancesScaled18 Current pool balances, sorted in token registration order
* @param lastVirtualBalanceA The last virtual balance of token A
* @param lastVirtualBalanceB The last virtual balance of token B
* @param dailyPriceShiftBase Internal time constant used to update virtual balances (1 - tau)
* @param lastTimestamp The timestamp of the last user interaction with the pool
* @param centerednessMargin A limit of the pool centeredness that defines if pool is outside the target range
* @param storedPriceRatioState A struct containing start and end price ratios and a time interval
* @return currentVirtualBalanceA The current virtual balance of token A
* @return currentVirtualBalanceB The current virtual balance of token B
* @return changed Whether the virtual balances have changed and must be updated in the pool
*/
function computeCurrentVirtualBalances(
uint256[] memory balancesScaled18,
uint256 lastVirtualBalanceA,
uint256 lastVirtualBalanceB,
uint256 dailyPriceShiftBase,
uint32 lastTimestamp,
uint64 centerednessMargin,
PriceRatioState storage storedPriceRatioState
) internal view returns (uint256 currentVirtualBalanceA, uint256 currentVirtualBalanceB, bool changed) {
uint32 currentTimestamp = block.timestamp.toUint32();
// If the last timestamp is the same as the current timestamp, virtual balances were already reviewed in the
// current block.
if (lastTimestamp == currentTimestamp) {
return (lastVirtualBalanceA, lastVirtualBalanceB, false);
}
currentVirtualBalanceA = lastVirtualBalanceA;
currentVirtualBalanceB = lastVirtualBalanceB;
{
// stack-too-deep
PriceRatioState memory priceRatioState = storedPriceRatioState;
uint256 currentFourthRootPriceRatio = computeFourthRootPriceRatio(
currentTimestamp,
priceRatioState.startFourthRootPriceRatio,
priceRatioState.endFourthRootPriceRatio,
priceRatioState.priceRatioUpdateStartTime,
priceRatioState.priceRatioUpdateEndTime
);
// If the price ratio is updating, shrink/expand the price interval by recalculating the virtual balances.
if (
currentTimestamp > priceRatioState.priceRatioUpdateStartTime &&
lastTimestamp < priceRatioState.priceRatioUpdateEndTime
) {
(currentVirtualBalanceA, currentVirtualBalanceB) = computeVirtualBalancesUpdatingPriceRatio(
currentFourthRootPriceRatio,
balancesScaled18,
lastVirtualBalanceA,
lastVirtualBalanceB
);
changed = true;
}
}
(uint256 centeredness, bool isPoolAboveCenter) = computeCenteredness(
balancesScaled18,
currentVirtualBalanceA,
currentVirtualBalanceB
);
// If the pool is outside the target range, track the market price by moving the price interval.
if (centeredness < centerednessMargin) {
(currentVirtualBalanceA, currentVirtualBalanceB) = computeVirtualBalancesUpdatingPriceRange(
balancesScaled18,
currentVirtualBalanceA,
currentVirtualBalanceB,
isPoolAboveCenter,
dailyPriceShiftBase,
currentTimestamp,
lastTimestamp
);
changed = true;
}
}
/**
* @notice Compute the virtual balances of the pool when the price ratio is updating.
* @dev This function uses a Bhaskara formula to shrink/expand the price interval by recalculating the virtual
* balances. It'll keep the pool centeredness constant, and track the desired price ratio. To derive this formula,
* we need to solve the following simultaneous equations:
*
* 1. centeredness = (Ra * Vb) / (Rb * Va)
* 2. PriceRatio = invariant^2/(Va * Vb)^2 (maxPrice / minPrice)
* 3. invariant = (Va + Ra) * (Vb + Rb)
*
* Substitute [3] in [2]. Then, isolate one of the V's. Finally, replace the isolated V in [1]. We get a quadratic
* equation that will be solved in this function.
*
* @param currentFourthRootPriceRatio The current fourth root of the price ratio of the pool
* @param balancesScaled18 Current pool balances, sorted in token registration order
* @param lastVirtualBalanceA The last virtual balance of token A
* @param lastVirtualBalanceB The last virtual balance of token B
* @return virtualBalanceA The virtual balance of token A
* @return virtualBalanceB The virtual balance of token B
*/
function computeVirtualBalancesUpdatingPriceRatio(
uint256 currentFourthRootPriceRatio,
uint256[] memory balancesScaled18,
uint256 lastVirtualBalanceA,
uint256 lastVirtualBalanceB
) internal pure returns (uint256 virtualBalanceA, uint256 virtualBalanceB) {
// Compute the current pool centeredness, which will remain constant.
(uint256 poolCenteredness, bool isPoolAboveCenter) = computeCenteredness(
balancesScaled18,
lastVirtualBalanceA,
lastVirtualBalanceB
);
// The overvalued token is the one with a lower token balance (therefore, rarer and more valuable).
(
uint256 balanceTokenUndervalued,
uint256 lastVirtualBalanceUndervalued,
uint256 lastVirtualBalanceOvervalued
) = isPoolAboveCenter
? (balancesScaled18[a], lastVirtualBalanceA, lastVirtualBalanceB)
: (balancesScaled18[b], lastVirtualBalanceB, lastVirtualBalanceA);
// The original formula for Vu (Virtual balance undervalued) was a quadratic equation, with terms:
// a = Q0 - 1
// b = - Ru (1 + C)
// c = - Ru^2 C
// where Q0 is the square root of the price ratio, Ru is the undervalued token balance, and C is the
// centeredness. Applying Bhaskara, we'd have: Vu = (-b + sqrt(b^2 - 4ac)) / 2a.
// The Bhaskara above can be simplified by replacing a, b and c with the terms above, which leads to:
// +--------------------------------------------------------+
// | |
// | Ru * (1 + C + √(1 + C (C + 4 * Q0 - 2))) |
// | Vu = ---------------------------------------- |
// | 2 * (Q0 - 1) |
// | |
// +--------------------------------------------------------+
uint256 sqrtPriceRatio = currentFourthRootPriceRatio.mulDown(currentFourthRootPriceRatio);
// Using FixedPoint math as little as possible to improve the precision of the result.
// Note: The input of Math.sqrt must be a 36-decimal number, so that the final result is 18 decimals.
uint256 virtualBalanceUndervalued = (balanceTokenUndervalued *
(FixedPoint.ONE +
poolCenteredness +
Math.sqrt(poolCenteredness * (poolCenteredness + 4 * sqrtPriceRatio - 2e18) + 1e36))) /
(2 * (sqrtPriceRatio - FixedPoint.ONE));
uint256 virtualBalanceOvervalued = (virtualBalanceUndervalued * lastVirtualBalanceOvervalued) /
lastVirtualBalanceUndervalued;
(virtualBalanceA, virtualBalanceB) = isPoolAboveCenter
? (virtualBalanceUndervalued, virtualBalanceOvervalued)
: (virtualBalanceOvervalued, virtualBalanceUndervalued);
}
/**
* @notice Compute new virtual balances when the pool is outside the target range.
* @dev This function will track the market price by moving the price interval. Note that it will increase the
* pool centeredness and change the token prices.
*
* @param balancesScaled18 Current pool balances, sorted in token registration order
* @param virtualBalanceA The last virtual balance of token A
* @param virtualBalanceB The last virtual balance of token B
* @param isPoolAboveCenter Whether the pool is above or below the center of the price range
* @param dailyPriceShiftBase Internal time constant used to update virtual balances (1 - tau)
* @param currentTimestamp The current timestamp
* @param lastTimestamp The timestamp of the last user interaction with the pool
* @return newVirtualBalanceA The new virtual balance of token A
* @return newVirtualBalanceB The new virtual balance of token B
*/
function computeVirtualBalancesUpdatingPriceRange(
uint256[] memory balancesScaled18,
uint256 virtualBalanceA,
uint256 virtualBalanceB,
bool isPoolAboveCenter,
uint256 dailyPriceShiftBase,
uint32 currentTimestamp,
uint32 lastTimestamp
) internal pure returns (uint256 newVirtualBalanceA, uint256 newVirtualBalanceB) {
uint256 sqrtPriceRatio = sqrtScaled18(computePriceRatio(balancesScaled18, virtualBalanceA, virtualBalanceB));
// The overvalued token is the one with a lower token balance (therefore, rarer and more valuable).
(uint256 balancesScaledUndervalued, uint256 balancesScaledOvervalued) = isPoolAboveCenter
? (balancesScaled18[a], balancesScaled18[b])
: (balancesScaled18[b], balancesScaled18[a]);
(uint256 virtualBalanceUndervalued, uint256 virtualBalanceOvervalued) = isPoolAboveCenter
? (virtualBalanceA, virtualBalanceB)
: (virtualBalanceB, virtualBalanceA);
// +-----------------------------------------+
// | (Tc - Tl) |
// | Vo = Vo * (Psb)^ |
// +-----------------------------------------+
// | Where: |
// | Vo = Virtual balance overvalued |
// | Psb = Price shift daily rate base |
// | Tc = Current timestamp |
// | Tl = Last timestamp |
// +-----------------------------------------+
// | Ru * (Vo + Ro) |
// | Vu = ---------------------- |
// | (Qo - 1) * Vo - Ro |
// +-----------------------------------------+
// | Where: |
// | Vu = Virtual balance undervalued |
// | Vo = Virtual balance overvalued |
// | Ru = Real balance undervalued |
// | Ro = Real balance overvalued |
// | Qo = Square root of price ratio |
// +-----------------------------------------+
// Cap the duration (time between operations) at 30 days, to ensure `powDown` does not overflow.
uint256 duration = Math.min(currentTimestamp - lastTimestamp, 30 days);
virtualBalanceOvervalued = virtualBalanceOvervalued.mulDown(
dailyPriceShiftBase.powDown(duration * FixedPoint.ONE)
);
// Ensure that Vo does not go below the minimum allowed value (corresponding to centeredness == 1).
virtualBalanceOvervalued = Math.max(
virtualBalanceOvervalued,
balancesScaledOvervalued.divDown(sqrtScaled18(sqrtPriceRatio) - FixedPoint.ONE)
);
virtualBalanceUndervalued =
(balancesScaledUndervalued * (virtualBalanceOvervalued + balancesScaledOvervalued)) /
((sqrtPriceRatio - FixedPoint.ONE).mulDown(virtualBalanceOvervalued) - balancesScaledOvervalued);
(newVirtualBalanceA, newVirtualBalanceB) = isPoolAboveCenter
? (virtualBalanceUndervalued, virtualBalanceOvervalued)
: (virtualBalanceOvervalued, virtualBalanceUndervalued);
}
/**
* @notice Check whether the pool is in range.
* @dev The pool is in range if the centeredness is greater than or equal to the centeredness margin.
* @param balancesScaled18 Current pool balances, sorted in token registration order
* @param virtualBalanceA The last virtual balances of token A
* @param virtualBalanceB The last virtual balances of token B
* @param centerednessMargin A symmetrical measure of how closely an unbalanced pool can approach the limits of the
* price range before it is considered out of range
* @return isWithinTargetRange Whether the pool is within the target price range
*/
function isPoolWithinTargetRange(
uint256[] memory balancesScaled18,
uint256 virtualBalanceA,
uint256 virtualBalanceB,
uint256 centerednessMargin
) internal pure returns (bool) {
(uint256 centeredness, ) = computeCenteredness(balancesScaled18, virtualBalanceA, virtualBalanceB);
return centeredness >= centerednessMargin;
}
/**
* @notice Compute the centeredness of the pool.
* @dev The centeredness is calculated as the ratio of the real balances divided by the ratio of the virtual
* balances. It's a percentage value, where 100% means that the token prices are centered, and 0% means that the
* token prices are at the edge of the price interval.
*
* @param balancesScaled18 Current pool balances, sorted in token registration order
* @param virtualBalanceA The last virtual balances of token A
* @param virtualBalanceB The last virtual balances of token B
* @return poolCenteredness The centeredness of the pool
* @return isPoolAboveCenter True if the pool is above the center, false otherwise
*/
function computeCenteredness(
uint256[] memory balancesScaled18,
uint256 virtualBalanceA,
uint256 virtualBalanceB
) internal pure returns (uint256 poolCenteredness, bool isPoolAboveCenter) {
if (balancesScaled18[a] == 0) {
// Also return false if both are 0 to be consistent with the logic below.
return (0, false);
} else if (balancesScaled18[b] == 0) {
return (0, true);
}
uint256 numerator = balancesScaled18[a] * virtualBalanceB;
uint256 denominator = virtualBalanceA * balancesScaled18[b];
// The centeredness is defined between 0 and 1. If the numerator is greater than the denominator, we compute
// the inverse ratio.
if (numerator <= denominator) {
poolCenteredness = numerator.divDown(denominator);
isPoolAboveCenter = false;
} else {
poolCenteredness = denominator.divDown(numerator);
isPoolAboveCenter = true;
}
return (poolCenteredness, isPoolAboveCenter);
}
/**
* @notice Compute the fourth root of the price ratio of the pool.
* @dev The current fourth root of price ratio is an interpolation of the price ratio between the start and end
* values in the price ratio state, using the percentage elapsed between the start and end times.
*
* @param currentTime The current timestamp
* @param startFourthRootPriceRatio The start fourth root of price ratio of the pool
* @param endFourthRootPriceRatio The end fourth root of price ratio of the pool
* @param priceRatioUpdateStartTime The timestamp of the last user interaction with the pool
* @param priceRatioUpdateEndTime The timestamp of the next user interaction with the pool
* @return fourthRootPriceRatio The fourth root of price ratio of the pool
*/
function computeFourthRootPriceRatio(
uint32 currentTime,
uint96 startFourthRootPriceRatio,
uint96 endFourthRootPriceRatio,
uint32 priceRatioUpdateStartTime,
uint32 priceRatioUpdateEndTime
) internal pure returns (uint96) {
// if start and end time are the same, return end value.
if (currentTime >= priceRatioUpdateEndTime) {
return endFourthRootPriceRatio;
} else if (currentTime <= priceRatioUpdateStartTime) {
return startFourthRootPriceRatio;
}
// +-------------------------------------------------+
// | / Tc - Ts \ |
// | ( ------- ) |
// | \ Te - Ts / |
// | ( Pe )^ |
// | Pc = Ps * (----) |
// | ( Ps ) |
// +-------------------------------------------------+
// | Where: |
// | Pc = Current fourth root price ratio |
// | Ps = Starting fourth root price ratio |
// | Pe = Ending fourth root price ratio |
// | Tc = Current time |
// | Ts = Start time |
// | Te = End time |
// +-------------------------------------------------+
uint256 exponent = uint256(currentTime - priceRatioUpdateStartTime).divDown(
priceRatioUpdateEndTime - priceRatioUpdateStartTime
);
uint256 currentFourthRootPriceRatio = uint256(startFourthRootPriceRatio).mulDown(
(uint256(endFourthRootPriceRatio).divDown(uint256(startFourthRootPriceRatio))).powDown(exponent)
);
// Since we're rounding current fourth root price ratio down, we only need to check the lower boundary.
uint256 minimumFourthRootPriceRatio = Math.min(startFourthRootPriceRatio, endFourthRootPriceRatio);
return Math.max(minimumFourthRootPriceRatio, currentFourthRootPriceRatio).toUint96();
}
/**
* @notice Compute the price ratio of the pool by dividing the maximum price by the minimum price.
* @dev The price ratio is calculated as maxPrice/minPrice, where maxPrice and minPrice are obtained
* from computePriceRange.
*
* @param balancesScaled18 Current pool balances, sorted in token registration order
* @param virtualBalanceA Virtual balance of token A
* @param virtualBalanceB Virtual balance of token B
* @return priceRatio The ratio between the maximum and minimum prices of the pool
*/
function computePriceRatio(
uint256[] memory balancesScaled18,
uint256 virtualBalanceA,
uint256 virtualBalanceB
) internal pure returns (uint256 priceRatio) {
(uint256 minPrice, uint256 maxPrice) = computePriceRange(balancesScaled18, virtualBalanceA, virtualBalanceB);
return maxPrice.divUp(minPrice);
}
/**
* @notice Compute the minimum and maximum prices for the pool based on virtual balances and current invariant.
* @dev The minimum price is calculated as Vb^2/invariant, where Vb is the virtual balance of token B.
* The maximum price is calculated as invariant/Va^2, where Va is the virtual balance of token A.
* These calculations are derived from the invariant equation: invariant = (Ra + Va)(Rb + Vb),
* where Ra and Rb are the real balances of tokens A and B respectively.
*
* @param balancesScaled18 Current pool balances, sorted in token registration order
* @param virtualBalanceA Virtual balance of token A
* @param virtualBalanceB Virtual balance of token B
* @return minPrice The minimum price of token A in terms of token B
* @return maxPrice The maximum price of token A in terms of token B
*/
function computePriceRange(
uint256[] memory balancesScaled18,
uint256 virtualBalanceA,
uint256 virtualBalanceB
) internal pure returns (uint256 minPrice, uint256 maxPrice) {
uint256 currentInvariant = ReClammMath.computeInvariant(
balancesScaled18,
virtualBalanceA,
virtualBalanceB,
Rounding.ROUND_DOWN
);
// P_min(a) = Vb / (Va + Ra_max)
// We don't have Ra_max, but: invariant = (Ra_max + Va) * Vb
// Then, (Va + Ra_max) = invariant / Vb, and:
// P_min(a) = Vb^2 / invariant
minPrice = (virtualBalanceB * virtualBalanceB) / currentInvariant;
// Similarly, P_max(a) = (Rb_max + Vb) / Va
// We don't have Rb_max, but: invariant = (Rb_max + Vb) * Va
// Then, (Rb_max + Vb) = invariant / Va, and:
// P_max(a) = invariant / Va^2
maxPrice = currentInvariant.divDown(virtualBalanceA.mulDown(virtualBalanceA));
}
/**
* @notice Convert from the external to the internal representation of the daily price shift exponent.
* @param dailyPriceShiftExponent The daily price shift exponent as an 18-decimal FP
* @return dailyPriceShiftBase Internal time constant used to update virtual balances (1 - tau)
*/
function toDailyPriceShiftBase(uint256 dailyPriceShiftExponent) internal pure returns (uint256) {
return FixedPoint.ONE - dailyPriceShiftExponent / _PRICE_SHIFT_EXPONENT_INTERNAL_ADJUSTMENT;
}
/**
* @notice Convert from the internal to the external representation of the daily price shift exponent.
* @dev The result is an 18-decimal FP percentage.
* @param dailyPriceShiftBase Internal time constant used to update virtual balances (1 - tau)
* @return dailyPriceShiftExponent The daily price shift exponent as an 18-decimal FP percentage
*/
function toDailyPriceShiftExponent(uint256 dailyPriceShiftBase) internal pure returns (uint256) {
return (FixedPoint.ONE - dailyPriceShiftBase) * _PRICE_SHIFT_EXPONENT_INTERNAL_ADJUSTMENT;
}
/**
* @notice Calculate the square root of a value scaled by 18 decimals.
* @param valueScaled18 The value to calculate the square root of, scaled by 18 decimals
* @return sqrtValueScaled18 The square root of the value scaled by 18 decimals
*/
function sqrtScaled18(uint256 valueScaled18) internal pure returns (uint256) {
return Math.sqrt(valueScaled18 * FixedPoint.ONE);
}
/**
* @notice Calculate the fourth root of a value scaled by 18 decimals.
* @param valueScaled18 The value to calculate the fourth root of, scaled by 18 decimals
* @return fourthRootValueScaled18 The fourth root of the value scaled by 18 decimals
*/
function fourthRootScaled18(uint256 valueScaled18) internal pure returns (uint256) {
return Math.sqrt(Math.sqrt(valueScaled18 * FixedPoint.ONE) * FixedPoint.ONE);
}
}{
"viaIR": true,
"evmVersion": "cancun",
"optimizer": {
"enabled": true,
"runs": 700,
"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
Contract ABI
API[{"inputs":[{"components":[{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"symbol","type":"string"},{"internalType":"string","name":"version","type":"string"},{"internalType":"uint256","name":"dailyPriceShiftExponent","type":"uint256"},{"internalType":"uint64","name":"centerednessMargin","type":"uint64"},{"internalType":"uint256","name":"initialMinPrice","type":"uint256"},{"internalType":"uint256","name":"initialMaxPrice","type":"uint256"},{"internalType":"uint256","name":"initialTargetPrice","type":"uint256"},{"internalType":"bool","name":"tokenAPriceIncludesRate","type":"bool"},{"internalType":"bool","name":"tokenBPriceIncludesRate","type":"bool"}],"internalType":"struct ReClammPoolParams","name":"params","type":"tuple"},{"internalType":"contract IVault","name":"vault","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"AmountOutGreaterThanBalance","type":"error"},{"inputs":[],"name":"BalanceRatioExceedsTolerance","type":"error"},{"inputs":[],"name":"BaseOutOfBounds","type":"error"},{"inputs":[],"name":"DailyPriceShiftExponentTooHigh","type":"error"},{"inputs":[],"name":"ECDSAInvalidSignature","type":"error"},{"inputs":[{"internalType":"uint256","name":"length","type":"uint256"}],"name":"ECDSAInvalidSignatureLength","type":"error"},{"inputs":[{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"ECDSAInvalidSignatureS","type":"error"},{"inputs":[{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"ERC2612ExpiredSignature","type":"error"},{"inputs":[{"internalType":"address","name":"signer","type":"address"},{"internalType":"address","name":"owner","type":"address"}],"name":"ERC2612InvalidSigner","type":"error"},{"inputs":[],"name":"ExponentOutOfBounds","type":"error"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"currentNonce","type":"uint256"}],"name":"InvalidAccountNonce","type":"error"},{"inputs":[],"name":"InvalidCenterednessMargin","type":"error"},{"inputs":[],"name":"InvalidExponent","type":"error"},{"inputs":[],"name":"InvalidInitialPrice","type":"error"},{"inputs":[],"name":"InvalidShortString","type":"error"},{"inputs":[{"internalType":"uint256","name":"resolvedStartTime","type":"uint256"},{"internalType":"uint256","name":"endTime","type":"uint256"}],"name":"InvalidStartTime","type":"error"},{"inputs":[],"name":"InvalidStartTime","type":"error"},{"inputs":[],"name":"InvalidToken","type":"error"},{"inputs":[],"name":"NotImplemented","type":"error"},{"inputs":[],"name":"PoolNotInitialized","type":"error"},{"inputs":[],"name":"PoolOutsideTargetRange","type":"error"},{"inputs":[{"internalType":"uint256","name":"fourthRootPriceRatioDelta","type":"uint256"}],"name":"PriceRatioDeltaBelowMin","type":"error"},{"inputs":[],"name":"PriceRatioNotUpdating","type":"error"},{"inputs":[],"name":"PriceRatioUpdateDurationTooShort","type":"error"},{"inputs":[],"name":"PriceRatioUpdateTooFast","type":"error"},{"inputs":[],"name":"ProductOutOfBounds","type":"error"},{"inputs":[],"name":"ReClammPoolBptRateUnsupported","type":"error"},{"inputs":[{"internalType":"uint8","name":"bits","type":"uint8"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"SafeCastOverflowedUintDowncast","type":"error"},{"inputs":[{"internalType":"address","name":"sender","type":"address"}],"name":"SenderIsNotVault","type":"error"},{"inputs":[],"name":"SenderNotAllowed","type":"error"},{"inputs":[{"internalType":"string","name":"str","type":"string"}],"name":"StringTooLong","type":"error"},{"inputs":[],"name":"VaultIsNotLocked","type":"error"},{"inputs":[],"name":"VaultNotSet","type":"error"},{"inputs":[],"name":"WrongInitializationPrices","type":"error"},{"inputs":[],"name":"ZeroDivision","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"centerednessMargin","type":"uint256"}],"name":"CenterednessMarginUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"dailyPriceShiftExponent","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"dailyPriceShiftBase","type":"uint256"}],"name":"DailyPriceShiftExponentUpdated","type":"event"},{"anonymous":false,"inputs":[],"name":"EIP712DomainChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint32","name":"lastTimestamp","type":"uint32"}],"name":"LastTimestampUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"startFourthRootPriceRatio","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"endFourthRootPriceRatio","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"priceRatioUpdateStartTime","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"priceRatioUpdateEndTime","type":"uint256"}],"name":"PriceRatioStateUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"virtualBalanceA","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"virtualBalanceB","type":"uint256"}],"name":"VirtualBalancesUpdated","type":"event"},{"inputs":[],"name":"DOMAIN_SEPARATOR","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"PERMIT_TYPEHASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"computeBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"computeCurrentFourthRootPriceRatio","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"computeCurrentPoolCenteredness","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"computeCurrentPriceRange","outputs":[{"internalType":"uint256","name":"minPrice","type":"uint256"},{"internalType":"uint256","name":"maxPrice","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"computeCurrentPriceRatio","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"computeCurrentSpotPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"computeCurrentVirtualBalances","outputs":[{"internalType":"uint256","name":"currentVirtualBalanceA","type":"uint256"},{"internalType":"uint256","name":"currentVirtualBalanceB","type":"uint256"},{"internalType":"bool","name":"changed","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"referenceToken","type":"address"},{"internalType":"uint256","name":"referenceAmountInRaw","type":"uint256"}],"name":"computeInitialBalancesRaw","outputs":[{"internalType":"uint256[]","name":"initialBalancesRaw","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"balancesScaled18","type":"uint256[]"},{"internalType":"enum Rounding","name":"rounding","type":"uint8"}],"name":"computeInvariant","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"eip712Domain","outputs":[{"internalType":"bytes1","name":"fields","type":"bytes1"},{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"version","type":"string"},{"internalType":"uint256","name":"chainId","type":"uint256"},{"internalType":"address","name":"verifyingContract","type":"address"},{"internalType":"bytes32","name":"salt","type":"bytes32"},{"internalType":"uint256[]","name":"extensions","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"emitApproval","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"emitTransfer","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":[],"name":"getAggregateFeePercentages","outputs":[{"internalType":"uint256","name":"aggregateSwapFeePercentage","type":"uint256"},{"internalType":"uint256","name":"aggregateYieldFeePercentage","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCenterednessMargin","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCurrentLiveBalances","outputs":[{"internalType":"uint256[]","name":"balancesLiveScaled18","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getDailyPriceShiftBase","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getDailyPriceShiftExponent","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getHookFlags","outputs":[{"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":"struct HookFlags","name":"hookFlags","type":"tuple"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getLastTimestamp","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getLastVirtualBalances","outputs":[{"internalType":"uint256","name":"virtualBalanceA","type":"uint256"},{"internalType":"uint256","name":"virtualBalanceB","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getMaximumInvariantRatio","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getMaximumSwapFeePercentage","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getMinimumInvariantRatio","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getMinimumSwapFeePercentage","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getPriceRatioState","outputs":[{"components":[{"internalType":"uint96","name":"startFourthRootPriceRatio","type":"uint96"},{"internalType":"uint96","name":"endFourthRootPriceRatio","type":"uint96"},{"internalType":"uint32","name":"priceRatioUpdateStartTime","type":"uint32"},{"internalType":"uint32","name":"priceRatioUpdateEndTime","type":"uint32"}],"internalType":"struct PriceRatioState","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getReClammPoolDynamicData","outputs":[{"components":[{"internalType":"uint256[]","name":"balancesLiveScaled18","type":"uint256[]"},{"internalType":"uint256[]","name":"tokenRates","type":"uint256[]"},{"internalType":"uint256","name":"staticSwapFeePercentage","type":"uint256"},{"internalType":"uint256","name":"totalSupply","type":"uint256"},{"internalType":"uint256","name":"lastTimestamp","type":"uint256"},{"internalType":"uint256[]","name":"lastVirtualBalances","type":"uint256[]"},{"internalType":"uint256","name":"dailyPriceShiftExponent","type":"uint256"},{"internalType":"uint256","name":"dailyPriceShiftBase","type":"uint256"},{"internalType":"uint256","name":"centerednessMargin","type":"uint256"},{"internalType":"uint256","name":"currentPriceRatio","type":"uint256"},{"internalType":"uint256","name":"currentFourthRootPriceRatio","type":"uint256"},{"internalType":"uint256","name":"startFourthRootPriceRatio","type":"uint256"},{"internalType":"uint256","name":"endFourthRootPriceRatio","type":"uint256"},{"internalType":"uint32","name":"priceRatioUpdateStartTime","type":"uint32"},{"internalType":"uint32","name":"priceRatioUpdateEndTime","type":"uint32"},{"internalType":"bool","name":"isPoolInitialized","type":"bool"},{"internalType":"bool","name":"isPoolPaused","type":"bool"},{"internalType":"bool","name":"isPoolInRecoveryMode","type":"bool"}],"internalType":"struct ReClammPoolDynamicData","name":"data","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getReClammPoolImmutableData","outputs":[{"components":[{"internalType":"contract IERC20[]","name":"tokens","type":"address[]"},{"internalType":"uint256[]","name":"decimalScalingFactors","type":"uint256[]"},{"internalType":"bool","name":"tokenAPriceIncludesRate","type":"bool"},{"internalType":"bool","name":"tokenBPriceIncludesRate","type":"bool"},{"internalType":"uint256","name":"minSwapFeePercentage","type":"uint256"},{"internalType":"uint256","name":"maxSwapFeePercentage","type":"uint256"},{"internalType":"uint256","name":"initialMinPrice","type":"uint256"},{"internalType":"uint256","name":"initialMaxPrice","type":"uint256"},{"internalType":"uint256","name":"initialTargetPrice","type":"uint256"},{"internalType":"uint256","name":"initialDailyPriceShiftExponent","type":"uint256"},{"internalType":"uint256","name":"initialCenterednessMargin","type":"uint256"},{"internalType":"uint256","name":"maxCenterednessMargin","type":"uint256"},{"internalType":"uint256","name":"maxDailyPriceShiftExponent","type":"uint256"},{"internalType":"uint256","name":"maxDailyPriceRatioUpdateRate","type":"uint256"},{"internalType":"uint256","name":"minPriceRatioUpdateDuration","type":"uint256"},{"internalType":"uint256","name":"minPriceRatioDelta","type":"uint256"},{"internalType":"uint256","name":"balanceRatioAndPriceTolerance","type":"uint256"}],"internalType":"struct ReClammPoolImmutableData","name":"data","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getStaticSwapFeePercentage","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getTokenInfo","outputs":[{"internalType":"contract IERC20[]","name":"tokens","type":"address[]"},{"components":[{"internalType":"enum TokenType","name":"tokenType","type":"uint8"},{"internalType":"contract IRateProvider","name":"rateProvider","type":"address"},{"internalType":"bool","name":"paysYieldFees","type":"bool"}],"internalType":"struct TokenInfo[]","name":"tokenInfo","type":"tuple[]"},{"internalType":"uint256[]","name":"balancesRaw","type":"uint256[]"},{"internalType":"uint256[]","name":"lastBalancesLiveScaled18","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getTokens","outputs":[{"internalType":"contract IERC20[]","name":"tokens","type":"address[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getVault","outputs":[{"internalType":"contract IVault","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"incrementNonce","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"isPoolWithinTargetRange","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"isPoolWithinTargetRangeUsingCurrentVirtualBalances","outputs":[{"internalType":"bool","name":"isWithinTargetRange","type":"bool"},{"internalType":"bool","name":"virtualBalancesChanged","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"nonces","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"},{"internalType":"enum AddLiquidityKind","name":"","type":"uint8"},{"internalType":"uint256[]","name":"","type":"uint256[]"},{"internalType":"uint256[]","name":"amountsInRaw","type":"uint256[]"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256[]","name":"","type":"uint256[]"},{"internalType":"bytes","name":"","type":"bytes"}],"name":"onAfterAddLiquidity","outputs":[{"internalType":"bool","name":"","type":"bool"},{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"bytes","name":"","type":"bytes"}],"name":"onAfterInitialize","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"},{"internalType":"enum RemoveLiquidityKind","name":"","type":"uint8"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256[]","name":"","type":"uint256[]"},{"internalType":"uint256[]","name":"amountsOutRaw","type":"uint256[]"},{"internalType":"uint256[]","name":"","type":"uint256[]"},{"internalType":"bytes","name":"","type":"bytes"}],"name":"onAfterRemoveLiquidity","outputs":[{"internalType":"bool","name":"","type":"bool"},{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"enum SwapKind","name":"kind","type":"uint8"},{"internalType":"contract IERC20","name":"tokenIn","type":"address"},{"internalType":"contract IERC20","name":"tokenOut","type":"address"},{"internalType":"uint256","name":"amountInScaled18","type":"uint256"},{"internalType":"uint256","name":"amountOutScaled18","type":"uint256"},{"internalType":"uint256","name":"tokenInBalanceScaled18","type":"uint256"},{"internalType":"uint256","name":"tokenOutBalanceScaled18","type":"uint256"},{"internalType":"uint256","name":"amountCalculatedScaled18","type":"uint256"},{"internalType":"uint256","name":"amountCalculatedRaw","type":"uint256"},{"internalType":"address","name":"router","type":"address"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"bytes","name":"userData","type":"bytes"}],"internalType":"struct AfterSwapParams","name":"","type":"tuple"}],"name":"onAfterSwap","outputs":[{"internalType":"bool","name":"","type":"bool"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum AddLiquidityKind","name":"","type":"uint8"},{"internalType":"uint256[]","name":"","type":"uint256[]"},{"internalType":"uint256","name":"exactBptAmountOut","type":"uint256"},{"internalType":"uint256[]","name":"balancesScaled18","type":"uint256[]"},{"internalType":"bytes","name":"","type":"bytes"}],"name":"onBeforeAddLiquidity","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"balancesScaled18","type":"uint256[]"},{"internalType":"bytes","name":"","type":"bytes"}],"name":"onBeforeInitialize","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum RemoveLiquidityKind","name":"","type":"uint8"},{"internalType":"uint256","name":"exactBptAmountIn","type":"uint256"},{"internalType":"uint256[]","name":"","type":"uint256[]"},{"internalType":"uint256[]","name":"balancesScaled18","type":"uint256[]"},{"internalType":"bytes","name":"","type":"bytes"}],"name":"onBeforeRemoveLiquidity","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"enum SwapKind","name":"kind","type":"uint8"},{"internalType":"uint256","name":"amountGivenScaled18","type":"uint256"},{"internalType":"uint256[]","name":"balancesScaled18","type":"uint256[]"},{"internalType":"uint256","name":"indexIn","type":"uint256"},{"internalType":"uint256","name":"indexOut","type":"uint256"},{"internalType":"address","name":"router","type":"address"},{"internalType":"bytes","name":"userData","type":"bytes"}],"internalType":"struct PoolSwapParams","name":"","type":"tuple"},{"internalType":"address","name":"","type":"address"}],"name":"onBeforeSwap","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"enum SwapKind","name":"kind","type":"uint8"},{"internalType":"uint256","name":"amountGivenScaled18","type":"uint256"},{"internalType":"uint256[]","name":"balancesScaled18","type":"uint256[]"},{"internalType":"uint256","name":"indexIn","type":"uint256"},{"internalType":"uint256","name":"indexOut","type":"uint256"},{"internalType":"address","name":"router","type":"address"},{"internalType":"bytes","name":"userData","type":"bytes"}],"internalType":"struct PoolSwapParams","name":"","type":"tuple"},{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"onComputeDynamicSwapFeePercentage","outputs":[{"internalType":"bool","name":"","type":"bool"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","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"}],"internalType":"struct TokenConfig[]","name":"tokenConfig","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"}],"internalType":"struct LiquidityManagement","name":"liquidityManagement","type":"tuple"}],"name":"onRegister","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[{"components":[{"internalType":"enum SwapKind","name":"kind","type":"uint8"},{"internalType":"uint256","name":"amountGivenScaled18","type":"uint256"},{"internalType":"uint256[]","name":"balancesScaled18","type":"uint256[]"},{"internalType":"uint256","name":"indexIn","type":"uint256"},{"internalType":"uint256","name":"indexOut","type":"uint256"},{"internalType":"address","name":"router","type":"address"},{"internalType":"bytes","name":"userData","type":"bytes"}],"internalType":"struct PoolSwapParams","name":"request","type":"tuple"}],"name":"onSwap","outputs":[{"internalType":"uint256","name":"amountCalculatedScaled18","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"permit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newCenterednessMargin","type":"uint256"}],"name":"setCenterednessMargin","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newDailyPriceShiftExponent","type":"uint256"}],"name":"setDailyPriceShiftExponent","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"endPriceRatio","type":"uint256"},{"internalType":"uint256","name":"priceRatioUpdateStartTime","type":"uint256"},{"internalType":"uint256","name":"priceRatioUpdateEndTime","type":"uint256"}],"name":"startPriceRatioUpdate","outputs":[{"internalType":"uint256","name":"actualPriceRatioUpdateStartTime","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"stopPriceRatioUpdate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"version","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}]Contract Creation Code
6103006040523461093357616c6f803803809161001e82610300610937565b610300396103000160406103008203126109335761030051906001600160401b038211610933576101408261030001820312610933576040519061014082016001600160401b03811183821017610847576040526103008301516001600160401b03811161093357816100969185610300010161095a565b82526103208301516001600160401b03811161093357816100bc9185610300010161095a565b6020830152610340830151906001600160401b038211610933576100e59184610300010161095a565b6040820181905261036083015160608301526103808301519092906001600160401b03811681036109335760808301526103a081015160a08301526103c081015160c08301526103e081015160e083015261015b906101209061014b61040082016109af565b61010085015261030001016109af565b610120820152610320516001600160a01b0381169290838103610933578251602084015160408051919691939084016001600160401b038111858210176108475760405260019384815260208101603160f81b81526101b9856109bc565b610120526101c682610b3f565b6101405284516020860120918260e05251902080610100524660a0526040519060208201927f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f8452604083015260608201524660808201523060a082015260a0815260c0810181811060018060401b03821117610847576040525190206080523060c0526101608190528251916001600160401b038311610847576003928354948686811c96168015610929575b6020871014610915578190601f968781116108c5575b50602090878311600114610866575f9261085b575b50505f1982861b1c191690861b1783555b87516001600160401b0381116108475760049889548781811c9116801561083d575b602082101461082a579081878493116107da575b5060209087831160011461077b575f92610770575b50505f1982861b1c191690861b1788555b8161018052336101a05215610762576101c0528351916001600160401b03831161074f5760059485548581811c91168015610745575b6020821014610732578281116106ef575b50602091841160011461068c579383949184925f95610681575b50501b925f19911b1c19161790555b60a08101518015908115610674575b8115610667575b8115610658575b8115610646575b8115610637575b506106285760a08101516102205260c08101516102405260e08101516102605260608101516102805260808101516001600160401b03166102a05261010081015115156102c052610120015115156102e052671bc16d674ec8000061020052604051615ff9610c76823960805181614923015260a051816149ef015260c051816148f4015260e0518161497201526101005181614998015261012051816116fc015261014051816117250152610160518181816103340152818161064b015281816108ce01528181610b1001528181610c6801528181610dea0152818161115f0152818161167b015281816117e801528181611aa00152818161208a015281816124420152818161266c015281816126b90152818161271a0152818161284701528181612b4b015281816135c901528181613b3501528181613bd301528181613de001528181613e36015281816141380152818161421d015281816145240152818161466f015281816147700152818161484301528181614a7101528181614aed0152614c30015261018051818181610a8501528181610ed401528181610f7901528181611091015261197b01526101a05181613aa401526101c0518181816109a801528181610be001528181611214015281816131950152613ccf01526101e051815050610200518181816112d50152611c6a0152610220518181816118ce01528181611b8b0152613f040152610240518181816118ef01528181611bb10152613f36015261026051818181611bd70152613f5d015261028051818181611bfe0152612dd701526102a051818181611c250152612e0101526102c051818181611b210152613ea901526102e051818181611b490152613ed10152615ff990f35b506359e6ae3360e11b5f908152fd5b905060c082015111155f6103a7565b60e083015160c08401511091506103a0565b80915060e08301511090610399565b60e0830151159150610392565b60c083015115915061038b565b015193505f8061036d565b9190601f19841692865f528460205f20945f5b888282106106d6575050106106bd575b50505050811b01905561037c565b01519060f8845f19921b161c191690555f8080806106af565b848601518855909601956020948501948893500161069f565b865f5260205f2083808701891c82019260208810610729575b01881c019086905b82811061071e575050610353565b5f8155018690610710565b92508192610708565b602289634e487b7160e01b5f525260245ffd5b90607f1690610342565b604187634e487b7160e01b5f525260245ffd5b86630647140b60e51b5f525ffd5b015190505f806102fb565b90889350601f198316918c5f5260205f20925f5b8181106107c2575084116107ab575b505050811b01885561030c565b01515f1983881b60f8161c191690555f808061079e565b8284015185558b96909401936020938401930161078f565b9091508a5f5260205f208780850160051c82019260208610610821575b918a91869594930160051c01915b8281106108135750506102e6565b5f81558594508a9101610805565b925081926107f7565b60228b634e487b7160e01b5f525260245ffd5b90607f16906102d2565b634e487b7160e01b5f52604160045260245ffd5b015190505f8061029f565b90889350601f19831691875f5260205f20925f5b8181106108ad57508411610896575b505050811b0183556102b0565b01515f1983881b60f8161c191690555f8080610889565b8284015185558b96909401936020938401930161087a565b909150855f5260205f208780850160051c8201926020861061090c575b918a91869594930160051c01915b8281106108fe57505061028a565b5f81558594508a91016108f0565b925081926108e2565b634e487b7160e01b5f52602260045260245ffd5b95607f1695610274565b5f80fd5b601f909101601f19168101906001600160401b0382119082101761084757604052565b81601f82011215610933578051906001600160401b038211610847576040519261098e601f8401601f191660200185610937565b8284526020838301011161093357815f9260208093018386015e8301015290565b5190811515820361093357565b805160209081811015610a325750601f8251116109f457808251920151908083106109e657501790565b825f19910360031b1b161790565b60448260405192839163305a27a960e01b83528160048401528051918291826024860152018484015e5f828201840152601f01601f19168101030190fd5b906001600160401b038211610847575f54926001938481811c91168015610b35575b8382101461091557601f8111610b02575b5081601f8411600114610aa057509282939183925f94610a95575b50501b915f199060031b1c1916175f5560ff90565b015192505f80610a80565b919083601f1981165f8052845f20945f905b88838310610ae85750505010610ad0575b505050811b015f5560ff90565b01515f1960f88460031b161c191690555f8080610ac3565b858701518855909601959485019487935090810190610ab2565b5f805284601f845f20920160051c820191601f860160051c015b828110610b2a575050610a65565b5f8155018590610b1c565b90607f1690610a54565b805160209081811015610b695750601f8251116109f457808251920151908083106109e657501790565b9192916001600160401b0381116108475760019182548381811c91168015610c6b575b8282101461091557601f8111610c38575b5080601f8311600114610bd85750819293945f92610bcd575b50505f19600383901b1c191690821b17905560ff90565b015190505f80610bb6565b90601f19831695845f52825f20925f905b888210610c215750508385969710610c09575b505050811b01905560ff90565b01515f1960f88460031b161c191690555f8080610bfc565b808785968294968601518155019501930190610be9565b835f5283601f835f20920160051c820191601f850160051c015b828110610c60575050610b9d565b5f8155018490610c52565b90607f1690610b8c56fe6080806040526004361015610012575f80fd5b5f3560e01c90816301ffc9a7146131f1575080630673d09a1461314957806306fdde03146130a0578063095ea7b3146130495780630b89f18214612f09578063105ae7ad14612edb57806316a0b3e014612e9a57806318160ddd14612e8057806318b6eb5514612e405780631c149e2814612bd45780631d8a5e7e14612baa578063215666a814612b8257806323b872dd14612b0057806323de665114612acf578063273c1adf14610ea55780632754888d14612a665780632c357688146125a657806330adf81f1461256c578063313ce567146125515780633644e51514612537578063378997701461251457806338be241d146124c957806345421ec7146123905780634efd88c9146123685780634f149b3f146123375780635211fa77146122f457806354fd4d501461224b5780635687f2b8146121f7578063569ee350146121d5578063627cdcb9146121ac578063654cf15d1461218a578063679aefce1461216b5780636bd838c7146120f057806370a082311461204357806372c9818614611e0e5780637ecebe0014611dd65780637f118b90146119fa57806381fa807c1461194d57806384254cf9146117c357806384b0196e146116e7578063851c1bb31461169f5780638d928af81461165c57806395d89b411461157e578063976907cc146114c6578063984de9e81461140a578063a0e8f5ac146113c2578063a103b044146113a8578063a6d2e718146111cf578063a9059cbb146110fb578063aa6ca80814611067578063abb1dc4414610f46578063b156aa0a14610eaa578063b677fa5614610ea5578063ba5f9f4014610d38578063c0209f1d14610cf7578063c480b9e614610c3e578063c66dbc4b14610b9c578063cd80a1af14610ae6578063ce20ece714610ac6578063d335b0cf14610a5a578063d4a5b63a1461096d578063d505accf14610775578063d77153a7146106b8578063dd62ed3e146105f3578063dfba3818146105cf5763e45801da146102f2575f80fd5b346105cb5760403660031901126105cb5761030b61324b565b602435906001600160a01b03906040519163ca4f280360e01b83523060048401525f83602481847f0000000000000000000000000000000000000000000000000000000000000000165afa928315610544575f936105a7575b50808061037085613657565b511692168092145f1461059e576001915f5b6001811480610589575b61057a57610398613e1b565b91906103cd6103b06103aa8584613ea7565b91613fa1565b5050506103c66103bf82613633565b5191613657565b51906140a3565b93856103d889613657565b5116036105745791925b846103ed8389613664565b51169460405197889163313ce56760e01b9788845283600460209c8d935afa928315610544578a928a915f9561054f575b509061042991613664565b51169660046040518099819382525afa958615610544575f96610512575b5060ff1660120391601283116104f757670de0b6b3a764000061048060ff9561047b6104756104b097613b0c565b8d6138be565b6138be565b04906040519961048f8b6132c4565b60028b526040368b8d01376104a4828c613664565b5261050b576001615f54565b921660120391601283116104f7576104d16104d79261047b6104de95613b0c565b906140a3565b9184613664565b526104f360405192828493845283019061344f565b0390f35b634e487b7160e01b5f52601160045260245ffd5b6003615f54565b60ff91965061053690893d8b1161053d575b61052e8183613310565b810190613af3565b9590610447565b503d610524565b6040513d5f823e3d90fd5b6104299291955061056c90853d871161053d5761052e8183613310565b94909161041e565b926103e2565b63c1ab6dc160e01b5f5260045ffd5b508261059486613633565b511682141561038c565b5f916001610382565b6105c49193503d805f833e6105bc8183613310565b810190613960565b915f610364565b5f80fd5b346105cb575f3660031901126105cb5760206105e9614c16565b6040519015158152f35b346105cb5760403660031901126105cb5761060c61324b565b6020610616613261565b60405163927da10560e01b81523060048201526001600160a01b039384166024820152908316604482015291829060649082907f0000000000000000000000000000000000000000000000000000000000000000165afa8015610544575f90610685575b602090604051908152f35b506020813d6020116106b0575b8161069f60209383613310565b810103126105cb576020905161067a565b3d9150610692565b346105cb575f3660031901126105cb576101406040516106d7816132a7565b5f81526020810190604081015f8152606082015f815260808301905f825260a084015f815260c0850160e08601915f83526101009485880194610120809901975f895260018b5260018552600187526040519a5f8c5251151560208c015251151560408b015251151560608a0152511515608089015251151560a088015251151560c087015251151560e08601525115159084015251151590820152f35b346105cb5760e03660031901126105cb5761078e61324b565b610796613261565b90604435916064359160843560ff811681036105cb5783421161095a576107d7826001600160a01b03165f52600260205260405f2080549060018201905590565b9060405160208101917f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c983526001600160a01b039687861694856040850152888816606085015289608085015260a084015260c083015260c0825260e082019082821067ffffffffffffffff83111761094657879361089293610889936040525190206108626148ea565b906040519161190160f01b83526002830152602282015260c43591604260a4359220615525565b909291926155a7565b168181036109315760405163e1f21c6760e01b81526001600160a01b03848116600483015285166024820152604481018790526020816064815f7f00000000000000000000000000000000000000000000000000000000000000008b165af18015610544576108fd57005b6020813d602011610929575b8161091660209383613310565b810103126105cb57610927906135a1565b005b3d9150610909565b6325c0072360e11b5f5260045260245260445ffd5b634e487b7160e01b5f52604160045260245ffd5b8363313c898160e11b5f5260045260245ffd5b346105cb575f3660031901126105cb57610985613b1a565b6040516374eef59360e11b81523060048201526001600160a01b036060826024817f000000000000000000000000000000000000000000000000000000000000000085165afa918215610544576109ea926020915f91610a2b575b5001511630613c78565b6109f2614ad3565b63ffffffff6060610a01613746565b0151164211610a1c576109274242610a17614829565b6141ef565b63771cef4f60e01b5f5260045ffd5b610a4d915060603d606011610a53575b610a458183613310565b810190613523565b846109e0565b503d610a3b565b346105cb575f3660031901126105cb5760405163b45090f960e01b81523060048201526020816024817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa8015610544575f9061068557602090604051908152f35b346105cb575f3660031901126105cb5760206040516509184e72a0008152f35b346105cb575f3660031901126105cb576040516333f0703b60e11b81523060048201525f816024817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa90811561054457604091610b68915f91610b76575b50600854906001600160801b038260801c921690614b6b565b825191825215156020820152f35b610b9291503d805f833e610b8a8183613310565b810190613986565b9250505083610b4f565b346105cb5760203660031901126105cb57610bb5613b1a565b610bbd613bbe565b6040516374eef59360e11b81523060048201526001600160a01b036060826024817f000000000000000000000000000000000000000000000000000000000000000085165afa91821561054457610c21926020915f91610a2b575001511630613c78565b610c29614ad3565b6020610c3660043561447e565b604051908152f35b346105cb575f3660031901126105cb576040516333f0703b60e11b81523060048201525f816024817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa8015610544576040915f91610cd9575b50610cc7610caf82614a15565b929167ffffffffffffffff60075460a01c1694614b6b565b50918351921015825215156020820152f35b610ced91503d805f833e610b8a8183613310565b9250505082610ca2565b346105cb575f3660031901126105cb576020610c36610d2c6104d1610d32610d1d614a56565b50610d2c839693949294613633565b51613894565b93613657565b346105cb5760e03660031901126105cb57610d5161324b565b50610d5a613261565b600460443510156105cb5767ffffffffffffffff6084358181116105cb57610d8690369060040161334a565b5060a4358181116105cb57610d9f90369060040161334a565b9060c4359081116105cb57602492610dbd60209236906004016133aa565b50610dc6613dd6565b6040516339370aa960e21b81526001600160a01b03918216600482015293849182907f0000000000000000000000000000000000000000000000000000000000000000165afa918215610544575f92610e6f575b610e5c610e43610e5685610e4b86610e5083610e4b610e3b60643583613ae6565b978894614a15565b5098906138be565b6138d1565b946138be565b906140c7565b610e6461471e565b602060405160018152f35b91506020823d602011610e9d575b81610e8a60209383613310565b810103126105cb57905190610e56610e1a565b3d9150610e7d565b613435565b346105cb575f3660031901126105cb576040516329ae7ec560e11b81523060048201525f816024817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa8015610544576104f3915f91610f24575b5060405191829160208352602083019061344f565b610f4091503d805f833e610f388183613310565b8101906136d9565b82610f0f565b346105cb575f3660031901126105cb576040516333f0703b60e11b81523060048201526001600160a01b03905f816024817f000000000000000000000000000000000000000000000000000000000000000086165afa8015610544575f915f935f915f93611046575b50610fc5604051946080865260808601906134b5565b6020858203818701528080885193848152019701925f905b83821061100a5787806104f389610ffc8d8b858203604087015261344f565b90838203606085015261344f565b909192939783606060019260408c518051611024816134f1565b8352808501518716858401520151151560408201520199019493920190610fdd565b925093505061105f91503d805f833e610b8a8183613310565b919385610faf565b346105cb575f3660031901126105cb5760405163ca4f280360e01b81523060048201525f816024817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa8015610544576104f3915f916110e1575b506040519182916020835260208301906134b5565b6110f591503d805f833e6105bc8183613310565b826110cc565b346105cb5760403660031901126105cb57611152602061111961324b565b6040516317d5759960e31b81523360048201526001600160a01b0390911660248083019190915235604482015291829081906064820190565b03815f6001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000165af180156105445761119657602060405160018152f35b6020813d6020116111c7575b816111af60209383613310565b810103126105cb576111c0906135a1565b5080610e64565b3d91506111a2565b346105cb5760603660031901126105cb57600435604435602480356111f2613b1a565b6040516374eef59360e11b81523060048201526001600160a01b0360608285817f000000000000000000000000000000000000000000000000000000000000000085165afa91821561054457611255926020915f91611389575001511630613c78565b8042118142180218928284116113745761126f8484613ae6565b9162015180938484106113655761128f90611288614ad3565b86846141ef565b9182811061135d578281035b620f4240811061134b5750828111156113265784810294818604149015171561131357506112d392916112cd916138be565b9061533c565b7f00000000000000000000000000000000000000000000000000000000000000001061130457602090604051908152f35b632b85f91760e11b5f5260045ffd5b634e487b7160e01b5f9081526011600452fd5b91848102948186041490151715611313575061134692916112cd916138be565b6112d3565b905063119d853760e21b5f526004525ffd5b80830361129b565b6319f8e85560e11b5f5260045ffd5b50826364bb3b8360e11b5f526004525260445ffd5b6113a2915060603d606011610a5357610a458183613310565b886109e0565b346105cb575f3660031901126105cb576020610c36614829565b346105cb576003196060368201126105cb576004359067ffffffffffffffff82116105cb5760e091360301126105cb576113fa613261565b50604080515f81525f6020820152f35b346105cb5760403660031901126105cb5767ffffffffffffffff6004358181116105cb5761143c90369060040161334a565b9060243560028110156105cb576020926114b26001611487610c3695600854906001600160801b03600754918260a01c16928163ffffffff8416938c1c16918160801c911688614fcf565b50919094611494816134f1565b036114b857610d2c6114ac6001955b610d2c86613657565b93613633565b91615f54565b610d2c6114ac6002956114a3565b346105cb576101003660031901126105cb576114e061324b565b506114e9613261565b50600560443510156105cb5767ffffffffffffffff6064358181116105cb5761151690369060040161334a565b506084358181116105cb5761152f90369060040161334a565b60c4358281116105cb5761154790369060040161334a565b5060e4359182116105cb576115636104f39236906004016133aa565b506040519182915f835260406020840152604083019061344f565b346105cb575f3660031901126105cb576040516004545f8261159f83613569565b91828252602093600190856001821691825f1461163c5750506001146115e1575b506115cd92500383613310565b6104f3604051928284938452830190613227565b84915060045f527f8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd19b905f915b8583106116245750506115cd9350820101856115c0565b8054838901850152879450869390920191810161160d565b60ff1916858201526115cd95151560051b85010192508791506115c09050565b346105cb575f3660031901126105cb5760206040516001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000168152f35b346105cb5760203660031901126105cb576004357fffffffff00000000000000000000000000000000000000000000000000000000811681036105cb57610c36602091613a9a565b346105cb575f3660031901126105cb576117207f000000000000000000000000000000000000000000000000000000000000000061536d565b6117497f0000000000000000000000000000000000000000000000000000000000000000615467565b9060405191602083019280841067ffffffffffffffff851117610946576117a36104f392611795956040525f8352604051958695600f60f81b875260e0602088015260e0870190613227565b908582036040870152613227565b904660608501523060808501525f60a085015283820360c085015261344f565b346105cb575f3660031901126105cb576040516314cb3b1f60e21b81523060048201527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690602081602481855afa908115610544575f91611913575b50156118c9575f602491604051928380926333f0703b60e11b82523060048301525afa8015610544576040915f916118ab575b5061189f610e4b670de0b6b3a764000061189861189161188861187d87614a15565b508181939299614dc6565b948186926138be565b94806138be565b04906140a3565b82519182526020820152f35b6118bf91503d805f833e610b8a8183613310565b925050508261185b565b5060407f00000000000000000000000000000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000000061189f565b90506020813d602011611945575b8161192e60209383613310565b810103126105cb5761193f906135a1565b82611828565b3d9150611921565b346105cb575f3660031901126105cb5760405163f29486a160e01b81523060048201526101a09081816024817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa908115610544576040925f926119cd575b505060608282015191015182519182526020820152f35b6119ec9250803d106119f3575b6119e48183613310565b81019061378b565b82806119b6565b503d6119da565b346105cb575f3660031901126105cb57604051610220810181811067ffffffffffffffff8211176109465760405260608152606060208201525f60408201525f60608201525f60808201525f60a08201525f60c08201525f60e08201525f6101008201525f6101208201525f6101408201525f6101608201525f6101808201525f6101a08201525f6101c08201525f6101e08201525f6102008201526001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001660405163ca4f280360e01b81523060048201525f81602481855afa918215610544576024925f928391611dbc575b508452604051633f1b0def60e11b815230600482015292839182905afa908115610544575f91611d99575b5060208201527f0000000000000000000000000000000000000000000000000000000000000000151560408201527f0000000000000000000000000000000000000000000000000000000000000000151560608201526509184e72a000608082015267016345785d8a000060a08201527f000000000000000000000000000000000000000000000000000000000000000060c08201527f000000000000000000000000000000000000000000000000000000000000000060e08201527f00000000000000000000000000000000000000000000000000000000000000006101008201527f00000000000000000000000000000000000000000000000000000000000000006101208201527f0000000000000000000000000000000000000000000000000000000000000000610140820152670c7d713b49da0000610160820152670de0b6b3a76400006101808201527f00000000000000000000000000000000000000000000000000000000000000006101a0820152620151806101c0820152620f42406101e0820152655af3107a4000610200820152604051809160208252610200611ced611cd7835161022060208701526102408601906134b5565b6020840151858203601f19016040870152613482565b91604081015115156060850152606081015115156080850152608081015160a085015260a081015160c085015260c081015160e085015260e08101516101008501526101008101516101208501526101208101516101408501526101408101516101608501526101608101516101808501526101808101516101a08501526101a08101516101c08501526101c08101516101e08501526101e08101518285015201516102208301520390f35b611db591503d805f833e611dad8183613310565b810190613702565b5082611b19565b611dd091503d8085833e6105bc8183613310565b85611aee565b346105cb5760203660031901126105cb576001600160a01b03611df761324b565b165f526002602052602060405f2054604051908152f35b346105cb576003196020368201126105cb576004359067ffffffffffffffff908183116105cb5760e09083360301126105cb576040519160e083018381108382111761094657604052806004013560028110156105cb578352602083016024820135815260448201358381116105cb57611e8e906004369185010161334a565b9360408101948552606081019360648401358552608082019360848101358552611eba60a48201613277565b60a084015260c48101359182116105cb576004611eda92369201016133aa565b60c0820152611ee7613dd6565b611ef18551614a15565b969196929092612034575b611f0461471e565b51611f0e816134f1565b611f17816134f1565b611faa57611f6b92611f60611f71969593611f6593519889975195519788945194859288155f14611f9257611f5992610d2c610d2c959361047b939792613664565b958a613664565b613894565b906138d1565b92613664565b518111611f8357602090604051908152f35b635a09f12960e11b5f5260045ffd5b93611f5992610d2c610d2c959361047b939792613664565b5193519251915193949092611fbf8387613664565b518511611f8357610d2c8593611feb611ff696611ff19585155f1461202a57610d2c9091955b8b613664565b97613664565b613ae6565b90811561201b5761200b6001916020946138be565b915f198301040190151502610c36565b630a0c22c760e01b5f5260045ffd5b94610d2c90611fe5565b61203e83886140c7565b611efc565b346105cb576020806003193601126105cb5760448161206061324b565b604051633de222bb60e21b81523060048201526001600160a01b03918216602482015292839182907f0000000000000000000000000000000000000000000000000000000000000000165afa908115610544575f916120c3575b50604051908152f35b90508181813d83116120e9575b6120da8183613310565b810103126105cb5751826120ba565b503d6120d0565b346105cb575f3660031901126105cb575f606060405161210f8161328b565b8281528260208201528260408201520152608061212a613746565b604051906bffffffffffffffffffffffff8082511683526020820151166020830152606060408201519163ffffffff80931660408501520151166060820152f35b346105cb575f3660031901126105cb576356b7fcdb60e01b5f5260045ffd5b346105cb575f3660031901126105cb57602060405167016345785d8a00008152f35b346105cb575f3660031901126105cb57335f908152600260205260409020805460018101909155005b346105cb575f3660031901126105cb576020610c366121f2614829565b6148ad565b346105cb5760207f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92561222836613400565b92919390612234613dd6565b6001600160a01b03809160405195865216941692a3005b346105cb575f3660031901126105cb576040516005545f8261226c83613569565b91828252602093600190856001821691825f1461163c57505060011461229957506115cd92500383613310565b84915060055f527f036b6384b5eca791c62761152d0c79bb0604c104a5fb6f4eb0703f3154bb3db0905f915b8583106122dc5750506115cd9350820101856115c0565b805483890185015287945086939092019181016122c5565b346105cb576003196040368201126105cb576004359067ffffffffffffffff82116105cb5760e091360301126105cb5761232c613261565b5060206040515f8152f35b346105cb575f3660031901126105cb576060612351614a56565b919250604051928352602083015215156040820152f35b346105cb575f3660031901126105cb576020610c366001600160801b03600754831c166147ff565b346105cb5760e03660031901126105cb576123a961324b565b506123b2613261565b600560443510156105cb5767ffffffffffffffff6064358181116105cb576123de90369060040161334a565b5060a4358181116105cb576123f790369060040161334a565b9060c4359081116105cb5760249261241560209236906004016133aa565b5061241e613dd6565b6040516339370aa960e21b81526001600160a01b03918216600482015293849182907f0000000000000000000000000000000000000000000000000000000000000000165afa918215610544575f92612493575b610e5c610e43610e5685610e4b86610e5083610e4b610e3b82608435613894565b91506020823d6020116124c1575b816124ae60209383613310565b810103126105cb57905190610e56612472565b3d91506124a1565b346105cb5760603660031901126105cb5767ffffffffffffffff6004358181116105cb576124fb90369060040161334a565b506044359081116105cb5761232c9036906004016133aa565b346105cb575f3660031901126105cb57602063ffffffff60075416604051908152f35b346105cb575f3660031901126105cb576020610c366148ea565b346105cb575f3660031901126105cb57602060405160128152f35b346105cb575f3660031901126105cb5760206040517f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c98152f35b346105cb575f3660031901126105cb57604051610240810181811067ffffffffffffffff8211176109465760405260608152606060208201525f60408201525f60608201525f6080820152606060a08201525f60c08201525f60e08201525f6101008201525f6101208201525f6101408201525f6101608201525f6101808201525f6101a08201525f6101c08201525f6101e08201525f6102008201525f6102208201526040516329ae7ec560e11b81523060048201525f816024816001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000165afa908115610544575f91612a4c575b508152604051633f1b0def60e11b81523060048201525f816024817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa908115610544575f91612a30575b50602082015260405163b45090f960e01b81523060048201526020816024816001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000165afa908115610544575f916129fe575b5060408201526127586135ae565b606082015267ffffffffffffffff60075463ffffffff811660808401526127cc604051612784816132c4565b600281526040366020830137600854906001600160801b03918281166127a983613657565b5260801c6127b682613633565b5260a08601528260201c168060e08601526147ff565b60c084015260a01c1661010082015263ffffffff60606127ea613746565b6bffffffffffffffffffffffff80825116610160860152602082015116610180850152826040820151166101a08501520151166101c082015260405163f29486a160e01b81523060048201526101a0816024816001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000165afa908115610544575f916129dd575b5061012060e0820151151591826101e08501526101008101511515610200850152015115156102208301526129bb575b60405180916020825261022061291d6128ce83516102406020870152610260860190613482565b6128ea602085015191601f199283888303016040890152613482565b906040850151606087015260608501516080870152608085015160a087015260a0850151908683030160c0870152613482565b9160c081015160e085015260e08101516101008501526101008101516101208501526101208101516101408501526101408101516101608501526101608101516101808501526101808101516101a085015263ffffffff6101a0820151166101c085015263ffffffff6101c0820151166101e08501526101e08101511515610200850152610200810151151582850152015115156102408301520390f35b6129d26129c6614829565b806101208401526148ad565b6101408201526128a7565b6129f891506101a03d6101a0116119f3576119e48183613310565b82612877565b90506020813d602011612a28575b81612a1960209383613310565b810103126105cb57518261274a565b3d9150612a0c565b612a4491503d805f833e611dad8183613310565b9050826126f1565b612a6091503d805f833e610f388183613310565b8261269c565b346105cb576101003660031901126105cb57612a8061324b565b50612a89613261565b50600460443510156105cb5767ffffffffffffffff6084358181116105cb57612ab690369060040161334a565b5060a4358181116105cb5761152f90369060040161334a565b346105cb5760207fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef61222836613400565b346105cb5760846020612b1236613400565b604051630aed65f560e11b81523360048201526001600160a01b0393841660248201529183166044830152606482015292839182905f907f0000000000000000000000000000000000000000000000000000000000000000165af180156105445761119657602060405160018152f35b346105cb575f3660031901126105cb5760206007546001600160801b0360405191831c168152f35b346105cb575f3660031901126105cb57602067ffffffffffffffff60075460a01c16604051908152f35b346105cb5760403660031901126105cb5767ffffffffffffffff6004358181116105cb57612c0690369060040161334a565b6024358281116105cb57612c1e9036906004016133aa565b50612c27613dd6565b60405191610120830190811183821017610946576040525f825260208201905f8252604083015f815260608401935f855260808101915f835260a08201926060845260c08301925f8452612caa612c9b60e08301925f84526101008101995f8b52612c90613e1b565b928382935252613ea7565b91828652818c52808752613fa1565b8a528352855280865297612cda612ccd612cc389613633565b516103c68a613657565b996103c66103bf82613633565b670de05bc096e9c000998a820282159b838204148c17156104f757670de0b6b3a76400009b670de111a6b7de40008085029485041417156104f7578b8091048210928315612e34575b505050612e255788612db8612da9612dc296612d9f958b85612dd19f612d7990612dc79f612da49f612d66906103c6612d5e612d6f94613657565b519151613657565b9e8f90516138be565b049c8d94516138be565b049b8c935197519951612d9f612d9286610d2c86613633565b6104d186610d2c87613657565b614d5b565b614dc6565b92612d9f84610e4b8b806138be565b61189886806138be565b6140c7565b51429042906141ef565b50612dfb7f000000000000000000000000000000000000000000000000000000000000000061447e565b50610e5c7f00000000000000000000000000000000000000000000000000000000000000006145e0565b6304f512cb60e41b5f5260045ffd5b041090508a8a81612d23565b346105cb576003196020368201126105cb576004359067ffffffffffffffff82116105cb5761018091360301126105cb57604080515f81525f6020820152f35b346105cb575f3660031901126105cb576020610c366135ae565b346105cb5760603660031901126105cb5760043567ffffffffffffffff81116105cb57612ecb90369060040161334a565b5063d623472560e01b5f5260045ffd5b346105cb575f3660031901126105cb5760406008548151906001600160801b038116825260801c6020820152f35b346105cb5760e03660031901126105cb57612f2261324b565b50612f2b613261565b5060443567ffffffffffffffff81116105cb57366023820112156105cb578060040135612f5781613332565b91612f656040519384613310565b81835260206024602085019360071b830101913683116105cb57602401925b828410612fd8578460803660631901126105cb575160021480612fc7575b80612fb5575b6020906040519015158152f35b5060c435801590811514612fa8575f80fd5b506064358015158114612fa2575f80fd5b6080843603126105cb5760405190612fef8261328b565b612ff885613277565b82528285013560028110156105cb578383015260408501356001600160a01b03811681036105cb576040830152606090818601359283151584036105cb576080938593820152815201930192612f84565b346105cb5760403660031901126105cb57611152602061306761324b565b60405163e1f21c6760e01b81523360048201526001600160a01b0390911660248083019190915235604482015291829081906064820190565b346105cb575f3660031901126105cb576040516003545f826130c183613569565b91828252602093600190856001821691825f1461163c5750506001146130ee57506115cd92500383613310565b84915060035f527fc2575a0e9e593c00f959f8c92f12db2869c3395a3b0502d05e2516446f71f85b905f915b8583106131315750506115cd9350820101856115c0565b8054838901850152879450869390920191810161311a565b346105cb5760203660031901126105cb57613162613b1a565b61316a613bbe565b613172613c5b565b6040516374eef59360e11b81523060048201526001600160a01b036060826024817f000000000000000000000000000000000000000000000000000000000000000085165afa918215610544576131d6926020915f91610a2b575001511630613c78565b6131de614ad3565b6131e96004356145e0565b610927613c5b565b346105cb5760203660031901126105cb576004359063ffffffff60e01b82168092036105cb576020916301ffc9a760e01b148152f35b805180835260209291819084018484015e5f828201840152601f01601f1916010190565b600435906001600160a01b03821682036105cb57565b602435906001600160a01b03821682036105cb57565b35906001600160a01b03821682036105cb57565b6080810190811067ffffffffffffffff82111761094657604052565b610140810190811067ffffffffffffffff82111761094657604052565b6060810190811067ffffffffffffffff82111761094657604052565b67ffffffffffffffff811161094657604052565b6040810190811067ffffffffffffffff82111761094657604052565b90601f8019910116810190811067ffffffffffffffff82111761094657604052565b67ffffffffffffffff81116109465760051b60200190565b9080601f830112156105cb57602090823561336481613332565b936133726040519586613310565b81855260208086019260051b8201019283116105cb57602001905b82821061339b575050505090565b8135815290830190830161338d565b81601f820112156105cb5780359067ffffffffffffffff821161094657604051926133df601f8401601f191660200185613310565b828452602083830101116105cb57815f926020809301838601378301015290565b60609060031901126105cb576001600160a01b039060043582811681036105cb579160243590811681036105cb579060443590565b346105cb575f3660031901126105cb5760206040515f8152f35b9081518082526020808093019301915f5b82811061346e575050505090565b835185529381019392810192600101613460565b9081518082526020808093019301915f5b8281106134a1575050505090565b835185529381019392810192600101613493565b9081518082526020808093019301915f5b8281106134d4575050505090565b83516001600160a01b0316855293810193928101926001016134c6565b600211156134fb57565b634e487b7160e01b5f52602160045260245ffd5b51906001600160a01b03821682036105cb57565b908160609103126105cb57613561604080519261353f846132c4565b6135488161350f565b84526135566020820161350f565b60208501520161350f565b604082015290565b90600182811c92168015613597575b602083101461358357565b634e487b7160e01b5f52602260045260245ffd5b91607f1691613578565b519081151582036105cb57565b6040516339370aa960e21b81523060048201526020816024817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa908115610544575f91613604575090565b90506020813d60201161362b575b8161361f60209383613310565b810103126105cb575190565b3d9150613612565b8051600110156136435760400190565b634e487b7160e01b5f52603260045260245ffd5b8051156136435760200190565b80518210156136435760209160051b010190565b9080601f830112156105cb5781519060209161369381613332565b936136a16040519586613310565b81855260208086019260051b8201019283116105cb57602001905b8282106136ca575050505090565b815181529083019083016136bc565b906020828203126105cb57815167ffffffffffffffff81116105cb576136ff9201613678565b90565b9190916040818403126105cb5780519267ffffffffffffffff938481116105cb578161372f918401613678565b9360208301519081116105cb576136ff9201613678565b604051906137538261328b565b8160606006546bffffffffffffffffffffffff808216845281831c16602084015263ffffffff8160c01c16604084015260e01c910152565b809103906101a082126105cb576080604051926137a7846132a7565b126105cb576040516137b88161328b565b6137c1826135a1565b81526137cf602083016135a1565b60208201526137e0604083016135a1565b60408201526137f1606083016135a1565b606082015282526080810151602083015260a0810151604083015260c0810151606083015260e081015164ffffffffff811681036105cb576080830152610100908181015163ffffffff811681036105cb5761388d916101809160a08601526101209361385f8583016135a1565b60c087015261387161014083016135a1565b60e087015261388361016083016135a1565b90860152016135a1565b9082015290565b919082018092116104f757565b90670de0b6b3a7640000918281029281840414901517156104f757565b818102929181159184041417156104f757565b81156138db570490565b634e487b7160e01b5f52601260045260245ffd5b9080601f830112156105cb5781519060209161390a81613332565b936139186040519586613310565b81855260208086019260051b8201019283116105cb57602001905b828210613941575050505090565b81516001600160a01b03811681036105cb578152908301908301613933565b906020828203126105cb57815167ffffffffffffffff81116105cb576136ff92016138ef565b916080838303126105cb5782519067ffffffffffffffff918281116105cb57836139b19186016138ef565b936020808201518481116105cb57820185601f820112156105cb578051906139d882613332565b9260406139e86040519586613310565b83855281850190826060809602850101938a85116105cb578301915b848310613a41575050505050509360408201518481116105cb5781613a2a918401613678565b9360608301519081116105cb576136ff9201613678565b85838c03126105cb57815190613a56826132c4565b835160028110156105cb57825284840151906001600160a01b03821682036105cb57828692838a950152613a8b8587016135a1565b85820152815201920191613a04565b60405160208101917f0000000000000000000000000000000000000000000000000000000000000000835263ffffffff60e01b16604082015260248152613ae0816132c4565b51902090565b919082039182116104f757565b908160209103126105cb575160ff811681036105cb5790565b604d81116104f757600a0a90565b6040516314cb3b1f60e21b81523060048201526020816024817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa908115610544575f91613b84575b5015613b7557565b63486aa30760e01b5f5260045ffd5b90506020813d602011613bb6575b81613b9f60209383613310565b810103126105cb57613bb0906135a1565b5f613b6d565b3d9150613b92565b604051638380edb760e01b81526020816004817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa908115610544575f91613c21575b50613c1257565b633915d7f960e21b5f5260045ffd5b90506020813d602011613c53575b81613c3c60209383613310565b810103126105cb57613c4d906135a1565b5f613c0b565b3d9150613c2f565b613c63614c16565b15613c6a57565b62f656ad60e21b5f5260045ffd5b906001600160a01b0390811680613dcb5750613cb65f357fffffffff0000000000000000000000000000000000000000000000000000000016613a9a565b60405163aaabadc560e01b8152602093909184836004817f000000000000000000000000000000000000000000000000000000000000000088165afa92831561054457859385915f91613d8c575b50906064929160405196879586946326f8aa2160e21b86526004860152336024860152166044840152165afa918215610544575f92613d56575b505015613d4757565b6323dada5360e01b5f5260045ffd5b90809250813d8311613d85575b613d6d8183613310565b810103126105cb57613d7e906135a1565b5f80613d3e565b503d613d63565b92948092508391503d8311613dc4575b613da68183613310565b810103126105cb575183811681036105cb5784929084906064613d04565b503d613d9c565b9150503303613d4757565b6001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000163303613e0857565b63089676d560e01b5f523360045260245ffd5b6040516333f0703b60e11b8152306004820152905f826024817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa918215610544575f92613e87575b506136ff613e816114ac613e8185613657565b51614cb8565b613e9c9192503d805f833e610b8a8183613310565b50509050905f613e6e565b7f000000000000000000000000000000000000000000000000000000000000000015613f9157905b7f000000000000000000000000000000000000000000000000000000000000000015613f8157915b6136ff613f2884610e4b857f00000000000000000000000000000000000000000000000000000000000000006138be565b93610e4b613f5a82610e4b877f00000000000000000000000000000000000000000000000000000000000000006138be565b947f00000000000000000000000000000000000000000000000000000000000000006138be565b50670de0b6b3a764000091613ef7565b50670de0b6b3a764000090613ecf565b9081610e4b613fb2929594956138a1565b613fc3613fbe826138a1565b614e9c565b670de0b6b3a763ffff1981019081116104f75780156138db57710b7abc627050305adf14a3d9e40000000000049269d3c21bcecceda1000000808501938486116104f75761401b670de0b6b3a76400009586926138be565b04936040519161402a836132c4565b60028352604036602085013782976140428782614d39565b8883018093116104f7576140a09361406c89611ff161406761409a9761409596614d39565b614d16565b61407587613633565b5261408389610d2c88613633565b9061408e848c6138be565b0490613ae6565b6140a3565b91613657565b52565b90670de0b6b3a7640000918281029281840414901517156104f7576136ff916138d1565b906001600160801b036140d983614df0565b166fffffffffffffffffffffffffffffffff196140f583614df0565b60801b16176008556040907ff36b0fd05d55ca91fb3ebd8493adf78a3405a11c145c054a092d0994b665b637828051858152836020820152a16001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001690825193602085015282840152818352614171836132c4565b803b156105cb576141c85f93918492845195868094819363c808824760e01b83527f5669727475616c42616c616e636573557064617465640000000000000000000060048401528860248401526044830190613227565b03925af19081156141e657506141db5750565b6141e4906132e0565b565b513d5f823e3d90fd5b9291908181118015614475575b61446657614208613746565b91614212856148ad565b946001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016916040938451926314cb3b1f60e21b84523060048501526020938481602481895afa90811561445c575f91614427575b501561441357506143bc92916143ae91614285614829565b9961428f8b6148ad565b985b6bffffffffffffffffffffffff806142a88c614e1b565b1682526142b483614e1b565b81888401911681527bffffffff00000000000000000000000000000000000000000000000077ffffffffffffffffffffffff0000000000000000000000006142fb87614e4b565b938d86019563ffffffff80961687526143138a614e4b565b95861660608201525116925160601b16935160c01b169163ffffffff60e01b9060e01b16921717176006557fa18562ea402e57cc2b437f8d7a139c6c38958adc1ecbb706ec37a7a64ff6e7a5818a61438886868d51948594859094939260609260808301968352602083015260408201520152565b0390a1875198899586019094939260609260808301968352602083015260408201520152565b03601f198101855284613310565b803b156105cb576141c85f93918492845195868094819363c808824760e01b83527f5072696365526174696f5374617465557064617465640000000000000000000060048401528860248401526044830190613227565b9597916143ae91836143bc9594989a614291565b90508481813d8311614455575b61443e8183613310565b810103126105cb5761444f906135a1565b5f61426d565b503d614434565b87513d5f823e3d90fd5b632ca4094f60e21b5f5260045ffd5b504281106141fc565b670de0b6b3a7640000908181116145d1576201e6e9900481039081116104f7576144a7816147ff565b906144b181614df0565b73ffffffffffffffffffffffffffffffff000000006007549160201b169073ffffffffffffffffffffffffffffffff0000000019161760075560407f7ee46b38be05f748de1c0fb2002ac48fe50a26a0017c654a92c0bf9228058847818051858152846020820152a16001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000168151928460208501528284015281835261455d836132c4565b803b156105cb576145b45f93918492845195868094819363c808824760e01b83527f4461696c79507269636553686966744578706f6e656e7455706461746564000060048401528860248401526044830190613227565b03925af19081156141e657506145c8575090565b6136ff906132e0565b632b5b4f3560e01b5f5260045ffd5b670c7d713b49da0000811161470f576007547fffffffff0000000000000000ffffffffffffffffffffffffffffffffffffffff7bffffffffffffffff00000000000000000000000000000000000000008360a01b169116176007557f74d468c8e414c37c502176a45cce93c85eaa0abf608e40641dc6664ca5efcab06020604051838152a16001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016604051916020830152602082526146a5826132f4565b803b156105cb576146fe5f9291839260405194858094819363c808824760e01b83527f43656e74657265646e6573734d617267696e55706461746564000000000000006004840152604060248401526044830190613227565b03925af18015610544576141db5750565b637304fac760e01b5f5260045ffd5b63ffffffff61472c42614e4b565b168063ffffffff1960075416176007557f32a9fd44bdf167703f584bd25098494d3d18a4653a2d97116d28da05209444d46020604051838152a16001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016604051916020830152602082526147a6826132f4565b803b156105cb576146fe5f9291839260405194858094819363c808824760e01b83527f4c61737454696d657374616d70557064617465640000000000000000000000006004840152604060248401526044830190613227565b670de0b6b3a7640000818103918183116104f7576201e6e980840293840414911417156104f75790565b6040516333f0703b60e11b81523060048201525f816024817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa8015610544576136ff915f9161488f575b5061488881614a15565b5091614e73565b6148a391503d805f833e610b8a8183613310565b925050505f61487e565b670de0b6b3a764000090818102908082048314901517156104f7576148d190614e9c565b8181029181830414901517156104f7576136ff90614e9c565b6001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000163014806149ec575b15614945577f000000000000000000000000000000000000000000000000000000000000000090565b60405160208101907f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f82527f000000000000000000000000000000000000000000000000000000000000000060408201527f000000000000000000000000000000000000000000000000000000000000000060608201524660808201523060a082015260a0815260c0810181811067ffffffffffffffff8211176109465760405251902090565b507f0000000000000000000000000000000000000000000000000000000000000000461461491c565b614a51906008546001600160801b036007549167ffffffffffffffff8360a01c16938263ffffffff85169460201c16928260801c921690614fcf565b909192565b6040516333f0703b60e11b8152306004820152905f826024817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa918215610544575f92614ab3575b50614a5182614a15565b614ac89192503d805f833e610b8a8183613310565b92505050905f614aa9565b6040516333f0703b60e11b81523060048201525f816024817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa801561054457614b2e915f91614b4d575b50614a15565b614b3d575b50506141e461471e565b614b46916140c7565b5f80614b33565b614b6191503d805f833e610b8a8183613310565b925050505f614b28565b9190614b7683613657565b51614b84575050505f905f90565b614b8d83613633565b5115614c0c57614bac6114ac614bb393614ba686613657565b516138be565b51906138be565b808211614be257670de0b6b3a7640000918281029281840414901517156104f757614bdd916138d1565b905f90565b90670de0b6b3a7640000918281029281840414901517156104f757614c06916138d1565b90600190565b5050505f90600190565b6040516333f0703b60e11b81523060048201525f816024817f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03165afa908115610544575f91614c9a575b50614c9460085467ffffffffffffffff60075460a01c16926001600160801b038260801c921690614b6b565b50101590565b614cae91503d805f833e610b8a8183613310565b925050505f614c68565b60018151614cc5816134f1565b614cce816134f1565b03614d095760206001600160a01b0381600493015116604051928380926333cd77e760e11b82525afa908115610544575f91613604575b5090565b50670de0b6b3a764000090565b670de0b6b3a7640000908181029181830414901517156104f7576136ff90614e9c565b90614d43916138be565b6001670de0b6b3a76400005f19830104019015150290565b670de05bc096e9c000808302831591848204148217156104f757670de0b6b3a764000091670de111a6b7de40008086029586041417156104f7578190048210928315614dba575b505050614dab57565b633d9f09df60e21b5f5260045ffd5b041090505f8080614da2565b614de6670de0b6b3a764000093610d2c6114ac614dec95610d2c86613657565b906138be565b0490565b6001600160801b0390818111614e04571690565b6306dfcc6560e41b5f52608060045260245260445ffd5b6bffffffffffffffffffffffff90818111614e34571690565b6306dfcc6560e41b5f52606060045260245260445ffd5b63ffffffff90818111614e5c571690565b6306dfcc6560e41b5f52602060045260245260445ffd5b610e4b670de0b6b3a7640000611898611891866118886136ff9888614e9798614dc6565b61533c565b600180821115614d0557614f6b908083700100000000000000000000000000000000811015614fb8575b80680100000000000000006004921015614fab575b640100000000811015614f9e575b62010000811015614f91575b610100811015614f85575b6010811015614f79575b1015614f72575b600302811c614f2081856138d1565b01811c614f2d81856138d1565b01811c614f3a81856138d1565b01811c614f4781856138d1565b01811c614f5481856138d1565b01811c614f6181856138d1565b01901c80926138d1565b8111900390565b811b614f11565b811c9160021b91614f0a565b60081c91811b91614f00565b60101c9160081b91614ef5565b60201c9160101b91614ee9565b60401c9160201b91614edb565b50680100000000000000009050608084901c614ec6565b9194939095925f94614fe042614e4b565b9263ffffffff918285169883831699808b1461532e578b87839c87615003613746565b94816bffffffffffffffffffffffff61503a81895116968260208b01511699604081019a6060868d5116920199868b51169361561d565b16965116109283615321575b5050506151ac575b5050509067ffffffffffffffff9493929161506a8a8c89614b6b565b969091161161507c575b505050505050565b88949750615097614067879a8c6150c19596979a9d99614e73565b948815615192576150b16150aa8b613657565b519a613633565b51965b891561518a575096615607565b1662278d0090818082109118029485821891670de0b6b3a7640000968784029384048814911417156104f757614de66150fb9287946156d3565b04918382028281048514831517156104f75761511690614e9c565b670de0b6b3a763ffff19938185019182116104f75761513861514c92846140a3565b90818082119118021896614de68389613894565b9282019182116104f75761516d9361516787611f65946138be565b04613ae6565b901561518557905b90916001905f8080808080615074565b615175565b905096615607565b6151a561519e8b613633565b519a613657565b51966150b4565b829b506151c19291949c5080939a5088614b6b565b9990918a1561530a576151ee906151d789613657565b519092945b670de0b6b3a7640000928184926138be565b04938083018093116104f7577f3fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff851685036104f7576152308560021b82613894565b671bc16d674ec7ffff1981019081116104f75761524c916138be565b6ec097ce7bc90715b34b9f100000000081018091116104f75761527b92615275614de692614e9c565b90613894565b670de0b6b3a763ffff1983019283116104f7577f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff831683036104f7576152cb610e4b916152d29460011b906138d1565b93846138be565b97156152f8579067ffffffffffffffff939291975b979860019790919293945f8061504e565b67ffffffffffffffff939291906152e7565b906151ee9061531889613633565b519092946151dc565b51161190505f8781615046565b5098505f9750505050505050565b90801561201b57670de0b6b3a7640000918281029281840414901517156104f7576001905f19830104019015150290565b60ff81146153a85760ff811690601f8211615399576040519161538f836132f4565b8252602082015290565b632cd44ac360e21b5f5260045ffd5b506040515f815f54916153ba83613569565b8083529260209060019081811690811561544357506001146153e5575b50506136ff92500382613310565b9150925f80527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563935f925b82841061542b57506136ff9450505081016020015f806153d7565b85548785018301529485019486945092810192615410565b915050602092506136ff94915060ff191682840152151560051b8201015f806153d7565b60ff81146154895760ff811690601f8211615399576040519161538f836132f4565b506040515f8160019160015461549e81613569565b808452936020916001811690811561544357506001146154c65750506136ff92500382613310565b91509260015f527fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6935f925b82841061550d57506136ff9450505081016020015f806153d7565b855487850183015294850194869450928101926154f2565b91907f7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0841161559c579160209360809260ff5f9560405194855216868401526040830152606082015282805260015afa15610544575f516001600160a01b0381161561559257905f905f90565b505f906001905f90565b5050505f9160039190565b60048110156134fb57806155b9575050565b600181036155d05763f645eedf60e01b5f5260045ffd5b600281036155eb575063fce698f760e01b5f5260045260245ffd5b6003146155f55750565b6335e2f38360e21b5f5260045260245ffd5b63ffffffff91821690821603919082116104f757565b90939192919063ffffffff90818116828416811061563f575050505050905090565b82851610156156cb57838261565a6156689661566194615607565b1693615607565b16906140a3565b916bffffffffffffffffffffffff8091169116670de0b6b3a7640000808202938285048214831517156104f7576156ae6156b4916156a9866136ff986138d1565b6156d3565b846138be565b049181808210911802188180821191180218614e1b565b505050505090565b670de0b6b3a7640000918083036156ea5750905090565b8290671bc16d674ec80000810361570757505080614dec916138be565b673782dace9d900000810361572b575061572482614dec936138be565b04806138be565b90506157369161579a565b6127108082029082820414821517156104f75760015f199384830104019015150290600182018083116104f757811015615771575050505f90565b030190565b80156138db576ec097ce7bc90715b34b9f10000000000590565b81156138db570590565b908015615f46578115615f40578160ff1c615f3257770bce5086492111aea88f4bb1ca6bcf584181ea8059f76532811015615f235781670c7d713b49da00001280615f12575b15615bc557670de0b6b3a7640000916ec097ce7bc90715b34b9f100000000090615823908402828101906ec097ce7bc90715b34b9f0fffffffff19018302615790565b9080828002059181838202058284820205838582020591848684020593858786020595808888020597880205600f900596600d900595600b900594600990059360079005926005900591600390050101010101010160011b918082818507020592050201670de0b6b3a7640000905b05680238fd42c5cf03ffff198181131580615bb2575b15615ba357819082121580615b90575b15615b81575f915f8112615b72575b506064906806f05b59d3b20000008112615b25576806f05b59d3b1ffffff190168056bc75e2d6310000082770195e54c5dd42177f53a27172fa9ec630262827000000000925b02819068ad78ebc5ac62000000811215615b02575b6856bc75e2d631000000811215615ade575b682b5e3af16b18800000811215615abc575b6815af1d78b58c400000811215615a9a575b680ad78ebc5ac6200000811215615a79575b82811215615a58575b6802b5e3af16b1880000811215615a37575b68015af1d78b58c40000811215615a16575b60028382800205056003848383020505600485848302050585600581868402050560068287830205056007838883020505906008848984020505926009858a8602050595600a868b8902050597600b878c8b02050599600c888d8d0205059b01010101010101010101010102050205905f146136ff576136ff90615776565b6806f5f1775788937937839168015af1d78b58c3ffff190192020590615997565b6808f00f760a4b2db55d83916802b5e3af16b187ffff190192020590615985565b680ebc5fb41746121110839168056bc75e2d630fffff190192020590615973565b68280e60114edb805d038391680ad78ebc5ac61fffff19019202059061596a565b690127fa27722cc06cc5e283916815af1d78b58c3fffff190192020590615958565b693f1fce3da636ea5cf8508391682b5e3af16b187fffff190192020590615946565b6b02df0ab5a80a22c61ab5a70083916856bc75e2d630ffffff190192020590615934565b6e01855144814a7ff805980ff0084000915068ad78ebc5ac61ffffff1901615922565b6803782dace9d90000008112615b5f576803782dace9d8ffffff190168056bc75e2d63100000826b1425982cf597cd205cef73809261590d565b68056bc75e2d631000008260019261590d565b600192505f03905060646158c7565b63d4794efd60e01b5f5260045ffd5b5068070c1cc73b00c800008213156158b8565b63a2f9f7e360e01b5f5260045ffd5b5068070c1cc73b00c800008213156158a8565b81670de0b6b3a7640000925f91848112615efc575b506064905f7e1600ef3172e58d2e933ec884fde10064c63b5372d805e203c0000000000000821215615ed1575b73011798004d755d3c8bc8e03204cf44619e000000821215615eb0575b820290808302906e01855144814a7ff805980ff00840009081831215615e8d575b50506b02df0ab5a80a22c61ab5a70080821215615e6d575b50693f1fce3da636ea5cf85080821215615e4d575b50690127fa27722cc06cc5e280821215615e2d575b5068280e60114edb805d0380821215615e0d575b50680ebc5fb4174612111080821215615df6575b506808f00f760a4b2db55d80821215615dd6575b506806f5f177578893793780821215615db6575b506806248f33704b28660380821215615d97575b506805c548670b9510e7ac80821215615d78575b50615d2568056bc75e2d6310000091828082019168056bc75e2d630fffff190102615790565b9080828002059181838202058284820205916003600560076009600b888a89020598808b8b02059a8b0205059805960594059205010101010160011b0105905f14615d73575f035b02615892565b615d6d565b68056bc75e2d631000006756bc75e2d63100009202059101905f615cff565b68056bc75e2d6310000067ad78ebc5ac6200009202059101905f615ceb565b68056bc75e2d6310000068015af1d78b58c400009202059101905f615cd7565b68056bc75e2d631000006802b5e3af16b18800009202059101905f615cc3565b68056bc75e2d63100000809202059101905f615caf565b68056bc75e2d63100000680ad78ebc5ac62000009202059101905f615c9b565b68056bc75e2d631000006815af1d78b58c4000009202059101905f615c87565b68056bc75e2d63100000682b5e3af16b188000009202059101905f615c72565b68056bc75e2d631000006856bc75e2d6310000009202059101905f615c5d565b68ad78ebc5ac62000000925069021e19e0c9bab240000002059101905f80615c45565b906b1425982cf597cd205cef73806803782dace9d900000091059101615c24565b50770195e54c5dd42177f53a27172fa9ec63026282700000000090056806f05b59d3b2000000615c07565b9050615f089150615776565b6001906064615bda565b50670f43fc2c04ee000082126157e0565b63d831731160e01b5f5260045ffd5b6211380f60e51b5f5260045ffd5b50505f90565b5050670de0b6b3a764000090565b91909180600314615f9f5780600114615f8b57600214615f8257634e487b7160e01b5f52605160045260245ffd5b6136ff91614d39565b50614dec90670de0b6b3a7640000926138be565b50670de0b6b3a7640000918281029281840414901517156104f7576136ff916138d156fea264697066735822122073bb74bdd2c560f8bc5cd564ffccc060dc947b788077fbef0933effb3542f54b64736f6c634300081b00330000000000000000000000000000000000000000000000000000000000000040000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba90000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000002c68af0bb14000000000000000000000000000000000000000000000000003635c9adc5dea000000000000000000000000000000000000000000000000000d8d726b7177a8000000000000000000000000000000000000000000000000000878678326eac90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001e444f204e4f5420555345202d204d6f636b205265436c616d6d20506f6f6c000000000000000000000000000000000000000000000000000000000000000000045445535400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004d7b226e616d65223a225265436c616d6d506f6f6c222c2276657273696f6e223a322c226465706c6f796d656e74223a2232303235303730322d76332d7265636c616d6d2d706f6f6c2d7632227d00000000000000000000000000000000000000
Deployed Bytecode
0x6080806040526004361015610012575f80fd5b5f3560e01c90816301ffc9a7146131f1575080630673d09a1461314957806306fdde03146130a0578063095ea7b3146130495780630b89f18214612f09578063105ae7ad14612edb57806316a0b3e014612e9a57806318160ddd14612e8057806318b6eb5514612e405780631c149e2814612bd45780631d8a5e7e14612baa578063215666a814612b8257806323b872dd14612b0057806323de665114612acf578063273c1adf14610ea55780632754888d14612a665780632c357688146125a657806330adf81f1461256c578063313ce567146125515780633644e51514612537578063378997701461251457806338be241d146124c957806345421ec7146123905780634efd88c9146123685780634f149b3f146123375780635211fa77146122f457806354fd4d501461224b5780635687f2b8146121f7578063569ee350146121d5578063627cdcb9146121ac578063654cf15d1461218a578063679aefce1461216b5780636bd838c7146120f057806370a082311461204357806372c9818614611e0e5780637ecebe0014611dd65780637f118b90146119fa57806381fa807c1461194d57806384254cf9146117c357806384b0196e146116e7578063851c1bb31461169f5780638d928af81461165c57806395d89b411461157e578063976907cc146114c6578063984de9e81461140a578063a0e8f5ac146113c2578063a103b044146113a8578063a6d2e718146111cf578063a9059cbb146110fb578063aa6ca80814611067578063abb1dc4414610f46578063b156aa0a14610eaa578063b677fa5614610ea5578063ba5f9f4014610d38578063c0209f1d14610cf7578063c480b9e614610c3e578063c66dbc4b14610b9c578063cd80a1af14610ae6578063ce20ece714610ac6578063d335b0cf14610a5a578063d4a5b63a1461096d578063d505accf14610775578063d77153a7146106b8578063dd62ed3e146105f3578063dfba3818146105cf5763e45801da146102f2575f80fd5b346105cb5760403660031901126105cb5761030b61324b565b602435906001600160a01b03906040519163ca4f280360e01b83523060048401525f83602481847f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9165afa928315610544575f936105a7575b50808061037085613657565b511692168092145f1461059e576001915f5b6001811480610589575b61057a57610398613e1b565b91906103cd6103b06103aa8584613ea7565b91613fa1565b5050506103c66103bf82613633565b5191613657565b51906140a3565b93856103d889613657565b5116036105745791925b846103ed8389613664565b51169460405197889163313ce56760e01b9788845283600460209c8d935afa928315610544578a928a915f9561054f575b509061042991613664565b51169660046040518099819382525afa958615610544575f96610512575b5060ff1660120391601283116104f757670de0b6b3a764000061048060ff9561047b6104756104b097613b0c565b8d6138be565b6138be565b04906040519961048f8b6132c4565b60028b526040368b8d01376104a4828c613664565b5261050b576001615f54565b921660120391601283116104f7576104d16104d79261047b6104de95613b0c565b906140a3565b9184613664565b526104f360405192828493845283019061344f565b0390f35b634e487b7160e01b5f52601160045260245ffd5b6003615f54565b60ff91965061053690893d8b1161053d575b61052e8183613310565b810190613af3565b9590610447565b503d610524565b6040513d5f823e3d90fd5b6104299291955061056c90853d871161053d5761052e8183613310565b94909161041e565b926103e2565b63c1ab6dc160e01b5f5260045ffd5b508261059486613633565b511682141561038c565b5f916001610382565b6105c49193503d805f833e6105bc8183613310565b810190613960565b915f610364565b5f80fd5b346105cb575f3660031901126105cb5760206105e9614c16565b6040519015158152f35b346105cb5760403660031901126105cb5761060c61324b565b6020610616613261565b60405163927da10560e01b81523060048201526001600160a01b039384166024820152908316604482015291829060649082907f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9165afa8015610544575f90610685575b602090604051908152f35b506020813d6020116106b0575b8161069f60209383613310565b810103126105cb576020905161067a565b3d9150610692565b346105cb575f3660031901126105cb576101406040516106d7816132a7565b5f81526020810190604081015f8152606082015f815260808301905f825260a084015f815260c0850160e08601915f83526101009485880194610120809901975f895260018b5260018552600187526040519a5f8c5251151560208c015251151560408b015251151560608a0152511515608089015251151560a088015251151560c087015251151560e08601525115159084015251151590820152f35b346105cb5760e03660031901126105cb5761078e61324b565b610796613261565b90604435916064359160843560ff811681036105cb5783421161095a576107d7826001600160a01b03165f52600260205260405f2080549060018201905590565b9060405160208101917f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c983526001600160a01b039687861694856040850152888816606085015289608085015260a084015260c083015260c0825260e082019082821067ffffffffffffffff83111761094657879361089293610889936040525190206108626148ea565b906040519161190160f01b83526002830152602282015260c43591604260a4359220615525565b909291926155a7565b168181036109315760405163e1f21c6760e01b81526001600160a01b03848116600483015285166024820152604481018790526020816064815f7f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba98b165af18015610544576108fd57005b6020813d602011610929575b8161091660209383613310565b810103126105cb57610927906135a1565b005b3d9150610909565b6325c0072360e11b5f5260045260245260445ffd5b634e487b7160e01b5f52604160045260245ffd5b8363313c898160e11b5f5260045260245ffd5b346105cb575f3660031901126105cb57610985613b1a565b6040516374eef59360e11b81523060048201526001600160a01b036060826024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba985165afa918215610544576109ea926020915f91610a2b575b5001511630613c78565b6109f2614ad3565b63ffffffff6060610a01613746565b0151164211610a1c576109274242610a17614829565b6141ef565b63771cef4f60e01b5f5260045ffd5b610a4d915060603d606011610a53575b610a458183613310565b810190613523565b846109e0565b503d610a3b565b346105cb575f3660031901126105cb5760405163b45090f960e01b81523060048201526020816024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa8015610544575f9061068557602090604051908152f35b346105cb575f3660031901126105cb5760206040516509184e72a0008152f35b346105cb575f3660031901126105cb576040516333f0703b60e11b81523060048201525f816024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa90811561054457604091610b68915f91610b76575b50600854906001600160801b038260801c921690614b6b565b825191825215156020820152f35b610b9291503d805f833e610b8a8183613310565b810190613986565b9250505083610b4f565b346105cb5760203660031901126105cb57610bb5613b1a565b610bbd613bbe565b6040516374eef59360e11b81523060048201526001600160a01b036060826024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba985165afa91821561054457610c21926020915f91610a2b575001511630613c78565b610c29614ad3565b6020610c3660043561447e565b604051908152f35b346105cb575f3660031901126105cb576040516333f0703b60e11b81523060048201525f816024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa8015610544576040915f91610cd9575b50610cc7610caf82614a15565b929167ffffffffffffffff60075460a01c1694614b6b565b50918351921015825215156020820152f35b610ced91503d805f833e610b8a8183613310565b9250505082610ca2565b346105cb575f3660031901126105cb576020610c36610d2c6104d1610d32610d1d614a56565b50610d2c839693949294613633565b51613894565b93613657565b346105cb5760e03660031901126105cb57610d5161324b565b50610d5a613261565b600460443510156105cb5767ffffffffffffffff6084358181116105cb57610d8690369060040161334a565b5060a4358181116105cb57610d9f90369060040161334a565b9060c4359081116105cb57602492610dbd60209236906004016133aa565b50610dc6613dd6565b6040516339370aa960e21b81526001600160a01b03918216600482015293849182907f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9165afa918215610544575f92610e6f575b610e5c610e43610e5685610e4b86610e5083610e4b610e3b60643583613ae6565b978894614a15565b5098906138be565b6138d1565b946138be565b906140c7565b610e6461471e565b602060405160018152f35b91506020823d602011610e9d575b81610e8a60209383613310565b810103126105cb57905190610e56610e1a565b3d9150610e7d565b613435565b346105cb575f3660031901126105cb576040516329ae7ec560e11b81523060048201525f816024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa8015610544576104f3915f91610f24575b5060405191829160208352602083019061344f565b610f4091503d805f833e610f388183613310565b8101906136d9565b82610f0f565b346105cb575f3660031901126105cb576040516333f0703b60e11b81523060048201526001600160a01b03905f816024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba986165afa8015610544575f915f935f915f93611046575b50610fc5604051946080865260808601906134b5565b6020858203818701528080885193848152019701925f905b83821061100a5787806104f389610ffc8d8b858203604087015261344f565b90838203606085015261344f565b909192939783606060019260408c518051611024816134f1565b8352808501518716858401520151151560408201520199019493920190610fdd565b925093505061105f91503d805f833e610b8a8183613310565b919385610faf565b346105cb575f3660031901126105cb5760405163ca4f280360e01b81523060048201525f816024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa8015610544576104f3915f916110e1575b506040519182916020835260208301906134b5565b6110f591503d805f833e6105bc8183613310565b826110cc565b346105cb5760403660031901126105cb57611152602061111961324b565b6040516317d5759960e31b81523360048201526001600160a01b0390911660248083019190915235604482015291829081906064820190565b03815f6001600160a01b037f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9165af180156105445761119657602060405160018152f35b6020813d6020116111c7575b816111af60209383613310565b810103126105cb576111c0906135a1565b5080610e64565b3d91506111a2565b346105cb5760603660031901126105cb57600435604435602480356111f2613b1a565b6040516374eef59360e11b81523060048201526001600160a01b0360608285817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba985165afa91821561054457611255926020915f91611389575001511630613c78565b8042118142180218928284116113745761126f8484613ae6565b9162015180938484106113655761128f90611288614ad3565b86846141ef565b9182811061135d578281035b620f4240811061134b5750828111156113265784810294818604149015171561131357506112d392916112cd916138be565b9061533c565b7f0000000000000000000000000000000000000000000000001bc16d674ec800001061130457602090604051908152f35b632b85f91760e11b5f5260045ffd5b634e487b7160e01b5f9081526011600452fd5b91848102948186041490151715611313575061134692916112cd916138be565b6112d3565b905063119d853760e21b5f526004525ffd5b80830361129b565b6319f8e85560e11b5f5260045ffd5b50826364bb3b8360e11b5f526004525260445ffd5b6113a2915060603d606011610a5357610a458183613310565b886109e0565b346105cb575f3660031901126105cb576020610c36614829565b346105cb576003196060368201126105cb576004359067ffffffffffffffff82116105cb5760e091360301126105cb576113fa613261565b50604080515f81525f6020820152f35b346105cb5760403660031901126105cb5767ffffffffffffffff6004358181116105cb5761143c90369060040161334a565b9060243560028110156105cb576020926114b26001611487610c3695600854906001600160801b03600754918260a01c16928163ffffffff8416938c1c16918160801c911688614fcf565b50919094611494816134f1565b036114b857610d2c6114ac6001955b610d2c86613657565b93613633565b91615f54565b610d2c6114ac6002956114a3565b346105cb576101003660031901126105cb576114e061324b565b506114e9613261565b50600560443510156105cb5767ffffffffffffffff6064358181116105cb5761151690369060040161334a565b506084358181116105cb5761152f90369060040161334a565b60c4358281116105cb5761154790369060040161334a565b5060e4359182116105cb576115636104f39236906004016133aa565b506040519182915f835260406020840152604083019061344f565b346105cb575f3660031901126105cb576040516004545f8261159f83613569565b91828252602093600190856001821691825f1461163c5750506001146115e1575b506115cd92500383613310565b6104f3604051928284938452830190613227565b84915060045f527f8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd19b905f915b8583106116245750506115cd9350820101856115c0565b8054838901850152879450869390920191810161160d565b60ff1916858201526115cd95151560051b85010192508791506115c09050565b346105cb575f3660031901126105cb5760206040516001600160a01b037f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9168152f35b346105cb5760203660031901126105cb576004357fffffffff00000000000000000000000000000000000000000000000000000000811681036105cb57610c36602091613a9a565b346105cb575f3660031901126105cb576117207f444f204e4f5420555345202d204d6f636b205265436c616d6d20506f6f6c001e61536d565b6117497f3100000000000000000000000000000000000000000000000000000000000001615467565b9060405191602083019280841067ffffffffffffffff851117610946576117a36104f392611795956040525f8352604051958695600f60f81b875260e0602088015260e0870190613227565b908582036040870152613227565b904660608501523060808501525f60a085015283820360c085015261344f565b346105cb575f3660031901126105cb576040516314cb3b1f60e21b81523060048201527f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b031690602081602481855afa908115610544575f91611913575b50156118c9575f602491604051928380926333f0703b60e11b82523060048301525afa8015610544576040915f916118ab575b5061189f610e4b670de0b6b3a764000061189861189161188861187d87614a15565b508181939299614dc6565b948186926138be565b94806138be565b04906140a3565b82519182526020820152f35b6118bf91503d805f833e610b8a8183613310565b925050508261185b565b5060407f00000000000000000000000000000000000000000000003635c9adc5dea000007f0000000000000000000000000000000000000000000000d8d726b7177a80000061189f565b90506020813d602011611945575b8161192e60209383613310565b810103126105cb5761193f906135a1565b82611828565b3d9150611921565b346105cb575f3660031901126105cb5760405163f29486a160e01b81523060048201526101a09081816024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa908115610544576040925f926119cd575b505060608282015191015182519182526020820152f35b6119ec9250803d106119f3575b6119e48183613310565b81019061378b565b82806119b6565b503d6119da565b346105cb575f3660031901126105cb57604051610220810181811067ffffffffffffffff8211176109465760405260608152606060208201525f60408201525f60608201525f60808201525f60a08201525f60c08201525f60e08201525f6101008201525f6101208201525f6101408201525f6101608201525f6101808201525f6101a08201525f6101c08201525f6101e08201525f6102008201526001600160a01b037f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba91660405163ca4f280360e01b81523060048201525f81602481855afa918215610544576024925f928391611dbc575b508452604051633f1b0def60e11b815230600482015292839182905afa908115610544575f91611d99575b5060208201527f0000000000000000000000000000000000000000000000000000000000000000151560408201527f0000000000000000000000000000000000000000000000000000000000000000151560608201526509184e72a000608082015267016345785d8a000060a08201527f00000000000000000000000000000000000000000000003635c9adc5dea0000060c08201527f0000000000000000000000000000000000000000000000d8d726b7177a80000060e08201527f0000000000000000000000000000000000000000000000878678326eac9000006101008201527f0000000000000000000000000000000000000000000000000de0b6b3a76400006101208201527f00000000000000000000000000000000000000000000000002c68af0bb140000610140820152670c7d713b49da0000610160820152670de0b6b3a76400006101808201527f0000000000000000000000000000000000000000000000001bc16d674ec800006101a0820152620151806101c0820152620f42406101e0820152655af3107a4000610200820152604051809160208252610200611ced611cd7835161022060208701526102408601906134b5565b6020840151858203601f19016040870152613482565b91604081015115156060850152606081015115156080850152608081015160a085015260a081015160c085015260c081015160e085015260e08101516101008501526101008101516101208501526101208101516101408501526101408101516101608501526101608101516101808501526101808101516101a08501526101a08101516101c08501526101c08101516101e08501526101e08101518285015201516102208301520390f35b611db591503d805f833e611dad8183613310565b810190613702565b5082611b19565b611dd091503d8085833e6105bc8183613310565b85611aee565b346105cb5760203660031901126105cb576001600160a01b03611df761324b565b165f526002602052602060405f2054604051908152f35b346105cb576003196020368201126105cb576004359067ffffffffffffffff908183116105cb5760e09083360301126105cb576040519160e083018381108382111761094657604052806004013560028110156105cb578352602083016024820135815260448201358381116105cb57611e8e906004369185010161334a565b9360408101948552606081019360648401358552608082019360848101358552611eba60a48201613277565b60a084015260c48101359182116105cb576004611eda92369201016133aa565b60c0820152611ee7613dd6565b611ef18551614a15565b969196929092612034575b611f0461471e565b51611f0e816134f1565b611f17816134f1565b611faa57611f6b92611f60611f71969593611f6593519889975195519788945194859288155f14611f9257611f5992610d2c610d2c959361047b939792613664565b958a613664565b613894565b906138d1565b92613664565b518111611f8357602090604051908152f35b635a09f12960e11b5f5260045ffd5b93611f5992610d2c610d2c959361047b939792613664565b5193519251915193949092611fbf8387613664565b518511611f8357610d2c8593611feb611ff696611ff19585155f1461202a57610d2c9091955b8b613664565b97613664565b613ae6565b90811561201b5761200b6001916020946138be565b915f198301040190151502610c36565b630a0c22c760e01b5f5260045ffd5b94610d2c90611fe5565b61203e83886140c7565b611efc565b346105cb576020806003193601126105cb5760448161206061324b565b604051633de222bb60e21b81523060048201526001600160a01b03918216602482015292839182907f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9165afa908115610544575f916120c3575b50604051908152f35b90508181813d83116120e9575b6120da8183613310565b810103126105cb5751826120ba565b503d6120d0565b346105cb575f3660031901126105cb575f606060405161210f8161328b565b8281528260208201528260408201520152608061212a613746565b604051906bffffffffffffffffffffffff8082511683526020820151166020830152606060408201519163ffffffff80931660408501520151166060820152f35b346105cb575f3660031901126105cb576356b7fcdb60e01b5f5260045ffd5b346105cb575f3660031901126105cb57602060405167016345785d8a00008152f35b346105cb575f3660031901126105cb57335f908152600260205260409020805460018101909155005b346105cb575f3660031901126105cb576020610c366121f2614829565b6148ad565b346105cb5760207f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92561222836613400565b92919390612234613dd6565b6001600160a01b03809160405195865216941692a3005b346105cb575f3660031901126105cb576040516005545f8261226c83613569565b91828252602093600190856001821691825f1461163c57505060011461229957506115cd92500383613310565b84915060055f527f036b6384b5eca791c62761152d0c79bb0604c104a5fb6f4eb0703f3154bb3db0905f915b8583106122dc5750506115cd9350820101856115c0565b805483890185015287945086939092019181016122c5565b346105cb576003196040368201126105cb576004359067ffffffffffffffff82116105cb5760e091360301126105cb5761232c613261565b5060206040515f8152f35b346105cb575f3660031901126105cb576060612351614a56565b919250604051928352602083015215156040820152f35b346105cb575f3660031901126105cb576020610c366001600160801b03600754831c166147ff565b346105cb5760e03660031901126105cb576123a961324b565b506123b2613261565b600560443510156105cb5767ffffffffffffffff6064358181116105cb576123de90369060040161334a565b5060a4358181116105cb576123f790369060040161334a565b9060c4359081116105cb5760249261241560209236906004016133aa565b5061241e613dd6565b6040516339370aa960e21b81526001600160a01b03918216600482015293849182907f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9165afa918215610544575f92612493575b610e5c610e43610e5685610e4b86610e5083610e4b610e3b82608435613894565b91506020823d6020116124c1575b816124ae60209383613310565b810103126105cb57905190610e56612472565b3d91506124a1565b346105cb5760603660031901126105cb5767ffffffffffffffff6004358181116105cb576124fb90369060040161334a565b506044359081116105cb5761232c9036906004016133aa565b346105cb575f3660031901126105cb57602063ffffffff60075416604051908152f35b346105cb575f3660031901126105cb576020610c366148ea565b346105cb575f3660031901126105cb57602060405160128152f35b346105cb575f3660031901126105cb5760206040517f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c98152f35b346105cb575f3660031901126105cb57604051610240810181811067ffffffffffffffff8211176109465760405260608152606060208201525f60408201525f60608201525f6080820152606060a08201525f60c08201525f60e08201525f6101008201525f6101208201525f6101408201525f6101608201525f6101808201525f6101a08201525f6101c08201525f6101e08201525f6102008201525f6102208201526040516329ae7ec560e11b81523060048201525f816024816001600160a01b037f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9165afa908115610544575f91612a4c575b508152604051633f1b0def60e11b81523060048201525f816024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa908115610544575f91612a30575b50602082015260405163b45090f960e01b81523060048201526020816024816001600160a01b037f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9165afa908115610544575f916129fe575b5060408201526127586135ae565b606082015267ffffffffffffffff60075463ffffffff811660808401526127cc604051612784816132c4565b600281526040366020830137600854906001600160801b03918281166127a983613657565b5260801c6127b682613633565b5260a08601528260201c168060e08601526147ff565b60c084015260a01c1661010082015263ffffffff60606127ea613746565b6bffffffffffffffffffffffff80825116610160860152602082015116610180850152826040820151166101a08501520151166101c082015260405163f29486a160e01b81523060048201526101a0816024816001600160a01b037f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9165afa908115610544575f916129dd575b5061012060e0820151151591826101e08501526101008101511515610200850152015115156102208301526129bb575b60405180916020825261022061291d6128ce83516102406020870152610260860190613482565b6128ea602085015191601f199283888303016040890152613482565b906040850151606087015260608501516080870152608085015160a087015260a0850151908683030160c0870152613482565b9160c081015160e085015260e08101516101008501526101008101516101208501526101208101516101408501526101408101516101608501526101608101516101808501526101808101516101a085015263ffffffff6101a0820151166101c085015263ffffffff6101c0820151166101e08501526101e08101511515610200850152610200810151151582850152015115156102408301520390f35b6129d26129c6614829565b806101208401526148ad565b6101408201526128a7565b6129f891506101a03d6101a0116119f3576119e48183613310565b82612877565b90506020813d602011612a28575b81612a1960209383613310565b810103126105cb57518261274a565b3d9150612a0c565b612a4491503d805f833e611dad8183613310565b9050826126f1565b612a6091503d805f833e610f388183613310565b8261269c565b346105cb576101003660031901126105cb57612a8061324b565b50612a89613261565b50600460443510156105cb5767ffffffffffffffff6084358181116105cb57612ab690369060040161334a565b5060a4358181116105cb5761152f90369060040161334a565b346105cb5760207fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef61222836613400565b346105cb5760846020612b1236613400565b604051630aed65f560e11b81523360048201526001600160a01b0393841660248201529183166044830152606482015292839182905f907f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9165af180156105445761119657602060405160018152f35b346105cb575f3660031901126105cb5760206007546001600160801b0360405191831c168152f35b346105cb575f3660031901126105cb57602067ffffffffffffffff60075460a01c16604051908152f35b346105cb5760403660031901126105cb5767ffffffffffffffff6004358181116105cb57612c0690369060040161334a565b6024358281116105cb57612c1e9036906004016133aa565b50612c27613dd6565b60405191610120830190811183821017610946576040525f825260208201905f8252604083015f815260608401935f855260808101915f835260a08201926060845260c08301925f8452612caa612c9b60e08301925f84526101008101995f8b52612c90613e1b565b928382935252613ea7565b91828652818c52808752613fa1565b8a528352855280865297612cda612ccd612cc389613633565b516103c68a613657565b996103c66103bf82613633565b670de05bc096e9c000998a820282159b838204148c17156104f757670de0b6b3a76400009b670de111a6b7de40008085029485041417156104f7578b8091048210928315612e34575b505050612e255788612db8612da9612dc296612d9f958b85612dd19f612d7990612dc79f612da49f612d66906103c6612d5e612d6f94613657565b519151613657565b9e8f90516138be565b049c8d94516138be565b049b8c935197519951612d9f612d9286610d2c86613633565b6104d186610d2c87613657565b614d5b565b614dc6565b92612d9f84610e4b8b806138be565b61189886806138be565b6140c7565b51429042906141ef565b50612dfb7f0000000000000000000000000000000000000000000000000de0b6b3a764000061447e565b50610e5c7f00000000000000000000000000000000000000000000000002c68af0bb1400006145e0565b6304f512cb60e41b5f5260045ffd5b041090508a8a81612d23565b346105cb576003196020368201126105cb576004359067ffffffffffffffff82116105cb5761018091360301126105cb57604080515f81525f6020820152f35b346105cb575f3660031901126105cb576020610c366135ae565b346105cb5760603660031901126105cb5760043567ffffffffffffffff81116105cb57612ecb90369060040161334a565b5063d623472560e01b5f5260045ffd5b346105cb575f3660031901126105cb5760406008548151906001600160801b038116825260801c6020820152f35b346105cb5760e03660031901126105cb57612f2261324b565b50612f2b613261565b5060443567ffffffffffffffff81116105cb57366023820112156105cb578060040135612f5781613332565b91612f656040519384613310565b81835260206024602085019360071b830101913683116105cb57602401925b828410612fd8578460803660631901126105cb575160021480612fc7575b80612fb5575b6020906040519015158152f35b5060c435801590811514612fa8575f80fd5b506064358015158114612fa2575f80fd5b6080843603126105cb5760405190612fef8261328b565b612ff885613277565b82528285013560028110156105cb578383015260408501356001600160a01b03811681036105cb576040830152606090818601359283151584036105cb576080938593820152815201930192612f84565b346105cb5760403660031901126105cb57611152602061306761324b565b60405163e1f21c6760e01b81523360048201526001600160a01b0390911660248083019190915235604482015291829081906064820190565b346105cb575f3660031901126105cb576040516003545f826130c183613569565b91828252602093600190856001821691825f1461163c5750506001146130ee57506115cd92500383613310565b84915060035f527fc2575a0e9e593c00f959f8c92f12db2869c3395a3b0502d05e2516446f71f85b905f915b8583106131315750506115cd9350820101856115c0565b8054838901850152879450869390920191810161311a565b346105cb5760203660031901126105cb57613162613b1a565b61316a613bbe565b613172613c5b565b6040516374eef59360e11b81523060048201526001600160a01b036060826024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba985165afa918215610544576131d6926020915f91610a2b575001511630613c78565b6131de614ad3565b6131e96004356145e0565b610927613c5b565b346105cb5760203660031901126105cb576004359063ffffffff60e01b82168092036105cb576020916301ffc9a760e01b148152f35b805180835260209291819084018484015e5f828201840152601f01601f1916010190565b600435906001600160a01b03821682036105cb57565b602435906001600160a01b03821682036105cb57565b35906001600160a01b03821682036105cb57565b6080810190811067ffffffffffffffff82111761094657604052565b610140810190811067ffffffffffffffff82111761094657604052565b6060810190811067ffffffffffffffff82111761094657604052565b67ffffffffffffffff811161094657604052565b6040810190811067ffffffffffffffff82111761094657604052565b90601f8019910116810190811067ffffffffffffffff82111761094657604052565b67ffffffffffffffff81116109465760051b60200190565b9080601f830112156105cb57602090823561336481613332565b936133726040519586613310565b81855260208086019260051b8201019283116105cb57602001905b82821061339b575050505090565b8135815290830190830161338d565b81601f820112156105cb5780359067ffffffffffffffff821161094657604051926133df601f8401601f191660200185613310565b828452602083830101116105cb57815f926020809301838601378301015290565b60609060031901126105cb576001600160a01b039060043582811681036105cb579160243590811681036105cb579060443590565b346105cb575f3660031901126105cb5760206040515f8152f35b9081518082526020808093019301915f5b82811061346e575050505090565b835185529381019392810192600101613460565b9081518082526020808093019301915f5b8281106134a1575050505090565b835185529381019392810192600101613493565b9081518082526020808093019301915f5b8281106134d4575050505090565b83516001600160a01b0316855293810193928101926001016134c6565b600211156134fb57565b634e487b7160e01b5f52602160045260245ffd5b51906001600160a01b03821682036105cb57565b908160609103126105cb57613561604080519261353f846132c4565b6135488161350f565b84526135566020820161350f565b60208501520161350f565b604082015290565b90600182811c92168015613597575b602083101461358357565b634e487b7160e01b5f52602260045260245ffd5b91607f1691613578565b519081151582036105cb57565b6040516339370aa960e21b81523060048201526020816024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa908115610544575f91613604575090565b90506020813d60201161362b575b8161361f60209383613310565b810103126105cb575190565b3d9150613612565b8051600110156136435760400190565b634e487b7160e01b5f52603260045260245ffd5b8051156136435760200190565b80518210156136435760209160051b010190565b9080601f830112156105cb5781519060209161369381613332565b936136a16040519586613310565b81855260208086019260051b8201019283116105cb57602001905b8282106136ca575050505090565b815181529083019083016136bc565b906020828203126105cb57815167ffffffffffffffff81116105cb576136ff9201613678565b90565b9190916040818403126105cb5780519267ffffffffffffffff938481116105cb578161372f918401613678565b9360208301519081116105cb576136ff9201613678565b604051906137538261328b565b8160606006546bffffffffffffffffffffffff808216845281831c16602084015263ffffffff8160c01c16604084015260e01c910152565b809103906101a082126105cb576080604051926137a7846132a7565b126105cb576040516137b88161328b565b6137c1826135a1565b81526137cf602083016135a1565b60208201526137e0604083016135a1565b60408201526137f1606083016135a1565b606082015282526080810151602083015260a0810151604083015260c0810151606083015260e081015164ffffffffff811681036105cb576080830152610100908181015163ffffffff811681036105cb5761388d916101809160a08601526101209361385f8583016135a1565b60c087015261387161014083016135a1565b60e087015261388361016083016135a1565b90860152016135a1565b9082015290565b919082018092116104f757565b90670de0b6b3a7640000918281029281840414901517156104f757565b818102929181159184041417156104f757565b81156138db570490565b634e487b7160e01b5f52601260045260245ffd5b9080601f830112156105cb5781519060209161390a81613332565b936139186040519586613310565b81855260208086019260051b8201019283116105cb57602001905b828210613941575050505090565b81516001600160a01b03811681036105cb578152908301908301613933565b906020828203126105cb57815167ffffffffffffffff81116105cb576136ff92016138ef565b916080838303126105cb5782519067ffffffffffffffff918281116105cb57836139b19186016138ef565b936020808201518481116105cb57820185601f820112156105cb578051906139d882613332565b9260406139e86040519586613310565b83855281850190826060809602850101938a85116105cb578301915b848310613a41575050505050509360408201518481116105cb5781613a2a918401613678565b9360608301519081116105cb576136ff9201613678565b85838c03126105cb57815190613a56826132c4565b835160028110156105cb57825284840151906001600160a01b03821682036105cb57828692838a950152613a8b8587016135a1565b85820152815201920191613a04565b60405160208101917f000000000000000000000000daa273aeec06e9ccb7428a77e2abb1e4659b16d2835263ffffffff60e01b16604082015260248152613ae0816132c4565b51902090565b919082039182116104f757565b908160209103126105cb575160ff811681036105cb5790565b604d81116104f757600a0a90565b6040516314cb3b1f60e21b81523060048201526020816024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa908115610544575f91613b84575b5015613b7557565b63486aa30760e01b5f5260045ffd5b90506020813d602011613bb6575b81613b9f60209383613310565b810103126105cb57613bb0906135a1565b5f613b6d565b3d9150613b92565b604051638380edb760e01b81526020816004817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa908115610544575f91613c21575b50613c1257565b633915d7f960e21b5f5260045ffd5b90506020813d602011613c53575b81613c3c60209383613310565b810103126105cb57613c4d906135a1565b5f613c0b565b3d9150613c2f565b613c63614c16565b15613c6a57565b62f656ad60e21b5f5260045ffd5b906001600160a01b0390811680613dcb5750613cb65f357fffffffff0000000000000000000000000000000000000000000000000000000016613a9a565b60405163aaabadc560e01b8152602093909184836004817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba988165afa92831561054457859385915f91613d8c575b50906064929160405196879586946326f8aa2160e21b86526004860152336024860152166044840152165afa918215610544575f92613d56575b505015613d4757565b6323dada5360e01b5f5260045ffd5b90809250813d8311613d85575b613d6d8183613310565b810103126105cb57613d7e906135a1565b5f80613d3e565b503d613d63565b92948092508391503d8311613dc4575b613da68183613310565b810103126105cb575183811681036105cb5784929084906064613d04565b503d613d9c565b9150503303613d4757565b6001600160a01b037f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9163303613e0857565b63089676d560e01b5f523360045260245ffd5b6040516333f0703b60e11b8152306004820152905f826024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa918215610544575f92613e87575b506136ff613e816114ac613e8185613657565b51614cb8565b613e9c9192503d805f833e610b8a8183613310565b50509050905f613e6e565b7f000000000000000000000000000000000000000000000000000000000000000015613f9157905b7f000000000000000000000000000000000000000000000000000000000000000015613f8157915b6136ff613f2884610e4b857f00000000000000000000000000000000000000000000003635c9adc5dea000006138be565b93610e4b613f5a82610e4b877f0000000000000000000000000000000000000000000000d8d726b7177a8000006138be565b947f0000000000000000000000000000000000000000000000878678326eac9000006138be565b50670de0b6b3a764000091613ef7565b50670de0b6b3a764000090613ecf565b9081610e4b613fb2929594956138a1565b613fc3613fbe826138a1565b614e9c565b670de0b6b3a763ffff1981019081116104f75780156138db57710b7abc627050305adf14a3d9e40000000000049269d3c21bcecceda1000000808501938486116104f75761401b670de0b6b3a76400009586926138be565b04936040519161402a836132c4565b60028352604036602085013782976140428782614d39565b8883018093116104f7576140a09361406c89611ff161406761409a9761409596614d39565b614d16565b61407587613633565b5261408389610d2c88613633565b9061408e848c6138be565b0490613ae6565b6140a3565b91613657565b52565b90670de0b6b3a7640000918281029281840414901517156104f7576136ff916138d1565b906001600160801b036140d983614df0565b166fffffffffffffffffffffffffffffffff196140f583614df0565b60801b16176008556040907ff36b0fd05d55ca91fb3ebd8493adf78a3405a11c145c054a092d0994b665b637828051858152836020820152a16001600160a01b037f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba91690825193602085015282840152818352614171836132c4565b803b156105cb576141c85f93918492845195868094819363c808824760e01b83527f5669727475616c42616c616e636573557064617465640000000000000000000060048401528860248401526044830190613227565b03925af19081156141e657506141db5750565b6141e4906132e0565b565b513d5f823e3d90fd5b9291908181118015614475575b61446657614208613746565b91614212856148ad565b946001600160a01b037f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba916916040938451926314cb3b1f60e21b84523060048501526020938481602481895afa90811561445c575f91614427575b501561441357506143bc92916143ae91614285614829565b9961428f8b6148ad565b985b6bffffffffffffffffffffffff806142a88c614e1b565b1682526142b483614e1b565b81888401911681527bffffffff00000000000000000000000000000000000000000000000077ffffffffffffffffffffffff0000000000000000000000006142fb87614e4b565b938d86019563ffffffff80961687526143138a614e4b565b95861660608201525116925160601b16935160c01b169163ffffffff60e01b9060e01b16921717176006557fa18562ea402e57cc2b437f8d7a139c6c38958adc1ecbb706ec37a7a64ff6e7a5818a61438886868d51948594859094939260609260808301968352602083015260408201520152565b0390a1875198899586019094939260609260808301968352602083015260408201520152565b03601f198101855284613310565b803b156105cb576141c85f93918492845195868094819363c808824760e01b83527f5072696365526174696f5374617465557064617465640000000000000000000060048401528860248401526044830190613227565b9597916143ae91836143bc9594989a614291565b90508481813d8311614455575b61443e8183613310565b810103126105cb5761444f906135a1565b5f61426d565b503d614434565b87513d5f823e3d90fd5b632ca4094f60e21b5f5260045ffd5b504281106141fc565b670de0b6b3a7640000908181116145d1576201e6e9900481039081116104f7576144a7816147ff565b906144b181614df0565b73ffffffffffffffffffffffffffffffff000000006007549160201b169073ffffffffffffffffffffffffffffffff0000000019161760075560407f7ee46b38be05f748de1c0fb2002ac48fe50a26a0017c654a92c0bf9228058847818051858152846020820152a16001600160a01b037f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9168151928460208501528284015281835261455d836132c4565b803b156105cb576145b45f93918492845195868094819363c808824760e01b83527f4461696c79507269636553686966744578706f6e656e7455706461746564000060048401528860248401526044830190613227565b03925af19081156141e657506145c8575090565b6136ff906132e0565b632b5b4f3560e01b5f5260045ffd5b670c7d713b49da0000811161470f576007547fffffffff0000000000000000ffffffffffffffffffffffffffffffffffffffff7bffffffffffffffff00000000000000000000000000000000000000008360a01b169116176007557f74d468c8e414c37c502176a45cce93c85eaa0abf608e40641dc6664ca5efcab06020604051838152a16001600160a01b037f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba916604051916020830152602082526146a5826132f4565b803b156105cb576146fe5f9291839260405194858094819363c808824760e01b83527f43656e74657265646e6573734d617267696e55706461746564000000000000006004840152604060248401526044830190613227565b03925af18015610544576141db5750565b637304fac760e01b5f5260045ffd5b63ffffffff61472c42614e4b565b168063ffffffff1960075416176007557f32a9fd44bdf167703f584bd25098494d3d18a4653a2d97116d28da05209444d46020604051838152a16001600160a01b037f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba916604051916020830152602082526147a6826132f4565b803b156105cb576146fe5f9291839260405194858094819363c808824760e01b83527f4c61737454696d657374616d70557064617465640000000000000000000000006004840152604060248401526044830190613227565b670de0b6b3a7640000818103918183116104f7576201e6e980840293840414911417156104f75790565b6040516333f0703b60e11b81523060048201525f816024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa8015610544576136ff915f9161488f575b5061488881614a15565b5091614e73565b6148a391503d805f833e610b8a8183613310565b925050505f61487e565b670de0b6b3a764000090818102908082048314901517156104f7576148d190614e9c565b8181029181830414901517156104f7576136ff90614e9c565b6001600160a01b037f000000000000000000000000000ab3853737842bed7dabe3540e3e5336fe62a6163014806149ec575b15614945577fc52098247b6e89f8558b6508bba043da29ef46699c7e042ec0aa6e6d8cd4b4d590565b60405160208101907f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f82527ff25806f7b04794efd88bbb8369df7da57ac474eadd2b7759b435caf8fa14292b60408201527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc660608201524660808201523060a082015260a0815260c0810181811067ffffffffffffffff8211176109465760405251902090565b507f0000000000000000000000000000000000000000000000000000000000000001461461491c565b614a51906008546001600160801b036007549167ffffffffffffffff8360a01c16938263ffffffff85169460201c16928260801c921690614fcf565b909192565b6040516333f0703b60e11b8152306004820152905f826024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa918215610544575f92614ab3575b50614a5182614a15565b614ac89192503d805f833e610b8a8183613310565b92505050905f614aa9565b6040516333f0703b60e11b81523060048201525f816024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa801561054457614b2e915f91614b4d575b50614a15565b614b3d575b50506141e461471e565b614b46916140c7565b5f80614b33565b614b6191503d805f833e610b8a8183613310565b925050505f614b28565b9190614b7683613657565b51614b84575050505f905f90565b614b8d83613633565b5115614c0c57614bac6114ac614bb393614ba686613657565b516138be565b51906138be565b808211614be257670de0b6b3a7640000918281029281840414901517156104f757614bdd916138d1565b905f90565b90670de0b6b3a7640000918281029281840414901517156104f757614c06916138d1565b90600190565b5050505f90600190565b6040516333f0703b60e11b81523060048201525f816024817f000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba96001600160a01b03165afa908115610544575f91614c9a575b50614c9460085467ffffffffffffffff60075460a01c16926001600160801b038260801c921690614b6b565b50101590565b614cae91503d805f833e610b8a8183613310565b925050505f614c68565b60018151614cc5816134f1565b614cce816134f1565b03614d095760206001600160a01b0381600493015116604051928380926333cd77e760e11b82525afa908115610544575f91613604575b5090565b50670de0b6b3a764000090565b670de0b6b3a7640000908181029181830414901517156104f7576136ff90614e9c565b90614d43916138be565b6001670de0b6b3a76400005f19830104019015150290565b670de05bc096e9c000808302831591848204148217156104f757670de0b6b3a764000091670de111a6b7de40008086029586041417156104f7578190048210928315614dba575b505050614dab57565b633d9f09df60e21b5f5260045ffd5b041090505f8080614da2565b614de6670de0b6b3a764000093610d2c6114ac614dec95610d2c86613657565b906138be565b0490565b6001600160801b0390818111614e04571690565b6306dfcc6560e41b5f52608060045260245260445ffd5b6bffffffffffffffffffffffff90818111614e34571690565b6306dfcc6560e41b5f52606060045260245260445ffd5b63ffffffff90818111614e5c571690565b6306dfcc6560e41b5f52602060045260245260445ffd5b610e4b670de0b6b3a7640000611898611891866118886136ff9888614e9798614dc6565b61533c565b600180821115614d0557614f6b908083700100000000000000000000000000000000811015614fb8575b80680100000000000000006004921015614fab575b640100000000811015614f9e575b62010000811015614f91575b610100811015614f85575b6010811015614f79575b1015614f72575b600302811c614f2081856138d1565b01811c614f2d81856138d1565b01811c614f3a81856138d1565b01811c614f4781856138d1565b01811c614f5481856138d1565b01811c614f6181856138d1565b01901c80926138d1565b8111900390565b811b614f11565b811c9160021b91614f0a565b60081c91811b91614f00565b60101c9160081b91614ef5565b60201c9160101b91614ee9565b60401c9160201b91614edb565b50680100000000000000009050608084901c614ec6565b9194939095925f94614fe042614e4b565b9263ffffffff918285169883831699808b1461532e578b87839c87615003613746565b94816bffffffffffffffffffffffff61503a81895116968260208b01511699604081019a6060868d5116920199868b51169361561d565b16965116109283615321575b5050506151ac575b5050509067ffffffffffffffff9493929161506a8a8c89614b6b565b969091161161507c575b505050505050565b88949750615097614067879a8c6150c19596979a9d99614e73565b948815615192576150b16150aa8b613657565b519a613633565b51965b891561518a575096615607565b1662278d0090818082109118029485821891670de0b6b3a7640000968784029384048814911417156104f757614de66150fb9287946156d3565b04918382028281048514831517156104f75761511690614e9c565b670de0b6b3a763ffff19938185019182116104f75761513861514c92846140a3565b90818082119118021896614de68389613894565b9282019182116104f75761516d9361516787611f65946138be565b04613ae6565b901561518557905b90916001905f8080808080615074565b615175565b905096615607565b6151a561519e8b613633565b519a613657565b51966150b4565b829b506151c19291949c5080939a5088614b6b565b9990918a1561530a576151ee906151d789613657565b519092945b670de0b6b3a7640000928184926138be565b04938083018093116104f7577f3fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff851685036104f7576152308560021b82613894565b671bc16d674ec7ffff1981019081116104f75761524c916138be565b6ec097ce7bc90715b34b9f100000000081018091116104f75761527b92615275614de692614e9c565b90613894565b670de0b6b3a763ffff1983019283116104f7577f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff831683036104f7576152cb610e4b916152d29460011b906138d1565b93846138be565b97156152f8579067ffffffffffffffff939291975b979860019790919293945f8061504e565b67ffffffffffffffff939291906152e7565b906151ee9061531889613633565b519092946151dc565b51161190505f8781615046565b5098505f9750505050505050565b90801561201b57670de0b6b3a7640000918281029281840414901517156104f7576001905f19830104019015150290565b60ff81146153a85760ff811690601f8211615399576040519161538f836132f4565b8252602082015290565b632cd44ac360e21b5f5260045ffd5b506040515f815f54916153ba83613569565b8083529260209060019081811690811561544357506001146153e5575b50506136ff92500382613310565b9150925f80527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563935f925b82841061542b57506136ff9450505081016020015f806153d7565b85548785018301529485019486945092810192615410565b915050602092506136ff94915060ff191682840152151560051b8201015f806153d7565b60ff81146154895760ff811690601f8211615399576040519161538f836132f4565b506040515f8160019160015461549e81613569565b808452936020916001811690811561544357506001146154c65750506136ff92500382613310565b91509260015f527fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6935f925b82841061550d57506136ff9450505081016020015f806153d7565b855487850183015294850194869450928101926154f2565b91907f7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0841161559c579160209360809260ff5f9560405194855216868401526040830152606082015282805260015afa15610544575f516001600160a01b0381161561559257905f905f90565b505f906001905f90565b5050505f9160039190565b60048110156134fb57806155b9575050565b600181036155d05763f645eedf60e01b5f5260045ffd5b600281036155eb575063fce698f760e01b5f5260045260245ffd5b6003146155f55750565b6335e2f38360e21b5f5260045260245ffd5b63ffffffff91821690821603919082116104f757565b90939192919063ffffffff90818116828416811061563f575050505050905090565b82851610156156cb57838261565a6156689661566194615607565b1693615607565b16906140a3565b916bffffffffffffffffffffffff8091169116670de0b6b3a7640000808202938285048214831517156104f7576156ae6156b4916156a9866136ff986138d1565b6156d3565b846138be565b049181808210911802188180821191180218614e1b565b505050505090565b670de0b6b3a7640000918083036156ea5750905090565b8290671bc16d674ec80000810361570757505080614dec916138be565b673782dace9d900000810361572b575061572482614dec936138be565b04806138be565b90506157369161579a565b6127108082029082820414821517156104f75760015f199384830104019015150290600182018083116104f757811015615771575050505f90565b030190565b80156138db576ec097ce7bc90715b34b9f10000000000590565b81156138db570590565b908015615f46578115615f40578160ff1c615f3257770bce5086492111aea88f4bb1ca6bcf584181ea8059f76532811015615f235781670c7d713b49da00001280615f12575b15615bc557670de0b6b3a7640000916ec097ce7bc90715b34b9f100000000090615823908402828101906ec097ce7bc90715b34b9f0fffffffff19018302615790565b9080828002059181838202058284820205838582020591848684020593858786020595808888020597880205600f900596600d900595600b900594600990059360079005926005900591600390050101010101010160011b918082818507020592050201670de0b6b3a7640000905b05680238fd42c5cf03ffff198181131580615bb2575b15615ba357819082121580615b90575b15615b81575f915f8112615b72575b506064906806f05b59d3b20000008112615b25576806f05b59d3b1ffffff190168056bc75e2d6310000082770195e54c5dd42177f53a27172fa9ec630262827000000000925b02819068ad78ebc5ac62000000811215615b02575b6856bc75e2d631000000811215615ade575b682b5e3af16b18800000811215615abc575b6815af1d78b58c400000811215615a9a575b680ad78ebc5ac6200000811215615a79575b82811215615a58575b6802b5e3af16b1880000811215615a37575b68015af1d78b58c40000811215615a16575b60028382800205056003848383020505600485848302050585600581868402050560068287830205056007838883020505906008848984020505926009858a8602050595600a868b8902050597600b878c8b02050599600c888d8d0205059b01010101010101010101010102050205905f146136ff576136ff90615776565b6806f5f1775788937937839168015af1d78b58c3ffff190192020590615997565b6808f00f760a4b2db55d83916802b5e3af16b187ffff190192020590615985565b680ebc5fb41746121110839168056bc75e2d630fffff190192020590615973565b68280e60114edb805d038391680ad78ebc5ac61fffff19019202059061596a565b690127fa27722cc06cc5e283916815af1d78b58c3fffff190192020590615958565b693f1fce3da636ea5cf8508391682b5e3af16b187fffff190192020590615946565b6b02df0ab5a80a22c61ab5a70083916856bc75e2d630ffffff190192020590615934565b6e01855144814a7ff805980ff0084000915068ad78ebc5ac61ffffff1901615922565b6803782dace9d90000008112615b5f576803782dace9d8ffffff190168056bc75e2d63100000826b1425982cf597cd205cef73809261590d565b68056bc75e2d631000008260019261590d565b600192505f03905060646158c7565b63d4794efd60e01b5f5260045ffd5b5068070c1cc73b00c800008213156158b8565b63a2f9f7e360e01b5f5260045ffd5b5068070c1cc73b00c800008213156158a8565b81670de0b6b3a7640000925f91848112615efc575b506064905f7e1600ef3172e58d2e933ec884fde10064c63b5372d805e203c0000000000000821215615ed1575b73011798004d755d3c8bc8e03204cf44619e000000821215615eb0575b820290808302906e01855144814a7ff805980ff00840009081831215615e8d575b50506b02df0ab5a80a22c61ab5a70080821215615e6d575b50693f1fce3da636ea5cf85080821215615e4d575b50690127fa27722cc06cc5e280821215615e2d575b5068280e60114edb805d0380821215615e0d575b50680ebc5fb4174612111080821215615df6575b506808f00f760a4b2db55d80821215615dd6575b506806f5f177578893793780821215615db6575b506806248f33704b28660380821215615d97575b506805c548670b9510e7ac80821215615d78575b50615d2568056bc75e2d6310000091828082019168056bc75e2d630fffff190102615790565b9080828002059181838202058284820205916003600560076009600b888a89020598808b8b02059a8b0205059805960594059205010101010160011b0105905f14615d73575f035b02615892565b615d6d565b68056bc75e2d631000006756bc75e2d63100009202059101905f615cff565b68056bc75e2d6310000067ad78ebc5ac6200009202059101905f615ceb565b68056bc75e2d6310000068015af1d78b58c400009202059101905f615cd7565b68056bc75e2d631000006802b5e3af16b18800009202059101905f615cc3565b68056bc75e2d63100000809202059101905f615caf565b68056bc75e2d63100000680ad78ebc5ac62000009202059101905f615c9b565b68056bc75e2d631000006815af1d78b58c4000009202059101905f615c87565b68056bc75e2d63100000682b5e3af16b188000009202059101905f615c72565b68056bc75e2d631000006856bc75e2d6310000009202059101905f615c5d565b68ad78ebc5ac62000000925069021e19e0c9bab240000002059101905f80615c45565b906b1425982cf597cd205cef73806803782dace9d900000091059101615c24565b50770195e54c5dd42177f53a27172fa9ec63026282700000000090056806f05b59d3b2000000615c07565b9050615f089150615776565b6001906064615bda565b50670f43fc2c04ee000082126157e0565b63d831731160e01b5f5260045ffd5b6211380f60e51b5f5260045ffd5b50505f90565b5050670de0b6b3a764000090565b91909180600314615f9f5780600114615f8b57600214615f8257634e487b7160e01b5f52605160045260245ffd5b6136ff91614d39565b50614dec90670de0b6b3a7640000926138be565b50670de0b6b3a7640000918281029281840414901517156104f7576136ff916138d156fea264697066735822122073bb74bdd2c560f8bc5cd564ffccc060dc947b788077fbef0933effb3542f54b64736f6c634300081b0033
Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)
0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba90000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000002c68af0bb14000000000000000000000000000000000000000000000000003635c9adc5dea000000000000000000000000000000000000000000000000000d8d726b7177a8000000000000000000000000000000000000000000000000000878678326eac90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001e444f204e4f5420555345202d204d6f636b205265436c616d6d20506f6f6c000000000000000000000000000000000000000000000000000000000000000000045445535400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004d7b226e616d65223a225265436c616d6d506f6f6c222c2276657273696f6e223a322c226465706c6f796d656e74223a2232303235303730322d76332d7265636c616d6d2d706f6f6c2d7632227d00000000000000000000000000000000000000
-----Decoded View---------------
Arg [0] : params (tuple):
Arg [1] : name (string): DO NOT USE - Mock ReClamm Pool
Arg [2] : symbol (string): TEST
Arg [3] : version (string): {"name":"ReClammPool","version":2,"deployment":"20250702-v3-reclamm-pool-v2"}
Arg [4] : dailyPriceShiftExponent (uint256): 1000000000000000000
Arg [5] : centerednessMargin (uint64): 200000000000000000
Arg [6] : initialMinPrice (uint256): 1000000000000000000000
Arg [7] : initialMaxPrice (uint256): 4000000000000000000000
Arg [8] : initialTargetPrice (uint256): 2500000000000000000000
Arg [9] : tokenAPriceIncludesRate (bool): False
Arg [10] : tokenBPriceIncludesRate (bool): False
Arg [1] : vault (address): 0xbA1333333333a1BA1108E8412f11850A5C319bA9
-----Encoded View---------------
20 Constructor Arguments found :
Arg [0] : 0000000000000000000000000000000000000000000000000000000000000040
Arg [1] : 000000000000000000000000ba1333333333a1ba1108e8412f11850a5c319ba9
Arg [2] : 0000000000000000000000000000000000000000000000000000000000000140
Arg [3] : 0000000000000000000000000000000000000000000000000000000000000180
Arg [4] : 00000000000000000000000000000000000000000000000000000000000001c0
Arg [5] : 0000000000000000000000000000000000000000000000000de0b6b3a7640000
Arg [6] : 00000000000000000000000000000000000000000000000002c68af0bb140000
Arg [7] : 00000000000000000000000000000000000000000000003635c9adc5dea00000
Arg [8] : 0000000000000000000000000000000000000000000000d8d726b7177a800000
Arg [9] : 0000000000000000000000000000000000000000000000878678326eac900000
Arg [10] : 0000000000000000000000000000000000000000000000000000000000000000
Arg [11] : 0000000000000000000000000000000000000000000000000000000000000000
Arg [12] : 000000000000000000000000000000000000000000000000000000000000001e
Arg [13] : 444f204e4f5420555345202d204d6f636b205265436c616d6d20506f6f6c0000
Arg [14] : 0000000000000000000000000000000000000000000000000000000000000004
Arg [15] : 5445535400000000000000000000000000000000000000000000000000000000
Arg [16] : 000000000000000000000000000000000000000000000000000000000000004d
Arg [17] : 7b226e616d65223a225265436c616d6d506f6f6c222c2276657273696f6e223a
Arg [18] : 322c226465706c6f796d656e74223a2232303235303730322d76332d7265636c
Arg [19] : 616d6d2d706f6f6c2d7632227d00000000000000000000000000000000000000
Loading...
Loading
Loading...
Loading
Multichain Portfolio | 34 Chains
| Chain | Token | Portfolio % | Price | Amount | Value |
|---|
Loading...
Loading
Loading...
Loading
Loading...
Loading
[ Download: CSV Export ]
A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.