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

107 lines
3.2 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;
import {SafeMath} from "../lib/SafeMath.sol";
import {SafeERC20} from "../lib/SafeERC20.sol";
2021-03-30 18:25:39 +08:00
import {DecimalMath} from "../lib/DecimalMath.sol";
2021-03-18 19:54:58 +08:00
import {IDVM} from "../DODOVendingMachine/intf/IDVM.sol";
import {IERC20} from "../intf/IERC20.sol";
2021-03-30 18:25:39 +08:00
import {InitializableMintableERC20} from "../external/ERC20/InitializableMintableERC20.sol";
2021-03-18 19:54:58 +08:00
2021-03-31 14:10:27 +08:00
//TODO?why mintable
2021-03-18 19:54:58 +08:00
contract Fragment is InitializableMintableERC20 {
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-03-18 19:54:58 +08:00
2021-03-31 14:10:27 +08:00
address public _COLLATERAL_VAULT_;
address public _QUOTE_;
address public _DVM_;
2021-03-18 19:54:58 +08:00
function init(
address owner,
address dvm,
address collateralVault,
uint256 supply,
2021-03-18 23:43:54 +08:00
uint256 ownerRatio,
uint256 buyoutTimestamp
2021-03-18 19:54:58 +08:00
) external {
// init local variables
initOwner(owner);
_DVM_ = dvm;
_COLLATERAL_VAULT_ = collateralVault;
2021-03-30 18:25:39 +08:00
_QUOTE_ = IDVM(_DVM_)._QUOTE_TOKEN_();
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-03-18 19:54:58 +08:00
string memory suffix = "FRAG_";
2021-03-30 18:25:39 +08:00
name = string(abi.encodePacked(suffix, IDVM(_DVM_).addressToShortString(_COLLATERAL_VAULT_)));
2021-03-18 19:54:58 +08:00
symbol = "FRAG";
decimals = 18;
// init FRAG distribution
totalSupply = supply;
balances[owner] = DecimalMath.mulFloor(supply, ownerRatio);
2021-03-31 14:10:27 +08:00
balances[dvm] = supply.sub(balances[owner]);
2021-03-18 19:54:58 +08:00
emit Transfer(address(0), owner, balances[owner]);
emit Transfer(address(0), dvm, balances[dvm]);
// 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-03-31 14:10:27 +08:00
//需要先转入QUOTE
2021-03-18 19:54:58 +08:00
function buyout() external {
2021-03-18 23:43:54 +08:00
require(!_IS_BUYOUT_, "ALREADY BUYOUT");
2021-03-18 19:54:58 +08:00
_IS_BUYOUT_ = true;
_BUYOUT_PRICE_ = IDVM(_DVM_).getMidPrice();
uint256 requireQuote = DecimalMath.mulCeil(_BUYOUT_PRICE_, totalSupply);
require(IERC20(_QUOTE_).balanceOf(address(this))>=requireQuote, "QUOTE NOT ENOUGH");
IDVM(_DVM_).sellShares(
IERC20(_DVM_).balanceOf(address(this)),
address(this),
0,
0,
"",
uint256(-1)
);
2021-03-30 18:25:39 +08:00
uint256 ownerQuote = DecimalMath.mulFloor(_BUYOUT_PRICE_, balances[address(this)]);
2021-03-18 19:54:58 +08:00
_clearSelfBalance();
2021-03-30 18:25:39 +08:00
IERC20(_QUOTE_).safeTransfer(_OWNER_, ownerQuote);
2021-03-18 19:54:58 +08:00
}
2021-03-31 14:10:27 +08:00
// buyout之后的恒定兑换需要先转入FRAG
2021-03-18 19:54:58 +08:00
function redeem(address to) external {
2021-03-18 23:43:54 +08:00
require(_IS_BUYOUT_, "NEED BUYOUT");
2021-03-31 14:10:27 +08:00
2021-03-30 18:25:39 +08:00
IERC20(_QUOTE_).safeTransfer(to, DecimalMath.mulFloor(_BUYOUT_PRICE_, balances[address(this)]));
2021-03-18 19:54:58 +08:00
_clearSelfBalance();
}
2021-03-30 18:25:39 +08:00
function getBuyoutRequirement() external view returns (uint256 requireQuote){
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);
}
function _clearSelfBalance() internal {
emit Transfer(address(this), address(0), balances[address(this)]);
balances[address(this)] = 0;
}
}