Overview
Max Total Supply
324,347.743409830664531369 PcDAI
Holders
3,192 (0.00%)
Market
Onchain Market Cap
$0.00
Circulating Supply Market Cap
-
Other Info
Token Contract (WITH 18 Decimals)
Balance
15 PcDAIValue
$0.00Loading...
Loading
Loading...
Loading
Loading...
Loading
# | Exchange | Pair | Price | 24H Volume | % Volume |
---|
Minimal Proxy Contract for 0x09576c5af80167fc410eb33f85c74a891832085f
Contract Name:
Ticket
Compiler Version
v0.6.12+commit.27d51765
Optimization Enabled:
Yes with 200 runs
Other Settings:
default evmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "sortition-sum-tree-factory/contracts/SortitionSumTreeFactory.sol"; import "@pooltogether/uniform-random-number/contracts/UniformRandomNumber.sol"; import "./ControlledToken.sol"; import "./TicketInterface.sol"; contract Ticket is ControlledToken, TicketInterface { using SortitionSumTreeFactory for SortitionSumTreeFactory.SortitionSumTrees; bytes32 constant private TREE_KEY = keccak256("PoolTogether/Ticket"); uint256 constant private MAX_TREE_LEAVES = 5; // Ticket-weighted odds SortitionSumTreeFactory.SortitionSumTrees internal sortitionSumTrees; /// @notice Initializes the Controlled Token with Token Details and the Controller /// @param _name The name of the Token /// @param _symbol The symbol for the Token /// @param _decimals The number of decimals for the Token /// @param _trustedForwarder Address of the Forwarding Contract for GSN Meta-Txs /// @param _controller Address of the Controller contract for minting & burning function initialize( string memory _name, string memory _symbol, uint8 _decimals, address _trustedForwarder, TokenControllerInterface _controller ) public virtual override initializer { super.initialize(_name, _symbol, _decimals, _trustedForwarder, _controller); sortitionSumTrees.createTree(TREE_KEY, MAX_TREE_LEAVES); } /// @notice Returns the user's chance of winning. function chanceOf(address user) external view returns (uint256) { return sortitionSumTrees.stakeOf(TREE_KEY, bytes32(uint256(user))); } /// @notice Selects a user using a random number. The random number will be uniformly bounded to the ticket totalSupply. /// @param randomNumber The random number to use to select a user. /// @return The winner function draw(uint256 randomNumber) public view override returns (address) { uint256 bound = totalSupply(); address selected; if (bound == 0) { selected = address(0); } else { uint256 token = UniformRandomNumber.uniform(randomNumber, bound); selected = address(uint256(sortitionSumTrees.draw(TREE_KEY, token))); } return selected; } /// @dev Controller hook to provide notifications & rule validations on token transfers to the controller. /// This includes minting and burning. /// May be overridden to provide more granular control over operator-burning /// @param from Address of the account sending the tokens (address(0x0) on minting) /// @param to Address of the account receiving the tokens (address(0x0) on burning) /// @param amount Amount of tokens being transferred function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override { super._beforeTokenTransfer(from, to, amount); if (from != address(0)) { uint256 fromBalance = balanceOf(from).sub(amount); sortitionSumTrees.set(TREE_KEY, fromBalance, bytes32(uint256(from))); } if (to != address(0)) { uint256 toBalance = balanceOf(to).add(amount); sortitionSumTrees.set(TREE_KEY, toBalance, bytes32(uint256(to))); } } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; pragma experimental ABIEncoderV2; import "./PrizePoolBuilder.sol"; import "./SingleRandomWinnerBuilder.sol"; import "../registry/RegistryInterface.sol"; import "../prize-strategy/single-random-winner/SingleRandomWinnerProxyFactory.sol"; import "../prize-pool/compound/CompoundPrizePoolProxyFactory.sol"; import "../token/ControlledTokenProxyFactory.sol"; import "../token/TicketProxyFactory.sol"; import "../external/compound/CTokenInterface.sol"; import "../external/openzeppelin/OpenZeppelinProxyFactoryInterface.sol"; /// @title Builds new Compound Prize Pools /* solium-disable security/no-block-members */ contract CompoundPrizePoolBuilder is PrizePoolBuilder { using SafeMath for uint256; using SafeCast for uint256; struct CompoundPrizePoolConfig { CTokenInterface cToken; uint256 maxExitFeeMantissa; uint256 maxTimelockDuration; } RegistryInterface public reserveRegistry; CompoundPrizePoolProxyFactory public compoundPrizePoolProxyFactory; SingleRandomWinnerBuilder public singleRandomWinnerBuilder; address public trustedForwarder; constructor ( RegistryInterface _reserveRegistry, address _trustedForwarder, CompoundPrizePoolProxyFactory _compoundPrizePoolProxyFactory, SingleRandomWinnerBuilder _singleRandomWinnerBuilder ) public { require(address(_reserveRegistry) != address(0), "CompoundPrizePoolBuilder/reserveRegistry-not-zero"); require(address(_singleRandomWinnerBuilder) != address(0), "CompoundPrizePoolBuilder/single-random-winner-builder-not-zero"); require(address(_compoundPrizePoolProxyFactory) != address(0), "CompoundPrizePoolBuilder/compound-prize-pool-builder-not-zero"); reserveRegistry = _reserveRegistry; singleRandomWinnerBuilder = _singleRandomWinnerBuilder; trustedForwarder = _trustedForwarder; compoundPrizePoolProxyFactory = _compoundPrizePoolProxyFactory; } function createSingleRandomWinner( CompoundPrizePoolConfig calldata prizePoolConfig, SingleRandomWinnerBuilder.SingleRandomWinnerConfig calldata prizeStrategyConfig, uint8 decimals ) external returns (CompoundPrizePool) { CompoundPrizePool prizePool = compoundPrizePoolProxyFactory.create(); SingleRandomWinner prizeStrategy = singleRandomWinnerBuilder.createSingleRandomWinner( prizePool, prizeStrategyConfig, decimals, msg.sender ); address[] memory tokens; prizePool.initialize( trustedForwarder, reserveRegistry, tokens, prizePoolConfig.maxExitFeeMantissa, prizePoolConfig.maxTimelockDuration, prizePoolConfig.cToken ); _setupSingleRandomWinner( prizePool, prizeStrategy, prizeStrategyConfig.ticketCreditRateMantissa, prizeStrategyConfig.ticketCreditLimitMantissa ); prizePool.transferOwnership(msg.sender); emit PrizePoolCreated(msg.sender, address(prizePool)); return prizePool; } function createCompoundPrizePool( CompoundPrizePoolConfig calldata config ) external returns (CompoundPrizePool) { CompoundPrizePool prizePool = compoundPrizePoolProxyFactory.create(); address[] memory tokens; prizePool.initialize( trustedForwarder, reserveRegistry, tokens, config.maxExitFeeMantissa, config.maxTimelockDuration, config.cToken ); prizePool.transferOwnership(msg.sender); emit PrizePoolCreated(msg.sender, address(prizePool)); return prizePool; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "../prize-pool/PrizePool.sol"; import "../prize-strategy/single-random-winner/SingleRandomWinner.sol"; contract PrizePoolBuilder { using SafeCast for uint256; event PrizePoolCreated ( address indexed creator, address indexed prizePool ); function _setupSingleRandomWinner( PrizePool prizePool, SingleRandomWinner singleRandomWinner, uint256 ticketCreditRateMantissa, uint256 ticketCreditLimitMantissa ) internal { address ticket = address(singleRandomWinner.ticket()); prizePool.setPrizeStrategy(singleRandomWinner); prizePool.addControlledToken(ticket); prizePool.addControlledToken(address(singleRandomWinner.sponsorship())); prizePool.setCreditPlanOf( ticket, ticketCreditRateMantissa.toUint128(), ticketCreditLimitMantissa.toUint128() ); } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/access/Ownable.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/utils/SafeCast.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/utils/ReentrancyGuard.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC721/IERC721.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/SafeERC20.sol"; import "../external/pooltogether/FixedPoint.sol"; import "../registry/RegistryInterface.sol"; import "../reserve/ReserveInterface.sol"; import "./YieldSource.sol"; import "../token/TokenListenerInterface.sol"; import "../token/ControlledToken.sol"; import "../token/TokenControllerInterface.sol"; import "../utils/MappedSinglyLinkedList.sol"; import "../utils/RelayRecipient.sol"; import "./PrizePoolInterface.sol"; /// @title Escrows assets and deposits them into a yield source. Exposes interest to Prize Strategy. Users deposit and withdraw from this contract to participate in Prize Pool. /// @notice Accounting is managed using Controlled Tokens, whose mint and burn functions can only be called by this contract. /// @dev Must be inherited to provide specific yield-bearing asset control, such as Compound cTokens abstract contract PrizePool is PrizePoolInterface, YieldSource, OwnableUpgradeSafe, RelayRecipient, ReentrancyGuardUpgradeSafe, TokenControllerInterface { using SafeMath for uint256; using SafeCast for uint256; using SafeERC20 for IERC20; using MappedSinglyLinkedList for MappedSinglyLinkedList.Mapping; /// @dev Emitted when an instance is initialized event Initialized( address trustedForwarder, address reserveRegistry, uint256 maxExitFeeMantissa, uint256 maxTimelockDuration ); /// @dev Event emitted when controlled token is added event ControlledTokenAdded( address indexed token ); /// @dev Emitted when reserve is captured. event ReserveFeeCaptured( uint256 amount ); event AwardCaptured( uint256 amount ); /// @dev Event emitted when assets are deposited event Deposited( address indexed operator, address indexed to, address indexed token, uint256 amount, address referrer ); /// @dev Event emitted when timelocked funds are re-deposited event TimelockDeposited( address indexed operator, address indexed to, address indexed token, uint256 amount ); /// @dev Event emitted when interest is awarded to a winner event Awarded( address indexed winner, address indexed token, uint256 amount ); /// @dev Event emitted when external ERC20s are awarded to a winner event AwardedExternalERC20( address indexed winner, address indexed token, uint256 amount ); /// @dev Event emitted when external ERC20s are transferred out event TransferredExternalERC20( address indexed to, address indexed token, uint256 amount ); /// @dev Event emitted when external ERC721s are awarded to a winner event AwardedExternalERC721( address indexed winner, address indexed token, uint256[] tokenIds ); /// @dev Event emitted when assets are withdrawn instantly event InstantWithdrawal( address indexed operator, address indexed from, address indexed token, uint256 amount, uint256 redeemed, uint256 exitFee ); /// @dev Event emitted upon a withdrawal with timelock event TimelockedWithdrawal( address indexed operator, address indexed from, address indexed token, uint256 amount, uint256 unlockTimestamp ); event ReserveWithdrawal( address indexed to, uint256 amount ); /// @dev Event emitted when timelocked funds are swept back to a user event TimelockedWithdrawalSwept( address indexed operator, address indexed from, uint256 amount, uint256 redeemed ); /// @dev Event emitted when the Liquidity Cap is set event LiquidityCapSet( uint256 liquidityCap ); /// @dev Event emitted when the Credit plan is set event CreditPlanSet( address token, uint128 creditLimitMantissa, uint128 creditRateMantissa ); /// @dev Event emitted when the Prize Strategy is set event PrizeStrategySet( address indexed prizeStrategy ); /// @dev Emitted when credit is minted event CreditMinted( address indexed user, address indexed token, uint256 amount ); /// @dev Emitted when credit is burned event CreditBurned( address indexed user, address indexed token, uint256 amount ); struct CreditPlan { uint128 creditLimitMantissa; uint128 creditRateMantissa; } struct CreditBalance { uint192 balance; uint32 timestamp; bool initialized; } /// @dev Reserve to which reserve fees are sent RegistryInterface public reserveRegistry; /// @dev A linked list of all the controlled tokens MappedSinglyLinkedList.Mapping internal _tokens; /// @dev The Prize Strategy that this Prize Pool is bound to. TokenListenerInterface public prizeStrategy; /// @dev The maximum possible exit fee fraction as a fixed point 18 number. /// For example, if the maxExitFeeMantissa is "0.1 ether", then the maximum exit fee for a withdrawal of 100 Dai will be 10 Dai uint256 public maxExitFeeMantissa; /// @dev The maximum possible timelock duration for a timelocked withdrawal (in seconds). uint256 public maxTimelockDuration; /// @dev The total funds that are timelocked. uint256 public timelockTotalSupply; /// @dev The total funds that have been allocated to the reserve uint256 public reserveTotalSupply; /// @dev The total amount of funds that the prize pool can hold. uint256 public liquidityCap; /// @dev the The awardable balance uint256 internal _currentAwardBalance; /// @dev The timelocked balances for each user mapping(address => uint256) internal _timelockBalances; /// @dev The unlock timestamps for each user mapping(address => uint256) internal _unlockTimestamps; /// @dev Stores the credit plan for each token. mapping(address => CreditPlan) internal tokenCreditPlans; /// @dev Stores each users balance of credit per token. mapping(address => mapping(address => CreditBalance)) internal tokenCreditBalances; /// @notice Initializes the Prize Pool /// @param _trustedForwarder Address of the Forwarding Contract for GSN Meta-Txs /// @param _controlledTokens Array of ControlledTokens that are controlled by this Prize Pool. /// @param _maxExitFeeMantissa The maximum exit fee size /// @param _maxTimelockDuration The maximum length of time the withdraw timelock function initialize ( address _trustedForwarder, RegistryInterface _reserveRegistry, address[] memory _controlledTokens, uint256 _maxExitFeeMantissa, uint256 _maxTimelockDuration ) public initializer { require(address(_reserveRegistry) != address(0), "PrizePool/reserveRegistry-not-zero"); require(_trustedForwarder != address(0), "PrizePool/forwarder-not-zero"); _tokens.initialize(); for (uint256 i = 0; i < _controlledTokens.length; i++) { _addControlledToken(_controlledTokens[i]); } __Ownable_init(); __ReentrancyGuard_init(); _setLiquidityCap(uint256(-1)); trustedForwarder = _trustedForwarder; reserveRegistry = _reserveRegistry; maxExitFeeMantissa = _maxExitFeeMantissa; maxTimelockDuration = _maxTimelockDuration; emit Initialized( _trustedForwarder, address(_reserveRegistry), maxExitFeeMantissa, maxTimelockDuration ); } /// @dev Returns the address of the underlying ERC20 asset /// @return The address of the asset function token() external override view returns (IERC20) { return _token(); } /// @dev Returns the total underlying balance of all assets. This includes both principal and interest. /// @return The underlying balance of assets function balance() external returns (uint256) { return _balance(); } /// @dev Checks with the Prize Pool if a specific token type may be awarded as an external prize /// @param _externalToken The address of the token to check /// @return True if the token may be awarded, false otherwise function canAwardExternal(address _externalToken) external view returns (bool) { return _canAwardExternal(_externalToken); } /// @notice Deposits timelocked tokens for a user back into the Prize Pool as another asset. /// @param to The address receiving the tokens /// @param amount The amount of timelocked assets to re-deposit /// @param controlledToken The type of token to be minted in exchange (i.e. tickets or sponsorship) function timelockDepositTo( address to, uint256 amount, address controlledToken ) external onlyControlledToken(controlledToken) canAddLiquidity(amount) nonReentrant { address operator = _msgSender(); _mint(to, amount, controlledToken, address(0)); _timelockBalances[operator] = _timelockBalances[operator].sub(amount); timelockTotalSupply = timelockTotalSupply.sub(amount); emit TimelockDeposited(operator, to, controlledToken, amount); } /// @notice Deposit assets into the Prize Pool in exchange for tokens /// @param to The address receiving the newly minted tokens /// @param amount The amount of assets to deposit /// @param controlledToken The address of the type of token the user is minting /// @param referrer The referrer of the deposit function depositTo( address to, uint256 amount, address controlledToken, address referrer ) external override onlyControlledToken(controlledToken) canAddLiquidity(amount) nonReentrant { address operator = _msgSender(); _mint(to, amount, controlledToken, referrer); _token().safeTransferFrom(operator, address(this), amount); _supply(amount); emit Deposited(operator, to, controlledToken, amount, referrer); } /// @notice Withdraw assets from the Prize Pool instantly. A fairness fee may be charged for an early exit. /// @param from The address to redeem tokens from. /// @param amount The amount of tokens to redeem for assets. /// @param controlledToken The address of the token to redeem (i.e. ticket or sponsorship) /// @param maximumExitFee The maximum exit fee the caller is willing to pay. This should be pre-calculated by the calculateExitFee() fxn. /// @return The actual exit fee paid function withdrawInstantlyFrom( address from, uint256 amount, address controlledToken, uint256 maximumExitFee ) external override nonReentrant onlyControlledToken(controlledToken) returns (uint256) { (uint256 exitFee, uint256 burnedCredit) = _calculateEarlyExitFeeLessBurnedCredit(from, controlledToken, amount); require(exitFee <= maximumExitFee, "PrizePool/exit-fee-exceeds-user-maximum"); // burn the credit _burnCredit(from, controlledToken, burnedCredit); // burn the tickets ControlledToken(controlledToken).controllerBurnFrom(_msgSender(), from, amount); // redeem the tickets less the fee uint256 amountLessFee = amount.sub(exitFee); uint256 redeemed = _redeem(amountLessFee); _token().safeTransfer(from, redeemed); emit InstantWithdrawal(_msgSender(), from, controlledToken, amount, redeemed, exitFee); return exitFee; } /// @notice Limits the exit fee to the maximum as hard-coded into the contract /// @param withdrawalAmount The amount that is attempting to be withdrawn /// @param exitFee The exit fee to check against the limit /// @return The passed exit fee if it is less than the maximum, otherwise the maximum fee is returned. function _limitExitFee(uint256 withdrawalAmount, uint256 exitFee) internal view returns (uint256) { uint256 maxFee = FixedPoint.multiplyUintByMantissa(withdrawalAmount, maxExitFeeMantissa); if (exitFee > maxFee) { exitFee = maxFee; } return exitFee; } /// @notice Withdraw assets from the Prize Pool by placing them into the timelock. /// The timelock is used to ensure that the tickets have contributed their fair share of the prize. /// @dev Note that if the user has previously timelocked funds then this contract will try to sweep them. /// If the existing timelocked funds are still locked, then the incoming /// balance is added to their existing balance and the new timelock unlock timestamp will overwrite the old one. /// @param from The address to withdraw from /// @param amount The amount to withdraw /// @param controlledToken The type of token being withdrawn /// @return The timestamp from which the funds can be swept function withdrawWithTimelockFrom( address from, uint256 amount, address controlledToken ) external override nonReentrant onlyControlledToken(controlledToken) returns (uint256) { uint256 blockTime = _currentTime(); (uint256 lockDuration, uint256 burnedCredit) = _calculateTimelockDuration(from, controlledToken, amount); uint256 unlockTimestamp = blockTime.add(lockDuration); _burnCredit(from, controlledToken, burnedCredit); ControlledToken(controlledToken).controllerBurnFrom(_msgSender(), from, amount); _mintTimelock(from, amount, unlockTimestamp); emit TimelockedWithdrawal(_msgSender(), from, controlledToken, amount, unlockTimestamp); // return the block at which the funds will be available return unlockTimestamp; } /// @notice Adds to a user's timelock balance. It will attempt to sweep before updating the balance. /// Note that this will overwrite the previous unlock timestamp. /// @param user The user whose timelock balance should increase /// @param amount The amount to increase by /// @param timestamp The new unlock timestamp function _mintTimelock(address user, uint256 amount, uint256 timestamp) internal { // Sweep the old balance, if any address[] memory users = new address[](1); users[0] = user; _sweepTimelockBalances(users); timelockTotalSupply = timelockTotalSupply.add(amount); _timelockBalances[user] = _timelockBalances[user].add(amount); _unlockTimestamps[user] = timestamp; // if the funds should already be unlocked if (timestamp <= _currentTime()) { _sweepTimelockBalances(users); } } /// @notice Updates the Prize Strategy when tokens are transferred between holders. /// @param from The address the tokens are being transferred from (0 if minting) /// @param to The address the tokens are being transferred to (0 if burning) /// @param amount The amount of tokens being trasferred function beforeTokenTransfer(address from, address to, uint256 amount) external override onlyControlledToken(msg.sender) { if (from != address(0)) { uint256 fromBeforeBalance = IERC20(msg.sender).balanceOf(from); // first accrue credit for their old balance uint256 newCreditBalance = _calculateCreditBalance(from, msg.sender, fromBeforeBalance, 0); // now limit their credit based on the new balance newCreditBalance = _applyCreditLimit(msg.sender, fromBeforeBalance.sub(amount), newCreditBalance); _updateCreditBalance(from, msg.sender, newCreditBalance); } if (to != address(0)) { _accrueCredit(to, msg.sender, IERC20(msg.sender).balanceOf(to), 0); } // if we aren't minting if (from != address(0) && address(prizeStrategy) != address(0)) { prizeStrategy.beforeTokenTransfer(from, to, amount, msg.sender); } } /// @notice Returns the balance that is available to award. /// @dev captureAwardBalance() should be called first /// @return The total amount of assets to be awarded for the current prize function awardBalance() external override view returns (uint256) { return _currentAwardBalance; } /// @notice Captures any available interest as award balance. /// @dev This function also captures the reserve fees. /// @return The total amount of assets to be awarded for the current prize function captureAwardBalance() external override nonReentrant returns (uint256) { uint256 tokenTotalSupply = _tokenTotalSupply(); // it's possible for the balance to be slightly less due to rounding errors in the underlying yield source uint256 currentBalance = _balance(); uint256 totalInterest = (currentBalance > tokenTotalSupply) ? currentBalance.sub(tokenTotalSupply) : 0; uint256 unaccountedPrizeBalance = (totalInterest > _currentAwardBalance) ? totalInterest.sub(_currentAwardBalance) : 0; if (unaccountedPrizeBalance > 0) { uint256 reserveFee = calculateReserveFee(unaccountedPrizeBalance); if (reserveFee > 0) { reserveTotalSupply = reserveTotalSupply.add(reserveFee); unaccountedPrizeBalance = unaccountedPrizeBalance.sub(reserveFee); emit ReserveFeeCaptured(reserveFee); } _currentAwardBalance = _currentAwardBalance.add(unaccountedPrizeBalance); emit AwardCaptured(unaccountedPrizeBalance); } return _currentAwardBalance; } function withdrawReserve(address to) external override onlyReserve returns (uint256) { uint256 amount = reserveTotalSupply; reserveTotalSupply = 0; uint256 redeemed = _redeem(amount); _token().safeTransfer(address(to), redeemed); emit ReserveWithdrawal(to, amount); return redeemed; } /// @notice Called by the prize strategy to award prizes. /// @dev The amount awarded must be less than the awardBalance() /// @param to The address of the winner that receives the award /// @param amount The amount of assets to be awarded /// @param controlledToken The address of the asset token being awarded function award( address to, uint256 amount, address controlledToken ) external override onlyPrizeStrategy onlyControlledToken(controlledToken) { if (amount == 0) { return; } require(amount <= _currentAwardBalance, "PrizePool/award-exceeds-avail"); _currentAwardBalance = _currentAwardBalance.sub(amount); _mint(to, amount, controlledToken, address(0)); uint256 extraCredit = _calculateEarlyExitFeeNoCredit(controlledToken, amount); _accrueCredit(to, controlledToken, IERC20(controlledToken).balanceOf(to), extraCredit); emit Awarded(to, controlledToken, amount); } /// @notice Called by the Prize-Strategy to transfer out external ERC20 tokens /// @dev Used to transfer out tokens held by the Prize Pool. Could be liquidated, or anything. /// @param to The address of the winner that receives the award /// @param amount The amount of external assets to be awarded /// @param externalToken The address of the external asset token being awarded function transferExternalERC20( address to, address externalToken, uint256 amount ) external override onlyPrizeStrategy { if (_transferOut(to, externalToken, amount)) { emit TransferredExternalERC20(to, externalToken, amount); } } /// @notice Called by the Prize-Strategy to award external ERC20 prizes /// @dev Used to award any arbitrary tokens held by the Prize Pool /// @param to The address of the winner that receives the award /// @param amount The amount of external assets to be awarded /// @param externalToken The address of the external asset token being awarded function awardExternalERC20( address to, address externalToken, uint256 amount ) external override onlyPrizeStrategy { if (_transferOut(to, externalToken, amount)) { emit AwardedExternalERC20(to, externalToken, amount); } } function _transferOut( address to, address externalToken, uint256 amount ) internal returns (bool) { require(_canAwardExternal(externalToken), "PrizePool/invalid-external-token"); if (amount == 0) { return false; } IERC20(externalToken).safeTransfer(to, amount); return true; } /// @notice Called to mint controlled tokens. Ensures that token listener callbacks are fired. /// @param to The user who is receiving the tokens /// @param amount The amount of tokens they are receiving /// @param controlledToken The token that is going to be minted /// @param referrer The user who referred the minting function _mint(address to, uint256 amount, address controlledToken, address referrer) internal { if (address(prizeStrategy) != address(0)) { prizeStrategy.beforeTokenMint(to, amount, controlledToken, referrer); } ControlledToken(controlledToken).controllerMint(to, amount); } /// @notice Called by the prize strategy to award external ERC721 prizes /// @dev Used to award any arbitrary NFTs held by the Prize Pool /// @param to The address of the winner that receives the award /// @param externalToken The address of the external NFT token being awarded /// @param tokenIds An array of NFT Token IDs to be transferred function awardExternalERC721( address to, address externalToken, uint256[] calldata tokenIds ) external override onlyPrizeStrategy { require(_canAwardExternal(externalToken), "PrizePool/invalid-external-token"); if (tokenIds.length == 0) { return; } for (uint256 i = 0; i < tokenIds.length; i++) { IERC721(externalToken).transferFrom(address(this), to, tokenIds[i]); } emit AwardedExternalERC721(to, externalToken, tokenIds); } /// @notice Calculates the reserve portion of the given amount of funds. If there is no reserve address, the portion will be zero. /// @param amount The prize amount /// @return The size of the reserve portion of the prize function calculateReserveFee(uint256 amount) public view returns (uint256) { ReserveInterface reserve = ReserveInterface(reserveRegistry.lookup()); if (address(reserve) == address(0)) { return 0; } uint256 reserveRateMantissa = reserve.reserveRateMantissa(address(this)); if (reserveRateMantissa == 0) { return 0; } return FixedPoint.multiplyUintByMantissa(amount, reserveRateMantissa); } /// @notice Sweep all timelocked balances and transfer unlocked assets to owner accounts /// @param users An array of account addresses to sweep balances for /// @return The total amount of assets swept from the Prize Pool function sweepTimelockBalances( address[] calldata users ) external override nonReentrant returns (uint256) { return _sweepTimelockBalances(users); } /// @notice Sweep available timelocked balances to their owners. The full balances will be swept to the owners. /// @param users An array of owner addresses /// @return The total amount of assets swept from the Prize Pool function _sweepTimelockBalances( address[] memory users ) internal returns (uint256) { address operator = _msgSender(); uint256[] memory balances = new uint256[](users.length); uint256 totalWithdrawal; uint256 i; for (i = 0; i < users.length; i++) { address user = users[i]; if (_unlockTimestamps[user] <= _currentTime()) { totalWithdrawal = totalWithdrawal.add(_timelockBalances[user]); balances[i] = _timelockBalances[user]; delete _timelockBalances[user]; } } // if there is nothing to do, just quit if (totalWithdrawal == 0) { return 0; } timelockTotalSupply = timelockTotalSupply.sub(totalWithdrawal); uint256 redeemed = _redeem(totalWithdrawal); IERC20 underlyingToken = IERC20(_token()); for (i = 0; i < users.length; i++) { if (balances[i] > 0) { delete _unlockTimestamps[users[i]]; uint256 shareMantissa = FixedPoint.calculateMantissa(balances[i], totalWithdrawal); uint256 transferAmount = FixedPoint.multiplyUintByMantissa(redeemed, shareMantissa); underlyingToken.safeTransfer(users[i], transferAmount); emit TimelockedWithdrawalSwept(operator, users[i], balances[i], transferAmount); } } return totalWithdrawal; } /// @notice Calculates a timelocked withdrawal duration and credit consumption. /// @param from The user who is withdrawing /// @param amount The amount the user is withdrawing /// @param controlledToken The type of collateral the user is withdrawing (i.e. ticket or sponsorship) /// @return durationSeconds The duration of the timelock in seconds function calculateTimelockDuration( address from, address controlledToken, uint256 amount ) external override returns ( uint256 durationSeconds, uint256 burnedCredit ) { return _calculateTimelockDuration(from, controlledToken, amount); } /// @dev Calculates a timelocked withdrawal duration and credit consumption. /// @param from The user who is withdrawing /// @param amount The amount the user is withdrawing /// @param controlledToken The type of collateral the user is withdrawing (i.e. ticket or sponsorship) /// @return durationSeconds The duration of the timelock in seconds /// @return burnedCredit The credit that was burned function _calculateTimelockDuration( address from, address controlledToken, uint256 amount ) internal returns ( uint256 durationSeconds, uint256 burnedCredit ) { (uint256 exitFee, uint256 _burnedCredit) = _calculateEarlyExitFeeLessBurnedCredit(from, controlledToken, amount); uint256 duration = _estimateCreditAccrualTime(controlledToken, amount, exitFee); if (duration > maxTimelockDuration) { duration = maxTimelockDuration; } return (duration, _burnedCredit); } /// @notice Calculates the early exit fee for the given amount /// @param from The user who is withdrawing /// @param controlledToken The type of collateral being withdrawn /// @param amount The amount of collateral to be withdrawn /// @return exitFee The exit fee /// @return burnedCredit The user's credit that was burned function calculateEarlyExitFee( address from, address controlledToken, uint256 amount ) external override returns ( uint256 exitFee, uint256 burnedCredit ) { return _calculateEarlyExitFeeLessBurnedCredit(from, controlledToken, amount); } /// @dev Calculates the early exit fee for the given amount /// @param amount The amount of collateral to be withdrawn /// @return Exit fee function _calculateEarlyExitFeeNoCredit(address controlledToken, uint256 amount) internal view returns (uint256) { return _limitExitFee( amount, FixedPoint.multiplyUintByMantissa(amount, tokenCreditPlans[controlledToken].creditLimitMantissa) ); } /// @notice Estimates the amount of time it will take for a given amount of funds to accrue the given amount of credit. /// @param _principal The principal amount on which interest is accruing /// @param _interest The amount of interest that must accrue /// @return durationSeconds The duration of time it will take to accrue the given amount of interest, in seconds. function estimateCreditAccrualTime( address _controlledToken, uint256 _principal, uint256 _interest ) external override view returns (uint256 durationSeconds) { return _estimateCreditAccrualTime( _controlledToken, _principal, _interest ); } /// @notice Estimates the amount of time it will take for a given amount of funds to accrue the given amount of credit /// @param _principal The principal amount on which interest is accruing /// @param _interest The amount of interest that must accrue /// @return durationSeconds The duration of time it will take to accrue the given amount of interest, in seconds. function _estimateCreditAccrualTime( address _controlledToken, uint256 _principal, uint256 _interest ) internal view returns (uint256 durationSeconds) { // interest = credit rate * principal * time // => time = interest / (credit rate * principal) uint256 accruedPerSecond = FixedPoint.multiplyUintByMantissa(_principal, tokenCreditPlans[_controlledToken].creditRateMantissa); if (accruedPerSecond == 0) { return 0; } return _interest.div(accruedPerSecond); } /// @notice Burns a users credit. /// @param user The user whose credit should be burned /// @param credit The amount of credit to burn function _burnCredit(address user, address controlledToken, uint256 credit) internal { tokenCreditBalances[controlledToken][user].balance = uint256(tokenCreditBalances[controlledToken][user].balance).sub(credit).toUint128(); emit CreditBurned(user, controlledToken, credit); } /// @notice Accrues ticket credit for a user assuming their current balance is the passed balance. May burn credit if they exceed their limit. /// @param user The user for whom to accrue credit /// @param controlledToken The controlled token whose balance we are checking /// @param controlledTokenBalance The balance to use for the user /// @param extra Additional credit to be added function _accrueCredit(address user, address controlledToken, uint256 controlledTokenBalance, uint256 extra) internal { _updateCreditBalance( user, controlledToken, _calculateCreditBalance(user, controlledToken, controlledTokenBalance, extra) ); } function _calculateCreditBalance(address user, address controlledToken, uint256 controlledTokenBalance, uint256 extra) internal view returns (uint256) { uint256 newBalance; CreditBalance storage creditBalance = tokenCreditBalances[controlledToken][user]; if (!creditBalance.initialized) { newBalance = 0; } else { uint256 credit = calculateAccruedCredit(user, controlledToken, controlledTokenBalance); newBalance = _applyCreditLimit(controlledToken, controlledTokenBalance, uint256(creditBalance.balance).add(credit).add(extra)); } return newBalance; } function _updateCreditBalance(address user, address controlledToken, uint256 newBalance) internal { uint256 oldBalance = tokenCreditBalances[controlledToken][user].balance; tokenCreditBalances[controlledToken][user] = CreditBalance({ balance: newBalance.toUint128(), timestamp: _currentTime().toUint32(), initialized: true }); if (oldBalance < newBalance) { emit CreditMinted(user, controlledToken, newBalance.sub(oldBalance)); } else { emit CreditBurned(user, controlledToken, oldBalance.sub(newBalance)); } } /// @notice Applies the credit limit to a credit balance. The balance cannot exceed the credit limit. /// @param controlledToken The controlled token that the user holds /// @param controlledTokenBalance The users ticket balance (used to calculate credit limit) /// @param creditBalance The new credit balance to be checked /// @return The users new credit balance. Will not exceed the credit limit. function _applyCreditLimit(address controlledToken, uint256 controlledTokenBalance, uint256 creditBalance) internal view returns (uint256) { uint256 creditLimit = FixedPoint.multiplyUintByMantissa( controlledTokenBalance, tokenCreditPlans[controlledToken].creditLimitMantissa ); if (creditBalance > creditLimit) { creditBalance = creditLimit; } return creditBalance; } /// @notice Calculates the accrued interest for a user /// @param user The user whose credit should be calculated. /// @param controlledToken The controlled token that the user holds /// @param controlledTokenBalance The user's current balance of the controlled tokens. /// @return The credit that has accrued since the last credit update. function calculateAccruedCredit(address user, address controlledToken, uint256 controlledTokenBalance) internal view returns (uint256) { uint256 userTimestamp = tokenCreditBalances[controlledToken][user].timestamp; if (!tokenCreditBalances[controlledToken][user].initialized) { return 0; } uint256 deltaTime = _currentTime().sub(userTimestamp); uint256 creditPerSecond = FixedPoint.multiplyUintByMantissa(controlledTokenBalance, tokenCreditPlans[controlledToken].creditRateMantissa); return deltaTime.mul(creditPerSecond); } /// @notice Returns the credit balance for a given user. Not that this includes both minted credit and pending credit. /// @param user The user whose credit balance should be returned /// @return The balance of the users credit function balanceOfCredit(address user, address controlledToken) external override onlyControlledToken(controlledToken) returns (uint256) { _accrueCredit(user, controlledToken, IERC20(controlledToken).balanceOf(user), 0); return tokenCreditBalances[controlledToken][user].balance; } /// @notice Sets the rate at which credit accrues per second. The credit rate is a fixed point 18 number (like Ether). /// @param _controlledToken The controlled token for whom to set the credit plan /// @param _creditRateMantissa The credit rate to set. Is a fixed point 18 decimal (like Ether). /// @param _creditLimitMantissa The credit limit to set. Is a fixed point 18 decimal (like Ether). function setCreditPlanOf( address _controlledToken, uint128 _creditRateMantissa, uint128 _creditLimitMantissa ) external override onlyControlledToken(_controlledToken) onlyOwner { tokenCreditPlans[_controlledToken] = CreditPlan({ creditLimitMantissa: _creditLimitMantissa, creditRateMantissa: _creditRateMantissa }); emit CreditPlanSet(_controlledToken, _creditLimitMantissa, _creditRateMantissa); } /// @notice Returns the credit rate of a controlled token /// @param controlledToken The controlled token to retrieve the credit rates for /// @return creditLimitMantissa The credit limit fraction. This number is used to calculate both the credit limit and early exit fee. /// @return creditRateMantissa The credit rate. This is the amount of tokens that accrue per second. function creditPlanOf( address controlledToken ) external override view returns ( uint128 creditLimitMantissa, uint128 creditRateMantissa ) { creditLimitMantissa = tokenCreditPlans[controlledToken].creditLimitMantissa; creditRateMantissa = tokenCreditPlans[controlledToken].creditRateMantissa; } /// @notice Calculate the early exit for a user given a withdrawal amount. The user's credit is taken into account. /// @param from The user who is withdrawing /// @param controlledToken The token they are withdrawing /// @param amount The amount of funds they are withdrawing /// @return earlyExitFee The additional exit fee that should be charged. /// @return creditBurned The amount of credit that will be burned function _calculateEarlyExitFeeLessBurnedCredit( address from, address controlledToken, uint256 amount ) internal returns ( uint256 earlyExitFee, uint256 creditBurned ) { uint256 controlledTokenBalance = IERC20(controlledToken).balanceOf(from); require(controlledTokenBalance >= amount, "PrizePool/insuff-funds"); _accrueCredit(from, controlledToken, controlledTokenBalance, 0); /* The credit is used *last*. Always charge the fees up-front. How to calculate: Calculate their remaining exit fee. I.e. full exit fee of their balance less their credit. If the exit fee on their withdrawal is greater than the remaining exit fee, then they'll have to pay the difference. */ // Determine available usable credit based on withdraw amount uint256 remainingExitFee = _calculateEarlyExitFeeNoCredit(controlledToken, controlledTokenBalance.sub(amount)); uint256 availableCredit; if (tokenCreditBalances[controlledToken][from].balance >= remainingExitFee) { availableCredit = uint256(tokenCreditBalances[controlledToken][from].balance).sub(remainingExitFee); } // Determine amount of credit to burn and amount of fees required uint256 totalExitFee = _calculateEarlyExitFeeNoCredit(controlledToken, amount); creditBurned = (availableCredit > totalExitFee) ? totalExitFee : availableCredit; earlyExitFee = totalExitFee.sub(creditBurned); return (earlyExitFee, creditBurned); } /// @notice Allows the Governor to set a cap on the amount of liquidity that he pool can hold /// @param _liquidityCap The new liquidity cap for the prize pool function setLiquidityCap(uint256 _liquidityCap) external override onlyOwner { _setLiquidityCap(_liquidityCap); } function _setLiquidityCap(uint256 _liquidityCap) internal { liquidityCap = _liquidityCap; emit LiquidityCapSet(_liquidityCap); } /// @notice Allows the Governor to add Controlled Tokens to the Prize Pool /// @param _controlledToken The address of the Controlled Token to add function addControlledToken(address _controlledToken) external override onlyOwner { _addControlledToken(_controlledToken); } /// @notice Adds a new controlled token /// @param _controlledToken The controlled token to add. Cannot be a duplicate. function _addControlledToken(address _controlledToken) internal { require(ControlledToken(_controlledToken).controller() == this, "PrizePool/token-ctrlr-mismatch"); _tokens.addAddress(_controlledToken); emit ControlledTokenAdded(_controlledToken); } /// @notice Sets the prize strategy of the prize pool. Only callable by the owner. /// @param _prizeStrategy The new prize strategy function setPrizeStrategy(TokenListenerInterface _prizeStrategy) external override onlyOwner { _setPrizeStrategy(_prizeStrategy); } /// @notice Sets the prize strategy of the prize pool. Only callable by the owner. /// @param _prizeStrategy The new prize strategy function _setPrizeStrategy(TokenListenerInterface _prizeStrategy) internal { require(address(_prizeStrategy) != address(0), "PrizePool/prizeStrategy-not-zero"); prizeStrategy = _prizeStrategy; emit PrizeStrategySet(address(_prizeStrategy)); } /// @notice An array of the Tokens controlled by the Prize Pool (ie. Tickets, Sponsorship) /// @return An array of controlled token addresses function tokens() external override view returns (address[] memory) { return _tokens.addressArray(); } /// @dev Gets the current time as represented by the current block /// @return The timestamp of the current block function _currentTime() internal virtual view returns (uint256) { return block.timestamp; } /// @notice The timestamp at which an account's timelocked balance will be made available to sweep /// @param user The address of an account with timelocked assets /// @return The timestamp at which the locked assets will be made available function timelockBalanceAvailableAt(address user) external override view returns (uint256) { return _unlockTimestamps[user]; } /// @notice The balance of timelocked assets for an account /// @param user The address of an account with timelocked assets /// @return The amount of assets that have been timelocked function timelockBalanceOf(address user) external override view returns (uint256) { return _timelockBalances[user]; } /// @notice The total of all controlled tokens and timelock. /// @return The current total of all tokens and timelock. function accountedBalance() external override view returns (uint256) { return _tokenTotalSupply(); } /// @notice The total of all controlled tokens and timelock. /// @return The current total of all tokens and timelock. function _tokenTotalSupply() internal view returns (uint256) { uint256 total = timelockTotalSupply.add(reserveTotalSupply); address currentToken = _tokens.start(); while (currentToken != address(0) && currentToken != _tokens.end()) { total = total.add(IERC20(currentToken).totalSupply()); currentToken = _tokens.next(currentToken); } return total; } /// @dev Checks if the Prize Pool can receive liquidity based on the current cap /// @param _amount The amount of liquidity to be added to the Prize Pool /// @return True if the Prize Pool can receive the specified amount of liquidity function _canAddLiquidity(uint256 _amount) internal view returns (bool) { uint256 tokenTotalSupply = _tokenTotalSupply(); return (tokenTotalSupply.add(_amount) <= liquidityCap); } /// @dev Checks if a specific token is controlled by the Prize Pool /// @param controlledToken The address of the token to check /// @return True if the token is a controlled token, false otherwise function _isControlled(address controlledToken) internal view returns (bool) { return _tokens.contains(controlledToken); } /// @dev Provides information about the current execution context for GSN Meta-Txs. /// @return The payable address of the message sender function _msgSender() internal override(BaseRelayRecipient, ContextUpgradeSafe) virtual view returns (address payable) { return BaseRelayRecipient._msgSender(); } /// @dev Provides information about the current execution context for GSN Meta-Txs. /// @return The payable address of the message sender function _msgData() internal override(BaseRelayRecipient, ContextUpgradeSafe) virtual view returns (bytes memory) { return BaseRelayRecipient._msgData(); } /// @dev Function modifier to ensure usage of tokens controlled by the Prize Pool /// @param controlledToken The address of the token to check modifier onlyControlledToken(address controlledToken) { require(_isControlled(controlledToken), "PrizePool/unknown-token"); _; } /// @dev Function modifier to ensure caller is the prize-strategy modifier onlyPrizeStrategy() { require(_msgSender() == address(prizeStrategy), "PrizePool/only-prizeStrategy"); _; } /// @dev Function modifier to ensure the deposit amount does not exceed the liquidity cap (if set) modifier canAddLiquidity(uint256 _amount) { require(_canAddLiquidity(_amount), "PrizePool/exceeds-liquidity-cap"); _; } modifier onlyReserve() { ReserveInterface reserve = ReserveInterface(reserveRegistry.lookup()); require(address(reserve) == msg.sender, "PrizePool/only-reserve"); _; } }
pragma solidity ^0.6.0; import "../GSN/Context.sol"; import "../Initializable.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. * * By default, the owner account will be the one that deploys the contract. This * can later be changed with {transferOwnership}. * * This module is used through inheritance. It will make available the modifier * `onlyOwner`, which can be applied to your functions to restrict their use to * the owner. */ contract OwnableUpgradeSafe is Initializable, ContextUpgradeSafe { address private _owner; event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); /** * @dev Initializes the contract setting the deployer as the initial owner. */ function __Ownable_init() internal initializer { __Context_init_unchained(); __Ownable_init_unchained(); } function __Ownable_init_unchained() internal initializer { 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(_owner == _msgSender(), "Ownable: caller is not the 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 virtual 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 virtual onlyOwner { require(newOwner != address(0), "Ownable: new owner is the zero address"); emit OwnershipTransferred(_owner, newOwner); _owner = newOwner; } uint256[49] private __gap; }
pragma solidity ^0.6.0; import "../Initializable.sol"; /* * @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 ContextUpgradeSafe is Initializable { // Empty internal constructor, to prevent people from mistakenly deploying // an instance of this contract, which should be used via inheritance. function __Context_init() internal initializer { __Context_init_unchained(); } function __Context_init_unchained() internal initializer { } function _msgSender() internal view virtual returns (address payable) { return msg.sender; } function _msgData() internal view virtual returns (bytes memory) { this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691 return msg.data; } uint256[50] private __gap; }
pragma solidity >=0.4.24 <0.7.0; /** * @title Initializable * * @dev Helper contract to support initializer functions. To use it, replace * the constructor with a function that has the `initializer` modifier. * WARNING: Unlike constructors, initializer functions must be manually * invoked. This applies both to deploying an Initializable contract, as well * as extending an Initializable contract via inheritance. * WARNING: When used with inheritance, manual care must be taken to not invoke * a parent initializer twice, or ensure that all initializers are idempotent, * because this is not dealt with automatically as with constructors. */ contract Initializable { /** * @dev Indicates that the contract has been initialized. */ bool private initialized; /** * @dev Indicates that the contract is in the process of being initialized. */ bool private initializing; /** * @dev Modifier to use in the initializer function of a contract. */ modifier initializer() { require(initializing || isConstructor() || !initialized, "Contract instance has already been initialized"); bool isTopLevelCall = !initializing; if (isTopLevelCall) { initializing = true; initialized = true; } _; if (isTopLevelCall) { initializing = false; } } /// @dev Returns true if and only if the function is running in the constructor function isConstructor() private view returns (bool) { // extcodesize checks the size of the code stored in an address, and // address returns the current address. Since the code is still not // deployed when running a constructor, any checks on its code size will // yield zero, making it an effective way to detect if a contract is // under construction or not. address self = address(this); uint256 cs; assembly { cs := extcodesize(self) } return cs == 0; } // Reserved storage space to allow for layout changes in the future. uint256[50] private ______gap; }
pragma solidity ^0.6.0; /** * @dev Wrappers over Solidity's uintXX casting operators with added overflow * checks. * * Downcasting from uint256 in Solidity does not revert on overflow. This can * easily result in undesired exploitation or bugs, since developers usually * assume that overflows raise errors. `SafeCast` restores this intuition by * reverting the transaction when such an operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. * * Can be combined with {SafeMath} to extend it to smaller types, by performing * all math on `uint256` and then downcasting. */ library SafeCast { /** * @dev Returns the downcasted uint128 from uint256, reverting on * overflow (when the input is greater than largest uint128). * * Counterpart to Solidity's `uint128` operator. * * Requirements: * * - input must fit into 128 bits */ function toUint128(uint256 value) internal pure returns (uint128) { require(value < 2**128, "SafeCast: value doesn\'t fit in 128 bits"); return uint128(value); } /** * @dev Returns the downcasted uint64 from uint256, reverting on * overflow (when the input is greater than largest uint64). * * Counterpart to Solidity's `uint64` operator. * * Requirements: * * - input must fit into 64 bits */ function toUint64(uint256 value) internal pure returns (uint64) { require(value < 2**64, "SafeCast: value doesn\'t fit in 64 bits"); return uint64(value); } /** * @dev Returns the downcasted uint32 from uint256, reverting on * overflow (when the input is greater than largest uint32). * * Counterpart to Solidity's `uint32` operator. * * Requirements: * * - input must fit into 32 bits */ function toUint32(uint256 value) internal pure returns (uint32) { require(value < 2**32, "SafeCast: value doesn\'t fit in 32 bits"); return uint32(value); } /** * @dev Returns the downcasted uint16 from uint256, reverting on * overflow (when the input is greater than largest uint16). * * Counterpart to Solidity's `uint16` operator. * * Requirements: * * - input must fit into 16 bits */ function toUint16(uint256 value) internal pure returns (uint16) { require(value < 2**16, "SafeCast: value doesn\'t fit in 16 bits"); return uint16(value); } /** * @dev Returns the downcasted uint8 from uint256, reverting on * overflow (when the input is greater than largest uint8). * * Counterpart to Solidity's `uint8` operator. * * Requirements: * * - input must fit into 8 bits. */ function toUint8(uint256 value) internal pure returns (uint8) { require(value < 2**8, "SafeCast: value doesn\'t fit in 8 bits"); return uint8(value); } /** * @dev Converts a signed int256 into an unsigned uint256. * * Requirements: * * - input must be greater than or equal to 0. */ function toUint256(int256 value) internal pure returns (uint256) { require(value >= 0, "SafeCast: value must be positive"); return uint256(value); } /** * @dev Converts an unsigned uint256 into a signed int256. * * Requirements: * * - input must be less than or equal to maxInt256. */ function toInt256(uint256 value) internal pure returns (int256) { require(value < 2**255, "SafeCast: value doesn't fit in an int256"); return int256(value); } }
pragma solidity ^0.6.0; import "../Initializable.sol"; /** * @dev Contract module that helps prevent reentrant calls to a function. * * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier * available, which can be applied to functions to make sure there are no nested * (reentrant) calls to them. * * Note that because there is a single `nonReentrant` guard, functions marked as * `nonReentrant` may not call one another. This can be worked around by making * those functions `private`, and then adding `external` `nonReentrant` entry * points to them. * * TIP: If you would like to learn more about reentrancy and alternative ways * to protect against it, check out our blog post * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul]. */ contract ReentrancyGuardUpgradeSafe is Initializable { bool private _notEntered; function __ReentrancyGuard_init() internal initializer { __ReentrancyGuard_init_unchained(); } function __ReentrancyGuard_init_unchained() internal initializer { // Storing an initial non-zero value makes deployment a bit more // expensive, but in exchange the refund on every call to nonReentrant // will be lower in amount. Since refunds are capped to a percetange of // the total transaction's gas, it is best to keep them low in cases // like this one, to increase the likelihood of the full refund coming // into effect. _notEntered = true; } /** * @dev Prevents a contract from calling itself, directly or indirectly. * Calling a `nonReentrant` function from another `nonReentrant` * function is not supported. It is possible to prevent this from happening * by making the `nonReentrant` function external, and make it call a * `private` function that does the actual work. */ modifier nonReentrant() { // On the first call to nonReentrant, _notEntered will be true require(_notEntered, "ReentrancyGuard: reentrant call"); // Any calls to nonReentrant after this point will fail _notEntered = false; _; // By storing the original value once again, a refund is triggered (see // https://eips.ethereum.org/EIPS/eip-2200) _notEntered = true; } uint256[49] private __gap; }
pragma solidity ^0.6.2; import "../../introspection/IERC165.sol"; /** * @dev Required interface of an ERC721 compliant contract. */ interface IERC721 is IERC165 { event Transfer(address indexed from, address indexed to, uint256 indexed tokenId); event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId); event ApprovalForAll(address indexed owner, address indexed operator, bool approved); /** * @dev Returns the number of NFTs in ``owner``'s account. */ function balanceOf(address owner) external view returns (uint256 balance); /** * @dev Returns the owner of the NFT specified by `tokenId`. */ function ownerOf(uint256 tokenId) external view returns (address owner); /** * @dev Transfers a specific NFT (`tokenId`) from one account (`from`) to * another (`to`). * * * * Requirements: * - `from`, `to` cannot be zero. * - `tokenId` must be owned by `from`. * - If the caller is not `from`, it must be have been allowed to move this * NFT by either {approve} or {setApprovalForAll}. */ function safeTransferFrom(address from, address to, uint256 tokenId) external; /** * @dev Transfers a specific NFT (`tokenId`) from one account (`from`) to * another (`to`). * * Requirements: * - If the caller is not `from`, it must be approved to move this NFT by * either {approve} or {setApprovalForAll}. */ function transferFrom(address from, address to, uint256 tokenId) external; function approve(address to, uint256 tokenId) external; function getApproved(uint256 tokenId) external view returns (address operator); function setApprovalForAll(address operator, bool _approved) external; function isApprovedForAll(address owner, address operator) external view returns (bool); function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external; }
pragma solidity ^0.6.0; /** * @dev Interface of the ERC165 standard, as defined in the * https://eips.ethereum.org/EIPS/eip-165[EIP]. * * Implementers can declare support of contract interfaces, which can then be * queried by others ({ERC165Checker}). * * For an implementation, see {ERC165}. */ interface IERC165 { /** * @dev Returns true if this contract implements the interface defined by * `interfaceId`. See the corresponding * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section] * to learn more about how these ids are created. * * This function call must use less than 30 000 gas. */ function supportsInterface(bytes4 interfaceId) external view returns (bool); }
pragma solidity ^0.6.0; import "./IERC20.sol"; import "../../math/SafeMath.sol"; import "../../utils/Address.sol"; /** * @title SafeERC20 * @dev Wrappers around ERC20 operations that throw on failure (when the token * contract returns false). Tokens that return no value (and instead revert or * throw on failure) are also supported, non-reverting calls are assumed to be * successful. * To use this library you can add a `using SafeERC20 for ERC20;` statement to your contract, * which allows you to call the safe operations as `token.safeTransfer(...)`, etc. */ library SafeERC20 { using SafeMath for uint256; using Address for address; function safeTransfer(IERC20 token, address to, uint256 value) internal { _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value)); } function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal { _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value)); } function safeApprove(IERC20 token, address spender, uint256 value) internal { // safeApprove should only be called when setting an initial allowance, // or when resetting it to zero. To increase and decrease it, use // 'safeIncreaseAllowance' and 'safeDecreaseAllowance' // solhint-disable-next-line max-line-length require((value == 0) || (token.allowance(address(this), spender) == 0), "SafeERC20: approve from non-zero to non-zero allowance" ); _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value)); } function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal { uint256 newAllowance = token.allowance(address(this), spender).add(value); _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance)); } function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal { uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeERC20: decreased allowance below zero"); _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance)); } /** * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement * on the return value: the return value is optional (but if data is returned, it must not be false). * @param token The token targeted by the call. * @param data The call data (encoded using abi.encode or one of its variants). */ function _callOptionalReturn(IERC20 token, bytes memory data) private { // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since // we're implementing it ourselves. // A Solidity high level call has three parts: // 1. The target address is checked to verify it contains contract code // 2. The call itself is made, and success asserted // 3. The return value is decoded, which in turn checks the size of the returned data. // solhint-disable-next-line max-line-length require(address(token).isContract(), "SafeERC20: call to non-contract"); // solhint-disable-next-line avoid-low-level-calls (bool success, bytes memory returndata) = address(token).call(data); require(success, "SafeERC20: low-level call failed"); if (returndata.length > 0) { // Return data is optional // solhint-disable-next-line max-line-length require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed"); } } }
pragma solidity ^0.6.0; /** * @dev Interface of the ERC20 standard as defined in the EIP. */ interface IERC20 { /** * @dev Returns the amount of tokens in existence. */ function totalSupply() external view returns (uint256); /** * @dev Returns the amount of tokens owned by `account`. */ function balanceOf(address account) external view returns (uint256); /** * @dev Moves `amount` tokens from the caller's account to `recipient`. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transfer(address recipient, uint256 amount) external returns (bool); /** * @dev Returns the remaining number of tokens that `spender` will be * allowed to spend on behalf of `owner` through {transferFrom}. This is * zero by default. * * This value changes when {approve} or {transferFrom} are called. */ function allowance(address owner, address spender) external view returns (uint256); /** * @dev Sets `amount` as the allowance of `spender` over the caller's tokens. * * Returns a boolean value indicating whether the operation succeeded. * * IMPORTANT: Beware that changing an allowance with this method brings the risk * that someone may use both the old and the new allowance by unfortunate * transaction ordering. One possible solution to mitigate this race * condition is to first reduce the spender's allowance to 0 and set the * desired value afterwards: * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 * * Emits an {Approval} event. */ function approve(address spender, uint256 amount) external returns (bool); /** * @dev Moves `amount` tokens from `sender` to `recipient` using the * allowance mechanism. `amount` is then deducted from the caller's * allowance. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transferFrom(address sender, address recipient, uint256 amount) external returns (bool); /** * @dev Emitted when `value` tokens are moved from one account (`from`) to * another (`to`). * * Note that `value` may be zero. */ event Transfer(address indexed from, address indexed to, uint256 value); /** * @dev Emitted when the allowance of a `spender` for an `owner` is set by * a call to {approve}. `value` is the new allowance. */ event Approval(address indexed owner, address indexed spender, uint256 value); }
pragma solidity ^0.6.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. */ 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. */ 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. */ function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b != 0, errorMessage); return a % b; } }
pragma solidity ^0.6.2; /** * @dev Collection of functions related to the address type */ library Address { /** * @dev Returns true if `account` is a contract. * * [IMPORTANT] * ==== * It is unsafe to assume that an address for which this function returns * false is an externally-owned account (EOA) and not a contract. * * Among others, `isContract` will return false for the following * types of addresses: * * - an externally-owned account * - a contract in construction * - an address where a contract will be created * - an address where a contract lived, but was destroyed * ==== */ function isContract(address account) internal view returns (bool) { // According to EIP-1052, 0x0 is the value returned for not-yet created accounts // and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned // for accounts without code, i.e. `keccak256('')` bytes32 codehash; bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470; // solhint-disable-next-line no-inline-assembly assembly { codehash := extcodehash(account) } return (codehash != accountHash && codehash != 0x0); } /** * @dev Replacement for Solidity's `transfer`: sends `amount` wei to * `recipient`, forwarding all available gas and reverting on errors. * * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost * of certain opcodes, possibly making contracts go over the 2300 gas limit * imposed by `transfer`, making them unable to receive funds via * `transfer`. {sendValue} removes this limitation. * * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more]. * * IMPORTANT: because control is transferred to `recipient`, care must be * taken to not create reentrancy vulnerabilities. Consider using * {ReentrancyGuard} or the * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern]. */ function sendValue(address payable recipient, uint256 amount) internal { require(address(this).balance >= amount, "Address: insufficient balance"); // solhint-disable-next-line avoid-low-level-calls, avoid-call-value (bool success, ) = recipient.call{ value: amount }(""); require(success, "Address: unable to send value, recipient may have reverted"); } }
/** Copyright 2020 PoolTogether Inc. This file is part of PoolTogether. PoolTogether is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation under version 3 of the License. PoolTogether is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with PoolTogether. If not, see <https://www.gnu.org/licenses/>. */ pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol"; /** * @author Brendan Asselstine * @notice Provides basic fixed point math calculations. * * This library calculates integer fractions by scaling values by 1e18 then performing standard integer math. */ library FixedPoint { using SafeMath for uint256; // The scale to use for fixed point numbers. Same as Ether for simplicity. uint256 internal constant SCALE = 1e18; /** * Calculates a Fixed18 mantissa given the numerator and denominator * * The mantissa = (numerator * 1e18) / denominator * * @param numerator The mantissa numerator * @param denominator The mantissa denominator * @return The mantissa of the fraction */ function calculateMantissa(uint256 numerator, uint256 denominator) internal pure returns (uint256) { uint256 mantissa = numerator.mul(SCALE); mantissa = mantissa.div(denominator); return mantissa; } /** * Multiplies a Fixed18 number by an integer. * * @param b The whole integer to multiply * @param mantissa The Fixed18 number * @return An integer that is the result of multiplying the params. */ function multiplyUintByMantissa(uint256 b, uint256 mantissa) internal pure returns (uint256) { uint256 result = mantissa.mul(b); result = result.div(SCALE); return result; } /** * Divides an integer by a fixed point 18 mantissa * * @param dividend The integer to divide * @param mantissa The fixed point 18 number to serve as the divisor * @return An integer that is the result of dividing an integer by a fixed point 18 mantissa */ function divideUintByMantissa(uint256 dividend, uint256 mantissa) internal pure returns (uint256) { uint256 result = SCALE.mul(dividend); result = result.div(mantissa); return result; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.5.0 <0.7.0; /// @title Interface that allows a user to draw an address using an index interface RegistryInterface { function lookup() external view returns (address); }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.5.0 <0.7.0; /// @title Interface that allows a user to draw an address using an index interface ReserveInterface { function reserveRateMantissa(address prizePool) external view returns (uint256); }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol"; /// @title Defines the functions used to interact with a yield source. The Prize Pool inherits this contract. /// @notice Prize Pools subclasses need to implement this interface so that yield can be generated. abstract contract YieldSource { /// @notice Determines whether the passed token can be transferred out as an external award. /// @dev Different yield sources will hold the deposits as another kind of token: such a Compound's cToken. The /// prize strategy should not be allowed to move those tokens. /// @param _externalToken The address of the token to check /// @return True if the token may be awarded, false otherwise function _canAwardExternal(address _externalToken) internal virtual view returns (bool); /// @notice Returns the ERC20 asset token used for deposits. /// @return The ERC20 asset token function _token() internal virtual view returns (IERC20); /// @notice Returns the total balance (in asset tokens). This includes the deposits and interest. /// @return The underlying balance of asset tokens function _balance() internal virtual returns (uint256); /// @notice Supplies asset tokens to the yield source. /// @param mintAmount The amount of asset tokens to be supplied function _supply(uint256 mintAmount) internal virtual; /// @notice Redeems asset tokens from the yield source. /// @param redeemAmount The amount of yield-bearing tokens to be redeemed /// @return The actual amount of tokens that were redeemed. function _redeem(uint256 redeemAmount) internal virtual returns (uint256); }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.5.0 <0.7.0; /// @title An interface that allows a contract to listen to token mint, transfer and burn events. interface TokenListenerInterface { /// @notice Called when tokens are minted. /// @param to The address of the receiver of the minted tokens. /// @param amount The amount of tokens being minted /// @param controlledToken The address of the token that is being minted /// @param referrer The address that referred the minting. function beforeTokenMint(address to, uint256 amount, address controlledToken, address referrer) external; /// @notice Called when tokens are transferred or burned. /// @param from The address of the sender of the token transfer /// @param to The address of the receiver of the token transfer. Will be the zero address if burning. /// @param amount The amount of tokens transferred /// @param controlledToken The address of the token that was transferred function beforeTokenTransfer(address from, address to, uint256 amount, address controlledToken) external; }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/ERC20.sol"; import "../utils/RelayRecipient.sol"; import "./TokenControllerInterface.sol"; /// @title Controlled ERC20 Token /// @notice ERC20 Tokens with a controller for minting & burning contract ControlledToken is RelayRecipient, ERC20UpgradeSafe { /// @notice Interface to the contract responsible for controlling mint/burn TokenControllerInterface public controller; /// @notice Initializes the Controlled Token with Token Details and the Controller /// @param _name The name of the Token /// @param _symbol The symbol for the Token /// @param _decimals The number of decimals for the Token /// @param _trustedForwarder Address of the Forwarding Contract for GSN Meta-Txs /// @param _controller Address of the Controller contract for minting & burning function initialize( string memory _name, string memory _symbol, uint8 _decimals, address _trustedForwarder, TokenControllerInterface _controller ) public virtual initializer { trustedForwarder = _trustedForwarder; __ERC20_init(_name, _symbol); controller = _controller; _setupDecimals(_decimals); } /// @notice Allows the controller to mint tokens for a user account /// @dev May be overridden to provide more granular control over minting /// @param _user Address of the receiver of the minted tokens /// @param _amount Amount of tokens to mint function controllerMint(address _user, uint256 _amount) external virtual onlyController { _mint(_user, _amount); } /// @notice Allows the controller to burn tokens from a user account /// @dev May be overridden to provide more granular control over burning /// @param _user Address of the holder account to burn tokens from /// @param _amount Amount of tokens to burn function controllerBurn(address _user, uint256 _amount) external virtual onlyController { _burn(_user, _amount); } /// @notice Allows an operator via the controller to burn tokens on behalf of a user account /// @dev May be overridden to provide more granular control over operator-burning /// @param _operator Address of the operator performing the burn action via the controller contract /// @param _user Address of the holder account to burn tokens from /// @param _amount Amount of tokens to burn function controllerBurnFrom(address _operator, address _user, uint256 _amount) external virtual onlyController { if (_operator != _user) { uint256 decreasedAllowance = allowance(_user, _operator).sub(_amount, "ControlledToken/exceeds-allowance"); _approve(_user, _operator, decreasedAllowance); } _burn(_user, _amount); } /// @dev Function modifier to ensure that the caller is the controller contract modifier onlyController { require(_msgSender() == address(controller), "ControlledToken/only-controller"); _; } /// @dev Controller hook to provide notifications & rule validations on token transfers to the controller. /// This includes minting and burning. /// May be overridden to provide more granular control over operator-burning /// @param from Address of the account sending the tokens (address(0x0) on minting) /// @param to Address of the account receiving the tokens (address(0x0) on burning) /// @param amount Amount of tokens being transferred function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override { controller.beforeTokenTransfer(from, to, amount); } /// @dev Provides information about the current execution context for GSN Meta-Txs. /// @return The payable address of the message sender function _msgSender() internal override(BaseRelayRecipient, ContextUpgradeSafe) virtual view returns (address payable) { return BaseRelayRecipient._msgSender(); } /// @dev Provides information about the current execution context for GSN Meta-Txs. /// @return The payable address of the message sender function _msgData() internal override(BaseRelayRecipient, ContextUpgradeSafe) virtual view returns (bytes memory) { return BaseRelayRecipient._msgData(); } }
pragma solidity ^0.6.0; import "../../GSN/Context.sol"; import "./IERC20.sol"; import "../../math/SafeMath.sol"; import "../../utils/Address.sol"; import "../../Initializable.sol"; /** * @dev Implementation of the {IERC20} interface. * * This implementation is agnostic to the way tokens are created. This means * that a supply mechanism has to be added in a derived contract using {_mint}. * For a generic mechanism see {ERC20MinterPauser}. * * TIP: For a detailed writeup see our guide * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How * to implement supply mechanisms]. * * We have followed general OpenZeppelin guidelines: functions revert instead * of returning `false` on failure. This behavior is nonetheless conventional * and does not conflict with the expectations of ERC20 applications. * * Additionally, an {Approval} event is emitted on calls to {transferFrom}. * This allows applications to reconstruct the allowance for all accounts just * by listening to said events. Other implementations of the EIP may not emit * these events, as it isn't required by the specification. * * Finally, the non-standard {decreaseAllowance} and {increaseAllowance} * functions have been added to mitigate the well-known issues around setting * allowances. See {IERC20-approve}. */ contract ERC20UpgradeSafe is Initializable, ContextUpgradeSafe, IERC20 { using SafeMath for uint256; using Address for address; mapping (address => uint256) private _balances; mapping (address => mapping (address => uint256)) private _allowances; uint256 private _totalSupply; string private _name; string private _symbol; uint8 private _decimals; /** * @dev Sets the values for {name} and {symbol}, initializes {decimals} with * a default value of 18. * * To select a different value for {decimals}, use {_setupDecimals}. * * All three of these values are immutable: they can only be set once during * construction. */ function __ERC20_init(string memory name, string memory symbol) internal initializer { __Context_init_unchained(); __ERC20_init_unchained(name, symbol); } function __ERC20_init_unchained(string memory name, string memory symbol) internal initializer { _name = name; _symbol = symbol; _decimals = 18; } /** * @dev Returns the name of the token. */ function name() public view returns (string memory) { return _name; } /** * @dev Returns the symbol of the token, usually a shorter version of the * name. */ function symbol() public view returns (string memory) { return _symbol; } /** * @dev Returns the number of decimals used to get its user representation. * For example, if `decimals` equals `2`, a balance of `505` tokens should * be displayed to a user as `5,05` (`505 / 10 ** 2`). * * Tokens usually opt for a value of 18, imitating the relationship between * Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is * called. * * NOTE: This information is only used for _display_ purposes: it in * no way affects any of the arithmetic of the contract, including * {IERC20-balanceOf} and {IERC20-transfer}. */ function decimals() public view returns (uint8) { return _decimals; } /** * @dev See {IERC20-totalSupply}. */ function totalSupply() public view override returns (uint256) { return _totalSupply; } /** * @dev See {IERC20-balanceOf}. */ function balanceOf(address account) public view override returns (uint256) { return _balances[account]; } /** * @dev See {IERC20-transfer}. * * Requirements: * * - `recipient` cannot be the zero address. * - the caller must have a balance of at least `amount`. */ function transfer(address recipient, uint256 amount) public virtual override returns (bool) { _transfer(_msgSender(), recipient, amount); return true; } /** * @dev See {IERC20-allowance}. */ function allowance(address owner, address spender) public view virtual override returns (uint256) { return _allowances[owner][spender]; } /** * @dev See {IERC20-approve}. * * Requirements: * * - `spender` cannot be the zero address. */ function approve(address spender, uint256 amount) public virtual override returns (bool) { _approve(_msgSender(), spender, amount); return true; } /** * @dev See {IERC20-transferFrom}. * * Emits an {Approval} event indicating the updated allowance. This is not * required by the EIP. See the note at the beginning of {ERC20}; * * Requirements: * - `sender` and `recipient` cannot be the zero address. * - `sender` must have a balance of at least `amount`. * - the caller must have allowance for ``sender``'s tokens of at least * `amount`. */ function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) { _transfer(sender, recipient, amount); _approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance")); return true; } /** * @dev Atomically increases the allowance granted to `spender` by the caller. * * This is an alternative to {approve} that can be used as a mitigation for * problems described in {IERC20-approve}. * * Emits an {Approval} event indicating the updated allowance. * * Requirements: * * - `spender` cannot be the zero address. */ function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) { _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue)); return true; } /** * @dev Atomically decreases the allowance granted to `spender` by the caller. * * This is an alternative to {approve} that can be used as a mitigation for * problems described in {IERC20-approve}. * * Emits an {Approval} event indicating the updated allowance. * * Requirements: * * - `spender` cannot be the zero address. * - `spender` must have allowance for the caller of at least * `subtractedValue`. */ function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) { _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero")); return true; } /** * @dev Moves tokens `amount` from `sender` to `recipient`. * * This is internal function is equivalent to {transfer}, and can be used to * e.g. implement automatic token fees, slashing mechanisms, etc. * * Emits a {Transfer} event. * * Requirements: * * - `sender` cannot be the zero address. * - `recipient` cannot be the zero address. * - `sender` must have a balance of at least `amount`. */ function _transfer(address sender, address recipient, uint256 amount) internal virtual { require(sender != address(0), "ERC20: transfer from the zero address"); require(recipient != address(0), "ERC20: transfer to the zero address"); _beforeTokenTransfer(sender, recipient, amount); _balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance"); _balances[recipient] = _balances[recipient].add(amount); emit Transfer(sender, recipient, amount); } /** @dev Creates `amount` tokens and assigns them to `account`, increasing * the total supply. * * Emits a {Transfer} event with `from` set to the zero address. * * Requirements * * - `to` cannot be the zero address. */ function _mint(address account, uint256 amount) internal virtual { require(account != address(0), "ERC20: mint to the zero address"); _beforeTokenTransfer(address(0), account, amount); _totalSupply = _totalSupply.add(amount); _balances[account] = _balances[account].add(amount); emit Transfer(address(0), account, amount); } /** * @dev Destroys `amount` tokens from `account`, reducing the * total supply. * * Emits a {Transfer} event with `to` set to the zero address. * * Requirements * * - `account` cannot be the zero address. * - `account` must have at least `amount` tokens. */ function _burn(address account, uint256 amount) internal virtual { require(account != address(0), "ERC20: burn from the zero address"); _beforeTokenTransfer(account, address(0), amount); _balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance"); _totalSupply = _totalSupply.sub(amount); emit Transfer(account, address(0), amount); } /** * @dev Sets `amount` as the allowance of `spender` over the `owner`s tokens. * * This is internal function is equivalent to `approve`, and can be used to * e.g. set automatic allowances for certain subsystems, etc. * * Emits an {Approval} event. * * Requirements: * * - `owner` cannot be the zero address. * - `spender` cannot be the zero address. */ function _approve(address owner, address spender, uint256 amount) internal virtual { require(owner != address(0), "ERC20: approve from the zero address"); require(spender != address(0), "ERC20: approve to the zero address"); _allowances[owner][spender] = amount; emit Approval(owner, spender, amount); } /** * @dev Sets {decimals} to a value other than the default one of 18. * * WARNING: This function should only be called from the constructor. Most * applications that interact with token contracts will not expect * {decimals} to ever change, and may work incorrectly if it does. */ function _setupDecimals(uint8 decimals_) internal { _decimals = decimals_; } /** * @dev Hook that is called before any transfer of tokens. This includes * minting and burning. * * Calling conditions: * * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens * will be to transferred to `to`. * - when `from` is zero, `amount` tokens will be minted for `to`. * - when `to` is zero, `amount` of ``from``'s tokens will be burned. * - `from` and `to` are never both zero. * * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. */ function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { } uint256[44] private __gap; }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@opengsn/gsn/contracts/BaseRelayRecipient.sol"; contract RelayRecipient is BaseRelayRecipient { function versionRecipient() external override view returns (string memory) { return "2.0.0"; } }
// SPDX-License-Identifier:MIT // solhint-disable no-inline-assembly pragma solidity ^0.6.2; import "./interfaces/IRelayRecipient.sol"; /** * A base contract to be inherited by any contract that want to receive relayed transactions * A subclass must use "_msgSender()" instead of "msg.sender" */ abstract contract BaseRelayRecipient is IRelayRecipient { /* * Forwarder singleton we accept calls from */ address public trustedForwarder; function isTrustedForwarder(address forwarder) public override view returns(bool) { return forwarder == trustedForwarder; } /** * return the sender of this call. * if the call came through our trusted forwarder, return the original sender. * otherwise, return `msg.sender`. * should be used in the contract anywhere instead of msg.sender */ function _msgSender() internal override virtual view returns (address payable ret) { if (msg.data.length >= 24 && isTrustedForwarder(msg.sender)) { // At this point we know that the sender is a trusted forwarder, // so we trust that the last bytes of msg.data are the verified sender address. // extract sender address from the end of msg.data assembly { ret := shr(96,calldataload(sub(calldatasize(),20))) } } else { return msg.sender; } } /** * return the msg.data of this call. * if the call came through our trusted forwarder, then the real sender was appended as the last 20 bytes * of the msg.data - so this method will strip those 20 bytes off. * otherwise, return `msg.data` * should be used in the contract instead of msg.data, where the difference matters (e.g. when explicitly * signing or hashing the */ function _msgData() internal override virtual view returns (bytes memory ret) { if (msg.data.length >= 24 && isTrustedForwarder(msg.sender)) { // At this point we know that the sender is a trusted forwarder, // we copy the msg.data , except the last 20 bytes (and update the total length) assembly { let ptr := mload(0x40) // copy only size-20 bytes let size := sub(calldatasize(),20) // structure RLP data as <offset> <length> <bytes> mstore(ptr, 0x20) mstore(add(ptr,32), size) calldatacopy(add(ptr,64), 0, size) return(ptr, add(size,64)) } } else { return msg.data; } } }
// SPDX-License-Identifier:MIT pragma solidity ^0.6.2; /** * a contract must implement this interface in order to support relayed transaction. * It is better to inherit the BaseRelayRecipient as its implementation. */ abstract contract IRelayRecipient { /** * return if the forwarder is trusted to forward relayed transactions to us. * the forwarder is required to verify the sender's signature, and verify * the call is not a replay. */ function isTrustedForwarder(address forwarder) public virtual view returns(bool); /** * return the sender of this call. * if the call came through our trusted forwarder, then the real sender is appended as the last 20 bytes * of the msg.data. * otherwise, return `msg.sender` * should be used in the contract anywhere instead of msg.sender */ function _msgSender() internal virtual view returns (address payable); /** * return the msg.data of this call. * if the call came through our trusted forwarder, then the real sender was appended as the last 20 bytes * of the msg.data - so this method will strip those 20 bytes off. * otherwise, return `msg.data` * should be used in the contract instead of msg.data, where the difference matters (e.g. when explicitly * signing or hashing the */ function _msgData() internal virtual view returns (bytes memory); function versionRecipient() external virtual view returns (string memory); }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.5.0 <0.7.0; /// @title Controlled ERC20 Token Interface /// @notice Required interface for Controlled ERC20 Tokens linked to a Prize Pool /// @dev Defines the spec required to be implemented by a Controlled ERC20 Token interface TokenControllerInterface { /// @dev Controller hook to provide notifications & rule validations on token transfers to the controller. /// This includes minting and burning. /// @param from Address of the account sending the tokens (address(0x0) on minting) /// @param to Address of the account receiving the tokens (address(0x0) on burning) /// @param amount Amount of tokens being transferred function beforeTokenTransfer(address from, address to, uint256 amount) external; }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; /// @notice An efficient implementation of a singly linked list of addresses /// @dev A mapping(address => address) tracks the 'next' pointer. A special address called the SENTINEL is used to denote the beginning and end of the list. library MappedSinglyLinkedList { /// @notice The special value address used to denote the end of the list address public constant SENTINEL = address(0x1); /// @notice The data structure to use for the list. struct Mapping { uint256 count; mapping(address => address) addressMap; } /// @notice Initializes the list. /// @dev It is important that this is called so that the SENTINEL is correctly setup. function initialize(Mapping storage self) internal { require(self.count == 0, "Already init"); self.addressMap[SENTINEL] = SENTINEL; } function start(Mapping storage self) internal view returns (address) { return self.addressMap[SENTINEL]; } function next(Mapping storage self, address current) internal view returns (address) { return self.addressMap[current]; } function end(Mapping storage) internal pure returns (address) { return SENTINEL; } function addAddresses(Mapping storage self, address[] memory addresses) internal { for (uint256 i = 0; i < addresses.length; i++) { addAddress(self, addresses[i]); } } /// @notice Adds an address to the front of the list. /// @param self The Mapping struct that this function is attached to /// @param newAddress The address to shift to the front of the list function addAddress(Mapping storage self, address newAddress) internal { require(newAddress != SENTINEL && newAddress != address(0), "Invalid address"); require(self.addressMap[newAddress] == address(0), "Already added"); self.addressMap[newAddress] = self.addressMap[SENTINEL]; self.addressMap[SENTINEL] = newAddress; self.count = self.count + 1; } /// @notice Removes an address from the list /// @param self The Mapping struct that this function is attached to /// @param prevAddress The address that precedes the address to be removed. This may be the SENTINEL if at the start. /// @param addr The address to remove from the list. function removeAddress(Mapping storage self, address prevAddress, address addr) internal { require(addr != SENTINEL && addr != address(0), "Invalid address"); require(self.addressMap[prevAddress] == addr, "Invalid prevAddress"); self.addressMap[prevAddress] = self.addressMap[addr]; delete self.addressMap[addr]; self.count = self.count - 1; } /// @notice Determines whether the list contains the given address /// @param self The Mapping struct that this function is attached to /// @param addr The address to check /// @return True if the address is contained, false otherwise. function contains(Mapping storage self, address addr) internal view returns (bool) { return addr != SENTINEL && addr != address(0) && self.addressMap[addr] != address(0); } /// @notice Returns an address array of all the addresses in this list /// @dev Contains a for loop, so complexity is O(n) wrt the list size /// @param self The Mapping struct that this function is attached to /// @return An array of all the addresses function addressArray(Mapping storage self) internal view returns (address[] memory) { address[] memory array = new address[](self.count); uint256 count; address currentAddress = self.addressMap[SENTINEL]; while (currentAddress != address(0) && currentAddress != SENTINEL) { array[count] = currentAddress; currentAddress = self.addressMap[currentAddress]; count++; } return array; } /// @notice Removes every address from the list /// @param self The Mapping struct that this function is attached to function clearAll(Mapping storage self) internal { address currentAddress = self.addressMap[SENTINEL]; while (currentAddress != address(0) && currentAddress != SENTINEL) { address nextAddress = self.addressMap[currentAddress]; delete self.addressMap[currentAddress]; currentAddress = nextAddress; } self.addressMap[SENTINEL] = SENTINEL; self.count = 0; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/access/Ownable.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/utils/SafeCast.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/utils/ReentrancyGuard.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC721/IERC721.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/SafeERC20.sol"; import "../external/pooltogether/FixedPoint.sol"; import "../reserve/ReserveInterface.sol"; import "./YieldSource.sol"; import "../token/TokenListenerInterface.sol"; import "../token/ControlledToken.sol"; import "../token/TokenControllerInterface.sol"; import "../utils/MappedSinglyLinkedList.sol"; import "../utils/RelayRecipient.sol"; /// @title Escrows assets and deposits them into a yield source. Exposes interest to Prize Strategy. Users deposit and withdraw from this contract to participate in Prize Pool. /// @notice Accounting is managed using Controlled Tokens, whose mint and burn functions can only be called by this contract. /// @dev Must be inherited to provide specific yield-bearing asset control, such as Compound cTokens interface PrizePoolInterface { /// @notice Deposit assets into the Prize Pool in exchange for tokens /// @param to The address receiving the newly minted tokens /// @param amount The amount of assets to deposit /// @param controlledToken The address of the type of token the user is minting /// @param referrer The referrer of the deposit function depositTo( address to, uint256 amount, address controlledToken, address referrer ) external; /// @notice Withdraw assets from the Prize Pool instantly. A fairness fee may be charged for an early exit. /// @param from The address to redeem tokens from. /// @param amount The amount of tokens to redeem for assets. /// @param controlledToken The address of the token to redeem (i.e. ticket or sponsorship) /// @param maximumExitFee The maximum exit fee the caller is willing to pay. This should be pre-calculated by the calculateExitFee() fxn. /// @return The actual exit fee paid function withdrawInstantlyFrom( address from, uint256 amount, address controlledToken, uint256 maximumExitFee ) external returns (uint256); /// @notice Withdraw assets from the Prize Pool by placing them into the timelock. /// The timelock is used to ensure that the tickets have contributed their fair share of the prize. /// @dev Note that if the user has previously timelocked funds then this contract will try to sweep them. /// If the existing timelocked funds are still locked, then the incoming /// balance is added to their existing balance and the new timelock unlock timestamp will overwrite the old one. /// @param from The address to withdraw from /// @param amount The amount to withdraw /// @param controlledToken The type of token being withdrawn /// @return The timestamp from which the funds can be swept function withdrawWithTimelockFrom( address from, uint256 amount, address controlledToken ) external returns (uint256); function withdrawReserve(address to) external returns (uint256); /// @notice Returns the balance that is available to award. /// @dev captureAwardBalance() should be called first /// @return The total amount of assets to be awarded for the current prize function awardBalance() external view returns (uint256); /// @notice Captures any available interest as award balance. /// @dev This function also captures the reserve fees. /// @return The total amount of assets to be awarded for the current prize function captureAwardBalance() external returns (uint256); /// @notice Called by the prize strategy to award prizes. /// @dev The amount awarded must be less than the awardBalance() /// @param to The address of the winner that receives the award /// @param amount The amount of assets to be awarded /// @param controlledToken The address of the asset token being awarded function award( address to, uint256 amount, address controlledToken ) external; /// @notice Called by the Prize-Strategy to transfer out external ERC20 tokens /// @dev Used to transfer out tokens held by the Prize Pool. Could be liquidated, or anything. /// @param to The address of the winner that receives the award /// @param amount The amount of external assets to be awarded /// @param externalToken The address of the external asset token being awarded function transferExternalERC20( address to, address externalToken, uint256 amount ) external; /// @notice Called by the Prize-Strategy to award external ERC20 prizes /// @dev Used to award any arbitrary tokens held by the Prize Pool /// @param to The address of the winner that receives the award /// @param amount The amount of external assets to be awarded /// @param externalToken The address of the external asset token being awarded function awardExternalERC20( address to, address externalToken, uint256 amount ) external; /// @notice Called by the prize strategy to award external ERC721 prizes /// @dev Used to award any arbitrary NFTs held by the Prize Pool /// @param to The address of the winner that receives the award /// @param externalToken The address of the external NFT token being awarded /// @param tokenIds An array of NFT Token IDs to be transferred function awardExternalERC721( address to, address externalToken, uint256[] calldata tokenIds ) external; /// @notice Sweep all timelocked balances and transfer unlocked assets to owner accounts /// @param users An array of account addresses to sweep balances for /// @return The total amount of assets swept from the Prize Pool function sweepTimelockBalances( address[] calldata users ) external returns (uint256); /// @notice Calculates a timelocked withdrawal duration and credit consumption. /// @param from The user who is withdrawing /// @param amount The amount the user is withdrawing /// @param controlledToken The type of collateral the user is withdrawing (i.e. ticket or sponsorship) /// @return durationSeconds The duration of the timelock in seconds function calculateTimelockDuration( address from, address controlledToken, uint256 amount ) external returns ( uint256 durationSeconds, uint256 burnedCredit ); /// @notice Calculates the early exit fee for the given amount /// @param from The user who is withdrawing /// @param controlledToken The type of collateral being withdrawn /// @param amount The amount of collateral to be withdrawn /// @return exitFee The exit fee /// @return burnedCredit The user's credit that was burned function calculateEarlyExitFee( address from, address controlledToken, uint256 amount ) external returns ( uint256 exitFee, uint256 burnedCredit ); /// @notice Estimates the amount of time it will take for a given amount of funds to accrue the given amount of credit. /// @param _principal The principal amount on which interest is accruing /// @param _interest The amount of interest that must accrue /// @return durationSeconds The duration of time it will take to accrue the given amount of interest, in seconds. function estimateCreditAccrualTime( address _controlledToken, uint256 _principal, uint256 _interest ) external view returns (uint256 durationSeconds); /// @notice Returns the credit balance for a given user. Not that this includes both minted credit and pending credit. /// @param user The user whose credit balance should be returned /// @return The balance of the users credit function balanceOfCredit(address user, address controlledToken) external returns (uint256); /// @notice Sets the rate at which credit accrues per second. The credit rate is a fixed point 18 number (like Ether). /// @param _controlledToken The controlled token for whom to set the credit plan /// @param _creditRateMantissa The credit rate to set. Is a fixed point 18 decimal (like Ether). /// @param _creditLimitMantissa The credit limit to set. Is a fixed point 18 decimal (like Ether). function setCreditPlanOf( address _controlledToken, uint128 _creditRateMantissa, uint128 _creditLimitMantissa ) external; /// @notice Returns the credit rate of a controlled token /// @param controlledToken The controlled token to retrieve the credit rates for /// @return creditLimitMantissa The credit limit fraction. This number is used to calculate both the credit limit and early exit fee. /// @return creditRateMantissa The credit rate. This is the amount of tokens that accrue per second. function creditPlanOf( address controlledToken ) external view returns ( uint128 creditLimitMantissa, uint128 creditRateMantissa ); /// @notice Allows the Governor to set a cap on the amount of liquidity that he pool can hold /// @param _liquidityCap The new liquidity cap for the prize pool function setLiquidityCap(uint256 _liquidityCap) external; /// @notice Allows the Governor to add Controlled Tokens to the Prize Pool /// @param _controlledToken The address of the Controlled Token to add function addControlledToken(address _controlledToken) external; /// @notice Sets the prize strategy of the prize pool. Only callable by the owner. /// @param _prizeStrategy The new prize strategy function setPrizeStrategy(TokenListenerInterface _prizeStrategy) external; /// @dev Returns the address of the underlying ERC20 asset /// @return The address of the asset function token() external view returns (IERC20); /// @notice An array of the Tokens controlled by the Prize Pool (ie. Tickets, Sponsorship) /// @return An array of controlled token addresses function tokens() external view returns (address[] memory); /// @notice The timestamp at which an account's timelocked balance will be made available to sweep /// @param user The address of an account with timelocked assets /// @return The timestamp at which the locked assets will be made available function timelockBalanceAvailableAt(address user) external view returns (uint256); /// @notice The balance of timelocked assets for an account /// @param user The address of an account with timelocked assets /// @return The amount of assets that have been timelocked function timelockBalanceOf(address user) external view returns (uint256); /// @notice The total of all controlled tokens and timelock. /// @return The current total of all tokens and timelock. function accountedBalance() external view returns (uint256); }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "../PeriodicPrizeStrategy.sol"; /* solium-disable security/no-block-members */ contract SingleRandomWinner is PeriodicPrizeStrategy { function _distribute(uint256 randomNumber) internal override { uint256 prize = prizePool.captureAwardBalance(); address winner = ticket.draw(randomNumber); if (winner != address(0)) { _awardTickets(winner, prize); _awardAllExternalTokens(winner); } } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/access/Ownable.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/utils/SafeCast.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/introspection/IERC1820Registry.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/utils/ReentrancyGuard.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/utils/Address.sol"; import "@pooltogether/pooltogether-rng-contracts/contracts/RNGInterface.sol"; import "../token/TokenListenerInterface.sol"; import "../external/pooltogether/FixedPoint.sol"; import "../token/TokenControllerInterface.sol"; import "../token/ControlledToken.sol"; import "../token/TicketInterface.sol"; import "../prize-pool/PrizePool.sol"; import "../Constants.sol"; import "../utils/RelayRecipient.sol"; /* solium-disable security/no-block-members */ abstract contract PeriodicPrizeStrategy is Initializable, OwnableUpgradeSafe, RelayRecipient, ReentrancyGuardUpgradeSafe, TokenListenerInterface { using SafeMath for uint256; using SafeCast for uint256; using MappedSinglyLinkedList for MappedSinglyLinkedList.Mapping; using Address for address; uint256 internal constant ETHEREUM_BLOCK_TIME_ESTIMATE_MANTISSA = 13.4 ether; event PrizePoolOpened( address indexed operator, uint256 indexed prizePeriodStartedAt ); event PrizePoolAwardStarted( address indexed operator, address indexed prizePool, uint32 indexed rngRequestId, uint32 rngLockBlock ); event RngRequestFailed(); event PrizePoolAwarded( address indexed operator, uint256 randomNumber ); event RngServiceUpdated( address indexed rngService ); event TokenListenerUpdated( address indexed tokenListener ); event RngRequestTimeoutSet( uint32 rngRequestTimeout ); event ExternalErc721AwardAdded( address indexed externalErc721, uint256[] tokenIds ); event ExternalErc20AwardAdded( address indexed externalErc20 ); event ExternalErc721AwardRemoved( address indexed externalErc721Award ); event ExternalErc20AwardRemoved( address indexed externalErc20Award ); struct RngRequest { uint32 id; uint32 lockBlock; uint32 requestedAt; } // Comptroller TokenListenerInterface public tokenListener; // Contract Interfaces PrizePool public prizePool; TicketInterface public ticket; IERC20 public sponsorship; RNGInterface public rng; // Current RNG Request RngRequest internal rngRequest; // RNG Request Timeout uint32 public rngRequestTimeout; // Prize period uint256 public prizePeriodSeconds; uint256 public prizePeriodStartedAt; // External tokens awarded as part of prize MappedSinglyLinkedList.Mapping internal externalErc20s; MappedSinglyLinkedList.Mapping internal externalErc721s; // External NFT token IDs to be awarded // NFT Address => TokenIds mapping (address => uint256[]) internal externalErc721TokenIds; function initialize ( address _trustedForwarder, uint256 _prizePeriodStart, uint256 _prizePeriodSeconds, PrizePool _prizePool, address _ticket, address _sponsorship, RNGInterface _rng, address[] memory _externalErc20s ) public initializer { require(_prizePeriodSeconds > 0, "PeriodicPrizeStrategy/prize-period-greater-than-zero"); require(address(_prizePool) != address(0), "PeriodicPrizeStrategy/prize-pool-not-zero"); require(address(_ticket) != address(0), "PeriodicPrizeStrategy/ticket-not-zero"); require(address(_sponsorship) != address(0), "PeriodicPrizeStrategy/sponsorship-not-zero"); require(address(_rng) != address(0), "PeriodicPrizeStrategy/rng-not-zero"); prizePool = _prizePool; ticket = TicketInterface(_ticket); rng = _rng; sponsorship = IERC20(_sponsorship); trustedForwarder = _trustedForwarder; __Ownable_init(); __ReentrancyGuard_init(); Constants.REGISTRY.setInterfaceImplementer(address(this), Constants.TOKENS_RECIPIENT_INTERFACE_HASH, address(this)); for (uint256 i = 0; i < _externalErc20s.length; i++) { require(prizePool.canAwardExternal(_externalErc20s[i]), "PeriodicPrizeStrategy/cannot-award-external"); } externalErc20s.initialize(); externalErc20s.addAddresses(_externalErc20s); prizePeriodSeconds = _prizePeriodSeconds; prizePeriodStartedAt = _prizePeriodStart; externalErc721s.initialize(); // 1 hour timeout _setRngRequestTimeout(3600); emit PrizePoolOpened(_msgSender(), prizePeriodStartedAt); } function _distribute(uint256 randomNumber) internal virtual; /// @notice Calculates and returns the currently accrued prize /// @return The current prize size function currentPrize() public view returns (uint256) { return prizePool.awardBalance(); } function setTokenListener(TokenListenerInterface _tokenListener) external onlyOwner { tokenListener = _tokenListener; emit TokenListenerUpdated(address(tokenListener)); } /// @notice Estimates the remaining blocks until the prize given a number of seconds per block /// @param secondsPerBlockMantissa The number of seconds per block to use for the calculation. Should be a fixed point 18 number like Ether. /// @return The estimated number of blocks remaining until the prize can be awarded. function estimateRemainingBlocksToPrize(uint256 secondsPerBlockMantissa) public view returns (uint256) { return FixedPoint.divideUintByMantissa( _prizePeriodRemainingSeconds(), secondsPerBlockMantissa ); } /// @notice Returns the number of seconds remaining until the prize can be awarded. /// @return The number of seconds remaining until the prize can be awarded. function prizePeriodRemainingSeconds() external view returns (uint256) { return _prizePeriodRemainingSeconds(); } /// @notice Returns the number of seconds remaining until the prize can be awarded. /// @return The number of seconds remaining until the prize can be awarded. function _prizePeriodRemainingSeconds() internal view returns (uint256) { uint256 endAt = _prizePeriodEndAt(); uint256 time = _currentTime(); if (time > endAt) { return 0; } return endAt.sub(time); } /// @notice Returns whether the prize period is over /// @return True if the prize period is over, false otherwise function isPrizePeriodOver() external view returns (bool) { return _isPrizePeriodOver(); } /// @notice Returns whether the prize period is over /// @return True if the prize period is over, false otherwise function _isPrizePeriodOver() internal view returns (bool) { return _currentTime() >= _prizePeriodEndAt(); } /// @notice Awards collateral as tickets to a user /// @param user The user to whom the tickets are minted /// @param amount The amount of interest to mint as tickets. function _awardTickets(address user, uint256 amount) internal { prizePool.award(user, amount, address(ticket)); } /// @notice Awards all external tokens with non-zero balances to the given user. The external tokens must be held by the PrizePool contract. /// @param winner The user to transfer the tokens to function _awardAllExternalTokens(address winner) internal { _awardExternalErc20s(winner); _awardExternalErc721s(winner); } /// @notice Awards all external ERC20 tokens with non-zero balances to the given user. /// The external tokens must be held by the PrizePool contract. /// @param winner The user to transfer the tokens to function _awardExternalErc20s(address winner) internal { address currentToken = externalErc20s.start(); while (currentToken != address(0) && currentToken != externalErc20s.end()) { uint256 balance = IERC20(currentToken).balanceOf(address(prizePool)); if (balance > 0) { prizePool.awardExternalERC20(winner, currentToken, balance); } currentToken = externalErc20s.next(currentToken); } } /// @notice Awards all external ERC721 tokens to the given user. /// The external tokens must be held by the PrizePool contract. /// @dev The list of ERC721s is reset after every award /// @param winner The user to transfer the tokens to function _awardExternalErc721s(address winner) internal { address currentToken = externalErc721s.start(); while (currentToken != address(0) && currentToken != externalErc721s.end()) { uint256 balance = IERC721(currentToken).balanceOf(address(prizePool)); if (balance > 0) { prizePool.awardExternalERC721(winner, currentToken, externalErc721TokenIds[currentToken]); delete externalErc721TokenIds[currentToken]; } currentToken = externalErc721s.next(currentToken); } externalErc721s.clearAll(); } /// @notice Returns the timestamp at which the prize period ends /// @return The timestamp at which the prize period ends. function prizePeriodEndAt() external view returns (uint256) { // current prize started at is non-inclusive, so add one return _prizePeriodEndAt(); } /// @notice Returns the timestamp at which the prize period ends /// @return The timestamp at which the prize period ends. function _prizePeriodEndAt() internal view returns (uint256) { // current prize started at is non-inclusive, so add one return prizePeriodStartedAt.add(prizePeriodSeconds); } /// @notice Called by the PrizePool for transfers of controlled tokens /// @dev Note that this is only for *transfers*, not mints or burns /// @param controlledToken The type of collateral that is being sent function beforeTokenTransfer(address from, address to, uint256 amount, address controlledToken) external override onlyPrizePool { if (controlledToken == address(ticket)) { _requireNotLocked(); } if (address(tokenListener) != address(0)) { tokenListener.beforeTokenTransfer(from, to, amount, controlledToken); } } /// @notice Called by the PrizePool when minting controlled tokens /// @param controlledToken The type of collateral that is being minted function beforeTokenMint( address to, uint256 amount, address controlledToken, address referrer ) external override onlyPrizePool { if (controlledToken == address(ticket)) { _requireNotLocked(); } if (address(tokenListener) != address(0)) { tokenListener.beforeTokenMint(to, amount, controlledToken, referrer); } } /// @notice returns the current time. Used for testing. /// @return The current time (block.timestamp) function _currentTime() internal virtual view returns (uint256) { return block.timestamp; } /// @notice returns the current time. Used for testing. /// @return The current time (block.timestamp) function _currentBlock() internal virtual view returns (uint256) { return block.number; } /// @notice Starts the award process by starting random number request. The prize period must have ended. /// @dev The RNG-Request-Fee is expected to be held within this contract before calling this function function startAward() external requireCanStartAward { if (isRngTimedOut()) { delete rngRequest; emit RngRequestFailed(); } (address feeToken, uint256 requestFee) = rng.getRequestFee(); if (feeToken != address(0) && requestFee > 0) { IERC20(feeToken).approve(address(rng), requestFee); } (uint32 requestId, uint32 lockBlock) = rng.requestRandomNumber(); rngRequest.id = requestId; rngRequest.lockBlock = lockBlock; rngRequest.requestedAt = _currentTime().toUint32(); emit PrizePoolAwardStarted(_msgSender(), address(prizePool), requestId, lockBlock); } /// @notice Completes the award process and awards the winners. The random number must have been requested and is now available. function completeAward() external requireCanCompleteAward { uint256 randomNumber = rng.randomNumber(rngRequest.id); delete rngRequest; _distribute(randomNumber); // to avoid clock drift, we should calculate the start time based on the previous period start time. prizePeriodStartedAt = _calculateNextPrizePeriodStartTime(_currentTime()); emit PrizePoolAwarded(_msgSender(), randomNumber); emit PrizePoolOpened(_msgSender(), prizePeriodStartedAt); } function _calculateNextPrizePeriodStartTime(uint256 currentTime) internal view returns (uint256) { uint256 elapsedPeriods = currentTime.sub(prizePeriodStartedAt).div(prizePeriodSeconds); return prizePeriodStartedAt.add(elapsedPeriods.mul(prizePeriodSeconds)); } function calculateNextPrizePeriodStartTime(uint256 currentTime) external view returns (uint256) { return _calculateNextPrizePeriodStartTime(currentTime); } /// @notice Returns whether an award process can be started /// @return True if an award can be started, false otherwise. function canStartAward() external view returns (bool) { return _isPrizePeriodOver() && !isRngRequested(); } /// @notice Returns whether an award process can be completed /// @return True if an award can be completed, false otherwise. function canCompleteAward() external view returns (bool) { return isRngRequested() && isRngCompleted(); } /// @notice Returns whether a random number has been requested /// @return True if a random number has been requested, false otherwise. function isRngRequested() public view returns (bool) { return rngRequest.id != 0; } /// @notice Returns whether the random number request has completed. /// @return True if a random number request has completed, false otherwise. function isRngCompleted() public view returns (bool) { return rng.isRequestComplete(rngRequest.id); } /// @notice Returns the block number that the current RNG request has been locked to /// @return The block number that the RNG request is locked to function getLastRngLockBlock() external view returns (uint32) { return rngRequest.lockBlock; } /// @notice Returns the current RNG Request ID /// @return The current Request ID function getLastRngRequestId() external view returns (uint32) { return rngRequest.id; } /// @notice Sets the RNG service that the Prize Strategy is connected to /// @param rngService The address of the new RNG service interface function setRngService(RNGInterface rngService) external onlyOwner { require(!isRngRequested(), "PeriodicPrizeStrategy/rng-in-flight"); rng = rngService; emit RngServiceUpdated(address(rngService)); } function setRngRequestTimeout(uint32 _rngRequestTimeout) external onlyOwner { _setRngRequestTimeout(_rngRequestTimeout); } function _setRngRequestTimeout(uint32 _rngRequestTimeout) internal { require(_rngRequestTimeout > 60, "PeriodicPrizeStrategy/rng-timeout-gt-60-secs"); rngRequestTimeout = _rngRequestTimeout; emit RngRequestTimeoutSet(rngRequestTimeout); } /// @notice Gets the current list of External ERC20 tokens that will be awarded with the current prize /// @return An array of External ERC20 token addresses function getExternalErc20Awards() external view returns (address[] memory) { return externalErc20s.addressArray(); } /// @notice Adds an external ERC20 token type as an additional prize that can be awarded /// @dev Only the Prize-Strategy owner/creator can assign external tokens, /// and they must be approved by the Prize-Pool /// @param _externalErc20 The address of an ERC20 token to be awarded function addExternalErc20Award(address _externalErc20) external onlyOwner { // require(_externalErc20.isContract(), "PeriodicPrizeStrategy/external-erc20-not-contract"); require(prizePool.canAwardExternal(_externalErc20), "PeriodicPrizeStrategy/cannot-award-external"); externalErc20s.addAddress(_externalErc20); emit ExternalErc20AwardAdded(_externalErc20); } /// @notice Removes an external ERC20 token type as an additional prize that can be awarded /// @dev Only the Prize-Strategy owner/creator can remove external tokens /// @param _externalErc20 The address of an ERC20 token to be removed /// @param _prevExternalErc20 The address of the previous ERC20 token in the `externalErc20s` list. /// If the ERC20 is the first address, then the previous address is the SENTINEL address: 0x0000000000000000000000000000000000000001 function removeExternalErc20Award(address _externalErc20, address _prevExternalErc20) external onlyOwner { externalErc20s.removeAddress(_prevExternalErc20, _externalErc20); emit ExternalErc20AwardRemoved(_externalErc20); } /// @notice Gets the current list of External ERC721 tokens that will be awarded with the current prize /// @return An array of External ERC721 token addresses function getExternalErc721Awards() external view returns (address[] memory) { return externalErc721s.addressArray(); } /// @notice Gets the current list of External ERC721 tokens that will be awarded with the current prize /// @return An array of External ERC721 token addresses function getExternalErc721AwardTokenIds(address _externalErc721) external view returns (uint256[] memory) { return externalErc721TokenIds[_externalErc721]; } /// @notice Adds an external ERC721 token as an additional prize that can be awarded /// @dev Only the Prize-Strategy owner/creator can assign external tokens, /// and they must be approved by the Prize-Pool /// NOTE: The NFT must already be owned by the Prize-Pool /// @param _externalErc721 The address of an ERC721 token to be awarded /// @param _tokenIds An array of token IDs of the ERC721 to be awarded function addExternalErc721Award(address _externalErc721, uint256[] calldata _tokenIds) external onlyOwner { // require(_externalErc721.isContract(), "PeriodicPrizeStrategy/external-erc721-not-contract"); require(prizePool.canAwardExternal(_externalErc721), "PeriodicPrizeStrategy/cannot-award-external"); externalErc721s.addAddress(_externalErc721); for (uint256 i = 0; i < _tokenIds.length; i++) { uint256 tokenId = _tokenIds[i]; require(IERC721(_externalErc721).ownerOf(tokenId) == address(prizePool), "PeriodicPrizeStrategy/unavailable-token"); externalErc721TokenIds[_externalErc721].push(tokenId); } emit ExternalErc721AwardAdded(_externalErc721, _tokenIds); } /// @notice Removes an external ERC721 token as an additional prize that can be awarded /// @dev Only the Prize-Strategy owner/creator can remove external tokens /// @param _externalErc721 The address of an ERC721 token to be removed /// @param _prevExternalErc721 The address of the previous ERC721 token in the list. /// If no previous, then pass the SENTINEL address: 0x0000000000000000000000000000000000000001 function removeExternalErc721Award(address _externalErc721, address _prevExternalErc721) external onlyOwner { externalErc721s.removeAddress(_prevExternalErc721, _externalErc721); delete externalErc721TokenIds[_externalErc721]; emit ExternalErc721AwardRemoved(_externalErc721); } /// @notice Allows the owner to transfer out external ERC20 tokens /// @dev Used to transfer out tokens held by the Prize Pool. Could be liquidated, or anything. /// @param to The address that receives the tokens /// @param externalToken The address of the external asset token being transferred /// @param amount The amount of external assets to be transferred function transferExternalERC20( address to, address externalToken, uint256 amount ) external onlyOwner { prizePool.transferExternalERC20(to, externalToken, amount); } function _requireNotLocked() internal view { uint256 currentBlock = _currentBlock(); require(rngRequest.lockBlock == 0 || currentBlock < rngRequest.lockBlock, "PeriodicPrizeStrategy/rng-in-flight"); } function isRngTimedOut() public view returns (bool) { if (rngRequest.requestedAt == 0) { return false; } else { return _currentTime() > uint256(rngRequestTimeout).add(rngRequest.requestedAt); } } /// @dev Provides information about the current execution context for GSN Meta-Txs. /// @return The payable address of the message sender function _msgSender() internal override(BaseRelayRecipient, ContextUpgradeSafe) virtual view returns (address payable) { return BaseRelayRecipient._msgSender(); } /// @dev Provides information about the current execution context for GSN Meta-Txs. /// @return The payable address of the message sender function _msgData() internal override(BaseRelayRecipient, ContextUpgradeSafe) virtual view returns (bytes memory) { return BaseRelayRecipient._msgData(); } modifier requireNotLocked() { _requireNotLocked(); _; } modifier requireCanStartAward() { require(_isPrizePeriodOver(), "PeriodicPrizeStrategy/prize-period-not-over"); require(!isRngRequested() || isRngTimedOut(), "PeriodicPrizeStrategy/rng-already-requested"); _; } modifier requireCanCompleteAward() { require(_isPrizePeriodOver(), "PeriodicPrizeStrategy/prize-period-not-over"); require(isRngRequested(), "PeriodicPrizeStrategy/rng-not-requested"); require(isRngCompleted(), "PeriodicPrizeStrategy/rng-not-complete"); _; } modifier onlyPrizePool() { require(_msgSender() == address(prizePool), "PeriodicPrizeStrategy/only-prize-pool"); _; } }
pragma solidity ^0.6.0; /** * @dev Interface of the global ERC1820 Registry, as defined in the * https://eips.ethereum.org/EIPS/eip-1820[EIP]. Accounts may register * implementers for interfaces in this registry, as well as query support. * * Implementers may be shared by multiple accounts, and can also implement more * than a single interface for each account. Contracts can implement interfaces * for themselves, but externally-owned accounts (EOA) must delegate this to a * contract. * * {IERC165} interfaces can also be queried via the registry. * * For an in-depth explanation and source code analysis, see the EIP text. */ interface IERC1820Registry { /** * @dev Sets `newManager` as the manager for `account`. A manager of an * account is able to set interface implementers for it. * * By default, each account is its own manager. Passing a value of `0x0` in * `newManager` will reset the manager to this initial state. * * Emits a {ManagerChanged} event. * * Requirements: * * - the caller must be the current manager for `account`. */ function setManager(address account, address newManager) external; /** * @dev Returns the manager for `account`. * * See {setManager}. */ function getManager(address account) external view returns (address); /** * @dev Sets the `implementer` contract as ``account``'s implementer for * `interfaceHash`. * * `account` being the zero address is an alias for the caller's address. * The zero address can also be used in `implementer` to remove an old one. * * See {interfaceHash} to learn how these are created. * * Emits an {InterfaceImplementerSet} event. * * Requirements: * * - the caller must be the current manager for `account`. * - `interfaceHash` must not be an {IERC165} interface id (i.e. it must not * end in 28 zeroes). * - `implementer` must implement {IERC1820Implementer} and return true when * queried for support, unless `implementer` is the caller. See * {IERC1820Implementer-canImplementInterfaceForAddress}. */ function setInterfaceImplementer(address account, bytes32 interfaceHash, address implementer) external; /** * @dev Returns the implementer of `interfaceHash` for `account`. If no such * implementer is registered, returns the zero address. * * If `interfaceHash` is an {IERC165} interface id (i.e. it ends with 28 * zeroes), `account` will be queried for support of it. * * `account` being the zero address is an alias for the caller's address. */ function getInterfaceImplementer(address account, bytes32 interfaceHash) external view returns (address); /** * @dev Returns the interface hash for an `interfaceName`, as defined in the * corresponding * https://eips.ethereum.org/EIPS/eip-1820#interface-name[section of the EIP]. */ function interfaceHash(string calldata interfaceName) external pure returns (bytes32); /** * @notice Updates the cache with whether the contract implements an ERC165 interface or not. * @param account Address of the contract for which to update the cache. * @param interfaceId ERC165 interface for which to update the cache. */ function updateERC165Cache(address account, bytes4 interfaceId) external; /** * @notice Checks whether a contract implements an ERC165 interface or not. * If the result is not cached a direct lookup on the contract address is performed. * If the result is not cached or the cached value is out-of-date, the cache MUST be updated manually by calling * {updateERC165Cache} with the contract address. * @param account Address of the contract to check. * @param interfaceId ERC165 interface to check. * @return True if `account` implements `interfaceId`, false otherwise. */ function implementsERC165Interface(address account, bytes4 interfaceId) external view returns (bool); /** * @notice Checks whether a contract implements an ERC165 interface or not without using nor updating the cache. * @param account Address of the contract to check. * @param interfaceId ERC165 interface to check. * @return True if `account` implements `interfaceId`, false otherwise. */ function implementsERC165InterfaceNoCache(address account, bytes4 interfaceId) external view returns (bool); event InterfaceImplementerSet(address indexed account, bytes32 indexed interfaceHash, address indexed implementer); event ManagerChanged(address indexed account, address indexed newManager); }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0; /// @title Random Number Generator Interface /// @notice Provides an interface for requesting random numbers from 3rd-party RNG services (Chainlink VRF, Starkware VDF, etc..) interface RNGInterface { /// @notice Emitted when a new request for a random number has been submitted /// @param requestId The indexed ID of the request used to get the results of the RNG service /// @param sender The indexed address of the sender of the request event RandomNumberRequested(uint32 indexed requestId, address indexed sender); /// @notice Emitted when an existing request for a random number has been completed /// @param requestId The indexed ID of the request used to get the results of the RNG service /// @param randomNumber The random number produced by the 3rd-party service event RandomNumberCompleted(uint32 indexed requestId, uint256 randomNumber); /// @notice Gets the last request id used by the RNG service /// @return requestId The last request id used in the last request function getLastRequestId() external view returns (uint32 requestId); /// @notice Gets the Fee for making a Request against an RNG service /// @return feeToken The address of the token that is used to pay fees /// @return requestFee The fee required to be paid to make a request function getRequestFee() external view returns (address feeToken, uint256 requestFee); /// @notice Sends a request for a random number to the 3rd-party service /// @dev Some services will complete the request immediately, others may have a time-delay /// @dev Some services require payment in the form of a token, such as $LINK for Chainlink VRF /// @return requestId The ID of the request used to get the results of the RNG service /// @return lockBlock The block number at which the RNG service will start generating time-delayed randomness. The calling contract /// should "lock" all activity until the result is available via the `requestId` function requestRandomNumber() external returns (uint32 requestId, uint32 lockBlock); /// @notice Checks if the request for randomness from the 3rd-party service has completed /// @dev For time-delayed requests, this function is used to check/confirm completion /// @param requestId The ID of the request used to get the results of the RNG service /// @return isCompleted True if the request has completed and a random number is available, false otherwise function isRequestComplete(uint32 requestId) external view returns (bool isCompleted); /// @notice Gets the random number produced by the 3rd-party service /// @param requestId The ID of the request used to get the results of the RNG service /// @return randomNum The random number function randomNumber(uint32 requestId) external returns (uint256 randomNum); }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.5.0 <0.7.0; /// @title Interface that allows a user to draw an address using an index interface TicketInterface { /// @notice Selects a user using a random number. The random number will be uniformly bounded to the ticket totalSupply. /// @param randomNumber The random number to use to select a user. /// @return The winner function draw(uint256 randomNumber) external view returns (address); }
pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/introspection/IERC1820Registry.sol"; library Constants { IERC1820Registry public constant REGISTRY = IERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24); // keccak256("ERC777TokensSender") bytes32 public constant TOKENS_SENDER_INTERFACE_HASH = 0x29ddb589b1fb5fc7cf394961c1adf5f8c6454761adf795e67fe149f658abe895; // keccak256("ERC777TokensRecipient") bytes32 public constant TOKENS_RECIPIENT_INTERFACE_HASH = 0xb281fc8c12954d22544db45de3159a39272895b169a852b314f9cc762e44c53b; // keccak256(abi.encodePacked("ERC1820_ACCEPT_MAGIC")); bytes32 public constant ACCEPT_MAGIC = 0xa2ef4600d742022d532d4747cb3547474667d6f13804902513b2ec01c848f4b4; }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; pragma experimental ABIEncoderV2; import "@pooltogether/pooltogether-rng-contracts/contracts/RNGInterface.sol"; import "../token/TokenListenerInterface.sol"; import "../prize-pool/PrizePool.sol"; import "../prize-strategy/single-random-winner/SingleRandomWinnerProxyFactory.sol"; import "../token/ControlledTokenProxyFactory.sol"; import "../token/TicketProxyFactory.sol"; import "../external/openzeppelin/OpenZeppelinProxyFactoryInterface.sol"; /* solium-disable security/no-block-members */ contract SingleRandomWinnerBuilder { using SafeCast for uint256; event SingleRandomWinnerCreated ( address indexed singleRandomWinner, address indexed ticket, address indexed sponsorship ); struct SingleRandomWinnerConfig { RNGInterface rngService; uint256 prizePeriodStart; uint256 prizePeriodSeconds; string ticketName; string ticketSymbol; string sponsorshipName; string sponsorshipSymbol; uint256 ticketCreditLimitMantissa; uint256 ticketCreditRateMantissa; address[] externalERC20Awards; } ControlledTokenProxyFactory public controlledTokenProxyFactory; TicketProxyFactory public ticketProxyFactory; SingleRandomWinnerProxyFactory public singleRandomWinnerProxyFactory; address public trustedForwarder; constructor ( SingleRandomWinnerProxyFactory _singleRandomWinnerProxyFactory, address _trustedForwarder, ControlledTokenProxyFactory _controlledTokenProxyFactory, TicketProxyFactory _ticketProxyFactory ) public { require(address(_singleRandomWinnerProxyFactory) != address(0), "SingleRandomWinnerBuilder/single-random-winner-factory-not-zero"); require(address(_controlledTokenProxyFactory) != address(0), "SingleRandomWinnerBuilder/controlled-token-proxy-factory-not-zero"); require(address(_ticketProxyFactory) != address(0), "SingleRandomWinnerBuilder/ticket-proxy-factory-not-zero"); ticketProxyFactory = _ticketProxyFactory; singleRandomWinnerProxyFactory = _singleRandomWinnerProxyFactory; trustedForwarder = _trustedForwarder; controlledTokenProxyFactory = _controlledTokenProxyFactory; } function createSingleRandomWinner( PrizePool prizePool, SingleRandomWinnerConfig calldata config, uint8 decimals, address owner ) external returns (SingleRandomWinner) { SingleRandomWinner prizeStrategy = singleRandomWinnerProxyFactory.create(); address ticket = address( _createTicket( prizePool, config.ticketName, config.ticketSymbol, decimals ) ); address sponsorship = address( _createControlledToken( prizePool, config.sponsorshipName, config.sponsorshipSymbol, decimals ) ); prizeStrategy.initialize( trustedForwarder, config.prizePeriodStart, config.prizePeriodSeconds, prizePool, ticket, sponsorship, config.rngService, config.externalERC20Awards ); prizeStrategy.transferOwnership(owner); emit SingleRandomWinnerCreated(address(prizeStrategy), ticket, sponsorship); return prizeStrategy; } function _createControlledToken( TokenControllerInterface controller, string memory name, string memory symbol, uint8 decimals ) internal returns (ControlledToken) { ControlledToken token = controlledTokenProxyFactory.create(); token.initialize(string(name), string(symbol), decimals, trustedForwarder, controller); return token; } function _createTicket( TokenControllerInterface controller, string memory name, string memory symbol, uint8 decimals ) internal returns (Ticket) { Ticket ticket = ticketProxyFactory.create(); ticket.initialize(string(name), string(symbol), decimals, trustedForwarder, controller); return ticket; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "./SingleRandomWinner.sol"; import "../../external/openzeppelin/ProxyFactory.sol"; contract SingleRandomWinnerProxyFactory is ProxyFactory { SingleRandomWinner public instance; constructor () public { instance = new SingleRandomWinner(); } function create() external returns (SingleRandomWinner) { return SingleRandomWinner(deployMinimal(address(instance), "")); } }
pragma solidity >=0.6.0 <0.7.0; // solium-disable security/no-inline-assembly // solium-disable security/no-low-level-calls contract ProxyFactory { event ProxyCreated(address proxy); function deployMinimal(address _logic, bytes memory _data) public returns (address proxy) { // Adapted from https://github.com/optionality/clone-factory/blob/32782f82dfc5a00d103a7e61a17a5dedbd1e8e9d/contracts/CloneFactory.sol bytes20 targetBytes = bytes20(_logic); assembly { let clone := mload(0x40) mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000) mstore(add(clone, 0x14), targetBytes) mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000) proxy := create(0, clone, 0x37) } emit ProxyCreated(address(proxy)); if(_data.length > 0) { (bool success,) = proxy.call(_data); require(success, "ProxyFactory/constructor-call-failed"); } } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "./ControlledToken.sol"; import "../external/openzeppelin/ProxyFactory.sol"; /// @title Controlled ERC20 Token Factory /// @notice Minimal proxy pattern for creating new Controlled ERC20 Tokens contract ControlledTokenProxyFactory is ProxyFactory { /// @notice Contract template for deploying proxied tokens ControlledToken public instance; /// @notice Initializes the Factory with an instance of the Controlled ERC20 Token constructor () public { instance = new ControlledToken(); } /// @notice Creates a new Controlled ERC20 Token as a proxy of the template instance /// @return A reference to the new proxied Controlled ERC20 Token function create() external returns (ControlledToken) { return ControlledToken(deployMinimal(address(instance), "")); } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/Initializable.sol"; import "./Ticket.sol"; import "../external/openzeppelin/ProxyFactory.sol"; /// @title Controlled ERC20 Token Factory /// @notice Minimal proxy pattern for creating new Controlled ERC20 Tokens contract TicketProxyFactory is ProxyFactory { /// @notice Contract template for deploying proxied tokens Ticket public instance; /// @notice Initializes the Factory with an instance of the Controlled ERC20 Token constructor () public { instance = new Ticket(); } /// @notice Creates a new Controlled ERC20 Token as a proxy of the template instance /// @return A reference to the new proxied Controlled ERC20 Token function create() external returns (Ticket) { return Ticket(deployMinimal(address(instance), "")); } }
/** * @reviewers: [@clesaege, @unknownunknown1, @ferittuncer] * @auditors: [] * @bounties: [<14 days 10 ETH max payout>] * @deployments: [] */ pragma solidity ^0.6.0; /** * @title SortitionSumTreeFactory * @author Enrique Piqueras - <[email protected]> * @dev A factory of trees that keep track of staked values for sortition. */ library SortitionSumTreeFactory { /* Structs */ struct SortitionSumTree { uint K; // The maximum number of childs per node. // We use this to keep track of vacant positions in the tree after removing a leaf. This is for keeping the tree as balanced as possible without spending gas on moving nodes around. uint[] stack; uint[] nodes; // Two-way mapping of IDs to node indexes. Note that node index 0 is reserved for the root node, and means the ID does not have a node. mapping(bytes32 => uint) IDsToNodeIndexes; mapping(uint => bytes32) nodeIndexesToIDs; } /* Storage */ struct SortitionSumTrees { mapping(bytes32 => SortitionSumTree) sortitionSumTrees; } /* internal */ /** * @dev Create a sortition sum tree at the specified key. * @param _key The key of the new tree. * @param _K The number of children each node in the tree should have. */ function createTree(SortitionSumTrees storage self, bytes32 _key, uint _K) internal { SortitionSumTree storage tree = self.sortitionSumTrees[_key]; require(tree.K == 0, "Tree already exists."); require(_K > 1, "K must be greater than one."); tree.K = _K; tree.stack = new uint[](0); tree.nodes = new uint[](0); tree.nodes.push(0); } /** * @dev Set a value of a tree. * @param _key The key of the tree. * @param _value The new value. * @param _ID The ID of the value. * `O(log_k(n))` where * `k` is the maximum number of childs per node in the tree, * and `n` is the maximum number of nodes ever appended. */ function set(SortitionSumTrees storage self, bytes32 _key, uint _value, bytes32 _ID) internal { SortitionSumTree storage tree = self.sortitionSumTrees[_key]; uint treeIndex = tree.IDsToNodeIndexes[_ID]; if (treeIndex == 0) { // No existing node. if (_value != 0) { // Non zero value. // Append. // Add node. if (tree.stack.length == 0) { // No vacant spots. // Get the index and append the value. treeIndex = tree.nodes.length; tree.nodes.push(_value); // Potentially append a new node and make the parent a sum node. if (treeIndex != 1 && (treeIndex - 1) % tree.K == 0) { // Is first child. uint parentIndex = treeIndex / tree.K; bytes32 parentID = tree.nodeIndexesToIDs[parentIndex]; uint newIndex = treeIndex + 1; tree.nodes.push(tree.nodes[parentIndex]); delete tree.nodeIndexesToIDs[parentIndex]; tree.IDsToNodeIndexes[parentID] = newIndex; tree.nodeIndexesToIDs[newIndex] = parentID; } } else { // Some vacant spot. // Pop the stack and append the value. treeIndex = tree.stack[tree.stack.length - 1]; tree.stack.pop(); tree.nodes[treeIndex] = _value; } // Add label. tree.IDsToNodeIndexes[_ID] = treeIndex; tree.nodeIndexesToIDs[treeIndex] = _ID; updateParents(self, _key, treeIndex, true, _value); } } else { // Existing node. if (_value == 0) { // Zero value. // Remove. // Remember value and set to 0. uint value = tree.nodes[treeIndex]; tree.nodes[treeIndex] = 0; // Push to stack. tree.stack.push(treeIndex); // Clear label. delete tree.IDsToNodeIndexes[_ID]; delete tree.nodeIndexesToIDs[treeIndex]; updateParents(self, _key, treeIndex, false, value); } else if (_value != tree.nodes[treeIndex]) { // New, non zero value. // Set. bool plusOrMinus = tree.nodes[treeIndex] <= _value; uint plusOrMinusValue = plusOrMinus ? _value - tree.nodes[treeIndex] : tree.nodes[treeIndex] - _value; tree.nodes[treeIndex] = _value; updateParents(self, _key, treeIndex, plusOrMinus, plusOrMinusValue); } } } /* internal Views */ /** * @dev Query the leaves of a tree. Note that if `startIndex == 0`, the tree is empty and the root node will be returned. * @param _key The key of the tree to get the leaves from. * @param _cursor The pagination cursor. * @param _count The number of items to return. * @return startIndex The index at which leaves start * @return values The values of the returned leaves * @return hasMore Whether there are more for pagination. * `O(n)` where * `n` is the maximum number of nodes ever appended. */ function queryLeafs( SortitionSumTrees storage self, bytes32 _key, uint _cursor, uint _count ) internal view returns(uint startIndex, uint[] memory values, bool hasMore) { SortitionSumTree storage tree = self.sortitionSumTrees[_key]; // Find the start index. for (uint i = 0; i < tree.nodes.length; i++) { if ((tree.K * i) + 1 >= tree.nodes.length) { startIndex = i; break; } } // Get the values. uint loopStartIndex = startIndex + _cursor; values = new uint[](loopStartIndex + _count > tree.nodes.length ? tree.nodes.length - loopStartIndex : _count); uint valuesIndex = 0; for (uint j = loopStartIndex; j < tree.nodes.length; j++) { if (valuesIndex < _count) { values[valuesIndex] = tree.nodes[j]; valuesIndex++; } else { hasMore = true; break; } } } /** * @dev Draw an ID from a tree using a number. Note that this function reverts if the sum of all values in the tree is 0. * @param _key The key of the tree. * @param _drawnNumber The drawn number. * @return ID The drawn ID. * `O(k * log_k(n))` where * `k` is the maximum number of childs per node in the tree, * and `n` is the maximum number of nodes ever appended. */ function draw(SortitionSumTrees storage self, bytes32 _key, uint _drawnNumber) internal view returns(bytes32 ID) { SortitionSumTree storage tree = self.sortitionSumTrees[_key]; uint treeIndex = 0; uint currentDrawnNumber = _drawnNumber % tree.nodes[0]; while ((tree.K * treeIndex) + 1 < tree.nodes.length) // While it still has children. for (uint i = 1; i <= tree.K; i++) { // Loop over children. uint nodeIndex = (tree.K * treeIndex) + i; uint nodeValue = tree.nodes[nodeIndex]; if (currentDrawnNumber >= nodeValue) currentDrawnNumber -= nodeValue; // Go to the next child. else { // Pick this child. treeIndex = nodeIndex; break; } } ID = tree.nodeIndexesToIDs[treeIndex]; } /** @dev Gets a specified ID's associated value. * @param _key The key of the tree. * @param _ID The ID of the value. * @return value The associated value. */ function stakeOf(SortitionSumTrees storage self, bytes32 _key, bytes32 _ID) internal view returns(uint value) { SortitionSumTree storage tree = self.sortitionSumTrees[_key]; uint treeIndex = tree.IDsToNodeIndexes[_ID]; if (treeIndex == 0) value = 0; else value = tree.nodes[treeIndex]; } function total(SortitionSumTrees storage self, bytes32 _key) internal view returns (uint) { SortitionSumTree storage tree = self.sortitionSumTrees[_key]; if (tree.nodes.length == 0) { return 0; } else { return tree.nodes[0]; } } /* Private */ /** * @dev Update all the parents of a node. * @param _key The key of the tree to update. * @param _treeIndex The index of the node to start from. * @param _plusOrMinus Wether to add (true) or substract (false). * @param _value The value to add or substract. * `O(log_k(n))` where * `k` is the maximum number of childs per node in the tree, * and `n` is the maximum number of nodes ever appended. */ function updateParents(SortitionSumTrees storage self, bytes32 _key, uint _treeIndex, bool _plusOrMinus, uint _value) private { SortitionSumTree storage tree = self.sortitionSumTrees[_key]; uint parentIndex = _treeIndex; while (parentIndex != 0) { parentIndex = (parentIndex - 1) / tree.K; tree.nodes[parentIndex] = _plusOrMinus ? tree.nodes[parentIndex] + _value : tree.nodes[parentIndex] - _value; } } }
/** Copyright 2019 PoolTogether LLC This file is part of PoolTogether. PoolTogether is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation under version 3 of the License. PoolTogether is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with PoolTogether. If not, see <https://www.gnu.org/licenses/>. */ pragma solidity ^0.6.0; /** * @author Brendan Asselstine * @notice A library that uses entropy to select a random number within a bound. Compensates for modulo bias. * @dev Thanks to https://medium.com/hownetworks/dont-waste-cycles-with-modulo-bias-35b6fdafcf94 */ library UniformRandomNumber { /// @notice Select a random number without modulo bias using a random seed and upper bound /// @param _entropy The seed for randomness /// @param _upperBound The upper bound of the desired number /// @return A random number less than the _upperBound function uniform(uint256 _entropy, uint256 _upperBound) internal pure returns (uint256) { require(_upperBound > 0, "UniformRand/min-bound"); uint256 min = -_upperBound % _upperBound; uint256 random = _entropy; while (true) { if (random >= min) { break; } random = uint256(keccak256(abi.encodePacked(random))); } return random % _upperBound; } }
pragma solidity >=0.6.0 <0.7.0; interface OpenZeppelinProxyFactoryInterface { function deploy(uint256 _salt, address _logic, address _admin, bytes calldata _data) external returns (address); function getDeploymentAddress(uint256 _salt, address _sender) external view returns (address); }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "./CompoundPrizePool.sol"; import "../../external/openzeppelin/ProxyFactory.sol"; /// @title Compound Prize Pool Proxy Factory /// @notice Minimal proxy pattern for creating new Compound Prize Pools contract CompoundPrizePoolProxyFactory is ProxyFactory { /// @notice Contract template for deploying proxied Prize Pools CompoundPrizePool public instance; /// @notice Initializes the Factory with an instance of the Compound Prize Pool constructor () public { instance = new CompoundPrizePool(); } /// @notice Creates a new Compound Prize Pool as a proxy of the template instance /// @return A reference to the new proxied Compound Prize Pool function create() external returns (CompoundPrizePool) { return CompoundPrizePool(deployMinimal(address(instance), "")); } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/Initializable.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol"; import "../../external/pooltogether/FixedPoint.sol"; import "../../external/compound/CTokenInterface.sol"; import "../PrizePool.sol"; /// @title Prize Pool with Compound's cToken /// @notice Manages depositing and withdrawing assets from the Prize Pool contract CompoundPrizePool is PrizePool { using SafeMath for uint256; event CompoundPrizePoolInitialized(address indexed cToken); /// @notice Interface for the Yield-bearing cToken by Compound CTokenInterface public cToken; /// @notice Initializes the Prize Pool and Yield Service with the required contract connections /// @param _trustedForwarder Address of the Forwarding Contract for GSN Meta-Txs /// @param _controlledTokens Array of addresses for the Ticket and Sponsorship Tokens controlled by the Prize Pool /// @param _maxExitFeeMantissa The maximum exit fee size, relative to the withdrawal amount /// @param _maxTimelockDuration The maximum length of time the withdraw timelock could be /// @param _cToken Address of the Compound cToken interface function initialize ( address _trustedForwarder, RegistryInterface _reserveRegistry, address[] memory _controlledTokens, uint256 _maxExitFeeMantissa, uint256 _maxTimelockDuration, CTokenInterface _cToken ) public initializer { PrizePool.initialize( _trustedForwarder, _reserveRegistry, _controlledTokens, _maxExitFeeMantissa, _maxTimelockDuration ); cToken = _cToken; emit CompoundPrizePoolInitialized(address(cToken)); } /// @dev Gets the balance of the underlying assets held by the Yield Service /// @return The underlying balance of asset tokens function _balance() internal override returns (uint256) { return cToken.balanceOfUnderlying(address(this)); } /// @dev Allows a user to supply asset tokens in exchange for yield-bearing tokens /// to be held in escrow by the Yield Service /// @param amount The amount of asset tokens to be supplied function _supply(uint256 amount) internal override { _token().approve(address(cToken), amount); require(cToken.mint(amount) == 0, "CompoundPrizePool/mint-failed"); } /// @dev Checks with the Prize Pool if a specific token type may be awarded as a prize enhancement /// @param _externalToken The address of the token to check /// @return True if the token may be awarded, false otherwise function _canAwardExternal(address _externalToken) internal override view returns (bool) { return _externalToken != address(cToken); } /// @dev Allows a user to redeem yield-bearing tokens in exchange for the underlying /// asset tokens held in escrow by the Yield Service /// @param amount The amount of underlying tokens to be redeemed /// @return The actual amount of tokens transferred function _redeem(uint256 amount) internal override returns (uint256) { IERC20 assetToken = _token(); uint256 before = assetToken.balanceOf(address(this)); require(cToken.redeemUnderlying(amount) == 0, "CompoundPrizePool/redeem-failed"); uint256 diff = assetToken.balanceOf(address(this)).sub(before); return diff; } /// @dev Gets the underlying asset token used by the Yield Service /// @return A reference to the interface of the underling asset token function _token() internal override view returns (IERC20) { return IERC20(cToken.underlying()); } }
pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol"; interface CTokenInterface is IERC20 { function decimals() external view returns (uint8); function totalSupply() external override view returns (uint256); function underlying() external view returns (address); function balanceOfUnderlying(address owner) external returns (uint256); function supplyRatePerBlock() external returns (uint256); function exchangeRateCurrent() external returns (uint256); function mint(uint256 mintAmount) external returns (uint256); function balanceOf(address user) external override view returns (uint256); function redeemUnderlying(uint256 redeemAmount) external returns (uint256); }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; pragma experimental ABIEncoderV2; import "../token/ControlledTokenProxyFactory.sol"; import "../token/TicketProxyFactory.sol"; /* solium-disable security/no-block-members */ contract ControlledTokenBuilder { event CreatedControlledToken(address indexed token); event CreatedTicket(address indexed token); address public trustedForwarder; ControlledTokenProxyFactory public controlledTokenProxyFactory; TicketProxyFactory public ticketProxyFactory; struct ControlledTokenConfig { string name; string symbol; uint8 decimals; TokenControllerInterface controller; } constructor ( address _trustedForwarder, ControlledTokenProxyFactory _controlledTokenProxyFactory, TicketProxyFactory _ticketProxyFactory ) public { require(address(_controlledTokenProxyFactory) != address(0), "ControlledTokenBuilder/controlledTokenProxyFactory-not-zero"); require(address(_ticketProxyFactory) != address(0), "ControlledTokenBuilder/ticketProxyFactory-not-zero"); controlledTokenProxyFactory = _controlledTokenProxyFactory; trustedForwarder = _trustedForwarder; ticketProxyFactory = _ticketProxyFactory; } function createControlledToken( ControlledTokenConfig calldata config ) external returns (ControlledToken) { ControlledToken token = controlledTokenProxyFactory.create(); token.initialize( config.name, config.symbol, config.decimals, trustedForwarder, config.controller ); emit CreatedControlledToken(address(token)); return token; } function createTicket( ControlledTokenConfig calldata config ) external returns (Ticket) { Ticket token = ticketProxyFactory.create(); token.initialize( config.name, config.symbol, config.decimals, trustedForwarder, config.controller ); emit CreatedTicket(address(token)); return token; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; pragma experimental ABIEncoderV2; import "./PrizePoolBuilder.sol"; import "../registry/RegistryInterface.sol"; import "./SingleRandomWinnerBuilder.sol"; import "../prize-pool/stake/StakePrizePoolProxyFactory.sol"; /* solium-disable security/no-block-members */ contract StakePrizePoolBuilder is PrizePoolBuilder { using SafeMath for uint256; using SafeCast for uint256; struct StakePrizePoolConfig { IERC20 token; uint256 maxExitFeeMantissa; uint256 maxTimelockDuration; } RegistryInterface public reserveRegistry; StakePrizePoolProxyFactory public stakePrizePoolProxyFactory; SingleRandomWinnerBuilder public singleRandomWinnerBuilder; address public trustedForwarder; constructor ( RegistryInterface _reserveRegistry, address _trustedForwarder, StakePrizePoolProxyFactory _stakePrizePoolProxyFactory, SingleRandomWinnerBuilder _singleRandomWinnerBuilder ) public { require(address(_reserveRegistry) != address(0), "StakePrizePoolBuilder/reserveRegistry-not-zero"); require(address(_singleRandomWinnerBuilder) != address(0), "StakePrizePoolBuilder/single-random-winner-builder-not-zero"); require(address(_stakePrizePoolProxyFactory) != address(0), "StakePrizePoolBuilder/stake-prize-pool-proxy-factory-not-zero"); reserveRegistry = _reserveRegistry; singleRandomWinnerBuilder = _singleRandomWinnerBuilder; trustedForwarder = _trustedForwarder; stakePrizePoolProxyFactory = _stakePrizePoolProxyFactory; } function createSingleRandomWinner( StakePrizePoolConfig calldata prizePoolConfig, SingleRandomWinnerBuilder.SingleRandomWinnerConfig calldata prizeStrategyConfig, uint8 decimals ) external returns (StakePrizePool) { StakePrizePool prizePool = stakePrizePoolProxyFactory.create(); SingleRandomWinner prizeStrategy = singleRandomWinnerBuilder.createSingleRandomWinner( prizePool, prizeStrategyConfig, decimals, msg.sender ); address[] memory tokens; prizePool.initialize( trustedForwarder, reserveRegistry, tokens, prizePoolConfig.maxExitFeeMantissa, prizePoolConfig.maxTimelockDuration, prizePoolConfig.token ); _setupSingleRandomWinner( prizePool, prizeStrategy, prizeStrategyConfig.ticketCreditRateMantissa, prizeStrategyConfig.ticketCreditLimitMantissa ); prizePool.transferOwnership(msg.sender); emit PrizePoolCreated(msg.sender, address(prizePool)); return prizePool; } function createStakePrizePool( StakePrizePoolConfig calldata config ) external returns (StakePrizePool) { StakePrizePool prizePool = stakePrizePoolProxyFactory.create(); address[] memory tokens; prizePool.initialize( trustedForwarder, reserveRegistry, tokens, config.maxExitFeeMantissa, config.maxTimelockDuration, config.token ); prizePool.transferOwnership(msg.sender); emit PrizePoolCreated(msg.sender, address(prizePool)); return prizePool; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "./StakePrizePool.sol"; import "../../external/openzeppelin/ProxyFactory.sol"; /// @title Stake Prize Pool Proxy Factory /// @notice Minimal proxy pattern for creating new yVault Prize Pools contract StakePrizePoolProxyFactory is ProxyFactory { /// @notice Contract template for deploying proxied Prize Pools StakePrizePool public instance; /// @notice Initializes the Factory with an instance of the yVault Prize Pool constructor () public { instance = new StakePrizePool(); } /// @notice Creates a new Stake Prize Pool as a proxy of the template instance /// @return A reference to the new proxied Stake Prize Pool function create() external returns (StakePrizePool) { return StakePrizePool(deployMinimal(address(instance), "")); } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol"; import "../PrizePool.sol"; contract StakePrizePool is PrizePool { IERC20 private stakeToken; event StakePrizePoolInitialized(address indexed stakeToken); /// @notice Initializes the Prize Pool and Yield Service with the required contract connections /// @param _trustedForwarder Address of the Forwarding Contract for GSN Meta-Txs /// @param _controlledTokens Array of addresses for the Ticket and Sponsorship Tokens controlled by the Prize Pool /// @param _maxExitFeeMantissa The maximum exit fee size, relative to the withdrawal amount /// @param _maxTimelockDuration The maximum length of time the withdraw timelock could be /// @param _stakeToken Address of the stake token function initialize ( address _trustedForwarder, RegistryInterface _reserveRegistry, address[] memory _controlledTokens, uint256 _maxExitFeeMantissa, uint256 _maxTimelockDuration, IERC20 _stakeToken ) public initializer { PrizePool.initialize( _trustedForwarder, _reserveRegistry, _controlledTokens, _maxExitFeeMantissa, _maxTimelockDuration ); stakeToken = _stakeToken; emit StakePrizePoolInitialized(address(stakeToken)); } /// @notice Determines whether the passed token can be transferred out as an external award. /// @dev Different yield sources will hold the deposits as another kind of token: such a Compound's cToken. The /// prize strategy should not be allowed to move those tokens. /// @param _externalToken The address of the token to check /// @return True if the token may be awarded, false otherwise function _canAwardExternal(address _externalToken) internal override view returns (bool) { return address(stakeToken) != _externalToken; } /// @notice Returns the total balance (in asset tokens). This includes the deposits and interest. /// @return The underlying balance of asset tokens function _balance() internal override returns (uint256) { return stakeToken.balanceOf(address(this)); } function _token() internal override view returns (IERC20) { return stakeToken; } /// @notice Supplies asset tokens to the yield source. /// @param mintAmount The amount of asset tokens to be supplied function _supply(uint256 mintAmount) internal override { // no-op because nothing else needs to be done } /// @notice Redeems asset tokens from the yield source. /// @param redeemAmount The amount of yield-bearing tokens to be redeemed /// @return The actual amount of tokens that were redeemed. function _redeem(uint256 redeemAmount) internal override returns (uint256) { // no-op because nothing needs to be done } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; pragma experimental ABIEncoderV2; import "../registry/RegistryInterface.sol"; import "./SingleRandomWinnerBuilder.sol"; import "./PrizePoolBuilder.sol"; import "../prize-strategy/single-random-winner/SingleRandomWinnerProxyFactory.sol"; import "../prize-pool/yearn/yVaultPrizePoolProxyFactory.sol"; import "../token/ControlledTokenProxyFactory.sol"; import "../token/TicketProxyFactory.sol"; import "../external/yearn/yVaultInterface.sol"; import "../external/openzeppelin/OpenZeppelinProxyFactoryInterface.sol"; /* solium-disable security/no-block-members */ contract yVaultPrizePoolBuilder is PrizePoolBuilder { using SafeMath for uint256; using SafeCast for uint256; struct yVaultPrizePoolConfig { yVaultInterface vault; uint256 reserveRateMantissa; uint256 maxExitFeeMantissa; uint256 maxTimelockDuration; } RegistryInterface public reserveRegistry; yVaultPrizePoolProxyFactory public vaultPrizePoolProxyFactory; SingleRandomWinnerBuilder public singleRandomWinnerBuilder; address public trustedForwarder; constructor ( RegistryInterface _reserveRegistry, address _trustedForwarder, yVaultPrizePoolProxyFactory _vaultPrizePoolProxyFactory, SingleRandomWinnerBuilder _singleRandomWinnerBuilder ) public { require(address(_reserveRegistry) != address(0), "yVaultPrizePoolBuilder/reserveRegistry-not-zero"); require(address(_singleRandomWinnerBuilder) != address(0), "yVaultPrizePoolBuilder/single-random-winner-builder-not-zero"); require(address(_vaultPrizePoolProxyFactory) != address(0), "yVaultPrizePoolBuilder/compound-prize-pool-builder-not-zero"); reserveRegistry = _reserveRegistry; singleRandomWinnerBuilder = _singleRandomWinnerBuilder; trustedForwarder = _trustedForwarder; vaultPrizePoolProxyFactory = _vaultPrizePoolProxyFactory; } function createSingleRandomWinner( yVaultPrizePoolConfig calldata prizePoolConfig, SingleRandomWinnerBuilder.SingleRandomWinnerConfig calldata prizeStrategyConfig, uint8 decimals ) external returns (yVaultPrizePool) { yVaultPrizePool prizePool = vaultPrizePoolProxyFactory.create(); SingleRandomWinner prizeStrategy = singleRandomWinnerBuilder.createSingleRandomWinner( prizePool, prizeStrategyConfig, decimals, msg.sender ); address[] memory tokens; prizePool.initialize( trustedForwarder, reserveRegistry, tokens, prizePoolConfig.maxExitFeeMantissa, prizePoolConfig.maxTimelockDuration, prizePoolConfig.vault, prizePoolConfig.reserveRateMantissa ); _setupSingleRandomWinner( prizePool, prizeStrategy, prizeStrategyConfig.ticketCreditRateMantissa, prizeStrategyConfig.ticketCreditLimitMantissa ); prizePool.setCreditPlanOf( address(prizeStrategy.sponsorship()), prizeStrategyConfig.ticketCreditRateMantissa.toUint128(), prizeStrategyConfig.ticketCreditLimitMantissa.toUint128() ); prizePool.transferOwnership(msg.sender); emit PrizePoolCreated(msg.sender, address(prizePool)); return prizePool; } function createyVaultPrizePool( yVaultPrizePoolConfig calldata config ) external returns (yVaultPrizePool) { yVaultPrizePool prizePool = vaultPrizePoolProxyFactory.create(); address[] memory tokens; prizePool.initialize( trustedForwarder, reserveRegistry, tokens, config.maxExitFeeMantissa, config.maxTimelockDuration, config.vault, config.reserveRateMantissa ); prizePool.transferOwnership(msg.sender); emit PrizePoolCreated(msg.sender, address(prizePool)); return prizePool; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "./yVaultPrizePool.sol"; import "../../external/openzeppelin/ProxyFactory.sol"; /// @title yVault Prize Pool Proxy Factory /// @notice Minimal proxy pattern for creating new yVault Prize Pools contract yVaultPrizePoolProxyFactory is ProxyFactory { /// @notice Contract template for deploying proxied Prize Pools yVaultPrizePool public instance; /// @notice Initializes the Factory with an instance of the yVault Prize Pool constructor () public { instance = new yVaultPrizePool(); } /// @notice Creates a new yVault Prize Pool as a proxy of the template instance /// @return A reference to the new proxied yVault Prize Pool function create() external returns (yVaultPrizePool) { return yVaultPrizePool(deployMinimal(address(instance), "")); } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/Initializable.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol"; import "../../external/yearn/yVaultInterface.sol"; import "../PrizePool.sol"; /// @title Prize Pool for yEarn's yVaults contract yVaultPrizePool is PrizePool { using SafeMath for uint256; event yVaultPrizePoolInitialized(address indexed vault); event ReserveRateMantissaSet(uint256 reserveRateMantissa); /// @notice Interface for the yEarn yVault yVaultInterface public vault; /// Amount that is never exposed to the prize uint256 public reserveRateMantissa; /// @notice Initializes the Prize Pool and Yield Service with the required contract connections /// @param _trustedForwarder Address of the Forwarding Contract for GSN Meta-Txs /// @param _controlledTokens Array of addresses for the Ticket and Sponsorship Tokens controlled by the Prize Pool /// @param _maxExitFeeMantissa The maximum exit fee size, relative to the withdrawal amount /// @param _maxTimelockDuration The maximum length of time the withdraw timelock could be /// @param _vault Address of the yEarn yVaultInterface function initialize ( address _trustedForwarder, RegistryInterface _reserveRegistry, address[] memory _controlledTokens, uint256 _maxExitFeeMantissa, uint256 _maxTimelockDuration, yVaultInterface _vault, uint256 _reserveRateMantissa ) public initializer { PrizePool.initialize( _trustedForwarder, _reserveRegistry, _controlledTokens, _maxExitFeeMantissa, _maxTimelockDuration ); vault = _vault; _setReserveRateMantissa(_reserveRateMantissa); emit yVaultPrizePoolInitialized(address(vault)); } function setReserveRateMantissa(uint256 _reserveRateMantissa) external onlyOwner { _setReserveRateMantissa(_reserveRateMantissa); } function _setReserveRateMantissa(uint256 _reserveRateMantissa) internal { require(_reserveRateMantissa < 1 ether, "yVaultPrizePool/reserve-rate-lt-one"); reserveRateMantissa = _reserveRateMantissa; emit ReserveRateMantissaSet(reserveRateMantissa); } /// @dev Gets the balance of the underlying assets held by the Yield Service /// @return The underlying balance of asset tokens function _balance() internal override returns (uint256) { uint256 total = _sharesToToken(vault.balanceOf(address(this))); uint256 reserve = FixedPoint.multiplyUintByMantissa(total, reserveRateMantissa); return total.sub(reserve); } /// @dev Allows a user to supply asset tokens in exchange for yield-bearing tokens /// to be held in escrow by the Yield Service function _supply(uint256) internal override { IERC20 assetToken = _token(); uint256 total = assetToken.balanceOf(address(this)); assetToken.approve(address(vault), total); vault.deposit(total); } /// @dev Allows a user to supply asset tokens in exchange for yield-bearing tokens /// to be held in escrow by the Yield Service function _supplySpecific(uint256 amount) internal { _token().approve(address(vault), amount); vault.deposit(amount); } /// @dev The external token cannot be yDai or Dai /// @param _externalToken The address of the token to check /// @return True if the token may be awarded, false otherwise function _canAwardExternal(address _externalToken) internal override view returns (bool) { return _externalToken != address(vault) && _externalToken != vault.token(); } /// @dev Allows a user to redeem yield-bearing tokens in exchange for the underlying /// asset tokens held in escrow by the Yield Service /// @param amount The amount of underlying tokens to be redeemed /// @return The actual amount of tokens transferred function _redeem(uint256 amount) internal override returns (uint256) { IERC20 token = _token(); require(_balance() >= amount, "yVaultPrizePool/insuff-liquidity"); // yVault will try to over-withdraw so that amount is always available // we want: amount = X - X*feeRate // amount = X(1 - feeRate) // amount / (1 - feeRate) = X // calculate possible fee uint256 withdrawal; if (reserveRateMantissa > 0) { withdrawal = FixedPoint.divideUintByMantissa(amount, uint256(1e18).sub(reserveRateMantissa)); } else { withdrawal = amount; } uint256 sharesToWithdraw = _tokenToShares(withdrawal); uint256 preBalance = token.balanceOf(address(this)); vault.withdraw(sharesToWithdraw); uint256 postBalance = token.balanceOf(address(this)); uint256 amountWithdrawn = postBalance.sub(preBalance); uint256 amountRedeemable = (amountWithdrawn < amount) ? amountWithdrawn : amount; // Redeposit any asset funds that were removed premptively for fees if (postBalance > amountRedeemable) { _supplySpecific(postBalance.sub(amountRedeemable)); } return amountRedeemable; } function _tokenToShares(uint256 tokens) internal view returns (uint256) { /** ex. rate = tokens / shares => shares = shares_total * (tokens / tokens total) */ return vault.totalSupply().mul(tokens).div(vault.balance()); } function _sharesToToken(uint256 shares) internal view returns (uint256) { uint256 ts = vault.totalSupply(); if (ts == 0 || shares == 0) { return 0; } return (vault.balance().mul(shares)).div(ts); } /// @dev Gets the underlying asset token used by the Yield Service /// @return A reference to the interface of the underling asset token function _token() internal override view returns (IERC20) { return IERC20(vault.token()); } }
pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol"; interface yVaultInterface is IERC20 { function token() external view returns (address); function balance() external view returns (uint); function deposit(uint _amount) external; function withdraw(uint _shares) external; function getPricePerFullShare() external view returns (uint); }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; pragma experimental ABIEncoderV2; import "@openzeppelin/contracts-ethereum-package/contracts/utils/SafeCast.sol"; import "../utils/UInt256Array.sol"; import "./ComptrollerStorage.sol"; import "../token/TokenListenerInterface.sol"; /// @title The Comptroller disburses rewards to pool users /* solium-disable security/no-block-members */ contract Comptroller is ComptrollerStorage, TokenListenerInterface { using SafeMath for uint256; using SafeCast for uint256; using UInt256Array for uint256[]; using ExtendedSafeCast for uint256; using BalanceDrip for BalanceDrip.State; using VolumeDrip for VolumeDrip.State; using BalanceDripManager for BalanceDripManager.State; using VolumeDripManager for VolumeDripManager.State; using MappedSinglyLinkedList for MappedSinglyLinkedList.Mapping; /// @notice Emitted when a balance drip is actived event BalanceDripActivated( address indexed source, address indexed measure, address indexed dripToken, uint256 dripRatePerSecond ); /// @notice Emitted when a balance drip is deactivated event BalanceDripDeactivated( address indexed source, address indexed measure, address indexed dripToken ); /// @notice Emitted when a balance drip rate is updated event BalanceDripRateSet( address indexed source, address indexed measure, address indexed dripToken, uint256 dripRatePerSecond ); /// @notice Emitted when a balance drip drips tokens event BalanceDripDripped( address indexed source, address indexed measure, address indexed dripToken, address user, uint256 amount ); event DripTokenDripped( address indexed dripToken, address indexed user, uint256 amount ); /// @notice Emitted when a volue drip drips tokens event VolumeDripDripped( address indexed source, address indexed measure, address indexed dripToken, bool isReferral, address user, uint256 amount ); /// @notice Emitted when a user claims drip tokens event DripTokenClaimed( address indexed operator, address indexed dripToken, address indexed user, uint256 amount ); /// @notice Emitted when a volume drip is activated event VolumeDripActivated( address indexed source, address indexed measure, address indexed dripToken, bool isReferral, uint256 periodSeconds, uint256 dripAmount ); event TransferredOut( address indexed token, address indexed to, uint256 amount ); /// @notice Emitted when a new volume drip period has started event VolumeDripPeriodStarted( address indexed source, address indexed measure, address indexed dripToken, bool isReferral, uint32 period, uint256 dripAmount, uint256 endTime ); /// @notice Emitted when a volume drip period has ended event VolumeDripPeriodEnded( address indexed source, address indexed measure, address indexed dripToken, bool isReferral, uint32 period, uint256 totalSupply, uint256 drippedTokens ); /// @notice Emitted when a volume drip is updated event VolumeDripSet( address indexed source, address indexed measure, address indexed dripToken, bool isReferral, uint256 periodSeconds, uint256 dripAmount ); /// @notice Emitted when a volume drip is deactivated. event VolumeDripDeactivated( address indexed source, address indexed measure, address indexed dripToken, bool isReferral ); /// @notice Convenience struct used when updating drips struct UpdatePair { address source; address measure; } /// @notice Convenience struct used to retrieve balances after updating drips struct DripTokenBalance { address dripToken; uint256 balance; } /// @notice Initializes a new Comptroller. constructor () public { __Ownable_init(); } function transferOut(address token, address to, uint256 amount) external onlyOwner { IERC20(token).transfer(to, amount); emit TransferredOut(token, to, amount); } /// @notice Activates a balance drip. Only callable by the owner. /// @param source The balance drip "source"; i.e. a Prize Pool address. /// @param measure The ERC20 token whose balances determines user's share of the drip rate. /// @param dripToken The token that is dripped to users. /// @param dripRatePerSecond The amount of drip tokens that are awarded each second to the total supply of measure. function activateBalanceDrip(address source, address measure, address dripToken, uint256 dripRatePerSecond) external onlyOwner { balanceDrips[source].activateDrip(measure, dripToken, dripRatePerSecond); emit BalanceDripActivated( source, measure, dripToken, dripRatePerSecond ); } /// @notice Deactivates a balance drip. Only callable by the owner. /// @param source The balance drip "source"; i.e. a Prize Pool address. /// @param measure The ERC20 token whose balances determines user's share of the drip rate. /// @param dripToken The token that is dripped to users. /// @param prevDripToken The previous drip token in the balance drip list. If the dripToken is the first address, /// then the previous address is the SENTINEL address: 0x0000000000000000000000000000000000000001 function deactivateBalanceDrip(address source, address measure, address dripToken, address prevDripToken) external onlyOwner { _deactivateBalanceDrip(source, measure, dripToken, prevDripToken); } /// @notice Deactivates a balance drip. Only callable by the owner. /// @param source The balance drip "source"; i.e. a Prize Pool address. /// @param measure The ERC20 token whose balances determines user's share of the drip rate. /// @param dripToken The token that is dripped to users. /// @param prevDripToken The previous drip token in the balance drip list. If the dripToken is the first address, /// then the previous address is the SENTINEL address: 0x0000000000000000000000000000000000000001 function _deactivateBalanceDrip(address source, address measure, address dripToken, address prevDripToken) internal { balanceDrips[source].deactivateDrip(measure, dripToken, prevDripToken, _currentTime().toUint32(), _availableDripTokenBalance(dripToken)); emit BalanceDripDeactivated(source, measure, dripToken); } /// @notice Gets a list of active balance drip tokens /// @param source The balance drip "source"; i.e. a Prize Pool address. /// @param measure The ERC20 token whose balances determines user's share of the drip rate. /// @return An array of active Balance Drip token addresses function getActiveBalanceDripTokens(address source, address measure) external view returns (address[] memory) { return balanceDrips[source].getActiveBalanceDrips(measure); } /// @notice Returns the state of a balance drip. /// @param source The balance drip "source"; i.e. Prize Pool /// @param measure The token that measure's a users share of the drip /// @param dripToken The token that is being dripped to users /// @return dripRatePerSecond The current drip rate of the balance drip. /// @return exchangeRateMantissa The current exchange rate from measure to dripTokens /// @return timestamp The timestamp at which the balance drip was last updated. function getBalanceDrip( address source, address measure, address dripToken ) external view returns ( uint256 dripRatePerSecond, uint128 exchangeRateMantissa, uint32 timestamp ) { BalanceDrip.State storage balanceDrip = balanceDrips[source].getDrip(measure, dripToken); dripRatePerSecond = balanceDrip.dripRatePerSecond; exchangeRateMantissa = balanceDrip.exchangeRateMantissa; timestamp = balanceDrip.timestamp; } /// @notice Sets the drip rate for a balance drip. The drip rate is the number of drip tokens given to the /// entire supply of measure tokens. Only callable by the owner. /// @param source The balance drip "source"; i.e. Prize Pool /// @param measure The token to use to measure a user's share of the drip rate /// @param dripToken The token that is dripped to the user /// @param dripRatePerSecond The new drip rate per second function setBalanceDripRate(address source, address measure, address dripToken, uint256 dripRatePerSecond) external onlyOwner { balanceDrips[source].setDripRate(measure, dripToken, dripRatePerSecond, _currentTime().toUint32(), _availableDripTokenBalance(dripToken)); emit BalanceDripRateSet( source, measure, dripToken, dripRatePerSecond ); } /// @notice Activates a volume drip. Volume drips distribute tokens to users based on their share of the activity within a period. /// @param source The Prize Pool for which to bind to /// @param measure The Prize Pool controlled token whose volume should be measured /// @param dripToken The token that is being disbursed /// @param isReferral Whether this volume drip is for referrals /// @param periodSeconds The period of the volume drip, in seconds /// @param dripAmount The amount of dripTokens disbursed each period. /// @param endTime The time at which the first period ends. function activateVolumeDrip( address source, address measure, address dripToken, bool isReferral, uint32 periodSeconds, uint112 dripAmount, uint32 endTime ) external onlyOwner { uint32 period; if (isReferral) { period = referralVolumeDrips[source].activate(measure, dripToken, periodSeconds, dripAmount, endTime); } else { period = volumeDrips[source].activate(measure, dripToken, periodSeconds, dripAmount, endTime); } emit VolumeDripActivated( source, measure, dripToken, isReferral, periodSeconds, dripAmount ); emit VolumeDripPeriodStarted( source, measure, dripToken, isReferral, period, dripAmount, endTime ); } /// @notice Deactivates a volume drip. Volume drips distribute tokens to users based on their share of the activity within a period. /// @param source The Prize Pool for which to bind to /// @param measure The Prize Pool controlled token whose volume should be measured /// @param dripToken The token that is being disbursed /// @param isReferral Whether this volume drip is for referrals /// @param prevDripToken The previous drip token in the volume drip list. Is different for referrals vs non-referral volume drips. function deactivateVolumeDrip( address source, address measure, address dripToken, bool isReferral, address prevDripToken ) external onlyOwner { _deactivateVolumeDrip(source, measure, dripToken, isReferral, prevDripToken); } /// @notice Deactivates a volume drip. Volume drips distribute tokens to users based on their share of the activity within a period. /// @param source The Prize Pool for which to bind to /// @param measure The Prize Pool controlled token whose volume should be measured /// @param dripToken The token that is being disbursed /// @param isReferral Whether this volume drip is for referrals /// @param prevDripToken The previous drip token in the volume drip list. Is different for referrals vs non-referral volume drips. function _deactivateVolumeDrip( address source, address measure, address dripToken, bool isReferral, address prevDripToken ) internal { if (isReferral) { referralVolumeDrips[source].deactivate(measure, dripToken, prevDripToken); } else { volumeDrips[source].deactivate(measure, dripToken, prevDripToken); } emit VolumeDripDeactivated( source, measure, dripToken, isReferral ); } /// @notice Sets the parameters for the *next* volume drip period. The source, measure, dripToken and isReferral combined /// are used to uniquely identify a volume drip. Only callable by the owner. /// @param source The Prize Pool of the volume drip /// @param measure The token whose volume is being measured /// @param dripToken The token that is being disbursed /// @param isReferral Whether this volume drip is a referral /// @param periodSeconds The length to use for the next period /// @param dripAmount The amount of tokens to drip for the next period function setVolumeDrip( address source, address measure, address dripToken, bool isReferral, uint32 periodSeconds, uint112 dripAmount ) external onlyOwner { if (isReferral) { referralVolumeDrips[source].set(measure, dripToken, periodSeconds, dripAmount); } else { volumeDrips[source].set(measure, dripToken, periodSeconds, dripAmount); } emit VolumeDripSet( source, measure, dripToken, isReferral, periodSeconds, dripAmount ); } function getVolumeDrip( address source, address measure, address dripToken, bool isReferral ) external view returns ( uint256 periodSeconds, uint256 dripAmount, uint256 periodCount ) { VolumeDrip.State memory drip; if (isReferral) { drip = referralVolumeDrips[source].volumeDrips[measure][dripToken]; } else { drip = volumeDrips[source].volumeDrips[measure][dripToken]; } return ( drip.nextPeriodSeconds, drip.nextDripAmount, drip.periodCount ); } /// @notice Gets a list of active volume drip tokens /// @param source The volume drip "source"; i.e. a Prize Pool address. /// @param measure The ERC20 token whose volume determines user's share of the drip rate. /// @param isReferral Whether this volume drip is a referral /// @return An array of active Volume Drip token addresses function getActiveVolumeDripTokens(address source, address measure, bool isReferral) external view returns (address[] memory) { if (isReferral) { return referralVolumeDrips[source].getActiveVolumeDrips(measure); } else { return volumeDrips[source].getActiveVolumeDrips(measure); } } function isVolumeDripActive( address source, address measure, address dripToken, bool isReferral ) external view returns (bool) { if (isReferral) { return referralVolumeDrips[source].isActive(measure, dripToken); } else { return volumeDrips[source].isActive(measure, dripToken); } } function getVolumeDripPeriod( address source, address measure, address dripToken, bool isReferral, uint16 period ) external view returns ( uint112 totalSupply, uint112 dripAmount, uint32 endTime ) { VolumeDrip.Period memory periodState; if (isReferral) { periodState = referralVolumeDrips[source].volumeDrips[measure][dripToken].periods[period]; } else { periodState = volumeDrips[source].volumeDrips[measure][dripToken].periods[period]; } return ( periodState.totalSupply, periodState.dripAmount, periodState.endTime ); } /// @notice Returns a users claimable balance of drip tokens. This is the combination of all balance and volume drips. /// @param dripToken The token that is being disbursed /// @param user The user whose balance should be checked. /// @return The claimable balance of the dripToken by the user. function balanceOfDrip(address user, address dripToken) external view returns (uint256) { return dripTokenBalances[dripToken][user]; } /// @notice Claims a drip token on behalf of a user. If the passed amount is less than or equal to the users drip balance, then /// they will be transferred that amount. Otherwise, it fails. /// @param user The user for whom to claim the drip tokens /// @param dripToken The drip token to claim /// @param amount The amount of drip token to claim function claimDrip(address user, address dripToken, uint256 amount) public { address sender = _msgSender(); dripTokenTotalSupply[dripToken] = dripTokenTotalSupply[dripToken].sub(amount); dripTokenBalances[dripToken][user] = dripTokenBalances[dripToken][user].sub(amount); require(IERC20(dripToken).transfer(user, amount), "Comptroller/claim-transfer-failed"); emit DripTokenClaimed(sender, dripToken, user, amount); } function claimDrips(address user, address[] memory dripTokens) public { for (uint i = 0; i < dripTokens.length; i++) { claimDrip(user, dripTokens[i], dripTokenBalances[dripTokens[i]][user]); } } function updateActiveBalanceDripsForPairs( UpdatePair[] memory pairs ) public { uint256 currentTime = _currentTime(); uint256 i; for (i = 0; i < pairs.length; i++) { UpdatePair memory pair = pairs[i]; _updateActiveBalanceDrips( balanceDrips[pair.source], pair.source, pair.measure, IERC20(pair.measure).totalSupply(), currentTime ); } } function updateActiveVolumeDripsForPairs( UpdatePair[] memory pairs ) public { uint256 i; for (i = 0; i < pairs.length; i++) { UpdatePair memory pair = pairs[i]; _updateActiveVolumeDrips( volumeDrips[pair.source], pair.source, pair.measure, false ); _updateActiveVolumeDrips( referralVolumeDrips[pair.source], pair.source, pair.measure, true ); } } function mintAndCaptureVolumeDripsForPairs( UpdatePair[] memory pairs, address user, uint256 amount, address[] memory dripTokens ) public { uint256 i; for (i = 0; i < pairs.length; i++) { UpdatePair memory pair = pairs[i]; _mintAndCaptureForVolumeDrips(pair.source, pair.measure, user, amount, dripTokens); _mintAndCaptureReferralVolumeDrips(pair.source, pair.measure, user, amount, dripTokens); } } function _mintAndCaptureForVolumeDrips( address source, address measure, address user, uint256 amount, address[] memory dripTokens ) internal { uint i; for (i = 0; i < dripTokens.length; i++) { address dripToken = dripTokens[i]; VolumeDrip.State storage state = volumeDrips[source].volumeDrips[measure][dripToken]; _captureClaimForVolumeDrip(state, source, measure, dripToken, false, user, amount); } } function _mintAndCaptureReferralVolumeDrips( address source, address measure, address user, uint256 amount, address[] memory dripTokens ) internal { uint i; for (i = 0; i < dripTokens.length; i++) { address dripToken = dripTokens[i]; VolumeDrip.State storage referralState = referralVolumeDrips[source].volumeDrips[measure][dripToken]; _captureClaimForVolumeDrip(referralState, source, measure, dripToken, true, user, amount); } } function _captureClaimForVolumeDrip( VolumeDrip.State storage dripState, address source, address measure, address dripToken, bool isReferral, address user, uint256 amount ) internal { uint256 newUserTokens = dripState.mint( user, amount ); if (newUserTokens > 0) { _addDripBalance(dripToken, user, newUserTokens); emit VolumeDripDripped(source, measure, dripToken, isReferral, user, newUserTokens); } } /// @param pairs The (source, measure) pairs to update. For each pair all of the balance drips, volume drips, and referral volume drips will be updated. /// @param user The user whose drips and balances will be updated. /// @param dripTokens The drip tokens to retrieve claim balances for. function captureClaimsForBalanceDripsForPairs( UpdatePair[] memory pairs, address user, address[] memory dripTokens ) public { uint256 i; for (i = 0; i < pairs.length; i++) { UpdatePair memory pair = pairs[i]; uint256 measureBalance = IERC20(pair.measure).balanceOf(user); _captureClaimsForBalanceDrips(pair.source, pair.measure, user, measureBalance, dripTokens); } } function _captureClaimsForBalanceDrips( address source, address measure, address user, uint256 userMeasureBalance, address[] memory dripTokens ) internal { uint i; for (i = 0; i < dripTokens.length; i++) { address dripToken = dripTokens[i]; BalanceDrip.State storage state = balanceDrips[source].balanceDrips[measure][dripToken]; if (state.exchangeRateMantissa > 0) { _captureClaimForBalanceDrip(state, source, measure, dripToken, user, userMeasureBalance); } } } function _captureClaimForBalanceDrip( BalanceDrip.State storage dripState, address source, address measure, address dripToken, address user, uint256 measureBalance ) internal { uint256 newUserTokens = dripState.captureNewTokensForUser( user, measureBalance ); if (newUserTokens > 0) { _addDripBalance(dripToken, user, newUserTokens); emit BalanceDripDripped(source, measure, dripToken, user, newUserTokens); } } function balanceOfClaims( address user, address[] memory dripTokens ) public view returns (DripTokenBalance[] memory) { DripTokenBalance[] memory balances = new DripTokenBalance[](dripTokens.length); uint256 i; for (i = 0; i < dripTokens.length; i++) { balances[i] = DripTokenBalance({ dripToken: dripTokens[i], balance: dripTokenBalances[dripTokens[i]][user] }); } return balances; } /// @notice Updates the given drips for a user and then claims the given drip tokens. This call will /// poke all of the drips and update the claim balances for the given user. /// @dev This function will be useful to check the *current* claim balances for a user. /// Just need to run this as a constant function to see the latest balances. /// in order to claim the values, this function needs to be run alongside a claimDrip function. /// @param pairs The (source, measure) pairs of drips to update for the given user /// @param user The user for whom to update and claim tokens /// @param dripTokens The drip tokens whose entire balance will be claimed after the update. /// @return The claimable balance of each of the passed drip tokens for the user. These are the post-update balances, and therefore the most accurate. function updateDrips( UpdatePair[] memory pairs, address user, address[] memory dripTokens ) public returns (DripTokenBalance[] memory) { updateActiveBalanceDripsForPairs(pairs); captureClaimsForBalanceDripsForPairs(pairs, user, dripTokens); updateActiveVolumeDripsForPairs(pairs); mintAndCaptureVolumeDripsForPairs(pairs, user, 0, dripTokens); DripTokenBalance[] memory balances = balanceOfClaims(user, dripTokens); return balances; } /// @notice Updates the given drips for a user and then claims the given drip tokens. This call will /// poke all of the drips and update the claim balances for the given user. /// @dev This function will be useful to check the *current* claim balances for a user. /// Just need to run this as a constant function to see the latest balances. /// in order to claim the values, this function needs to be run alongside a claimDrip function. /// @param pairs The (source, measure) pairs of drips to update for the given user /// @param user The user for whom to update and claim tokens /// @param dripTokens The drip tokens whose entire balance will be claimed after the update. /// @return The claimable balance of each of the passed drip tokens for the user. These are the post-update balances, and therefore the most accurate. function updateAndClaimDrips( UpdatePair[] calldata pairs, address user, address[] calldata dripTokens ) external returns (DripTokenBalance[] memory) { DripTokenBalance[] memory balances = updateDrips(pairs, user, dripTokens); claimDrips(user, dripTokens); return balances; } function _activeBalanceDripTokens(address source, address measure) internal view returns (address[] memory) { return balanceDrips[source].activeBalanceDrips[measure].addressArray(); } function _activeVolumeDripTokens(address source, address measure) internal view returns (address[] memory) { return volumeDrips[source].activeVolumeDrips[measure].addressArray(); } function _activeReferralVolumeDripTokens(address source, address measure) internal view returns (address[] memory) { return referralVolumeDrips[source].activeVolumeDrips[measure].addressArray(); } /// @notice Updates the balance drips /// @param source The Prize Pool of the balance drip /// @param manager The BalanceDripManager whose drips should be updated /// @param measure The measure token whose balance is changing /// @param measureTotalSupply The last total supply of the measure tokens /// @param currentTime The current function _updateActiveBalanceDrips( BalanceDripManager.State storage manager, address source, address measure, uint256 measureTotalSupply, uint256 currentTime ) internal { address prevDripToken = manager.activeBalanceDrips[measure].end(); address currentDripToken = manager.activeBalanceDrips[measure].start(); while (currentDripToken != address(0) && currentDripToken != manager.activeBalanceDrips[measure].end()) { BalanceDrip.State storage dripState = manager.balanceDrips[measure][currentDripToken]; uint256 limit = _availableDripTokenBalance(currentDripToken); uint256 newTokens = dripState.drip( measureTotalSupply, currentTime, limit ); // if we've hit the limit, then kill it. bool isDripComplete = newTokens == limit; if (isDripComplete) { _deactivateBalanceDrip(source, measure, currentDripToken, prevDripToken); } prevDripToken = currentDripToken; currentDripToken = manager.activeBalanceDrips[measure].next(currentDripToken); } } /// @notice Records a deposit for a volume drip /// @param source The Prize Pool of the volume drip /// @param manager The VolumeDripManager containing the drips that need to be iterated through. /// @param isReferral Whether the passed manager contains referral volume drip /// @param measure The token that was deposited function _updateActiveVolumeDrips( VolumeDripManager.State storage manager, address source, address measure, bool isReferral ) internal { address prevDripToken = manager.activeVolumeDrips[measure].end(); uint256 currentTime = _currentTime(); address currentDripToken = manager.activeVolumeDrips[measure].start(); while (currentDripToken != address(0) && currentDripToken != manager.activeVolumeDrips[measure].end()) { VolumeDrip.State storage dripState = manager.volumeDrips[measure][currentDripToken]; uint256 limit = _availableDripTokenBalance(currentDripToken); uint32 lastPeriod = dripState.periodCount; uint256 newTokens = dripState.drip( currentTime, limit ); if (lastPeriod != dripState.periodCount) { emit VolumeDripPeriodEnded( source, measure, currentDripToken, isReferral, lastPeriod, dripState.periods[lastPeriod].totalSupply, newTokens ); emit VolumeDripPeriodStarted( source, measure, currentDripToken, isReferral, dripState.periodCount, dripState.periods[dripState.periodCount].dripAmount, dripState.periods[dripState.periodCount].endTime ); } // if we've hit the limit, then kill it. bool isDripComplete = newTokens == limit; if (isDripComplete) { _deactivateVolumeDrip(source, measure, currentDripToken, isReferral, prevDripToken); } prevDripToken = currentDripToken; currentDripToken = manager.activeVolumeDrips[measure].next(currentDripToken); } } function _addDripBalance(address dripToken, address user, uint256 amount) internal returns (uint256) { uint256 amountAvailable = _availableDripTokenBalance(dripToken); uint256 actualAmount = (amount > amountAvailable) ? amountAvailable : amount; dripTokenTotalSupply[dripToken] = dripTokenTotalSupply[dripToken].add(actualAmount); dripTokenBalances[dripToken][user] = dripTokenBalances[dripToken][user].add(actualAmount); emit DripTokenDripped(dripToken, user, actualAmount); return actualAmount; } function _availableDripTokenBalance(address dripToken) internal view returns (uint256) { uint256 comptrollerBalance = IERC20(dripToken).balanceOf(address(this)); uint256 totalClaimable = dripTokenTotalSupply[dripToken]; return (totalClaimable < comptrollerBalance) ? comptrollerBalance.sub(totalClaimable) : 0; } /// @notice Called by a "source" (i.e. Prize Pool) when a user mints new "measure" tokens. /// @param to The user who is minting the tokens /// @param amount The amount of tokens they are minting /// @param measure The measure token they are minting /// @param referrer The user who referred the minting. function beforeTokenMint( address to, uint256 amount, address measure, address referrer ) external override { address source = _msgSender(); uint256 balance = IERC20(measure).balanceOf(to); uint256 totalSupply = IERC20(measure).totalSupply(); address[] memory balanceDripTokens = _activeBalanceDripTokens(source, measure); _updateActiveBalanceDrips( balanceDrips[source], source, measure, totalSupply, _currentTime() ); _captureClaimsForBalanceDrips(source, measure, to, balance, balanceDripTokens); address[] memory volumeDripTokens = _activeVolumeDripTokens(source, measure); _updateActiveVolumeDrips( volumeDrips[source], source, measure, false ); _mintAndCaptureForVolumeDrips(source, measure, to, amount, volumeDripTokens); if (referrer != address(0)) { address[] memory referralVolumeDripTokens = _activeReferralVolumeDripTokens(source, measure); _updateActiveVolumeDrips( referralVolumeDrips[source], source, measure, true ); _mintAndCaptureReferralVolumeDrips(source, measure, referrer, amount, referralVolumeDripTokens); } } /// @notice Called by a "source" (i.e. Prize Pool) when tokens change hands or are burned /// @param from The user who is sending the tokens /// @param to The user who is receiving the tokens /// @param measure The measure token they are burning function beforeTokenTransfer( address from, address to, uint256, address measure ) external override { if (from == address(0)) { // ignore minting return; } address source = _msgSender(); uint256 totalSupply = IERC20(measure).totalSupply(); uint256 fromBalance = IERC20(measure).balanceOf(from); address[] memory balanceDripTokens = _activeBalanceDripTokens(source, measure); _updateActiveBalanceDrips( balanceDrips[source], source, measure, totalSupply, _currentTime() ); _captureClaimsForBalanceDrips(source, measure, from, fromBalance, balanceDripTokens); if (to != address(0)) { uint256 toBalance = IERC20(measure).balanceOf(to); _captureClaimsForBalanceDrips(source, measure, to, toBalance, balanceDripTokens); } } /// @notice returns the current time. Allows for override in testing. /// @return The current time (block.timestamp) function _currentTime() internal virtual view returns (uint256) { return block.timestamp; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; library UInt256Array { function remove(uint256[] storage self, uint256 index) internal { require(index < self.length, "UInt256Array/unknown-index"); self[index] = self[self.length-1]; delete self[self.length-1]; self.pop(); } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/access/Ownable.sol"; import "../drip/BalanceDripManager.sol"; import "../drip/VolumeDripManager.sol"; contract ComptrollerStorage is OwnableUpgradeSafe { mapping(address => VolumeDripManager.State) internal volumeDrips; mapping(address => VolumeDripManager.State) internal referralVolumeDrips; mapping(address => BalanceDripManager.State) internal balanceDrips; mapping(address => uint256) internal dripTokenTotalSupply; mapping(address => mapping(address => uint256)) internal dripTokenBalances; }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol"; import "../utils/MappedSinglyLinkedList.sol"; import "./BalanceDrip.sol"; /// @title Manages the lifecycle of a set of Balance Drips. library BalanceDripManager { using SafeMath for uint256; using MappedSinglyLinkedList for MappedSinglyLinkedList.Mapping; using BalanceDrip for BalanceDrip.State; struct State { mapping(address => MappedSinglyLinkedList.Mapping) activeBalanceDrips; mapping(address => mapping(address => BalanceDrip.State)) balanceDrips; } /// @notice Activates a drip by setting it's state and adding it to the active balance drips list. /// @param self The BalanceDripManager state /// @param measure The measure token /// @param dripToken The drip token /// @param dripRatePerSecond The amount of the drip token to be dripped per second function activateDrip( State storage self, address measure, address dripToken, uint256 dripRatePerSecond ) internal { require(!self.activeBalanceDrips[measure].contains(dripToken), "BalanceDripManager/drip-active"); if (self.activeBalanceDrips[measure].count == 0) { self.activeBalanceDrips[measure].initialize(); } self.activeBalanceDrips[measure].addAddress(dripToken); self.balanceDrips[measure][dripToken].resetTotalDripped(); self.balanceDrips[measure][dripToken].dripRatePerSecond = dripRatePerSecond; } /// @notice Deactivates an active balance drip. The balance drip is removed from the active balance drips list. /// The drip rate for the balance drip will be set to zero to ensure it's "frozen". /// @param measure The measure token /// @param dripToken The drip token /// @param prevDripToken The previous drip token previous in the list. /// If no previous, then pass the SENTINEL address: 0x0000000000000000000000000000000000000001 /// @param currentTime The current time function deactivateDrip( State storage self, address measure, address dripToken, address prevDripToken, uint32 currentTime, uint256 maxNewTokens ) internal { self.activeBalanceDrips[measure].removeAddress(prevDripToken, dripToken); self.balanceDrips[measure][dripToken].drip(IERC20(measure).totalSupply(), currentTime, maxNewTokens); self.balanceDrips[measure][dripToken].dripRatePerSecond = 0; } /// @notice Gets a list of active balance drip tokens /// @param self The BalanceDripManager state /// @param measure The measure token /// @return An array of Balance Drip token addresses function getActiveBalanceDrips(State storage self, address measure) internal view returns (address[] memory) { return self.activeBalanceDrips[measure].addressArray(); } /// @notice Sets the drip rate for an active balance drip. /// @param self The BalanceDripManager state /// @param measure The measure token /// @param dripToken The drip token /// @param dripRatePerSecond The amount to drip of the token each second /// @param currentTime The current time. function setDripRate( State storage self, address measure, address dripToken, uint256 dripRatePerSecond, uint32 currentTime, uint256 maxNewTokens ) internal { require(self.activeBalanceDrips[measure].contains(dripToken), "BalanceDripManager/drip-not-active"); self.balanceDrips[measure][dripToken].drip(IERC20(measure).totalSupply(), currentTime, maxNewTokens); self.balanceDrips[measure][dripToken].dripRatePerSecond = dripRatePerSecond; } /// @notice Returns whether or not a drip is active for the given measure, dripToken pair /// @param self The BalanceDripManager state /// @param measure The measure token /// @param dripToken The drip token /// @return True if there is an active balance drip for the pair, false otherwise function isDripActive(State storage self, address measure, address dripToken) internal view returns (bool) { return self.activeBalanceDrips[measure].contains(dripToken); } /// @notice Returns the BalanceDrip.State for the given measure, dripToken pair /// @param self The BalanceDripManager state /// @param measure The measure token /// @param dripToken The drip token /// @return The BalanceDrip.State for the pair function getDrip(State storage self, address measure, address dripToken) internal view returns (BalanceDrip.State storage) { return self.balanceDrips[measure][dripToken]; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/utils/SafeCast.sol"; import "../utils/ExtendedSafeCast.sol"; import "../external/pooltogether/FixedPoint.sol"; /// @title Calculates a users share of a token faucet. /// @notice The tokens are dripped at a "drip rate per second". This is the number of tokens that /// are dripped each second to the entire supply of a "measure" token. A user's share of ownership /// of the measure token corresponds to the share of the drip tokens per second. library BalanceDrip { using SafeMath for uint256; using SafeCast for uint256; using ExtendedSafeCast for uint256; struct UserState { uint128 lastExchangeRateMantissa; } struct State { uint256 dripRatePerSecond; uint112 exchangeRateMantissa; uint112 totalDripped; uint32 timestamp; mapping(address => UserState) userStates; } /// @notice Captures new tokens for a user /// @dev This must be called before changes to the user's balance (i.e. before mint, transfer or burns) /// @param self The balance drip state /// @param user The user to capture tokens for /// @param userMeasureBalance The current balance of the user's measure tokens /// @return The number of new tokens function captureNewTokensForUser( State storage self, address user, uint256 userMeasureBalance ) internal returns (uint128) { return _captureNewTokensForUser( self, user, userMeasureBalance ); } function resetTotalDripped(State storage self) internal { self.totalDripped = 0; } /// @notice Drips new tokens. /// @dev Should be called immediately before a change to the measure token's total supply /// @param self The balance drip state /// @param measureTotalSupply The measure token's last total supply (prior to any change) /// @param timestamp The current time /// @param maxNewTokens Maximum new tokens that can be dripped /// @return The number of new tokens dripped. function drip( State storage self, uint256 measureTotalSupply, uint256 timestamp, uint256 maxNewTokens ) internal returns (uint256) { // this should only run once per block. if (self.timestamp == uint32(timestamp)) { return 0; } uint256 lastTime = self.timestamp == 0 ? timestamp : self.timestamp; uint256 newSeconds = timestamp.sub(lastTime); uint112 exchangeRateMantissa = self.exchangeRateMantissa == 0 ? FixedPoint.SCALE.toUint112() : self.exchangeRateMantissa; uint256 newTokens; if (newSeconds > 0 && self.dripRatePerSecond > 0) { newTokens = newSeconds.mul(self.dripRatePerSecond); if (newTokens > maxNewTokens) { newTokens = maxNewTokens; } uint256 indexDeltaMantissa = measureTotalSupply > 0 ? FixedPoint.calculateMantissa(newTokens, measureTotalSupply) : 0; exchangeRateMantissa = uint256(exchangeRateMantissa).add(indexDeltaMantissa).toUint112(); } self.exchangeRateMantissa = exchangeRateMantissa; self.totalDripped = uint256(self.totalDripped).add(newTokens).toUint112(); self.timestamp = timestamp.toUint32(); return newTokens; } function _captureNewTokensForUser( State storage self, address user, uint256 userMeasureBalance ) private returns (uint128) { UserState storage userState = self.userStates[user]; uint256 lastExchangeRateMantissa = userState.lastExchangeRateMantissa; if (lastExchangeRateMantissa == 0) { // if the index is not intialized lastExchangeRateMantissa = FixedPoint.SCALE.toUint112(); } uint256 deltaExchangeRateMantissa = uint256(self.exchangeRateMantissa).sub(lastExchangeRateMantissa); uint128 newTokens = FixedPoint.multiplyUintByMantissa(userMeasureBalance, deltaExchangeRateMantissa).toUint128(); self.userStates[user] = UserState({ lastExchangeRateMantissa: self.exchangeRateMantissa }); return newTokens; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; library ExtendedSafeCast { /** * @dev Converts an unsigned uint256 into a unsigned uint112. * * Requirements: * * - input must be less than or equal to maxUint112. */ function toUint112(uint256 value) internal pure returns (uint112) { require(value < 2**112, "SafeCast: value doesn't fit in an uint112"); return uint112(value); } /** * @dev Converts an unsigned uint256 into a unsigned uint96. * * Requirements: * * - input must be less than or equal to maxUint96. */ function toUint96(uint256 value) internal pure returns (uint96) { require(value < 2**96, "SafeCast: value doesn't fit in an uint96"); return uint96(value); } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol"; import "../utils/MappedSinglyLinkedList.sol"; import "./VolumeDrip.sol"; /// @title Manages the active set of Volume Drips. library VolumeDripManager { using SafeMath for uint256; using MappedSinglyLinkedList for MappedSinglyLinkedList.Mapping; using VolumeDrip for VolumeDrip.State; struct State { mapping(address => MappedSinglyLinkedList.Mapping) activeVolumeDrips; mapping(address => mapping(address => VolumeDrip.State)) volumeDrips; } /// @notice Activates a volume drip for the given (measure,dripToken) pair. /// @param self The VolumeDripManager state /// @param measure The measure token /// @param dripToken The drip token /// @param periodSeconds The period of the volume drip in seconds /// @param dripAmount The amount of tokens to drip each period /// @param endTime The end time to set for the current period. function activate( State storage self, address measure, address dripToken, uint32 periodSeconds, uint112 dripAmount, uint32 endTime ) internal returns (uint32) { require(!self.activeVolumeDrips[measure].contains(dripToken), "VolumeDripManager/drip-active"); if (self.activeVolumeDrips[measure].count == 0) { self.activeVolumeDrips[measure].initialize(); } self.activeVolumeDrips[measure].addAddress(dripToken); self.volumeDrips[measure][dripToken].setNewPeriod(periodSeconds, dripAmount, endTime); return self.volumeDrips[measure][dripToken].periodCount; } /// @notice Deactivates the volume drip for the given (measure, dripToken) pair. /// @param self The VolumeDripManager state /// @param measure The measure token /// @param dripToken The drip token /// @param prevDripToken The active drip token previous to the passed on in the list. function deactivate( State storage self, address measure, address dripToken, address prevDripToken ) internal { self.activeVolumeDrips[measure].removeAddress(prevDripToken, dripToken); } /// @notice Gets a list of active balance drip tokens /// @param self The BalanceDripManager state /// @param measure The measure token /// @return An array of Balance Drip token addresses function getActiveVolumeDrips(State storage self, address measure) internal view returns (address[] memory) { return self.activeVolumeDrips[measure].addressArray(); } /// @notice Sets the parameters for the next period of an active volume drip /// @param self The VolumeDripManager state /// @param measure The measure token /// @param dripToken The drip token /// @param periodSeconds The length in seconds to use for the next period /// @param dripAmount The amount of tokens to be dripped in the next period function set(State storage self, address measure, address dripToken, uint32 periodSeconds, uint112 dripAmount) internal { require(self.activeVolumeDrips[measure].contains(dripToken), "VolumeDripManager/drip-not-active"); self.volumeDrips[measure][dripToken].setNextPeriod(periodSeconds, dripAmount); } /// @notice Returns whether or not an active volume drip exists for the given (measure, dripToken) pair /// @param self The VolumeDripManager state /// @param measure The measure token /// @param dripToken The drip token function isActive(State storage self, address measure, address dripToken) internal view returns (bool) { return self.activeVolumeDrips[measure].contains(dripToken); } /// @notice Returns the VolumeDrip.State for the given (measure, dripToken) pair. /// @param self The VolumeDripManager state /// @param measure The measure token /// @param dripToken The drip token function getDrip(State storage self, address measure, address dripToken) internal view returns (VolumeDrip.State storage) { return self.volumeDrips[measure][dripToken]; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/utils/SafeCast.sol"; import "../external/pooltogether/FixedPoint.sol"; import "../utils/ExtendedSafeCast.sol"; library VolumeDrip { using SafeMath for uint256; using SafeCast for uint256; using ExtendedSafeCast for uint256; struct Deposit { uint112 balance; uint32 period; } struct Period { uint112 totalSupply; uint112 dripAmount; uint32 endTime; } struct State { mapping(address => Deposit) deposits; mapping(uint32 => Period) periods; uint32 nextPeriodSeconds; uint112 nextDripAmount; uint112 __gap; uint112 totalDripped; uint32 periodCount; } function setNewPeriod( State storage self, uint32 _periodSeconds, uint112 dripAmount, uint32 endTime ) internal minPeriod(_periodSeconds) { self.nextPeriodSeconds = _periodSeconds; self.nextDripAmount = dripAmount; self.totalDripped = 0; self.periodCount = uint256(self.periodCount).add(1).toUint16(); self.periods[self.periodCount] = Period({ totalSupply: 0, dripAmount: dripAmount, endTime: endTime }); } function setNextPeriod( State storage self, uint32 _periodSeconds, uint112 dripAmount ) internal minPeriod(_periodSeconds) { self.nextPeriodSeconds = _periodSeconds; self.nextDripAmount = dripAmount; } function drip( State storage self, uint256 currentTime, uint256 maxNewTokens ) internal returns (uint256) { if (_isPeriodOver(self, currentTime)) { return _completePeriod(self, currentTime, maxNewTokens); } return 0; } function mint( State storage self, address user, uint256 amount ) internal returns (uint256) { if (self.periodCount == 0) { return 0; } uint256 accrued = _lastBalanceAccruedAmount(self, self.deposits[user].period, self.deposits[user].balance); uint32 currentPeriod = self.periodCount; if (accrued > 0) { self.deposits[user] = Deposit({ balance: amount.toUint112(), period: currentPeriod }); } else { self.deposits[user] = Deposit({ balance: uint256(self.deposits[user].balance).add(amount).toUint112(), period: currentPeriod }); } self.periods[currentPeriod].totalSupply = uint256(self.periods[currentPeriod].totalSupply).add(amount).toUint112(); return accrued; } function currentPeriod(State storage self) internal view returns (Period memory) { return self.periods[self.periodCount]; } function _isPeriodOver(State storage self, uint256 currentTime) private view returns (bool) { return currentTime >= self.periods[self.periodCount].endTime; } function _completePeriod( State storage self, uint256 currentTime, uint256 maxNewTokens ) private onlyPeriodOver(self, currentTime) returns (uint256) { // calculate the actual drip amount uint112 dripAmount; // If no one deposited, then don't drip anything if (self.periods[self.periodCount].totalSupply > 0) { dripAmount = self.periods[self.periodCount].dripAmount; } // if the drip amount is not valid, it has to be updated. if (dripAmount > maxNewTokens) { dripAmount = maxNewTokens.toUint112(); self.periods[self.periodCount].dripAmount = dripAmount; } // if we are completing the period far into the future, then we'll have skipped a lot of periods. // Here we set the end time so that it's the next period from *now* uint256 lastEndTime = self.periods[self.periodCount].endTime; uint256 numberOfPeriods = currentTime.sub(lastEndTime).div(self.nextPeriodSeconds).add(1); uint256 endTime = lastEndTime.add(numberOfPeriods.mul(self.nextPeriodSeconds)); self.totalDripped = uint256(self.totalDripped).add(dripAmount).toUint112(); self.periodCount = uint256(self.periodCount).add(1).toUint16(); self.periods[self.periodCount] = Period({ totalSupply: 0, dripAmount: self.nextDripAmount, endTime: endTime.toUint32() }); return dripAmount; } function _lastBalanceAccruedAmount( State storage self, uint32 depositPeriod, uint128 balance ) private view returns (uint256) { uint256 accrued; if (depositPeriod < self.periodCount && self.periods[depositPeriod].totalSupply > 0) { uint256 fractionMantissa = FixedPoint.calculateMantissa(balance, self.periods[depositPeriod].totalSupply); accrued = FixedPoint.multiplyUintByMantissa(self.periods[depositPeriod].dripAmount, fractionMantissa); } return accrued; } modifier onlyPeriodNotOver(State storage self, uint256 _currentTime) { require(!_isPeriodOver(self, _currentTime), "VolumeDrip/period-over"); _; } modifier onlyPeriodOver(State storage self, uint256 _currentTime) { require(_isPeriodOver(self, _currentTime), "VolumeDrip/period-not-over"); _; } modifier minPeriod(uint256 _periodSeconds) { require(_periodSeconds > 0, "VolumeDrip/period-gt-zero"); _; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "../prize-pool/PrizePool.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol"; contract CounterfactualAction { function depositTo(address payable user, PrizePool prizePool, address output, address referrer) external { IERC20 token = prizePool.token(); uint256 amount = token.balanceOf(address(this)); token.approve(address(prizePool), amount); prizePool.depositTo(user, amount, output, referrer); selfdestruct(user); } function cancel(address payable user, PrizePool prizePool) external { IERC20 token = prizePool.token(); token.transfer(user, token.balanceOf(address(this))); selfdestruct(user); } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/utils/Create2.sol"; import "./CounterfactualAction.sol"; import "../utils/MinimalProxyLibrary.sol"; contract CounterfactualActionFactory { CounterfactualAction public depositor; PrizePool public prizePool; function initialize(PrizePool _prizePool) external { require(address(_prizePool) != address(0), "CounterfactualActionFactory/prize-pool-not-zero"); depositor = new CounterfactualAction(); prizePool = _prizePool; } function calculateAddress(address payable user) external view returns (address) { return Create2.computeAddress(salt(user), keccak256(MinimalProxyLibrary.minimalProxy(address(depositor)))); } function depositTo(address payable user, address token, address referrer) external { CounterfactualAction d = newAction(user); d.depositTo(user, prizePool, token, referrer); } function cancel(address payable user) external { CounterfactualAction d = newAction(user); d.cancel(user, prizePool); } function newAction(address payable user) internal returns (CounterfactualAction) { return CounterfactualAction(Create2.deploy(0, salt(user), MinimalProxyLibrary.minimalProxy(address(depositor)))); } function salt(address payable user) internal pure returns (bytes32) { return keccak256(abi.encodePacked(user)); } function code() external view returns (bytes memory) { return MinimalProxyLibrary.minimalProxy(address(depositor)); } }
pragma solidity ^0.6.0; /** * @dev Helper to make usage of the `CREATE2` EVM opcode easier and safer. * `CREATE2` can be used to compute in advance the address where a smart * contract will be deployed, which allows for interesting new mechanisms known * as 'counterfactual interactions'. * * See the https://eips.ethereum.org/EIPS/eip-1014#motivation[EIP] for more * information. */ library Create2 { /** * @dev Deploys a contract using `CREATE2`. The address where the contract * will be deployed can be known in advance via {computeAddress}. * * The bytecode for a contract can be obtained from Solidity with * `type(contractName).creationCode`. * * Requirements: * * - `bytecode` must not be empty. * - `salt` must have not been used for `bytecode` already. * - the factory must have a balance of at least `amount`. * - if `amount` is non-zero, `bytecode` must have a `payable` constructor. */ function deploy(uint256 amount, bytes32 salt, bytes memory bytecode) internal returns (address) { address addr; require(address(this).balance >= amount, "Create2: insufficient balance"); require(bytecode.length != 0, "Create2: bytecode length is zero"); // solhint-disable-next-line no-inline-assembly assembly { addr := create2(amount, add(bytecode, 0x20), mload(bytecode), salt) } require(addr != address(0), "Create2: Failed on deploy"); return addr; } /** * @dev Returns the address where a contract will be stored if deployed via {deploy}. Any change in the * `bytecodeHash` or `salt` will result in a new destination address. */ function computeAddress(bytes32 salt, bytes32 bytecodeHash) internal view returns (address) { return computeAddress(salt, bytecodeHash, address(this)); } /** * @dev Returns the address where a contract will be stored if deployed via {deploy} from a contract located at * `deployer`. If `deployer` is this contract's address, returns the same value as {computeAddress}. */ function computeAddress(bytes32 salt, bytes32 bytecodeHash, address deployer) internal pure returns (address) { bytes32 _data = keccak256( abi.encodePacked(bytes1(0xff), deployer, salt, bytecodeHash) ); return address(bytes20(_data << 96)); } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.7.0; // solium-disable security/no-inline-assembly library MinimalProxyLibrary { function minimalProxy(address _logic) internal pure returns (bytes memory clone) { // Adapted from https://github.com/optionality/clone-factory/blob/32782f82dfc5a00d103a7e61a17a5dedbd1e8e9d/contracts/CloneFactory.sol bytes20 targetBytes = bytes20(_logic); // solhint-disable-next-line no-inline-assembly assembly { let size := 0x37 // allocate output byte array - this could also be done without assembly // by using clone = new bytes(size) clone := mload(0x40) // new "memory end" including padding mstore(0x40, add(clone, and(add(add(size, 0x20), 0x1f), not(0x1f)))) // store length in memory mstore(clone, size) mstore(add(clone, 0x20), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000) mstore(add(clone, 0x34), targetBytes) mstore(add(clone, 0x48), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000) } } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.6.12; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol"; interface DaiInterface is IERC20 { // --- Approve by signature --- function permit(address holder, address spender, uint256 nonce, uint256 expiry, bool allowed, uint8 v, bytes32 r, bytes32 s) external; function transferFrom(address src, address dst, uint wad) external override returns (bool); }
pragma solidity >=0.6.0 <0.7.0; pragma experimental ABIEncoderV2; import "@opengsn/gsn/contracts/forwarder/Forwarder.sol"; contract TrustedForwarder is Forwarder {}
// SPDX-License-Identifier:MIT pragma solidity ^0.6.2; pragma experimental ABIEncoderV2; import "@openzeppelin/contracts/cryptography/ECDSA.sol"; import "./IForwarder.sol"; contract Forwarder is IForwarder { using ECDSA for bytes32; string public constant GENERIC_PARAMS = "address from,address to,uint256 value,uint256 gas,uint256 nonce,bytes data"; string public constant EIP712_DOMAIN_TYPE = "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"; mapping(bytes32 => bool) public typeHashes; mapping(bytes32 => bool) public domains; // Nonces of senders, used to prevent replay attacks mapping(address => uint256) private nonces; // solhint-disable-next-line no-empty-blocks receive() external payable {} function getNonce(address from) public view override returns (uint256) { return nonces[from]; } constructor() public { string memory requestType = string(abi.encodePacked("ForwardRequest(", GENERIC_PARAMS, ")")); registerRequestTypeInternal(requestType); } function verify( ForwardRequest memory req, bytes32 domainSeparator, bytes32 requestTypeHash, bytes calldata suffixData, bytes calldata sig) external override view { _verifyNonce(req); _verifySig(req, domainSeparator, requestTypeHash, suffixData, sig); } function execute( ForwardRequest memory req, bytes32 domainSeparator, bytes32 requestTypeHash, bytes calldata suffixData, bytes calldata sig ) external payable override returns (bool success, bytes memory ret) { _verifyNonce(req); _verifySig(req, domainSeparator, requestTypeHash, suffixData, sig); _updateNonce(req); // solhint-disable-next-line avoid-low-level-calls (success,ret) = req.to.call{gas : req.gas, value : req.value}(abi.encodePacked(req.data, req.from)); if ( address(this).balance>0 ) { //can't fail: req.from signed (off-chain) the request, so it must be an EOA... payable(req.from).transfer(address(this).balance); } return (success,ret); } function _verifyNonce(ForwardRequest memory req) internal view { require(nonces[req.from] == req.nonce, "nonce mismatch"); } function _updateNonce(ForwardRequest memory req) internal { nonces[req.from]++; } function registerRequestType(string calldata typeName, string calldata typeSuffix) external override { for (uint i = 0; i < bytes(typeName).length; i++) { bytes1 c = bytes(typeName)[i]; require(c != "(" && c != ")", "invalid typename"); } string memory requestType = string(abi.encodePacked(typeName, "(", GENERIC_PARAMS, ",", typeSuffix)); registerRequestTypeInternal(requestType); } function registerDomainSeparator(string calldata name, string calldata version) external override { uint256 chainId; /* solhint-disable-next-line no-inline-assembly */ assembly { chainId := chainid() } bytes memory domainValue = abi.encode( keccak256(bytes(EIP712_DOMAIN_TYPE)), keccak256(bytes(name)), keccak256(bytes(version)), chainId, address(this)); bytes32 domainHash = keccak256(domainValue); domains[domainHash] = true; emit DomainRegistered(domainHash, domainValue); } function registerRequestTypeInternal(string memory requestType) internal { bytes32 requestTypehash = keccak256(bytes(requestType)); typeHashes[requestTypehash] = true; emit RequestTypeRegistered(requestTypehash, requestType); } event DomainRegistered(bytes32 indexed domainSeparator, bytes domainValue); event RequestTypeRegistered(bytes32 indexed typeHash, string typeStr); function _verifySig( ForwardRequest memory req, bytes32 domainSeparator, bytes32 requestTypeHash, bytes memory suffixData, bytes memory sig) internal view { require(domains[domainSeparator], "unregistered domain separator"); require(typeHashes[requestTypeHash], "unregistered request typehash"); bytes32 digest = keccak256(abi.encodePacked( "\x19\x01", domainSeparator, keccak256(_getEncoded(req, requestTypeHash, suffixData)) )); require(digest.recover(sig) == req.from, "signature mismatch"); } function _getEncoded( ForwardRequest memory req, bytes32 requestTypeHash, bytes memory suffixData ) public pure returns ( bytes memory ) { return abi.encodePacked( requestTypeHash, abi.encode( req.from, req.to, req.value, req.gas, req.nonce, keccak256(req.data) ), suffixData ); } }
// SPDX-License-Identifier: MIT pragma solidity ^0.6.0; /** * @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations. * * These functions can be used to verify that a message was signed by the holder * of the private keys of a given address. */ library ECDSA { /** * @dev Returns the address that signed a hashed message (`hash`) with * `signature`. This address can then be used for verification purposes. * * The `ecrecover` EVM opcode allows for malleable (non-unique) signatures: * this function rejects them by requiring the `s` value to be in the lower * half order, and the `v` value to be either 27 or 28. * * IMPORTANT: `hash` _must_ be the result of a hash operation for the * verification to be secure: it is possible to craft signatures that * recover to arbitrary addresses for non-hashed data. A safe way to ensure * this is by receiving a hash of the original message (which may otherwise * be too long), and then calling {toEthSignedMessageHash} on it. */ function recover(bytes32 hash, bytes memory signature) internal pure returns (address) { // Check the signature length if (signature.length != 65) { revert("ECDSA: invalid signature length"); } // Divide the signature in r, s and v variables bytes32 r; bytes32 s; uint8 v; // ecrecover takes the signature parameters, and the only way to get them // currently is to use assembly. // solhint-disable-next-line no-inline-assembly assembly { r := mload(add(signature, 0x20)) s := mload(add(signature, 0x40)) v := byte(0, mload(add(signature, 0x60))) } // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines // the valid range for s in (281): 0 < s < secp256k1n ÷ 2 + 1, and for v in (282): v ∈ {27, 28}. Most // signatures from current libraries generate a unique signature with an s-value in the lower half order. // // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept // these malleable signatures as well. if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) { revert("ECDSA: invalid signature 's' value"); } if (v != 27 && v != 28) { revert("ECDSA: invalid signature 'v' value"); } // If the signature is valid (and not malleable), return the signer address address signer = ecrecover(hash, v, r, s); require(signer != address(0), "ECDSA: invalid signature"); return signer; } /** * @dev Returns an Ethereum Signed Message, created from a `hash`. This * replicates the behavior of the * https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign[`eth_sign`] * JSON-RPC method. * * See {recover}. */ function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) { // 32 is the length in bytes of hash, // enforced by the type signature above return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash)); } }
// SPDX-License-Identifier:MIT pragma solidity ^0.6.2; pragma experimental ABIEncoderV2; interface IForwarder { struct ForwardRequest { address from; address to; uint256 value; uint256 gas; uint256 nonce; bytes data; } function getNonce(address from) external view returns(uint256); /** * verify the transaction would execute. * validate the signature and the nonce of the request. * revert if either signature or nonce are incorrect. */ function verify( ForwardRequest calldata forwardRequest, bytes32 domainSeparator, bytes32 requestTypeHash, bytes calldata suffixData, bytes calldata signature ) external view; /** * execute a transaction * @param forwardRequest - all transaction parameters * @param domainSeparator - domain used when signing this request * @param requestTypeHash - request type used when signing this request. * @param suffixData - the extension data used when signing this request. * @param signature - signature to validate. * * the transaction is verified, and then executed. * the success and ret of "call" are returned. * This method would revert only verification errors. target errors * are reported using the returned "success" and ret string */ function execute( ForwardRequest calldata forwardRequest, bytes32 domainSeparator, bytes32 requestTypeHash, bytes calldata suffixData, bytes calldata signature ) external payable returns (bool success, bytes memory ret); /** * Register a new Request typehash. * @param typeName - the name of the request type. * @param typeSuffix - anything after the generic params can be empty string (if no extra fields are needed) * if it does contain a value, then a comma is added first. */ function registerRequestType(string calldata typeName, string calldata typeSuffix) external; /** * Register a new domain separator. * The domain separator must have the following fields: name,version,chainId, verifyingContract. * the chainId is the current network's chainId, and the verifyingContract is this forwarder. * This method is given the domain name and version to create and register the domain separator value. * @param name the domain's display name * @param version the domain/protocol version */ function registerDomainSeparator(string calldata name, string calldata version) external; }
// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.6.12; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/SafeERC20.sol"; import "../external/maker/DaiInterface.sol"; import "../prize-pool/PrizePoolInterface.sol"; /// @title Allows users to approve and deposit dai into a prize pool in a single transaction. contract PermitAndDepositDai { using SafeERC20 for DaiInterface; /// @notice Permits this contract to spend on a users behalf, and deposits into the prize pool. /// @dev The Dai permit params match the Dai#permit function, but it expects the `spender` to be /// the address of this contract. /// @param holder The address spending the tokens /// @param nonce The nonce of the tx. Should be retrieved from the Dai token /// @param expiry The timestamp at which the sig expires /// @param allowed If true, then the spender is approving holder the max allowance. False makes the allowance zero. /// @param v The `v` portion of the signature. /// @param r The `r` portion of the signature. /// @param s The `s` portion of the signature. /// @param prizePool The prize pool to deposit into /// @param to The address that will receive the controlled tokens /// @param amount The amount to deposit /// @param controlledToken The type of token to be minted in exchange (i.e. tickets or sponsorship) /// @param referrer The address that referred the deposit function permitAndDepositTo( // --- Approve by signature --- address dai, address holder, uint256 nonce, uint256 expiry, bool allowed, uint8 v, bytes32 r, bytes32 s, address prizePool, address to, uint256 amount, address controlledToken, address referrer ) external { DaiInterface(dai).permit(holder, address(this), nonce, expiry, allowed, v, r, s); _depositTo(dai, holder, prizePool, to, amount, controlledToken, referrer); } /// @notice Deposits into a Prize Pool from the sender. Tokens will be transferred from the sender /// then deposited into the Pool on the sender's behalf. This can be called after permitAndDepositTo is called, /// as this contract will have full approval for a user. /// @param prizePool The prize pool to deposit into /// @param to The address that will receive the controlled tokens /// @param amount The amount to deposit /// @param controlledToken The type of token to be minted in exchange (i.e. tickets or sponsorship) /// @param referrer The address that referred the deposit function depositTo( address dai, address prizePool, address to, uint256 amount, address controlledToken, address referrer ) external { _depositTo(dai, msg.sender, prizePool, to, amount, controlledToken, referrer); } function _depositTo( address dai, address holder, address prizePool, address to, uint256 amount, address controlledToken, address referrer ) internal { DaiInterface(dai).safeTransferFrom(holder, address(this), amount); DaiInterface(dai).approve(address(prizePool), amount); PrizePoolInterface(prizePool).depositTo(to, amount, controlledToken, referrer); } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.5.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/access/Ownable.sol"; import "./RegistryInterface.sol"; /// @title Interface that allows a user to draw an address using an index contract Registry is OwnableUpgradeSafe, RegistryInterface { address private pointer; event Registered(address indexed pointer); constructor () public { __Ownable_init(); } function register(address _pointer) external onlyOwner { pointer = _pointer; emit Registered(pointer); } function lookup() external override view returns (address) { return pointer; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.5.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/access/Ownable.sol"; import "./ReserveInterface.sol"; import "../prize-pool/PrizePoolInterface.sol"; /// @title Interface that allows a user to draw an address using an index contract Reserve is OwnableUpgradeSafe, ReserveInterface { event ReserveRateMantissaSet(uint256 rateMantissa); uint256 public rateMantissa; constructor () public { __Ownable_init(); } function setRateMantissa( uint256 _rateMantissa ) external onlyOwner { rateMantissa = _rateMantissa; emit ReserveRateMantissaSet(rateMantissa); } function withdrawReserve(address prizePool, address to) external onlyOwner returns (uint256) { return PrizePoolInterface(prizePool).withdrawReserve(to); } function reserveRateMantissa(address) external view override returns (uint256) { return rateMantissa; } }
pragma solidity >=0.6.0 <0.7.0; import "../drip/BalanceDrip.sol"; contract BalanceDripExposed { using BalanceDrip for BalanceDrip.State; event DrippedTotalSupply( uint256 newTokens ); event Dripped( address indexed user, uint256 newTokens ); BalanceDrip.State internal dripState; function setDripRate( uint256 dripRatePerSecond ) external { dripState.dripRatePerSecond = dripRatePerSecond; } function drip( uint256 measureTotalSupply, uint256 currentTime, uint256 maxNewTokens ) external returns (uint256) { uint256 newTokens = dripState.drip( measureTotalSupply, currentTime, maxNewTokens ); emit DrippedTotalSupply(newTokens); return newTokens; } function captureNewTokensForUser( address user, uint256 userMeasureBalance ) external returns (uint128) { uint128 newTokens = dripState.captureNewTokensForUser( user, userMeasureBalance ); emit Dripped(user, newTokens); return newTokens; } function dripTwice( uint256 measureTotalSupply, uint256 currentTime, uint256 maxNewTokens ) external returns (uint256) { uint256 newTokens = dripState.drip( measureTotalSupply, currentTime, maxNewTokens ); newTokens = newTokens + dripState.drip( measureTotalSupply, currentTime, maxNewTokens ); emit DrippedTotalSupply(newTokens); return newTokens; } function exchangeRateMantissa() external view returns (uint256) { return dripState.exchangeRateMantissa; } function totalDripped() external view returns (uint256) { return dripState.totalDripped; } function resetTotalDripped() external { dripState.resetTotalDripped(); } }
pragma solidity >=0.6.0 <0.7.0; import "../drip/BalanceDripManager.sol"; contract BalanceDripManagerExposed { using BalanceDripManager for BalanceDripManager.State; BalanceDripManager.State dripManager; function activateDrip(address measure, address dripToken, uint256 dripRatePerSecond) external { dripManager.activateDrip(measure, dripToken, dripRatePerSecond); } function deactivateDrip(address measure, address prevDripToken, address dripToken, uint32 currentTime, uint256 maxNewTokens) external { dripManager.deactivateDrip(measure, prevDripToken, dripToken, currentTime, maxNewTokens); } function isDripActive(address measure, address dripToken) external view returns (bool) { return dripManager.isDripActive(measure, dripToken); } function setDripRate(address measure, address dripToken, uint256 dripRatePerSecond, uint32 currentTime, uint256 maxNewTokens) external { dripManager.setDripRate(measure, dripToken, dripRatePerSecond, currentTime, maxNewTokens); } function getActiveBalanceDrips(address measure) external view returns (address[] memory) { return dripManager.getActiveBalanceDrips(measure); } function getDrip( address measure, address dripToken ) external view returns ( uint256 dripRatePerSecond, uint128 exchangeRateMantissa, uint32 timestamp ) { BalanceDrip.State storage dripState = dripManager.getDrip(measure, dripToken); dripRatePerSecond = dripState.dripRatePerSecond; exchangeRateMantissa = dripState.exchangeRateMantissa; timestamp = dripState.timestamp; } }
pragma solidity >=0.6.0 <0.7.0; import "../prize-pool/compound/CompoundPrizePool.sol"; /* solium-disable security/no-block-members */ contract CompoundPrizePoolHarness is CompoundPrizePool { uint256 public currentTime; function setCurrentTime(uint256 _currentTime) external { currentTime = _currentTime; } function setTimelockBalance(uint256 _timelockBalance) external { timelockTotalSupply = _timelockBalance; } function _currentTime() internal override view returns (uint256) { return currentTime; } function supply(uint256 mintAmount) external { _supply(mintAmount); } function redeem(uint256 redeemAmount) external returns (uint256) { return _redeem(redeemAmount); } }
pragma solidity >=0.6.0 <0.7.0; import "./CompoundPrizePoolHarness.sol"; import "../external/openzeppelin/ProxyFactory.sol"; /// @title Compound Prize Pool Proxy Factory /// @notice Minimal proxy pattern for creating new Compound Prize Pools contract CompoundPrizePoolHarnessProxyFactory is ProxyFactory { /// @notice Contract template for deploying proxied Prize Pools CompoundPrizePoolHarness public instance; /// @notice Initializes the Factory with an instance of the Compound Prize Pool constructor () public { instance = new CompoundPrizePoolHarness(); } /// @notice Creates a new Compound Prize Pool as a proxy of the template instance /// @return A reference to the new proxied Compound Prize Pool function create() external returns (CompoundPrizePoolHarness) { return CompoundPrizePoolHarness(deployMinimal(address(instance), "")); } }
pragma solidity >=0.6.0 <0.7.0; pragma experimental ABIEncoderV2; import "../comptroller/Comptroller.sol"; /* solium-disable security/no-block-members */ contract ComptrollerHarness is Comptroller { uint256 internal time; function setCurrentTime(uint256 _time) external { time = _time; } function _currentTime() internal override view returns (uint256) { return time; } }
/** Copyright 2019 PoolTogether LLC This file is part of PoolTogether. PoolTogether is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation under version 3 of the License. PoolTogether is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with PoolTogether. If not, see <https://www.gnu.org/licenses/>. */ pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/ERC20.sol"; import "../external/pooltogether/FixedPoint.sol"; import "./ERC20Mintable.sol"; contract CTokenMock is ERC20UpgradeSafe { mapping(address => uint256) internal ownerTokenAmounts; ERC20Mintable public underlying; uint256 internal __supplyRatePerBlock; constructor ( ERC20Mintable _token, uint256 _supplyRatePerBlock ) public { require(address(_token) != address(0), "token is not defined"); underlying = _token; __supplyRatePerBlock = _supplyRatePerBlock; } function mint(uint256 amount) external returns (uint) { uint256 newCTokens; if (totalSupply() == 0) { newCTokens = amount; } else { // they need to hold the same assets as tokens. // Need to calculate the current exchange rate uint256 fractionOfCredit = FixedPoint.calculateMantissa(amount, underlying.balanceOf(address(this))); newCTokens = FixedPoint.multiplyUintByMantissa(totalSupply(), fractionOfCredit); } _mint(msg.sender, newCTokens); require(underlying.transferFrom(msg.sender, address(this), amount), "could not transfer tokens"); return 0; } function getCash() external view returns (uint) { return underlying.balanceOf(address(this)); } function redeemUnderlying(uint256 requestedAmount) external returns (uint) { uint256 cTokens = cTokenValueOf(requestedAmount); _burn(msg.sender, cTokens); require(underlying.transfer(msg.sender, requestedAmount), "could not transfer tokens"); } function accrue() external { uint256 newTokens = (underlying.balanceOf(address(this)) * 120) / 100; underlying.mint(address(this), newTokens); } function accrueCustom(uint256 amount) external { underlying.mint(address(this), amount); } function burn(uint256 amount) external { underlying.burn(address(this), amount); } function cTokenValueOf(uint256 tokens) public view returns (uint256) { return FixedPoint.divideUintByMantissa(tokens, exchangeRateCurrent()); } function balanceOfUnderlying(address account) public view returns (uint) { return FixedPoint.multiplyUintByMantissa(balanceOf(account), exchangeRateCurrent()); } function exchangeRateCurrent() public view returns (uint256) { if (totalSupply() == 0) { return FixedPoint.SCALE; } else { return FixedPoint.calculateMantissa(underlying.balanceOf(address(this)), totalSupply()); } } function supplyRatePerBlock() external view returns (uint) { return __supplyRatePerBlock; } function setSupplyRateMantissa(uint256 _supplyRatePerBlock) external { __supplyRatePerBlock = _supplyRatePerBlock; } }
pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/ERC20.sol"; /** * @dev Extension of {ERC20} that adds a set of accounts with the {MinterRole}, * which have permission to mint (create) new tokens as they see fit. * * At construction, the deployer of the contract is the only minter. */ contract ERC20Mintable is ERC20UpgradeSafe { constructor(string memory _name, string memory _symbol) public { __ERC20_init(_name, _symbol); } /** * @dev See {ERC20-_mint}. * * Requirements: * * - the caller must have the {MinterRole}. */ function mint(address account, uint256 amount) public returns (bool) { _mint(account, amount); return true; } function burn(address account, uint256 amount) public returns (bool) { _burn(account, amount); return true; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.6.12; import "@openzeppelin/contracts/math/SafeMath.sol"; import "@openzeppelin/contracts/utils/Address.sol"; import "../external/maker/DaiInterface.sol"; contract Dai is DaiInterface { using SafeMath for uint256; using Address for address; mapping (address => uint256) private _balances; mapping (address => mapping (address => uint256)) private _allowances; uint256 private _totalSupply; string private _name; string private _symbol; uint8 private _decimals; /** * @dev Sets the values for {name} and {symbol}, initializes {decimals} with * a default value of 18. * * To select a different value for {decimals}, use {_setupDecimals}. * * All three of these values are immutable: they can only be set once during * construction. */ constructor (uint256 chainId_) public { string memory version = "1"; _name = "Dai Stablecoin"; _symbol = "DAI"; _decimals = 18; DOMAIN_SEPARATOR = keccak256( abi.encode( keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"), keccak256(bytes(_name)), keccak256(bytes(version)), chainId_, address(this) ) ); } /** * @dev Returns the name of the token. */ function name() public view returns (string memory) { return _name; } /** * @dev Returns the symbol of the token, usually a shorter version of the * name. */ function symbol() public view returns (string memory) { return _symbol; } /** * @dev Returns the number of decimals used to get its user representation. * For example, if `decimals` equals `2`, a balance of `505` tokens should * be displayed to a user as `5,05` (`505 / 10 ** 2`). * * Tokens usually opt for a value of 18, imitating the relationship between * Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is * called. * * NOTE: This information is only used for _display_ purposes: it in * no way affects any of the arithmetic of the contract, including * {IERC20-balanceOf} and {IERC20-transfer}. */ function decimals() public view returns (uint8) { return _decimals; } /** * @dev See {IERC20-totalSupply}. */ function totalSupply() public view override returns (uint256) { return _totalSupply; } /** * @dev See {IERC20-balanceOf}. */ function balanceOf(address account) public view override returns (uint256) { return _balances[account]; } /** * @dev See {IERC20-transfer}. * * Requirements: * * - `recipient` cannot be the zero address. * - the caller must have a balance of at least `amount`. */ function transfer(address recipient, uint256 amount) public virtual override returns (bool) { _transfer(msg.sender, recipient, amount); return true; } /** * @dev See {IERC20-allowance}. */ function allowance(address owner, address spender) public view virtual override returns (uint256) { return _allowances[owner][spender]; } /** * @dev See {IERC20-approve}. * * Requirements: * * - `spender` cannot be the zero address. */ function approve(address spender, uint256 amount) public virtual override returns (bool) { _approve(msg.sender, spender, amount); return true; } /** * @dev See {IERC20-transferFrom}. * * Emits an {Approval} event indicating the updated allowance. This is not * required by the EIP. See the note at the beginning of {ERC20}; * * Requirements: * - `sender` and `recipient` cannot be the zero address. * - `sender` must have a balance of at least `amount`. * - the caller must have allowance for ``sender``'s tokens of at least * `amount`. */ function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) { _transfer(sender, recipient, amount); _approve(sender, msg.sender, _allowances[sender][msg.sender].sub(amount, "ERC20: transfer amount exceeds allowance")); return true; } /** * @dev Atomically increases the allowance granted to `spender` by the caller. * * This is an alternative to {approve} that can be used as a mitigation for * problems described in {IERC20-approve}. * * Emits an {Approval} event indicating the updated allowance. * * Requirements: * * - `spender` cannot be the zero address. */ function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) { _approve(msg.sender, spender, _allowances[msg.sender][spender].add(addedValue)); return true; } /** * @dev Atomically decreases the allowance granted to `spender` by the caller. * * This is an alternative to {approve} that can be used as a mitigation for * problems described in {IERC20-approve}. * * Emits an {Approval} event indicating the updated allowance. * * Requirements: * * - `spender` cannot be the zero address. * - `spender` must have allowance for the caller of at least * `subtractedValue`. */ function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) { _approve(msg.sender, spender, _allowances[msg.sender][spender].sub(subtractedValue, "ERC20: decreased allowance below zero")); return true; } /** * @dev Moves tokens `amount` from `sender` to `recipient`. * * This is internal function is equivalent to {transfer}, and can be used to * e.g. implement automatic token fees, slashing mechanisms, etc. * * Emits a {Transfer} event. * * Requirements: * * - `sender` cannot be the zero address. * - `recipient` cannot be the zero address. * - `sender` must have a balance of at least `amount`. */ function _transfer(address sender, address recipient, uint256 amount) internal virtual { require(sender != address(0), "ERC20: transfer from the zero address"); require(recipient != address(0), "ERC20: transfer to the zero address"); _beforeTokenTransfer(sender, recipient, amount); _balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance"); _balances[recipient] = _balances[recipient].add(amount); emit Transfer(sender, recipient, amount); } /** @dev Creates `amount` tokens and assigns them to `account`, increasing * the total supply. * * Emits a {Transfer} event with `from` set to the zero address. * * Requirements * * - `to` cannot be the zero address. */ function _mint(address account, uint256 amount) internal virtual { require(account != address(0), "ERC20: mint to the zero address"); _beforeTokenTransfer(address(0), account, amount); _totalSupply = _totalSupply.add(amount); _balances[account] = _balances[account].add(amount); emit Transfer(address(0), account, amount); } /** * @dev Destroys `amount` tokens from `account`, reducing the * total supply. * * Emits a {Transfer} event with `to` set to the zero address. * * Requirements * * - `account` cannot be the zero address. * - `account` must have at least `amount` tokens. */ function _burn(address account, uint256 amount) internal virtual { require(account != address(0), "ERC20: burn from the zero address"); _beforeTokenTransfer(account, address(0), amount); _balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance"); _totalSupply = _totalSupply.sub(amount); emit Transfer(account, address(0), amount); } /** * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens. * * This internal function is equivalent to `approve`, and can be used to * e.g. set automatic allowances for certain subsystems, etc. * * Emits an {Approval} event. * * Requirements: * * - `owner` cannot be the zero address. * - `spender` cannot be the zero address. */ function _approve(address owner, address spender, uint256 amount) internal virtual { require(owner != address(0), "ERC20: approve from the zero address"); require(spender != address(0), "ERC20: approve to the zero address"); _allowances[owner][spender] = amount; emit Approval(owner, spender, amount); } /** * @dev Sets {decimals} to a value other than the default one of 18. * * WARNING: This function should only be called from the constructor. Most * applications that interact with token contracts will not expect * {decimals} to ever change, and may work incorrectly if it does. */ function _setupDecimals(uint8 decimals_) internal { _decimals = decimals_; } /** * @dev Hook that is called before any transfer of tokens. This includes * minting and burning. * * Calling conditions: * * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens * will be to transferred to `to`. * - when `from` is zero, `amount` tokens will be minted for `to`. * - when `to` is zero, `amount` of ``from``'s tokens will be burned. * - `from` and `to` are never both zero. * * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. */ function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { } mapping (address => uint) public nonces; // --- EIP712 niceties --- bytes32 public DOMAIN_SEPARATOR; // bytes32 public constant PERMIT_TYPEHASH = keccak256("Permit(address holder,address spender,uint256 nonce,uint256 expiry,bool allowed)"); bytes32 public constant PERMIT_TYPEHASH = 0xea2aa0a1be11a07ed86d755c93467f4f82362b452371d1ba94d1715123511acb; // --- Approve by signature --- function permit( address holder, address spender, uint256 nonce, uint256 expiry, bool allowed, uint8 v, bytes32 r, bytes32 s) external override { bytes32 digest = keccak256( abi.encodePacked( "\x19\x01", DOMAIN_SEPARATOR, keccak256( abi.encode( PERMIT_TYPEHASH, holder, spender, nonce, expiry, allowed ) ) ) ); require(holder != address(0), "Dai/invalid-address-0"); require(holder == ecrecover(digest, v, r, s), "Dai/invalid-permit"); require(expiry == 0 || now <= expiry, "Dai/permit-expired"); require(nonce == nonces[holder]++, "Dai/invalid-nonce"); uint wad = allowed ? uint(-1) : 0; _allowances[holder][spender] = wad; emit Approval(holder, spender, wad); } function mint(address to, uint256 amount) external { _mint(to, amount); } }
// SPDX-License-Identifier: MIT pragma solidity ^0.6.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. */ 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. */ function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { 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. */ function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b != 0, errorMessage); return a % b; } }
// SPDX-License-Identifier: MIT pragma solidity ^0.6.2; /** * @dev Collection of functions related to the address type */ library Address { /** * @dev Returns true if `account` is a contract. * * [IMPORTANT] * ==== * It is unsafe to assume that an address for which this function returns * false is an externally-owned account (EOA) and not a contract. * * Among others, `isContract` will return false for the following * types of addresses: * * - an externally-owned account * - a contract in construction * - an address where a contract will be created * - an address where a contract lived, but was destroyed * ==== */ function isContract(address account) internal view returns (bool) { // This method relies in extcodesize, which returns 0 for contracts in // construction, since the code is only stored at the end of the // constructor execution. uint256 size; // solhint-disable-next-line no-inline-assembly assembly { size := extcodesize(account) } return size > 0; } /** * @dev Replacement for Solidity's `transfer`: sends `amount` wei to * `recipient`, forwarding all available gas and reverting on errors. * * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost * of certain opcodes, possibly making contracts go over the 2300 gas limit * imposed by `transfer`, making them unable to receive funds via * `transfer`. {sendValue} removes this limitation. * * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more]. * * IMPORTANT: because control is transferred to `recipient`, care must be * taken to not create reentrancy vulnerabilities. Consider using * {ReentrancyGuard} or the * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern]. */ function sendValue(address payable recipient, uint256 amount) internal { require(address(this).balance >= amount, "Address: insufficient balance"); // solhint-disable-next-line avoid-low-level-calls, avoid-call-value (bool success, ) = recipient.call{ value: amount }(""); require(success, "Address: unable to send value, recipient may have reverted"); } /** * @dev Performs a Solidity function call using a low level `call`. A * plain`call` is an unsafe replacement for a function call: use this * function instead. * * If `target` reverts with a revert reason, it is bubbled up by this * function (like regular Solidity function calls). * * Returns the raw returned data. To convert to the expected return value, * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`]. * * Requirements: * * - `target` must be a contract. * - calling `target` with `data` must not revert. * * _Available since v3.1._ */ function functionCall(address target, bytes memory data) internal returns (bytes memory) { return functionCall(target, data, "Address: low-level call failed"); } /** * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with * `errorMessage` as a fallback revert reason when `target` reverts. * * _Available since v3.1._ */ function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) { return _functionCallWithValue(target, data, 0, errorMessage); } /** * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], * but also transferring `value` wei to `target`. * * Requirements: * * - the calling contract must have an ETH balance of at least `value`. * - the called Solidity function must be `payable`. * * _Available since v3.1._ */ function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) { return functionCallWithValue(target, data, value, "Address: low-level call with value failed"); } /** * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but * with `errorMessage` as a fallback revert reason when `target` reverts. * * _Available since v3.1._ */ function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) { require(address(this).balance >= value, "Address: insufficient balance for call"); return _functionCallWithValue(target, data, value, errorMessage); } function _functionCallWithValue(address target, bytes memory data, uint256 weiValue, string memory errorMessage) private returns (bytes memory) { require(isContract(target), "Address: call to non-contract"); // solhint-disable-next-line avoid-low-level-calls (bool success, bytes memory returndata) = target.call{ value: weiValue }(data); if (success) { return returndata; } else { // Look for revert reason and bubble it up if present if (returndata.length > 0) { // The easiest way to bubble the revert reason is using memory via assembly // solhint-disable-next-line no-inline-assembly assembly { let returndata_size := mload(returndata) revert(add(32, returndata), returndata_size) } } else { revert(errorMessage); } } } }
pragma solidity >=0.6.0 <0.7.0; /* solium-disable security/no-inline-assembly */ contract DoppelgangerWithExec { struct MockCall { bool initialized; bool reverts; bytes returnValue; } mapping(bytes32 => MockCall) mockConfig; fallback() external payable { MockCall storage mockCall = __internal__getMockCall(); if (mockCall.reverts == true) { __internal__mockRevert(); return; } __internal__mockReturn(mockCall.returnValue); } function __waffle__mockReverts(bytes memory data) public { mockConfig[keccak256(data)] = MockCall({ initialized: true, reverts: true, returnValue: "" }); } function __waffle__mockReturns(bytes memory data, bytes memory value) public { mockConfig[keccak256(data)] = MockCall({ initialized: true, reverts: false, returnValue: value }); } function __waffle__call(address target, bytes calldata data) external returns (bytes memory) { (bool succeeded, bytes memory returnValue) = target.call(data); require(succeeded, string(returnValue)); return returnValue; } function __waffle__staticcall(address target, bytes calldata data) external view returns (bytes memory) { (bool succeeded, bytes memory returnValue) = target.staticcall(data); require(succeeded, string(returnValue)); return returnValue; } function __internal__getMockCall() view private returns (MockCall storage mockCall) { mockCall = mockConfig[keccak256(msg.data)]; if (mockCall.initialized == true) { // Mock method with specified arguments return mockCall; } mockCall = mockConfig[keccak256(abi.encodePacked(msg.sig))]; if (mockCall.initialized == true) { // Mock method with any arguments return mockCall; } revert("Mock on the method is not initialized"); } function __internal__mockReturn(bytes memory ret) pure private { assembly { return (add(ret, 0x20), mload(ret)) } } function __internal__mockRevert() pure private { revert("Mock revert"); } }
pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/introspection/IERC1820Implementer.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC777/IERC777Recipient.sol"; import "../Constants.sol"; contract ERC1820ImplementerMock is IERC1820Implementer, IERC777Recipient { constructor () public { Constants.REGISTRY.setInterfaceImplementer(address(this), Constants.TOKENS_RECIPIENT_INTERFACE_HASH, address(this)); } function canImplementInterfaceForAddress(bytes32, address) external view virtual override returns(bytes32) { return Constants.ACCEPT_MAGIC; } function tokensReceived( address operator, address from, address to, uint256 amount, bytes calldata userData, bytes calldata operatorData ) external override { } }
pragma solidity ^0.6.0; /** * @dev Interface for an ERC1820 implementer, as defined in the * https://eips.ethereum.org/EIPS/eip-1820#interface-implementation-erc1820implementerinterface[EIP]. * Used by contracts that will be registered as implementers in the * {IERC1820Registry}. */ interface IERC1820Implementer { /** * @dev Returns a special value (`ERC1820_ACCEPT_MAGIC`) if this contract * implements `interfaceHash` for `account`. * * See {IERC1820Registry-setInterfaceImplementer}. */ function canImplementInterfaceForAddress(bytes32 interfaceHash, address account) external view returns (bytes32); }
pragma solidity ^0.6.0; /** * @dev Interface of the ERC777TokensRecipient standard as defined in the EIP. * * Accounts can be notified of {IERC777} tokens being sent to them by having a * contract implement this interface (contract holders can be their own * implementer) and registering it on the * https://eips.ethereum.org/EIPS/eip-1820[ERC1820 global registry]. * * See {IERC1820Registry} and {ERC1820Implementer}. */ interface IERC777Recipient { /** * @dev Called by an {IERC777} token contract whenever tokens are being * moved or created into a registered account (`to`). The type of operation * is conveyed by `from` being the zero address or not. * * This call occurs _after_ the token contract's state is updated, so * {IERC777-balanceOf}, etc., can be used to query the post-operation state. * * This function may revert to prevent the operation from being executed. */ function tokensReceived( address operator, address from, address to, uint256 amount, bytes calldata userData, bytes calldata operatorData ) external; }
pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC721/ERC721.sol"; /** * @dev Extension of {ERC721} for Minting/Burning */ contract ERC721Mintable is ERC721UpgradeSafe { constructor () public { __ERC721_init("ERC 721", "NFT"); } /** * @dev See {ERC721-_mint}. */ function mint(address to, uint256 tokenId) public { _mint(to, tokenId); } /** * @dev See {ERC721-_burn}. */ function burn(uint256 tokenId) public { _burn(tokenId); } }
pragma solidity ^0.6.0; import "../../GSN/Context.sol"; import "./IERC721.sol"; import "./IERC721Metadata.sol"; import "./IERC721Enumerable.sol"; import "./IERC721Receiver.sol"; import "../../introspection/ERC165.sol"; import "../../math/SafeMath.sol"; import "../../utils/Address.sol"; import "../../utils/EnumerableSet.sol"; import "../../utils/EnumerableMap.sol"; import "../../utils/Strings.sol"; import "../../Initializable.sol"; /** * @title ERC721 Non-Fungible Token Standard basic implementation * @dev see https://eips.ethereum.org/EIPS/eip-721 */ contract ERC721UpgradeSafe is Initializable, ContextUpgradeSafe, ERC165UpgradeSafe, IERC721, IERC721Metadata, IERC721Enumerable { using SafeMath for uint256; using Address for address; using EnumerableSet for EnumerableSet.UintSet; using EnumerableMap for EnumerableMap.UintToAddressMap; using Strings for uint256; // Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))` // which can be also obtained as `IERC721Receiver(0).onERC721Received.selector` bytes4 private constant _ERC721_RECEIVED = 0x150b7a02; // Mapping from holder address to their (enumerable) set of owned tokens mapping (address => EnumerableSet.UintSet) private _holderTokens; // Enumerable mapping from token ids to their owners EnumerableMap.UintToAddressMap private _tokenOwners; // Mapping from token ID to approved address mapping (uint256 => address) private _tokenApprovals; // Mapping from owner to operator approvals mapping (address => mapping (address => bool)) private _operatorApprovals; // Token name string private _name; // Token symbol string private _symbol; // Optional mapping for token URIs mapping(uint256 => string) private _tokenURIs; // Base URI string private _baseURI; /* * bytes4(keccak256('balanceOf(address)')) == 0x70a08231 * bytes4(keccak256('ownerOf(uint256)')) == 0x6352211e * bytes4(keccak256('approve(address,uint256)')) == 0x095ea7b3 * bytes4(keccak256('getApproved(uint256)')) == 0x081812fc * bytes4(keccak256('setApprovalForAll(address,bool)')) == 0xa22cb465 * bytes4(keccak256('isApprovedForAll(address,address)')) == 0xe985e9c5 * bytes4(keccak256('transferFrom(address,address,uint256)')) == 0x23b872dd * bytes4(keccak256('safeTransferFrom(address,address,uint256)')) == 0x42842e0e * bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)')) == 0xb88d4fde * * => 0x70a08231 ^ 0x6352211e ^ 0x095ea7b3 ^ 0x081812fc ^ * 0xa22cb465 ^ 0xe985e9c ^ 0x23b872dd ^ 0x42842e0e ^ 0xb88d4fde == 0x80ac58cd */ bytes4 private constant _INTERFACE_ID_ERC721 = 0x80ac58cd; /* * bytes4(keccak256('name()')) == 0x06fdde03 * bytes4(keccak256('symbol()')) == 0x95d89b41 * bytes4(keccak256('tokenURI(uint256)')) == 0xc87b56dd * * => 0x06fdde03 ^ 0x95d89b41 ^ 0xc87b56dd == 0x5b5e139f */ bytes4 private constant _INTERFACE_ID_ERC721_METADATA = 0x5b5e139f; /* * bytes4(keccak256('totalSupply()')) == 0x18160ddd * bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) == 0x2f745c59 * bytes4(keccak256('tokenByIndex(uint256)')) == 0x4f6ccce7 * * => 0x18160ddd ^ 0x2f745c59 ^ 0x4f6ccce7 == 0x780e9d63 */ bytes4 private constant _INTERFACE_ID_ERC721_ENUMERABLE = 0x780e9d63; function __ERC721_init(string memory name, string memory symbol) internal initializer { __Context_init_unchained(); __ERC165_init_unchained(); __ERC721_init_unchained(name, symbol); } function __ERC721_init_unchained(string memory name, string memory symbol) internal initializer { _name = name; _symbol = symbol; // register the supported interfaces to conform to ERC721 via ERC165 _registerInterface(_INTERFACE_ID_ERC721); _registerInterface(_INTERFACE_ID_ERC721_METADATA); _registerInterface(_INTERFACE_ID_ERC721_ENUMERABLE); } /** * @dev Gets the balance of the specified address. * @param owner address to query the balance of * @return uint256 representing the amount owned by the passed address */ function balanceOf(address owner) public view override returns (uint256) { require(owner != address(0), "ERC721: balance query for the zero address"); return _holderTokens[owner].length(); } /** * @dev Gets the owner of the specified token ID. * @param tokenId uint256 ID of the token to query the owner of * @return address currently marked as the owner of the given token ID */ function ownerOf(uint256 tokenId) public view override returns (address) { return _tokenOwners.get(tokenId, "ERC721: owner query for nonexistent token"); } /** * @dev Gets the token name. * @return string representing the token name */ function name() public view override returns (string memory) { return _name; } /** * @dev Gets the token symbol. * @return string representing the token symbol */ function symbol() public view override returns (string memory) { return _symbol; } /** * @dev Returns the URI for a given token ID. May return an empty string. * * If a base URI is set (via {_setBaseURI}), it is added as a prefix to the * token's own URI (via {_setTokenURI}). * * If there is a base URI but no token URI, the token's ID will be used as * its URI when appending it to the base URI. This pattern for autogenerated * token URIs can lead to large gas savings. * * .Examples * |=== * |`_setBaseURI()` |`_setTokenURI()` |`tokenURI()` * | "" * | "" * | "" * | "" * | "token.uri/123" * | "token.uri/123" * | "token.uri/" * | "123" * | "token.uri/123" * | "token.uri/" * | "" * | "token.uri/<tokenId>" * |=== * * Requirements: * * - `tokenId` must exist. */ function tokenURI(uint256 tokenId) public view override returns (string memory) { require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token"); string memory _tokenURI = _tokenURIs[tokenId]; // If there is no base URI, return the token URI. if (bytes(_baseURI).length == 0) { return _tokenURI; } // If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked). if (bytes(_tokenURI).length > 0) { return string(abi.encodePacked(_baseURI, _tokenURI)); } // If there is a baseURI but no tokenURI, concatenate the tokenID to the baseURI. return string(abi.encodePacked(_baseURI, tokenId.toString())); } /** * @dev Returns the base URI set via {_setBaseURI}. This will be * automatically added as a prefix in {tokenURI} to each token's URI, or * to the token ID if no specific URI is set for that token ID. */ function baseURI() public view returns (string memory) { return _baseURI; } /** * @dev Gets the token ID at a given index of the tokens list of the requested owner. * @param owner address owning the tokens list to be accessed * @param index uint256 representing the index to be accessed of the requested tokens list * @return uint256 token ID at the given index of the tokens list owned by the requested address */ function tokenOfOwnerByIndex(address owner, uint256 index) public view override returns (uint256) { return _holderTokens[owner].at(index); } /** * @dev Gets the total amount of tokens stored by the contract. * @return uint256 representing the total amount of tokens */ function totalSupply() public view override returns (uint256) { // _tokenOwners are indexed by tokenIds, so .length() returns the number of tokenIds return _tokenOwners.length(); } /** * @dev Gets the token ID at a given index of all the tokens in this contract * Reverts if the index is greater or equal to the total number of tokens. * @param index uint256 representing the index to be accessed of the tokens list * @return uint256 token ID at the given index of the tokens list */ function tokenByIndex(uint256 index) public view override returns (uint256) { (uint256 tokenId, ) = _tokenOwners.at(index); return tokenId; } /** * @dev Approves another address to transfer the given token ID * The zero address indicates there is no approved address. * There can only be one approved address per token at a given time. * Can only be called by the token owner or an approved operator. * @param to address to be approved for the given token ID * @param tokenId uint256 ID of the token to be approved */ function approve(address to, uint256 tokenId) public virtual override { address owner = ownerOf(tokenId); require(to != owner, "ERC721: approval to current owner"); require(_msgSender() == owner || isApprovedForAll(owner, _msgSender()), "ERC721: approve caller is not owner nor approved for all" ); _approve(to, tokenId); } /** * @dev Gets the approved address for a token ID, or zero if no address set * Reverts if the token ID does not exist. * @param tokenId uint256 ID of the token to query the approval of * @return address currently approved for the given token ID */ function getApproved(uint256 tokenId) public view override returns (address) { require(_exists(tokenId), "ERC721: approved query for nonexistent token"); return _tokenApprovals[tokenId]; } /** * @dev Sets or unsets the approval of a given operator * An operator is allowed to transfer all tokens of the sender on their behalf. * @param operator operator address to set the approval * @param approved representing the status of the approval to be set */ function setApprovalForAll(address operator, bool approved) public virtual override { require(operator != _msgSender(), "ERC721: approve to caller"); _operatorApprovals[_msgSender()][operator] = approved; emit ApprovalForAll(_msgSender(), operator, approved); } /** * @dev Tells whether an operator is approved by a given owner. * @param owner owner address which you want to query the approval of * @param operator operator address which you want to query the approval of * @return bool whether the given operator is approved by the given owner */ function isApprovedForAll(address owner, address operator) public view override returns (bool) { return _operatorApprovals[owner][operator]; } /** * @dev Transfers the ownership of a given token ID to another address. * Usage of this method is discouraged, use {safeTransferFrom} whenever possible. * Requires the msg.sender to be the owner, approved, or operator. * @param from current owner of the token * @param to address to receive the ownership of the given token ID * @param tokenId uint256 ID of the token to be transferred */ function transferFrom(address from, address to, uint256 tokenId) public virtual override { //solhint-disable-next-line max-line-length require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved"); _transfer(from, to, tokenId); } /** * @dev Safely transfers the ownership of a given token ID to another address * If the target address is a contract, it must implement {IERC721Receiver-onERC721Received}, * which is called upon a safe transfer, and return the magic value * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, * the transfer is reverted. * Requires the msg.sender to be the owner, approved, or operator * @param from current owner of the token * @param to address to receive the ownership of the given token ID * @param tokenId uint256 ID of the token to be transferred */ function safeTransferFrom(address from, address to, uint256 tokenId) public virtual override { safeTransferFrom(from, to, tokenId, ""); } /** * @dev Safely transfers the ownership of a given token ID to another address * If the target address is a contract, it must implement {IERC721Receiver-onERC721Received}, * which is called upon a safe transfer, and return the magic value * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, * the transfer is reverted. * Requires the _msgSender() to be the owner, approved, or operator * @param from current owner of the token * @param to address to receive the ownership of the given token ID * @param tokenId uint256 ID of the token to be transferred * @param _data bytes data to send along with a safe transfer check */ function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public virtual override { require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved"); _safeTransfer(from, to, tokenId, _data); } /** * @dev Safely transfers the ownership of a given token ID to another address * If the target address is a contract, it must implement `onERC721Received`, * which is called upon a safe transfer, and return the magic value * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, * the transfer is reverted. * Requires the msg.sender to be the owner, approved, or operator * @param from current owner of the token * @param to address to receive the ownership of the given token ID * @param tokenId uint256 ID of the token to be transferred * @param _data bytes data to send along with a safe transfer check */ function _safeTransfer(address from, address to, uint256 tokenId, bytes memory _data) internal virtual { _transfer(from, to, tokenId); require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer"); } /** * @dev Returns whether the specified token exists. * @param tokenId uint256 ID of the token to query the existence of * @return bool whether the token exists */ function _exists(uint256 tokenId) internal view returns (bool) { return _tokenOwners.contains(tokenId); } /** * @dev Returns whether the given spender can transfer a given token ID. * @param spender address of the spender to query * @param tokenId uint256 ID of the token to be transferred * @return bool whether the msg.sender is approved for the given token ID, * is an operator of the owner, or is the owner of the token */ function _isApprovedOrOwner(address spender, uint256 tokenId) internal view returns (bool) { require(_exists(tokenId), "ERC721: operator query for nonexistent token"); address owner = ownerOf(tokenId); return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender)); } /** * @dev Internal function to safely mint a new token. * Reverts if the given token ID already exists. * If the target address is a contract, it must implement `onERC721Received`, * which is called upon a safe transfer, and return the magic value * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, * the transfer is reverted. * @param to The address that will own the minted token * @param tokenId uint256 ID of the token to be minted */ function _safeMint(address to, uint256 tokenId) internal virtual { _safeMint(to, tokenId, ""); } /** * @dev Internal function to safely mint a new token. * Reverts if the given token ID already exists. * If the target address is a contract, it must implement `onERC721Received`, * which is called upon a safe transfer, and return the magic value * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, * the transfer is reverted. * @param to The address that will own the minted token * @param tokenId uint256 ID of the token to be minted * @param _data bytes data to send along with a safe transfer check */ function _safeMint(address to, uint256 tokenId, bytes memory _data) internal virtual { _mint(to, tokenId); require(_checkOnERC721Received(address(0), to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer"); } /** * @dev Internal function to mint a new token. * Reverts if the given token ID already exists. * @param to The address that will own the minted token * @param tokenId uint256 ID of the token to be minted */ function _mint(address to, uint256 tokenId) internal virtual { require(to != address(0), "ERC721: mint to the zero address"); require(!_exists(tokenId), "ERC721: token already minted"); _beforeTokenTransfer(address(0), to, tokenId); _holderTokens[to].add(tokenId); _tokenOwners.set(tokenId, to); emit Transfer(address(0), to, tokenId); } /** * @dev Internal function to burn a specific token. * Reverts if the token does not exist. * @param tokenId uint256 ID of the token being burned */ function _burn(uint256 tokenId) internal virtual { address owner = ownerOf(tokenId); _beforeTokenTransfer(owner, address(0), tokenId); // Clear approvals _approve(address(0), tokenId); // Clear metadata (if any) if (bytes(_tokenURIs[tokenId]).length != 0) { delete _tokenURIs[tokenId]; } _holderTokens[owner].remove(tokenId); _tokenOwners.remove(tokenId); emit Transfer(owner, address(0), tokenId); } /** * @dev Internal function to transfer ownership of a given token ID to another address. * As opposed to {transferFrom}, this imposes no restrictions on msg.sender. * @param from current owner of the token * @param to address to receive the ownership of the given token ID * @param tokenId uint256 ID of the token to be transferred */ function _transfer(address from, address to, uint256 tokenId) internal virtual { require(ownerOf(tokenId) == from, "ERC721: transfer of token that is not own"); require(to != address(0), "ERC721: transfer to the zero address"); _beforeTokenTransfer(from, to, tokenId); // Clear approvals from the previous owner _approve(address(0), tokenId); _holderTokens[from].remove(tokenId); _holderTokens[to].add(tokenId); _tokenOwners.set(tokenId, to); emit Transfer(from, to, tokenId); } /** * @dev Internal function to set the token URI for a given token. * * Reverts if the token ID does not exist. * * TIP: If all token IDs share a prefix (for example, if your URIs look like * `https://api.myproject.com/token/<id>`), use {_setBaseURI} to store * it and save gas. */ function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual { require(_exists(tokenId), "ERC721Metadata: URI set of nonexistent token"); _tokenURIs[tokenId] = _tokenURI; } /** * @dev Internal function to set the base URI for all token IDs. It is * automatically added as a prefix to the value returned in {tokenURI}, * or to the token ID if {tokenURI} is empty. */ function _setBaseURI(string memory baseURI_) internal virtual { _baseURI = baseURI_; } /** * @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address. * The call is not executed if the target address is not a contract. * * @param from address representing the previous owner of the given token ID * @param to target address that will receive the tokens * @param tokenId uint256 ID of the token to be transferred * @param _data bytes optional data to send along with the call * @return bool whether the call correctly returned the expected magic value */ function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data) private returns (bool) { if (!to.isContract()) { return true; } // solhint-disable-next-line avoid-low-level-calls (bool success, bytes memory returndata) = to.call(abi.encodeWithSelector( IERC721Receiver(to).onERC721Received.selector, _msgSender(), from, tokenId, _data )); if (!success) { if (returndata.length > 0) { // solhint-disable-next-line no-inline-assembly assembly { let returndata_size := mload(returndata) revert(add(32, returndata), returndata_size) } } else { revert("ERC721: transfer to non ERC721Receiver implementer"); } } else { bytes4 retval = abi.decode(returndata, (bytes4)); return (retval == _ERC721_RECEIVED); } } function _approve(address to, uint256 tokenId) private { _tokenApprovals[tokenId] = to; emit Approval(ownerOf(tokenId), to, tokenId); } /** * @dev Hook that is called before any token transfer. This includes minting * and burning. * * Calling conditions: * * - when `from` and `to` are both non-zero, ``from``'s `tokenId` will be * transferred to `to`. * - when `from` is zero, `tokenId` will be minted for `to`. * - when `to` is zero, ``from``'s `tokenId` will be burned. * - `from` and `to` are never both zero. * * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. */ function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual { } uint256[41] private __gap; }
pragma solidity ^0.6.2; import "./IERC721.sol"; /** * @title ERC-721 Non-Fungible Token Standard, optional metadata extension * @dev See https://eips.ethereum.org/EIPS/eip-721 */ interface IERC721Metadata is IERC721 { function name() external view returns (string memory); function symbol() external view returns (string memory); function tokenURI(uint256 tokenId) external view returns (string memory); }
pragma solidity ^0.6.2; import "./IERC721.sol"; /** * @title ERC-721 Non-Fungible Token Standard, optional enumeration extension * @dev See https://eips.ethereum.org/EIPS/eip-721 */ interface IERC721Enumerable is IERC721 { function totalSupply() external view returns (uint256); function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256 tokenId); function tokenByIndex(uint256 index) external view returns (uint256); }
pragma solidity ^0.6.0; /** * @title ERC721 token receiver interface * @dev Interface for any contract that wants to support safeTransfers * from ERC721 asset contracts. */ interface IERC721Receiver { /** * @notice Handle the receipt of an NFT * @dev The ERC721 smart contract calls this function on the recipient * after a {IERC721-safeTransferFrom}. This function MUST return the function selector, * otherwise the caller will revert the transaction. The selector to be * returned can be obtained as `this.onERC721Received.selector`. This * function MAY throw to revert and reject the transfer. * Note: the ERC721 contract address is always the message sender. * @param operator The address which called `safeTransferFrom` function * @param from The address which previously owned the token * @param tokenId The NFT identifier which is being transferred * @param data Additional data with no specified format * @return bytes4 `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))` */ function onERC721Received(address operator, address from, uint256 tokenId, bytes calldata data) external returns (bytes4); }
pragma solidity ^0.6.0; import "./IERC165.sol"; import "../Initializable.sol"; /** * @dev Implementation of the {IERC165} interface. * * Contracts may inherit from this and call {_registerInterface} to declare * their support of an interface. */ contract ERC165UpgradeSafe is Initializable, IERC165 { /* * bytes4(keccak256('supportsInterface(bytes4)')) == 0x01ffc9a7 */ bytes4 private constant _INTERFACE_ID_ERC165 = 0x01ffc9a7; /** * @dev Mapping of interface ids to whether or not it's supported. */ mapping(bytes4 => bool) private _supportedInterfaces; function __ERC165_init() internal initializer { __ERC165_init_unchained(); } function __ERC165_init_unchained() internal initializer { // Derived contracts need only register support for their own interfaces, // we register support for ERC165 itself here _registerInterface(_INTERFACE_ID_ERC165); } /** * @dev See {IERC165-supportsInterface}. * * Time complexity O(1), guaranteed to always use less than 30 000 gas. */ function supportsInterface(bytes4 interfaceId) public view override returns (bool) { return _supportedInterfaces[interfaceId]; } /** * @dev Registers the contract as an implementer of the interface defined by * `interfaceId`. Support of the actual ERC165 interface is automatic and * registering its interface id is not required. * * See {IERC165-supportsInterface}. * * Requirements: * * - `interfaceId` cannot be the ERC165 invalid interface (`0xffffffff`). */ function _registerInterface(bytes4 interfaceId) internal virtual { require(interfaceId != 0xffffffff, "ERC165: invalid interface id"); _supportedInterfaces[interfaceId] = true; } uint256[49] private __gap; }
pragma solidity ^0.6.0; /** * @dev Library for managing * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive * types. * * Sets have the following properties: * * - Elements are added, removed, and checked for existence in constant time * (O(1)). * - Elements are enumerated in O(n). No guarantees are made on the ordering. * * ``` * contract Example { * // Add the library methods * using EnumerableSet for EnumerableSet.AddressSet; * * // Declare a set state variable * EnumerableSet.AddressSet private mySet; * } * ``` * * As of v3.0.0, only sets of type `address` (`AddressSet`) and `uint256` * (`UintSet`) are supported. */ library EnumerableSet { // To implement this library for multiple types with as little code // repetition as possible, we write it in terms of a generic Set type with // bytes32 values. // The Set implementation uses private functions, and user-facing // implementations (such as AddressSet) are just wrappers around the // underlying Set. // This means that we can only create new EnumerableSets for types that fit // in bytes32. struct Set { // Storage of set values bytes32[] _values; // Position of the value in the `values` array, plus 1 because index 0 // means a value is not in the set. mapping (bytes32 => uint256) _indexes; } /** * @dev Add a value to a set. O(1). * * Returns true if the value was added to the set, that is if it was not * already present. */ function _add(Set storage set, bytes32 value) private returns (bool) { if (!_contains(set, value)) { set._values.push(value); // The value is stored at length-1, but we add 1 to all indexes // and use 0 as a sentinel value set._indexes[value] = set._values.length; return true; } else { return false; } } /** * @dev Removes a value from a set. O(1). * * Returns true if the value was removed from the set, that is if it was * present. */ function _remove(Set storage set, bytes32 value) private returns (bool) { // We read and store the value's index to prevent multiple reads from the same storage slot uint256 valueIndex = set._indexes[value]; if (valueIndex != 0) { // Equivalent to contains(set, value) // To delete an element from the _values array in O(1), we swap the element to delete with the last one in // the array, and then remove the last element (sometimes called as 'swap and pop'). // This modifies the order of the array, as noted in {at}. uint256 toDeleteIndex = valueIndex - 1; uint256 lastIndex = set._values.length - 1; // When the value to delete is the last one, the swap operation is unnecessary. However, since this occurs // so rarely, we still do the swap anyway to avoid the gas cost of adding an 'if' statement. bytes32 lastvalue = set._values[lastIndex]; // Move the last value to the index where the value to delete is set._values[toDeleteIndex] = lastvalue; // Update the index for the moved value set._indexes[lastvalue] = toDeleteIndex + 1; // All indexes are 1-based // Delete the slot where the moved value was stored set._values.pop(); // Delete the index for the deleted slot delete set._indexes[value]; return true; } else { return false; } } /** * @dev Returns true if the value is in the set. O(1). */ function _contains(Set storage set, bytes32 value) private view returns (bool) { return set._indexes[value] != 0; } /** * @dev Returns the number of values on the set. O(1). */ function _length(Set storage set) private view returns (uint256) { return set._values.length; } /** * @dev Returns the value stored at position `index` in the set. O(1). * * Note that there are no guarantees on the ordering of values inside the * array, and it may change when more values are added or removed. * * Requirements: * * - `index` must be strictly less than {length}. */ function _at(Set storage set, uint256 index) private view returns (bytes32) { require(set._values.length > index, "EnumerableSet: index out of bounds"); return set._values[index]; } // AddressSet struct AddressSet { Set _inner; } /** * @dev Add a value to a set. O(1). * * Returns true if the value was added to the set, that is if it was not * already present. */ function add(AddressSet storage set, address value) internal returns (bool) { return _add(set._inner, bytes32(uint256(value))); } /** * @dev Removes a value from a set. O(1). * * Returns true if the value was removed from the set, that is if it was * present. */ function remove(AddressSet storage set, address value) internal returns (bool) { return _remove(set._inner, bytes32(uint256(value))); } /** * @dev Returns true if the value is in the set. O(1). */ function contains(AddressSet storage set, address value) internal view returns (bool) { return _contains(set._inner, bytes32(uint256(value))); } /** * @dev Returns the number of values in the set. O(1). */ function length(AddressSet storage set) internal view returns (uint256) { return _length(set._inner); } /** * @dev Returns the value stored at position `index` in the set. O(1). * * Note that there are no guarantees on the ordering of values inside the * array, and it may change when more values are added or removed. * * Requirements: * * - `index` must be strictly less than {length}. */ function at(AddressSet storage set, uint256 index) internal view returns (address) { return address(uint256(_at(set._inner, index))); } // UintSet struct UintSet { Set _inner; } /** * @dev Add a value to a set. O(1). * * Returns true if the value was added to the set, that is if it was not * already present. */ function add(UintSet storage set, uint256 value) internal returns (bool) { return _add(set._inner, bytes32(value)); } /** * @dev Removes a value from a set. O(1). * * Returns true if the value was removed from the set, that is if it was * present. */ function remove(UintSet storage set, uint256 value) internal returns (bool) { return _remove(set._inner, bytes32(value)); } /** * @dev Returns true if the value is in the set. O(1). */ function contains(UintSet storage set, uint256 value) internal view returns (bool) { return _contains(set._inner, bytes32(value)); } /** * @dev Returns the number of values on the set. O(1). */ function length(UintSet storage set) internal view returns (uint256) { return _length(set._inner); } /** * @dev Returns the value stored at position `index` in the set. O(1). * * Note that there are no guarantees on the ordering of values inside the * array, and it may change when more values are added or removed. * * Requirements: * * - `index` must be strictly less than {length}. */ function at(UintSet storage set, uint256 index) internal view returns (uint256) { return uint256(_at(set._inner, index)); } }
pragma solidity ^0.6.0; /** * @dev Library for managing an enumerable variant of Solidity's * https://solidity.readthedocs.io/en/latest/types.html#mapping-types[`mapping`] * type. * * Maps have the following properties: * * - Entries are added, removed, and checked for existence in constant time * (O(1)). * - Entries are enumerated in O(n). No guarantees are made on the ordering. * * ``` * contract Example { * // Add the library methods * using EnumerableMap for EnumerableMap.UintToAddressMap; * * // Declare a set state variable * EnumerableMap.UintToAddressMap private myMap; * } * ``` * * As of v3.0.0, only maps of type `uint256 -> address` (`UintToAddressMap`) are * supported. */ library EnumerableMap { // To implement this library for multiple types with as little code // repetition as possible, we write it in terms of a generic Map type with // bytes32 keys and values. // The Map implementation uses private functions, and user-facing // implementations (such as Uint256ToAddressMap) are just wrappers around // the underlying Map. // This means that we can only create new EnumerableMaps for types that fit // in bytes32. struct MapEntry { bytes32 _key; bytes32 _value; } struct Map { // Storage of map keys and values MapEntry[] _entries; // Position of the entry defined by a key in the `entries` array, plus 1 // because index 0 means a key is not in the map. mapping (bytes32 => uint256) _indexes; } /** * @dev Adds a key-value pair to a map, or updates the value for an existing * key. O(1). * * Returns true if the key was added to the map, that is if it was not * already present. */ function _set(Map storage map, bytes32 key, bytes32 value) private returns (bool) { // We read and store the key's index to prevent multiple reads from the same storage slot uint256 keyIndex = map._indexes[key]; if (keyIndex == 0) { // Equivalent to !contains(map, key) map._entries.push(MapEntry({ _key: key, _value: value })); // The entry is stored at length-1, but we add 1 to all indexes // and use 0 as a sentinel value map._indexes[key] = map._entries.length; return true; } else { map._entries[keyIndex - 1]._value = value; return false; } } /** * @dev Removes a key-value pair from a map. O(1). * * Returns true if the key was removed from the map, that is if it was present. */ function _remove(Map storage map, bytes32 key) private returns (bool) { // We read and store the key's index to prevent multiple reads from the same storage slot uint256 keyIndex = map._indexes[key]; if (keyIndex != 0) { // Equivalent to contains(map, key) // To delete a key-value pair from the _entries array in O(1), we swap the entry to delete with the last one // in the array, and then remove the last entry (sometimes called as 'swap and pop'). // This modifies the order of the array, as noted in {at}. uint256 toDeleteIndex = keyIndex - 1; uint256 lastIndex = map._entries.length - 1; // When the entry to delete is the last one, the swap operation is unnecessary. However, since this occurs // so rarely, we still do the swap anyway to avoid the gas cost of adding an 'if' statement. MapEntry storage lastEntry = map._entries[lastIndex]; // Move the last entry to the index where the entry to delete is map._entries[toDeleteIndex] = lastEntry; // Update the index for the moved entry map._indexes[lastEntry._key] = toDeleteIndex + 1; // All indexes are 1-based // Delete the slot where the moved entry was stored map._entries.pop(); // Delete the index for the deleted slot delete map._indexes[key]; return true; } else { return false; } } /** * @dev Returns true if the key is in the map. O(1). */ function _contains(Map storage map, bytes32 key) private view returns (bool) { return map._indexes[key] != 0; } /** * @dev Returns the number of key-value pairs in the map. O(1). */ function _length(Map storage map) private view returns (uint256) { return map._entries.length; } /** * @dev Returns the key-value pair stored at position `index` in the map. O(1). * * Note that there are no guarantees on the ordering of entries inside the * array, and it may change when more entries are added or removed. * * Requirements: * * - `index` must be strictly less than {length}. */ function _at(Map storage map, uint256 index) private view returns (bytes32, bytes32) { require(map._entries.length > index, "EnumerableMap: index out of bounds"); MapEntry storage entry = map._entries[index]; return (entry._key, entry._value); } /** * @dev Returns the value associated with `key`. O(1). * * Requirements: * * - `key` must be in the map. */ function _get(Map storage map, bytes32 key) private view returns (bytes32) { return _get(map, key, "EnumerableMap: nonexistent key"); } /** * @dev Same as {_get}, with a custom error message when `key` is not in the map. */ function _get(Map storage map, bytes32 key, string memory errorMessage) private view returns (bytes32) { uint256 keyIndex = map._indexes[key]; require(keyIndex != 0, errorMessage); // Equivalent to contains(map, key) return map._entries[keyIndex - 1]._value; // All indexes are 1-based } // UintToAddressMap struct UintToAddressMap { Map _inner; } /** * @dev Adds a key-value pair to a map, or updates the value for an existing * key. O(1). * * Returns true if the key was added to the map, that is if it was not * already present. */ function set(UintToAddressMap storage map, uint256 key, address value) internal returns (bool) { return _set(map._inner, bytes32(key), bytes32(uint256(value))); } /** * @dev Removes a value from a set. O(1). * * Returns true if the key was removed from the map, that is if it was present. */ function remove(UintToAddressMap storage map, uint256 key) internal returns (bool) { return _remove(map._inner, bytes32(key)); } /** * @dev Returns true if the key is in the map. O(1). */ function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) { return _contains(map._inner, bytes32(key)); } /** * @dev Returns the number of elements in the map. O(1). */ function length(UintToAddressMap storage map) internal view returns (uint256) { return _length(map._inner); } /** * @dev Returns the element stored at position `index` in the set. O(1). * Note that there are no guarantees on the ordering of values inside the * array, and it may change when more values are added or removed. * * Requirements: * * - `index` must be strictly less than {length}. */ function at(UintToAddressMap storage map, uint256 index) internal view returns (uint256, address) { (bytes32 key, bytes32 value) = _at(map._inner, index); return (uint256(key), address(uint256(value))); } /** * @dev Returns the value associated with `key`. O(1). * * Requirements: * * - `key` must be in the map. */ function get(UintToAddressMap storage map, uint256 key) internal view returns (address) { return address(uint256(_get(map._inner, bytes32(key)))); } /** * @dev Same as {get}, with a custom error message when `key` is not in the map. */ function get(UintToAddressMap storage map, uint256 key, string memory errorMessage) internal view returns (address) { return address(uint256(_get(map._inner, bytes32(key), errorMessage))); } }
pragma solidity ^0.6.0; /** * @dev String operations. */ library Strings { /** * @dev Converts a `uint256` to its ASCII `string` representation. */ function toString(uint256 value) internal pure returns (string memory) { // Inspired by OraclizeAPI's implementation - MIT licence // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol if (value == 0) { return "0"; } uint256 temp = value; uint256 digits; while (temp != 0) { digits++; temp /= 10; } bytes memory buffer = new bytes(digits); uint256 index = digits - 1; temp = value; while (temp != 0) { buffer[index--] = byte(uint8(48 + temp % 10)); temp /= 10; } return string(buffer); } }
pragma solidity >=0.6.0 <0.7.0; import "../utils/ExtendedSafeCast.sol"; contract ExtendedSafeCastExposed { function toUint112(uint256 value) external pure returns (uint112) { return ExtendedSafeCast.toUint112(value); } function toUint96(uint256 value) external pure returns (uint96) { return ExtendedSafeCast.toUint96(value); } }
pragma solidity >=0.6.0 <0.7.0; import "../utils/MappedSinglyLinkedList.sol"; contract MappedSinglyLinkedListExposed { using MappedSinglyLinkedList for MappedSinglyLinkedList.Mapping; MappedSinglyLinkedList.Mapping list; function initialize() external { list.initialize(); } function addressArray() external view returns (address[] memory) { return list.addressArray(); } function addAddresses(address[] calldata addresses) external { list.addAddresses(addresses); } function addAddress(address newAddress) external { list.addAddress(newAddress); } function removeAddress(address prevAddress, address addr) external { list.removeAddress(prevAddress, addr); } function contains(address addr) external view returns (bool) { return list.contains(addr); } function clearAll() external { list.clearAll(); } }
pragma solidity >=0.6.0 <0.7.0; import "../prize-pool/PrizePool.sol"; import "./YieldSourceStub.sol"; contract PrizePoolHarness is PrizePool { uint256 public currentTime; YieldSourceStub stubYieldSource; function initializeAll( address _trustedForwarder, RegistryInterface _reserveRegistry, address[] memory _controlledTokens, uint256 _maxExitFeeMantissa, uint256 _maxTimelockDuration, YieldSourceStub _stubYieldSource ) public { PrizePool.initialize( _trustedForwarder, _reserveRegistry, _controlledTokens, _maxExitFeeMantissa, _maxTimelockDuration ); stubYieldSource = _stubYieldSource; } function supply(uint256 mintAmount) external { _supply(mintAmount); } function redeem(uint256 redeemAmount) external { _redeem(redeemAmount); } function setCurrentTime(uint256 _currentTime) external { currentTime = _currentTime; } function setTimelockBalance(uint256 _timelockBalance) external { timelockTotalSupply = _timelockBalance; } function _currentTime() internal override view returns (uint256) { return currentTime; } function _canAwardExternal(address _externalToken) internal override view returns (bool) { return stubYieldSource.canAwardExternal(_externalToken); } function _token() internal override view returns (IERC20) { return stubYieldSource.token(); } function _balance() internal override returns (uint256) { return stubYieldSource.balance(); } function _supply(uint256 mintAmount) internal override { return stubYieldSource.supply(mintAmount); } function _redeem(uint256 redeemAmount) internal override returns (uint256) { return stubYieldSource.redeem(redeemAmount); } }
pragma solidity >=0.6.0 <0.7.0; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol"; interface YieldSourceStub { function canAwardExternal(address _externalToken) external view returns (bool); function token() external view returns (IERC20); function balance() external returns (uint256); function supply(uint256 mintAmount) external; function redeem(uint256 redeemAmount) external returns (uint256); }
pragma solidity >=0.6.0 <0.7.0; import "@pooltogether/pooltogether-rng-contracts/contracts/RNGInterface.sol"; contract RNGServiceMock is RNGInterface { uint256 internal random; address internal feeToken; uint256 internal requestFee; function getLastRequestId() external override view returns (uint32 requestId) { return 1; } function setRequestFee(address _feeToken, uint256 _requestFee) external { feeToken = _feeToken; requestFee = _requestFee; } /// @return _feeToken /// @return _requestFee function getRequestFee() external override view returns (address _feeToken, uint256 _requestFee) { return (feeToken, requestFee); } function setRandomNumber(uint256 _random) external { random = _random; } function requestRandomNumber() external override returns (uint32, uint32) { return (1, 1); } function isRequestComplete(uint32) external override view returns (bool) { return true; } function randomNumber(uint32) external override returns (uint256) { return random; } }
pragma solidity >=0.6.0 <0.7.0; import "../prize-strategy/single-random-winner/SingleRandomWinner.sol"; /* solium-disable security/no-block-members */ contract SingleRandomWinnerHarness is SingleRandomWinner { uint256 internal time; function setCurrentTime(uint256 _time) external { time = _time; } function _currentTime() internal override view returns (uint256) { return time; } function setRngRequest(uint32 requestId, uint32 lockBlock) external { rngRequest.id = requestId; rngRequest.lockBlock = lockBlock; } }
pragma solidity >=0.6.0 <0.7.0; import "./SingleRandomWinnerHarness.sol"; import "../external/openzeppelin/ProxyFactory.sol"; contract SingleRandomWinnerHarnessProxyFactory is ProxyFactory { SingleRandomWinnerHarness public instance; constructor () public { instance = new SingleRandomWinnerHarness(); } function create() external returns (SingleRandomWinnerHarness) { return SingleRandomWinnerHarness(deployMinimal(address(instance), "")); } }
pragma solidity >=0.6.0 <0.7.0; /* solium-disable security/no-block-members */ contract Timestamp { function blockTime() public view returns (uint256) { return block.timestamp; } }
pragma solidity >=0.6.0 <0.7.0; import "../utils/UInt256Array.sol"; contract UInt256ArrayExposed { using UInt256Array for uint256[]; uint256[] internal array; constructor (uint256[] memory _array) public { array = new uint256[](_array.length); for (uint256 i = 0; i < _array.length; i++) { array[i] = _array[i]; } } function remove(uint256 index) external { array.remove(index); } function toArray() external view returns (uint256[] memory) { return array; } }
pragma solidity >=0.6.0 <0.7.0; import "../drip/VolumeDrip.sol"; contract VolumeDripExposed { using VolumeDrip for VolumeDrip.State; event DripTokensBurned(address user, uint256 amount); event Minted(uint256 amount); event MintedTotalSupply(uint256 amount); VolumeDrip.State state; function setNewPeriod(uint32 periodSeconds, uint112 dripAmount, uint32 endTime) external { state.setNewPeriod(periodSeconds, dripAmount, endTime); } function setNextPeriod(uint32 periodSeconds, uint112 dripAmount) external { state.setNextPeriod(periodSeconds, dripAmount); } function drip(uint256 currentTime, uint256 maxNewTokens) external returns (uint256) { uint256 newTokens = state.drip(currentTime, maxNewTokens); emit MintedTotalSupply(newTokens); return newTokens; } function mint(address user, uint256 amount) external returns (uint256) { uint256 accrued = state.mint(user, amount); emit Minted(accrued); return accrued; } function getDrip() external view returns ( uint32 periodSeconds, uint128 dripAmount ) { periodSeconds = state.nextPeriodSeconds; dripAmount = state.nextDripAmount; } function getPeriod(uint32 period) external view returns ( uint112 totalSupply, uint112 dripAmount, uint32 endTime ) { totalSupply = state.periods[period].totalSupply; endTime = state.periods[period].endTime; dripAmount = state.periods[period].dripAmount; } function getDeposit(address user) external view returns ( uint112 balance, uint32 period ) { balance = state.deposits[user].balance; period = state.deposits[user].period; } }
pragma solidity >=0.6.0 <0.7.0; import "../drip/VolumeDripManager.sol"; contract VolumeDripManagerExposed { using VolumeDripManager for VolumeDripManager.State; using VolumeDrip for VolumeDrip.State; VolumeDripManager.State manager; function activate( address measure, address dripToken, uint32 periodSeconds, uint112 dripAmount, uint32 endTime ) external { manager.activate(measure, dripToken, periodSeconds, dripAmount, endTime); } function deactivate( address measure, address dripToken, address prevDripToken ) external { manager.deactivate(measure, dripToken, prevDripToken); } function set(address measure, address dripToken, uint32 periodSeconds, uint112 dripAmount) external { manager.set(measure, dripToken, periodSeconds, dripAmount); } function isActive(address measure, address dripToken) external view returns (bool) { return manager.isActive(measure, dripToken); } function getPeriod( address measure, address dripToken, uint32 period ) external view returns ( uint112 totalSupply, uint112 dripAmount, uint32 endTime ) { VolumeDrip.State storage drip = manager.getDrip(measure, dripToken); VolumeDrip.Period memory state = drip.periods[period]; totalSupply = state.totalSupply; dripAmount = state.dripAmount; endTime = state.endTime; } function getActiveVolumeDrips(address measure) external view returns (address[] memory) { return manager.getActiveVolumeDrips(measure); } function getDrip( address measure, address dripToken ) external view returns ( uint32 periodSeconds, uint112 dripAmount ) { VolumeDrip.State storage drip = manager.getDrip(measure, dripToken); dripAmount = drip.nextDripAmount; periodSeconds = drip.nextPeriodSeconds; } }
pragma solidity >=0.6.0 <0.7.0; import "../external/yearn/yVaultInterface.sol"; import "./ERC20Mintable.sol"; import "../external/pooltogether/FixedPoint.sol"; import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/ERC20.sol"; contract yVaultMock is yVaultInterface, ERC20UpgradeSafe { ERC20UpgradeSafe private asset; uint256 public vaultFeeMantissa; constructor (ERC20Mintable _asset) public { asset = _asset; vaultFeeMantissa = 0.05 ether; } function token() external override view returns (address) { return address(asset); } function balance() public override view returns (uint) { return asset.balanceOf(address(this)); } function removeLiquidity(uint _amount) external { asset.transfer(msg.sender, _amount); } function setVaultFeeMantissa(uint256 _vaultFeeMantissa) external { vaultFeeMantissa = _vaultFeeMantissa; } function deposit(uint _amount) external override { uint _pool = balance(); uint _before = asset.balanceOf(address(this)); asset.transferFrom(msg.sender, address(this), _amount); uint _after = asset.balanceOf(address(this)); uint diff = _after.sub(_before); // Additional check for deflationary assets uint shares = 0; if (totalSupply() == 0) { shares = diff; } else { shares = (diff.mul(totalSupply())).div(_pool); } _mint(msg.sender, shares); } function withdraw(uint _shares) external override { uint256 sharesFee = FixedPoint.multiplyUintByMantissa(_shares, vaultFeeMantissa); uint256 withdrawal = (balance().mul(_shares.sub(sharesFee))).div(totalSupply()); asset.transfer(msg.sender, withdrawal); _mint(address(this), sharesFee); _burn(msg.sender, _shares); } function getPricePerFullShare() external override view returns (uint) { return balance().mul(1e18).div(totalSupply()); } }
pragma solidity >=0.6.0 <0.7.0; import "../prize-pool/yearn/yVaultPrizePool.sol"; contract yVaultPrizePoolHarness is yVaultPrizePool { uint256 public currentTime; function setCurrentTime(uint256 _currentTime) external { currentTime = _currentTime; } function _currentTime() internal override view returns (uint256) { return currentTime; } function supply(uint256 mintAmount) external { _supply(mintAmount); } function redeem(uint256 redeemAmount) external returns (uint256) { return _redeem(redeemAmount); } }
pragma solidity >=0.6.0 <0.7.0; import "./yVaultPrizePoolHarness.sol"; import "../external/openzeppelin/ProxyFactory.sol"; /// @title Compound Prize Pool Proxy Factory /// @notice Minimal proxy pattern for creating new Compound Prize Pools contract yVaultPrizePoolHarnessProxyFactory is ProxyFactory { /// @notice Contract template for deploying proxied Prize Pools yVaultPrizePoolHarness public instance; /// @notice Initializes the Factory with an instance of the Compound Prize Pool constructor () public { instance = new yVaultPrizePoolHarness(); } /// @notice Creates a new Compound Prize Pool as a proxy of the template instance /// @return A reference to the new proxied Compound Prize Pool function create() external returns (yVaultPrizePoolHarness) { return yVaultPrizePoolHarness(deployMinimal(address(instance), "")); } }
{ "metadata": { "useLiteralContent": true }, "optimizer": { "enabled": true, "runs": 200 }, "outputSelection": { "*": { "*": [ "evm.bytecode", "evm.deployedBytecode", "abi" ] } }, "evmVersion": "istanbul", "libraries": {} }
[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"chanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"controller","outputs":[{"internalType":"contract TokenControllerInterface","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"controllerBurn","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_operator","type":"address"},{"internalType":"address","name":"_user","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"controllerBurnFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"controllerMint","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"randomNumber","type":"uint256"}],"name":"draw","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"uint8","name":"_decimals","type":"uint8"},{"internalType":"address","name":"_trustedForwarder","type":"address"},{"internalType":"contract TokenControllerInterface","name":"_controller","type":"address"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"forwarder","type":"address"}],"name":"isTrustedForwarder","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"trustedForwarder","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"versionRecipient","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}]
Loading...
Loading
Loading...
Loading
[ Download: CSV Export ]
[ Download: CSV Export ]
A token is a representation of an on-chain or off-chain asset. The token page shows information such as price, total supply, holders, transfers and social links. Learn more about this page in our Knowledge Base.