2021-04-13 21:22:30 +00:00
|
|
|
pragma solidity ^0.8.0;
|
2020-10-30 03:41:19 +00:00
|
|
|
|
|
|
|
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
2021-04-13 21:22:30 +00:00
|
|
|
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
|
2020-10-30 03:41:19 +00:00
|
|
|
import "@openzeppelin/contracts/access/Ownable.sol";
|
2021-04-13 21:22:30 +00:00
|
|
|
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
|
2020-10-30 03:41:19 +00:00
|
|
|
import "./DioneToken.sol";
|
|
|
|
|
|
|
|
// DioneStaking is the main contract for Dione oracle network Proof-of-Stake mechanism
|
|
|
|
//
|
|
|
|
// Note that it's ownable and the owner has power over changing miner rewards and minimum DIONE stake
|
|
|
|
// The ownership of the contract would be transfered to 24 hours Timelock
|
|
|
|
contract DioneStaking is Ownable, ReentrancyGuard {
|
|
|
|
using SafeMath for uint256;
|
|
|
|
|
|
|
|
// MinerInfo contains total DIONEs staked by miner, how much tasks been already computed
|
|
|
|
// and timestamp of first deposit
|
|
|
|
struct MinerInfo {
|
|
|
|
uint256 amount; // How many DIONE tokens was staked by the miner.
|
|
|
|
uint256 firstStakeBlock; // First block for miner DIONE tokens reward.
|
|
|
|
uint256 lastRewardBlock; // Last block for miner DIONE tokens reward
|
|
|
|
}
|
|
|
|
|
|
|
|
DioneToken public dione;
|
2021-04-13 21:22:30 +00:00
|
|
|
// DioneOracle contract address.
|
|
|
|
address public dioneOracleAddress;
|
2021-03-02 22:04:19 +00:00
|
|
|
// Dispute contract address.
|
|
|
|
address public disputeContractAddr;
|
2020-10-30 03:41:19 +00:00
|
|
|
// Miner rewards in DIONE tokens.
|
|
|
|
uint256 public minerReward;
|
|
|
|
// The block number when DIONE mining starts.
|
|
|
|
uint256 public startBlock;
|
|
|
|
// Minimum amount of staked DIONE tokens required to start mining
|
|
|
|
uint256 public minimumStake;
|
|
|
|
// Total amount of DIONE tokens staked
|
|
|
|
uint256 private _totalStake;
|
|
|
|
|
|
|
|
// Info of each miner that stakes DIONE tokens.
|
|
|
|
mapping (address => MinerInfo) public minerInfo;
|
|
|
|
|
|
|
|
event Stake(address indexed miner, uint256 amount);
|
|
|
|
event Withdraw(address indexed miner, uint256 amount);
|
|
|
|
event Mine(address indexed miner, uint256 blockNumber);
|
2021-04-28 20:12:51 +00:00
|
|
|
event RewardChanged(uint256 oldValue, uint256 newValue);
|
|
|
|
event MinimumStakeChanged(uint256 oldValue, uint256 newValue);
|
2020-11-25 18:54:59 +00:00
|
|
|
|
2020-10-30 03:41:19 +00:00
|
|
|
constructor(
|
|
|
|
DioneToken _dione,
|
|
|
|
uint256 _minerReward,
|
|
|
|
uint256 _startBlock,
|
2020-10-30 05:23:08 +00:00
|
|
|
uint256 _minimumStake
|
2021-04-13 21:22:30 +00:00
|
|
|
) {
|
2020-10-30 03:41:19 +00:00
|
|
|
dione = _dione;
|
2021-04-14 23:04:28 +00:00
|
|
|
minerReward = _minerReward;
|
2020-10-30 03:41:19 +00:00
|
|
|
startBlock = _startBlock;
|
2021-04-14 23:04:28 +00:00
|
|
|
minimumStake = _minimumStake;
|
2020-10-30 03:41:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 21:40:27 +00:00
|
|
|
// Mine new dione oracle task, only can be executed by oracle contract
|
2020-10-30 03:41:19 +00:00
|
|
|
function mine(address _minerAddr) public nonReentrant {
|
2021-04-14 23:04:28 +00:00
|
|
|
require(msg.sender == dioneOracleAddress, "not oracle contract");
|
2020-10-30 03:41:19 +00:00
|
|
|
MinerInfo storage miner = minerInfo[_minerAddr];
|
|
|
|
dione.mint(_minerAddr, minerReward);
|
|
|
|
miner.lastRewardBlock = block.number;
|
2020-10-30 05:23:08 +00:00
|
|
|
emit Mine(_minerAddr, block.number);
|
2020-10-30 03:41:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 23:04:28 +00:00
|
|
|
// Mine new dione oracle task and stake miner reward, only can be executed by oracle contract
|
2020-10-30 03:41:19 +00:00
|
|
|
function mineAndStake(address _minerAddr) public nonReentrant {
|
2021-04-14 23:04:28 +00:00
|
|
|
require(msg.sender == dioneOracleAddress, "not oracle contract");
|
2020-10-30 03:41:19 +00:00
|
|
|
MinerInfo storage miner = minerInfo[_minerAddr];
|
|
|
|
dione.mint(address(this), minerReward);
|
|
|
|
_totalStake = _totalStake.add(minerReward);
|
|
|
|
miner.amount = miner.amount.add(minerReward);
|
|
|
|
miner.lastRewardBlock = block.number;
|
2020-10-30 05:23:08 +00:00
|
|
|
emit Mine(_minerAddr, block.number);
|
2020-10-30 03:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deposit DIONE tokens to mine on dione network
|
|
|
|
function stake(uint256 _amount) public nonReentrant {
|
2021-04-28 20:12:51 +00:00
|
|
|
require(_amount > 0, "cannot stake zero");
|
|
|
|
require(_amount >= minimumStake, "actual stake amount is less than minimum stake amount");
|
2020-10-30 03:41:19 +00:00
|
|
|
MinerInfo storage miner = minerInfo[msg.sender];
|
2021-04-28 20:12:51 +00:00
|
|
|
dione.transferFrom(address(msg.sender), address(this), _amount);
|
2020-10-30 05:23:08 +00:00
|
|
|
_totalStake = _totalStake.add(_amount);
|
2020-10-30 03:41:19 +00:00
|
|
|
miner.amount = miner.amount.add(_amount);
|
2021-04-28 20:12:51 +00:00
|
|
|
if (miner.firstStakeBlock == 0) {
|
2020-10-30 03:41:19 +00:00
|
|
|
miner.firstStakeBlock = block.number > startBlock ? block.number : startBlock;
|
|
|
|
}
|
|
|
|
emit Stake(msg.sender, _amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Withdraw DIONE tokens from DioneStaking
|
|
|
|
function withdraw(uint256 _amount) public nonReentrant {
|
|
|
|
MinerInfo storage miner = minerInfo[msg.sender];
|
|
|
|
require(miner.amount >= _amount, "withdraw: not enough tokens");
|
2021-04-28 20:12:51 +00:00
|
|
|
require(_amount > 0, "cannot withdraw zero");
|
|
|
|
_totalStake = _totalStake.sub(_amount);
|
|
|
|
miner.amount = miner.amount.sub(_amount);
|
|
|
|
dione.transfer(address(msg.sender), _amount);
|
2020-10-30 03:41:19 +00:00
|
|
|
emit Withdraw(msg.sender, _amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns total amount of DIONE tokens in PoS mining
|
|
|
|
function totalStake() external view returns (uint256) {
|
|
|
|
return _totalStake;
|
|
|
|
}
|
|
|
|
|
2020-11-13 14:04:10 +00:00
|
|
|
function minerStake(address _minerAddr) external view returns (uint256) {
|
2020-11-25 18:54:59 +00:00
|
|
|
return minerInfo[_minerAddr].amount;
|
2020-10-30 03:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update miner reward in DIONE tokens, only can be executed by owner of the contract
|
|
|
|
function setMinerReward(uint256 _minerReward) public onlyOwner {
|
2021-04-28 20:12:51 +00:00
|
|
|
require(_minerReward > 0, "reward must not be zero");
|
|
|
|
uint256 oldRewardValue = minerReward;
|
2020-10-30 03:41:19 +00:00
|
|
|
minerReward = _minerReward;
|
2021-04-28 20:12:51 +00:00
|
|
|
emit RewardChanged(oldRewardValue, _minerReward);
|
2020-10-30 03:41:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:54:59 +00:00
|
|
|
function isMiner(address _minerAddr) public view returns (bool) {
|
|
|
|
return minerInfo[_minerAddr].amount >= minimumStake;
|
2020-11-14 11:25:14 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 03:41:19 +00:00
|
|
|
// Update minimum stake in DIONE tokens for miners, only can be executed by owner of the contract
|
|
|
|
function setMinimumStake(uint256 _minimumStake) public onlyOwner {
|
2021-04-28 20:12:51 +00:00
|
|
|
require(_minimumStake > 0, "minimum stake must not be zero");
|
|
|
|
uint256 oldValue = minimumStake;
|
2020-10-30 03:41:19 +00:00
|
|
|
minimumStake = _minimumStake;
|
2021-04-28 20:12:51 +00:00
|
|
|
emit MinimumStakeChanged(oldValue, _minimumStake);
|
2020-10-30 03:41:19 +00:00
|
|
|
}
|
2020-11-14 11:25:14 +00:00
|
|
|
|
2021-04-13 21:22:30 +00:00
|
|
|
function setOracleContractAddress(address _addr) public onlyOwner {
|
2021-04-28 20:12:51 +00:00
|
|
|
require(_addr != address(0), "address must not be zero");
|
2021-04-13 21:22:30 +00:00
|
|
|
dioneOracleAddress = _addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setDisputeContractAddress(address _addr) public onlyOwner {
|
2021-04-28 20:12:51 +00:00
|
|
|
require(_addr != address(0), "address must not be zero");
|
2021-04-13 21:22:30 +00:00
|
|
|
disputeContractAddr = _addr;
|
2020-11-25 18:54:59 +00:00
|
|
|
}
|
2021-03-02 22:04:19 +00:00
|
|
|
|
2021-03-04 18:22:05 +00:00
|
|
|
function slashMiner(address miner, address[] memory receipentMiners) public {
|
2021-04-28 20:12:51 +00:00
|
|
|
require(msg.sender == disputeContractAddr, "caller is not the dispute contract");
|
|
|
|
require(miner != address(0), "slashing address must not be zero");
|
|
|
|
require(isMiner(miner), "slashing address isn't dione miner");
|
2021-04-20 21:40:27 +00:00
|
|
|
|
2021-03-02 22:04:19 +00:00
|
|
|
uint256 share = minerInfo[miner].amount.div(receipentMiners.length);
|
|
|
|
|
2021-04-28 20:12:51 +00:00
|
|
|
|
2021-03-04 18:22:05 +00:00
|
|
|
for (uint8 i = 0; i < receipentMiners.length; i++) {
|
2021-04-28 20:12:51 +00:00
|
|
|
require(receipentMiners[i] != miner, "receipent address must not be slashing address");
|
|
|
|
require(isMiner(receipentMiners[i]), "receipent address isn't dione miner");
|
2021-04-20 21:40:27 +00:00
|
|
|
minerInfo[miner].amount = minerInfo[miner].amount.sub(share);
|
|
|
|
minerInfo[receipentMiners[i]].amount = minerInfo[receipentMiners[i]].amount.add(share);
|
2021-03-02 22:04:19 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-13 21:22:30 +00:00
|
|
|
}
|