ing
This commit is contained in:
@@ -14,56 +14,153 @@ import {IERC721Receiver} from "../../intf/IERC721Receiver.sol";
|
||||
import {IERC1155} from "../../intf/IERC1155.sol";
|
||||
import {IERC1155Receiver} from "../../intf/IERC1155Receiver.sol";
|
||||
|
||||
|
||||
contract NFTCollateralVault is InitializableOwnable, IERC721Receiver, IERC1155Receiver {
|
||||
using SafeMath for uint256;
|
||||
|
||||
function directTransferOwnership(address newOwner) public onlyOwner {
|
||||
emit OwnershipTransferred(_OWNER_, newOwner);
|
||||
// ============ Storage ============
|
||||
string public name;
|
||||
struct NftInfo {
|
||||
uint256 tokenId;
|
||||
uint256 amount;
|
||||
address nftContract;
|
||||
}
|
||||
NftInfo[] public nftInfos;
|
||||
|
||||
constructor (string memory _name) public {
|
||||
name = _name;
|
||||
}
|
||||
|
||||
// ============ Event ============
|
||||
event RemoveNftToken(address nftContract, uint256 tokenId, uint256 amount);
|
||||
event AddNftToken(address nftContract, uint256 tokenId, uint256 amount);
|
||||
|
||||
|
||||
// ============ Ownable ============
|
||||
function directTransferOwnership(address newOwner) external onlyOwner {
|
||||
_OWNER_ = newOwner;
|
||||
emit OwnershipTransferred(_OWNER_, newOwner);
|
||||
}
|
||||
|
||||
//TODO? assetTo
|
||||
function withdrawERC721(address nftContract, uint256 tokenId) public onlyOwner {
|
||||
IERC721(nftContract).safeTransferFrom(msg.sender, _OWNER_, tokenId, "");
|
||||
// IERC721(nftContract).safeTransferFrom(address(this), _OWNER_, tokenId, "");
|
||||
function createFragment(address nftProxy, bytes calldata data) external onlyOwner {
|
||||
require(nftProxy != address(0), "DODONftVault: PROXY_INVALID");
|
||||
_OWNER_ = nftProxy;
|
||||
emit OwnershipTransferred(_OWNER_, nftProxy);
|
||||
(bool success, ) = nftProxy.call(data);
|
||||
require(success, "DODONftVault: TRANSFER_OWNER_FAILED");
|
||||
}
|
||||
|
||||
//TODO? assetTo
|
||||
function withdrawERC1155(address nftContract, uint256[] memory tokenIds, uint256[] memory amounts) public onlyOwner {
|
||||
IERC1155(nftContract).safeBatchTransferFrom(msg.sender, _OWNER_, tokenIds, amounts, "");
|
||||
// IERC1155(nftContract).safeBatchTransferFrom(address(this), _OWNER_, tokenIds, amounts, "");
|
||||
function withdrawERC721(address nftContract, uint256 tokenId) external onlyOwner {
|
||||
_removeNftInfo(nftContract, tokenId, 1);
|
||||
IERC721(nftContract).safeTransferFrom(address(this), _OWNER_, tokenId, "");
|
||||
}
|
||||
|
||||
function withdrawERC1155(address nftContract, uint256[] memory tokenIds, uint256[] memory amounts) external onlyOwner {
|
||||
require(tokenIds.length == amounts.length, "PARAMS_NOT_MATCH");
|
||||
for(uint256 i = 0; i < tokenIds.length; i++) {
|
||||
_removeNftInfo(nftContract, tokenIds[i], amounts[i]);
|
||||
}
|
||||
IERC1155(nftContract).safeBatchTransferFrom(address(this), _OWNER_, tokenIds, amounts, "");
|
||||
}
|
||||
|
||||
// ============ View ============
|
||||
|
||||
function getNftInfoById(uint256 i) external view returns (address nftContract, uint256 tokenId, uint256 amount) {
|
||||
require(i < nftInfos.length, "ID_OVERFLOW");
|
||||
NftInfo memory ni = nftInfos[i];
|
||||
nftContract = ni.nftContract;
|
||||
tokenId = ni.tokenId;
|
||||
amount = ni.amount;
|
||||
}
|
||||
|
||||
function getIdByTokenIdAndAddr(address nftContract, uint256 tokenId) external view returns(uint256) {
|
||||
uint256 len = nftInfos.length;
|
||||
for (uint256 i = 0; i < len; i++) {
|
||||
if (nftContract == nftInfos[i].nftContract && tokenId == nftInfos[i].tokenId) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
require(false, "TOKEN_ID_NOT_FOUND");
|
||||
}
|
||||
|
||||
function supportsInterface(bytes4 interfaceId) public override view returns (bool) {
|
||||
return interfaceId == type(IERC1155Receiver).interfaceId
|
||||
|| interfaceId == type(IERC721Receiver).interfaceId;
|
||||
}
|
||||
|
||||
// ============ Callback ============
|
||||
function onERC721Received(
|
||||
address,
|
||||
address,
|
||||
uint256,
|
||||
uint256 tokenId,
|
||||
bytes calldata
|
||||
) external override returns (bytes4) {
|
||||
_addNftInfo(msg.sender, tokenId, 1);
|
||||
return IERC721Receiver.onERC721Received.selector;
|
||||
}
|
||||
|
||||
function onERC1155Received(
|
||||
address,
|
||||
address,
|
||||
uint256,
|
||||
uint256,
|
||||
uint256 id,
|
||||
uint256 value,
|
||||
bytes calldata
|
||||
) external override returns (bytes4){
|
||||
_addNftInfo(msg.sender, id, value);
|
||||
return IERC1155Receiver.onERC1155Received.selector;
|
||||
}
|
||||
|
||||
function onERC1155BatchReceived(
|
||||
address,
|
||||
address,
|
||||
uint256[] calldata,
|
||||
uint256[] calldata,
|
||||
uint256[] calldata ids,
|
||||
uint256[] calldata values,
|
||||
bytes calldata
|
||||
) external override returns (bytes4){
|
||||
require(ids.length == values.length, "PARAMS_NOT_MATCH");
|
||||
for(uint256 i = 0; i < ids.length; i++) {
|
||||
_addNftInfo(msg.sender, ids[i], values[i]);
|
||||
}
|
||||
return IERC1155Receiver.onERC1155BatchReceived.selector;
|
||||
}
|
||||
|
||||
function supportsInterface(bytes4 interfaceId) public override view returns (bool) {
|
||||
return interfaceId == type(IERC1155).interfaceId
|
||||
|| interfaceId == type(IERC721).interfaceId;
|
||||
// ========== internal ===============
|
||||
function _addNftInfo(address nftContract, uint256 tokenId, uint256 addAmount) internal {
|
||||
uint256 len = nftInfos.length;
|
||||
for(uint256 i = 0; i < len; i++) {
|
||||
NftInfo memory nftInfo = nftInfos[i];
|
||||
if (nftContract == nftInfo.nftContract && tokenId == nftInfo.tokenId) {
|
||||
nftInfos[i].amount = nftInfo.amount.add(addAmount);
|
||||
emit AddNftToken(nftContract, tokenId, addAmount);
|
||||
return;
|
||||
}
|
||||
}
|
||||
nftInfos.push(
|
||||
NftInfo({
|
||||
tokenId: tokenId,
|
||||
amount: addAmount,
|
||||
nftContract: nftContract
|
||||
})
|
||||
);
|
||||
emit AddNftToken(nftContract, tokenId, addAmount);
|
||||
}
|
||||
|
||||
function _removeNftInfo(address nftContract, uint256 tokenId, uint256 removeAmount) internal {
|
||||
uint256 len = nftInfos.length;
|
||||
for (uint256 i = 0; i < len; i++) {
|
||||
NftInfo memory nftInfo = nftInfos[i];
|
||||
if (nftContract == nftInfo.nftContract && tokenId == nftInfo.tokenId) {
|
||||
if(removeAmount >= nftInfo.amount) {
|
||||
if(i != len - 1) {
|
||||
nftInfos[i] = nftInfos[len - 1];
|
||||
}
|
||||
nftInfos.pop();
|
||||
}else {
|
||||
nftInfos[i].amount = nftInfo.amount.sub(removeAmount);
|
||||
}
|
||||
emit RemoveNftToken(nftContract, tokenId, removeAmount);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,63 +12,77 @@ import {SafeERC20} from "../lib/SafeERC20.sol";
|
||||
import {DecimalMath} from "../lib/DecimalMath.sol";
|
||||
import {IDVM} from "../DODOVendingMachine/intf/IDVM.sol";
|
||||
import {IERC20} from "../intf/IERC20.sol";
|
||||
import {InitializableMintableERC20} from "../external/ERC20/InitializableMintableERC20.sol";
|
||||
import {InitializableERC20} from "../external/ERC20/InitializableERC20.sol";
|
||||
|
||||
interface ICollateralVault {
|
||||
function directTransferOwnership(address newOwner) external;
|
||||
}
|
||||
|
||||
|
||||
//TODO?:why mintable
|
||||
contract Fragment is InitializableMintableERC20 {
|
||||
contract Fragment is InitializableERC20 {
|
||||
using SafeMath for uint256;
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
// ============ Storage ============
|
||||
|
||||
bool public _IS_BUYOUT_;
|
||||
bool public _IS_OPEN_BUYOUT_;
|
||||
uint256 public _BUYOUT_TIMESTAMP_;
|
||||
uint256 public _BUYOUT_PRICE_;
|
||||
|
||||
address public _COLLATERAL_VAULT_;
|
||||
address public _VAULT_PRE_OWNER_;
|
||||
address public _QUOTE_;
|
||||
address public _DVM_;
|
||||
|
||||
bool internal _FRAG_INITIALIZED_;
|
||||
|
||||
function init(
|
||||
address owner,
|
||||
address dvm,
|
||||
address collateralVault,
|
||||
uint256 supply,
|
||||
address vaultPreOwner,
|
||||
address collateralVault,
|
||||
uint256 totalSupply,
|
||||
uint256 ownerRatio,
|
||||
uint256 buyoutTimestamp
|
||||
uint256 buyoutTimestamp,
|
||||
bool isOpenBuyout
|
||||
) external {
|
||||
require(!_FRAG_INITIALIZED_, "DODOFragment: ALREADY_INITIALIZED");
|
||||
_FRAG_INITIALIZED_ = true;
|
||||
|
||||
// init local variables
|
||||
initOwner(owner);
|
||||
_DVM_ = dvm;
|
||||
_COLLATERAL_VAULT_ = collateralVault;
|
||||
_QUOTE_ = IDVM(_DVM_)._QUOTE_TOKEN_();
|
||||
_VAULT_PRE_OWNER_ = vaultPreOwner;
|
||||
_COLLATERAL_VAULT_ = collateralVault;
|
||||
_BUYOUT_TIMESTAMP_ = buyoutTimestamp;
|
||||
_IS_OPEN_BUYOUT_ = isOpenBuyout;
|
||||
|
||||
// init FRAG meta data
|
||||
string memory suffix = "FRAG_";
|
||||
name = string(abi.encodePacked(suffix, IDVM(_DVM_).addressToShortString(_COLLATERAL_VAULT_)));
|
||||
string memory prefix = "FRAG_";
|
||||
name = string(abi.encodePacked(prefix, IDVM(_DVM_).addressToShortString(_COLLATERAL_VAULT_)));
|
||||
symbol = "FRAG";
|
||||
decimals = 18;
|
||||
super.init(address(this), totalSupply, name, symbol, decimals);
|
||||
|
||||
// init FRAG distribution
|
||||
totalSupply = supply;
|
||||
balances[owner] = DecimalMath.mulFloor(supply, ownerRatio);
|
||||
balances[dvm] = supply.sub(balances[owner]);
|
||||
emit Transfer(address(0), owner, balances[owner]);
|
||||
emit Transfer(address(0), dvm, balances[dvm]);
|
||||
uint256 vaultPreOwnerBalance = DecimalMath.mulFloor(totalSupply, ownerRatio);
|
||||
transfer(_VAULT_PRE_OWNER_,vaultPreOwnerBalance);
|
||||
transfer(_DVM_,totalSupply.sub(vaultPreOwnerBalance));
|
||||
|
||||
// init DVM liquidity
|
||||
IDVM(_DVM_).buyShares(address(this));
|
||||
}
|
||||
|
||||
//需要先转入QUOTE
|
||||
function buyout() external {
|
||||
require(!_IS_BUYOUT_, "ALREADY BUYOUT");
|
||||
|
||||
function buyout(address newVaultOwner) external {
|
||||
require(_IS_OPEN_BUYOUT_, "DODOFragment: NOT_SUPPORT_BUYOUT");
|
||||
require(block.timestamp > _BUYOUT_TIMESTAMP_, "DODOFragment: BUYOUT_NOT_START");
|
||||
require(!_IS_BUYOUT_, "DODOFragment: ALREADY_BUYOUT");
|
||||
_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");
|
||||
require(IERC20(_QUOTE_).balanceOf(address(this)) >= requireQuote, "DODOFragment: QUOTE_NOT_ENOUGH");
|
||||
|
||||
IDVM(_DVM_).sellShares(
|
||||
IERC20(_DVM_).balanceOf(address(this)),
|
||||
@@ -77,30 +91,38 @@ contract Fragment is InitializableMintableERC20 {
|
||||
0,
|
||||
"",
|
||||
uint256(-1)
|
||||
);
|
||||
);
|
||||
|
||||
uint256 ownerQuote = DecimalMath.mulFloor(_BUYOUT_PRICE_, balances[address(this)]);
|
||||
_clearSelfBalance();
|
||||
uint256 preOwnerQuote = DecimalMath.mulFloor(_BUYOUT_PRICE_, balances[_VAULT_PRE_OWNER_]);
|
||||
IERC20(_QUOTE_).safeTransfer(_VAULT_PRE_OWNER_, preOwnerQuote);
|
||||
_clearBalance(_VAULT_PRE_OWNER_);
|
||||
|
||||
IERC20(_QUOTE_).safeTransfer(_OWNER_, ownerQuote);
|
||||
uint256 newOwnerQuote = DecimalMath.mulFloor(_BUYOUT_PRICE_, balances[newVaultOwner]);
|
||||
IERC20(_QUOTE_).safeTransfer(newVaultOwner, newOwnerQuote);
|
||||
_clearBalance(newVaultOwner);
|
||||
|
||||
ICollateralVault(_COLLATERAL_VAULT_).directTransferOwnership(newVaultOwner);
|
||||
}
|
||||
|
||||
// buyout之后的恒定兑换,需要先转入FRAG
|
||||
function redeem(address to) external {
|
||||
require(_IS_BUYOUT_, "NEED BUYOUT");
|
||||
|
||||
IERC20(_QUOTE_).safeTransfer(to, DecimalMath.mulFloor(_BUYOUT_PRICE_, balances[address(this)]));
|
||||
_clearSelfBalance();
|
||||
function redeem(address to) external {
|
||||
require(_IS_BUYOUT_, "DODOFragment: NEED_BUYOUT");
|
||||
|
||||
IERC20(_QUOTE_).safeTransfer(to, DecimalMath.mulFloor(_BUYOUT_PRICE_, balances[to]));
|
||||
_clearBalance(to);
|
||||
}
|
||||
|
||||
function getBuyoutRequirement() external view returns (uint256 requireQuote){
|
||||
require(_IS_OPEN_BUYOUT_, "NOT SUPPORT BUYOUT");
|
||||
require(!_IS_BUYOUT_, "ALREADY BUYOUT");
|
||||
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;
|
||||
function _clearBalance(address account) internal {
|
||||
uint256 clearBalance = balances[account];
|
||||
balances[account] = 0;
|
||||
balances[address(0)] = clearBalance;
|
||||
emit Transfer(account, address(0), clearBalance);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user