Warning! Contract bytecode has been changed and doesn't match the verified one. Therefore, interaction with this smart contract may be risky.
This contract has been verified via Sourcify.
View contract in Sourcify repository
- Contract name:
- SmartPoolManager
- Optimization enabled
- true
- Compiler version
- v0.6.12+commit.27d51765
- Optimization runs
- 200
- EVM Version
- istanbul
- Verified at
- 2026-03-07T19:06:20.316451Z
libraries/SmartPoolManager.sol
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
// Needed to pass in structs
pragma experimental ABIEncoderV2;
// Imports
import "../interfaces/IERC20.sol";
import "../interfaces/IConfigurableRightsPool.sol";
import "../contracts/IBFactory.sol";
import "./BalancerSafeMath.sol";
import "./SafeApprove.sol";
/**
* @author Balancer Labs
* @title Factor out the weight updates
*/
library SmartPoolManager {
// Type declarations
struct NewTokenParams {
address addr;
bool isCommitted;
uint commitBlock;
uint denorm;
uint balance;
}
// For blockwise, automated weight updates
// Move weights linearly from startWeights to endWeights,
// between startBlock and endBlock
struct GradualUpdateParams {
uint startBlock;
uint endBlock;
uint[] startWeights;
uint[] endWeights;
}
// updateWeight and pokeWeights are unavoidably long
/* solhint-disable function-max-lines */
/**
* @notice Update the weight of an existing token
* @dev Refactored to library to make CRPFactory deployable
* @param self - ConfigurableRightsPool instance calling the library
* @param bPool - Core BPool the CRP is wrapping
* @param token - token to be reweighted
* @param newWeight - new weight of the token
*/
function updateWeight(
IConfigurableRightsPool self,
IBPool bPool,
address token,
uint newWeight
)
external
{
require(newWeight >= BalancerConstants.MIN_WEIGHT, "ERR_MIN_WEIGHT");
require(newWeight <= BalancerConstants.MAX_WEIGHT, "ERR_MAX_WEIGHT");
uint currentWeight = bPool.getDenormalizedWeight(token);
// Save gas; return immediately on NOOP
if (currentWeight == newWeight) {
return;
}
uint currentBalance = bPool.getBalance(token);
uint totalSupply = self.totalSupply();
uint totalWeight = bPool.getTotalDenormalizedWeight();
uint poolShares;
uint deltaBalance;
uint deltaWeight;
uint newBalance;
if (newWeight < currentWeight) {
// This means the controller will withdraw tokens to keep price
// So they need to redeem PCTokens
deltaWeight = BalancerSafeMath.bsub(currentWeight, newWeight);
// poolShares = totalSupply * (deltaWeight / totalWeight)
poolShares = BalancerSafeMath.bmul(totalSupply,
BalancerSafeMath.bdiv(deltaWeight, totalWeight));
// deltaBalance = currentBalance * (deltaWeight / currentWeight)
deltaBalance = BalancerSafeMath.bmul(currentBalance,
BalancerSafeMath.bdiv(deltaWeight, currentWeight));
// New balance cannot be lower than MIN_BALANCE
newBalance = BalancerSafeMath.bsub(currentBalance, deltaBalance);
require(newBalance >= BalancerConstants.MIN_BALANCE, "ERR_MIN_BALANCE");
// First get the tokens from this contract (Pool Controller) to msg.sender
bPool.rebind(token, newBalance, newWeight);
// Now with the tokens this contract can send them to msg.sender
bool xfer = IERC20(token).transfer(msg.sender, deltaBalance);
require(xfer, "ERR_ERC20_FALSE");
self.pullPoolShareFromLib(msg.sender, poolShares);
self.burnPoolShareFromLib(poolShares);
}
else {
// This means the controller will deposit tokens to keep the price.
// They will be minted and given PCTokens
deltaWeight = BalancerSafeMath.bsub(newWeight, currentWeight);
require(BalancerSafeMath.badd(totalWeight, deltaWeight) <= BalancerConstants.MAX_TOTAL_WEIGHT,
"ERR_MAX_TOTAL_WEIGHT");
// poolShares = totalSupply * (deltaWeight / totalWeight)
poolShares = BalancerSafeMath.bmul(totalSupply,
BalancerSafeMath.bdiv(deltaWeight, totalWeight));
// deltaBalance = currentBalance * (deltaWeight / currentWeight)
deltaBalance = BalancerSafeMath.bmul(currentBalance,
BalancerSafeMath.bdiv(deltaWeight, currentWeight));
// First gets the tokens from msg.sender to this contract (Pool Controller)
bool xfer = IERC20(token).transferFrom(msg.sender, address(this), deltaBalance);
require(xfer, "ERR_ERC20_FALSE");
// Now with the tokens this contract can bind them to the pool it controls
bPool.rebind(token, BalancerSafeMath.badd(currentBalance, deltaBalance), newWeight);
self.mintPoolShareFromLib(poolShares);
self.pushPoolShareFromLib(msg.sender, poolShares);
}
}
/**
* @notice External function called to make the contract update weights according to plan
* @param bPool - Core BPool the CRP is wrapping
* @param gradualUpdate - gradual update parameters from the CRP
*/
function pokeWeights(
IBPool bPool,
GradualUpdateParams storage gradualUpdate
)
external
{
// Do nothing if we call this when there is no update plan
if (gradualUpdate.startBlock == 0) {
return;
}
// Error to call it before the start of the plan
require(block.number >= gradualUpdate.startBlock, "ERR_CANT_POKE_YET");
// Proposed error message improvement
// require(block.number >= startBlock, "ERR_NO_HOKEY_POKEY");
// This allows for pokes after endBlock that get weights to endWeights
// Get the current block (or the endBlock, if we're already past the end)
uint currentBlock;
if (block.number > gradualUpdate.endBlock) {
currentBlock = gradualUpdate.endBlock;
}
else {
currentBlock = block.number;
}
uint blockPeriod = BalancerSafeMath.bsub(gradualUpdate.endBlock, gradualUpdate.startBlock);
uint blocksElapsed = BalancerSafeMath.bsub(currentBlock, gradualUpdate.startBlock);
uint weightDelta;
uint deltaPerBlock;
uint newWeight;
address[] memory tokens = bPool.getCurrentTokens();
// This loop contains external calls
// External calls are to math libraries or the underlying pool, so low risk
for (uint i = 0; i < tokens.length; i++) {
// Make sure it does nothing if the new and old weights are the same (saves gas)
// It's a degenerate case if they're *all* the same, but you certainly could have
// a plan where you only change some of the weights in the set
if (gradualUpdate.startWeights[i] != gradualUpdate.endWeights[i]) {
if (gradualUpdate.endWeights[i] < gradualUpdate.startWeights[i]) {
// We are decreasing the weight
// First get the total weight delta
weightDelta = BalancerSafeMath.bsub(gradualUpdate.startWeights[i],
gradualUpdate.endWeights[i]);
// And the amount it should change per block = total change/number of blocks in the period
deltaPerBlock = BalancerSafeMath.bdiv(weightDelta, blockPeriod);
//deltaPerBlock = bdivx(weightDelta, blockPeriod);
// newWeight = startWeight - (blocksElapsed * deltaPerBlock)
newWeight = BalancerSafeMath.bsub(gradualUpdate.startWeights[i],
BalancerSafeMath.bmul(blocksElapsed, deltaPerBlock));
}
else {
// We are increasing the weight
// First get the total weight delta
weightDelta = BalancerSafeMath.bsub(gradualUpdate.endWeights[i],
gradualUpdate.startWeights[i]);
// And the amount it should change per block = total change/number of blocks in the period
deltaPerBlock = BalancerSafeMath.bdiv(weightDelta, blockPeriod);
//deltaPerBlock = bdivx(weightDelta, blockPeriod);
// newWeight = startWeight + (blocksElapsed * deltaPerBlock)
newWeight = BalancerSafeMath.badd(gradualUpdate.startWeights[i],
BalancerSafeMath.bmul(blocksElapsed, deltaPerBlock));
}
uint bal = bPool.getBalance(tokens[i]);
bPool.rebind(tokens[i], bal, newWeight);
}
}
// Reset to allow add/remove tokens, or manual weight updates
if (block.number >= gradualUpdate.endBlock) {
gradualUpdate.startBlock = 0;
}
}
/* solhint-enable function-max-lines */
/**
* @notice Schedule (commit) a token to be added; must call applyAddToken after a fixed
* number of blocks to actually add the token
* @param bPool - Core BPool the CRP is wrapping
* @param token - the token to be added
* @param balance - how much to be added
* @param denormalizedWeight - the desired token weight
* @param newToken - NewTokenParams struct used to hold the token data (in CRP storage)
*/
function commitAddToken(
IBPool bPool,
address token,
uint balance,
uint denormalizedWeight,
NewTokenParams storage newToken
)
external
{
require(!bPool.isBound(token), "ERR_IS_BOUND");
require(denormalizedWeight <= BalancerConstants.MAX_WEIGHT, "ERR_WEIGHT_ABOVE_MAX");
require(denormalizedWeight >= BalancerConstants.MIN_WEIGHT, "ERR_WEIGHT_BELOW_MIN");
require(BalancerSafeMath.badd(bPool.getTotalDenormalizedWeight(),
denormalizedWeight) <= BalancerConstants.MAX_TOTAL_WEIGHT,
"ERR_MAX_TOTAL_WEIGHT");
require(balance >= BalancerConstants.MIN_BALANCE, "ERR_BALANCE_BELOW_MIN");
newToken.addr = token;
newToken.balance = balance;
newToken.denorm = denormalizedWeight;
newToken.commitBlock = block.number;
newToken.isCommitted = true;
}
/**
* @notice Add the token previously committed (in commitAddToken) to the pool
* @param self - ConfigurableRightsPool instance calling the library
* @param bPool - Core BPool the CRP is wrapping
* @param addTokenTimeLockInBlocks - Wait time between committing and applying a new token
* @param newToken - NewTokenParams struct used to hold the token data (in CRP storage)
*/
function applyAddToken(
IConfigurableRightsPool self,
IBPool bPool,
uint addTokenTimeLockInBlocks,
NewTokenParams storage newToken
)
external
{
require(newToken.isCommitted, "ERR_NO_TOKEN_COMMIT");
require(BalancerSafeMath.bsub(block.number, newToken.commitBlock) >= addTokenTimeLockInBlocks,
"ERR_TIMELOCK_STILL_COUNTING");
uint totalSupply = self.totalSupply();
// poolShares = totalSupply * newTokenWeight / totalWeight
uint poolShares = BalancerSafeMath.bdiv(BalancerSafeMath.bmul(totalSupply, newToken.denorm),
bPool.getTotalDenormalizedWeight());
// Clear this to allow adding more tokens
newToken.isCommitted = false;
// First gets the tokens from msg.sender to this contract (Pool Controller)
bool returnValue = IERC20(newToken.addr).transferFrom(self.getController(), address(self), newToken.balance);
require(returnValue, "ERR_ERC20_FALSE");
// Now with the tokens this contract can bind them to the pool it controls
// Approves bPool to pull from this controller
// Approve unlimited, same as when creating the pool, so they can join pools later
returnValue = SafeApprove.safeApprove(IERC20(newToken.addr), address(bPool), BalancerConstants.MAX_UINT);
require(returnValue, "ERR_ERC20_FALSE");
bPool.bind(newToken.addr, newToken.balance, newToken.denorm);
self.mintPoolShareFromLib(poolShares);
self.pushPoolShareFromLib(msg.sender, poolShares);
}
/**
* @notice Remove a token from the pool
* @dev Logic in the CRP controls when ths can be called. There are two related permissions:
* AddRemoveTokens - which allows removing down to the underlying BPool limit of two
* RemoveAllTokens - which allows completely draining the pool by removing all tokens
* This can result in a non-viable pool with 0 or 1 tokens (by design),
* meaning all swapping or binding operations would fail in this state
* @param self - ConfigurableRightsPool instance calling the library
* @param bPool - Core BPool the CRP is wrapping
* @param token - token to remove
*/
function removeToken(
IConfigurableRightsPool self,
IBPool bPool,
address token
)
external
{
uint totalSupply = self.totalSupply();
// poolShares = totalSupply * tokenWeight / totalWeight
uint poolShares = BalancerSafeMath.bdiv(BalancerSafeMath.bmul(totalSupply,
bPool.getDenormalizedWeight(token)),
bPool.getTotalDenormalizedWeight());
// this is what will be unbound from the pool
// Have to get it before unbinding
uint balance = bPool.getBalance(token);
// Unbind and get the tokens out of balancer pool
bPool.unbind(token);
// Now with the tokens this contract can send them to msg.sender
bool xfer = IERC20(token).transfer(self.getController(), balance);
require(xfer, "ERR_ERC20_FALSE");
self.pullPoolShareFromLib(self.getController(), poolShares);
self.burnPoolShareFromLib(poolShares);
}
/**
* @notice Non ERC20-conforming tokens are problematic; don't allow them in pools
* @dev Will revert if invalid
* @param token - The prospective token to verify
*/
function verifyTokenCompliance(address token) external {
verifyTokenComplianceInternal(token);
}
/**
* @notice Non ERC20-conforming tokens are problematic; don't allow them in pools
* @dev Will revert if invalid - overloaded to save space in the main contract
* @param tokens - The prospective tokens to verify
*/
function verifyTokenCompliance(address[] calldata tokens) external {
for (uint i = 0; i < tokens.length; i++) {
verifyTokenComplianceInternal(tokens[i]);
}
}
/**
* @notice Update weights in a predetermined way, between startBlock and endBlock,
* through external cals to pokeWeights
* @param bPool - Core BPool the CRP is wrapping
* @param newWeights - final weights we want to get to
* @param startBlock - when weights should start to change
* @param endBlock - when weights will be at their final values
* @param minimumWeightChangeBlockPeriod - needed to validate the block period
*/
function updateWeightsGradually(
IBPool bPool,
GradualUpdateParams storage gradualUpdate,
uint[] calldata newWeights,
uint startBlock,
uint endBlock,
uint minimumWeightChangeBlockPeriod
)
external
{
require(block.number < endBlock, "ERR_GRADUAL_UPDATE_TIME_TRAVEL");
if (block.number > startBlock) {
// This means the weight update should start ASAP
// Moving the start block up prevents a big jump/discontinuity in the weights
gradualUpdate.startBlock = block.number;
}
else{
gradualUpdate.startBlock = startBlock;
}
// Enforce a minimum time over which to make the changes
// The also prevents endBlock <= startBlock
require(BalancerSafeMath.bsub(endBlock, gradualUpdate.startBlock) >= minimumWeightChangeBlockPeriod,
"ERR_WEIGHT_CHANGE_TIME_BELOW_MIN");
address[] memory tokens = bPool.getCurrentTokens();
// Must specify weights for all tokens
require(newWeights.length == tokens.length, "ERR_START_WEIGHTS_MISMATCH");
uint weightsSum = 0;
gradualUpdate.startWeights = new uint[](tokens.length);
// Check that endWeights are valid now to avoid reverting in a future pokeWeights call
//
// This loop contains external calls
// External calls are to math libraries or the underlying pool, so low risk
for (uint i = 0; i < tokens.length; i++) {
require(newWeights[i] <= BalancerConstants.MAX_WEIGHT, "ERR_WEIGHT_ABOVE_MAX");
require(newWeights[i] >= BalancerConstants.MIN_WEIGHT, "ERR_WEIGHT_BELOW_MIN");
weightsSum = BalancerSafeMath.badd(weightsSum, newWeights[i]);
gradualUpdate.startWeights[i] = bPool.getDenormalizedWeight(tokens[i]);
}
require(weightsSum <= BalancerConstants.MAX_TOTAL_WEIGHT, "ERR_MAX_TOTAL_WEIGHT");
gradualUpdate.endBlock = endBlock;
gradualUpdate.endWeights = newWeights;
}
/**
* @notice Join a pool
* @param self - ConfigurableRightsPool instance calling the library
* @param bPool - Core BPool the CRP is wrapping
* @param poolAmountOut - number of pool tokens to receive
* @param maxAmountsIn - Max amount of asset tokens to spend
* @return actualAmountsIn - calculated values of the tokens to pull in
*/
function joinPool(
IConfigurableRightsPool self,
IBPool bPool,
uint poolAmountOut,
uint[] calldata maxAmountsIn
)
external
view
returns (uint[] memory actualAmountsIn)
{
address[] memory tokens = bPool.getCurrentTokens();
require(maxAmountsIn.length == tokens.length, "ERR_AMOUNTS_MISMATCH");
uint poolTotal = self.totalSupply();
// Subtract 1 to ensure any rounding errors favor the pool
uint ratio = BalancerSafeMath.bdiv(poolAmountOut,
BalancerSafeMath.bsub(poolTotal, 1));
require(ratio != 0, "ERR_MATH_APPROX");
// We know the length of the array; initialize it, and fill it below
// Cannot do "push" in memory
actualAmountsIn = new uint[](tokens.length);
// This loop contains external calls
// External calls are to math libraries or the underlying pool, so low risk
for (uint i = 0; i < tokens.length; i++) {
address t = tokens[i];
uint bal = bPool.getBalance(t);
// Add 1 to ensure any rounding errors favor the pool
uint tokenAmountIn = BalancerSafeMath.bmul(ratio,
BalancerSafeMath.badd(bal, 1));
require(tokenAmountIn != 0, "ERR_MATH_APPROX");
require(tokenAmountIn <= maxAmountsIn[i], "ERR_LIMIT_IN");
actualAmountsIn[i] = tokenAmountIn;
}
}
/**
* @notice Exit a pool - redeem pool tokens for underlying assets
* @param self - ConfigurableRightsPool instance calling the library
* @param bPool - Core BPool the CRP is wrapping
* @param poolAmountIn - amount of pool tokens to redeem
* @param minAmountsOut - minimum amount of asset tokens to receive
* @return exitFee - calculated exit fee
* @return pAiAfterExitFee - final amount in (after accounting for exit fee)
* @return actualAmountsOut - calculated amounts of each token to pull
*/
function exitPool(
IConfigurableRightsPool self,
IBPool bPool,
uint poolAmountIn,
uint[] calldata minAmountsOut
)
external
view
returns (uint exitFee, uint pAiAfterExitFee, uint[] memory actualAmountsOut)
{
address[] memory tokens = bPool.getCurrentTokens();
require(minAmountsOut.length == tokens.length, "ERR_AMOUNTS_MISMATCH");
uint poolTotal = self.totalSupply();
// Calculate exit fee and the final amount in
exitFee = BalancerSafeMath.bmul(poolAmountIn, BalancerConstants.EXIT_FEE);
pAiAfterExitFee = BalancerSafeMath.bsub(poolAmountIn, exitFee);
uint ratio = BalancerSafeMath.bdiv(pAiAfterExitFee,
BalancerSafeMath.badd(poolTotal, 1));
require(ratio != 0, "ERR_MATH_APPROX");
actualAmountsOut = new uint[](tokens.length);
// This loop contains external calls
// External calls are to math libraries or the underlying pool, so low risk
for (uint i = 0; i < tokens.length; i++) {
address t = tokens[i];
uint bal = bPool.getBalance(t);
// Subtract 1 to ensure any rounding errors favor the pool
uint tokenAmountOut = BalancerSafeMath.bmul(ratio,
BalancerSafeMath.bsub(bal, 1));
require(tokenAmountOut != 0, "ERR_MATH_APPROX");
require(tokenAmountOut >= minAmountsOut[i], "ERR_LIMIT_OUT");
actualAmountsOut[i] = tokenAmountOut;
}
}
/**
* @notice Join by swapping a fixed amount of an external token in (must be present in the pool)
* System calculates the pool token amount
* @param self - ConfigurableRightsPool instance calling the library
* @param bPool - Core BPool the CRP is wrapping
* @param tokenIn - which token we're transferring in
* @param tokenAmountIn - amount of deposit
* @param minPoolAmountOut - minimum of pool tokens to receive
* @return poolAmountOut - amount of pool tokens minted and transferred
*/
function joinswapExternAmountIn(
IConfigurableRightsPool self,
IBPool bPool,
address tokenIn,
uint tokenAmountIn,
uint minPoolAmountOut
)
external
view
returns (uint poolAmountOut)
{
require(bPool.isBound(tokenIn), "ERR_NOT_BOUND");
require(tokenAmountIn <= BalancerSafeMath.bmul(bPool.getBalance(tokenIn),
BalancerConstants.MAX_IN_RATIO),
"ERR_MAX_IN_RATIO");
poolAmountOut = bPool.calcPoolOutGivenSingleIn(
bPool.getBalance(tokenIn),
bPool.getDenormalizedWeight(tokenIn),
self.totalSupply(),
bPool.getTotalDenormalizedWeight(),
tokenAmountIn,
bPool.getSwapFee()
);
require(poolAmountOut >= minPoolAmountOut, "ERR_LIMIT_OUT");
}
/**
* @notice Join by swapping an external token in (must be present in the pool)
* To receive an exact amount of pool tokens out. System calculates the deposit amount
* @param self - ConfigurableRightsPool instance calling the library
* @param bPool - Core BPool the CRP is wrapping
* @param tokenIn - which token we're transferring in (system calculates amount required)
* @param poolAmountOut - amount of pool tokens to be received
* @param maxAmountIn - Maximum asset tokens that can be pulled to pay for the pool tokens
* @return tokenAmountIn - amount of asset tokens transferred in to purchase the pool tokens
*/
function joinswapPoolAmountOut(
IConfigurableRightsPool self,
IBPool bPool,
address tokenIn,
uint poolAmountOut,
uint maxAmountIn
)
external
view
returns (uint tokenAmountIn)
{
require(bPool.isBound(tokenIn), "ERR_NOT_BOUND");
tokenAmountIn = bPool.calcSingleInGivenPoolOut(
bPool.getBalance(tokenIn),
bPool.getDenormalizedWeight(tokenIn),
self.totalSupply(),
bPool.getTotalDenormalizedWeight(),
poolAmountOut,
bPool.getSwapFee()
);
require(tokenAmountIn != 0, "ERR_MATH_APPROX");
require(tokenAmountIn <= maxAmountIn, "ERR_LIMIT_IN");
require(tokenAmountIn <= BalancerSafeMath.bmul(bPool.getBalance(tokenIn),
BalancerConstants.MAX_IN_RATIO),
"ERR_MAX_IN_RATIO");
}
/**
* @notice Exit a pool - redeem a specific number of pool tokens for an underlying asset
* Asset must be present in the pool, and will incur an EXIT_FEE (if set to non-zero)
* @param self - ConfigurableRightsPool instance calling the library
* @param bPool - Core BPool the CRP is wrapping
* @param tokenOut - which token the caller wants to receive
* @param poolAmountIn - amount of pool tokens to redeem
* @param minAmountOut - minimum asset tokens to receive
* @return exitFee - calculated exit fee
* @return tokenAmountOut - amount of asset tokens returned
*/
function exitswapPoolAmountIn(
IConfigurableRightsPool self,
IBPool bPool,
address tokenOut,
uint poolAmountIn,
uint minAmountOut
)
external
view
returns (uint exitFee, uint tokenAmountOut)
{
require(bPool.isBound(tokenOut), "ERR_NOT_BOUND");
tokenAmountOut = bPool.calcSingleOutGivenPoolIn(
bPool.getBalance(tokenOut),
bPool.getDenormalizedWeight(tokenOut),
self.totalSupply(),
bPool.getTotalDenormalizedWeight(),
poolAmountIn,
bPool.getSwapFee()
);
require(tokenAmountOut >= minAmountOut, "ERR_LIMIT_OUT");
require(tokenAmountOut <= BalancerSafeMath.bmul(bPool.getBalance(tokenOut),
BalancerConstants.MAX_OUT_RATIO),
"ERR_MAX_OUT_RATIO");
exitFee = BalancerSafeMath.bmul(poolAmountIn, BalancerConstants.EXIT_FEE);
}
/**
* @notice Exit a pool - redeem pool tokens for a specific amount of underlying assets
* Asset must be present in the pool
* @param self - ConfigurableRightsPool instance calling the library
* @param bPool - Core BPool the CRP is wrapping
* @param tokenOut - which token the caller wants to receive
* @param tokenAmountOut - amount of underlying asset tokens to receive
* @param maxPoolAmountIn - maximum pool tokens to be redeemed
* @return exitFee - calculated exit fee
* @return poolAmountIn - amount of pool tokens redeemed
*/
function exitswapExternAmountOut(
IConfigurableRightsPool self,
IBPool bPool,
address tokenOut,
uint tokenAmountOut,
uint maxPoolAmountIn
)
external
view
returns (uint exitFee, uint poolAmountIn)
{
require(bPool.isBound(tokenOut), "ERR_NOT_BOUND");
require(tokenAmountOut <= BalancerSafeMath.bmul(bPool.getBalance(tokenOut),
BalancerConstants.MAX_OUT_RATIO),
"ERR_MAX_OUT_RATIO");
poolAmountIn = bPool.calcPoolInGivenSingleOut(
bPool.getBalance(tokenOut),
bPool.getDenormalizedWeight(tokenOut),
self.totalSupply(),
bPool.getTotalDenormalizedWeight(),
tokenAmountOut,
bPool.getSwapFee()
);
require(poolAmountIn != 0, "ERR_MATH_APPROX");
require(poolAmountIn <= maxPoolAmountIn, "ERR_LIMIT_IN");
exitFee = BalancerSafeMath.bmul(poolAmountIn, BalancerConstants.EXIT_FEE);
}
// Internal functions
// Check for zero transfer, and make sure it returns true to returnValue
function verifyTokenComplianceInternal(address token) internal {
bool returnValue = IERC20(token).transfer(msg.sender, 0);
require(returnValue, "ERR_NONCONFORMING_TOKEN");
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
// Needed to handle structures externally
pragma experimental ABIEncoderV2;
// Imports
import "./ConfigurableRightsPool.sol";
// Contracts
/**
* @author Balancer Labs
* @title Configurable Rights Pool Factory - create parameterized smart pools
* @dev Rights are held in a corresponding struct in ConfigurableRightsPool
* Index values are as follows:
* 0: canPauseSwapping - can setPublicSwap back to false after turning it on
* by default, it is off on initialization and can only be turned on
* 1: canChangeSwapFee - can setSwapFee after initialization (by default, it is fixed at create time)
* 2: canChangeWeights - can bind new token weights (allowed by default in base pool)
* 3: canAddRemoveTokens - can bind/unbind tokens (allowed by default in base pool)
* 4: canWhitelistLPs - if set, only whitelisted addresses can join pools
* (enables private pools with more than one LP)
* 5: canChangeCap - can change the BSP cap (max # of pool tokens)
*/
contract CRPFactory {
// State variables
// Keep a list of all Configurable Rights Pools
mapping(address=>bool) private _isCrp;
// Event declarations
// Log the address of each new smart pool, and its creator
event LogNewCrp(
address indexed caller,
address indexed pool
);
// Function declarations
/**
* @notice Create a new CRP
* @dev emits a LogNewCRP event
* @param factoryAddress - the BFactory instance used to create the underlying pool
* @param poolParams - struct containing the names, tokens, weights, balances, and swap fee
* @param rights - struct of permissions, configuring this CRP instance (see above for definitions)
*/
function newCrp(
address factoryAddress,
ConfigurableRightsPool.PoolParams calldata poolParams,
RightsManager.Rights calldata rights
)
external
returns (ConfigurableRightsPool)
{
require(poolParams.constituentTokens.length >= BalancerConstants.MIN_ASSET_LIMIT, "ERR_TOO_FEW_TOKENS");
// Arrays must be parallel
require(poolParams.tokenBalances.length == poolParams.constituentTokens.length, "ERR_START_BALANCES_MISMATCH");
require(poolParams.tokenWeights.length == poolParams.constituentTokens.length, "ERR_START_WEIGHTS_MISMATCH");
ConfigurableRightsPool crp = new ConfigurableRightsPool(
factoryAddress,
poolParams,
rights
);
emit LogNewCrp(msg.sender, address(crp));
_isCrp[address(crp)] = true;
// The caller is the controller of the CRP
// The CRP will be the controller of the underlying Core BPool
crp.setController(msg.sender);
return crp;
}
/**
* @notice Check to see if a given address is a CRP
* @param addr - address to check
* @return boolean indicating whether it is a CRP
*/
function isCrp(address addr) external view returns (bool) {
return _isCrp[addr];
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
// Interface declarations
// Introduce to avoid circularity (otherwise, the CRP and SmartPoolManager include each other)
// Removing circularity allows flattener tools to work, which enables Etherscan verification
interface IConfigurableRightsPool {
function mintPoolShareFromLib(uint amount) external;
function pushPoolShareFromLib(address to, uint amount) external;
function pullPoolShareFromLib(address from, uint amount) external;
function burnPoolShareFromLib(uint amount) external;
function totalSupply() external view returns (uint);
function getController() external view returns (address);
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
// Needed to handle structures externally
pragma experimental ABIEncoderV2;
/**
* @author Balancer Labs
* @title Manage Configurable Rights for the smart pool
* canPauseSwapping - can setPublicSwap back to false after turning it on
* by default, it is off on initialization and can only be turned on
* canChangeSwapFee - can setSwapFee after initialization (by default, it is fixed at create time)
* canChangeWeights - can bind new token weights (allowed by default in base pool)
* canAddRemoveTokens - can bind/unbind tokens (allowed by default in base pool)
* canWhitelistLPs - can limit liquidity providers to a given set of addresses
* canChangeCap - can change the BSP cap (max # of pool tokens)
*/
library RightsManager {
// Type declarations
enum Permissions { PAUSE_SWAPPING,
CHANGE_SWAP_FEE,
CHANGE_WEIGHTS,
ADD_REMOVE_TOKENS,
WHITELIST_LPS,
CHANGE_CAP }
struct Rights {
bool canPauseSwapping;
bool canChangeSwapFee;
bool canChangeWeights;
bool canAddRemoveTokens;
bool canWhitelistLPs;
bool canChangeCap;
}
// State variables (can only be constants in a library)
bool public constant DEFAULT_CAN_PAUSE_SWAPPING = false;
bool public constant DEFAULT_CAN_CHANGE_SWAP_FEE = true;
bool public constant DEFAULT_CAN_CHANGE_WEIGHTS = true;
bool public constant DEFAULT_CAN_ADD_REMOVE_TOKENS = false;
bool public constant DEFAULT_CAN_WHITELIST_LPS = false;
bool public constant DEFAULT_CAN_CHANGE_CAP = false;
// Functions
/**
* @notice create a struct from an array (or return defaults)
* @dev If you pass an empty array, it will construct it using the defaults
* @param a - array input
* @return Rights struct
*/
function constructRights(bool[] calldata a) external pure returns (Rights memory) {
if (a.length == 0) {
return Rights(DEFAULT_CAN_PAUSE_SWAPPING,
DEFAULT_CAN_CHANGE_SWAP_FEE,
DEFAULT_CAN_CHANGE_WEIGHTS,
DEFAULT_CAN_ADD_REMOVE_TOKENS,
DEFAULT_CAN_WHITELIST_LPS,
DEFAULT_CAN_CHANGE_CAP);
}
else {
return Rights(a[0], a[1], a[2], a[3], a[4], a[5]);
}
}
/**
* @notice Convert rights struct to an array (e.g., for events, GUI)
* @dev avoids multiple calls to hasPermission
* @param rights - the rights struct to convert
* @return boolean array containing the rights settings
*/
function convertRights(Rights calldata rights) external pure returns (bool[] memory) {
bool[] memory result = new bool[](6);
result[0] = rights.canPauseSwapping;
result[1] = rights.canChangeSwapFee;
result[2] = rights.canChangeWeights;
result[3] = rights.canAddRemoveTokens;
result[4] = rights.canWhitelistLPs;
result[5] = rights.canChangeCap;
return result;
}
// Though it is actually simple, the number of branches triggers code-complexity
/* solhint-disable code-complexity */
/**
* @notice Externally check permissions using the Enum
* @param self - Rights struct containing the permissions
* @param permission - The permission to check
* @return Boolean true if it has the permission
*/
function hasPermission(Rights calldata self, Permissions permission) external pure returns (bool) {
if (Permissions.PAUSE_SWAPPING == permission) {
return self.canPauseSwapping;
}
else if (Permissions.CHANGE_SWAP_FEE == permission) {
return self.canChangeSwapFee;
}
else if (Permissions.CHANGE_WEIGHTS == permission) {
return self.canChangeWeights;
}
else if (Permissions.ADD_REMOVE_TOKENS == permission) {
return self.canAddRemoveTokens;
}
else if (Permissions.WHITELIST_LPS == permission) {
return self.canWhitelistLPs;
}
else if (Permissions.CHANGE_CAP == permission) {
return self.canChangeCap;
}
}
/* solhint-enable code-complexity */
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
/**
* @author Balancer Labs (and OpenZeppelin)
* @title Protect against reentrant calls (and also selectively protect view functions)
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {_lock_} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `_lock_` guard, functions marked as
* `_lock_` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `_lock_` entry
* points to them.
*
* Also adds a _lockview_ modifier, which doesn't create a lock, but fails
* if another _lock_ call is in progress
*/
contract BalancerReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint private constant _NOT_ENTERED = 1;
uint private constant _ENTERED = 2;
uint private _status;
constructor () internal {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `_lock_` function from another `_lock_`
* function is not supported. It is possible to prevent this from happening
* by making the `_lock_` function external, and make it call a
* `private` function that does the actual work.
*/
modifier lock() {
// On the first call to _lock_, _notEntered will be true
require(_status != _ENTERED, "ERR_REENTRY");
// Any calls to _lock_ after this point will fail
_status = _ENTERED;
_;
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
/**
* @dev Also add a modifier that doesn't create a lock, but protects functions that
* should not be called while a _lock_ function is running
*/
modifier viewlock() {
require(_status != _ENTERED, "ERR_REENTRY_VIEW");
_;
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
// Imports
import "../libraries/BalancerSafeMath.sol";
import "../interfaces/IERC20.sol";
// Contracts
/* solhint-disable func-order */
/**
* @author Balancer Labs
* @title Highly opinionated token implementation
*/
contract PCToken is IERC20 {
using BalancerSafeMath for uint;
// State variables
string public constant NAME = "Balancer Smart Pool";
uint8 public constant DECIMALS = 18;
// No leading underscore per naming convention (non-private)
// Cannot call totalSupply (name conflict)
// solhint-disable-next-line private-vars-leading-underscore
uint internal varTotalSupply;
mapping(address => uint) private _balance;
mapping(address => mapping(address => uint)) private _allowance;
string private _symbol;
string private _name;
// Event declarations
// See definitions above; must be redeclared to be emitted from this contract
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
// Function declarations
/**
* @notice Base token constructor
* @param tokenSymbol - the token symbol
*/
constructor (string memory tokenSymbol, string memory tokenName) public {
_symbol = tokenSymbol;
_name = tokenName;
}
// External functions
/**
* @notice Getter for allowance: amount spender will be allowed to spend on behalf of owner
* @param owner - owner of the tokens
* @param spender - entity allowed to spend the tokens
* @return uint - remaining amount spender is allowed to transfer
*/
function allowance(address owner, address spender) external view override returns (uint) {
return _allowance[owner][spender];
}
/**
* @notice Getter for current account balance
* @param account - address we're checking the balance of
* @return uint - token balance in the account
*/
function balanceOf(address account) external view override returns (uint) {
return _balance[account];
}
/**
* @notice Approve owner (sender) to spend a certain amount
* @dev emits an Approval event
* @param spender - entity the owner (sender) is approving to spend his tokens
* @param amount - number of tokens being approved
* @return bool - result of the approval (will always be true if it doesn't revert)
*/
function approve(address spender, uint amount) external override returns (bool) {
/* In addition to the increase/decreaseApproval functions, could
avoid the "approval race condition" by only allowing calls to approve
when the current approval amount is 0
require(_allowance[msg.sender][spender] == 0, "ERR_RACE_CONDITION");
Some token contracts (e.g., KNC), already revert if you call approve
on a non-zero allocation. To deal with these, we use the SafeApprove library
and safeApprove function when adding tokens to the pool.
*/
_allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
/**
* @notice Increase the amount the spender is allowed to spend on behalf of the owner (sender)
* @dev emits an Approval event
* @param spender - entity the owner (sender) is approving to spend his tokens
* @param amount - number of tokens being approved
* @return bool - result of the approval (will always be true if it doesn't revert)
*/
function increaseApproval(address spender, uint amount) external returns (bool) {
_allowance[msg.sender][spender] = BalancerSafeMath.badd(_allowance[msg.sender][spender], amount);
emit Approval(msg.sender, spender, _allowance[msg.sender][spender]);
return true;
}
/**
* @notice Decrease the amount the spender is allowed to spend on behalf of the owner (sender)
* @dev emits an Approval event
* @dev If you try to decrease it below the current limit, it's just set to zero (not an error)
* @param spender - entity the owner (sender) is approving to spend his tokens
* @param amount - number of tokens being approved
* @return bool - result of the approval (will always be true if it doesn't revert)
*/
function decreaseApproval(address spender, uint amount) external returns (bool) {
uint oldValue = _allowance[msg.sender][spender];
// Gas optimization - if amount == oldValue (or is larger), set to zero immediately
if (amount >= oldValue) {
_allowance[msg.sender][spender] = 0;
} else {
_allowance[msg.sender][spender] = BalancerSafeMath.bsub(oldValue, amount);
}
emit Approval(msg.sender, spender, _allowance[msg.sender][spender]);
return true;
}
/**
* @notice Transfer the given amount from sender (caller) to recipient
* @dev _move emits a Transfer event if successful
* @param recipient - entity receiving the tokens
* @param amount - number of tokens being transferred
* @return bool - result of the transfer (will always be true if it doesn't revert)
*/
function transfer(address recipient, uint amount) external override returns (bool) {
require(recipient != address(0), "ERR_ZERO_ADDRESS");
_move(msg.sender, recipient, amount);
return true;
}
/**
* @notice Transfer the given amount from sender to recipient
* @dev _move emits a Transfer event if successful; may also emit an Approval event
* @param sender - entity sending the tokens (must be caller or allowed to spend on behalf of caller)
* @param recipient - recipient of the tokens
* @param amount - number of tokens being transferred
* @return bool - result of the transfer (will always be true if it doesn't revert)
*/
function transferFrom(address sender, address recipient, uint amount) external override returns (bool) {
require(recipient != address(0), "ERR_ZERO_ADDRESS");
require(msg.sender == sender || amount <= _allowance[sender][msg.sender], "ERR_PCTOKEN_BAD_CALLER");
_move(sender, recipient, amount);
// memoize for gas optimization
uint oldAllowance = _allowance[sender][msg.sender];
// If the sender is not the caller, adjust the allowance by the amount transferred
if (msg.sender != sender && oldAllowance != uint(-1)) {
_allowance[sender][msg.sender] = BalancerSafeMath.bsub(oldAllowance, amount);
emit Approval(msg.sender, recipient, _allowance[sender][msg.sender]);
}
return true;
}
// public functions
/**
* @notice Getter for the total supply
* @dev declared external for gas optimization
* @return uint - total number of tokens in existence
*/
function totalSupply() external view override returns (uint) {
return varTotalSupply;
}
// Public functions
/**
* @dev Returns the name of the token.
* We allow the user to set this name (as well as the symbol).
* Alternatives are 1) A fixed string (original design)
* 2) A fixed string plus the user-defined symbol
* return string(abi.encodePacked(NAME, "-", _symbol));
*/
function name() external view returns (string memory) {
return _name;
}
/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() external 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() external pure returns (uint8) {
return DECIMALS;
}
// internal functions
// Mint an amount of new tokens, and add them to the balance (and total supply)
// Emit a transfer amount from the null address to this contract
function _mint(uint amount) internal virtual {
_balance[address(this)] = BalancerSafeMath.badd(_balance[address(this)], amount);
varTotalSupply = BalancerSafeMath.badd(varTotalSupply, amount);
emit Transfer(address(0), address(this), amount);
}
// Burn an amount of new tokens, and subtract them from the balance (and total supply)
// Emit a transfer amount from this contract to the null address
function _burn(uint amount) internal virtual {
// Can't burn more than we have
// Remove require for gas optimization - bsub will revert on underflow
// require(_balance[address(this)] >= amount, "ERR_INSUFFICIENT_BAL");
_balance[address(this)] = BalancerSafeMath.bsub(_balance[address(this)], amount);
varTotalSupply = BalancerSafeMath.bsub(varTotalSupply, amount);
emit Transfer(address(this), address(0), amount);
}
// Transfer tokens from sender to recipient
// Adjust balances, and emit a Transfer event
function _move(address sender, address recipient, uint amount) internal virtual {
// Can't send more than sender has
// Remove require for gas optimization - bsub will revert on underflow
// require(_balance[sender] >= amount, "ERR_INSUFFICIENT_BAL");
_balance[sender] = BalancerSafeMath.bsub(_balance[sender], amount);
_balance[recipient] = BalancerSafeMath.badd(_balance[recipient], amount);
emit Transfer(sender, recipient, amount);
}
// Transfer from this contract to recipient
// Emits a transfer event if successful
function _push(address recipient, uint amount) internal {
_move(address(this), recipient, amount);
}
// Transfer from recipient to this contract
// Emits a transfer event if successful
function _pull(address sender, uint amount) internal {
_move(sender, address(this), amount);
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.6.6;
import "./BColor.sol";
contract BConst is BBronze {
uint public constant BONE = 10**18;
uint public constant MIN_BOUND_TOKENS = 2;
uint public constant MAX_BOUND_TOKENS = 8;
uint public constant MIN_FEE = BONE / 10**6;
uint public constant MAX_FEE = BONE / 10;
uint public constant EXIT_FEE = 0;
uint public constant MIN_WEIGHT = BONE;
uint public constant MAX_WEIGHT = BONE * 50;
uint public constant MAX_TOTAL_WEIGHT = BONE * 50;
uint public constant MIN_BALANCE = BONE / 10**12;
uint public constant INIT_POOL_SUPPLY = BONE * 100;
uint public constant MIN_BPOW_BASE = 1 wei;
uint public constant MAX_BPOW_BASE = (2 * BONE) - 1 wei;
uint public constant BPOW_PRECISION = BONE / 10**10;
uint public constant MAX_IN_RATIO = BONE / 2;
uint public constant MAX_OUT_RATIO = (BONE / 3) + 1 wei;
}
/
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
// Imports
import "../../libraries/BalancerSafeMath.sol";
// Contracts
/*
* @author Balancer Labs
* @title Wrap BalancerSafeMath for testing
*/
contract BalancerSafeMathMock {
function bmul(uint a, uint b) external pure returns (uint) {
return BalancerSafeMath.bmul(a, b);
}
function bdiv(uint a, uint b) external pure returns (uint) {
return BalancerSafeMath.bdiv(a, b);
}
function bsub(uint a, uint b) external pure returns (uint) {
return BalancerSafeMath.bsub(a, b);
}
function badd(uint a, uint b) external pure returns (uint) {
return BalancerSafeMath.badd(a, b);
}
function bmod(uint a, uint b) external pure returns (uint) {
return BalancerSafeMath.bmod(a, b);
}
function bmax(uint a, uint b) external pure returns (uint) {
return BalancerSafeMath.bmax(a, b);
}
function bmin(uint a, uint b) external pure returns (uint) {
return BalancerSafeMath.bmin(a, b);
}
function baverage(uint a, uint b) external pure returns (uint) {
return BalancerSafeMath.baverage(a, b);
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
// Needed to handle structures externally
pragma experimental ABIEncoderV2;
// Imports
import "./ElasticSupplyPool.sol";
// Contracts
/**
* @author Balancer Labs
* @title Configurable Rights Pool Factory - create parameterized smart pools
* @dev Rights are held in a corresponding struct in ConfigurableRightsPool
* Index values are as follows:
* 0: canPauseSwapping - can setPublicSwap back to false after turning it on
* by default, it is off on initialization and can only be turned on
* 1: canChangeSwapFee - can setSwapFee after initialization (by default, it is fixed at create time)
* 2: canChangeWeights - can bind new token weights (allowed by default in base pool)
* 3: canAddRemoveTokens - can bind/unbind tokens (allowed by default in base pool)
* 4: canWhitelistLPs - if set, only whitelisted addresses can join pools
* (enables private pools with more than one LP)
*/
contract ESPFactory {
// State variables
// Keep a list of all Elastic Supply Pools
mapping(address => bool) private _isEsp;
// Event declarations
// Log the address of each new smart pool, and its creator
event LogNewEsp(
address indexed caller,
address indexed pool
);
// Function declarations
/**
* @notice Create a new ESP
* @dev emits a LogNewESP event
* @param factoryAddress - the BFactory instance used to create the underlying pool
* @param poolParams - CRP pool parameters
* @param rights - struct of permissions, configuring this CRP instance (see above for definitions)
*/
function newEsp(
address factoryAddress,
ConfigurableRightsPool.PoolParams calldata poolParams,
RightsManager.Rights calldata rights
)
external
returns (ElasticSupplyPool)
{
require(poolParams.constituentTokens.length >= BalancerConstants.MIN_ASSET_LIMIT, "ERR_TOO_FEW_TOKENS");
// Arrays must be parallel
require(poolParams.tokenBalances.length == poolParams.constituentTokens.length, "ERR_START_BALANCES_MISMATCH");
require(poolParams.tokenWeights.length == poolParams.constituentTokens.length, "ERR_START_WEIGHTS_MISMATCH");
ElasticSupplyPool esp = new ElasticSupplyPool(
factoryAddress,
poolParams,
rights
);
emit LogNewEsp(msg.sender, address(esp));
_isEsp[address(esp)] = true;
esp.setController(msg.sender);
return esp;
}
/**
* @notice Check to see if a given address is an ESP
* @param addr - address to check
* @return boolean indicating whether it is an ESP
*/
function isEsp(address addr) external view returns (bool) {
return _isEsp[addr];
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
// Imports
import "../interfaces/IERC20.sol";
// Libraries
/**
* @author PieDAO (ported to Balancer Labs)
* @title SafeApprove - set approval for tokens that require 0 prior approval
* @dev Perhaps to address the known ERC20 race condition issue
* See https://github.com/crytic/not-so-smart-contracts/tree/master/race_condition
* Some tokens - notably KNC - only allow approvals to be increased from 0
*/
library SafeApprove {
/**
* @notice handle approvals of tokens that require approving from a base of 0
* @param token - the token we're approving
* @param spender - entity the owner (sender) is approving to spend his tokens
* @param amount - number of tokens being approved
*/
function safeApprove(IERC20 token, address spender, uint amount) internal returns (bool) {
uint currentAllowance = token.allowance(address(this), spender);
// Do nothing if allowance is already set to this value
if(currentAllowance == amount) {
return true;
}
// If approval is not zero reset it to zero first
if(currentAllowance != 0) {
return token.approve(spender, 0);
}
// do the actual approval
return token.approve(spender, amount);
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
// Interface declarations
/* solhint-disable func-order */
interface IERC20 {
// 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, uint value);
// 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, uint value);
// Returns the amount of tokens in existence
function totalSupply() external view returns (uint);
// Returns the amount of tokens owned by account
function balanceOf(address account) external view returns (uint);
// 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 (uint);
// Sets amount as the allowance of spender over the caller’s tokens
// Returns a boolean value indicating whether the operation succeeded
// Emits an Approval event.
function approve(address spender, uint amount) external returns (bool);
// 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, uint amount) external returns (bool);
// 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, uint amount) external returns (bool);
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
interface IBPool {
function rebind(address token, uint balance, uint denorm) external;
function setSwapFee(uint swapFee) external;
function setPublicSwap(bool publicSwap) external;
function bind(address token, uint balance, uint denorm) external;
function unbind(address token) external;
function gulp(address token) external;
function isBound(address token) external view returns(bool);
function getBalance(address token) external view returns (uint);
function totalSupply() external view returns (uint);
function getSwapFee() external view returns (uint);
function isPublicSwap() external view returns (bool);
function getDenormalizedWeight(address token) external view returns (uint);
function getTotalDenormalizedWeight() external view returns (uint);
// solhint-disable-next-line func-name-mixedcase
function EXIT_FEE() external view returns (uint);
function calcPoolOutGivenSingleIn(
uint tokenBalanceIn,
uint tokenWeightIn,
uint poolSupply,
uint totalWeight,
uint tokenAmountIn,
uint swapFee
)
external pure
returns (uint poolAmountOut);
function calcSingleInGivenPoolOut(
uint tokenBalanceIn,
uint tokenWeightIn,
uint poolSupply,
uint totalWeight,
uint poolAmountOut,
uint swapFee
)
external pure
returns (uint tokenAmountIn);
function calcSingleOutGivenPoolIn(
uint tokenBalanceOut,
uint tokenWeightOut,
uint poolSupply,
uint totalWeight,
uint poolAmountIn,
uint swapFee
)
external pure
returns (uint tokenAmountOut);
function calcPoolInGivenSingleOut(
uint tokenBalanceOut,
uint tokenWeightOut,
uint poolSupply,
uint totalWeight,
uint tokenAmountOut,
uint swapFee
)
external pure
returns (uint poolAmountIn);
function getCurrentTokens()
external view
returns (address[] memory tokens);
}
interface IBFactory {
function newBPool() external returns (IBPool);
function setBLabs(address b) external;
function collect(IBPool pool) external;
function isBPool(address b) external view returns (bool);
function getBLabs() external view returns (address);
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.6.6;
// Builds new BPools, logging their addresses and providing `isBPool(address) -> (bool)`
import "./BPool.sol";
// Core contract; can't be changed. So disable solhint (reminder for v2)
/* solhint-disable func-order */
/* solhint-disable event-name-camelcase */
contract BFactory is BBronze {
event LOG_NEW_POOL(
address indexed caller,
address indexed pool
);
event LOG_BLABS(
address indexed caller,
address indexed blabs
);
mapping(address=>bool) private _isBPool;
function isBPool(address b)
external view returns (bool)
{
return _isBPool[b];
}
function newBPool()
external
returns (BPool)
{
BPool bpool = new BPool();
_isBPool[address(bpool)] = true;
emit LOG_NEW_POOL(msg.sender, address(bpool));
bpool.setController(msg.sender);
return bpool;
}
address private _blabs;
constructor() public {
_blabs = msg.sender;
}
function getBLabs()
external view
returns (address)
{
return _blabs;
}
function setBLabs(address b)
external
{
require(msg.sender == _blabs, "ERR_NOT_BLABS");
emit LOG_BLABS(msg.sender, b);
_blabs = b;
}
function collect(BPool pool)
external
{
require(msg.sender == _blabs, "ERR_NOT_BLABS");
uint collected = IERC20(pool).balanceOf(address(this));
bool xfer = pool.transfer(_blabs, collected);
require(xfer, "ERR_ERC20_FAILED");
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.6.6;
contract BadToken {
string internal _name;
string internal _symbol;
uint8 internal _decimals;
address internal _owner;
uint internal _totalSupply;
mapping(address => uint) internal _balance;
mapping(address => mapping(address=>uint)) internal _allowance;
modifier _onlyOwner_() {
require(msg.sender == _owner, "ERR_NOT_OWNER");
_;
}
event Approval(address indexed src, address indexed dst, uint amt);
event Transfer(address indexed src, address indexed dst, uint amt);
/* solhint-disable private-vars-leading-underscore */
// Math
function add(uint a, uint b) internal pure returns (uint c) {
require((c = a + b) >= a, "ERR_ADD");
}
function sub(uint a, uint b) internal pure returns (uint c) {
require((c = a - b) <= a, "ERR_SUB");
}
/* solhint-disable func-order */
constructor(
string memory name,
string memory symbol,
uint8 decimals
) public {
_name = name;
_symbol = symbol;
_decimals = decimals;
_owner = msg.sender;
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns(uint8) {
return _decimals;
}
function _move(address src, address dst, uint amt) internal virtual {
// Fail if trying to transfer 0
//require(amt > 0, "ERR_NO_ZERO_XFER");
require(_balance[src] >= amt, "ERR_INSUFFICIENT_BAL");
_balance[src] = sub(_balance[src], amt);
_balance[dst] = add(_balance[dst], amt);
emit Transfer(src, dst, amt);
}
function _push(address to, uint amt) internal {
_move(address(this), to, amt);
}
function _pull(address from, uint amt) internal {
_move(from, address(this), amt);
}
function _mint(address dst, uint amt) internal {
_balance[dst] = add(_balance[dst], amt);
_totalSupply = add(_totalSupply, amt);
emit Transfer(address(0), dst, amt);
}
function allowance(address src, address dst) external view returns (uint) {
return _allowance[src][dst];
}
function balanceOf(address whom) external view returns (uint) {
return _balance[whom];
}
function totalSupply() public view returns (uint) {
return _totalSupply;
}
function approve(address dst, uint amt) external virtual returns (bool) {
// Fail if prior approval is not zero
//require(_allowance[msg.sender][dst] == 0, "ERR_PRIOR_ZERO_APPROVE");
_allowance[msg.sender][dst] = amt;
emit Approval(msg.sender, dst, amt);
return true;
}
function mint(address dst, uint256 amt) public _onlyOwner_ returns (bool) {
_mint(dst, amt);
return true;
}
function burn(uint amt) public returns (bool) {
require(_balance[address(this)] >= amt, "ERR_INSUFFICIENT_BAL");
_balance[address(this)] = sub(_balance[address(this)], amt);
_totalSupply = sub(_totalSupply, amt);
emit Transfer(address(this), address(0), amt);
return true;
}
function transfer(address dst, uint amt) external virtual returns (bool) {
_move(msg.sender, dst, amt);
return true;
}
function transferFrom(address src, address dst, uint amt) external virtual returns (bool) {
require(msg.sender == src || amt <= _allowance[src][msg.sender], "ERR_BTOKEN_BAD_CALLER");
_move(src, dst, amt);
if (msg.sender != src && _allowance[src][msg.sender] != uint256(-1)) {
_allowance[src][msg.sender] = sub(_allowance[src][msg.sender], amt);
emit Approval(msg.sender, dst, _allowance[src][msg.sender]);
}
return true;
}
}
/* solhint-disable no-empty-blocks */
contract NoZeroXferToken is BadToken {
constructor(
string memory name,
string memory symbol,
uint8 decimals
)
public
BadToken(name, symbol, decimals)
{
}
function _move(address src, address dst, uint amt) internal override {
// Fail if trying to transfer 0
require(amt > 0, "ERR_NO_ZERO_XFER");
require(_balance[src] >= amt, "ERR_INSUFFICIENT_BAL");
_balance[src] = sub(_balance[src], amt);
_balance[dst] = add(_balance[dst], amt);
emit Transfer(src, dst, amt);
}
}
contract NoPriorApprovalToken is BadToken {
constructor(
string memory name,
string memory symbol,
uint8 decimals
)
public
BadToken(name, symbol, decimals)
{
}
function approve(address dst, uint amt) external override returns (bool) {
// Fail if prior approval is not zero
require(_allowance[msg.sender][dst] == 0, "ERR_PRIOR_ZERO_APPROVE");
_allowance[msg.sender][dst] = amt;
emit Approval(msg.sender, dst, amt);
return true;
}
}
contract FalseReturningToken is BadToken {
constructor(
string memory name,
string memory symbol,
uint8 decimals
)
public
BadToken(name, symbol, decimals)
{
}
function transfer(address dst, uint amt) external override returns (bool) {
_move(msg.sender, dst, amt);
// Don't return anything (or return false; same result)
}
function transferFrom(address src, address dst, uint amt) external override returns (bool) {
require(msg.sender == src || amt <= _allowance[src][msg.sender], "ERR_BTOKEN_BAD_CALLER");
_move(src, dst, amt);
if (msg.sender != src && _allowance[src][msg.sender] != uint256(-1)) {
_allowance[src][msg.sender] = sub(_allowance[src][msg.sender], amt);
emit Approval(msg.sender, dst, _allowance[src][msg.sender]);
}
return false;
}
}
contract TaxingToken is BadToken {
constructor(
string memory name,
string memory symbol,
uint8 decimals
)
public
BadToken(name, symbol, decimals)
{
}
function transfer(address dst, uint amt) external override returns (bool) {
_move(msg.sender, dst, amt - 1);
return true;
}
function transferFrom(address src, address dst, uint amt) external override returns (bool) {
require(msg.sender == src || amt <= _allowance[src][msg.sender], "ERR_BTOKEN_BAD_CALLER");
_move(src, dst, amt - 1);
if (msg.sender != src && _allowance[src][msg.sender] != uint256(-1)) {
_allowance[src][msg.sender] = sub(_allowance[src][msg.sender], amt - 1);
emit Approval(msg.sender, dst, _allowance[src][msg.sender]);
}
return true;
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.6.6;
import "./BConst.sol";
// Core contract; can't be changed. So disable solhint (reminder for v2)
/* solhint-disable private-vars-leading-underscore */
contract BNum is BConst {
function btoi(uint a)
internal pure
returns (uint)
{
return a / BONE;
}
function bfloor(uint a)
internal pure
returns (uint)
{
return btoi(a) * BONE;
}
function badd(uint a, uint b)
internal pure
returns (uint)
{
uint c = a + b;
require(c >= a, "ERR_ADD_OVERFLOW");
return c;
}
function bsub(uint a, uint b)
internal pure
returns (uint)
{
(uint c, bool flag) = bsubSign(a, b);
require(!flag, "ERR_SUB_UNDERFLOW");
return c;
}
function bsubSign(uint a, uint b)
internal pure
returns (uint, bool)
{
if (a >= b) {
return (a - b, false);
} else {
return (b - a, true);
}
}
function bmul(uint a, uint b)
internal pure
returns (uint)
{
uint c0 = a * b;
require(a == 0 || c0 / a == b, "ERR_MUL_OVERFLOW");
uint c1 = c0 + (BONE / 2);
require(c1 >= c0, "ERR_MUL_OVERFLOW");
uint c2 = c1 / BONE;
return c2;
}
function bdiv(uint a, uint b)
internal pure
returns (uint)
{
require(b != 0, "ERR_DIV_ZERO");
uint c0 = a * BONE;
require(a == 0 || c0 / a == BONE, "ERR_DIV_INTERNAL"); // bmul overflow
uint c1 = c0 + (b / 2);
require(c1 >= c0, "ERR_DIV_INTERNAL"); // badd require
uint c2 = c1 / b;
return c2;
}
// DSMath.wpow
function bpowi(uint a, uint n)
internal pure
returns (uint)
{
uint z = n % 2 != 0 ? a : BONE;
for (n /= 2; n != 0; n /= 2) {
a = bmul(a, a);
if (n % 2 != 0) {
z = bmul(z, a);
}
}
return z;
}
// Compute b^(e.w) by splitting it into (b^e)*(b^0.w).
// Use `bpowi` for `b^e` and `bpowK` for k iterations
// of approximation of b^0.w
function bpow(uint base, uint exp)
internal pure
returns (uint)
{
require(base >= MIN_BPOW_BASE, "ERR_BPOW_BASE_TOO_LOW");
require(base <= MAX_BPOW_BASE, "ERR_BPOW_BASE_TOO_HIGH");
uint whole = bfloor(exp);
uint remain = bsub(exp, whole);
uint wholePow = bpowi(base, btoi(whole));
if (remain == 0) {
return wholePow;
}
uint partialResult = bpowApprox(base, remain, BPOW_PRECISION);
return bmul(wholePow, partialResult);
}
function bpowApprox(uint base, uint exp, uint precision)
internal pure
returns (uint)
{
// term 0:
uint a = exp;
(uint x, bool xneg) = bsubSign(base, BONE);
uint term = BONE;
uint sum = term;
bool negative = false;
// term(k) = numer / denom
// = (product(a - i - 1, i=1-->k) * x^k) / (k!)
// each iteration, multiply previous term by (a-(k-1)) * x / k
// continue until term is less than precision
for (uint i = 1; term >= precision; i++) {
uint bigK = i * BONE;
(uint c, bool cneg) = bsubSign(a, bsub(bigK, BONE));
term = bmul(term, bmul(c, x));
term = bdiv(term, bigK);
if (term == 0) break;
if (xneg) negative = !negative;
if (cneg) negative = !negative;
if (negative) {
sum = bsub(sum, term);
} else {
sum = badd(sum, term);
}
}
return sum;
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.6.6;
import "./BNum.sol";
import "../PCToken.sol";
// Highly opinionated token implementation
/*
interface IERC20 {
event Approval(address indexed src, address indexed dst, uint amt);
event Transfer(address indexed src, address indexed dst, uint amt);
function totalSupply() external view returns (uint);
function balanceOf(address whom) external view returns (uint);
function allowance(address src, address dst) external view returns (uint);
function approve(address dst, uint amt) external returns (bool);
function transfer(address dst, uint amt) external returns (bool);
function transferFrom(
address src, address dst, uint amt
) external returns (bool);
}
*/
// Core contract; can't be changed. So disable solhint (reminder for v2)
/* solhint-disable func-order */
contract BTokenBase is BNum {
mapping(address => uint) internal _balance;
mapping(address => mapping(address=>uint)) internal _allowance;
uint internal _totalSupply;
event Approval(address indexed src, address indexed dst, uint amt);
event Transfer(address indexed src, address indexed dst, uint amt);
function _mint(uint amt) internal {
_balance[address(this)] = badd(_balance[address(this)], amt);
_totalSupply = badd(_totalSupply, amt);
emit Transfer(address(0), address(this), amt);
}
function _burn(uint amt) internal {
require(_balance[address(this)] >= amt, "ERR_INSUFFICIENT_BAL");
_balance[address(this)] = bsub(_balance[address(this)], amt);
_totalSupply = bsub(_totalSupply, amt);
emit Transfer(address(this), address(0), amt);
}
function _move(address src, address dst, uint amt) internal {
require(_balance[src] >= amt, "ERR_INSUFFICIENT_BAL");
_balance[src] = bsub(_balance[src], amt);
_balance[dst] = badd(_balance[dst], amt);
emit Transfer(src, dst, amt);
}
function _push(address to, uint amt) internal {
_move(address(this), to, amt);
}
function _pull(address from, uint amt) internal {
_move(from, address(this), amt);
}
}
contract BToken is BTokenBase, IERC20 {
string private _name = "Balancer Pool Token";
string private _symbol = "BPT";
uint8 private _decimals = 18;
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns(uint8) {
return _decimals;
}
function allowance(address src, address dst) external view override returns (uint) {
return _allowance[src][dst];
}
function balanceOf(address whom) external view override returns (uint) {
return _balance[whom];
}
function totalSupply() public view override returns (uint) {
return _totalSupply;
}
function approve(address dst, uint amt) external override returns (bool) {
_allowance[msg.sender][dst] = amt;
emit Approval(msg.sender, dst, amt);
return true;
}
function increaseApproval(address dst, uint amt) external returns (bool) {
_allowance[msg.sender][dst] = badd(_allowance[msg.sender][dst], amt);
emit Approval(msg.sender, dst, _allowance[msg.sender][dst]);
return true;
}
function decreaseApproval(address dst, uint amt) external returns (bool) {
uint oldValue = _allowance[msg.sender][dst];
if (amt > oldValue) {
_allowance[msg.sender][dst] = 0;
} else {
_allowance[msg.sender][dst] = bsub(oldValue, amt);
}
emit Approval(msg.sender, dst, _allowance[msg.sender][dst]);
return true;
}
function transfer(address dst, uint amt) external override returns (bool) {
_move(msg.sender, dst, amt);
return true;
}
function transferFrom(address src, address dst, uint amt) external override returns (bool) {
require(msg.sender == src || amt <= _allowance[src][msg.sender], "ERR_BTOKEN_BAD_CALLER");
_move(src, dst, amt);
if (msg.sender != src && _allowance[src][msg.sender] != uint256(-1)) {
_allowance[src][msg.sender] = bsub(_allowance[src][msg.sender], amt);
emit Approval(msg.sender, dst, _allowance[src][msg.sender]);
}
return true;
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.6.6;
// abstract contract BColor {
// function getColor()
// external view virtual
// returns (bytes32);
// }
contract BBronze {
function getColor()
external view
returns (bytes32) {
return bytes32("BRONZE");
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
contract BalancerOwnable {
// State variables
address private _owner;
// Event declarations
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
// Modifiers
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(_owner == msg.sender, "ERR_NOT_CONTROLLER");
_;
}
// Function declarations
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor () internal {
_owner = msg.sender;
}
/**
* @notice Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner
* @dev external for gas optimization
* @param newOwner - address of new owner
*/
function setController(address newOwner) external onlyOwner {
require(newOwner != address(0), "ERR_ZERO_ADDRESS");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
/**
* @notice Returns the address of the current owner
* @dev external for gas optimization
* @return address - of the owner (AKA controller)
*/
function getController() external view returns (address) {
return _owner;
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
// Imports
import "./BalancerConstants.sol";
/**
* @author Balancer Labs
* @title SafeMath - wrap Solidity operators to prevent underflow/overflow
* @dev badd and bsub are basically identical to OpenZeppelin SafeMath; mul/div have extra checks
*/
library BalancerSafeMath {
/**
* @notice Safe addition
* @param a - first operand
* @param b - second operand
* @dev if we are adding b to a, the resulting sum must be greater than a
* @return - sum of operands; throws if overflow
*/
function badd(uint a, uint b) internal pure returns (uint) {
uint c = a + b;
require(c >= a, "ERR_ADD_OVERFLOW");
return c;
}
/**
* @notice Safe unsigned subtraction
* @param a - first operand
* @param b - second operand
* @dev Do a signed subtraction, and check that it produces a positive value
* (i.e., a - b is valid if b <= a)
* @return - a - b; throws if underflow
*/
function bsub(uint a, uint b) internal pure returns (uint) {
(uint c, bool negativeResult) = bsubSign(a, b);
require(!negativeResult, "ERR_SUB_UNDERFLOW");
return c;
}
/**
* @notice Safe signed subtraction
* @param a - first operand
* @param b - second operand
* @dev Do a signed subtraction
* @return - difference between a and b, and a flag indicating a negative result
* (i.e., a - b if a is greater than or equal to b; otherwise b - a)
*/
function bsubSign(uint a, uint b) internal pure returns (uint, bool) {
if (b <= a) {
return (a - b, false);
} else {
return (b - a, true);
}
}
/**
* @notice Safe multiplication
* @param a - first operand
* @param b - second operand
* @dev Multiply safely (and efficiently), rounding down
* @return - product of operands; throws if overflow or rounding error
*/
function bmul(uint a, uint b) internal pure returns (uint) {
// Gas optimization (see github.com/OpenZeppelin/openzeppelin-contracts/pull/522)
if (a == 0) {
return 0;
}
// Standard overflow check: a/a*b=b
uint c0 = a * b;
require(c0 / a == b, "ERR_MUL_OVERFLOW");
// Round to 0 if x*y < BONE/2?
uint c1 = c0 + (BalancerConstants.BONE / 2);
require(c1 >= c0, "ERR_MUL_OVERFLOW");
uint c2 = c1 / BalancerConstants.BONE;
return c2;
}
/**
* @notice Safe division
* @param dividend - first operand
* @param divisor - second operand
* @dev Divide safely (and efficiently), rounding down
* @return - quotient; throws if overflow or rounding error
*/
function bdiv(uint dividend, uint divisor) internal pure returns (uint) {
require(divisor != 0, "ERR_DIV_ZERO");
// Gas optimization
if (dividend == 0){
return 0;
}
uint c0 = dividend * BalancerConstants.BONE;
require(c0 / dividend == BalancerConstants.BONE, "ERR_DIV_INTERNAL"); // bmul overflow
uint c1 = c0 + (divisor / 2);
require(c1 >= c0, "ERR_DIV_INTERNAL"); // badd require
uint c2 = c1 / divisor;
return c2;
}
/**
* @notice Safe unsigned integer modulo
* @dev Returns the remainder of dividing two unsigned integers.
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* @param dividend - first operand
* @param divisor - second operand -- cannot be zero
* @return - quotient; throws if overflow or rounding error
*/
function bmod(uint dividend, uint divisor) internal pure returns (uint) {
require(divisor != 0, "ERR_MODULO_BY_ZERO");
return dividend % divisor;
}
/**
* @notice Safe unsigned integer max
* @dev Returns the greater of the two input values
*
* @param a - first operand
* @param b - second operand
* @return - the maximum of a and b
*/
function bmax(uint a, uint b) internal pure returns (uint) {
return a >= b ? a : b;
}
/**
* @notice Safe unsigned integer min
* @dev returns b, if b < a; otherwise returns a
*
* @param a - first operand
* @param b - second operand
* @return - the lesser of the two input values
*/
function bmin(uint a, uint b) internal pure returns (uint) {
return a < b ? a : b;
}
/**
* @notice Safe unsigned integer average
* @dev Guard against (a+b) overflow by dividing each operand separately
*
* @param a - first operand
* @param b - second operand
* @return - the average of the two values
*/
function baverage(uint a, uint b) internal pure returns (uint) {
// (a + b) / 2 can overflow, so we distribute
return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2);
}
/**
* @notice Babylonian square root implementation
* @dev (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)
* @param y - operand
* @return z - the square root result
*/
function sqrt(uint y) internal pure returns (uint z) {
if (y > 3) {
z = y;
uint x = y / 2 + 1;
while (x < z) {
z = x;
x = (y / x + x) / 2;
}
}
else if (y != 0) {
z = 1;
}
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
// Needed to handle structures externally
pragma experimental ABIEncoderV2;
// Imports
import "./IBFactory.sol";
import "./PCToken.sol";
import "./utils/BalancerReentrancyGuard.sol";
import "./utils/BalancerOwnable.sol";
// Interfaces
// Libraries
import { RightsManager } from "../libraries/RightsManager.sol";
import "../libraries/SmartPoolManager.sol";
import "../libraries/SafeApprove.sol";
// Contracts
/**
* @author Balancer Labs
* @title Smart Pool with customizable features
* @notice PCToken is the "Balancer Smart Pool" token (transferred upon finalization)
* @dev Rights are defined as follows (index values into the array)
* 0: canPauseSwapping - can setPublicSwap back to false after turning it on
* by default, it is off on initialization and can only be turned on
* 1: canChangeSwapFee - can setSwapFee after initialization (by default, it is fixed at create time)
* 2: canChangeWeights - can bind new token weights (allowed by default in base pool)
* 3: canAddRemoveTokens - can bind/unbind tokens (allowed by default in base pool)
* 4: canWhitelistLPs - can restrict LPs to a whitelist
* 5: canChangeCap - can change the BSP cap (max # of pool tokens)
*
* Note that functions called on bPool and bFactory may look like internal calls,
* but since they are contracts accessed through an interface, they are really external.
* To make this explicit, we could write "IBPool(address(bPool)).function()" everywhere,
* instead of "bPool.function()".
*/
contract ConfigurableRightsPool is PCToken, BalancerOwnable, BalancerReentrancyGuard {
using BalancerSafeMath for uint;
using SafeApprove for IERC20;
// Type declarations
struct PoolParams {
// Balancer Pool Token (representing shares of the pool)
string poolTokenSymbol;
string poolTokenName;
// Tokens inside the Pool
address[] constituentTokens;
uint[] tokenBalances;
uint[] tokenWeights;
uint swapFee;
}
// State variables
IBFactory public bFactory;
IBPool public bPool;
// Struct holding the rights configuration
RightsManager.Rights public rights;
// Hold the parameters used in updateWeightsGradually
SmartPoolManager.GradualUpdateParams public gradualUpdate;
// This is for adding a new (currently unbound) token to the pool
// It's a two-step process: commitAddToken(), then applyAddToken()
SmartPoolManager.NewTokenParams public newToken;
// Fee is initialized on creation, and can be changed if permission is set
// Only needed for temporary storage between construction and createPool
// Thereafter, the swap fee should always be read from the underlying pool
uint private _initialSwapFee;
// Store the list of tokens in the pool, and balances
// NOTE that the token list is *only* used to store the pool tokens between
// construction and createPool - thereafter, use the underlying BPool's list
// (avoids synchronization issues)
address[] private _initialTokens;
uint[] private _initialBalances;
// Enforce a minimum time between the start and end blocks
uint public minimumWeightChangeBlockPeriod;
// Enforce a mandatory wait time between updates
// This is also the wait time between committing and applying a new token
uint public addTokenTimeLockInBlocks;
// Whitelist of LPs (if configured)
mapping(address => bool) private _liquidityProviderWhitelist;
// Cap on the pool size (i.e., # of tokens minted when joining)
// Limits the risk of experimental pools; failsafe/backup for fixed-size pools
uint public bspCap;
// Event declarations
// Anonymous logger event - can only be filtered by contract address
event LogCall(
bytes4 indexed sig,
address indexed caller,
bytes data
) anonymous;
event LogJoin(
address indexed caller,
address indexed tokenIn,
uint tokenAmountIn
);
event LogExit(
address indexed caller,
address indexed tokenOut,
uint tokenAmountOut
);
event CapChanged(
address indexed caller,
uint oldCap,
uint newCap
);
event NewTokenCommitted(
address indexed token,
address indexed pool,
address indexed caller
);
// Modifiers
modifier logs() {
emit LogCall(msg.sig, msg.sender, msg.data);
_;
}
// Mark functions that require delegation to the underlying Pool
modifier needsBPool() {
require(address(bPool) != address(0), "ERR_NOT_CREATED");
_;
}
modifier lockUnderlyingPool() {
// Turn off swapping on the underlying pool during joins
// Otherwise tokens with callbacks would enable attacks involving simultaneous swaps and joins
bool origSwapState = bPool.isPublicSwap();
bPool.setPublicSwap(false);
_;
bPool.setPublicSwap(origSwapState);
}
// Default values for these variables (used only in updateWeightsGradually), set in the constructor
// Pools without permission to update weights cannot use them anyway, and should call
// the default createPool() function.
// To override these defaults, pass them into the overloaded createPool()
// Period is in blocks; 500 blocks ~ 2 hours; 90,000 blocks ~ 2 weeks
uint public constant DEFAULT_MIN_WEIGHT_CHANGE_BLOCK_PERIOD = 90000;
uint public constant DEFAULT_ADD_TOKEN_TIME_LOCK_IN_BLOCKS = 500;
// Function declarations
/**
* @notice Construct a new Configurable Rights Pool (wrapper around BPool)
* @dev _initialTokens and _swapFee are only used for temporary storage between construction
* and create pool, and should not be used thereafter! _initialTokens is destroyed in
* createPool to prevent this, and _swapFee is kept in sync (defensively), but
* should never be used except in this constructor and createPool()
* @param factoryAddress - the BPoolFactory used to create the underlying pool
* @param poolParams - struct containing pool parameters
* @param rightsStruct - Set of permissions we are assigning to this smart pool
*/
constructor(
address factoryAddress,
PoolParams memory poolParams,
RightsManager.Rights memory rightsStruct
)
public
PCToken(poolParams.poolTokenSymbol, poolParams.poolTokenName)
{
// We don't have a pool yet; check now or it will fail later (in order of likelihood to fail)
// (and be unrecoverable if they don't have permission set to change it)
// Most likely to fail, so check first
require(poolParams.swapFee >= BalancerConstants.MIN_FEE, "ERR_INVALID_SWAP_FEE");
require(poolParams.swapFee <= BalancerConstants.MAX_FEE, "ERR_INVALID_SWAP_FEE");
// Arrays must be parallel
require(poolParams.tokenBalances.length == poolParams.constituentTokens.length, "ERR_START_BALANCES_MISMATCH");
require(poolParams.tokenWeights.length == poolParams.constituentTokens.length, "ERR_START_WEIGHTS_MISMATCH");
// Cannot have too many or too few - technically redundant, since BPool.bind() would fail later
// But if we don't check now, we could have a useless contract with no way to create a pool
require(poolParams.constituentTokens.length >= BalancerConstants.MIN_ASSET_LIMIT, "ERR_TOO_FEW_TOKENS");
require(poolParams.constituentTokens.length <= BalancerConstants.MAX_ASSET_LIMIT, "ERR_TOO_MANY_TOKENS");
// There are further possible checks (e.g., if they use the same token twice), but
// we can let bind() catch things like that (i.e., not things that might reasonably work)
SmartPoolManager.verifyTokenCompliance(poolParams.constituentTokens);
bFactory = IBFactory(factoryAddress);
rights = rightsStruct;
_initialTokens = poolParams.constituentTokens;
_initialBalances = poolParams.tokenBalances;
_initialSwapFee = poolParams.swapFee;
// These default block time parameters can be overridden in createPool
minimumWeightChangeBlockPeriod = DEFAULT_MIN_WEIGHT_CHANGE_BLOCK_PERIOD;
addTokenTimeLockInBlocks = DEFAULT_ADD_TOKEN_TIME_LOCK_IN_BLOCKS;
gradualUpdate.startWeights = poolParams.tokenWeights;
// Initializing (unnecessarily) for documentation - 0 means no gradual weight change has been initiated
gradualUpdate.startBlock = 0;
// By default, there is no cap (unlimited pool token minting)
bspCap = BalancerConstants.MAX_UINT;
}
// External functions
/**
* @notice Set the swap fee on the underlying pool
* @dev Keep the local version and core in sync (see below)
* bPool is a contract interface; function calls on it are external
* @param swapFee in Wei
*/
function setSwapFee(uint swapFee)
external
logs
lock
onlyOwner
needsBPool
virtual
{
require(rights.canChangeSwapFee, "ERR_NOT_CONFIGURABLE_SWAP_FEE");
// Underlying pool will check against min/max fee
bPool.setSwapFee(swapFee);
}
/**
* @notice Getter for the publicSwap field on the underlying pool
* @dev viewLock, because setPublicSwap is lock
* bPool is a contract interface; function calls on it are external
* @return Current value of isPublicSwap
*/
function isPublicSwap()
external
view
viewlock
needsBPool
virtual
returns (bool)
{
return bPool.isPublicSwap();
}
/**
* @notice Set the cap (max # of pool tokens)
* @dev _bspCap defaults in the constructor to unlimited
* Can set to 0 (or anywhere below the current supply), to halt new investment
* Prevent setting it before creating a pool, since createPool sets to intialSupply
* (it does this to avoid an unlimited cap window between construction and createPool)
* Therefore setting it before then has no effect, so should not be allowed
* @param newCap - new value of the cap
*/
function setCap(uint newCap)
external
logs
lock
needsBPool
onlyOwner
{
require(rights.canChangeCap, "ERR_CANNOT_CHANGE_CAP");
emit CapChanged(msg.sender, bspCap, newCap);
bspCap = newCap;
}
/**
* @notice Set the public swap flag on the underlying pool
* @dev If this smart pool has canPauseSwapping enabled, we can turn publicSwap off if it's already on
* Note that if they turn swapping off - but then finalize the pool - finalizing will turn the
* swapping back on. They're not supposed to finalize the underlying pool... would defeat the
* smart pool functions. (Only the owner can finalize the pool - which is this contract -
* so there is no risk from outside.)
*
* bPool is a contract interface; function calls on it are external
* @param publicSwap new value of the swap
*/
function setPublicSwap(bool publicSwap)
external
logs
lock
onlyOwner
needsBPool
virtual
{
require(rights.canPauseSwapping, "ERR_NOT_PAUSABLE_SWAP");
bPool.setPublicSwap(publicSwap);
}
/**
* @notice Create a new Smart Pool - and set the block period time parameters
* @dev Initialize the swap fee to the value provided in the CRP constructor
* Can be changed if the canChangeSwapFee permission is enabled
* Time parameters will be fixed at these values
*
* If this contract doesn't have canChangeWeights permission - or you want to use the default
* values, the block time arguments are not needed, and you can just call the single-argument
* createPool()
* @param initialSupply - Starting token balance
* @param minimumWeightChangeBlockPeriodParam - Enforce a minimum time between the start and end blocks
* @param addTokenTimeLockInBlocksParam - Enforce a mandatory wait time between updates
* This is also the wait time between committing and applying a new token
*/
function createPool(
uint initialSupply,
uint minimumWeightChangeBlockPeriodParam,
uint addTokenTimeLockInBlocksParam
)
external
onlyOwner
logs
lock
virtual
{
require (minimumWeightChangeBlockPeriodParam >= addTokenTimeLockInBlocksParam,
"ERR_INCONSISTENT_TOKEN_TIME_LOCK");
minimumWeightChangeBlockPeriod = minimumWeightChangeBlockPeriodParam;
addTokenTimeLockInBlocks = addTokenTimeLockInBlocksParam;
createPoolInternal(initialSupply);
}
/**
* @notice Create a new Smart Pool
* @dev Delegates to internal function
* @param initialSupply starting token balance
*/
function createPool(uint initialSupply)
external
onlyOwner
logs
lock
virtual
{
createPoolInternal(initialSupply);
}
/**
* @notice Update the weight of an existing token
* @dev Notice Balance is not an input (like with rebind on BPool) since we will require prices not to change
* This is achieved by forcing balances to change proportionally to weights, so that prices don't change
* If prices could be changed, this would allow the controller to drain the pool by arbing price changes
* @param token - token to be reweighted
* @param newWeight - new weight of the token
*/
function updateWeight(address token, uint newWeight)
external
logs
lock
onlyOwner
needsBPool
virtual
{
require(rights.canChangeWeights, "ERR_NOT_CONFIGURABLE_WEIGHTS");
// We don't want people to set weights manually if there's a block-based update in progress
require(gradualUpdate.startBlock == 0, "ERR_NO_UPDATE_DURING_GRADUAL");
// Delegate to library to save space
SmartPoolManager.updateWeight(IConfigurableRightsPool(address(this)), bPool, token, newWeight);
}
/**
* @notice Update weights in a predetermined way, between startBlock and endBlock,
* through external calls to pokeWeights
* @dev Must call pokeWeights at least once past the end for it to do the final update
* and enable calling this again.
* It is possible to call updateWeightsGradually during an update in some use cases
* For instance, setting newWeights to currentWeights to stop the update where it is
* @param newWeights - final weights we want to get to. Note that the ORDER (and number) of
* tokens can change if you have added or removed tokens from the pool
* It ensures the counts are correct, but can't help you with the order!
* You can get the underlying BPool (it's public), and call
* getCurrentTokens() to see the current ordering, if you're not sure
* @param startBlock - when weights should start to change
* @param endBlock - when weights will be at their final values
*/
function updateWeightsGradually(
uint[] calldata newWeights,
uint startBlock,
uint endBlock
)
external
logs
lock
onlyOwner
needsBPool
virtual
{
require(rights.canChangeWeights, "ERR_NOT_CONFIGURABLE_WEIGHTS");
// Don't start this when we're in the middle of adding a new token
require(!newToken.isCommitted, "ERR_PENDING_TOKEN_ADD");
// Library computes the startBlock, computes startWeights as the current
// denormalized weights of the core pool tokens.
SmartPoolManager.updateWeightsGradually(
bPool,
gradualUpdate,
newWeights,
startBlock,
endBlock,
minimumWeightChangeBlockPeriod
);
}
/**
* @notice External function called to make the contract update weights according to plan
* @dev Still works if we poke after the end of the period; also works if the weights don't change
* Resets if we are poking beyond the end, so that we can do it again
*/
function pokeWeights()
external
logs
lock
needsBPool
virtual
{
require(rights.canChangeWeights, "ERR_NOT_CONFIGURABLE_WEIGHTS");
// Delegate to library to save space
SmartPoolManager.pokeWeights(bPool, gradualUpdate);
}
/**
* @notice Schedule (commit) a token to be added; must call applyAddToken after a fixed
* number of blocks to actually add the token
*
* @dev The purpose of this two-stage commit is to give warning of a potentially dangerous
* operation. A malicious pool operator could add a large amount of a low-value token,
* then drain the pool through price manipulation. Of course, there are many
* legitimate purposes, such as adding additional collateral tokens.
*
* @param token - the token to be added
* @param balance - how much to be added
* @param denormalizedWeight - the desired token weight
*/
function commitAddToken(
address token,
uint balance,
uint denormalizedWeight
)
external
logs
lock
onlyOwner
needsBPool
virtual
{
require(rights.canAddRemoveTokens, "ERR_CANNOT_ADD_REMOVE_TOKENS");
// Can't do this while a progressive update is happening
require(gradualUpdate.startBlock == 0, "ERR_NO_UPDATE_DURING_GRADUAL");
SmartPoolManager.verifyTokenCompliance(token);
emit NewTokenCommitted(token, address(this), msg.sender);
// Delegate to library to save space
SmartPoolManager.commitAddToken(
bPool,
token,
balance,
denormalizedWeight,
newToken
);
}
/**
* @notice Add the token previously committed (in commitAddToken) to the pool
*/
function applyAddToken()
external
logs
lock
onlyOwner
needsBPool
virtual
{
require(rights.canAddRemoveTokens, "ERR_CANNOT_ADD_REMOVE_TOKENS");
// Delegate to library to save space
SmartPoolManager.applyAddToken(
IConfigurableRightsPool(address(this)),
bPool,
addTokenTimeLockInBlocks,
newToken
);
}
/**
* @notice Remove a token from the pool
* @dev bPool is a contract interface; function calls on it are external
* @param token - token to remove
*/
function removeToken(address token)
external
logs
lock
onlyOwner
needsBPool
{
// It's possible to have remove rights without having add rights
require(rights.canAddRemoveTokens,"ERR_CANNOT_ADD_REMOVE_TOKENS");
// After createPool, token list is maintained in the underlying BPool
require(!newToken.isCommitted, "ERR_REMOVE_WITH_ADD_PENDING");
// Prevent removing during an update (or token lists can get out of sync)
require(gradualUpdate.startBlock == 0, "ERR_NO_UPDATE_DURING_GRADUAL");
// Delegate to library to save space
SmartPoolManager.removeToken(IConfigurableRightsPool(address(this)), bPool, token);
}
/**
* @notice Join a pool
* @dev Emits a LogJoin event (for each token)
* bPool is a contract interface; function calls on it are external
* @param poolAmountOut - number of pool tokens to receive
* @param maxAmountsIn - Max amount of asset tokens to spend
*/
function joinPool(uint poolAmountOut, uint[] calldata maxAmountsIn)
external
logs
lock
needsBPool
lockUnderlyingPool
{
require(!rights.canWhitelistLPs || _liquidityProviderWhitelist[msg.sender],
"ERR_NOT_ON_WHITELIST");
// Delegate to library to save space
// Library computes actualAmountsIn, and does many validations
// Cannot call the push/pull/min from an external library for
// any of these pool functions. Since msg.sender can be anybody,
// they must be internal
uint[] memory actualAmountsIn = SmartPoolManager.joinPool(
IConfigurableRightsPool(address(this)),
bPool,
poolAmountOut,
maxAmountsIn
);
// After createPool, token list is maintained in the underlying BPool
address[] memory poolTokens = bPool.getCurrentTokens();
for (uint i = 0; i < poolTokens.length; i++) {
address t = poolTokens[i];
uint tokenAmountIn = actualAmountsIn[i];
emit LogJoin(msg.sender, t, tokenAmountIn);
_pullUnderlying(t, msg.sender, tokenAmountIn);
}
_mintPoolShare(poolAmountOut);
_pushPoolShare(msg.sender, poolAmountOut);
}
/**
* @notice Exit a pool - redeem pool tokens for underlying assets
* @dev Emits a LogExit event for each token
* bPool is a contract interface; function calls on it are external
* @param poolAmountIn - amount of pool tokens to redeem
* @param minAmountsOut - minimum amount of asset tokens to receive
*/
function exitPool(uint poolAmountIn, uint[] calldata minAmountsOut)
external
logs
lock
needsBPool
lockUnderlyingPool
{
// Delegate to library to save space
// Library computes actualAmountsOut, and does many validations
// Also computes the exitFee and pAiAfterExitFee
(uint exitFee,
uint pAiAfterExitFee,
uint[] memory actualAmountsOut) = SmartPoolManager.exitPool(
IConfigurableRightsPool(address(this)),
bPool,
poolAmountIn,
minAmountsOut
);
_pullPoolShare(msg.sender, poolAmountIn);
_pushPoolShare(address(bFactory), exitFee);
_burnPoolShare(pAiAfterExitFee);
// After createPool, token list is maintained in the underlying BPool
address[] memory poolTokens = bPool.getCurrentTokens();
for (uint i = 0; i < poolTokens.length; i++) {
address t = poolTokens[i];
uint tokenAmountOut = actualAmountsOut[i];
emit LogExit(msg.sender, t, tokenAmountOut);
_pushUnderlying(t, msg.sender, tokenAmountOut);
}
}
/**
* @notice Join by swapping a fixed amount of an external token in (must be present in the pool)
* System calculates the pool token amount
* @dev emits a LogJoin event
* @param tokenIn - which token we're transferring in
* @param tokenAmountIn - amount of deposit
* @param minPoolAmountOut - minimum of pool tokens to receive
* @return poolAmountOut - amount of pool tokens minted and transferred
*/
function joinswapExternAmountIn(
address tokenIn,
uint tokenAmountIn,
uint minPoolAmountOut
)
external
logs
lock
needsBPool
returns (uint poolAmountOut)
{
require(!rights.canWhitelistLPs || _liquidityProviderWhitelist[msg.sender],
"ERR_NOT_ON_WHITELIST");
// Delegate to library to save space
poolAmountOut = SmartPoolManager.joinswapExternAmountIn(
IConfigurableRightsPool(address(this)),
bPool,
tokenIn,
tokenAmountIn,
minPoolAmountOut
);
emit LogJoin(msg.sender, tokenIn, tokenAmountIn);
_mintPoolShare(poolAmountOut);
_pushPoolShare(msg.sender, poolAmountOut);
_pullUnderlying(tokenIn, msg.sender, tokenAmountIn);
return poolAmountOut;
}
/**
* @notice Join by swapping an external token in (must be present in the pool)
* To receive an exact amount of pool tokens out. System calculates the deposit amount
* @dev emits a LogJoin event
* @param tokenIn - which token we're transferring in (system calculates amount required)
* @param poolAmountOut - amount of pool tokens to be received
* @param maxAmountIn - Maximum asset tokens that can be pulled to pay for the pool tokens
* @return tokenAmountIn - amount of asset tokens transferred in to purchase the pool tokens
*/
function joinswapPoolAmountOut(
address tokenIn,
uint poolAmountOut,
uint maxAmountIn
)
external
logs
lock
needsBPool
returns (uint tokenAmountIn)
{
require(!rights.canWhitelistLPs || _liquidityProviderWhitelist[msg.sender],
"ERR_NOT_ON_WHITELIST");
// Delegate to library to save space
tokenAmountIn = SmartPoolManager.joinswapPoolAmountOut(
IConfigurableRightsPool(address(this)),
bPool,
tokenIn,
poolAmountOut,
maxAmountIn
);
emit LogJoin(msg.sender, tokenIn, tokenAmountIn);
_mintPoolShare(poolAmountOut);
_pushPoolShare(msg.sender, poolAmountOut);
_pullUnderlying(tokenIn, msg.sender, tokenAmountIn);
return tokenAmountIn;
}
/**
* @notice Exit a pool - redeem a specific number of pool tokens for an underlying asset
* Asset must be present in the pool, and will incur an EXIT_FEE (if set to non-zero)
* @dev Emits a LogExit event for the token
* @param tokenOut - which token the caller wants to receive
* @param poolAmountIn - amount of pool tokens to redeem
* @param minAmountOut - minimum asset tokens to receive
* @return tokenAmountOut - amount of asset tokens returned
*/
function exitswapPoolAmountIn(
address tokenOut,
uint poolAmountIn,
uint minAmountOut
)
external
logs
lock
needsBPool
returns (uint tokenAmountOut)
{
// Delegate to library to save space
// Calculates final amountOut, and the fee and final amount in
(uint exitFee,
uint amountOut) = SmartPoolManager.exitswapPoolAmountIn(
IConfigurableRightsPool(address(this)),
bPool,
tokenOut,
poolAmountIn,
minAmountOut
);
tokenAmountOut = amountOut;
uint pAiAfterExitFee = BalancerSafeMath.bsub(poolAmountIn, exitFee);
emit LogExit(msg.sender, tokenOut, tokenAmountOut);
_pullPoolShare(msg.sender, poolAmountIn);
_burnPoolShare(pAiAfterExitFee);
_pushPoolShare(address(bFactory), exitFee);
_pushUnderlying(tokenOut, msg.sender, tokenAmountOut);
return tokenAmountOut;
}
/**
* @notice Exit a pool - redeem pool tokens for a specific amount of underlying assets
* Asset must be present in the pool
* @dev Emits a LogExit event for the token
* @param tokenOut - which token the caller wants to receive
* @param tokenAmountOut - amount of underlying asset tokens to receive
* @param maxPoolAmountIn - maximum pool tokens to be redeemed
* @return poolAmountIn - amount of pool tokens redeemed
*/
function exitswapExternAmountOut(
address tokenOut,
uint tokenAmountOut,
uint maxPoolAmountIn
)
external
logs
lock
needsBPool
returns (uint poolAmountIn)
{
// Delegate to library to save space
// Calculates final amounts in, accounting for the exit fee
(uint exitFee,
uint amountIn) = SmartPoolManager.exitswapExternAmountOut(
IConfigurableRightsPool(address(this)),
bPool,
tokenOut,
tokenAmountOut,
maxPoolAmountIn
);
poolAmountIn = amountIn;
uint pAiAfterExitFee = BalancerSafeMath.bsub(poolAmountIn, exitFee);
emit LogExit(msg.sender, tokenOut, tokenAmountOut);
_pullPoolShare(msg.sender, poolAmountIn);
_burnPoolShare(pAiAfterExitFee);
_pushPoolShare(address(bFactory), exitFee);
_pushUnderlying(tokenOut, msg.sender, tokenAmountOut);
return poolAmountIn;
}
/**
* @notice Add to the whitelist of liquidity providers (if enabled)
* @param provider - address of the liquidity provider
*/
function whitelistLiquidityProvider(address provider)
external
onlyOwner
lock
logs
{
require(rights.canWhitelistLPs, "ERR_CANNOT_WHITELIST_LPS");
require(provider != address(0), "ERR_INVALID_ADDRESS");
_liquidityProviderWhitelist[provider] = true;
}
/**
* @notice Remove from the whitelist of liquidity providers (if enabled)
* @param provider - address of the liquidity provider
*/
function removeWhitelistedLiquidityProvider(address provider)
external
onlyOwner
lock
logs
{
require(rights.canWhitelistLPs, "ERR_CANNOT_WHITELIST_LPS");
require(_liquidityProviderWhitelist[provider], "ERR_LP_NOT_WHITELISTED");
require(provider != address(0), "ERR_INVALID_ADDRESS");
_liquidityProviderWhitelist[provider] = false;
}
/**
* @notice Check if an address is a liquidity provider
* @dev If the whitelist feature is not enabled, anyone can provide liquidity (assuming finalized)
* @return boolean value indicating whether the address can join a pool
*/
function canProvideLiquidity(address provider)
external
view
returns(bool)
{
if (rights.canWhitelistLPs) {
return _liquidityProviderWhitelist[provider];
}
else {
// Probably don't strictly need this (could just return true)
// But the null address can't provide funds
return provider != address(0);
}
}
/**
* @notice Getter for specific permissions
* @dev value of the enum is just the 0-based index in the enumeration
* For instance canPauseSwapping is 0; canChangeWeights is 2
* @return token boolean true if we have the given permission
*/
function hasPermission(RightsManager.Permissions permission)
external
view
virtual
returns(bool)
{
return RightsManager.hasPermission(rights, permission);
}
/**
* @notice Get the denormalized weight of a token
* @dev viewlock to prevent calling if it's being updated
* @return token weight
*/
function getDenormalizedWeight(address token)
external
view
viewlock
needsBPool
returns (uint)
{
return bPool.getDenormalizedWeight(token);
}
/**
* @notice Getter for the RightsManager contract
* @dev Convenience function to get the address of the RightsManager library (so clients can check version)
* @return address of the RightsManager library
*/
function getRightsManagerVersion() external pure returns (address) {
return address(RightsManager);
}
/**
* @notice Getter for the BalancerSafeMath contract
* @dev Convenience function to get the address of the BalancerSafeMath library (so clients can check version)
* @return address of the BalancerSafeMath library
*/
function getBalancerSafeMathVersion() external pure returns (address) {
return address(BalancerSafeMath);
}
/**
* @notice Getter for the SmartPoolManager contract
* @dev Convenience function to get the address of the SmartPoolManager library (so clients can check version)
* @return address of the SmartPoolManager library
*/
function getSmartPoolManagerVersion() external pure returns (address) {
return address(SmartPoolManager);
}
// Public functions
// "Public" versions that can safely be called from SmartPoolManager
// Allows only the contract itself to call them (not the controller or any external account)
function mintPoolShareFromLib(uint amount) public {
require (msg.sender == address(this), "ERR_NOT_CONTROLLER");
_mint(amount);
}
function pushPoolShareFromLib(address to, uint amount) public {
require (msg.sender == address(this), "ERR_NOT_CONTROLLER");
_push(to, amount);
}
function pullPoolShareFromLib(address from, uint amount) public {
require (msg.sender == address(this), "ERR_NOT_CONTROLLER");
_pull(from, amount);
}
function burnPoolShareFromLib(uint amount) public {
require (msg.sender == address(this), "ERR_NOT_CONTROLLER");
_burn(amount);
}
// Internal functions
// Lint wants the function to have a leading underscore too
/* solhint-disable private-vars-leading-underscore */
/**
* @notice Create a new Smart Pool
* @dev Initialize the swap fee to the value provided in the CRP constructor
* Can be changed if the canChangeSwapFee permission is enabled
* @param initialSupply starting token balance
*/
function createPoolInternal(uint initialSupply) internal {
require(address(bPool) == address(0), "ERR_IS_CREATED");
require(initialSupply >= BalancerConstants.MIN_POOL_SUPPLY, "ERR_INIT_SUPPLY_MIN");
require(initialSupply <= BalancerConstants.MAX_POOL_SUPPLY, "ERR_INIT_SUPPLY_MAX");
// If the controller can change the cap, initialize it to the initial supply
// Defensive programming, so that there is no gap between creating the pool
// (initialized to unlimited in the constructor), and setting the cap,
// which they will presumably do if they have this right.
if (rights.canChangeCap) {
bspCap = initialSupply;
}
// There is technically reentrancy here, since we're making external calls and
// then transferring tokens. However, the external calls are all to the underlying BPool
// To the extent possible, modify state variables before calling functions
_mintPoolShare(initialSupply);
_pushPoolShare(msg.sender, initialSupply);
// Deploy new BPool (bFactory and bPool are interfaces; all calls are external)
bPool = bFactory.newBPool();
// EXIT_FEE must always be zero, or ConfigurableRightsPool._pushUnderlying will fail
require(bPool.EXIT_FEE() == 0, "ERR_NONZERO_EXIT_FEE");
require(BalancerConstants.EXIT_FEE == 0, "ERR_NONZERO_EXIT_FEE");
for (uint i = 0; i < _initialTokens.length; i++) {
address t = _initialTokens[i];
uint bal = _initialBalances[i];
uint denorm = gradualUpdate.startWeights[i];
bool returnValue = IERC20(t).transferFrom(msg.sender, address(this), bal);
require(returnValue, "ERR_ERC20_FALSE");
returnValue = IERC20(t).safeApprove(address(bPool), BalancerConstants.MAX_UINT);
require(returnValue, "ERR_ERC20_FALSE");
bPool.bind(t, bal, denorm);
}
while (_initialTokens.length > 0) {
// Modifying state variable after external calls here,
// but not essential, so not dangerous
_initialTokens.pop();
}
// Set fee to the initial value set in the constructor
// Hereafter, read the swapFee from the underlying pool, not the local state variable
bPool.setSwapFee(_initialSwapFee);
bPool.setPublicSwap(true);
// "destroy" the temporary swap fee (like _initialTokens above) in case a subclass tries to use it
_initialSwapFee = 0;
}
/* solhint-enable private-vars-leading-underscore */
// Rebind BPool and pull tokens from address
// bPool is a contract interface; function calls on it are external
function _pullUnderlying(address erc20, address from, uint amount) internal needsBPool {
// Gets current Balance of token i, Bi, and weight of token i, Wi, from BPool.
uint tokenBalance = bPool.getBalance(erc20);
uint tokenWeight = bPool.getDenormalizedWeight(erc20);
bool xfer = IERC20(erc20).transferFrom(from, address(this), amount);
require(xfer, "ERR_ERC20_FALSE");
bPool.rebind(erc20, BalancerSafeMath.badd(tokenBalance, amount), tokenWeight);
}
// Rebind BPool and push tokens to address
// bPool is a contract interface; function calls on it are external
function _pushUnderlying(address erc20, address to, uint amount) internal needsBPool {
// Gets current Balance of token i, Bi, and weight of token i, Wi, from BPool.
uint tokenBalance = bPool.getBalance(erc20);
uint tokenWeight = bPool.getDenormalizedWeight(erc20);
bPool.rebind(erc20, BalancerSafeMath.bsub(tokenBalance, amount), tokenWeight);
bool xfer = IERC20(erc20).transfer(to, amount);
require(xfer, "ERR_ERC20_FALSE");
}
// Wrappers around corresponding core functions
//
function _mint(uint amount) internal override {
super._mint(amount);
require(varTotalSupply <= bspCap, "ERR_CAP_LIMIT_REACHED");
}
function _mintPoolShare(uint amount) internal {
_mint(amount);
}
function _pushPoolShare(address to, uint amount) internal {
_push(to, amount);
}
function _pullPoolShare(address from, uint amount) internal {
_pull(from, amount);
}
function _burnPoolShare(uint amount) internal {
_burn(amount);
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.6.6;
// Core contract; can't be changed. So disable solhint (reminder for v2)
/* solhint-disable private-vars-leading-underscore */
/* solhint-disable reason-string */
/* solhint-disable func-order */
// Test Token
contract TToken {
string private _name;
string private _symbol;
uint8 private _decimals;
address private _owner;
uint internal _totalSupply;
mapping(address => uint) private _balance;
mapping(address => mapping(address=>uint)) private _allowance;
modifier _onlyOwner_() {
require(msg.sender == _owner, "ERR_NOT_OWNER");
_;
}
event Approval(address indexed src, address indexed dst, uint amt);
event Transfer(address indexed src, address indexed dst, uint amt);
// Math
function add(uint a, uint b) internal pure returns (uint c) {
require((c = a + b) >= a);
}
function sub(uint a, uint b) internal pure returns (uint c) {
require((c = a - b) <= a);
}
constructor(
string memory name,
string memory symbol,
uint8 decimals
) public {
_name = name;
_symbol = symbol;
_decimals = decimals;
_owner = msg.sender;
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns(uint8) {
return _decimals;
}
function _move(address src, address dst, uint amt) internal {
require(_balance[src] >= amt, "ERR_INSUFFICIENT_BAL");
_balance[src] = sub(_balance[src], amt);
_balance[dst] = add(_balance[dst], amt);
emit Transfer(src, dst, amt);
}
function _push(address to, uint amt) internal {
_move(address(this), to, amt);
}
function _pull(address from, uint amt) internal {
_move(from, address(this), amt);
}
function _mint(address dst, uint amt) internal {
_balance[dst] = add(_balance[dst], amt);
_totalSupply = add(_totalSupply, amt);
emit Transfer(address(0), dst, amt);
}
function allowance(address src, address dst) external view returns (uint) {
return _allowance[src][dst];
}
function balanceOf(address whom) external view returns (uint) {
return _balance[whom];
}
function totalSupply() public view returns (uint) {
return _totalSupply;
}
function approve(address dst, uint amt) external returns (bool) {
_allowance[msg.sender][dst] = amt;
emit Approval(msg.sender, dst, amt);
return true;
}
function mint(address dst, uint256 amt) public _onlyOwner_ returns (bool) {
_mint(dst, amt);
return true;
}
function burn(uint amt) public returns (bool) {
require(_balance[address(this)] >= amt, "ERR_INSUFFICIENT_BAL");
_balance[address(this)] = sub(_balance[address(this)], amt);
_totalSupply = sub(_totalSupply, amt);
emit Transfer(address(this), address(0), amt);
return true;
}
function transfer(address dst, uint amt) external returns (bool) {
_move(msg.sender, dst, amt);
return true;
}
function transferFrom(address src, address dst, uint amt) external returns (bool) {
require(msg.sender == src || amt <= _allowance[src][msg.sender], "ERR_BTOKEN_BAD_CALLER");
_move(src, dst, amt);
if (msg.sender != src && _allowance[src][msg.sender] != uint256(-1)) {
_allowance[src][msg.sender] = sub(_allowance[src][msg.sender], amt);
emit Approval(msg.sender, dst, _allowance[src][msg.sender]);
}
return true;
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.6.6;
import "./BNum.sol";
contract BMath is BBronze, BConst, BNum {
/**********************************************************************************************
// calcSpotPrice //
// sP = spotPrice //
// bI = tokenBalanceIn ( bI / wI ) 1 //
// bO = tokenBalanceOut sP = ----------- * ---------- //
// wI = tokenWeightIn ( bO / wO ) ( 1 - sF ) //
// wO = tokenWeightOut //
// sF = swapFee //
**********************************************************************************************/
function calcSpotPrice(
uint tokenBalanceIn,
uint tokenWeightIn,
uint tokenBalanceOut,
uint tokenWeightOut,
uint swapFee
)
public pure
returns (uint spotPrice)
{
uint numer = bdiv(tokenBalanceIn, tokenWeightIn);
uint denom = bdiv(tokenBalanceOut, tokenWeightOut);
uint ratio = bdiv(numer, denom);
uint scale = bdiv(BONE, bsub(BONE, swapFee));
return (spotPrice = bmul(ratio, scale));
}
/**********************************************************************************************
// calcOutGivenIn //
// aO = tokenAmountOut //
// bO = tokenBalanceOut //
// bI = tokenBalanceIn / / bI \ (wI / wO) \ //
// aI = tokenAmountIn aO = bO * | 1 - | -------------------------- | ^ | //
// wI = tokenWeightIn \ \ ( bI + ( aI * ( 1 - sF )) / / //
// wO = tokenWeightOut //
// sF = swapFee //
**********************************************************************************************/
function calcOutGivenIn(
uint tokenBalanceIn,
uint tokenWeightIn,
uint tokenBalanceOut,
uint tokenWeightOut,
uint tokenAmountIn,
uint swapFee
)
public pure
returns (uint tokenAmountOut)
{
uint weightRatio = bdiv(tokenWeightIn, tokenWeightOut);
uint adjustedIn = bsub(BONE, swapFee);
adjustedIn = bmul(tokenAmountIn, adjustedIn);
uint y = bdiv(tokenBalanceIn, badd(tokenBalanceIn, adjustedIn));
uint foo = bpow(y, weightRatio);
uint bar = bsub(BONE, foo);
tokenAmountOut = bmul(tokenBalanceOut, bar);
return tokenAmountOut;
}
/**********************************************************************************************
// calcInGivenOut //
// aI = tokenAmountIn //
// bO = tokenBalanceOut / / bO \ (wO / wI) \ //
// bI = tokenBalanceIn bI * | | ------------ | ^ - 1 | //
// aO = tokenAmountOut aI = \ \ ( bO - aO ) / / //
// wI = tokenWeightIn -------------------------------------------- //
// wO = tokenWeightOut ( 1 - sF ) //
// sF = swapFee //
**********************************************************************************************/
function calcInGivenOut(
uint tokenBalanceIn,
uint tokenWeightIn,
uint tokenBalanceOut,
uint tokenWeightOut,
uint tokenAmountOut,
uint swapFee
)
public pure
returns (uint tokenAmountIn)
{
uint weightRatio = bdiv(tokenWeightOut, tokenWeightIn);
uint diff = bsub(tokenBalanceOut, tokenAmountOut);
uint y = bdiv(tokenBalanceOut, diff);
uint foo = bpow(y, weightRatio);
foo = bsub(foo, BONE);
tokenAmountIn = bsub(BONE, swapFee);
tokenAmountIn = bdiv(bmul(tokenBalanceIn, foo), tokenAmountIn);
return tokenAmountIn;
}
/**********************************************************************************************
// calcPoolOutGivenSingleIn //
// pAo = poolAmountOut / \ //
// tAi = tokenAmountIn /// / // wI \ \\ \ wI \ //
// wI = tokenWeightIn //| tAi *| 1 - || 1 - -- | * sF || + tBi \ -- \ //
// tW = totalWeight pAo=|| \ \ \\ tW / // | ^ tW | * pS - pS //
// tBi = tokenBalanceIn \\ ------------------------------------- / / //
// pS = poolSupply \\ tBi / / //
// sF = swapFee \ / //
**********************************************************************************************/
function calcPoolOutGivenSingleIn(
uint tokenBalanceIn,
uint tokenWeightIn,
uint poolSupply,
uint totalWeight,
uint tokenAmountIn,
uint swapFee
)
public pure
returns (uint poolAmountOut)
{
// Charge the trading fee for the proportion of tokenAi
/// which is implicitly traded to the other pool tokens.
// That proportion is (1- weightTokenIn)
// tokenAiAfterFee = tAi * (1 - (1-weightTi) * poolFee);
uint normalizedWeight = bdiv(tokenWeightIn, totalWeight);
uint zaz = bmul(bsub(BONE, normalizedWeight), swapFee);
uint tokenAmountInAfterFee = bmul(tokenAmountIn, bsub(BONE, zaz));
uint newTokenBalanceIn = badd(tokenBalanceIn, tokenAmountInAfterFee);
uint tokenInRatio = bdiv(newTokenBalanceIn, tokenBalanceIn);
// uint newPoolSupply = (ratioTi ^ weightTi) * poolSupply;
uint poolRatio = bpow(tokenInRatio, normalizedWeight);
uint newPoolSupply = bmul(poolRatio, poolSupply);
poolAmountOut = bsub(newPoolSupply, poolSupply);
return poolAmountOut;
}
/**********************************************************************************************
// calcSingleInGivenPoolOut //
// tAi = tokenAmountIn //(pS + pAo)\ / 1 \\ //
// pS = poolSupply || --------- | ^ | --------- || * bI - bI //
// pAo = poolAmountOut \\ pS / \(wI / tW)// //
// bI = balanceIn tAi = -------------------------------------------- //
// wI = weightIn / wI \ //
// tW = totalWeight | 1 - ---- | * sF //
// sF = swapFee \ tW / //
**********************************************************************************************/
function calcSingleInGivenPoolOut(
uint tokenBalanceIn,
uint tokenWeightIn,
uint poolSupply,
uint totalWeight,
uint poolAmountOut,
uint swapFee
)
public pure
returns (uint tokenAmountIn)
{
uint normalizedWeight = bdiv(tokenWeightIn, totalWeight);
uint newPoolSupply = badd(poolSupply, poolAmountOut);
uint poolRatio = bdiv(newPoolSupply, poolSupply);
//uint newBalTi = poolRatio^(1/weightTi) * balTi;
uint boo = bdiv(BONE, normalizedWeight);
uint tokenInRatio = bpow(poolRatio, boo);
uint newTokenBalanceIn = bmul(tokenInRatio, tokenBalanceIn);
uint tokenAmountInAfterFee = bsub(newTokenBalanceIn, tokenBalanceIn);
// Do reverse order of fees charged in joinswap_ExternAmountIn, this way
// ``` pAo == joinswap_ExternAmountIn(Ti, joinswap_PoolAmountOut(pAo, Ti)) ```
//uint tAi = tAiAfterFee / (1 - (1-weightTi) * swapFee) ;
uint zar = bmul(bsub(BONE, normalizedWeight), swapFee);
tokenAmountIn = bdiv(tokenAmountInAfterFee, bsub(BONE, zar));
return tokenAmountIn;
}
/**********************************************************************************************
// calcSingleOutGivenPoolIn //
// tAo = tokenAmountOut / / \\ //
// bO = tokenBalanceOut / // pS - (pAi * (1 - eF)) \ / 1 \ \\ //
// pAi = poolAmountIn | bO - || ----------------------- | ^ | --------- | * b0 || //
// ps = poolSupply \ \\ pS / \(wO / tW)/ // //
// wI = tokenWeightIn tAo = \ \ // //
// tW = totalWeight / / wO \ \ //
// sF = swapFee * | 1 - | 1 - ---- | * sF | //
// eF = exitFee \ \ tW / / //
**********************************************************************************************/
function calcSingleOutGivenPoolIn(
uint tokenBalanceOut,
uint tokenWeightOut,
uint poolSupply,
uint totalWeight,
uint poolAmountIn,
uint swapFee
)
public pure
returns (uint tokenAmountOut)
{
uint normalizedWeight = bdiv(tokenWeightOut, totalWeight);
// charge exit fee on the pool token side
// pAiAfterExitFee = pAi*(1-exitFee)
uint poolAmountInAfterExitFee = bmul(poolAmountIn, bsub(BONE, EXIT_FEE));
uint newPoolSupply = bsub(poolSupply, poolAmountInAfterExitFee);
uint poolRatio = bdiv(newPoolSupply, poolSupply);
// newBalTo = poolRatio^(1/weightTo) * balTo;
uint tokenOutRatio = bpow(poolRatio, bdiv(BONE, normalizedWeight));
uint newTokenBalanceOut = bmul(tokenOutRatio, tokenBalanceOut);
uint tokenAmountOutBeforeSwapFee = bsub(tokenBalanceOut, newTokenBalanceOut);
// charge swap fee on the output token side
//uint tAo = tAoBeforeSwapFee * (1 - (1-weightTo) * swapFee)
uint zaz = bmul(bsub(BONE, normalizedWeight), swapFee);
tokenAmountOut = bmul(tokenAmountOutBeforeSwapFee, bsub(BONE, zaz));
return tokenAmountOut;
}
/**********************************************************************************************
// calcPoolInGivenSingleOut //
// pAi = poolAmountIn // / tAo \\ / wO \ \ //
// bO = tokenBalanceOut // | bO - -------------------------- |\ | ---- | \ //
// tAo = tokenAmountOut pS - || \ 1 - ((1 - (tO / tW)) * sF)/ | ^ \ tW / * pS | //
// ps = poolSupply \\ -----------------------------------/ / //
// wO = tokenWeightOut pAi = \\ bO / / //
// tW = totalWeight ------------------------------------------------------------- //
// sF = swapFee ( 1 - eF ) //
// eF = exitFee //
**********************************************************************************************/
function calcPoolInGivenSingleOut(
uint tokenBalanceOut,
uint tokenWeightOut,
uint poolSupply,
uint totalWeight,
uint tokenAmountOut,
uint swapFee
)
public pure
returns (uint poolAmountIn)
{
// charge swap fee on the output token side
uint normalizedWeight = bdiv(tokenWeightOut, totalWeight);
//uint tAoBeforeSwapFee = tAo / (1 - (1-weightTo) * swapFee) ;
uint zoo = bsub(BONE, normalizedWeight);
uint zar = bmul(zoo, swapFee);
uint tokenAmountOutBeforeSwapFee = bdiv(tokenAmountOut, bsub(BONE, zar));
uint newTokenBalanceOut = bsub(tokenBalanceOut, tokenAmountOutBeforeSwapFee);
uint tokenOutRatio = bdiv(newTokenBalanceOut, tokenBalanceOut);
//uint newPoolSupply = (ratioTo ^ weightTo) * poolSupply;
uint poolRatio = bpow(tokenOutRatio, normalizedWeight);
uint newPoolSupply = bmul(poolRatio, poolSupply);
uint poolAmountInAfterExitFee = bsub(poolSupply, newPoolSupply);
// charge exit fee on the pool token side
// pAi = pAiAfterExitFee/(1-exitFee)
poolAmountIn = bdiv(poolAmountInAfterExitFee, bsub(BONE, EXIT_FEE));
return poolAmountIn;
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.6.6;
import "./BToken.sol";
import "./BMath.sol";
// Core contract; can't be changed. So disable solhint (reminder for v2)
/* solhint-disable func-order */
/* solhint-disable event-name-camelcase */
contract BPool is BBronze, BToken, BMath {
struct Record {
bool bound; // is token bound to pool
uint index; // private
uint denorm; // denormalized weight
uint balance;
}
event LOG_SWAP(
address indexed caller,
address indexed tokenIn,
address indexed tokenOut,
uint256 tokenAmountIn,
uint256 tokenAmountOut
);
event LOG_JOIN(
address indexed caller,
address indexed tokenIn,
uint256 tokenAmountIn
);
event LOG_EXIT(
address indexed caller,
address indexed tokenOut,
uint256 tokenAmountOut
);
event LOG_CALL(
bytes4 indexed sig,
address indexed caller,
bytes data
) anonymous;
modifier _logs_() {
emit LOG_CALL(msg.sig, msg.sender, msg.data);
_;
}
modifier _lock_() {
require(!_mutex, "ERR_REENTRY");
_mutex = true;
_;
_mutex = false;
}
modifier _viewlock_() {
require(!_mutex, "ERR_REENTRY");
_;
}
bool private _mutex;
address private _factory; // BFactory address to push token exitFee to
address private _controller; // has CONTROL role
bool private _publicSwap; // true if PUBLIC can call SWAP functions
// `setSwapFee` and `finalize` require CONTROL
// `finalize` sets `PUBLIC can SWAP`, `PUBLIC can JOIN`
uint private _swapFee;
bool private _finalized;
address[] private _tokens;
mapping(address=>Record) private _records;
uint private _totalWeight;
constructor() public {
_controller = msg.sender;
_factory = msg.sender;
_swapFee = MIN_FEE;
_publicSwap = false;
_finalized = false;
}
function isPublicSwap()
external view
returns (bool)
{
return _publicSwap;
}
function isFinalized()
external view
returns (bool)
{
return _finalized;
}
function isBound(address t)
external view
returns (bool)
{
return _records[t].bound;
}
function getNumTokens()
external view
returns (uint)
{
return _tokens.length;
}
function getCurrentTokens()
external view _viewlock_
returns (address[] memory tokens)
{
return _tokens;
}
function getFinalTokens()
external view
_viewlock_
returns (address[] memory tokens)
{
require(_finalized, "ERR_NOT_FINALIZED");
return _tokens;
}
function getDenormalizedWeight(address token)
external view
_viewlock_
returns (uint)
{
require(_records[token].bound, "ERR_NOT_BOUND");
return _records[token].denorm;
}
function getTotalDenormalizedWeight()
external view
_viewlock_
returns (uint)
{
return _totalWeight;
}
function getNormalizedWeight(address token)
external view
_viewlock_
returns (uint)
{
require(_records[token].bound, "ERR_NOT_BOUND");
uint denorm = _records[token].denorm;
return bdiv(denorm, _totalWeight);
}
function getBalance(address token)
external view
_viewlock_
returns (uint)
{
require(_records[token].bound, "ERR_NOT_BOUND");
return _records[token].balance;
}
function getSwapFee()
external view
_viewlock_
returns (uint)
{
return _swapFee;
}
function getController()
external view
_viewlock_
returns (address)
{
return _controller;
}
function setSwapFee(uint swapFee)
external
_logs_
_lock_
{
require(!_finalized, "ERR_IS_FINALIZED");
require(msg.sender == _controller, "ERR_NOT_CONTROLLER");
require(swapFee >= MIN_FEE, "ERR_MIN_FEE");
require(swapFee <= MAX_FEE, "ERR_MAX_FEE");
_swapFee = swapFee;
}
function setController(address manager)
external
_logs_
_lock_
{
require(msg.sender == _controller, "ERR_NOT_CONTROLLER");
_controller = manager;
}
function setPublicSwap(bool public_)
external
_logs_
_lock_
{
require(!_finalized, "ERR_IS_FINALIZED");
require(msg.sender == _controller, "ERR_NOT_CONTROLLER");
_publicSwap = public_;
}
function finalize()
external
_logs_
_lock_
{
require(msg.sender == _controller, "ERR_NOT_CONTROLLER");
require(!_finalized, "ERR_IS_FINALIZED");
require(_tokens.length >= MIN_BOUND_TOKENS, "ERR_MIN_TOKENS");
_finalized = true;
_publicSwap = true;
_mintPoolShare(INIT_POOL_SUPPLY);
_pushPoolShare(msg.sender, INIT_POOL_SUPPLY);
}
function bind(address token, uint balance, uint denorm)
external
_logs_
// _lock_ Bind does not lock because it jumps to `rebind`, which does
{
require(msg.sender == _controller, "ERR_NOT_CONTROLLER");
require(!_records[token].bound, "ERR_IS_BOUND");
require(!_finalized, "ERR_IS_FINALIZED");
require(_tokens.length < MAX_BOUND_TOKENS, "ERR_MAX_TOKENS");
_records[token] = Record({
bound: true,
index: _tokens.length,
denorm: 0, // balance and denorm will be validated
balance: 0 // and set by `rebind`
});
_tokens.push(token);
rebind(token, balance, denorm);
}
function rebind(address token, uint balance, uint denorm)
public
_logs_
_lock_
{
require(msg.sender == _controller, "ERR_NOT_CONTROLLER");
require(_records[token].bound, "ERR_NOT_BOUND");
require(!_finalized, "ERR_IS_FINALIZED");
require(denorm >= MIN_WEIGHT, "ERR_MIN_WEIGHT");
require(denorm <= MAX_WEIGHT, "ERR_MAX_WEIGHT");
require(balance >= MIN_BALANCE, "ERR_MIN_BALANCE");
// Adjust the denorm and totalWeight
uint oldWeight = _records[token].denorm;
if (denorm > oldWeight) {
_totalWeight = badd(_totalWeight, bsub(denorm, oldWeight));
require(_totalWeight <= MAX_TOTAL_WEIGHT, "ERR_MAX_TOTAL_WEIGHT");
} else if (denorm < oldWeight) {
_totalWeight = bsub(_totalWeight, bsub(oldWeight, denorm));
}
_records[token].denorm = denorm;
// Adjust the balance record and actual token balance
uint oldBalance = _records[token].balance;
_records[token].balance = balance;
if (balance > oldBalance) {
_pullUnderlying(token, msg.sender, bsub(balance, oldBalance));
} else if (balance < oldBalance) {
// In this case liquidity is being withdrawn, so charge EXIT_FEE
uint tokenBalanceWithdrawn = bsub(oldBalance, balance);
uint tokenExitFee = bmul(tokenBalanceWithdrawn, EXIT_FEE);
_pushUnderlying(token, msg.sender, bsub(tokenBalanceWithdrawn, tokenExitFee));
_pushUnderlying(token, _factory, tokenExitFee);
}
}
function unbind(address token)
external
_logs_
_lock_
{
require(msg.sender == _controller, "ERR_NOT_CONTROLLER");
require(_records[token].bound, "ERR_NOT_BOUND");
require(!_finalized, "ERR_IS_FINALIZED");
uint tokenBalance = _records[token].balance;
uint tokenExitFee = bmul(tokenBalance, EXIT_FEE);
_totalWeight = bsub(_totalWeight, _records[token].denorm);
// Swap the token-to-unbind with the last token,
// then delete the last token
uint index = _records[token].index;
uint last = _tokens.length - 1;
_tokens[index] = _tokens[last];
_records[_tokens[index]].index = index;
_tokens.pop();
_records[token] = Record({
bound: false,
index: 0,
denorm: 0,
balance: 0
});
_pushUnderlying(token, msg.sender, bsub(tokenBalance, tokenExitFee));
_pushUnderlying(token, _factory, tokenExitFee);
}
// Absorb any tokens that have been sent to this contract into the pool
function gulp(address token)
external
_logs_
_lock_
{
require(_records[token].bound, "ERR_NOT_BOUND");
_records[token].balance = IERC20(token).balanceOf(address(this));
}
function getSpotPrice(address tokenIn, address tokenOut)
external view
_viewlock_
returns (uint spotPrice)
{
require(_records[tokenIn].bound, "ERR_NOT_BOUND");
require(_records[tokenOut].bound, "ERR_NOT_BOUND");
Record storage inRecord = _records[tokenIn];
Record storage outRecord = _records[tokenOut];
return calcSpotPrice(inRecord.balance, inRecord.denorm, outRecord.balance, outRecord.denorm, _swapFee);
}
function getSpotPriceSansFee(address tokenIn, address tokenOut)
external view
_viewlock_
returns (uint spotPrice)
{
require(_records[tokenIn].bound, "ERR_NOT_BOUND");
require(_records[tokenOut].bound, "ERR_NOT_BOUND");
Record storage inRecord = _records[tokenIn];
Record storage outRecord = _records[tokenOut];
return calcSpotPrice(inRecord.balance, inRecord.denorm, outRecord.balance, outRecord.denorm, 0);
}
function joinPool(uint poolAmountOut, uint[] calldata maxAmountsIn)
external
_logs_
_lock_
{
require(_finalized, "ERR_NOT_FINALIZED");
uint poolTotal = totalSupply();
uint ratio = bdiv(poolAmountOut, poolTotal);
require(ratio != 0, "ERR_MATH_APPROX");
for (uint i = 0; i < _tokens.length; i++) {
address t = _tokens[i];
uint bal = _records[t].balance;
uint tokenAmountIn = bmul(ratio, bal);
require(tokenAmountIn != 0, "ERR_MATH_APPROX");
require(tokenAmountIn <= maxAmountsIn[i], "ERR_LIMIT_IN");
_records[t].balance = badd(_records[t].balance, tokenAmountIn);
emit LOG_JOIN(msg.sender, t, tokenAmountIn);
_pullUnderlying(t, msg.sender, tokenAmountIn);
}
_mintPoolShare(poolAmountOut);
_pushPoolShare(msg.sender, poolAmountOut);
}
function exitPool(uint poolAmountIn, uint[] calldata minAmountsOut)
external
_logs_
_lock_
{
require(_finalized, "ERR_NOT_FINALIZED");
uint poolTotal = totalSupply();
uint exitFee = bmul(poolAmountIn, EXIT_FEE);
uint pAiAfterExitFee = bsub(poolAmountIn, exitFee);
uint ratio = bdiv(pAiAfterExitFee, poolTotal);
require(ratio != 0, "ERR_MATH_APPROX");
_pullPoolShare(msg.sender, poolAmountIn);
_pushPoolShare(_factory, exitFee);
_burnPoolShare(pAiAfterExitFee);
for (uint i = 0; i < _tokens.length; i++) {
address t = _tokens[i];
uint bal = _records[t].balance;
uint tokenAmountOut = bmul(ratio, bal);
require(tokenAmountOut != 0, "ERR_MATH_APPROX");
require(tokenAmountOut >= minAmountsOut[i], "ERR_LIMIT_OUT");
_records[t].balance = bsub(_records[t].balance, tokenAmountOut);
emit LOG_EXIT(msg.sender, t, tokenAmountOut);
_pushUnderlying(t, msg.sender, tokenAmountOut);
}
}
function swapExactAmountIn(
address tokenIn,
uint tokenAmountIn,
address tokenOut,
uint minAmountOut,
uint maxPrice
)
external
_logs_
_lock_
returns (uint tokenAmountOut, uint spotPriceAfter)
{
require(_records[tokenIn].bound, "ERR_NOT_BOUND");
require(_records[tokenOut].bound, "ERR_NOT_BOUND");
require(_publicSwap, "ERR_SWAP_NOT_PUBLIC");
Record storage inRecord = _records[address(tokenIn)];
Record storage outRecord = _records[address(tokenOut)];
require(tokenAmountIn <= bmul(inRecord.balance, MAX_IN_RATIO), "ERR_MAX_IN_RATIO");
uint spotPriceBefore = calcSpotPrice(
inRecord.balance,
inRecord.denorm,
outRecord.balance,
outRecord.denorm,
_swapFee
);
require(spotPriceBefore <= maxPrice, "ERR_BAD_LIMIT_PRICE");
tokenAmountOut = calcOutGivenIn(
inRecord.balance,
inRecord.denorm,
outRecord.balance,
outRecord.denorm,
tokenAmountIn,
_swapFee
);
require(tokenAmountOut >= minAmountOut, "ERR_LIMIT_OUT");
inRecord.balance = badd(inRecord.balance, tokenAmountIn);
outRecord.balance = bsub(outRecord.balance, tokenAmountOut);
spotPriceAfter = calcSpotPrice(
inRecord.balance,
inRecord.denorm,
outRecord.balance,
outRecord.denorm,
_swapFee
);
require(spotPriceAfter >= spotPriceBefore, "ERR_MATH_APPROX");
require(spotPriceAfter <= maxPrice, "ERR_LIMIT_PRICE");
require(spotPriceBefore <= bdiv(tokenAmountIn, tokenAmountOut), "ERR_MATH_APPROX");
emit LOG_SWAP(msg.sender, tokenIn, tokenOut, tokenAmountIn, tokenAmountOut);
_pullUnderlying(tokenIn, msg.sender, tokenAmountIn);
_pushUnderlying(tokenOut, msg.sender, tokenAmountOut);
return (tokenAmountOut, spotPriceAfter);
}
function swapExactAmountOut(
address tokenIn,
uint maxAmountIn,
address tokenOut,
uint tokenAmountOut,
uint maxPrice
)
external
_logs_
_lock_
returns (uint tokenAmountIn, uint spotPriceAfter)
{
require(_records[tokenIn].bound, "ERR_NOT_BOUND");
require(_records[tokenOut].bound, "ERR_NOT_BOUND");
require(_publicSwap, "ERR_SWAP_NOT_PUBLIC");
Record storage inRecord = _records[address(tokenIn)];
Record storage outRecord = _records[address(tokenOut)];
require(tokenAmountOut <= bmul(outRecord.balance, MAX_OUT_RATIO), "ERR_MAX_OUT_RATIO");
uint spotPriceBefore = calcSpotPrice(
inRecord.balance,
inRecord.denorm,
outRecord.balance,
outRecord.denorm,
_swapFee
);
require(spotPriceBefore <= maxPrice, "ERR_BAD_LIMIT_PRICE");
tokenAmountIn = calcInGivenOut(
inRecord.balance,
inRecord.denorm,
outRecord.balance,
outRecord.denorm,
tokenAmountOut,
_swapFee
);
require(tokenAmountIn <= maxAmountIn, "ERR_LIMIT_IN");
inRecord.balance = badd(inRecord.balance, tokenAmountIn);
outRecord.balance = bsub(outRecord.balance, tokenAmountOut);
spotPriceAfter = calcSpotPrice(
inRecord.balance,
inRecord.denorm,
outRecord.balance,
outRecord.denorm,
_swapFee
);
require(spotPriceAfter >= spotPriceBefore, "ERR_MATH_APPROX");
require(spotPriceAfter <= maxPrice, "ERR_LIMIT_PRICE");
require(spotPriceBefore <= bdiv(tokenAmountIn, tokenAmountOut), "ERR_MATH_APPROX");
emit LOG_SWAP(msg.sender, tokenIn, tokenOut, tokenAmountIn, tokenAmountOut);
_pullUnderlying(tokenIn, msg.sender, tokenAmountIn);
_pushUnderlying(tokenOut, msg.sender, tokenAmountOut);
return (tokenAmountIn, spotPriceAfter);
}
function joinswapExternAmountIn(address tokenIn, uint tokenAmountIn, uint minPoolAmountOut)
external
_logs_
_lock_
returns (uint poolAmountOut)
{
require(_finalized, "ERR_NOT_FINALIZED");
require(_records[tokenIn].bound, "ERR_NOT_BOUND");
require(tokenAmountIn <= bmul(_records[tokenIn].balance, MAX_IN_RATIO), "ERR_MAX_IN_RATIO");
Record storage inRecord = _records[tokenIn];
poolAmountOut = calcPoolOutGivenSingleIn(
inRecord.balance,
inRecord.denorm,
_totalSupply,
_totalWeight,
tokenAmountIn,
_swapFee
);
require(poolAmountOut >= minPoolAmountOut, "ERR_LIMIT_OUT");
inRecord.balance = badd(inRecord.balance, tokenAmountIn);
emit LOG_JOIN(msg.sender, tokenIn, tokenAmountIn);
_mintPoolShare(poolAmountOut);
_pushPoolShare(msg.sender, poolAmountOut);
_pullUnderlying(tokenIn, msg.sender, tokenAmountIn);
return poolAmountOut;
}
function joinswapPoolAmountOut(address tokenIn, uint poolAmountOut, uint maxAmountIn)
external
_logs_
_lock_
returns (uint tokenAmountIn)
{
require(_finalized, "ERR_NOT_FINALIZED");
require(_records[tokenIn].bound, "ERR_NOT_BOUND");
Record storage inRecord = _records[tokenIn];
tokenAmountIn = calcSingleInGivenPoolOut(
inRecord.balance,
inRecord.denorm,
_totalSupply,
_totalWeight,
poolAmountOut,
_swapFee
);
require(tokenAmountIn != 0, "ERR_MATH_APPROX");
require(tokenAmountIn <= maxAmountIn, "ERR_LIMIT_IN");
require(tokenAmountIn <= bmul(_records[tokenIn].balance, MAX_IN_RATIO), "ERR_MAX_IN_RATIO");
inRecord.balance = badd(inRecord.balance, tokenAmountIn);
emit LOG_JOIN(msg.sender, tokenIn, tokenAmountIn);
_mintPoolShare(poolAmountOut);
_pushPoolShare(msg.sender, poolAmountOut);
_pullUnderlying(tokenIn, msg.sender, tokenAmountIn);
return tokenAmountIn;
}
function exitswapPoolAmountIn(address tokenOut, uint poolAmountIn, uint minAmountOut)
external
_logs_
_lock_
returns (uint tokenAmountOut)
{
require(_finalized, "ERR_NOT_FINALIZED");
require(_records[tokenOut].bound, "ERR_NOT_BOUND");
Record storage outRecord = _records[tokenOut];
tokenAmountOut = calcSingleOutGivenPoolIn(
outRecord.balance,
outRecord.denorm,
_totalSupply,
_totalWeight,
poolAmountIn,
_swapFee
);
require(tokenAmountOut >= minAmountOut, "ERR_LIMIT_OUT");
require(tokenAmountOut <= bmul(_records[tokenOut].balance, MAX_OUT_RATIO), "ERR_MAX_OUT_RATIO");
outRecord.balance = bsub(outRecord.balance, tokenAmountOut);
uint exitFee = bmul(poolAmountIn, EXIT_FEE);
emit LOG_EXIT(msg.sender, tokenOut, tokenAmountOut);
_pullPoolShare(msg.sender, poolAmountIn);
_burnPoolShare(bsub(poolAmountIn, exitFee));
_pushPoolShare(_factory, exitFee);
_pushUnderlying(tokenOut, msg.sender, tokenAmountOut);
return tokenAmountOut;
}
function exitswapExternAmountOut(address tokenOut, uint tokenAmountOut, uint maxPoolAmountIn)
external
_logs_
_lock_
returns (uint poolAmountIn)
{
require(_finalized, "ERR_NOT_FINALIZED");
require(_records[tokenOut].bound, "ERR_NOT_BOUND");
require(tokenAmountOut <= bmul(_records[tokenOut].balance, MAX_OUT_RATIO), "ERR_MAX_OUT_RATIO");
Record storage outRecord = _records[tokenOut];
poolAmountIn = calcPoolInGivenSingleOut(
outRecord.balance,
outRecord.denorm,
_totalSupply,
_totalWeight,
tokenAmountOut,
_swapFee
);
require(poolAmountIn != 0, "ERR_MATH_APPROX");
require(poolAmountIn <= maxPoolAmountIn, "ERR_LIMIT_IN");
outRecord.balance = bsub(outRecord.balance, tokenAmountOut);
uint exitFee = bmul(poolAmountIn, EXIT_FEE);
emit LOG_EXIT(msg.sender, tokenOut, tokenAmountOut);
_pullPoolShare(msg.sender, poolAmountIn);
_burnPoolShare(bsub(poolAmountIn, exitFee));
_pushPoolShare(_factory, exitFee);
_pushUnderlying(tokenOut, msg.sender, tokenAmountOut);
return poolAmountIn;
}
// ==
// 'Underlying' token-manipulation functions make external calls but are NOT locked
// You must `_lock_` or otherwise ensure reentry-safety
function _pullUnderlying(address erc20, address from, uint amount)
internal
{
bool xfer = IERC20(erc20).transferFrom(from, address(this), amount);
require(xfer, "ERR_ERC20_FALSE");
}
function _pushUnderlying(address erc20, address to, uint amount)
internal
{
bool xfer = IERC20(erc20).transfer(to, amount);
require(xfer, "ERR_ERC20_FALSE");
}
function _pullPoolShare(address from, uint amount)
internal
{
_pull(from, amount);
}
function _pushPoolShare(address to, uint amount)
internal
{
_push(to, amount);
}
function _mintPoolShare(uint amount)
internal
{
_mint(amount);
}
function _burnPoolShare(uint amount)
internal
{
_burn(amount);
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
// Needed to handle structures externally
pragma experimental ABIEncoderV2;
// Imports
import "../IBFactory.sol";
import "../PCToken.sol";
import "../utils/BalancerReentrancyGuard.sol";
import "../utils/BalancerOwnable.sol";
import "../ConfigurableRightsPool.sol";
// Interfaces
// Libraries
import { RightsManager } from "../../libraries/RightsManager.sol";
// Contracts
/**
* @author Ampleforth engineering team & Balancer Labs
*
* Reference:
* https://github.com/balancer-labs/configurable-rights-pool/blob/master/contracts/templates/ElasticSupplyPool.sol
*
* @title Ampl Elastic Configurable Rights Pool.
*
* @dev Extension of Balancer labs' configurable rights pool (smart-pool).
* Amples are a dynamic supply tokens, supply and individual balances change daily by a Rebase operation.
* In constant-function markets, Ampleforth's supply adjustments result in Impermanent Loss (IL)
* to liquidity providers. The AmplElasticCRP is an extension of Balancer Lab's
* ConfigurableRightsPool which mitigates IL induced by supply adjustments.
*
* It accomplishes this by doing the following mechanism:
* The `resyncWeight` method will be invoked atomically after rebase through Ampleforth's orchestrator.
*
* When rebase changes supply, ampl weight is updated to the geometric mean of
* the current ampl weight and the target. Every other token's weight is updated
* proportionally such that relative ratios are same.
*
* Weights: {w_ampl, w_t1 ... w_tn}
*
* Rebase_change: x% (Ample's supply changes by x%, can be positive or negative)
*
* Ample target weight: w_ampl_target = (100+x)/100 * w_ampl
*
* w_ampl_new = sqrt(w_ampl * w_ampl_target) // geometric mean
* for i in tn:
* w_ti_new = (w_ampl_new * w_ti) / w_ampl_target
*
*/
contract ElasticSupplyPool is ConfigurableRightsPool {
using BalancerSafeMath for uint;
// Event declarations
// Have to redeclare in the subclass, to be emitted from this contract
event LogCall(
bytes4 indexed sig,
address indexed caller,
bytes data
) anonymous;
event LogJoin(
address indexed caller,
address indexed tokenIn,
uint tokenAmountIn
);
event LogExit(
address indexed caller,
address indexed tokenOut,
uint tokenAmountOut
);
// Modifiers
// Function declarations
/**
* @notice Construct a new Configurable Rights Pool (wrapper around BPool)
* @param factoryAddress - the BPoolFactory used to create the underlying pool
* @param poolParams - CRP pool parameters
* @param rightsParams - Set of permissions we are assigning to this smart pool
*/
constructor(
address factoryAddress,
ConfigurableRightsPool.PoolParams memory poolParams,
RightsManager.Rights memory rightsParams
)
// solhint-disable-next-line visibility-modifier-order
public
ConfigurableRightsPool(factoryAddress, poolParams, rightsParams)
{
require(rightsParams.canChangeWeights, "ERR_NOT_CONFIGURABLE_WEIGHTS");
}
// External functions
/**
* @notice ElasticSupply pools don't have updateWeightsGradually, so cannot call this
* param initialSupply starting token balance
* param minimumWeightChangeBlockPeriod - Enforce a minimum time between the start and end blocks
* param addTokenTimeLockInBlocks - Enforce a mandatory wait time between updates
* This is also the wait time between committing and applying a new token
*/
function createPool(
uint, // initialSupply
uint, // minimumWeightChangeBlockPeriod
uint // addTokenTimeLockInBlocks
)
external
override
{
revert("ERR_UNSUPPORTED_OPERATION");
}
/**
* @notice Update the weight of an existing token - cannot do this in ElasticSupplyPools
* param token - token to be reweighted
* param newWeight - new weight of the token
*/
function updateWeight(
address, // token
uint // newWeight
)
external
logs
onlyOwner
needsBPool
override
{
revert("ERR_UNSUPPORTED_OPERATION");
}
/**
* @notice Update weights in a predetermined way, between startBlock and endBlock,
* through external calls to pokeWeights -- cannot do this in ElasticSupplyPools
* @dev Makes sure we aren't already in a weight update scheme
* Must call pokeWeights at least once past the end for it to do the final update
* and enable calling this again. (Could make this check for that case, but unwarranted complexity.)
* param newWeights - final weights we want to get to
* param startBlock - when weights should start to change
* param endBlock - when weights will be at their final values
*/
function updateWeightsGradually(
uint[] calldata, // newWeights
uint, // startBlock
uint // endBlock
)
external
logs
onlyOwner
needsBPool
override
{
revert("ERR_UNSUPPORTED_OPERATION");
}
/**
* @notice External function called to make the contract update weights according to plan
* Unsupported in ElasticSupplyPools
*/
function pokeWeights()
external
logs
needsBPool
override
{
revert("ERR_UNSUPPORTED_OPERATION");
}
/**
* @notice Update the weight of a token without changing the price (or transferring tokens)
* @param token The address of the token in the underlying BPool to be weight adjusted.
* @dev Checks if the token's current pool balance has deviated from cached balance,
* if so it adjusts the token's weights proportional to the deviation.
* The underlying BPool enforces bounds on MIN_WEIGHTS=1e18, MAX_WEIGHT=50e18 and TOTAL_WEIGHT=50e18.
* NOTE: The BPool.rebind function CAN REVERT if the updated weights go beyond the enforced bounds.
*/
function resyncWeight(address token)
external
logs
lock
needsBPool
virtual
{
require(gradualUpdate.startBlock == 0, "ERR_NO_UPDATE_DURING_GRADUAL");
require(IBPool(address(bPool)).isBound(token), "ERR_NOT_BOUND");
// get cached balance
uint tokenBalanceBefore = IBPool(address(bPool)).getBalance(token);
// sync balance
IBPool(address(bPool)).gulp(token);
// get new balance
uint tokenBalanceAfter = IBPool(address(bPool)).getBalance(token);
// No-Op
if(tokenBalanceBefore == tokenBalanceAfter) {
return;
}
// current token weight
uint tokenWeightBefore = IBPool(address(bPool)).getDenormalizedWeight(token);
// target token weight = RebaseRatio * previous token weight
uint tokenWeightTarget = BalancerSafeMath.bdiv(
BalancerSafeMath.bmul(tokenWeightBefore, tokenBalanceAfter),
tokenBalanceBefore
);
// new token weight = sqrt(current token weight * target token weight)
uint tokenWeightAfter = BalancerSafeMath.sqrt(
BalancerSafeMath.bdiv(
BalancerSafeMath.bmul(tokenWeightBefore, tokenWeightTarget), 1
)
);
address[] memory tokens = IBPool(address(bPool)).getCurrentTokens();
for(uint i=0; i<tokens.length; i++){
if(tokens[i] == token) {
// adjust weight
IBPool(address(bPool)).rebind(token, tokenBalanceAfter, tokenWeightAfter);
}
else {
uint otherWeightBefore = IBPool(address(bPool)).getDenormalizedWeight(tokens[i]);
uint otherBalance = bPool.getBalance(tokens[i]);
// other token weight = (new token weight * other token weight before) / target token weight
uint otherWeightAfter = BalancerSafeMath.bdiv(
BalancerSafeMath.bmul(tokenWeightAfter, otherWeightBefore), tokenWeightTarget
);
// adjust weight
IBPool(address(bPool)).rebind(tokens[i], otherBalance, otherWeightAfter);
}
}
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
contract Migrations {
address public owner;
uint public lastCompletedMigration;
constructor() public {
owner = msg.sender;
}
modifier restricted() {
if (msg.sender == owner) _;
}
/**
* @notice set lastCompletedMigration variable
* @param completed - id of the desired migration level
*/
function setCompleted(uint completed) external restricted {
lastCompletedMigration = completed;
}
}
/
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.12;
/**
* @author Balancer Labs
* @title Put all the constants in one place
*/
library BalancerConstants {
// State variables (must be constant in a library)
// B "ONE" - all math is in the "realm" of 10 ** 18;
// where numeric 1 = 10 ** 18
uint public constant BONE = 10**18;
uint public constant MIN_WEIGHT = BONE;
uint public constant MAX_WEIGHT = BONE * 50;
uint public constant MAX_TOTAL_WEIGHT = BONE * 50;
uint public constant MIN_BALANCE = BONE / 10**6;
uint public constant MAX_BALANCE = BONE * 10**12;
uint public constant MIN_POOL_SUPPLY = BONE * 100;
uint public constant MAX_POOL_SUPPLY = BONE * 10**9;
uint public constant MIN_FEE = BONE / 10**6;
uint public constant MAX_FEE = BONE / 10;
// EXIT_FEE must always be zero, or ConfigurableRightsPool._pushUnderlying will fail
uint public constant EXIT_FEE = 0;
uint public constant MAX_IN_RATIO = BONE / 2;
uint public constant MAX_OUT_RATIO = (BONE / 3) + 1 wei;
// Must match BConst.MIN_BOUND_TOKENS and BConst.MAX_BOUND_TOKENS
uint public constant MIN_ASSET_LIMIT = 2;
uint public constant MAX_ASSET_LIMIT = 8;
uint public constant MAX_UINT = uint(-1);
}
Compiler Settings
{"remappings":[],"optimizer":{"runs":200,"enabled":true},"metadata":{"useLiteralContent":true,"bytecodeHash":"ipfs"},"libraries":{},"evmVersion":"istanbul","compilationTarget":{"libraries/SmartPoolManager.sol":"SmartPoolManager"}}
Contract ABI
[{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"exitFee","internalType":"uint256"},{"type":"uint256","name":"pAiAfterExitFee","internalType":"uint256"},{"type":"uint256[]","name":"actualAmountsOut","internalType":"uint256[]"}],"name":"exitPool","inputs":[{"type":"IConfigurableRightsPool","name":"self","internalType":"contract IConfigurableRightsPool"},{"type":"IBPool","name":"bPool","internalType":"contract IBPool"},{"type":"uint256","name":"poolAmountIn","internalType":"uint256"},{"type":"uint256[]","name":"minAmountsOut","internalType":"uint256[]"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"exitFee","internalType":"uint256"},{"type":"uint256","name":"poolAmountIn","internalType":"uint256"}],"name":"exitswapExternAmountOut","inputs":[{"type":"IConfigurableRightsPool","name":"self","internalType":"contract IConfigurableRightsPool"},{"type":"IBPool","name":"bPool","internalType":"contract IBPool"},{"type":"address","name":"tokenOut","internalType":"address"},{"type":"uint256","name":"tokenAmountOut","internalType":"uint256"},{"type":"uint256","name":"maxPoolAmountIn","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"exitFee","internalType":"uint256"},{"type":"uint256","name":"tokenAmountOut","internalType":"uint256"}],"name":"exitswapPoolAmountIn","inputs":[{"type":"IConfigurableRightsPool","name":"self","internalType":"contract IConfigurableRightsPool"},{"type":"IBPool","name":"bPool","internalType":"contract IBPool"},{"type":"address","name":"tokenOut","internalType":"address"},{"type":"uint256","name":"poolAmountIn","internalType":"uint256"},{"type":"uint256","name":"minAmountOut","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256[]","name":"actualAmountsIn","internalType":"uint256[]"}],"name":"joinPool","inputs":[{"type":"IConfigurableRightsPool","name":"self","internalType":"contract IConfigurableRightsPool"},{"type":"IBPool","name":"bPool","internalType":"contract IBPool"},{"type":"uint256","name":"poolAmountOut","internalType":"uint256"},{"type":"uint256[]","name":"maxAmountsIn","internalType":"uint256[]"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"poolAmountOut","internalType":"uint256"}],"name":"joinswapExternAmountIn","inputs":[{"type":"IConfigurableRightsPool","name":"self","internalType":"contract IConfigurableRightsPool"},{"type":"IBPool","name":"bPool","internalType":"contract IBPool"},{"type":"address","name":"tokenIn","internalType":"address"},{"type":"uint256","name":"tokenAmountIn","internalType":"uint256"},{"type":"uint256","name":"minPoolAmountOut","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"tokenAmountIn","internalType":"uint256"}],"name":"joinswapPoolAmountOut","inputs":[{"type":"IConfigurableRightsPool","name":"self","internalType":"contract IConfigurableRightsPool"},{"type":"IBPool","name":"bPool","internalType":"contract IBPool"},{"type":"address","name":"tokenIn","internalType":"address"},{"type":"uint256","name":"poolAmountOut","internalType":"uint256"},{"type":"uint256","name":"maxAmountIn","internalType":"uint256"}]}]
Contract Creation Code
0x613f71610026600b82828239805160001a60731461001957fe5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600436106100f45760003560e01c806382495b4511610096578063d505a94e11610070578063d505a94e14610247578063df90de0b1461025a578063efa587741461026d578063f544aa1c1461028d576100f4565b806382495b45146101e7578063a1925f1d14610207578063b489ec1914610227576100f4565b80635471c3e4116100d25780635471c3e41461015b5780636a6dc6e714610186578063724a2d53146101a657806377d44349146101c7576100f4565b80630970e47f146100f957806313b8bf241461011b5780631c1af1361461013b575b600080fd5b81801561010557600080fd5b5061011961011436600461372d565b6102ad565b005b81801561012757600080fd5b506101196101363660046136b8565b610754565b81801561014757600080fd5b506101196101563660046134fa565b610a46565b61016e610169366004613821565b610a52565b60405161017d93929190613eb8565b60405180910390f35b6101996101943660046137c7565b610d2b565b60405161017d9190613ea1565b6101b96101b43660046137c7565b611180565b60405161017d929190613eaa565b8180156101d357600080fd5b506101196101e2366004613532565b6115e3565b8180156101f357600080fd5b5061011961020236600461368d565b611624565b61021a610215366004613821565b6119a6565b60405161017d91906139b5565b81801561023357600080fd5b5061011961024236600461363d565b611c61565b6101b96102553660046137c7565b611e5a565b6101996102683660046137c7565b612236565b81801561027957600080fd5b50610119610288366004613892565b612607565b81801561029957600080fd5b506101196102a8366004613777565b612979565b6000836001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b1580156102e857600080fd5b505afa1580156102fc573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061032091906138d7565b905060006104226103ac83866001600160a01b031663948d8ce6876040518263ffffffff1660e01b81526004016103579190613929565b60206040518083038186803b15801561036f57600080fd5b505afa158015610383573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103a791906138d7565b613032565b856001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b1580156103e557600080fd5b505afa1580156103f9573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061041d91906138d7565b6130b1565b90506000846001600160a01b031663f8b2cb4f856040518263ffffffff1660e01b81526004016104529190613929565b60206040518083038186803b15801561046a57600080fd5b505afa15801561047e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906104a291906138d7565b60405163cf5e7bd360e01b81529091506001600160a01b0386169063cf5e7bd3906104d1908790600401613929565b600060405180830381600087803b1580156104eb57600080fd5b505af11580156104ff573d6000803e3d6000fd5b505050506000846001600160a01b031663a9059cbb886001600160a01b0316633018205f6040518163ffffffff1660e01b815260040160206040518083038186803b15801561054d57600080fd5b505afa158015610561573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906105859190613516565b846040518363ffffffff1660e01b81526004016105a3929190613961565b602060405180830381600087803b1580156105bd57600080fd5b505af11580156105d1573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906105f5919061361d565b90508061061d5760405162461bcd60e51b815260040161061490613e23565b60405180910390fd5b866001600160a01b03166355c32a23886001600160a01b0316633018205f6040518163ffffffff1660e01b815260040160206040518083038186803b15801561066557600080fd5b505afa158015610679573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061069d9190613516565b856040518363ffffffff1660e01b81526004016106bb929190613961565b600060405180830381600087803b1580156106d557600080fd5b505af11580156106e9573d6000803e3d6000fd5b505060405163011075bb60e51b81526001600160a01b038a16925063220eb7609150610719908690600401613ea1565b600060405180830381600087803b15801561073357600080fd5b505af1158015610747573d6000803e3d6000fd5b5050505050505050505050565b8143106107735760405162461bcd60e51b8152600401610614906139f5565b8243111561078357438655610787565b8286555b80610796838860000154613142565b10156107b45760405162461bcd60e51b815260040161061490613a2c565b6060876001600160a01b031663cc77828d6040518163ffffffff1660e01b815260040160006040518083038186803b1580156107ef57600080fd5b505afa158015610803573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f1916820160405261082b9190810190613572565b8051909150851461084e5760405162461bcd60e51b815260040161061490613a61565b6000815167ffffffffffffffff8111801561086857600080fd5b50604051908082528060200260200182016040528015610892578160200160208202803683370190505b5080516108a99160028b0191602090910190613412565b5060005b82518110156109fb576802b5e3af16b18800008888838181106108cc57fe5b9050602002013511156108f15760405162461bcd60e51b815260040161061490613d08565b670de0b6b3a764000088888381811061090657fe5b90506020020135101561092b5760405162461bcd60e51b815260040161061490613c60565b6109478289898481811061093b57fe5b9050602002013561317b565b9150896001600160a01b031663948d8ce684838151811061096457fe5b60200260200101516040518263ffffffff1660e01b81526004016109889190613929565b60206040518083038186803b1580156109a057600080fd5b505afa1580156109b4573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906109d891906138d7565b8960020182815481106109e757fe5b6000918252602090912001556001016108ad565b506802b5e3af16b1880000811115610a255760405162461bcd60e51b815260040161061490613b39565b60018801849055610a3a60038901888861345d565b50505050505050505050565b610a4f816131a7565b50565b600080606080876001600160a01b031663cc77828d6040518163ffffffff1660e01b815260040160006040518083038186803b158015610a9157600080fd5b505afa158015610aa5573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610acd9190810190613572565b80519091508514610af05760405162461bcd60e51b815260040161061490613b67565b6000896001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b158015610b2b57600080fd5b505afa158015610b3f573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610b6391906138d7565b9050610b70886000613032565b9450610b7c8886613142565b93506000610b8f8561041d84600161317b565b905080610bae5760405162461bcd60e51b815260040161061490613ae8565b825167ffffffffffffffff81118015610bc657600080fd5b50604051908082528060200260200182016040528015610bf0578160200160208202803683370190505b50935060005b8351811015610d1c576000848281518110610c0d57fe5b6020026020010151905060008c6001600160a01b031663f8b2cb4f836040518263ffffffff1660e01b8152600401610c459190613929565b60206040518083038186803b158015610c5d57600080fd5b505afa158015610c71573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610c9591906138d7565b90506000610ca8856103a7846001613142565b905080610cc75760405162461bcd60e51b815260040161061490613ae8565b8b8b85818110610cd357fe5b90506020020135811015610cf95760405162461bcd60e51b815260040161061490613b95565b80888581518110610d0657fe5b6020908102919091010152505050600101610bf6565b50505050955095509592505050565b604051630bcded8960e21b81526000906001600160a01b03861690632f37b62490610d5a908790600401613929565b60206040518083038186803b158015610d7257600080fd5b505afa158015610d86573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610daa919061361d565b610dc65760405162461bcd60e51b815260040161061490613cb9565b60405163f8b2cb4f60e01b81526001600160a01b03861690635c1bbaf790829063f8b2cb4f90610dfa908990600401613929565b60206040518083038186803b158015610e1257600080fd5b505afa158015610e26573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610e4a91906138d7565b604051634a46c67360e11b81526001600160a01b0389169063948d8ce690610e76908a90600401613929565b60206040518083038186803b158015610e8e57600080fd5b505afa158015610ea2573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610ec691906138d7565b896001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b158015610eff57600080fd5b505afa158015610f13573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610f3791906138d7565b896001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b158015610f7057600080fd5b505afa158015610f84573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610fa891906138d7565b888b6001600160a01b031663d4cadf686040518163ffffffff1660e01b815260040160206040518083038186803b158015610fe257600080fd5b505afa158015610ff6573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061101a91906138d7565b6040518763ffffffff1660e01b815260040161103b96959493929190613ed7565b60206040518083038186803b15801561105357600080fd5b505afa158015611067573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061108b91906138d7565b9050806110aa5760405162461bcd60e51b815260040161061490613ae8565b818111156110ca5760405162461bcd60e51b815260040161061490613c3a565b60405163f8b2cb4f60e01b8152611158906001600160a01b0387169063f8b2cb4f906110fa908890600401613929565b60206040518083038186803b15801561111257600080fd5b505afa158015611126573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061114a91906138d7565b6706f05b59d3b20000613032565b8111156111775760405162461bcd60e51b815260040161061490613abe565b95945050505050565b600080856001600160a01b0316632f37b624866040518263ffffffff1660e01b81526004016111af9190613929565b60206040518083038186803b1580156111c757600080fd5b505afa1580156111db573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111ff919061361d565b61121b5760405162461bcd60e51b815260040161061490613cb9565b60405163f8b2cb4f60e01b81526112a9906001600160a01b0388169063f8b2cb4f9061124b908990600401613929565b60206040518083038186803b15801561126357600080fd5b505afa158015611277573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061129b91906138d7565b6704a03ce68d215556613032565b8411156112c85760405162461bcd60e51b815260040161061490613c8e565b60405163f8b2cb4f60e01b81526001600160a01b038716906382f652ad90829063f8b2cb4f906112fc908a90600401613929565b60206040518083038186803b15801561131457600080fd5b505afa158015611328573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061134c91906138d7565b604051634a46c67360e11b81526001600160a01b038a169063948d8ce690611378908b90600401613929565b60206040518083038186803b15801561139057600080fd5b505afa1580156113a4573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906113c891906138d7565b8a6001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b15801561140157600080fd5b505afa158015611415573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061143991906138d7565b8a6001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b15801561147257600080fd5b505afa158015611486573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906114aa91906138d7565b898c6001600160a01b031663d4cadf686040518163ffffffff1660e01b815260040160206040518083038186803b1580156114e457600080fd5b505afa1580156114f8573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061151c91906138d7565b6040518763ffffffff1660e01b815260040161153d96959493929190613ed7565b60206040518083038186803b15801561155557600080fd5b505afa158015611569573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061158d91906138d7565b9050806115ac5760405162461bcd60e51b815260040161061490613ae8565b828111156115cc5760405162461bcd60e51b815260040161061490613c3a565b6115d7816000613032565b91509550959350505050565b60005b8181101561161f576116178383838181106115fd57fe5b905060200201602081019061161291906134fa565b6131a7565b6001016115e6565b505050565b805461162f576119a2565b80544310156116505760405162461bcd60e51b815260040161061490613be5565b600081600101544311156116695750600181015461166c565b50435b600061168083600101548460000154613142565b90506000611692838560000154613142565b905060008060006060886001600160a01b031663cc77828d6040518163ffffffff1660e01b815260040160006040518083038186803b1580156116d457600080fd5b505afa1580156116e8573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526117109190810190613572565b905060005b81518110156119895788600301818154811061172d57fe5b906000526020600020015489600201828154811061174757fe5b9060005260206000200154146119815788600201818154811061176657fe5b906000526020600020015489600301828154811061178057fe5b9060005260206000200154101561180e576117cd8960020182815481106117a357fe5b90600052602060002001548a60030183815481106117bd57fe5b9060005260206000200154613142565b94506117d985886130b1565b93506118078960020182815481106117ed57fe5b90600052602060002001546118028887613032565b613142565b9250611877565b61183a89600301828154811061182057fe5b90600052602060002001548a60020183815481106117bd57fe5b945061184685886130b1565b935061187489600201828154811061185a57fe5b906000526020600020015461186f8887613032565b61317b565b92505b60008a6001600160a01b031663f8b2cb4f84848151811061189457fe5b60200260200101516040518263ffffffff1660e01b81526004016118b89190613929565b60206040518083038186803b1580156118d057600080fd5b505afa1580156118e4573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061190891906138d7565b90508a6001600160a01b0316633fdddaa284848151811061192557fe5b602002602001015183876040518463ffffffff1660e01b815260040161194d93929190613994565b600060405180830381600087803b15801561196757600080fd5b505af115801561197b573d6000803e3d6000fd5b50505050505b600101611715565b508760010154431061199a57600088555b505050505050505b5050565b606080856001600160a01b031663cc77828d6040518163ffffffff1660e01b815260040160006040518083038186803b1580156119e257600080fd5b505afa1580156119f6573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052611a1e9190810190613572565b80519091508314611a415760405162461bcd60e51b815260040161061490613b67565b6000876001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b158015611a7c57600080fd5b505afa158015611a90573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611ab491906138d7565b90506000611ac78761041d846001613142565b905080611ae65760405162461bcd60e51b815260040161061490613ae8565b825167ffffffffffffffff81118015611afe57600080fd5b50604051908082528060200260200182016040528015611b28578160200160208202803683370190505b50935060005b8351811015611c54576000848281518110611b4557fe5b6020026020010151905060008a6001600160a01b031663f8b2cb4f836040518263ffffffff1660e01b8152600401611b7d9190613929565b60206040518083038186803b158015611b9557600080fd5b505afa158015611ba9573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611bcd91906138d7565b90506000611be0856103a784600161317b565b905080611bff5760405162461bcd60e51b815260040161061490613ae8565b898985818110611c0b57fe5b90506020020135811115611c315760405162461bcd60e51b815260040161061490613c3a565b80888581518110611c3e57fe5b6020908102919091010152505050600101611b2e565b5050505095945050505050565b604051630bcded8960e21b81526001600160a01b03861690632f37b62490611c8d908790600401613929565b60206040518083038186803b158015611ca557600080fd5b505afa158015611cb9573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611cdd919061361d565b15611cfa5760405162461bcd60e51b815260040161061490613a98565b6802b5e3af16b1880000821115611d235760405162461bcd60e51b815260040161061490613d08565b670de0b6b3a7640000821015611d4b5760405162461bcd60e51b815260040161061490613c60565b670de0b6b3a7640000603202611dd1866001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b158015611d9357600080fd5b505afa158015611da7573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611dcb91906138d7565b8461317b565b1115611def5760405162461bcd60e51b815260040161061490613b39565b64e8d4a51000831015611e145760405162461bcd60e51b815260040161061490613df4565b805460038201939093556002810191909155436001820155600160a01b6001600160a01b03199092166001600160a01b03939093169290921760ff60a01b191617905550565b600080856001600160a01b0316632f37b624866040518263ffffffff1660e01b8152600401611e899190613929565b60206040518083038186803b158015611ea157600080fd5b505afa158015611eb5573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611ed9919061361d565b611ef55760405162461bcd60e51b815260040161061490613cb9565b60405163f8b2cb4f60e01b81526001600160a01b03871690638929801290829063f8b2cb4f90611f29908a90600401613929565b60206040518083038186803b158015611f4157600080fd5b505afa158015611f55573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611f7991906138d7565b604051634a46c67360e11b81526001600160a01b038a169063948d8ce690611fa5908b90600401613929565b60206040518083038186803b158015611fbd57600080fd5b505afa158015611fd1573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611ff591906138d7565b8a6001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b15801561202e57600080fd5b505afa158015612042573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061206691906138d7565b8a6001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b15801561209f57600080fd5b505afa1580156120b3573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906120d791906138d7565b898c6001600160a01b031663d4cadf686040518163ffffffff1660e01b815260040160206040518083038186803b15801561211157600080fd5b505afa158015612125573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061214991906138d7565b6040518763ffffffff1660e01b815260040161216a96959493929190613ed7565b60206040518083038186803b15801561218257600080fd5b505afa158015612196573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906121ba91906138d7565b9050828110156121dc5760405162461bcd60e51b815260040161061490613b95565b60405163f8b2cb4f60e01b815261220c906001600160a01b0388169063f8b2cb4f9061124b908990600401613929565b81111561222b5760405162461bcd60e51b815260040161061490613c8e565b6115d7846000613032565b604051630bcded8960e21b81526000906001600160a01b03861690632f37b62490612265908790600401613929565b60206040518083038186803b15801561227d57600080fd5b505afa158015612291573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906122b5919061361d565b6122d15760405162461bcd60e51b815260040161061490613cb9565b60405163f8b2cb4f60e01b8152612301906001600160a01b0387169063f8b2cb4f906110fa908890600401613929565b8311156123205760405162461bcd60e51b815260040161061490613abe565b60405163f8b2cb4f60e01b81526001600160a01b03861690638656b65390829063f8b2cb4f90612354908990600401613929565b60206040518083038186803b15801561236c57600080fd5b505afa158015612380573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906123a491906138d7565b604051634a46c67360e11b81526001600160a01b0389169063948d8ce6906123d0908a90600401613929565b60206040518083038186803b1580156123e857600080fd5b505afa1580156123fc573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061242091906138d7565b896001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b15801561245957600080fd5b505afa15801561246d573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061249191906138d7565b896001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b1580156124ca57600080fd5b505afa1580156124de573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061250291906138d7565b888b6001600160a01b031663d4cadf686040518163ffffffff1660e01b815260040160206040518083038186803b15801561253c57600080fd5b505afa158015612550573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061257491906138d7565b6040518763ffffffff1660e01b815260040161259596959493929190613ed7565b60206040518083038186803b1580156125ad57600080fd5b505afa1580156125c1573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906125e591906138d7565b9050818110156111775760405162461bcd60e51b815260040161061490613b95565b8054600160a01b900460ff1661262f5760405162461bcd60e51b8152600401610614906139c8565b8161263e438360010154613142565b101561265c5760405162461bcd60e51b815260040161061490613d36565b6000846001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b15801561269757600080fd5b505afa1580156126ab573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906126cf91906138d7565b9050600061271d6126e4838560020154613032565b866001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b1580156103e557600080fd5b835460ff60a01b198116855560408051633018205f60e01b815290519293506000926001600160a01b03928316926323b872dd92908b1691633018205f91600480820192602092909190829003018186803b15801561277b57600080fd5b505afa15801561278f573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906127b39190613516565b8987600301546040518463ffffffff1660e01b81526004016127d79392919061393d565b602060405180830381600087803b1580156127f157600080fd5b505af1158015612805573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612829919061361d565b9050806128485760405162461bcd60e51b815260040161061490613e23565b8354612860906001600160a01b031687600019613249565b90508061287f5760405162461bcd60e51b815260040161061490613e23565b835460038501546002860154604051631c9c3ca760e31b81526001600160a01b03808b169463e4e1e538946128bb949190921692600401613994565b600060405180830381600087803b1580156128d557600080fd5b505af11580156128e9573d6000803e3d6000fd5b50506040516325d2bc4160e11b81526001600160a01b038a169250634ba578829150612919908590600401613ea1565b600060405180830381600087803b15801561293357600080fd5b505af1158015612947573d6000803e3d6000fd5b5050604051630257733360e21b81526001600160a01b038a16925063095dcccc91506107199033908690600401613961565b670de0b6b3a76400008110156129a15760405162461bcd60e51b815260040161061490613ce0565b6802b5e3af16b18800008111156129ca5760405162461bcd60e51b815260040161061490613b11565b604051634a46c67360e11b81526000906001600160a01b0385169063948d8ce6906129f9908690600401613929565b60206040518083038186803b158015612a1157600080fd5b505afa158015612a25573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612a4991906138d7565b905081811415612a59575061302c565b60405163f8b2cb4f60e01b81526000906001600160a01b0386169063f8b2cb4f90612a88908790600401613929565b60206040518083038186803b158015612aa057600080fd5b505afa158015612ab4573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612ad891906138d7565b90506000866001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b158015612b1557600080fd5b505afa158015612b29573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612b4d91906138d7565b90506000866001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b158015612b8a57600080fd5b505afa158015612b9e573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612bc291906138d7565b905060008060008087891015612df757612bdc888a613142565b9150612bec866103a784886130b1565b9350612bfc876103a7848b6130b1565b9250612c088784613142565b905064e8d4a51000811015612c2f5760405162461bcd60e51b815260040161061490613bbc565b604051631feeed5160e11b81526001600160a01b038c1690633fdddaa290612c5f908d9085908e90600401613994565b600060405180830381600087803b158015612c7957600080fd5b505af1158015612c8d573d6000803e3d6000fd5b505060405163a9059cbb60e01b8152600092506001600160a01b038d16915063a9059cbb90612cc29033908890600401613961565b602060405180830381600087803b158015612cdc57600080fd5b505af1158015612cf0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612d14919061361d565b905080612d335760405162461bcd60e51b815260040161061490613e23565b6040516355c32a2360e01b81526001600160a01b038e16906355c32a2390612d619033908990600401613961565b600060405180830381600087803b158015612d7b57600080fd5b505af1158015612d8f573d6000803e3d6000fd5b505050508c6001600160a01b031663220eb760866040518263ffffffff1660e01b8152600401612dbf9190613ea1565b600060405180830381600087803b158015612dd957600080fd5b505af1158015612ded573d6000803e3d6000fd5b5050505050613023565b612e018989613142565b91506802b5e3af16b1880000612e17868461317b565b1115612e355760405162461bcd60e51b815260040161061490613b39565b612e43866103a784886130b1565b9350612e53876103a7848b6130b1565b925060008a6001600160a01b03166323b872dd3330876040518463ffffffff1660e01b8152600401612e879392919061393d565b602060405180830381600087803b158015612ea157600080fd5b505af1158015612eb5573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612ed9919061361d565b905080612ef85760405162461bcd60e51b815260040161061490613e23565b8b6001600160a01b0316633fdddaa28c612f128b8861317b565b8d6040518463ffffffff1660e01b8152600401612f3193929190613994565b600060405180830381600087803b158015612f4b57600080fd5b505af1158015612f5f573d6000803e3d6000fd5b505050508c6001600160a01b0316634ba57882866040518263ffffffff1660e01b8152600401612f8f9190613ea1565b600060405180830381600087803b158015612fa957600080fd5b505af1158015612fbd573d6000803e3d6000fd5b505050508c6001600160a01b031663095dcccc33876040518363ffffffff1660e01b8152600401612fef929190613961565b600060405180830381600087803b15801561300957600080fd5b505af115801561301d573d6000803e3d6000fd5b50505050505b50505050505050505b50505050565b600082613041575060006130ab565b8282028284828161304e57fe5b041461306c5760405162461bcd60e51b815260040161061490613d6d565b6706f05b59d3b200008101818110156130975760405162461bcd60e51b815260040161061490613d6d565b6000670de0b6b3a7640000825b0493505050505b92915050565b6000816130d05760405162461bcd60e51b815260040161061490613dce565b826130dd575060006130ab565b670de0b6b3a7640000838102908482816130f357fe5b04146131115760405162461bcd60e51b815260040161061490613c10565b600283048101818110156131375760405162461bcd60e51b815260040161061490613c10565b60008482816130a457fe5b600080600061315185856133ed565b9150915080156131735760405162461bcd60e51b815260040161061490613e4c565b509392505050565b6000828201838110156131a05760405162461bcd60e51b815260040161061490613e77565b9392505050565b60405163a9059cbb60e01b81526000906001600160a01b0383169063a9059cbb906131d89033908590600401613961565b602060405180830381600087803b1580156131f257600080fd5b505af1158015613206573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061322a919061361d565b9050806119a25760405162461bcd60e51b815260040161061490613d97565b600080846001600160a01b031663dd62ed3e30866040518363ffffffff1660e01b815260040161327a92919061397a565b60206040518083038186803b15801561329257600080fd5b505afa1580156132a6573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906132ca91906138d7565b9050828114156132de5760019150506131a0565b801561336d5760405163095ea7b360e01b81526001600160a01b0386169063095ea7b390613313908790600090600401613961565b602060405180830381600087803b15801561332d57600080fd5b505af1158015613341573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190613365919061361d565b9150506131a0565b60405163095ea7b360e01b81526001600160a01b0386169063095ea7b39061339b9087908790600401613961565b602060405180830381600087803b1580156133b557600080fd5b505af11580156133c9573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611177919061361d565b600080838311613403575050808203600061340b565b505081810360015b9250929050565b82805482825590600052602060002090810192821561344d579160200282015b8281111561344d578251825591602001919060010190613432565b50613459929150613498565b5090565b82805482825590600052602060002090810192821561344d579160200282015b8281111561344d57823582559160200191906001019061347d565b5b808211156134595760008155600101613499565b80516130ab81613f26565b60008083601f8401126134c9578182fd5b50813567ffffffffffffffff8111156134e0578182fd5b602083019150836020808302850101111561340b57600080fd5b60006020828403121561350b578081fd5b81356131a081613f26565b600060208284031215613527578081fd5b81516131a081613f26565b60008060208385031215613544578081fd5b823567ffffffffffffffff81111561355a578182fd5b613566858286016134b8565b90969095509350505050565b60006020808385031215613584578182fd5b825167ffffffffffffffff8082111561359b578384fd5b818501915085601f8301126135ae578384fd5b8151818111156135bc578485fd5b83810291506135cc848301613eff565b8181528481019084860184860187018a10156135e6578788fd5b8795505b83861015613610576135fc8a826134ad565b8352600195909501949186019186016135ea565b5098975050505050505050565b60006020828403121561362e578081fd5b815180151581146131a0578182fd5b600080600080600060a08688031215613654578081fd5b853561365f81613f26565b9450602086013561366f81613f26565b94979496505050506040830135926060810135926080909101359150565b6000806040838503121561369f578182fd5b82356136aa81613f26565b946020939093013593505050565b600080600080600080600060c0888a0312156136d2578182fd5b87356136dd81613f26565b965060208801359550604088013567ffffffffffffffff8111156136ff578283fd5b61370b8a828b016134b8565b989b979a50986060810135976080820135975060a09091013595509350505050565b600080600060608486031215613741578283fd5b833561374c81613f26565b9250602084013561375c81613f26565b9150604084013561376c81613f26565b809150509250925092565b6000806000806080858703121561378c578384fd5b843561379781613f26565b935060208501356137a781613f26565b925060408501356137b781613f26565b9396929550929360600135925050565b600080600080600060a086880312156137de578283fd5b85356137e981613f26565b945060208601356137f981613f26565b9350604086013561380981613f26565b94979396509394606081013594506080013592915050565b600080600080600060808688031215613838578283fd5b853561384381613f26565b9450602086013561385381613f26565b935060408601359250606086013567ffffffffffffffff811115613875578182fd5b613881888289016134b8565b969995985093965092949392505050565b600080600080608085870312156138a7578182fd5b84356138b281613f26565b935060208501356138c281613f26565b93969395505050506040820135916060013590565b6000602082840312156138e8578081fd5b5051919050565b6000815180845260208085019450808401835b8381101561391e57815187529582019590820190600101613902565b509495945050505050565b6001600160a01b0391909116815260200190565b6001600160a01b039384168152919092166020820152604081019190915260600190565b6001600160a01b03929092168252602082015260400190565b6001600160a01b0392831681529116602082015260400190565b6001600160a01b039390931683526020830191909152604082015260600190565b6000602082526131a060208301846138ef565b60208082526013908201527211549497d393d7d513d2d15397d0d3d3535255606a1b604082015260600190565b6020808252601e908201527f4552525f4752414455414c5f5550444154455f54494d455f54524156454c0000604082015260600190565b6020808252818101527f4552525f5745494748545f4348414e47455f54494d455f42454c4f575f4d494e604082015260600190565b6020808252601a908201527f4552525f53544152545f574549474854535f4d49534d41544348000000000000604082015260600190565b6020808252600c908201526b11549497d254d7d093d5539160a21b604082015260600190565b60208082526010908201526f4552525f4d41585f494e5f524154494f60801b604082015260600190565b6020808252600f908201526e08aa4a4be9a82a890be82a0a0a49eb608b1b604082015260600190565b6020808252600e908201526d11549497d3505617d5d15251d21560921b604082015260600190565b60208082526014908201527311549497d3505617d513d5105317d5d15251d21560621b604082015260600190565b60208082526014908201527308aa4a4be829a9eaa9ca8a6be9a92a69a82a886960631b604082015260600190565b6020808252600d908201526c11549497d31253525517d3d555609a1b604082015260600190565b6020808252600f908201526e4552525f4d494e5f42414c414e434560881b604082015260600190565b60208082526011908201527011549497d0d0539517d413d2d157d65155607a1b604082015260600190565b60208082526010908201526f11549497d1125597d25395115493905360821b604082015260600190565b6020808252600c908201526b22a9292fa624a6a4aa2fa4a760a11b604082015260600190565b60208082526014908201527322a9292faba2a4a3a42a2fa122a627abafa6a4a760611b604082015260600190565b6020808252601190820152704552525f4d41585f4f55545f524154494f60781b604082015260600190565b6020808252600d908201526c11549497d393d517d093d55391609a1b604082015260600190565b6020808252600e908201526d11549497d3525397d5d15251d21560921b604082015260600190565b60208082526014908201527308aa4a4beae8a928e90a8be82849eac8abe9a82b60631b604082015260600190565b6020808252601b908201527f4552525f54494d454c4f434b5f5354494c4c5f434f554e54494e470000000000604082015260600190565b60208082526010908201526f4552525f4d554c5f4f564552464c4f5760801b604082015260600190565b60208082526017908201527f4552525f4e4f4e434f4e464f524d494e475f544f4b454e000000000000000000604082015260600190565b6020808252600c908201526b4552525f4449565f5a45524f60a01b604082015260600190565b60208082526015908201527422a9292fa120a620a721a2afa122a627abafa6a4a760591b604082015260600190565b6020808252600f908201526e4552525f45524332305f46414c534560881b604082015260600190565b6020808252601190820152704552525f5355425f554e444552464c4f5760781b604082015260600190565b60208082526010908201526f4552525f4144445f4f564552464c4f5760801b604082015260600190565b90815260200190565b918252602082015260400190565b60008482528360208301526060604083015261117760608301846138ef565b958652602086019490945260408501929092526060840152608083015260a082015260c00190565b60405181810167ffffffffffffffff81118282101715613f1e57600080fd5b604052919050565b6001600160a01b0381168114610a4f57600080fdfea264697066735822122038b46142c2f828dddc9c74991de440998e196b4ad87b9d6cebaf9c78120f1f3564736f6c634300060c0033
Deployed ByteCode
0x73a3f9145cb0b50d907930840bb2dcff4146df8ab430146080604052600436106100f45760003560e01c806382495b4511610096578063d505a94e11610070578063d505a94e14610247578063df90de0b1461025a578063efa587741461026d578063f544aa1c1461028d576100f4565b806382495b45146101e7578063a1925f1d14610207578063b489ec1914610227576100f4565b80635471c3e4116100d25780635471c3e41461015b5780636a6dc6e714610186578063724a2d53146101a657806377d44349146101c7576100f4565b80630970e47f146100f957806313b8bf241461011b5780631c1af1361461013b575b600080fd5b81801561010557600080fd5b5061011961011436600461372d565b6102ad565b005b81801561012757600080fd5b506101196101363660046136b8565b610754565b81801561014757600080fd5b506101196101563660046134fa565b610a46565b61016e610169366004613821565b610a52565b60405161017d93929190613eb8565b60405180910390f35b6101996101943660046137c7565b610d2b565b60405161017d9190613ea1565b6101b96101b43660046137c7565b611180565b60405161017d929190613eaa565b8180156101d357600080fd5b506101196101e2366004613532565b6115e3565b8180156101f357600080fd5b5061011961020236600461368d565b611624565b61021a610215366004613821565b6119a6565b60405161017d91906139b5565b81801561023357600080fd5b5061011961024236600461363d565b611c61565b6101b96102553660046137c7565b611e5a565b6101996102683660046137c7565b612236565b81801561027957600080fd5b50610119610288366004613892565b612607565b81801561029957600080fd5b506101196102a8366004613777565b612979565b6000836001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b1580156102e857600080fd5b505afa1580156102fc573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061032091906138d7565b905060006104226103ac83866001600160a01b031663948d8ce6876040518263ffffffff1660e01b81526004016103579190613929565b60206040518083038186803b15801561036f57600080fd5b505afa158015610383573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103a791906138d7565b613032565b856001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b1580156103e557600080fd5b505afa1580156103f9573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061041d91906138d7565b6130b1565b90506000846001600160a01b031663f8b2cb4f856040518263ffffffff1660e01b81526004016104529190613929565b60206040518083038186803b15801561046a57600080fd5b505afa15801561047e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906104a291906138d7565b60405163cf5e7bd360e01b81529091506001600160a01b0386169063cf5e7bd3906104d1908790600401613929565b600060405180830381600087803b1580156104eb57600080fd5b505af11580156104ff573d6000803e3d6000fd5b505050506000846001600160a01b031663a9059cbb886001600160a01b0316633018205f6040518163ffffffff1660e01b815260040160206040518083038186803b15801561054d57600080fd5b505afa158015610561573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906105859190613516565b846040518363ffffffff1660e01b81526004016105a3929190613961565b602060405180830381600087803b1580156105bd57600080fd5b505af11580156105d1573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906105f5919061361d565b90508061061d5760405162461bcd60e51b815260040161061490613e23565b60405180910390fd5b866001600160a01b03166355c32a23886001600160a01b0316633018205f6040518163ffffffff1660e01b815260040160206040518083038186803b15801561066557600080fd5b505afa158015610679573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061069d9190613516565b856040518363ffffffff1660e01b81526004016106bb929190613961565b600060405180830381600087803b1580156106d557600080fd5b505af11580156106e9573d6000803e3d6000fd5b505060405163011075bb60e51b81526001600160a01b038a16925063220eb7609150610719908690600401613ea1565b600060405180830381600087803b15801561073357600080fd5b505af1158015610747573d6000803e3d6000fd5b5050505050505050505050565b8143106107735760405162461bcd60e51b8152600401610614906139f5565b8243111561078357438655610787565b8286555b80610796838860000154613142565b10156107b45760405162461bcd60e51b815260040161061490613a2c565b6060876001600160a01b031663cc77828d6040518163ffffffff1660e01b815260040160006040518083038186803b1580156107ef57600080fd5b505afa158015610803573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f1916820160405261082b9190810190613572565b8051909150851461084e5760405162461bcd60e51b815260040161061490613a61565b6000815167ffffffffffffffff8111801561086857600080fd5b50604051908082528060200260200182016040528015610892578160200160208202803683370190505b5080516108a99160028b0191602090910190613412565b5060005b82518110156109fb576802b5e3af16b18800008888838181106108cc57fe5b9050602002013511156108f15760405162461bcd60e51b815260040161061490613d08565b670de0b6b3a764000088888381811061090657fe5b90506020020135101561092b5760405162461bcd60e51b815260040161061490613c60565b6109478289898481811061093b57fe5b9050602002013561317b565b9150896001600160a01b031663948d8ce684838151811061096457fe5b60200260200101516040518263ffffffff1660e01b81526004016109889190613929565b60206040518083038186803b1580156109a057600080fd5b505afa1580156109b4573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906109d891906138d7565b8960020182815481106109e757fe5b6000918252602090912001556001016108ad565b506802b5e3af16b1880000811115610a255760405162461bcd60e51b815260040161061490613b39565b60018801849055610a3a60038901888861345d565b50505050505050505050565b610a4f816131a7565b50565b600080606080876001600160a01b031663cc77828d6040518163ffffffff1660e01b815260040160006040518083038186803b158015610a9157600080fd5b505afa158015610aa5573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610acd9190810190613572565b80519091508514610af05760405162461bcd60e51b815260040161061490613b67565b6000896001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b158015610b2b57600080fd5b505afa158015610b3f573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610b6391906138d7565b9050610b70886000613032565b9450610b7c8886613142565b93506000610b8f8561041d84600161317b565b905080610bae5760405162461bcd60e51b815260040161061490613ae8565b825167ffffffffffffffff81118015610bc657600080fd5b50604051908082528060200260200182016040528015610bf0578160200160208202803683370190505b50935060005b8351811015610d1c576000848281518110610c0d57fe5b6020026020010151905060008c6001600160a01b031663f8b2cb4f836040518263ffffffff1660e01b8152600401610c459190613929565b60206040518083038186803b158015610c5d57600080fd5b505afa158015610c71573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610c9591906138d7565b90506000610ca8856103a7846001613142565b905080610cc75760405162461bcd60e51b815260040161061490613ae8565b8b8b85818110610cd357fe5b90506020020135811015610cf95760405162461bcd60e51b815260040161061490613b95565b80888581518110610d0657fe5b6020908102919091010152505050600101610bf6565b50505050955095509592505050565b604051630bcded8960e21b81526000906001600160a01b03861690632f37b62490610d5a908790600401613929565b60206040518083038186803b158015610d7257600080fd5b505afa158015610d86573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610daa919061361d565b610dc65760405162461bcd60e51b815260040161061490613cb9565b60405163f8b2cb4f60e01b81526001600160a01b03861690635c1bbaf790829063f8b2cb4f90610dfa908990600401613929565b60206040518083038186803b158015610e1257600080fd5b505afa158015610e26573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610e4a91906138d7565b604051634a46c67360e11b81526001600160a01b0389169063948d8ce690610e76908a90600401613929565b60206040518083038186803b158015610e8e57600080fd5b505afa158015610ea2573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610ec691906138d7565b896001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b158015610eff57600080fd5b505afa158015610f13573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610f3791906138d7565b896001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b158015610f7057600080fd5b505afa158015610f84573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610fa891906138d7565b888b6001600160a01b031663d4cadf686040518163ffffffff1660e01b815260040160206040518083038186803b158015610fe257600080fd5b505afa158015610ff6573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061101a91906138d7565b6040518763ffffffff1660e01b815260040161103b96959493929190613ed7565b60206040518083038186803b15801561105357600080fd5b505afa158015611067573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061108b91906138d7565b9050806110aa5760405162461bcd60e51b815260040161061490613ae8565b818111156110ca5760405162461bcd60e51b815260040161061490613c3a565b60405163f8b2cb4f60e01b8152611158906001600160a01b0387169063f8b2cb4f906110fa908890600401613929565b60206040518083038186803b15801561111257600080fd5b505afa158015611126573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061114a91906138d7565b6706f05b59d3b20000613032565b8111156111775760405162461bcd60e51b815260040161061490613abe565b95945050505050565b600080856001600160a01b0316632f37b624866040518263ffffffff1660e01b81526004016111af9190613929565b60206040518083038186803b1580156111c757600080fd5b505afa1580156111db573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111ff919061361d565b61121b5760405162461bcd60e51b815260040161061490613cb9565b60405163f8b2cb4f60e01b81526112a9906001600160a01b0388169063f8b2cb4f9061124b908990600401613929565b60206040518083038186803b15801561126357600080fd5b505afa158015611277573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061129b91906138d7565b6704a03ce68d215556613032565b8411156112c85760405162461bcd60e51b815260040161061490613c8e565b60405163f8b2cb4f60e01b81526001600160a01b038716906382f652ad90829063f8b2cb4f906112fc908a90600401613929565b60206040518083038186803b15801561131457600080fd5b505afa158015611328573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061134c91906138d7565b604051634a46c67360e11b81526001600160a01b038a169063948d8ce690611378908b90600401613929565b60206040518083038186803b15801561139057600080fd5b505afa1580156113a4573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906113c891906138d7565b8a6001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b15801561140157600080fd5b505afa158015611415573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061143991906138d7565b8a6001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b15801561147257600080fd5b505afa158015611486573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906114aa91906138d7565b898c6001600160a01b031663d4cadf686040518163ffffffff1660e01b815260040160206040518083038186803b1580156114e457600080fd5b505afa1580156114f8573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061151c91906138d7565b6040518763ffffffff1660e01b815260040161153d96959493929190613ed7565b60206040518083038186803b15801561155557600080fd5b505afa158015611569573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061158d91906138d7565b9050806115ac5760405162461bcd60e51b815260040161061490613ae8565b828111156115cc5760405162461bcd60e51b815260040161061490613c3a565b6115d7816000613032565b91509550959350505050565b60005b8181101561161f576116178383838181106115fd57fe5b905060200201602081019061161291906134fa565b6131a7565b6001016115e6565b505050565b805461162f576119a2565b80544310156116505760405162461bcd60e51b815260040161061490613be5565b600081600101544311156116695750600181015461166c565b50435b600061168083600101548460000154613142565b90506000611692838560000154613142565b905060008060006060886001600160a01b031663cc77828d6040518163ffffffff1660e01b815260040160006040518083038186803b1580156116d457600080fd5b505afa1580156116e8573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526117109190810190613572565b905060005b81518110156119895788600301818154811061172d57fe5b906000526020600020015489600201828154811061174757fe5b9060005260206000200154146119815788600201818154811061176657fe5b906000526020600020015489600301828154811061178057fe5b9060005260206000200154101561180e576117cd8960020182815481106117a357fe5b90600052602060002001548a60030183815481106117bd57fe5b9060005260206000200154613142565b94506117d985886130b1565b93506118078960020182815481106117ed57fe5b90600052602060002001546118028887613032565b613142565b9250611877565b61183a89600301828154811061182057fe5b90600052602060002001548a60020183815481106117bd57fe5b945061184685886130b1565b935061187489600201828154811061185a57fe5b906000526020600020015461186f8887613032565b61317b565b92505b60008a6001600160a01b031663f8b2cb4f84848151811061189457fe5b60200260200101516040518263ffffffff1660e01b81526004016118b89190613929565b60206040518083038186803b1580156118d057600080fd5b505afa1580156118e4573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061190891906138d7565b90508a6001600160a01b0316633fdddaa284848151811061192557fe5b602002602001015183876040518463ffffffff1660e01b815260040161194d93929190613994565b600060405180830381600087803b15801561196757600080fd5b505af115801561197b573d6000803e3d6000fd5b50505050505b600101611715565b508760010154431061199a57600088555b505050505050505b5050565b606080856001600160a01b031663cc77828d6040518163ffffffff1660e01b815260040160006040518083038186803b1580156119e257600080fd5b505afa1580156119f6573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052611a1e9190810190613572565b80519091508314611a415760405162461bcd60e51b815260040161061490613b67565b6000876001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b158015611a7c57600080fd5b505afa158015611a90573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611ab491906138d7565b90506000611ac78761041d846001613142565b905080611ae65760405162461bcd60e51b815260040161061490613ae8565b825167ffffffffffffffff81118015611afe57600080fd5b50604051908082528060200260200182016040528015611b28578160200160208202803683370190505b50935060005b8351811015611c54576000848281518110611b4557fe5b6020026020010151905060008a6001600160a01b031663f8b2cb4f836040518263ffffffff1660e01b8152600401611b7d9190613929565b60206040518083038186803b158015611b9557600080fd5b505afa158015611ba9573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611bcd91906138d7565b90506000611be0856103a784600161317b565b905080611bff5760405162461bcd60e51b815260040161061490613ae8565b898985818110611c0b57fe5b90506020020135811115611c315760405162461bcd60e51b815260040161061490613c3a565b80888581518110611c3e57fe5b6020908102919091010152505050600101611b2e565b5050505095945050505050565b604051630bcded8960e21b81526001600160a01b03861690632f37b62490611c8d908790600401613929565b60206040518083038186803b158015611ca557600080fd5b505afa158015611cb9573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611cdd919061361d565b15611cfa5760405162461bcd60e51b815260040161061490613a98565b6802b5e3af16b1880000821115611d235760405162461bcd60e51b815260040161061490613d08565b670de0b6b3a7640000821015611d4b5760405162461bcd60e51b815260040161061490613c60565b670de0b6b3a7640000603202611dd1866001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b158015611d9357600080fd5b505afa158015611da7573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611dcb91906138d7565b8461317b565b1115611def5760405162461bcd60e51b815260040161061490613b39565b64e8d4a51000831015611e145760405162461bcd60e51b815260040161061490613df4565b805460038201939093556002810191909155436001820155600160a01b6001600160a01b03199092166001600160a01b03939093169290921760ff60a01b191617905550565b600080856001600160a01b0316632f37b624866040518263ffffffff1660e01b8152600401611e899190613929565b60206040518083038186803b158015611ea157600080fd5b505afa158015611eb5573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611ed9919061361d565b611ef55760405162461bcd60e51b815260040161061490613cb9565b60405163f8b2cb4f60e01b81526001600160a01b03871690638929801290829063f8b2cb4f90611f29908a90600401613929565b60206040518083038186803b158015611f4157600080fd5b505afa158015611f55573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611f7991906138d7565b604051634a46c67360e11b81526001600160a01b038a169063948d8ce690611fa5908b90600401613929565b60206040518083038186803b158015611fbd57600080fd5b505afa158015611fd1573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611ff591906138d7565b8a6001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b15801561202e57600080fd5b505afa158015612042573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061206691906138d7565b8a6001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b15801561209f57600080fd5b505afa1580156120b3573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906120d791906138d7565b898c6001600160a01b031663d4cadf686040518163ffffffff1660e01b815260040160206040518083038186803b15801561211157600080fd5b505afa158015612125573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061214991906138d7565b6040518763ffffffff1660e01b815260040161216a96959493929190613ed7565b60206040518083038186803b15801561218257600080fd5b505afa158015612196573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906121ba91906138d7565b9050828110156121dc5760405162461bcd60e51b815260040161061490613b95565b60405163f8b2cb4f60e01b815261220c906001600160a01b0388169063f8b2cb4f9061124b908990600401613929565b81111561222b5760405162461bcd60e51b815260040161061490613c8e565b6115d7846000613032565b604051630bcded8960e21b81526000906001600160a01b03861690632f37b62490612265908790600401613929565b60206040518083038186803b15801561227d57600080fd5b505afa158015612291573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906122b5919061361d565b6122d15760405162461bcd60e51b815260040161061490613cb9565b60405163f8b2cb4f60e01b8152612301906001600160a01b0387169063f8b2cb4f906110fa908890600401613929565b8311156123205760405162461bcd60e51b815260040161061490613abe565b60405163f8b2cb4f60e01b81526001600160a01b03861690638656b65390829063f8b2cb4f90612354908990600401613929565b60206040518083038186803b15801561236c57600080fd5b505afa158015612380573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906123a491906138d7565b604051634a46c67360e11b81526001600160a01b0389169063948d8ce6906123d0908a90600401613929565b60206040518083038186803b1580156123e857600080fd5b505afa1580156123fc573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061242091906138d7565b896001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b15801561245957600080fd5b505afa15801561246d573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061249191906138d7565b896001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b1580156124ca57600080fd5b505afa1580156124de573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061250291906138d7565b888b6001600160a01b031663d4cadf686040518163ffffffff1660e01b815260040160206040518083038186803b15801561253c57600080fd5b505afa158015612550573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061257491906138d7565b6040518763ffffffff1660e01b815260040161259596959493929190613ed7565b60206040518083038186803b1580156125ad57600080fd5b505afa1580156125c1573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906125e591906138d7565b9050818110156111775760405162461bcd60e51b815260040161061490613b95565b8054600160a01b900460ff1661262f5760405162461bcd60e51b8152600401610614906139c8565b8161263e438360010154613142565b101561265c5760405162461bcd60e51b815260040161061490613d36565b6000846001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b15801561269757600080fd5b505afa1580156126ab573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906126cf91906138d7565b9050600061271d6126e4838560020154613032565b866001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b1580156103e557600080fd5b835460ff60a01b198116855560408051633018205f60e01b815290519293506000926001600160a01b03928316926323b872dd92908b1691633018205f91600480820192602092909190829003018186803b15801561277b57600080fd5b505afa15801561278f573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906127b39190613516565b8987600301546040518463ffffffff1660e01b81526004016127d79392919061393d565b602060405180830381600087803b1580156127f157600080fd5b505af1158015612805573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612829919061361d565b9050806128485760405162461bcd60e51b815260040161061490613e23565b8354612860906001600160a01b031687600019613249565b90508061287f5760405162461bcd60e51b815260040161061490613e23565b835460038501546002860154604051631c9c3ca760e31b81526001600160a01b03808b169463e4e1e538946128bb949190921692600401613994565b600060405180830381600087803b1580156128d557600080fd5b505af11580156128e9573d6000803e3d6000fd5b50506040516325d2bc4160e11b81526001600160a01b038a169250634ba578829150612919908590600401613ea1565b600060405180830381600087803b15801561293357600080fd5b505af1158015612947573d6000803e3d6000fd5b5050604051630257733360e21b81526001600160a01b038a16925063095dcccc91506107199033908690600401613961565b670de0b6b3a76400008110156129a15760405162461bcd60e51b815260040161061490613ce0565b6802b5e3af16b18800008111156129ca5760405162461bcd60e51b815260040161061490613b11565b604051634a46c67360e11b81526000906001600160a01b0385169063948d8ce6906129f9908690600401613929565b60206040518083038186803b158015612a1157600080fd5b505afa158015612a25573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612a4991906138d7565b905081811415612a59575061302c565b60405163f8b2cb4f60e01b81526000906001600160a01b0386169063f8b2cb4f90612a88908790600401613929565b60206040518083038186803b158015612aa057600080fd5b505afa158015612ab4573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612ad891906138d7565b90506000866001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b158015612b1557600080fd5b505afa158015612b29573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612b4d91906138d7565b90506000866001600160a01b031663936c34776040518163ffffffff1660e01b815260040160206040518083038186803b158015612b8a57600080fd5b505afa158015612b9e573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612bc291906138d7565b905060008060008087891015612df757612bdc888a613142565b9150612bec866103a784886130b1565b9350612bfc876103a7848b6130b1565b9250612c088784613142565b905064e8d4a51000811015612c2f5760405162461bcd60e51b815260040161061490613bbc565b604051631feeed5160e11b81526001600160a01b038c1690633fdddaa290612c5f908d9085908e90600401613994565b600060405180830381600087803b158015612c7957600080fd5b505af1158015612c8d573d6000803e3d6000fd5b505060405163a9059cbb60e01b8152600092506001600160a01b038d16915063a9059cbb90612cc29033908890600401613961565b602060405180830381600087803b158015612cdc57600080fd5b505af1158015612cf0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612d14919061361d565b905080612d335760405162461bcd60e51b815260040161061490613e23565b6040516355c32a2360e01b81526001600160a01b038e16906355c32a2390612d619033908990600401613961565b600060405180830381600087803b158015612d7b57600080fd5b505af1158015612d8f573d6000803e3d6000fd5b505050508c6001600160a01b031663220eb760866040518263ffffffff1660e01b8152600401612dbf9190613ea1565b600060405180830381600087803b158015612dd957600080fd5b505af1158015612ded573d6000803e3d6000fd5b5050505050613023565b612e018989613142565b91506802b5e3af16b1880000612e17868461317b565b1115612e355760405162461bcd60e51b815260040161061490613b39565b612e43866103a784886130b1565b9350612e53876103a7848b6130b1565b925060008a6001600160a01b03166323b872dd3330876040518463ffffffff1660e01b8152600401612e879392919061393d565b602060405180830381600087803b158015612ea157600080fd5b505af1158015612eb5573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612ed9919061361d565b905080612ef85760405162461bcd60e51b815260040161061490613e23565b8b6001600160a01b0316633fdddaa28c612f128b8861317b565b8d6040518463ffffffff1660e01b8152600401612f3193929190613994565b600060405180830381600087803b158015612f4b57600080fd5b505af1158015612f5f573d6000803e3d6000fd5b505050508c6001600160a01b0316634ba57882866040518263ffffffff1660e01b8152600401612f8f9190613ea1565b600060405180830381600087803b158015612fa957600080fd5b505af1158015612fbd573d6000803e3d6000fd5b505050508c6001600160a01b031663095dcccc33876040518363ffffffff1660e01b8152600401612fef929190613961565b600060405180830381600087803b15801561300957600080fd5b505af115801561301d573d6000803e3d6000fd5b50505050505b50505050505050505b50505050565b600082613041575060006130ab565b8282028284828161304e57fe5b041461306c5760405162461bcd60e51b815260040161061490613d6d565b6706f05b59d3b200008101818110156130975760405162461bcd60e51b815260040161061490613d6d565b6000670de0b6b3a7640000825b0493505050505b92915050565b6000816130d05760405162461bcd60e51b815260040161061490613dce565b826130dd575060006130ab565b670de0b6b3a7640000838102908482816130f357fe5b04146131115760405162461bcd60e51b815260040161061490613c10565b600283048101818110156131375760405162461bcd60e51b815260040161061490613c10565b60008482816130a457fe5b600080600061315185856133ed565b9150915080156131735760405162461bcd60e51b815260040161061490613e4c565b509392505050565b6000828201838110156131a05760405162461bcd60e51b815260040161061490613e77565b9392505050565b60405163a9059cbb60e01b81526000906001600160a01b0383169063a9059cbb906131d89033908590600401613961565b602060405180830381600087803b1580156131f257600080fd5b505af1158015613206573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061322a919061361d565b9050806119a25760405162461bcd60e51b815260040161061490613d97565b600080846001600160a01b031663dd62ed3e30866040518363ffffffff1660e01b815260040161327a92919061397a565b60206040518083038186803b15801561329257600080fd5b505afa1580156132a6573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906132ca91906138d7565b9050828114156132de5760019150506131a0565b801561336d5760405163095ea7b360e01b81526001600160a01b0386169063095ea7b390613313908790600090600401613961565b602060405180830381600087803b15801561332d57600080fd5b505af1158015613341573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190613365919061361d565b9150506131a0565b60405163095ea7b360e01b81526001600160a01b0386169063095ea7b39061339b9087908790600401613961565b602060405180830381600087803b1580156133b557600080fd5b505af11580156133c9573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611177919061361d565b600080838311613403575050808203600061340b565b505081810360015b9250929050565b82805482825590600052602060002090810192821561344d579160200282015b8281111561344d578251825591602001919060010190613432565b50613459929150613498565b5090565b82805482825590600052602060002090810192821561344d579160200282015b8281111561344d57823582559160200191906001019061347d565b5b808211156134595760008155600101613499565b80516130ab81613f26565b60008083601f8401126134c9578182fd5b50813567ffffffffffffffff8111156134e0578182fd5b602083019150836020808302850101111561340b57600080fd5b60006020828403121561350b578081fd5b81356131a081613f26565b600060208284031215613527578081fd5b81516131a081613f26565b60008060208385031215613544578081fd5b823567ffffffffffffffff81111561355a578182fd5b613566858286016134b8565b90969095509350505050565b60006020808385031215613584578182fd5b825167ffffffffffffffff8082111561359b578384fd5b818501915085601f8301126135ae578384fd5b8151818111156135bc578485fd5b83810291506135cc848301613eff565b8181528481019084860184860187018a10156135e6578788fd5b8795505b83861015613610576135fc8a826134ad565b8352600195909501949186019186016135ea565b5098975050505050505050565b60006020828403121561362e578081fd5b815180151581146131a0578182fd5b600080600080600060a08688031215613654578081fd5b853561365f81613f26565b9450602086013561366f81613f26565b94979496505050506040830135926060810135926080909101359150565b6000806040838503121561369f578182fd5b82356136aa81613f26565b946020939093013593505050565b600080600080600080600060c0888a0312156136d2578182fd5b87356136dd81613f26565b965060208801359550604088013567ffffffffffffffff8111156136ff578283fd5b61370b8a828b016134b8565b989b979a50986060810135976080820135975060a09091013595509350505050565b600080600060608486031215613741578283fd5b833561374c81613f26565b9250602084013561375c81613f26565b9150604084013561376c81613f26565b809150509250925092565b6000806000806080858703121561378c578384fd5b843561379781613f26565b935060208501356137a781613f26565b925060408501356137b781613f26565b9396929550929360600135925050565b600080600080600060a086880312156137de578283fd5b85356137e981613f26565b945060208601356137f981613f26565b9350604086013561380981613f26565b94979396509394606081013594506080013592915050565b600080600080600060808688031215613838578283fd5b853561384381613f26565b9450602086013561385381613f26565b935060408601359250606086013567ffffffffffffffff811115613875578182fd5b613881888289016134b8565b969995985093965092949392505050565b600080600080608085870312156138a7578182fd5b84356138b281613f26565b935060208501356138c281613f26565b93969395505050506040820135916060013590565b6000602082840312156138e8578081fd5b5051919050565b6000815180845260208085019450808401835b8381101561391e57815187529582019590820190600101613902565b509495945050505050565b6001600160a01b0391909116815260200190565b6001600160a01b039384168152919092166020820152604081019190915260600190565b6001600160a01b03929092168252602082015260400190565b6001600160a01b0392831681529116602082015260400190565b6001600160a01b039390931683526020830191909152604082015260600190565b6000602082526131a060208301846138ef565b60208082526013908201527211549497d393d7d513d2d15397d0d3d3535255606a1b604082015260600190565b6020808252601e908201527f4552525f4752414455414c5f5550444154455f54494d455f54524156454c0000604082015260600190565b6020808252818101527f4552525f5745494748545f4348414e47455f54494d455f42454c4f575f4d494e604082015260600190565b6020808252601a908201527f4552525f53544152545f574549474854535f4d49534d41544348000000000000604082015260600190565b6020808252600c908201526b11549497d254d7d093d5539160a21b604082015260600190565b60208082526010908201526f4552525f4d41585f494e5f524154494f60801b604082015260600190565b6020808252600f908201526e08aa4a4be9a82a890be82a0a0a49eb608b1b604082015260600190565b6020808252600e908201526d11549497d3505617d5d15251d21560921b604082015260600190565b60208082526014908201527311549497d3505617d513d5105317d5d15251d21560621b604082015260600190565b60208082526014908201527308aa4a4be829a9eaa9ca8a6be9a92a69a82a886960631b604082015260600190565b6020808252600d908201526c11549497d31253525517d3d555609a1b604082015260600190565b6020808252600f908201526e4552525f4d494e5f42414c414e434560881b604082015260600190565b60208082526011908201527011549497d0d0539517d413d2d157d65155607a1b604082015260600190565b60208082526010908201526f11549497d1125597d25395115493905360821b604082015260600190565b6020808252600c908201526b22a9292fa624a6a4aa2fa4a760a11b604082015260600190565b60208082526014908201527322a9292faba2a4a3a42a2fa122a627abafa6a4a760611b604082015260600190565b6020808252601190820152704552525f4d41585f4f55545f524154494f60781b604082015260600190565b6020808252600d908201526c11549497d393d517d093d55391609a1b604082015260600190565b6020808252600e908201526d11549497d3525397d5d15251d21560921b604082015260600190565b60208082526014908201527308aa4a4beae8a928e90a8be82849eac8abe9a82b60631b604082015260600190565b6020808252601b908201527f4552525f54494d454c4f434b5f5354494c4c5f434f554e54494e470000000000604082015260600190565b60208082526010908201526f4552525f4d554c5f4f564552464c4f5760801b604082015260600190565b60208082526017908201527f4552525f4e4f4e434f4e464f524d494e475f544f4b454e000000000000000000604082015260600190565b6020808252600c908201526b4552525f4449565f5a45524f60a01b604082015260600190565b60208082526015908201527422a9292fa120a620a721a2afa122a627abafa6a4a760591b604082015260600190565b6020808252600f908201526e4552525f45524332305f46414c534560881b604082015260600190565b6020808252601190820152704552525f5355425f554e444552464c4f5760781b604082015260600190565b60208082526010908201526f4552525f4144445f4f564552464c4f5760801b604082015260600190565b90815260200190565b918252602082015260400190565b60008482528360208301526060604083015261117760608301846138ef565b958652602086019490945260408501929092526060840152608083015260a082015260c00190565b60405181810167ffffffffffffffff81118282101715613f1e57600080fd5b604052919050565b6001600160a01b0381168114610a4f57600080fdfea264697066735822122038b46142c2f828dddc9c74991de440998e196b4ad87b9d6cebaf9c78120f1f3564736f6c634300060c0033