bsc trade incentive

This commit is contained in:
owen05
2021-03-14 10:02:20 +08:00
parent 01c5447802
commit 6b1864866a
15 changed files with 1523 additions and 34 deletions

View File

@@ -0,0 +1,99 @@
/*
Copyright 2020 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
pragma solidity 0.6.9;
import {InitializableOwnable} from "../lib/InitializableOwnable.sol";
import {ILockedTokenVault02} from "../intf/ILockedTokenVault02.sol";
import {SafeERC20} from "../lib/SafeERC20.sol";
import {SafeMath} from "../lib/SafeMath.sol";
import {IERC20} from "../intf/IERC20.sol";
/**
* @title DODOIncentiveBsc
* @author DODO Breeder
*
* @notice Trade Incentive in DODO platform
*/
contract DODOIncentiveBsc is InitializableOwnable {
using SafeMath for uint256;
using SafeERC20 for IERC20;
// ============ Storage ============
address public immutable _DODO_TOKEN_;
address public _DODO_PROXY_;
address public _LOCKED_VAULT_;
mapping(address => bool) public stableList;
mapping(address => bool) public tokenList; //Not include stable tokens
uint256 public baseAmount = 1000;
uint256 public baseReward = 10**18;
// ============ Events ============
event SetStableList(address token, bool isUse);
event SetTokenList(address token, bool isUse);
event SetBaseAmount(uint256 baseAmount);
event SetBaseReward(uint256 baseReward);
event Incentive(address user, uint256 reward);
constructor(address _dodoToken) public {
_DODO_TOKEN_ = _dodoToken;
}
// ============ Ownable ============
function setContract(address dodoProxy,address lockedVault) external onlyOwner {
_DODO_PROXY_ = dodoProxy;
_LOCKED_VAULT_ = lockedVault;
}
function setStableList(address token, bool isUse) external onlyOwner {
require(token != address(0));
stableList[token] = isUse;
emit SetStableList(token, isUse);
}
function setTokenList(address token, bool isUse) external onlyOwner {
require(token != address(0));
tokenList[token] = isUse;
emit SetTokenList(token, isUse);
}
function changeBaseAmount(uint256 newBaseAmount) external onlyOwner {
baseAmount = newBaseAmount;
emit SetBaseAmount(newBaseAmount);
}
function changeBaseReward(uint256 newBaseReward) external onlyOwner {
baseReward = newBaseReward;
emit SetBaseAmount(newBaseReward);
}
// ============ Incentive function ============
function triggerIncentive(
address fromToken,
address toToken,
uint256 fromAmount,
uint256 returnAmount,
address assetTo
) external {
require(msg.sender == _DODO_PROXY_, "DODOIncentiveBsc:Access restricted");
uint256 reward = 0;
if(stableList[fromToken] && tokenList[toToken]) {
reward = fromAmount.div(baseAmount);
} else if (stableList[toToken] && tokenList[fromToken]) {
reward = returnAmount.div(baseAmount);
}
if (reward != 0) {
ILockedTokenVault02(_LOCKED_VAULT_).tradeIncentive(assetTo,reward);
emit Incentive(assetTo, reward);
}
}
}

View File

@@ -0,0 +1,196 @@
/*
Copyright 2020 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
pragma solidity 0.6.9;
pragma experimental ABIEncoderV2;
import {InitializableOwnable} from "../lib/InitializableOwnable.sol";
import {SafeMath} from "../lib/SafeMath.sol";
import {DecimalMath} from "../lib/DecimalMath.sol";
import {SafeERC20} from "../lib/SafeERC20.sol";
import {IERC20} from "../intf/IERC20.sol";
/**
* @title LockedTokenVault02
* @author DODO Breeder
*
* @notice Lock Token and release it linearly
*/
contract LockedTokenVault02 is InitializableOwnable {
using SafeMath for uint256;
using SafeERC20 for IERC20;
address _TOKEN_;
address public _DODO_INCENTIVE_;
mapping(address => uint256) internal originBalances;
mapping(address => uint256) internal claimedBalances;
// uint256 public _UNDISTRIBUTED_AMOUNT_;
uint256 public _START_RELEASE_TIME_;
uint256 public _RELEASE_DURATION_;
uint256 public _CLIFF_RATE_;
bool public _DISTRIBUTE_FINISHED_;
// ============ Events ============
event Claim(address indexed holder, uint256 origin, uint256 claimed, uint256 amount);
// ============ Modifiers ============
modifier beforeStartRelease() {
require(block.timestamp < _START_RELEASE_TIME_, "RELEASE START");
_;
}
modifier afterStartRelease() {
require(block.timestamp >= _START_RELEASE_TIME_, "RELEASE NOT START");
_;
}
modifier distributeNotFinished() {
require(!_DISTRIBUTE_FINISHED_, "DISTRIBUTE FINISHED");
_;
}
// ============ Init Functions ============
constructor(
address _token,
uint256 _startReleaseTime,
uint256 _releaseDuration,
uint256 _cliffRate
) public {
_TOKEN_ = _token;
_START_RELEASE_TIME_ = _startReleaseTime;
_RELEASE_DURATION_ = _releaseDuration;
_CLIFF_RATE_ = _cliffRate;
}
function updateParams(uint256 startReleaseTime, uint256 releaseDuration, uint256 cliffRate) external onlyOwner {
_START_RELEASE_TIME_ = startReleaseTime;
_RELEASE_DURATION_ = releaseDuration;
_CLIFF_RATE_ = cliffRate;
}
function deposit(uint256 amount) external onlyOwner {
_tokenTransferIn(_OWNER_, amount);
// _UNDISTRIBUTED_AMOUNT_ = _UNDISTRIBUTED_AMOUNT_.add(amount);
}
function withdraw(uint256 amount) external onlyOwner {
// _UNDISTRIBUTED_AMOUNT_ = _UNDISTRIBUTED_AMOUNT_.sub(amount);
_tokenTransferOut(_OWNER_, amount);
}
function finishDistribute() external onlyOwner {
_DISTRIBUTE_FINISHED_ = true;
}
// ============ For DODOIncentiveBsc ===========
function tradeIncentive(address trader, uint256 amount) external {
require(_DODO_INCENTIVE_ == msg.sender, "LockedTokenVault02:Access restricted");
originBalances[trader] = originBalances[trader].add(amount);
// _UNDISTRIBUTED_AMOUNT_ = _UNDISTRIBUTED_AMOUNT_.sub(amount);
}
// ================== For Owner ================
function grant(address[] calldata holderList, uint256[] calldata amountList)
external
onlyOwner
{
require(holderList.length == amountList.length, "batch grant length not match");
uint256 amount = 0;
for (uint256 i = 0; i < holderList.length; ++i) {
// for saving gas, no event for grant
originBalances[holderList[i]] = originBalances[holderList[i]].add(amountList[i]);
amount = amount.add(amountList[i]);
}
// _UNDISTRIBUTED_AMOUNT_ = _UNDISTRIBUTED_AMOUNT_.sub(amount);
}
function recall(address holder) external onlyOwner distributeNotFinished {
// _UNDISTRIBUTED_AMOUNT_ = _UNDISTRIBUTED_AMOUNT_.add(originBalances[holder]).sub(
// claimedBalances[holder]
// );
originBalances[holder] = 0;
claimedBalances[holder] = 0;
}
// ============ For Holder ============
function transferLockedToken(address to) external {
originBalances[to] = originBalances[to].add(originBalances[msg.sender]);
claimedBalances[to] = claimedBalances[to].add(claimedBalances[msg.sender]);
originBalances[msg.sender] = 0;
claimedBalances[msg.sender] = 0;
}
function claim() external {
uint256 claimableToken = getClaimableBalance(msg.sender);
_tokenTransferOut(msg.sender, claimableToken);
claimedBalances[msg.sender] = claimedBalances[msg.sender].add(claimableToken);
emit Claim(
msg.sender,
originBalances[msg.sender],
claimedBalances[msg.sender],
claimableToken
);
}
// ============ View ============
function isReleaseStart() external view returns (bool) {
return block.timestamp >= _START_RELEASE_TIME_;
}
function getOriginBalance(address holder) external view returns (uint256) {
return originBalances[holder];
}
function getClaimedBalance(address holder) external view returns (uint256) {
return claimedBalances[holder];
}
function getClaimableBalance(address holder) public view returns (uint256) {
uint256 remainingToken = getRemainingBalance(holder);
return originBalances[holder].sub(remainingToken).sub(claimedBalances[holder]);
}
function getRemainingBalance(address holder) public view returns (uint256) {
uint256 remainingRatio = getRemainingRatio(block.timestamp);
return DecimalMath.mulFloor(originBalances[holder], remainingRatio);
}
function getRemainingRatio(uint256 timestamp) public view returns (uint256) {
if (timestamp < _START_RELEASE_TIME_) {
return DecimalMath.ONE;
}
uint256 timePast = timestamp.sub(_START_RELEASE_TIME_);
if (timePast < _RELEASE_DURATION_) {
uint256 remainingTime = _RELEASE_DURATION_.sub(timePast);
return DecimalMath.ONE.sub(_CLIFF_RATE_).mul(remainingTime).div(_RELEASE_DURATION_);
} else {
return 0;
}
}
// ============ Internal Helper ============
function _tokenTransferIn(address from, uint256 amount) internal {
IERC20(_TOKEN_).safeTransferFrom(from, address(this), amount);
}
function _tokenTransferOut(address to, uint256 amount) internal {
IERC20(_TOKEN_).safeTransfer(to, amount);
}
}