Files
dodo-contractV2/contracts/DODOToken/DODOIncentive.sol

160 lines
4.9 KiB
Solidity
Raw Normal View History

/*
Copyright 2020 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
pragma solidity 0.6.9;
import {InitializableOwnable} from "../lib/InitializableOwnable.sol";
import {SafeERC20} from "../lib/SafeERC20.sol";
import {SafeMath} from "../lib/SafeMath.sol";
import {IERC20} from "../intf/IERC20.sol";
2021-01-06 18:02:44 +08:00
interface IDODOIncentive {
2021-01-14 16:24:48 +08:00
function triggerIncentive(
address fromToken,
address toToken,
address assetTo
) external;
2021-01-06 18:02:44 +08:00
}
/**
* @title DODOIncentive
* @author DODO Breeder
*
* @notice Trade Incentive in DODO platform
*/
contract DODOIncentive is InitializableOwnable {
using SafeMath for uint256;
using SafeERC20 for IERC20;
2021-01-14 16:24:48 +08:00
// ============ Storage ============
address public immutable _DODO_TOKEN_;
address public _DODO_PROXY_;
2021-01-14 19:36:29 +08:00
uint256 public dodoPerBlock;
2021-01-06 18:02:44 +08:00
uint256 public defaultRate = 10;
mapping(address => uint256) public boosts;
2021-01-06 18:02:44 +08:00
uint32 public lastRewardBlock;
uint112 public totalReward;
uint112 public totalDistribution;
// ============ Events ============
event SetBoost(address token, uint256 boostRate);
2021-01-05 18:08:20 +08:00
event SetNewProxy(address dodoProxy);
event SetPerReward(uint256 dodoPerBlock);
event SetDefaultRate(uint256 defaultRate);
2021-01-14 16:24:48 +08:00
event Incentive(address user, uint256 reward);
constructor(address _dodoToken) public {
_DODO_TOKEN_ = _dodoToken;
}
// ============ Ownable ============
function changeBoost(address _token, uint256 _boostRate) public onlyOwner {
require(_token != address(0));
2021-01-14 16:24:48 +08:00
require(_boostRate + defaultRate <= 1000);
boosts[_token] = _boostRate;
2021-01-14 16:24:48 +08:00
emit SetBoost(_token, _boostRate);
}
function changePerReward(uint256 _dodoPerBlock) public onlyOwner {
2021-01-14 16:24:48 +08:00
_updateTotalReward();
dodoPerBlock = _dodoPerBlock;
emit SetPerReward(dodoPerBlock);
}
function changeDefaultRate(uint256 _defaultRate) public onlyOwner {
defaultRate = _defaultRate;
emit SetDefaultRate(defaultRate);
}
2021-01-05 18:08:20 +08:00
function changeDODOProxy(address _dodoProxy) public onlyOwner {
_DODO_PROXY_ = _dodoProxy;
emit SetNewProxy(_DODO_PROXY_);
}
function emptyReward(address assetTo) public onlyOwner {
uint256 balance = IERC20(_DODO_TOKEN_).balanceOf(address(this));
IERC20(_DODO_TOKEN_).transfer(assetTo, balance);
}
2021-01-13 20:39:41 +08:00
// ============ Incentive function ============
2021-01-14 16:24:48 +08:00
function triggerIncentive(
address fromToken,
address toToken,
address assetTo
) external {
require(msg.sender == _DODO_PROXY_, "DODOIncentive:Access restricted");
uint256 curTotalDistribution = totalDistribution;
uint256 fromRate = boosts[fromToken];
uint256 toRate = boosts[toToken];
2021-01-06 18:02:44 +08:00
uint256 rate = (fromRate >= toRate ? fromRate : toRate) + defaultRate;
2021-01-21 21:48:40 +08:00
require(rate <= 1000, "RATE_INVALID");
2021-01-14 16:24:48 +08:00
uint256 _totalReward = _getTotalReward();
uint256 reward = ((_totalReward - curTotalDistribution) * rate) / 1000;
2021-01-06 18:02:44 +08:00
uint256 _totalDistribution = curTotalDistribution + reward;
2021-01-14 16:24:48 +08:00
_update(_totalReward, _totalDistribution);
if (reward != 0) {
IERC20(_DODO_TOKEN_).transfer(assetTo, reward);
emit Incentive(assetTo, reward);
}
}
2021-01-06 18:02:44 +08:00
2021-01-14 16:24:48 +08:00
function _updateTotalReward() internal {
2021-01-21 17:10:14 +08:00
uint256 _totalReward = _getTotalReward();
require(_totalReward < uint112(-1), "OVERFLOW");
totalReward = uint112(_totalReward);
2021-01-21 17:56:49 +08:00
lastRewardBlock = uint32(block.number);
}
2021-01-06 18:02:44 +08:00
function _update(uint256 _totalReward, uint256 _totalDistribution) internal {
2021-01-14 16:24:48 +08:00
require(
2021-01-21 17:56:49 +08:00
_totalReward < uint112(-1) && _totalDistribution < uint112(-1) && block.number < uint32(-1),
2021-01-14 16:24:48 +08:00
"OVERFLOW"
);
2021-01-21 17:56:49 +08:00
lastRewardBlock = uint32(block.number);
2021-01-06 18:02:44 +08:00
totalReward = uint112(_totalReward);
totalDistribution = uint112(_totalDistribution);
}
2021-01-13 20:39:41 +08:00
2021-01-14 16:24:48 +08:00
function _getTotalReward() internal view returns (uint256) {
2021-01-14 19:36:29 +08:00
if (lastRewardBlock == 0) {
2021-01-14 16:24:48 +08:00
return totalReward;
} else {
return totalReward + (block.number - lastRewardBlock) * dodoPerBlock;
}
}
2021-01-13 20:39:41 +08:00
// ============= Helper function ===============
2021-01-14 16:24:48 +08:00
function incentiveStatus(address fromToken, address toToken)
external
view
returns (
uint256 reward,
uint256 baseRate,
uint256 totalRate,
2021-01-18 22:32:12 +08:00
uint256 curTotalReward,
uint256 perBlockReward
2021-01-14 16:24:48 +08:00
)
{
baseRate = defaultRate;
uint256 fromRate = boosts[fromToken];
uint256 toRate = boosts[toToken];
totalRate = (fromRate >= toRate ? fromRate : toRate) + defaultRate;
uint256 _totalReward = _getTotalReward();
reward = ((_totalReward - totalDistribution) * totalRate) / 1000;
curTotalReward = _totalReward - totalDistribution;
2021-01-18 22:32:12 +08:00
perBlockReward = dodoPerBlock;
2021-01-13 20:39:41 +08:00
}
2021-01-14 16:24:48 +08:00
}