2020-03-30 15:33:25 +00:00
|
|
|
/* solium-disable security/no-block-members */
|
|
|
|
/* solium-disable security/no-inline-assembly */
|
|
|
|
pragma solidity >=0.5.0 <0.6.0;
|
2019-06-18 13:59:23 +00:00
|
|
|
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
|
2019-06-18 12:14:29 +00:00
|
|
|
import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
|
|
|
|
import "openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol";
|
|
|
|
import "openzeppelin-solidity/contracts/token/ERC20/ERC20Burnable.sol";
|
2019-06-18 19:56:50 +00:00
|
|
|
import "./math.sol";
|
|
|
|
import "./token/ApproveAndCallFallBack.sol";
|
2020-03-30 15:33:25 +00:00
|
|
|
import "./token/MiniMeTokenInterface.sol";
|
2019-06-18 12:14:29 +00:00
|
|
|
|
2019-06-18 19:56:50 +00:00
|
|
|
contract StakingPool is ERC20, ERC20Detailed, ERC20Burnable, DSMath, ApproveAndCallFallBack {
|
2020-03-30 15:33:25 +00:00
|
|
|
uint public MAX_SUPPLY = 0;
|
|
|
|
|
|
|
|
MiniMeTokenInterface public token;
|
|
|
|
uint public stakingBlockLimit;
|
|
|
|
uint public blockToCheckBalance;
|
2019-06-18 12:14:29 +00:00
|
|
|
|
2020-03-30 15:33:25 +00:00
|
|
|
/**
|
|
|
|
* @param _tokenAddress SNT token address
|
|
|
|
* @param _stakingPeriodLen Number of block that represents the period when Staking is available
|
|
|
|
*/
|
|
|
|
constructor (address _tokenAddress, uint _stakingPeriodLen) public ERC20Detailed("Status Stake Token", "SST", 18) {
|
|
|
|
token = MiniMeTokenInterface(_tokenAddress);
|
|
|
|
stakingBlockLimit = block.number + _stakingPeriodLen;
|
|
|
|
blockToCheckBalance = block.number;
|
2019-06-18 12:14:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 15:33:25 +00:00
|
|
|
/**
|
|
|
|
* @notice Determine exchange rate
|
|
|
|
* @return Exchange rate
|
|
|
|
*/
|
2019-06-18 12:14:29 +00:00
|
|
|
function exchangeRate (uint256 excludeAmount) public view returns (uint256) {
|
|
|
|
if (totalSupply() == 0) return 1000000000000000000;
|
2019-06-18 13:59:23 +00:00
|
|
|
return wdiv(token.balanceOf(address(this)), totalSupply());
|
2019-06-18 12:14:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 15:33:25 +00:00
|
|
|
/**
|
|
|
|
* @notice Estimate the number of tokens that will be minted based on an amount of SNT
|
|
|
|
* @param _value Amount of SNT used in calculation
|
|
|
|
*/
|
|
|
|
function estimatedTokens(uint256 _value) public view returns (uint256) {
|
|
|
|
uint256 rate = exchangeRate(_value);
|
|
|
|
return wdiv(_value, wdiv(rate, 1000000000000000000));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @notice Determine max amount that can be staked
|
|
|
|
* @return Max amount to stake
|
|
|
|
*/
|
|
|
|
function maxAmountToStake() public view returns (uint256) {
|
|
|
|
return MAX_SUPPLY - totalSupply();
|
2019-06-18 12:14:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 15:33:25 +00:00
|
|
|
/**
|
|
|
|
* @notice Stake SNT in the pool and receive tSNT. During the stake period you can stake up to the amount of SNT you had when the pool was created. Afterwards, the amount you can stake can not exceed MAXSUPPLY - TOTALSUPPLY
|
|
|
|
* @dev Use this function with approveAndCall, since it requires a SNT transfer
|
|
|
|
* @param _amount Amount to stake
|
|
|
|
*/
|
|
|
|
function stake(uint256 _amount) public payable {
|
|
|
|
if(block.number <= stakingBlockLimit){
|
|
|
|
uint maxBalance = token.balanceOfAt(msg.sender, blockToCheckBalance);
|
|
|
|
require(_amount <= maxBalance, "Stake amount exceeds SNT balance at pool creation");
|
|
|
|
_stake(msg.sender, _amount);
|
|
|
|
MAX_SUPPLY = totalSupply();
|
|
|
|
} else {
|
|
|
|
uint maxAmountToStake = MAX_SUPPLY - totalSupply();
|
|
|
|
require(_amount <= maxAmountToStake, "Max stake amount exceeded");
|
|
|
|
_stake(msg.sender, _amount);
|
|
|
|
}
|
2019-06-18 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 15:33:25 +00:00
|
|
|
/**
|
|
|
|
* @dev Stake SNT in the contract, and receive tSNT
|
|
|
|
* @param _from Address transfering the SNT
|
|
|
|
* @param _amount Amount being staked
|
|
|
|
*/
|
|
|
|
function _stake(address _from, uint256 _amount) internal returns (uint equivalentTokens){
|
|
|
|
equivalentTokens = estimatedTokens(_amount);
|
|
|
|
require(token.transferFrom(_from, address(this), _amount), "Couldn't transfer");
|
2019-06-18 19:56:50 +00:00
|
|
|
_mint(_from, equivalentTokens);
|
2019-06-18 12:14:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 15:33:25 +00:00
|
|
|
/**
|
|
|
|
* @notice Withdraw SNT from Staking Pool, by burning tSNT
|
|
|
|
* @param amount Amount to withdraw
|
|
|
|
*/
|
2019-06-18 12:14:29 +00:00
|
|
|
function withdraw (uint256 amount) public {
|
|
|
|
uint256 rate = exchangeRate(0);
|
|
|
|
burn(amount);
|
2020-03-30 15:33:25 +00:00
|
|
|
|
|
|
|
if(block.number <= stakingBlockLimit){
|
|
|
|
MAX_SUPPLY = totalSupply();
|
|
|
|
}
|
|
|
|
|
2019-06-18 19:56:50 +00:00
|
|
|
require(token.transfer(msg.sender, wmul(amount, wdiv(rate, 1000000000000000000))), "Couldn't transfer");
|
2019-06-18 13:59:23 +00:00
|
|
|
}
|
2019-06-18 12:14:29 +00:00
|
|
|
|
2019-06-18 19:56:50 +00:00
|
|
|
/**
|
|
|
|
* @notice Support for "approveAndCall". Callable only by `token()`.
|
|
|
|
* @param _from Who approved.
|
|
|
|
* @param _amount Amount being approved,
|
|
|
|
* @param _token Token being approved, need to be equal `token()`.
|
2020-03-30 15:33:25 +00:00
|
|
|
* @param _data ABI encoded data`.
|
2019-06-18 19:56:50 +00:00
|
|
|
*/
|
|
|
|
function receiveApproval(address _from, uint256 _amount, address _token, bytes memory _data) public {
|
|
|
|
require(_token == address(token), "Wrong token");
|
|
|
|
require(_token == address(msg.sender), "Wrong call");
|
|
|
|
require(_data.length == 36, "Wrong data length");
|
|
|
|
|
|
|
|
bytes4 sig;
|
|
|
|
uint amount;
|
2020-03-30 15:33:25 +00:00
|
|
|
(sig, amount) = abiDecode(_data);
|
2019-06-18 19:56:50 +00:00
|
|
|
|
|
|
|
require(amount == _amount, "Amounts mismatch");
|
2020-03-30 15:33:25 +00:00
|
|
|
require(sig == 0xa694fc3a, "Wrong method selector"); // stake(uint256)
|
|
|
|
_stake(_from, amount);
|
2019-06-18 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 15:33:25 +00:00
|
|
|
/**
|
|
|
|
* @dev Decode calldata - stake(uint256)
|
|
|
|
* @param _data Calldata, ABI encoded
|
|
|
|
*/
|
|
|
|
function abiDecode(bytes memory _data) internal returns(
|
2019-06-18 19:56:50 +00:00
|
|
|
bytes4 sig,
|
|
|
|
uint256 amount
|
|
|
|
) {
|
|
|
|
assembly {
|
|
|
|
sig := mload(add(_data, add(0x20, 0)))
|
|
|
|
amount := mload(add(_data, 36))
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 15:33:25 +00:00
|
|
|
}
|