add buyout extend

This commit is contained in:
owen05
2021-06-17 14:04:46 +08:00
parent 1ce8eec5f5
commit 40a4c081de
11 changed files with 138 additions and 26 deletions

View File

@@ -0,0 +1,87 @@
/*
Copyright 2021 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
pragma solidity 0.6.9;
pragma experimental ABIEncoderV2;
import {InitializableOwnable} from "../../lib/InitializableOwnable.sol";
import {IERC20} from "../../intf/IERC20.sol";
import {SafeMath} from "../../lib/SafeMath.sol";
interface IBuyout {
function getBuyoutQualification(address user) external view returns (bool);
}
contract BuyoutModel is InitializableOwnable {
using SafeMath for uint256;
uint256 public _MIN_FRAG_ = 100; //0.1
uint256 public _MAX_FRAG_ = 1000; //1
int public _BUYOUT_FEE_ = 0;
struct FragInfo {
uint256 minFrag;
uint256 maxFrag;
address buyoutAddr;
bool isSet;
}
mapping(address => FragInfo) frags;
function addFragInfo(address fragAddr, uint256 minFrag, uint256 maxFrag, address buyoutAddr) external onlyOwner {
FragInfo memory fragInfo = FragInfo({
minFrag: minFrag,
maxFrag: maxFrag,
buyoutAddr: buyoutAddr,
isSet: true
});
frags[fragAddr] = fragInfo;
}
function setFragInfo(address fragAddr, uint256 minFrag, uint256 maxFrag, address buyoutAddr) external onlyOwner {
frags[fragAddr].minFrag = minFrag;
frags[fragAddr].maxFrag = maxFrag;
frags[fragAddr].buyoutAddr = buyoutAddr;
}
function setGlobalParam(uint256 minFrag, uint256 maxFrag, uint256 buyoutFee) external onlyOwner {
require(minFrag <= 1000 && maxFrag <= 1000, "PARAM_INVALID");
_MIN_FRAG_ = minFrag;
_MAX_FRAG_ = maxFrag;
_BUYOUT_FEE_ = int(buyoutFee);
}
function getBuyoutStatus(address fragAddr, address user) external view returns (int) {
FragInfo memory fragInfo = frags[fragAddr];
uint256 userBalance = IERC20(fragAddr).balanceOf(user);
uint256 totalSupply = IERC20(fragAddr).totalSupply();
uint256 minFrag = _MIN_FRAG_;
uint256 maxFrag = _MAX_FRAG_;
if(fragInfo.isSet) {
address buyoutAddr = fragInfo.buyoutAddr;
if(buyoutAddr != address(0)) {
bool isQualified = IBuyout(buyoutAddr).getBuyoutQualification(user);
if(isQualified) {
return _BUYOUT_FEE_;
}else {
return -1;
}
}
minFrag = fragInfo.minFrag;
maxFrag = fragInfo.maxFrag;
}
if(userBalance >= totalSupply.mul(minFrag).div(1000) && userBalance <= totalSupply.mul(maxFrag).div(1000)) {
return _BUYOUT_FEE_;
}else {
return -1;
}
}
}

View File

@@ -16,6 +16,10 @@ import {IERC20} from "../../intf/IERC20.sol";
import {InitializableERC20} from "../../external/ERC20/InitializableERC20.sol";
import {ICollateralVault} from "../../CollateralVault/intf/ICollateralVault.sol";
interface IBuyoutModel {
function getBuyoutStatus(address fragAddr, address user) external view returns (int);
}
contract Fragment is InitializableERC20 {
using SafeMath for uint256;
using SafeERC20 for IERC20;
@@ -25,7 +29,6 @@ contract Fragment is InitializableERC20 {
bool public _IS_BUYOUT_;
uint256 public _BUYOUT_TIMESTAMP_;
uint256 public _BUYOUT_PRICE_;
uint256 public _DEFAULT_BUYOUT_FEE_;
uint256 public _DISTRIBUTION_RATIO_;
address public _COLLATERAL_VAULT_;
@@ -33,6 +36,7 @@ contract Fragment is InitializableERC20 {
address public _QUOTE_;
address public _DVM_;
address public _DEFAULT_MAINTAINER_;
address public _BUYOUT_MODEL_;
bool internal _FRAG_INITIALIZED_;
@@ -53,7 +57,7 @@ contract Fragment is InitializableERC20 {
uint256 ownerRatio,
uint256 buyoutTimestamp,
address defaultMaintainer,
uint256 defaultBuyoutFee,
address buyoutModel,
uint256 distributionRatio,
string memory _symbol
) external {
@@ -67,7 +71,7 @@ contract Fragment is InitializableERC20 {
_COLLATERAL_VAULT_ = collateralVault;
_BUYOUT_TIMESTAMP_ = buyoutTimestamp;
_DEFAULT_MAINTAINER_ = defaultMaintainer;
_DEFAULT_BUYOUT_FEE_ = defaultBuyoutFee;
_BUYOUT_MODEL_ = buyoutModel;
_DISTRIBUTION_RATIO_ = distributionRatio;
// init FRAG meta data
@@ -93,6 +97,10 @@ contract Fragment is InitializableERC20 {
require(_BUYOUT_TIMESTAMP_ != 0, "DODOFragment: NOT_SUPPORT_BUYOUT");
require(block.timestamp > _BUYOUT_TIMESTAMP_, "DODOFragment: BUYOUT_NOT_START");
require(!_IS_BUYOUT_, "DODOFragment: ALREADY_BUYOUT");
int buyoutFee = IBuyoutModel(_BUYOUT_MODEL_).getBuyoutStatus(address(this), newVaultOwner);
require(buyoutFee != -1, "DODOFragment: USER_UNABLE_BUYOUT");
_IS_BUYOUT_ = true;
_BUYOUT_PRICE_ = IDVM(_DVM_).getMidPrice();
@@ -114,10 +122,10 @@ contract Fragment is InitializableERC20 {
_clearBalance(address(this));
_clearBalance(_VAULT_PRE_OWNER_);
uint256 buyoutFee = DecimalMath.mulFloor(ownerQuoteWithoutFee, _DEFAULT_BUYOUT_FEE_);
uint256 buyoutFeeAmount = DecimalMath.mulFloor(ownerQuoteWithoutFee, uint256(buyoutFee));
IERC20(_QUOTE_).safeTransfer(_DEFAULT_MAINTAINER_, buyoutFee);
IERC20(_QUOTE_).safeTransfer(_VAULT_PRE_OWNER_, ownerQuoteWithoutFee.sub(buyoutFee));
IERC20(_QUOTE_).safeTransfer(_DEFAULT_MAINTAINER_, buyoutFeeAmount);
IERC20(_QUOTE_).safeTransfer(_VAULT_PRE_OWNER_, ownerQuoteWithoutFee.sub(buyoutFeeAmount));
ICollateralVault(_COLLATERAL_VAULT_).directTransferOwnership(newVaultOwner);