Address Details
contract
0x21cDa172b2820722f8807fcCC0FF96066e6DFc1d
- Contract Name
- BiPoolManager
- Creator
- 0xfcf982–bb718e at 0x12d0b1–22d32c
- Balance
- 0 CELO
- Locked CELO Balance
- 0.00 CELO
- Voting CELO Balance
- 0.00 CELO
- Pending Unlocked Gold
- 0.00 CELO
- Tokens
-
Fetching tokens...
- Transactions
- 0 Transactions
- Transfers
- 0 Transfers
- Gas Used
- Fetching gas used...
- Last Balance Update
- 14465696
This contract has been verified via Sourcify.
View contract in Sourcify repository
- Contract name:
- BiPoolManager
- Optimization enabled
- true
- Compiler version
- v0.5.17+commit.d19bba13
- Optimization runs
- 10000
- EVM Version
- istanbul
- Verified at
- 2022-12-05T20:29:12.084773Z
contracts/BiPoolManager.sol
pragma solidity ^0.5.13; pragma experimental ABIEncoderV2; import { Ownable } from "openzeppelin-solidity/contracts/ownership/Ownable.sol"; import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol"; import { IExchangeProvider } from "./interfaces/IExchangeProvider.sol"; import { IBiPoolManager } from "./interfaces/IBiPoolManager.sol"; import { IReserve } from "./interfaces/IReserve.sol"; import { IPricingModule } from "./interfaces/IPricingModule.sol"; import { ISortedOracles } from "./interfaces/ISortedOracles.sol"; import { IBreakerBox } from "./interfaces/IBreakerBox.sol"; import { StableToken } from "./StableToken.sol"; import { Initializable } from "./common/Initializable.sol"; import { FixidityLib } from "./common/FixidityLib.sol"; // TODO: Remove when migrating to mento-core. Newer versions of OZ-contracts have this interface interface IERC20Metadata { function symbol() external view returns (string memory); } /** * @title BiPoolExchangeManager * @notice An exchange manager that manages asset exchanges consisting of two assets */ contract BiPoolManager is IExchangeProvider, IBiPoolManager, Initializable, Ownable { using FixidityLib for FixidityLib.Fraction; using SafeMath for uint256; /* ==================== State Variables ==================== */ // Address of the broker contract. address public broker; // Maps an exchange id to the corresponding PoolExchange struct. // exchangeId is in the format "asset0Symbol:asset1Symbol:pricingModuleName" mapping(bytes32 => PoolExchange) public exchanges; bytes32[] public exchangeIds; uint256 private constant TRADING_MODE_BIDIRECTIONAL = 0; // Address of the Mento Reserve contract IReserve public reserve; //Address of the Mento BreakerBox contract IBreakerBox public breakerBox; // Address of the Mento SortedOracles contract ISortedOracles public sortedOracles; /* ==================== Constructor ==================== */ /** * @notice Sets initialized == true on implementation contracts. * @param test Set to true to skip implementation initialization. */ // solhint-disable-next-line no-empty-blocks constructor(bool test) public Initializable(test) {} /** * @notice Allows the contract to be upgradable via the proxy. * @param _broker The address of the broker contract. * @param _reserve The address of the reserve contract. */ function initialize( address _broker, IReserve _reserve, ISortedOracles _sortedOracles, IBreakerBox _breakerBox ) external initializer { _transferOwnership(msg.sender); setBroker(_broker); setReserve(_reserve); setSortedOracles(_sortedOracles); setBreakerBox(_breakerBox); } /* ==================== Modifiers ==================== */ modifier onlyBroker() { require(msg.sender == broker, "Caller is not the Broker"); _; } /* ==================== View Functions ==================== */ /** * @notice Get a PoolExchange from storage. * @param exchangeId the exchange id */ function getPoolExchange(bytes32 exchangeId) public view returns (PoolExchange memory exchange) { exchange = exchanges[exchangeId]; require(exchange.asset0 != address(0), "An exchange with the specified id does not exist"); } /** * @notice Get all exchange IDs. * @return exchangeIds List of the exchangeIds. */ function getExchangeIds() external view returns (bytes32[] memory) { return exchangeIds; } /** * @notice Get all exchanges (used by interfaces) * @dev We don't expect the number of exchanges to grow to * astronomical values so this is safe gas-wise as is. */ function getExchanges() public view returns (Exchange[] memory _exchanges) { _exchanges = new Exchange[](exchangeIds.length); for (uint256 i = 0; i < exchangeIds.length; i++) { _exchanges[i].exchangeId = exchangeIds[i]; _exchanges[i].assets = new address[](2); _exchanges[i].assets[0] = exchanges[exchangeIds[i]].asset0; _exchanges[i].assets[1] = exchanges[exchangeIds[i]].asset1; } } /** * @notice Calculate amountOut of tokenOut received for a given amountIn of tokenIn * @param exchangeId The id of the exchange i.e PoolExchange to use * @param tokenIn The token to be sold * @param tokenOut The token to be bought * @param amountIn The amount of tokenIn to be sold * @return amountOut The amount of tokenOut to be bought */ function getAmountOut( bytes32 exchangeId, address tokenIn, address tokenOut, uint256 amountIn ) external view returns (uint256 amountOut) { PoolExchange memory exchange = getPoolExchange(exchangeId); (amountOut, ) = _getAmountOut(exchange, tokenIn, tokenOut, amountIn); } /** * @notice Calculate amountIn of tokenIn for a given amountIn of tokenIn * @param exchangeId The id of the exchange i.e PoolExchange to use * @param tokenIn The token to be sold * @param tokenOut The token to be bought * @param amountOut The amount of tokenOut to be bought * @return amountIn The amount of tokenIn to be sold */ function getAmountIn( bytes32 exchangeId, address tokenIn, address tokenOut, uint256 amountOut ) external view returns (uint256 amountIn) { PoolExchange memory exchange = getPoolExchange(exchangeId); (amountIn, ) = _getAmountIn(exchange, tokenIn, tokenOut, amountOut); } /* ==================== Mutative Functions ==================== */ /** * @notice Sets the address of the broker contract. * @param _broker The new address of the broker contract. */ function setBroker(address _broker) public onlyOwner { require(_broker != address(0), "Broker address must be set"); broker = _broker; emit BrokerUpdated(_broker); } /** * @notice Sets the address of the reserve contract. * @param _reserve The new address of the reserve contract. */ function setReserve(IReserve _reserve) public onlyOwner { require(address(_reserve) != address(0), "Reserve address must be set"); reserve = _reserve; emit ReserveUpdated(address(_reserve)); } /** * @notice Sets the address of the BreakerBox. * @param _breakerBox The new BreakerBox address. */ function setBreakerBox(IBreakerBox _breakerBox) public onlyOwner { require(address(_breakerBox) != address(0), "BreakerBox address must be set"); breakerBox = _breakerBox; emit BreakerBoxUpdated(address(_breakerBox)); } /** * @notice Sets the address of the sortedOracles contract. * @param _sortedOracles The new address of the sorted oracles contract. */ function setSortedOracles(ISortedOracles _sortedOracles) public onlyOwner { require(address(_sortedOracles) != address(0), "SortedOracles address must be set"); sortedOracles = _sortedOracles; emit SortedOraclesUpdated(address(_sortedOracles)); } /** * @notice Creates a new exchange using the given parameters. * @param _exchange the PoolExchange to create. * @return exchangeId The id of the newly created exchange. */ function createExchange(PoolExchange calldata _exchange) external onlyOwner returns (bytes32 exchangeId) { PoolExchange memory exchange = _exchange; require(address(exchange.pricingModule) != address(0), "pricingModule must be set"); require(exchange.asset0 != address(0), "asset0 must be set"); require(exchange.asset1 != address(0), "asset1 must be set"); exchangeId = keccak256( abi.encodePacked( IERC20Metadata(exchange.asset0).symbol(), IERC20Metadata(exchange.asset1).symbol(), exchange.pricingModule.name() ) ); require(exchanges[exchangeId].asset0 == address(0), "An exchange with the specified assets and exchange exists"); validate(exchange); (uint256 bucket0, uint256 bucket1) = getUpdatedBuckets(exchange); exchange.bucket0 = bucket0; exchange.bucket1 = bucket1; exchanges[exchangeId] = exchange; exchangeIds.push(exchangeId); emit ExchangeCreated(exchangeId, exchange.asset0, exchange.asset1, address(exchange.pricingModule)); } /** * @notice Destroys a exchange with the given parameters if it exists and frees up * the collateral and stable allocation it was using. * @param exchangeId the id of the exchange to destroy * @param exchangeIdIndex The index of the exchangeId in the ids array * @return destroyed A boolean indicating whether or not the exchange was successfully destroyed. */ function destroyExchange(bytes32 exchangeId, uint256 exchangeIdIndex) external onlyOwner returns (bool destroyed) { require(exchangeIdIndex < exchangeIds.length, "exchangeIdIndex not in range"); require(exchangeIds[exchangeIdIndex] == exchangeId, "exchangeId at index doesn't match"); PoolExchange memory exchange = exchanges[exchangeId]; delete exchanges[exchangeId]; exchangeIds[exchangeIdIndex] = exchangeIds[exchangeIds.length - 1]; exchangeIds.pop(); destroyed = true; emit ExchangeDestroyed(exchangeId, exchange.asset0, exchange.asset1, address(exchange.pricingModule)); } /** * @notice Execute a token swap with fixed amountIn * @param exchangeId The id of exchange, i.e. PoolExchange to use * @param tokenIn The token to be sold * @param tokenOut The token to be bought * @param amountIn The amount of tokenIn to be sold * @return amountOut The amount of tokenOut to be bought */ function swapIn( bytes32 exchangeId, address tokenIn, address tokenOut, uint256 amountIn ) external onlyBroker returns (uint256 amountOut) { PoolExchange memory exchange = getPoolExchange(exchangeId); require( breakerBox.getRateFeedTradingMode(exchange.config.referenceRateFeedID) == TRADING_MODE_BIDIRECTIONAL, "Trading is suspended for this reference rate" ); bool bucketsUpdated; (amountOut, bucketsUpdated) = _getAmountOut(exchange, tokenIn, tokenOut, amountIn); executeSwap(exchangeId, exchange, tokenIn, amountIn, amountOut, bucketsUpdated); return amountOut; } /** * @notice Execute a token swap with fixed amountOut * @param exchangeId The id of exchange, i.e. PoolExchange to use * @param tokenIn The token to be sold * @param tokenOut The token to be bought * @param amountOut The amount of tokenOut to be bought * @return amountIn The amount of tokenIn to be sold */ function swapOut( bytes32 exchangeId, address tokenIn, address tokenOut, uint256 amountOut ) external onlyBroker returns (uint256 amountIn) { PoolExchange memory exchange = getPoolExchange(exchangeId); require( breakerBox.getRateFeedTradingMode(exchange.config.referenceRateFeedID) == TRADING_MODE_BIDIRECTIONAL, "Trading is suspended for this reference rate" ); bool bucketsUpdated; (amountIn, bucketsUpdated) = _getAmountIn(exchange, tokenIn, tokenOut, amountOut); executeSwap(exchangeId, exchange, tokenIn, amountIn, amountOut, bucketsUpdated); return amountIn; } /* ==================== Private Functions ==================== */ /** * @notice Execute a swap against the in memory exchange and write * the new bucket sizes to storage. * @param exchangeId The id of the exchange * @param exchange The exchange to operate on * @param tokenIn The token to be sold * @param amountIn The amount of tokenIn to be sold * @param amountOut The amount of tokenOut to be bought * @param bucketsUpdated wether the buckets updated during the swap */ function executeSwap( bytes32 exchangeId, PoolExchange memory exchange, address tokenIn, uint256 amountIn, uint256 amountOut, bool bucketsUpdated ) internal { if (bucketsUpdated) { // solhint-disable-next-line not-rely-on-time exchanges[exchangeId].lastBucketUpdate = now; emit BucketsUpdated(exchangeId, exchange.bucket0, exchange.bucket1); } if (tokenIn == exchange.asset0) { exchanges[exchangeId].bucket0 = exchange.bucket0 + amountIn; exchanges[exchangeId].bucket1 = exchange.bucket1 - amountOut; } else { exchanges[exchangeId].bucket0 = exchange.bucket0 - amountOut; exchanges[exchangeId].bucket1 = exchange.bucket1 + amountIn; } } /** * @notice Calculate amountOut of tokenOut received for a given amountIn of tokenIn * @param exchange The exchange to operate on * @param tokenIn The token to be sold * @param tokenOut The token to be bought * @param amountIn The amount of tokenIn to be sold * @return amountOut The amount of tokenOut to be bought * @return bucketsUpdated Wether the buckets were updated during the quote */ function _getAmountOut( PoolExchange memory exchange, address tokenIn, address tokenOut, uint256 amountIn ) internal view returns (uint256 amountOut, bool bucketsUpdated) { require( (tokenIn == exchange.asset0 && tokenOut == exchange.asset1) || (tokenIn == exchange.asset1 && tokenOut == exchange.asset0), "tokenIn and tokenOut must match exchange" ); (exchange, bucketsUpdated) = updateBucketsIfNecessary(exchange); if (tokenIn == exchange.asset0) { amountOut = exchange.pricingModule.getAmountOut( exchange.bucket0, exchange.bucket1, exchange.config.spread.unwrap(), amountIn ); } else { amountOut = exchange.pricingModule.getAmountOut( exchange.bucket1, exchange.bucket0, exchange.config.spread.unwrap(), amountIn ); } } /** * @notice Calculate amountIn of tokenIn for a given amountIn of tokenIn * @param exchange The exchange to operate on * @param tokenIn The token to be sold * @param tokenOut The token to be bought * @param amountOut The amount of tokenOut to be bought * @return amountIn The amount of tokenIn to be sold * @return bucketsUpdated Wether the buckets were updated during the quote */ function _getAmountIn( PoolExchange memory exchange, address tokenIn, address tokenOut, uint256 amountOut ) internal view returns (uint256 amountIn, bool bucketsUpdated) { require( (tokenIn == exchange.asset0 && tokenOut == exchange.asset1) || (tokenIn == exchange.asset1 && tokenOut == exchange.asset0), "tokenIn and tokenOut must match exchange" ); (exchange, bucketsUpdated) = updateBucketsIfNecessary(exchange); if (tokenIn == exchange.asset0) { amountIn = exchange.pricingModule.getAmountIn( exchange.bucket0, exchange.bucket1, exchange.config.spread.unwrap(), amountOut ); } else { amountIn = exchange.pricingModule.getAmountIn( exchange.bucket1, exchange.bucket0, exchange.config.spread.unwrap(), amountOut ); } } /** * @notice If conditions are met, update the exchange bucket sizes. * @dev This doesn't checkpoint the exchange, just updates the in-memory one * so it should be used in a context that then checkpoints the exchange. * @param exchange The exchange being updated. * @return exchangeAfter The updated exchange. */ function updateBucketsIfNecessary(PoolExchange memory exchange) internal view returns (PoolExchange memory, bool updated) { if (shouldUpdateBuckets(exchange)) { (exchange.bucket0, exchange.bucket1) = getUpdatedBuckets(exchange); updated = true; } return (exchange, updated); } /** * @notice Determine if a exchange's buckets should be updated * based on staleness of buckets and oracle rates. * @param exchange The PoolExchange. * @return shouldUpdate */ function shouldUpdateBuckets(PoolExchange memory exchange) internal view returns (bool) { (bool isReportExpired, ) = sortedOracles.isOldestReportExpired(exchange.config.referenceRateFeedID); // solhint-disable-next-line not-rely-on-time bool timePassed = now >= exchange.lastBucketUpdate.add(exchange.config.referenceRateResetFrequency); bool enoughReports = (sortedOracles.numRates(exchange.config.referenceRateFeedID) >= exchange.config.minimumReports); // solhint-disable-next-line not-rely-on-time bool medianReportRecent = sortedOracles.medianTimestamp(exchange.config.referenceRateFeedID) > now.sub(exchange.config.referenceRateResetFrequency); return timePassed && enoughReports && medianReportRecent && !isReportExpired; } /** * @notice Calculate the new bucket sizes for a exchange. * @param exchange The PoolExchange in context. * @return bucket0 The size of bucket0. * @return bucket1 The size of bucket1. */ function getUpdatedBuckets(PoolExchange memory exchange) internal view returns (uint256 bucket0, uint256 bucket1) { // TODO: Take max fraction/min supply in account when setting the bucket size bucket0 = exchange.config.stablePoolResetSize; uint256 exchangeRateNumerator; uint256 exchangeRateDenominator; (exchangeRateNumerator, exchangeRateDenominator) = getOracleExchangeRate(exchange.config.referenceRateFeedID); bucket1 = exchangeRateDenominator.mul(bucket0).div(exchangeRateNumerator); } /** * @notice Get the exchange rate as numerator,denominator from sorted oracles * and protect in case of a 0-denominator. * @param target the reportTarget to read from SortedOracles * @return rateNumerator * @return rateDenominator */ function getOracleExchangeRate(address target) internal view returns (uint256, uint256) { uint256 rateNumerator; uint256 rateDenominator; (rateNumerator, rateDenominator) = sortedOracles.medianRate(target); require(rateDenominator > 0, "exchange rate denominator must be greater than 0"); return (rateNumerator, rateDenominator); } /** * @notice Valitates a PoolExchange's parameters and configuration * @dev Reverts if not valid * @param exchange The PoolExchange to validate */ function validate(PoolExchange memory exchange) private view { require(reserve.isStableAsset(exchange.asset0), "asset0 must be a stable registered with the reserve"); require( reserve.isStableAsset(exchange.asset1) || reserve.isCollateralAsset(exchange.asset1), "asset1 must be a stable or collateral" ); require(FixidityLib.lte(exchange.config.spread, FixidityLib.fixed1()), "spread must be less than or equal to 1"); require(exchange.config.referenceRateFeedID != address(0), "referenceRateFeedID must be set"); // TODO: Stable bucket max fraction should not exceed available stable bucket fraction. // TODO: minSupplyForStableBucketCap gt 0 & is there an aggregated value that needs to be checked } }
/contracts/common/Initializable.sol
pragma solidity ^0.5.13; contract Initializable { bool public initialized; constructor(bool testingDeployment) public { if (!testingDeployment) { initialized = true; } } modifier initializer() { require(!initialized, "contract already initialized"); initialized = true; _; } }
/contracts/common/Freezable.sol
pragma solidity ^0.5.13; import "./UsingRegistry.sol"; contract Freezable is UsingRegistry { // onlyWhenNotFrozen functions can only be called when `frozen` is false, otherwise they will // revert. modifier onlyWhenNotFrozen() { require(!getFreezer().isFrozen(address(this)), "can't call when contract is frozen"); _; } }
/contracts/common/FixidityLib.sol
pragma solidity ^0.5.13; /** * @title FixidityLib * @author Gadi Guy, Alberto Cuesta Canada * @notice This library provides fixed point arithmetic with protection against * overflow. * All operations are done with uint256 and the operands must have been created * with any of the newFrom* functions, which shift the comma digits() to the * right and check for limits, or with wrap() which expects a number already * in the internal representation of a fraction. * When using this library be sure to use maxNewFixed() as the upper limit for * creation of fixed point numbers. * @dev All contained functions are pure and thus marked internal to be inlined * on consuming contracts at compile time for gas efficiency. */ library FixidityLib { struct Fraction { uint256 value; } /** * @notice Number of positions that the comma is shifted to the right. */ function digits() internal pure returns (uint8) { return 24; } uint256 private constant FIXED1_UINT = 1000000000000000000000000; /** * @notice This is 1 in the fixed point units used in this library. * @dev Test fixed1() equals 10^digits() * Hardcoded to 24 digits. */ function fixed1() internal pure returns (Fraction memory) { return Fraction(FIXED1_UINT); } /** * @notice Wrap a uint256 that represents a 24-decimal fraction in a Fraction * struct. * @param x Number that already represents a 24-decimal fraction. * @return A Fraction struct with contents x. */ function wrap(uint256 x) internal pure returns (Fraction memory) { return Fraction(x); } /** * @notice Unwraps the uint256 inside of a Fraction struct. */ function unwrap(Fraction memory x) internal pure returns (uint256) { return x.value; } /** * @notice The amount of decimals lost on each multiplication operand. * @dev Test mulPrecision() equals sqrt(fixed1) */ function mulPrecision() internal pure returns (uint256) { return 1000000000000; } /** * @notice Maximum value that can be converted to fixed point. Optimize for deployment. * @dev * Test maxNewFixed() equals maxUint256() / fixed1() */ function maxNewFixed() internal pure returns (uint256) { return 115792089237316195423570985008687907853269984665640564; } /** * @notice Converts a uint256 to fixed point Fraction * @dev Test newFixed(0) returns 0 * Test newFixed(1) returns fixed1() * Test newFixed(maxNewFixed()) returns maxNewFixed() * fixed1() * Test newFixed(maxNewFixed()+1) fails */ function newFixed(uint256 x) internal pure returns (Fraction memory) { require(x <= maxNewFixed(), "can't create fixidity number larger than maxNewFixed()"); return Fraction(x * FIXED1_UINT); } /** * @notice Converts a uint256 in the fixed point representation of this * library to a non decimal. All decimal digits will be truncated. */ function fromFixed(Fraction memory x) internal pure returns (uint256) { return x.value / FIXED1_UINT; } /** * @notice Converts two uint256 representing a fraction to fixed point units, * equivalent to multiplying dividend and divisor by 10^digits(). * @param numerator numerator must be <= maxNewFixed() * @param denominator denominator must be <= maxNewFixed() and denominator can't be 0 * @dev * Test newFixedFraction(1,0) fails * Test newFixedFraction(0,1) returns 0 * Test newFixedFraction(1,1) returns fixed1() * Test newFixedFraction(1,fixed1()) returns 1 */ function newFixedFraction(uint256 numerator, uint256 denominator) internal pure returns (Fraction memory) { Fraction memory convertedNumerator = newFixed(numerator); Fraction memory convertedDenominator = newFixed(denominator); return divide(convertedNumerator, convertedDenominator); } /** * @notice Returns the integer part of a fixed point number. * @dev * Test integer(0) returns 0 * Test integer(fixed1()) returns fixed1() * Test integer(newFixed(maxNewFixed())) returns maxNewFixed()*fixed1() */ function integer(Fraction memory x) internal pure returns (Fraction memory) { return Fraction((x.value / FIXED1_UINT) * FIXED1_UINT); // Can't overflow } /** * @notice Returns the fractional part of a fixed point number. * In the case of a negative number the fractional is also negative. * @dev * Test fractional(0) returns 0 * Test fractional(fixed1()) returns 0 * Test fractional(fixed1()-1) returns 10^24-1 */ function fractional(Fraction memory x) internal pure returns (Fraction memory) { return Fraction(x.value - (x.value / FIXED1_UINT) * FIXED1_UINT); // Can't overflow } /** * @notice x+y. * @dev The maximum value that can be safely used as an addition operator is defined as * maxFixedAdd = maxUint256()-1 / 2, or * 57896044618658097711785492504343953926634992332820282019728792003956564819967. * Test add(maxFixedAdd,maxFixedAdd) equals maxFixedAdd + maxFixedAdd * Test add(maxFixedAdd+1,maxFixedAdd+1) throws */ function add(Fraction memory x, Fraction memory y) internal pure returns (Fraction memory) { uint256 z = x.value + y.value; require(z >= x.value, "add overflow detected"); return Fraction(z); } /** * @notice x-y. * @dev * Test subtract(6, 10) fails */ function subtract(Fraction memory x, Fraction memory y) internal pure returns (Fraction memory) { require(x.value >= y.value, "substraction underflow detected"); return Fraction(x.value - y.value); } /** * @notice x*y. If any of the operators is higher than the max multiplier value it * might overflow. * @dev The maximum value that can be safely used as a multiplication operator * (maxFixedMul) is calculated as sqrt(maxUint256()*fixed1()), * or 340282366920938463463374607431768211455999999999999 * Test multiply(0,0) returns 0 * Test multiply(maxFixedMul,0) returns 0 * Test multiply(0,maxFixedMul) returns 0 * Test multiply(fixed1()/mulPrecision(),fixed1()*mulPrecision()) returns fixed1() * Test multiply(maxFixedMul,maxFixedMul) is around maxUint256() * Test multiply(maxFixedMul+1,maxFixedMul+1) fails */ // solhint-disable-next-line code-complexity function multiply(Fraction memory x, Fraction memory y) internal pure returns (Fraction memory) { if (x.value == 0 || y.value == 0) return Fraction(0); if (y.value == FIXED1_UINT) return x; if (x.value == FIXED1_UINT) return y; // Separate into integer and fractional parts // x = x1 + x2, y = y1 + y2 uint256 x1 = integer(x).value / FIXED1_UINT; uint256 x2 = fractional(x).value; uint256 y1 = integer(y).value / FIXED1_UINT; uint256 y2 = fractional(y).value; // (x1 + x2) * (y1 + y2) = (x1 * y1) + (x1 * y2) + (x2 * y1) + (x2 * y2) uint256 x1y1 = x1 * y1; if (x1 != 0) require(x1y1 / x1 == y1, "overflow x1y1 detected"); // x1y1 needs to be multiplied back by fixed1 // solhint-disable-next-line var-name-mixedcase uint256 fixed_x1y1 = x1y1 * FIXED1_UINT; if (x1y1 != 0) require(fixed_x1y1 / x1y1 == FIXED1_UINT, "overflow x1y1 * fixed1 detected"); x1y1 = fixed_x1y1; uint256 x2y1 = x2 * y1; if (x2 != 0) require(x2y1 / x2 == y1, "overflow x2y1 detected"); uint256 x1y2 = x1 * y2; if (x1 != 0) require(x1y2 / x1 == y2, "overflow x1y2 detected"); x2 = x2 / mulPrecision(); y2 = y2 / mulPrecision(); uint256 x2y2 = x2 * y2; if (x2 != 0) require(x2y2 / x2 == y2, "overflow x2y2 detected"); // result = fixed1() * x1 * y1 + x1 * y2 + x2 * y1 + x2 * y2 / fixed1(); Fraction memory result = Fraction(x1y1); result = add(result, Fraction(x2y1)); // Add checks for overflow result = add(result, Fraction(x1y2)); // Add checks for overflow result = add(result, Fraction(x2y2)); // Add checks for overflow return result; } /** * @notice 1/x * @dev * Test reciprocal(0) fails * Test reciprocal(fixed1()) returns fixed1() * Test reciprocal(fixed1()*fixed1()) returns 1 // Testing how the fractional is truncated * Test reciprocal(1+fixed1()*fixed1()) returns 0 // Testing how the fractional is truncated * Test reciprocal(newFixedFraction(1, 1e24)) returns newFixed(1e24) */ function reciprocal(Fraction memory x) internal pure returns (Fraction memory) { require(x.value != 0, "can't call reciprocal(0)"); return Fraction((FIXED1_UINT * FIXED1_UINT) / x.value); // Can't overflow } /** * @notice x/y. If the dividend is higher than the max dividend value, it * might overflow. You can use multiply(x,reciprocal(y)) instead. * @dev The maximum value that can be safely used as a dividend (maxNewFixed) is defined as * divide(maxNewFixed,newFixedFraction(1,fixed1())) is around maxUint256(). * This yields the value 115792089237316195423570985008687907853269984665640564. * Test maxNewFixed equals maxUint256()/fixed1() * Test divide(maxNewFixed,1) equals maxNewFixed*(fixed1) * Test divide(maxNewFixed+1,multiply(mulPrecision(),mulPrecision())) throws * Test divide(fixed1(),0) fails * Test divide(maxNewFixed,1) = maxNewFixed*(10^digits()) * Test divide(maxNewFixed+1,1) throws */ function divide(Fraction memory x, Fraction memory y) internal pure returns (Fraction memory) { require(y.value != 0, "can't divide by 0"); // solhint-disable-next-line var-name-mixedcase uint256 X = x.value * FIXED1_UINT; require(X / FIXED1_UINT == x.value, "overflow at divide"); return Fraction(X / y.value); } /** * @notice x > y */ function gt(Fraction memory x, Fraction memory y) internal pure returns (bool) { return x.value > y.value; } /** * @notice x >= y */ function gte(Fraction memory x, Fraction memory y) internal pure returns (bool) { return x.value >= y.value; } /** * @notice x < y */ function lt(Fraction memory x, Fraction memory y) internal pure returns (bool) { return x.value < y.value; } /** * @notice x <= y */ function lte(Fraction memory x, Fraction memory y) internal pure returns (bool) { return x.value <= y.value; } /** * @notice x == y */ function equals(Fraction memory x, Fraction memory y) internal pure returns (bool) { return x.value == y.value; } /** * @notice x <= 1 */ function isProperFraction(Fraction memory x) internal pure returns (bool) { return lte(x, fixed1()); } }
/contracts/common/CalledByVm.sol
pragma solidity ^0.5.13; contract CalledByVm { modifier onlyVm() { require(msg.sender == address(0), "Only VM can call"); _; } }
/contracts/StableToken.sol
pragma solidity ^0.5.13; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; import "openzeppelin-solidity/contracts/ownership/Ownable.sol"; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; import "./interfaces/IStableToken.sol"; import "./interfaces/ICeloToken.sol"; import "./common/interfaces/ICeloVersionedContract.sol"; import "./common/CalledByVm.sol"; import "./common/Initializable.sol"; import "./common/FixidityLib.sol"; import "./common/Freezable.sol"; import "./common/UsingRegistry.sol"; import "./common/UsingPrecompiles.sol"; /** * @title An ERC20 compliant token with adjustable supply. */ // solhint-disable-next-line max-line-length contract StableToken is ICeloVersionedContract, Ownable, Initializable, UsingRegistry, UsingPrecompiles, Freezable, CalledByVm, IStableToken, IERC20, ICeloToken { using FixidityLib for FixidityLib.Fraction; using SafeMath for uint256; event InflationFactorUpdated(uint256 factor, uint256 lastUpdated); event InflationParametersUpdated(uint256 rate, uint256 updatePeriod, uint256 lastUpdated); event Transfer(address indexed from, address indexed to, uint256 value); event TransferComment(string comment); bytes32 private constant GRANDA_MENTO_REGISTRY_ID = keccak256(abi.encodePacked("GrandaMento")); bytes32 private constant BROKER_REGISTRY_ID = keccak256(abi.encodePacked("Broker")); string internal name_; string internal symbol_; uint8 internal decimals_; // Stored as units. Value can be found using unitsToValue(). mapping(address => uint256) internal balances; uint256 internal totalSupply_; // Stored as values. Units can be found using valueToUnits(). mapping(address => mapping(address => uint256)) internal allowed; // STABILITY FEE PARAMETERS // The `rate` is how much the `factor` is adjusted by per `updatePeriod`. // The `factor` describes units/value of StableToken, and is greater than or equal to 1. // The `updatePeriod` governs how often the `factor` is updated. // `factorLastUpdated` indicates when the inflation factor was last updated. struct InflationState { FixidityLib.Fraction rate; FixidityLib.Fraction factor; uint256 updatePeriod; uint256 factorLastUpdated; } // solhint-disable-next-line state-visibility InflationState inflationState; // The registry ID of the exchange contract with permission to mint and burn this token. // Unique per StableToken instance. // solhint-disable-next-line state-visibility bytes32 exchangeRegistryId; /** * @notice Recomputes and updates inflation factor if more than `updatePeriod` * has passed since last update. */ modifier updateInflationFactor() { FixidityLib.Fraction memory updatedInflationFactor; uint256 lastUpdated; (updatedInflationFactor, lastUpdated) = getUpdatedInflationFactor(); if (lastUpdated != inflationState.factorLastUpdated) { inflationState.factor = updatedInflationFactor; inflationState.factorLastUpdated = lastUpdated; emit InflationFactorUpdated(inflationState.factor.unwrap(), inflationState.factorLastUpdated); } _; } /** * @notice Returns the storage, major, minor, and patch version of the contract. * @return Storage version of the contract. * @return Major version of the contract. * @return Minor version of the contract. * @return Patch version of the contract. */ function getVersionNumber() external pure returns ( uint256, uint256, uint256, uint256 ) { return (1, 2, 1, 0); } /** * @notice Sets initialized == true on implementation contracts * @param test Set to true to skip implementation initialization */ constructor(bool test) public Initializable(test) {} /** * @param _name The name of the stable token (English) * @param _symbol A short symbol identifying the token (e.g. "cUSD") * @param _decimals Tokens are divisible to this many decimal places. * @param registryAddress Address of the Registry contract. * @param inflationRate Weekly inflation rate. * @param inflationFactorUpdatePeriod How often the inflation factor is updated, in seconds. * @param initialBalanceAddresses Array of addresses with an initial balance. * @param initialBalanceValues Array of balance values corresponding to initialBalanceAddresses. * @param exchangeIdentifier String identifier of exchange in registry (for specific fiat pairs) */ function initialize( string calldata _name, string calldata _symbol, uint8 _decimals, address registryAddress, uint256 inflationRate, uint256 inflationFactorUpdatePeriod, address[] calldata initialBalanceAddresses, uint256[] calldata initialBalanceValues, string calldata exchangeIdentifier ) external initializer { require(inflationRate != 0, "Must provide a non-zero inflation rate"); require(inflationFactorUpdatePeriod > 0, "inflationFactorUpdatePeriod must be > 0"); _transferOwnership(msg.sender); totalSupply_ = 0; name_ = _name; symbol_ = _symbol; decimals_ = _decimals; inflationState.rate = FixidityLib.wrap(inflationRate); inflationState.factor = FixidityLib.fixed1(); inflationState.updatePeriod = inflationFactorUpdatePeriod; // solhint-disable-next-line not-rely-on-time inflationState.factorLastUpdated = now; require(initialBalanceAddresses.length == initialBalanceValues.length, "Array length mismatch"); for (uint256 i = 0; i < initialBalanceAddresses.length; i = i.add(1)) { _mint(initialBalanceAddresses[i], initialBalanceValues[i]); } setRegistry(registryAddress); exchangeRegistryId = keccak256(abi.encodePacked(exchangeIdentifier)); } /** * @notice Updates Inflation Parameters. * @param rate New rate. * @param updatePeriod How often inflationFactor is updated. */ function setInflationParameters(uint256 rate, uint256 updatePeriod) external onlyOwner updateInflationFactor { require(rate != 0, "Must provide a non-zero inflation rate."); require(updatePeriod > 0, "updatePeriod must be > 0"); inflationState.rate = FixidityLib.wrap(rate); inflationState.updatePeriod = updatePeriod; emit InflationParametersUpdated( rate, updatePeriod, // solhint-disable-next-line not-rely-on-time now ); } /** * @notice Increase the allowance of another user. * @param spender The address which is being approved to spend StableToken. * @param value The increment of the amount of StableToken approved to the spender. * @return True if the transaction succeeds. */ function increaseAllowance(address spender, uint256 value) external updateInflationFactor returns (bool) { require(spender != address(0), "reserved address 0x0 cannot have allowance"); uint256 oldValue = allowed[msg.sender][spender]; uint256 newValue = oldValue.add(value); allowed[msg.sender][spender] = newValue; emit Approval(msg.sender, spender, newValue); return true; } /** * @notice Decrease the allowance of another user. * @param spender The address which is being approved to spend StableToken. * @param value The decrement of the amount of StableToken approved to the spender. * @return True if the transaction succeeds. */ function decreaseAllowance(address spender, uint256 value) external updateInflationFactor returns (bool) { uint256 oldValue = allowed[msg.sender][spender]; uint256 newValue = oldValue.sub(value); allowed[msg.sender][spender] = newValue; emit Approval(msg.sender, spender, newValue); return true; } /** * @notice Approve a user to transfer StableToken on behalf of another user. * @param spender The address which is being approved to spend StableToken. * @param value The amount of StableToken approved to the spender. * @return True if the transaction succeeds. */ function approve(address spender, uint256 value) external updateInflationFactor returns (bool) { require(spender != address(0), "reserved address 0x0 cannot have allowance"); allowed[msg.sender][spender] = value; emit Approval(msg.sender, spender, value); return true; } /** * @notice Mints new StableToken and gives it to 'to'. * @param to The account for which to mint tokens. * @param value The amount of StableToken to mint. */ function mint(address to, uint256 value) external updateInflationFactor returns (bool) { require( msg.sender == registry.getAddressForOrDie(getExchangeRegistryId()) || msg.sender == registry.getAddressFor(VALIDATORS_REGISTRY_ID) || msg.sender == registry.getAddressFor(GRANDA_MENTO_REGISTRY_ID) || msg.sender == registry.getAddressFor(BROKER_REGISTRY_ID), "Sender not authorized to mint" ); return _mint(to, value); } /** * @notice Mints new StableToken and gives it to 'to'. * @param to The account for which to mint tokens. * @param value The amount of StableToken to mint. */ function _mint(address to, uint256 value) private returns (bool) { require(to != address(0), "0 is a reserved address"); if (value == 0) { return true; } uint256 units = _valueToUnits(inflationState.factor, value); totalSupply_ = totalSupply_.add(units); balances[to] = balances[to].add(units); emit Transfer(address(0), to, value); return true; } /** * @notice Transfer token for a specified address * @param to The address to transfer to. * @param value The amount to be transferred. * @param comment The transfer comment. * @return True if the transaction succeeds. */ function transferWithComment( address to, uint256 value, string calldata comment ) external updateInflationFactor onlyWhenNotFrozen returns (bool) { bool succeeded = transfer(to, value); emit TransferComment(comment); return succeeded; } /** * @notice Burns StableToken from the balance of msg.sender. * @param value The amount of StableToken to burn. */ function burn(uint256 value) external updateInflationFactor returns (bool) { require( msg.sender == registry.getAddressForOrDie(getExchangeRegistryId()) || msg.sender == registry.getAddressFor(GRANDA_MENTO_REGISTRY_ID) || msg.sender == registry.getAddressFor(BROKER_REGISTRY_ID), "Sender not authorized to burn" ); uint256 units = _valueToUnits(inflationState.factor, value); require(units <= balances[msg.sender], "value exceeded balance of sender"); totalSupply_ = totalSupply_.sub(units); balances[msg.sender] = balances[msg.sender].sub(units); emit Transfer(msg.sender, address(0), units); return true; } /** * @notice Transfers StableToken from one address to another on behalf of a user. * @param from The address to transfer StableToken from. * @param to The address to transfer StableToken to. * @param value The amount of StableToken to transfer. * @return True if the transaction succeeds. */ function transferFrom( address from, address to, uint256 value ) external updateInflationFactor onlyWhenNotFrozen returns (bool) { uint256 units = _valueToUnits(inflationState.factor, value); require(to != address(0), "transfer attempted to reserved address 0x0"); require(units <= balances[from], "transfer value exceeded balance of sender"); require(value <= allowed[from][msg.sender], "transfer value exceeded sender's allowance for recipient"); balances[to] = balances[to].add(units); balances[from] = balances[from].sub(units); allowed[from][msg.sender] = allowed[from][msg.sender].sub(value); emit Transfer(from, to, value); return true; } /** * @return The name of the stable token. */ function name() external view returns (string memory) { return name_; } /** * @return The symbol of the stable token. */ function symbol() external view returns (string memory) { return symbol_; } /** * @return The number of decimal places to which StableToken is divisible. */ function decimals() external view returns (uint8) { return decimals_; } /** * @notice Gets the amount of owner's StableToken allowed to be spent by spender. * @param accountOwner The owner of the StableToken. * @param spender The spender of the StableToken. * @return The amount of StableToken owner is allowing spender to spend. */ function allowance(address accountOwner, address spender) external view returns (uint256) { return allowed[accountOwner][spender]; } /** * @notice Gets the balance of the specified address using the presently stored inflation factor. * @param accountOwner The address to query the balance of. * @return The balance of the specified address. */ function balanceOf(address accountOwner) external view returns (uint256) { return unitsToValue(balances[accountOwner]); } /** * @return The total value of StableToken in existence * @dev Though totalSupply_ is stored in units, this returns value. */ function totalSupply() external view returns (uint256) { return unitsToValue(totalSupply_); } /** * @notice gets inflation parameters. * @return rate * @return factor * @return updatePeriod * @return factorLastUpdated */ function getInflationParameters() external view returns ( uint256, uint256, uint256, uint256 ) { return ( inflationState.rate.unwrap(), inflationState.factor.unwrap(), inflationState.updatePeriod, inflationState.factorLastUpdated ); } /** * @notice Returns the units for a given value given the current inflation factor. * @param value The value to convert to units. * @return The units corresponding to `value` given the current inflation factor. * @dev We don't compute the updated inflationFactor here because * we assume any function calling this will have updated the inflation factor. */ function valueToUnits(uint256 value) external view returns (uint256) { FixidityLib.Fraction memory updatedInflationFactor; (updatedInflationFactor, ) = getUpdatedInflationFactor(); return _valueToUnits(updatedInflationFactor, value); } /** * @notice Returns the exchange id in the registry of the corresponding fiat pair exchange. * @dev When this storage is uninitialized, it falls back to the default EXCHANGE_REGISTRY_ID. * exchangeRegistryId was introduced after the initial release of cUSD's StableToken, * so exchangeRegistryId will be uninitialized for that contract. If cUSD's StableToken * exchangeRegistryId were to be correctly initialized, this function could be deprecated * in favor of using exchangeRegistryId directly. * @return Registry id for the corresponding exchange. */ function getExchangeRegistryId() public view returns (bytes32) { if (exchangeRegistryId == bytes32(0)) { return EXCHANGE_REGISTRY_ID; } else { return exchangeRegistryId; } } /** * @notice Returns the value of a given number of units given the current inflation factor. * @param units The units to convert to value. * @return The value corresponding to `units` given the current inflation factor. */ function unitsToValue(uint256 units) public view returns (uint256) { FixidityLib.Fraction memory updatedInflationFactor; (updatedInflationFactor, ) = getUpdatedInflationFactor(); // We're ok using FixidityLib.divide here because updatedInflationFactor is // not going to surpass maxFixedDivisor any time soon. // Quick upper-bound estimation: if annual inflation were 5% (an order of // magnitude more than the initial proposal of 0.5%), in 500 years, the // inflation factor would be on the order of 10**10, which is still a safe // divisor. return FixidityLib.newFixed(units).divide(updatedInflationFactor).fromFixed(); } /** * @notice Returns the units for a given value given the current inflation factor. * @param inflationFactor The current inflation factor. * @param value The value to convert to units. * @return The units corresponding to `value` given the current inflation factor. * @dev We assume any function calling this will have updated the inflation factor. */ function _valueToUnits(FixidityLib.Fraction memory inflationFactor, uint256 value) private pure returns (uint256) { return inflationFactor.multiply(FixidityLib.newFixed(value)).fromFixed(); } /** * @notice Computes the up-to-date inflation factor. * @return Current inflation factor. * @return Last time when the returned inflation factor was updated. */ function getUpdatedInflationFactor() private view returns (FixidityLib.Fraction memory, uint256) { /* solhint-disable not-rely-on-time */ if (now < inflationState.factorLastUpdated.add(inflationState.updatePeriod)) { return (inflationState.factor, inflationState.factorLastUpdated); } uint256 numerator; uint256 denominator; // TODO: handle retroactive updates given decreases to updatePeriod uint256 timesToApplyInflation = now.sub(inflationState.factorLastUpdated).div(inflationState.updatePeriod); (numerator, denominator) = fractionMulExp( inflationState.factor.unwrap(), FixidityLib.fixed1().unwrap(), inflationState.rate.unwrap(), FixidityLib.fixed1().unwrap(), timesToApplyInflation, decimals_ ); // This should never happen. If something went wrong updating the // inflation factor, keep the previous factor if (numerator == 0 || denominator == 0) { return (inflationState.factor, inflationState.factorLastUpdated); } FixidityLib.Fraction memory currentInflationFactor = FixidityLib.wrap(numerator).divide( FixidityLib.wrap(denominator) ); uint256 lastUpdated = inflationState.factorLastUpdated.add(inflationState.updatePeriod.mul(timesToApplyInflation)); return (currentInflationFactor, lastUpdated); /* solhint-enable not-rely-on-time */ } /** * @notice Transfers `value` from `msg.sender` to `to` * @param to The address to transfer to. * @param value The amount to be transferred. */ // solhint-disable-next-line no-simple-event-func-name function transfer(address to, uint256 value) public updateInflationFactor onlyWhenNotFrozen returns (bool) { return _transfer(to, value); } /** * @notice Transfers StableToken from one address to another * @param to The address to transfer StableToken to. * @param value The amount of StableToken to be transferred. */ function _transfer(address to, uint256 value) internal returns (bool) { require(to != address(0), "transfer attempted to reserved address 0x0"); uint256 units = _valueToUnits(inflationState.factor, value); require(balances[msg.sender] >= units, "transfer value exceeded balance of sender"); balances[msg.sender] = balances[msg.sender].sub(units); balances[to] = balances[to].add(units); emit Transfer(msg.sender, to, value); return true; } /** * @notice Reserve balance for making payments for gas in this StableToken currency. * @param from The account to reserve balance from * @param value The amount of balance to reserve * @dev Note that this function is called by the protocol when paying for tx fees in this * currency. After the tx is executed, gas is refunded to the sender and credited to the * various tx fee recipients via a call to `creditGasFees`. Note too that the events emitted * by `creditGasFees` reflect the *net* gas fee payments for the transaction. */ function debitGasFees(address from, uint256 value) external onlyVm onlyWhenNotFrozen updateInflationFactor { uint256 units = _valueToUnits(inflationState.factor, value); balances[from] = balances[from].sub(units); totalSupply_ = totalSupply_.sub(units); } /** * @notice Alternative function to credit balance after making payments * for gas in this StableToken currency. * @param from The account to debit balance from * @param feeRecipient Coinbase address * @param gatewayFeeRecipient Gateway address * @param communityFund Community fund address * @param tipTxFee Coinbase fee * @param baseTxFee Community fund fee * @param gatewayFee Gateway fee * @dev Note that this function is called by the protocol when paying for tx fees in this * currency. Before the tx is executed, gas is debited from the sender via a call to * `debitGasFees`. Note too that the events emitted by `creditGasFees` reflect the *net* gas fee * payments for the transaction. */ function creditGasFees( address from, address feeRecipient, address gatewayFeeRecipient, address communityFund, uint256 refund, uint256 tipTxFee, uint256 gatewayFee, uint256 baseTxFee ) external onlyVm onlyWhenNotFrozen { uint256 units = _valueToUnits(inflationState.factor, refund); balances[from] = balances[from].add(units); units = units.add(_creditGas(from, communityFund, baseTxFee)); units = units.add(_creditGas(from, feeRecipient, tipTxFee)); units = units.add(_creditGas(from, gatewayFeeRecipient, gatewayFee)); totalSupply_ = totalSupply_.add(units); } function _creditGas( address from, address to, uint256 value ) internal returns (uint256) { if (to == address(0)) { return 0; } uint256 units = _valueToUnits(inflationState.factor, value); balances[to] = balances[to].add(units); emit Transfer(from, to, value); return units; } }
/contracts/interfaces/IExchange.sol
pragma solidity ^0.5.13; interface IExchange { function buy( uint256, uint256, bool ) external returns (uint256); function sell( uint256, uint256, bool ) external returns (uint256); function exchange( uint256, uint256, bool ) external returns (uint256); function setUpdateFrequency(uint256) external; function getBuyTokenAmount(uint256, bool) external view returns (uint256); function getSellTokenAmount(uint256, bool) external view returns (uint256); function getBuyAndSellBuckets(bool) external view returns (uint256, uint256); }
/lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol
pragma solidity ^0.5.0; /** * @dev Interface of the ERC20 standard as defined in the EIP. Does not include * the optional functions; to access them see {ERC20Detailed}. */ interface IERC20 { /** * @dev Returns the amount of tokens in existence. */ function totalSupply() external view returns (uint256); /** * @dev Returns the amount of tokens owned by `account`. */ function balanceOf(address account) external view returns (uint256); /** * @dev Moves `amount` tokens from the caller's account to `recipient`. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transfer(address recipient, uint256 amount) external returns (bool); /** * @dev Returns the remaining number of tokens that `spender` will be * allowed to spend on behalf of `owner` through {transferFrom}. This is * zero by default. * * This value changes when {approve} or {transferFrom} are called. */ function allowance(address owner, address spender) external view returns (uint256); /** * @dev Sets `amount` as the allowance of `spender` over the caller's tokens. * * Returns a boolean value indicating whether the operation succeeded. * * IMPORTANT: Beware that changing an allowance with this method brings the risk * that someone may use both the old and the new allowance by unfortunate * transaction ordering. One possible solution to mitigate this race * condition is to first reduce the spender's allowance to 0 and set the * desired value afterwards: * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 * * Emits an {Approval} event. */ function approve(address spender, uint256 amount) external returns (bool); /** * @dev Moves `amount` tokens from `sender` to `recipient` using the * allowance mechanism. `amount` is then deducted from the caller's * allowance. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transferFrom(address sender, address recipient, uint256 amount) external returns (bool); /** * @dev Emitted when `value` tokens are moved from one account (`from`) to * another (`to`). * * Note that `value` may be zero. */ event Transfer(address indexed from, address indexed to, uint256 value); /** * @dev Emitted when the allowance of a `spender` for an `owner` is set by * a call to {approve}. `value` is the new allowance. */ event Approval(address indexed owner, address indexed spender, uint256 value); }
/lib/openzeppelin-contracts/contracts/ownership/Ownable.sol
pragma solidity ^0.5.0; import "../GSN/Context.sol"; /** * @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. * * 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 Ownable is Context { address private _owner; event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); /** * @dev Initializes the contract setting the deployer as the initial owner. */ constructor () internal { address msgSender = _msgSender(); _owner = msgSender; emit OwnershipTransferred(address(0), msgSender); } /** * @dev Returns the address of the current owner. */ function owner() public view returns (address) { return _owner; } /** * @dev Throws if called by any account other than the owner. */ modifier onlyOwner() { require(isOwner(), "Ownable: caller is not the owner"); _; } /** * @dev Returns true if the caller is the current owner. */ function isOwner() public view returns (bool) { return _msgSender() == _owner; } /** * @dev Leaves the contract without owner. It will not be possible to call * `onlyOwner` functions anymore. Can only be called by the current owner. * * NOTE: Renouncing ownership will leave the contract without an owner, * thereby removing any functionality that is only available to the owner. */ function renounceOwnership() public onlyOwner { emit OwnershipTransferred(_owner, address(0)); _owner = address(0); } /** * @dev Transfers ownership of the contract to a new account (`newOwner`). * Can only be called by the current owner. */ function transferOwnership(address newOwner) public onlyOwner { _transferOwnership(newOwner); } /** * @dev Transfers ownership of the contract to a new account (`newOwner`). */ function _transferOwnership(address newOwner) internal { require(newOwner != address(0), "Ownable: new owner is the zero address"); emit OwnershipTransferred(_owner, newOwner); _owner = newOwner; } }
/lib/openzeppelin-contracts/contracts/math/SafeMath.sol
pragma solidity ^0.5.0; /** * @dev Wrappers over Solidity's arithmetic operations with added overflow * checks. * * Arithmetic operations in Solidity wrap on overflow. This can easily result * in bugs, because programmers usually assume that an overflow raises an * error, which is the standard behavior in high level programming languages. * `SafeMath` restores this intuition by reverting the transaction when an * operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. */ library SafeMath { /** * @dev Returns the addition of two unsigned integers, reverting on * overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, "SafeMath: addition overflow"); return c; } /** * @dev Returns the subtraction of two unsigned integers, reverting on * overflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot overflow. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { return sub(a, b, "SafeMath: subtraction overflow"); } /** * @dev Returns the subtraction of two unsigned integers, reverting with custom message on * overflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot overflow. * * _Available since v2.4.0._ */ function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b <= a, errorMessage); uint256 c = a - b; return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on * overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, "SafeMath: multiplication overflow"); return c; } /** * @dev Returns the integer division of two unsigned integers. Reverts on * division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { return div(a, b, "SafeMath: division by zero"); } /** * @dev Returns the integer division of two unsigned integers. Reverts with custom message on * division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. * * _Available since v2.4.0._ */ function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Solidity only automatically asserts when dividing by 0 require(b > 0, errorMessage); uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * 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). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { return mod(a, b, "SafeMath: modulo by zero"); } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts with custom message 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). * * Requirements: * - The divisor cannot be zero. * * _Available since v2.4.0._ */ function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b != 0, errorMessage); return a % b; } }
/contracts/interfaces/ICeloToken.sol
pragma solidity ^0.5.13; /** * @title This interface describes the non- ERC20 shared interface for all Celo Tokens, and * in the absence of interface inheritance is intended as a companion to IERC20.sol. */ interface ICeloToken { function transferWithComment( address, uint256, string calldata ) external returns (bool); function name() external view returns (string memory); function symbol() external view returns (string memory); function decimals() external view returns (uint8); }
/lib/openzeppelin-contracts/contracts/GSN/Context.sol
pragma solidity ^0.5.0; /* * @dev Provides information about the current execution context, including the * sender of the transaction and its data. While these are generally available * via msg.sender and msg.data, they should not be accessed in such a direct * manner, since when dealing with GSN meta-transactions the account sending and * paying for execution may not be the actual sender (as far as an application * is concerned). * * This contract is only required for intermediate, library-like contracts. */ contract Context { // Empty internal constructor, to prevent people from mistakenly deploying // an instance of this contract, which should be used via inheritance. constructor () internal { } // solhint-disable-previous-line no-empty-blocks function _msgSender() internal view returns (address payable) { return msg.sender; } function _msgData() internal view returns (bytes memory) { this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691 return msg.data; } }
/contracts/interfaces/IStableToken.sol
pragma solidity ^0.5.13; /** * @title This interface describes the functions specific to Celo Stable Tokens, and in the * absence of interface inheritance is intended as a companion to IERC20.sol and ICeloToken.sol. */ interface IStableToken { function mint(address, uint256) external returns (bool); function burn(uint256) external returns (bool); function setInflationParameters(uint256, uint256) external; function valueToUnits(uint256) external view returns (uint256); function unitsToValue(uint256) external view returns (uint256); function getInflationParameters() external view returns ( uint256, uint256, uint256, uint256 ); function getExchangeRegistryId() external view returns (bytes32); // NOTE: duplicated with IERC20.sol, remove once interface inheritance is supported. function balanceOf(address) external view returns (uint256); }
/contracts/interfaces/ISortedOracles.sol
pragma solidity ^0.5.13; import "../common/linkedlists/SortedLinkedListWithMedian.sol"; interface ISortedOracles { function addOracle(address, address) external; function removeOracle( address, address, uint256 ) external; function report( address, uint256, address, address ) external; function removeExpiredReports(address, uint256) external; function isOldestReportExpired(address token) external view returns (bool, address); function numRates(address) external view returns (uint256); function medianRate(address) external view returns (uint256, uint256); function numTimestamps(address) external view returns (uint256); function medianTimestamp(address) external view returns (uint256); function previousMedianRate(address) external view returns (uint256); function getOracles(address) external view returns (address[] memory); function getTimestamps(address token) external view returns ( address[] memory, uint256[] memory, SortedLinkedListWithMedian.MedianRelation[] memory ); }
/contracts/interfaces/IReserve.sol
pragma solidity ^0.5.13; interface IReserve { function setTobinTaxStalenessThreshold(uint256) external; function addToken(address) external returns (bool); function removeToken(address, uint256) external returns (bool); function transferGold(address payable, uint256) external returns (bool); function transferExchangeGold(address payable, uint256) external returns (bool); function transferCollateralAsset( address collateralAsset, address payable to, uint256 value ) external returns (bool); function getReserveGoldBalance() external view returns (uint256); function getUnfrozenReserveGoldBalance() external view returns (uint256); function getOrComputeTobinTax() external returns (uint256, uint256); function getTokens() external view returns (address[] memory); function getReserveRatio() external view returns (uint256); function addExchangeSpender(address) external; function removeExchangeSpender(address, uint256) external; function addSpender(address) external; function removeSpender(address) external; function isStableAsset(address) external view returns (bool); function isCollateralAsset(address) external view returns (bool); function getDailySpendingRatioForCollateralAsset(address collateralAsset) external view returns (uint256); function isExchangeSpender(address exchange) external view returns (bool); function addCollateralAsset(address asset) external returns (bool); }
/contracts/interfaces/IPricingModule.sol
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.5.13; /** * @title Interface for a Mento Pricing Module. * @notice A Mento pricing module represents an exchange relation between a pair of ERC20 assets. */ interface IPricingModule { /** * @notice Returns the output amount and new bucket sizes for a given input amount. * @param tokenInBucketSize Size of the tokenIn bucket. * @param tokenOutBucketSize Size of the tokenOut bucket. * @param spread Spread charged on exchanges. * @param amountIn Amount of tokenIn being paid in. * @return amountOut Amount of tokenOut that will be paid out. */ function getAmountOut( uint256 tokenInBucketSize, uint256 tokenOutBucketSize, uint256 spread, uint256 amountIn ) external view returns (uint256 amountOut); /** * @notice Returns the input amount necessary for a given output amount. * @param tokenInBucketSize Size of the tokenIn bucket. * @param tokenOutBucketSize Size of the tokenOut bucket. * @param spread Spread charged on exchanges. * @param amountOut Amount of tokenIn being paid out. * @return amountIn Amount of tokenOut that would have to be paid in. */ function getAmountIn( uint256 tokenInBucketSize, uint256 tokenOutBucketSize, uint256 spread, uint256 amountOut ) external view returns (uint256 amountIn); /** * @notice Retrieve the name of this pricing module. * @return exchangeName The name of the pricing module. */ function name() external view returns (string memory pricingModuleName); }
/contracts/interfaces/IExchangeProvider.sol
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.5.13; pragma experimental ABIEncoderV2; /** * @title ExchangeProvider interface * @notice The IExchangeProvider interface is the interface that the Broker uses * to communicate with different exchange manager implementations like the BiPoolManager */ interface IExchangeProvider { /** * @notice Exchange - a struct that's used only by UIs (frontends/CLIs) * in order to discover what asset swaps are possible within an * exchange provider. * It's up to the specific exchange provider to convert its internal * representation to this universal struct. This conversion should * only happen in view calls used for discovery. * @param exchangeId The ID of the exchange, used to initiate swaps or get quotes. * @param assets An array of addresses of ERC20 tokens that can be swapped. */ struct Exchange { bytes32 exchangeId; address[] assets; } /** * @notice Get all exchanges supported by the ExchangeProvider. * @return exchanges An array of Exchange structs. */ function getExchanges() external view returns (Exchange[] memory exchanges); /** * @notice Execute a token swap with fixed amountIn * @param exchangeId The id of the exchange to use * @param tokenIn The token to be sold * @param tokenOut The token to be bought * @param amountIn The amount of tokenIn to be sold * @return amountOut The amount of tokenOut to be bought */ function swapIn( bytes32 exchangeId, address tokenIn, address tokenOut, uint256 amountIn ) external returns (uint256 amountOut); /** * @notice Execute a token swap with fixed amountOut * @param exchangeId The id of the exchange to use * @param tokenIn The token to be sold * @param tokenOut The token to be bought * @param amountOut The amount of tokenOut to be bought * @return amountIn The amount of tokenIn to be sold */ function swapOut( bytes32 exchangeId, address tokenIn, address tokenOut, uint256 amountOut ) external returns (uint256 amountIn); /** * @notice Calculate amountOut of tokenOut received for a given amountIn of tokenIn * @param exchangeId The id of the exchange to use * @param tokenIn The token to be sold * @param tokenOut The token to be bought * @param amountIn The amount of tokenIn to be sold * @return amountOut The amount of tokenOut to be bought */ function getAmountOut( bytes32 exchangeId, address tokenIn, address tokenOut, uint256 amountIn ) external view returns (uint256 amountOut); /** * @notice Calculate amountIn of tokenIn needed for a given amountOut of tokenOut * @param exchangeId The id of the exchange to use * @param tokenIn The token to be sold * @param tokenOut The token to be bought * @param amountOut The amount of tokenOut to be bought * @return amountIn The amount of tokenIn to be sold */ function getAmountIn( bytes32 exchangeId, address tokenIn, address tokenOut, uint256 amountOut ) external view returns (uint256 amountIn); }
/contracts/interfaces/IBreakerBox.sol
pragma solidity ^0.5.13; /** * @title Breaker Box Interface * @notice Defines the basic interface for the Breaker Box */ interface IBreakerBox { /** * @dev Used to track additional info about * the current trading mode a specific rate feed ID is in. * LastUpdatedTime helps to check cooldown. * LastUpdatedBlock helps to determine if check should be executed. */ struct TradingModeInfo { uint64 tradingMode; uint64 lastUpdatedTime; uint128 lastUpdatedBlock; } /** * @notice Emitted when a new breaker is added to the breaker box. * @param breaker The address of the new breaker. */ event BreakerAdded(address indexed breaker); /** * @notice Emitted when a breaker is removed from the breaker box. * @param breaker The address of the breaker that was removed. */ event BreakerRemoved(address indexed breaker); /** * @notice Emitted when a breaker is tripped by a rateFeedID. * @param breaker The address of the breaker that was tripped. * @param rateFeedID The address of the rateFeedID. */ event BreakerTripped(address indexed breaker, address indexed rateFeedID); /** * @notice Emitted when a new rateFeedID is added to the breaker box. * @param rateFeedID The address of the rateFeedID that was added. */ event RateFeedAdded(address indexed rateFeedID); /** * @notice Emitted when a rateFeedID is removed from the breaker box. * @param rateFeedID The rateFeedID that was removed. */ event RateFeedRemoved(address indexed rateFeedID); /** * @notice Emitted when the trading mode for a rateFeedID is updated * @param rateFeedID The address of the rataFeedID. * @param tradingMode The new trading mode of the rateFeedID. */ event TradingModeUpdated(address indexed rateFeedID, uint256 tradingMode); /** * @notice Emitted after a reset attempt is successful. * @param rateFeedID The address of the rateFeedID. * @param breaker The address of the breaker. */ event ResetSuccessful(address indexed rateFeedID, address indexed breaker); /** * @notice Emitted after a reset attempt fails when the * rateFeedID fails the breakers reset criteria. * @param rateFeedID The address of the rateFeedID. * @param breaker The address of the breaker. */ event ResetAttemptCriteriaFail(address indexed rateFeedID, address indexed breaker); /** * @notice Emitted after a reset attempt fails when cooldown time has not elapsed. * @param rateFeedID The address of the rateFeedID. * @param breaker The address of the breaker. */ event ResetAttemptNotCool(address indexed rateFeedID, address indexed breaker); /** * @notice Emitted when the sortedOracles address is updated. * @param newSortedOracles The address of the new sortedOracles. */ event SortedOraclesUpdated(address indexed newSortedOracles); /** * @notice Retrives an ordered array of all breaker addresses. */ function getBreakers() external view returns (address[] memory); /** * @notice Checks if a breaker with the specified address has been added to the breaker box. * @param breaker The address of the breaker to check; * @return A bool indicating whether or not the breaker has been added. */ function isBreaker(address breaker) external view returns (bool); /** * @notice Checks breakers for the rateFeedID and sets correct trading mode * if any breakers are tripped or need to be reset. * @param rateFeedID The registryId of the rateFeedID to run checks for. */ function checkAndSetBreakers(address rateFeedID) external; /** * @notice Gets the trading mode for the specified rateFeedID. * @param rateFeedID The address of the rateFeedID to retrieve the trading mode for. */ function getRateFeedTradingMode(address rateFeedID) external view returns (uint256 tradingMode); }
/contracts/interfaces/IBiPoolManager.sol
// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.5.13; pragma experimental ABIEncoderV2; import { IPricingModule } from "./IPricingModule.sol"; import { FixidityLib } from "../common/FixidityLib.sol"; /** * @title BiPool Manager interface * @notice The two asset pool manager is responsible for * managing the state of all two-asset virtual pools. */ interface IBiPoolManager { /** * @title PoolExchange * @notice The PoolExchange is a type of asset exchange that * that implements an AMM with two virtual buckets. */ struct PoolExchange { address asset0; address asset1; IPricingModule pricingModule; uint256 bucket0; uint256 bucket1; uint256 lastBucketUpdate; PoolConfig config; } /** * @notice Variables related to bucket updates and sizing. * @dev Broken down into a separate struct because the compiler * version doesn't support structs with too many members. * Sad reacts only. */ struct PoolConfig { FixidityLib.Fraction spread; address referenceRateFeedID; // rateFeedID of the price that this pool follows (i.e. it's reference rate) uint256 referenceRateResetFrequency; uint256 minimumReports; uint256 stablePoolResetSize; } /** * @notice Emitted when a new PoolExchange has been created. * @param exchangeId The id of the new PoolExchange * @param asset0 The address of asset0 * @param asset1 The address of asset1 * @param pricingModule the address of the pricingModule */ event ExchangeCreated( bytes32 indexed exchangeId, address indexed asset0, address indexed asset1, address pricingModule ); /** * @notice Emitted when a PoolExchange has been destroyed. * @param exchangeId The id of the PoolExchange * @param asset0 The address of asset0 * @param asset1 The address of asset1 * @param pricingModule the address of the pricingModule */ event ExchangeDestroyed( bytes32 indexed exchangeId, address indexed asset0, address indexed asset1, address pricingModule ); /** * @notice Emitted when the broker address is updated. * @param newBroker The address of the new broker. */ event BrokerUpdated(address indexed newBroker); /** * @notice Emitted when the reserve address is updated. * @param newReserve The address of the new reserve. */ event ReserveUpdated(address indexed newReserve); /** * @notice Emitted when the breakerBox address is updated. * @param newBreakerBox The address of the new breakerBox. */ event BreakerBoxUpdated(address newBreakerBox); /** * @notice Emitted when the sortedOracles address is updated. * @param newSortedOracles The address of the new sortedOracles. */ event SortedOraclesUpdated(address indexed newSortedOracles); /** * @notice Emitted when the buckets for a specified exchange are updated. * @param exchangeId The id of the exchange * @param bucket0 The new bucket0 size * @param bucket1 The new bucket1 size */ event BucketsUpdated(bytes32 indexed exchangeId, uint256 bucket0, uint256 bucket1); /** * @notice Retrieves the pool with the specified exchangeId. * @param exchangeId The id of the pool to be retrieved. * @return exchange The PoolExchange with that ID. */ function getPoolExchange(bytes32 exchangeId) external view returns (PoolExchange memory exchange); /** * @notice Get all exchange IDs. * @return exchangeIds List of the exchangeIds. */ function getExchangeIds() external view returns (bytes32[] memory exchangeIds); /** * @notice Create a PoolExchange with the provided data. * @param exchange The PoolExchange to be created. * @return exchangeId The id of the exchange. */ function createExchange(PoolExchange calldata exchange) external returns (bytes32 exchangeId); /** * @notice Delete a PoolExchange. * @param exchangeId The PoolExchange to be created. * @param exchangeIdIndex The index of the exchangeId in the exchangeIds array. * @return destroyed - true on successful delition. */ function destroyExchange(bytes32 exchangeId, uint256 exchangeIdIndex) external returns (bool destroyed); }
/contracts/common/linkedlists/SortedLinkedListWithMedian.sol
pragma solidity ^0.5.13; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; import "./LinkedList.sol"; import "./SortedLinkedList.sol"; /** * @title Maintains a sorted list of unsigned ints keyed by bytes32. */ library SortedLinkedListWithMedian { using SafeMath for uint256; using SortedLinkedList for SortedLinkedList.List; enum MedianAction { None, Lesser, Greater } enum MedianRelation { Undefined, Lesser, Greater, Equal } struct List { SortedLinkedList.List list; bytes32 median; mapping(bytes32 => MedianRelation) relation; } /** * @notice Inserts an element into a doubly linked list. * @param list A storage pointer to the underlying list. * @param key The key of the element to insert. * @param value The element value. * @param lesserKey The key of the element less than the element to insert. * @param greaterKey The key of the element greater than the element to insert. */ function insert( List storage list, bytes32 key, uint256 value, bytes32 lesserKey, bytes32 greaterKey ) internal { list.list.insert(key, value, lesserKey, greaterKey); LinkedList.Element storage element = list.list.list.elements[key]; MedianAction action = MedianAction.None; if (list.list.list.numElements == 1) { list.median = key; list.relation[key] = MedianRelation.Equal; } else if (list.list.list.numElements % 2 == 1) { // When we have an odd number of elements, and the element that we inserted is less than // the previous median, we need to slide the median down one element, since we had previously // selected the greater of the two middle elements. if (element.previousKey == bytes32(0) || list.relation[element.previousKey] == MedianRelation.Lesser) { action = MedianAction.Lesser; list.relation[key] = MedianRelation.Lesser; } else { list.relation[key] = MedianRelation.Greater; } } else { // When we have an even number of elements, and the element that we inserted is greater than // the previous median, we need to slide the median up one element, since we always select // the greater of the two middle elements. if (element.nextKey == bytes32(0) || list.relation[element.nextKey] == MedianRelation.Greater) { action = MedianAction.Greater; list.relation[key] = MedianRelation.Greater; } else { list.relation[key] = MedianRelation.Lesser; } } updateMedian(list, action); } /** * @notice Removes an element from the doubly linked list. * @param list A storage pointer to the underlying list. * @param key The key of the element to remove. */ function remove(List storage list, bytes32 key) internal { MedianAction action = MedianAction.None; if (list.list.list.numElements == 0) { list.median = bytes32(0); } else if (list.list.list.numElements % 2 == 0) { // When we have an even number of elements, we always choose the higher of the two medians. // Thus, if the element we're removing is greaterKey than or equal to the median we need to // slide the median left by one. if (list.relation[key] == MedianRelation.Greater || list.relation[key] == MedianRelation.Equal) { action = MedianAction.Lesser; } } else { // When we don't have an even number of elements, we just choose the median value. // Thus, if the element we're removing is less than or equal to the median, we need to slide // median right by one. if (list.relation[key] == MedianRelation.Lesser || list.relation[key] == MedianRelation.Equal) { action = MedianAction.Greater; } } updateMedian(list, action); list.list.remove(key); } /** * @notice Updates an element in the list. * @param list A storage pointer to the underlying list. * @param key The element key. * @param value The element value. * @param lesserKey The key of the element will be just left of `key` after the update. * @param greaterKey The key of the element will be just right of `key` after the update. * @dev Note that only one of "lesserKey" or "greaterKey" needs to be correct to reduce friction. */ function update( List storage list, bytes32 key, uint256 value, bytes32 lesserKey, bytes32 greaterKey ) internal { remove(list, key); insert(list, key, value, lesserKey, greaterKey); } /** * @notice Inserts an element at the tail of the doubly linked list. * @param list A storage pointer to the underlying list. * @param key The key of the element to insert. */ function push(List storage list, bytes32 key) internal { insert(list, key, 0, bytes32(0), list.list.list.tail); } /** * @notice Removes N elements from the head of the list and returns their keys. * @param list A storage pointer to the underlying list. * @param n The number of elements to pop. * @return The keys of the popped elements. */ function popN(List storage list, uint256 n) internal returns (bytes32[] memory) { require(n <= list.list.list.numElements, "not enough elements"); bytes32[] memory keys = new bytes32[](n); for (uint256 i = 0; i < n; i = i.add(1)) { bytes32 key = list.list.list.head; keys[i] = key; remove(list, key); } return keys; } /** * @notice Returns whether or not a particular key is present in the sorted list. * @param list A storage pointer to the underlying list. * @param key The element key. * @return Whether or not the key is in the sorted list. */ function contains(List storage list, bytes32 key) internal view returns (bool) { return list.list.contains(key); } /** * @notice Returns the value for a particular key in the sorted list. * @param list A storage pointer to the underlying list. * @param key The element key. * @return The element value. */ function getValue(List storage list, bytes32 key) internal view returns (uint256) { return list.list.values[key]; } /** * @notice Returns the median value of the sorted list. * @param list A storage pointer to the underlying list. * @return The median value. */ function getMedianValue(List storage list) internal view returns (uint256) { return getValue(list, list.median); } /** * @notice Returns the key of the first element in the list. * @param list A storage pointer to the underlying list. * @return The key of the first element in the list. */ function getHead(List storage list) internal view returns (bytes32) { return list.list.list.head; } /** * @notice Returns the key of the median element in the list. * @param list A storage pointer to the underlying list. * @return The key of the median element in the list. */ function getMedian(List storage list) internal view returns (bytes32) { return list.median; } /** * @notice Returns the key of the last element in the list. * @param list A storage pointer to the underlying list. * @return The key of the last element in the list. */ function getTail(List storage list) internal view returns (bytes32) { return list.list.list.tail; } /** * @notice Returns the number of elements in the list. * @param list A storage pointer to the underlying list. * @return The number of elements in the list. */ function getNumElements(List storage list) internal view returns (uint256) { return list.list.list.numElements; } /** * @notice Gets all elements from the doubly linked list. * @param list A storage pointer to the underlying list. * @return Array of all keys in the list. * @return Values corresponding to keys, which will be ordered largest to smallest. * @return Array of relations to median of corresponding list elements. */ function getElements(List storage list) internal view returns ( bytes32[] memory, uint256[] memory, MedianRelation[] memory ) { bytes32[] memory keys = getKeys(list); uint256[] memory values = new uint256[](keys.length); MedianRelation[] memory relations = new MedianRelation[](keys.length); for (uint256 i = 0; i < keys.length; i = i.add(1)) { values[i] = list.list.values[keys[i]]; relations[i] = list.relation[keys[i]]; } return (keys, values, relations); } /** * @notice Gets all element keys from the doubly linked list. * @param list A storage pointer to the underlying list. * @return All element keys from head to tail. */ function getKeys(List storage list) internal view returns (bytes32[] memory) { return list.list.getKeys(); } /** * @notice Moves the median pointer right or left of its current value. * @param list A storage pointer to the underlying list. * @param action Which direction to move the median pointer. */ function updateMedian(List storage list, MedianAction action) private { LinkedList.Element storage previousMedian = list.list.list.elements[list.median]; if (action == MedianAction.Lesser) { list.relation[list.median] = MedianRelation.Greater; list.median = previousMedian.previousKey; } else if (action == MedianAction.Greater) { list.relation[list.median] = MedianRelation.Lesser; list.median = previousMedian.nextKey; } list.relation[list.median] = MedianRelation.Equal; } }
/contracts/common/linkedlists/SortedLinkedList.sol
pragma solidity ^0.5.13; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; import "./LinkedList.sol"; /** * @title Maintains a sorted list of unsigned ints keyed by bytes32. */ library SortedLinkedList { using SafeMath for uint256; using LinkedList for LinkedList.List; struct List { LinkedList.List list; mapping(bytes32 => uint256) values; } /** * @notice Inserts an element into a doubly linked list. * @param list A storage pointer to the underlying list. * @param key The key of the element to insert. * @param value The element value. * @param lesserKey The key of the element less than the element to insert. * @param greaterKey The key of the element greater than the element to insert. */ function insert( List storage list, bytes32 key, uint256 value, bytes32 lesserKey, bytes32 greaterKey ) internal { require(key != bytes32(0) && key != lesserKey && key != greaterKey && !contains(list, key), "invalid key"); require( (lesserKey != bytes32(0) || greaterKey != bytes32(0)) || list.list.numElements == 0, "greater and lesser key zero" ); require(contains(list, lesserKey) || lesserKey == bytes32(0), "invalid lesser key"); require(contains(list, greaterKey) || greaterKey == bytes32(0), "invalid greater key"); (lesserKey, greaterKey) = getLesserAndGreater(list, value, lesserKey, greaterKey); list.list.insert(key, lesserKey, greaterKey); list.values[key] = value; } /** * @notice Removes an element from the doubly linked list. * @param list A storage pointer to the underlying list. * @param key The key of the element to remove. */ function remove(List storage list, bytes32 key) internal { list.list.remove(key); list.values[key] = 0; } /** * @notice Updates an element in the list. * @param list A storage pointer to the underlying list. * @param key The element key. * @param value The element value. * @param lesserKey The key of the element will be just left of `key` after the update. * @param greaterKey The key of the element will be just right of `key` after the update. * @dev Note that only one of "lesserKey" or "greaterKey" needs to be correct to reduce friction. */ function update( List storage list, bytes32 key, uint256 value, bytes32 lesserKey, bytes32 greaterKey ) internal { remove(list, key); insert(list, key, value, lesserKey, greaterKey); } /** * @notice Inserts an element at the tail of the doubly linked list. * @param list A storage pointer to the underlying list. * @param key The key of the element to insert. */ function push(List storage list, bytes32 key) internal { insert(list, key, 0, bytes32(0), list.list.tail); } /** * @notice Removes N elements from the head of the list and returns their keys. * @param list A storage pointer to the underlying list. * @param n The number of elements to pop. * @return The keys of the popped elements. */ function popN(List storage list, uint256 n) internal returns (bytes32[] memory) { require(n <= list.list.numElements, "not enough elements"); bytes32[] memory keys = new bytes32[](n); for (uint256 i = 0; i < n; i = i.add(1)) { bytes32 key = list.list.head; keys[i] = key; remove(list, key); } return keys; } /** * @notice Returns whether or not a particular key is present in the sorted list. * @param list A storage pointer to the underlying list. * @param key The element key. * @return Whether or not the key is in the sorted list. */ function contains(List storage list, bytes32 key) internal view returns (bool) { return list.list.contains(key); } /** * @notice Returns the value for a particular key in the sorted list. * @param list A storage pointer to the underlying list. * @param key The element key. * @return The element value. */ function getValue(List storage list, bytes32 key) internal view returns (uint256) { return list.values[key]; } /** * @notice Gets all elements from the doubly linked list. * @param list A storage pointer to the underlying list. * @return Array of all keys in the list. * @return Values corresponding to keys, which will be ordered largest to smallest. */ function getElements(List storage list) internal view returns (bytes32[] memory, uint256[] memory) { bytes32[] memory keys = getKeys(list); uint256[] memory values = new uint256[](keys.length); for (uint256 i = 0; i < keys.length; i = i.add(1)) { values[i] = list.values[keys[i]]; } return (keys, values); } /** * @notice Gets all element keys from the doubly linked list. * @param list A storage pointer to the underlying list. * @return All element keys from head to tail. */ function getKeys(List storage list) internal view returns (bytes32[] memory) { return list.list.getKeys(); } /** * @notice Returns first N greatest elements of the list. * @param list A storage pointer to the underlying list. * @param n The number of elements to return. * @return The keys of the first n elements. * @dev Reverts if n is greater than the number of elements in the list. */ function headN(List storage list, uint256 n) internal view returns (bytes32[] memory) { return list.list.headN(n); } /** * @notice Returns the keys of the elements greaterKey than and less than the provided value. * @param list A storage pointer to the underlying list. * @param value The element value. * @param lesserKey The key of the element which could be just left of the new value. * @param greaterKey The key of the element which could be just right of the new value. * @return The correct lesserKey keys. * @return The correct greaterKey keys. */ function getLesserAndGreater( List storage list, uint256 value, bytes32 lesserKey, bytes32 greaterKey ) private view returns (bytes32, bytes32) { // Check for one of the following conditions and fail if none are met: // 1. The value is less than the current lowest value // 2. The value is greater than the current greatest value // 3. The value is just greater than the value for `lesserKey` // 4. The value is just less than the value for `greaterKey` if (lesserKey == bytes32(0) && isValueBetween(list, value, lesserKey, list.list.tail)) { return (lesserKey, list.list.tail); } else if (greaterKey == bytes32(0) && isValueBetween(list, value, list.list.head, greaterKey)) { return (list.list.head, greaterKey); } else if ( lesserKey != bytes32(0) && isValueBetween(list, value, lesserKey, list.list.elements[lesserKey].nextKey) ) { return (lesserKey, list.list.elements[lesserKey].nextKey); } else if ( greaterKey != bytes32(0) && isValueBetween(list, value, list.list.elements[greaterKey].previousKey, greaterKey) ) { return (list.list.elements[greaterKey].previousKey, greaterKey); } else { require(false, "get lesser and greater failure"); } } /** * @notice Returns whether or not a given element is between two other elements. * @param list A storage pointer to the underlying list. * @param value The element value. * @param lesserKey The key of the element whose value should be lesserKey. * @param greaterKey The key of the element whose value should be greaterKey. * @return True if the given element is between the two other elements. */ function isValueBetween( List storage list, uint256 value, bytes32 lesserKey, bytes32 greaterKey ) private view returns (bool) { bool isLesser = lesserKey == bytes32(0) || list.values[lesserKey] <= value; bool isGreater = greaterKey == bytes32(0) || list.values[greaterKey] >= value; return isLesser && isGreater; } }
/contracts/common/linkedlists/LinkedList.sol
pragma solidity ^0.5.13; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; /** * @title Maintains a doubly linked list keyed by bytes32. * @dev Following the `next` pointers will lead you to the head, rather than the tail. */ library LinkedList { using SafeMath for uint256; struct Element { bytes32 previousKey; bytes32 nextKey; bool exists; } struct List { bytes32 head; bytes32 tail; uint256 numElements; mapping(bytes32 => Element) elements; } /** * @notice Inserts an element into a doubly linked list. * @param list A storage pointer to the underlying list. * @param key The key of the element to insert. * @param previousKey The key of the element that comes before the element to insert. * @param nextKey The key of the element that comes after the element to insert. */ function insert( List storage list, bytes32 key, bytes32 previousKey, bytes32 nextKey ) internal { require(key != bytes32(0), "Key must be defined"); require(!contains(list, key), "Can't insert an existing element"); require(previousKey != key && nextKey != key, "Key cannot be the same as previousKey or nextKey"); Element storage element = list.elements[key]; element.exists = true; if (list.numElements == 0) { list.tail = key; list.head = key; } else { require(previousKey != bytes32(0) || nextKey != bytes32(0), "Either previousKey or nextKey must be defined"); element.previousKey = previousKey; element.nextKey = nextKey; if (previousKey != bytes32(0)) { require(contains(list, previousKey), "If previousKey is defined, it must exist in the list"); Element storage previousElement = list.elements[previousKey]; require(previousElement.nextKey == nextKey, "previousKey must be adjacent to nextKey"); previousElement.nextKey = key; } else { list.tail = key; } if (nextKey != bytes32(0)) { require(contains(list, nextKey), "If nextKey is defined, it must exist in the list"); Element storage nextElement = list.elements[nextKey]; require(nextElement.previousKey == previousKey, "previousKey must be adjacent to nextKey"); nextElement.previousKey = key; } else { list.head = key; } } list.numElements = list.numElements.add(1); } /** * @notice Inserts an element at the tail of the doubly linked list. * @param list A storage pointer to the underlying list. * @param key The key of the element to insert. */ function push(List storage list, bytes32 key) internal { insert(list, key, bytes32(0), list.tail); } /** * @notice Removes an element from the doubly linked list. * @param list A storage pointer to the underlying list. * @param key The key of the element to remove. */ function remove(List storage list, bytes32 key) internal { Element storage element = list.elements[key]; require(key != bytes32(0) && contains(list, key), "key not in list"); if (element.previousKey != bytes32(0)) { Element storage previousElement = list.elements[element.previousKey]; previousElement.nextKey = element.nextKey; } else { list.tail = element.nextKey; } if (element.nextKey != bytes32(0)) { Element storage nextElement = list.elements[element.nextKey]; nextElement.previousKey = element.previousKey; } else { list.head = element.previousKey; } delete list.elements[key]; list.numElements = list.numElements.sub(1); } /** * @notice Updates an element in the list. * @param list A storage pointer to the underlying list. * @param key The element key. * @param previousKey The key of the element that comes before the updated element. * @param nextKey The key of the element that comes after the updated element. */ function update( List storage list, bytes32 key, bytes32 previousKey, bytes32 nextKey ) internal { require(key != bytes32(0) && key != previousKey && key != nextKey && contains(list, key), "key on in list"); remove(list, key); insert(list, key, previousKey, nextKey); } /** * @notice Returns whether or not a particular key is present in the sorted list. * @param list A storage pointer to the underlying list. * @param key The element key. * @return Whether or not the key is in the sorted list. */ function contains(List storage list, bytes32 key) internal view returns (bool) { return list.elements[key].exists; } /** * @notice Returns the keys of the N elements at the head of the list. * @param list A storage pointer to the underlying list. * @param n The number of elements to return. * @return The keys of the N elements at the head of the list. * @dev Reverts if n is greater than the number of elements in the list. */ function headN(List storage list, uint256 n) internal view returns (bytes32[] memory) { require(n <= list.numElements, "not enough elements"); bytes32[] memory keys = new bytes32[](n); bytes32 key = list.head; for (uint256 i = 0; i < n; i = i.add(1)) { keys[i] = key; key = list.elements[key].previousKey; } return keys; } /** * @notice Gets all element keys from the doubly linked list. * @param list A storage pointer to the underlying list. * @return All element keys from head to tail. */ function getKeys(List storage list) internal view returns (bytes32[] memory) { return headN(list, list.numElements); } }
/contracts/common/interfaces/IRegistry.sol
pragma solidity ^0.5.13; interface IRegistry { function setAddressFor(string calldata, address) external; function getAddressForOrDie(bytes32) external view returns (address); function getAddressFor(bytes32) external view returns (address); function getAddressForStringOrDie(string calldata identifier) external view returns (address); function getAddressForString(string calldata identifier) external view returns (address); function isOneOf(bytes32[] calldata, address) external view returns (bool); }
/contracts/common/interfaces/IFreezer.sol
pragma solidity ^0.5.13; interface IFreezer { function isFrozen(address) external view returns (bool); }
/contracts/common/interfaces/ICeloVersionedContract.sol
pragma solidity ^0.5.13; interface ICeloVersionedContract { /** * @notice Returns the storage, major, minor, and patch version of the contract. * @return Storage version of the contract. * @return Major version of the contract. * @return Minor version of the contract. * @return Patch version of the contract. */ function getVersionNumber() external pure returns ( uint256, uint256, uint256, uint256 ); }
/contracts/common/UsingRegistry.sol
pragma solidity ^0.5.13; import "openzeppelin-solidity/contracts/ownership/Ownable.sol"; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; import "./interfaces/IFreezer.sol"; import "./interfaces/IRegistry.sol"; import "../interfaces/IExchange.sol"; import "../interfaces/IReserve.sol"; import "../interfaces/ISortedOracles.sol"; import "../interfaces/IStableToken.sol"; contract UsingRegistry is Ownable { event RegistrySet(address indexed registryAddress); // solhint-disable state-visibility bytes32 constant ACCOUNTS_REGISTRY_ID = keccak256(abi.encodePacked("Accounts")); bytes32 constant ATTESTATIONS_REGISTRY_ID = keccak256(abi.encodePacked("Attestations")); bytes32 constant DOWNTIME_SLASHER_REGISTRY_ID = keccak256(abi.encodePacked("DowntimeSlasher")); bytes32 constant DOUBLE_SIGNING_SLASHER_REGISTRY_ID = keccak256(abi.encodePacked("DoubleSigningSlasher")); bytes32 constant ELECTION_REGISTRY_ID = keccak256(abi.encodePacked("Election")); bytes32 constant EXCHANGE_REGISTRY_ID = keccak256(abi.encodePacked("Exchange")); bytes32 constant FEE_CURRENCY_WHITELIST_REGISTRY_ID = keccak256(abi.encodePacked("FeeCurrencyWhitelist")); bytes32 constant FREEZER_REGISTRY_ID = keccak256(abi.encodePacked("Freezer")); bytes32 constant GOLD_TOKEN_REGISTRY_ID = keccak256(abi.encodePacked("GoldToken")); bytes32 constant GOVERNANCE_REGISTRY_ID = keccak256(abi.encodePacked("Governance")); bytes32 constant GOVERNANCE_SLASHER_REGISTRY_ID = keccak256(abi.encodePacked("GovernanceSlasher")); bytes32 constant LOCKED_GOLD_REGISTRY_ID = keccak256(abi.encodePacked("LockedGold")); bytes32 constant RESERVE_REGISTRY_ID = keccak256(abi.encodePacked("Reserve")); bytes32 constant RANDOM_REGISTRY_ID = keccak256(abi.encodePacked("Random")); bytes32 constant SORTED_ORACLES_REGISTRY_ID = keccak256(abi.encodePacked("SortedOracles")); bytes32 constant STABLE_TOKEN_REGISTRY_ID = keccak256(abi.encodePacked("StableToken")); bytes32 constant VALIDATORS_REGISTRY_ID = keccak256(abi.encodePacked("Validators")); // solhint-enable state-visibility IRegistry public registry; modifier onlyRegisteredContract(bytes32 identifierHash) { require(registry.getAddressForOrDie(identifierHash) == msg.sender, "only registered contract"); _; } modifier onlyRegisteredContracts(bytes32[] memory identifierHashes) { require(registry.isOneOf(identifierHashes, msg.sender), "only registered contracts"); _; } /** * @notice Updates the address pointing to a Registry contract. * @param registryAddress The address of a registry contract for routing to other contracts. */ function setRegistry(address registryAddress) public onlyOwner { require(registryAddress != address(0), "Cannot register the null address"); registry = IRegistry(registryAddress); emit RegistrySet(registryAddress); } function getExchange() internal view returns (IExchange) { return IExchange(registry.getAddressForOrDie(EXCHANGE_REGISTRY_ID)); } function getFreezer() internal view returns (IFreezer) { return IFreezer(registry.getAddressForOrDie(FREEZER_REGISTRY_ID)); } function getGoldToken() internal view returns (IERC20) { return IERC20(registry.getAddressForOrDie(GOLD_TOKEN_REGISTRY_ID)); } function getReserve() internal view returns (IReserve) { return IReserve(registry.getAddressForOrDie(RESERVE_REGISTRY_ID)); } function getSortedOracles() internal view returns (ISortedOracles) { return ISortedOracles(registry.getAddressForOrDie(SORTED_ORACLES_REGISTRY_ID)); } function getStableToken() internal view returns (IStableToken) { return IStableToken(registry.getAddressForOrDie(STABLE_TOKEN_REGISTRY_ID)); } }
/contracts/common/UsingPrecompiles.sol
// solhint-disable state-visibility pragma solidity ^0.5.13; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; import "./interfaces/ICeloVersionedContract.sol"; contract UsingPrecompiles { using SafeMath for uint256; address constant TRANSFER = address(0xff - 2); address constant FRACTION_MUL = address(0xff - 3); address constant PROOF_OF_POSSESSION = address(0xff - 4); address constant GET_VALIDATOR = address(0xff - 5); address constant NUMBER_VALIDATORS = address(0xff - 6); address constant EPOCH_SIZE = address(0xff - 7); address constant BLOCK_NUMBER_FROM_HEADER = address(0xff - 8); address constant HASH_HEADER = address(0xff - 9); address constant GET_PARENT_SEAL_BITMAP = address(0xff - 10); address constant GET_VERIFIED_SEAL_BITMAP = address(0xff - 11); /** * @notice calculate a * b^x for fractions a, b to `decimals` precision * @param aNumerator Numerator of first fraction * @param aDenominator Denominator of first fraction * @param bNumerator Numerator of exponentiated fraction * @param bDenominator Denominator of exponentiated fraction * @param exponent exponent to raise b to * @param _decimals precision * @return Numerator of the computed quantity (not reduced). * @return Denominator of the computed quantity (not reduced). */ function fractionMulExp( uint256 aNumerator, uint256 aDenominator, uint256 bNumerator, uint256 bDenominator, uint256 exponent, uint256 _decimals ) public view returns (uint256, uint256) { require(aDenominator != 0 && bDenominator != 0, "a denominator is zero"); uint256 returnNumerator; uint256 returnDenominator; bool success; bytes memory out; (success, out) = FRACTION_MUL.staticcall( abi.encodePacked(aNumerator, aDenominator, bNumerator, bDenominator, exponent, _decimals) ); require(success, "error calling fractionMulExp precompile"); returnNumerator = getUint256FromBytes(out, 0); returnDenominator = getUint256FromBytes(out, 32); return (returnNumerator, returnDenominator); } /** * @notice Returns the current epoch size in blocks. * @return The current epoch size in blocks. */ function getEpochSize() public view returns (uint256) { bytes memory out; bool success; (success, out) = EPOCH_SIZE.staticcall(abi.encodePacked()); require(success, "error calling getEpochSize precompile"); return getUint256FromBytes(out, 0); } /** * @notice Returns the epoch number at a block. * @param blockNumber Block number where epoch number is calculated. * @return Epoch number. */ function getEpochNumberOfBlock(uint256 blockNumber) public view returns (uint256) { return epochNumberOfBlock(blockNumber, getEpochSize()); } /** * @notice Returns the epoch number at a block. * @return Current epoch number. */ function getEpochNumber() public view returns (uint256) { return getEpochNumberOfBlock(block.number); } /** * @notice Returns the epoch number at a block. * @param blockNumber Block number where epoch number is calculated. * @param epochSize The epoch size in blocks. * @return Epoch number. */ function epochNumberOfBlock(uint256 blockNumber, uint256 epochSize) internal pure returns (uint256) { // Follows GetEpochNumber from celo-blockchain/blob/master/consensus/istanbul/utils.go uint256 epochNumber = blockNumber / epochSize; if (blockNumber % epochSize == 0) { return epochNumber; } else { return epochNumber.add(1); } } /** * @notice Gets a validator address from the current validator set. * @param index Index of requested validator in the validator set. * @return Address of validator at the requested index. */ function validatorSignerAddressFromCurrentSet(uint256 index) public view returns (address) { bytes memory out; bool success; (success, out) = GET_VALIDATOR.staticcall(abi.encodePacked(index, uint256(block.number))); require(success, "error calling validatorSignerAddressFromCurrentSet precompile"); return address(getUint256FromBytes(out, 0)); } /** * @notice Gets a validator address from the validator set at the given block number. * @param index Index of requested validator in the validator set. * @param blockNumber Block number to retrieve the validator set from. * @return Address of validator at the requested index. */ function validatorSignerAddressFromSet(uint256 index, uint256 blockNumber) public view returns (address) { bytes memory out; bool success; (success, out) = GET_VALIDATOR.staticcall(abi.encodePacked(index, blockNumber)); require(success, "error calling validatorSignerAddressFromSet precompile"); return address(getUint256FromBytes(out, 0)); } /** * @notice Gets the size of the current elected validator set. * @return Size of the current elected validator set. */ function numberValidatorsInCurrentSet() public view returns (uint256) { bytes memory out; bool success; (success, out) = NUMBER_VALIDATORS.staticcall(abi.encodePacked(uint256(block.number))); require(success, "error calling numberValidatorsInCurrentSet precompile"); return getUint256FromBytes(out, 0); } /** * @notice Gets the size of the validator set that must sign the given block number. * @param blockNumber Block number to retrieve the validator set from. * @return Size of the validator set. */ function numberValidatorsInSet(uint256 blockNumber) public view returns (uint256) { bytes memory out; bool success; (success, out) = NUMBER_VALIDATORS.staticcall(abi.encodePacked(blockNumber)); require(success, "error calling numberValidatorsInSet precompile"); return getUint256FromBytes(out, 0); } /** * @notice Checks a BLS proof of possession. * @param sender The address signed by the BLS key to generate the proof of possession. * @param blsKey The BLS public key that the validator is using for consensus, should pass proof * of possession. 48 bytes. * @param blsPop The BLS public key proof-of-possession, which consists of a signature on the * account address. 96 bytes. * @return True upon success. */ function checkProofOfPossession( address sender, bytes memory blsKey, bytes memory blsPop ) public view returns (bool) { bool success; (success, ) = PROOF_OF_POSSESSION.staticcall(abi.encodePacked(sender, blsKey, blsPop)); return success; } /** * @notice Parses block number out of header. * @param header RLP encoded header * @return Block number. */ function getBlockNumberFromHeader(bytes memory header) public view returns (uint256) { bytes memory out; bool success; (success, out) = BLOCK_NUMBER_FROM_HEADER.staticcall(abi.encodePacked(header)); require(success, "error calling getBlockNumberFromHeader precompile"); return getUint256FromBytes(out, 0); } /** * @notice Computes hash of header. * @param header RLP encoded header * @return Header hash. */ function hashHeader(bytes memory header) public view returns (bytes32) { bytes memory out; bool success; (success, out) = HASH_HEADER.staticcall(abi.encodePacked(header)); require(success, "error calling hashHeader precompile"); return getBytes32FromBytes(out, 0); } /** * @notice Gets the parent seal bitmap from the header at the given block number. * @param blockNumber Block number to retrieve. Must be within 4 epochs of the current number. * @return Bitmap parent seal with set bits at indices corresponding to signing validators. */ function getParentSealBitmap(uint256 blockNumber) public view returns (bytes32) { bytes memory out; bool success; (success, out) = GET_PARENT_SEAL_BITMAP.staticcall(abi.encodePacked(blockNumber)); require(success, "error calling getParentSealBitmap precompile"); return getBytes32FromBytes(out, 0); } /** * @notice Verifies the BLS signature on the header and returns the seal bitmap. * The validator set used for verification is retrieved based on the parent hash field of the * header. If the parent hash is not in the blockchain, verification fails. * @param header RLP encoded header * @return Bitmap parent seal with set bits at indices correspoinding to signing validators. */ function getVerifiedSealBitmapFromHeader(bytes memory header) public view returns (bytes32) { bytes memory out; bool success; (success, out) = GET_VERIFIED_SEAL_BITMAP.staticcall(abi.encodePacked(header)); require(success, "error calling getVerifiedSealBitmapFromHeader precompile"); return getBytes32FromBytes(out, 0); } /** * @notice Converts bytes to uint256. * @param bs byte[] data * @param start offset into byte data to convert * @return uint256 data */ function getUint256FromBytes(bytes memory bs, uint256 start) internal pure returns (uint256) { return uint256(getBytes32FromBytes(bs, start)); } /** * @notice Converts bytes to bytes32. * @param bs byte[] data * @param start offset into byte data to convert * @return bytes32 data */ function getBytes32FromBytes(bytes memory bs, uint256 start) internal pure returns (bytes32) { require(bs.length >= start.add(32), "slicing out of range"); bytes32 x; // solhint-disable-next-line no-inline-assembly assembly { x := mload(add(bs, add(start, 32))) } return x; } /** * @notice Returns the minimum number of required signers for a given block number. * @dev Computed in celo-blockchain as int(math.Ceil(float64(2*valSet.Size()) / 3)) */ function minQuorumSize(uint256 blockNumber) public view returns (uint256) { return numberValidatorsInSet(blockNumber).mul(2).add(2).div(3); } /** * @notice Computes byzantine quorum from current validator set size * @return Byzantine quorum of validators. */ function minQuorumSizeInCurrentSet() public view returns (uint256) { return minQuorumSize(block.number); } }
Contract ABI
[{"type":"constructor","stateMutability":"nonpayable","payable":false,"inputs":[{"type":"bool","name":"test","internalType":"bool"}]},{"type":"event","name":"BreakerBoxUpdated","inputs":[{"type":"address","name":"newBreakerBox","internalType":"address","indexed":false}],"anonymous":false},{"type":"event","name":"BrokerUpdated","inputs":[{"type":"address","name":"newBroker","internalType":"address","indexed":true}],"anonymous":false},{"type":"event","name":"BucketsUpdated","inputs":[{"type":"bytes32","name":"exchangeId","internalType":"bytes32","indexed":true},{"type":"uint256","name":"bucket0","internalType":"uint256","indexed":false},{"type":"uint256","name":"bucket1","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"ExchangeCreated","inputs":[{"type":"bytes32","name":"exchangeId","internalType":"bytes32","indexed":true},{"type":"address","name":"asset0","internalType":"address","indexed":true},{"type":"address","name":"asset1","internalType":"address","indexed":true},{"type":"address","name":"pricingModule","internalType":"address","indexed":false}],"anonymous":false},{"type":"event","name":"ExchangeDestroyed","inputs":[{"type":"bytes32","name":"exchangeId","internalType":"bytes32","indexed":true},{"type":"address","name":"asset0","internalType":"address","indexed":true},{"type":"address","name":"asset1","internalType":"address","indexed":true},{"type":"address","name":"pricingModule","internalType":"address","indexed":false}],"anonymous":false},{"type":"event","name":"OwnershipTransferred","inputs":[{"type":"address","name":"previousOwner","internalType":"address","indexed":true},{"type":"address","name":"newOwner","internalType":"address","indexed":true}],"anonymous":false},{"type":"event","name":"ReserveUpdated","inputs":[{"type":"address","name":"newReserve","internalType":"address","indexed":true}],"anonymous":false},{"type":"event","name":"SortedOraclesUpdated","inputs":[{"type":"address","name":"newSortedOracles","internalType":"address","indexed":true}],"anonymous":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"address","name":"","internalType":"contract IBreakerBox"}],"name":"breakerBox","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"address","name":"","internalType":"address"}],"name":"broker","inputs":[],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[{"type":"bytes32","name":"exchangeId","internalType":"bytes32"}],"name":"createExchange","inputs":[{"type":"tuple","name":"_exchange","internalType":"struct IBiPoolManager.PoolExchange","components":[{"type":"address","name":"asset0","internalType":"address"},{"type":"address","name":"asset1","internalType":"address"},{"type":"address","name":"pricingModule","internalType":"contract IPricingModule"},{"type":"uint256","name":"bucket0","internalType":"uint256"},{"type":"uint256","name":"bucket1","internalType":"uint256"},{"type":"uint256","name":"lastBucketUpdate","internalType":"uint256"},{"type":"tuple","name":"config","internalType":"struct IBiPoolManager.PoolConfig","components":[{"type":"tuple","name":"spread","internalType":"struct FixidityLib.Fraction","components":[{"type":"uint256","name":"value","internalType":"uint256"}]},{"type":"address","name":"referenceRateFeedID","internalType":"address"},{"type":"uint256","name":"referenceRateResetFrequency","internalType":"uint256"},{"type":"uint256","name":"minimumReports","internalType":"uint256"},{"type":"uint256","name":"stablePoolResetSize","internalType":"uint256"}]}]}],"constant":false},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[{"type":"bool","name":"destroyed","internalType":"bool"}],"name":"destroyExchange","inputs":[{"type":"bytes32","name":"exchangeId","internalType":"bytes32"},{"type":"uint256","name":"exchangeIdIndex","internalType":"uint256"}],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"bytes32","name":"","internalType":"bytes32"}],"name":"exchangeIds","inputs":[{"type":"uint256","name":"","internalType":"uint256"}],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"address","name":"asset0","internalType":"address"},{"type":"address","name":"asset1","internalType":"address"},{"type":"address","name":"pricingModule","internalType":"contract IPricingModule"},{"type":"uint256","name":"bucket0","internalType":"uint256"},{"type":"uint256","name":"bucket1","internalType":"uint256"},{"type":"uint256","name":"lastBucketUpdate","internalType":"uint256"},{"type":"tuple","name":"config","internalType":"struct IBiPoolManager.PoolConfig","components":[{"type":"tuple","name":"spread","internalType":"struct FixidityLib.Fraction","components":[{"type":"uint256","name":"value","internalType":"uint256"}]},{"type":"address","name":"referenceRateFeedID","internalType":"address"},{"type":"uint256","name":"referenceRateResetFrequency","internalType":"uint256"},{"type":"uint256","name":"minimumReports","internalType":"uint256"},{"type":"uint256","name":"stablePoolResetSize","internalType":"uint256"}]}],"name":"exchanges","inputs":[{"type":"bytes32","name":"","internalType":"bytes32"}],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"amountIn","internalType":"uint256"}],"name":"getAmountIn","inputs":[{"type":"bytes32","name":"exchangeId","internalType":"bytes32"},{"type":"address","name":"tokenIn","internalType":"address"},{"type":"address","name":"tokenOut","internalType":"address"},{"type":"uint256","name":"amountOut","internalType":"uint256"}],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"amountOut","internalType":"uint256"}],"name":"getAmountOut","inputs":[{"type":"bytes32","name":"exchangeId","internalType":"bytes32"},{"type":"address","name":"tokenIn","internalType":"address"},{"type":"address","name":"tokenOut","internalType":"address"},{"type":"uint256","name":"amountIn","internalType":"uint256"}],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"bytes32[]","name":"","internalType":"bytes32[]"}],"name":"getExchangeIds","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"tuple[]","name":"_exchanges","internalType":"struct IExchangeProvider.Exchange[]","components":[{"type":"bytes32","name":"exchangeId","internalType":"bytes32"},{"type":"address[]","name":"assets","internalType":"address[]"}]}],"name":"getExchanges","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"tuple","name":"exchange","internalType":"struct IBiPoolManager.PoolExchange","components":[{"type":"address","name":"asset0","internalType":"address"},{"type":"address","name":"asset1","internalType":"address"},{"type":"address","name":"pricingModule","internalType":"contract IPricingModule"},{"type":"uint256","name":"bucket0","internalType":"uint256"},{"type":"uint256","name":"bucket1","internalType":"uint256"},{"type":"uint256","name":"lastBucketUpdate","internalType":"uint256"},{"type":"tuple","name":"config","internalType":"struct IBiPoolManager.PoolConfig","components":[{"type":"tuple","name":"spread","internalType":"struct FixidityLib.Fraction","components":[{"type":"uint256","name":"value","internalType":"uint256"}]},{"type":"address","name":"referenceRateFeedID","internalType":"address"},{"type":"uint256","name":"referenceRateResetFrequency","internalType":"uint256"},{"type":"uint256","name":"minimumReports","internalType":"uint256"},{"type":"uint256","name":"stablePoolResetSize","internalType":"uint256"}]}]}],"name":"getPoolExchange","inputs":[{"type":"bytes32","name":"exchangeId","internalType":"bytes32"}],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"initialize","inputs":[{"type":"address","name":"_broker","internalType":"address"},{"type":"address","name":"_reserve","internalType":"contract IReserve"},{"type":"address","name":"_sortedOracles","internalType":"contract ISortedOracles"},{"type":"address","name":"_breakerBox","internalType":"contract IBreakerBox"}],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"bool","name":"","internalType":"bool"}],"name":"initialized","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"bool","name":"","internalType":"bool"}],"name":"isOwner","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"address","name":"","internalType":"address"}],"name":"owner","inputs":[],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"renounceOwnership","inputs":[],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"address","name":"","internalType":"contract IReserve"}],"name":"reserve","inputs":[],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"setBreakerBox","inputs":[{"type":"address","name":"_breakerBox","internalType":"contract IBreakerBox"}],"constant":false},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"setBroker","inputs":[{"type":"address","name":"_broker","internalType":"address"}],"constant":false},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"setReserve","inputs":[{"type":"address","name":"_reserve","internalType":"contract IReserve"}],"constant":false},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"setSortedOracles","inputs":[{"type":"address","name":"_sortedOracles","internalType":"contract ISortedOracles"}],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"address","name":"","internalType":"contract ISortedOracles"}],"name":"sortedOracles","inputs":[],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[{"type":"uint256","name":"amountOut","internalType":"uint256"}],"name":"swapIn","inputs":[{"type":"bytes32","name":"exchangeId","internalType":"bytes32"},{"type":"address","name":"tokenIn","internalType":"address"},{"type":"address","name":"tokenOut","internalType":"address"},{"type":"uint256","name":"amountIn","internalType":"uint256"}],"constant":false},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[{"type":"uint256","name":"amountIn","internalType":"uint256"}],"name":"swapOut","inputs":[{"type":"bytes32","name":"exchangeId","internalType":"bytes32"},{"type":"address","name":"tokenIn","internalType":"address"},{"type":"address","name":"tokenOut","internalType":"address"},{"type":"uint256","name":"amountOut","internalType":"uint256"}],"constant":false},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"transferOwnership","inputs":[{"type":"address","name":"newOwner","internalType":"address"}],"constant":false}]
Contract Creation Code
0x60806040523480156200001157600080fd5b50604051620036c2380380620036c28339810160408190526200003491620000cf565b808062000049576000805460ff191660011790555b5060006200005f6001600160e01b03620000b816565b60008054610100600160a81b0319166101006001600160a01b038416908102919091178255604051929350917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0908290a3505062000117565b3390565b8051620000c981620000fd565b92915050565b600060208284031215620000e257600080fd5b6000620000f08484620000bc565b949350505050565b151590565b6200010881620000f8565b81146200011457600080fd5b50565b61359b80620001276000396000f3fe608060405234801561001057600080fd5b50600436106101a35760003560e01c80639cecc80a116100ee578063d482dda611610097578063f2fde38b11610071578063f2fde38b1461034d578063f414c5e414610360578063f670dde114610368578063f8c8765e1461037b576101a3565b8063d482dda6146102ff578063dc162e3614610325578063e46eb5b61461033a576101a3565b8063bf0d0213116100c8578063bf0d0213146102d1578063cd3293de146102e4578063d3385d05146102ec576101a3565b80639cecc80a146102a3578063abff0110146102b6578063b0898691146102be576101a3565b806342bfc99c116101505780638da5cb5b1161012a5780638da5cb5b146102735780638f32d59b1461028857806393c7e3bc14610290576101a3565b806342bfc99c146102455780634afb215e14610258578063715018a61461026b576101a3565b80631e2e3a6b116101815780631e2e3a6b146101f0578063278488a41461020557806338b1e9f714610225576101a3565b8063040bbd35146101a8578063132e8aa7146101bd578063158ef93e146101db575b600080fd5b6101bb6101b636600461269c565b61038e565b005b6101c561044f565b6040516101d2919061326d565b60405180910390f35b6101e361045e565b6040516101d29190613251565b6101f8610467565b6040516101d29190613240565b6102186102133660046125f9565b61062f565b6040516101d2919061340c565b6102386102333660046125f9565b610712565b6040516101d2919061325f565b610238610253366004612617565b610730565b6101bb61026636600461269c565b61085c565b6101bb610908565b61027b610992565b6040516101d291906131b8565b6101e36109a6565b61023861029e366004612617565b6109cf565b6101bb6102b136600461269c565b6109fb565b61027b610aa7565b6102386102cc3660046126ef565b610ab6565b6101bb6102df36600461251a565b610f1c565b6101c5610fc8565b6102386102fa366004612617565b610fd7565b61031261030d3660046125f9565b6110f9565b6040516101d297969594939291906131c6565b61032d611185565b6040516101d2919061322f565b6101e361034836600461266c565b6111dd565b6101bb61035b36600461251a565b61152d565b6101c561155d565b610238610376366004612617565b61156c565b6101bb610389366004612540565b61158d565b6103966109a6565b6103bb5760405162461bcd60e51b81526004016103b29061336c565b60405180910390fd5b6001600160a01b0381166103e15760405162461bcd60e51b81526004016103b2906133dc565b600580547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383161790556040517f21921b3b46ef2c939e85d6a14410c6e3b9ce132b66e944357ff4f789f68e00e2906104449083906131b8565b60405180910390a150565b6006546001600160a01b031681565b60005460ff1681565b60035460408051828152602080840282010190915260609180156104a557816020015b610492612243565b81526020019060019003908161048a5790505b50905060005b60035481101561062b57600381815481106104c257fe5b90600052602060002001548282815181106104d957fe5b60209081029190910101515260408051600280825260608201909252908160200160208202803883390190505082828151811061051257fe5b602002602001015160200181905250600260006003838154811061053257fe5b9060005260206000200154815260200190815260200160002060000160009054906101000a90046001600160a01b031682828151811061056e57fe5b60200260200101516020015160008151811061058657fe5b60200260200101906001600160a01b031690816001600160a01b03168152505060026000600383815481106105b757fe5b9060005260206000200154815260200190815260200160002060010160009054906101000a90046001600160a01b03168282815181106105f357fe5b60200260200101516020015160018151811061060b57fe5b6001600160a01b03909216602092830291909101909101526001016104ab565b5090565b61063761225b565b50600081815260026020818152604092839020835160e08101855281546001600160a01b03908116825260018301548116828501529382015484168186015260038201546060808301919091526004830154608080840191909152600584015460a080850191909152875160c08181018a5260068701549282019283529181526007860154881696810196909652600885015497860197909752600984015491850191909152600a90920154918301919091529283015281511661070d5760405162461bcd60e51b81526004016103b2906133ac565b919050565b6003818154811061071f57fe5b600091825260209091200154905081565b6001546000906001600160a01b0316331461075d5760405162461bcd60e51b81526004016103b29061331c565b61076561225b565b61076e8661062f565b60055460c0820151602001516040517fc0b05da00000000000000000000000000000000000000000000000000000000081529293506000926001600160a01b039092169163c0b05da0916107c4916004016131b8565b60206040518083038186803b1580156107dc57600080fd5b505afa1580156107f0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610814919081019061272d565b146108315760405162461bcd60e51b81526004016103b29061328c565b600061083f8287878761160e565b9093509050610852878388878786611810565b5050949350505050565b6108646109a6565b6108805760405162461bcd60e51b81526004016103b29061336c565b6001600160a01b0381166108a65760405162461bcd60e51b81526004016103b2906133bc565b600680547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081179091556040517f590fd633a008765ce9e65e8081adfba311e99e11b958a5ecb5000ea3355f735390600090a250565b6109106109a6565b61092c5760405162461bcd60e51b81526004016103b29061336c565b600080546040516101009091046001600160a01b0316907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0908390a3600080547fffffffffffffffffffffff0000000000000000000000000000000000000000ff169055565b60005461010090046001600160a01b031690565b6000805461010090046001600160a01b03166109c06118e9565b6001600160a01b031614905090565b60006109d961225b565b6109e28661062f565b90506109f08186868661160e565b509695505050505050565b610a036109a6565b610a1f5760405162461bcd60e51b81526004016103b29061336c565b6001600160a01b038116610a455760405162461bcd60e51b81526004016103b2906133cc565b600480547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081179091556040517f0b248ab246a87e452fbedee8dc12dfc53e3f3bfdc6920999062c56dea4ab522a90600090a250565b6001546001600160a01b031681565b6000610ac06109a6565b610adc5760405162461bcd60e51b81526004016103b29061336c565b610ae461225b565b610af33684900384018461270e565b60408101519091506001600160a01b0316610b205760405162461bcd60e51b81526004016103b29061334c565b80516001600160a01b0316610b475760405162461bcd60e51b81526004016103b2906132cc565b60208101516001600160a01b0316610b715760405162461bcd60e51b81526004016103b29061337c565b80600001516001600160a01b03166395d89b416040518163ffffffff1660e01b815260040160006040518083038186803b158015610bae57600080fd5b505afa158015610bc2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610bea91908101906126ba565b81602001516001600160a01b03166395d89b416040518163ffffffff1660e01b815260040160006040518083038186803b158015610c2757600080fd5b505afa158015610c3b573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610c6391908101906126ba565b82604001516001600160a01b03166306fdde036040518163ffffffff1660e01b815260040160006040518083038186803b158015610ca057600080fd5b505afa158015610cb4573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610cdc91908101906126ba565b604051602001610cee93929190613194565b60408051601f198184030181529181528151602092830120600081815260029093529120549092506001600160a01b031615610d3c5760405162461bcd60e51b81526004016103b2906132fc565b610d45816118ed565b600080610d5183611b5d565b915091508183606001818152505080836080018181525050826002600086815260200190815260200160002060008201518160000160006101000a8154816001600160a01b0302191690836001600160a01b0316021790555060208201518160010160006101000a8154816001600160a01b0302191690836001600160a01b0316021790555060408201518160020160006101000a8154816001600160a01b0302191690836001600160a01b03160217905550606082015181600301556080820151816004015560a0820151816005015560c08201518160060160008201518160000160008201518160000155505060208201518160010160006101000a8154816001600160a01b0302191690836001600160a01b031602179055506040820151816002015560608201518160030155608082015181600401555050905050600384908060018154018082558091505090600182039060005260206000200160009091929091909150555082602001516001600160a01b031683600001516001600160a01b0316857fb374789237d43aed0f75b4c50a45793fd70e354bb96eecd573f35f5d509d78a18660400151604051610f0c91906131b8565b60405180910390a4505050919050565b610f246109a6565b610f405760405162461bcd60e51b81526004016103b29061336c565b6001600160a01b038116610f665760405162461bcd60e51b81526004016103b2906133fc565b600180547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081179091556040517f865dab7821134b6eb27cba259b40e33bbc1b898e970a535a18a83147f380a51f90600090a250565b6004546001600160a01b031681565b6001546000906001600160a01b031633146110045760405162461bcd60e51b81526004016103b29061331c565b61100c61225b565b6110158661062f565b60055460c0820151602001516040517fc0b05da00000000000000000000000000000000000000000000000000000000081529293506000926001600160a01b039092169163c0b05da09161106b916004016131b8565b60206040518083038186803b15801561108357600080fd5b505afa158015611097573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506110bb919081019061272d565b146110d85760405162461bcd60e51b81526004016103b29061328c565b60006110e682878787611bad565b9093509050610852878388868886611810565b6002602081815260009283526040928390208054600182015493820154600383015460048401546005850154885160c081018a52600687015460a08201908152815260078701546001600160a01b0390811698820198909852600887015499810199909952600986015460608a0152600a90950154608089015292851696958516959190941693929087565b606060038054806020026020016040519081016040528092919081815260200182805480156111d357602002820191906000526020600020905b8154815260200190600101908083116111bf575b5050505050905090565b60006111e76109a6565b6112035760405162461bcd60e51b81526004016103b29061336c565b60035482106112245760405162461bcd60e51b81526004016103b2906132dc565b826003838154811061123257fe5b90600052602060002001541461125a5760405162461bcd60e51b81526004016103b29061338c565b61126261225b565b600260008581526020019081526020016000206040518060e00160405290816000820160009054906101000a90046001600160a01b03166001600160a01b03166001600160a01b031681526020016001820160009054906101000a90046001600160a01b03166001600160a01b03166001600160a01b031681526020016002820160009054906101000a90046001600160a01b03166001600160a01b03166001600160a01b03168152602001600382015481526020016004820154815260200160058201548152602001600682016040518060a00160405290816000820160405180602001604052908160008201548152505081526020016001820160009054906101000a90046001600160a01b03166001600160a01b03166001600160a01b03168152602001600282015481526020016003820154815260200160048201548152505081525050905060026000858152602001908152602001600020600080820160006101000a8154906001600160a01b0302191690556001820160006101000a8154906001600160a01b0302191690556002820160006101000a8154906001600160a01b03021916905560038201600090556004820160009055600582016000905560068201600080820160008082016000905550506001820160006101000a8154906001600160a01b0302191690556002820160009055600382016000905560048201600090555050505060036001600380549050038154811061148557fe5b90600052602060002001546003848154811061149d57fe5b60009182526020909120015560038054806114b457fe5b600190038181906000526020600020016000905590556001915080602001516001600160a01b031681600001516001600160a01b0316857fadbbec6c203cb0248e89fe6d15ad651061a9d5203a1ab0273256e2b7decffa89846040015160405161151e91906131b8565b60405180910390a45092915050565b6115356109a6565b6115515760405162461bcd60e51b81526004016103b29061336c565b61155a81611cbc565b50565b6005546001600160a01b031681565b600061157661225b565b61157f8661062f565b90506109f081868686611bad565b60005460ff16156115b05760405162461bcd60e51b81526004016103b2906132ac565b600080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001660011790556115e433611cbc565b6115ed84610f1c565b6115f6836109fb565b6115ff8261085c565b6116088161038e565b50505050565b60008085600001516001600160a01b0316856001600160a01b031614801561164b575085602001516001600160a01b0316846001600160a01b0316145b80611682575085602001516001600160a01b0316856001600160a01b0316148015611682575085516001600160a01b038581169116145b61169e5760405162461bcd60e51b81526004016103b2906133ec565b6116a786611d5f565b815191975091506001600160a01b03868116911614156117665785604001516001600160a01b03166352707d8c876060015188608001516116ef8a60c0015160000151611d94565b876040518563ffffffff1660e01b815260040161170f9493929190613436565b60206040518083038186803b15801561172757600080fd5b505afa15801561173b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061175f919081019061272d565b9150611807565b85604001516001600160a01b03166352707d8c876080015188606001516117948a60c0015160000151611d94565b876040518563ffffffff1660e01b81526004016117b49493929190613436565b60206040518083038186803b1580156117cc57600080fd5b505afa1580156117e0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611804919081019061272d565b91505b94509492505050565b801561187457426002600088815260200190815260200160002060050181905550857f949f5db193cbfa01f2d443b1c656bfede15497de0d86838cea089bd60c4383428660600151876080015160405161186b92919061341b565b60405180910390a25b84516001600160a01b03858116911614156118b75760608501516000878152600260205260409020908401600382015560808601518390036004909101556118e1565b60608501516000878152600260205260409020908390036003820155608086015184016004909101555b505050505050565b3390565b6004805482516040517f4f8e6e230000000000000000000000000000000000000000000000000000000081526001600160a01b0390921692634f8e6e23926119369291016131b8565b60206040518083038186803b15801561194e57600080fd5b505afa158015611962573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061198691908101906125a1565b6119a25760405162461bcd60e51b81526004016103b29061329c565b6004805460208301516040517f4f8e6e230000000000000000000000000000000000000000000000000000000081526001600160a01b0390921692634f8e6e23926119ee9291016131b8565b60206040518083038186803b158015611a0657600080fd5b505afa158015611a1a573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611a3e91908101906125a1565b80611ae057506004805460208301516040517fcae182fe0000000000000000000000000000000000000000000000000000000081526001600160a01b039092169263cae182fe92611a909291016131b8565b60206040518083038186803b158015611aa857600080fd5b505afa158015611abc573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611ae091908101906125a1565b611afc5760405162461bcd60e51b81526004016103b29061330c565b60c081015151611b1390611b0e611d98565b611dbc565b611b2f5760405162461bcd60e51b81526004016103b29061333c565b60c0810151602001516001600160a01b031661155a5760405162461bcd60e51b81526004016103b29061339c565b6000808260c00151608001519150600080611b7f8560c0015160200151611dc9565b9092509050611ba482611b98838763ffffffff611e9616565b9063ffffffff611ed716565b92505050915091565b60008085600001516001600160a01b0316856001600160a01b0316148015611bea575085602001516001600160a01b0316846001600160a01b0316145b80611c21575085602001516001600160a01b0316856001600160a01b0316148015611c21575085516001600160a01b038581169116145b611c3d5760405162461bcd60e51b81526004016103b2906133ec565b611c4686611d5f565b815191975091506001600160a01b0386811691161415611c8e5785604001516001600160a01b031663571fd012876060015188608001516116ef8a60c0015160000151611d94565b85604001516001600160a01b031663571fd012876080015188606001516117948a60c0015160000151611d94565b6001600160a01b038116611ce25760405162461bcd60e51b81526004016103b2906132bc565b600080546040516001600160a01b038085169361010090930416917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e091a3600080546001600160a01b03909216610100027fffffffffffffffffffffff0000000000000000000000000000000000000000ff909216919091179055565b611d6761225b565b6000611d7283611f19565b15611d8e57611d8083611b5d565b608085015260608401525060015b91929050565b5190565b611da06122b8565b50604080516020810190915269d3c21bcecceda1000000815290565b8051825111155b92915050565b6006546040517fef90e1b00000000000000000000000000000000000000000000000000000000081526000918291829182916001600160a01b039091169063ef90e1b090611e1b9088906004016131b8565b604080518083038186803b158015611e3257600080fd5b505afa158015611e46573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611e6a919081019061274b565b909250905080611e8c5760405162461bcd60e51b81526004016103b29061332c565b9092509050915091565b600082611ea557506000611dc3565b82820282848281611eb257fe5b0414611ed05760405162461bcd60e51b81526004016103b29061335c565b9392505050565b6000611ed083836040518060400160405280601a81526020017f536166654d6174683a206469766973696f6e206279207a65726f00000000000081525061217e565b60065460c0820151602001516040517fffe736bf00000000000000000000000000000000000000000000000000000000815260009283926001600160a01b039091169163ffe736bf91611f6e916004016131b8565b604080518083038186803b158015611f8557600080fd5b505afa158015611f99573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611fbd91908101906125bf565b5090506000611fe18460c00151604001518560a001516121b590919063ffffffff16565b60c085015160608101516006546020909201516040517fbbc66a94000000000000000000000000000000000000000000000000000000008152429490941015945060009391926001600160a01b03169163bbc66a9491612043916004016131b8565b60206040518083038186803b15801561205b57600080fd5b505afa15801561206f573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250612093919081019061272d565b1015905060006120b48660c0015160400151426121da90919063ffffffff16565b60065460c0880151602001516040517f071b48fc0000000000000000000000000000000000000000000000000000000081526001600160a01b039092169163071b48fc91612104916004016131b8565b60206040518083038186803b15801561211c57600080fd5b505afa158015612130573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250612154919081019061272d565b1190508280156121615750815b801561216a5750805b8015612174575083155b9695505050505050565b6000818361219f5760405162461bcd60e51b81526004016103b2919061327b565b5060008385816121ab57fe5b0495945050505050565b600082820183811015611ed05760405162461bcd60e51b81526004016103b2906132ec565b6000611ed083836040518060400160405280601e81526020017f536166654d6174683a207375627472616374696f6e206f766572666c6f7700008152506000818484111561223b5760405162461bcd60e51b81526004016103b2919061327b565b505050900390565b60408051808201909152600081526060602082015290565b6040518060e0016040528060006001600160a01b0316815260200160006001600160a01b0316815260200160006001600160a01b031681526020016000815260200160008152602001600081526020016122b36122cb565b905290565b6040518060200160405280600081525090565b6040518060a001604052806122de6122b8565b815260200160006001600160a01b031681526020016000815260200160008152602001600081525090565b8035611dc381613529565b8051611dc381613529565b8051611dc38161353d565b8035611dc381613546565b8035611dc38161354f565b600082601f83011261235157600080fd5b815161236461235f82613492565b61346b565b9150808252602083016020830185838301111561238057600080fd5b61238b8382846134f3565b50505092915050565b6000602082840312156123a657600080fd5b6123b0602061346b565b905060006123be848461232a565b82525092915050565b600060a082840312156123d957600080fd5b6123e360a061346b565b905060006123f18484612394565b825250602061240284848301612309565b60208301525060406124168482850161232a565b604083015250606061242a8482850161232a565b606083015250608061243e8482850161232a565b60808301525092915050565b6000610160828403121561245d57600080fd5b50919050565b6000610160828403121561247657600080fd5b61248060e061346b565b9050600061248e8484612309565b825250602061249f84848301612309565b60208301525060406124b384828501612335565b60408301525060606124c78482850161232a565b60608301525060806124db8482850161232a565b60808301525060a06124ef8482850161232a565b60a08301525060c0612503848285016123c7565b60c08301525092915050565b8051611dc381613546565b60006020828403121561252c57600080fd5b60006125388484612309565b949350505050565b6000806000806080858703121561255657600080fd5b60006125628787612309565b945050602061257387828801612335565b935050604061258487828801612335565b925050606061259587828801612335565b91505092959194509250565b6000602082840312156125b357600080fd5b6000612538848461231f565b600080604083850312156125d257600080fd5b60006125de858561231f565b92505060206125ef85828601612314565b9150509250929050565b60006020828403121561260b57600080fd5b6000612538848461232a565b6000806000806080858703121561262d57600080fd5b6000612639878761232a565b945050602061264a87828801612309565b935050604061265b87828801612309565b92505060606125958782880161232a565b6000806040838503121561267f57600080fd5b600061268b858561232a565b92505060206125ef8582860161232a565b6000602082840312156126ae57600080fd5b60006125388484612335565b6000602082840312156126cc57600080fd5b815167ffffffffffffffff8111156126e357600080fd5b61253884828501612340565b6000610160828403121561270257600080fd5b6000612538848461244a565b6000610160828403121561272157600080fd5b60006125388484612463565b60006020828403121561273f57600080fd5b6000612538848461250f565b6000806040838503121561275e57600080fd5b600061276a858561250f565b92505060206125ef8582860161250f565b600061278783836127a7565b505060200190565b600061278783836128d4565b6000611ed0838361306d565b6127b0816134c9565b82525050565b60006127c182611d94565b6127cb81856134c0565b93506127d6836134ba565b8060005b838110156128045781516127ee888261277b565b97506127f9836134ba565b9250506001016127da565b509495945050505050565b600061281a82611d94565b61282481856134c0565b935061282f836134ba565b8060005b83811015612804578151612847888261278f565b9750612852836134ba565b925050600101612833565b600061286882611d94565b61287281856134c0565b935083602082028501612884856134ba565b8060005b858110156128be57848403895281516128a1858261279b565b94506128ac836134ba565b60209a909a0199925050600101612888565b5091979650505050505050565b6127b0816134d4565b6127b0816134d9565b6127b0816134dc565b60006128f182611d94565b6128fb81856134c0565b935061290b8185602086016134f3565b6129148161351f565b9093019392505050565b600061292982611d94565b612933818561070d565b93506129438185602086016134f3565b9290920192915050565b600061295a602c836134c0565b7f54726164696e672069732073757370656e64656420666f72207468697320726581527f666572656e636520726174650000000000000000000000000000000000000000602082015260400192915050565b60006129b96033836134c0565b7f617373657430206d757374206265206120737461626c6520726567697374657281527f6564207769746820746865207265736572766500000000000000000000000000602082015260400192915050565b6000612a18601c836134c0565b7f636f6e747261637420616c726561647920696e697469616c697a656400000000815260200192915050565b6000612a516026836134c0565b7f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206181527f6464726573730000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612ab06012836134c0565b7f617373657430206d757374206265207365740000000000000000000000000000815260200192915050565b6000612ae9601c836134c0565b7f65786368616e67654964496e646578206e6f7420696e2072616e676500000000815260200192915050565b6000612b22601b836134c0565b7f536166654d6174683a206164646974696f6e206f766572666c6f770000000000815260200192915050565b6000612b5b6039836134c0565b7f416e2065786368616e676520776974682074686520737065636966696564206181527f737365747320616e642065786368616e67652065786973747300000000000000602082015260400192915050565b6000612bba6025836134c0565b7f617373657431206d757374206265206120737461626c65206f7220636f6c6c6181527f746572616c000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612c196018836134c0565b7f43616c6c6572206973206e6f74207468652042726f6b65720000000000000000815260200192915050565b6000612c526030836134c0565b7f65786368616e676520726174652064656e6f6d696e61746f72206d757374206281527f652067726561746572207468616e203000000000000000000000000000000000602082015260400192915050565b6000612cb16026836134c0565b7f737072656164206d757374206265206c657373207468616e206f72206571756181527f6c20746f20310000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612d106019836134c0565b7f70726963696e674d6f64756c65206d7573742062652073657400000000000000815260200192915050565b6000612d496021836134c0565b7f536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f81527f7700000000000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612da86020836134c0565b7f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572815260200192915050565b6000612de16012836134c0565b7f617373657431206d757374206265207365740000000000000000000000000000815260200192915050565b6000612e1a6021836134c0565b7f65786368616e6765496420617420696e64657820646f65736e2774206d61746381527f6800000000000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612e79601f836134c0565b7f7265666572656e636552617465466565644944206d7573742062652073657400815260200192915050565b6000612eb26030836134c0565b7f416e2065786368616e676520776974682074686520737065636966696564206981527f6420646f6573206e6f7420657869737400000000000000000000000000000000602082015260400192915050565b6000612f116021836134c0565b7f536f727465644f7261636c65732061646472657373206d75737420626520736581527f7400000000000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612f70601b836134c0565b7f526573657276652061646472657373206d757374206265207365740000000000815260200192915050565b6000612fa9601e836134c0565b7f427265616b6572426f782061646472657373206d757374206265207365740000815260200192915050565b6000612fe26028836134c0565b7f746f6b656e496e20616e6420746f6b656e4f7574206d757374206d617463682081527f65786368616e6765000000000000000000000000000000000000000000000000602082015260400192915050565b6000613041601a836134c0565b7f42726f6b65722061646472657373206d75737420626520736574000000000000815260200192915050565b8051600090604084019061308185826128d4565b506020830151848203602086015261309982826127b6565b95945050505050565b8051602083019061160884826128d4565b805160a08301906130c484826130a2565b5060208201516130d760208501826127a7565b5060408201516130ea60408501826128d4565b5060608201516130fd60608501826128d4565b50608082015161160860808501826128d4565b805161016083019061312284826127a7565b50602082015161313560208501826127a7565b50604082015161314860408501826128dd565b50606082015161315b60608501826128d4565b50608082015161316e60808501826128d4565b5060a082015161318160a08501826128d4565b5060c082015161160860c08501826130b3565b60006131a0828661291e565b91506131ac828561291e565b9150613099828461291e565b60208101611dc382846127a7565b61016081016131d5828a6127a7565b6131e260208301896127a7565b6131ef60408301886128dd565b6131fc60608301876128d4565b61320960808301866128d4565b61321660a08301856128d4565b61322360c08301846130b3565b98975050505050505050565b60208082528101611ed0818461280f565b60208082528101611ed0818461285d565b60208101611dc382846128cb565b60208101611dc382846128d4565b60208101611dc382846128dd565b60208082528101611ed081846128e6565b60208082528101611dc38161294d565b60208082528101611dc3816129ac565b60208082528101611dc381612a0b565b60208082528101611dc381612a44565b60208082528101611dc381612aa3565b60208082528101611dc381612adc565b60208082528101611dc381612b15565b60208082528101611dc381612b4e565b60208082528101611dc381612bad565b60208082528101611dc381612c0c565b60208082528101611dc381612c45565b60208082528101611dc381612ca4565b60208082528101611dc381612d03565b60208082528101611dc381612d3c565b60208082528101611dc381612d9b565b60208082528101611dc381612dd4565b60208082528101611dc381612e0d565b60208082528101611dc381612e6c565b60208082528101611dc381612ea5565b60208082528101611dc381612f04565b60208082528101611dc381612f63565b60208082528101611dc381612f9c565b60208082528101611dc381612fd5565b60208082528101611dc381613034565b6101608101611dc38284613110565b6040810161342982856128d4565b611ed060208301846128d4565b6080810161344482876128d4565b61345160208301866128d4565b61345e60408301856128d4565b61309960608301846128d4565b60405181810167ffffffffffffffff8111828210171561348a57600080fd5b604052919050565b600067ffffffffffffffff8211156134a957600080fd5b506020601f91909101601f19160190565b60200190565b90815260200190565b6000611dc3826134e7565b151590565b90565b6000611dc3826134c9565b6001600160a01b031690565b60005b8381101561350e5781810151838201526020016134f6565b838111156116085750506000910152565b601f01601f191690565b613532816134c9565b811461155a57600080fd5b613532816134d4565b613532816134d9565b613532816134dc56fea365627a7a723158201b4a65d4084324663ba27e7dedcd64199ce92754fc4311cf94ea0392b47b19d76c6578706572696d656e74616cf564736f6c634300051100400000000000000000000000000000000000000000000000000000000000000000
Deployed ByteCode
0x608060405234801561001057600080fd5b50600436106101a35760003560e01c80639cecc80a116100ee578063d482dda611610097578063f2fde38b11610071578063f2fde38b1461034d578063f414c5e414610360578063f670dde114610368578063f8c8765e1461037b576101a3565b8063d482dda6146102ff578063dc162e3614610325578063e46eb5b61461033a576101a3565b8063bf0d0213116100c8578063bf0d0213146102d1578063cd3293de146102e4578063d3385d05146102ec576101a3565b80639cecc80a146102a3578063abff0110146102b6578063b0898691146102be576101a3565b806342bfc99c116101505780638da5cb5b1161012a5780638da5cb5b146102735780638f32d59b1461028857806393c7e3bc14610290576101a3565b806342bfc99c146102455780634afb215e14610258578063715018a61461026b576101a3565b80631e2e3a6b116101815780631e2e3a6b146101f0578063278488a41461020557806338b1e9f714610225576101a3565b8063040bbd35146101a8578063132e8aa7146101bd578063158ef93e146101db575b600080fd5b6101bb6101b636600461269c565b61038e565b005b6101c561044f565b6040516101d2919061326d565b60405180910390f35b6101e361045e565b6040516101d29190613251565b6101f8610467565b6040516101d29190613240565b6102186102133660046125f9565b61062f565b6040516101d2919061340c565b6102386102333660046125f9565b610712565b6040516101d2919061325f565b610238610253366004612617565b610730565b6101bb61026636600461269c565b61085c565b6101bb610908565b61027b610992565b6040516101d291906131b8565b6101e36109a6565b61023861029e366004612617565b6109cf565b6101bb6102b136600461269c565b6109fb565b61027b610aa7565b6102386102cc3660046126ef565b610ab6565b6101bb6102df36600461251a565b610f1c565b6101c5610fc8565b6102386102fa366004612617565b610fd7565b61031261030d3660046125f9565b6110f9565b6040516101d297969594939291906131c6565b61032d611185565b6040516101d2919061322f565b6101e361034836600461266c565b6111dd565b6101bb61035b36600461251a565b61152d565b6101c561155d565b610238610376366004612617565b61156c565b6101bb610389366004612540565b61158d565b6103966109a6565b6103bb5760405162461bcd60e51b81526004016103b29061336c565b60405180910390fd5b6001600160a01b0381166103e15760405162461bcd60e51b81526004016103b2906133dc565b600580547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383161790556040517f21921b3b46ef2c939e85d6a14410c6e3b9ce132b66e944357ff4f789f68e00e2906104449083906131b8565b60405180910390a150565b6006546001600160a01b031681565b60005460ff1681565b60035460408051828152602080840282010190915260609180156104a557816020015b610492612243565b81526020019060019003908161048a5790505b50905060005b60035481101561062b57600381815481106104c257fe5b90600052602060002001548282815181106104d957fe5b60209081029190910101515260408051600280825260608201909252908160200160208202803883390190505082828151811061051257fe5b602002602001015160200181905250600260006003838154811061053257fe5b9060005260206000200154815260200190815260200160002060000160009054906101000a90046001600160a01b031682828151811061056e57fe5b60200260200101516020015160008151811061058657fe5b60200260200101906001600160a01b031690816001600160a01b03168152505060026000600383815481106105b757fe5b9060005260206000200154815260200190815260200160002060010160009054906101000a90046001600160a01b03168282815181106105f357fe5b60200260200101516020015160018151811061060b57fe5b6001600160a01b03909216602092830291909101909101526001016104ab565b5090565b61063761225b565b50600081815260026020818152604092839020835160e08101855281546001600160a01b03908116825260018301548116828501529382015484168186015260038201546060808301919091526004830154608080840191909152600584015460a080850191909152875160c08181018a5260068701549282019283529181526007860154881696810196909652600885015497860197909752600984015491850191909152600a90920154918301919091529283015281511661070d5760405162461bcd60e51b81526004016103b2906133ac565b919050565b6003818154811061071f57fe5b600091825260209091200154905081565b6001546000906001600160a01b0316331461075d5760405162461bcd60e51b81526004016103b29061331c565b61076561225b565b61076e8661062f565b60055460c0820151602001516040517fc0b05da00000000000000000000000000000000000000000000000000000000081529293506000926001600160a01b039092169163c0b05da0916107c4916004016131b8565b60206040518083038186803b1580156107dc57600080fd5b505afa1580156107f0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610814919081019061272d565b146108315760405162461bcd60e51b81526004016103b29061328c565b600061083f8287878761160e565b9093509050610852878388878786611810565b5050949350505050565b6108646109a6565b6108805760405162461bcd60e51b81526004016103b29061336c565b6001600160a01b0381166108a65760405162461bcd60e51b81526004016103b2906133bc565b600680547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081179091556040517f590fd633a008765ce9e65e8081adfba311e99e11b958a5ecb5000ea3355f735390600090a250565b6109106109a6565b61092c5760405162461bcd60e51b81526004016103b29061336c565b600080546040516101009091046001600160a01b0316907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0908390a3600080547fffffffffffffffffffffff0000000000000000000000000000000000000000ff169055565b60005461010090046001600160a01b031690565b6000805461010090046001600160a01b03166109c06118e9565b6001600160a01b031614905090565b60006109d961225b565b6109e28661062f565b90506109f08186868661160e565b509695505050505050565b610a036109a6565b610a1f5760405162461bcd60e51b81526004016103b29061336c565b6001600160a01b038116610a455760405162461bcd60e51b81526004016103b2906133cc565b600480547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081179091556040517f0b248ab246a87e452fbedee8dc12dfc53e3f3bfdc6920999062c56dea4ab522a90600090a250565b6001546001600160a01b031681565b6000610ac06109a6565b610adc5760405162461bcd60e51b81526004016103b29061336c565b610ae461225b565b610af33684900384018461270e565b60408101519091506001600160a01b0316610b205760405162461bcd60e51b81526004016103b29061334c565b80516001600160a01b0316610b475760405162461bcd60e51b81526004016103b2906132cc565b60208101516001600160a01b0316610b715760405162461bcd60e51b81526004016103b29061337c565b80600001516001600160a01b03166395d89b416040518163ffffffff1660e01b815260040160006040518083038186803b158015610bae57600080fd5b505afa158015610bc2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610bea91908101906126ba565b81602001516001600160a01b03166395d89b416040518163ffffffff1660e01b815260040160006040518083038186803b158015610c2757600080fd5b505afa158015610c3b573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610c6391908101906126ba565b82604001516001600160a01b03166306fdde036040518163ffffffff1660e01b815260040160006040518083038186803b158015610ca057600080fd5b505afa158015610cb4573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610cdc91908101906126ba565b604051602001610cee93929190613194565b60408051601f198184030181529181528151602092830120600081815260029093529120549092506001600160a01b031615610d3c5760405162461bcd60e51b81526004016103b2906132fc565b610d45816118ed565b600080610d5183611b5d565b915091508183606001818152505080836080018181525050826002600086815260200190815260200160002060008201518160000160006101000a8154816001600160a01b0302191690836001600160a01b0316021790555060208201518160010160006101000a8154816001600160a01b0302191690836001600160a01b0316021790555060408201518160020160006101000a8154816001600160a01b0302191690836001600160a01b03160217905550606082015181600301556080820151816004015560a0820151816005015560c08201518160060160008201518160000160008201518160000155505060208201518160010160006101000a8154816001600160a01b0302191690836001600160a01b031602179055506040820151816002015560608201518160030155608082015181600401555050905050600384908060018154018082558091505090600182039060005260206000200160009091929091909150555082602001516001600160a01b031683600001516001600160a01b0316857fb374789237d43aed0f75b4c50a45793fd70e354bb96eecd573f35f5d509d78a18660400151604051610f0c91906131b8565b60405180910390a4505050919050565b610f246109a6565b610f405760405162461bcd60e51b81526004016103b29061336c565b6001600160a01b038116610f665760405162461bcd60e51b81526004016103b2906133fc565b600180547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081179091556040517f865dab7821134b6eb27cba259b40e33bbc1b898e970a535a18a83147f380a51f90600090a250565b6004546001600160a01b031681565b6001546000906001600160a01b031633146110045760405162461bcd60e51b81526004016103b29061331c565b61100c61225b565b6110158661062f565b60055460c0820151602001516040517fc0b05da00000000000000000000000000000000000000000000000000000000081529293506000926001600160a01b039092169163c0b05da09161106b916004016131b8565b60206040518083038186803b15801561108357600080fd5b505afa158015611097573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506110bb919081019061272d565b146110d85760405162461bcd60e51b81526004016103b29061328c565b60006110e682878787611bad565b9093509050610852878388868886611810565b6002602081815260009283526040928390208054600182015493820154600383015460048401546005850154885160c081018a52600687015460a08201908152815260078701546001600160a01b0390811698820198909852600887015499810199909952600986015460608a0152600a90950154608089015292851696958516959190941693929087565b606060038054806020026020016040519081016040528092919081815260200182805480156111d357602002820191906000526020600020905b8154815260200190600101908083116111bf575b5050505050905090565b60006111e76109a6565b6112035760405162461bcd60e51b81526004016103b29061336c565b60035482106112245760405162461bcd60e51b81526004016103b2906132dc565b826003838154811061123257fe5b90600052602060002001541461125a5760405162461bcd60e51b81526004016103b29061338c565b61126261225b565b600260008581526020019081526020016000206040518060e00160405290816000820160009054906101000a90046001600160a01b03166001600160a01b03166001600160a01b031681526020016001820160009054906101000a90046001600160a01b03166001600160a01b03166001600160a01b031681526020016002820160009054906101000a90046001600160a01b03166001600160a01b03166001600160a01b03168152602001600382015481526020016004820154815260200160058201548152602001600682016040518060a00160405290816000820160405180602001604052908160008201548152505081526020016001820160009054906101000a90046001600160a01b03166001600160a01b03166001600160a01b03168152602001600282015481526020016003820154815260200160048201548152505081525050905060026000858152602001908152602001600020600080820160006101000a8154906001600160a01b0302191690556001820160006101000a8154906001600160a01b0302191690556002820160006101000a8154906001600160a01b03021916905560038201600090556004820160009055600582016000905560068201600080820160008082016000905550506001820160006101000a8154906001600160a01b0302191690556002820160009055600382016000905560048201600090555050505060036001600380549050038154811061148557fe5b90600052602060002001546003848154811061149d57fe5b60009182526020909120015560038054806114b457fe5b600190038181906000526020600020016000905590556001915080602001516001600160a01b031681600001516001600160a01b0316857fadbbec6c203cb0248e89fe6d15ad651061a9d5203a1ab0273256e2b7decffa89846040015160405161151e91906131b8565b60405180910390a45092915050565b6115356109a6565b6115515760405162461bcd60e51b81526004016103b29061336c565b61155a81611cbc565b50565b6005546001600160a01b031681565b600061157661225b565b61157f8661062f565b90506109f081868686611bad565b60005460ff16156115b05760405162461bcd60e51b81526004016103b2906132ac565b600080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001660011790556115e433611cbc565b6115ed84610f1c565b6115f6836109fb565b6115ff8261085c565b6116088161038e565b50505050565b60008085600001516001600160a01b0316856001600160a01b031614801561164b575085602001516001600160a01b0316846001600160a01b0316145b80611682575085602001516001600160a01b0316856001600160a01b0316148015611682575085516001600160a01b038581169116145b61169e5760405162461bcd60e51b81526004016103b2906133ec565b6116a786611d5f565b815191975091506001600160a01b03868116911614156117665785604001516001600160a01b03166352707d8c876060015188608001516116ef8a60c0015160000151611d94565b876040518563ffffffff1660e01b815260040161170f9493929190613436565b60206040518083038186803b15801561172757600080fd5b505afa15801561173b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061175f919081019061272d565b9150611807565b85604001516001600160a01b03166352707d8c876080015188606001516117948a60c0015160000151611d94565b876040518563ffffffff1660e01b81526004016117b49493929190613436565b60206040518083038186803b1580156117cc57600080fd5b505afa1580156117e0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611804919081019061272d565b91505b94509492505050565b801561187457426002600088815260200190815260200160002060050181905550857f949f5db193cbfa01f2d443b1c656bfede15497de0d86838cea089bd60c4383428660600151876080015160405161186b92919061341b565b60405180910390a25b84516001600160a01b03858116911614156118b75760608501516000878152600260205260409020908401600382015560808601518390036004909101556118e1565b60608501516000878152600260205260409020908390036003820155608086015184016004909101555b505050505050565b3390565b6004805482516040517f4f8e6e230000000000000000000000000000000000000000000000000000000081526001600160a01b0390921692634f8e6e23926119369291016131b8565b60206040518083038186803b15801561194e57600080fd5b505afa158015611962573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061198691908101906125a1565b6119a25760405162461bcd60e51b81526004016103b29061329c565b6004805460208301516040517f4f8e6e230000000000000000000000000000000000000000000000000000000081526001600160a01b0390921692634f8e6e23926119ee9291016131b8565b60206040518083038186803b158015611a0657600080fd5b505afa158015611a1a573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611a3e91908101906125a1565b80611ae057506004805460208301516040517fcae182fe0000000000000000000000000000000000000000000000000000000081526001600160a01b039092169263cae182fe92611a909291016131b8565b60206040518083038186803b158015611aa857600080fd5b505afa158015611abc573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611ae091908101906125a1565b611afc5760405162461bcd60e51b81526004016103b29061330c565b60c081015151611b1390611b0e611d98565b611dbc565b611b2f5760405162461bcd60e51b81526004016103b29061333c565b60c0810151602001516001600160a01b031661155a5760405162461bcd60e51b81526004016103b29061339c565b6000808260c00151608001519150600080611b7f8560c0015160200151611dc9565b9092509050611ba482611b98838763ffffffff611e9616565b9063ffffffff611ed716565b92505050915091565b60008085600001516001600160a01b0316856001600160a01b0316148015611bea575085602001516001600160a01b0316846001600160a01b0316145b80611c21575085602001516001600160a01b0316856001600160a01b0316148015611c21575085516001600160a01b038581169116145b611c3d5760405162461bcd60e51b81526004016103b2906133ec565b611c4686611d5f565b815191975091506001600160a01b0386811691161415611c8e5785604001516001600160a01b031663571fd012876060015188608001516116ef8a60c0015160000151611d94565b85604001516001600160a01b031663571fd012876080015188606001516117948a60c0015160000151611d94565b6001600160a01b038116611ce25760405162461bcd60e51b81526004016103b2906132bc565b600080546040516001600160a01b038085169361010090930416917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e091a3600080546001600160a01b03909216610100027fffffffffffffffffffffff0000000000000000000000000000000000000000ff909216919091179055565b611d6761225b565b6000611d7283611f19565b15611d8e57611d8083611b5d565b608085015260608401525060015b91929050565b5190565b611da06122b8565b50604080516020810190915269d3c21bcecceda1000000815290565b8051825111155b92915050565b6006546040517fef90e1b00000000000000000000000000000000000000000000000000000000081526000918291829182916001600160a01b039091169063ef90e1b090611e1b9088906004016131b8565b604080518083038186803b158015611e3257600080fd5b505afa158015611e46573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611e6a919081019061274b565b909250905080611e8c5760405162461bcd60e51b81526004016103b29061332c565b9092509050915091565b600082611ea557506000611dc3565b82820282848281611eb257fe5b0414611ed05760405162461bcd60e51b81526004016103b29061335c565b9392505050565b6000611ed083836040518060400160405280601a81526020017f536166654d6174683a206469766973696f6e206279207a65726f00000000000081525061217e565b60065460c0820151602001516040517fffe736bf00000000000000000000000000000000000000000000000000000000815260009283926001600160a01b039091169163ffe736bf91611f6e916004016131b8565b604080518083038186803b158015611f8557600080fd5b505afa158015611f99573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611fbd91908101906125bf565b5090506000611fe18460c00151604001518560a001516121b590919063ffffffff16565b60c085015160608101516006546020909201516040517fbbc66a94000000000000000000000000000000000000000000000000000000008152429490941015945060009391926001600160a01b03169163bbc66a9491612043916004016131b8565b60206040518083038186803b15801561205b57600080fd5b505afa15801561206f573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250612093919081019061272d565b1015905060006120b48660c0015160400151426121da90919063ffffffff16565b60065460c0880151602001516040517f071b48fc0000000000000000000000000000000000000000000000000000000081526001600160a01b039092169163071b48fc91612104916004016131b8565b60206040518083038186803b15801561211c57600080fd5b505afa158015612130573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250612154919081019061272d565b1190508280156121615750815b801561216a5750805b8015612174575083155b9695505050505050565b6000818361219f5760405162461bcd60e51b81526004016103b2919061327b565b5060008385816121ab57fe5b0495945050505050565b600082820183811015611ed05760405162461bcd60e51b81526004016103b2906132ec565b6000611ed083836040518060400160405280601e81526020017f536166654d6174683a207375627472616374696f6e206f766572666c6f7700008152506000818484111561223b5760405162461bcd60e51b81526004016103b2919061327b565b505050900390565b60408051808201909152600081526060602082015290565b6040518060e0016040528060006001600160a01b0316815260200160006001600160a01b0316815260200160006001600160a01b031681526020016000815260200160008152602001600081526020016122b36122cb565b905290565b6040518060200160405280600081525090565b6040518060a001604052806122de6122b8565b815260200160006001600160a01b031681526020016000815260200160008152602001600081525090565b8035611dc381613529565b8051611dc381613529565b8051611dc38161353d565b8035611dc381613546565b8035611dc38161354f565b600082601f83011261235157600080fd5b815161236461235f82613492565b61346b565b9150808252602083016020830185838301111561238057600080fd5b61238b8382846134f3565b50505092915050565b6000602082840312156123a657600080fd5b6123b0602061346b565b905060006123be848461232a565b82525092915050565b600060a082840312156123d957600080fd5b6123e360a061346b565b905060006123f18484612394565b825250602061240284848301612309565b60208301525060406124168482850161232a565b604083015250606061242a8482850161232a565b606083015250608061243e8482850161232a565b60808301525092915050565b6000610160828403121561245d57600080fd5b50919050565b6000610160828403121561247657600080fd5b61248060e061346b565b9050600061248e8484612309565b825250602061249f84848301612309565b60208301525060406124b384828501612335565b60408301525060606124c78482850161232a565b60608301525060806124db8482850161232a565b60808301525060a06124ef8482850161232a565b60a08301525060c0612503848285016123c7565b60c08301525092915050565b8051611dc381613546565b60006020828403121561252c57600080fd5b60006125388484612309565b949350505050565b6000806000806080858703121561255657600080fd5b60006125628787612309565b945050602061257387828801612335565b935050604061258487828801612335565b925050606061259587828801612335565b91505092959194509250565b6000602082840312156125b357600080fd5b6000612538848461231f565b600080604083850312156125d257600080fd5b60006125de858561231f565b92505060206125ef85828601612314565b9150509250929050565b60006020828403121561260b57600080fd5b6000612538848461232a565b6000806000806080858703121561262d57600080fd5b6000612639878761232a565b945050602061264a87828801612309565b935050604061265b87828801612309565b92505060606125958782880161232a565b6000806040838503121561267f57600080fd5b600061268b858561232a565b92505060206125ef8582860161232a565b6000602082840312156126ae57600080fd5b60006125388484612335565b6000602082840312156126cc57600080fd5b815167ffffffffffffffff8111156126e357600080fd5b61253884828501612340565b6000610160828403121561270257600080fd5b6000612538848461244a565b6000610160828403121561272157600080fd5b60006125388484612463565b60006020828403121561273f57600080fd5b6000612538848461250f565b6000806040838503121561275e57600080fd5b600061276a858561250f565b92505060206125ef8582860161250f565b600061278783836127a7565b505060200190565b600061278783836128d4565b6000611ed0838361306d565b6127b0816134c9565b82525050565b60006127c182611d94565b6127cb81856134c0565b93506127d6836134ba565b8060005b838110156128045781516127ee888261277b565b97506127f9836134ba565b9250506001016127da565b509495945050505050565b600061281a82611d94565b61282481856134c0565b935061282f836134ba565b8060005b83811015612804578151612847888261278f565b9750612852836134ba565b925050600101612833565b600061286882611d94565b61287281856134c0565b935083602082028501612884856134ba565b8060005b858110156128be57848403895281516128a1858261279b565b94506128ac836134ba565b60209a909a0199925050600101612888565b5091979650505050505050565b6127b0816134d4565b6127b0816134d9565b6127b0816134dc565b60006128f182611d94565b6128fb81856134c0565b935061290b8185602086016134f3565b6129148161351f565b9093019392505050565b600061292982611d94565b612933818561070d565b93506129438185602086016134f3565b9290920192915050565b600061295a602c836134c0565b7f54726164696e672069732073757370656e64656420666f72207468697320726581527f666572656e636520726174650000000000000000000000000000000000000000602082015260400192915050565b60006129b96033836134c0565b7f617373657430206d757374206265206120737461626c6520726567697374657281527f6564207769746820746865207265736572766500000000000000000000000000602082015260400192915050565b6000612a18601c836134c0565b7f636f6e747261637420616c726561647920696e697469616c697a656400000000815260200192915050565b6000612a516026836134c0565b7f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206181527f6464726573730000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612ab06012836134c0565b7f617373657430206d757374206265207365740000000000000000000000000000815260200192915050565b6000612ae9601c836134c0565b7f65786368616e67654964496e646578206e6f7420696e2072616e676500000000815260200192915050565b6000612b22601b836134c0565b7f536166654d6174683a206164646974696f6e206f766572666c6f770000000000815260200192915050565b6000612b5b6039836134c0565b7f416e2065786368616e676520776974682074686520737065636966696564206181527f737365747320616e642065786368616e67652065786973747300000000000000602082015260400192915050565b6000612bba6025836134c0565b7f617373657431206d757374206265206120737461626c65206f7220636f6c6c6181527f746572616c000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612c196018836134c0565b7f43616c6c6572206973206e6f74207468652042726f6b65720000000000000000815260200192915050565b6000612c526030836134c0565b7f65786368616e676520726174652064656e6f6d696e61746f72206d757374206281527f652067726561746572207468616e203000000000000000000000000000000000602082015260400192915050565b6000612cb16026836134c0565b7f737072656164206d757374206265206c657373207468616e206f72206571756181527f6c20746f20310000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612d106019836134c0565b7f70726963696e674d6f64756c65206d7573742062652073657400000000000000815260200192915050565b6000612d496021836134c0565b7f536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f81527f7700000000000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612da86020836134c0565b7f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572815260200192915050565b6000612de16012836134c0565b7f617373657431206d757374206265207365740000000000000000000000000000815260200192915050565b6000612e1a6021836134c0565b7f65786368616e6765496420617420696e64657820646f65736e2774206d61746381527f6800000000000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612e79601f836134c0565b7f7265666572656e636552617465466565644944206d7573742062652073657400815260200192915050565b6000612eb26030836134c0565b7f416e2065786368616e676520776974682074686520737065636966696564206981527f6420646f6573206e6f7420657869737400000000000000000000000000000000602082015260400192915050565b6000612f116021836134c0565b7f536f727465644f7261636c65732061646472657373206d75737420626520736581527f7400000000000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612f70601b836134c0565b7f526573657276652061646472657373206d757374206265207365740000000000815260200192915050565b6000612fa9601e836134c0565b7f427265616b6572426f782061646472657373206d757374206265207365740000815260200192915050565b6000612fe26028836134c0565b7f746f6b656e496e20616e6420746f6b656e4f7574206d757374206d617463682081527f65786368616e6765000000000000000000000000000000000000000000000000602082015260400192915050565b6000613041601a836134c0565b7f42726f6b65722061646472657373206d75737420626520736574000000000000815260200192915050565b8051600090604084019061308185826128d4565b506020830151848203602086015261309982826127b6565b95945050505050565b8051602083019061160884826128d4565b805160a08301906130c484826130a2565b5060208201516130d760208501826127a7565b5060408201516130ea60408501826128d4565b5060608201516130fd60608501826128d4565b50608082015161160860808501826128d4565b805161016083019061312284826127a7565b50602082015161313560208501826127a7565b50604082015161314860408501826128dd565b50606082015161315b60608501826128d4565b50608082015161316e60808501826128d4565b5060a082015161318160a08501826128d4565b5060c082015161160860c08501826130b3565b60006131a0828661291e565b91506131ac828561291e565b9150613099828461291e565b60208101611dc382846127a7565b61016081016131d5828a6127a7565b6131e260208301896127a7565b6131ef60408301886128dd565b6131fc60608301876128d4565b61320960808301866128d4565b61321660a08301856128d4565b61322360c08301846130b3565b98975050505050505050565b60208082528101611ed0818461280f565b60208082528101611ed0818461285d565b60208101611dc382846128cb565b60208101611dc382846128d4565b60208101611dc382846128dd565b60208082528101611ed081846128e6565b60208082528101611dc38161294d565b60208082528101611dc3816129ac565b60208082528101611dc381612a0b565b60208082528101611dc381612a44565b60208082528101611dc381612aa3565b60208082528101611dc381612adc565b60208082528101611dc381612b15565b60208082528101611dc381612b4e565b60208082528101611dc381612bad565b60208082528101611dc381612c0c565b60208082528101611dc381612c45565b60208082528101611dc381612ca4565b60208082528101611dc381612d03565b60208082528101611dc381612d3c565b60208082528101611dc381612d9b565b60208082528101611dc381612dd4565b60208082528101611dc381612e0d565b60208082528101611dc381612e6c565b60208082528101611dc381612ea5565b60208082528101611dc381612f04565b60208082528101611dc381612f63565b60208082528101611dc381612f9c565b60208082528101611dc381612fd5565b60208082528101611dc381613034565b6101608101611dc38284613110565b6040810161342982856128d4565b611ed060208301846128d4565b6080810161344482876128d4565b61345160208301866128d4565b61345e60408301856128d4565b61309960608301846128d4565b60405181810167ffffffffffffffff8111828210171561348a57600080fd5b604052919050565b600067ffffffffffffffff8211156134a957600080fd5b506020601f91909101601f19160190565b60200190565b90815260200190565b6000611dc3826134e7565b151590565b90565b6000611dc3826134c9565b6001600160a01b031690565b60005b8381101561350e5781810151838201526020016134f6565b838111156116085750506000910152565b601f01601f191690565b613532816134c9565b811461155a57600080fd5b613532816134d4565b613532816134d9565b613532816134dc56fea365627a7a723158201b4a65d4084324663ba27e7dedcd64199ce92754fc4311cf94ea0392b47b19d76c6578706572696d656e74616cf564736f6c63430005110040