2021-03-18 19:54:58 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
Copyright 2020 DODO ZOO.
|
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
pragma solidity 0.6.9;
|
|
|
|
|
|
2021-04-04 01:05:08 +08:00
|
|
|
import {SafeMath} from "../../lib/SafeMath.sol";
|
|
|
|
|
import {SafeERC20} from "../../lib/SafeERC20.sol";
|
|
|
|
|
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
|
|
|
|
import {IDVM} from "../../DODOVendingMachine/intf/IDVM.sol";
|
|
|
|
|
import {IDODOCallee} from "../../intf/IDODOCallee.sol";
|
|
|
|
|
import {IERC20} from "../../intf/IERC20.sol";
|
|
|
|
|
import {InitializableERC20} from "../../external/ERC20/InitializableERC20.sol";
|
|
|
|
|
import {ICollateralVault} from "../../CollateralVault/intf/ICollateralVault.sol";
|
2021-03-31 14:10:27 +08:00
|
|
|
|
2021-04-02 11:49:05 +08:00
|
|
|
contract Fragment is InitializableERC20 {
|
2021-03-18 19:54:58 +08:00
|
|
|
using SafeMath for uint256;
|
2021-03-30 18:25:39 +08:00
|
|
|
using SafeERC20 for IERC20;
|
2021-03-18 19:54:58 +08:00
|
|
|
|
2021-03-31 14:10:27 +08:00
|
|
|
// ============ Storage ============
|
|
|
|
|
|
|
|
|
|
bool public _IS_BUYOUT_;
|
|
|
|
|
uint256 public _BUYOUT_TIMESTAMP_;
|
|
|
|
|
uint256 public _BUYOUT_PRICE_;
|
2021-04-28 15:07:28 +08:00
|
|
|
uint256 public _DEFAULT_BUYOUT_FEE_;
|
2021-05-25 14:29:49 +08:00
|
|
|
uint256 public _DISTRIBUTION_RATIO_;
|
2021-03-18 19:54:58 +08:00
|
|
|
|
2021-03-31 14:10:27 +08:00
|
|
|
address public _COLLATERAL_VAULT_;
|
2021-04-02 11:49:05 +08:00
|
|
|
address public _VAULT_PRE_OWNER_;
|
2021-03-31 14:10:27 +08:00
|
|
|
address public _QUOTE_;
|
|
|
|
|
address public _DVM_;
|
2021-04-28 15:07:28 +08:00
|
|
|
address public _DEFAULT_MAINTAINER_;
|
2021-03-18 19:54:58 +08:00
|
|
|
|
2021-04-02 11:49:05 +08:00
|
|
|
bool internal _FRAG_INITIALIZED_;
|
|
|
|
|
|
2021-04-06 18:47:35 +08:00
|
|
|
// ============ Event ============
|
|
|
|
|
event RemoveNftToken(address nftContract, uint256 tokenId, uint256 amount);
|
|
|
|
|
event AddNftToken(address nftContract, uint256 tokenId, uint256 amount);
|
|
|
|
|
event InitInfo(address vault, string name, string baseURI);
|
|
|
|
|
event CreateFragment();
|
|
|
|
|
event Buyout(address newOwner);
|
|
|
|
|
event Redeem(address sender, uint256 baseAmount, uint256 quoteAmount);
|
|
|
|
|
|
|
|
|
|
|
2021-03-18 19:54:58 +08:00
|
|
|
function init(
|
|
|
|
|
address dvm,
|
2021-04-02 11:49:05 +08:00
|
|
|
address vaultPreOwner,
|
|
|
|
|
address collateralVault,
|
2021-04-28 00:04:40 +08:00
|
|
|
uint256 _totalSupply,
|
2021-03-18 23:43:54 +08:00
|
|
|
uint256 ownerRatio,
|
2021-04-28 15:07:28 +08:00
|
|
|
uint256 buyoutTimestamp,
|
|
|
|
|
address defaultMaintainer,
|
2021-05-25 14:29:49 +08:00
|
|
|
uint256 defaultBuyoutFee,
|
|
|
|
|
uint256 distributionRatio
|
2021-03-18 19:54:58 +08:00
|
|
|
) external {
|
2021-04-02 11:49:05 +08:00
|
|
|
require(!_FRAG_INITIALIZED_, "DODOFragment: ALREADY_INITIALIZED");
|
|
|
|
|
_FRAG_INITIALIZED_ = true;
|
|
|
|
|
|
2021-03-18 19:54:58 +08:00
|
|
|
// init local variables
|
|
|
|
|
_DVM_ = dvm;
|
2021-03-30 18:25:39 +08:00
|
|
|
_QUOTE_ = IDVM(_DVM_)._QUOTE_TOKEN_();
|
2021-04-02 11:49:05 +08:00
|
|
|
_VAULT_PRE_OWNER_ = vaultPreOwner;
|
|
|
|
|
_COLLATERAL_VAULT_ = collateralVault;
|
2021-03-18 23:43:54 +08:00
|
|
|
_BUYOUT_TIMESTAMP_ = buyoutTimestamp;
|
2021-04-28 15:07:28 +08:00
|
|
|
_DEFAULT_MAINTAINER_ = defaultMaintainer;
|
|
|
|
|
_DEFAULT_BUYOUT_FEE_ = defaultBuyoutFee;
|
2021-05-25 14:29:49 +08:00
|
|
|
_DISTRIBUTION_RATIO_ = distributionRatio;
|
2021-03-18 19:54:58 +08:00
|
|
|
|
2021-03-18 23:43:54 +08:00
|
|
|
// init FRAG meta data
|
2021-04-02 11:49:05 +08:00
|
|
|
string memory prefix = "FRAG_";
|
|
|
|
|
name = string(abi.encodePacked(prefix, IDVM(_DVM_).addressToShortString(_COLLATERAL_VAULT_)));
|
2021-03-18 19:54:58 +08:00
|
|
|
symbol = "FRAG";
|
|
|
|
|
decimals = 18;
|
2021-04-28 00:04:40 +08:00
|
|
|
super.init(address(this), _totalSupply, name, symbol, decimals);
|
2021-03-18 19:54:58 +08:00
|
|
|
|
|
|
|
|
// init FRAG distribution
|
2021-04-28 00:04:40 +08:00
|
|
|
uint256 vaultPreOwnerBalance = DecimalMath.mulFloor(_totalSupply, ownerRatio);
|
2021-05-25 14:29:49 +08:00
|
|
|
uint256 distributionBalance = DecimalMath.mulFloor(vaultPreOwnerBalance, distributionRatio);
|
|
|
|
|
|
|
|
|
|
if(distributionBalance > 0) _transfer(address(this), _DEFAULT_MAINTAINER_, distributionBalance);
|
|
|
|
|
_transfer(address(this), _VAULT_PRE_OWNER_, vaultPreOwnerBalance.sub(distributionBalance));
|
2021-04-28 00:04:40 +08:00
|
|
|
_transfer(address(this), _DVM_, _totalSupply.sub(vaultPreOwnerBalance));
|
2021-03-18 19:54:58 +08:00
|
|
|
|
|
|
|
|
// init DVM liquidity
|
2021-03-30 18:25:39 +08:00
|
|
|
IDVM(_DVM_).buyShares(address(this));
|
2021-03-18 19:54:58 +08:00
|
|
|
}
|
|
|
|
|
|
2021-04-02 11:49:05 +08:00
|
|
|
|
|
|
|
|
function buyout(address newVaultOwner) external {
|
2021-04-29 01:17:44 +08:00
|
|
|
require(_BUYOUT_TIMESTAMP_ != 0, "DODOFragment: NOT_SUPPORT_BUYOUT");
|
|
|
|
|
require(block.timestamp > _BUYOUT_TIMESTAMP_, "DODOFragment: BUYOUT_NOT_START");
|
|
|
|
|
require(!_IS_BUYOUT_, "DODOFragment: ALREADY_BUYOUT");
|
|
|
|
|
_IS_BUYOUT_ = true;
|
2021-04-02 11:49:05 +08:00
|
|
|
|
2021-04-29 01:17:44 +08:00
|
|
|
_BUYOUT_PRICE_ = IDVM(_DVM_).getMidPrice();
|
|
|
|
|
uint256 requireQuote = DecimalMath.mulCeil(_BUYOUT_PRICE_, totalSupply);
|
|
|
|
|
uint256 payQuote = IERC20(_QUOTE_).balanceOf(address(this));
|
|
|
|
|
require(payQuote >= requireQuote, "DODOFragment: QUOTE_NOT_ENOUGH");
|
|
|
|
|
|
|
|
|
|
IDVM(_DVM_).sellShares(
|
|
|
|
|
IERC20(_DVM_).balanceOf(address(this)),
|
|
|
|
|
address(this),
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
"",
|
|
|
|
|
uint256(-1)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
uint256 redeemFrag = totalSupply.sub(balances[address(this)]).sub(balances[_VAULT_PRE_OWNER_]);
|
|
|
|
|
uint256 ownerQuoteWithoutFee = IERC20(_QUOTE_).balanceOf(address(this)).sub(DecimalMath.mulCeil(_BUYOUT_PRICE_, redeemFrag));
|
|
|
|
|
_clearBalance(address(this));
|
|
|
|
|
_clearBalance(_VAULT_PRE_OWNER_);
|
|
|
|
|
|
|
|
|
|
uint256 buyoutFee = DecimalMath.mulFloor(ownerQuoteWithoutFee, _DEFAULT_BUYOUT_FEE_);
|
2021-04-28 15:07:28 +08:00
|
|
|
|
2021-04-29 01:17:44 +08:00
|
|
|
IERC20(_QUOTE_).safeTransfer(_DEFAULT_MAINTAINER_, buyoutFee);
|
|
|
|
|
IERC20(_QUOTE_).safeTransfer(_VAULT_PRE_OWNER_, ownerQuoteWithoutFee.sub(buyoutFee));
|
2021-04-02 11:49:05 +08:00
|
|
|
|
2021-04-29 01:17:44 +08:00
|
|
|
ICollateralVault(_COLLATERAL_VAULT_).directTransferOwnership(newVaultOwner);
|
2021-04-06 18:47:35 +08:00
|
|
|
|
2021-04-29 01:17:44 +08:00
|
|
|
emit Buyout(newVaultOwner);
|
2021-03-18 19:54:58 +08:00
|
|
|
}
|
|
|
|
|
|
2021-04-02 11:49:05 +08:00
|
|
|
|
2021-04-04 01:05:08 +08:00
|
|
|
function redeem(address to, bytes calldata data) external {
|
2021-04-29 01:17:44 +08:00
|
|
|
require(_IS_BUYOUT_, "DODOFragment: NEED_BUYOUT");
|
|
|
|
|
|
|
|
|
|
uint256 baseAmount = balances[msg.sender];
|
|
|
|
|
uint256 quoteAmount = DecimalMath.mulFloor(_BUYOUT_PRICE_, baseAmount);
|
|
|
|
|
_clearBalance(msg.sender);
|
|
|
|
|
IERC20(_QUOTE_).safeTransfer(to, quoteAmount);
|
|
|
|
|
|
|
|
|
|
if (data.length > 0) {
|
|
|
|
|
IDODOCallee(to).NFTRedeemCall(
|
|
|
|
|
msg.sender,
|
|
|
|
|
quoteAmount,
|
|
|
|
|
data
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emit Redeem(msg.sender, baseAmount, quoteAmount);
|
2021-03-18 19:54:58 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-30 18:25:39 +08:00
|
|
|
function getBuyoutRequirement() external view returns (uint256 requireQuote){
|
2021-04-29 01:17:44 +08:00
|
|
|
require(_BUYOUT_TIMESTAMP_ != 0, "NOT SUPPORT BUYOUT");
|
|
|
|
|
require(!_IS_BUYOUT_, "ALREADY BUYOUT");
|
|
|
|
|
uint256 price = IDVM(_DVM_).getMidPrice();
|
|
|
|
|
requireQuote = DecimalMath.mulCeil(price, totalSupply);
|
2021-03-18 19:54:58 +08:00
|
|
|
}
|
|
|
|
|
|
2021-04-02 11:49:05 +08:00
|
|
|
function _clearBalance(address account) internal {
|
2021-04-29 01:17:44 +08:00
|
|
|
uint256 clearBalance = balances[account];
|
|
|
|
|
balances[account] = 0;
|
|
|
|
|
balances[address(0)] = balances[address(0)].add(clearBalance);
|
|
|
|
|
emit Transfer(account, address(0), clearBalance);
|
2021-03-18 19:54:58 +08:00
|
|
|
}
|
|
|
|
|
}
|