Address Details
contract
0x6f7B6A1b8637b9df021e62CAD52C2589e63823D8
- Contract Name
- BiPoolManager
- Creator
- 0x278160–50fc38 at 0xe8a5b2–6d9466
- Balance
- 0 CELO
- Tokens
-
Fetching tokens...
- Transactions
- 1 Transactions
- Transfers
- 0 Transfers
- Gas Used
- 28,931
- Last Balance Update
- 14227374
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-11-21T23:21:06.352561Z
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 { 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; // Address of the Mento reserve contract IReserve public reserve; // Address of the Mento reserve 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 ) external initializer { _transferOwnership(msg.sender); setBroker(_broker); setReserve(_reserve); setSortedOracles(_sortedOracles); } /* ==================== 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 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); 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); 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.oracleReportTarget); // solhint-disable-next-line not-rely-on-time bool timePassed = now >= exchange.lastBucketUpdate.add(exchange.config.referenceRateResetFrequency); bool enoughReports = (sortedOracles.numRates(exchange.config.oracleReportTarget) >= exchange.config.minimumReports); // solhint-disable-next-line not-rely-on-time bool medianReportRecent = sortedOracles.medianTimestamp(exchange.config.oracleReportTarget) > 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.oracleReportTarget); 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.oracleReportTarget != address(0), "oracleReportTarget 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/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 ); }
/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; } }
/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 ); // 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; 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); }
/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); }
/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/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); }
/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); }
/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 oracleReportTarget; // can be a stable address or custom 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 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/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/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); } }
/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, 0, 1); } /** * @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; } }
Contract ABI
[{"type":"constructor","stateMutability":"nonpayable","payable":false,"inputs":[{"type":"bool","name":"test","internalType":"bool"}]},{"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":"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":"oracleReportTarget","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":"oracleReportTarget","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":"oracleReportTarget","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"}],"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":"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}]
Deployed ByteCode
0x608060405234801561001057600080fd5b506004361061018d5760003560e01c80639cecc80a116100e3578063d3385d051161008c578063e46eb5b611610066578063e46eb5b614610324578063f2fde38b14610337578063f670dde11461034a5761018d565b8063d3385d05146102d6578063d482dda6146102e9578063dc162e361461030f5761018d565b8063bf0d0213116100bd578063bf0d0213146102a8578063c0c53b8b146102bb578063cd3293de146102ce5761018d565b80639cecc80a1461027a578063abff01101461028d578063b0898691146102955761018d565b806342bfc99c116101455780638da5cb5b1161011f5780638da5cb5b1461024a5780638f32d59b1461025f57806393c7e3bc146102675761018d565b806342bfc99c1461021a5780634afb215e1461022d578063715018a6146102425761018d565b80631e2e3a6b116101765780631e2e3a6b146101c5578063278488a4146101da57806338b1e9f7146101fa5761018d565b8063132e8aa714610192578063158ef93e146101b0575b600080fd5b61019a61035d565b6040516101a79190612f4f565b60405180910390f35b6101b861036c565b6040516101a79190612f33565b6101cd610375565b6040516101a79190612f22565b6101ed6101e8366004612361565b61053d565b6040516101a791906130ce565b61020d610208366004612361565b610629565b6040516101a79190612f41565b61020d61022836600461237f565b610647565b61024061023b366004612410565b6106b2565b005b61024061075e565b6102526107e8565b6040516101a79190612e9a565b6101b86107fc565b61020d61027536600461237f565b610825565b610240610288366004612410565b610851565b6102526108fd565b61020d6102a3366004612463565b61090c565b6102406102b6366004612296565b610d72565b6102406102c93660046122bc565b610e1e565b61019a610e95565b61020d6102e436600461237f565b610ea4565b6102fc6102f7366004612361565b610f05565b6040516101a79796959493929190612ea8565b610317610f91565b6040516101a79190612f11565b6101b86103323660046123e0565b610fe9565b610240610345366004612296565b611339565b61020d61035836600461237f565b611369565b6005546001600160a01b031681565b60005460ff1681565b60035460408051828152602080840282010190915260609180156103b357816020015b6103a0611fbf565b8152602001906001900390816103985790505b50905060005b60035481101561053957600381815481106103d057fe5b90600052602060002001548282815181106103e757fe5b60209081029190910101515260408051600280825260608201909252908160200160208202803883390190505082828151811061042057fe5b602002602001015160200181905250600260006003838154811061044057fe5b9060005260206000200154815260200190815260200160002060000160009054906101000a90046001600160a01b031682828151811061047c57fe5b60200260200101516020015160008151811061049457fe5b60200260200101906001600160a01b031690816001600160a01b03168152505060026000600383815481106104c557fe5b9060005260206000200154815260200190815260200160002060010160009054906101000a90046001600160a01b031682828151811061050157fe5b60200260200101516020015160018151811061051957fe5b6001600160a01b03909216602092830291909101909101526001016103b9565b5090565b610545611fd7565b50600081815260026020818152604092839020835160e08101855281546001600160a01b03908116825260018301548116828501529382015484168186015260038201546060808301919091526004830154608080840191909152600584015460a080850191909152875160c08181018a5260068701549282019283529181526007860154881696810196909652600885015497860197909752600984015491850191909152600a9092015491830191909152928301528151166106245760405162461bcd60e51b815260040161061b9061307e565b60405180910390fd5b919050565b6003818154811061063657fe5b600091825260209091200154905081565b6001546000906001600160a01b031633146106745760405162461bcd60e51b815260040161061b90612fee565b61067c611fd7565b6106858661053d565b905060006106958287878761138a565b90935090506106a887838887878661158c565b5050949350505050565b6106ba6107fc565b6106d65760405162461bcd60e51b815260040161061b9061304e565b6001600160a01b0381166106fc5760405162461bcd60e51b815260040161061b9061308e565b600580547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081179091556040517f590fd633a008765ce9e65e8081adfba311e99e11b958a5ecb5000ea3355f735390600090a250565b6107666107fc565b6107825760405162461bcd60e51b815260040161061b9061304e565b600080546040516101009091046001600160a01b0316907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0908390a3600080547fffffffffffffffffffffff0000000000000000000000000000000000000000ff169055565b60005461010090046001600160a01b031690565b6000805461010090046001600160a01b0316610816611665565b6001600160a01b031614905090565b600061082f611fd7565b6108388661053d565b90506108468186868661138a565b509695505050505050565b6108596107fc565b6108755760405162461bcd60e51b815260040161061b9061304e565b6001600160a01b03811661089b5760405162461bcd60e51b815260040161061b9061309e565b600480547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081179091556040517f0b248ab246a87e452fbedee8dc12dfc53e3f3bfdc6920999062c56dea4ab522a90600090a250565b6001546001600160a01b031681565b60006109166107fc565b6109325760405162461bcd60e51b815260040161061b9061304e565b61093a611fd7565b61094936849003840184612482565b60408101519091506001600160a01b03166109765760405162461bcd60e51b815260040161061b9061302e565b80516001600160a01b031661099d5760405162461bcd60e51b815260040161061b90612f9e565b60208101516001600160a01b03166109c75760405162461bcd60e51b815260040161061b9061305e565b80600001516001600160a01b03166395d89b416040518163ffffffff1660e01b815260040160006040518083038186803b158015610a0457600080fd5b505afa158015610a18573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610a40919081019061242e565b81602001516001600160a01b03166395d89b416040518163ffffffff1660e01b815260040160006040518083038186803b158015610a7d57600080fd5b505afa158015610a91573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610ab9919081019061242e565b82604001516001600160a01b03166306fdde036040518163ffffffff1660e01b815260040160006040518083038186803b158015610af657600080fd5b505afa158015610b0a573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610b32919081019061242e565b604051602001610b4493929190612e76565b60408051601f198184030181529181528151602092830120600081815260029093529120549092506001600160a01b031615610b925760405162461bcd60e51b815260040161061b90612fce565b610b9b81611669565b600080610ba7836118d9565b915091508183606001818152505080836080018181525050826002600086815260200190815260200160002060008201518160000160006101000a8154816001600160a01b0302191690836001600160a01b0316021790555060208201518160010160006101000a8154816001600160a01b0302191690836001600160a01b0316021790555060408201518160020160006101000a8154816001600160a01b0302191690836001600160a01b03160217905550606082015181600301556080820151816004015560a0820151816005015560c08201518160060160008201518160000160008201518160000155505060208201518160010160006101000a8154816001600160a01b0302191690836001600160a01b031602179055506040820151816002015560608201518160030155608082015181600401555050905050600384908060018154018082558091505090600182039060005260206000200160009091929091909150555082602001516001600160a01b031683600001516001600160a01b0316857fb374789237d43aed0f75b4c50a45793fd70e354bb96eecd573f35f5d509d78a18660400151604051610d629190612e9a565b60405180910390a4505050919050565b610d7a6107fc565b610d965760405162461bcd60e51b815260040161061b9061304e565b6001600160a01b038116610dbc5760405162461bcd60e51b815260040161061b906130be565b600180547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081179091556040517f865dab7821134b6eb27cba259b40e33bbc1b898e970a535a18a83147f380a51f90600090a250565b60005460ff1615610e415760405162461bcd60e51b815260040161061b90612f7e565b600080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00166001179055610e7533611929565b610e7e83610d72565b610e8782610851565b610e90816106b2565b505050565b6004546001600160a01b031681565b6001546000906001600160a01b03163314610ed15760405162461bcd60e51b815260040161061b90612fee565b610ed9611fd7565b610ee28661053d565b90506000610ef2828787876119cc565b90935090506106a887838886888661158c565b6002602081815260009283526040928390208054600182015493820154600383015460048401546005850154885160c081018a52600687015460a08201908152815260078701546001600160a01b0390811698820198909852600887015499810199909952600986015460608a0152600a90950154608089015292851696958516959190941693929087565b60606003805480602002602001604051908101604052809291908181526020018280548015610fdf57602002820191906000526020600020905b815481526020019060010190808311610fcb575b5050505050905090565b6000610ff36107fc565b61100f5760405162461bcd60e51b815260040161061b9061304e565b60035482106110305760405162461bcd60e51b815260040161061b90612fae565b826003838154811061103e57fe5b9060005260206000200154146110665760405162461bcd60e51b815260040161061b9061306e565b61106e611fd7565b600260008581526020019081526020016000206040518060e00160405290816000820160009054906101000a90046001600160a01b03166001600160a01b03166001600160a01b031681526020016001820160009054906101000a90046001600160a01b03166001600160a01b03166001600160a01b031681526020016002820160009054906101000a90046001600160a01b03166001600160a01b03166001600160a01b03168152602001600382015481526020016004820154815260200160058201548152602001600682016040518060a00160405290816000820160405180602001604052908160008201548152505081526020016001820160009054906101000a90046001600160a01b03166001600160a01b03166001600160a01b03168152602001600282015481526020016003820154815260200160048201548152505081525050905060026000858152602001908152602001600020600080820160006101000a8154906001600160a01b0302191690556001820160006101000a8154906001600160a01b0302191690556002820160006101000a8154906001600160a01b03021916905560038201600090556004820160009055600582016000905560068201600080820160008082016000905550506001820160006101000a8154906001600160a01b0302191690556002820160009055600382016000905560048201600090555050505060036001600380549050038154811061129157fe5b9060005260206000200154600384815481106112a957fe5b60009182526020909120015560038054806112c057fe5b600190038181906000526020600020016000905590556001915080602001516001600160a01b031681600001516001600160a01b0316857fadbbec6c203cb0248e89fe6d15ad651061a9d5203a1ab0273256e2b7decffa89846040015160405161132a9190612e9a565b60405180910390a45092915050565b6113416107fc565b61135d5760405162461bcd60e51b815260040161061b9061304e565b61136681611929565b50565b6000611373611fd7565b61137c8661053d565b9050610846818686866119cc565b60008085600001516001600160a01b0316856001600160a01b03161480156113c7575085602001516001600160a01b0316846001600160a01b0316145b806113fe575085602001516001600160a01b0316856001600160a01b03161480156113fe575085516001600160a01b038581169116145b61141a5760405162461bcd60e51b815260040161061b906130ae565b61142386611adb565b815191975091506001600160a01b03868116911614156114e25785604001516001600160a01b03166352707d8c8760600151886080015161146b8a60c0015160000151611b10565b876040518563ffffffff1660e01b815260040161148b94939291906130f8565b60206040518083038186803b1580156114a357600080fd5b505afa1580156114b7573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506114db91908101906124a1565b9150611583565b85604001516001600160a01b03166352707d8c876080015188606001516115108a60c0015160000151611b10565b876040518563ffffffff1660e01b815260040161153094939291906130f8565b60206040518083038186803b15801561154857600080fd5b505afa15801561155c573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061158091908101906124a1565b91505b94509492505050565b80156115f057426002600088815260200190815260200160002060050181905550857f949f5db193cbfa01f2d443b1c656bfede15497de0d86838cea089bd60c438342866060015187608001516040516115e79291906130dd565b60405180910390a25b84516001600160a01b038581169116141561163357606085015160008781526002602052604090209084016003820155608086015183900360049091015561165d565b60608501516000878152600260205260409020908390036003820155608086015184016004909101555b505050505050565b3390565b6004805482516040517f4f8e6e230000000000000000000000000000000000000000000000000000000081526001600160a01b0390921692634f8e6e23926116b2929101612e9a565b60206040518083038186803b1580156116ca57600080fd5b505afa1580156116de573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506117029190810190612309565b61171e5760405162461bcd60e51b815260040161061b90612f6e565b6004805460208301516040517f4f8e6e230000000000000000000000000000000000000000000000000000000081526001600160a01b0390921692634f8e6e239261176a929101612e9a565b60206040518083038186803b15801561178257600080fd5b505afa158015611796573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506117ba9190810190612309565b8061185c57506004805460208301516040517fcae182fe0000000000000000000000000000000000000000000000000000000081526001600160a01b039092169263cae182fe9261180c929101612e9a565b60206040518083038186803b15801561182457600080fd5b505afa158015611838573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061185c9190810190612309565b6118785760405162461bcd60e51b815260040161061b90612fde565b60c08101515161188f9061188a611b14565b611b38565b6118ab5760405162461bcd60e51b815260040161061b9061300e565b60c0810151602001516001600160a01b03166113665760405162461bcd60e51b815260040161061b9061301e565b6000808260c001516080015191506000806118fb8560c0015160200151611b45565b909250905061192082611914838763ffffffff611c1216565b9063ffffffff611c5316565b92505050915091565b6001600160a01b03811661194f5760405162461bcd60e51b815260040161061b90612f8e565b600080546040516001600160a01b038085169361010090930416917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e091a3600080546001600160a01b03909216610100027fffffffffffffffffffffff0000000000000000000000000000000000000000ff909216919091179055565b60008085600001516001600160a01b0316856001600160a01b0316148015611a09575085602001516001600160a01b0316846001600160a01b0316145b80611a40575085602001516001600160a01b0316856001600160a01b0316148015611a40575085516001600160a01b038581169116145b611a5c5760405162461bcd60e51b815260040161061b906130ae565b611a6586611adb565b815191975091506001600160a01b0386811691161415611aad5785604001516001600160a01b031663571fd0128760600151886080015161146b8a60c0015160000151611b10565b85604001516001600160a01b031663571fd012876080015188606001516115108a60c0015160000151611b10565b611ae3611fd7565b6000611aee83611c95565b15611b0a57611afc836118d9565b608085015260608401525060015b91929050565b5190565b611b1c612034565b50604080516020810190915269d3c21bcecceda1000000815290565b8051825111155b92915050565b6005546040517fef90e1b00000000000000000000000000000000000000000000000000000000081526000918291829182916001600160a01b039091169063ef90e1b090611b97908890600401612e9a565b604080518083038186803b158015611bae57600080fd5b505afa158015611bc2573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611be691908101906124bf565b909250905080611c085760405162461bcd60e51b815260040161061b90612ffe565b9092509050915091565b600082611c2157506000611b3f565b82820282848281611c2e57fe5b0414611c4c5760405162461bcd60e51b815260040161061b9061303e565b9392505050565b6000611c4c83836040518060400160405280601a81526020017f536166654d6174683a206469766973696f6e206279207a65726f000000000000815250611efa565b60055460c0820151602001516040517fffe736bf00000000000000000000000000000000000000000000000000000000815260009283926001600160a01b039091169163ffe736bf91611cea91600401612e9a565b604080518083038186803b158015611d0157600080fd5b505afa158015611d15573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611d399190810190612327565b5090506000611d5d8460c00151604001518560a00151611f3190919063ffffffff16565b60c085015160608101516005546020909201516040517fbbc66a94000000000000000000000000000000000000000000000000000000008152429490941015945060009391926001600160a01b03169163bbc66a9491611dbf91600401612e9a565b60206040518083038186803b158015611dd757600080fd5b505afa158015611deb573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611e0f91908101906124a1565b101590506000611e308660c001516040015142611f5690919063ffffffff16565b60055460c0880151602001516040517f071b48fc0000000000000000000000000000000000000000000000000000000081526001600160a01b039092169163071b48fc91611e8091600401612e9a565b60206040518083038186803b158015611e9857600080fd5b505afa158015611eac573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611ed091908101906124a1565b119050828015611edd5750815b8015611ee65750805b8015611ef0575083155b9695505050505050565b60008183611f1b5760405162461bcd60e51b815260040161061b9190612f5d565b506000838581611f2757fe5b0495945050505050565b600082820183811015611c4c5760405162461bcd60e51b815260040161061b90612fbe565b6000611c4c83836040518060400160405280601e81526020017f536166654d6174683a207375627472616374696f6e206f766572666c6f77000081525060008184841115611fb75760405162461bcd60e51b815260040161061b9190612f5d565b505050900390565b60408051808201909152600081526060602082015290565b6040518060e0016040528060006001600160a01b0316815260200160006001600160a01b0316815260200160006001600160a01b0316815260200160008152602001600081526020016000815260200161202f612047565b905290565b6040518060200160405280600081525090565b6040518060a0016040528061205a612034565b815260200160006001600160a01b031681526020016000815260200160008152602001600081525090565b8035611b3f816131eb565b8051611b3f816131eb565b8051611b3f816131ff565b8035611b3f81613208565b8035611b3f81613211565b600082601f8301126120cd57600080fd5b81516120e06120db82613154565b61312d565b915080825260208301602083018583830111156120fc57600080fd5b6121078382846131b5565b50505092915050565b60006020828403121561212257600080fd5b61212c602061312d565b9050600061213a84846120a6565b82525092915050565b600060a0828403121561215557600080fd5b61215f60a061312d565b9050600061216d8484612110565b825250602061217e84848301612085565b6020830152506040612192848285016120a6565b60408301525060606121a6848285016120a6565b60608301525060806121ba848285016120a6565b60808301525092915050565b600061016082840312156121d957600080fd5b50919050565b600061016082840312156121f257600080fd5b6121fc60e061312d565b9050600061220a8484612085565b825250602061221b84848301612085565b602083015250604061222f848285016120b1565b6040830152506060612243848285016120a6565b6060830152506080612257848285016120a6565b60808301525060a061226b848285016120a6565b60a08301525060c061227f84828501612143565b60c08301525092915050565b8051611b3f81613208565b6000602082840312156122a857600080fd5b60006122b48484612085565b949350505050565b6000806000606084860312156122d157600080fd5b60006122dd8686612085565b93505060206122ee868287016120b1565b92505060406122ff868287016120b1565b9150509250925092565b60006020828403121561231b57600080fd5b60006122b4848461209b565b6000806040838503121561233a57600080fd5b6000612346858561209b565b925050602061235785828601612090565b9150509250929050565b60006020828403121561237357600080fd5b60006122b484846120a6565b6000806000806080858703121561239557600080fd5b60006123a187876120a6565b94505060206123b287828801612085565b93505060406123c387828801612085565b92505060606123d4878288016120a6565b91505092959194509250565b600080604083850312156123f357600080fd5b60006123ff85856120a6565b9250506020612357858286016120a6565b60006020828403121561242257600080fd5b60006122b484846120b1565b60006020828403121561244057600080fd5b815167ffffffffffffffff81111561245757600080fd5b6122b4848285016120bc565b6000610160828403121561247657600080fd5b60006122b484846121c6565b6000610160828403121561249557600080fd5b60006122b484846121df565b6000602082840312156124b357600080fd5b60006122b4848461228b565b600080604083850312156124d257600080fd5b60006124de858561228b565b92505060206123578582860161228b565b60006124fb838361251b565b505060200190565b60006124fb8383612648565b6000611c4c8383612d49565b6125248161318b565b82525050565b600061253582611b10565b61253f8185613182565b935061254a8361317c565b8060005b8381101561257857815161256288826124ef565b975061256d8361317c565b92505060010161254e565b509495945050505050565b600061258e82611b10565b6125988185613182565b93506125a38361317c565b8060005b838110156125785781516125bb8882612503565b97506125c68361317c565b9250506001016125a7565b60006125dc82611b10565b6125e68185613182565b9350836020820285016125f88561317c565b8060005b858110156126325784840389528151612615858261250f565b94506126208361317c565b60209a909a01999250506001016125fc565b5091979650505050505050565b61252481613196565b6125248161319b565b6125248161319e565b600061266582611b10565b61266f8185613182565b935061267f8185602086016131b5565b612688816131e1565b9093019392505050565b600061269d82611b10565b6126a78185610624565b93506126b78185602086016131b5565b9290920192915050565b60006126ce603383613182565b7f617373657430206d757374206265206120737461626c6520726567697374657281527f6564207769746820746865207265736572766500000000000000000000000000602082015260400192915050565b600061272d601c83613182565b7f636f6e747261637420616c726561647920696e697469616c697a656400000000815260200192915050565b6000612766602683613182565b7f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206181527f6464726573730000000000000000000000000000000000000000000000000000602082015260400192915050565b60006127c5601283613182565b7f617373657430206d757374206265207365740000000000000000000000000000815260200192915050565b60006127fe601c83613182565b7f65786368616e67654964496e646578206e6f7420696e2072616e676500000000815260200192915050565b6000612837601b83613182565b7f536166654d6174683a206164646974696f6e206f766572666c6f770000000000815260200192915050565b6000612870603983613182565b7f416e2065786368616e676520776974682074686520737065636966696564206181527f737365747320616e642065786368616e67652065786973747300000000000000602082015260400192915050565b60006128cf602583613182565b7f617373657431206d757374206265206120737461626c65206f7220636f6c6c6181527f746572616c000000000000000000000000000000000000000000000000000000602082015260400192915050565b600061292e601883613182565b7f43616c6c6572206973206e6f74207468652042726f6b65720000000000000000815260200192915050565b6000612967603083613182565b7f65786368616e676520726174652064656e6f6d696e61746f72206d757374206281527f652067726561746572207468616e203000000000000000000000000000000000602082015260400192915050565b60006129c6602683613182565b7f737072656164206d757374206265206c657373207468616e206f72206571756181527f6c20746f20310000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612a25601e83613182565b7f6f7261636c655265706f7274546172676574206d757374206265207365740000815260200192915050565b6000612a5e601983613182565b7f70726963696e674d6f64756c65206d7573742062652073657400000000000000815260200192915050565b6000612a97602183613182565b7f536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f81527f7700000000000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612af6602083613182565b7f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572815260200192915050565b6000612b2f601283613182565b7f617373657431206d757374206265207365740000000000000000000000000000815260200192915050565b6000612b68602183613182565b7f65786368616e6765496420617420696e64657820646f65736e2774206d61746381527f6800000000000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612bc7603083613182565b7f416e2065786368616e676520776974682074686520737065636966696564206981527f6420646f6573206e6f7420657869737400000000000000000000000000000000602082015260400192915050565b6000612c26602183613182565b7f536f727465644f7261636c65732061646472657373206d75737420626520736581527f7400000000000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000612c85601b83613182565b7f526573657276652061646472657373206d757374206265207365740000000000815260200192915050565b6000612cbe602883613182565b7f746f6b656e496e20616e6420746f6b656e4f7574206d757374206d617463682081527f65786368616e6765000000000000000000000000000000000000000000000000602082015260400192915050565b6000612d1d601a83613182565b7f42726f6b65722061646472657373206d75737420626520736574000000000000815260200192915050565b80516000906040840190612d5d8582612648565b5060208301518482036020860152612d75828261252a565b95945050505050565b80516020830190612d8f8482612648565b50505050565b805160a0830190612da68482612d7e565b506020820151612db9602085018261251b565b506040820151612dcc6040850182612648565b506060820151612ddf6060850182612648565b506080820151612d8f6080850182612648565b8051610160830190612e04848261251b565b506020820151612e17602085018261251b565b506040820151612e2a6040850182612651565b506060820151612e3d6060850182612648565b506080820151612e506080850182612648565b5060a0820151612e6360a0850182612648565b5060c0820151612d8f60c0850182612d95565b6000612e828286612692565b9150612e8e8285612692565b9150612d758284612692565b60208101611b3f828461251b565b6101608101612eb7828a61251b565b612ec4602083018961251b565b612ed16040830188612651565b612ede6060830187612648565b612eeb6080830186612648565b612ef860a0830185612648565b612f0560c0830184612d95565b98975050505050505050565b60208082528101611c4c8184612583565b60208082528101611c4c81846125d1565b60208101611b3f828461263f565b60208101611b3f8284612648565b60208101611b3f8284612651565b60208082528101611c4c818461265a565b60208082528101611b3f816126c1565b60208082528101611b3f81612720565b60208082528101611b3f81612759565b60208082528101611b3f816127b8565b60208082528101611b3f816127f1565b60208082528101611b3f8161282a565b60208082528101611b3f81612863565b60208082528101611b3f816128c2565b60208082528101611b3f81612921565b60208082528101611b3f8161295a565b60208082528101611b3f816129b9565b60208082528101611b3f81612a18565b60208082528101611b3f81612a51565b60208082528101611b3f81612a8a565b60208082528101611b3f81612ae9565b60208082528101611b3f81612b22565b60208082528101611b3f81612b5b565b60208082528101611b3f81612bba565b60208082528101611b3f81612c19565b60208082528101611b3f81612c78565b60208082528101611b3f81612cb1565b60208082528101611b3f81612d10565b6101608101611b3f8284612df2565b604081016130eb8285612648565b611c4c6020830184612648565b608081016131068287612648565b6131136020830186612648565b6131206040830185612648565b612d756060830184612648565b60405181810167ffffffffffffffff8111828210171561314c57600080fd5b604052919050565b600067ffffffffffffffff82111561316b57600080fd5b506020601f91909101601f19160190565b60200190565b90815260200190565b6000611b3f826131a9565b151590565b90565b6000611b3f8261318b565b6001600160a01b031690565b60005b838110156131d05781810151838201526020016131b8565b83811115612d8f5750506000910152565b601f01601f191690565b6131f48161318b565b811461136657600080fd5b6131f481613196565b6131f48161319b565b6131f48161319e56fea365627a7a72315820971b646468f58f9e70927520e43212cc3da3e28be5f408bcf765b7531052a77b6c6578706572696d656e74616cf564736f6c63430005110040