Files
dodo-contractV2/contracts/GeneralizedFragment/impl/Fragment.sol

149 lines
4.9 KiB
Solidity
Raw Normal View History

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_;
2021-04-02 11:49:05 +08:00
bool public _IS_OPEN_BUYOUT_;
2021-03-31 14:10:27 +08:00
uint256 public _BUYOUT_TIMESTAMP_;
uint256 public _BUYOUT_PRICE_;
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-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,
uint256 totalSupply,
2021-03-18 23:43:54 +08:00
uint256 ownerRatio,
2021-04-08 00:31:25 +08:00
uint256 buyoutTimestamp
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-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-02 11:49:05 +08:00
super.init(address(this), totalSupply, name, symbol, decimals);
2021-03-18 19:54:58 +08:00
// init FRAG distribution
2021-04-02 11:49:05 +08:00
uint256 vaultPreOwnerBalance = DecimalMath.mulFloor(totalSupply, ownerRatio);
2021-04-09 20:07:39 +08:00
_transfer(address(this), _VAULT_PRE_OWNER_, vaultPreOwnerBalance);
_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-13 15:21:49 +08:00
require(_BUYOUT_TIMESTAMP_ != 0, "DODOFragment: NOT_SUPPORT_BUYOUT");
2021-04-02 11:49:05 +08:00
require(block.timestamp > _BUYOUT_TIMESTAMP_, "DODOFragment: BUYOUT_NOT_START");
require(!_IS_BUYOUT_, "DODOFragment: ALREADY_BUYOUT");
2021-03-18 19:54:58 +08:00
_IS_BUYOUT_ = true;
2021-04-02 11:49:05 +08:00
2021-03-18 19:54:58 +08:00
_BUYOUT_PRICE_ = IDVM(_DVM_).getMidPrice();
uint256 requireQuote = DecimalMath.mulCeil(_BUYOUT_PRICE_, totalSupply);
2021-04-02 11:49:05 +08:00
require(IERC20(_QUOTE_).balanceOf(address(this)) >= requireQuote, "DODOFragment: QUOTE_NOT_ENOUGH");
2021-03-18 19:54:58 +08:00
IDVM(_DVM_).sellShares(
IERC20(_DVM_).balanceOf(address(this)),
address(this),
0,
0,
"",
uint256(-1)
2021-04-02 11:49:05 +08:00
);
2021-03-18 19:54:58 +08:00
2021-04-13 15:21:49 +08:00
_clearBalance(address(this));
2021-04-02 11:49:05 +08:00
uint256 preOwnerQuote = DecimalMath.mulFloor(_BUYOUT_PRICE_, balances[_VAULT_PRE_OWNER_]);
IERC20(_QUOTE_).safeTransfer(_VAULT_PRE_OWNER_, preOwnerQuote);
_clearBalance(_VAULT_PRE_OWNER_);
2021-03-18 19:54:58 +08:00
2021-04-02 11:49:05 +08:00
uint256 newOwnerQuote = DecimalMath.mulFloor(_BUYOUT_PRICE_, balances[newVaultOwner]);
IERC20(_QUOTE_).safeTransfer(newVaultOwner, newOwnerQuote);
_clearBalance(newVaultOwner);
ICollateralVault(_COLLATERAL_VAULT_).directTransferOwnership(newVaultOwner);
2021-04-06 18:47:35 +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-02 11:49:05 +08:00
require(_IS_BUYOUT_, "DODOFragment: NEED_BUYOUT");
2021-03-31 14:10:27 +08:00
2021-04-06 18:47:35 +08:00
uint256 baseAmount = balances[msg.sender];
uint256 quoteAmount = DecimalMath.mulFloor(_BUYOUT_PRICE_, baseAmount);
2021-04-04 01:05:08 +08:00
IERC20(_QUOTE_).safeTransfer(to, quoteAmount);
_clearBalance(msg.sender);
if (data.length > 0) {
IDODOCallee(to).NFTRedeemCall(
msg.sender,
quoteAmount,
data
);
}
2021-04-06 18:47:35 +08:00
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-13 15:21:49 +08:00
require(_BUYOUT_TIMESTAMP_ != 0, "NOT SUPPORT BUYOUT");
2021-03-18 23:43:54 +08:00
require(!_IS_BUYOUT_, "ALREADY BUYOUT");
2021-03-18 19:54:58 +08:00
uint256 price = IDVM(_DVM_).getMidPrice();
requireQuote = DecimalMath.mulCeil(price, totalSupply);
}
2021-04-02 11:49:05 +08:00
function _clearBalance(address account) internal {
uint256 clearBalance = balances[account];
balances[account] = 0;
balances[address(0)] = clearBalance;
emit Transfer(account, address(0), clearBalance);
2021-03-18 19:54:58 +08:00
}
}