From 2485ab6b56b05f8962fe980b99b18b20b6ccabc0 Mon Sep 17 00:00:00 2001 From: owen05 Date: Wed, 28 Apr 2021 15:07:28 +0800 Subject: [PATCH] update buyout --- contracts/GeneralizedFragment/impl/Fragment.sol | 15 ++++++++++++--- contracts/GeneralizedFragment/intf/IFragment.sol | 4 +++- contracts/SmartRoute/proxies/DODONFTProxy.sol | 13 ++++++++++++- test/DODONFT/nftMainFlow.test.ts | 5 +++-- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/contracts/GeneralizedFragment/impl/Fragment.sol b/contracts/GeneralizedFragment/impl/Fragment.sol index cf3e487..dc4af0e 100644 --- a/contracts/GeneralizedFragment/impl/Fragment.sol +++ b/contracts/GeneralizedFragment/impl/Fragment.sol @@ -26,11 +26,13 @@ contract Fragment is InitializableERC20 { bool public _IS_OPEN_BUYOUT_; uint256 public _BUYOUT_TIMESTAMP_; uint256 public _BUYOUT_PRICE_; + uint256 public _DEFAULT_BUYOUT_FEE_; address public _COLLATERAL_VAULT_; address public _VAULT_PRE_OWNER_; address public _QUOTE_; address public _DVM_; + address public _DEFAULT_MAINTAINER_; bool internal _FRAG_INITIALIZED_; @@ -49,7 +51,9 @@ contract Fragment is InitializableERC20 { address collateralVault, uint256 _totalSupply, uint256 ownerRatio, - uint256 buyoutTimestamp + uint256 buyoutTimestamp, + address defaultMaintainer, + uint256 defaultBuyoutFee ) external { require(!_FRAG_INITIALIZED_, "DODOFragment: ALREADY_INITIALIZED"); _FRAG_INITIALIZED_ = true; @@ -60,6 +64,8 @@ contract Fragment is InitializableERC20 { _VAULT_PRE_OWNER_ = vaultPreOwner; _COLLATERAL_VAULT_ = collateralVault; _BUYOUT_TIMESTAMP_ = buyoutTimestamp; + _DEFAULT_MAINTAINER_ = defaultMaintainer; + _DEFAULT_BUYOUT_FEE_ = defaultBuyoutFee; // init FRAG meta data string memory prefix = "FRAG_"; @@ -99,11 +105,14 @@ contract Fragment is InitializableERC20 { ); uint256 redeemFrag = totalSupply.sub(balances[address(this)]).sub(balances[_VAULT_PRE_OWNER_]); - uint256 preOwnerQuote = payQuote.sub(DecimalMath.mulCeil(_BUYOUT_PRICE_, redeemFrag)); + uint256 ownerQuoteWithoutFee = IERC20(_QUOTE_).balanceOf(address(this)).sub(DecimalMath.mulCeil(_BUYOUT_PRICE_, redeemFrag)); _clearBalance(address(this)); _clearBalance(_VAULT_PRE_OWNER_); - IERC20(_QUOTE_).safeTransfer(_VAULT_PRE_OWNER_, preOwnerQuote); + uint256 buyoutFee = DecimalMath.mulFloor(ownerQuoteWithoutFee, _DEFAULT_BUYOUT_FEE_); + + IERC20(_QUOTE_).safeTransfer(_DEFAULT_MAINTAINER_, buyoutFee); + IERC20(_QUOTE_).safeTransfer(_VAULT_PRE_OWNER_, ownerQuoteWithoutFee.sub(buyoutFee)); ICollateralVault(_COLLATERAL_VAULT_).directTransferOwnership(newVaultOwner); diff --git a/contracts/GeneralizedFragment/intf/IFragment.sol b/contracts/GeneralizedFragment/intf/IFragment.sol index b3caf40..ba7bc06 100644 --- a/contracts/GeneralizedFragment/intf/IFragment.sol +++ b/contracts/GeneralizedFragment/intf/IFragment.sol @@ -16,7 +16,9 @@ interface IFragment { address collateralVault, uint256 totalSupply, uint256 ownerRatio, - uint256 buyoutTimestamp + uint256 buyoutTimestamp, + address defaultMaintainer, + uint256 defaultBuyoutFee ) external; function buyout(address newVaultOwner) external; diff --git a/contracts/SmartRoute/proxies/DODONFTProxy.sol b/contracts/SmartRoute/proxies/DODONFTProxy.sol index 0025145..1fb03e3 100644 --- a/contracts/SmartRoute/proxies/DODONFTProxy.sol +++ b/contracts/SmartRoute/proxies/DODONFTProxy.sol @@ -49,6 +49,8 @@ contract DODONFTProxy is ReentrancyGuard, InitializableOwnable { address public _DVM_TEMPLATE_; address public _MTFEE_TEMPLATE_; + uint256 public _DEFAULT_BUYOUT_FEE_; + // ============ Events ============ event ChangeVaultTemplate(address newVaultTemplate); event ChangeFragTemplate(address newFragTemplate); @@ -59,6 +61,7 @@ contract DODONFTProxy is ReentrancyGuard, InitializableOwnable { event CreateFragment(address vault, address fragment, address dvm, address feeDistributor); event Buyout(address from, address fragment, uint256 amount); event Stake(address from, address feeDistributor, uint256 amount); + event ChangeBuyoutFee(uint256 newBuyoutFee); // ============ Modifiers ============ @@ -140,7 +143,9 @@ contract DODONFTProxy is ReentrancyGuard, InitializableOwnable { msg.sender, _fragParams[0], _fragParams[1], - _fragParams[2] + _fragParams[2], + _DEFAULT_MAINTAINER_, + _DEFAULT_BUYOUT_FEE_ ); } @@ -220,6 +225,12 @@ contract DODONFTProxy is ReentrancyGuard, InitializableOwnable { emit ChangeDvmTemplate(newDvmTemplate); } + function updateBuyoutFee(uint256 buyoutFee) external onlyOwner { + _DEFAULT_BUYOUT_FEE_ = buyoutFee; + emit ChangeBuyoutFee(buyoutFee); + } + + //============= Internal ================ function _createConstantMtFeeRateModel(uint256 mtFee) internal returns (address mtFeeModel) { diff --git a/test/DODONFT/nftMainFlow.test.ts b/test/DODONFT/nftMainFlow.test.ts index 72158df..6c3c30e 100644 --- a/test/DODONFT/nftMainFlow.test.ts +++ b/test/DODONFT/nftMainFlow.test.ts @@ -285,13 +285,14 @@ describe("DODONFT", () => { await getUserBalance(fragAddress, fragInstance, ctx.USDT, "FRAG Before"); var requireQuote = await fragInstance.methods.getBuyoutRequirement().call(); - await logGas(await ctx.NFTProxy.methods.buyout(fragAddress, requireQuote, 0), ctx.sendParam(buyer), "buyout"); + await logGas(await ctx.NFTProxy.methods.buyout(fragAddress, requireQuote, 0, Math.floor(new Date().getTime() / 1000 + 60 * 10)), ctx.sendParam(buyer), "buyout"); let [authorFrag, authorQuote] = await getUserBalance(author, fragInstance, ctx.USDT, "Author After"); await getUserBalance(buyer, fragInstance, ctx.USDT, "Buyer After"); await getUserBalance(dvmAddress, fragInstance, ctx.USDT, "DVM After"); await getUserBalance(fragAddress, fragInstance, ctx.USDT, "FRAG After"); - assert(authorQuote, "2034932000"); + // assert(authorQuote, "2034932000"); + assert(authorQuote, "10174055148"); assert(authorFrag, "0"); var vaultNewOwner = await vaultInstance.methods._OWNER_().call();