ERC-20
Overview
Max Total Supply
182,823.553083388958040512 ECLP-paUSD-GYD
Holders
11
Market
Onchain Market Cap
$0.00
Circulating Supply Market Cap
-
Other Info
Token Contract (WITH 18 Decimals)
Balance
0.000000000000036336 ECLP-paUSD-GYDValue
$0.00Loading...
Loading
Loading...
Loading
Loading...
Loading
# | Exchange | Pair | Price | 24H Volume | % Volume |
---|
This contract may be a proxy contract. Click on More Options and select Is this a proxy? to confirm and enable the "Read as Proxy" & "Write as Proxy" tabs.
Similar Match Source Code This contract matches the deployed Bytecode of the Source Code for Contract 0x81E99852...9D23a5747 The constructor portion of the code might be different and could alter the actual behaviour of the contract
Contract Name:
GyroECLPPool
Compiler Version
v0.7.6+commit.7338295f
Optimization Enabled:
Yes with 200 runs
Other Settings:
berlin EvmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: LicenseRef-Gyro-1.0 // for information on licensing please see the README in the GitHub repository <https://github.com/gyrostable/concentrated-lps>. pragma solidity 0.7.6; pragma experimental ABIEncoderV2; // import "@balancer-labs/v2-solidity-utils/contracts/math/FixedPoint.sol"; import "../../libraries/GyroFixedPoint.sol"; import "@balancer-labs/v2-pool-weighted/contracts/WeightedPoolUserDataHelpers.sol"; import "@balancer-labs/v2-pool-weighted/contracts/WeightedPool2TokensMiscData.sol"; import "@balancer-labs/v2-pool-utils/contracts/interfaces/IRateProvider.sol"; import "../../libraries/GyroConfigKeys.sol"; import "../../libraries/GyroConfigHelpers.sol"; import "../../libraries/GyroErrors.sol"; import "../../interfaces/IGyroConfig.sol"; import "../../libraries/GyroPoolMath.sol"; import "../ExtensibleWeightedPool2Tokens.sol"; import "./GyroECLPMath.sol"; import "../CappedLiquidity.sol"; import "../LocallyPausable.sol"; contract GyroECLPPool is ExtensibleWeightedPool2Tokens, CappedLiquidity, LocallyPausable { using GyroFixedPoint for uint256; using WeightedPoolUserDataHelpers for bytes; using WeightedPool2TokensMiscData for bytes32; using SafeCast for int256; using SafeCast for uint256; using GyroConfigHelpers for IGyroConfig; uint256 private constant _MINIMUM_BPT = 1e6; bytes32 private constant POOL_TYPE = "ECLP"; /// @dev Parameters of the ECLP pool int256 internal immutable _paramsAlpha; int256 internal immutable _paramsBeta; int256 internal immutable _paramsC; int256 internal immutable _paramsS; int256 internal immutable _paramsLambda; int256 internal immutable _tauAlphaX; int256 internal immutable _tauAlphaY; int256 internal immutable _tauBetaX; int256 internal immutable _tauBetaY; int256 internal immutable _u; int256 internal immutable _v; int256 internal immutable _w; int256 internal immutable _z; int256 internal immutable _dSq; IGyroConfig public gyroConfig; /// @dev for rate scaling IRateProvider public immutable rateProvider0; IRateProvider public immutable rateProvider1; struct GyroParams { NewPoolParams baseParams; GyroECLPMath.Params eclpParams; GyroECLPMath.DerivedParams derivedEclpParams; address rateProvider0; address rateProvider1; address capManager; CapParams capParams; address pauseManager; } event ECLPParamsValidated(bool paramsValidated); event ECLPDerivedParamsValidated(bool derivedParamsValidated); event InvariantAterInitializeJoin(uint256 invariantAfterJoin); event InvariantOldAndNew(uint256 oldInvariant, uint256 newInvariant); event SwapParams(uint256[] balances, GyroECLPMath.Vector2 invariant, uint256 amount); constructor(GyroParams memory params, address configAddress) ExtensibleWeightedPool2Tokens(params.baseParams) CappedLiquidity(params.capManager, params.capParams) LocallyPausable(params.pauseManager) { _grequire(configAddress != address(0x0), GyroECLPPoolErrors.ADDRESS_IS_ZERO_ADDRESS); GyroECLPMath.validateParams(params.eclpParams); emit ECLPParamsValidated(true); GyroECLPMath.validateDerivedParamsLimits(params.eclpParams, params.derivedEclpParams); emit ECLPDerivedParamsValidated(true); (_paramsAlpha, _paramsBeta, _paramsC, _paramsS, _paramsLambda) = ( params.eclpParams.alpha, params.eclpParams.beta, params.eclpParams.c, params.eclpParams.s, params.eclpParams.lambda ); (_tauAlphaX, _tauAlphaY, _tauBetaX, _tauBetaY, _u, _v, _w, _z, _dSq) = ( params.derivedEclpParams.tauAlpha.x, params.derivedEclpParams.tauAlpha.y, params.derivedEclpParams.tauBeta.x, params.derivedEclpParams.tauBeta.y, params.derivedEclpParams.u, params.derivedEclpParams.v, params.derivedEclpParams.w, params.derivedEclpParams.z, params.derivedEclpParams.dSq ); gyroConfig = IGyroConfig(configAddress); rateProvider0 = IRateProvider(params.rateProvider0); rateProvider1 = IRateProvider(params.rateProvider1); } /** @dev reconstructs ECLP params structs from immutable arrays */ function reconstructECLPParams() internal view returns (GyroECLPMath.Params memory params, GyroECLPMath.DerivedParams memory d) { (params.alpha, params.beta, params.c, params.s, params.lambda) = (_paramsAlpha, _paramsBeta, _paramsC, _paramsS, _paramsLambda); (d.tauAlpha.x, d.tauAlpha.y, d.tauBeta.x, d.tauBeta.y) = (_tauAlphaX, _tauAlphaY, _tauBetaX, _tauBetaY); (d.u, d.v, d.w, d.z, d.dSq) = (_u, _v, _w, _z, _dSq); } function getECLPParams() external view returns (GyroECLPMath.Params memory params, GyroECLPMath.DerivedParams memory d) { return reconstructECLPParams(); } /** @dev Reads the balance of a token from the balancer vault and returns the scaled amount. Smaller storage access * compared to getVault().getPoolTokens(). * Copied from the 3CLP *except* that for the 2CLP, the scalingFactor is interpreted as a regular integer, not a * FixedPoint number. This is an inconsistency between the base contracts. */ function _getScaledTokenBalance(IERC20 token, uint256 scalingFactor) internal view returns (uint256 balance) { // Signature of getPoolTokenInfo(): (pool id, token) -> (cash, managed, lastChangeBlock, assetManager) // and total amount = cash + managed. See balancer repo, PoolTokens.sol and BalanceAllocation.sol (uint256 cash, uint256 managed, , ) = getVault().getPoolTokenInfo(getPoolId(), token); balance = cash + managed; // can't overflow, see BalanceAllocation.sol::total() in the Balancer repo. balance = balance.mulDown(scalingFactor); } /** @dev Get all balances in the pool, scaled by the appropriate scaling factors, in a relatively gas-efficient way. * Essentially copied from the 3CLP. */ function _getAllBalances() internal view returns (uint256[] memory balances) { // The below is more gas-efficient than the following line because the token slots don't have to be read in the // vault. // (, uint256[] memory balances, ) = getVault().getPoolTokens(getPoolId()); balances = new uint256[](2); balances[0] = _getScaledTokenBalance(_token0, _scalingFactor(true)); balances[1] = _getScaledTokenBalance(_token1, _scalingFactor(false)); return balances; } /** * @dev Returns the current value of the invariant. * Note: This function is not used internally; it's public, not external, so we can override it cleanly. */ function getInvariant() public view override returns (uint256) { uint256[] memory balances = _getAllBalances(); (GyroECLPMath.Params memory eclpParams, GyroECLPMath.DerivedParams memory derivedECLPParams) = reconstructECLPParams(); return GyroECLPMath.calculateInvariant(balances, eclpParams, derivedECLPParams); } /** When rateProvider{0,1} is provided, this returns the *scaled* price, suitable to compare *rate scaled* balances. * To compare (decimal- but) not-rate-scaled balances, apply _adjustPrice() to the result. */ function _getPrice( uint256[] memory balances, uint256 invariant, GyroECLPMath.Params memory eclpParams, GyroECLPMath.DerivedParams memory derivedECLPParams ) internal view returns (uint256 spotPrice) { spotPrice = GyroECLPMath.calcSpotPrice0in1(balances, eclpParams, derivedECLPParams, invariant.toInt256()); } /** Returns the current spot price of token0 quoted in units of token1. When rateProvider{0,1} is provided, the * returned price corresponds to tokens *before* rate scaling. */ function getPrice() external view returns (uint256 spotPrice) { uint256[] memory balances = _getAllBalances(); (GyroECLPMath.Params memory eclpParams, GyroECLPMath.DerivedParams memory derivedECLPParams) = reconstructECLPParams(); uint256 invariant = GyroECLPMath.calculateInvariant(balances, eclpParams, derivedECLPParams); spotPrice = _getPrice(balances, invariant, eclpParams, derivedECLPParams); spotPrice = _adjustPrice(spotPrice); } // Swap Hooks function onSwap( SwapRequest memory request, uint256 balanceTokenIn, uint256 balanceTokenOut ) public virtual override whenNotPaused onlyVault(request.poolId) returns (uint256) { bool tokenInIsToken0; if (request.tokenIn == _token0 && request.tokenOut == _token1) { tokenInIsToken0 = true; } else if (request.tokenIn == _token1 && request.tokenOut == _token0) { tokenInIsToken0 = false; } else { _revert(GyroECLPPoolErrors.TOKEN_IN_IS_NOT_TOKEN_0); } uint256 scalingFactorTokenIn = _scalingFactor(tokenInIsToken0); uint256 scalingFactorTokenOut = _scalingFactor(!tokenInIsToken0); // All token amounts are upscaled. balanceTokenIn = _upscale(balanceTokenIn, scalingFactorTokenIn); balanceTokenOut = _upscale(balanceTokenOut, scalingFactorTokenOut); // We "undo" the pre-processing that the caller of onSwap() did: In contrast to other pools, we don't exploit // symmetry here, and we identify the two tokens explicitly. uint256[] memory balances = _balancesFromTokenInOut(balanceTokenIn, balanceTokenOut, tokenInIsToken0); (GyroECLPMath.Params memory eclpParams, GyroECLPMath.DerivedParams memory derivedECLPParams) = reconstructECLPParams(); GyroECLPMath.Vector2 memory invariant; { (int256 currentInvariant, int256 invErr) = GyroECLPMath.calculateInvariantWithError(balances, eclpParams, derivedECLPParams); // invariant = overestimate in x-component, underestimate in y-component // No overflow in `+` due to constraints to the different values enforced in GyroECLPMath. invariant = GyroECLPMath.Vector2(currentInvariant + 2 * invErr, currentInvariant); } if (request.kind == IVault.SwapKind.GIVEN_IN) { // Fees are subtracted before scaling, to reduce the complexity of the rounding direction analysis. // This is amount - fee amount, so we round up (favoring a higher fee amount). uint256 feeAmount = request.amount.mulUp(getSwapFeePercentage()); request.amount = _upscale(request.amount.sub(feeAmount), scalingFactorTokenIn); uint256 amountOut = _onSwapGivenIn(request, balances, tokenInIsToken0, eclpParams, derivedECLPParams, invariant); emit SwapParams(balances, invariant, amountOut); // amountOut tokens are exiting the Pool, so we round down. return _downscaleDown(amountOut, scalingFactorTokenOut); } else { request.amount = _upscale(request.amount, scalingFactorTokenOut); uint256 amountIn = _onSwapGivenOut(request, balances, tokenInIsToken0, eclpParams, derivedECLPParams, invariant); emit SwapParams(balances, invariant, amountIn); // amountIn tokens are entering the Pool, so we round up. amountIn = _downscaleUp(amountIn, scalingFactorTokenIn); // Fees are added after scaling happens, to reduce the complexity of the rounding direction analysis. // This is amount + fee amount, so we round up (favoring a higher fee amount). return amountIn.divUp(getSwapFeePercentage().complement()); } } function _onSwapGivenIn( SwapRequest memory swapRequest, uint256[] memory balances, bool tokenInIsToken0, GyroECLPMath.Params memory eclpParams, GyroECLPMath.DerivedParams memory derivedECLPParams, GyroECLPMath.Vector2 memory invariant ) private pure returns (uint256) { // Swaps are disabled while the contract is paused. return GyroECLPMath.calcOutGivenIn(balances, swapRequest.amount, tokenInIsToken0, eclpParams, derivedECLPParams, invariant); } function _onSwapGivenOut( SwapRequest memory swapRequest, uint256[] memory balances, bool tokenInIsToken0, GyroECLPMath.Params memory eclpParams, GyroECLPMath.DerivedParams memory derivedECLPParams, GyroECLPMath.Vector2 memory invariant ) private pure returns (uint256) { // Swaps are disabled while the contract is paused. return GyroECLPMath.calcInGivenOut(balances, swapRequest.amount, tokenInIsToken0, eclpParams, derivedECLPParams, invariant); } /** * @dev Called when the Pool is joined for the first time; that is, when the BPT total supply is zero. * * Returns the amount of BPT to mint, and the token amounts the Pool will receive in return. * * Minted BPT will be sent to `recipient`, except for _MINIMUM_BPT, which will be deducted from this amount and sent * to the zero address instead. This will cause that BPT to remain forever locked there, preventing total BTP from * ever dropping below that value, and ensuring `_onInitializePool` can only be called once in the entire Pool's * lifetime. * * The tokens granted to the Pool will be transferred from `sender`. These amounts are considered upscaled and will * be downscaled (rounding up) before being returned to the Vault. */ function _onInitializePool( bytes32, address, address, bytes memory userData ) internal override returns (uint256, uint256[] memory) { BaseWeightedPool.JoinKind kind = userData.joinKind(); _require(kind == BaseWeightedPool.JoinKind.INIT, Errors.UNINITIALIZED); uint256[] memory amountsIn = userData.initialAmountsIn(); InputHelpers.ensureInputLengthMatch(amountsIn.length, 2); _upscaleArray(amountsIn); (GyroECLPMath.Params memory eclpParams, GyroECLPMath.DerivedParams memory derivedECLPParams) = reconstructECLPParams(); uint256 invariantAfterJoin = GyroECLPMath.calculateInvariant(amountsIn, eclpParams, derivedECLPParams); emit InvariantAterInitializeJoin(invariantAfterJoin); /* We initialize the number of BPT tokens such that one BPT token corresponds to one unit of token1 at the initialized pool price. This makes BPT tokens comparable across pools with different parameters. Note that the invariant does *not* have this property! */ uint256 spotPrice = _getPrice(amountsIn, invariantAfterJoin, eclpParams, derivedECLPParams); uint256 bptAmountOut = Math.add(amountsIn[0].mulDown(spotPrice), amountsIn[1]); _lastInvariant = invariantAfterJoin; return (bptAmountOut, amountsIn); } /** * @dev Called whenever the Pool is joined after the first initialization join (see `_onInitializePool`). * * Returns the amount of BPT to mint, the token amounts that the Pool will receive in return, and the number of * tokens to pay in protocol swap fees. * * Implementations of this function might choose to mutate the `balances` array to save gas (e.g. when * performing intermediate calculations, such as subtraction of due protocol fees). This can be done safely. * * Minted BPT will be sent to `recipient`. * * The tokens granted to the Pool will be transferred from `sender`. These amounts are considered upscaled and will * be downscaled (rounding up) before being returned to the Vault. * * Due protocol swap fees will be taken from the Pool's balance in the Vault (see `IBasePool.onJoinPool`). These * amounts are considered upscaled and will be downscaled (rounding down) before being returned to the Vault. * * protocolSwapFeePercentage argument is intentionally unused as protocol fees are handled in a different way */ function _onJoinPool( bytes32, address, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256, //protocolSwapFeePercentage, bytes memory userData ) internal override returns ( uint256, uint256[] memory, uint256[] memory ) { // Due protocol swap fee amounts are computed by measuring the growth of the invariant between the previous join // or exit event and now - the invariant's growth is due exclusively to swap fees. This avoids spending gas // computing them on each individual swap (GyroECLPMath.Params memory eclpParams, GyroECLPMath.DerivedParams memory derivedECLPParams) = reconstructECLPParams(); uint256 invariantBeforeAction = GyroECLPMath.calculateInvariant(balances, eclpParams, derivedECLPParams); _distributeFees(invariantBeforeAction); (uint256 bptAmountOut, uint256[] memory amountsIn) = _doJoin(balances, userData); if (_capParams.capEnabled) { _ensureCap(bptAmountOut, balanceOf(recipient), totalSupply()); } // Since we pay fees in BPT, they have not changed the invariant and 'invariantBeforeAction' is still consistent with // 'balances'. Therefore, we can use a simplified method to update the invariant that does not require a full // re-computation. // Note: Should this be changed in the future, we also need to reduce the invariant proportionally by the total // protocol fee factor. _lastInvariant = GyroPoolMath.liquidityInvariantUpdate(invariantBeforeAction, bptAmountOut, totalSupply(), true); emit InvariantOldAndNew(invariantBeforeAction, _lastInvariant); // returns a new uint256[](2) b/c Balancer vault is expecting a fee array, but fees paid in BPT instead return (bptAmountOut, amountsIn, new uint256[](2)); } function _doJoin(uint256[] memory balances, bytes memory userData) internal view returns (uint256 bptAmountOut, uint256[] memory amountsIn) { BaseWeightedPool.JoinKind kind = userData.joinKind(); // We do NOT currently support unbalanced update, i.e., EXACT_TOKENS_IN_FOR_BPT_OUT or TOKEN_IN_FOR_EXACT_BPT_OUT if (kind == BaseWeightedPool.JoinKind.ALL_TOKENS_IN_FOR_EXACT_BPT_OUT) { (bptAmountOut, amountsIn) = _joinAllTokensInForExactBPTOut(balances, userData); } else { _revert(Errors.UNHANDLED_JOIN_KIND); } } function _joinAllTokensInForExactBPTOut(uint256[] memory balances, bytes memory userData) internal view override returns (uint256, uint256[] memory) { uint256 bptAmountOut = userData.allTokensInForExactBptOut(); // Note that there is no maximum amountsIn parameter: this is handled by `IVault.joinPool`. uint256[] memory amountsIn = GyroPoolMath._calcAllTokensInGivenExactBptOut(balances, bptAmountOut, totalSupply()); return (bptAmountOut, amountsIn); } /** * @dev Called whenever the Pool is exited. * * Returns the amount of BPT to burn, the token amounts for each Pool token that the Pool will grant in return, and * the number of tokens to pay in protocol swap fees. * * Implementations of this function might choose to mutate the `balances` array to save gas (e.g. when * performing intermediate calculations, such as subtraction of due protocol fees). This can be done safely. * * BPT will be burnt from `sender`. * * The Pool will grant tokens to `recipient`. These amounts are considered upscaled and will be downscaled * (rounding down) before being returned to the Vault. * * Due protocol swap fees will be taken from the Pool's balance in the Vault (see `IBasePool.onExitPool`). These * amounts are considered upscaled and will be downscaled (rounding down) before being returned to the Vault. * * protocolSwapFeePercentage argument is intentionally unused as protocol fees are handled in a different way */ function _onExitPool( bytes32, address, address, uint256[] memory balances, uint256 lastChangeBlock, uint256, // protocolSwapFeePercentage, bytes memory userData ) internal override returns ( uint256 bptAmountIn, uint256[] memory amountsOut, uint256[] memory dueProtocolFeeAmounts ) { // Exits are not completely disabled while the contract is paused: proportional exits (exact BPT in for tokens // out) remain functional. (GyroECLPMath.Params memory eclpParams, GyroECLPMath.DerivedParams memory derivedECLPParams) = reconstructECLPParams(); // Note: If the contract is paused, swap protocol fee amounts are not charged // to avoid extra calculations and reduce the potential for errors. if (_isNotPaused()) { // Due protocol swap fee amounts are computed by measuring the growth of the invariant between the previous // join or exit event and now - the invariant's growth is due exclusively to swap fees. This avoids // spending gas calculating the fees on each individual swap. uint256 invariantBeforeAction = GyroECLPMath.calculateInvariant(balances, eclpParams, derivedECLPParams); _distributeFees(invariantBeforeAction); (bptAmountIn, amountsOut) = _doExit(balances, userData); // Since we pay fees in BPT, they have not changed the invariant and 'invariantBeforeAction' is still consistent with // 'balances'. Therefore, we can use a simplified method to update the invariant that does not require a full // re-computation. // Note: Should this be changed in the future, we also need to reduce the invariant proportionally by the total // protocol fee factor. _lastInvariant = GyroPoolMath.liquidityInvariantUpdate(invariantBeforeAction, bptAmountIn, totalSupply(), false); emit InvariantOldAndNew(invariantBeforeAction, _lastInvariant); } else { // Note: If the contract is paused, swap protocol fee amounts are not charged // to avoid extra calculations and reduce the potential for errors. (bptAmountIn, amountsOut) = _doExit(balances, userData); // Invalidate _lastInvariant. We do not compute the invariant to make sure the pool is not locking // up b/c numerical limits might be violated. Instead, we set the invariant such that any following // (non-paused) join/exit will ignore and recompute it. (see GyroPoolMath._calcProtocolFees()) _lastInvariant = type(uint256).max; } // returns a new uint256[](2) b/c Balancer vault is expecting a fee array, but fees paid in BPT instead return (bptAmountIn, amountsOut, new uint256[](2)); } function _doExit(uint256[] memory balances, bytes memory userData) internal view returns (uint256 bptAmountIn, uint256[] memory amountsOut) { BaseWeightedPool.ExitKind kind = userData.exitKind(); // We do NOT support unbalanced exit at the moment, i.e., EXACT_BPT_IN_FOR_ONE_TOKEN_OUT or // BPT_IN_FOR_EXACT_TOKENS_OUT. if (kind == BaseWeightedPool.ExitKind.EXACT_BPT_IN_FOR_TOKENS_OUT) { (bptAmountIn, amountsOut) = _exitExactBPTInForTokensOut(balances, userData); } else { _revert(Errors.UNHANDLED_EXIT_KIND); } } function _exitExactBPTInForTokensOut(uint256[] memory balances, bytes memory userData) internal view override returns (uint256, uint256[] memory) { // This exit function is the only one that is not disabled if the contract is paused: it remains unrestricted // in an attempt to provide users with a mechanism to retrieve their tokens in case of an emergency. // This particular exit function is the only one that remains available because it is the simplest one, and // therefore the one with the lowest likelihood of errors. uint256 bptAmountIn = userData.exactBptInForTokensOut(); // Note that there is no minimum amountOut parameter: this is handled by `IVault.exitPool`. uint256[] memory amountsOut = GyroPoolMath._calcTokensOutGivenExactBptIn(balances, bptAmountIn, totalSupply()); return (bptAmountIn, amountsOut); } // Helpers. function _balancesFromTokenInOut( uint256 balanceTokenIn, uint256 balanceTokenOut, bool tokenInIsToken0 ) internal pure returns (uint256[] memory balances) { balances = new uint256[](2); if (tokenInIsToken0) { balances[0] = balanceTokenIn; balances[1] = balanceTokenOut; } else { balances[0] = balanceTokenOut; balances[1] = balanceTokenIn; } } // Fee helpers. These are exactly the same as in the Gyro2CLPPool. // TODO prob about time to make a base class. /** * Note: This function is identical to that used in Gyro2CLPPool.sol * @dev Computes and distributes fees between the Balancer and the Gyro treasury * The fees are computed and distributed in BPT rather than using the * Balancer regular distribution mechanism which would pay these in underlying */ function _distributeFees(uint256 invariantBeforeAction) internal { // calculate Protocol fees in BPT // lastInvariant is the invariant logged at the end of the last liquidity update // protocol fees are calculated on swap fees earned between liquidity updates (uint256 gyroFees, uint256 balancerFees, address gyroTreasury, address balTreasury) = _getDueProtocolFeeAmounts( _lastInvariant, invariantBeforeAction ); // Pay fees in BPT _payFeesBpt(gyroFees, balancerFees, gyroTreasury, balTreasury); } /** * Note: This function is identical to that used in Gyro2CLPPool.sol * @dev this function overrides inherited function to make sure it is never used */ function _getDueProtocolFeeAmounts( uint256[] memory, // balances, uint256[] memory, // normalizedWeights, uint256, // previousInvariant, uint256, // currentInvariant, uint256 // protocolSwapFeePercentage ) internal pure override returns (uint256[] memory) { revert("Not implemented"); } /** * @dev * Note: This function is identical to that used in Gyro2CLPPool.sol. * Calculates protocol fee amounts in BPT terms. * protocolSwapFeePercentage is not used here b/c we take parameters from GyroConfig instead. * Returns: BPT due to Gyro, BPT due to Balancer, receiving address for Gyro fees, receiving address for Balancer * fees. */ function _getDueProtocolFeeAmounts(uint256 previousInvariant, uint256 currentInvariant) internal view returns ( uint256, uint256, address, address ) { (uint256 protocolSwapFeePerc, uint256 protocolFeeGyroPortion, address gyroTreasury, address balTreasury) = getFeesMetadata(); // Early return if the protocol swap fee percentage is zero, saving gas. if (protocolSwapFeePerc == 0) { return (0, 0, gyroTreasury, balTreasury); } // Calculate fees in BPT (uint256 gyroFees, uint256 balancerFees) = GyroPoolMath._calcProtocolFees( previousInvariant, currentInvariant, totalSupply(), protocolSwapFeePerc, protocolFeeGyroPortion ); return (gyroFees, balancerFees, gyroTreasury, balTreasury); } // Note: This function is identical to that used in Gyro2CLPPool.sol function _payFeesBpt( uint256 gyroFees, uint256 balancerFees, address gyroTreasury, address balTreasury ) internal { // Pay fees in BPT to gyro treasury if (gyroFees > 0) { _mintPoolTokens(gyroTreasury, gyroFees); } // Pay fees in BPT to bal treasury if (balancerFees > 0) { _mintPoolTokens(balTreasury, balancerFees); } } // Note: This function is identical to that used in Gyro2CLPPool.sol function getFeesMetadata() public view returns ( uint256, uint256, address, address ) { return ( gyroConfig.getSwapFeePercForPool(address(this), POOL_TYPE), gyroConfig.getProtocolFeeGyroPortionForPool(address(this), POOL_TYPE), gyroConfig.getAddress(GyroConfigKeys.GYRO_TREASURY_KEY), gyroConfig.getAddress(GyroConfigKeys.BAL_TREASURY_KEY) ); } /** @notice Effective BPT supply. * * This is the same as `totalSupply()` but also accounts for the fact that the pool owes * protocol fees to the pool in the form of unminted LP shares created on the next join/exit, * diluting LPers. Thus, this is the totalSupply() that the next join/exit operation will see. * * Equivalent to the respective function in, e.g., WeightedPool, see: * https://github.com/balancer/balancer-v2-monorepo/blob/master/pkg/pool-weighted/contracts/WeightedPool.sol#L325-L344 */ function getActualSupply() external view returns (uint256) { uint256 supply = totalSupply(); (uint256 gyroFees, uint256 balancerFees, , ) = _getDueProtocolFeeAmounts(_lastInvariant, getInvariant()); return supply.add(gyroFees).add(balancerFees); } /// @notice Equivalent to but more efficient than `getInvariant().divDown(getActualSupply())`. function getInvariantDivActualSupply() external view returns (uint256) { uint256 invariant = getInvariant(); uint256 supply = totalSupply(); (uint256 gyroFees, uint256 balancerFees, , ) = _getDueProtocolFeeAmounts(_lastInvariant, invariant); uint256 actualSupply = supply.add(gyroFees).add(balancerFees); return invariant.divDown(actualSupply); } function _setPausedState(bool paused) internal override { _setPaused(paused); } // Rate scaling function _scalingFactor(bool token0) internal view override returns (uint256) { IRateProvider rateProvider; uint256 scalingFactor; if (token0) { rateProvider = rateProvider0; scalingFactor = _scalingFactor0; } else { rateProvider = rateProvider1; scalingFactor = _scalingFactor1; } if (address(rateProvider) != address(0)) scalingFactor = scalingFactor.mulDown(rateProvider.getRate()); return scalingFactor; } function _adjustPrice(uint256 spotPrice) internal view override returns (uint256) { if (address(rateProvider0) != address(0)) spotPrice = spotPrice.mulDown(rateProvider0.getRate()); if (address(rateProvider1) != address(0)) spotPrice = spotPrice.divDown(rateProvider1.getRate()); return spotPrice; } /// @notice Convenience function to fetch the two rates used for scaling the two tokens, as of rateProvider{0,1}. /// Note that these rates do *not* contain scaling to account for differences in the number of decimals. The rates /// themselves are always 18-decimals. function getTokenRates() public view returns (uint256 rate0, uint256 rate1) { rate0 = address(rateProvider0) != address(0) ? rateProvider0.getRate() : GyroFixedPoint.ONE; rate1 = address(rateProvider1) != address(0) ? rateProvider1.getRate() : GyroFixedPoint.ONE; } }
// SPDX-License-Identifier: LicenseRef-Gyro-1.0 // for information on licensing please see the README in the GitHub repository <https://github.com/gyrostable/concentrated-lps>. pragma solidity ^0.7.0; import "@balancer-labs/v2-solidity-utils/contracts/math/LogExpMath.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/BalancerErrors.sol"; import "@balancer-labs/v2-solidity-utils/contracts/math/Math.sol"; /* solhint-disable private-vars-leading-underscore */ // Gyroscope: Copied from Balancer's FixedPoint library. We added a few additional functions and made _require()s more // gas-efficient. // We renamed this to `GyroFixedPoint` to avoid name clashes with functions used in other Balancer libraries we use. library GyroFixedPoint { uint256 internal constant ONE = 1e18; // 18 decimal places uint256 internal constant MIDDECIMAL = 1e9; // splits the fixed point decimals into two equal parts. uint256 internal constant MAX_POW_RELATIVE_ERROR = 10000; // 10^(-14) // Minimum base for the power function when the exponent is 'free' (larger than ONE). uint256 internal constant MIN_POW_BASE_FREE_EXPONENT = 0.7e18; function add(uint256 a, uint256 b) internal pure returns (uint256) { // Fixed Point addition is the same as regular checked addition uint256 c = a + b; if (!(c >= a)) { _require(false, Errors.ADD_OVERFLOW); } return c; } function sub(uint256 a, uint256 b) internal pure returns (uint256) { // Fixed Point addition is the same as regular checked addition if (!(b <= a)) { _require(false, Errors.SUB_OVERFLOW); } uint256 c = a - b; return c; } function mulDown(uint256 a, uint256 b) internal pure returns (uint256) { uint256 product = a * b; if (!(a == 0 || product / a == b)) { _require(false, Errors.MUL_OVERFLOW); } return product / ONE; } /// @dev "U" denotes version of the math function that does not check for overflows in order to save gas function mulDownU(uint256 a, uint256 b) internal pure returns (uint256) { return (a * b) / ONE; } function mulUp(uint256 a, uint256 b) internal pure returns (uint256) { uint256 product = a * b; if (!(a == 0 || product / a == b)) { _require(false, Errors.MUL_OVERFLOW); } if (product == 0) { return 0; } // 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, which we already tested for. return ((product - 1) / ONE) + 1; } function mulUpU(uint256 a, uint256 b) internal pure returns (uint256) { uint256 product = a * b; if (product == 0) { return 0; } // 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, which we already tested for. return ((product - 1) / ONE) + 1; } function divDown(uint256 a, uint256 b) internal pure returns (uint256) { if (b == 0) { _require(false, Errors.ZERO_DIVISION); } if (a == 0) { return 0; } uint256 aInflated = a * ONE; if (!(aInflated / a == ONE)) { _require(false, Errors.DIV_INTERNAL); // mul overflow } return aInflated / b; } function divDownU(uint256 a, uint256 b) internal pure returns (uint256) { if (b == 0) { _require(false, Errors.ZERO_DIVISION); } return (a * ONE) / b; } function divUp(uint256 a, uint256 b) internal pure returns (uint256) { if (b == 0) { _require(false, Errors.ZERO_DIVISION); } if (a == 0) { return 0; } uint256 aInflated = a * ONE; if (!(aInflated / a == ONE)) { _require(aInflated / a == ONE, Errors.DIV_INTERNAL); // mul overflow } // 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, which we already tested for. return ((aInflated - 1) / b) + 1; } function divUpU(uint256 a, uint256 b) internal pure returns (uint256) { if (b == 0) { _require(false, Errors.ZERO_DIVISION); } if (a == 0) { return 0; } return ((a * ONE - 1) / b) + 1; } /** * @dev Like mulDown(), but it also works in some situations where mulDown(a, b) would overflow because a * b is too * large. We achieve this by splitting up `a` into its integer and its fractional part. `a` should be the bigger of * the two numbers to achieve the best overflow guarantees. * This won't overflow if both of * - a * b ≤ 1.15e95 (raw values, i.e., a * b ≤ 1.15e59 with respect to the fixed-point values that they describe) * - b ≤ 1.15e59 (raw values, i.e., a ≤ 1.15e41 with respect to the values that a describes) * hold. That's better than mulDown(), where we would need a * b ≤ 1.15e77 approximately. */ function mulDownLargeSmall(uint256 a, uint256 b) internal pure returns (uint256) { return add(Math.mul(a / ONE, b), mulDown(a % ONE, b)); } function mulDownLargeSmallU(uint256 a, uint256 b) internal pure returns (uint256) { return (a / ONE) * b + mulDownU(a % ONE, b); } /** * @dev Like divDown(), but it also works when `a` would overflow in `divDown`. This is safe if both of * - a ≤ 1.15e68 (raw, i.e., a ≤ 1.15e50 with respect to the value that is represented) * - b ≥ 1e9 (raw, i.e., b ≥ 1e-9 with respect to the value represented) * hold. For `divDown` it's 1.15e59 and 1.15e41, respectively. * Introduces some rounding error that is relevant iff b is small. */ function divDownLarge(uint256 a, uint256 b) internal pure returns (uint256) { return divDownLarge(a, b, MIDDECIMAL, MIDDECIMAL); } function divDownLargeU(uint256 a, uint256 b) internal pure returns (uint256) { return divDownLargeU(a, b, MIDDECIMAL, MIDDECIMAL); } /** * @dev Like divDown(), but it also works when `a` would overflow in `divDown`. d and e must be chosen such that * d * e = 1e18 (raw numbers, or d * e = 1e-18 with respect to the numbers they represent in fixed point). Note that * this requires d, e ≤ 1e18 (raw, or d, e ≤ 1 with respect to the numbers represented). * This operation is safe if both of * - a * d ≤ 1.15e77 (raw, i.e., a * d ≤ 1.15e41 with respect to the value that is represented) * - b ≥ e (with respect to raw or represented numbers) * hold. * Introduces some rounding error that is relevant iff b is small and is proportional to e. */ function divDownLarge( uint256 a, uint256 b, uint256 d, uint256 e ) internal pure returns (uint256) { return Math.divDown(Math.mul(a, d), Math.divUp(b, e)); } /// @dev e is assumed to be non-zero, and so division by zero is not checked for it function divDownLargeU( uint256 a, uint256 b, uint256 d, uint256 e ) internal pure returns (uint256) { // (a * d) / (b / e) if (b == 0) { // In this case only, the denominator of the outer division is zero, and we revert _require(false, Errors.ZERO_DIVISION); } uint256 denom = 1 + (b - 1) / e; return (a * d) / denom; } /** * @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) { uint256 raw = LogExpMath.pow(x, y); uint256 maxError = add(mulUp(raw, MAX_POW_RELATIVE_ERROR), 1); if (raw < maxError) { return 0; } return sub(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) { uint256 raw = LogExpMath.pow(x, y); uint256 maxError = add(mulUp(raw, MAX_POW_RELATIVE_ERROR), 1); return add(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) { return (x < ONE) ? (ONE - x) : 0; } }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; import "@balancer-labs/v2-solidity-utils/contracts/openzeppelin/IERC20.sol"; import "./BaseWeightedPool.sol"; library WeightedPoolUserDataHelpers { function joinKind(bytes memory self) internal pure returns (BaseWeightedPool.JoinKind) { return abi.decode(self, (BaseWeightedPool.JoinKind)); } function exitKind(bytes memory self) internal pure returns (BaseWeightedPool.ExitKind) { return abi.decode(self, (BaseWeightedPool.ExitKind)); } // Joins function initialAmountsIn(bytes memory self) internal pure returns (uint256[] memory amountsIn) { (, amountsIn) = abi.decode(self, (BaseWeightedPool.JoinKind, uint256[])); } function exactTokensInForBptOut(bytes memory self) internal pure returns (uint256[] memory amountsIn, uint256 minBPTAmountOut) { (, amountsIn, minBPTAmountOut) = abi.decode(self, (BaseWeightedPool.JoinKind, uint256[], uint256)); } function tokenInForExactBptOut(bytes memory self) internal pure returns (uint256 bptAmountOut, uint256 tokenIndex) { (, bptAmountOut, tokenIndex) = abi.decode(self, (BaseWeightedPool.JoinKind, uint256, uint256)); } function allTokensInForExactBptOut(bytes memory self) internal pure returns (uint256 bptAmountOut) { (, bptAmountOut) = abi.decode(self, (BaseWeightedPool.JoinKind, uint256)); } // Exits function exactBptInForTokenOut(bytes memory self) internal pure returns (uint256 bptAmountIn, uint256 tokenIndex) { (, bptAmountIn, tokenIndex) = abi.decode(self, (BaseWeightedPool.ExitKind, uint256, uint256)); } function exactBptInForTokensOut(bytes memory self) internal pure returns (uint256 bptAmountIn) { (, bptAmountIn) = abi.decode(self, (BaseWeightedPool.ExitKind, uint256)); } function bptInForExactTokensOut(bytes memory self) internal pure returns (uint256[] memory amountsOut, uint256 maxBPTAmountIn) { (, amountsOut, maxBPTAmountIn) = abi.decode(self, (BaseWeightedPool.ExitKind, uint256[], uint256)); } }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; import "@balancer-labs/v2-solidity-utils/contracts/helpers/WordCodec.sol"; /** * @dev This module provides an interface to store seemingly unrelated pieces of information, in particular used by * pools with a price oracle. * * These pieces of information are all kept together in a single storage slot to reduce the number of storage reads. In * particular, we not only store configuration values (such as the swap fee percentage), but also cache * reduced-precision versions of the total BPT supply and invariant, which lets us not access nor compute these values * when producing oracle updates during a swap. * * Data is stored with the following structure: * * [ swap fee pct | oracle enabled | oracle index | oracle sample initial timestamp | log supply | log invariant ] * [ uint64 | bool | uint10 | uint31 | int22 | int22 ] * * Note that we are not using the most-significant 106 bits. */ library WeightedPool2TokensMiscData { using WordCodec for bytes32; using WordCodec for uint256; uint256 private constant _LOG_INVARIANT_OFFSET = 0; uint256 private constant _LOG_TOTAL_SUPPLY_OFFSET = 22; uint256 private constant _ORACLE_SAMPLE_CREATION_TIMESTAMP_OFFSET = 44; uint256 private constant _ORACLE_INDEX_OFFSET = 75; uint256 private constant _ORACLE_ENABLED_OFFSET = 85; uint256 private constant _SWAP_FEE_PERCENTAGE_OFFSET = 86; /** * @dev Returns the cached logarithm of the invariant. */ function logInvariant(bytes32 data) internal pure returns (int256) { return data.decodeInt22(_LOG_INVARIANT_OFFSET); } /** * @dev Returns the cached logarithm of the total supply. */ function logTotalSupply(bytes32 data) internal pure returns (int256) { return data.decodeInt22(_LOG_TOTAL_SUPPLY_OFFSET); } /** * @dev Returns the timestamp of the creation of the oracle's latest sample. */ function oracleSampleCreationTimestamp(bytes32 data) internal pure returns (uint256) { return data.decodeUint31(_ORACLE_SAMPLE_CREATION_TIMESTAMP_OFFSET); } /** * @dev Returns the index of the oracle's latest sample. */ function oracleIndex(bytes32 data) internal pure returns (uint256) { return data.decodeUint10(_ORACLE_INDEX_OFFSET); } /** * @dev Returns true if the oracle is enabled. */ function oracleEnabled(bytes32 data) internal pure returns (bool) { return data.decodeBool(_ORACLE_ENABLED_OFFSET); } /** * @dev Returns the swap fee percentage. */ function swapFeePercentage(bytes32 data) internal pure returns (uint256) { return data.decodeUint64(_SWAP_FEE_PERCENTAGE_OFFSET); } /** * @dev Sets the logarithm of the invariant in `data`, returning the updated value. */ function setLogInvariant(bytes32 data, int256 _logInvariant) internal pure returns (bytes32) { return data.insertInt22(_logInvariant, _LOG_INVARIANT_OFFSET); } /** * @dev Sets the logarithm of the total supply in `data`, returning the updated value. */ function setLogTotalSupply(bytes32 data, int256 _logTotalSupply) internal pure returns (bytes32) { return data.insertInt22(_logTotalSupply, _LOG_TOTAL_SUPPLY_OFFSET); } /** * @dev Sets the timestamp of the creation of the oracle's latest sample in `data`, returning the updated value. */ function setOracleSampleCreationTimestamp(bytes32 data, uint256 _initialTimestamp) internal pure returns (bytes32) { return data.insertUint31(_initialTimestamp, _ORACLE_SAMPLE_CREATION_TIMESTAMP_OFFSET); } /** * @dev Sets the index of the oracle's latest sample in `data`, returning the updated value. */ function setOracleIndex(bytes32 data, uint256 _oracleIndex) internal pure returns (bytes32) { return data.insertUint10(_oracleIndex, _ORACLE_INDEX_OFFSET); } /** * @dev Enables or disables the oracle in `data`, returning the updated value. */ function setOracleEnabled(bytes32 data, bool _oracleEnabled) internal pure returns (bytes32) { return data.insertBool(_oracleEnabled, _ORACLE_ENABLED_OFFSET); } /** * @dev Sets the swap fee percentage in `data`, returning the updated value. */ function setSwapFeePercentage(bytes32 data, uint256 _swapFeePercentage) internal pure returns (bytes32) { return data.insertUint64(_swapFeePercentage, _SWAP_FEE_PERCENTAGE_OFFSET); } }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; interface IRateProvider { function getRate() external view returns (uint256); }
// SPDX-License-Identifier: LicenseRef-Gyro-1.0 // for information on licensing please see the README in the GitHub repository <https://github.com/gyrostable/concentrated-lps>. pragma solidity 0.7.6; library GyroConfigKeys { bytes32 public constant PROTOCOL_SWAP_FEE_PERC_KEY = "PROTOCOL_SWAP_FEE_PERC"; bytes32 public constant PROTOCOL_FEE_GYRO_PORTION_KEY = "PROTOCOL_FEE_GYRO_PORTION"; bytes32 public constant GYRO_TREASURY_KEY = "GYRO_TREASURY"; bytes32 public constant BAL_TREASURY_KEY = "BAL_TREASURY"; }
// SPDX-License-Identifier: UNLICENSED pragma solidity 0.7.6; import "../interfaces/IGyroConfig.sol"; import "./GyroConfigKeys.sol"; library GyroConfigHelpers { function getSwapFeePercForPool( IGyroConfig gyroConfig, address poolAddress, bytes32 poolType ) internal view returns (uint256) { return _getPoolSetting(gyroConfig, GyroConfigKeys.PROTOCOL_SWAP_FEE_PERC_KEY, poolType, poolAddress); } function getProtocolFeeGyroPortionForPool( IGyroConfig gyroConfig, address poolAddress, bytes32 poolType ) internal view returns (uint256) { return _getPoolSetting(gyroConfig, GyroConfigKeys.PROTOCOL_FEE_GYRO_PORTION_KEY, poolType, poolAddress); } function _getPoolSetting( IGyroConfig gyroConfig, bytes32 globalKey, bytes32 poolType, address poolAddress ) internal view returns (uint256) { bytes32 poolSpecificKey = keccak256(abi.encode(globalKey, poolAddress)); // Fetch the key. To do this we first check for a pool-specific fee, // and if not present, use the pool-type key. // Failing that we fall back to the global setting. if (gyroConfig.hasKey(poolSpecificKey)) { return gyroConfig.getUint(poolSpecificKey); } bytes32 poolTypeKey = keccak256(abi.encodePacked(globalKey, poolType)); if (gyroConfig.hasKey(poolTypeKey)) { return gyroConfig.getUint(poolTypeKey); } return gyroConfig.getUint(globalKey); } }
// SPDX-License-Identifier: LicenseRef-Gyro-1.0 // for information on licensing please see the README in the GitHub repository <https://github.com/gyrostable/concentrated-lps>. pragma solidity ^0.7.0; /** * @dev Reverts if `condition` is false, with a revert reason containing `errorCode`. Only codes up to 999 are * supported. * Uses the default 'BAL' prefix for the error code */ function _grequire(bool condition, uint256 errorCode) pure { if (!condition) _grevert(errorCode); } /** * @dev Reverts if `condition` is false, with a revert reason containing `errorCode`. Only codes up to 999 are * supported. */ function _grequire( bool condition, uint256 errorCode, bytes3 prefix ) pure { if (!condition) _grevert(errorCode, prefix); } /** * @dev Reverts with a revert reason containing `errorCode`. Only codes up to 999 are supported. * Uses the default 'BAL' prefix for the error code */ function _grevert(uint256 errorCode) pure { _grevert(errorCode, 0x475952); // This is the raw byte representation of "GYR" } /** * @dev Reverts with a revert reason containing `errorCode`. Only codes up to 999 are supported. */ function _grevert(uint256 errorCode, bytes3 prefix) pure { uint256 prefixUint = uint256(uint24(prefix)); // We're going to dynamically create a revert string based on the error code, with the following format: // 'BAL#{errorCode}' // where the code is left-padded with zeroes to three digits (so they range from 000 to 999). // // We don't have revert strings embedded in the contract to save bytecode size: it takes much less space to store a // number (8 to 16 bits) than the individual string characters. // // The dynamic string creation algorithm that follows could be implemented in Solidity, but assembly allows for a // much denser implementation, again saving bytecode size. Given this function unconditionally reverts, this is a // safe place to rely on it without worrying about how its usage might affect e.g. memory contents. assembly { // First, we need to compute the ASCII representation of the error code. We assume that it is in the 0-999 // range, so we only need to convert three digits. To convert the digits to ASCII, we add 0x30, the value for // the '0' character. let units := add(mod(errorCode, 10), 0x30) errorCode := div(errorCode, 10) let tenths := add(mod(errorCode, 10), 0x30) errorCode := div(errorCode, 10) let hundreds := add(mod(errorCode, 10), 0x30) // With the individual characters, we can now construct the full string. // We first append the '#' character (0x23) to the prefix. In the case of 'BAL', it results in 0x42414c23 ('BAL#') // Then, we shift this by 24 (to provide space for the 3 bytes of the error code), and add the // characters to it, each shifted by a multiple of 8. // The revert reason is then shifted left by 200 bits (256 minus the length of the string, 7 characters * 8 bits // per character = 56) to locate it in the most significant part of the 256 slot (the beginning of a byte // array). let formattedPrefix := shl(24, add(0x23, shl(8, prefixUint))) let revertReason := shl(200, add(formattedPrefix, add(add(units, shl(8, tenths)), shl(16, hundreds)))) // We can now encode the reason in memory, which can be safely overwritten as we're about to revert. The encoded // message will have the following layout: // [ revert reason identifier ] [ string location offset ] [ string length ] [ string contents ] // The Solidity revert reason identifier is 0x08c739a0, the function selector of the Error(string) function. We // also write zeroes to the next 28 bytes of memory, but those are about to be overwritten. mstore(0x0, 0x08c379a000000000000000000000000000000000000000000000000000000000) // Next is the offset to the location of the string, which will be placed immediately after (20 bytes away). mstore(0x04, 0x0000000000000000000000000000000000000000000000000000000000000020) // The string length is fixed: 7 characters. mstore(0x24, 7) // Finally, the string itself is stored. mstore(0x44, revertReason) // Even if the string is only 7 bytes long, we need to return a full 32 byte slot containing it. The length of // the encoded message is therefore 4 + 32 + 32 + 32 = 100. revert(0, 100) } } library GyroErrors { uint256 internal constant ZERO_ADDRESS = 105; }
// SPDX-License-Identifier: LicenseRef-Gyro-1.0 // for information on licensing please see the README in the GitHub repository <https://github.com/gyrostable/concentrated-lps>. pragma solidity 0.7.6; /// @notice IGyroConfig stores the global configuration of the Gyroscope protocol interface IGyroConfig { /// @notice Event emitted every time a configuration is changed event ConfigChanged(bytes32 key, uint256 previousValue, uint256 newValue); event ConfigChanged(bytes32 key, address previousValue, address newValue); /// @notice Returns a set of known configuration keys function listKeys() external view returns (bytes32[] memory); /// @notice Returns a uint256 value from the config function getUint(bytes32 key) external view returns (uint256); /// @notice Returns an address value from the config function getAddress(bytes32 key) external view returns (address); /// @notice Set a uint256 config /// NOTE: We avoid overloading to avoid complications with some clients function setUint(bytes32 key, uint256 newValue) external; /// @notice Check whether a key exists function hasKey(bytes32 key) external view returns (bool); /// @notice Set an address config function setAddress(bytes32 key, address newValue) external; }
// SPDX-License-Identifier: LicenseRef-Gyro-1.0 // for information on licensing please see the README in the GitHub repository <https://github.com/gyrostable/concentrated-lps>. pragma solidity 0.7.6; // import "@balancer-labs/v2-solidity-utils/contracts/math/FixedPoint.sol"; import "./GyroFixedPoint.sol"; import "@balancer-labs/v2-solidity-utils/contracts/math/Math.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/InputHelpers.sol"; library GyroPoolMath { using GyroFixedPoint for uint256; uint256 private constant SQRT_1E_NEG_1 = 316227766016837933; uint256 private constant SQRT_1E_NEG_3 = 31622776601683793; uint256 private constant SQRT_1E_NEG_5 = 3162277660168379; uint256 private constant SQRT_1E_NEG_7 = 316227766016837; uint256 private constant SQRT_1E_NEG_9 = 31622776601683; uint256 private constant SQRT_1E_NEG_11 = 3162277660168; uint256 private constant SQRT_1E_NEG_13 = 316227766016; uint256 private constant SQRT_1E_NEG_15 = 31622776601; uint256 private constant SQRT_1E_NEG_17 = 3162277660; // Note: this function is identical to that in WeightedMath.sol audited by Balancer function _calcAllTokensInGivenExactBptOut( uint256[] memory balances, uint256 bptOut, uint256 totalBPT ) internal pure returns (uint256[] memory amountsIn) { /************************************************************************************ // tokensInForExactBptOut // // / bptOut \ // // amountsIn[i] = balances[i] * | ------------ | // // \ totalBPT / // ************************************************************************************/ // We adjust the order of operations to minimize error amplification, assuming that // balances[i], totalBPT > 1 (which is usually the case). // Tokens in, so we round up overall. amountsIn = new uint256[](balances.length); for (uint256 i = 0; i < balances.length; i++) { amountsIn[i] = balances[i].mulUp(bptOut).divUp(totalBPT); } return amountsIn; } // Note: this function is identical to that in WeightedMath.sol audited by Balancer function _calcTokensOutGivenExactBptIn( uint256[] memory balances, uint256 bptIn, uint256 totalBPT ) internal pure returns (uint256[] memory amountsOut) { /********************************************************************************************** // exactBPTInForTokensOut // // (per token) // // / bptIn \ // // amountsOut[i] = balances[i] * | --------------------- | // // \ totalBPT / // **********************************************************************************************/ // We adjust the order of operations to minimize error amplification, assuming that // balances[i], totalBPT > 1 (which is usually the case). // Since we're computing an amount out, we round down overall. This means rounding down on both the // multiplication and division. amountsOut = new uint256[](balances.length); for (uint256 i = 0; i < balances.length; i++) { amountsOut[i] = balances[i].mulDown(bptIn).divDown(totalBPT); } return amountsOut; } /** @dev Calculates protocol fees due to Gyro and Balancer * Note: we do this differently than normal Balancer pools by paying fees in BPT tokens * b/c this is much more gas efficient than doing many transfers of underlying assets * This function gets protocol fee parameters from GyroConfig */ function _calcProtocolFees( uint256 previousInvariant, uint256 currentInvariant, uint256 currentBptSupply, uint256 protocolSwapFeePerc, uint256 protocolFeeGyroPortion ) internal pure returns (uint256, uint256) { /********************************************************************************* /* Protocol fee collection should decrease the invariant L by * Delta L = protocolSwapFeePerc * (currentInvariant - previousInvariant) * To take these fees in BPT LP shares, the protocol mints Delta S new LP shares where * Delta S = S * Delta L / ( currentInvariant - Delta L ) * where S = current BPT supply * The protocol then splits the fees (in BPT) considering protocolFeeGyroPortion * See also the write-up, Proposition 7. *********************************************************************************/ if (currentInvariant <= previousInvariant) { // This shouldn't happen outside of rounding errors, but have this safeguard nonetheless to prevent the Pool // from entering a locked state in which joins and exits revert while computing accumulated swap fees. // NB: This condition is also used by the pools to indicate that _lastInvariant is invalid and should be ignored. return (0, 0); } // Calculate due protocol fees in BPT terms // We round down to prevent issues in the Pool's accounting, even if it means paying slightly less in protocol // fees to the Vault. // For the numerator, we need to round down delta L. Also for the denominator b/c subtracted // Ordering multiplications for best fixed point precision considering that S and currentInvariant-previousInvariant could be large uint256 numerator = (currentBptSupply.mulDown(currentInvariant.sub(previousInvariant))).mulDown(protocolSwapFeePerc); uint256 diffInvariant = protocolSwapFeePerc.mulDown(currentInvariant.sub(previousInvariant)); uint256 denominator = currentInvariant.sub(diffInvariant); uint256 deltaS = numerator.divDown(denominator); // Split fees between Gyro and Balancer uint256 gyroFees = protocolFeeGyroPortion.mulDown(deltaS); uint256 balancerFees = deltaS.sub(gyroFees); return (gyroFees, balancerFees); } /** @dev Implements square root algorithm using Newton's method and a first-guess optimisation **/ function _sqrt(uint256 input, uint256 tolerance) internal pure returns (uint256) { if (input == 0) { return 0; } uint256 guess = _makeInitialGuess(input); // 7 iterations guess = (guess + ((input * GyroFixedPoint.ONE) / guess)) / 2; guess = (guess + ((input * GyroFixedPoint.ONE) / guess)) / 2; guess = (guess + ((input * GyroFixedPoint.ONE) / guess)) / 2; guess = (guess + ((input * GyroFixedPoint.ONE) / guess)) / 2; guess = (guess + ((input * GyroFixedPoint.ONE) / guess)) / 2; guess = (guess + ((input * GyroFixedPoint.ONE) / guess)) / 2; guess = (guess + ((input * GyroFixedPoint.ONE) / guess)) / 2; // Check in some epsilon range // Check square is more or less correct uint256 guessSquared = guess.mulDown(guess); require(guessSquared <= input.add(guess.mulUp(tolerance)) && guessSquared >= input.sub(guess.mulUp(tolerance)), "_sqrt FAILED"); return guess; } // function _makeInitialGuess10(uint256 input) internal pure returns (uint256) { // uint256 orderUpperBound = 72; // uint256 orderLowerBound = 0; // uint256 orderMiddle; // orderMiddle = (orderUpperBound + orderLowerBound) / 2; // while (orderUpperBound - orderLowerBound != 1) { // if (10**orderMiddle > input) { // orderUpperBound = orderMiddle; // } else { // orderLowerBound = orderMiddle; // } // } // return 10**(orderUpperBound / 2); // } function _makeInitialGuess(uint256 input) internal pure returns (uint256) { if (input >= GyroFixedPoint.ONE) { return (1 << (_intLog2Halved(input / GyroFixedPoint.ONE))) * GyroFixedPoint.ONE; } else { if (input <= 10) { return SQRT_1E_NEG_17; } if (input <= 1e2) { return 1e10; } if (input <= 1e3) { return SQRT_1E_NEG_15; } if (input <= 1e4) { return 1e11; } if (input <= 1e5) { return SQRT_1E_NEG_13; } if (input <= 1e6) { return 1e12; } if (input <= 1e7) { return SQRT_1E_NEG_11; } if (input <= 1e8) { return 1e13; } if (input <= 1e9) { return SQRT_1E_NEG_9; } if (input <= 1e10) { return 1e14; } if (input <= 1e11) { return SQRT_1E_NEG_7; } if (input <= 1e12) { return 1e15; } if (input <= 1e13) { return SQRT_1E_NEG_5; } if (input <= 1e14) { return 1e16; } if (input <= 1e15) { return SQRT_1E_NEG_3; } if (input <= 1e16) { return 1e17; } if (input <= 1e17) { return SQRT_1E_NEG_1; } return input; } } function _intLog2Halved(uint256 x) public pure returns (uint256 n) { if (x >= 1 << 128) { x >>= 128; n += 64; } if (x >= 1 << 64) { x >>= 64; n += 32; } if (x >= 1 << 32) { x >>= 32; n += 16; } if (x >= 1 << 16) { x >>= 16; n += 8; } if (x >= 1 << 8) { x >>= 8; n += 4; } if (x >= 1 << 4) { x >>= 4; n += 2; } if (x >= 1 << 2) { x >>= 2; n += 1; } } /** @dev If liquidity update is proportional so that price stays the same ("balanced liquidity update"), then this * returns the invariant after that change. This is more efficient than calling `calculateInvariant()` on the updated balances. * `isIncreaseLiq` denotes the sign of the update. See the writeup, Corollary 3 in Section 3.1.3. */ function liquidityInvariantUpdate( uint256 uinvariant, uint256 changeBptSupply, uint256 currentBptSupply, bool isIncreaseLiq ) internal pure returns (uint256 unewInvariant) { // change in invariant if (isIncreaseLiq) { // round new invariant up so that protocol fees not triggered uint256 dL = uinvariant.mulUp(changeBptSupply).divUp(currentBptSupply); unewInvariant = uinvariant.add(dL); } else { // round new invariant up (and so round dL down) so that protocol fees not triggered uint256 dL = uinvariant.mulDown(changeBptSupply).divDown(currentBptSupply); unewInvariant = uinvariant.sub(dL); } } /** @dev If `deltaBalances` are such that, when changing `balances` by it, the price stays the same ("balanced * liquidity update"), then this returns the invariant after that change. This is more efficient than calling * `calculateInvariant()` on the updated balances. `isIncreaseLiq` denotes the sign of the update. * See the writeup, Corollary 3 in Section 3.1.3. * * DEPRECATED and will go out of use and be removed once pending changes to the ECLP are merged. Use the other liquidityInvariantUpdate() function instead! */ function liquidityInvariantUpdate( uint256[] memory balances, uint256 uinvariant, uint256[] memory deltaBalances, bool isIncreaseLiq ) internal pure returns (uint256 unewInvariant) { uint256 largestBalanceIndex; uint256 largestBalance; for (uint256 i = 0; i < balances.length; i++) { if (balances[i] > largestBalance) { largestBalance = balances[i]; largestBalanceIndex = i; } } uint256 deltaInvariant = uinvariant.mulDown(deltaBalances[largestBalanceIndex]).divDown(balances[largestBalanceIndex]); unewInvariant = isIncreaseLiq ? uinvariant.add(deltaInvariant) : uinvariant.sub(deltaInvariant); } }
// SPDX-License-Identifier: LicenseRef-Gyro-1.0 // for information on licensing please see the README in the GitHub repository <https://github.com/gyrostable/concentrated-lps>. pragma solidity 0.7.6; pragma experimental ABIEncoderV2; // import "@balancer-labs/v2-solidity-utils/contracts/math/FixedPoint.sol"; import "../libraries/GyroFixedPoint.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/InputHelpers.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/LogCompression.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/TemporarilyPausable.sol"; import "@balancer-labs/v2-solidity-utils/contracts/openzeppelin/ERC20.sol"; import "@balancer-labs/v2-vault/contracts/interfaces/IMinimalSwapInfoPool.sol"; import "@balancer-labs/v2-pool-utils/contracts/BasePool.sol"; import "@balancer-labs/v2-pool-utils/contracts/BasePoolAuthorization.sol"; import "@balancer-labs/v2-pool-utils/contracts/BalancerPoolToken.sol"; import "@balancer-labs/v2-pool-weighted/contracts/WeightedMath.sol"; import "@balancer-labs/v2-pool-weighted/contracts/WeightedPoolUserDataHelpers.sol"; import "@balancer-labs/v2-pool-weighted/contracts/WeightedPool2TokensMiscData.sol"; /** @dev Extensible version (i.e., methods can be overriden) of the WeightedPool2Tokens. */ abstract contract ExtensibleWeightedPool2Tokens is IMinimalSwapInfoPool, BasePoolAuthorization, BalancerPoolToken, TemporarilyPausable { using GyroFixedPoint for uint256; using WeightedPoolUserDataHelpers for bytes; using WeightedPool2TokensMiscData for bytes32; uint256 private constant _MINIMUM_BPT = 1e6; // 1e18 corresponds to 1.0, or a 100% fee uint256 private constant _MIN_SWAP_FEE_PERCENTAGE = 1e12; // 0.0001% uint256 private constant _MAX_SWAP_FEE_PERCENTAGE = 1e17; // 10% // The swap fee is internally stored using 64 bits, which is enough to represent _MAX_SWAP_FEE_PERCENTAGE. bytes32 internal _miscData; uint256 internal _lastInvariant; bytes32 private immutable _poolId; IERC20 internal immutable _token0; IERC20 internal immutable _token1; uint256 private immutable _normalizedWeight0 = 5e17; uint256 private immutable _normalizedWeight1 = 5e17; // The protocol fees will always be charged using the token associated with the max weight in the pool. // Since these Pools will register tokens only once, we can assume this index will be constant. uint256 internal immutable _maxWeightTokenIndex = 0; // All token balances are normalized to behave as if the token had 18 decimals. We assume a token's decimals will // not change throughout its lifetime, and store the corresponding scaling factor for each at construction time. // These factors are always greater than or equal to one: tokens with more than 18 decimals are not supported. // We still store these as 18-decimal (GyroFixedPoint) values for composability. uint256 internal immutable _scalingFactor0; uint256 internal immutable _scalingFactor1; event SwapFeePercentageChanged(uint256 swapFeePercentage); modifier onlyVault(bytes32 poolId) { _require(msg.sender == address(getVault()), Errors.CALLER_NOT_VAULT); _require(poolId == getPoolId(), Errors.INVALID_POOL_ID); _; } struct NewPoolParams { IVault vault; string name; string symbol; IERC20 token0; IERC20 token1; uint256 swapFeePercentage; uint256 pauseWindowDuration; uint256 bufferPeriodDuration; address owner; } constructor(NewPoolParams memory params) // Base Pools are expected to be deployed using factories. By using the factory address as the action // disambiguator, we make all Pools deployed by the same factory share action identifiers. This allows for // simpler management of permissions (such as being able to manage granting the 'set fee percentage' action in // any Pool created by the same factory), while still making action identifiers unique among different factories // if the selectors match, preventing accidental errors. Authentication(bytes32(uint256(msg.sender))) BalancerPoolToken(params.name, params.symbol, params.vault) BasePoolAuthorization(params.owner) TemporarilyPausable(params.pauseWindowDuration, params.bufferPeriodDuration) { IERC20[] memory tokens = new IERC20[](2); tokens[0] = params.token0; tokens[1] = params.token1; // See BasePool's constructor for why we need this. // This contract is not derived from BasePool, so we need to do this check ourselves. InputHelpers.ensureArrayIsSorted(tokens); _setSwapFeePercentage(params.swapFeePercentage); bytes32 poolId = params.vault.registerPool(IVault.PoolSpecialization.TWO_TOKEN); // Pass in zero addresses for Asset Managers params.vault.registerTokens(poolId, tokens, new address[](2)); // Set immutable state variables - these cannot be read from during construction _poolId = poolId; _token0 = params.token0; _token1 = params.token1; _scalingFactor0 = _computeScalingFactor(params.token0); _scalingFactor1 = _computeScalingFactor(params.token1); } // Getters / Setters function getPoolId() public view override returns (bytes32) { return _poolId; } /** @dev Only `swapFeePercentage` is non-trivial; everything else is 0/false because the oracle is not used. * These variables are returned to keep the call signature compatible. */ function getMiscData() external view returns ( int256 logInvariant, int256 logTotalSupply, uint256 oracleSampleCreationTimestamp, uint256 oracleIndex, bool oracleEnabled, uint256 swapFeePercentage ) { bytes32 miscData = _miscData; logInvariant = miscData.logInvariant(); logTotalSupply = miscData.logTotalSupply(); oracleSampleCreationTimestamp = miscData.oracleSampleCreationTimestamp(); oracleIndex = miscData.oracleIndex(); oracleEnabled = miscData.oracleEnabled(); swapFeePercentage = miscData.swapFeePercentage(); } function getSwapFeePercentage() public view returns (uint256) { return _miscData.swapFeePercentage(); } // Caller must be approved by the Vault's Authorizer function setSwapFeePercentage(uint256 swapFeePercentage) public virtual authenticate whenNotPaused { _setSwapFeePercentage(swapFeePercentage); } function _setSwapFeePercentage(uint256 swapFeePercentage) private { _require(swapFeePercentage >= _MIN_SWAP_FEE_PERCENTAGE, Errors.MIN_SWAP_FEE_PERCENTAGE); _require(swapFeePercentage <= _MAX_SWAP_FEE_PERCENTAGE, Errors.MAX_SWAP_FEE_PERCENTAGE); _miscData = _miscData.setSwapFeePercentage(swapFeePercentage); emit SwapFeePercentageChanged(swapFeePercentage); } function _isOwnerOnlyAction(bytes32 actionId) internal view virtual override returns (bool) { return (actionId == getActionId(BasePool.setSwapFeePercentage.selector)) || (actionId == getActionId(BasePool.setAssetManagerPoolConfig.selector)); } // Caller must be approved by the Vault's Authorizer function setPaused(bool paused) external authenticate { _setPaused(paused); } function getNormalizedWeights() external view returns (uint256[] memory) { return _normalizedWeights(); } function _normalizedWeights() internal view virtual returns (uint256[] memory) { uint256[] memory normalizedWeights = new uint256[](2); normalizedWeights[0] = _normalizedWeights(true); normalizedWeights[1] = _normalizedWeights(false); return normalizedWeights; } function _normalizedWeights(bool token0) internal view virtual returns (uint256) { return token0 ? _normalizedWeight0 : _normalizedWeight1; } function getLastInvariant() external view returns (uint256) { return _lastInvariant; } /** * @dev Returns the current value of the invariant. */ function getInvariant() public view virtual returns (uint256) { (, uint256[] memory balances, ) = getVault().getPoolTokens(getPoolId()); // Since the Pool hooks always work with upscaled balances, we manually // upscale here for consistency _upscaleArray(balances); uint256[] memory normalizedWeights = _normalizedWeights(); return WeightedMath._calculateInvariant(normalizedWeights, balances); } // Swap Hooks function onSwap( SwapRequest memory request, uint256 balanceTokenIn, uint256 balanceTokenOut ) public virtual override whenNotPaused onlyVault(request.poolId) returns (uint256) { bool tokenInIsToken0 = request.tokenIn == _token0; uint256 scalingFactorTokenIn = _scalingFactor(tokenInIsToken0); uint256 scalingFactorTokenOut = _scalingFactor(!tokenInIsToken0); uint256 normalizedWeightIn = _normalizedWeights(tokenInIsToken0); uint256 normalizedWeightOut = _normalizedWeights(!tokenInIsToken0); // All token amounts are upscaled. balanceTokenIn = _upscale(balanceTokenIn, scalingFactorTokenIn); balanceTokenOut = _upscale(balanceTokenOut, scalingFactorTokenOut); if (request.kind == IVault.SwapKind.GIVEN_IN) { // Fees are subtracted before scaling, to reduce the complexity of the rounding direction analysis. // This is amount - fee amount, so we round up (favoring a higher fee amount). uint256 feeAmount = request.amount.mulUp(getSwapFeePercentage()); request.amount = _upscale(request.amount.sub(feeAmount), scalingFactorTokenIn); uint256 amountOut = _onSwapGivenIn(request, balanceTokenIn, balanceTokenOut, normalizedWeightIn, normalizedWeightOut); // amountOut tokens are exiting the Pool, so we round down. return _downscaleDown(amountOut, scalingFactorTokenOut); } else { request.amount = _upscale(request.amount, scalingFactorTokenOut); uint256 amountIn = _onSwapGivenOut(request, balanceTokenIn, balanceTokenOut, normalizedWeightIn, normalizedWeightOut); // amountIn tokens are entering the Pool, so we round up. amountIn = _downscaleUp(amountIn, scalingFactorTokenIn); // Fees are added after scaling happens, to reduce the complexity of the rounding direction analysis. // This is amount + fee amount, so we round up (favoring a higher fee amount). return amountIn.divUp(getSwapFeePercentage().complement()); } } function _onSwapGivenIn( SwapRequest memory swapRequest, uint256 currentBalanceTokenIn, uint256 currentBalanceTokenOut, uint256 normalizedWeightIn, uint256 normalizedWeightOut ) internal pure virtual returns (uint256) { // Swaps are disabled while the contract is paused. return WeightedMath._calcOutGivenIn(currentBalanceTokenIn, normalizedWeightIn, currentBalanceTokenOut, normalizedWeightOut, swapRequest.amount); } function _onSwapGivenOut( SwapRequest memory swapRequest, uint256 currentBalanceTokenIn, uint256 currentBalanceTokenOut, uint256 normalizedWeightIn, uint256 normalizedWeightOut ) internal pure virtual returns (uint256) { // Swaps are disabled while the contract is paused. return WeightedMath._calcInGivenOut(currentBalanceTokenIn, normalizedWeightIn, currentBalanceTokenOut, normalizedWeightOut, swapRequest.amount); } // Join Hook function onJoinPool( bytes32 poolId, address sender, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, bytes memory userData ) public virtual override onlyVault(poolId) whenNotPaused returns (uint256[] memory amountsIn, uint256[] memory dueProtocolFeeAmounts) { // All joins, including initializations, are disabled while the contract is paused. uint256 bptAmountOut; if (totalSupply() == 0) { (bptAmountOut, amountsIn) = _onInitializePool(poolId, sender, recipient, userData); // On initialization, we lock _MINIMUM_BPT by minting it for the zero address. This BPT acts as a minimum // as it will never be burned, which reduces potential issues with rounding, and also prevents the Pool from // ever being fully drained. _require(bptAmountOut >= _MINIMUM_BPT, Errors.MINIMUM_BPT); _mintPoolTokens(address(0), _MINIMUM_BPT); _mintPoolTokens(recipient, bptAmountOut - _MINIMUM_BPT); // amountsIn are amounts entering the Pool, so we round up. _downscaleUpArray(amountsIn); // There are no due protocol fee amounts during initialization dueProtocolFeeAmounts = new uint256[](2); } else { _upscaleArray(balances); (bptAmountOut, amountsIn, dueProtocolFeeAmounts) = _onJoinPool( poolId, sender, recipient, balances, lastChangeBlock, protocolSwapFeePercentage, userData ); // Note we no longer use `balances` after calling `_onJoinPool`, which may mutate it. _mintPoolTokens(recipient, bptAmountOut); // amountsIn are amounts entering the Pool, so we round up. _downscaleUpArray(amountsIn); // dueProtocolFeeAmounts are amounts exiting the Pool, so we round down. _downscaleDownArray(dueProtocolFeeAmounts); } } /** * @dev Called when the Pool is joined for the first time; that is, when the BPT total supply is zero. * * Returns the amount of BPT to mint, and the token amounts the Pool will receive in return. * * Minted BPT will be sent to `recipient`, except for _MINIMUM_BPT, which will be deducted from this amount and sent * to the zero address instead. This will cause that BPT to remain forever locked there, preventing total BTP from * ever dropping below that value, and ensuring `_onInitializePool` can only be called once in the entire Pool's * lifetime. * * The tokens granted to the Pool will be transferred from `sender`. These amounts are considered upscaled and will * be downscaled (rounding up) before being returned to the Vault. */ function _onInitializePool( bytes32, address, address, bytes memory userData ) internal virtual returns (uint256, uint256[] memory) { BaseWeightedPool.JoinKind kind = userData.joinKind(); _require(kind == BaseWeightedPool.JoinKind.INIT, Errors.UNINITIALIZED); uint256[] memory amountsIn = userData.initialAmountsIn(); InputHelpers.ensureInputLengthMatch(amountsIn.length, 2); _upscaleArray(amountsIn); uint256[] memory normalizedWeights = _normalizedWeights(); uint256 invariantAfterJoin = WeightedMath._calculateInvariant(normalizedWeights, amountsIn); // Set the initial BPT to the value of the invariant times the number of tokens. This makes BPT supply more // consistent in Pools with similar compositions but different number of tokens. uint256 bptAmountOut = Math.mul(invariantAfterJoin, 2); _lastInvariant = invariantAfterJoin; return (bptAmountOut, amountsIn); } /** * @dev Called whenever the Pool is joined after the first initialization join (see `_onInitializePool`). * * Returns the amount of BPT to mint, the token amounts that the Pool will receive in return, and the number of * tokens to pay in protocol swap fees. * * Implementations of this function might choose to mutate the `balances` array to save gas (e.g. when * performing intermediate calculations, such as subtraction of due protocol fees). This can be done safely. * * Minted BPT will be sent to `recipient`. * * The tokens granted to the Pool will be transferred from `sender`. These amounts are considered upscaled and will * be downscaled (rounding up) before being returned to the Vault. * * Due protocol swap fees will be taken from the Pool's balance in the Vault (see `IBasePool.onJoinPool`). These * amounts are considered upscaled and will be downscaled (rounding down) before being returned to the Vault. */ function _onJoinPool( bytes32, address, address, uint256[] memory balances, uint256, uint256 protocolSwapFeePercentage, bytes memory userData ) internal virtual returns ( uint256, uint256[] memory, uint256[] memory ) { uint256[] memory normalizedWeights = _normalizedWeights(); // Due protocol swap fee amounts are computed by measuring the growth of the invariant between the previous join // or exit event and now - the invariant's growth is due exclusively to swap fees. This avoids spending gas // computing them on each individual swap uint256 invariantBeforeJoin = WeightedMath._calculateInvariant(normalizedWeights, balances); uint256[] memory dueProtocolFeeAmounts = _getDueProtocolFeeAmounts( balances, normalizedWeights, _lastInvariant, invariantBeforeJoin, protocolSwapFeePercentage ); // Update current balances by subtracting the protocol fee amounts _mutateAmounts(balances, dueProtocolFeeAmounts, GyroFixedPoint.sub); (uint256 bptAmountOut, uint256[] memory amountsIn) = _doJoin(balances, normalizedWeights, userData); // Update the invariant with the balances the Pool will have after the join, in order to compute the // protocol swap fee amounts due in future joins and exits. _mutateAmounts(balances, amountsIn, GyroFixedPoint.add); _lastInvariant = WeightedMath._calculateInvariant(normalizedWeights, balances); return (bptAmountOut, amountsIn, dueProtocolFeeAmounts); } function _doJoin( uint256[] memory balances, uint256[] memory normalizedWeights, bytes memory userData ) internal view virtual returns (uint256, uint256[] memory) { BaseWeightedPool.JoinKind kind = userData.joinKind(); if (kind == BaseWeightedPool.JoinKind.EXACT_TOKENS_IN_FOR_BPT_OUT) { return _joinExactTokensInForBPTOut(balances, normalizedWeights, userData); } else if (kind == BaseWeightedPool.JoinKind.TOKEN_IN_FOR_EXACT_BPT_OUT) { return _joinTokenInForExactBPTOut(balances, normalizedWeights, userData); } else if (kind == BaseWeightedPool.JoinKind.ALL_TOKENS_IN_FOR_EXACT_BPT_OUT) { return _joinAllTokensInForExactBPTOut(balances, userData); } else { _revert(Errors.UNHANDLED_JOIN_KIND); } } function _joinExactTokensInForBPTOut( uint256[] memory balances, uint256[] memory normalizedWeights, bytes memory userData ) private view returns (uint256, uint256[] memory) { (uint256[] memory amountsIn, uint256 minBPTAmountOut) = userData.exactTokensInForBptOut(); InputHelpers.ensureInputLengthMatch(amountsIn.length, 2); _upscaleArray(amountsIn); (uint256 bptAmountOut, ) = WeightedMath._calcBptOutGivenExactTokensIn( balances, normalizedWeights, amountsIn, totalSupply(), getSwapFeePercentage() ); _require(bptAmountOut >= minBPTAmountOut, Errors.BPT_OUT_MIN_AMOUNT); return (bptAmountOut, amountsIn); } function _joinTokenInForExactBPTOut( uint256[] memory balances, uint256[] memory normalizedWeights, bytes memory userData ) private view returns (uint256, uint256[] memory) { (uint256 bptAmountOut, uint256 tokenIndex) = userData.tokenInForExactBptOut(); // Note that there is no maximum amountIn parameter: this is handled by `IVault.joinPool`. _require(tokenIndex < 2, Errors.OUT_OF_BOUNDS); uint256[] memory amountsIn = new uint256[](2); (amountsIn[tokenIndex], ) = WeightedMath._calcTokenInGivenExactBptOut( balances[tokenIndex], normalizedWeights[tokenIndex], bptAmountOut, totalSupply(), getSwapFeePercentage() ); return (bptAmountOut, amountsIn); } function _joinAllTokensInForExactBPTOut(uint256[] memory balances, bytes memory userData) internal view virtual returns (uint256, uint256[] memory) { uint256 bptAmountOut = userData.allTokensInForExactBptOut(); // Note that there is no maximum amountsIn parameter: this is handled by `IVault.joinPool`. uint256[] memory amountsIn = WeightedMath._calcAllTokensInGivenExactBptOut(balances, bptAmountOut, totalSupply()); return (bptAmountOut, amountsIn); } // Exit Hook function onExitPool( bytes32 poolId, address sender, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, bytes memory userData ) public virtual override onlyVault(poolId) returns (uint256[] memory, uint256[] memory) { _upscaleArray(balances); (uint256 bptAmountIn, uint256[] memory amountsOut, uint256[] memory dueProtocolFeeAmounts) = _onExitPool( poolId, sender, recipient, balances, lastChangeBlock, protocolSwapFeePercentage, userData ); // Note we no longer use `balances` after calling `_onExitPool`, which may mutate it. _burnPoolTokens(sender, bptAmountIn); // Both amountsOut and dueProtocolFeeAmounts are amounts exiting the Pool, so we round down. _downscaleDownArray(amountsOut); _downscaleDownArray(dueProtocolFeeAmounts); return (amountsOut, dueProtocolFeeAmounts); } /** * @dev Called whenever the Pool is exited. * * Returns the amount of BPT to burn, the token amounts for each Pool token that the Pool will grant in return, and * the number of tokens to pay in protocol swap fees. * * Implementations of this function might choose to mutate the `balances` array to save gas (e.g. when * performing intermediate calculations, such as subtraction of due protocol fees). This can be done safely. * * BPT will be burnt from `sender`. * * The Pool will grant tokens to `recipient`. These amounts are considered upscaled and will be downscaled * (rounding down) before being returned to the Vault. * * Due protocol swap fees will be taken from the Pool's balance in the Vault (see `IBasePool.onExitPool`). These * amounts are considered upscaled and will be downscaled (rounding down) before being returned to the Vault. */ function _onExitPool( bytes32, address, address, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, bytes memory userData ) internal virtual returns ( uint256 bptAmountIn, uint256[] memory amountsOut, uint256[] memory dueProtocolFeeAmounts ) { // Exits are not completely disabled while the contract is paused: proportional exits (exact BPT in for tokens // out) remain functional. uint256[] memory normalizedWeights = _normalizedWeights(); if (_isNotPaused()) { // Due protocol swap fee amounts are computed by measuring the growth of the invariant between the previous // join or exit event and now - the invariant's growth is due exclusively to swap fees. This avoids // spending gas calculating the fees on each individual swap. uint256 invariantBeforeExit = WeightedMath._calculateInvariant(normalizedWeights, balances); dueProtocolFeeAmounts = _getDueProtocolFeeAmounts( balances, normalizedWeights, _lastInvariant, invariantBeforeExit, protocolSwapFeePercentage ); // Update current balances by subtracting the protocol fee amounts _mutateAmounts(balances, dueProtocolFeeAmounts, GyroFixedPoint.sub); } else { // If the contract is paused, swap protocol fee amounts are not charged // to avoid extra calculations and reduce the potential for errors. dueProtocolFeeAmounts = new uint256[](2); } (bptAmountIn, amountsOut) = _doExit(balances, normalizedWeights, userData); // Update the invariant with the balances the Pool will have after the exit, in order to compute the // protocol swap fees due in future joins and exits. _mutateAmounts(balances, amountsOut, GyroFixedPoint.sub); _lastInvariant = WeightedMath._calculateInvariant(normalizedWeights, balances); return (bptAmountIn, amountsOut, dueProtocolFeeAmounts); } function _doExit( uint256[] memory balances, uint256[] memory normalizedWeights, bytes memory userData ) internal view virtual returns (uint256, uint256[] memory) { BaseWeightedPool.ExitKind kind = userData.exitKind(); if (kind == BaseWeightedPool.ExitKind.EXACT_BPT_IN_FOR_ONE_TOKEN_OUT) { return _exitExactBPTInForTokenOut(balances, normalizedWeights, userData); } else if (kind == BaseWeightedPool.ExitKind.EXACT_BPT_IN_FOR_TOKENS_OUT) { return _exitExactBPTInForTokensOut(balances, userData); } else if (kind == BaseWeightedPool.ExitKind.BPT_IN_FOR_EXACT_TOKENS_OUT) { return _exitBPTInForExactTokensOut(balances, normalizedWeights, userData); } else { _revert(Errors.UNHANDLED_EXIT_KIND); } } function _exitExactBPTInForTokenOut( uint256[] memory balances, uint256[] memory normalizedWeights, bytes memory userData ) private view whenNotPaused returns (uint256, uint256[] memory) { // This exit function is disabled if the contract is paused. (uint256 bptAmountIn, uint256 tokenIndex) = userData.exactBptInForTokenOut(); // Note that there is no minimum amountOut parameter: this is handled by `IVault.exitPool`. _require(tokenIndex < 2, Errors.OUT_OF_BOUNDS); // We exit in a single token, so we initialize amountsOut with zeros uint256[] memory amountsOut = new uint256[](2); // And then assign the result to the selected token (amountsOut[tokenIndex], ) = WeightedMath._calcTokenOutGivenExactBptIn( balances[tokenIndex], normalizedWeights[tokenIndex], bptAmountIn, totalSupply(), getSwapFeePercentage() ); return (bptAmountIn, amountsOut); } function _exitExactBPTInForTokensOut(uint256[] memory balances, bytes memory userData) internal view virtual returns (uint256, uint256[] memory) { // This exit function is the only one that is not disabled if the contract is paused: it remains unrestricted // in an attempt to provide users with a mechanism to retrieve their tokens in case of an emergency. // This particular exit function is the only one that remains available because it is the simplest one, and // therefore the one with the lowest likelihood of errors. uint256 bptAmountIn = userData.exactBptInForTokensOut(); // Note that there is no minimum amountOut parameter: this is handled by `IVault.exitPool`. uint256[] memory amountsOut = WeightedMath._calcTokensOutGivenExactBptIn(balances, bptAmountIn, totalSupply()); return (bptAmountIn, amountsOut); } function _exitBPTInForExactTokensOut( uint256[] memory balances, uint256[] memory normalizedWeights, bytes memory userData ) private view whenNotPaused returns (uint256, uint256[] memory) { // This exit function is disabled if the contract is paused. (uint256[] memory amountsOut, uint256 maxBPTAmountIn) = userData.bptInForExactTokensOut(); InputHelpers.ensureInputLengthMatch(amountsOut.length, 2); _upscaleArray(amountsOut); (uint256 bptAmountIn, ) = WeightedMath._calcBptInGivenExactTokensOut( balances, normalizedWeights, amountsOut, totalSupply(), getSwapFeePercentage() ); _require(bptAmountIn <= maxBPTAmountIn, Errors.BPT_IN_MAX_AMOUNT); return (bptAmountIn, amountsOut); } /** @dev Applies the reverse of the internal scaling rate to the relative spot price. */ function _adjustPrice(uint256 spotPrice) internal view virtual returns (uint256) { return spotPrice; } // Query functions /** * @dev Returns the amount of BPT that would be granted to `recipient` if the `onJoinPool` hook were called by the * Vault with the same arguments, along with the number of tokens `sender` would have to supply. * * This function is not meant to be called directly, but rather from a helper contract that fetches current Vault * data, such as the protocol swap fee percentage and Pool balances. * * Like `IVault.queryBatchSwap`, this function is not view due to internal implementation details: the caller must * explicitly use eth_call instead of eth_sendTransaction. */ function queryJoin( bytes32 poolId, address sender, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, bytes memory userData ) external returns (uint256 bptOut, uint256[] memory amountsIn) { InputHelpers.ensureInputLengthMatch(balances.length, 2); _queryAction(poolId, sender, recipient, balances, lastChangeBlock, protocolSwapFeePercentage, userData, _onJoinPool, _downscaleUpArray); // The `return` opcode is executed directly inside `_queryAction`, so execution never reaches this statement, // and we don't need to return anything here - it just silences compiler warnings. return (bptOut, amountsIn); } /** * @dev Returns the amount of BPT that would be burned from `sender` if the `onExitPool` hook were called by the * Vault with the same arguments, along with the number of tokens `recipient` would receive. * * This function is not meant to be called directly, but rather from a helper contract that fetches current Vault * data, such as the protocol swap fee percentage and Pool balances. * * Like `IVault.queryBatchSwap`, this function is not view due to internal implementation details: the caller must * explicitly use eth_call instead of eth_sendTransaction. */ function queryExit( bytes32 poolId, address sender, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, bytes memory userData ) external returns (uint256 bptIn, uint256[] memory amountsOut) { InputHelpers.ensureInputLengthMatch(balances.length, 2); _queryAction(poolId, sender, recipient, balances, lastChangeBlock, protocolSwapFeePercentage, userData, _onExitPool, _downscaleDownArray); // The `return` opcode is executed directly inside `_queryAction`, so execution never reaches this statement, // and we don't need to return anything here - it just silences compiler warnings. return (bptIn, amountsOut); } // Helpers function _getDueProtocolFeeAmounts( uint256[] memory balances, uint256[] memory normalizedWeights, uint256 previousInvariant, uint256 currentInvariant, uint256 protocolSwapFeePercentage ) internal view virtual returns (uint256[] memory) { // Initialize with zeros uint256[] memory dueProtocolFeeAmounts = new uint256[](2); // Early return if the protocol swap fee percentage is zero, saving gas. if (protocolSwapFeePercentage == 0) { return dueProtocolFeeAmounts; } // The protocol swap fees are always paid using the token with the largest weight in the Pool. As this is the // token that is expected to have the largest balance, using it to pay fees should not unbalance the Pool. dueProtocolFeeAmounts[_maxWeightTokenIndex] = WeightedMath._calcDueTokenProtocolSwapFeeAmount( balances[_maxWeightTokenIndex], normalizedWeights[_maxWeightTokenIndex], previousInvariant, currentInvariant, protocolSwapFeePercentage ); return dueProtocolFeeAmounts; } /** * @dev Mutates `amounts` by applying `mutation` with each entry in `arguments`. * * Equivalent to `amounts = amounts.map(mutation)`. */ function _mutateAmounts( uint256[] memory toMutate, uint256[] memory arguments, function(uint256, uint256) pure returns (uint256) mutation ) internal pure { toMutate[0] = mutation(toMutate[0], arguments[0]); toMutate[1] = mutation(toMutate[1], arguments[1]); } /** * @dev This function returns the appreciation of one BPT relative to the * underlying tokens. This starts at 1 when the pool is created and grows over time */ function getRate() public view returns (uint256) { // The initial BPT supply is equal to the invariant times the number of tokens. return Math.mul(getInvariant(), 2).divDown(totalSupply()); } // Scaling /** * @dev Returns a scaling factor that, when multiplied to a token amount for `token`, normalizes its balance as if * it had 18 decimals. The scaling factor itself is 18-decimal FixedPoint, so needs to be multiplied via * `mulDown()` or `mulUp()`, not "*". */ function _computeScalingFactor(IERC20 token) private view returns (uint256) { // Tokens that don't implement the `decimals` method are not supported. uint256 tokenDecimals = ERC20(address(token)).decimals(); // Tokens with more than 18 decimals are not supported. uint256 decimalsDifference = Math.sub(18, tokenDecimals); return 10**decimalsDifference * GyroFixedPoint.ONE; } /** * @dev Returns the scaling factor for one of the Pool's tokens. Reverts if `token` is not a token registered by the * Pool. The scaling factor is an 18-decimal FixedPoint number. */ function _scalingFactor(bool token0) internal view virtual returns (uint256) { return token0 ? _scalingFactor0 : _scalingFactor1; } /** * @dev Applies `scalingFactor` to `amount`, resulting in a larger or equal value depending on whether it needed * scaling or not. */ function _upscale(uint256 amount, uint256 scalingFactor) internal pure returns (uint256) { return GyroFixedPoint.mulDown(amount, scalingFactor); } /** * @dev Same as `_upscale`, but for an entire array (of two elements). This function does not return anything, but * instead *mutates* the `amounts` array. */ function _upscaleArray(uint256[] memory amounts) internal view { amounts[0] = GyroFixedPoint.mulDown(amounts[0], _scalingFactor(true)); amounts[1] = GyroFixedPoint.mulDown(amounts[1], _scalingFactor(false)); } /** * @dev Reverses the `scalingFactor` applied to `amount`, resulting in a smaller or equal value depending on * whether it needed scaling or not. The result is rounded down. */ function _downscaleDown(uint256 amount, uint256 scalingFactor) internal pure returns (uint256) { return GyroFixedPoint.divDown(amount, scalingFactor); } /** * @dev Same as `_downscaleDown`, but for an entire array (of two elements). This function does not return anything, * but instead *mutates* the `amounts` array. */ function _downscaleDownArray(uint256[] memory amounts) internal view { amounts[0] = GyroFixedPoint.divDown(amounts[0], _scalingFactor(true)); amounts[1] = GyroFixedPoint.divDown(amounts[1], _scalingFactor(false)); } /** * @dev Reverses the `scalingFactor` applied to `amount`, resulting in a smaller or equal value depending on * whether it needed scaling or not. The result is rounded up. */ function _downscaleUp(uint256 amount, uint256 scalingFactor) internal pure returns (uint256) { return GyroFixedPoint.divUp(amount, scalingFactor); } /** * @dev Same as `_downscaleUp`, but for an entire array (of two elements). This function does not return anything, * but instead *mutates* the `amounts` array. */ function _downscaleUpArray(uint256[] memory amounts) internal view { amounts[0] = GyroFixedPoint.divUp(amounts[0], _scalingFactor(true)); amounts[1] = GyroFixedPoint.divUp(amounts[1], _scalingFactor(false)); } function _getAuthorizer() internal view override returns (IAuthorizer) { // Access control management is delegated to the Vault's Authorizer. This lets Balancer Governance manage which // accounts can call permissioned functions: for example, to perform emergency pauses. // If the owner is delegated, then *all* permissioned functions, including `setSwapFeePercentage`, will be under // Governance control. return getVault().getAuthorizer(); } function _queryAction( bytes32 poolId, address sender, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, bytes memory userData, function(bytes32, address, address, uint256[] memory, uint256, uint256, bytes memory) internal returns (uint256, uint256[] memory, uint256[] memory) _action, function(uint256[] memory) internal view _downscaleArray ) private { // This uses the same technique used by the Vault in queryBatchSwap. Refer to that function for a detailed // explanation. if (msg.sender != address(this)) { // We perform an external call to ourselves, forwarding the same calldata. In this call, the else clause of // the preceding if statement will be executed instead. // solhint-disable-next-line avoid-low-level-calls (bool success, ) = address(this).call(msg.data); // solhint-disable-next-line no-inline-assembly assembly { // This call should always revert to decode the bpt and token amounts from the revert reason switch success case 0 { // Note we are manually writing the memory slot 0. We can safely overwrite whatever is // stored there as we take full control of the execution and then immediately return. // We copy the first 4 bytes to check if it matches with the expected signature, otherwise // there was another revert reason and we should forward it. returndatacopy(0, 0, 0x04) let error := and(mload(0), 0xffffffff00000000000000000000000000000000000000000000000000000000) // If the first 4 bytes don't match with the expected signature, we forward the revert reason. if eq(eq(error, 0x43adbafb00000000000000000000000000000000000000000000000000000000), 0) { returndatacopy(0, 0, returndatasize()) revert(0, returndatasize()) } // The returndata contains the signature, followed by the raw memory representation of the // `bptAmount` and `tokenAmounts` (array: length + data). We need to return an ABI-encoded // representation of these. // An ABI-encoded response will include one additional field to indicate the starting offset of // the `tokenAmounts` array. The `bptAmount` will be laid out in the first word of the // returndata. // // In returndata: // [ signature ][ bptAmount ][ tokenAmounts length ][ tokenAmounts values ] // [ 4 bytes ][ 32 bytes ][ 32 bytes ][ (32 * length) bytes ] // // We now need to return (ABI-encoded values): // [ bptAmount ][ tokeAmounts offset ][ tokenAmounts length ][ tokenAmounts values ] // [ 32 bytes ][ 32 bytes ][ 32 bytes ][ (32 * length) bytes ] // We copy 32 bytes for the `bptAmount` from returndata into memory. // Note that we skip the first 4 bytes for the error signature returndatacopy(0, 0x04, 32) // The offsets are 32-bytes long, so the array of `tokenAmounts` will start after // the initial 64 bytes. mstore(0x20, 64) // We now copy the raw memory array for the `tokenAmounts` from returndata into memory. // Since bpt amount and offset take up 64 bytes, we start copying at address 0x40. We also // skip the first 36 bytes from returndata, which correspond to the signature plus bpt amount. returndatacopy(0x40, 0x24, sub(returndatasize(), 36)) // We finally return the ABI-encoded uint256 and the array, which has a total length equal to // the size of returndata, plus the 32 bytes of the offset but without the 4 bytes of the // error signature. return(0, add(returndatasize(), 28)) } default { // This call should always revert, but we fail nonetheless if that didn't happen invalid() } } } else { _upscaleArray(balances); (uint256 bptAmount, uint256[] memory tokenAmounts, ) = _action( poolId, sender, recipient, balances, lastChangeBlock, protocolSwapFeePercentage, userData ); _downscaleArray(tokenAmounts); // solhint-disable-next-line no-inline-assembly assembly { // We will return a raw representation of `bptAmount` and `tokenAmounts` in memory, which is composed of // a 32-byte uint256, followed by a 32-byte for the array length, and finally the 32-byte uint256 values // Because revert expects a size in bytes, we multiply the array length (stored at `tokenAmounts`) by 32 let size := mul(mload(tokenAmounts), 32) // We store the `bptAmount` in the previous slot to the `tokenAmounts` array. We can make sure there // will be at least one available slot due to how the memory scratch space works. // We can safely overwrite whatever is stored in this slot as we will revert immediately after that. let start := sub(tokenAmounts, 0x20) mstore(start, bptAmount) // We send one extra value for the error signature "QueryError(uint256,uint256[])" which is 0x43adbafb // We use the previous slot to `bptAmount`. mstore(sub(start, 0x20), 0x0000000000000000000000000000000000000000000000000000000043adbafb) start := sub(start, 0x04) // When copying from `tokenAmounts` into returndata, we copy the additional 68 bytes to also return // the `bptAmount`, the array length, and the error signature. revert(start, add(size, 68)) } } } }
// SPDX-License-Identifier: LicenseRef-Gyro-1.0 // for information on licensing please see the README in the GitHub repository <https://github.com/gyrostable/concentrated-lps>. pragma solidity 0.7.6; pragma experimental ABIEncoderV2; // import "@balancer-labs/v2-solidity-utils/contracts/math/FixedPoint.sol"; import "../../libraries/GyroFixedPoint.sol"; import "../../libraries/GyroErrors.sol"; import "../../libraries/SignedFixedPoint.sol"; import "../../libraries/GyroPoolMath.sol"; import "./GyroECLPPoolErrors.sol"; import "@balancer-labs/v2-solidity-utils/contracts/math/Math.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/InputHelpers.sol"; import "@openzeppelin/contracts/utils/SafeCast.sol"; // solhint-disable private-vars-leading-underscore /** @dev ECLP math library. Pretty much a direct translation of the python version (see `tests/`). * We use *signed* values here because some of the intermediate results can be negative (e.g. coordinates of points in * the untransformed circle, "prices" in the untransformed circle). */ library GyroECLPMath { uint256 internal constant ONEHALF = 0.5e18; int256 internal constant ONE = 1e18; // 18 decimal places int256 internal constant ONE_XP = 1e38; // 38 decimal places using SignedFixedPoint for int256; using GyroFixedPoint for uint256; using SafeCast for uint256; using SafeCast for int256; // Anti-overflow limits: Params and DerivedParams (static, only needs to be checked on pool creation) int256 internal constant _ROTATION_VECTOR_NORM_ACCURACY = 1e3; // 1e-15 in normal precision int256 internal constant _MAX_STRETCH_FACTOR = 1e26; // 1e8 in normal precision int256 internal constant _DERIVED_TAU_NORM_ACCURACY_XP = 1e23; // 1e-15 in extra precision int256 internal constant _MAX_INV_INVARIANT_DENOMINATOR_XP = 1e43; // 1e5 in extra precision int256 internal constant _DERIVED_DSQ_NORM_ACCURACY_XP = 1e23; // 1e-15 in extra precision // Anti-overflow limits: Dynamic values (checked before operations that use them) int256 internal constant _MAX_BALANCES = 1e34; // 1e16 in normal precision int256 internal constant _MAX_INVARIANT = 3e37; // 3e19 in normal precision // Note that all t values (not tp or tpp) could consist of uint's, as could all Params. But it's complicated to // convert all the time, so we make them all signed. We also store all intermediate values signed. An exception are // the functions that are used by the contract b/c there the values are stored unsigned. struct Params { // Price bounds (lower and upper). 0 < alpha < beta int256 alpha; int256 beta; // Rotation vector: // phi in (-90 degrees, 0] is the implicit rotation vector. It's stored as a point: int256 c; // c = cos(-phi) >= 0. rounded to 18 decimals int256 s; // s = sin(-phi) >= 0. rounded to 18 decimals // Invariant: c^2 + s^2 == 1, i.e., the point (c, s) is normalized. // due to rounding, this may not = 1. The term dSq in DerivedParams corrects for this in extra precision // Stretching factor: int256 lambda; // lambda >= 1 where lambda == 1 is the circle. } // terms in this struct are stored in extra precision (38 decimals) with final decimal rounded down struct DerivedParams { Vector2 tauAlpha; Vector2 tauBeta; int256 u; // from (A chi)_y = lambda * u + v int256 v; // from (A chi)_y = lambda * u + v int256 w; // from (A chi)_x = w / lambda + z int256 z; // from (A chi)_x = w / lambda + z int256 dSq; // error in c^2 + s^2 = dSq, used to correct errors in c, s, tau, u,v,w,z calculations //int256 dAlpha; // normalization constant for tau(alpha) //int256 dBeta; // normalization constant for tau(beta) } struct Vector2 { int256 x; int256 y; } struct QParams { int256 a; int256 b; int256 c; } /** @dev Enforces limits and approximate normalization of the rotation vector. */ function validateParams(Params memory params) internal pure { _grequire(0 <= params.s && params.s <= ONE, GyroECLPPoolErrors.ROTATION_VECTOR_WRONG); _grequire(0 <= params.c && params.c <= ONE, GyroECLPPoolErrors.ROTATION_VECTOR_WRONG); Vector2 memory sc = Vector2(params.s, params.c); int256 scnorm2 = scalarProd(sc, sc); // squared norm _grequire( ONE - _ROTATION_VECTOR_NORM_ACCURACY <= scnorm2 && scnorm2 <= ONE + _ROTATION_VECTOR_NORM_ACCURACY, GyroECLPPoolErrors.ROTATION_VECTOR_NOT_NORMALIZED ); _grequire(0 <= params.lambda && params.lambda <= _MAX_STRETCH_FACTOR, GyroECLPPoolErrors.STRETCHING_FACTOR_WRONG); } /** @dev Enforces limits and approximate normalization of the derived values. Does NOT check for internal consistency of 'derived' with 'params'. */ function validateDerivedParamsLimits(Params memory params, DerivedParams memory derived) external pure { int256 norm2; norm2 = scalarProdXp(derived.tauAlpha, derived.tauAlpha); _grequire( ONE_XP - _DERIVED_TAU_NORM_ACCURACY_XP <= norm2 && norm2 <= ONE_XP + _DERIVED_TAU_NORM_ACCURACY_XP, GyroECLPPoolErrors.DERIVED_TAU_NOT_NORMALIZED ); norm2 = scalarProdXp(derived.tauBeta, derived.tauBeta); _grequire( ONE_XP - _DERIVED_TAU_NORM_ACCURACY_XP <= norm2 && norm2 <= ONE_XP + _DERIVED_TAU_NORM_ACCURACY_XP, GyroECLPPoolErrors.DERIVED_TAU_NOT_NORMALIZED ); _grequire(derived.u <= ONE_XP, GyroECLPPoolErrors.DERIVED_UVWZ_WRONG); _grequire(derived.v <= ONE_XP, GyroECLPPoolErrors.DERIVED_UVWZ_WRONG); _grequire(derived.w <= ONE_XP, GyroECLPPoolErrors.DERIVED_UVWZ_WRONG); _grequire(derived.z <= ONE_XP, GyroECLPPoolErrors.DERIVED_UVWZ_WRONG); _grequire( ONE_XP - _DERIVED_DSQ_NORM_ACCURACY_XP <= derived.dSq && derived.dSq <= ONE_XP + _DERIVED_DSQ_NORM_ACCURACY_XP, GyroECLPPoolErrors.DERIVED_DSQ_WRONG ); // NB No anti-overflow checks are required given the checks done above and in validateParams(). int256 mulDenominator = ONE_XP.divXpU(calcAChiAChiInXp(params, derived) - ONE_XP); _grequire(mulDenominator <= _MAX_INV_INVARIANT_DENOMINATOR_XP, GyroECLPPoolErrors.INVARIANT_DENOMINATOR_WRONG); } function scalarProd(Vector2 memory t1, Vector2 memory t2) internal pure returns (int256 ret) { ret = t1.x.mulDownMag(t2.x).add(t1.y.mulDownMag(t2.y)); } /// @dev scalar product for extra-precision values function scalarProdXp(Vector2 memory t1, Vector2 memory t2) internal pure returns (int256 ret) { ret = t1.x.mulXp(t2.x).add(t1.y.mulXp(t2.y)); } // "Methods" for Params. We could put these into a separate library and import them via 'using' to get method call // syntax. /** @dev Calculate A t where A is given in Section 2.2 * This is reversing rotation and scaling of the ellipse (mapping back to circle) */ function mulA(Params memory params, Vector2 memory tp) internal pure returns (Vector2 memory t) { // NB: This function is only used inside calculatePrice(). This is why we can make two simplifications: // 1. We don't correct for precision of s, c using d.dSq because that level of precision is not important in this context. // 2. We don't need to check for over/underflow b/c these are impossible in that context and given the (checked) assumptions on the various values. t.x = params.c.mulDownMagU(tp.x).divDownMagU(params.lambda) - params.s.mulDownMagU(tp.y).divDownMagU(params.lambda); t.y = params.s.mulDownMagU(tp.x) + params.c.mulDownMagU(tp.y); } /** @dev Calculate virtual offset a given invariant r. * See calculation in Section 2.1.2 Computing reserve offsets * Note that, in contrast to virtual reserve offsets in CPMM, these are *subtracted* from the real * reserves, moving the curve to the upper-right. They can be positive or negative, but not both can be negative. * Calculates a = r*(A^{-1}tau(beta))_x rounding up in signed direction * Notice that error in r is scaled by lambda, and so rounding direction is important */ function virtualOffset0( Params memory p, DerivedParams memory d, Vector2 memory r // overestimate in x component, underestimate in y ) internal pure returns (int256 a) { // a = r lambda c tau(beta)_x + rs tau(beta)_y // account for 1 factors of dSq (2 s,c factors) int256 termXp = d.tauBeta.x.divXpU(d.dSq); a = d.tauBeta.x > 0 ? r.x.mulUpMagU(p.lambda).mulUpMagU(p.c).mulUpXpToNpU(termXp) : r.y.mulDownMagU(p.lambda).mulDownMagU(p.c).mulUpXpToNpU(termXp); // use fact that tau(beta)_y > 0, so the required rounding direction is clear. a = a + r.x.mulUpMagU(p.s).mulUpXpToNpU(d.tauBeta.y.divXpU(d.dSq)); } /** @dev calculate virtual offset b given invariant r. * Calculates b = r*(A^{-1}tau(alpha))_y rounding up in signed direction */ function virtualOffset1( Params memory p, DerivedParams memory d, Vector2 memory r // overestimate in x component, underestimate in y ) internal pure returns (int256 b) { // b = -r \lambda s tau(alpha)_x + rc tau(alpha)_y // account for 1 factors of dSq (2 s,c factors) int256 termXp = d.tauAlpha.x.divXpU(d.dSq); b = (d.tauAlpha.x < 0) ? r.x.mulUpMagU(p.lambda).mulUpMagU(p.s).mulUpXpToNpU(-termXp) : (-r.y).mulDownMagU(p.lambda).mulDownMagU(p.s).mulUpXpToNpU(termXp); // use fact that tau(alpha)_y > 0, so the required rounding direction is clear. b = b + r.x.mulUpMagU(p.c).mulUpXpToNpU(d.tauAlpha.y.divXpU(d.dSq)); } /** Maximal value for the real reserves x when the respective other balance is 0 for given invariant * See calculation in Section 2.1.2. Calculation is ordered here for precision, but error in r is magnified by lambda * Rounds down in signed direction */ function maxBalances0( Params memory p, DerivedParams memory d, Vector2 memory r // overestimate in x-component, underestimate in y-component ) internal pure returns (int256 xp) { // x^+ = r lambda c (tau(beta)_x - tau(alpha)_x) + rs (tau(beta)_y - tau(alpha)_y) // account for 1 factors of dSq (2 s,c factors) int256 termXp1 = (d.tauBeta.x - d.tauAlpha.x).divXpU(d.dSq); // note tauBeta.x > tauAlpha.x, so this is > 0 and rounding direction is clear int256 termXp2 = (d.tauBeta.y - d.tauAlpha.y).divXpU(d.dSq); // note this may be negative, but since tauBeta.y, tauAlpha.y >= 0, it is always in [-1, 1]. xp = r.y.mulDownMagU(p.lambda).mulDownMagU(p.c).mulDownXpToNpU(termXp1); xp = xp + (termXp2 > 0 ? r.y.mulDownMagU(p.s) : r.x.mulUpMagU(p.s)).mulDownXpToNpU(termXp2); } /** Maximal value for the real reserves y when the respective other balance is 0 for given invariant * See calculation in Section 2.1.2. Calculation is ordered here for precision, but erorr in r is magnified by lambda * Rounds down in signed direction */ function maxBalances1( Params memory p, DerivedParams memory d, Vector2 memory r // overestimate in x-component, underestimate in y-component ) internal pure returns (int256 yp) { // y^+ = r lambda s (tau(beta)_x - tau(alpha)_x) + rc (tau(alpha)_y - tau(beta)_y) // account for 1 factors of dSq (2 s,c factors) int256 termXp1 = (d.tauBeta.x - d.tauAlpha.x).divXpU(d.dSq); // note tauBeta.x > tauAlpha.x int256 termXp2 = (d.tauAlpha.y - d.tauBeta.y).divXpU(d.dSq); yp = r.y.mulDownMagU(p.lambda).mulDownMagU(p.s).mulDownXpToNpU(termXp1); yp = yp + (termXp2 > 0 ? r.y.mulDownMagU(p.c) : r.x.mulUpMagU(p.c)).mulDownXpToNpU(termXp2); } /** @dev Compute the invariant 'r' corresponding to the given values. The invariant can't be negative, but * we use a signed value to store it because all the other calculations are happening with signed ints, too. * Computes r according to Prop 13 in 2.2.1 Initialization from Real Reserves * orders operations to achieve best precision * Returns an underestimate and a bound on error size. * Enforces anti-overflow limits on balances and the computed invariant in the process. */ function calculateInvariantWithError( uint256[] memory balances, Params memory params, DerivedParams memory derived ) public pure returns (int256, int256) { (int256 x, int256 y) = (balances[0].toInt256(), balances[1].toInt256()); _grequire(x.add(y) <= _MAX_BALANCES, GyroECLPPoolErrors.MAX_ASSETS_EXCEEDED); int256 AtAChi = calcAtAChi(x, y, params, derived); (int256 sqrt, int256 err) = calcInvariantSqrt(x, y, params, derived); // calculate the error in the square root term, separates cases based on sqrt >= 1/2 // somedayTODO: can this be improved for cases of large balances (when xp error magnifies to np) // Note: the minimum non-zero value of sqrt is 1e-9 since the minimum argument is 1e-18 if (sqrt > 0) { // err + 1 to account for O(eps_np) term ignored before err = (err + 1).divUpMagU(2 * sqrt); } else { // in the false case here, the extra precision error does not magnify, and so the error inside the sqrt is O(1e-18) // somedayTODO: The true case will almost surely never happen (can it be removed) err = err > 0 ? GyroPoolMath._sqrt(err.toUint256(), 5).toInt256() : 1e9; } // calculate the error in the numerator, scale the error by 20 to be sure all possible terms accounted for err = ((params.lambda.mulUpMagU(x + y) / ONE_XP) + err + 1) * 20; // A chi \cdot A chi > 1, so round it up to round denominator up // denominator uses extra precision, so we do * 1/denominator so we are sure the calculation doesn't overflow int256 mulDenominator = ONE_XP.divXpU(calcAChiAChiInXp(params, derived) - ONE_XP); // NOTE: Anti-overflow limits on mulDenominator are checked on contract creation. // as alternative, could do, but could overflow: invariant = (AtAChi.add(sqrt) - err).divXp(denominator); int256 invariant = (AtAChi + sqrt - err).mulDownXpToNpU(mulDenominator); // error scales if denominator is small // NB: This error calculation computes the error in the expression "numerator / denominator", but in this code // we actually use the formula "numerator * (1 / denominator)" to compute the invariant. This affects this line // and the one below. err = err.mulUpXpToNpU(mulDenominator); // account for relative error due to error in the denominator // error in denominator is O(epsilon) if lambda<1e11, scale up by 10 to be sure we catch it, and add O(eps) // error in denominator is lambda^2 * 2e-37 and scales relative to the result / denominator // Scale by a constant to account for errors in the scaling factor itself and limited compounding. // calculating lambda^2 w/o decimals so that the calculation will never overflow, the lost precision isn't important err = err + ((invariant.mulUpXpToNpU(mulDenominator) * ((params.lambda * params.lambda) / 1e36)) * 40) / ONE_XP + 1; _grequire(invariant.add(err) <= _MAX_INVARIANT, GyroECLPPoolErrors.MAX_INVARIANT_EXCEEDED); return (invariant, err); } function calculateInvariant( uint256[] memory balances, Params memory params, DerivedParams memory derived ) external pure returns (uint256 uinvariant) { (int256 invariant, ) = calculateInvariantWithError(balances, params, derived); uinvariant = invariant.toUint256(); } /// @dev calculate At \cdot A chi, ignores rounding direction. We will later compensate for the rounding error. function calcAtAChi( int256 x, int256 y, Params memory p, DerivedParams memory d ) internal pure returns (int256 val) { // to save gas, pre-compute dSq^2 as it will be used 3 times int256 dSq2 = d.dSq.mulXpU(d.dSq); // (cx - sy) * (w/lambda + z) / lambda // account for 2 factors of dSq (4 s,c factors) int256 termXp = (d.w.divDownMagU(p.lambda) + d.z).divDownMagU(p.lambda).divXpU(dSq2); val = (x.mulDownMagU(p.c) - y.mulDownMagU(p.s)).mulDownXpToNpU(termXp); // (x lambda s + y lambda c) * u, note u > 0 int256 termNp = x.mulDownMagU(p.lambda).mulDownMagU(p.s) + y.mulDownMagU(p.lambda).mulDownMagU(p.c); val = val + termNp.mulDownXpToNpU(d.u.divXpU(dSq2)); // (sx+cy) * v, note v > 0 termNp = x.mulDownMagU(p.s) + y.mulDownMagU(p.c); val = val + termNp.mulDownXpToNpU(d.v.divXpU(dSq2)); } /// @dev calculates A chi \cdot A chi in extra precision /// Note: this can be >1 (and involves factor of lambda^2). We can compute it in extra precision w/o overflowing b/c it will be /// at most 38 + 16 digits (38 from decimals, 2*8 from lambda^2 if lambda=1e8) /// Since we will only divide by this later, we will not need to worry about overflow in that operation if done in the right way /// TODO: is rounding direction ok? function calcAChiAChiInXp(Params memory p, DerivedParams memory d) internal pure returns (int256 val) { // to save gas, pre-compute dSq^3 as it will be used 4 times int256 dSq3 = d.dSq.mulXpU(d.dSq).mulXpU(d.dSq); // (A chi)_y^2 = lambda^2 u^2 + lambda 2 u v + v^2 // account for 3 factors of dSq (6 s,c factors) // SOMEDAY: In these calcs, a calculated value is multiplied by lambda and lambda^2, resp, which implies some // error amplification. It's fine b/c we're doing it in extra precision here, but would still be nice if it // could be avoided, perhaps by splitting up the numbers into a high and low part. val = p.lambda.mulUpMagU((2 * d.u).mulXpU(d.v).divXpU(dSq3)); // for lambda^2 u^2 factor in rounding error in u since lambda could be big // Note: lambda^2 is multiplied at the end to be sure the calculation doesn't overflow, but this can lose some precision val = val + ((d.u + 1).mulXpU(d.u + 1).divXpU(dSq3)).mulUpMagU(p.lambda).mulUpMagU(p.lambda); // the next line converts from extre precision to normal precision post-computation while rounding up val = val + (d.v).mulXpU(d.v).divXpU(dSq3); // (A chi)_x^2 = (w/lambda + z)^2 // account for 3 factors of dSq (6 s,c factors) int256 termXp = d.w.divUpMagU(p.lambda) + d.z; val = val + termXp.mulXpU(termXp).divXpU(dSq3); } /// @dev calculate -(At)_x ^2 (A chi)_y ^2 + (At)_x ^2, rounding down in signed direction function calcMinAtxAChiySqPlusAtxSq( int256 x, int256 y, Params memory p, DerivedParams memory d ) internal pure returns (int256 val) { //////////////////////////////////////////////////////////////////////////////////// // (At)_x^2 (A chi)_y^2 = (x^2 c^2 - xy2sc + y^2 s^2) (u^2 + 2uv/lambda + v^2/lambda^2) // account for 4 factors of dSq (8 s,c factors) // // (At)_x^2 = (x^2 c^2 - xy2sc + y^2 s^2)/lambda^2 // account for 1 factor of dSq (2 s,c factors) //////////////////////////////////////////////////////////////////////////////////// int256 termNp = x.mulUpMagU(x).mulUpMagU(p.c).mulUpMagU(p.c) + y.mulUpMagU(y).mulUpMagU(p.s).mulUpMagU(p.s); termNp = termNp - x.mulDownMagU(y).mulDownMagU(p.c * 2).mulDownMagU(p.s); int256 termXp = d.u.mulXpU(d.u) + (2 * d.u).mulXpU(d.v).divDownMagU(p.lambda) + d.v.mulXpU(d.v).divDownMagU(p.lambda).divDownMagU(p.lambda); termXp = termXp.divXpU(d.dSq.mulXpU(d.dSq).mulXpU(d.dSq).mulXpU(d.dSq)); val = (-termNp).mulDownXpToNpU(termXp); // now calculate (At)_x^2 accounting for possible rounding error to round down // need to do 1/dSq in a way so that there is no overflow for large balances val = val + (termNp - 9).divDownMagU(p.lambda).divDownMagU(p.lambda).mulDownXpToNpU(SignedFixedPoint.ONE_XP.divXpU(d.dSq)); } /// @dev calculate 2(At)_x * (At)_y * (A chi)_x * (A chi)_y, ignores rounding direction // Note: this ignores rounding direction and is corrected for later function calc2AtxAtyAChixAChiy( int256 x, int256 y, Params memory p, DerivedParams memory d ) internal pure returns (int256 val) { //////////////////////////////////////////////////////////////////////////////////// // = ((x^2 - y^2)sc + yx(c^2-s^2)) * 2 * (zu + (wu + zv)/lambda + wv/lambda^2) // account for 4 factors of dSq (8 s,c factors) //////////////////////////////////////////////////////////////////////////////////// int256 termNp = (x.mulDownMagU(x) - y.mulUpMagU(y)).mulDownMagU(2 * p.c).mulDownMagU(p.s); int256 xy = y.mulDownMagU(2 * x); termNp = termNp + xy.mulDownMagU(p.c).mulDownMagU(p.c) - xy.mulDownMagU(p.s).mulDownMagU(p.s); int256 termXp = d.z.mulXpU(d.u) + d.w.mulXpU(d.v).divDownMagU(p.lambda).divDownMagU(p.lambda); termXp = termXp + (d.w.mulXpU(d.u) + d.z.mulXpU(d.v)).divDownMagU(p.lambda); termXp = termXp.divXpU(d.dSq.mulXpU(d.dSq).mulXpU(d.dSq).mulXpU(d.dSq)); val = termNp.mulDownXpToNpU(termXp); } /// @dev calculate -(At)_y ^2 (A chi)_x ^2 + (At)_y ^2, rounding down in signed direction function calcMinAtyAChixSqPlusAtySq( int256 x, int256 y, Params memory p, DerivedParams memory d ) internal pure returns (int256 val) { //////////////////////////////////////////////////////////////////////////////////// // (At)_y^2 (A chi)_x^2 = (x^2 s^2 + xy2sc + y^2 c^2) * (z^2 + 2zw/lambda + w^2/lambda^2) // account for 4 factors of dSq (8 s,c factors) // (At)_y^2 = (x^2 s^2 + xy2sc + y^2 c^2) // account for 1 factor of dSq (2 s,c factors) //////////////////////////////////////////////////////////////////////////////////// int256 termNp = x.mulUpMagU(x).mulUpMagU(p.s).mulUpMagU(p.s) + y.mulUpMagU(y).mulUpMagU(p.c).mulUpMagU(p.c); termNp = termNp + x.mulUpMagU(y).mulUpMagU(p.s * 2).mulUpMagU(p.c); int256 termXp = d.z.mulXpU(d.z) + d.w.mulXpU(d.w).divDownMagU(p.lambda).divDownMagU(p.lambda); termXp = termXp + (2 * d.z).mulXpU(d.w).divDownMagU(p.lambda); termXp = termXp.divXpU(d.dSq.mulXpU(d.dSq).mulXpU(d.dSq).mulXpU(d.dSq)); val = (-termNp).mulDownXpToNpU(termXp); // now calculate (At)_y^2 accounting for possible rounding error to round down // need to do 1/dSq in a way so that there is no overflow for large balances val = val + (termNp - 9).mulDownXpToNpU(SignedFixedPoint.ONE_XP.divXpU(d.dSq)); } /// @dev Rounds down. Also returns an estimate for the error of the term under the sqrt (!) and without the regular /// normal-precision error of O(1e-18). function calcInvariantSqrt( int256 x, int256 y, Params memory p, DerivedParams memory d ) internal pure returns (int256 val, int256 err) { val = calcMinAtxAChiySqPlusAtxSq(x, y, p, d) + calc2AtxAtyAChixAChiy(x, y, p, d); val = val + calcMinAtyAChixSqPlusAtySq(x, y, p, d); // error inside the square root is O((x^2 + y^2) * eps_xp) + O(eps_np), where eps_xp=1e-38, eps_np=1e-18 // note that in terms of rounding down, error corrects for calc2AtxAtyAChixAChiy() // however, we also use this error to correct the invariant for an overestimate in swaps, it is all the same order though // Note the O(eps_np) term will be dealt with later, so not included yet // Note that the extra precision term doesn't propagate unless balances are > 100b err = (x.mulUpMagU(x) + y.mulUpMagU(y)) / 1e38; // we will account for the error later after the square root // mathematically, terms in square root > 0, so treat as 0 if it is < 0 b/c of rounding error val = val > 0 ? GyroPoolMath._sqrt(val.toUint256(), 5).toInt256() : 0; } /** @dev Spot price of token 0 in units of token 1. * See Prop. 12 in 2.1.6 Computing Prices */ function calcSpotPrice0in1( uint256[] memory balances, Params memory params, DerivedParams memory derived, int256 invariant ) external pure returns (uint256 px) { // shift by virtual offsets to get v(t) Vector2 memory r = Vector2(invariant, invariant); // ignore r rounding for spot price, precision will be lost in TWAP anyway Vector2 memory ab = Vector2(virtualOffset0(params, derived, r), virtualOffset1(params, derived, r)); Vector2 memory vec = Vector2(balances[0].toInt256() - ab.x, balances[1].toInt256() - ab.y); // transform to circle to get Av(t) vec = mulA(params, vec); // compute prices on circle Vector2 memory pc = Vector2(vec.x.divDownMagU(vec.y), ONE); // Convert prices back to ellipse // NB: These operations check for overflow because the price pc[0] might be large when vex.y is small. // SOMEDAY I think this probably can't actually happen due to our bounds on the different values. In this case we could do this unchecked as well. int256 pgx = scalarProd(pc, mulA(params, Vector2(ONE, 0))); px = pgx.divDownMag(scalarProd(pc, mulA(params, Vector2(0, ONE)))).toUint256(); } /** @dev Check that post-swap balances obey maximal asset bounds * newBalance = post-swap balance of one asset * assetIndex gives the index of the provided asset (0 = X, 1 = Y) */ function checkAssetBounds( Params memory params, DerivedParams memory derived, Vector2 memory invariant, int256 newBal, uint8 assetIndex ) internal pure { if (assetIndex == 0) { int256 xPlus = maxBalances0(params, derived, invariant); if (!(newBal <= _MAX_BALANCES && newBal <= xPlus)) _grequire(false, GyroECLPPoolErrors.ASSET_BOUNDS_EXCEEDED); return; } { int256 yPlus = maxBalances1(params, derived, invariant); if (!(newBal <= _MAX_BALANCES && newBal <= yPlus)) _grequire(false, GyroECLPPoolErrors.ASSET_BOUNDS_EXCEEDED); } } function calcOutGivenIn( uint256[] memory balances, uint256 amountIn, bool tokenInIsToken0, Params memory params, DerivedParams memory derived, Vector2 memory invariant ) external pure returns (uint256 amountOut) { function(int256, Params memory, DerivedParams memory, Vector2 memory) pure returns (int256) calcGiven; uint8 ixIn; uint8 ixOut; if (tokenInIsToken0) { ixIn = 0; ixOut = 1; calcGiven = calcYGivenX; } else { ixIn = 1; ixOut = 0; calcGiven = calcXGivenY; } int256 balInNew = balances[ixIn].add(amountIn).toInt256(); // checked because amountIn is given by the user. checkAssetBounds(params, derived, invariant, balInNew, ixIn); int256 balOutNew = calcGiven(balInNew, params, derived, invariant); // Make sub checked as an extra check against numerical error; but this really should never happen amountOut = balances[ixOut].sub(balOutNew.toUint256()); // The above line guarantees that amountOut <= balances[ixOut]. } function calcInGivenOut( uint256[] memory balances, uint256 amountOut, bool tokenInIsToken0, Params memory params, DerivedParams memory derived, Vector2 memory invariant ) external pure returns (uint256 amountIn) { function(int256, Params memory, DerivedParams memory, Vector2 memory) pure returns (int256) calcGiven; uint8 ixIn; uint8 ixOut; if (tokenInIsToken0) { ixIn = 0; ixOut = 1; calcGiven = calcXGivenY; // this reverses compared to calcOutGivenIn } else { ixIn = 1; ixOut = 0; calcGiven = calcYGivenX; // this reverses compared to calcOutGivenIn } if (!(amountOut <= balances[ixOut])) _grequire(false, GyroECLPPoolErrors.ASSET_BOUNDS_EXCEEDED); int256 balOutNew = (balances[ixOut] - amountOut).toInt256(); int256 balInNew = calcGiven(balOutNew, params, derived, invariant); // The checks in the following two lines should really always succeed; we keep them as extra safety against numerical error. checkAssetBounds(params, derived, invariant, balInNew, ixIn); amountIn = balInNew.toUint256().sub(balances[ixIn]); } /** @dev Variables are named for calculating y given x * to calculate x given y, change x->y, s->c, c->s, a_>b, b->a, tauBeta.x -> -tauAlpha.x, tauBeta.y -> tauAlpha.y * calculates an overestimate of calculated reserve post-swap */ function solveQuadraticSwap( int256 lambda, int256 x, int256 s, int256 c, Vector2 memory r, // overestimate in x component, underestimate in y Vector2 memory ab, Vector2 memory tauBeta, int256 dSq ) internal pure returns (int256) { // x component will round up, y will round down, use extra precision Vector2 memory lamBar; lamBar.x = SignedFixedPoint.ONE_XP - SignedFixedPoint.ONE_XP.divDownMagU(lambda).divDownMagU(lambda); // Note: The following cannot become negative even with errors because we require lambda >= 1 and // divUpMag returns the exact result if the quotient is representable in 18 decimals. lamBar.y = SignedFixedPoint.ONE_XP - SignedFixedPoint.ONE_XP.divUpMagU(lambda).divUpMagU(lambda); // using qparams struct to avoid "stack too deep" QParams memory q; // shift by the virtual offsets // note that we want an overestimate of offset here so that -x'*lambar*s*c is overestimated in signed direction // account for 1 factor of dSq (2 s,c factors) int256 xp = x - ab.x; if (xp > 0) { q.b = (-xp).mulDownMagU(s).mulDownMagU(c).mulUpXpToNpU(lamBar.y.divXpU(dSq)); } else { q.b = (-xp).mulUpMagU(s).mulUpMagU(c).mulUpXpToNpU(lamBar.x.divXpU(dSq) + 1); } // x component will round up, y will round down, use extra precision // account for 1 factor of dSq (2 s,c factors) Vector2 memory sTerm; // we wil take sTerm = 1 - sTerm below, using multiple lines to avoid "stack too deep" sTerm.x = lamBar.y.mulDownMagU(s).mulDownMagU(s).divXpU(dSq); sTerm.y = lamBar.x.mulUpMagU(s); sTerm.y = sTerm.y.mulUpMagU(s).divXpU(dSq + 1) + 1; // account for rounding error in dSq, divXp sTerm = Vector2(SignedFixedPoint.ONE_XP - sTerm.x, SignedFixedPoint.ONE_XP - sTerm.y); // ^^ NB: The components of sTerm are non-negative: We only need to worry about sTerm.y. This is non-negative b/c, because of bounds on lambda lamBar <= 1 - 1e-16, and division by dSq ensures we have enough precision so that rounding errors are never magnitude 1e-16. // now compute the argument of the square root q.c = -calcXpXpDivLambdaLambda(x, r, lambda, s, c, tauBeta, dSq); q.c = q.c + r.y.mulDownMagU(r.y).mulDownXpToNpU(sTerm.y); // the square root is always being subtracted, so round it down to overestimate the end balance // mathematically, terms in square root > 0, so treat as 0 if it is < 0 b/c of rounding error q.c = q.c > 0 ? GyroPoolMath._sqrt(q.c.toUint256(), 5).toInt256() : 0; // calculate the result in q.a if (q.b - q.c > 0) { q.a = (q.b - q.c).mulUpXpToNpU(SignedFixedPoint.ONE_XP.divXpU(sTerm.y) + 1); } else { q.a = (q.b - q.c).mulUpXpToNpU(SignedFixedPoint.ONE_XP.divXpU(sTerm.x)); } // lastly, add the offset, note that we want an overestimate of offset here return q.a + ab.y; } /** @dev Calculates x'x'/λ^2 where x' = x - b = x - r (A^{-1}tau(beta))_x * calculates an overestimate * to calculate y'y', change x->y, s->c, c->s, tauBeta.x -> -tauAlpha.x, tauBeta.y -> tauAlpha.y */ function calcXpXpDivLambdaLambda( int256 x, Vector2 memory r, // overestimate in x component, underestimate in y int256 lambda, int256 s, int256 c, Vector2 memory tauBeta, int256 dSq ) internal pure returns (int256) { ////////////////////////////////////////////////////////////////////////////////// // x'x'/lambda^2 = r^2 c^2 tau(beta)_x^2 // + ( r^2 2s c tau(beta)_x tau(beta)_y - rx 2c tau(beta)_x ) / lambda // + ( r^2 s^2 tau(beta)_y^2 - rx 2s tau(beta)_y + x^2 ) / lambda^2 ////////////////////////////////////////////////////////////////////////////////// // to save gas, pre-compute dSq^2 as it will be used 3 times, and r.x^2 as it will be used 2-3 times // sqVars = (dSq^2, r.x^2) Vector2 memory sqVars = Vector2(dSq.mulXpU(dSq), r.x.mulUpMagU(r.x)); QParams memory q; // for working terms // q.a = r^2 s 2c tau(beta)_x tau(beta)_y // account for 2 factors of dSq (4 s,c factors) int256 termXp = tauBeta.x.mulXpU(tauBeta.y).divXpU(sqVars.x); if (termXp > 0) { q.a = sqVars.y.mulUpMagU(2 * s); q.a = q.a.mulUpMagU(c).mulUpXpToNpU(termXp + 7); // +7 account for rounding in termXp } else { q.a = r.y.mulDownMagU(r.y).mulDownMagU(2 * s); q.a = q.a.mulDownMagU(c).mulUpXpToNpU(termXp); } // -rx 2c tau(beta)_x // account for 1 factor of dSq (2 s,c factors) if (tauBeta.x < 0) { // +3 account for rounding in extra precision terms q.b = r.x.mulUpMagU(x).mulUpMagU(2 * c).mulUpXpToNpU(-tauBeta.x.divXpU(dSq) + 3); } else { q.b = (-r.y).mulDownMagU(x).mulDownMagU(2 * c).mulUpXpToNpU(tauBeta.x.divXpU(dSq)); } // q.a later needs to be divided by lambda q.a = q.a + q.b; // q.b = r^2 s^2 tau(beta)_y^2 // account for 2 factors of dSq (4 s,c factors) termXp = tauBeta.y.mulXpU(tauBeta.y).divXpU(sqVars.x) + 7; // +7 account for rounding in termXp q.b = sqVars.y.mulUpMagU(s); q.b = q.b.mulUpMagU(s).mulUpXpToNpU(termXp); // q.c = -rx 2s tau(beta)_y, recall that tauBeta.y > 0 so round lower in magnitude // account for 1 factor of dSq (2 s,c factors) q.c = (-r.y).mulDownMagU(x).mulDownMagU(2 * s).mulUpXpToNpU(tauBeta.y.divXpU(dSq)); // (q.b + q.c + x^2) / lambda q.b = q.b + q.c + x.mulUpMagU(x); q.b = q.b > 0 ? q.b.divUpMagU(lambda) : q.b.divDownMagU(lambda); // remaining calculation is (q.a + q.b) / lambda q.a = q.a + q.b; q.a = q.a > 0 ? q.a.divUpMagU(lambda) : q.a.divDownMagU(lambda); // + r^2 c^2 tau(beta)_x^2 // account for 2 factors of dSq (4 s,c factors) termXp = tauBeta.x.mulXpU(tauBeta.x).divXpU(sqVars.x) + 7; // +7 account for rounding in termXp int256 val = sqVars.y.mulUpMagU(c).mulUpMagU(c); return (val.mulUpXpToNpU(termXp)) + q.a; } /** @dev compute y such that (x, y) satisfy the invariant at the given parameters. * Note that we calculate an overestimate of y * See Prop 14 in section 2.2.2 Trade Execution */ function calcYGivenX( int256 x, Params memory params, DerivedParams memory d, Vector2 memory r // overestimate in x component, underestimate in y ) internal pure returns (int256 y) { // want to overestimate the virtual offsets except in a particular setting that will be corrected for later // note that the error correction in the invariant should more than make up for uncaught rounding directions (in 38 decimals) in virtual offsets Vector2 memory ab = Vector2(virtualOffset0(params, d, r), virtualOffset1(params, d, r)); y = solveQuadraticSwap(params.lambda, x, params.s, params.c, r, ab, d.tauBeta, d.dSq); } function calcXGivenY( int256 y, Params memory params, DerivedParams memory d, Vector2 memory r // overestimate in x component, underestimate in y ) internal pure returns (int256 x) { // want to overestimate the virtual offsets except in a particular setting that will be corrected for later // note that the error correction in the invariant should more than make up for uncaught rounding directions (in 38 decimals) in virtual offsets Vector2 memory ba = Vector2(virtualOffset1(params, d, r), virtualOffset0(params, d, r)); // change x->y, s->c, c->s, b->a, a->b, tauBeta.x -> -tauAlpha.x, tauBeta.y -> tauAlpha.y vs calcYGivenX x = solveQuadraticSwap(params.lambda, y, params.c, params.s, r, ba, Vector2(-d.tauAlpha.x, d.tauAlpha.y), d.dSq); } }
// SPDX-License-Identifier: LicenseRef-Gyro-1.0 // for information on licensing please see the README in the GitHub repository <https://github.com/gyrostable/concentrated-lps>. pragma solidity 0.7.6; pragma experimental ABIEncoderV2; // import "@balancer-labs/v2-solidity-utils/contracts/math/FixedPoint.sol"; import "../libraries/GyroFixedPoint.sol"; import "../interfaces/ICappedLiquidity.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/IAuthentication.sol"; /** @dev Enables caps on i) per-LP and ii) total caps on the pool size. Caps are in terms of BPT tokens! Pool functions * have to call _ensureCap() to enforce the cap. */ abstract contract CappedLiquidity is ICappedLiquidity { using GyroFixedPoint for uint256; string internal constant _OVER_GLOBAL_CAP = "over global liquidity cap"; string internal constant _OVER_ADDRESS_CAP = "over address liquidity cap"; string internal constant _NOT_AUTHORIZED = "not authorized"; string internal constant _UNCAPPED = "pool is uncapped"; CapParams internal _capParams; address public override capManager; constructor(address _capManager, CapParams memory params) { require(_capManager != address(0), _NOT_AUTHORIZED); capManager = _capManager; _capParams.capEnabled = params.capEnabled; _capParams.perAddressCap = params.perAddressCap; _capParams.globalCap = params.globalCap; } function setCapManager(address _capManager) external { require(msg.sender == capManager, _NOT_AUTHORIZED); capManager = _capManager; emit CapManagerUpdated(_capManager); } function capParams() external view override returns (CapParams memory) { return _capParams; } function setCapParams(CapParams memory params) external override { require(msg.sender == capManager, _NOT_AUTHORIZED); require(_capParams.capEnabled, _UNCAPPED); _capParams.capEnabled = params.capEnabled; _capParams.perAddressCap = params.perAddressCap; _capParams.globalCap = params.globalCap; emit CapParamsUpdated(_capParams); } function _ensureCap( uint256 amountMinted, uint256 userBalance, uint256 currentSupply ) internal view { CapParams memory params = _capParams; require(amountMinted.add(userBalance) <= params.perAddressCap, _OVER_ADDRESS_CAP); require(amountMinted.add(currentSupply) <= params.globalCap, _OVER_GLOBAL_CAP); } }
// SPDX-License-Identifier: LicenseRef-Gyro-1.0 // for information on licensing please see the README in the GitHub repository <https://github.com/gyrostable/concentrated-lps>. pragma solidity 0.7.6; import "../interfaces/ILocallyPausable.sol"; import "../libraries/GyroErrors.sol"; /** * @notice This contract is used to allow a pool to be paused directly, rather than going through Balancer's * authentication system. */ abstract contract LocallyPausable is ILocallyPausable { address public pauseManager; string internal constant _NOT_PAUSE_MANAGER = "not pause manager"; constructor(address _pauseManager) { _grequire(_pauseManager != address(0), GyroErrors.ZERO_ADDRESS); pauseManager = _pauseManager; } /// @inheritdoc ILocallyPausable function changePauseManager(address _pauseManager) external override { address currentPauseManager = pauseManager; require(currentPauseManager == msg.sender, _NOT_PAUSE_MANAGER); pauseManager = _pauseManager; emit PauseManagerChanged(currentPauseManager, _pauseManager); } /// @inheritdoc ILocallyPausable function pause() external override { require(pauseManager == msg.sender, _NOT_PAUSE_MANAGER); _setPausedState(true); emit PausedLocally(); } /// @inheritdoc ILocallyPausable function unpause() external override { require(pauseManager == msg.sender, _NOT_PAUSE_MANAGER); _setPausedState(false); emit UnpausedLocally(); } function _setPausedState(bool paused) internal virtual; }
// SPDX-License-Identifier: MIT // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated // documentation files (the “Software”), to deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to the following conditions: // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the // Software. // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pragma solidity ^0.7.0; import "../helpers/BalancerErrors.sol"; /* 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). * * @author Fernando Martinelli - @fernandomartinelli * @author Sergio Yuhjtman - @sergioyuhjtman * @author Daniel Fernandez - @dmf7z */ library LogExpMath { // 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. _require(x < 2**255, Errors.X_OUT_OF_BOUNDS); 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. _require(y < MILD_EXPONENT_BOUND, Errors.Y_OUT_OF_BOUNDS); int256 y_int256 = int256(y); int256 logx_times_y; 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 _require( MIN_NATURAL_EXPONENT <= logx_times_y && logx_times_y <= MAX_NATURAL_EXPONENT, Errors.PRODUCT_OUT_OF_BOUNDS ); 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) { _require(x >= MIN_NATURAL_EXPONENT && x <= MAX_NATURAL_EXPONENT, Errors.INVALID_EXPONENT); 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). // Fixed point division requires multiplying by ONE_18. return ((ONE_18 * ONE_18) / exp(-x)); } // 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; 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; 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; 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. return (((product * seriesSum) / ONE_20) * firstAN) / 100; } /** * @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; if (LN_36_LOWER_BOUND < base && base < LN_36_UPPER_BOUND) { logBase = _ln_36(base); } else { logBase = _ln(base) * ONE_18; } int256 logArg; 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. _require(a > 0, Errors.OUT_OF_BOUNDS); if (LN_36_LOWER_BOUND < a && a < LN_36_UPPER_BOUND) { 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) { 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, and this if statement will not be entered in the recursive call. // Fixed point division requires multiplying by ONE_18. return (-_ln((ONE_18 * ONE_18) / a)); } // 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; 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. 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. return (sum + seriesSum) / 100; } /** * @dev Intrnal 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. 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 // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; // solhint-disable /** * @dev Reverts if `condition` is false, with a revert reason containing `errorCode`. Only codes up to 999 are * supported. */ function _require(bool condition, uint256 errorCode) pure { if (!condition) _revert(errorCode); } /** * @dev Reverts with a revert reason containing `errorCode`. Only codes up to 999 are supported. */ function _revert(uint256 errorCode) pure { // We're going to dynamically create a revert string based on the error code, with the following format: // 'BAL#{errorCode}' // where the code is left-padded with zeroes to three digits (so they range from 000 to 999). // // We don't have revert strings embedded in the contract to save bytecode size: it takes much less space to store a // number (8 to 16 bits) than the individual string characters. // // The dynamic string creation algorithm that follows could be implemented in Solidity, but assembly allows for a // much denser implementation, again saving bytecode size. Given this function unconditionally reverts, this is a // safe place to rely on it without worrying about how its usage might affect e.g. memory contents. assembly { // First, we need to compute the ASCII representation of the error code. We assume that it is in the 0-999 // range, so we only need to convert three digits. To convert the digits to ASCII, we add 0x30, the value for // the '0' character. let units := add(mod(errorCode, 10), 0x30) errorCode := div(errorCode, 10) let tenths := add(mod(errorCode, 10), 0x30) errorCode := div(errorCode, 10) let hundreds := add(mod(errorCode, 10), 0x30) // With the individual characters, we can now construct the full string. The "BAL#" part is a known constant // (0x42414c23): we simply shift this by 24 (to provide space for the 3 bytes of the error code), and add the // characters to it, each shifted by a multiple of 8. // The revert reason is then shifted left by 200 bits (256 minus the length of the string, 7 characters * 8 bits // per character = 56) to locate it in the most significant part of the 256 slot (the beginning of a byte // array). let revertReason := shl(200, add(0x42414c23000000, add(add(units, shl(8, tenths)), shl(16, hundreds)))) // We can now encode the reason in memory, which can be safely overwritten as we're about to revert. The encoded // message will have the following layout: // [ revert reason identifier ] [ string location offset ] [ string length ] [ string contents ] // The Solidity revert reason identifier is 0x08c739a0, the function selector of the Error(string) function. We // also write zeroes to the next 28 bytes of memory, but those are about to be overwritten. mstore(0x0, 0x08c379a000000000000000000000000000000000000000000000000000000000) // Next is the offset to the location of the string, which will be placed immediately after (20 bytes away). mstore(0x04, 0x0000000000000000000000000000000000000000000000000000000000000020) // The string length is fixed: 7 characters. mstore(0x24, 7) // Finally, the string itself is stored. mstore(0x44, revertReason) // Even if the string is only 7 bytes long, we need to return a full 32 byte slot containing it. The length of // the encoded message is therefore 4 + 32 + 32 + 32 = 100. revert(0, 100) } } library Errors { // Math uint256 internal constant ADD_OVERFLOW = 0; uint256 internal constant SUB_OVERFLOW = 1; uint256 internal constant SUB_UNDERFLOW = 2; uint256 internal constant MUL_OVERFLOW = 3; uint256 internal constant ZERO_DIVISION = 4; uint256 internal constant DIV_INTERNAL = 5; uint256 internal constant X_OUT_OF_BOUNDS = 6; uint256 internal constant Y_OUT_OF_BOUNDS = 7; uint256 internal constant PRODUCT_OUT_OF_BOUNDS = 8; uint256 internal constant INVALID_EXPONENT = 9; // Input uint256 internal constant OUT_OF_BOUNDS = 100; uint256 internal constant UNSORTED_ARRAY = 101; uint256 internal constant UNSORTED_TOKENS = 102; uint256 internal constant INPUT_LENGTH_MISMATCH = 103; uint256 internal constant ZERO_TOKEN = 104; // Shared pools uint256 internal constant MIN_TOKENS = 200; uint256 internal constant MAX_TOKENS = 201; uint256 internal constant MAX_SWAP_FEE_PERCENTAGE = 202; uint256 internal constant MIN_SWAP_FEE_PERCENTAGE = 203; uint256 internal constant MINIMUM_BPT = 204; uint256 internal constant CALLER_NOT_VAULT = 205; uint256 internal constant UNINITIALIZED = 206; uint256 internal constant BPT_IN_MAX_AMOUNT = 207; uint256 internal constant BPT_OUT_MIN_AMOUNT = 208; uint256 internal constant EXPIRED_PERMIT = 209; uint256 internal constant NOT_TWO_TOKENS = 210; // Pools uint256 internal constant MIN_AMP = 300; uint256 internal constant MAX_AMP = 301; uint256 internal constant MIN_WEIGHT = 302; uint256 internal constant MAX_STABLE_TOKENS = 303; uint256 internal constant MAX_IN_RATIO = 304; uint256 internal constant MAX_OUT_RATIO = 305; uint256 internal constant MIN_BPT_IN_FOR_TOKEN_OUT = 306; uint256 internal constant MAX_OUT_BPT_FOR_TOKEN_IN = 307; uint256 internal constant NORMALIZED_WEIGHT_INVARIANT = 308; uint256 internal constant INVALID_TOKEN = 309; uint256 internal constant UNHANDLED_JOIN_KIND = 310; uint256 internal constant ZERO_INVARIANT = 311; uint256 internal constant ORACLE_INVALID_SECONDS_QUERY = 312; uint256 internal constant ORACLE_NOT_INITIALIZED = 313; uint256 internal constant ORACLE_QUERY_TOO_OLD = 314; uint256 internal constant ORACLE_INVALID_INDEX = 315; uint256 internal constant ORACLE_BAD_SECS = 316; uint256 internal constant AMP_END_TIME_TOO_CLOSE = 317; uint256 internal constant AMP_ONGOING_UPDATE = 318; uint256 internal constant AMP_RATE_TOO_HIGH = 319; uint256 internal constant AMP_NO_ONGOING_UPDATE = 320; uint256 internal constant STABLE_INVARIANT_DIDNT_CONVERGE = 321; uint256 internal constant STABLE_GET_BALANCE_DIDNT_CONVERGE = 322; uint256 internal constant RELAYER_NOT_CONTRACT = 323; uint256 internal constant BASE_POOL_RELAYER_NOT_CALLED = 324; uint256 internal constant REBALANCING_RELAYER_REENTERED = 325; uint256 internal constant GRADUAL_UPDATE_TIME_TRAVEL = 326; uint256 internal constant SWAPS_DISABLED = 327; uint256 internal constant CALLER_IS_NOT_LBP_OWNER = 328; uint256 internal constant PRICE_RATE_OVERFLOW = 329; uint256 internal constant INVALID_JOIN_EXIT_KIND_WHILE_SWAPS_DISABLED = 330; uint256 internal constant WEIGHT_CHANGE_TOO_FAST = 331; uint256 internal constant LOWER_GREATER_THAN_UPPER_TARGET = 332; uint256 internal constant UPPER_TARGET_TOO_HIGH = 333; uint256 internal constant UNHANDLED_BY_LINEAR_POOL = 334; uint256 internal constant OUT_OF_TARGET_RANGE = 335; uint256 internal constant UNHANDLED_EXIT_KIND = 336; uint256 internal constant UNAUTHORIZED_EXIT = 337; uint256 internal constant MAX_MANAGEMENT_SWAP_FEE_PERCENTAGE = 338; uint256 internal constant UNHANDLED_BY_INVESTMENT_POOL = 339; uint256 internal constant UNHANDLED_BY_PHANTOM_POOL = 340; uint256 internal constant TOKEN_DOES_NOT_HAVE_RATE_PROVIDER = 341; uint256 internal constant INVALID_INITIALIZATION = 342; // Lib uint256 internal constant REENTRANCY = 400; uint256 internal constant SENDER_NOT_ALLOWED = 401; uint256 internal constant PAUSED = 402; uint256 internal constant PAUSE_WINDOW_EXPIRED = 403; uint256 internal constant MAX_PAUSE_WINDOW_DURATION = 404; uint256 internal constant MAX_BUFFER_PERIOD_DURATION = 405; uint256 internal constant INSUFFICIENT_BALANCE = 406; uint256 internal constant INSUFFICIENT_ALLOWANCE = 407; uint256 internal constant ERC20_TRANSFER_FROM_ZERO_ADDRESS = 408; uint256 internal constant ERC20_TRANSFER_TO_ZERO_ADDRESS = 409; uint256 internal constant ERC20_MINT_TO_ZERO_ADDRESS = 410; uint256 internal constant ERC20_BURN_FROM_ZERO_ADDRESS = 411; uint256 internal constant ERC20_APPROVE_FROM_ZERO_ADDRESS = 412; uint256 internal constant ERC20_APPROVE_TO_ZERO_ADDRESS = 413; uint256 internal constant ERC20_TRANSFER_EXCEEDS_ALLOWANCE = 414; uint256 internal constant ERC20_DECREASED_ALLOWANCE_BELOW_ZERO = 415; uint256 internal constant ERC20_TRANSFER_EXCEEDS_BALANCE = 416; uint256 internal constant ERC20_BURN_EXCEEDS_ALLOWANCE = 417; uint256 internal constant SAFE_ERC20_CALL_FAILED = 418; uint256 internal constant ADDRESS_INSUFFICIENT_BALANCE = 419; uint256 internal constant ADDRESS_CANNOT_SEND_VALUE = 420; uint256 internal constant SAFE_CAST_VALUE_CANT_FIT_INT256 = 421; uint256 internal constant GRANT_SENDER_NOT_ADMIN = 422; uint256 internal constant REVOKE_SENDER_NOT_ADMIN = 423; uint256 internal constant RENOUNCE_SENDER_NOT_ALLOWED = 424; uint256 internal constant BUFFER_PERIOD_EXPIRED = 425; uint256 internal constant CALLER_IS_NOT_OWNER = 426; uint256 internal constant NEW_OWNER_IS_ZERO = 427; uint256 internal constant CODE_DEPLOYMENT_FAILED = 428; uint256 internal constant CALL_TO_NON_CONTRACT = 429; uint256 internal constant LOW_LEVEL_CALL_FAILED = 430; // Vault uint256 internal constant INVALID_POOL_ID = 500; uint256 internal constant CALLER_NOT_POOL = 501; uint256 internal constant SENDER_NOT_ASSET_MANAGER = 502; uint256 internal constant USER_DOESNT_ALLOW_RELAYER = 503; uint256 internal constant INVALID_SIGNATURE = 504; uint256 internal constant EXIT_BELOW_MIN = 505; uint256 internal constant JOIN_ABOVE_MAX = 506; uint256 internal constant SWAP_LIMIT = 507; uint256 internal constant SWAP_DEADLINE = 508; uint256 internal constant CANNOT_SWAP_SAME_TOKEN = 509; uint256 internal constant UNKNOWN_AMOUNT_IN_FIRST_SWAP = 510; uint256 internal constant MALCONSTRUCTED_MULTIHOP_SWAP = 511; uint256 internal constant INTERNAL_BALANCE_OVERFLOW = 512; uint256 internal constant INSUFFICIENT_INTERNAL_BALANCE = 513; uint256 internal constant INVALID_ETH_INTERNAL_BALANCE = 514; uint256 internal constant INVALID_POST_LOAN_BALANCE = 515; uint256 internal constant INSUFFICIENT_ETH = 516; uint256 internal constant UNALLOCATED_ETH = 517; uint256 internal constant ETH_TRANSFER = 518; uint256 internal constant CANNOT_USE_ETH_SENTINEL = 519; uint256 internal constant TOKENS_MISMATCH = 520; uint256 internal constant TOKEN_NOT_REGISTERED = 521; uint256 internal constant TOKEN_ALREADY_REGISTERED = 522; uint256 internal constant TOKENS_ALREADY_SET = 523; uint256 internal constant TOKENS_LENGTH_MUST_BE_2 = 524; uint256 internal constant NONZERO_TOKEN_BALANCE = 525; uint256 internal constant BALANCE_TOTAL_OVERFLOW = 526; uint256 internal constant POOL_NO_TOKENS = 527; uint256 internal constant INSUFFICIENT_FLASH_LOAN_BALANCE = 528; // Fees uint256 internal constant SWAP_FEE_PERCENTAGE_TOO_HIGH = 600; uint256 internal constant FLASH_LOAN_FEE_PERCENTAGE_TOO_HIGH = 601; uint256 internal constant INSUFFICIENT_FLASH_LOAN_FEE_AMOUNT = 602; }
// SPDX-License-Identifier: MIT pragma solidity ^0.7.0; import "../helpers/BalancerErrors.sol"; /** * @dev Wrappers over Solidity's arithmetic operations with added overflow checks. * Adapted from OpenZeppelin's SafeMath library */ library Math { /** * @dev Returns the addition of two unsigned integers of 256 bits, reverting on overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; _require(c >= a, Errors.ADD_OVERFLOW); return c; } /** * @dev Returns the addition of two signed integers, reverting on overflow. */ function add(int256 a, int256 b) internal pure returns (int256) { int256 c = a + b; _require((b >= 0 && c >= a) || (b < 0 && c < a), Errors.ADD_OVERFLOW); return c; } /** * @dev Returns the subtraction of two unsigned integers of 256 bits, reverting on overflow. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { _require(b <= a, Errors.SUB_OVERFLOW); uint256 c = a - b; return c; } /** * @dev Returns the subtraction of two signed integers, reverting on overflow. */ function sub(int256 a, int256 b) internal pure returns (int256) { int256 c = a - b; _require((b >= 0 && c <= a) || (b < 0 && c > a), Errors.SUB_OVERFLOW); return c; } /** * @dev Returns the largest of two numbers of 256 bits. */ function max(uint256 a, uint256 b) internal pure returns (uint256) { return a >= b ? a : b; } /** * @dev Returns the smallest of two numbers of 256 bits. */ function min(uint256 a, uint256 b) internal pure returns (uint256) { return a < b ? a : b; } function mul(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a * b; _require(a == 0 || c / a == b, Errors.MUL_OVERFLOW); return c; } function div( uint256 a, uint256 b, bool roundUp ) internal pure returns (uint256) { return roundUp ? divUp(a, b) : divDown(a, b); } function divDown(uint256 a, uint256 b) internal pure returns (uint256) { _require(b != 0, Errors.ZERO_DIVISION); return a / b; } function divUp(uint256 a, uint256 b) internal pure returns (uint256) { _require(b != 0, Errors.ZERO_DIVISION); if (a == 0) { return 0; } else { return 1 + (a - 1) / b; } } }
// SPDX-License-Identifier: MIT pragma solidity ^0.7.0; /** * @dev Interface of the ERC20 standard as defined in the EIP. */ interface IERC20 { /** * @dev Returns the amount of tokens in existence. */ function totalSupply() external view returns (uint256); /** * @dev Returns the amount of tokens owned by `account`. */ function balanceOf(address account) external view returns (uint256); /** * @dev Moves `amount` tokens from the caller's account to `recipient`. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transfer(address recipient, uint256 amount) external returns (bool); /** * @dev Returns the remaining number of tokens that `spender` will be * allowed to spend on behalf of `owner` through {transferFrom}. This is * zero by default. * * This value changes when {approve} or {transferFrom} are called. */ function allowance(address owner, address spender) external view returns (uint256); /** * @dev Sets `amount` as the allowance of `spender` over the caller's tokens. * * Returns a boolean value indicating whether the operation succeeded. * * IMPORTANT: Beware that changing an allowance with this method brings the risk * that someone may use both the old and the new allowance by unfortunate * transaction ordering. One possible solution to mitigate this race * condition is to first reduce the spender's allowance to 0 and set the * desired value afterwards: * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 * * Emits an {Approval} event. */ function approve(address spender, uint256 amount) external returns (bool); /** * @dev Moves `amount` tokens from `sender` to `recipient` using the * allowance mechanism. `amount` is then deducted from the caller's * allowance. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transferFrom( address sender, address recipient, uint256 amount ) external returns (bool); /** * @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); }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; import "@balancer-labs/v2-solidity-utils/contracts/math/FixedPoint.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/InputHelpers.sol"; import "@balancer-labs/v2-pool-utils/contracts/BaseMinimalSwapInfoPool.sol"; import "./WeightedMath.sol"; import "./WeightedPoolUserDataHelpers.sol"; /** * @dev Base class for WeightedPools containing swap, join and exit logic, but leaving storage and management of * the weights to subclasses. Derived contracts can choose to make weights immutable, mutable, or even dynamic * based on local or external logic. */ abstract contract BaseWeightedPool is BaseMinimalSwapInfoPool { using FixedPoint for uint256; using WeightedPoolUserDataHelpers for bytes; uint256 private _lastInvariant; // For backwards compatibility, make sure new join and exit kinds are added at the end of the enum. enum JoinKind { INIT, EXACT_TOKENS_IN_FOR_BPT_OUT, TOKEN_IN_FOR_EXACT_BPT_OUT, ALL_TOKENS_IN_FOR_EXACT_BPT_OUT } enum ExitKind { EXACT_BPT_IN_FOR_ONE_TOKEN_OUT, EXACT_BPT_IN_FOR_TOKENS_OUT, BPT_IN_FOR_EXACT_TOKENS_OUT, MANAGEMENT_FEE_TOKENS_OUT // for InvestmentPool } constructor( IVault vault, string memory name, string memory symbol, IERC20[] memory tokens, address[] memory assetManagers, uint256 swapFeePercentage, uint256 pauseWindowDuration, uint256 bufferPeriodDuration, address owner ) BasePool( vault, // Given BaseMinimalSwapInfoPool supports both of these specializations, and this Pool never registers or // deregisters any tokens after construction, picking Two Token when the Pool only has two tokens is free // gas savings. tokens.length == 2 ? IVault.PoolSpecialization.TWO_TOKEN : IVault.PoolSpecialization.MINIMAL_SWAP_INFO, name, symbol, tokens, assetManagers, swapFeePercentage, pauseWindowDuration, bufferPeriodDuration, owner ) { // solhint-disable-previous-line no-empty-blocks } // Virtual functions /** * @dev Returns the normalized weight of `token`. Weights are fixed point numbers that sum to FixedPoint.ONE. */ function _getNormalizedWeight(IERC20 token) internal view virtual returns (uint256); /** * @dev Returns all normalized weights, in the same order as the Pool's tokens. */ function _getNormalizedWeights() internal view virtual returns (uint256[] memory); /** * @dev Returns all normalized weights, in the same order as the Pool's tokens, along with the index of the token * with the highest weight. */ function _getNormalizedWeightsAndMaxWeightIndex() internal view virtual returns (uint256[] memory, uint256); function getLastInvariant() public view virtual returns (uint256) { return _lastInvariant; } /** * @dev Returns the current value of the invariant. */ function getInvariant() public view returns (uint256) { (, uint256[] memory balances, ) = getVault().getPoolTokens(getPoolId()); // Since the Pool hooks always work with upscaled balances, we manually // upscale here for consistency _upscaleArray(balances, _scalingFactors()); (uint256[] memory normalizedWeights, ) = _getNormalizedWeightsAndMaxWeightIndex(); return WeightedMath._calculateInvariant(normalizedWeights, balances); } function getNormalizedWeights() external view returns (uint256[] memory) { return _getNormalizedWeights(); } // Base Pool handlers // Swap function _onSwapGivenIn( SwapRequest memory swapRequest, uint256 currentBalanceTokenIn, uint256 currentBalanceTokenOut ) internal view virtual override whenNotPaused returns (uint256) { // Swaps are disabled while the contract is paused. return WeightedMath._calcOutGivenIn( currentBalanceTokenIn, _getNormalizedWeight(swapRequest.tokenIn), currentBalanceTokenOut, _getNormalizedWeight(swapRequest.tokenOut), swapRequest.amount ); } function _onSwapGivenOut( SwapRequest memory swapRequest, uint256 currentBalanceTokenIn, uint256 currentBalanceTokenOut ) internal view virtual override whenNotPaused returns (uint256) { // Swaps are disabled while the contract is paused. return WeightedMath._calcInGivenOut( currentBalanceTokenIn, _getNormalizedWeight(swapRequest.tokenIn), currentBalanceTokenOut, _getNormalizedWeight(swapRequest.tokenOut), swapRequest.amount ); } // Initialize function _onInitializePool( bytes32, address, address, uint256[] memory scalingFactors, bytes memory userData ) internal virtual override whenNotPaused returns (uint256, uint256[] memory) { // It would be strange for the Pool to be paused before it is initialized, but for consistency we prevent // initialization in this case. JoinKind kind = userData.joinKind(); _require(kind == JoinKind.INIT, Errors.UNINITIALIZED); uint256[] memory amountsIn = userData.initialAmountsIn(); InputHelpers.ensureInputLengthMatch(_getTotalTokens(), amountsIn.length); _upscaleArray(amountsIn, scalingFactors); (uint256[] memory normalizedWeights, ) = _getNormalizedWeightsAndMaxWeightIndex(); uint256 invariantAfterJoin = WeightedMath._calculateInvariant(normalizedWeights, amountsIn); // Set the initial BPT to the value of the invariant times the number of tokens. This makes BPT supply more // consistent in Pools with similar compositions but different number of tokens. uint256 bptAmountOut = Math.mul(invariantAfterJoin, _getTotalTokens()); _lastInvariant = invariantAfterJoin; return (bptAmountOut, amountsIn); } // Join function _onJoinPool( bytes32, address, address, uint256[] memory balances, uint256, uint256 protocolSwapFeePercentage, uint256[] memory scalingFactors, bytes memory userData ) internal virtual override whenNotPaused returns ( uint256, uint256[] memory, uint256[] memory ) { // All joins are disabled while the contract is paused. (uint256[] memory normalizedWeights, uint256 maxWeightTokenIndex) = _getNormalizedWeightsAndMaxWeightIndex(); // Due protocol swap fee amounts are computed by measuring the growth of the invariant between the previous join // or exit event and now - the invariant's growth is due exclusively to swap fees. This avoids spending gas // computing them on each individual swap uint256 invariantBeforeJoin = WeightedMath._calculateInvariant(normalizedWeights, balances); uint256[] memory dueProtocolFeeAmounts = _getDueProtocolFeeAmounts( balances, normalizedWeights, maxWeightTokenIndex, _lastInvariant, invariantBeforeJoin, protocolSwapFeePercentage ); // Update current balances by subtracting the protocol fee amounts _mutateAmounts(balances, dueProtocolFeeAmounts, FixedPoint.sub); (uint256 bptAmountOut, uint256[] memory amountsIn) = _doJoin( balances, normalizedWeights, scalingFactors, userData ); // Update the invariant with the balances the Pool will have after the join, in order to compute the // protocol swap fee amounts due in future joins and exits. _lastInvariant = _invariantAfterJoin(balances, amountsIn, normalizedWeights); return (bptAmountOut, amountsIn, dueProtocolFeeAmounts); } function _doJoin( uint256[] memory balances, uint256[] memory normalizedWeights, uint256[] memory scalingFactors, bytes memory userData ) internal returns (uint256, uint256[] memory) { JoinKind kind = userData.joinKind(); if (kind == JoinKind.EXACT_TOKENS_IN_FOR_BPT_OUT) { return _joinExactTokensInForBPTOut(balances, normalizedWeights, scalingFactors, userData); } else if (kind == JoinKind.TOKEN_IN_FOR_EXACT_BPT_OUT) { return _joinTokenInForExactBPTOut(balances, normalizedWeights, userData); } else if (kind == JoinKind.ALL_TOKENS_IN_FOR_EXACT_BPT_OUT) { return _joinAllTokensInForExactBPTOut(balances, userData); } else { _revert(Errors.UNHANDLED_JOIN_KIND); } } function _joinExactTokensInForBPTOut( uint256[] memory balances, uint256[] memory normalizedWeights, uint256[] memory scalingFactors, bytes memory userData ) private returns (uint256, uint256[] memory) { (uint256[] memory amountsIn, uint256 minBPTAmountOut) = userData.exactTokensInForBptOut(); InputHelpers.ensureInputLengthMatch(_getTotalTokens(), amountsIn.length); _upscaleArray(amountsIn, scalingFactors); (uint256 bptAmountOut, uint256[] memory swapFees) = WeightedMath._calcBptOutGivenExactTokensIn( balances, normalizedWeights, amountsIn, totalSupply(), getSwapFeePercentage() ); // Note that swapFees is already upscaled _processSwapFeeAmounts(swapFees); _require(bptAmountOut >= minBPTAmountOut, Errors.BPT_OUT_MIN_AMOUNT); return (bptAmountOut, amountsIn); } function _joinTokenInForExactBPTOut( uint256[] memory balances, uint256[] memory normalizedWeights, bytes memory userData ) private returns (uint256, uint256[] memory) { (uint256 bptAmountOut, uint256 tokenIndex) = userData.tokenInForExactBptOut(); // Note that there is no maximum amountIn parameter: this is handled by `IVault.joinPool`. _require(tokenIndex < _getTotalTokens(), Errors.OUT_OF_BOUNDS); (uint256 amountIn, uint256 swapFee) = WeightedMath._calcTokenInGivenExactBptOut( balances[tokenIndex], normalizedWeights[tokenIndex], bptAmountOut, totalSupply(), getSwapFeePercentage() ); // Note that swapFee is already upscaled _processSwapFeeAmount(tokenIndex, swapFee); // We join in a single token, so we initialize amountsIn with zeros uint256[] memory amountsIn = new uint256[](_getTotalTokens()); // And then assign the result to the selected token amountsIn[tokenIndex] = amountIn; return (bptAmountOut, amountsIn); } function _joinAllTokensInForExactBPTOut(uint256[] memory balances, bytes memory userData) private view returns (uint256, uint256[] memory) { uint256 bptAmountOut = userData.allTokensInForExactBptOut(); // Note that there is no maximum amountsIn parameter: this is handled by `IVault.joinPool`. uint256[] memory amountsIn = WeightedMath._calcAllTokensInGivenExactBptOut( balances, bptAmountOut, totalSupply() ); return (bptAmountOut, amountsIn); } // Exit function _onExitPool( bytes32, address, address, uint256[] memory balances, uint256, uint256 protocolSwapFeePercentage, uint256[] memory scalingFactors, bytes memory userData ) internal virtual override returns ( uint256 bptAmountIn, uint256[] memory amountsOut, uint256[] memory dueProtocolFeeAmounts ) { (uint256[] memory normalizedWeights, uint256 maxWeightTokenIndex) = _getNormalizedWeightsAndMaxWeightIndex(); // Exits are not completely disabled while the contract is paused: proportional exits (exact BPT in for tokens // out) remain functional. if (_isNotPaused()) { // Due protocol swap fee amounts are computed by measuring the growth of the invariant between the previous // join or exit event and now - the invariant's growth is due exclusively to swap fees. This avoids // spending gas calculating the fees on each individual swap. uint256 invariantBeforeExit = WeightedMath._calculateInvariant(normalizedWeights, balances); dueProtocolFeeAmounts = _getDueProtocolFeeAmounts( balances, normalizedWeights, maxWeightTokenIndex, _lastInvariant, invariantBeforeExit, protocolSwapFeePercentage ); // Update current balances by subtracting the protocol fee amounts _mutateAmounts(balances, dueProtocolFeeAmounts, FixedPoint.sub); } else { // If the contract is paused, swap protocol fee amounts are not charged to avoid extra calculations and // reduce the potential for errors. dueProtocolFeeAmounts = new uint256[](_getTotalTokens()); } (bptAmountIn, amountsOut) = _doExit(balances, normalizedWeights, scalingFactors, userData); // Update the invariant with the balances the Pool will have after the exit, in order to compute the // protocol swap fees due in future joins and exits. _lastInvariant = _invariantAfterExit(balances, amountsOut, normalizedWeights); return (bptAmountIn, amountsOut, dueProtocolFeeAmounts); } function _doExit( uint256[] memory balances, uint256[] memory normalizedWeights, uint256[] memory scalingFactors, bytes memory userData ) internal returns (uint256, uint256[] memory) { ExitKind kind = userData.exitKind(); if (kind == ExitKind.EXACT_BPT_IN_FOR_ONE_TOKEN_OUT) { return _exitExactBPTInForTokenOut(balances, normalizedWeights, userData); } else if (kind == ExitKind.EXACT_BPT_IN_FOR_TOKENS_OUT) { return _exitExactBPTInForTokensOut(balances, userData); } else if (kind == ExitKind.BPT_IN_FOR_EXACT_TOKENS_OUT) { return _exitBPTInForExactTokensOut(balances, normalizedWeights, scalingFactors, userData); } else { _revert(Errors.UNHANDLED_EXIT_KIND); } } function _exitExactBPTInForTokenOut( uint256[] memory balances, uint256[] memory normalizedWeights, bytes memory userData ) private whenNotPaused returns (uint256, uint256[] memory) { // This exit function is disabled if the contract is paused. (uint256 bptAmountIn, uint256 tokenIndex) = userData.exactBptInForTokenOut(); // Note that there is no minimum amountOut parameter: this is handled by `IVault.exitPool`. _require(tokenIndex < _getTotalTokens(), Errors.OUT_OF_BOUNDS); (uint256 amountOut, uint256 swapFee) = WeightedMath._calcTokenOutGivenExactBptIn( balances[tokenIndex], normalizedWeights[tokenIndex], bptAmountIn, totalSupply(), getSwapFeePercentage() ); // This is an exceptional situation in which the fee is charged on a token out instead of a token in. // Note that swapFee is already upscaled. _processSwapFeeAmount(tokenIndex, swapFee); // We exit in a single token, so we initialize amountsOut with zeros uint256[] memory amountsOut = new uint256[](_getTotalTokens()); // And then assign the result to the selected token amountsOut[tokenIndex] = amountOut; return (bptAmountIn, amountsOut); } function _exitExactBPTInForTokensOut(uint256[] memory balances, bytes memory userData) private view returns (uint256, uint256[] memory) { // This exit function is the only one that is not disabled if the contract is paused: it remains unrestricted // in an attempt to provide users with a mechanism to retrieve their tokens in case of an emergency. // This particular exit function is the only one that remains available because it is the simplest one, and // therefore the one with the lowest likelihood of errors. uint256 bptAmountIn = userData.exactBptInForTokensOut(); // Note that there is no minimum amountOut parameter: this is handled by `IVault.exitPool`. uint256[] memory amountsOut = WeightedMath._calcTokensOutGivenExactBptIn(balances, bptAmountIn, totalSupply()); return (bptAmountIn, amountsOut); } function _exitBPTInForExactTokensOut( uint256[] memory balances, uint256[] memory normalizedWeights, uint256[] memory scalingFactors, bytes memory userData ) private whenNotPaused returns (uint256, uint256[] memory) { // This exit function is disabled if the contract is paused. (uint256[] memory amountsOut, uint256 maxBPTAmountIn) = userData.bptInForExactTokensOut(); InputHelpers.ensureInputLengthMatch(amountsOut.length, _getTotalTokens()); _upscaleArray(amountsOut, scalingFactors); (uint256 bptAmountIn, uint256[] memory swapFees) = WeightedMath._calcBptInGivenExactTokensOut( balances, normalizedWeights, amountsOut, totalSupply(), getSwapFeePercentage() ); _require(bptAmountIn <= maxBPTAmountIn, Errors.BPT_IN_MAX_AMOUNT); // This is an exceptional situation in which the fee is charged on a token out instead of a token in. // Note that swapFee is already upscaled. _processSwapFeeAmounts(swapFees); return (bptAmountIn, amountsOut); } // Helpers function _getDueProtocolFeeAmounts( uint256[] memory balances, uint256[] memory normalizedWeights, uint256 maxWeightTokenIndex, uint256 previousInvariant, uint256 currentInvariant, uint256 protocolSwapFeePercentage ) private view returns (uint256[] memory) { // Initialize with zeros uint256[] memory dueProtocolFeeAmounts = new uint256[](_getTotalTokens()); // Early return if the protocol swap fee percentage is zero, saving gas. if (protocolSwapFeePercentage == 0) { return dueProtocolFeeAmounts; } // The protocol swap fees are always paid using the token with the largest weight in the Pool. As this is the // token that is expected to have the largest balance, using it to pay fees should not unbalance the Pool. dueProtocolFeeAmounts[maxWeightTokenIndex] = WeightedMath._calcDueTokenProtocolSwapFeeAmount( balances[maxWeightTokenIndex], normalizedWeights[maxWeightTokenIndex], previousInvariant, currentInvariant, protocolSwapFeePercentage ); return dueProtocolFeeAmounts; } /** * @dev Returns the value of the invariant given `balances`, assuming they are increased by `amountsIn`. All * amounts are expected to be upscaled. */ function _invariantAfterJoin( uint256[] memory balances, uint256[] memory amountsIn, uint256[] memory normalizedWeights ) private view returns (uint256) { _mutateAmounts(balances, amountsIn, FixedPoint.add); return WeightedMath._calculateInvariant(normalizedWeights, balances); } function _invariantAfterExit( uint256[] memory balances, uint256[] memory amountsOut, uint256[] memory normalizedWeights ) private view returns (uint256) { _mutateAmounts(balances, amountsOut, FixedPoint.sub); return WeightedMath._calculateInvariant(normalizedWeights, balances); } /** * @dev Mutates `amounts` by applying `mutation` with each entry in `arguments`. * * Equivalent to `amounts = amounts.map(mutation)`. */ function _mutateAmounts( uint256[] memory toMutate, uint256[] memory arguments, function(uint256, uint256) pure returns (uint256) mutation ) private view { for (uint256 i = 0; i < _getTotalTokens(); ++i) { toMutate[i] = mutation(toMutate[i], arguments[i]); } } /** * @dev This function returns the appreciation of one BPT relative to the * underlying tokens. This starts at 1 when the pool is created and grows over time */ function getRate() public view returns (uint256) { // The initial BPT supply is equal to the invariant times the number of tokens. return Math.mul(getInvariant(), _getTotalTokens()).divDown(totalSupply()); } }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; import "./LogExpMath.sol"; import "../helpers/BalancerErrors.sol"; /* solhint-disable private-vars-leading-underscore */ library FixedPoint { uint256 internal constant ONE = 1e18; // 18 decimal places uint256 internal constant MAX_POW_RELATIVE_ERROR = 10000; // 10^(-14) // Minimum base for the power function when the exponent is 'free' (larger than ONE). uint256 internal constant MIN_POW_BASE_FREE_EXPONENT = 0.7e18; function add(uint256 a, uint256 b) internal pure returns (uint256) { // Fixed Point addition is the same as regular checked addition uint256 c = a + b; _require(c >= a, Errors.ADD_OVERFLOW); return c; } function sub(uint256 a, uint256 b) internal pure returns (uint256) { // Fixed Point addition is the same as regular checked addition _require(b <= a, Errors.SUB_OVERFLOW); uint256 c = a - b; return c; } function mulDown(uint256 a, uint256 b) internal pure returns (uint256) { uint256 product = a * b; _require(a == 0 || product / a == b, Errors.MUL_OVERFLOW); return product / ONE; } function mulUp(uint256 a, uint256 b) internal pure returns (uint256) { uint256 product = a * b; _require(a == 0 || product / a == b, Errors.MUL_OVERFLOW); if (product == 0) { return 0; } else { // 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, which we already tested for. return ((product - 1) / ONE) + 1; } } function divDown(uint256 a, uint256 b) internal pure returns (uint256) { _require(b != 0, Errors.ZERO_DIVISION); if (a == 0) { return 0; } else { uint256 aInflated = a * ONE; _require(aInflated / a == ONE, Errors.DIV_INTERNAL); // mul overflow return aInflated / b; } } function divUp(uint256 a, uint256 b) internal pure returns (uint256) { _require(b != 0, Errors.ZERO_DIVISION); if (a == 0) { return 0; } else { uint256 aInflated = a * ONE; _require(aInflated / a == ONE, Errors.DIV_INTERNAL); // mul overflow // 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, which we already tested for. return ((aInflated - 1) / b) + 1; } } /** * @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) { uint256 raw = LogExpMath.pow(x, y); uint256 maxError = add(mulUp(raw, MAX_POW_RELATIVE_ERROR), 1); if (raw < maxError) { return 0; } else { return sub(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) { uint256 raw = LogExpMath.pow(x, y); uint256 maxError = add(mulUp(raw, MAX_POW_RELATIVE_ERROR), 1); return add(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) { return (x < ONE) ? (ONE - x) : 0; } }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; import "../openzeppelin/IERC20.sol"; import "./BalancerErrors.sol"; library InputHelpers { function ensureInputLengthMatch(uint256 a, uint256 b) internal pure { _require(a == b, Errors.INPUT_LENGTH_MISMATCH); } function ensureInputLengthMatch( uint256 a, uint256 b, uint256 c ) internal pure { _require(a == b && b == c, Errors.INPUT_LENGTH_MISMATCH); } function ensureArrayIsSorted(IERC20[] memory array) internal pure { address[] memory addressArray; // solhint-disable-next-line no-inline-assembly assembly { addressArray := array } ensureArrayIsSorted(addressArray); } function ensureArrayIsSorted(address[] memory array) internal pure { if (array.length < 2) { return; } address previous = array[0]; for (uint256 i = 1; i < array.length; ++i) { address current = array[i]; _require(previous < current, Errors.UNSORTED_ARRAY); previous = current; } } }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; import "./BasePool.sol"; import "@balancer-labs/v2-vault/contracts/interfaces/IMinimalSwapInfoPool.sol"; /** * @dev Extension of `BasePool`, adding a handler for `IMinimalSwapInfoPool.onSwap`. * * Derived contracts must call `BasePool`'s constructor, and implement `_onSwapGivenIn` and `_onSwapGivenOut` along with * `BasePool`'s virtual functions. Inheriting from this contract lets derived contracts choose the Two Token or Minimal * Swap Info specialization settings. */ abstract contract BaseMinimalSwapInfoPool is IMinimalSwapInfoPool, BasePool { // Swap Hooks function onSwap( SwapRequest memory request, uint256 balanceTokenIn, uint256 balanceTokenOut ) public virtual override onlyVault(request.poolId) returns (uint256) { uint256 scalingFactorTokenIn = _scalingFactor(request.tokenIn); uint256 scalingFactorTokenOut = _scalingFactor(request.tokenOut); if (request.kind == IVault.SwapKind.GIVEN_IN) { // Fees are subtracted before scaling, to reduce the complexity of the rounding direction analysis. uint256 amountInMinusSwapFees = _subtractSwapFeeAmount(request.amount); // Process the (upscaled!) swap fee. uint256 swapFee = request.amount - amountInMinusSwapFees; _processSwapFeeAmount(request.tokenIn, _upscale(swapFee, scalingFactorTokenIn)); request.amount = amountInMinusSwapFees; // All token amounts are upscaled. balanceTokenIn = _upscale(balanceTokenIn, scalingFactorTokenIn); balanceTokenOut = _upscale(balanceTokenOut, scalingFactorTokenOut); request.amount = _upscale(request.amount, scalingFactorTokenIn); uint256 amountOut = _onSwapGivenIn(request, balanceTokenIn, balanceTokenOut); // amountOut tokens are exiting the Pool, so we round down. return _downscaleDown(amountOut, scalingFactorTokenOut); } else { // All token amounts are upscaled. balanceTokenIn = _upscale(balanceTokenIn, scalingFactorTokenIn); balanceTokenOut = _upscale(balanceTokenOut, scalingFactorTokenOut); request.amount = _upscale(request.amount, scalingFactorTokenOut); uint256 amountIn = _onSwapGivenOut(request, balanceTokenIn, balanceTokenOut); // amountIn tokens are entering the Pool, so we round up. amountIn = _downscaleUp(amountIn, scalingFactorTokenIn); // Fees are added after scaling happens, to reduce the complexity of the rounding direction analysis. uint256 amountInPlusSwapFees = _addSwapFeeAmount(amountIn); // Process the (upscaled!) swap fee. uint256 swapFee = amountInPlusSwapFees - amountIn; _processSwapFeeAmount(request.tokenIn, _upscale(swapFee, scalingFactorTokenIn)); return amountInPlusSwapFees; } } /* * @dev Called when a swap with the Pool occurs, where the amount of tokens entering the Pool is known. * * Returns the amount of tokens that will be taken from the Pool in return. * * All amounts inside `swapRequest`, `balanceTokenIn` and `balanceTokenOut` are upscaled. The swap fee has already * been deducted from `swapRequest.amount`. * * The return value is also considered upscaled, and will be downscaled (rounding down) before returning it to the * Vault. */ function _onSwapGivenIn( SwapRequest memory swapRequest, uint256 balanceTokenIn, uint256 balanceTokenOut ) internal virtual returns (uint256); /* * @dev Called when a swap with the Pool occurs, where the amount of tokens exiting the Pool is known. * * Returns the amount of tokens that will be granted to the Pool in return. * * All amounts inside `swapRequest`, `balanceTokenIn` and `balanceTokenOut` are upscaled. * * The return value is also considered upscaled, and will be downscaled (rounding up) before applying the swap fee * and returning it to the Vault. */ function _onSwapGivenOut( SwapRequest memory swapRequest, uint256 balanceTokenIn, uint256 balanceTokenOut ) internal virtual returns (uint256); /** * @dev Called whenever a swap fee is charged. Implementations should call their parents via super, to ensure all * implementations in the inheritance tree are called. * * Callers must call one of the three `_processSwapFeeAmount` functions when swap fees are computed, * and upscale `amount`. */ function _processSwapFeeAmount( uint256, /*index*/ uint256 /*amount*/ ) internal virtual { // solhint-disable-previous-line no-empty-blocks } function _processSwapFeeAmount(IERC20 token, uint256 amount) internal { _processSwapFeeAmount(_tokenAddressToIndex(token), amount); } function _processSwapFeeAmounts(uint256[] memory amounts) internal { InputHelpers.ensureInputLengthMatch(amounts.length, _getTotalTokens()); for (uint256 i = 0; i < _getTotalTokens(); ++i) { _processSwapFeeAmount(i, amounts[i]); } } /** * @dev Returns the index of `token` in the Pool's token array (i.e. the one `vault.getPoolTokens()` would return). * * A trivial (and incorrect!) implementation is already provided for Pools that don't override * `_processSwapFeeAmount` and skip the entire feature. However, Pools that do override `_processSwapFeeAmount` * *must* override this function with a meaningful implementation. */ function _tokenAddressToIndex( IERC20 /*token*/ ) internal view virtual returns (uint256) { return 0; } }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; import "@balancer-labs/v2-solidity-utils/contracts/math/FixedPoint.sol"; import "@balancer-labs/v2-solidity-utils/contracts/math/Math.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/InputHelpers.sol"; // These functions start with an underscore, as if they were part of a contract and not a library. At some point this // should be fixed. // solhint-disable private-vars-leading-underscore library WeightedMath { using FixedPoint for uint256; // A minimum normalized weight imposes a maximum weight ratio. We need this due to limitations in the // implementation of the power function, as these ratios are often exponents. uint256 internal constant _MIN_WEIGHT = 0.01e18; // Having a minimum normalized weight imposes a limit on the maximum number of tokens; // i.e., the largest possible pool is one where all tokens have exactly the minimum weight. uint256 internal constant _MAX_WEIGHTED_TOKENS = 100; // Pool limits that arise from limitations in the fixed point power function (and the imposed 1:100 maximum weight // ratio). // Swap limits: amounts swapped may not be larger than this percentage of total balance. uint256 internal constant _MAX_IN_RATIO = 0.3e18; uint256 internal constant _MAX_OUT_RATIO = 0.3e18; // Invariant growth limit: non-proportional joins cannot cause the invariant to increase by more than this ratio. uint256 internal constant _MAX_INVARIANT_RATIO = 3e18; // Invariant shrink limit: non-proportional exits cannot cause the invariant to decrease by less than this ratio. uint256 internal constant _MIN_INVARIANT_RATIO = 0.7e18; // About swap fees on joins and exits: // Any join or exit that is not perfectly balanced (e.g. all single token joins or exits) is mathematically // equivalent to a perfectly balanced join or exit followed by a series of swaps. Since these swaps would charge // swap fees, it follows that (some) joins and exits should as well. // On these operations, we split the token amounts in 'taxable' and 'non-taxable' portions, where the 'taxable' part // is the one to which swap fees are applied. // Invariant is used to collect protocol swap fees by comparing its value between two times. // So we can round always to the same direction. It is also used to initiate the BPT amount // and, because there is a minimum BPT, we round down the invariant. function _calculateInvariant(uint256[] memory normalizedWeights, uint256[] memory balances) internal pure returns (uint256 invariant) { /********************************************************************************************** // invariant _____ // // wi = weight index i | | wi // // bi = balance index i | | bi ^ = i // // i = invariant // **********************************************************************************************/ invariant = FixedPoint.ONE; for (uint256 i = 0; i < normalizedWeights.length; i++) { invariant = invariant.mulDown(balances[i].powDown(normalizedWeights[i])); } _require(invariant > 0, Errors.ZERO_INVARIANT); } // Computes how many tokens can be taken out of a pool if `amountIn` are sent, given the // current balances and weights. function _calcOutGivenIn( uint256 balanceIn, uint256 weightIn, uint256 balanceOut, uint256 weightOut, uint256 amountIn ) internal pure returns (uint256) { /********************************************************************************************** // outGivenIn // // aO = amountOut // // bO = balanceOut // // bI = balanceIn / / bI \ (wI / wO) \ // // aI = amountIn aO = bO * | 1 - | -------------------------- | ^ | // // wI = weightIn \ \ ( bI + aI ) / / // // wO = weightOut // **********************************************************************************************/ // Amount out, so we round down overall. // The multiplication rounds down, and the subtrahend (power) rounds up (so the base rounds up too). // Because bI / (bI + aI) <= 1, the exponent rounds down. // Cannot exceed maximum in ratio _require(amountIn <= balanceIn.mulDown(_MAX_IN_RATIO), Errors.MAX_IN_RATIO); uint256 denominator = balanceIn.add(amountIn); uint256 base = balanceIn.divUp(denominator); uint256 exponent = weightIn.divDown(weightOut); uint256 power = base.powUp(exponent); return balanceOut.mulDown(power.complement()); } // Computes how many tokens must be sent to a pool in order to take `amountOut`, given the // current balances and weights. function _calcInGivenOut( uint256 balanceIn, uint256 weightIn, uint256 balanceOut, uint256 weightOut, uint256 amountOut ) internal pure returns (uint256) { /********************************************************************************************** // inGivenOut // // aO = amountOut // // bO = balanceOut // // bI = balanceIn / / bO \ (wO / wI) \ // // aI = amountIn aI = bI * | | -------------------------- | ^ - 1 | // // wI = weightIn \ \ ( bO - aO ) / / // // wO = weightOut // **********************************************************************************************/ // Amount in, so we round up overall. // The multiplication rounds up, and the power rounds up (so the base rounds up too). // Because b0 / (b0 - a0) >= 1, the exponent rounds up. // Cannot exceed maximum out ratio _require(amountOut <= balanceOut.mulDown(_MAX_OUT_RATIO), Errors.MAX_OUT_RATIO); uint256 base = balanceOut.divUp(balanceOut.sub(amountOut)); uint256 exponent = weightOut.divUp(weightIn); uint256 power = base.powUp(exponent); // Because the base is larger than one (and the power rounds up), the power should always be larger than one, so // the following subtraction should never revert. uint256 ratio = power.sub(FixedPoint.ONE); return balanceIn.mulUp(ratio); } function _calcBptOutGivenExactTokensIn( uint256[] memory balances, uint256[] memory normalizedWeights, uint256[] memory amountsIn, uint256 bptTotalSupply, uint256 swapFeePercentage ) internal pure returns (uint256, uint256[] memory) { // BPT out, so we round down overall. uint256[] memory balanceRatiosWithFee = new uint256[](amountsIn.length); uint256 invariantRatioWithFees = 0; for (uint256 i = 0; i < balances.length; i++) { balanceRatiosWithFee[i] = balances[i].add(amountsIn[i]).divDown(balances[i]); invariantRatioWithFees = invariantRatioWithFees.add(balanceRatiosWithFee[i].mulDown(normalizedWeights[i])); } (uint256 invariantRatio, uint256[] memory swapFees) = _computeJoinExactTokensInInvariantRatio( balances, normalizedWeights, amountsIn, balanceRatiosWithFee, invariantRatioWithFees, swapFeePercentage ); uint256 bptOut = (invariantRatio > FixedPoint.ONE) ? bptTotalSupply.mulDown(invariantRatio.sub(FixedPoint.ONE)) : 0; return (bptOut, swapFees); } /** * @dev Intermediate function to avoid stack-too-deep errors. */ function _computeJoinExactTokensInInvariantRatio( uint256[] memory balances, uint256[] memory normalizedWeights, uint256[] memory amountsIn, uint256[] memory balanceRatiosWithFee, uint256 invariantRatioWithFees, uint256 swapFeePercentage ) private pure returns (uint256 invariantRatio, uint256[] memory swapFees) { // Swap fees are charged on all tokens that are being added in a larger proportion than the overall invariant // increase. swapFees = new uint256[](amountsIn.length); invariantRatio = FixedPoint.ONE; for (uint256 i = 0; i < balances.length; i++) { uint256 amountInWithoutFee; if (balanceRatiosWithFee[i] > invariantRatioWithFees) { uint256 nonTaxableAmount = balances[i].mulDown(invariantRatioWithFees.sub(FixedPoint.ONE)); uint256 taxableAmount = amountsIn[i].sub(nonTaxableAmount); uint256 swapFee = taxableAmount.mulUp(swapFeePercentage); amountInWithoutFee = nonTaxableAmount.add(taxableAmount.sub(swapFee)); swapFees[i] = swapFee; } else { amountInWithoutFee = amountsIn[i]; } uint256 balanceRatio = balances[i].add(amountInWithoutFee).divDown(balances[i]); invariantRatio = invariantRatio.mulDown(balanceRatio.powDown(normalizedWeights[i])); } } function _calcTokenInGivenExactBptOut( uint256 balance, uint256 normalizedWeight, uint256 bptAmountOut, uint256 bptTotalSupply, uint256 swapFeePercentage ) internal pure returns (uint256 amountIn, uint256 swapFee) { /****************************************************************************************** // tokenInForExactBPTOut // // a = amountIn // // b = balance / / totalBPT + bptOut \ (1 / w) \ // // bptOut = bptAmountOut a = b * | | -------------------------- | ^ - 1 | // // bpt = totalBPT \ \ totalBPT / / // // w = weight // ******************************************************************************************/ // Token in, so we round up overall. // Calculate the factor by which the invariant will increase after minting BPTAmountOut uint256 invariantRatio = bptTotalSupply.add(bptAmountOut).divUp(bptTotalSupply); _require(invariantRatio <= _MAX_INVARIANT_RATIO, Errors.MAX_OUT_BPT_FOR_TOKEN_IN); // Calculate by how much the token balance has to increase to match the invariantRatio uint256 balanceRatio = invariantRatio.powUp(FixedPoint.ONE.divUp(normalizedWeight)); uint256 amountInWithoutFee = balance.mulUp(balanceRatio.sub(FixedPoint.ONE)); // We can now compute how much extra balance is being deposited and used in virtual swaps, and charge swap fees // accordingly. uint256 taxablePercentage = normalizedWeight.complement(); uint256 taxableAmount = amountInWithoutFee.mulUp(taxablePercentage); uint256 nonTaxableAmount = amountInWithoutFee.sub(taxableAmount); uint256 taxableAmountPlusFees = taxableAmount.divUp(FixedPoint.ONE.sub(swapFeePercentage)); swapFee = taxableAmountPlusFees - taxableAmount; amountIn = nonTaxableAmount.add(taxableAmountPlusFees); } function _calcAllTokensInGivenExactBptOut( uint256[] memory balances, uint256 bptAmountOut, uint256 totalBPT ) internal pure returns (uint256[] memory) { /************************************************************************************ // tokensInForExactBptOut // // (per token) // // aI = amountIn / bptOut \ // // b = balance aI = b * | ------------ | // // bptOut = bptAmountOut \ totalBPT / // // bpt = totalBPT // ************************************************************************************/ // Tokens in, so we round up overall. uint256 bptRatio = bptAmountOut.divUp(totalBPT); uint256[] memory amountsIn = new uint256[](balances.length); for (uint256 i = 0; i < balances.length; i++) { amountsIn[i] = balances[i].mulUp(bptRatio); } return amountsIn; } function _calcBptInGivenExactTokensOut( uint256[] memory balances, uint256[] memory normalizedWeights, uint256[] memory amountsOut, uint256 bptTotalSupply, uint256 swapFeePercentage ) internal pure returns (uint256, uint256[] memory) { // BPT in, so we round up overall. uint256[] memory balanceRatiosWithoutFee = new uint256[](amountsOut.length); uint256 invariantRatioWithoutFees = 0; for (uint256 i = 0; i < balances.length; i++) { balanceRatiosWithoutFee[i] = balances[i].sub(amountsOut[i]).divUp(balances[i]); invariantRatioWithoutFees = invariantRatioWithoutFees.add( balanceRatiosWithoutFee[i].mulUp(normalizedWeights[i]) ); } (uint256 invariantRatio, uint256[] memory swapFees) = _computeExitExactTokensOutInvariantRatio( balances, normalizedWeights, amountsOut, balanceRatiosWithoutFee, invariantRatioWithoutFees, swapFeePercentage ); uint256 bptIn = bptTotalSupply.mulUp(invariantRatio.complement()); return (bptIn, swapFees); } /** * @dev Intermediate function to avoid stack-too-deep errors. */ function _computeExitExactTokensOutInvariantRatio( uint256[] memory balances, uint256[] memory normalizedWeights, uint256[] memory amountsOut, uint256[] memory balanceRatiosWithoutFee, uint256 invariantRatioWithoutFees, uint256 swapFeePercentage ) private pure returns (uint256 invariantRatio, uint256[] memory swapFees) { swapFees = new uint256[](amountsOut.length); invariantRatio = FixedPoint.ONE; for (uint256 i = 0; i < balances.length; i++) { // Swap fees are typically charged on 'token in', but there is no 'token in' here, so we apply it to // 'token out'. This results in slightly larger price impact. uint256 amountOutWithFee; if (invariantRatioWithoutFees > balanceRatiosWithoutFee[i]) { uint256 nonTaxableAmount = balances[i].mulDown(invariantRatioWithoutFees.complement()); uint256 taxableAmount = amountsOut[i].sub(nonTaxableAmount); uint256 taxableAmountPlusFees = taxableAmount.divUp(FixedPoint.ONE.sub(swapFeePercentage)); swapFees[i] = taxableAmountPlusFees - taxableAmount; amountOutWithFee = nonTaxableAmount.add(taxableAmountPlusFees); } else { amountOutWithFee = amountsOut[i]; } uint256 balanceRatio = balances[i].sub(amountOutWithFee).divDown(balances[i]); invariantRatio = invariantRatio.mulDown(balanceRatio.powDown(normalizedWeights[i])); } } function _calcTokenOutGivenExactBptIn( uint256 balance, uint256 normalizedWeight, uint256 bptAmountIn, uint256 bptTotalSupply, uint256 swapFeePercentage ) internal pure returns (uint256 amountOut, uint256 swapFee) { /***************************************************************************************** // exactBPTInForTokenOut // // a = amountOut // // b = balance / / totalBPT - bptIn \ (1 / w) \ // // bptIn = bptAmountIn a = b * | 1 - | -------------------------- | ^ | // // bpt = totalBPT \ \ totalBPT / / // // w = weight // *****************************************************************************************/ // Token out, so we round down overall. The multiplication rounds down, but the power rounds up (so the base // rounds up). Because (totalBPT - bptIn) / totalBPT <= 1, the exponent rounds down. // Calculate the factor by which the invariant will decrease after burning BPTAmountIn uint256 invariantRatio = bptTotalSupply.sub(bptAmountIn).divUp(bptTotalSupply); _require(invariantRatio >= _MIN_INVARIANT_RATIO, Errors.MIN_BPT_IN_FOR_TOKEN_OUT); // Calculate by how much the token balance has to decrease to match invariantRatio uint256 balanceRatio = invariantRatio.powUp(FixedPoint.ONE.divDown(normalizedWeight)); // Because of rounding up, balanceRatio can be greater than one. Using complement prevents reverts. uint256 amountOutWithoutFee = balance.mulDown(balanceRatio.complement()); // We can now compute how much excess balance is being withdrawn as a result of the virtual swaps, which result // in swap fees. uint256 taxablePercentage = normalizedWeight.complement(); // Swap fees are typically charged on 'token in', but there is no 'token in' here, so we apply it // to 'token out'. This results in slightly larger price impact. Fees are rounded up. uint256 taxableAmount = amountOutWithoutFee.mulUp(taxablePercentage); uint256 nonTaxableAmount = amountOutWithoutFee.sub(taxableAmount); swapFee = taxableAmount.mulUp(swapFeePercentage); amountOut = nonTaxableAmount.add(taxableAmount.sub(swapFee)); } function _calcTokensOutGivenExactBptIn( uint256[] memory balances, uint256 bptAmountIn, uint256 totalBPT ) internal pure returns (uint256[] memory) { /********************************************************************************************** // exactBPTInForTokensOut // // (per token) // // aO = amountOut / bptIn \ // // b = balance a0 = b * | --------------------- | // // bptIn = bptAmountIn \ totalBPT / // // bpt = totalBPT // **********************************************************************************************/ // Since we're computing an amount out, we round down overall. This means rounding down on both the // multiplication and division. uint256 bptRatio = bptAmountIn.divDown(totalBPT); uint256[] memory amountsOut = new uint256[](balances.length); for (uint256 i = 0; i < balances.length; i++) { amountsOut[i] = balances[i].mulDown(bptRatio); } return amountsOut; } function _calcDueTokenProtocolSwapFeeAmount( uint256 balance, uint256 normalizedWeight, uint256 previousInvariant, uint256 currentInvariant, uint256 protocolSwapFeePercentage ) internal pure returns (uint256) { /********************************************************************************* /* protocolSwapFeePercentage * balanceToken * ( 1 - (previousInvariant / currentInvariant) ^ (1 / weightToken)) *********************************************************************************/ if (currentInvariant <= previousInvariant) { // This shouldn't happen outside of rounding errors, but have this safeguard nonetheless to prevent the Pool // from entering a locked state in which joins and exits revert while computing accumulated swap fees. return 0; } // We round down to prevent issues in the Pool's accounting, even if it means paying slightly less in protocol // fees to the Vault. // Fee percentage and balance multiplications round down, while the subtrahend (power) rounds up (as does the // base). Because previousInvariant / currentInvariant <= 1, the exponent rounds down. uint256 base = previousInvariant.divUp(currentInvariant); uint256 exponent = FixedPoint.ONE.divDown(normalizedWeight); // Because the exponent is larger than one, the base of the power function has a lower bound. We cap to this // value to avoid numeric issues, which means in the extreme case (where the invariant growth is larger than // 1 / min exponent) the Pool will pay less in protocol fees than it should. base = Math.max(base, FixedPoint.MIN_POW_BASE_FREE_EXPONENT); uint256 power = base.powUp(exponent); uint256 tokenAccruedFees = balance.mulDown(power.complement()); return tokenAccruedFees.mulDown(protocolSwapFeePercentage); } }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; import "@balancer-labs/v2-solidity-utils/contracts/math/Math.sol"; import "@balancer-labs/v2-solidity-utils/contracts/math/FixedPoint.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/InputHelpers.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/TemporarilyPausable.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/WordCodec.sol"; import "@balancer-labs/v2-solidity-utils/contracts/openzeppelin/ERC20.sol"; import "@balancer-labs/v2-vault/contracts/interfaces/IVault.sol"; import "@balancer-labs/v2-vault/contracts/interfaces/IBasePool.sol"; import "@balancer-labs/v2-asset-manager-utils/contracts/IAssetManager.sol"; import "./BalancerPoolToken.sol"; import "./BasePoolAuthorization.sol"; // solhint-disable max-states-count /** * @dev Reference implementation for the base layer of a Pool contract that manages a single Pool with optional * Asset Managers, an admin-controlled swap fee percentage, and an emergency pause mechanism. * * Note that neither swap fees nor the pause mechanism are used by this contract. They are passed through so that * derived contracts can use them via the `_addSwapFeeAmount` and `_subtractSwapFeeAmount` functions, and the * `whenNotPaused` modifier. * * No admin permissions are checked here: instead, this contract delegates that to the Vault's own Authorizer. * * Because this contract doesn't implement the swap hooks, derived contracts should generally inherit from * BaseGeneralPool or BaseMinimalSwapInfoPool. Otherwise, subclasses must inherit from the corresponding interfaces * and implement the swap callbacks themselves. */ abstract contract BasePool is IBasePool, BasePoolAuthorization, BalancerPoolToken, TemporarilyPausable { using WordCodec for bytes32; using FixedPoint for uint256; uint256 private constant _MIN_TOKENS = 2; uint256 private constant _MINIMUM_BPT = 1e6; // 1e18 corresponds to 1.0, or a 100% fee uint256 private constant _MIN_SWAP_FEE_PERCENTAGE = 1e12; // 0.0001% uint256 private constant _MAX_SWAP_FEE_PERCENTAGE = 1e17; // 10% - this fits in 64 bits // Storage slot that can be used to store unrelated pieces of information. In particular, by default is used // to store only the swap fee percentage of a pool. But it can be extended to store some more pieces of information. // The swap fee percentage is stored in the most-significant 64 bits, therefore the remaining 192 bits can be // used to store any other piece of information. bytes32 private _miscData; uint256 private constant _SWAP_FEE_PERCENTAGE_OFFSET = 192; bytes32 private immutable _poolId; event SwapFeePercentageChanged(uint256 swapFeePercentage); constructor( IVault vault, IVault.PoolSpecialization specialization, string memory name, string memory symbol, IERC20[] memory tokens, address[] memory assetManagers, uint256 swapFeePercentage, uint256 pauseWindowDuration, uint256 bufferPeriodDuration, address owner ) // Base Pools are expected to be deployed using factories. By using the factory address as the action // disambiguator, we make all Pools deployed by the same factory share action identifiers. This allows for // simpler management of permissions (such as being able to manage granting the 'set fee percentage' action in // any Pool created by the same factory), while still making action identifiers unique among different factories // if the selectors match, preventing accidental errors. Authentication(bytes32(uint256(msg.sender))) BalancerPoolToken(name, symbol, vault) BasePoolAuthorization(owner) TemporarilyPausable(pauseWindowDuration, bufferPeriodDuration) { _require(tokens.length >= _MIN_TOKENS, Errors.MIN_TOKENS); _require(tokens.length <= _getMaxTokens(), Errors.MAX_TOKENS); // The Vault only requires the token list to be ordered for the Two Token Pools specialization. However, // to make the developer experience consistent, we are requiring this condition for all the native pools. // Also, since these Pools will register tokens only once, we can ensure the Pool tokens will follow the same // order. We rely on this property to make Pools simpler to write, as it lets us assume that the // order of token-specific parameters (such as token weights) will not change. InputHelpers.ensureArrayIsSorted(tokens); _setSwapFeePercentage(swapFeePercentage); bytes32 poolId = vault.registerPool(specialization); vault.registerTokens(poolId, tokens, assetManagers); // Set immutable state variables - these cannot be read from during construction _poolId = poolId; } // Getters / Setters function getPoolId() public view override returns (bytes32) { return _poolId; } function _getTotalTokens() internal view virtual returns (uint256); function _getMaxTokens() internal pure virtual returns (uint256); function _getMinimumBpt() internal pure virtual returns (uint256) { return _MINIMUM_BPT; } function getSwapFeePercentage() public view returns (uint256) { return _miscData.decodeUint64(_SWAP_FEE_PERCENTAGE_OFFSET); } function setSwapFeePercentage(uint256 swapFeePercentage) external virtual authenticate whenNotPaused { _setSwapFeePercentage(swapFeePercentage); } function _setSwapFeePercentage(uint256 swapFeePercentage) private { _require(swapFeePercentage >= _MIN_SWAP_FEE_PERCENTAGE, Errors.MIN_SWAP_FEE_PERCENTAGE); _require(swapFeePercentage <= _MAX_SWAP_FEE_PERCENTAGE, Errors.MAX_SWAP_FEE_PERCENTAGE); _miscData = _miscData.insertUint64(swapFeePercentage, _SWAP_FEE_PERCENTAGE_OFFSET); emit SwapFeePercentageChanged(swapFeePercentage); } function setAssetManagerPoolConfig(IERC20 token, bytes memory poolConfig) public virtual authenticate whenNotPaused { _setAssetManagerPoolConfig(token, poolConfig); } function _setAssetManagerPoolConfig(IERC20 token, bytes memory poolConfig) private { bytes32 poolId = getPoolId(); (, , , address assetManager) = getVault().getPoolTokenInfo(poolId, token); IAssetManager(assetManager).setConfig(poolId, poolConfig); } function setPaused(bool paused) external authenticate { _setPaused(paused); } function _isOwnerOnlyAction(bytes32 actionId) internal view virtual override returns (bool) { return (actionId == getActionId(this.setSwapFeePercentage.selector)) || (actionId == getActionId(this.setAssetManagerPoolConfig.selector)); } function _getMiscData() internal view returns (bytes32) { return _miscData; } /** * Inserts data into the least-significant 192 bits of the misc data storage slot. * Note that the remaining 64 bits are used for the swap fee percentage and cannot be overloaded. */ function _setMiscData(bytes32 newData) internal { _miscData = _miscData.insertBits192(newData, 0); } // Join / Exit Hooks modifier onlyVault(bytes32 poolId) { _require(msg.sender == address(getVault()), Errors.CALLER_NOT_VAULT); _require(poolId == getPoolId(), Errors.INVALID_POOL_ID); _; } function onJoinPool( bytes32 poolId, address sender, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, bytes memory userData ) public virtual override onlyVault(poolId) returns (uint256[] memory, uint256[] memory) { uint256[] memory scalingFactors = _scalingFactors(); if (totalSupply() == 0) { (uint256 bptAmountOut, uint256[] memory amountsIn) = _onInitializePool( poolId, sender, recipient, scalingFactors, userData ); // On initialization, we lock _getMinimumBpt() by minting it for the zero address. This BPT acts as a // minimum as it will never be burned, which reduces potential issues with rounding, and also prevents the // Pool from ever being fully drained. _require(bptAmountOut >= _getMinimumBpt(), Errors.MINIMUM_BPT); _mintPoolTokens(address(0), _getMinimumBpt()); _mintPoolTokens(recipient, bptAmountOut - _getMinimumBpt()); // amountsIn are amounts entering the Pool, so we round up. _downscaleUpArray(amountsIn, scalingFactors); return (amountsIn, new uint256[](_getTotalTokens())); } else { _upscaleArray(balances, scalingFactors); (uint256 bptAmountOut, uint256[] memory amountsIn, uint256[] memory dueProtocolFeeAmounts) = _onJoinPool( poolId, sender, recipient, balances, lastChangeBlock, protocolSwapFeePercentage, scalingFactors, userData ); // Note we no longer use `balances` after calling `_onJoinPool`, which may mutate it. _mintPoolTokens(recipient, bptAmountOut); // amountsIn are amounts entering the Pool, so we round up. _downscaleUpArray(amountsIn, scalingFactors); // dueProtocolFeeAmounts are amounts exiting the Pool, so we round down. _downscaleDownArray(dueProtocolFeeAmounts, scalingFactors); return (amountsIn, dueProtocolFeeAmounts); } } function onExitPool( bytes32 poolId, address sender, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, bytes memory userData ) public virtual override onlyVault(poolId) returns (uint256[] memory, uint256[] memory) { uint256[] memory scalingFactors = _scalingFactors(); _upscaleArray(balances, scalingFactors); (uint256 bptAmountIn, uint256[] memory amountsOut, uint256[] memory dueProtocolFeeAmounts) = _onExitPool( poolId, sender, recipient, balances, lastChangeBlock, protocolSwapFeePercentage, scalingFactors, userData ); // Note we no longer use `balances` after calling `_onExitPool`, which may mutate it. _burnPoolTokens(sender, bptAmountIn); // Both amountsOut and dueProtocolFeeAmounts are amounts exiting the Pool, so we round down. _downscaleDownArray(amountsOut, scalingFactors); _downscaleDownArray(dueProtocolFeeAmounts, scalingFactors); return (amountsOut, dueProtocolFeeAmounts); } // Query functions /** * @dev Returns the amount of BPT that would be granted to `recipient` if the `onJoinPool` hook were called by the * Vault with the same arguments, along with the number of tokens `sender` would have to supply. * * This function is not meant to be called directly, but rather from a helper contract that fetches current Vault * data, such as the protocol swap fee percentage and Pool balances. * * Like `IVault.queryBatchSwap`, this function is not view due to internal implementation details: the caller must * explicitly use eth_call instead of eth_sendTransaction. */ function queryJoin( bytes32 poolId, address sender, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, bytes memory userData ) external returns (uint256 bptOut, uint256[] memory amountsIn) { InputHelpers.ensureInputLengthMatch(balances.length, _getTotalTokens()); _queryAction( poolId, sender, recipient, balances, lastChangeBlock, protocolSwapFeePercentage, userData, _onJoinPool, _downscaleUpArray ); // The `return` opcode is executed directly inside `_queryAction`, so execution never reaches this statement, // and we don't need to return anything here - it just silences compiler warnings. return (bptOut, amountsIn); } /** * @dev Returns the amount of BPT that would be burned from `sender` if the `onExitPool` hook were called by the * Vault with the same arguments, along with the number of tokens `recipient` would receive. * * This function is not meant to be called directly, but rather from a helper contract that fetches current Vault * data, such as the protocol swap fee percentage and Pool balances. * * Like `IVault.queryBatchSwap`, this function is not view due to internal implementation details: the caller must * explicitly use eth_call instead of eth_sendTransaction. */ function queryExit( bytes32 poolId, address sender, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, bytes memory userData ) external returns (uint256 bptIn, uint256[] memory amountsOut) { InputHelpers.ensureInputLengthMatch(balances.length, _getTotalTokens()); _queryAction( poolId, sender, recipient, balances, lastChangeBlock, protocolSwapFeePercentage, userData, _onExitPool, _downscaleDownArray ); // The `return` opcode is executed directly inside `_queryAction`, so execution never reaches this statement, // and we don't need to return anything here - it just silences compiler warnings. return (bptIn, amountsOut); } // Internal hooks to be overridden by derived contracts - all token amounts (except BPT) in these interfaces are // upscaled. /** * @dev Called when the Pool is joined for the first time; that is, when the BPT total supply is zero. * * Returns the amount of BPT to mint, and the token amounts the Pool will receive in return. * * Minted BPT will be sent to `recipient`, except for _getMinimumBpt(), which will be deducted from this amount and * sent to the zero address instead. This will cause that BPT to remain forever locked there, preventing total BTP * from ever dropping below that value, and ensuring `_onInitializePool` can only be called once in the entire * Pool's lifetime. * * The tokens granted to the Pool will be transferred from `sender`. These amounts are considered upscaled and will * be downscaled (rounding up) before being returned to the Vault. */ function _onInitializePool( bytes32 poolId, address sender, address recipient, uint256[] memory scalingFactors, bytes memory userData ) internal virtual returns (uint256 bptAmountOut, uint256[] memory amountsIn); /** * @dev Called whenever the Pool is joined after the first initialization join (see `_onInitializePool`). * * Returns the amount of BPT to mint, the token amounts that the Pool will receive in return, and the number of * tokens to pay in protocol swap fees. * * Implementations of this function might choose to mutate the `balances` array to save gas (e.g. when * performing intermediate calculations, such as subtraction of due protocol fees). This can be done safely. * * Minted BPT will be sent to `recipient`. * * The tokens granted to the Pool will be transferred from `sender`. These amounts are considered upscaled and will * be downscaled (rounding up) before being returned to the Vault. * * Due protocol swap fees will be taken from the Pool's balance in the Vault (see `IBasePool.onJoinPool`). These * amounts are considered upscaled and will be downscaled (rounding down) before being returned to the Vault. */ function _onJoinPool( bytes32 poolId, address sender, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, uint256[] memory scalingFactors, bytes memory userData ) internal virtual returns ( uint256 bptAmountOut, uint256[] memory amountsIn, uint256[] memory dueProtocolFeeAmounts ); /** * @dev Called whenever the Pool is exited. * * Returns the amount of BPT to burn, the token amounts for each Pool token that the Pool will grant in return, and * the number of tokens to pay in protocol swap fees. * * Implementations of this function might choose to mutate the `balances` array to save gas (e.g. when * performing intermediate calculations, such as subtraction of due protocol fees). This can be done safely. * * BPT will be burnt from `sender`. * * The Pool will grant tokens to `recipient`. These amounts are considered upscaled and will be downscaled * (rounding down) before being returned to the Vault. * * Due protocol swap fees will be taken from the Pool's balance in the Vault (see `IBasePool.onExitPool`). These * amounts are considered upscaled and will be downscaled (rounding down) before being returned to the Vault. */ function _onExitPool( bytes32 poolId, address sender, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, uint256[] memory scalingFactors, bytes memory userData ) internal virtual returns ( uint256 bptAmountIn, uint256[] memory amountsOut, uint256[] memory dueProtocolFeeAmounts ); // Internal functions /** * @dev Adds swap fee amount to `amount`, returning a higher value. */ function _addSwapFeeAmount(uint256 amount) internal view returns (uint256) { // This returns amount + fee amount, so we round up (favoring a higher fee amount). return amount.divUp(FixedPoint.ONE.sub(getSwapFeePercentage())); } /** * @dev Subtracts swap fee amount from `amount`, returning a lower value. */ function _subtractSwapFeeAmount(uint256 amount) internal view returns (uint256) { // This returns amount - fee amount, so we round up (favoring a higher fee amount). uint256 feeAmount = amount.mulUp(getSwapFeePercentage()); return amount.sub(feeAmount); } // Scaling /** * @dev Returns a scaling factor that, when multiplied to a token amount for `token`, normalizes its balance as if * it had 18 decimals. */ function _computeScalingFactor(IERC20 token) internal view returns (uint256) { if (address(token) == address(this)) { return FixedPoint.ONE; } // Tokens that don't implement the `decimals` method are not supported. uint256 tokenDecimals = ERC20(address(token)).decimals(); // Tokens with more than 18 decimals are not supported. uint256 decimalsDifference = Math.sub(18, tokenDecimals); return FixedPoint.ONE * 10**decimalsDifference; } /** * @dev Returns the scaling factor for one of the Pool's tokens. Reverts if `token` is not a token registered by the * Pool. * * All scaling factors are fixed-point values with 18 decimals, to allow for this function to be overridden by * derived contracts that need to apply further scaling, making these factors potentially non-integer. * * The largest 'base' scaling factor (i.e. in tokens with less than 18 decimals) is 10**18, which in fixed-point is * 10**36. This value can be multiplied with a 112 bit Vault balance with no overflow by a factor of ~1e7, making * even relatively 'large' factors safe to use. * * The 1e7 figure is the result of 2**256 / (1e18 * 1e18 * 2**112). */ function _scalingFactor(IERC20 token) internal view virtual returns (uint256); /** * @dev Same as `_scalingFactor()`, except for all registered tokens (in the same order as registered). The Vault * will always pass balances in this order when calling any of the Pool hooks. */ function _scalingFactors() internal view virtual returns (uint256[] memory); function getScalingFactors() external view returns (uint256[] memory) { return _scalingFactors(); } /** * @dev Applies `scalingFactor` to `amount`, resulting in a larger or equal value depending on whether it needed * scaling or not. */ function _upscale(uint256 amount, uint256 scalingFactor) internal pure returns (uint256) { // Upscale rounding wouldn't necessarily always go in the same direction: in a swap for example the balance of // token in should be rounded up, and that of token out rounded down. This is the only place where we round in // the same direction for all amounts, as the impact of this rounding is expected to be minimal (and there's no // rounding error unless `_scalingFactor()` is overriden). return FixedPoint.mulDown(amount, scalingFactor); } /** * @dev Same as `_upscale`, but for an entire array. This function does not return anything, but instead *mutates* * the `amounts` array. */ function _upscaleArray(uint256[] memory amounts, uint256[] memory scalingFactors) internal view { for (uint256 i = 0; i < _getTotalTokens(); ++i) { amounts[i] = FixedPoint.mulDown(amounts[i], scalingFactors[i]); } } /** * @dev Reverses the `scalingFactor` applied to `amount`, resulting in a smaller or equal value depending on * whether it needed scaling or not. The result is rounded down. */ function _downscaleDown(uint256 amount, uint256 scalingFactor) internal pure returns (uint256) { return FixedPoint.divDown(amount, scalingFactor); } /** * @dev Same as `_downscaleDown`, but for an entire array. This function does not return anything, but instead * *mutates* the `amounts` array. */ function _downscaleDownArray(uint256[] memory amounts, uint256[] memory scalingFactors) internal view { for (uint256 i = 0; i < _getTotalTokens(); ++i) { amounts[i] = FixedPoint.divDown(amounts[i], scalingFactors[i]); } } /** * @dev Reverses the `scalingFactor` applied to `amount`, resulting in a smaller or equal value depending on * whether it needed scaling or not. The result is rounded up. */ function _downscaleUp(uint256 amount, uint256 scalingFactor) internal pure returns (uint256) { return FixedPoint.divUp(amount, scalingFactor); } /** * @dev Same as `_downscaleUp`, but for an entire array. This function does not return anything, but instead * *mutates* the `amounts` array. */ function _downscaleUpArray(uint256[] memory amounts, uint256[] memory scalingFactors) internal view { for (uint256 i = 0; i < _getTotalTokens(); ++i) { amounts[i] = FixedPoint.divUp(amounts[i], scalingFactors[i]); } } function _getAuthorizer() internal view override returns (IAuthorizer) { // Access control management is delegated to the Vault's Authorizer. This lets Balancer Governance manage which // accounts can call permissioned functions: for example, to perform emergency pauses. // If the owner is delegated, then *all* permissioned functions, including `setSwapFeePercentage`, will be under // Governance control. return getVault().getAuthorizer(); } function _queryAction( bytes32 poolId, address sender, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, bytes memory userData, function(bytes32, address, address, uint256[] memory, uint256, uint256, uint256[] memory, bytes memory) internal returns (uint256, uint256[] memory, uint256[] memory) _action, function(uint256[] memory, uint256[] memory) internal view _downscaleArray ) private { // This uses the same technique used by the Vault in queryBatchSwap. Refer to that function for a detailed // explanation. if (msg.sender != address(this)) { // We perform an external call to ourselves, forwarding the same calldata. In this call, the else clause of // the preceding if statement will be executed instead. // solhint-disable-next-line avoid-low-level-calls (bool success, ) = address(this).call(msg.data); // solhint-disable-next-line no-inline-assembly assembly { // This call should always revert to decode the bpt and token amounts from the revert reason switch success case 0 { // Note we are manually writing the memory slot 0. We can safely overwrite whatever is // stored there as we take full control of the execution and then immediately return. // We copy the first 4 bytes to check if it matches with the expected signature, otherwise // there was another revert reason and we should forward it. returndatacopy(0, 0, 0x04) let error := and(mload(0), 0xffffffff00000000000000000000000000000000000000000000000000000000) // If the first 4 bytes don't match with the expected signature, we forward the revert reason. if eq(eq(error, 0x43adbafb00000000000000000000000000000000000000000000000000000000), 0) { returndatacopy(0, 0, returndatasize()) revert(0, returndatasize()) } // The returndata contains the signature, followed by the raw memory representation of the // `bptAmount` and `tokenAmounts` (array: length + data). We need to return an ABI-encoded // representation of these. // An ABI-encoded response will include one additional field to indicate the starting offset of // the `tokenAmounts` array. The `bptAmount` will be laid out in the first word of the // returndata. // // In returndata: // [ signature ][ bptAmount ][ tokenAmounts length ][ tokenAmounts values ] // [ 4 bytes ][ 32 bytes ][ 32 bytes ][ (32 * length) bytes ] // // We now need to return (ABI-encoded values): // [ bptAmount ][ tokeAmounts offset ][ tokenAmounts length ][ tokenAmounts values ] // [ 32 bytes ][ 32 bytes ][ 32 bytes ][ (32 * length) bytes ] // We copy 32 bytes for the `bptAmount` from returndata into memory. // Note that we skip the first 4 bytes for the error signature returndatacopy(0, 0x04, 32) // The offsets are 32-bytes long, so the array of `tokenAmounts` will start after // the initial 64 bytes. mstore(0x20, 64) // We now copy the raw memory array for the `tokenAmounts` from returndata into memory. // Since bpt amount and offset take up 64 bytes, we start copying at address 0x40. We also // skip the first 36 bytes from returndata, which correspond to the signature plus bpt amount. returndatacopy(0x40, 0x24, sub(returndatasize(), 36)) // We finally return the ABI-encoded uint256 and the array, which has a total length equal to // the size of returndata, plus the 32 bytes of the offset but without the 4 bytes of the // error signature. return(0, add(returndatasize(), 28)) } default { // This call should always revert, but we fail nonetheless if that didn't happen invalid() } } } else { uint256[] memory scalingFactors = _scalingFactors(); _upscaleArray(balances, scalingFactors); (uint256 bptAmount, uint256[] memory tokenAmounts, ) = _action( poolId, sender, recipient, balances, lastChangeBlock, protocolSwapFeePercentage, scalingFactors, userData ); _downscaleArray(tokenAmounts, scalingFactors); // solhint-disable-next-line no-inline-assembly assembly { // We will return a raw representation of `bptAmount` and `tokenAmounts` in memory, which is composed of // a 32-byte uint256, followed by a 32-byte for the array length, and finally the 32-byte uint256 values // Because revert expects a size in bytes, we multiply the array length (stored at `tokenAmounts`) by 32 let size := mul(mload(tokenAmounts), 32) // We store the `bptAmount` in the previous slot to the `tokenAmounts` array. We can make sure there // will be at least one available slot due to how the memory scratch space works. // We can safely overwrite whatever is stored in this slot as we will revert immediately after that. let start := sub(tokenAmounts, 0x20) mstore(start, bptAmount) // We send one extra value for the error signature "QueryError(uint256,uint256[])" which is 0x43adbafb // We use the previous slot to `bptAmount`. mstore(sub(start, 0x20), 0x0000000000000000000000000000000000000000000000000000000043adbafb) start := sub(start, 0x04) // When copying from `tokenAmounts` into returndata, we copy the additional 68 bytes to also return // the `bptAmount`, the array 's length, and the error signature. revert(start, add(size, 68)) } } } }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; import "./IBasePool.sol"; /** * @dev Pool contracts with the MinimalSwapInfo or TwoToken specialization settings should implement this interface. * * This is called by the Vault when a user calls `IVault.swap` or `IVault.batchSwap` to swap with this Pool. * Returns the number of tokens the Pool will grant to the user in a 'given in' swap, or that the user will grant * to the pool in a 'given out' swap. * * This can often be implemented by a `view` function, since many pricing algorithms don't need to track state * changes in swaps. However, contracts implementing this in non-view functions should check that the caller is * indeed the Vault. */ interface IMinimalSwapInfoPool is IBasePool { function onSwap( SwapRequest memory swapRequest, uint256 currentBalanceTokenIn, uint256 currentBalanceTokenOut ) external returns (uint256 amount); }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; import "@balancer-labs/v2-solidity-utils/contracts/helpers/BalancerErrors.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/ITemporarilyPausable.sol"; /** * @dev Allows for a contract to be paused during an initial period after deployment, disabling functionality. Can be * used as an emergency switch in case a security vulnerability or threat is identified. * * The contract can only be paused during the Pause Window, a period that starts at deployment. It can also be * unpaused and repaused any number of times during this period. This is intended to serve as a safety measure: it lets * system managers react quickly to potentially dangerous situations, knowing that this action is reversible if careful * analysis later determines there was a false alarm. * * If the contract is paused when the Pause Window finishes, it will remain in the paused state through an additional * Buffer Period, after which it will be automatically unpaused forever. This is to ensure there is always enough time * to react to an emergency, even if the threat is discovered shortly before the Pause Window expires. * * Note that since the contract can only be paused within the Pause Window, unpausing during the Buffer Period is * irreversible. */ abstract contract TemporarilyPausable is ITemporarilyPausable { // The Pause Window and Buffer Period are timestamp-based: they should not be relied upon for sub-minute accuracy. // solhint-disable not-rely-on-time uint256 private immutable _pauseWindowEndTime; uint256 private immutable _bufferPeriodEndTime; bool private _paused; constructor(uint256 pauseWindowDuration, uint256 bufferPeriodDuration) { uint256 pauseWindowEndTime = block.timestamp + pauseWindowDuration; _pauseWindowEndTime = pauseWindowEndTime; _bufferPeriodEndTime = pauseWindowEndTime + bufferPeriodDuration; } /** * @dev Reverts if the contract is paused. */ modifier whenNotPaused() { _ensureNotPaused(); _; } /** * @dev Returns the current contract pause status, as well as the end times of the Pause Window and Buffer * Period. */ function getPausedState() external view override returns ( bool paused, uint256 pauseWindowEndTime, uint256 bufferPeriodEndTime ) { paused = !_isNotPaused(); pauseWindowEndTime = _getPauseWindowEndTime(); bufferPeriodEndTime = _getBufferPeriodEndTime(); } /** * @dev Sets the pause state to `paused`. The contract can only be paused until the end of the Pause Window, and * unpaused until the end of the Buffer Period. * * Once the Buffer Period expires, this function reverts unconditionally. */ function _setPaused(bool paused) internal { if (paused) { _require(block.timestamp < _getPauseWindowEndTime(), Errors.PAUSE_WINDOW_EXPIRED); } else { _require(block.timestamp < _getBufferPeriodEndTime(), Errors.BUFFER_PERIOD_EXPIRED); } _paused = paused; emit PausedStateChanged(paused); } /** * @dev Reverts if the contract is paused. */ function _ensureNotPaused() internal view { _require(_isNotPaused(), Errors.PAUSED); } /** * @dev Returns true if the contract is unpaused. * * Once the Buffer Period expires, the gas cost of calling this function is reduced dramatically, as storage is no * longer accessed. */ function _isNotPaused() internal view returns (bool) { // After the Buffer Period, the (inexpensive) timestamp check short-circuits the storage access. return block.timestamp > _getBufferPeriodEndTime() || !_paused; } // These getters lead to reduced bytecode size by inlining the immutable variables in a single place. function _getPauseWindowEndTime() private view returns (uint256) { return _pauseWindowEndTime; } function _getBufferPeriodEndTime() private view returns (uint256) { return _bufferPeriodEndTime; } }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; /** * @dev Library for encoding and decoding values stored inside a 256 bit word. Typically used to pack multiple values in * a single storage slot, saving gas by performing less storage accesses. * * Each value is defined by its size and the least significant bit in the word, also known as offset. For example, two * 128 bit values may be encoded in a word by assigning one an offset of 0, and the other an offset of 128. * * We could use Solidity structs to pack values together in a single storage slot instead of relying on a custom and * error-prone library, but unfortunately Solidity only allows for structs to live in either storage, calldata or * memory. Because a memory struct uses not just memory but also a slot in the stack (to store its memory location), * using memory for word-sized values (i.e. of 256 bits or less) is strictly less gas performant, and doesn't even * prevent stack-too-deep issues. This is compounded by the fact that Balancer contracts typically are memory-intensive, * and the cost of accesing memory increases quadratically with the number of allocated words. Manual packing and * unpacking is therefore the preferred approach. */ library WordCodec { // Masks are values with the least significant N bits set. They can be used to extract an encoded value from a word, // or to insert a new one replacing the old. uint256 private constant _MASK_1 = 2**(1) - 1; uint256 private constant _MASK_5 = 2**(5) - 1; uint256 private constant _MASK_7 = 2**(7) - 1; uint256 private constant _MASK_10 = 2**(10) - 1; uint256 private constant _MASK_16 = 2**(16) - 1; uint256 private constant _MASK_22 = 2**(22) - 1; uint256 private constant _MASK_31 = 2**(31) - 1; uint256 private constant _MASK_32 = 2**(32) - 1; uint256 private constant _MASK_53 = 2**(53) - 1; uint256 private constant _MASK_64 = 2**(64) - 1; uint256 private constant _MASK_128 = 2**(128) - 1; uint256 private constant _MASK_192 = 2**(192) - 1; // Largest positive values that can be represented as N bits signed integers. int256 private constant _MAX_INT_22 = 2**(21) - 1; int256 private constant _MAX_INT_53 = 2**(52) - 1; // In-place insertion /** * @dev Inserts a boolean value shifted by an offset into a 256 bit word, replacing the old value. Returns the new * word. */ function insertBool( bytes32 word, bool value, uint256 offset ) internal pure returns (bytes32) { bytes32 clearedWord = bytes32(uint256(word) & ~(_MASK_1 << offset)); return clearedWord | bytes32(uint256(value ? 1 : 0) << offset); } // Unsigned /** * @dev Inserts a 5 bit unsigned integer shifted by an offset into a 256 bit word, replacing the old value. Returns * the new word. * * Assumes `value` only uses its least significant 5 bits, otherwise it may overwrite sibling bytes. */ function insertUint5( bytes32 word, uint256 value, uint256 offset ) internal pure returns (bytes32) { bytes32 clearedWord = bytes32(uint256(word) & ~(_MASK_5 << offset)); return clearedWord | bytes32(value << offset); } /** * @dev Inserts a 7 bit unsigned integer shifted by an offset into a 256 bit word, replacing the old value. Returns * the new word. * * Assumes `value` only uses its least significant 7 bits, otherwise it may overwrite sibling bytes. */ function insertUint7( bytes32 word, uint256 value, uint256 offset ) internal pure returns (bytes32) { bytes32 clearedWord = bytes32(uint256(word) & ~(_MASK_7 << offset)); return clearedWord | bytes32(value << offset); } /** * @dev Inserts a 10 bit unsigned integer shifted by an offset into a 256 bit word, replacing the old value. Returns * the new word. * * Assumes `value` only uses its least significant 10 bits, otherwise it may overwrite sibling bytes. */ function insertUint10( bytes32 word, uint256 value, uint256 offset ) internal pure returns (bytes32) { bytes32 clearedWord = bytes32(uint256(word) & ~(_MASK_10 << offset)); return clearedWord | bytes32(value << offset); } /** * @dev Inserts a 16 bit unsigned integer shifted by an offset into a 256 bit word, replacing the old value. * Returns the new word. * * Assumes `value` only uses its least significant 16 bits, otherwise it may overwrite sibling bytes. */ function insertUint16( bytes32 word, uint256 value, uint256 offset ) internal pure returns (bytes32) { bytes32 clearedWord = bytes32(uint256(word) & ~(_MASK_16 << offset)); return clearedWord | bytes32(value << offset); } /** * @dev Inserts a 31 bit unsigned integer shifted by an offset into a 256 bit word, replacing the old value. Returns * the new word. * * Assumes `value` can be represented using 31 bits. */ function insertUint31( bytes32 word, uint256 value, uint256 offset ) internal pure returns (bytes32) { bytes32 clearedWord = bytes32(uint256(word) & ~(_MASK_31 << offset)); return clearedWord | bytes32(value << offset); } /** * @dev Inserts a 32 bit unsigned integer shifted by an offset into a 256 bit word, replacing the old value. Returns * the new word. * * Assumes `value` only uses its least significant 32 bits, otherwise it may overwrite sibling bytes. */ function insertUint32( bytes32 word, uint256 value, uint256 offset ) internal pure returns (bytes32) { bytes32 clearedWord = bytes32(uint256(word) & ~(_MASK_32 << offset)); return clearedWord | bytes32(value << offset); } /** * @dev Inserts a 64 bit unsigned integer shifted by an offset into a 256 bit word, replacing the old value. Returns * the new word. * * Assumes `value` only uses its least significant 64 bits, otherwise it may overwrite sibling bytes. */ function insertUint64( bytes32 word, uint256 value, uint256 offset ) internal pure returns (bytes32) { bytes32 clearedWord = bytes32(uint256(word) & ~(_MASK_64 << offset)); return clearedWord | bytes32(value << offset); } // Signed /** * @dev Inserts a 22 bits signed integer shifted by an offset into a 256 bit word, replacing the old value. Returns * the new word. * * Assumes `value` can be represented using 22 bits. */ function insertInt22( bytes32 word, int256 value, uint256 offset ) internal pure returns (bytes32) { bytes32 clearedWord = bytes32(uint256(word) & ~(_MASK_22 << offset)); // Integer values need masking to remove the upper bits of negative values. return clearedWord | bytes32((uint256(value) & _MASK_22) << offset); } // Bytes /** * @dev Inserts 192 bit shifted by an offset into a 256 bit word, replacing the old value. Returns the new word. * * Assumes `value` can be represented using 192 bits. */ function insertBits192( bytes32 word, bytes32 value, uint256 offset ) internal pure returns (bytes32) { bytes32 clearedWord = bytes32(uint256(word) & ~(_MASK_192 << offset)); return clearedWord | bytes32((uint256(value) & _MASK_192) << offset); } // Encoding // Unsigned /** * @dev Encodes an unsigned integer shifted by an offset. This performs no size checks: it is up to the caller to * ensure that the values are bounded. * * The return value can be logically ORed with other encoded values to form a 256 bit word. */ function encodeUint(uint256 value, uint256 offset) internal pure returns (bytes32) { return bytes32(value << offset); } // Signed /** * @dev Encodes a 22 bits signed integer shifted by an offset. * * The return value can be logically ORed with other encoded values to form a 256 bit word. */ function encodeInt22(int256 value, uint256 offset) internal pure returns (bytes32) { // Integer values need masking to remove the upper bits of negative values. return bytes32((uint256(value) & _MASK_22) << offset); } /** * @dev Encodes a 53 bits signed integer shifted by an offset. * * The return value can be logically ORed with other encoded values to form a 256 bit word. */ function encodeInt53(int256 value, uint256 offset) internal pure returns (bytes32) { // Integer values need masking to remove the upper bits of negative values. return bytes32((uint256(value) & _MASK_53) << offset); } // Decoding /** * @dev Decodes and returns a boolean shifted by an offset from a 256 bit word. */ function decodeBool(bytes32 word, uint256 offset) internal pure returns (bool) { return (uint256(word >> offset) & _MASK_1) == 1; } // Unsigned /** * @dev Decodes and returns a 5 bit unsigned integer shifted by an offset from a 256 bit word. */ function decodeUint5(bytes32 word, uint256 offset) internal pure returns (uint256) { return uint256(word >> offset) & _MASK_5; } /** * @dev Decodes and returns a 7 bit unsigned integer shifted by an offset from a 256 bit word. */ function decodeUint7(bytes32 word, uint256 offset) internal pure returns (uint256) { return uint256(word >> offset) & _MASK_7; } /** * @dev Decodes and returns a 10 bit unsigned integer shifted by an offset from a 256 bit word. */ function decodeUint10(bytes32 word, uint256 offset) internal pure returns (uint256) { return uint256(word >> offset) & _MASK_10; } /** * @dev Decodes and returns a 16 bit unsigned integer shifted by an offset from a 256 bit word. */ function decodeUint16(bytes32 word, uint256 offset) internal pure returns (uint256) { return uint256(word >> offset) & _MASK_16; } /** * @dev Decodes and returns a 31 bit unsigned integer shifted by an offset from a 256 bit word. */ function decodeUint31(bytes32 word, uint256 offset) internal pure returns (uint256) { return uint256(word >> offset) & _MASK_31; } /** * @dev Decodes and returns a 32 bit unsigned integer shifted by an offset from a 256 bit word. */ function decodeUint32(bytes32 word, uint256 offset) internal pure returns (uint256) { return uint256(word >> offset) & _MASK_32; } /** * @dev Decodes and returns a 64 bit unsigned integer shifted by an offset from a 256 bit word. */ function decodeUint64(bytes32 word, uint256 offset) internal pure returns (uint256) { return uint256(word >> offset) & _MASK_64; } /** * @dev Decodes and returns a 128 bit unsigned integer shifted by an offset from a 256 bit word. */ function decodeUint128(bytes32 word, uint256 offset) internal pure returns (uint256) { return uint256(word >> offset) & _MASK_128; } // Signed /** * @dev Decodes and returns a 22 bits signed integer shifted by an offset from a 256 bit word. */ function decodeInt22(bytes32 word, uint256 offset) internal pure returns (int256) { int256 value = int256(uint256(word >> offset) & _MASK_22); // In case the decoded value is greater than the max positive integer that can be represented with 22 bits, // we know it was originally a negative integer. Therefore, we mask it to restore the sign in the 256 bit // representation. return value > _MAX_INT_22 ? (value | int256(~_MASK_22)) : value; } /** * @dev Decodes and returns a 53 bits signed integer shifted by an offset from a 256 bit word. */ function decodeInt53(bytes32 word, uint256 offset) internal pure returns (int256) { int256 value = int256(uint256(word >> offset) & _MASK_53); // In case the decoded value is greater than the max positive integer that can be represented with 53 bits, // we know it was originally a negative integer. Therefore, we mask it to restore the sign in the 256 bit // representation. return value > _MAX_INT_53 ? (value | int256(~_MASK_53)) : value; } }
// SPDX-License-Identifier: MIT pragma solidity ^0.7.0; import "../helpers/BalancerErrors.sol"; import "./IERC20.sol"; import "./SafeMath.sol"; /** * @dev Implementation of the {IERC20} interface. * * This implementation is agnostic to the way tokens are created. This means * that a supply mechanism has to be added in a derived contract using {_mint}. * For a generic mechanism see {ERC20PresetMinterPauser}. * * TIP: For a detailed writeup see our guide * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How * to implement supply mechanisms]. * * We have followed general OpenZeppelin guidelines: functions revert instead * of returning `false` on failure. This behavior is nonetheless conventional * and does not conflict with the expectations of ERC20 applications. * * Additionally, an {Approval} event is emitted on calls to {transferFrom}. * This allows applications to reconstruct the allowance for all accounts just * by listening to said events. Other implementations of the EIP may not emit * these events, as it isn't required by the specification. * * Finally, the non-standard {decreaseAllowance} and {increaseAllowance} * functions have been added to mitigate the well-known issues around setting * allowances. See {IERC20-approve}. */ contract ERC20 is IERC20 { using SafeMath for uint256; mapping(address => uint256) private _balances; mapping(address => mapping(address => uint256)) private _allowances; uint256 private _totalSupply; string private _name; string private _symbol; uint8 private _decimals; /** * @dev Sets the values for {name} and {symbol}, initializes {decimals} with * a default value of 18. * * To select a different value for {decimals}, use {_setupDecimals}. * * All three of these values are immutable: they can only be set once during * construction. */ constructor(string memory name_, string memory symbol_) { _name = name_; _symbol = symbol_; _decimals = 18; } /** * @dev Returns the name of the token. */ function name() public view returns (string memory) { return _name; } /** * @dev Returns the symbol of the token, usually a shorter version of the * name. */ function symbol() public view returns (string memory) { return _symbol; } /** * @dev Returns the number of decimals used to get its user representation. * For example, if `decimals` equals `2`, a balance of `505` tokens should * be displayed to a user as `5,05` (`505 / 10 ** 2`). * * Tokens usually opt for a value of 18, imitating the relationship between * Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is * called. * * NOTE: This information is only used for _display_ purposes: it in * no way affects any of the arithmetic of the contract, including * {IERC20-balanceOf} and {IERC20-transfer}. */ function decimals() public view returns (uint8) { return _decimals; } /** * @dev See {IERC20-totalSupply}. */ function totalSupply() public view override returns (uint256) { return _totalSupply; } /** * @dev See {IERC20-balanceOf}. */ function balanceOf(address account) public view override returns (uint256) { return _balances[account]; } /** * @dev See {IERC20-transfer}. * * Requirements: * * - `recipient` cannot be the zero address. * - the caller must have a balance of at least `amount`. */ function transfer(address recipient, uint256 amount) public virtual override returns (bool) { _transfer(msg.sender, recipient, amount); return true; } /** * @dev See {IERC20-allowance}. */ function allowance(address owner, address spender) public view virtual override returns (uint256) { return _allowances[owner][spender]; } /** * @dev See {IERC20-approve}. * * Requirements: * * - `spender` cannot be the zero address. */ function approve(address spender, uint256 amount) public virtual override returns (bool) { _approve(msg.sender, spender, amount); return true; } /** * @dev See {IERC20-transferFrom}. * * Emits an {Approval} event indicating the updated allowance. This is not * required by the EIP. See the note at the beginning of {ERC20}. * * Requirements: * * - `sender` and `recipient` cannot be the zero address. * - `sender` must have a balance of at least `amount`. * - the caller must have allowance for ``sender``'s tokens of at least * `amount`. */ function transferFrom( address sender, address recipient, uint256 amount ) public virtual override returns (bool) { _transfer(sender, recipient, amount); _approve( sender, msg.sender, _allowances[sender][msg.sender].sub(amount, Errors.ERC20_TRANSFER_EXCEEDS_ALLOWANCE) ); return true; } /** * @dev Atomically increases the allowance granted to `spender` by the caller. * * This is an alternative to {approve} that can be used as a mitigation for * problems described in {IERC20-approve}. * * Emits an {Approval} event indicating the updated allowance. * * Requirements: * * - `spender` cannot be the zero address. */ function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) { _approve(msg.sender, spender, _allowances[msg.sender][spender].add(addedValue)); return true; } /** * @dev Atomically decreases the allowance granted to `spender` by the caller. * * This is an alternative to {approve} that can be used as a mitigation for * problems described in {IERC20-approve}. * * Emits an {Approval} event indicating the updated allowance. * * Requirements: * * - `spender` cannot be the zero address. * - `spender` must have allowance for the caller of at least * `subtractedValue`. */ function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) { _approve( msg.sender, spender, _allowances[msg.sender][spender].sub(subtractedValue, Errors.ERC20_DECREASED_ALLOWANCE_BELOW_ZERO) ); return true; } /** * @dev Moves tokens `amount` from `sender` to `recipient`. * * This is internal function is equivalent to {transfer}, and can be used to * e.g. implement automatic token fees, slashing mechanisms, etc. * * Emits a {Transfer} event. * * Requirements: * * - `sender` cannot be the zero address. * - `recipient` cannot be the zero address. * - `sender` must have a balance of at least `amount`. */ function _transfer( address sender, address recipient, uint256 amount ) internal virtual { _require(sender != address(0), Errors.ERC20_TRANSFER_FROM_ZERO_ADDRESS); _require(recipient != address(0), Errors.ERC20_TRANSFER_TO_ZERO_ADDRESS); _beforeTokenTransfer(sender, recipient, amount); _balances[sender] = _balances[sender].sub(amount, Errors.ERC20_TRANSFER_EXCEEDS_BALANCE); _balances[recipient] = _balances[recipient].add(amount); emit Transfer(sender, recipient, amount); } /** @dev Creates `amount` tokens and assigns them to `account`, increasing * the total supply. * * Emits a {Transfer} event with `from` set to the zero address. * * Requirements: * * - `to` cannot be the zero address. */ function _mint(address account, uint256 amount) internal virtual { _beforeTokenTransfer(address(0), account, amount); _totalSupply = _totalSupply.add(amount); _balances[account] = _balances[account].add(amount); emit Transfer(address(0), account, amount); } /** * @dev Destroys `amount` tokens from `account`, reducing the * total supply. * * Emits a {Transfer} event with `to` set to the zero address. * * Requirements: * * - `account` cannot be the zero address. * - `account` must have at least `amount` tokens. */ function _burn(address account, uint256 amount) internal virtual { _require(account != address(0), Errors.ERC20_BURN_FROM_ZERO_ADDRESS); _beforeTokenTransfer(account, address(0), amount); _balances[account] = _balances[account].sub(amount, Errors.ERC20_BURN_EXCEEDS_ALLOWANCE); _totalSupply = _totalSupply.sub(amount); emit Transfer(account, address(0), amount); } /** * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens. * * This internal function is equivalent to `approve`, and can be used to * e.g. set automatic allowances for certain subsystems, etc. * * Emits an {Approval} event. * * Requirements: * * - `owner` cannot be the zero address. * - `spender` cannot be the zero address. */ function _approve( address owner, address spender, uint256 amount ) internal virtual { _allowances[owner][spender] = amount; emit Approval(owner, spender, amount); } /** * @dev Sets {decimals} to a value other than the default one of 18. * * WARNING: This function should only be called from the constructor. Most * applications that interact with token contracts will not expect * {decimals} to ever change, and may work incorrectly if it does. */ function _setupDecimals(uint8 decimals_) internal { _decimals = decimals_; } /** * @dev Hook that is called before any transfer of tokens. This includes * minting and burning. * * Calling conditions: * * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens * will be to transferred to `to`. * - when `from` is zero, `amount` tokens will be minted for `to`. * - when `to` is zero, `amount` of ``from``'s tokens will be burned. * - `from` and `to` are never both zero. * * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. */ function _beforeTokenTransfer( address from, address to, uint256 amount ) internal virtual {} }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma experimental ABIEncoderV2; import "@balancer-labs/v2-solidity-utils/contracts/openzeppelin/IERC20.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/ISignaturesValidator.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/ITemporarilyPausable.sol"; import "@balancer-labs/v2-solidity-utils/contracts/misc/IWETH.sol"; import "./IAsset.sol"; import "./IAuthorizer.sol"; import "./IFlashLoanRecipient.sol"; import "./IProtocolFeesCollector.sol"; pragma solidity ^0.7.0; /** * @dev Full external interface for the Vault core contract - no external or public methods exist in the contract that * don't override one of these declarations. */ interface IVault is ISignaturesValidator, ITemporarilyPausable { // Generalities about the Vault: // // - Whenever documentation refers to 'tokens', it strictly refers to ERC20-compliant token contracts. Tokens are // transferred out of the Vault by calling the `IERC20.transfer` function, and transferred in by calling // `IERC20.transferFrom`. In these cases, the sender must have previously allowed the Vault to use their tokens by // calling `IERC20.approve`. The only deviation from the ERC20 standard that is supported is functions not returning // a boolean value: in these scenarios, a non-reverting call is assumed to be successful. // // - All non-view functions in the Vault are non-reentrant: calling them while another one is mid-execution (e.g. // while execution control is transferred to a token contract during a swap) will result in a revert. View // functions can be called in a re-reentrant way, but doing so might cause them to return inconsistent results. // Contracts calling view functions in the Vault must make sure the Vault has not already been entered. // // - View functions revert if referring to either unregistered Pools, or unregistered tokens for registered Pools. // Authorizer // // Some system actions are permissioned, like setting and collecting protocol fees. This permissioning system exists // outside of the Vault in the Authorizer contract: the Vault simply calls the Authorizer to check if the caller // can perform a given action. /** * @dev Returns the Vault's Authorizer. */ function getAuthorizer() external view returns (IAuthorizer); /** * @dev Sets a new Authorizer for the Vault. The caller must be allowed by the current Authorizer to do this. * * Emits an `AuthorizerChanged` event. */ function setAuthorizer(IAuthorizer newAuthorizer) external; /** * @dev Emitted when a new authorizer is set by `setAuthorizer`. */ event AuthorizerChanged(IAuthorizer indexed newAuthorizer); // Relayers // // Additionally, it is possible for an account to perform certain actions on behalf of another one, using their // Vault ERC20 allowance and Internal Balance. These accounts are said to be 'relayers' for these Vault functions, // and are expected to be smart contracts with sound authentication mechanisms. For an account to be able to wield // this power, two things must occur: // - The Authorizer must grant the account the permission to be a relayer for the relevant Vault function. This // means that Balancer governance must approve each individual contract to act as a relayer for the intended // functions. // - Each user must approve the relayer to act on their behalf. // This double protection means users cannot be tricked into approving malicious relayers (because they will not // have been allowed by the Authorizer via governance), nor can malicious relayers approved by a compromised // Authorizer or governance drain user funds, since they would also need to be approved by each individual user. /** * @dev Returns true if `user` has approved `relayer` to act as a relayer for them. */ function hasApprovedRelayer(address user, address relayer) external view returns (bool); /** * @dev Allows `relayer` to act as a relayer for `sender` if `approved` is true, and disallows it otherwise. * * Emits a `RelayerApprovalChanged` event. */ function setRelayerApproval( address sender, address relayer, bool approved ) external; /** * @dev Emitted every time a relayer is approved or disapproved by `setRelayerApproval`. */ event RelayerApprovalChanged(address indexed relayer, address indexed sender, bool approved); // Internal Balance // // Users can deposit tokens into the Vault, where they are allocated to their Internal Balance, and later // transferred or withdrawn. It can also be used as a source of tokens when joining Pools, as a destination // when exiting them, and as either when performing swaps. This usage of Internal Balance results in greatly reduced // gas costs when compared to relying on plain ERC20 transfers, leading to large savings for frequent users. // // Internal Balance management features batching, which means a single contract call can be used to perform multiple // operations of different kinds, with different senders and recipients, at once. /** * @dev Returns `user`'s Internal Balance for a set of tokens. */ function getInternalBalance(address user, IERC20[] memory tokens) external view returns (uint256[] memory); /** * @dev Performs a set of user balance operations, which involve Internal Balance (deposit, withdraw or transfer) * and plain ERC20 transfers using the Vault's allowance. This last feature is particularly useful for relayers, as * it lets integrators reuse a user's Vault allowance. * * For each operation, if the caller is not `sender`, it must be an authorized relayer for them. */ function manageUserBalance(UserBalanceOp[] memory ops) external payable; /** * @dev Data for `manageUserBalance` operations, which include the possibility for ETH to be sent and received without manual WETH wrapping or unwrapping. */ struct UserBalanceOp { UserBalanceOpKind kind; IAsset asset; uint256 amount; address sender; address payable recipient; } // There are four possible operations in `manageUserBalance`: // // - DEPOSIT_INTERNAL // Increases the Internal Balance of the `recipient` account by transferring tokens from the corresponding // `sender`. The sender must have allowed the Vault to use their tokens via `IERC20.approve()`. // // ETH can be used by passing the ETH sentinel value as the asset and forwarding ETH in the call: it will be wrapped // and deposited as WETH. Any ETH amount remaining will be sent back to the caller (not the sender, which is // relevant for relayers). // // Emits an `InternalBalanceChanged` event. // // // - WITHDRAW_INTERNAL // Decreases the Internal Balance of the `sender` account by transferring tokens to the `recipient`. // // ETH can be used by passing the ETH sentinel value as the asset. This will deduct WETH instead, unwrap it and send // it to the recipient as ETH. // // Emits an `InternalBalanceChanged` event. // // // - TRANSFER_INTERNAL // Transfers tokens from the Internal Balance of the `sender` account to the Internal Balance of `recipient`. // // Reverts if the ETH sentinel value is passed. // // Emits an `InternalBalanceChanged` event. // // // - TRANSFER_EXTERNAL // Transfers tokens from `sender` to `recipient`, using the Vault's ERC20 allowance. This is typically used by // relayers, as it lets them reuse a user's Vault allowance. // // Reverts if the ETH sentinel value is passed. // // Emits an `ExternalBalanceTransfer` event. enum UserBalanceOpKind { DEPOSIT_INTERNAL, WITHDRAW_INTERNAL, TRANSFER_INTERNAL, TRANSFER_EXTERNAL } /** * @dev Emitted when a user's Internal Balance changes, either from calls to `manageUserBalance`, or through * interacting with Pools using Internal Balance. * * Because Internal Balance works exclusively with ERC20 tokens, ETH deposits and withdrawals will use the WETH * address. */ event InternalBalanceChanged(address indexed user, IERC20 indexed token, int256 delta); /** * @dev Emitted when a user's Vault ERC20 allowance is used by the Vault to transfer tokens to an external account. */ event ExternalBalanceTransfer(IERC20 indexed token, address indexed sender, address recipient, uint256 amount); // Pools // // There are three specialization settings for Pools, which allow for cheaper swaps at the cost of reduced // functionality: // // - General: no specialization, suited for all Pools. IGeneralPool is used for swap request callbacks, passing the // balance of all tokens in the Pool. These Pools have the largest swap costs (because of the extra storage reads), // which increase with the number of registered tokens. // // - Minimal Swap Info: IMinimalSwapInfoPool is used instead of IGeneralPool, which saves gas by only passing the // balance of the two tokens involved in the swap. This is suitable for some pricing algorithms, like the weighted // constant product one popularized by Balancer V1. Swap costs are smaller compared to general Pools, and are // independent of the number of registered tokens. // // - Two Token: only allows two tokens to be registered. This achieves the lowest possible swap gas cost. Like // minimal swap info Pools, these are called via IMinimalSwapInfoPool. enum PoolSpecialization { GENERAL, MINIMAL_SWAP_INFO, TWO_TOKEN } /** * @dev Registers the caller account as a Pool with a given specialization setting. Returns the Pool's ID, which * is used in all Pool-related functions. Pools cannot be deregistered, nor can the Pool's specialization be * changed. * * The caller is expected to be a smart contract that implements either `IGeneralPool` or `IMinimalSwapInfoPool`, * depending on the chosen specialization setting. This contract is known as the Pool's contract. * * Note that the same contract may register itself as multiple Pools with unique Pool IDs, or in other words, * multiple Pools may share the same contract. * * Emits a `PoolRegistered` event. */ function registerPool(PoolSpecialization specialization) external returns (bytes32); /** * @dev Emitted when a Pool is registered by calling `registerPool`. */ event PoolRegistered(bytes32 indexed poolId, address indexed poolAddress, PoolSpecialization specialization); /** * @dev Returns a Pool's contract address and specialization setting. */ function getPool(bytes32 poolId) external view returns (address, PoolSpecialization); /** * @dev Registers `tokens` for the `poolId` Pool. Must be called by the Pool's contract. * * Pools can only interact with tokens they have registered. Users join a Pool by transferring registered tokens, * exit by receiving registered tokens, and can only swap registered tokens. * * Each token can only be registered once. For Pools with the Two Token specialization, `tokens` must have a length * of two, that is, both tokens must be registered in the same `registerTokens` call, and they must be sorted in * ascending order. * * The `tokens` and `assetManagers` arrays must have the same length, and each entry in these indicates the Asset * Manager for the corresponding token. Asset Managers can manage a Pool's tokens via `managePoolBalance`, * depositing and withdrawing them directly, and can even set their balance to arbitrary amounts. They are therefore * expected to be highly secured smart contracts with sound design principles, and the decision to register an * Asset Manager should not be made lightly. * * Pools can choose not to assign an Asset Manager to a given token by passing in the zero address. Once an Asset * Manager is set, it cannot be changed except by deregistering the associated token and registering again with a * different Asset Manager. * * Emits a `TokensRegistered` event. */ function registerTokens( bytes32 poolId, IERC20[] memory tokens, address[] memory assetManagers ) external; /** * @dev Emitted when a Pool registers tokens by calling `registerTokens`. */ event TokensRegistered(bytes32 indexed poolId, IERC20[] tokens, address[] assetManagers); /** * @dev Deregisters `tokens` for the `poolId` Pool. Must be called by the Pool's contract. * * Only registered tokens (via `registerTokens`) can be deregistered. Additionally, they must have zero total * balance. For Pools with the Two Token specialization, `tokens` must have a length of two, that is, both tokens * must be deregistered in the same `deregisterTokens` call. * * A deregistered token can be re-registered later on, possibly with a different Asset Manager. * * Emits a `TokensDeregistered` event. */ function deregisterTokens(bytes32 poolId, IERC20[] memory tokens) external; /** * @dev Emitted when a Pool deregisters tokens by calling `deregisterTokens`. */ event TokensDeregistered(bytes32 indexed poolId, IERC20[] tokens); /** * @dev Returns detailed information for a Pool's registered token. * * `cash` is the number of tokens the Vault currently holds for the Pool. `managed` is the number of tokens * withdrawn and held outside the Vault by the Pool's token Asset Manager. The Pool's total balance for `token` * equals the sum of `cash` and `managed`. * * Internally, `cash` and `managed` are stored using 112 bits. No action can ever cause a Pool's token `cash`, * `managed` or `total` balance to be greater than 2^112 - 1. * * `lastChangeBlock` is the number of the block in which `token`'s total balance was last modified (via either a * join, exit, swap, or Asset Manager update). This value is useful to avoid so-called 'sandwich attacks', for * example when developing price oracles. A change of zero (e.g. caused by a swap with amount zero) is considered a * change for this purpose, and will update `lastChangeBlock`. * * `assetManager` is the Pool's token Asset Manager. */ function getPoolTokenInfo(bytes32 poolId, IERC20 token) external view returns ( uint256 cash, uint256 managed, uint256 lastChangeBlock, address assetManager ); /** * @dev Returns a Pool's registered tokens, the total balance for each, and the latest block when *any* of * the tokens' `balances` changed. * * The order of the `tokens` array is the same order that will be used in `joinPool`, `exitPool`, as well as in all * Pool hooks (where applicable). Calls to `registerTokens` and `deregisterTokens` may change this order. * * If a Pool only registers tokens once, and these are sorted in ascending order, they will be stored in the same * order as passed to `registerTokens`. * * Total balances include both tokens held by the Vault and those withdrawn by the Pool's Asset Managers. These are * the amounts used by joins, exits and swaps. For a detailed breakdown of token balances, use `getPoolTokenInfo` * instead. */ function getPoolTokens(bytes32 poolId) external view returns ( IERC20[] memory tokens, uint256[] memory balances, uint256 lastChangeBlock ); /** * @dev Called by users to join a Pool, which transfers tokens from `sender` into the Pool's balance. This will * trigger custom Pool behavior, which will typically grant something in return to `recipient` - often tokenized * Pool shares. * * If the caller is not `sender`, it must be an authorized relayer for them. * * The `assets` and `maxAmountsIn` arrays must have the same length, and each entry indicates the maximum amount * to send for each asset. The amounts to send are decided by the Pool and not the Vault: it just enforces * these maximums. * * If joining a Pool that holds WETH, it is possible to send ETH directly: the Vault will do the wrapping. To enable * this mechanism, the IAsset sentinel value (the zero address) must be passed in the `assets` array instead of the * WETH address. Note that it is not possible to combine ETH and WETH in the same join. Any excess ETH will be sent * back to the caller (not the sender, which is important for relayers). * * `assets` must have the same length and order as the array returned by `getPoolTokens`. This prevents issues when * interacting with Pools that register and deregister tokens frequently. If sending ETH however, the array must be * sorted *before* replacing the WETH address with the ETH sentinel value (the zero address), which means the final * `assets` array might not be sorted. Pools with no registered tokens cannot be joined. * * If `fromInternalBalance` is true, the caller's Internal Balance will be preferred: ERC20 transfers will only * be made for the difference between the requested amount and Internal Balance (if any). Note that ETH cannot be * withdrawn from Internal Balance: attempting to do so will trigger a revert. * * This causes the Vault to call the `IBasePool.onJoinPool` hook on the Pool's contract, where Pools implement * their own custom logic. This typically requires additional information from the user (such as the expected number * of Pool shares). This can be encoded in the `userData` argument, which is ignored by the Vault and passed * directly to the Pool's contract, as is `recipient`. * * Emits a `PoolBalanceChanged` event. */ function joinPool( bytes32 poolId, address sender, address recipient, JoinPoolRequest memory request ) external payable; struct JoinPoolRequest { IAsset[] assets; uint256[] maxAmountsIn; bytes userData; bool fromInternalBalance; } /** * @dev Called by users to exit a Pool, which transfers tokens from the Pool's balance to `recipient`. This will * trigger custom Pool behavior, which will typically ask for something in return from `sender` - often tokenized * Pool shares. The amount of tokens that can be withdrawn is limited by the Pool's `cash` balance (see * `getPoolTokenInfo`). * * If the caller is not `sender`, it must be an authorized relayer for them. * * The `tokens` and `minAmountsOut` arrays must have the same length, and each entry in these indicates the minimum * token amount to receive for each token contract. The amounts to send are decided by the Pool and not the Vault: * it just enforces these minimums. * * If exiting a Pool that holds WETH, it is possible to receive ETH directly: the Vault will do the unwrapping. To * enable this mechanism, the IAsset sentinel value (the zero address) must be passed in the `assets` array instead * of the WETH address. Note that it is not possible to combine ETH and WETH in the same exit. * * `assets` must have the same length and order as the array returned by `getPoolTokens`. This prevents issues when * interacting with Pools that register and deregister tokens frequently. If receiving ETH however, the array must * be sorted *before* replacing the WETH address with the ETH sentinel value (the zero address), which means the * final `assets` array might not be sorted. Pools with no registered tokens cannot be exited. * * If `toInternalBalance` is true, the tokens will be deposited to `recipient`'s Internal Balance. Otherwise, * an ERC20 transfer will be performed. Note that ETH cannot be deposited to Internal Balance: attempting to * do so will trigger a revert. * * `minAmountsOut` is the minimum amount of tokens the user expects to get out of the Pool, for each token in the * `tokens` array. This array must match the Pool's registered tokens. * * This causes the Vault to call the `IBasePool.onExitPool` hook on the Pool's contract, where Pools implement * their own custom logic. This typically requires additional information from the user (such as the expected number * of Pool shares to return). This can be encoded in the `userData` argument, which is ignored by the Vault and * passed directly to the Pool's contract. * * Emits a `PoolBalanceChanged` event. */ function exitPool( bytes32 poolId, address sender, address payable recipient, ExitPoolRequest memory request ) external; struct ExitPoolRequest { IAsset[] assets; uint256[] minAmountsOut; bytes userData; bool toInternalBalance; } /** * @dev Emitted when a user joins or exits a Pool by calling `joinPool` or `exitPool`, respectively. */ event PoolBalanceChanged( bytes32 indexed poolId, address indexed liquidityProvider, IERC20[] tokens, int256[] deltas, uint256[] protocolFeeAmounts ); enum PoolBalanceChangeKind { JOIN, EXIT } // Swaps // // Users can swap tokens with Pools by calling the `swap` and `batchSwap` functions. To do this, // they need not trust Pool contracts in any way: all security checks are made by the Vault. They must however be // aware of the Pools' pricing algorithms in order to estimate the prices Pools will quote. // // The `swap` function executes a single swap, while `batchSwap` can perform multiple swaps in sequence. // In each individual swap, tokens of one kind are sent from the sender to the Pool (this is the 'token in'), // and tokens of another kind are sent from the Pool to the recipient in exchange (this is the 'token out'). // More complex swaps, such as one token in to multiple tokens out can be achieved by batching together // individual swaps. // // There are two swap kinds: // - 'given in' swaps, where the amount of tokens in (sent to the Pool) is known, and the Pool determines (via the // `onSwap` hook) the amount of tokens out (to send to the recipient). // - 'given out' swaps, where the amount of tokens out (received from the Pool) is known, and the Pool determines // (via the `onSwap` hook) the amount of tokens in (to receive from the sender). // // Additionally, it is possible to chain swaps using a placeholder input amount, which the Vault replaces with // the calculated output of the previous swap. If the previous swap was 'given in', this will be the calculated // tokenOut amount. If the previous swap was 'given out', it will use the calculated tokenIn amount. These extended // swaps are known as 'multihop' swaps, since they 'hop' through a number of intermediate tokens before arriving at // the final intended token. // // In all cases, tokens are only transferred in and out of the Vault (or withdrawn from and deposited into Internal // Balance) after all individual swaps have been completed, and the net token balance change computed. This makes // certain swap patterns, such as multihops, or swaps that interact with the same token pair in multiple Pools, cost // much less gas than they would otherwise. // // It also means that under certain conditions it is possible to perform arbitrage by swapping with multiple // Pools in a way that results in net token movement out of the Vault (profit), with no tokens being sent in (only // updating the Pool's internal accounting). // // To protect users from front-running or the market changing rapidly, they supply a list of 'limits' for each token // involved in the swap, where either the maximum number of tokens to send (by passing a positive value) or the // minimum amount of tokens to receive (by passing a negative value) is specified. // // Additionally, a 'deadline' timestamp can also be provided, forcing the swap to fail if it occurs after // this point in time (e.g. if the transaction failed to be included in a block promptly). // // If interacting with Pools that hold WETH, it is possible to both send and receive ETH directly: the Vault will do // the wrapping and unwrapping. To enable this mechanism, the IAsset sentinel value (the zero address) must be // passed in the `assets` array instead of the WETH address. Note that it is possible to combine ETH and WETH in the // same swap. Any excess ETH will be sent back to the caller (not the sender, which is relevant for relayers). // // Finally, Internal Balance can be used when either sending or receiving tokens. enum SwapKind { GIVEN_IN, GIVEN_OUT } /** * @dev Performs a swap with a single Pool. * * If the swap is 'given in' (the number of tokens to send to the Pool is known), it returns the amount of tokens * taken from the Pool, which must be greater than or equal to `limit`. * * If the swap is 'given out' (the number of tokens to take from the Pool is known), it returns the amount of tokens * sent to the Pool, which must be less than or equal to `limit`. * * Internal Balance usage and the recipient are determined by the `funds` struct. * * Emits a `Swap` event. */ function swap( SingleSwap memory singleSwap, FundManagement memory funds, uint256 limit, uint256 deadline ) external payable returns (uint256); /** * @dev Data for a single swap executed by `swap`. `amount` is either `amountIn` or `amountOut` depending on * the `kind` value. * * `assetIn` and `assetOut` are either token addresses, or the IAsset sentinel value for ETH (the zero address). * Note that Pools never interact with ETH directly: it will be wrapped to or unwrapped from WETH by the Vault. * * The `userData` field is ignored by the Vault, but forwarded to the Pool in the `onSwap` hook, and may be * used to extend swap behavior. */ struct SingleSwap { bytes32 poolId; SwapKind kind; IAsset assetIn; IAsset assetOut; uint256 amount; bytes userData; } /** * @dev Performs a series of swaps with one or multiple Pools. In each individual swap, the caller determines either * the amount of tokens sent to or received from the Pool, depending on the `kind` value. * * Returns an array with the net Vault asset balance deltas. Positive amounts represent tokens (or ETH) sent to the * Vault, and negative amounts represent tokens (or ETH) sent by the Vault. Each delta corresponds to the asset at * the same index in the `assets` array. * * Swaps are executed sequentially, in the order specified by the `swaps` array. Each array element describes a * Pool, the token to be sent to this Pool, the token to receive from it, and an amount that is either `amountIn` or * `amountOut` depending on the swap kind. * * Multihop swaps can be executed by passing an `amount` value of zero for a swap. This will cause the amount in/out * of the previous swap to be used as the amount in for the current one. In a 'given in' swap, 'tokenIn' must equal * the previous swap's `tokenOut`. For a 'given out' swap, `tokenOut` must equal the previous swap's `tokenIn`. * * The `assets` array contains the addresses of all assets involved in the swaps. These are either token addresses, * or the IAsset sentinel value for ETH (the zero address). Each entry in the `swaps` array specifies tokens in and * out by referencing an index in `assets`. Note that Pools never interact with ETH directly: it will be wrapped to * or unwrapped from WETH by the Vault. * * Internal Balance usage, sender, and recipient are determined by the `funds` struct. The `limits` array specifies * the minimum or maximum amount of each token the vault is allowed to transfer. * * `batchSwap` can be used to make a single swap, like `swap` does, but doing so requires more gas than the * equivalent `swap` call. * * Emits `Swap` events. */ function batchSwap( SwapKind kind, BatchSwapStep[] memory swaps, IAsset[] memory assets, FundManagement memory funds, int256[] memory limits, uint256 deadline ) external payable returns (int256[] memory); /** * @dev Data for each individual swap executed by `batchSwap`. The asset in and out fields are indexes into the * `assets` array passed to that function, and ETH assets are converted to WETH. * * If `amount` is zero, the multihop mechanism is used to determine the actual amount based on the amount in/out * from the previous swap, depending on the swap kind. * * The `userData` field is ignored by the Vault, but forwarded to the Pool in the `onSwap` hook, and may be * used to extend swap behavior. */ struct BatchSwapStep { bytes32 poolId; uint256 assetInIndex; uint256 assetOutIndex; uint256 amount; bytes userData; } /** * @dev Emitted for each individual swap performed by `swap` or `batchSwap`. */ event Swap( bytes32 indexed poolId, IERC20 indexed tokenIn, IERC20 indexed tokenOut, uint256 amountIn, uint256 amountOut ); /** * @dev All tokens in a swap are either sent from the `sender` account to the Vault, or from the Vault to the * `recipient` account. * * If the caller is not `sender`, it must be an authorized relayer for them. * * If `fromInternalBalance` is true, the `sender`'s Internal Balance will be preferred, performing an ERC20 * transfer for the difference between the requested amount and the User's Internal Balance (if any). The `sender` * must have allowed the Vault to use their tokens via `IERC20.approve()`. This matches the behavior of * `joinPool`. * * If `toInternalBalance` is true, tokens will be deposited to `recipient`'s internal balance instead of * transferred. This matches the behavior of `exitPool`. * * Note that ETH cannot be deposited to or withdrawn from Internal Balance: attempting to do so will trigger a * revert. */ struct FundManagement { address sender; bool fromInternalBalance; address payable recipient; bool toInternalBalance; } /** * @dev Simulates a call to `batchSwap`, returning an array of Vault asset deltas. Calls to `swap` cannot be * simulated directly, but an equivalent `batchSwap` call can and will yield the exact same result. * * Each element in the array corresponds to the asset at the same index, and indicates the number of tokens (or ETH) * the Vault would take from the sender (if positive) or send to the recipient (if negative). The arguments it * receives are the same that an equivalent `batchSwap` call would receive. * * Unlike `batchSwap`, this function performs no checks on the sender or recipient field in the `funds` struct. * This makes it suitable to be called by off-chain applications via eth_call without needing to hold tokens, * approve them for the Vault, or even know a user's address. * * Note that this function is not 'view' (due to implementation details): the client code must explicitly execute * eth_call instead of eth_sendTransaction. */ function queryBatchSwap( SwapKind kind, BatchSwapStep[] memory swaps, IAsset[] memory assets, FundManagement memory funds ) external returns (int256[] memory assetDeltas); // Flash Loans /** * @dev Performs a 'flash loan', sending tokens to `recipient`, executing the `receiveFlashLoan` hook on it, * and then reverting unless the tokens plus a proportional protocol fee have been returned. * * The `tokens` and `amounts` arrays must have the same length, and each entry in these indicates the loan amount * for each token contract. `tokens` must be sorted in ascending order. * * The 'userData' field is ignored by the Vault, and forwarded as-is to `recipient` as part of the * `receiveFlashLoan` call. * * Emits `FlashLoan` events. */ function flashLoan( IFlashLoanRecipient recipient, IERC20[] memory tokens, uint256[] memory amounts, bytes memory userData ) external; /** * @dev Emitted for each individual flash loan performed by `flashLoan`. */ event FlashLoan(IFlashLoanRecipient indexed recipient, IERC20 indexed token, uint256 amount, uint256 feeAmount); // Asset Management // // Each token registered for a Pool can be assigned an Asset Manager, which is able to freely withdraw the Pool's // tokens from the Vault, deposit them, or assign arbitrary values to its `managed` balance (see // `getPoolTokenInfo`). This makes them extremely powerful and dangerous. Even if an Asset Manager only directly // controls one of the tokens in a Pool, a malicious manager could set that token's balance to manipulate the // prices of the other tokens, and then drain the Pool with swaps. The risk of using Asset Managers is therefore // not constrained to the tokens they are managing, but extends to the entire Pool's holdings. // // However, a properly designed Asset Manager smart contract can be safely used for the Pool's benefit, // for example by lending unused tokens out for interest, or using them to participate in voting protocols. // // This concept is unrelated to the IAsset interface. /** * @dev Performs a set of Pool balance operations, which may be either withdrawals, deposits or updates. * * Pool Balance management features batching, which means a single contract call can be used to perform multiple * operations of different kinds, with different Pools and tokens, at once. * * For each operation, the caller must be registered as the Asset Manager for `token` in `poolId`. */ function managePoolBalance(PoolBalanceOp[] memory ops) external; struct PoolBalanceOp { PoolBalanceOpKind kind; bytes32 poolId; IERC20 token; uint256 amount; } /** * Withdrawals decrease the Pool's cash, but increase its managed balance, leaving the total balance unchanged. * * Deposits increase the Pool's cash, but decrease its managed balance, leaving the total balance unchanged. * * Updates don't affect the Pool's cash balance, but because the managed balance changes, it does alter the total. * The external amount can be either increased or decreased by this call (i.e., reporting a gain or a loss). */ enum PoolBalanceOpKind { WITHDRAW, DEPOSIT, UPDATE } /** * @dev Emitted when a Pool's token Asset Manager alters its balance via `managePoolBalance`. */ event PoolBalanceManaged( bytes32 indexed poolId, address indexed assetManager, IERC20 indexed token, int256 cashDelta, int256 managedDelta ); // Protocol Fees // // Some operations cause the Vault to collect tokens in the form of protocol fees, which can then be withdrawn by // permissioned accounts. // // There are two kinds of protocol fees: // // - flash loan fees: charged on all flash loans, as a percentage of the amounts lent. // // - swap fees: a percentage of the fees charged by Pools when performing swaps. For a number of reasons, including // swap gas costs and interface simplicity, protocol swap fees are not charged on each individual swap. Rather, // Pools are expected to keep track of how much they have charged in swap fees, and pay any outstanding debts to the // Vault when they are joined or exited. This prevents users from joining a Pool with unpaid debt, as well as // exiting a Pool in debt without first paying their share. /** * @dev Returns the current protocol fee module. */ function getProtocolFeesCollector() external view returns (IProtocolFeesCollector); /** * @dev Safety mechanism to pause most Vault operations in the event of an emergency - typically detection of an * error in some part of the system. * * The Vault can only be paused during an initial time period, after which pausing is forever disabled. * * While the contract is paused, the following features are disabled: * - depositing and transferring internal balance * - transferring external balance (using the Vault's allowance) * - swaps * - joining Pools * - Asset Manager interactions * * Internal Balance can still be withdrawn, and Pools exited. */ function setPaused(bool paused) external; /** * @dev Returns the Vault's WETH instance. */ function WETH() external view returns (IWETH); // solhint-disable-previous-line func-name-mixedcase }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; import "./IVault.sol"; import "./IPoolSwapStructs.sol"; /** * @dev Interface for adding and removing liquidity that all Pool contracts should implement. Note that this is not * the complete Pool contract interface, as it is missing the swap hooks. Pool contracts should also inherit from * either IGeneralPool or IMinimalSwapInfoPool */ interface IBasePool is IPoolSwapStructs { /** * @dev Called by the Vault when a user calls `IVault.joinPool` to add liquidity to this Pool. Returns how many of * each registered token the user should provide, as well as the amount of protocol fees the Pool owes to the Vault. * The Vault will then take tokens from `sender` and add them to the Pool's balances, as well as collect * the reported amount in protocol fees, which the pool should calculate based on `protocolSwapFeePercentage`. * * Protocol fees are reported and charged on join events so that the Pool is free of debt whenever new users join. * * `sender` is the account performing the join (from which tokens will be withdrawn), and `recipient` is the account * designated to receive any benefits (typically pool shares). `balances` contains the total balances * for each token the Pool registered in the Vault, in the same order that `IVault.getPoolTokens` would return. * * `lastChangeBlock` is the last block in which *any* of the Pool's registered tokens last changed its total * balance. * * `userData` contains any pool-specific instructions needed to perform the calculations, such as the type of * join (e.g., proportional given an amount of pool shares, single-asset, multi-asset, etc.) * * Contracts implementing this function should check that the caller is indeed the Vault before performing any * state-changing operations, such as minting pool shares. */ function onJoinPool( bytes32 poolId, address sender, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, bytes memory userData ) external returns (uint256[] memory amountsIn, uint256[] memory dueProtocolFeeAmounts); /** * @dev Called by the Vault when a user calls `IVault.exitPool` to remove liquidity from this Pool. Returns how many * tokens the Vault should deduct from the Pool's balances, as well as the amount of protocol fees the Pool owes * to the Vault. The Vault will then take tokens from the Pool's balances and send them to `recipient`, * as well as collect the reported amount in protocol fees, which the Pool should calculate based on * `protocolSwapFeePercentage`. * * Protocol fees are charged on exit events to guarantee that users exiting the Pool have paid their share. * * `sender` is the account performing the exit (typically the pool shareholder), and `recipient` is the account * to which the Vault will send the proceeds. `balances` contains the total token balances for each token * the Pool registered in the Vault, in the same order that `IVault.getPoolTokens` would return. * * `lastChangeBlock` is the last block in which *any* of the Pool's registered tokens last changed its total * balance. * * `userData` contains any pool-specific instructions needed to perform the calculations, such as the type of * exit (e.g., proportional given an amount of pool shares, single-asset, multi-asset, etc.) * * Contracts implementing this function should check that the caller is indeed the Vault before performing any * state-changing operations, such as burning pool shares. */ function onExitPool( bytes32 poolId, address sender, address recipient, uint256[] memory balances, uint256 lastChangeBlock, uint256 protocolSwapFeePercentage, bytes memory userData ) external returns (uint256[] memory amountsOut, uint256[] memory dueProtocolFeeAmounts); function getPoolId() external view returns (bytes32); }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; import "@balancer-labs/v2-solidity-utils/contracts/openzeppelin/IERC20.sol"; interface IAssetManager { /** * @notice Emitted when asset manager is rebalanced */ event Rebalance(bytes32 poolId); /** * @notice Sets the config */ function setConfig(bytes32 poolId, bytes calldata config) external; /** * Note: No function to read the asset manager config is included in IAssetManager * as the signature is expected to vary between asset manager implementations */ /** * @notice Returns the asset manager's token */ function getToken() external view returns (IERC20); /** * @return the current assets under management of this asset manager */ function getAUM(bytes32 poolId) external view returns (uint256); /** * @return poolCash - The up-to-date cash balance of the pool * @return poolManaged - The up-to-date managed balance of the pool */ function getPoolBalances(bytes32 poolId) external view returns (uint256 poolCash, uint256 poolManaged); /** * @return The difference in tokens between the target investment * and the currently invested amount (i.e. the amount that can be invested) */ function maxInvestableBalance(bytes32 poolId) external view returns (int256); /** * @notice Updates the Vault on the value of the pool's investment returns */ function updateBalanceOfPool(bytes32 poolId) external; /** * @notice Determines whether the pool should rebalance given the provided balances */ function shouldRebalance(uint256 cash, uint256 managed) external view returns (bool); /** * @notice Rebalances funds between the pool and the asset manager to maintain target investment percentage. * @param poolId - the poolId of the pool to be rebalanced * @param force - a boolean representing whether a rebalance should be forced even when the pool is near balance */ function rebalance(bytes32 poolId, bool force) external; /** * @notice allows an authorized rebalancer to remove capital to facilitate large withdrawals * @param poolId - the poolId of the pool to withdraw funds back to * @param amount - the amount of tokens to withdraw back to the pool */ function capitalOut(bytes32 poolId, uint256 amount) external; }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; import "@balancer-labs/v2-solidity-utils/contracts/openzeppelin/ERC20.sol"; import "@balancer-labs/v2-solidity-utils/contracts/openzeppelin/ERC20Permit.sol"; import "@balancer-labs/v2-vault/contracts/interfaces/IVault.sol"; /** * @title Highly opinionated token implementation * @author Balancer Labs * @dev * - Includes functions to increase and decrease allowance as a workaround * for the well-known issue with `approve`: * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 * - Allows for 'infinite allowance', where an allowance of 0xff..ff is not * decreased by calls to transferFrom * - Lets a token holder use `transferFrom` to send their own tokens, * without first setting allowance * - Emits 'Approval' events whenever allowance is changed by `transferFrom` * - Assigns infinite allowance for all token holders to the Vault */ contract BalancerPoolToken is ERC20, ERC20Permit { IVault private immutable _vault; constructor( string memory tokenName, string memory tokenSymbol, IVault vault ) ERC20(tokenName, tokenSymbol) ERC20Permit(tokenName) { _vault = vault; } function getVault() public view returns (IVault) { return _vault; } // Overrides /** * @dev Override to grant the Vault infinite allowance, causing for Pool Tokens to not require approval. * * This is sound as the Vault already provides authorization mechanisms when initiation token transfers, which this * contract inherits. */ function allowance(address owner, address spender) public view override returns (uint256) { if (spender == address(getVault())) { return uint256(-1); } else { return super.allowance(owner, spender); } } /** * @dev Override to allow for 'infinite allowance' and let the token owner use `transferFrom` with no self-allowance */ function transferFrom( address sender, address recipient, uint256 amount ) public override returns (bool) { uint256 currentAllowance = allowance(sender, msg.sender); _require(msg.sender == sender || currentAllowance >= amount, Errors.ERC20_TRANSFER_EXCEEDS_ALLOWANCE); _transfer(sender, recipient, amount); if (msg.sender != sender && currentAllowance != uint256(-1)) { // Because of the previous require, we know that if msg.sender != sender then currentAllowance >= amount _approve(sender, msg.sender, currentAllowance - amount); } return true; } /** * @dev Override to allow decreasing allowance by more than the current amount (setting it to zero) */ function decreaseAllowance(address spender, uint256 amount) public override returns (bool) { uint256 currentAllowance = allowance(msg.sender, spender); if (amount >= currentAllowance) { _approve(msg.sender, spender, 0); } else { // No risk of underflow due to if condition _approve(msg.sender, spender, currentAllowance - amount); } return true; } // Internal functions function _mintPoolTokens(address recipient, uint256 amount) internal { _mint(recipient, amount); } function _burnPoolTokens(address sender, uint256 amount) internal { _burn(sender, amount); } }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; import "@balancer-labs/v2-solidity-utils/contracts/helpers/Authentication.sol"; import "@balancer-labs/v2-vault/contracts/interfaces/IAuthorizer.sol"; import "./BasePool.sol"; /** * @dev Base authorization layer implementation for Pools. * * The owner account can call some of the permissioned functions - access control of the rest is delegated to the * Authorizer. Note that this owner is immutable: more sophisticated permission schemes, such as multiple ownership, * granular roles, etc., could be built on top of this by making the owner a smart contract. * * Access control of all other permissioned functions is delegated to an Authorizer. It is also possible to delegate * control of *all* permissioned functions to the Authorizer by setting the owner address to `_DELEGATE_OWNER`. */ abstract contract BasePoolAuthorization is Authentication { address private immutable _owner; address private constant _DELEGATE_OWNER = 0xBA1BA1ba1BA1bA1bA1Ba1BA1ba1BA1bA1ba1ba1B; constructor(address owner) { _owner = owner; } function getOwner() public view returns (address) { return _owner; } function getAuthorizer() external view returns (IAuthorizer) { return _getAuthorizer(); } function _canPerform(bytes32 actionId, address account) internal view override returns (bool) { if ((getOwner() != _DELEGATE_OWNER) && _isOwnerOnlyAction(actionId)) { // Only the owner can perform "owner only" actions, unless the owner is delegated. return msg.sender == getOwner(); } else { // Non-owner actions are always processed via the Authorizer, as "owner only" ones are when delegated. return _getAuthorizer().canPerform(actionId, account, address(this)); } } function _isOwnerOnlyAction(bytes32 actionId) internal view virtual returns (bool); function _getAuthorizer() internal view virtual returns (IAuthorizer); }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; /** * @dev Interface for the TemporarilyPausable helper. */ interface ITemporarilyPausable { /** * @dev Emitted every time the pause state changes by `_setPaused`. */ event PausedStateChanged(bool paused); /** * @dev Returns the current paused state. */ function getPausedState() external view returns ( bool paused, uint256 pauseWindowEndTime, uint256 bufferPeriodEndTime ); }
// SPDX-License-Identifier: MIT pragma solidity ^0.7.0; import "../helpers/BalancerErrors.sol"; /** * @dev Wrappers over Solidity's arithmetic operations with added overflow * checks. * * Arithmetic operations in Solidity wrap on overflow. This can easily result * in bugs, because programmers usually assume that an overflow raises an * error, which is the standard behavior in high level programming languages. * `SafeMath` restores this intuition by reverting the transaction when 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 SafeMath { /** * @dev Returns the addition of two unsigned integers, reverting on * overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * * - Addition cannot overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; _require(c >= a, Errors.ADD_OVERFLOW); return c; } /** * @dev Returns the subtraction of two unsigned integers, reverting on * overflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * * - Subtraction cannot overflow. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { return sub(a, b, Errors.SUB_OVERFLOW); } /** * @dev Returns the subtraction of two unsigned integers, reverting with custom message on * overflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * * - Subtraction cannot overflow. */ function sub(uint256 a, uint256 b, uint256 errorCode) internal pure returns (uint256) { _require(b <= a, errorCode); uint256 c = a - b; return c; } }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; /** * @dev Interface for the SignatureValidator helper, used to support meta-transactions. */ interface ISignaturesValidator { /** * @dev Returns the EIP712 domain separator. */ function getDomainSeparator() external view returns (bytes32); /** * @dev Returns the next nonce used by an address to sign messages. */ function getNextNonce(address user) external view returns (uint256); }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; import "../openzeppelin/IERC20.sol"; /** * @dev Interface for WETH9. * See https://github.com/gnosis/canonical-weth/blob/0dd1ea3e295eef916d0c6223ec63141137d22d67/contracts/WETH9.sol */ interface IWETH is IERC20 { function deposit() external payable; function withdraw(uint256 amount) external; }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; /** * @dev This is an empty interface used to represent either ERC20-conforming token contracts or ETH (using the zero * address sentinel value). We're just relying on the fact that `interface` can be used to declare new address-like * types. * * This concept is unrelated to a Pool's Asset Managers. */ interface IAsset { // solhint-disable-previous-line no-empty-blocks }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; interface IAuthorizer { /** * @dev Returns true if `account` can perform the action described by `actionId` in the contract `where`. */ function canPerform( bytes32 actionId, address account, address where ) external view returns (bool); }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; // Inspired by Aave Protocol's IFlashLoanReceiver. import "@balancer-labs/v2-solidity-utils/contracts/openzeppelin/IERC20.sol"; interface IFlashLoanRecipient { /** * @dev When `flashLoan` is called on the Vault, it invokes the `receiveFlashLoan` hook on the recipient. * * At the time of the call, the Vault will have transferred `amounts` for `tokens` to the recipient. Before this * call returns, the recipient must have transferred `amounts` plus `feeAmounts` for each token back to the * Vault, or else the entire flash loan will revert. * * `userData` is the same value passed in the `IVault.flashLoan` call. */ function receiveFlashLoan( IERC20[] memory tokens, uint256[] memory amounts, uint256[] memory feeAmounts, bytes memory userData ) external; }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; import "@balancer-labs/v2-solidity-utils/contracts/openzeppelin/IERC20.sol"; import "./IVault.sol"; import "./IAuthorizer.sol"; interface IProtocolFeesCollector { event SwapFeePercentageChanged(uint256 newSwapFeePercentage); event FlashLoanFeePercentageChanged(uint256 newFlashLoanFeePercentage); function withdrawCollectedFees( IERC20[] calldata tokens, uint256[] calldata amounts, address recipient ) external; function setSwapFeePercentage(uint256 newSwapFeePercentage) external; function setFlashLoanFeePercentage(uint256 newFlashLoanFeePercentage) external; function getSwapFeePercentage() external view returns (uint256); function getFlashLoanFeePercentage() external view returns (uint256); function getCollectedFeeAmounts(IERC20[] memory tokens) external view returns (uint256[] memory feeAmounts); function getAuthorizer() external view returns (IAuthorizer); function vault() external view returns (IVault); }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; import "@balancer-labs/v2-solidity-utils/contracts/openzeppelin/IERC20.sol"; import "./IVault.sol"; interface IPoolSwapStructs { // This is not really an interface - it just defines common structs used by other interfaces: IGeneralPool and // IMinimalSwapInfoPool. // // This data structure represents a request for a token swap, where `kind` indicates the swap type ('given in' or // 'given out') which indicates whether or not the amount sent by the pool is known. // // The pool receives `tokenIn` and sends `tokenOut`. `amount` is the number of `tokenIn` tokens the pool will take // in, or the number of `tokenOut` tokens the Pool will send out, depending on the given swap `kind`. // // All other fields are not strictly necessary for most swaps, but are provided to support advanced scenarios in // some Pools. // // `poolId` is the ID of the Pool involved in the swap - this is useful for Pool contracts that implement more than // one Pool. // // The meaning of `lastChangeBlock` depends on the Pool specialization: // - Two Token or Minimal Swap Info: the last block in which either `tokenIn` or `tokenOut` changed its total // balance. // - General: the last block in which *any* of the Pool's registered tokens changed its total balance. // // `from` is the origin address for the funds the Pool receives, and `to` is the destination address // where the Pool sends the outgoing tokens. // // `userData` is extra data provided by the caller - typically a signature from a trusted party. struct SwapRequest { IVault.SwapKind kind; IERC20 tokenIn; IERC20 tokenOut; uint256 amount; // Misc data bytes32 poolId; uint256 lastChangeBlock; address from; address to; bytes userData; } }
// SPDX-License-Identifier: MIT pragma solidity ^0.7.0; import "./ERC20.sol"; import "./IERC20Permit.sol"; import "./EIP712.sol"; /** * @dev Implementation of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612]. * * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by * presenting a message signed by the account. By not relying on `{IERC20-approve}`, the token holder account doesn't * need to send a transaction, and thus is not required to hold Ether at all. * * _Available since v3.4._ */ abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712 { mapping(address => uint256) private _nonces; // solhint-disable-next-line var-name-mixedcase bytes32 private immutable _PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); /** * @dev Initializes the {EIP712} domain separator using the `name` parameter, and setting `version` to `"1"`. * * It's a good idea to use the same `name` that is defined as the ERC20 token name. */ constructor(string memory name) EIP712(name, "1") {} /** * @dev See {IERC20Permit-permit}. */ function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) public virtual override { // solhint-disable-next-line not-rely-on-time _require(block.timestamp <= deadline, Errors.EXPIRED_PERMIT); uint256 nonce = _nonces[owner]; bytes32 structHash = keccak256(abi.encode(_PERMIT_TYPEHASH, owner, spender, value, nonce, deadline)); bytes32 hash = _hashTypedDataV4(structHash); address signer = ecrecover(hash, v, r, s); _require((signer != address(0)) && (signer == owner), Errors.INVALID_SIGNATURE); _nonces[owner] = nonce + 1; _approve(owner, spender, value); } /** * @dev See {IERC20Permit-nonces}. */ function nonces(address owner) public view override returns (uint256) { return _nonces[owner]; } /** * @dev See {IERC20Permit-DOMAIN_SEPARATOR}. */ // solhint-disable-next-line func-name-mixedcase function DOMAIN_SEPARATOR() external view override returns (bytes32) { return _domainSeparatorV4(); } }
// SPDX-License-Identifier: MIT pragma solidity ^0.7.0; /** * @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612]. * * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by * presenting a message signed by the account. By not relying on `{IERC20-approve}`, the token holder account doesn't * need to send a transaction, and thus is not required to hold Ether at all. */ 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]. */ 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 pragma solidity ^0.7.0; /** * @dev https://eips.ethereum.org/EIPS/eip-712[EIP 712] is a standard for hashing and signing of typed structured data. * * The encoding specified in the EIP is very generic, and such a generic 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 their contracts 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]. * * _Available since v3.4._ */ abstract contract EIP712 { /* solhint-disable var-name-mixedcase */ bytes32 private immutable _HASHED_NAME; bytes32 private immutable _HASHED_VERSION; bytes32 private immutable _TYPE_HASH; /* solhint-enable var-name-mixedcase */ /** * @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) { _HASHED_NAME = keccak256(bytes(name)); _HASHED_VERSION = keccak256(bytes(version)); _TYPE_HASH = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"); } /** * @dev Returns the domain separator for the current chain. */ function _domainSeparatorV4() internal view virtual returns (bytes32) { return keccak256(abi.encode(_TYPE_HASH, _HASHED_NAME, _HASHED_VERSION, _getChainId(), 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 keccak256(abi.encodePacked("\x19\x01", _domainSeparatorV4(), structHash)); } function _getChainId() private view returns (uint256 chainId) { // Silence state mutability warning without generating bytecode. // See https://github.com/ethereum/solidity/issues/10090#issuecomment-741789128 and // https://github.com/ethereum/solidity/issues/2691 this; // solhint-disable-next-line no-inline-assembly assembly { chainId := chainid() } } }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; import "./BalancerErrors.sol"; import "./IAuthentication.sol"; /** * @dev Building block for performing access control on external functions. * * This contract is used via the `authenticate` modifier (or the `_authenticateCaller` function), which can be applied * to external functions to only make them 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); _require(_canPerform(actionId, msg.sender), Errors.SENDER_NOT_ALLOWED); } 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)); } function _canPerform(bytes32 actionId, address user) internal view virtual returns (bool); }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; interface IAuthentication { /** * @dev Returns the action identifier associated with the external function described by `selector`. */ function getActionId(bytes4 selector) external view returns (bytes32); }
// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. pragma solidity ^0.7.0; import "../math/LogExpMath.sol"; /** * @dev Library for encoding and decoding values stored inside a 256 bit word. Typically used to pack multiple values in * a single storage slot, saving gas by performing less storage accesses. * * Each value is defined by its size and the least significant bit in the word, also known as offset. For example, two * 128 bit values may be encoded in a word by assigning one an offset of 0, and the other an offset of 128. */ library LogCompression { int256 private constant _LOG_COMPRESSION_FACTOR = 1e14; int256 private constant _HALF_LOG_COMPRESSION_FACTOR = 0.5e14; /** * @dev Returns the natural logarithm of `value`, dropping most of the decimal places to arrive at a value that, * when passed to `fromLowResLog`, will have a maximum relative error of ~0.05% compared to `value`. * * Values returned from this function should not be mixed with other fixed-point values (as they have a different * number of digits), but can be added or subtracted. Use `fromLowResLog` to undo this process and return to an * 18 decimal places fixed point value. * * Because so much precision is lost, the logarithmic values can be stored using much fewer bits than the original * value required. */ function toLowResLog(uint256 value) internal pure returns (int256) { int256 ln = LogExpMath.ln(int256(value)); // Rounding division for signed numerator int256 lnWithError = (ln > 0 ? ln + _HALF_LOG_COMPRESSION_FACTOR : ln - _HALF_LOG_COMPRESSION_FACTOR); return lnWithError / _LOG_COMPRESSION_FACTOR; } /** * @dev Restores `value` from logarithmic space. `value` is expected to be the result of a call to `toLowResLog`, * any other function that returns 4 decimals fixed point logarithms, or the sum of such values. */ function fromLowResLog(int256 value) internal pure returns (uint256) { return uint256(LogExpMath.exp(value * _LOG_COMPRESSION_FACTOR)); } }
// SPDX-License-Identifier: LicenseRef-Gyro-1.0 // for information on licensing please see the README in the GitHub repository <https://github.com/gyrostable/concentrated-lps>. pragma solidity 0.7.6; // import "@balancer-labs/v2-solidity-utils/contracts/math/FixedPoint.sol"; import "./GyroFixedPoint.sol"; import "@balancer-labs/v2-solidity-utils/contracts/helpers/BalancerErrors.sol"; /* solhint-disable private-vars-leading-underscore */ /// @dev Signed fixed point operations based on Balancer's FixedPoint library. /// Note: The `{mul,div}{UpMag,DownMag}()` functions do *not* round up or down, respectively, /// in a signed fashion (like ceil and floor operations), but *in absolute value* (or *magnitude*), i.e., /// towards 0. This is useful in some applications. library SignedFixedPoint { int256 internal constant ONE = 1e18; // 18 decimal places // setting extra precision at 38 decimals, which is the most we can get w/o overflowing on normal multiplication // this allows 20 extra digits to absorb error when multiplying by large numbers int256 internal constant ONE_XP = 1e38; // 38 decimal places function add(int256 a, int256 b) internal pure returns (int256) { // Fixed Point addition is the same as regular checked addition int256 c = a + b; if (!(b >= 0 ? c >= a : c < a)) _require(false, Errors.ADD_OVERFLOW); return c; } function addMag(int256 a, int256 b) internal pure returns (int256 c) { // add b in the same signed direction as a, i.e. increase the magnitude of a by b c = a > 0 ? add(a, b) : sub(a, b); } function sub(int256 a, int256 b) internal pure returns (int256) { // Fixed Point subtraction is the same as regular checked subtraction int256 c = a - b; if (!(b <= 0 ? c >= a : c < a)) _require(false, Errors.SUB_OVERFLOW); return c; } /// @dev This rounds towards 0, i.e., down *in absolute value*! function mulDownMag(int256 a, int256 b) internal pure returns (int256) { int256 product = a * b; if (!(a == 0 || product / a == b)) _require(false, Errors.MUL_OVERFLOW); return product / ONE; } /// @dev this implements mulDownMag w/o checking for over/under-flows, which saves significantly on gas if these aren't needed function mulDownMagU(int256 a, int256 b) internal pure returns (int256) { return (a * b) / ONE; } /// @dev This rounds away from 0, i.e., up *in absolute value*! function mulUpMag(int256 a, int256 b) internal pure returns (int256) { int256 product = a * b; if (!(a == 0 || product / a == b)) _require(false, Errors.MUL_OVERFLOW); // If product > 0, the result should be ceil(p/ONE) = floor((p-1)/ONE) + 1, where floor() is implicit. If // product < 0, the result should be floor(p/ONE) = ceil((p+1)/ONE) - 1, where ceil() is implicit. // Addition for signed numbers: Case selection so we round away from 0, not always up. if (product > 0) return ((product - 1) / ONE) + 1; else if (product < 0) return ((product + 1) / ONE) - 1; // product == 0 return 0; } /// @dev this implements mulUpMag w/o checking for over/under-flows, which saves significantly on gas if these aren't needed function mulUpMagU(int256 a, int256 b) internal pure returns (int256) { int256 product = a * b; // If product > 0, the result should be ceil(p/ONE) = floor((p-1)/ONE) + 1, where floor() is implicit. If // product < 0, the result should be floor(p/ONE) = ceil((p+1)/ONE) - 1, where ceil() is implicit. // Addition for signed numbers: Case selection so we round away from 0, not always up. if (product > 0) return ((product - 1) / ONE) + 1; else if (product < 0) return ((product + 1) / ONE) - 1; // product == 0 return 0; } /// @dev Rounds towards 0, i.e., down in absolute value. function divDownMag(int256 a, int256 b) internal pure returns (int256) { if (b == 0) _require(false, Errors.ZERO_DIVISION); if (a == 0) { return 0; } int256 aInflated = a * ONE; if (aInflated / a != ONE) _require(false, Errors.DIV_INTERNAL); return aInflated / b; } /// @dev this implements divDownMag w/o checking for over/under-flows, which saves significantly on gas if these aren't needed function divDownMagU(int256 a, int256 b) internal pure returns (int256) { if (b == 0) _require(false, Errors.ZERO_DIVISION); return (a * ONE) / b; } /// @dev Rounds away from 0, i.e., up in absolute value. function divUpMag(int256 a, int256 b) internal pure returns (int256) { if (b == 0) _require(false, Errors.ZERO_DIVISION); if (a == 0) { return 0; } if (b < 0) { // Required so the below is correct. b = -b; a = -a; } int256 aInflated = a * ONE; if (aInflated / a != ONE) _require(false, Errors.DIV_INTERNAL); if (aInflated > 0) return ((aInflated - 1) / b) + 1; return ((aInflated + 1) / b) - 1; } /// @dev this implements divUpMag w/o checking for over/under-flows, which saves significantly on gas if these aren't needed function divUpMagU(int256 a, int256 b) internal pure returns (int256) { if (b == 0) _require(false, Errors.ZERO_DIVISION); if (a == 0) { return 0; } // SOMEDAY check if we can shave off some gas by logically refactoring this vs the below case distinction into one (on a * b or so). if (b < 0) { // Ensure b > 0 so the below is correct. b = -b; a = -a; } if (a > 0) return ((a * ONE - 1) / b) + 1; return ((a * ONE + 1) / b) - 1; } /// @dev multiplies two extra precision numbers (with 38 decimals) /// rounds down in magnitude but this shouldn't matter /// multiplication can overflow if a,b are > 2 in magnitude function mulXp(int256 a, int256 b) internal pure returns (int256) { int256 product = a * b; if (!(a == 0 || product / a == b)) _require(false, Errors.MUL_OVERFLOW); return product / ONE_XP; } /// @dev multiplies two extra precision numbers (with 38 decimals) /// rounds down in magnitude but this shouldn't matter /// multiplication can overflow if a,b are > 2 in magnitude /// this implements mulXp w/o checking for over/under-flows, which saves significantly on gas if these aren't needed function mulXpU(int256 a, int256 b) internal pure returns (int256) { return (a * b) / ONE_XP; } /// @dev divides two extra precision numbers (with 38 decimals) /// rounds down in magnitude but this shouldn't matter /// can overflow if a > 2 or b << 1 in magnitude function divXp(int256 a, int256 b) internal pure returns (int256) { if (b == 0) _require(false, Errors.ZERO_DIVISION); if (a == 0) { return 0; } int256 aInflated = a * ONE_XP; if (aInflated / a != ONE_XP) _require(false, Errors.DIV_INTERNAL); return aInflated / b; } /// @dev divides two extra precision numbers (with 38 decimals) /// rounds down in magnitude but this shouldn't matter /// can overflow if a > 2 or b << 1 in magnitude /// this implements divXp w/o checking for over/under-flows, which saves significantly on gas if these aren't needed function divXpU(int256 a, int256 b) internal pure returns (int256) { if (b == 0) _require(false, Errors.ZERO_DIVISION); return (a * ONE_XP) / b; } /// @dev multiplies normal precision a with extra precision b (with 38 decimals) /// Rounds down in signed direction /// returns normal precision of the product function mulDownXpToNp(int256 a, int256 b) internal pure returns (int256) { int256 b1 = b / 1e19; int256 prod1 = a * b1; if (!(a == 0 || prod1 / a == b1)) _require(false, Errors.MUL_OVERFLOW); int256 b2 = b % 1e19; int256 prod2 = a * b2; if (!(a == 0 || prod2 / a == b2)) _require(false, Errors.MUL_OVERFLOW); return prod1 >= 0 && prod2 >= 0 ? (prod1 + prod2 / 1e19) / 1e19 : (prod1 + prod2 / 1e19 + 1) / 1e19 - 1; } /// @dev multiplies normal precision a with extra precision b (with 38 decimals) /// Rounds down in signed direction /// returns normal precision of the product /// this implements mulDownXpToNp w/o checking for over/under-flows, which saves significantly on gas if these aren't needed function mulDownXpToNpU(int256 a, int256 b) internal pure returns (int256) { int256 b1 = b / 1e19; int256 b2 = b % 1e19; // SOMEDAY check if we eliminate these vars and save some gas (by only checking the sign of prod1, say) int256 prod1 = a * b1; int256 prod2 = a * b2; return prod1 >= 0 && prod2 >= 0 ? (prod1 + prod2 / 1e19) / 1e19 : (prod1 + prod2 / 1e19 + 1) / 1e19 - 1; } /// @dev multiplies normal precision a with extra precision b (with 38 decimals) /// Rounds up in signed direction /// returns normal precision of the product function mulUpXpToNp(int256 a, int256 b) internal pure returns (int256) { int256 b1 = b / 1e19; int256 prod1 = a * b1; if (!(a == 0 || prod1 / a == b1)) _require(false, Errors.MUL_OVERFLOW); int256 b2 = b % 1e19; int256 prod2 = a * b2; if (!(a == 0 || prod2 / a == b2)) _require(false, Errors.MUL_OVERFLOW); return prod1 <= 0 && prod2 <= 0 ? (prod1 + prod2 / 1e19) / 1e19 : (prod1 + prod2 / 1e19 - 1) / 1e19 + 1; } /// @dev multiplies normal precision a with extra precision b (with 38 decimals) /// Rounds up in signed direction /// returns normal precision of the product /// this implements mulUpXpToNp w/o checking for over/under-flows, which saves significantly on gas if these aren't needed function mulUpXpToNpU(int256 a, int256 b) internal pure returns (int256) { int256 b1 = b / 1e19; int256 b2 = b % 1e19; // SOMEDAY check if we eliminate these vars and save some gas (by only checking the sign of prod1, say) int256 prod1 = a * b1; int256 prod2 = a * b2; return prod1 <= 0 && prod2 <= 0 ? (prod1 + prod2 / 1e19) / 1e19 : (prod1 + prod2 / 1e19 - 1) / 1e19 + 1; } // not implementing the pow functions right now b/c it's annoying and slightly ill-defined, and we don't use them. /** * @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(int256 x) internal pure returns (int256) { if (x >= ONE || x <= 0) return 0; return ONE - x; } }
// SPDX-License-Identifier: LicenseRef-Gyro-1.0 // for information on licensing please see the README in the GitHub repository <https://github.com/gyrostable/concentrated-lps>. pragma solidity 0.7.6; // solhint-disable library GyroECLPPoolErrors { // Input uint256 internal constant ADDRESS_IS_ZERO_ADDRESS = 120; uint256 internal constant TOKEN_IN_IS_NOT_TOKEN_0 = 121; // Math uint256 internal constant PRICE_BOUNDS_WRONG = 354; uint256 internal constant ROTATION_VECTOR_WRONG = 355; uint256 internal constant ROTATION_VECTOR_NOT_NORMALIZED = 356; uint256 internal constant ASSET_BOUNDS_EXCEEDED = 357; uint256 internal constant DERIVED_TAU_NOT_NORMALIZED = 358; uint256 internal constant DERIVED_ZETA_WRONG = 359; uint256 internal constant STRETCHING_FACTOR_WRONG = 360; uint256 internal constant DERIVED_UVWZ_WRONG = 361; uint256 internal constant INVARIANT_DENOMINATOR_WRONG = 362; uint256 internal constant MAX_ASSETS_EXCEEDED = 363; uint256 internal constant MAX_INVARIANT_EXCEEDED = 363; uint256 internal constant DERIVED_DSQ_WRONG = 364; }
// SPDX-License-Identifier: MIT pragma solidity >=0.6.0 <0.8.0; /** * @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow * checks. * * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can * easily result in undesired exploitation or bugs, since developers usually * assume that overflows raise errors. `SafeCast` restores this intuition by * reverting the transaction when such an operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. * * Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing * all math on `uint256` and `int256` and then downcasting. */ library SafeCast { /** * @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) { require(value < 2**128, "SafeCast: value doesn\'t fit in 128 bits"); return uint128(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) { require(value < 2**64, "SafeCast: value doesn\'t fit in 64 bits"); return uint64(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) { require(value < 2**32, "SafeCast: value doesn\'t fit in 32 bits"); return uint32(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) { require(value < 2**16, "SafeCast: value doesn\'t fit in 16 bits"); 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) { require(value < 2**8, "SafeCast: value doesn\'t fit in 8 bits"); 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) { require(value >= 0, "SafeCast: value must be positive"); return uint256(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 * * _Available since v3.1._ */ function toInt128(int256 value) internal pure returns (int128) { require(value >= -2**127 && value < 2**127, "SafeCast: value doesn\'t fit in 128 bits"); return int128(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 * * _Available since v3.1._ */ function toInt64(int256 value) internal pure returns (int64) { require(value >= -2**63 && value < 2**63, "SafeCast: value doesn\'t fit in 64 bits"); return int64(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 * * _Available since v3.1._ */ function toInt32(int256 value) internal pure returns (int32) { require(value >= -2**31 && value < 2**31, "SafeCast: value doesn\'t fit in 32 bits"); return int32(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 * * _Available since v3.1._ */ function toInt16(int256 value) internal pure returns (int16) { require(value >= -2**15 && value < 2**15, "SafeCast: value doesn\'t fit in 16 bits"); return int16(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. * * _Available since v3.1._ */ function toInt8(int256 value) internal pure returns (int8) { require(value >= -2**7 && value < 2**7, "SafeCast: value doesn\'t fit in 8 bits"); return int8(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) { require(value < 2**255, "SafeCast: value doesn't fit in an int256"); return int256(value); } }
// SPDX-License-Identifier: LicenseRef-Gyro-1.0 // for information on licensing please see the README in the GitHub repository <https://github.com/gyrostable/concentrated-lps>. pragma solidity 0.7.6; pragma experimental ABIEncoderV2; interface ICappedLiquidity { event CapParamsUpdated(CapParams params); event CapManagerUpdated(address capManager); struct CapParams { bool capEnabled; uint120 perAddressCap; uint128 globalCap; } function setCapParams(CapParams memory params) external; function capParams() external view returns (CapParams memory); function capManager() external view returns (address); }
// SPDX-License-Identifier: LicenseRef-Gyro-1.0 // for information on licensing please see the README in the GitHub repository <https://github.com/gyrostable/concentrated-lps>. pragma solidity ^0.7.0; interface ILocallyPausable { event PausedLocally(); event UnpausedLocally(); event PauseManagerChanged(address oldPauseManager, address newPauseManager); struct PauseParams { uint256 pauseWindowDuration; uint256 bufferPeriodDuration; } /// @notice Changes the account that is allowed to pause a pool. function changePauseManager(address _pauseManager) external; /// @notice Pauses the pool. /// Can only be called by the pause manager. function pause() external; /// @notice Unpauses the pool. /// Can only be called by the pause manager. function unpause() external; }
{ "evmVersion": "berlin", "optimizer": { "enabled": true, "runs": 200 }, "outputSelection": { "*": { "*": [ "evm.bytecode", "evm.deployedBytecode", "devdoc", "userdoc", "metadata", "abi" ] } }, "libraries": { "contracts/eclp/GyroECLPMath.sol": { "GyroECLPMath": "0xf89a1713998593a441cda571780f0900dbef20f9" } } }
Contract Security Audit
- No Contract Security Audit Submitted- Submit Audit Here
[{"inputs":[{"components":[{"components":[{"internalType":"contract IVault","name":"vault","type":"address"},{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"symbol","type":"string"},{"internalType":"contract IERC20","name":"token0","type":"address"},{"internalType":"contract IERC20","name":"token1","type":"address"},{"internalType":"uint256","name":"swapFeePercentage","type":"uint256"},{"internalType":"uint256","name":"pauseWindowDuration","type":"uint256"},{"internalType":"uint256","name":"bufferPeriodDuration","type":"uint256"},{"internalType":"address","name":"owner","type":"address"}],"internalType":"struct ExtensibleWeightedPool2Tokens.NewPoolParams","name":"baseParams","type":"tuple"},{"components":[{"internalType":"int256","name":"alpha","type":"int256"},{"internalType":"int256","name":"beta","type":"int256"},{"internalType":"int256","name":"c","type":"int256"},{"internalType":"int256","name":"s","type":"int256"},{"internalType":"int256","name":"lambda","type":"int256"}],"internalType":"struct GyroECLPMath.Params","name":"eclpParams","type":"tuple"},{"components":[{"components":[{"internalType":"int256","name":"x","type":"int256"},{"internalType":"int256","name":"y","type":"int256"}],"internalType":"struct GyroECLPMath.Vector2","name":"tauAlpha","type":"tuple"},{"components":[{"internalType":"int256","name":"x","type":"int256"},{"internalType":"int256","name":"y","type":"int256"}],"internalType":"struct GyroECLPMath.Vector2","name":"tauBeta","type":"tuple"},{"internalType":"int256","name":"u","type":"int256"},{"internalType":"int256","name":"v","type":"int256"},{"internalType":"int256","name":"w","type":"int256"},{"internalType":"int256","name":"z","type":"int256"},{"internalType":"int256","name":"dSq","type":"int256"}],"internalType":"struct GyroECLPMath.DerivedParams","name":"derivedEclpParams","type":"tuple"},{"internalType":"address","name":"rateProvider0","type":"address"},{"internalType":"address","name":"rateProvider1","type":"address"},{"internalType":"address","name":"capManager","type":"address"},{"components":[{"internalType":"bool","name":"capEnabled","type":"bool"},{"internalType":"uint120","name":"perAddressCap","type":"uint120"},{"internalType":"uint128","name":"globalCap","type":"uint128"}],"internalType":"struct ICappedLiquidity.CapParams","name":"capParams","type":"tuple"},{"internalType":"address","name":"pauseManager","type":"address"}],"internalType":"struct GyroECLPPool.GyroParams","name":"params","type":"tuple"},{"internalType":"address","name":"configAddress","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"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":"address","name":"capManager","type":"address"}],"name":"CapManagerUpdated","type":"event"},{"anonymous":false,"inputs":[{"components":[{"internalType":"bool","name":"capEnabled","type":"bool"},{"internalType":"uint120","name":"perAddressCap","type":"uint120"},{"internalType":"uint128","name":"globalCap","type":"uint128"}],"indexed":false,"internalType":"struct ICappedLiquidity.CapParams","name":"params","type":"tuple"}],"name":"CapParamsUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bool","name":"derivedParamsValidated","type":"bool"}],"name":"ECLPDerivedParamsValidated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bool","name":"paramsValidated","type":"bool"}],"name":"ECLPParamsValidated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"invariantAfterJoin","type":"uint256"}],"name":"InvariantAterInitializeJoin","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"oldInvariant","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newInvariant","type":"uint256"}],"name":"InvariantOldAndNew","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"oldPauseManager","type":"address"},{"indexed":false,"internalType":"address","name":"newPauseManager","type":"address"}],"name":"PauseManagerChanged","type":"event"},{"anonymous":false,"inputs":[],"name":"PausedLocally","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bool","name":"paused","type":"bool"}],"name":"PausedStateChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"swapFeePercentage","type":"uint256"}],"name":"SwapFeePercentageChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256[]","name":"balances","type":"uint256[]"},{"components":[{"internalType":"int256","name":"x","type":"int256"},{"internalType":"int256","name":"y","type":"int256"}],"indexed":false,"internalType":"struct GyroECLPMath.Vector2","name":"invariant","type":"tuple"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"SwapParams","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":[],"name":"UnpausedLocally","type":"event"},{"inputs":[],"name":"DOMAIN_SEPARATOR","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":[],"name":"capManager","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"capParams","outputs":[{"components":[{"internalType":"bool","name":"capEnabled","type":"bool"},{"internalType":"uint120","name":"perAddressCap","type":"uint120"},{"internalType":"uint128","name":"globalCap","type":"uint128"}],"internalType":"struct ICappedLiquidity.CapParams","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_pauseManager","type":"address"}],"name":"changePauseManager","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"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":"getActualSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getAuthorizer","outputs":[{"internalType":"contract IAuthorizer","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getECLPParams","outputs":[{"components":[{"internalType":"int256","name":"alpha","type":"int256"},{"internalType":"int256","name":"beta","type":"int256"},{"internalType":"int256","name":"c","type":"int256"},{"internalType":"int256","name":"s","type":"int256"},{"internalType":"int256","name":"lambda","type":"int256"}],"internalType":"struct GyroECLPMath.Params","name":"params","type":"tuple"},{"components":[{"components":[{"internalType":"int256","name":"x","type":"int256"},{"internalType":"int256","name":"y","type":"int256"}],"internalType":"struct GyroECLPMath.Vector2","name":"tauAlpha","type":"tuple"},{"components":[{"internalType":"int256","name":"x","type":"int256"},{"internalType":"int256","name":"y","type":"int256"}],"internalType":"struct GyroECLPMath.Vector2","name":"tauBeta","type":"tuple"},{"internalType":"int256","name":"u","type":"int256"},{"internalType":"int256","name":"v","type":"int256"},{"internalType":"int256","name":"w","type":"int256"},{"internalType":"int256","name":"z","type":"int256"},{"internalType":"int256","name":"dSq","type":"int256"}],"internalType":"struct GyroECLPMath.DerivedParams","name":"d","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getFeesMetadata","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getInvariant","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getInvariantDivActualSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getLastInvariant","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getMiscData","outputs":[{"internalType":"int256","name":"logInvariant","type":"int256"},{"internalType":"int256","name":"logTotalSupply","type":"int256"},{"internalType":"uint256","name":"oracleSampleCreationTimestamp","type":"uint256"},{"internalType":"uint256","name":"oracleIndex","type":"uint256"},{"internalType":"bool","name":"oracleEnabled","type":"bool"},{"internalType":"uint256","name":"swapFeePercentage","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getNormalizedWeights","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPausedState","outputs":[{"internalType":"bool","name":"paused","type":"bool"},{"internalType":"uint256","name":"pauseWindowEndTime","type":"uint256"},{"internalType":"uint256","name":"bufferPeriodEndTime","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPoolId","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPrice","outputs":[{"internalType":"uint256","name":"spotPrice","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getSwapFeePercentage","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getTokenRates","outputs":[{"internalType":"uint256","name":"rate0","type":"uint256"},{"internalType":"uint256","name":"rate1","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getVault","outputs":[{"internalType":"contract IVault","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"gyroConfig","outputs":[{"internalType":"contract IGyroConfig","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","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":"bytes32","name":"poolId","type":"bytes32"},{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256[]","name":"balances","type":"uint256[]"},{"internalType":"uint256","name":"lastChangeBlock","type":"uint256"},{"internalType":"uint256","name":"protocolSwapFeePercentage","type":"uint256"},{"internalType":"bytes","name":"userData","type":"bytes"}],"name":"onExitPool","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"},{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"poolId","type":"bytes32"},{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256[]","name":"balances","type":"uint256[]"},{"internalType":"uint256","name":"lastChangeBlock","type":"uint256"},{"internalType":"uint256","name":"protocolSwapFeePercentage","type":"uint256"},{"internalType":"bytes","name":"userData","type":"bytes"}],"name":"onJoinPool","outputs":[{"internalType":"uint256[]","name":"amountsIn","type":"uint256[]"},{"internalType":"uint256[]","name":"dueProtocolFeeAmounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"enum IVault.SwapKind","name":"kind","type":"uint8"},{"internalType":"contract IERC20","name":"tokenIn","type":"address"},{"internalType":"contract IERC20","name":"tokenOut","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bytes32","name":"poolId","type":"bytes32"},{"internalType":"uint256","name":"lastChangeBlock","type":"uint256"},{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bytes","name":"userData","type":"bytes"}],"internalType":"struct IPoolSwapStructs.SwapRequest","name":"request","type":"tuple"},{"internalType":"uint256","name":"balanceTokenIn","type":"uint256"},{"internalType":"uint256","name":"balanceTokenOut","type":"uint256"}],"name":"onSwap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"pause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"pauseManager","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"permit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"poolId","type":"bytes32"},{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256[]","name":"balances","type":"uint256[]"},{"internalType":"uint256","name":"lastChangeBlock","type":"uint256"},{"internalType":"uint256","name":"protocolSwapFeePercentage","type":"uint256"},{"internalType":"bytes","name":"userData","type":"bytes"}],"name":"queryExit","outputs":[{"internalType":"uint256","name":"bptIn","type":"uint256"},{"internalType":"uint256[]","name":"amountsOut","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"poolId","type":"bytes32"},{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256[]","name":"balances","type":"uint256[]"},{"internalType":"uint256","name":"lastChangeBlock","type":"uint256"},{"internalType":"uint256","name":"protocolSwapFeePercentage","type":"uint256"},{"internalType":"bytes","name":"userData","type":"bytes"}],"name":"queryJoin","outputs":[{"internalType":"uint256","name":"bptOut","type":"uint256"},{"internalType":"uint256[]","name":"amountsIn","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"rateProvider0","outputs":[{"internalType":"contract IRateProvider","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"rateProvider1","outputs":[{"internalType":"contract IRateProvider","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_capManager","type":"address"}],"name":"setCapManager","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"bool","name":"capEnabled","type":"bool"},{"internalType":"uint120","name":"perAddressCap","type":"uint120"},{"internalType":"uint128","name":"globalCap","type":"uint128"}],"internalType":"struct ICappedLiquidity.CapParams","name":"params","type":"tuple"}],"name":"setCapParams","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bool","name":"paused","type":"bool"}],"name":"setPaused","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"swapFeePercentage","type":"uint256"}],"name":"setSwapFeePercentage","outputs":[],"stateMutability":"nonpayable","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":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unpause","outputs":[],"stateMutability":"nonpayable","type":"function"}]
Contract Creation Code
6104a06040527f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9610120526706f05b59d3b20000610200819052610220526000610240523480156200005057600080fd5b506040516200608b3803806200608b833981016040819052620000739162000f68565b8160e001518260a001518360c0015184600001518060c001518160e001518260200151836040015184600001518280604051806040016040528060018152602001603160f81b81525085858a6101000151336001600160a01b031660001b806080818152505050806001600160a01b031660a0816001600160a01b031660601b815250505081600390805190602001906200011092919062000b65565b5080516200012690600490602084019062000b65565b505060058054601260ff1990911617905550815160209283012060c052805191012060e052507f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f61010052606090811b6001600160601b031916610140524294909401610160819052909201610180525050604080516002808252928101909152600091816020016020820280368337019050509050816060015181600081518110620001cf57fe5b60200260200101906001600160a01b031690816001600160a01b0316815250508160800151816001815181106200020257fe5b60200260200101906001600160a01b031690816001600160a01b0316815250506200023881620006a860201b62001bc31760201c565b60a08201516200024890620006b8565b81516040516309b2760f60e01b81526000916001600160a01b0316906309b2760f906200027b9060029060040162001154565b602060405180830381600087803b1580156200029657600080fd5b505af1158015620002ab573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190620002d1919062000f4f565b83516040805160028082526060820183529394506001600160a01b03909216926366a9c7d2928592879290602083019080368337019050506040518463ffffffff1660e01b81526004016200032993929190620010ba565b600060405180830381600087803b1580156200034457600080fd5b505af115801562000359573d6000803e3d6000fd5b5050506101a082905250606080840180516001600160601b031990831b81166101c052608086015190921b9091166101e05251620003979062000741565b610260526080830151620003ab9062000741565b61028052505060408051808201909152600e81526d1b9bdd08185d5d1a1bdc9a5e995960921b602082015290506001600160a01b0383166200040b5760405162461bcd60e51b815260040162000402919062001169565b60405180910390fd5b50600b80546001600160a01b0319166001600160a01b039384161790558051600a8054602084015160409094015160ff1990911692151592909217610100600160801b0319166101006001600160781b0390941693909302929092176001600160801b03908116600160801b91909216021790556200049090821615156069620007ef565b600c80546001600160a01b0319166001600160a01b03928316179055620004bd90821615156078620007ef565b620004d782602001516200080060201b62001bd11760201c565b7ff468331437e1ec2fec64fc27f23a28b085a09e90f385338d4b58cb63860797776001604051620005099190620010af565b60405180910390a160208201516040808401519051636e70c2ef60e01b815273f89a1713998593a441cda571780f0900dbef20f992636e70c2ef9262000552926004016200119e565b60006040518083038186803b1580156200056b57600080fd5b505af415801562000580573d6000803e3d6000fd5b505050507f614fd1489cdee8181582fbbc1933d6870cd06c6f22d314b97a650ffa547c6ccd6001604051620005b69190620010af565b60405180910390a160208281015180518183015160408084015160608086015160809687015161032052610300526102e0919091526102c0929092526102a09290925281860151805180519086015182870151805197015194830151838501518488015160a086015160c0909601516104405261042095909552610400949094526103e0939093526103c0929092526103a093909352610380949094526103609390935261034052600d80546001600160a01b0319166001600160a01b03949094169390931790925580830151811b6001600160601b031990811661046052919092015190911b166104805262001296565b80620006b481620008eb565b5050565b620006cd64e8d4a5100082101560cb62000975565b620006e567016345785d8a000082111560ca62000975565b62000701816008546200098660201b62001cae1790919060201c565b6008556040517fa9ba3ffe0b6c366b81232caab38605a0699ad5398d6cce76f91ee809e322dafc906200073690839062001236565b60405180910390a150565b600080826001600160a01b031663313ce5676040518163ffffffff1660e01b815260040160206040518083038186803b1580156200077e57600080fd5b505afa15801562000793573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190620007b991906200107d565b60ff1690506000620007d8601283620009ac60201b62001cbc1760201c565b600a0a670de0b6b3a764000002925050505b919050565b81620006b457620006b481620009c4565b6200082f8160600151600013158015620008265750670de0b6b3a7640000826060015113155b610163620007ef565b6200085d8160400151600013158015620008265750670de0b6b3a764000082604001511315610163620007ef565b604080518082018252606083015181529082015160208201526000620008848280620009d6565b9050620008b4670de0b6b3a763fc188212801590620008ab5750670de0b6b3a76403e88213155b610164620007ef565b620008e68360800151600013158015620008dd57506a52b7d2dcc80cd2e4000000846080015113155b610168620007ef565b505050565b600281511015620008fc5762000972565b6000816000815181106200090c57fe5b602002602001015190506000600190505b8251811015620008e65760008382815181106200093657fe5b6020026020010151905062000967816001600160a01b0316846001600160a01b03161060656200097560201b60201c565b91506001016200091d565b50565b81620006b457620006b48162000a2d565b6000620009a58260568562000a8060201b62001cd2179092919060201c565b9392505050565b6000620009be83831115600162000975565b50900390565b62000972816223aca960e91b62000a95565b6000620009a5620009fe8360200151856020015162000af660201b62001ce81790919060201c565b8351855162000a199162000af6602090811b62001ce817901c565b62000b3760201b62001d241790919060201c565b62461bcd60e51b6000908152602060045260076024526642414c23000030600a808404818106603090810160081b95839006959095019082900491820690940160101b939093010160c81b604452606490fd5b6001600160401b03811b1992909216911b1790565b62461bcd60e51b600090815260206004526007602452600a808404818106603090810160081b958390069590950190829004918206850160101b01602363ffffff0060e086901c160160181b0190930160c81b60445260e882901c90606490fd5b600082820283158062000b1257508284828162000b0f57fe5b05145b62000b255762000b256000600362000975565b670de0b6b3a764000090059392505050565b60008282018183121562000b4e5783811262000b53565b838112155b620009a557620009a560008062000975565b828054600181600116156101000203166002900490600052602060002090601f01602090048101928262000b9d576000855562000be8565b82601f1062000bb857805160ff191683800117855562000be8565b8280016001018555821562000be8579182015b8281111562000be857825182559160200191906001019062000bcb565b5062000bf692915062000bfa565b5090565b5b8082111562000bf6576000815560010162000bfb565b80516001600160a01b0381168114620007ea57600080fd5b600082601f83011262000c3a578081fd5b81516001600160401b0381111562000c4e57fe5b62000c63601f8201601f19166020016200123f565b81815284602083860101111562000c78578283fd5b62000c8b82602083016020870162001263565b949350505050565b60006060828403121562000ca5578081fd5b604051606081016001600160401b038111828210171562000cc257fe5b80604052508091508251801515811462000cdb57600080fd5b815260208301516001600160781b038116811462000cf857600080fd5b602082015260408301516001600160801b038116811462000d1857600080fd5b6040919091015292915050565b6000610120828403121562000d38578081fd5b60405160e081016001600160401b038111828210171562000d5557fe5b60405290508062000d67848462000f06565b815262000d78846040850162000f06565b60208201526080830151604082015260a0830151606082015260c0830151608082015260e083015160a082015261010083015160c08201525092915050565b600061012080838503121562000dcb578182fd5b62000dd6816200123f565b91505062000de48262000c11565b815260208201516001600160401b038082111562000e0157600080fd5b62000e0f8583860162000c29565b6020840152604084015191508082111562000e2957600080fd5b5062000e388482850162000c29565b60408301525062000e4c6060830162000c11565b606082015262000e5f6080830162000c11565b608082015260a082015160a082015260c082015160c082015260e082015160e082015261010062000e9281840162000c11565b9082015292915050565b600060a0828403121562000eae578081fd5b60405160a081016001600160401b038111828210171562000ecb57fe5b806040525080915082518152602083015160208201526040830151604082015260608301516060820152608083015160808201525092915050565b60006040828403121562000f18578081fd5b604080519081016001600160401b038111828210171562000f3557fe5b604052825181526020928301519281019290925250919050565b60006020828403121562000f61578081fd5b5051919050565b6000806040838503121562000f7b578081fd5b82516001600160401b038082111562000f92578283fd5b908401906102c0828703121562000fa7578283fd5b62000fb46101006200123f565b82518281111562000fc3578485fd5b62000fd18882860162000db7565b82525062000fe3876020850162000e9c565b602082015262000ff78760c0850162000d25565b60408201526200100b6101e0840162000c11565b60608201526200101f610200840162000c11565b608082015262001033610220840162000c11565b60a08201526200104887610240850162000c93565b60c08201526200105c6102a0840162000c11565b60e08201529350620010749150506020840162000c11565b90509250929050565b6000602082840312156200108f578081fd5b815160ff81168114620009a5578182fd5b80518252602090810151910152565b901515815260200190565b60006060820185835260206060818501528186518084526080860191508288019350845b81811015620011055784516001600160a01b031683529383019391830191600101620010de565b505084810360408601528551808252908201925081860190845b81811015620011465782516001600160a01b0316855293830193918301916001016200111f565b509298975050505050505050565b60208101600383106200116357fe5b91905290565b60006020825282518060208401526200118a81604085016020870162001263565b601f01601f19169190910160400192915050565b60006101c0820190508351825260208401516020830152604084015160408301526060840151606083015260808401516080830152620011e360a083018451620010a0565b6020830151620011f760e0840182620010a0565b5060408301516101208301526060830151610140830152608083015161016083015260a083015161018083015260c08301516101a08301529392505050565b90815260200190565b6040518181016001600160401b03811182821017156200125b57fe5b604052919050565b60005b838110156200128057818101518382015260200162001266565b8381111562001290576000848401525b50505050565b60805160a05160601c60c05160e05161010051610120516101405160601c61016051610180516101a0516101c05160601c6101e05160601c61020051610220516102405161026051610280516102a0516102c0516102e05161030051610320516103405161036051610380516103a0516103c0516103e0516104005161042051610440516104605160601c6104805160601c614c4a6200144160003980610b035280610c2d5280610c695280612bf45280612c265280612d6e525080610b5e5280610b9a5280610ff75280612b275280612b595280612d2452508061273c5250806127625250806127885250806127b05250806127d55250806126a95250806126ce5250806126f15250806127195250806125d852508061260252508061262c525080612656525080612680525080612d91525080612d4752505080613f0a525080613f3152508061137352806113ba5280612a3e52508061133352806113fa52806129f2525080610860525080611e9c525080611e785250806111b6525080611956525080611fb8525080611ffa525080611fd95250806111925250806110d75250614c4a6000f3fe608060405234801561001057600080fd5b50600436106102d65760003560e01c806376e54a461161018257806398d5fdca116100e9578063aaabadc5116100a2578063d505accf1161007c578063d505accf146105c8578063d5c096c4146105db578063dd62ed3e146105ee578063f89f27ed14610601576102d6565b8063aaabadc5146105b0578063abd13afe146105b8578063c0ff1a15146105c0576102d6565b806398d5fdca1461054f5780639b02cdde146105575780639d2c110c1461055f5780639ef3e37d14610572578063a457c2d71461058a578063a9059cbb1461059d576102d6565b8063851c1bb31161013b578063851c1bb314610509578063876f303b1461051c57806387ec681714610524578063893d20e8146105375780638d928af81461053f57806395d89b4114610547576102d6565b806376e54a46146104aa5780637be219a2146104bd5780637ecebe00146104d35780638002df18146104e6578063828a7c34146104f95780638456cb5914610501576102d6565b80633aeae0d311610241578063623a2e75116101fa5780636d785a87116101d45780636d785a8714610458578063702ec0911461046057806370a082311461047657806374f3b00914610489576102d6565b8063623a2e751461044057806362ced09214610448578063679aefce14610450576102d6565b80633aeae0d3146103cb5780633f4ba83a146103e057806344bbdb55146103e85780634a6b0b15146103fd57806355c67628146104175780636028bfd41461041f576102d6565b806323b872dd1161029357806323b872dd1461036d578063313ce567146103805780633644e5151461039557806338e9922e1461039d57806338fff2d0146103b057806339509351146103b8576102d6565b806306fdde03146102db578063095ea7b3146102f95780630e608b301461031957806316c38b3c1461032e57806318160ddd146103415780631c0de05114610356575b600080fd5b6102e3610616565b6040516102f09190614a36565b60405180910390f35b61030c61030736600461442d565b6106ac565b6040516102f091906149c9565b61032c610327366004614308565b6106c3565b005b61032c61033c366004614458565b610774565b610349610788565b6040516102f091906149ec565b61035e61078e565b6040516102f0939291906149d4565b61030c61037b366004614378565b6107b7565b61038861082d565b6040516102f09190614ba5565b610349610836565b61032c6103ab36600461473f565b610845565b61034961085e565b61030c6103c636600461442d565b610882565b6103d36108bd565b6040516102f09190614a89565b61032c610908565b6103f06109fd565b6040516102f0919061488d565b610405610a0c565b6040516102f096959493929190614a0c565b610349610a67565b61043261042d366004614472565b610a74565b6040516102f0929190614b57565b610349610aa5565b6103f0610b01565b610349610b25565b6103f0610b4a565b610468610b59565b6040516102f0929190614b70565b610349610484366004614308565b610cfe565b61049c610497366004614472565b610d1d565b6040516102f09291906148b4565b61032c6104b83660046145c9565b610dad565b6104c5610ed4565b6040516102f0929190614ae9565b6103496104e1366004614308565b610ef4565b61032c6104f4366004614308565b610f0f565b6103f0610ff5565b61032c611019565b61034961051736600461457e565b6110d1565b61034961112a565b610432610532366004614472565b61116b565b6103f0611190565b6103f06111b4565b6102e36111d8565b610349611239565b610349611304565b61034961056d366004614648565b61130a565b61057a6116a9565b6040516102f09493929190614b7e565b61030c61059836600461442d565b61181b565b61030c6105ab36600461442d565b611859565b6103f0611866565b6103f0611870565b61034961187f565b61032c6105d63660046143b8565b611927565b61049c6105e9366004614472565b611a97565b6103496105fc366004614340565b611b7c565b610609611bb9565b6040516102f091906148a1565b60038054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156106a25780601f10610677576101008083540402835291602001916106a2565b820191906000526020600020905b81548152906001019060200180831161068557829003601f168201915b5050505050905090565b60006106b9338484611d4d565b5060015b92915050565b600b5460408051808201909152600e81526d1b9bdd08185d5d1a1bdc9a5e995960921b6020820152906001600160a01b0316331461071d5760405162461bcd60e51b81526004016107149190614a36565b60405180910390fd5b50600b80546001600160a01b0319166001600160a01b0383161790556040517fb8fd9afc34c38fcd13b9a3b7646482eb1fddcefb40af2c70609972816eba32089061076990839061488d565b60405180910390a150565b61077c611daf565b61078581611ddd565b50565b60025490565b600080600061079b611e59565b1592506107a6611e76565b91506107b0611e9a565b9050909192565b6000806107c48533611b7c565b90506107e8336001600160a01b03871614806107e05750838210155b61019e611ebe565b6107f3858585611ecc565b336001600160a01b0386161480159061080e57506000198114155b15610820576108208533858403611d4d565b60019150505b9392505050565b60055460ff1690565b6000610840611fb4565b905090565b61084d611daf565b610855612072565b61078581612087565b7f000000000000000000000000000000000000000000000000000000000000000090565b3360008181526001602090815260408083206001600160a01b038716845290915281205490916106b99185906108b890866120f0565b611d4d565b6108c56141c7565b5060408051606081018252600a5460ff81161515825261010081046001600160781b03166020830152600160801b90046001600160801b03169181019190915290565b600c546040805180820190915260118152703737ba103830bab9b29036b0b730b3b2b960791b6020820152906001600160a01b031633146109c75760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b8381101561098c578181015183820152602001610974565b50505050905090810190601f1680156109b95780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b506109d2600061077c565b6040517f215385c8bfc731925ec8068d7baa50f6b6f34417cc7804f7273dc2c0b3db279090600090a1565b600d546001600160a01b031681565b60008060008060008060006008549050610a2581612102565b9650610a308161210e565b9550610a3b8161211b565b9450610a4681612128565b9350610a5181612135565b9250610a5c81612142565b915050909192939495565b6000610840600854612142565b60006060610a848651600261214f565b610a998989898989898961215c6122ca612348565b97509795505050505050565b600080610ab061187f565b90506000610abc610788565b9050600080610acd6009548561245b565b50919350915060009050610aeb82610ae586866124c4565b906124c4565b9050610af785826124dc565b9550505050505090565b7f000000000000000000000000000000000000000000000000000000000000000081565b6000610840610b32610788565b610b44610b3d61187f565b6002612536565b906124dc565b600b546001600160a01b031681565b6000807f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316610b9857670de0b6b3a7640000610c29565b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663679aefce6040518163ffffffff1660e01b815260040160206040518083038186803b158015610bf157600080fd5b505afa158015610c05573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610c299190614757565b91507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316610c6757670de0b6b3a7640000610cf8565b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663679aefce6040518163ffffffff1660e01b815260040160206040518083038186803b158015610cc057600080fd5b505afa158015610cd4573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610cf89190614757565b90509091565b6001600160a01b0381166000908152602081905260409020545b919050565b60608088610d47610d2c6111b4565b6001600160a01b0316336001600160a01b03161460cd611ebe565b610d5c610d5261085e565b82146101f4611ebe565b610d658761255a565b6000806000610d798d8d8d8d8d8d8d61215c565b925092509250610d898c846125bc565b610d92826122ca565b610d9b816122ca565b909c909b509950505050505050505050565b600b5460408051808201909152600e81526d1b9bdd08185d5d1a1bdc9a5e995960921b6020820152906001600160a01b03163314610dfe5760405162461bcd60e51b81526004016107149190614a36565b50600a5460408051808201909152601081526f1c1bdbdb081a5cc81d5b98d85c1c195960821b60208201529060ff16610e4a5760405162461bcd60e51b81526004016107149190614a36565b508051600a8054602084015160408086015160ff19909316941515949094176fffffffffffffffffffffffffffffff0019166101006001600160781b0390921691909102176001600160801b03908116600160801b919092160217815590517f2abf5970d7bc1a2b36eaae52dd2cc353edb0337c7284ef8a01973081b4e044249161076991614abe565b610edc6141e7565b610ee4614216565b610eec6125c6565b915091509091565b6001600160a01b031660009081526006602052604090205490565b600c546040805180820190915260118152703737ba103830bab9b29036b0b730b3b2b960791b60208201526001600160a01b0390911690338214610f945760405162461bcd60e51b815260206004820181815283516024840152835190928392604490910191908501908083836000831561098c578181015183820152602001610974565b50600c80546001600160a01b0319166001600160a01b03848116918217909255604080519284168352602083019190915280517f1ffccd1fa96f4fb3415df8048a0fd107344e262d2d246c85105e2a3f8f3e81989281900390910190a15050565b7f000000000000000000000000000000000000000000000000000000000000000081565b600c546040805180820190915260118152703737ba103830bab9b29036b0b730b3b2b960791b6020820152906001600160a01b0316331461109b5760405162461bcd60e51b815260206004820181815283516024840152835190928392604490910191908501908083836000831561098c578181015183820152602001610974565b506110a6600161077c565b6040517f681eace576127d0553b224436a09801fb47d042942cbff59d7d4ff04d8fa03d790600090a1565b604080517f00000000000000000000000000000000000000000000000000000000000000006020808301919091526001600160e01b03198416828401528251602481840301815260449092019092528051910120919050565b600080611135610788565b905060008061114d60095461114861187f565b61245b565b509193509150611163905081610ae585856124c4565b935050505090565b6000606061117b8651600261214f565b610a99898989898989896127fc61296b612348565b7f000000000000000000000000000000000000000000000000000000000000000090565b7f000000000000000000000000000000000000000000000000000000000000000090565b60048054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156106a25780601f10610677576101008083540402835291602001916106a2565b6000806112446129cd565b90506000806112516125c6565b91509150600073f89a1713998593a441cda571780f0900dbef20f963d83ca8628585856040518463ffffffff1660e01b8152600401611292939291906148d9565b60206040518083038186803b1580156112aa57600080fd5b505af41580156112be573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906112e29190614757565b90506112f084828585612a83565b94506112fb85612b23565b94505050505090565b60095490565b6000611314612072565b8360800151611324610d2c6111b4565b61132f610d5261085e565b60007f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031686602001516001600160a01b03161480156113ab57507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031686604001516001600160a01b0316145b156113b857506001611449565b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031686602001516001600160a01b031614801561143257507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031686604001516001600160a01b0316145b1561143f57506000611449565b6114496079612cc3565b600061145482612d16565b905060006114628315612d16565b905061146e8783612e43565b965061147a8682612e43565b95506000611489888886612e4f565b90506000806114966125c6565b915091506114a261425f565b60008073f89a1713998593a441cda571780f0900dbef20f96378ace8578787876040518463ffffffff1660e01b81526004016114e0939291906148d9565b604080518083038186803b1580156114f757600080fd5b505af415801561150b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061152f91906145a6565b6040805180820190915260029091028201815260208101919091529250600091506115579050565b8c51600181111561156457fe5b1415611610576000611582611577610a67565b60608f015190612ee9565b90506115a461159e828f60600151612f3e90919063ffffffff16565b88612e43565b60608e015260006115b98e878b888888612f54565b90507f4e1d56f7310a8c32b2267f756b19ba65019b4890068ce114a25009abe54de5ba8684836040516115ee9392919061494b565b60405180910390a16116008188612ff7565b9a505050505050505050506116a1565b61161e8c6060015186612e43565b60608d015260006116338d868a878787613003565b90507f4e1d56f7310a8c32b2267f756b19ba65019b4890068ce114a25009abe54de5ba8583836040516116689392919061494b565b60405180910390a161167a818861304b565b905061169661168f61168a610a67565b613057565b829061307d565b995050505050505050505b509392505050565b600d546000908190819081906116d0906001600160a01b03163063045434c560e41b6130e3565b600d546116ee906001600160a01b03163063045434c560e41b61310a565b600d546040516321f8a72160e01b81526001600160a01b03909116906321f8a7219061172e906c4759524f5f545245415355525960981b906004016149ec565b60206040518083038186803b15801561174657600080fd5b505afa15801561175a573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061177e9190614324565b600d546040516321f8a72160e01b81526001600160a01b03909116906321f8a721906117bd906b42414c5f545245415355525960a01b906004016149ec565b60206040518083038186803b1580156117d557600080fd5b505afa1580156117e9573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061180d9190614324565b935093509350935090919293565b6000806118283385611b7c565b90508083106118425761183d33856000611d4d565b61184f565b61184f3385858403611d4d565b5060019392505050565b60006106b9338484611ecc565b6000610840613138565b600c546001600160a01b031681565b60008061188a6129cd565b90506000806118976125c6565b604051636c1e543160e11b8152919350915073f89a1713998593a441cda571780f0900dbef20f99063d83ca862906118d7908690869086906004016148d9565b60206040518083038186803b1580156118ef57600080fd5b505af4158015611903573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111639190614757565b6119358442111560d1611ebe565b6001600160a01b0380881660008181526006602090815260408083205481517f00000000000000000000000000000000000000000000000000000000000000008185015280830195909552948b166060850152608084018a905260a0840185905260c08085018a90528151808603909101815260e090940190528251920191909120906119c1826131b2565b9050600060018288888860405160008152602001604052604051808581526020018460ff1681526020018381526020018281526020019450505050506020604051602081039080840390855afa158015611a1f573d6000803e3d6000fd5b5050604051601f1901519150611a6190506001600160a01b03821615801590611a5957508b6001600160a01b0316826001600160a01b0316145b6101f8611ebe565b6001600160a01b038b166000908152600660205260409020600185019055611a8a8b8b8b611d4d565b5050505050505050505050565b60608088611aa6610d2c6111b4565b611ab1610d5261085e565b611ab9612072565b6000611ac3610788565b611b3357611ad38b8b8b886131fe565b94509050611ae8620f424082101560cc611ebe565b611af66000620f424061338e565b611b0589620f4240830361338e565b611b0e8461296b565b6040805160028082526060820183529091602083019080368337019050509250611b6e565b611b3c8861255a565b611b4b8b8b8b8b8b8b8b6127fc565b90955093509050611b5c898261338e565b611b658461296b565b611b6e836122ca565b505097509795505050505050565b6000611b866111b4565b6001600160a01b0316826001600160a01b03161415611ba857506000196106bd565b611bb28383613398565b90506106bd565b60606108406133c3565b80611bcd81613432565b5050565b611bfd8160600151600013158015611bf55750670de0b6b3a7640000826060015113155b6101636134ab565b611c288160400151600013158015611bf55750670de0b6b3a7640000826040015113156101636134ab565b604080518082018252606083015181529082015160208201526000611c4d82806134b9565b9050611c7a670de0b6b3a763fc188212801590611c725750670de0b6b3a76403e88213155b6101646134ab565b611ca98360800151600013158015611ca157506a52b7d2dcc80cd2e4000000846080015113155b6101686134ab565b505050565b600061082683836056611cd2565b6000611ccc838311156001611ebe565b50900390565b67ffffffffffffffff811b1992909216911b1790565b6000828202831580611d02575082848281611cff57fe5b05145b611d1257611d1260006003611ebe565b670de0b6b3a764000090059392505050565b600082820181831215611d3957838112611d3e565b838112155b61082657610826600080611ebe565b6001600160a01b03808416600081815260016020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6000611dc66000356001600160e01b0319166110d1565b9050610785611dd582336134ec565b610191611ebe565b8015611dfd57611df8611dee611e76565b4210610193611ebe565b611e12565b611e12611e08611e9a565b42106101a9611ebe565b6007805482151560ff19909116811790915560408051918252517f9e3a5e37224532dea67b89face185703738a228a6e8a23dee546960180d3be649181900360200190a150565b6000611e63611e9a565b42118061084057505060075460ff161590565b7f000000000000000000000000000000000000000000000000000000000000000090565b7f000000000000000000000000000000000000000000000000000000000000000090565b81611bcd57611bcd81612cc3565b611ee36001600160a01b0384161515610198611ebe565b611efa6001600160a01b0383161515610199611ebe565b611f05838383611ca9565b6001600160a01b038316600090815260208190526040902054611f2b90826101a06135ee565b6001600160a01b038085166000908152602081905260408082209390935590841681522054611f5a90826120f0565b6001600160a01b038084166000818152602081815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b60007f00000000000000000000000000000000000000000000000000000000000000007f00000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000000612021613604565b3060405160200180868152602001858152602001848152602001838152602001826001600160a01b031681526020019550505050505060405160208183030381529060405280519060200120905090565b61208561207d611e59565b610192611ebe565b565b61209a64e8d4a5100082101560cb611ebe565b6120b067016345785d8a000082111560ca611ebe565b6008546120bd9082611cae565b6008556040517fa9ba3ffe0b6c366b81232caab38605a0699ad5398d6cce76f91ee809e322dafc906107699083906149ec565b60008282016108268482101583611ebe565b60006106bd8282613608565b60006106bd826016613608565b60006106bd82602c61362f565b60006106bd82604b613639565b60006106bd826055613641565b60006106bd82605661364b565b611bcd8183146067611ebe565b600060608060008061216c6125c6565b91509150612178611e59565b1561227d57604051636c1e543160e11b815260009073f89a1713998593a441cda571780f0900dbef20f99063d83ca862906121bb908d90879087906004016148d9565b60206040518083038186803b1580156121d357600080fd5b505af41580156121e7573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061220b9190614757565b905061221681613659565b6122208a88613686565b90965094506122398187612232610788565b60006136d2565b60098190556040517fe1d39caf039c7dc6f364b1e29d1e67c4e330003a43f4991d50cf3e8840d95eac9161226f91849190614b70565b60405180910390a150612293565b6122878987613686565b60001960095590955093505b6040805160028082526060820183528792879291906020830190803683370190505094509450945050509750975097945050505050565b6122f1816000815181106122da57fe5b60200260200101516122ec6001612d16565b6124dc565b816000815181106122fe57fe5b60200260200101818152505061232c8160018151811061231a57fe5b60200260200101516122ec6000612d16565b8160018151811061233957fe5b60200260200101818152505050565b333014612406576000306001600160a01b031660003660405161236c92919061487d565b6000604051808303816000865af19150503d80600081146123a9576040519150601f19603f3d011682016040523d82523d6000602084013e6123ae565b606091505b5050905080600081146123bd57fe5b60046000803e6000516001600160e01b0319166343adbafb60e01b81146123e8573d6000803e3d6000fd5b506020600460003e604060205260243d03602460403e601c3d016000f35b61240f8661255a565b6000806124258b8b8b8b8b8b8b8b63ffffffff16565b5091509150612437818463ffffffff16565b8051601f1982018390526343adbafb603f1983015260200260231982016044820181fd5b60008060008060008060008061246f6116a9565b93509350935093508360001415612494576000975087965090945092506124bb915050565b6000806124ab8c8c6124a4610788565b898961372a565b909a509850929650909450505050505b92959194509250565b60008282018381101561082657610826600080611ebe565b6000816124ef576124ef60006004611ebe565b826124fc575060006106bd565b670de0b6b3a76400008381029084828161251257fe5b04146125245761252460006005611ebe565b82818161252d57fe5b04949350505050565b600082820261082684158061255357508385838161255057fe5b04145b6003611ebe565b6125818160008151811061256a57fe5b602002602001015161257c6001612d16565b6137c5565b8160008151811061258e57fe5b60200260200101818152505061232c816001815181106125aa57fe5b602002602001015161257c6000612d16565b611bcd82826137fe565b6125ce6141e7565b6125d6614216565b7f00000000000000000000000000000000000000000000000000000000000000006080808401919091527f00000000000000000000000000000000000000000000000000000000000000006060808501919091527f00000000000000000000000000000000000000000000000000000000000000006040808601919091527f00000000000000000000000000000000000000000000000000000000000000006020808701919091527f000000000000000000000000000000000000000000000000000000000000000086528451858201517f0000000000000000000000000000000000000000000000000000000000000000818401527f000000000000000000000000000000000000000000000000000000000000000090527f0000000000000000000000000000000000000000000000000000000000000000918101919091527f000000000000000000000000000000000000000000000000000000000000000090527f000000000000000000000000000000000000000000000000000000000000000060c08501527f000000000000000000000000000000000000000000000000000000000000000060a08501527f0000000000000000000000000000000000000000000000000000000000000000928401929092527f0000000000000000000000000000000000000000000000000000000000000000908301527f0000000000000000000000000000000000000000000000000000000000000000908201529091565b600060608060008061280c6125c6565b91509150600073f89a1713998593a441cda571780f0900dbef20f963d83ca8628b85856040518463ffffffff1660e01b815260040161284d939291906148d9565b60206040518083038186803b15801561286557600080fd5b505af4158015612879573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061289d9190614757565b90506128a881613659565b6000806128b58c8a6138b5565b600a54919350915060ff16156128df576128df826128d28f610cfe565b6128da610788565b6138ef565b6128f383836128ec610788565b60016136d2565b60098190556040517fe1d39caf039c7dc6f364b1e29d1e67c4e330003a43f4991d50cf3e8840d95eac9161292991869190614b70565b60405180910390a16040805160028082526060820183528492849291906020830190803683370190505097509750975050505050509750975097945050505050565b6129928160008151811061297b57fe5b602002602001015161298d6001612d16565b61307d565b8160008151811061299f57fe5b60200260200101818152505061232c816001815181106129bb57fe5b602002602001015161298d6000612d16565b6040805160028082526060808301845292602083019080368337019050509050612a207f0000000000000000000000000000000000000000000000000000000000000000612a1b6001612d16565b613a01565b81600081518110612a2d57fe5b602002602001018181525050612a677f0000000000000000000000000000000000000000000000000000000000000000612a1b6000612d16565b81600181518110612a7457fe5b60200260200101818152505090565b600073f89a1713998593a441cda571780f0900dbef20f963c7fe78d0868585612aab89613aa8565b6040518563ffffffff1660e01b8152600401612aca949392919061490a565b60206040518083038186803b158015612ae257600080fd5b505af4158015612af6573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612b1a9190614757565b95945050505050565b60007f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031615612bf257612bef7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663679aefce6040518163ffffffff1660e01b815260040160206040518083038186803b158015612bb057600080fd5b505afa158015612bc4573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612be89190614757565b83906137c5565b91505b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031615612cbf57612cbc7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663679aefce6040518163ffffffff1660e01b815260040160206040518083038186803b158015612c7d57600080fd5b505afa158015612c91573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612cb59190614757565b83906124dc565b91505b5090565b62461bcd60e51b6000908152602060045260076024526642414c23000030600a808404818106603090810160081b95839006959095019082900491820690940160101b939093010160c81b604452606490fd5b60008060008315612d6b57507f000000000000000000000000000000000000000000000000000000000000000090507f0000000000000000000000000000000000000000000000000000000000000000612db1565b507f000000000000000000000000000000000000000000000000000000000000000090507f00000000000000000000000000000000000000000000000000000000000000005b6001600160a01b0382161561082657612e3b826001600160a01b031663679aefce6040518163ffffffff1660e01b815260040160206040518083038186803b158015612dfc57600080fd5b505afa158015612e10573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612e349190614757565b82906137c5565b949350505050565b600061082683836137c5565b60408051600280825260608083018452926020830190803683370190505090508115612eae578381600081518110612e8357fe5b6020026020010181815250508281600181518110612e9d57fe5b602002602001018181525050610826565b8281600081518110612ebc57fe5b6020026020010181815250508381600181518110612ed657fe5b6020026020010181815250509392505050565b6000828202831580612f03575082848281612f0057fe5b04145b612f1357612f1360006003611ebe565b80612f225760009150506106bd565b670de0b6b3a764000060001982015b0460010191505092915050565b600082821115611ccc57611ccc60006001611ebe565b60608601516040516330ffa11b60e11b815260009173f89a1713998593a441cda571780f0900dbef20f9916361ff423691612f9c918a91908a908a908a908a9060040161497b565b60206040518083038186803b158015612fb457600080fd5b505af4158015612fc8573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612fec9190614757565b979650505050505050565b600061082683836124dc565b6060860151604051633b18ce7b60e21b815260009173f89a1713998593a441cda571780f0900dbef20f99163ec6339ec91612f9c918a91908a908a908a908a9060040161497b565b6000610826838361307d565b6000670de0b6b3a7640000821061306f5760006106bd565b50670de0b6b3a76400000390565b6000816130905761309060006004611ebe565b8261309d575060006106bd565b670de0b6b3a7640000838102908482816130b357fe5b04146130d7576130d7670de0b6b3a76400008583816130ce57fe5b04146005611ebe565b826001820381612f3157fe5b6000612e3b847550524f544f434f4c5f535741505f4645455f5045524360501b8486613aec565b6000612e3b847f50524f544f434f4c5f4645455f4759524f5f504f5254494f4e000000000000008486613aec565b60006131426111b4565b6001600160a01b031663aaabadc56040518163ffffffff1660e01b815260040160206040518083038186803b15801561317a57600080fd5b505afa15801561318e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906108409190614324565b60006131bc611fb4565b82604051602001808061190160f01b81525060020183815260200182815260200192505050604051602081830303815290604052805190602001209050919050565b60006060600061320d84613d99565b9050613228600082600381111561322057fe5b1460ce611ebe565b600061323385613db8565b90506132418151600261214f565b61324a8161255a565b6000806132556125c6565b91509150600073f89a1713998593a441cda571780f0900dbef20f963d83ca8628585856040518463ffffffff1660e01b8152600401613296939291906148d9565b60206040518083038186803b1580156132ae57600080fd5b505af41580156132c2573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906132e69190614757565b90507faf65f1a72bf4f8bc48e8153858e1f7b307ce39c88361a677c0a79c2a1ae571ef8160405161331791906149ec565b60405180910390a1600061332d85838686612a83565b9050600061337561335b838860008151811061334557fe5b60200260200101516137c590919063ffffffff16565b8760018151811061336857fe5b60200260200101516120f0565b60099390935550909b939a509298505050505050505050565b611bcd8282613e6c565b6001600160a01b03918216600090815260016020908152604080832093909416825291909152205490565b604080516002808252606080830184529260009291906020830190803683370190505090506133f26001613f01565b816000815181106133ff57fe5b6020026020010181815250506134156000613f01565b8160018151811061342257fe5b6020908102919091010152905090565b60028151101561344157610785565b60008160008151811061345057fe5b602002602001015190506000600190505b8251811015611ca957600083828151811061347857fe5b602002602001015190506134a1816001600160a01b0316846001600160a01b0316106065611ebe565b9150600101613461565b81611bcd57611bcd81613f55565b60006108266134d983602001518560200151611ce890919063ffffffff16565b835185516134e691611ce8565b90611d24565b600073ba1ba1ba1ba1ba1ba1ba1ba1ba1ba1ba1ba1ba1b61350b611190565b6001600160a01b031614158015613526575061352683613f65565b1561354e57613533611190565b6001600160a01b0316336001600160a01b03161490506106bd565b613556613138565b6001600160a01b0316639be2a8848484306040518463ffffffff1660e01b815260040180848152602001836001600160a01b03168152602001826001600160a01b03168152602001935050505060206040518083038186803b1580156135bb57600080fd5b505afa1580156135cf573d6000803e3d6000fd5b505050506040513d60208110156135e557600080fd5b505190506106bd565b60006135fd8484111583611ebe565b5050900390565b4690565b600082821c623fffff16621fffff81136136225780612e3b565b623fffff19179392505050565b1c637fffffff1690565b1c6103ff1690565b1c60019081161490565b1c67ffffffffffffffff1690565b60008060008061366b6009548661245b565b935093509350935061367f84848484613f97565b5050505050565b60006060600061369584613d99565b905060018160038111156136a557fe5b14156136bf576136b58585613fbd565b90935091506136ca565b6136ca610150612cc3565b509250929050565b600081156137045760006136f0846136ea8888612ee9565b9061307d565b90506136fc86826124c4565b915050612e3b565b600061371484610b4488886137c5565b90506137208682612f3e565b9695505050505050565b60008086861161373f575060009050806137bb565b600061375f856137596137528a8c612f3e565b89906137c5565b906137c5565b90506000613777613770898b612f3e565b87906137c5565b905060006137858983612f3e565b9050600061379384836124dc565b905060006137a188836137c5565b905060006137af8383612f3e565b91975090955050505050505b9550959350505050565b60008282028315806137df5750828482816137dc57fe5b04145b6137ef576137ef60006003611ebe565b670de0b6b3a76400008161252d565b6138156001600160a01b038316151561019b611ebe565b61382182600083611ca9565b6001600160a01b03821660009081526020819052604090205461384790826101a16135ee565b6001600160a01b03831660009081526020819052604090205560025461386d9082613fef565b6002556040805182815290516000916001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9181900360200190a35050565b6000606060006138c484613d99565b905060038160038111156138d457fe5b14156138e4576136b58585613ffd565b6136ca610136612cc3565b60408051606081018252600a5460ff81161515825261010081046001600160781b031660208301819052600160801b9091046001600160801b0316928201929092529061393c85856124c4565b11156040518060400160405280601a81526020017f6f7665722061646472657373206c697175696469747920636170000000000000815250906139925760405162461bcd60e51b81526004016107149190614a36565b5060408101516001600160801b03166139ab85846124c4565b11156040518060400160405280601981526020017f6f76657220676c6f62616c206c697175696469747920636170000000000000008152509061367f5760405162461bcd60e51b81526004016107149190614a36565b6000806000613a0e6111b4565b6001600160a01b031663b05f8e48613a2461085e565b876040518363ffffffff1660e01b8152600401613a429291906149f5565b60806040518083038186803b158015613a5a57600080fd5b505afa158015613a6e573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190613a92919061476f565b505081810194509092509050612b1a83856137c5565b6000600160ff1b8210612cbf5760405162461bcd60e51b8152600401808060200182810382526028815260200180614bed6028913960400191505060405180910390fd5b6040805160208082018690526001600160a01b03808516838501528351808403850181526060840180865281519184019190912063696eb37560e01b909152606484018190529351600094939189169263696eb3759260848082019391829003018186803b158015613b5d57600080fd5b505afa158015613b71573d6000803e3d6000fd5b505050506040513d6020811015613b8757600080fd5b505115613c0757856001600160a01b031663bd02d0f5826040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b158015613bd257600080fd5b505afa158015613be6573d6000803e3d6000fd5b505050506040513d6020811015613bfc57600080fd5b50519150612e3b9050565b6040805160208082018890528183018790528251808303840181526060830180855281519183019190912063696eb37560e01b9091526064830181905292516001600160a01b038a169263696eb375926084808301939192829003018186803b158015613c7357600080fd5b505afa158015613c87573d6000803e3d6000fd5b505050506040513d6020811015613c9d57600080fd5b505115613d1e57866001600160a01b031663bd02d0f5826040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b158015613ce857600080fd5b505afa158015613cfc573d6000803e3d6000fd5b505050506040513d6020811015613d1257600080fd5b50519250612e3b915050565b866001600160a01b031663bd02d0f5876040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b158015613d6257600080fd5b505afa158015613d76573d6000803e3d6000fd5b505050506040513d6020811015613d8c57600080fd5b5051979650505050505050565b6000818060200190516020811015613db057600080fd5b505192915050565b6060818060200190516040811015613dcf57600080fd5b815160208301805160405192949293830192919084640100000000821115613df657600080fd5b908301906020820185811115613e0b57600080fd5b8251866020820283011164010000000082111715613e2857600080fd5b82525081516020918201928201910280838360005b83811015613e55578181015183820152602001613e3d565b505050509190910160405250929695505050505050565b613e7860008383611ca9565b600254613e8590826120f0565b6002556001600160a01b038216600090815260208190526040902054613eab90826120f0565b6001600160a01b0383166000818152602081815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b600081613f2e577f00000000000000000000000000000000000000000000000000000000000000006106bd565b507f0000000000000000000000000000000000000000000000000000000000000000919050565b610785816223aca960e91b614022565b6000613f77631c74c91760e11b6110d1565b8214806106bd5750613f8f6350dd6ed960e01b6110d1565b909114919050565b8315613fa757613fa7828561338e565b8215613fb757613fb7818461338e565b50505050565b600060606000613fcc84614083565b90506000613fe28683613fdd610788565b6140a5565b9196919550909350505050565b6000610826838360016135ee565b60006060600061400c84614083565b90506000613fe2868361401d610788565b61412b565b62461bcd60e51b600090815260206004526007602452600a808404818106603090810160081b958390069590950190829004918206850160101b01602363ffffff0060e086901c160160181b0190930160c81b60445260e882901c90606490fd5b600081806020019051604081101561409a57600080fd5b506020015192915050565b6060835167ffffffffffffffff811180156140bf57600080fd5b506040519080825280602002602001820160405280156140e9578160200160208202803683370190505b50905060005b84518110156116a15761410c83610b448688858151811061334557fe5b82828151811061411857fe5b60209081029190910101526001016140ef565b6060835167ffffffffffffffff8111801561414557600080fd5b5060405190808252806020026020018201604052801561416f578160200160208202803683370190505b50905060005b84518110156116a1576141a8836136ea8688858151811061419257fe5b6020026020010151612ee990919063ffffffff16565b8282815181106141b457fe5b6020908102919091010152600101614175565b604080516060810182526000808252602082018190529181019190915290565b6040518060a0016040528060008152602001600081526020016000815260200160008152602001600081525090565b6040518060e0016040528061422961425f565b815260200161423661425f565b815260200160008152602001600081526020016000815260200160008152602001600081525090565b604051806040016040528060008152602001600081525090565b8035610d1881614bd7565b80358015158114610d1857600080fd5b600082601f8301126142a4578081fd5b813567ffffffffffffffff8111156142b857fe5b6142cb601f8201601f1916602001614bb3565b8181528460208386010111156142df578283fd5b816020850160208301379081016020019190915292915050565b803560028110610d1857600080fd5b600060208284031215614319578081fd5b813561082681614bd7565b600060208284031215614335578081fd5b815161082681614bd7565b60008060408385031215614352578081fd5b823561435d81614bd7565b9150602083013561436d81614bd7565b809150509250929050565b60008060006060848603121561438c578081fd5b833561439781614bd7565b925060208401356143a781614bd7565b929592945050506040919091013590565b600080600080600080600060e0888a0312156143d2578283fd5b87356143dd81614bd7565b965060208801356143ed81614bd7565b95506040880135945060608801359350608088013560ff81168114614410578384fd5b9699959850939692959460a0840135945060c09093013592915050565b6000806040838503121561443f578182fd5b823561444a81614bd7565b946020939093013593505050565b600060208284031215614469578081fd5b61082682614284565b600080600080600080600060e0888a03121561448c578081fd5b87359650602088013561449e81614bd7565b955060408801356144ae81614bd7565b9450606088013567ffffffffffffffff808211156144ca578283fd5b818a0191508a601f8301126144dd578283fd5b8135818111156144e957fe5b602081026144f960208201614bb3565b80838252602082019150602086018f6020858901011115614518578788fd5b8796505b8487101561453b5780358352600196909601956020928301920161451c565b50985050505060808a0135945060a08a0135935060c08a0135915080821115614562578283fd5b5061456f8a828b01614294565b91505092959891949750929550565b60006020828403121561458f578081fd5b81356001600160e01b031981168114610826578182fd5b600080604083850312156145b8578182fd5b505080516020909101519092909150565b6000606082840312156145da578081fd5b6040516060810181811067ffffffffffffffff821117156145f757fe5b60405261460383614284565b815260208301356001600160781b038116811461461e578283fd5b602082015260408301356001600160801b038116811461463c578283fd5b60408201529392505050565b60008060006060848603121561465c578081fd5b833567ffffffffffffffff80821115614673578283fd5b8186019150610120808389031215614689578384fd5b61469281614bb3565b905061469d836142f9565b81526146ab60208401614279565b60208201526146bc60408401614279565b6040820152606083013560608201526080830135608082015260a083013560a08201526146eb60c08401614279565b60c08201526146fc60e08401614279565b60e08201526101008084013583811115614714578586fd5b6147208a828701614294565b9183019190915250976020870135975060409096013595945050505050565b600060208284031215614750578081fd5b5035919050565b600060208284031215614768578081fd5b5051919050565b60008060008060808587031215614784578182fd5b84519350602085015192506040850151915060608501516147a481614bd7565b939692955090935050565b6000815180845260208085019450808401835b838110156147de578151875295820195908201906001016147c2565b509495945050505050565b6147f482825161486e565b6020810151614806604084018261486e565b5060408101516080830152606081015160a0830152608081015160c083015260a081015160e083015260c08101516101008301525050565b80518252602081015160208301526040810151604083015260608101516060830152608081015160808301525050565b80518252602090810151910152565b6000828483379101908152919050565b6001600160a01b0391909116815260200190565b60006020825261082660208301846147af565b6000604082526148c760408301856147af565b8281036020840152612b1a81856147af565b60006101e08083526148ed818401876147af565b9150506148fd602083018561483e565b612e3b60c08301846147e9565b600061020080835261491e818401886147af565b91505061492e602083018661483e565b61493b60c08301856147e9565b826101e083015295945050505050565b60006080825261495e60808301866147af565b905061496d602083018561486e565b826060830152949350505050565b600061026080835261498f8184018a6147af565b91505086602083015285151560408301526149ad606083018661483e565b6149bb6101008301856147e9565b612fec61022083018461486e565b901515815260200190565b92151583526020830191909152604082015260600190565b90815260200190565b9182526001600160a01b0316602082015260400190565b9586526020860194909452604085019290925260608401521515608083015260a082015260c00190565b6000602080835283518082850152825b81811015614a6257858101830151858201604001528201614a46565b81811115614a735783604083870101525b50601f01601f1916929092016040019392505050565b8151151581526020808301516001600160781b0316908201526040918201516001600160801b03169181019190915260600190565b905460ff811615158252600881901c6001600160781b0316602083015260801c604082015260600190565b6101c08101614af8828561483e565b614b0660a08301845161486e565b6020830151614b1860e084018261486e565b5060408301516101208301526060830151610140830152608083015161016083015260a083015161018083015260c08301516101a08301529392505050565b600083825260406020830152612e3b60408301846147af565b918252602082015260400190565b93845260208401929092526001600160a01b03908116604084015216606082015260800190565b60ff91909116815260200190565b60405181810167ffffffffffffffff81118282101715614bcf57fe5b604052919050565b6001600160a01b038116811461078557600080fdfe53616665436173743a2076616c756520646f65736e27742066697420696e20616e20696e74323536a2646970667358221220804f414f2fd23de5749b22830088e0b3ca1b44e5fcef57a5d2128c45dda1014064736f6c634300070600330000000000000000000000000000000000000000000000000000000000000040000000000000000000000000ac89cc9d78bbad7eb3a02601b4d65daa1f908aa600000000000000000000000000000000000000000000000000000000000002c00000000000000000000000000000000000000000000000000dd60e37b91080000000000000000000000000000000000000000000000000000deb5f2f95b77fff00000000000000000000000000000000000000000000000009d025defee4df4400000000000000000000000000000000000000000000000009d025defee4df44000000000000000000000000000000000000000000000066ffcbfd5e5a300000ffffffffffffffffffffffffffffffffb8fff04ffc51e6c366c5e67beddd88f20000000000000000000000000000000018dfffbf541b893374eb70afd7d309480000000000000000000000000000000046fa15be44911890f6c1f95e3e44a3750000000000000000000000000000000018f10816fb42ce33f7252b486856dd420000000000000000000000000000000046fd12b7241f98e0fb536ae08e145ece0000000000000000000000000000000018e883eb27af2bb1ad18d26ae7673383000000000000000000000000000000000008842bd393a280406ac0d768fe3f24fffffffffffffffffffffffffffffffffffd030720717faa2f026eadc9198379000000000000000000000000000000004b3b4ca85a86c473e421ffd60063dc8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d096c2ebe242801466e6f1ac2bf5228ce1fd445c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d096c2ebe242801466e6f1ac2bf5228ce1fd445c000000000000000000000000ba12222222228d8ba445958a75a0704d566bf2c800000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000160000000000000000000000000056fd409e1d7a124bd7017459dfea2f387b6d5cd0000000000000000000000008e870d67f660d95d5be530380d0ec0bd388289e100000000000000000000000000000000000000000000000000005af3107a40000000000000000000000000000000000000000000000000000000000012cc03000000000000000000000000000000000000000000000000000000000000278d00000000000000000000000000d096c2ebe242801466e6f1ac2bf5228ce1fd445c00000000000000000000000000000000000000000000000000000000000000184779726f73636f70652045434c5020555344502f475553440000000000000000000000000000000000000000000000000000000000000000000000000000000e45434c502d555344502d47555344000000000000000000000000000000000000
Deployed Bytecode
0x608060405234801561001057600080fd5b50600436106102d65760003560e01c806376e54a461161018257806398d5fdca116100e9578063aaabadc5116100a2578063d505accf1161007c578063d505accf146105c8578063d5c096c4146105db578063dd62ed3e146105ee578063f89f27ed14610601576102d6565b8063aaabadc5146105b0578063abd13afe146105b8578063c0ff1a15146105c0576102d6565b806398d5fdca1461054f5780639b02cdde146105575780639d2c110c1461055f5780639ef3e37d14610572578063a457c2d71461058a578063a9059cbb1461059d576102d6565b8063851c1bb31161013b578063851c1bb314610509578063876f303b1461051c57806387ec681714610524578063893d20e8146105375780638d928af81461053f57806395d89b4114610547576102d6565b806376e54a46146104aa5780637be219a2146104bd5780637ecebe00146104d35780638002df18146104e6578063828a7c34146104f95780638456cb5914610501576102d6565b80633aeae0d311610241578063623a2e75116101fa5780636d785a87116101d45780636d785a8714610458578063702ec0911461046057806370a082311461047657806374f3b00914610489576102d6565b8063623a2e751461044057806362ced09214610448578063679aefce14610450576102d6565b80633aeae0d3146103cb5780633f4ba83a146103e057806344bbdb55146103e85780634a6b0b15146103fd57806355c67628146104175780636028bfd41461041f576102d6565b806323b872dd1161029357806323b872dd1461036d578063313ce567146103805780633644e5151461039557806338e9922e1461039d57806338fff2d0146103b057806339509351146103b8576102d6565b806306fdde03146102db578063095ea7b3146102f95780630e608b301461031957806316c38b3c1461032e57806318160ddd146103415780631c0de05114610356575b600080fd5b6102e3610616565b6040516102f09190614a36565b60405180910390f35b61030c61030736600461442d565b6106ac565b6040516102f091906149c9565b61032c610327366004614308565b6106c3565b005b61032c61033c366004614458565b610774565b610349610788565b6040516102f091906149ec565b61035e61078e565b6040516102f0939291906149d4565b61030c61037b366004614378565b6107b7565b61038861082d565b6040516102f09190614ba5565b610349610836565b61032c6103ab36600461473f565b610845565b61034961085e565b61030c6103c636600461442d565b610882565b6103d36108bd565b6040516102f09190614a89565b61032c610908565b6103f06109fd565b6040516102f0919061488d565b610405610a0c565b6040516102f096959493929190614a0c565b610349610a67565b61043261042d366004614472565b610a74565b6040516102f0929190614b57565b610349610aa5565b6103f0610b01565b610349610b25565b6103f0610b4a565b610468610b59565b6040516102f0929190614b70565b610349610484366004614308565b610cfe565b61049c610497366004614472565b610d1d565b6040516102f09291906148b4565b61032c6104b83660046145c9565b610dad565b6104c5610ed4565b6040516102f0929190614ae9565b6103496104e1366004614308565b610ef4565b61032c6104f4366004614308565b610f0f565b6103f0610ff5565b61032c611019565b61034961051736600461457e565b6110d1565b61034961112a565b610432610532366004614472565b61116b565b6103f0611190565b6103f06111b4565b6102e36111d8565b610349611239565b610349611304565b61034961056d366004614648565b61130a565b61057a6116a9565b6040516102f09493929190614b7e565b61030c61059836600461442d565b61181b565b61030c6105ab36600461442d565b611859565b6103f0611866565b6103f0611870565b61034961187f565b61032c6105d63660046143b8565b611927565b61049c6105e9366004614472565b611a97565b6103496105fc366004614340565b611b7c565b610609611bb9565b6040516102f091906148a1565b60038054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156106a25780601f10610677576101008083540402835291602001916106a2565b820191906000526020600020905b81548152906001019060200180831161068557829003601f168201915b5050505050905090565b60006106b9338484611d4d565b5060015b92915050565b600b5460408051808201909152600e81526d1b9bdd08185d5d1a1bdc9a5e995960921b6020820152906001600160a01b0316331461071d5760405162461bcd60e51b81526004016107149190614a36565b60405180910390fd5b50600b80546001600160a01b0319166001600160a01b0383161790556040517fb8fd9afc34c38fcd13b9a3b7646482eb1fddcefb40af2c70609972816eba32089061076990839061488d565b60405180910390a150565b61077c611daf565b61078581611ddd565b50565b60025490565b600080600061079b611e59565b1592506107a6611e76565b91506107b0611e9a565b9050909192565b6000806107c48533611b7c565b90506107e8336001600160a01b03871614806107e05750838210155b61019e611ebe565b6107f3858585611ecc565b336001600160a01b0386161480159061080e57506000198114155b15610820576108208533858403611d4d565b60019150505b9392505050565b60055460ff1690565b6000610840611fb4565b905090565b61084d611daf565b610855612072565b61078581612087565b7f81e998523f02adf4679ff57fff8ca2b9d23a574700020000000000000000060a90565b3360008181526001602090815260408083206001600160a01b038716845290915281205490916106b99185906108b890866120f0565b611d4d565b6108c56141c7565b5060408051606081018252600a5460ff81161515825261010081046001600160781b03166020830152600160801b90046001600160801b03169181019190915290565b600c546040805180820190915260118152703737ba103830bab9b29036b0b730b3b2b960791b6020820152906001600160a01b031633146109c75760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b8381101561098c578181015183820152602001610974565b50505050905090810190601f1680156109b95780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b506109d2600061077c565b6040517f215385c8bfc731925ec8068d7baa50f6b6f34417cc7804f7273dc2c0b3db279090600090a1565b600d546001600160a01b031681565b60008060008060008060006008549050610a2581612102565b9650610a308161210e565b9550610a3b8161211b565b9450610a4681612128565b9350610a5181612135565b9250610a5c81612142565b915050909192939495565b6000610840600854612142565b60006060610a848651600261214f565b610a998989898989898961215c6122ca612348565b97509795505050505050565b600080610ab061187f565b90506000610abc610788565b9050600080610acd6009548561245b565b50919350915060009050610aeb82610ae586866124c4565b906124c4565b9050610af785826124dc565b9550505050505090565b7f000000000000000000000000000000000000000000000000000000000000000081565b6000610840610b32610788565b610b44610b3d61187f565b6002612536565b906124dc565b600b546001600160a01b031681565b6000807f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316610b9857670de0b6b3a7640000610c29565b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663679aefce6040518163ffffffff1660e01b815260040160206040518083038186803b158015610bf157600080fd5b505afa158015610c05573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610c299190614757565b91507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316610c6757670de0b6b3a7640000610cf8565b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663679aefce6040518163ffffffff1660e01b815260040160206040518083038186803b158015610cc057600080fd5b505afa158015610cd4573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610cf89190614757565b90509091565b6001600160a01b0381166000908152602081905260409020545b919050565b60608088610d47610d2c6111b4565b6001600160a01b0316336001600160a01b03161460cd611ebe565b610d5c610d5261085e565b82146101f4611ebe565b610d658761255a565b6000806000610d798d8d8d8d8d8d8d61215c565b925092509250610d898c846125bc565b610d92826122ca565b610d9b816122ca565b909c909b509950505050505050505050565b600b5460408051808201909152600e81526d1b9bdd08185d5d1a1bdc9a5e995960921b6020820152906001600160a01b03163314610dfe5760405162461bcd60e51b81526004016107149190614a36565b50600a5460408051808201909152601081526f1c1bdbdb081a5cc81d5b98d85c1c195960821b60208201529060ff16610e4a5760405162461bcd60e51b81526004016107149190614a36565b508051600a8054602084015160408086015160ff19909316941515949094176fffffffffffffffffffffffffffffff0019166101006001600160781b0390921691909102176001600160801b03908116600160801b919092160217815590517f2abf5970d7bc1a2b36eaae52dd2cc353edb0337c7284ef8a01973081b4e044249161076991614abe565b610edc6141e7565b610ee4614216565b610eec6125c6565b915091509091565b6001600160a01b031660009081526006602052604090205490565b600c546040805180820190915260118152703737ba103830bab9b29036b0b730b3b2b960791b60208201526001600160a01b0390911690338214610f945760405162461bcd60e51b815260206004820181815283516024840152835190928392604490910191908501908083836000831561098c578181015183820152602001610974565b50600c80546001600160a01b0319166001600160a01b03848116918217909255604080519284168352602083019190915280517f1ffccd1fa96f4fb3415df8048a0fd107344e262d2d246c85105e2a3f8f3e81989281900390910190a15050565b7f000000000000000000000000000000000000000000000000000000000000000081565b600c546040805180820190915260118152703737ba103830bab9b29036b0b730b3b2b960791b6020820152906001600160a01b0316331461109b5760405162461bcd60e51b815260206004820181815283516024840152835190928392604490910191908501908083836000831561098c578181015183820152602001610974565b506110a6600161077c565b6040517f681eace576127d0553b224436a09801fb47d042942cbff59d7d4ff04d8fa03d790600090a1565b604080517f000000000000000000000000412a5b2e7a678471985542757a6855847d4931d56020808301919091526001600160e01b03198416828401528251602481840301815260449092019092528051910120919050565b600080611135610788565b905060008061114d60095461114861187f565b61245b565b509193509150611163905081610ae585856124c4565b935050505090565b6000606061117b8651600261214f565b610a99898989898989896127fc61296b612348565b7f000000000000000000000000d096c2ebe242801466e6f1ac2bf5228ce1fd445c90565b7f000000000000000000000000ba12222222228d8ba445958a75a0704d566bf2c890565b60048054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156106a25780601f10610677576101008083540402835291602001916106a2565b6000806112446129cd565b90506000806112516125c6565b91509150600073f89a1713998593a441cda571780f0900dbef20f963d83ca8628585856040518463ffffffff1660e01b8152600401611292939291906148d9565b60206040518083038186803b1580156112aa57600080fd5b505af41580156112be573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906112e29190614757565b90506112f084828585612a83565b94506112fb85612b23565b94505050505090565b60095490565b6000611314612072565b8360800151611324610d2c6111b4565b61132f610d5261085e565b60007f000000000000000000000000056fd409e1d7a124bd7017459dfea2f387b6d5cd6001600160a01b031686602001516001600160a01b03161480156113ab57507f0000000000000000000000008e870d67f660d95d5be530380d0ec0bd388289e16001600160a01b031686604001516001600160a01b0316145b156113b857506001611449565b7f0000000000000000000000008e870d67f660d95d5be530380d0ec0bd388289e16001600160a01b031686602001516001600160a01b031614801561143257507f000000000000000000000000056fd409e1d7a124bd7017459dfea2f387b6d5cd6001600160a01b031686604001516001600160a01b0316145b1561143f57506000611449565b6114496079612cc3565b600061145482612d16565b905060006114628315612d16565b905061146e8783612e43565b965061147a8682612e43565b95506000611489888886612e4f565b90506000806114966125c6565b915091506114a261425f565b60008073f89a1713998593a441cda571780f0900dbef20f96378ace8578787876040518463ffffffff1660e01b81526004016114e0939291906148d9565b604080518083038186803b1580156114f757600080fd5b505af415801561150b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061152f91906145a6565b6040805180820190915260029091028201815260208101919091529250600091506115579050565b8c51600181111561156457fe5b1415611610576000611582611577610a67565b60608f015190612ee9565b90506115a461159e828f60600151612f3e90919063ffffffff16565b88612e43565b60608e015260006115b98e878b888888612f54565b90507f4e1d56f7310a8c32b2267f756b19ba65019b4890068ce114a25009abe54de5ba8684836040516115ee9392919061494b565b60405180910390a16116008188612ff7565b9a505050505050505050506116a1565b61161e8c6060015186612e43565b60608d015260006116338d868a878787613003565b90507f4e1d56f7310a8c32b2267f756b19ba65019b4890068ce114a25009abe54de5ba8583836040516116689392919061494b565b60405180910390a161167a818861304b565b905061169661168f61168a610a67565b613057565b829061307d565b995050505050505050505b509392505050565b600d546000908190819081906116d0906001600160a01b03163063045434c560e41b6130e3565b600d546116ee906001600160a01b03163063045434c560e41b61310a565b600d546040516321f8a72160e01b81526001600160a01b03909116906321f8a7219061172e906c4759524f5f545245415355525960981b906004016149ec565b60206040518083038186803b15801561174657600080fd5b505afa15801561175a573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061177e9190614324565b600d546040516321f8a72160e01b81526001600160a01b03909116906321f8a721906117bd906b42414c5f545245415355525960a01b906004016149ec565b60206040518083038186803b1580156117d557600080fd5b505afa1580156117e9573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061180d9190614324565b935093509350935090919293565b6000806118283385611b7c565b90508083106118425761183d33856000611d4d565b61184f565b61184f3385858403611d4d565b5060019392505050565b60006106b9338484611ecc565b6000610840613138565b600c546001600160a01b031681565b60008061188a6129cd565b90506000806118976125c6565b604051636c1e543160e11b8152919350915073f89a1713998593a441cda571780f0900dbef20f99063d83ca862906118d7908690869086906004016148d9565b60206040518083038186803b1580156118ef57600080fd5b505af4158015611903573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111639190614757565b6119358442111560d1611ebe565b6001600160a01b0380881660008181526006602090815260408083205481517f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c98185015280830195909552948b166060850152608084018a905260a0840185905260c08085018a90528151808603909101815260e090940190528251920191909120906119c1826131b2565b9050600060018288888860405160008152602001604052604051808581526020018460ff1681526020018381526020018281526020019450505050506020604051602081039080840390855afa158015611a1f573d6000803e3d6000fd5b5050604051601f1901519150611a6190506001600160a01b03821615801590611a5957508b6001600160a01b0316826001600160a01b0316145b6101f8611ebe565b6001600160a01b038b166000908152600660205260409020600185019055611a8a8b8b8b611d4d565b5050505050505050505050565b60608088611aa6610d2c6111b4565b611ab1610d5261085e565b611ab9612072565b6000611ac3610788565b611b3357611ad38b8b8b886131fe565b94509050611ae8620f424082101560cc611ebe565b611af66000620f424061338e565b611b0589620f4240830361338e565b611b0e8461296b565b6040805160028082526060820183529091602083019080368337019050509250611b6e565b611b3c8861255a565b611b4b8b8b8b8b8b8b8b6127fc565b90955093509050611b5c898261338e565b611b658461296b565b611b6e836122ca565b505097509795505050505050565b6000611b866111b4565b6001600160a01b0316826001600160a01b03161415611ba857506000196106bd565b611bb28383613398565b90506106bd565b60606108406133c3565b80611bcd81613432565b5050565b611bfd8160600151600013158015611bf55750670de0b6b3a7640000826060015113155b6101636134ab565b611c288160400151600013158015611bf55750670de0b6b3a7640000826040015113156101636134ab565b604080518082018252606083015181529082015160208201526000611c4d82806134b9565b9050611c7a670de0b6b3a763fc188212801590611c725750670de0b6b3a76403e88213155b6101646134ab565b611ca98360800151600013158015611ca157506a52b7d2dcc80cd2e4000000846080015113155b6101686134ab565b505050565b600061082683836056611cd2565b6000611ccc838311156001611ebe565b50900390565b67ffffffffffffffff811b1992909216911b1790565b6000828202831580611d02575082848281611cff57fe5b05145b611d1257611d1260006003611ebe565b670de0b6b3a764000090059392505050565b600082820181831215611d3957838112611d3e565b838112155b61082657610826600080611ebe565b6001600160a01b03808416600081815260016020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6000611dc66000356001600160e01b0319166110d1565b9050610785611dd582336134ec565b610191611ebe565b8015611dfd57611df8611dee611e76565b4210610193611ebe565b611e12565b611e12611e08611e9a565b42106101a9611ebe565b6007805482151560ff19909116811790915560408051918252517f9e3a5e37224532dea67b89face185703738a228a6e8a23dee546960180d3be649181900360200190a150565b6000611e63611e9a565b42118061084057505060075460ff161590565b7f0000000000000000000000000000000000000000000000000000000077ffd6b390565b7f00000000000000000000000000000000000000000000000000000000782763b390565b81611bcd57611bcd81612cc3565b611ee36001600160a01b0384161515610198611ebe565b611efa6001600160a01b0383161515610199611ebe565b611f05838383611ca9565b6001600160a01b038316600090815260208190526040902054611f2b90826101a06135ee565b6001600160a01b038085166000908152602081905260408082209390935590841681522054611f5a90826120f0565b6001600160a01b038084166000818152602081815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b60007f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f7f94e825ab0da11b3638a0bec65a128ed09ecbd816ad6448d324cde19e98bcbbad7fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6612021613604565b3060405160200180868152602001858152602001848152602001838152602001826001600160a01b031681526020019550505050505060405160208183030381529060405280519060200120905090565b61208561207d611e59565b610192611ebe565b565b61209a64e8d4a5100082101560cb611ebe565b6120b067016345785d8a000082111560ca611ebe565b6008546120bd9082611cae565b6008556040517fa9ba3ffe0b6c366b81232caab38605a0699ad5398d6cce76f91ee809e322dafc906107699083906149ec565b60008282016108268482101583611ebe565b60006106bd8282613608565b60006106bd826016613608565b60006106bd82602c61362f565b60006106bd82604b613639565b60006106bd826055613641565b60006106bd82605661364b565b611bcd8183146067611ebe565b600060608060008061216c6125c6565b91509150612178611e59565b1561227d57604051636c1e543160e11b815260009073f89a1713998593a441cda571780f0900dbef20f99063d83ca862906121bb908d90879087906004016148d9565b60206040518083038186803b1580156121d357600080fd5b505af41580156121e7573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061220b9190614757565b905061221681613659565b6122208a88613686565b90965094506122398187612232610788565b60006136d2565b60098190556040517fe1d39caf039c7dc6f364b1e29d1e67c4e330003a43f4991d50cf3e8840d95eac9161226f91849190614b70565b60405180910390a150612293565b6122878987613686565b60001960095590955093505b6040805160028082526060820183528792879291906020830190803683370190505094509450945050509750975097945050505050565b6122f1816000815181106122da57fe5b60200260200101516122ec6001612d16565b6124dc565b816000815181106122fe57fe5b60200260200101818152505061232c8160018151811061231a57fe5b60200260200101516122ec6000612d16565b8160018151811061233957fe5b60200260200101818152505050565b333014612406576000306001600160a01b031660003660405161236c92919061487d565b6000604051808303816000865af19150503d80600081146123a9576040519150601f19603f3d011682016040523d82523d6000602084013e6123ae565b606091505b5050905080600081146123bd57fe5b60046000803e6000516001600160e01b0319166343adbafb60e01b81146123e8573d6000803e3d6000fd5b506020600460003e604060205260243d03602460403e601c3d016000f35b61240f8661255a565b6000806124258b8b8b8b8b8b8b8b63ffffffff16565b5091509150612437818463ffffffff16565b8051601f1982018390526343adbafb603f1983015260200260231982016044820181fd5b60008060008060008060008061246f6116a9565b93509350935093508360001415612494576000975087965090945092506124bb915050565b6000806124ab8c8c6124a4610788565b898961372a565b909a509850929650909450505050505b92959194509250565b60008282018381101561082657610826600080611ebe565b6000816124ef576124ef60006004611ebe565b826124fc575060006106bd565b670de0b6b3a76400008381029084828161251257fe5b04146125245761252460006005611ebe565b82818161252d57fe5b04949350505050565b600082820261082684158061255357508385838161255057fe5b04145b6003611ebe565b6125818160008151811061256a57fe5b602002602001015161257c6001612d16565b6137c5565b8160008151811061258e57fe5b60200260200101818152505061232c816001815181106125aa57fe5b602002602001015161257c6000612d16565b611bcd82826137fe565b6125ce6141e7565b6125d6614216565b7f000000000000000000000000000000000000000000000066ffcbfd5e5a3000006080808401919091527f00000000000000000000000000000000000000000000000009d025defee4df446060808501919091527f00000000000000000000000000000000000000000000000009d025defee4df446040808601919091527f0000000000000000000000000000000000000000000000000deb5f2f95b77fff6020808701919091527f0000000000000000000000000000000000000000000000000dd60e37b910800086528451858201517f0000000000000000000000000000000018f10816fb42ce33f7252b486856dd42818401527f0000000000000000000000000000000046fa15be44911890f6c1f95e3e44a37590527f0000000000000000000000000000000018dfffbf541b893374eb70afd7d30948918101919091527fffffffffffffffffffffffffffffffffb8fff04ffc51e6c366c5e67beddd88f290527f000000000000000000000000000000004b3b4ca85a86c473e421ffd60063dc8060c08501527ffffffffffffffffffffffffffffffffffffd030720717faa2f026eadc919837960a08501527f000000000000000000000000000000000008842bd393a280406ac0d768fe3f24928401929092527f0000000000000000000000000000000018e883eb27af2bb1ad18d26ae7673383908301527f0000000000000000000000000000000046fd12b7241f98e0fb536ae08e145ece908201529091565b600060608060008061280c6125c6565b91509150600073f89a1713998593a441cda571780f0900dbef20f963d83ca8628b85856040518463ffffffff1660e01b815260040161284d939291906148d9565b60206040518083038186803b15801561286557600080fd5b505af4158015612879573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061289d9190614757565b90506128a881613659565b6000806128b58c8a6138b5565b600a54919350915060ff16156128df576128df826128d28f610cfe565b6128da610788565b6138ef565b6128f383836128ec610788565b60016136d2565b60098190556040517fe1d39caf039c7dc6f364b1e29d1e67c4e330003a43f4991d50cf3e8840d95eac9161292991869190614b70565b60405180910390a16040805160028082526060820183528492849291906020830190803683370190505097509750975050505050509750975097945050505050565b6129928160008151811061297b57fe5b602002602001015161298d6001612d16565b61307d565b8160008151811061299f57fe5b60200260200101818152505061232c816001815181106129bb57fe5b602002602001015161298d6000612d16565b6040805160028082526060808301845292602083019080368337019050509050612a207f000000000000000000000000056fd409e1d7a124bd7017459dfea2f387b6d5cd612a1b6001612d16565b613a01565b81600081518110612a2d57fe5b602002602001018181525050612a677f0000000000000000000000008e870d67f660d95d5be530380d0ec0bd388289e1612a1b6000612d16565b81600181518110612a7457fe5b60200260200101818152505090565b600073f89a1713998593a441cda571780f0900dbef20f963c7fe78d0868585612aab89613aa8565b6040518563ffffffff1660e01b8152600401612aca949392919061490a565b60206040518083038186803b158015612ae257600080fd5b505af4158015612af6573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612b1a9190614757565b95945050505050565b60007f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031615612bf257612bef7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663679aefce6040518163ffffffff1660e01b815260040160206040518083038186803b158015612bb057600080fd5b505afa158015612bc4573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612be89190614757565b83906137c5565b91505b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031615612cbf57612cbc7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663679aefce6040518163ffffffff1660e01b815260040160206040518083038186803b158015612c7d57600080fd5b505afa158015612c91573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612cb59190614757565b83906124dc565b91505b5090565b62461bcd60e51b6000908152602060045260076024526642414c23000030600a808404818106603090810160081b95839006959095019082900491820690940160101b939093010160c81b604452606490fd5b60008060008315612d6b57507f000000000000000000000000000000000000000000000000000000000000000090507f000000000000000000000000000000000001ed09bead87c0378d8e6400000000612db1565b507f000000000000000000000000000000000000000000000000000000000000000090507f0000000000000000000000000000000000000000000000000de0b6b3a76400005b6001600160a01b0382161561082657612e3b826001600160a01b031663679aefce6040518163ffffffff1660e01b815260040160206040518083038186803b158015612dfc57600080fd5b505afa158015612e10573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612e349190614757565b82906137c5565b949350505050565b600061082683836137c5565b60408051600280825260608083018452926020830190803683370190505090508115612eae578381600081518110612e8357fe5b6020026020010181815250508281600181518110612e9d57fe5b602002602001018181525050610826565b8281600081518110612ebc57fe5b6020026020010181815250508381600181518110612ed657fe5b6020026020010181815250509392505050565b6000828202831580612f03575082848281612f0057fe5b04145b612f1357612f1360006003611ebe565b80612f225760009150506106bd565b670de0b6b3a764000060001982015b0460010191505092915050565b600082821115611ccc57611ccc60006001611ebe565b60608601516040516330ffa11b60e11b815260009173f89a1713998593a441cda571780f0900dbef20f9916361ff423691612f9c918a91908a908a908a908a9060040161497b565b60206040518083038186803b158015612fb457600080fd5b505af4158015612fc8573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612fec9190614757565b979650505050505050565b600061082683836124dc565b6060860151604051633b18ce7b60e21b815260009173f89a1713998593a441cda571780f0900dbef20f99163ec6339ec91612f9c918a91908a908a908a908a9060040161497b565b6000610826838361307d565b6000670de0b6b3a7640000821061306f5760006106bd565b50670de0b6b3a76400000390565b6000816130905761309060006004611ebe565b8261309d575060006106bd565b670de0b6b3a7640000838102908482816130b357fe5b04146130d7576130d7670de0b6b3a76400008583816130ce57fe5b04146005611ebe565b826001820381612f3157fe5b6000612e3b847550524f544f434f4c5f535741505f4645455f5045524360501b8486613aec565b6000612e3b847f50524f544f434f4c5f4645455f4759524f5f504f5254494f4e000000000000008486613aec565b60006131426111b4565b6001600160a01b031663aaabadc56040518163ffffffff1660e01b815260040160206040518083038186803b15801561317a57600080fd5b505afa15801561318e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906108409190614324565b60006131bc611fb4565b82604051602001808061190160f01b81525060020183815260200182815260200192505050604051602081830303815290604052805190602001209050919050565b60006060600061320d84613d99565b9050613228600082600381111561322057fe5b1460ce611ebe565b600061323385613db8565b90506132418151600261214f565b61324a8161255a565b6000806132556125c6565b91509150600073f89a1713998593a441cda571780f0900dbef20f963d83ca8628585856040518463ffffffff1660e01b8152600401613296939291906148d9565b60206040518083038186803b1580156132ae57600080fd5b505af41580156132c2573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906132e69190614757565b90507faf65f1a72bf4f8bc48e8153858e1f7b307ce39c88361a677c0a79c2a1ae571ef8160405161331791906149ec565b60405180910390a1600061332d85838686612a83565b9050600061337561335b838860008151811061334557fe5b60200260200101516137c590919063ffffffff16565b8760018151811061336857fe5b60200260200101516120f0565b60099390935550909b939a509298505050505050505050565b611bcd8282613e6c565b6001600160a01b03918216600090815260016020908152604080832093909416825291909152205490565b604080516002808252606080830184529260009291906020830190803683370190505090506133f26001613f01565b816000815181106133ff57fe5b6020026020010181815250506134156000613f01565b8160018151811061342257fe5b6020908102919091010152905090565b60028151101561344157610785565b60008160008151811061345057fe5b602002602001015190506000600190505b8251811015611ca957600083828151811061347857fe5b602002602001015190506134a1816001600160a01b0316846001600160a01b0316106065611ebe565b9150600101613461565b81611bcd57611bcd81613f55565b60006108266134d983602001518560200151611ce890919063ffffffff16565b835185516134e691611ce8565b90611d24565b600073ba1ba1ba1ba1ba1ba1ba1ba1ba1ba1ba1ba1ba1b61350b611190565b6001600160a01b031614158015613526575061352683613f65565b1561354e57613533611190565b6001600160a01b0316336001600160a01b03161490506106bd565b613556613138565b6001600160a01b0316639be2a8848484306040518463ffffffff1660e01b815260040180848152602001836001600160a01b03168152602001826001600160a01b03168152602001935050505060206040518083038186803b1580156135bb57600080fd5b505afa1580156135cf573d6000803e3d6000fd5b505050506040513d60208110156135e557600080fd5b505190506106bd565b60006135fd8484111583611ebe565b5050900390565b4690565b600082821c623fffff16621fffff81136136225780612e3b565b623fffff19179392505050565b1c637fffffff1690565b1c6103ff1690565b1c60019081161490565b1c67ffffffffffffffff1690565b60008060008061366b6009548661245b565b935093509350935061367f84848484613f97565b5050505050565b60006060600061369584613d99565b905060018160038111156136a557fe5b14156136bf576136b58585613fbd565b90935091506136ca565b6136ca610150612cc3565b509250929050565b600081156137045760006136f0846136ea8888612ee9565b9061307d565b90506136fc86826124c4565b915050612e3b565b600061371484610b4488886137c5565b90506137208682612f3e565b9695505050505050565b60008086861161373f575060009050806137bb565b600061375f856137596137528a8c612f3e565b89906137c5565b906137c5565b90506000613777613770898b612f3e565b87906137c5565b905060006137858983612f3e565b9050600061379384836124dc565b905060006137a188836137c5565b905060006137af8383612f3e565b91975090955050505050505b9550959350505050565b60008282028315806137df5750828482816137dc57fe5b04145b6137ef576137ef60006003611ebe565b670de0b6b3a76400008161252d565b6138156001600160a01b038316151561019b611ebe565b61382182600083611ca9565b6001600160a01b03821660009081526020819052604090205461384790826101a16135ee565b6001600160a01b03831660009081526020819052604090205560025461386d9082613fef565b6002556040805182815290516000916001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9181900360200190a35050565b6000606060006138c484613d99565b905060038160038111156138d457fe5b14156138e4576136b58585613ffd565b6136ca610136612cc3565b60408051606081018252600a5460ff81161515825261010081046001600160781b031660208301819052600160801b9091046001600160801b0316928201929092529061393c85856124c4565b11156040518060400160405280601a81526020017f6f7665722061646472657373206c697175696469747920636170000000000000815250906139925760405162461bcd60e51b81526004016107149190614a36565b5060408101516001600160801b03166139ab85846124c4565b11156040518060400160405280601981526020017f6f76657220676c6f62616c206c697175696469747920636170000000000000008152509061367f5760405162461bcd60e51b81526004016107149190614a36565b6000806000613a0e6111b4565b6001600160a01b031663b05f8e48613a2461085e565b876040518363ffffffff1660e01b8152600401613a429291906149f5565b60806040518083038186803b158015613a5a57600080fd5b505afa158015613a6e573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190613a92919061476f565b505081810194509092509050612b1a83856137c5565b6000600160ff1b8210612cbf5760405162461bcd60e51b8152600401808060200182810382526028815260200180614bed6028913960400191505060405180910390fd5b6040805160208082018690526001600160a01b03808516838501528351808403850181526060840180865281519184019190912063696eb37560e01b909152606484018190529351600094939189169263696eb3759260848082019391829003018186803b158015613b5d57600080fd5b505afa158015613b71573d6000803e3d6000fd5b505050506040513d6020811015613b8757600080fd5b505115613c0757856001600160a01b031663bd02d0f5826040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b158015613bd257600080fd5b505afa158015613be6573d6000803e3d6000fd5b505050506040513d6020811015613bfc57600080fd5b50519150612e3b9050565b6040805160208082018890528183018790528251808303840181526060830180855281519183019190912063696eb37560e01b9091526064830181905292516001600160a01b038a169263696eb375926084808301939192829003018186803b158015613c7357600080fd5b505afa158015613c87573d6000803e3d6000fd5b505050506040513d6020811015613c9d57600080fd5b505115613d1e57866001600160a01b031663bd02d0f5826040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b158015613ce857600080fd5b505afa158015613cfc573d6000803e3d6000fd5b505050506040513d6020811015613d1257600080fd5b50519250612e3b915050565b866001600160a01b031663bd02d0f5876040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b158015613d6257600080fd5b505afa158015613d76573d6000803e3d6000fd5b505050506040513d6020811015613d8c57600080fd5b5051979650505050505050565b6000818060200190516020811015613db057600080fd5b505192915050565b6060818060200190516040811015613dcf57600080fd5b815160208301805160405192949293830192919084640100000000821115613df657600080fd5b908301906020820185811115613e0b57600080fd5b8251866020820283011164010000000082111715613e2857600080fd5b82525081516020918201928201910280838360005b83811015613e55578181015183820152602001613e3d565b505050509190910160405250929695505050505050565b613e7860008383611ca9565b600254613e8590826120f0565b6002556001600160a01b038216600090815260208190526040902054613eab90826120f0565b6001600160a01b0383166000818152602081815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b600081613f2e577f00000000000000000000000000000000000000000000000006f05b59d3b200006106bd565b507f00000000000000000000000000000000000000000000000006f05b59d3b20000919050565b610785816223aca960e91b614022565b6000613f77631c74c91760e11b6110d1565b8214806106bd5750613f8f6350dd6ed960e01b6110d1565b909114919050565b8315613fa757613fa7828561338e565b8215613fb757613fb7818461338e565b50505050565b600060606000613fcc84614083565b90506000613fe28683613fdd610788565b6140a5565b9196919550909350505050565b6000610826838360016135ee565b60006060600061400c84614083565b90506000613fe2868361401d610788565b61412b565b62461bcd60e51b600090815260206004526007602452600a808404818106603090810160081b958390069590950190829004918206850160101b01602363ffffff0060e086901c160160181b0190930160c81b60445260e882901c90606490fd5b600081806020019051604081101561409a57600080fd5b506020015192915050565b6060835167ffffffffffffffff811180156140bf57600080fd5b506040519080825280602002602001820160405280156140e9578160200160208202803683370190505b50905060005b84518110156116a15761410c83610b448688858151811061334557fe5b82828151811061411857fe5b60209081029190910101526001016140ef565b6060835167ffffffffffffffff8111801561414557600080fd5b5060405190808252806020026020018201604052801561416f578160200160208202803683370190505b50905060005b84518110156116a1576141a8836136ea8688858151811061419257fe5b6020026020010151612ee990919063ffffffff16565b8282815181106141b457fe5b6020908102919091010152600101614175565b604080516060810182526000808252602082018190529181019190915290565b6040518060a0016040528060008152602001600081526020016000815260200160008152602001600081525090565b6040518060e0016040528061422961425f565b815260200161423661425f565b815260200160008152602001600081526020016000815260200160008152602001600081525090565b604051806040016040528060008152602001600081525090565b8035610d1881614bd7565b80358015158114610d1857600080fd5b600082601f8301126142a4578081fd5b813567ffffffffffffffff8111156142b857fe5b6142cb601f8201601f1916602001614bb3565b8181528460208386010111156142df578283fd5b816020850160208301379081016020019190915292915050565b803560028110610d1857600080fd5b600060208284031215614319578081fd5b813561082681614bd7565b600060208284031215614335578081fd5b815161082681614bd7565b60008060408385031215614352578081fd5b823561435d81614bd7565b9150602083013561436d81614bd7565b809150509250929050565b60008060006060848603121561438c578081fd5b833561439781614bd7565b925060208401356143a781614bd7565b929592945050506040919091013590565b600080600080600080600060e0888a0312156143d2578283fd5b87356143dd81614bd7565b965060208801356143ed81614bd7565b95506040880135945060608801359350608088013560ff81168114614410578384fd5b9699959850939692959460a0840135945060c09093013592915050565b6000806040838503121561443f578182fd5b823561444a81614bd7565b946020939093013593505050565b600060208284031215614469578081fd5b61082682614284565b600080600080600080600060e0888a03121561448c578081fd5b87359650602088013561449e81614bd7565b955060408801356144ae81614bd7565b9450606088013567ffffffffffffffff808211156144ca578283fd5b818a0191508a601f8301126144dd578283fd5b8135818111156144e957fe5b602081026144f960208201614bb3565b80838252602082019150602086018f6020858901011115614518578788fd5b8796505b8487101561453b5780358352600196909601956020928301920161451c565b50985050505060808a0135945060a08a0135935060c08a0135915080821115614562578283fd5b5061456f8a828b01614294565b91505092959891949750929550565b60006020828403121561458f578081fd5b81356001600160e01b031981168114610826578182fd5b600080604083850312156145b8578182fd5b505080516020909101519092909150565b6000606082840312156145da578081fd5b6040516060810181811067ffffffffffffffff821117156145f757fe5b60405261460383614284565b815260208301356001600160781b038116811461461e578283fd5b602082015260408301356001600160801b038116811461463c578283fd5b60408201529392505050565b60008060006060848603121561465c578081fd5b833567ffffffffffffffff80821115614673578283fd5b8186019150610120808389031215614689578384fd5b61469281614bb3565b905061469d836142f9565b81526146ab60208401614279565b60208201526146bc60408401614279565b6040820152606083013560608201526080830135608082015260a083013560a08201526146eb60c08401614279565b60c08201526146fc60e08401614279565b60e08201526101008084013583811115614714578586fd5b6147208a828701614294565b9183019190915250976020870135975060409096013595945050505050565b600060208284031215614750578081fd5b5035919050565b600060208284031215614768578081fd5b5051919050565b60008060008060808587031215614784578182fd5b84519350602085015192506040850151915060608501516147a481614bd7565b939692955090935050565b6000815180845260208085019450808401835b838110156147de578151875295820195908201906001016147c2565b509495945050505050565b6147f482825161486e565b6020810151614806604084018261486e565b5060408101516080830152606081015160a0830152608081015160c083015260a081015160e083015260c08101516101008301525050565b80518252602081015160208301526040810151604083015260608101516060830152608081015160808301525050565b80518252602090810151910152565b6000828483379101908152919050565b6001600160a01b0391909116815260200190565b60006020825261082660208301846147af565b6000604082526148c760408301856147af565b8281036020840152612b1a81856147af565b60006101e08083526148ed818401876147af565b9150506148fd602083018561483e565b612e3b60c08301846147e9565b600061020080835261491e818401886147af565b91505061492e602083018661483e565b61493b60c08301856147e9565b826101e083015295945050505050565b60006080825261495e60808301866147af565b905061496d602083018561486e565b826060830152949350505050565b600061026080835261498f8184018a6147af565b91505086602083015285151560408301526149ad606083018661483e565b6149bb6101008301856147e9565b612fec61022083018461486e565b901515815260200190565b92151583526020830191909152604082015260600190565b90815260200190565b9182526001600160a01b0316602082015260400190565b9586526020860194909452604085019290925260608401521515608083015260a082015260c00190565b6000602080835283518082850152825b81811015614a6257858101830151858201604001528201614a46565b81811115614a735783604083870101525b50601f01601f1916929092016040019392505050565b8151151581526020808301516001600160781b0316908201526040918201516001600160801b03169181019190915260600190565b905460ff811615158252600881901c6001600160781b0316602083015260801c604082015260600190565b6101c08101614af8828561483e565b614b0660a08301845161486e565b6020830151614b1860e084018261486e565b5060408301516101208301526060830151610140830152608083015161016083015260a083015161018083015260c08301516101a08301529392505050565b600083825260406020830152612e3b60408301846147af565b918252602082015260400190565b93845260208401929092526001600160a01b03908116604084015216606082015260800190565b60ff91909116815260200190565b60405181810167ffffffffffffffff81118282101715614bcf57fe5b604052919050565b6001600160a01b038116811461078557600080fdfe53616665436173743a2076616c756520646f65736e27742066697420696e20616e20696e74323536a2646970667358221220804f414f2fd23de5749b22830088e0b3ca1b44e5fcef57a5d2128c45dda1014064736f6c63430007060033
Loading...
Loading
Loading...
Loading
[ Download: CSV Export ]
[ Download: CSV Export ]
A token is a representation of an on-chain or off-chain asset. The token page shows information such as price, total supply, holders, transfers and social links. Learn more about this page in our Knowledge Base.