Contract Name:
PerpRewardVesting
Contract Source Code:
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.9;
pragma experimental ABIEncoderV2;
import { IERC20 } from "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol";
import { SafeMath } from "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol";
import { MerkleRedeemUpgradeSafe } from "./Balancer/MerkleRedeemUpgradeSafe.sol";
import { Decimal } from "../utils/Decimal.sol";
import { DecimalERC20 } from "../utils/DecimalERC20.sol";
import { BlockContext } from "../utils/BlockContext.sol";
contract PerpRewardVesting is MerkleRedeemUpgradeSafe, BlockContext {
using Decimal for Decimal.decimal;
using SafeMath for uint256;
//**********************************************************//
// The below state variables can not change the order //
//**********************************************************//
// {weekMerkleRootsIndex: timestamp}
mapping(uint256 => uint256) public merkleRootTimestampMap;
// array of weekMerkleRootsIndex
uint256[] public merkleRootIndexes;
uint256 public vestingPeriod;
//**********************************************************//
// The above state variables can not change the order //
//**********************************************************//
//◥◤◥◤◥◤◥◤◥◤◥◤◥◤◥◤ add state variables below ◥◤◥◤◥◤◥◤◥◤◥◤◥◤◥◤//
//◢◣◢◣◢◣◢◣◢◣◢◣◢◣◢◣ add state variable, ables above ◢◣◢◣◢◣◢◣◢◣◢◣◢◣◢◣//
uint256[50] private __gap;
function initialize(IERC20 _token, uint256 _vestingPeriod) external initializer {
require(address(_token) != address(0), "Invalid input");
__MerkleRedeem_init(_token);
vestingPeriod = _vestingPeriod;
}
function claimWeeks(address _account, Claim[] memory _claims) public virtual override {
for (uint256 i; i < _claims.length; i++) {
claimWeek(_account, _claims[i].week, _claims[i].balance, _claims[i].merkleProof);
}
}
function claimWeek(
address _account,
uint256 _week,
uint256 _claimedBalance,
bytes32[] memory _merkleProof
) public virtual override {
//
// +----------------+
// | vesting period |
// +----------------+----------+
// | vesting period | |
// ---------+------+---+-----+------+---+
// | | | now |
// | week2 | merkleRootTimestampMap[week1]
// | |
// week1 merkleRootTimestampMap[week1]
//
// week1 -> claimable
// week2 -> non-claimable
//
require(
_blockTimestamp() >= merkleRootTimestampMap[_week] && merkleRootTimestampMap[_week] > 0,
"Invalid claim"
);
super.claimWeek(_account, _week, _claimedBalance, _merkleProof);
}
function seedAllocations(
uint256 _week,
bytes32 _merkleRoot,
uint256 _totalAllocation
) public override onlyOwner {
super.seedAllocations(_week, _merkleRoot, _totalAllocation);
merkleRootTimestampMap[_week] = _blockTimestamp().add(vestingPeriod);
merkleRootIndexes.push(_week);
}
//
// INTERNAL
//
function getLengthOfMerkleRoots() external view returns (uint256) {
return merkleRootIndexes.length;
}
}
// source: https://github.com/balancer-labs/erc20-redeemable/blob/master/merkle/contracts/MerkleRedeem.sol
// changes:
// 1. add license and update solidity version to 0.6.9
// 2. make it upgradeable
// 3. add virtual modifier in claim functions
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.9;
pragma experimental ABIEncoderV2;
import { MerkleProof } from "@openzeppelin/contracts-ethereum-package/contracts/cryptography/MerkleProof.sol";
import { IERC20 } from "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol";
import { PerpFiOwnableUpgrade } from "../../utils/PerpFiOwnableUpgrade.sol";
contract MerkleRedeemUpgradeSafe is PerpFiOwnableUpgrade {
event Claimed(address _claimant, uint256 _balance);
//**********************************************************//
// The below state variables can not change the order //
//**********************************************************//
// Recorded weeks
mapping(uint256 => bytes32) public weekMerkleRoots;
mapping(uint256 => mapping(address => bool)) public claimed;
IERC20 public token;
//**********************************************************//
// The above state variables can not change the order //
//**********************************************************//
//◥◤◥◤◥◤◥◤◥◤◥◤◥◤◥◤ add state variables below ◥◤◥◤◥◤◥◤◥◤◥◤◥◤◥◤//
//◢◣◢◣◢◣◢◣◢◣◢◣◢◣◢◣ add state variables above ◢◣◢◣◢◣◢◣◢◣◢◣◢◣◢◣//
uint256[50] private __gap;
//
// FUNCTIONS
//
function __MerkleRedeem_init(IERC20 _token) internal initializer {
__Ownable_init();
__MerkleRedeem_init_unchained(_token);
}
function __MerkleRedeem_init_unchained(IERC20 _token) internal initializer {
token = _token;
}
function disburse(address _liquidityProvider, uint256 _balance) private {
if (_balance > 0) {
emit Claimed(_liquidityProvider, _balance);
require(token.transfer(_liquidityProvider, _balance), "ERR_TRANSFER_FAILED");
}
}
function claimWeek(
address _liquidityProvider,
uint256 _week,
uint256 _claimedBalance,
bytes32[] memory _merkleProof
) public virtual {
require(!claimed[_week][_liquidityProvider], "Claimed already");
require(verifyClaim(_liquidityProvider, _week, _claimedBalance, _merkleProof), "Incorrect merkle proof");
claimed[_week][_liquidityProvider] = true;
disburse(_liquidityProvider, _claimedBalance);
}
struct Claim {
uint256 week;
uint256 balance;
bytes32[] merkleProof;
}
function claimWeeks(address _liquidityProvider, Claim[] memory claims) public virtual {
uint256 totalBalance = 0;
Claim memory claim;
for (uint256 i = 0; i < claims.length; i++) {
claim = claims[i];
require(!claimed[claim.week][_liquidityProvider], "Claimed already");
require(
verifyClaim(_liquidityProvider, claim.week, claim.balance, claim.merkleProof),
"Incorrect merkle proof"
);
totalBalance += claim.balance;
claimed[claim.week][_liquidityProvider] = true;
}
disburse(_liquidityProvider, totalBalance);
}
function claimStatus(
address _liquidityProvider,
uint256 _begin,
uint256 _end
) external view returns (bool[] memory) {
uint256 size = 1 + _end - _begin;
bool[] memory arr = new bool[](size);
for (uint256 i = 0; i < size; i++) {
arr[i] = claimed[_begin + i][_liquidityProvider];
}
return arr;
}
function merkleRoots(uint256 _begin, uint256 _end) external view returns (bytes32[] memory) {
uint256 size = 1 + _end - _begin;
bytes32[] memory arr = new bytes32[](size);
for (uint256 i = 0; i < size; i++) {
arr[i] = weekMerkleRoots[_begin + i];
}
return arr;
}
function verifyClaim(
address _liquidityProvider,
uint256 _week,
uint256 _claimedBalance,
bytes32[] memory _merkleProof
) public view virtual returns (bool valid) {
bytes32 leaf = keccak256(abi.encodePacked(_liquidityProvider, _claimedBalance));
return MerkleProof.verify(_merkleProof, weekMerkleRoots[_week], leaf);
}
function seedAllocations(
uint256 _week,
bytes32 _merkleRoot,
uint256 _totalAllocation
) public virtual {
require(weekMerkleRoots[_week] == bytes32(0), "cannot rewrite merkle root");
weekMerkleRoots[_week] = _merkleRoot;
require(token.transferFrom(msg.sender, address(this), _totalAllocation), "ERR_TRANSFER_FAILED");
}
}
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.9;
import { ContextUpgradeSafe } from "@openzeppelin/contracts-ethereum-package/contracts/GSN/Context.sol";
// copy from openzeppelin Ownable, only modify how the owner transfer
/**
* @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 PerpFiOwnableUpgrade is ContextUpgradeSafe {
address private _owner;
address private _candidate;
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;
}
function candidate() public view returns (address) {
return _candidate;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(_owner == _msgSender(), "PerpFiOwnableUpgrade: 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 Set ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function setOwner(address newOwner) public onlyOwner {
require(newOwner != address(0), "PerpFiOwnableUpgrade: zero address");
require(newOwner != _owner, "PerpFiOwnableUpgrade: same as original");
require(newOwner != _candidate, "PerpFiOwnableUpgrade: same as candidate");
_candidate = newOwner;
}
/**
* @dev Transfers ownership of the contract to a new account (`_candidate`).
* Can only be called by the new owner.
*/
function updateOwner() public {
require(_candidate != address(0), "PerpFiOwnableUpgrade: candidate is zero address");
require(_candidate == _msgSender(), "PerpFiOwnableUpgrade: not the new owner");
emit OwnershipTransferred(_owner, _candidate);
_owner = _candidate;
_candidate = address(0);
}
uint256[50] private __gap;
}
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.9;
import { SafeMath } from "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol";
import { DecimalMath } from "./DecimalMath.sol";
library Decimal {
using DecimalMath for uint256;
using SafeMath for uint256;
struct decimal {
uint256 d;
}
function zero() internal pure returns (decimal memory) {
return decimal(0);
}
function one() internal pure returns (decimal memory) {
return decimal(DecimalMath.unit(18));
}
function toUint(decimal memory x) internal pure returns (uint256) {
return x.d;
}
function modD(decimal memory x, decimal memory y) internal pure returns (decimal memory) {
return decimal(x.d.mul(DecimalMath.unit(18)) % y.d);
}
function cmp(decimal memory x, decimal memory y) internal pure returns (int8) {
if (x.d > y.d) {
return 1;
} else if (x.d < y.d) {
return -1;
}
return 0;
}
/// @dev add two decimals
function addD(decimal memory x, decimal memory y) internal pure returns (decimal memory) {
decimal memory t;
t.d = x.d.add(y.d);
return t;
}
/// @dev subtract two decimals
function subD(decimal memory x, decimal memory y) internal pure returns (decimal memory) {
decimal memory t;
t.d = x.d.sub(y.d);
return t;
}
/// @dev multiple two decimals
function mulD(decimal memory x, decimal memory y) internal pure returns (decimal memory) {
decimal memory t;
t.d = x.d.muld(y.d);
return t;
}
/// @dev multiple a decimal by a uint256
function mulScalar(decimal memory x, uint256 y) internal pure returns (decimal memory) {
decimal memory t;
t.d = x.d.mul(y);
return t;
}
/// @dev divide two decimals
function divD(decimal memory x, decimal memory y) internal pure returns (decimal memory) {
decimal memory t;
t.d = x.d.divd(y.d);
return t;
}
/// @dev divide a decimal by a uint256
function divScalar(decimal memory x, uint256 y) internal pure returns (decimal memory) {
decimal memory t;
t.d = x.d.div(y);
return t;
}
}
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.9;
import { SafeMath } from "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol";
/// @dev Implements simple fixed point math add, sub, mul and div operations.
/// @author Alberto Cuesta Cañada
library DecimalMath {
using SafeMath for uint256;
/// @dev Returns 1 in the fixed point representation, with `decimals` decimals.
function unit(uint8 decimals) internal pure returns (uint256) {
return 10**uint256(decimals);
}
/// @dev Adds x and y, assuming they are both fixed point with 18 decimals.
function addd(uint256 x, uint256 y) internal pure returns (uint256) {
return x.add(y);
}
/// @dev Subtracts y from x, assuming they are both fixed point with 18 decimals.
function subd(uint256 x, uint256 y) internal pure returns (uint256) {
return x.sub(y);
}
/// @dev Multiplies x and y, assuming they are both fixed point with 18 digits.
function muld(uint256 x, uint256 y) internal pure returns (uint256) {
return muld(x, y, 18);
}
/// @dev Multiplies x and y, assuming they are both fixed point with `decimals` digits.
function muld(
uint256 x,
uint256 y,
uint8 decimals
) internal pure returns (uint256) {
return x.mul(y).div(unit(decimals));
}
/// @dev Divides x between y, assuming they are both fixed point with 18 digits.
function divd(uint256 x, uint256 y) internal pure returns (uint256) {
return divd(x, y, 18);
}
/// @dev Divides x between y, assuming they are both fixed point with `decimals` digits.
function divd(
uint256 x,
uint256 y,
uint8 decimals
) internal pure returns (uint256) {
return x.mul(unit(decimals)).div(y);
}
}
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.9;
import { IERC20 } from "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol";
import { SafeMath } from "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol";
import { Decimal } from "./Decimal.sol";
abstract contract DecimalERC20 {
using SafeMath for uint256;
using Decimal for Decimal.decimal;
mapping(address => uint256) private decimalMap;
//◥◤◥◤◥◤◥◤◥◤◥◤◥◤◥◤ add state variables below ◥◤◥◤◥◤◥◤◥◤◥◤◥◤◥◤//
//◢◣◢◣◢◣◢◣◢◣◢◣◢◣◢◣ add state variables above ◢◣◢◣◢◣◢◣◢◣◢◣◢◣◢◣//
uint256[50] private __gap;
//
// INTERNAL functions
//
// CAUTION: do not input _from == _to s.t. this function will always fail
function _transfer(
IERC20 _token,
address _to,
Decimal.decimal memory _value
) internal {
_updateDecimal(address(_token));
Decimal.decimal memory balanceBefore = _balanceOf(_token, _to);
uint256 roundedDownValue = _toUint(_token, _value);
// solhint-disable avoid-low-level-calls
(bool success, bytes memory data) =
address(_token).call(abi.encodeWithSelector(_token.transfer.selector, _to, roundedDownValue));
require(success && (data.length == 0 || abi.decode(data, (bool))), "DecimalERC20: transfer failed");
_validateBalance(_token, _to, roundedDownValue, balanceBefore);
}
function _transferFrom(
IERC20 _token,
address _from,
address _to,
Decimal.decimal memory _value
) internal {
_updateDecimal(address(_token));
Decimal.decimal memory balanceBefore = _balanceOf(_token, _to);
uint256 roundedDownValue = _toUint(_token, _value);
// solhint-disable avoid-low-level-calls
(bool success, bytes memory data) =
address(_token).call(abi.encodeWithSelector(_token.transferFrom.selector, _from, _to, roundedDownValue));
require(success && (data.length == 0 || abi.decode(data, (bool))), "DecimalERC20: transferFrom failed");
_validateBalance(_token, _to, roundedDownValue, balanceBefore);
}
function _approve(
IERC20 _token,
address _spender,
Decimal.decimal memory _value
) internal {
_updateDecimal(address(_token));
// to be compatible with some erc20 tokens like USDT
__approve(_token, _spender, Decimal.zero());
__approve(_token, _spender, _value);
}
//
// VIEW
//
function _allowance(
IERC20 _token,
address _owner,
address _spender
) internal view returns (Decimal.decimal memory) {
return _toDecimal(_token, _token.allowance(_owner, _spender));
}
function _balanceOf(IERC20 _token, address _owner) internal view returns (Decimal.decimal memory) {
return _toDecimal(_token, _token.balanceOf(_owner));
}
function _totalSupply(IERC20 _token) internal view returns (Decimal.decimal memory) {
return _toDecimal(_token, _token.totalSupply());
}
function _toDecimal(IERC20 _token, uint256 _number) internal view returns (Decimal.decimal memory) {
uint256 tokenDecimals = _getTokenDecimals(address(_token));
if (tokenDecimals >= 18) {
return Decimal.decimal(_number.div(10**(tokenDecimals.sub(18))));
}
return Decimal.decimal(_number.mul(10**(uint256(18).sub(tokenDecimals))));
}
function _toUint(IERC20 _token, Decimal.decimal memory _decimal) internal view returns (uint256) {
uint256 tokenDecimals = _getTokenDecimals(address(_token));
if (tokenDecimals >= 18) {
return _decimal.toUint().mul(10**(tokenDecimals.sub(18)));
}
return _decimal.toUint().div(10**(uint256(18).sub(tokenDecimals)));
}
function _getTokenDecimals(address _token) internal view returns (uint256) {
uint256 tokenDecimals = decimalMap[_token];
if (tokenDecimals == 0) {
(bool success, bytes memory data) = _token.staticcall(abi.encodeWithSignature("decimals()"));
require(success && data.length != 0, "DecimalERC20: get decimals failed");
tokenDecimals = abi.decode(data, (uint256));
}
return tokenDecimals;
}
//
// PRIVATE
//
function _updateDecimal(address _token) private {
uint256 tokenDecimals = _getTokenDecimals(_token);
if (decimalMap[_token] != tokenDecimals) {
decimalMap[_token] = tokenDecimals;
}
}
function __approve(
IERC20 _token,
address _spender,
Decimal.decimal memory _value
) private {
// solhint-disable avoid-low-level-calls
(bool success, bytes memory data) =
address(_token).call(abi.encodeWithSelector(_token.approve.selector, _spender, _toUint(_token, _value)));
require(success && (data.length == 0 || abi.decode(data, (bool))), "DecimalERC20: approve failed");
}
// To prevent from deflationary token, check receiver's balance is as expectation.
function _validateBalance(
IERC20 _token,
address _to,
uint256 _roundedDownValue,
Decimal.decimal memory _balanceBefore
) private view {
require(
_balanceOf(_token, _to).cmp(_balanceBefore.addD(_toDecimal(_token, _roundedDownValue))) == 0,
"DecimalERC20: balance inconsistent"
);
}
}
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.6.9;
// wrap block.xxx functions for testing
// only support timestamp and number so far
abstract contract BlockContext {
//◥◤◥◤◥◤◥◤◥◤◥◤◥◤◥◤ add state variables below ◥◤◥◤◥◤◥◤◥◤◥◤◥◤◥◤//
//◢◣◢◣◢◣◢◣◢◣◢◣◢◣◢◣ add state variables above ◢◣◢◣◢◣◢◣◢◣◢◣◢◣◢◣//
uint256[50] private __gap;
function _blockTimestamp() internal view virtual returns (uint256) {
return block.timestamp;
}
function _blockNumber() internal view virtual returns (uint256) {
return block.number;
}
}
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.0;
/**
* @dev These functions deal with verification of Merkle trees (hash trees),
*/
library MerkleProof {
/**
* @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
* defined by `root`. For this, a `proof` must be provided, containing
* sibling hashes on the branch from the leaf to the root of the tree. Each
* pair of leaves and each pair of pre-images are assumed to be sorted.
*/
function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
bytes32 proofElement = proof[i];
if (computedHash <= proofElement) {
// Hash(current computed hash + current element of the proof)
computedHash = keccak256(abi.encodePacked(computedHash, proofElement));
} else {
// Hash(current element of the proof + current computed hash)
computedHash = keccak256(abi.encodePacked(proofElement, computedHash));
}
}
// Check if the computed hash (root) is equal to the provided root
return computedHash == root;
}
}
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;
}