/**
*Submitted for verification at Etherscan.io on 2020-10-30
*/
/**
*Submitted for verification at Etherscan.io on 2020-10-30
*/
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.6.12;
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
abstract contract Context {
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;
}
}
contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor () internal {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view returns (address) {
return _owner;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(_owner == _msgSender(), "Ownable: caller is not the owner");
_;
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
library Address {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*/
function isContract(address account) internal view returns (bool) {
// According to EIP-1052, 0x0 is the value returned for not-yet created accounts
// and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
// for accounts without code, i.e. `keccak256('')`
bytes32 codehash;
bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
// solhint-disable-next-line no-inline-assembly
assembly { codehash := extcodehash(account) }
return (codehash != accountHash && codehash != 0x0);
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
// solhint-disable-next-line avoid-low-level-calls, avoid-call-value
(bool success, ) = recipient.call{ value: amount }("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain`call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
return _functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
return _functionCallWithValue(target, data, value, errorMessage);
}
function _functionCallWithValue(address target, bytes memory data, uint256 weiValue, string memory errorMessage) private returns (bytes memory) {
require(isContract(target), "Address: call to non-contract");
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory returndata) = target.call{ value: weiValue }(data);
if (success) {
return returndata;
} else {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
// solhint-disable-next-line no-inline-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
/*
Copyright 2016, Adrià Massanet
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Checked results with FIPS test vectors
https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/dss/186-2rsatestvectors.zip
file SigVer15_186-3.rsp
*/
contract SolRsaVerify {
function memcpy(uint _dest, uint _src, uint _len) pure internal {
// Copy word-length chunks while possible
for ( ;_len >= 32; _len -= 32) {
assembly {
mstore(_dest, mload(_src))
}
_dest += 32;
_src += 32;
}
// Copy remaining bytes
uint mask = 256 ** (32 - _len) - 1;
assembly {
let srcpart := and(mload(_src), not(mask))
let destpart := and(mload(_dest), mask)
mstore(_dest, or(destpart, srcpart))
}
}
function join(
bytes memory _s, bytes memory _e, bytes memory _m
) pure internal returns (bytes memory) {
uint inputLen = 0x60+_s.length+_e.length+_m.length;
uint slen = _s.length;
uint elen = _e.length;
uint mlen = _m.length;
uint sptr;
uint eptr;
uint mptr;
uint inputPtr;
bytes memory input = new bytes(inputLen);
assembly {
sptr := add(_s,0x20)
eptr := add(_e,0x20)
mptr := add(_m,0x20)
mstore(add(input,0x20),slen)
mstore(add(input,0x40),elen)
mstore(add(input,0x60),mlen)
inputPtr := add(input,0x20)
}
memcpy(inputPtr+0x60,sptr,_s.length);
memcpy(inputPtr+0x60+_s.length,eptr,_e.length);
memcpy(inputPtr+0x60+_s.length+_e.length,mptr,_m.length);
return input;
}
/** @dev Verifies a PKCSv1.5 SHA256 signature
* @param _sha256 is the sha256 of the data
* @param _s is the signature
* @param _e is the exponent
* @param _m is the modulus
* @return 0 if success, >0 otherwise
*/
function pkcs1Sha256Verify(
bytes32 _sha256,
bytes memory _s, bytes memory _e, bytes memory _m
) public view returns (uint) {
uint8[19] memory sha256Prefix = [
0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20
];
require(_m.length >= sha256Prefix.length+_sha256.length+11);
uint i;
/// decipher
bytes memory input = join(_s,_e,_m);
uint inputlen = input.length;
uint decipherlen = _m.length;
bytes memory decipher = new bytes(decipherlen);
assembly {
pop(staticcall(sub(gas(), 2000), 5, add(input,0x20), inputlen, add(decipher,0x20), decipherlen))
}
/// 0x00 || 0x01 || PS || 0x00 || DigestInfo
/// PS is padding filled with 0xff
// DigestInfo ::= SEQUENCE {
// digestAlgorithm AlgorithmIdentifier,
// digest OCTET STRING
// }
uint paddingLen = decipherlen - 3 - sha256Prefix.length - 32;
if (decipher[0] != 0 || uint8(decipher[1]) != 1) {
return 1;
}
for (i = 2;i<2+paddingLen;i++) {
if (decipher[i] != 0xff) {
return 2;
}
}
if (decipher[2+paddingLen] != 0) {
return 3;
}
for (i = 0;i<sha256Prefix.length;i++) {
if (uint8(decipher[3+paddingLen+i])!=sha256Prefix[i]) {
return 4;
}
}
for (i = 0;i<_sha256.length;i++) {
if (decipher[3+paddingLen+sha256Prefix.length+i]!=_sha256[i]) {
return 5;
}
}
return 0;
}
/** @dev Verifies a PKCSv1.5 SHA256 signature
* @param _data to verify
* @param _s is the signature
* @param _e is the exponent
* @param _m is the modulus
* @return 0 if success, >0 otherwise
*/
function pkcs1Sha256VerifyRaw(
bytes memory _data,
bytes memory _s, bytes memory _e, bytes memory _m
) public view returns (uint) {
return pkcs1Sha256Verify(sha256(_data),_s,_e,_m);
}
}
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);
}
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));
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/
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. We use {Address.functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data, "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");
}
}
}
interface IMilk2Token {
function mint(address _to, uint256 _amount) external returns (bool);
function burn(address _to, uint256 _amount) external returns (bool);
}
contract MultiplierMath {
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a > b ? a : b;
}
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
function getInterval(uint256 a, uint256 b) internal pure returns(uint256) {
return a > b ? a - b : 0;
}
}
contract ShadowStaking is Ownable, SolRsaVerify, MultiplierMath {
using SafeMath for uint256;
using SafeERC20 for IERC20;
struct UserInfo {
uint256 rewardDebt;
uint256 lastBlock;
}
struct PoolInfo {
IERC20 lpToken;
uint256 allocPointAmount;
uint256 blockCreation;
}
struct KeyInfo{
bytes keyModule;
bytes exponent;
bool keyStatus;
}
IMilk2Token public milk;
mapping (address => UserInfo) private userInfo;
address[] internal users;
PoolInfo[] private poolInfo;
KeyInfo[] private keyInfo;
uint256 private totalPoints;
uint256[5] internal epochs;
uint256[5] internal multipliers;
event Harvest(address sender, uint256 amount, uint256 blockNumber);
event AddNewPool(address token, uint256 pid);
event PoolUpdate(uint256 poolPid, uint256 previusPoints, uint256 newPoints);
event AddNewKey(bytes keyHash, uint256 id);
constructor(IMilk2Token _milk, uint256[5] memory _epochs, uint256[5] memory _multipliers) public {
milk = _milk;
epochs = _epochs;
multipliers = _multipliers;
}
/**
* @dev Add a new lp to the pool.
*
* @param _lpToken - address of ERC-20 LP token
* @param _newPoints - share in the total amount of rewards
* DO NOT add the same LP token more than once. Rewards will be messed up if you do.
* Can only be called by the current owner.
*/
function addNewPool(IERC20 _lpToken, uint256 _newPoints) public onlyOwner {
totalPoints = totalPoints.add(_newPoints);
poolInfo.push(PoolInfo({lpToken: _lpToken, allocPointAmount: _newPoints, blockCreation:block.number}));
emit AddNewPool(address(_lpToken), _newPoints);
}
/**
* @dev Update lp address to the pool.
*
* @param _poolPid - number of pool
* @param _newPoints - new amount of allocation points
* DO NOT add the same LP token more than once. Rewards will be messed up if you do.
* Can only be called by the current owner.
*/
function setPoll(uint256 _poolPid, uint256 _newPoints) public onlyOwner {
PoolInfo memory _poolInfo = poolInfo[_poolPid];
uint256 _previousPoints = poolInfo[_poolPid].allocPointAmount;
_poolInfo.allocPointAmount = _newPoints;
totalPoints = totalPoints.sub(poolInfo[_poolPid].allocPointAmount).add(_newPoints);
emit PoolUpdate(_poolPid, _previousPoints, _newPoints);
}
function getPool(uint256 _poolPid) public view returns(address _lpToken, uint256 _block, uint256 _weight) {
PoolInfo memory _poolInfo = poolInfo[_poolPid];
_lpToken = address(_poolInfo.lpToken);
_block = _poolInfo.blockCreation;
_weight = _poolInfo.allocPointAmount;
}
/**
* @dev - return Number of keys
*/
function getPoolsCount() public view returns(uint256) {
return poolInfo.length;
}
/**
* @dev - return info about current user's reward
* @param _user - user's address
*/
function getRewards(address _user) public view returns(uint256) {
return userInfo[_user].rewardDebt;
}
/**
* @dev - return info about user's last block with update
*
* @param _user - user's address
*/
function getLastBlock(address _user) public view returns(uint256) {
return userInfo[_user].lastBlock;
}
/**
* @dev - return total allocation points
*/
function getTotalPoints() public view returns(uint256) {
return totalPoints;
}
/**
* @dev Update the given pool's allocation point. Can only be called by the owner.
*
* @param _keyId - unique id key in contract storage
* @param _amount -
* @param _lastBlockNumber - last update number of block
* @param _currentBlockNumber - last update block in Ethereum mainnet
* @param _sign - bytes32 signature
*/
function withdraw( uint256 _keyId,
uint256 _amount,
uint256 _lastBlockNumber,
uint256 _currentBlockNumber,
bytes memory _sign) public {
require(_keyId < keyInfo.length, "This key is not exist");
require(keyInfo[_keyId].keyStatus, "This key is disable");
require(_currentBlockNumber < block.number, "currentBlockNumber cannot be larger than the last block");
require(pkcs1Sha256Verify(getData(_amount, _lastBlockNumber, _currentBlockNumber, msg.sender), _sign, keyInfo[_keyId].exponent, keyInfo[_keyId].keyModule) == 0, "Incorrect data");
require(userInfo[msg.sender].lastBlock == _lastBlockNumber, "lastBlockNumber must be equal to the value in the storage");
userInfo[msg.sender].rewardDebt = userInfo[msg.sender].rewardDebt.add(_amount);
userInfo[msg.sender].lastBlock = _currentBlockNumber;
if (_amount > 0) {
milk.mint(msg.sender, _amount);
}
emit Harvest(msg.sender, _amount, _currentBlockNumber);
}
function registration() public {
require(userInfo[msg.sender].lastBlock == 0, "User already exist");
UserInfo storage _userInfo = userInfo[msg.sender];
_userInfo.rewardDebt = 0;
_userInfo.lastBlock = block.number;
users.push(msg.sender);
}
function getData(uint256 _amount,
uint256 _lastBlockNumber,
uint256 _currentBlockNumber,
address _sender) public pure returns(bytes32) {
return sha256(abi.encode(_amount, _lastBlockNumber, _currentBlockNumber, _sender));
}
/**
* @dev
*
* @param _newModule - new module of key
* @param _keyExponent - new exponent of key
* Can only be called by the current owner.
*/
function addNewKey(bytes memory _newModule, bytes memory _keyExponent) public onlyOwner returns(uint256) {
keyInfo.push(KeyInfo({keyModule: _newModule, exponent: _keyExponent, keyStatus: true}));
emit AddNewKey(_newModule, keyInfo.length - 1);
return keyInfo.length - 1;
}
/**
* @dev
*
* @param _keyId - available public key for signing.
* Can only be called by the current owner.
*/
function enableKey(uint256 _keyId) public onlyOwner {
require(!keyInfo[_keyId].keyStatus, "This key already enable");
keyInfo[_keyId].keyStatus = true;
}
/**
* @dev
*
* @param _keyId - available public key for signing.
* Can only be called by the current owner.
*/
function disableKey(uint256 _keyId) public onlyOwner {
require(keyInfo[_keyId].keyStatus, "This key already disable");
keyInfo[_keyId].keyStatus = false;
}
/**
* @dev Return info about available key
* @param _keyId - available public key for signing
*/
function getKeyInfo(uint256 _keyId) public view returns(bytes memory _key, bytes memory _exponent, bool _status) {
_key = keyInfo[_keyId].keyModule;
_exponent = keyInfo[_keyId].exponent;
_status = keyInfo[_keyId].keyStatus;
}
/**
* @dev - return Number of keys
*/
function getKeyCount() public view returns(uint256) {
return keyInfo.length;
}
/**
* @dev - return Number of users
*/
function getUsersCount() public view returns(uint256) {
return users.length;
}
/**
* @dev - return address of user
* @param - _userId - unique number of user in array
*/
function getUser(uint256 _userId) public view returns(address) {
return users[_userId];
}
/**
* @dev - return total rewards
*/
function getTotalRewards(address _user) public view returns(uint256) {
return userInfo[_user].rewardDebt;
}
/**
* @param - _id - multiplier's id (0-4)
* @dev - return value of multiplier
*/
function getValueMultiplier(uint256 _id) public view returns(uint256) {
return multipliers[_id];
}
/**
* @param - _id - epoch's id(0-4)
* @dev - return value of epoch
*/
function getValueEpoch(uint256 _id) public view returns(uint256) {
return epochs[_id];
}
function getMultiplier(uint256 f, uint256 t) public view returns(uint256) {
return getInterval(min(t, epochs[1]), max(f, epochs[0])) * multipliers[0] +
getInterval(min(t, epochs[2]), max(f, epochs[1])) * multipliers[1] +
getInterval(min(t, epochs[3]), max(f, epochs[2])) * multipliers[2] +
getInterval(min(t, epochs[4]), max(f, epochs[3])) * multipliers[3] +
getInterval(max(t, epochs[4]), max(f, epochs[4])) * multipliers[4];
}
function getCurrentMultiplier() public view returns(uint256) {
if (block.number < epochs[0]) {
return 0;
}
if (block.number < epochs[1]) {
return multipliers[0];
}
if (block.number < epochs[2]) {
return multipliers[1];
}
if (block.number < epochs[3]) {
return multipliers[2];
}
if (block.number < epochs[4]) {
return multipliers[3];
}
if (block.number > epochs[4]) {
return multipliers[4];
}
}
}
{
"compilationTarget": {
"ShadowStaking.sol": "ShadowStaking"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"contract IMilk2Token","name":"_milk","type":"address"},{"internalType":"uint256[5]","name":"_epochs","type":"uint256[5]"},{"internalType":"uint256[5]","name":"_multipliers","type":"uint256[5]"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes","name":"keyHash","type":"bytes"},{"indexed":false,"internalType":"uint256","name":"id","type":"uint256"}],"name":"AddNewKey","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"pid","type":"uint256"}],"name":"AddNewPool","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"sender","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"blockNumber","type":"uint256"}],"name":"Harvest","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"poolPid","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"previusPoints","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newPoints","type":"uint256"}],"name":"PoolUpdate","type":"event"},{"inputs":[{"internalType":"bytes","name":"_newModule","type":"bytes"},{"internalType":"bytes","name":"_keyExponent","type":"bytes"}],"name":"addNewKey","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"_lpToken","type":"address"},{"internalType":"uint256","name":"_newPoints","type":"uint256"}],"name":"addNewPool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_keyId","type":"uint256"}],"name":"disableKey","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_keyId","type":"uint256"}],"name":"enableKey","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"getCurrentMultiplier","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"},{"internalType":"uint256","name":"_lastBlockNumber","type":"uint256"},{"internalType":"uint256","name":"_currentBlockNumber","type":"uint256"},{"internalType":"address","name":"_sender","type":"address"}],"name":"getData","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getKeyCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_keyId","type":"uint256"}],"name":"getKeyInfo","outputs":[{"internalType":"bytes","name":"_key","type":"bytes"},{"internalType":"bytes","name":"_exponent","type":"bytes"},{"internalType":"bool","name":"_status","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"getLastBlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"f","type":"uint256"},{"internalType":"uint256","name":"t","type":"uint256"}],"name":"getMultiplier","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_poolPid","type":"uint256"}],"name":"getPool","outputs":[{"internalType":"address","name":"_lpToken","type":"address"},{"internalType":"uint256","name":"_block","type":"uint256"},{"internalType":"uint256","name":"_weight","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPoolsCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"getRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getTotalPoints","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"getTotalRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_userId","type":"uint256"}],"name":"getUser","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getUsersCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_id","type":"uint256"}],"name":"getValueEpoch","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_id","type":"uint256"}],"name":"getValueMultiplier","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"milk","outputs":[{"internalType":"contract IMilk2Token","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_sha256","type":"bytes32"},{"internalType":"bytes","name":"_s","type":"bytes"},{"internalType":"bytes","name":"_e","type":"bytes"},{"internalType":"bytes","name":"_m","type":"bytes"}],"name":"pkcs1Sha256Verify","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"_data","type":"bytes"},{"internalType":"bytes","name":"_s","type":"bytes"},{"internalType":"bytes","name":"_e","type":"bytes"},{"internalType":"bytes","name":"_m","type":"bytes"}],"name":"pkcs1Sha256VerifyRaw","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"registration","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_poolPid","type":"uint256"},{"internalType":"uint256","name":"_newPoints","type":"uint256"}],"name":"setPoll","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_keyId","type":"uint256"},{"internalType":"uint256","name":"_amount","type":"uint256"},{"internalType":"uint256","name":"_lastBlockNumber","type":"uint256"},{"internalType":"uint256","name":"_currentBlockNumber","type":"uint256"},{"internalType":"bytes","name":"_sign","type":"bytes"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"}]