-
-
Save dulumao/c3d1052f1eba1cca840a8048b171c344 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pragma solidity 0.5.17; | |
| import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol"; | |
| import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | |
| import "@openzeppelin/contracts/token/ERC20/ERC20Burnable.sol"; | |
| import "@openzeppelin/contracts/ownership/Ownable.sol"; | |
| import './interfaces/IUniswapV2Router01.sol'; | |
| contract IERC20Burnable is IERC20 { | |
| function burn(uint256 amount) public; | |
| } | |
| contract ERC20ReservePool is Ownable { | |
| using SafeERC20 for IERC20; | |
| using SafeERC20 for IERC20Burnable; | |
| bool public openBuyBackAndBurn = false; | |
| IERC20 public reserveToken; | |
| IERC20Burnable public typhoonToken; | |
| IUniswapV2Router01 public router; | |
| address[] path; | |
| uint256 public totalBurnedAmount = 0; | |
| constructor(address _reserveToken, address _typhoonToken, IUniswapV2Router01 router_) public { | |
| reserveToken = IERC20(_reserveToken); | |
| typhoonToken = IERC20Burnable(_typhoonToken); | |
| router = router_; | |
| path = [_reserveToken, _typhoonToken]; | |
| } | |
| function universalApprove(IERC20 token, address to, uint256 amount) internal { | |
| if (amount == 0) { | |
| token.safeApprove(to, 0); | |
| return; | |
| } | |
| uint256 allowance = token.allowance(address(this), to); | |
| if (allowance < amount) { | |
| if (allowance > 0) { | |
| token.safeApprove(to, 0); | |
| } | |
| token.safeApprove(to, amount); | |
| } | |
| } | |
| function setPath(address[] memory _path) public onlyOwner { | |
| require(_path[_path.length - 1] == address(typhoonToken)); | |
| path = _path; | |
| } | |
| function getPath() public view returns (address[] memory) { | |
| return path; | |
| } | |
| function setOpenBuyBackAndBurn(bool _openBuyBackAndBurn) public onlyOwner { | |
| openBuyBackAndBurn = _openBuyBackAndBurn; | |
| } | |
| function buyBackAndBurn() public { | |
| require(openBuyBackAndBurn, "Buyback And Burn Not Opened."); | |
| _buyBackAndBurn(); | |
| } | |
| function ownerBuyBackAndBurn() public onlyOwner { | |
| _buyBackAndBurn(); | |
| } | |
| function _buyBackAndBurn() internal { | |
| require(reserveToken.balanceOf(address(this)) > 0, "Reserve Token Balance zero."); | |
| uint256 amountIn = reserveToken.balanceOf(address(this)); | |
| universalApprove(reserveToken, address(router), amountIn); | |
| uint deadline = block.timestamp + 10000; | |
| router.swapExactTokensForTokens( | |
| amountIn, | |
| 0, | |
| path, | |
| address(this), | |
| deadline | |
| ); | |
| // Record total burn amount | |
| totalBurnedAmount += amountIn; | |
| // Burn | |
| typhoonToken.burn(typhoonToken.balanceOf(address(this))); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://tornado.cash | |
| /* | |
| * d888888P dP a88888b. dP | |
| * 88 88 d8' `88 88 | |
| * 88 .d8888b. 88d888b. 88d888b. .d8888b. .d888b88 .d8888b. 88 .d8888b. .d8888b. 88d888b. | |
| * 88 88' `88 88' `88 88' `88 88' `88 88' `88 88' `88 88 88' `88 Y8ooooo. 88' `88 | |
| * 88 88. .88 88 88 88 88. .88 88. .88 88. .88 dP Y8. .88 88. .88 88 88 88 | |
| * dP `88888P' dP dP dP `88888P8 `88888P8 `88888P' 88 Y88888P' `88888P8 `88888P' dP dP | |
| * ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo | |
| */ | |
| pragma solidity 0.5.17; | |
| import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol"; | |
| import "./Tornado.sol"; | |
| contract ERC20Tornado is Tornado { | |
| using SafeERC20 for IERC20; | |
| address public token; | |
| constructor( | |
| IVerifier _verifier, | |
| uint256 _denomination, | |
| uint32 _merkleTreeHeight, | |
| address _operator, | |
| address _governance, | |
| address _reserve, | |
| address _token | |
| ) Tornado(_verifier, _denomination, _merkleTreeHeight, _operator, _governance, _reserve) public { | |
| token = _token; | |
| } | |
| function _processDeposit() internal { | |
| require(msg.value == 0, "ETH value is supposed to be 0 for ERC20 instance"); | |
| _safeErc20TransferFrom(msg.sender, address(this), denomination); | |
| } | |
| function _processWithdraw(address payable _recipient, uint256 _withdraw_fee, address payable _relayer, uint256 _fee, uint256 _refund) internal { | |
| require(msg.value == _refund, "Incorrect refund amount received by the contract"); | |
| _safeErc20Transfer(reserve, _withdraw_fee); | |
| _safeErc20Transfer(_recipient, denomination - _withdraw_fee - _fee); | |
| if (_fee > 0) { | |
| _safeErc20Transfer(_relayer, _fee); | |
| } | |
| if (_refund > 0) { | |
| (bool success, ) = _recipient.call.value(_refund)(""); | |
| if (!success) { | |
| // let's return _refund back to the relayer | |
| _relayer.transfer(_refund); | |
| } | |
| } | |
| } | |
| function _safeErc20TransferFrom(address _from, address _to, uint256 _amount) internal { | |
| (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd /* transferFrom */, _from, _to, _amount)); | |
| require(success, "not enough allowed tokens"); | |
| // if contract returns some data lets make sure that is `true` according to standard | |
| if (data.length > 0) { | |
| require(data.length == 32, "data length should be either 0 or 32 bytes"); | |
| success = abi.decode(data, (bool)); | |
| require(success, "not enough allowed tokens. Token returns false."); | |
| } | |
| } | |
| function _safeErc20Transfer(address _to, uint256 _amount) internal { | |
| (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb /* transfer */, _to, _amount)); | |
| require(success, "not enough tokens"); | |
| // if contract returns some data lets make sure that is `true` according to standard | |
| if (data.length > 0) { | |
| require(data.length == 32, "data length should be either 0 or 32 bytes"); | |
| success = abi.decode(data, (bool)); | |
| require(success, "not enough tokens. Token returns false."); | |
| } | |
| } | |
| /** @dev seize accidental sent token */ | |
| function seize(IERC20 _token, uint amount) external onlyGovernance { | |
| require(address(_token) != token, "cannot be token"); | |
| _token.safeTransfer(governance, amount); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| *Submitted for verification at Etherscan.io on 2020-07-17 | |
| */ | |
| /* | |
| ____ __ __ __ _ | |
| / __/__ __ ___ / /_ / / ___ / /_ (_)__ __ | |
| _\ \ / // // _ \/ __// _ \/ -_)/ __// / \ \ / | |
| /___/ \_, //_//_/\__//_//_/\__/ \__//_/ /_\_\ | |
| /___/ | |
| * Synthetix: YFIRewards.sol | |
| * | |
| * Docs: https://docs.synthetix.io/ | |
| * | |
| * | |
| * MIT License | |
| * =========== | |
| * | |
| * Copyright (c) 2020 Synthetix | |
| * | |
| * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| * of this software and associated documentation files (the "Software"), to deal | |
| * in the Software without restriction, including without limitation the rights | |
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| * copies of the Software, and to permit persons to whom the Software is | |
| * furnished to do so, subject to the following conditions: | |
| * | |
| * The above copyright notice and this permission notice shall be included in all | |
| * copies or substantial portions of the Software. | |
| * | |
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| */ | |
| // File: @openzeppelin/contracts/math/Math.sol | |
| pragma solidity ^0.5.0; | |
| /** | |
| * @dev Standard math utilities missing in the Solidity language. | |
| */ | |
| library Math { | |
| /** | |
| * @dev Returns the largest of two numbers. | |
| */ | |
| function max(uint256 a, uint256 b) internal pure returns (uint256) { | |
| return a >= b ? a : b; | |
| } | |
| /** | |
| * @dev Returns the smallest of two numbers. | |
| */ | |
| function min(uint256 a, uint256 b) internal pure returns (uint256) { | |
| return a < b ? a : b; | |
| } | |
| /** | |
| * @dev Returns the average of two numbers. The result is rounded towards | |
| * zero. | |
| */ | |
| function average(uint256 a, uint256 b) internal pure returns (uint256) { | |
| // (a + b) / 2 can overflow, so we distribute | |
| return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2); | |
| } | |
| } | |
| // File: @openzeppelin/contracts/math/SafeMath.sol | |
| pragma solidity ^0.5.0; | |
| /** | |
| * @dev Wrappers over Solidity's arithmetic operations with added overflow | |
| * checks. | |
| * | |
| * Arithmetic operations in Solidity wrap on overflow. This can easily result | |
| * in bugs, because programmers usually assume that an overflow raises an | |
| * error, which is the standard behavior in high level programming languages. | |
| * `SafeMath` restores this intuition by reverting the transaction when an | |
| * operation overflows. | |
| * | |
| * Using this library instead of the unchecked operations eliminates an entire | |
| * class of bugs, so it's recommended to use it always. | |
| */ | |
| library SafeMath { | |
| /** | |
| * @dev Returns the addition of two unsigned integers, reverting on | |
| * overflow. | |
| * | |
| * Counterpart to Solidity's `+` operator. | |
| * | |
| * Requirements: | |
| * - Addition cannot overflow. | |
| */ | |
| function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
| uint256 c = a + b; | |
| require(c >= a, "SafeMath: addition overflow"); | |
| return c; | |
| } | |
| /** | |
| * @dev Returns the subtraction of two unsigned integers, reverting on | |
| * overflow (when the result is negative). | |
| * | |
| * Counterpart to Solidity's `-` operator. | |
| * | |
| * Requirements: | |
| * - Subtraction cannot overflow. | |
| */ | |
| function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
| return sub(a, b, "SafeMath: subtraction overflow"); | |
| } | |
| /** | |
| * @dev Returns the subtraction of two unsigned integers, reverting with custom message on | |
| * overflow (when the result is negative). | |
| * | |
| * Counterpart to Solidity's `-` operator. | |
| * | |
| * Requirements: | |
| * - Subtraction cannot overflow. | |
| * | |
| * _Available since v2.4.0._ | |
| */ | |
| function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
| require(b <= a, errorMessage); | |
| uint256 c = a - b; | |
| return c; | |
| } | |
| /** | |
| * @dev Returns the multiplication of two unsigned integers, reverting on | |
| * overflow. | |
| * | |
| * Counterpart to Solidity's `*` operator. | |
| * | |
| * Requirements: | |
| * - Multiplication cannot overflow. | |
| */ | |
| function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
| // Gas optimization: this is cheaper than requiring 'a' not being zero, but the | |
| // benefit is lost if 'b' is also tested. | |
| // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 | |
| if (a == 0) { | |
| return 0; | |
| } | |
| uint256 c = a * b; | |
| require(c / a == b, "SafeMath: multiplication overflow"); | |
| return c; | |
| } | |
| /** | |
| * @dev Returns the integer division of two unsigned integers. Reverts on | |
| * division by zero. The result is rounded towards zero. | |
| * | |
| * Counterpart to Solidity's `/` operator. Note: this function uses a | |
| * `revert` opcode (which leaves remaining gas untouched) while Solidity | |
| * uses an invalid opcode to revert (consuming all remaining gas). | |
| * | |
| * Requirements: | |
| * - The divisor cannot be zero. | |
| */ | |
| function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
| return div(a, b, "SafeMath: division by zero"); | |
| } | |
| /** | |
| * @dev Returns the integer division of two unsigned integers. Reverts with custom message on | |
| * division by zero. The result is rounded towards zero. | |
| * | |
| * Counterpart to Solidity's `/` operator. Note: this function uses a | |
| * `revert` opcode (which leaves remaining gas untouched) while Solidity | |
| * uses an invalid opcode to revert (consuming all remaining gas). | |
| * | |
| * Requirements: | |
| * - The divisor cannot be zero. | |
| * | |
| * _Available since v2.4.0._ | |
| */ | |
| function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
| // Solidity only automatically asserts when dividing by 0 | |
| require(b > 0, errorMessage); | |
| uint256 c = a / b; | |
| // assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
| return c; | |
| } | |
| /** | |
| * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), | |
| * Reverts when dividing by zero. | |
| * | |
| * Counterpart to Solidity's `%` operator. This function uses a `revert` | |
| * opcode (which leaves remaining gas untouched) while Solidity uses an | |
| * invalid opcode to revert (consuming all remaining gas). | |
| * | |
| * Requirements: | |
| * - The divisor cannot be zero. | |
| */ | |
| function mod(uint256 a, uint256 b) internal pure returns (uint256) { | |
| return mod(a, b, "SafeMath: modulo by zero"); | |
| } | |
| /** | |
| * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), | |
| * Reverts with custom message when dividing by zero. | |
| * | |
| * Counterpart to Solidity's `%` operator. This function uses a `revert` | |
| * opcode (which leaves remaining gas untouched) while Solidity uses an | |
| * invalid opcode to revert (consuming all remaining gas). | |
| * | |
| * Requirements: | |
| * - The divisor cannot be zero. | |
| * | |
| * _Available since v2.4.0._ | |
| */ | |
| function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
| require(b != 0, errorMessage); | |
| return a % b; | |
| } | |
| } | |
| // File: @openzeppelin/contracts/GSN/Context.sol | |
| pragma solidity ^0.5.0; | |
| /* | |
| * @dev Provides information about the current execution context, including the | |
| * sender of the transaction and its data. While these are generally available | |
| * via msg.sender and msg.data, they should not be accessed in such a direct | |
| * manner, since when dealing with GSN meta-transactions the account sending and | |
| * paying for execution may not be the actual sender (as far as an application | |
| * is concerned). | |
| * | |
| * This contract is only required for intermediate, library-like contracts. | |
| */ | |
| contract Context { | |
| // Empty internal constructor, to prevent people from mistakenly deploying | |
| // an instance of this contract, which should be used via inheritance. | |
| constructor () internal { } | |
| // solhint-disable-previous-line no-empty-blocks | |
| function _msgSender() internal view returns (address payable) { | |
| return msg.sender; | |
| } | |
| function _msgData() internal view returns (bytes memory) { | |
| this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691 | |
| return msg.data; | |
| } | |
| } | |
| // File: @openzeppelin/contracts/ownership/Ownable.sol | |
| pragma solidity ^0.5.0; | |
| /** | |
| * @dev Contract module which provides a basic access control mechanism, where | |
| * there is an account (an owner) that can be granted exclusive access to | |
| * specific functions. | |
| * | |
| * This module is used through inheritance. It will make available the modifier | |
| * `onlyOwner`, which can be applied to your functions to restrict their use to | |
| * the owner. | |
| */ | |
| contract Ownable is Context { | |
| address private _owner; | |
| event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); | |
| /** | |
| * @dev Initializes the contract setting the deployer as the initial owner. | |
| */ | |
| constructor () internal { | |
| _owner = _msgSender(); | |
| emit OwnershipTransferred(address(0), _owner); | |
| } | |
| /** | |
| * @dev Returns the address of the current owner. | |
| */ | |
| function owner() public view returns (address) { | |
| return _owner; | |
| } | |
| /** | |
| * @dev Throws if called by any account other than the owner. | |
| */ | |
| modifier onlyOwner() { | |
| require(isOwner(), "Ownable: caller is not the owner"); | |
| _; | |
| } | |
| /** | |
| * @dev Returns true if the caller is the current owner. | |
| */ | |
| function isOwner() public view returns (bool) { | |
| return _msgSender() == _owner; | |
| } | |
| /** | |
| * @dev Leaves the contract without owner. It will not be possible to call | |
| * `onlyOwner` functions anymore. Can only be called by the current owner. | |
| * | |
| * NOTE: Renouncing ownership will leave the contract without an owner, | |
| * thereby removing any functionality that is only available to the owner. | |
| */ | |
| function renounceOwnership() public onlyOwner { | |
| emit OwnershipTransferred(_owner, address(0)); | |
| _owner = address(0); | |
| } | |
| /** | |
| * @dev Transfers ownership of the contract to a new account (`newOwner`). | |
| * Can only be called by the current owner. | |
| */ | |
| function transferOwnership(address newOwner) public onlyOwner { | |
| _transferOwnership(newOwner); | |
| } | |
| /** | |
| * @dev Transfers ownership of the contract to a new account (`newOwner`). | |
| */ | |
| function _transferOwnership(address newOwner) internal { | |
| require(newOwner != address(0), "Ownable: new owner is the zero address"); | |
| emit OwnershipTransferred(_owner, newOwner); | |
| _owner = newOwner; | |
| } | |
| } | |
| // File: @openzeppelin/contracts/token/ERC20/IERC20.sol | |
| pragma solidity ^0.5.0; | |
| /** | |
| * @dev Interface of the ERC20 standard as defined in the EIP. Does not include | |
| * the optional functions; to access them see {ERC20Detailed}. | |
| */ | |
| interface IERC20 { | |
| /** | |
| * @dev Returns the amount of tokens in existence. | |
| */ | |
| function totalSupply() external view returns (uint256); | |
| /** | |
| * @dev Returns the amount of tokens owned by `account`. | |
| */ | |
| function balanceOf(address account) external view returns (uint256); | |
| /** | |
| * @dev Moves `amount` tokens from the caller's account to `recipient`. | |
| * | |
| * Returns a boolean value indicating whether the operation succeeded. | |
| * | |
| * Emits a {Transfer} event. | |
| */ | |
| function transfer(address recipient, uint256 amount) external returns (bool); | |
| /** | |
| * @dev Returns the remaining number of tokens that `spender` will be | |
| * allowed to spend on behalf of `owner` through {transferFrom}. This is | |
| * zero by default. | |
| * | |
| * This value changes when {approve} or {transferFrom} are called. | |
| */ | |
| function allowance(address owner, address spender) external view returns (uint256); | |
| /** | |
| * @dev Sets `amount` as the allowance of `spender` over the caller's tokens. | |
| * | |
| * Returns a boolean value indicating whether the operation succeeded. | |
| * | |
| * IMPORTANT: Beware that changing an allowance with this method brings the risk | |
| * that someone may use both the old and the new allowance by unfortunate | |
| * transaction ordering. One possible solution to mitigate this race | |
| * condition is to first reduce the spender's allowance to 0 and set the | |
| * desired value afterwards: | |
| * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 | |
| * | |
| * Emits an {Approval} event. | |
| */ | |
| function approve(address spender, uint256 amount) external returns (bool); | |
| /** | |
| * @dev Moves `amount` tokens from `sender` to `recipient` using the | |
| * allowance mechanism. `amount` is then deducted from the caller's | |
| * allowance. | |
| * | |
| * Returns a boolean value indicating whether the operation succeeded. | |
| * | |
| * Emits a {Transfer} event. | |
| */ | |
| function transferFrom(address sender, address recipient, uint256 amount) external returns (bool); | |
| /** | |
| * @dev Emitted when `value` tokens are moved from one account (`from`) to | |
| * another (`to`). | |
| * | |
| * Note that `value` may be zero. | |
| */ | |
| event Transfer(address indexed from, address indexed to, uint256 value); | |
| /** | |
| * @dev Emitted when the allowance of a `spender` for an `owner` is set by | |
| * a call to {approve}. `value` is the new allowance. | |
| */ | |
| event Approval(address indexed owner, address indexed spender, uint256 value); | |
| } | |
| contract ERC20Detailed is IERC20 { | |
| string private _name; | |
| string private _symbol; | |
| uint8 private _decimals; | |
| constructor (string memory name, string memory symbol, uint8 decimals) public { | |
| _name = name; | |
| _symbol = symbol; | |
| _decimals = decimals; | |
| } | |
| function name() public view returns (string memory) { | |
| return _name; | |
| } | |
| function symbol() public view returns (string memory) { | |
| return _symbol; | |
| } | |
| function decimals() public view returns (uint8) { | |
| return _decimals; | |
| } | |
| } | |
| // File: @openzeppelin/contracts/utils/Address.sol | |
| pragma solidity ^0.5.5; | |
| /** | |
| * @dev Collection of functions related to the address type | |
| */ | |
| library Address { | |
| /** | |
| * @dev Returns true if `account` is a contract. | |
| * | |
| * This test is non-exhaustive, and there may be false-negatives: during the | |
| * execution of a contract's constructor, its address will be reported as | |
| * not containing 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. | |
| */ | |
| 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. | |
| // 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 != 0x0 && codehash != accountHash); | |
| } | |
| /** | |
| * @dev Converts an `address` into `address payable`. Note that this is | |
| * simply a type cast: the actual underlying value is not changed. | |
| * | |
| * _Available since v2.4.0._ | |
| */ | |
| function toPayable(address account) internal pure returns (address payable) { | |
| return address(uint160(account)); | |
| } | |
| /** | |
| * @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]. | |
| * | |
| * _Available since v2.4.0._ | |
| */ | |
| function sendValue(address payable recipient, uint256 amount) internal { | |
| require(address(this).balance >= amount, "Address: insufficient balance"); | |
| // solhint-disable-next-line avoid-call-value | |
| (bool success, ) = recipient.call.value(amount)(""); | |
| require(success, "Address: unable to send value, recipient may have reverted"); | |
| } | |
| } | |
| // File: @openzeppelin/contracts/token/ERC20/SafeERC20.sol | |
| pragma solidity ^0.5.0; | |
| /** | |
| * @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"); | |
| } | |
| } | |
| } | |
| // Tornado contract interface. | |
| interface Tornado { | |
| function denomination() external view returns (uint256); | |
| function token() external view returns (address); | |
| function nullifierHashes(bytes32 _nullifierHash) external view returns(bool); | |
| function getStakedCount() external view returns (uint256); | |
| } | |
| // File: contracts/IRewardDistributionRecipient.sol | |
| pragma solidity ^0.5.0; | |
| contract IRewardDistributionRecipient is Ownable { | |
| address rewardDistribution; | |
| function notifyRewardAmount(uint256 reward) external; | |
| modifier onlyRewardDistribution() { | |
| require(_msgSender() == rewardDistribution, "Caller is not reward distribution"); | |
| _; | |
| } | |
| function setRewardDistribution(address _rewardDistribution) | |
| external | |
| onlyOwner | |
| { | |
| rewardDistribution = _rewardDistribution; | |
| } | |
| } | |
| // File: contracts/CurveRewards.sol | |
| pragma solidity ^0.5.0; | |
| contract ERC20YFIRewards is IRewardDistributionRecipient { | |
| using SafeMath for uint256; | |
| using SafeERC20 for IERC20; | |
| IERC20 public typhoon; | |
| uint256 public constant DURATION = 7 days; | |
| uint256 public periodFinish = 0; | |
| uint256 public rewardRate = 0; | |
| uint256 public lastUpdateTime; | |
| uint256 public rewardPerTokenStored; | |
| Tornado public tornado; | |
| ERC20Detailed public tornadoInputToken; | |
| mapping(bytes32 => uint256) public userRewardPerTokenPaid; | |
| mapping(bytes32 => uint256) public rewards; | |
| mapping(bytes32 => bool) public nullifierHashDeposit; | |
| event RewardAdded(uint256 reward); | |
| event Staked(bytes32 indexed nullifierHash); | |
| event Withdrawn(bytes32 indexed nullifierHash); | |
| event RewardPaid(address indexed user, uint256 reward); | |
| modifier updateReward(bytes32 _nullifierHash) { | |
| rewardPerTokenStored = rewardPerToken(); | |
| lastUpdateTime = lastTimeRewardApplicable(); | |
| if (_nullifierHash != 0x0) { | |
| rewards[_nullifierHash] = earned(_nullifierHash); | |
| userRewardPerTokenPaid[_nullifierHash] = rewardPerTokenStored; | |
| } | |
| _; | |
| } | |
| constructor(Tornado _tornado, IERC20 _typhoon) public { | |
| tornado = _tornado; | |
| tornadoInputToken = ERC20Detailed(tornado.token()); | |
| typhoon = _typhoon; | |
| } | |
| function seize(IERC20 _token, uint amount) external onlyOwner { | |
| require(_token != typhoon, "typhoon"); | |
| _token.safeTransfer(msg.sender, amount); | |
| } | |
| function balanceOf(bytes32 _nullifierHash) public view returns (uint256) { | |
| if (!nullifierHashDeposit[_nullifierHash]) { | |
| return 0; | |
| } | |
| return tornado.denomination(); | |
| } | |
| function totalSupply() public view returns (uint256) { | |
| uint256 depositCount = tornado.getStakedCount(); | |
| uint256 denomination = tornado.denomination(); | |
| return depositCount.mul(denomination); | |
| } | |
| function lastTimeRewardApplicable() public view returns (uint256) { | |
| return Math.min(block.timestamp, periodFinish); | |
| } | |
| function rewardPerToken() public view returns (uint256) { | |
| if (totalSupply() == 0) { | |
| return rewardPerTokenStored; | |
| } | |
| return | |
| rewardPerTokenStored.add( | |
| lastTimeRewardApplicable() | |
| .sub(lastUpdateTime) | |
| .mul(rewardRate) | |
| .mul(1e18) | |
| .div(totalSupply()) | |
| ); | |
| } | |
| function earned(bytes32 _nullifierHash) public view returns (uint256) { | |
| return | |
| balanceOf(_nullifierHash) | |
| .mul(rewardPerToken().sub(userRewardPerTokenPaid[_nullifierHash])) | |
| .div(1e18) | |
| .add(rewards[_nullifierHash]); | |
| } | |
| function stake(bytes32 _nullifierHash) public updateReward(_nullifierHash) { | |
| // only ternado contract can trigger this method | |
| require(msg.sender == address(tornado), "sender not tornado contract"); | |
| require(!nullifierHashDeposit[_nullifierHash]); | |
| nullifierHashDeposit[_nullifierHash] = true; | |
| emit Staked(_nullifierHash); | |
| } | |
| function withdraw(bytes32 _nullifierHash, address payable _recipient) public { | |
| // only ternado contract can trigger this method | |
| require(msg.sender == address(tornado), "sender not tornado contract"); | |
| uint256 reward = earned(_nullifierHash); | |
| if (reward > 0) { | |
| rewards[_nullifierHash] = 0; | |
| typhoon.safeTransfer(_recipient, reward); | |
| emit RewardPaid(_recipient, reward); | |
| } | |
| emit Withdrawn(_nullifierHash); | |
| } | |
| function notifyRewardAmount(uint256 reward) | |
| external | |
| onlyRewardDistribution | |
| updateReward(0x0) | |
| { | |
| if (block.timestamp >= periodFinish) { | |
| rewardRate = reward.div(DURATION); | |
| } else { | |
| uint256 remaining = periodFinish.sub(block.timestamp); | |
| uint256 leftover = remaining.mul(rewardRate); | |
| rewardRate = reward.add(leftover).div(DURATION); | |
| } | |
| lastUpdateTime = block.timestamp; | |
| periodFinish = block.timestamp.add(DURATION); | |
| emit RewardAdded(reward); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://tornado.cash | |
| /* | |
| * d888888P dP a88888b. dP | |
| * 88 88 d8' `88 88 | |
| * 88 .d8888b. 88d888b. 88d888b. .d8888b. .d888b88 .d8888b. 88 .d8888b. .d8888b. 88d888b. | |
| * 88 88' `88 88' `88 88' `88 88' `88 88' `88 88' `88 88 88' `88 Y8ooooo. 88' `88 | |
| * 88 88. .88 88 88 88 88. .88 88. .88 88. .88 dP Y8. .88 88. .88 88 88 88 | |
| * dP `88888P' dP dP dP `88888P8 `88888P8 `88888P' 88 Y88888P' `88888P8 `88888P' dP dP | |
| * ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo | |
| */ | |
| pragma solidity 0.5.17; | |
| library Hasher { | |
| function MiMCSponge(uint256 in_xL, uint256 in_xR) public pure returns (uint256 xL, uint256 xR); | |
| } | |
| contract MerkleTreeWithHistory { | |
| uint256 public constant FIELD_SIZE = 21888242871839275222246405745257275088548364400416034343698204186575808495617; | |
| uint256 public constant ZERO_VALUE = 21663839004416932945382355908790599225266501822907911457504978515578255421292; // = keccak256("tornado") % FIELD_SIZE | |
| uint32 public levels; | |
| // the following variables are made public for easier testing and debugging and | |
| // are not supposed to be accessed in regular code | |
| bytes32[] public filledSubtrees; | |
| bytes32[] public zeros; | |
| uint32 public currentRootIndex = 0; | |
| uint32 public nextIndex = 0; | |
| uint32 public constant ROOT_HISTORY_SIZE = 100; | |
| bytes32[ROOT_HISTORY_SIZE] public roots; | |
| constructor(uint32 _treeLevels) public { | |
| require(_treeLevels > 0, "_treeLevels should be greater than zero"); | |
| require(_treeLevels < 32, "_treeLevels should be less than 32"); | |
| levels = _treeLevels; | |
| bytes32 currentZero = bytes32(ZERO_VALUE); | |
| zeros.push(currentZero); | |
| filledSubtrees.push(currentZero); | |
| for (uint32 i = 1; i < levels; i++) { | |
| currentZero = hashLeftRight(currentZero, currentZero); | |
| zeros.push(currentZero); | |
| filledSubtrees.push(currentZero); | |
| } | |
| roots[0] = hashLeftRight(currentZero, currentZero); | |
| } | |
| /** | |
| @dev Hash 2 tree leaves, returns MiMC(_left, _right) | |
| */ | |
| function hashLeftRight(bytes32 _left, bytes32 _right) public pure returns (bytes32) { | |
| require(uint256(_left) < FIELD_SIZE, "_left should be inside the field"); | |
| require(uint256(_right) < FIELD_SIZE, "_right should be inside the field"); | |
| uint256 R = uint256(_left); | |
| uint256 C = 0; | |
| (R, C) = Hasher.MiMCSponge(R, C); | |
| R = addmod(R, uint256(_right), FIELD_SIZE); | |
| (R, C) = Hasher.MiMCSponge(R, C); | |
| return bytes32(R); | |
| } | |
| function _insert(bytes32 _leaf) internal returns(uint32 index) { | |
| uint32 currentIndex = nextIndex; | |
| require(currentIndex != uint32(2)**levels, "Merkle tree is full. No more leafs can be added"); | |
| nextIndex += 1; | |
| bytes32 currentLevelHash = _leaf; | |
| bytes32 left; | |
| bytes32 right; | |
| for (uint32 i = 0; i < levels; i++) { | |
| if (currentIndex % 2 == 0) { | |
| left = currentLevelHash; | |
| right = zeros[i]; | |
| filledSubtrees[i] = currentLevelHash; | |
| } else { | |
| left = filledSubtrees[i]; | |
| right = currentLevelHash; | |
| } | |
| currentLevelHash = hashLeftRight(left, right); | |
| currentIndex /= 2; | |
| } | |
| currentRootIndex = (currentRootIndex + 1) % ROOT_HISTORY_SIZE; | |
| roots[currentRootIndex] = currentLevelHash; | |
| return nextIndex - 1; | |
| } | |
| /** | |
| @dev Whether the root is present in the root history | |
| */ | |
| function isKnownRoot(bytes32 _root) public view returns(bool) { | |
| if (_root == 0) { | |
| return false; | |
| } | |
| uint32 i = currentRootIndex; | |
| do { | |
| if (_root == roots[i]) { | |
| return true; | |
| } | |
| if (i == 0) { | |
| i = ROOT_HISTORY_SIZE; | |
| } | |
| i--; | |
| } while (i != currentRootIndex); | |
| return false; | |
| } | |
| /** | |
| @dev Returns the last root | |
| */ | |
| function getLastRoot() public view returns(bytes32) { | |
| return roots[currentRootIndex]; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://tornado.cash | |
| /* | |
| * d888888P dP a88888b. dP | |
| * 88 88 d8' `88 88 | |
| * 88 .d8888b. 88d888b. 88d888b. .d8888b. .d888b88 .d8888b. 88 .d8888b. .d8888b. 88d888b. | |
| * 88 88' `88 88' `88 88' `88 88' `88 88' `88 88' `88 88 88' `88 Y8ooooo. 88' `88 | |
| * 88 88. .88 88 88 88 88. .88 88. .88 88. .88 dP Y8. .88 88. .88 88 88 88 | |
| * dP `88888P' dP dP dP `88888P8 `88888P8 `88888P' 88 Y88888P' `88888P8 `88888P' dP dP | |
| * ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo | |
| */ | |
| pragma solidity 0.5.17; | |
| import "./MerkleTreeWithHistory.sol"; | |
| import "./Counter.sol"; | |
| import "@openzeppelin/contracts/math/SafeMath.sol"; | |
| import "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; | |
| contract IVerifier { | |
| function verifyProof(bytes memory _proof, uint256[6] memory _input) public returns(bool); | |
| } | |
| interface IReward { | |
| function stake(bytes32 _nullifierHash) external; | |
| function withdraw(bytes32 _nullifierHash, address payable _recipient) external; | |
| } | |
| contract Tornado is MerkleTreeWithHistory, ReentrancyGuard { | |
| using SafeMath for uint256; | |
| using Counters for Counters.Counter; | |
| Counters.Counter private _stakedCounter; | |
| uint256 public denomination; | |
| mapping(bytes32 => bool) public nullifierHashes; | |
| // we store all commitments just to prevent accidental deposits with the same commitment | |
| mapping(bytes32 => bool) public commitments; | |
| IVerifier public verifier; | |
| IReward public rewarder; | |
| mapping(address => mapping(bytes32 => bool)) public stakedNullifierHashes; | |
| mapping(bytes32 => address) public stakedNullifierHashOwner; | |
| // reserve pool address | |
| address public reserve; | |
| // operator can update snark verification key | |
| // after the final trusted setup ceremony operator rights are supposed to be transferred to zero address | |
| address public operator; | |
| modifier onlyOperator { | |
| require(msg.sender == operator, "Only operator can call this function."); | |
| _; | |
| } | |
| address public governance; | |
| modifier onlyGovernance { | |
| require(msg.sender == governance, "Only governance can call this function."); | |
| _; | |
| } | |
| uint public withdrawalFee = 50; | |
| uint constant public withdrawalMax = 10000; | |
| event Deposit(bytes32 indexed commitment, uint32 leafIndex, uint256 timestamp); | |
| event Withdrawal(address to, bytes32 nullifierHash, address indexed relayer, uint256 fee); | |
| event StakedWithdrawal(address indexed recipient, bytes32 nullifierHash, uint256 timestamp); | |
| event UnstakedWithdrawal(address indexed recipient, bytes32 nullifierHash); | |
| /** | |
| @dev The constructor | |
| @param _verifier the address of SNARK verifier for this contract | |
| @param _denomination transfer amount for each deposit | |
| @param _merkleTreeHeight the height of deposits' Merkle Tree | |
| @param _operator operator address (see operator comment above) | |
| */ | |
| constructor( | |
| IVerifier _verifier, | |
| uint256 _denomination, | |
| uint32 _merkleTreeHeight, | |
| address _operator, | |
| address _governance, | |
| address _reserve | |
| ) MerkleTreeWithHistory(_merkleTreeHeight) public { | |
| require(_denomination > 0, "denomination should be greater than 0"); | |
| verifier = _verifier; | |
| operator = _operator; | |
| governance = _governance; | |
| denomination = _denomination; | |
| reserve = _reserve; | |
| } | |
| /** | |
| @dev Deposit funds into the contract. The caller must send (for ETH) or approve (for ERC20) value equal to or `denomination` of this instance. | |
| @param _commitment the note commitment, which is PedersenHash(nullifier + secret) | |
| */ | |
| function deposit(bytes32 _commitment) external payable nonReentrant { | |
| require(!commitments[_commitment], "The commitment has been submitted"); | |
| uint32 insertedIndex = _insert(_commitment); | |
| commitments[_commitment] = true; | |
| _processDeposit(); | |
| emit Deposit(_commitment, insertedIndex, block.timestamp); | |
| } | |
| /** @dev this function is defined in a child contract */ | |
| function _processDeposit() internal; | |
| /** | |
| @dev Withdraw a deposit from the contract. `proof` is a zkSNARK proof data, and input is an array of circuit public inputs | |
| `input` array consists of: | |
| - merkle root of all deposits in the contract | |
| - hash of unique deposit nullifier to prevent double spends | |
| - the recipient of funds | |
| - optional fee that goes to the transaction sender (usually a relay) | |
| */ | |
| function withdraw(bytes calldata _proof, bytes32 _root, bytes32 _nullifierHash, address payable _recipient, address payable _relayer, uint256 _fee, uint256 _refund) external payable nonReentrant { | |
| uint256 _withdraw_fee = denomination.mul(withdrawalFee).div(withdrawalMax); | |
| require(_withdraw_fee + _fee <= denomination, "Fee exceeds transfer value"); | |
| require(!nullifierHashes[_nullifierHash], "The note has been already spent"); | |
| require(isKnownRoot(_root), "Cannot find your merkle root"); // Make sure to use a recent one | |
| require(verifier.verifyProof(_proof, [uint256(_root), uint256(_nullifierHash), uint256(_recipient), uint256(_relayer), _fee, _refund]), "Invalid withdraw proof"); | |
| nullifierHashes[_nullifierHash] = true; | |
| _processWithdraw(_recipient, _withdraw_fee, _relayer, _fee, _refund); | |
| emit Withdrawal(_recipient, _nullifierHash, _relayer, _fee); | |
| } | |
| function stakeWithdraw(bytes calldata _proof, bytes32 _root, bytes32 _nullifierHash, address payable _recipient) external payable nonReentrant { | |
| uint256 _withdraw_fee = denomination.mul(withdrawalFee).div(withdrawalMax); | |
| require(_withdraw_fee <= denomination, "Fee exceeds transfer value"); | |
| require(!nullifierHashes[_nullifierHash], "The note has been already spent"); | |
| require(isKnownRoot(_root), "Cannot find your merkle root"); // Make sure to use a recent one | |
| require(verifier.verifyProof(_proof, [uint256(_root), uint256(_nullifierHash), uint256(_recipient), uint256(address(0)), 0, 0]), "Invalid withdraw proof"); | |
| nullifierHashes[_nullifierHash] = true; | |
| rewarder.stake(_nullifierHash); | |
| stakedNullifierHashOwner[_nullifierHash] = _recipient; | |
| stakedNullifierHashes[_recipient][_nullifierHash] = true; | |
| _stakedCounter.increment(); | |
| emit StakedWithdrawal(_recipient, _nullifierHash, block.timestamp); | |
| } | |
| function unstakeAndWithdraw(bytes32 _nullifierHash) external payable nonReentrant { | |
| require(stakedNullifierHashes[msg.sender][_nullifierHash]); | |
| uint256 _withdraw_fee = denomination.mul(withdrawalFee).div(withdrawalMax); | |
| require(_withdraw_fee <= denomination, "Fee exceeds transfer value"); | |
| _processWithdraw(msg.sender, _withdraw_fee, address(0), 0, 0); | |
| emit Withdrawal(msg.sender, _nullifierHash, address(0), 0); | |
| rewarder.withdraw(_nullifierHash, msg.sender); | |
| stakedNullifierHashes[msg.sender][_nullifierHash] = false; | |
| _stakedCounter.decrement(); | |
| emit UnstakedWithdrawal(msg.sender, _nullifierHash); | |
| } | |
| /** @dev this function is defined in a child contract */ | |
| function _processWithdraw(address payable _recipient, uint256 _withdraw_fee, address payable _relayer, uint256 _fee, uint256 _refund) internal; | |
| /** @dev whether a note is already spent */ | |
| function isSpent(bytes32 _nullifierHash) public view returns(bool) { | |
| return nullifierHashes[_nullifierHash]; | |
| } | |
| /** @dev whether an array of notes is already spent */ | |
| function isSpentArray(bytes32[] calldata _nullifierHashes) external view returns(bool[] memory spent) { | |
| spent = new bool[](_nullifierHashes.length); | |
| for(uint i = 0; i < _nullifierHashes.length; i++) { | |
| if (isSpent(_nullifierHashes[i])) { | |
| spent[i] = true; | |
| } | |
| } | |
| } | |
| /** | |
| @dev allow operator to update SNARK verification keys. This is needed to update keys after the final trusted setup ceremony is held. | |
| After that operator rights are supposed to be transferred to zero address | |
| */ | |
| function updateVerifier(address _newVerifier) external onlyOperator { | |
| verifier = IVerifier(_newVerifier); | |
| } | |
| function updateRewarder(address _newRewarder) external onlyOperator { | |
| rewarder = IReward(_newRewarder); | |
| } | |
| /** @dev operator can change his address */ | |
| function changeOperator(address _newOperator) external onlyOperator { | |
| operator = _newOperator; | |
| } | |
| /** @dev get current staked count */ | |
| function getStakedCount() public view returns (uint256) { | |
| return _stakedCounter.current(); | |
| } | |
| /** @dev set governance address */ | |
| function setGovernance(address _governance) external onlyGovernance { | |
| governance = _governance; | |
| } | |
| /** @dev set withdraw fee */ | |
| function setWithdrawalFee(uint _withdrawalFee) external onlyGovernance { | |
| withdrawalFee = _withdrawalFee; | |
| } | |
| function setReserveAddress(address _newReserve) external onlyGovernance { | |
| reserve = _newReserve; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| *Submitted for verification at Etherscan.io on 2020-07-17 | |
| */ | |
| /* | |
| ____ __ __ __ _ | |
| / __/__ __ ___ / /_ / / ___ / /_ (_)__ __ | |
| _\ \ / // // _ \/ __// _ \/ -_)/ __// / \ \ / | |
| /___/ \_, //_//_/\__//_//_/\__/ \__//_/ /_\_\ | |
| /___/ | |
| * Synthetix: YFIRewards.sol | |
| * | |
| * Docs: https://docs.synthetix.io/ | |
| * | |
| * | |
| * MIT License | |
| * =========== | |
| * | |
| * Copyright (c) 2020 Synthetix | |
| * | |
| * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| * of this software and associated documentation files (the "Software"), to deal | |
| * in the Software without restriction, including without limitation the rights | |
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| * copies of the Software, and to permit persons to whom the Software is | |
| * furnished to do so, subject to the following conditions: | |
| * | |
| * The above copyright notice and this permission notice shall be included in all | |
| * copies or substantial portions of the Software. | |
| * | |
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| */ | |
| // File: @openzeppelin/contracts/math/Math.sol | |
| pragma solidity ^0.5.0; | |
| /** | |
| * @dev Standard math utilities missing in the Solidity language. | |
| */ | |
| library Math { | |
| /** | |
| * @dev Returns the largest of two numbers. | |
| */ | |
| function max(uint256 a, uint256 b) internal pure returns (uint256) { | |
| return a >= b ? a : b; | |
| } | |
| /** | |
| * @dev Returns the smallest of two numbers. | |
| */ | |
| function min(uint256 a, uint256 b) internal pure returns (uint256) { | |
| return a < b ? a : b; | |
| } | |
| /** | |
| * @dev Returns the average of two numbers. The result is rounded towards | |
| * zero. | |
| */ | |
| function average(uint256 a, uint256 b) internal pure returns (uint256) { | |
| // (a + b) / 2 can overflow, so we distribute | |
| return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2); | |
| } | |
| } | |
| // File: @openzeppelin/contracts/math/SafeMath.sol | |
| pragma solidity ^0.5.0; | |
| /** | |
| * @dev Wrappers over Solidity's arithmetic operations with added overflow | |
| * checks. | |
| * | |
| * Arithmetic operations in Solidity wrap on overflow. This can easily result | |
| * in bugs, because programmers usually assume that an overflow raises an | |
| * error, which is the standard behavior in high level programming languages. | |
| * `SafeMath` restores this intuition by reverting the transaction when an | |
| * operation overflows. | |
| * | |
| * Using this library instead of the unchecked operations eliminates an entire | |
| * class of bugs, so it's recommended to use it always. | |
| */ | |
| library SafeMath { | |
| /** | |
| * @dev Returns the addition of two unsigned integers, reverting on | |
| * overflow. | |
| * | |
| * Counterpart to Solidity's `+` operator. | |
| * | |
| * Requirements: | |
| * - Addition cannot overflow. | |
| */ | |
| function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
| uint256 c = a + b; | |
| require(c >= a, "SafeMath: addition overflow"); | |
| return c; | |
| } | |
| /** | |
| * @dev Returns the subtraction of two unsigned integers, reverting on | |
| * overflow (when the result is negative). | |
| * | |
| * Counterpart to Solidity's `-` operator. | |
| * | |
| * Requirements: | |
| * - Subtraction cannot overflow. | |
| */ | |
| function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
| return sub(a, b, "SafeMath: subtraction overflow"); | |
| } | |
| /** | |
| * @dev Returns the subtraction of two unsigned integers, reverting with custom message on | |
| * overflow (when the result is negative). | |
| * | |
| * Counterpart to Solidity's `-` operator. | |
| * | |
| * Requirements: | |
| * - Subtraction cannot overflow. | |
| * | |
| * _Available since v2.4.0._ | |
| */ | |
| function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
| require(b <= a, errorMessage); | |
| uint256 c = a - b; | |
| return c; | |
| } | |
| /** | |
| * @dev Returns the multiplication of two unsigned integers, reverting on | |
| * overflow. | |
| * | |
| * Counterpart to Solidity's `*` operator. | |
| * | |
| * Requirements: | |
| * - Multiplication cannot overflow. | |
| */ | |
| function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
| // Gas optimization: this is cheaper than requiring 'a' not being zero, but the | |
| // benefit is lost if 'b' is also tested. | |
| // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 | |
| if (a == 0) { | |
| return 0; | |
| } | |
| uint256 c = a * b; | |
| require(c / a == b, "SafeMath: multiplication overflow"); | |
| return c; | |
| } | |
| /** | |
| * @dev Returns the integer division of two unsigned integers. Reverts on | |
| * division by zero. The result is rounded towards zero. | |
| * | |
| * Counterpart to Solidity's `/` operator. Note: this function uses a | |
| * `revert` opcode (which leaves remaining gas untouched) while Solidity | |
| * uses an invalid opcode to revert (consuming all remaining gas). | |
| * | |
| * Requirements: | |
| * - The divisor cannot be zero. | |
| */ | |
| function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
| return div(a, b, "SafeMath: division by zero"); | |
| } | |
| /** | |
| * @dev Returns the integer division of two unsigned integers. Reverts with custom message on | |
| * division by zero. The result is rounded towards zero. | |
| * | |
| * Counterpart to Solidity's `/` operator. Note: this function uses a | |
| * `revert` opcode (which leaves remaining gas untouched) while Solidity | |
| * uses an invalid opcode to revert (consuming all remaining gas). | |
| * | |
| * Requirements: | |
| * - The divisor cannot be zero. | |
| * | |
| * _Available since v2.4.0._ | |
| */ | |
| function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
| // Solidity only automatically asserts when dividing by 0 | |
| require(b > 0, errorMessage); | |
| uint256 c = a / b; | |
| // assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
| return c; | |
| } | |
| /** | |
| * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), | |
| * Reverts when dividing by zero. | |
| * | |
| * Counterpart to Solidity's `%` operator. This function uses a `revert` | |
| * opcode (which leaves remaining gas untouched) while Solidity uses an | |
| * invalid opcode to revert (consuming all remaining gas). | |
| * | |
| * Requirements: | |
| * - The divisor cannot be zero. | |
| */ | |
| function mod(uint256 a, uint256 b) internal pure returns (uint256) { | |
| return mod(a, b, "SafeMath: modulo by zero"); | |
| } | |
| /** | |
| * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), | |
| * Reverts with custom message when dividing by zero. | |
| * | |
| * Counterpart to Solidity's `%` operator. This function uses a `revert` | |
| * opcode (which leaves remaining gas untouched) while Solidity uses an | |
| * invalid opcode to revert (consuming all remaining gas). | |
| * | |
| * Requirements: | |
| * - The divisor cannot be zero. | |
| * | |
| * _Available since v2.4.0._ | |
| */ | |
| function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
| require(b != 0, errorMessage); | |
| return a % b; | |
| } | |
| } | |
| // File: @openzeppelin/contracts/GSN/Context.sol | |
| pragma solidity ^0.5.0; | |
| /* | |
| * @dev Provides information about the current execution context, including the | |
| * sender of the transaction and its data. While these are generally available | |
| * via msg.sender and msg.data, they should not be accessed in such a direct | |
| * manner, since when dealing with GSN meta-transactions the account sending and | |
| * paying for execution may not be the actual sender (as far as an application | |
| * is concerned). | |
| * | |
| * This contract is only required for intermediate, library-like contracts. | |
| */ | |
| contract Context { | |
| // Empty internal constructor, to prevent people from mistakenly deploying | |
| // an instance of this contract, which should be used via inheritance. | |
| constructor () internal { } | |
| // solhint-disable-previous-line no-empty-blocks | |
| function _msgSender() internal view returns (address payable) { | |
| return msg.sender; | |
| } | |
| function _msgData() internal view returns (bytes memory) { | |
| this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691 | |
| return msg.data; | |
| } | |
| } | |
| // File: @openzeppelin/contracts/ownership/Ownable.sol | |
| pragma solidity ^0.5.0; | |
| /** | |
| * @dev Contract module which provides a basic access control mechanism, where | |
| * there is an account (an owner) that can be granted exclusive access to | |
| * specific functions. | |
| * | |
| * This module is used through inheritance. It will make available the modifier | |
| * `onlyOwner`, which can be applied to your functions to restrict their use to | |
| * the owner. | |
| */ | |
| contract Ownable is Context { | |
| address private _owner; | |
| event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); | |
| /** | |
| * @dev Initializes the contract setting the deployer as the initial owner. | |
| */ | |
| constructor () internal { | |
| _owner = _msgSender(); | |
| emit OwnershipTransferred(address(0), _owner); | |
| } | |
| /** | |
| * @dev Returns the address of the current owner. | |
| */ | |
| function owner() public view returns (address) { | |
| return _owner; | |
| } | |
| /** | |
| * @dev Throws if called by any account other than the owner. | |
| */ | |
| modifier onlyOwner() { | |
| require(isOwner(), "Ownable: caller is not the owner"); | |
| _; | |
| } | |
| /** | |
| * @dev Returns true if the caller is the current owner. | |
| */ | |
| function isOwner() public view returns (bool) { | |
| return _msgSender() == _owner; | |
| } | |
| /** | |
| * @dev Leaves the contract without owner. It will not be possible to call | |
| * `onlyOwner` functions anymore. Can only be called by the current owner. | |
| * | |
| * NOTE: Renouncing ownership will leave the contract without an owner, | |
| * thereby removing any functionality that is only available to the owner. | |
| */ | |
| function renounceOwnership() public onlyOwner { | |
| emit OwnershipTransferred(_owner, address(0)); | |
| _owner = address(0); | |
| } | |
| /** | |
| * @dev Transfers ownership of the contract to a new account (`newOwner`). | |
| * Can only be called by the current owner. | |
| */ | |
| function transferOwnership(address newOwner) public onlyOwner { | |
| _transferOwnership(newOwner); | |
| } | |
| /** | |
| * @dev Transfers ownership of the contract to a new account (`newOwner`). | |
| */ | |
| function _transferOwnership(address newOwner) internal { | |
| require(newOwner != address(0), "Ownable: new owner is the zero address"); | |
| emit OwnershipTransferred(_owner, newOwner); | |
| _owner = newOwner; | |
| } | |
| } | |
| // File: @openzeppelin/contracts/token/ERC20/IERC20.sol | |
| pragma solidity ^0.5.0; | |
| /** | |
| * @dev Interface of the ERC20 standard as defined in the EIP. Does not include | |
| * the optional functions; to access them see {ERC20Detailed}. | |
| */ | |
| interface IERC20 { | |
| /** | |
| * @dev Returns the amount of tokens in existence. | |
| */ | |
| function totalSupply() external view returns (uint256); | |
| /** | |
| * @dev Returns the amount of tokens owned by `account`. | |
| */ | |
| function balanceOf(address account) external view returns (uint256); | |
| /** | |
| * @dev Moves `amount` tokens from the caller's account to `recipient`. | |
| * | |
| * Returns a boolean value indicating whether the operation succeeded. | |
| * | |
| * Emits a {Transfer} event. | |
| */ | |
| function transfer(address recipient, uint256 amount) external returns (bool); | |
| /** | |
| * @dev Returns the remaining number of tokens that `spender` will be | |
| * allowed to spend on behalf of `owner` through {transferFrom}. This is | |
| * zero by default. | |
| * | |
| * This value changes when {approve} or {transferFrom} are called. | |
| */ | |
| function allowance(address owner, address spender) external view returns (uint256); | |
| /** | |
| * @dev Sets `amount` as the allowance of `spender` over the caller's tokens. | |
| * | |
| * Returns a boolean value indicating whether the operation succeeded. | |
| * | |
| * IMPORTANT: Beware that changing an allowance with this method brings the risk | |
| * that someone may use both the old and the new allowance by unfortunate | |
| * transaction ordering. One possible solution to mitigate this race | |
| * condition is to first reduce the spender's allowance to 0 and set the | |
| * desired value afterwards: | |
| * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 | |
| * | |
| * Emits an {Approval} event. | |
| */ | |
| function approve(address spender, uint256 amount) external returns (bool); | |
| /** | |
| * @dev Moves `amount` tokens from `sender` to `recipient` using the | |
| * allowance mechanism. `amount` is then deducted from the caller's | |
| * allowance. | |
| * | |
| * Returns a boolean value indicating whether the operation succeeded. | |
| * | |
| * Emits a {Transfer} event. | |
| */ | |
| function transferFrom(address sender, address recipient, uint256 amount) external returns (bool); | |
| /** | |
| * @dev Emitted when `value` tokens are moved from one account (`from`) to | |
| * another (`to`). | |
| * | |
| * Note that `value` may be zero. | |
| */ | |
| event Transfer(address indexed from, address indexed to, uint256 value); | |
| /** | |
| * @dev Emitted when the allowance of a `spender` for an `owner` is set by | |
| * a call to {approve}. `value` is the new allowance. | |
| */ | |
| event Approval(address indexed owner, address indexed spender, uint256 value); | |
| } | |
| // File: @openzeppelin/contracts/utils/Address.sol | |
| pragma solidity ^0.5.5; | |
| /** | |
| * @dev Collection of functions related to the address type | |
| */ | |
| library Address { | |
| /** | |
| * @dev Returns true if `account` is a contract. | |
| * | |
| * This test is non-exhaustive, and there may be false-negatives: during the | |
| * execution of a contract's constructor, its address will be reported as | |
| * not containing 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. | |
| */ | |
| 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. | |
| // 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 != 0x0 && codehash != accountHash); | |
| } | |
| /** | |
| * @dev Converts an `address` into `address payable`. Note that this is | |
| * simply a type cast: the actual underlying value is not changed. | |
| * | |
| * _Available since v2.4.0._ | |
| */ | |
| function toPayable(address account) internal pure returns (address payable) { | |
| return address(uint160(account)); | |
| } | |
| /** | |
| * @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]. | |
| * | |
| * _Available since v2.4.0._ | |
| */ | |
| function sendValue(address payable recipient, uint256 amount) internal { | |
| require(address(this).balance >= amount, "Address: insufficient balance"); | |
| // solhint-disable-next-line avoid-call-value | |
| (bool success, ) = recipient.call.value(amount)(""); | |
| require(success, "Address: unable to send value, recipient may have reverted"); | |
| } | |
| } | |
| // File: @openzeppelin/contracts/token/ERC20/SafeERC20.sol | |
| pragma solidity ^0.5.0; | |
| /** | |
| * @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"); | |
| } | |
| } | |
| } | |
| // File: contracts/IRewardDistributionRecipient.sol | |
| pragma solidity ^0.5.0; | |
| contract IRewardDistributionRecipient is Ownable { | |
| address rewardDistribution; | |
| function notifyRewardAmount(uint256 reward) external; | |
| modifier onlyRewardDistribution() { | |
| require(_msgSender() == rewardDistribution, "Caller is not reward distribution"); | |
| _; | |
| } | |
| function setRewardDistribution(address _rewardDistribution) | |
| external | |
| onlyOwner | |
| { | |
| rewardDistribution = _rewardDistribution; | |
| } | |
| } | |
| // File: contracts/CurveRewards.sol | |
| pragma solidity ^0.5.0; | |
| contract LPTokenWrapper { | |
| using SafeMath for uint256; | |
| using SafeERC20 for IERC20; | |
| IERC20 public rewardLP; | |
| uint256 private _totalSupply; | |
| mapping(address => uint256) private _balances; | |
| constructor(IERC20 _rewardLP) public { | |
| rewardLP = _rewardLP; | |
| } | |
| function totalSupply() public view returns (uint256) { | |
| return _totalSupply; | |
| } | |
| function balanceOf(address account) public view returns (uint256) { | |
| return _balances[account]; | |
| } | |
| function stake(uint256 amount) public { | |
| _totalSupply = _totalSupply.add(amount); | |
| _balances[msg.sender] = _balances[msg.sender].add(amount); | |
| rewardLP.safeTransferFrom(msg.sender, address(this), amount); | |
| } | |
| function withdraw(uint256 amount) public { | |
| _totalSupply = _totalSupply.sub(amount); | |
| _balances[msg.sender] = _balances[msg.sender].sub(amount); | |
| rewardLP.safeTransfer(msg.sender, amount); | |
| } | |
| } | |
| contract UniswapLPReward is LPTokenWrapper, IRewardDistributionRecipient { | |
| IERC20 public typhoon; | |
| uint256 public constant DURATION = 7 days; | |
| uint256 public periodFinish = 0; | |
| uint256 public rewardRate = 0; | |
| uint256 public lastUpdateTime; | |
| uint256 public rewardPerTokenStored; | |
| mapping(address => uint256) public userRewardPerTokenPaid; | |
| mapping(address => uint256) public rewards; | |
| event RewardAdded(uint256 reward); | |
| event Staked(address indexed user, uint256 amount); | |
| event Withdrawn(address indexed user, uint256 amount); | |
| event RewardPaid(address indexed user, uint256 reward); | |
| constructor(IERC20 _typhoon, IERC20 _rewardLP) LPTokenWrapper(_rewardLP) public { | |
| typhoon = _typhoon; | |
| } | |
| modifier updateReward(address account) { | |
| rewardPerTokenStored = rewardPerToken(); | |
| lastUpdateTime = lastTimeRewardApplicable(); | |
| if (account != address(0)) { | |
| rewards[account] = earned(account); | |
| userRewardPerTokenPaid[account] = rewardPerTokenStored; | |
| } | |
| _; | |
| } | |
| function lastTimeRewardApplicable() public view returns (uint256) { | |
| return Math.min(block.timestamp, periodFinish); | |
| } | |
| function rewardPerToken() public view returns (uint256) { | |
| if (totalSupply() == 0) { | |
| return rewardPerTokenStored; | |
| } | |
| return | |
| rewardPerTokenStored.add( | |
| lastTimeRewardApplicable() | |
| .sub(lastUpdateTime) | |
| .mul(rewardRate) | |
| .mul(1e18) | |
| .div(totalSupply()) | |
| ); | |
| } | |
| function earned(address account) public view returns (uint256) { | |
| return | |
| balanceOf(account) | |
| .mul(rewardPerToken().sub(userRewardPerTokenPaid[account])) | |
| .div(1e18) | |
| .add(rewards[account]); | |
| } | |
| // stake visibility is public as overriding LPTokenWrapper's stake() function | |
| function stake(uint256 amount) public updateReward(msg.sender) { | |
| require(amount > 0, "Cannot stake 0"); | |
| super.stake(amount); | |
| emit Staked(msg.sender, amount); | |
| } | |
| function withdraw(uint256 amount) public updateReward(msg.sender) { | |
| require(amount > 0, "Cannot withdraw 0"); | |
| super.withdraw(amount); | |
| emit Withdrawn(msg.sender, amount); | |
| } | |
| function exit() external { | |
| withdraw(balanceOf(msg.sender)); | |
| getReward(); | |
| } | |
| function getReward() public updateReward(msg.sender) { | |
| uint256 reward = earned(msg.sender); | |
| if (reward > 0) { | |
| rewards[msg.sender] = 0; | |
| typhoon.safeTransfer(msg.sender, reward); | |
| emit RewardPaid(msg.sender, reward); | |
| } | |
| } | |
| function notifyRewardAmount(uint256 reward) | |
| external | |
| onlyRewardDistribution | |
| updateReward(address(0)) | |
| { | |
| if (block.timestamp >= periodFinish) { | |
| rewardRate = reward.div(DURATION); | |
| } else { | |
| uint256 remaining = periodFinish.sub(block.timestamp); | |
| uint256 leftover = remaining.mul(rewardRate); | |
| rewardRate = reward.add(leftover).div(DURATION); | |
| } | |
| lastUpdateTime = block.timestamp; | |
| periodFinish = block.timestamp.add(DURATION); | |
| emit RewardAdded(reward); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Copyright 2017 Christian Reitwiessner | |
| // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| // | |
| // 2019 OKIMS | |
| // ported to solidity 0.5 | |
| // fixed linter warnings | |
| // added requiere error messages | |
| // | |
| pragma solidity ^0.5.0; | |
| library Pairing { | |
| struct G1Point { | |
| uint X; | |
| uint Y; | |
| } | |
| // Encoding of field elements is: X[0] * z + X[1] | |
| struct G2Point { | |
| uint[2] X; | |
| uint[2] Y; | |
| } | |
| /// @return the generator of G1 | |
| function P1() internal pure returns (G1Point memory) { | |
| return G1Point(1, 2); | |
| } | |
| /// @return the generator of G2 | |
| function P2() internal pure returns (G2Point memory) { | |
| // Original code point | |
| return G2Point( | |
| [11559732032986387107991004021392285783925812861821192530917403151452391805634, | |
| 10857046999023057135944570762232829481370756359578518086990519993285655852781], | |
| [4082367875863433681332203403145435568316851327593401208105741076214120093531, | |
| 8495653923123431417604973247489272438418190587263600148770280649306958101930] | |
| ); | |
| /* | |
| // Changed by Jordi point | |
| return G2Point( | |
| [10857046999023057135944570762232829481370756359578518086990519993285655852781, | |
| 11559732032986387107991004021392285783925812861821192530917403151452391805634], | |
| [8495653923123431417604973247489272438418190587263600148770280649306958101930, | |
| 4082367875863433681332203403145435568316851327593401208105741076214120093531] | |
| ); | |
| */ | |
| } | |
| /// @return the negation of p, i.e. p.addition(p.negate()) should be zero. | |
| function negate(G1Point memory p) internal pure returns (G1Point memory) { | |
| // The prime q in the base field F_q for G1 | |
| uint q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; | |
| if (p.X == 0 && p.Y == 0) | |
| return G1Point(0, 0); | |
| return G1Point(p.X, q - (p.Y % q)); | |
| } | |
| /// @return the sum of two points of G1 | |
| function addition(G1Point memory p1, G1Point memory p2) internal view returns (G1Point memory r) { | |
| uint[4] memory input; | |
| input[0] = p1.X; | |
| input[1] = p1.Y; | |
| input[2] = p2.X; | |
| input[3] = p2.Y; | |
| bool success; | |
| // solium-disable-next-line security/no-inline-assembly | |
| assembly { | |
| success := staticcall(sub(gas, 2000), 6, input, 0xc0, r, 0x60) | |
| // Use "invalid" to make gas estimation work | |
| switch success case 0 { invalid() } | |
| } | |
| require(success,"pairing-add-failed"); | |
| } | |
| /// @return the product of a point on G1 and a scalar, i.e. | |
| /// p == p.scalar_mul(1) and p.addition(p) == p.scalar_mul(2) for all points p. | |
| function scalar_mul(G1Point memory p, uint s) internal view returns (G1Point memory r) { | |
| uint[3] memory input; | |
| input[0] = p.X; | |
| input[1] = p.Y; | |
| input[2] = s; | |
| bool success; | |
| // solium-disable-next-line security/no-inline-assembly | |
| assembly { | |
| success := staticcall(sub(gas, 2000), 7, input, 0x80, r, 0x60) | |
| // Use "invalid" to make gas estimation work | |
| switch success case 0 { invalid() } | |
| } | |
| require (success,"pairing-mul-failed"); | |
| } | |
| /// @return the result of computing the pairing check | |
| /// e(p1[0], p2[0]) * .... * e(p1[n], p2[n]) == 1 | |
| /// For example pairing([P1(), P1().negate()], [P2(), P2()]) should | |
| /// return true. | |
| function pairing(G1Point[] memory p1, G2Point[] memory p2) internal view returns (bool) { | |
| require(p1.length == p2.length,"pairing-lengths-failed"); | |
| uint elements = p1.length; | |
| uint inputSize = elements * 6; | |
| uint[] memory input = new uint[](inputSize); | |
| for (uint i = 0; i < elements; i++) | |
| { | |
| input[i * 6 + 0] = p1[i].X; | |
| input[i * 6 + 1] = p1[i].Y; | |
| input[i * 6 + 2] = p2[i].X[0]; | |
| input[i * 6 + 3] = p2[i].X[1]; | |
| input[i * 6 + 4] = p2[i].Y[0]; | |
| input[i * 6 + 5] = p2[i].Y[1]; | |
| } | |
| uint[1] memory out; | |
| bool success; | |
| // solium-disable-next-line security/no-inline-assembly | |
| assembly { | |
| success := staticcall(sub(gas, 2000), 8, add(input, 0x20), mul(inputSize, 0x20), out, 0x20) | |
| // Use "invalid" to make gas estimation work | |
| switch success case 0 { invalid() } | |
| } | |
| require(success,"pairing-opcode-failed"); | |
| return out[0] != 0; | |
| } | |
| /// Convenience method for a pairing check for two pairs. | |
| function pairingProd2(G1Point memory a1, G2Point memory a2, G1Point memory b1, G2Point memory b2) internal view returns (bool) { | |
| G1Point[] memory p1 = new G1Point[](2); | |
| G2Point[] memory p2 = new G2Point[](2); | |
| p1[0] = a1; | |
| p1[1] = b1; | |
| p2[0] = a2; | |
| p2[1] = b2; | |
| return pairing(p1, p2); | |
| } | |
| /// Convenience method for a pairing check for three pairs. | |
| function pairingProd3( | |
| G1Point memory a1, G2Point memory a2, | |
| G1Point memory b1, G2Point memory b2, | |
| G1Point memory c1, G2Point memory c2 | |
| ) internal view returns (bool) { | |
| G1Point[] memory p1 = new G1Point[](3); | |
| G2Point[] memory p2 = new G2Point[](3); | |
| p1[0] = a1; | |
| p1[1] = b1; | |
| p1[2] = c1; | |
| p2[0] = a2; | |
| p2[1] = b2; | |
| p2[2] = c2; | |
| return pairing(p1, p2); | |
| } | |
| /// Convenience method for a pairing check for four pairs. | |
| function pairingProd4( | |
| G1Point memory a1, G2Point memory a2, | |
| G1Point memory b1, G2Point memory b2, | |
| G1Point memory c1, G2Point memory c2, | |
| G1Point memory d1, G2Point memory d2 | |
| ) internal view returns (bool) { | |
| G1Point[] memory p1 = new G1Point[](4); | |
| G2Point[] memory p2 = new G2Point[](4); | |
| p1[0] = a1; | |
| p1[1] = b1; | |
| p1[2] = c1; | |
| p1[3] = d1; | |
| p2[0] = a2; | |
| p2[1] = b2; | |
| p2[2] = c2; | |
| p2[3] = d2; | |
| return pairing(p1, p2); | |
| } | |
| } | |
| contract Verifier { | |
| using Pairing for *; | |
| struct VerifyingKey { | |
| Pairing.G1Point alfa1; | |
| Pairing.G2Point beta2; | |
| Pairing.G2Point gamma2; | |
| Pairing.G2Point delta2; | |
| Pairing.G1Point[] IC; | |
| } | |
| struct Proof { | |
| Pairing.G1Point A; | |
| Pairing.G2Point B; | |
| Pairing.G1Point C; | |
| } | |
| function verifyingKey() internal pure returns (VerifyingKey memory vk) { | |
| vk.alfa1 = Pairing.G1Point(2982144582572309378677994376322524767030016467963387837333701159275976850752,10473363291039546027382818162552615619859763122194868053924963205554982703452); | |
| vk.beta2 = Pairing.G2Point([10451667558789560907763849179571574159612949864217868791136917903491757528487,15036432393456626996638634525919497941917502353558735153049382467955810683741], [10344765293544166558768325939163365637784921877748677994617664982244901924771,6102126183896382625488679116785030248553168865690685729492441634804487294683]); | |
| vk.gamma2 = Pairing.G2Point([11032259708608997025562005926790798403284299171929179535187002828269003704464,15280366315674202353947098832315081914025883646884792057439755093239538299373], [21478106731851857075382661435451121904744705854136455142798743280669497273372,14155988994621945168460345460085095553039441387338846251304178245065772720249]); | |
| vk.delta2 = Pairing.G2Point([3508913516296918463116877845497252453892920738661007972046611123286792069262,6007084858842878233461033978401793989818199519396634537663140313821221442763], [9057111366286925084738105236580240956364221063380526959240455250108502058712,12880740444922655136059614673124913632940756259102594270018907373033428828422]); | |
| vk.IC = new Pairing.G1Point[](7); | |
| vk.IC[0] = Pairing.G1Point(12374969992659386171412367953630561552889572723576850887299094638775225101371,2714948481852269114266512487495706402006906978199689591196086975179718952093); | |
| vk.IC[1] = Pairing.G1Point(15300322435620162539543269988321729577069516781751203460709754655204532231783,1074952274165165140953261763783297728811171068992748199909447586351190442505); | |
| vk.IC[2] = Pairing.G1Point(4519043602761674922595788001815748113204396971978712963405400808302293579552,20106064383535002666086111887802524435921153279992504843538604151107030335542); | |
| vk.IC[3] = Pairing.G1Point(12210470874483895558754167860295493103356781263440210020199226576054917159269,305387947335425167491417641244798279872827473014793922072301428489985505652); | |
| vk.IC[4] = Pairing.G1Point(10578735549044333095051252837410792055628389999708876263730244180380856727433,4880576849951713829693964778644145674932810699759557840217959649663585688896); | |
| vk.IC[5] = Pairing.G1Point(13927221933831259657860969783142780896767396200158512212668432414160377834606,4398421108880316706015068152667548490339882264678462205844238062056717382116); | |
| vk.IC[6] = Pairing.G1Point(17241814867991912407876751573133331205998558769984413385276930508873990265435,16334956899543109305490651031794803145511648945557516809081556298767505463780); | |
| } | |
| function verify(uint[] memory input, Proof memory proof) internal view returns (uint) { | |
| uint256 snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617; | |
| VerifyingKey memory vk = verifyingKey(); | |
| require(input.length + 1 == vk.IC.length,"verifier-bad-input"); | |
| // Compute the linear combination vk_x | |
| Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0); | |
| for (uint i = 0; i < input.length; i++) { | |
| require(input[i] < snark_scalar_field,"verifier-gte-snark-scalar-field"); | |
| vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.IC[i + 1], input[i])); | |
| } | |
| vk_x = Pairing.addition(vk_x, vk.IC[0]); | |
| if (!Pairing.pairingProd4( | |
| Pairing.negate(proof.A), proof.B, | |
| vk.alfa1, vk.beta2, | |
| vk_x, vk.gamma2, | |
| proof.C, vk.delta2 | |
| )) return 1; | |
| return 0; | |
| } | |
| function verifyProof( | |
| uint[2] memory a, | |
| uint[2][2] memory b, | |
| uint[2] memory c, | |
| uint[6] memory input | |
| ) public view returns (bool r) { | |
| Proof memory proof; | |
| proof.A = Pairing.G1Point(a[0], a[1]); | |
| proof.B = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]); | |
| proof.C = Pairing.G1Point(c[0], c[1]); | |
| uint[] memory inputValues = new uint[](input.length); | |
| for(uint i = 0; i < input.length; i++){ | |
| inputValues[i] = input[i]; | |
| } | |
| if (verify(inputValues, proof) == 0) { | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } | |
| function verifyProof(bytes calldata proof, uint[6] calldata inputs) external view returns (bool r) { | |
| // solidity does not support decoding uint[2][2] yet | |
| (uint[2] memory a, uint[2] memory b1, uint[2] memory b2, uint[2] memory c) = abi.decode(proof, (uint[2], uint[2], uint[2], uint[2])); | |
| return verifyProof(a, [b1, b2], c, inputs); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pragma solidity 0.5.17; | |
| import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol"; | |
| import "@openzeppelin/contracts/ownership/Ownable.sol"; | |
| import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | |
| contract WithdrawWhiteList is Ownable { | |
| using SafeERC20 for IERC20; | |
| mapping(address => bool) public withdrawWhiteListMap; | |
| address[] withdrawWhiteList; | |
| constructor(address[] memory _withdrawWhiteList) public { | |
| withdrawWhiteList = _withdrawWhiteList; | |
| for (uint i=0; i < withdrawWhiteList.length; i++) { | |
| withdrawWhiteListMap[withdrawWhiteList[i]] = true; | |
| } | |
| } | |
| function withdraw(IERC20 token, uint amount, address toAddress) public onlyOwner { | |
| require(withdrawWhiteListMap[toAddress], "Can only withdraw to whitelist address."); | |
| token.safeTransfer(toAddress, amount); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment