From 6dfb96b86e145e537feba35fa0f3d323d6c34daa Mon Sep 17 00:00:00 2001 From: owen05 Date: Thu, 9 Sep 2021 10:27:09 +0800 Subject: [PATCH] add nftPoolProxy --- contracts/NFTPool/impl/FilterModel01.sol | 67 ++++++++++++------- contracts/NFTPool/intf/IFilterAdmin.sol | 10 +++ .../SmartRoute/proxies/DODONFTPoolProxy.sol | 56 +++++++++++++--- 3 files changed, 99 insertions(+), 34 deletions(-) diff --git a/contracts/NFTPool/impl/FilterModel01.sol b/contracts/NFTPool/impl/FilterModel01.sol index b85a721..233b274 100644 --- a/contracts/NFTPool/impl/FilterModel01.sol +++ b/contracts/NFTPool/impl/FilterModel01.sol @@ -100,7 +100,7 @@ contract FilterModel01 is InitializableOwnable, IERC721Receiver { } } - function getAvaliableNFTIn() external view returns(uint256) { + function getAvaliableNFTIn() public view returns(uint256) { if(_MAX_NFT_AMOUNT_ < _TOKEN_IDS_.length) { return 0; }else { @@ -108,7 +108,7 @@ contract FilterModel01 is InitializableOwnable, IERC721Receiver { } } - function getAvaliableNFTOut() external view returns(uint256) { + function getAvaliableNFTOut() public view returns(uint256) { if(_TOKEN_IDS_.length < _MIN_NFT_AMOUNT_) { return 0; }else { @@ -117,36 +117,21 @@ contract FilterModel01 is InitializableOwnable, IERC721Receiver { } function getNFTInPrice(address, uint256) external view returns(uint256) { - uint256 nftAmount = _TOKEN_IDS_.length; - if(nftAmount == 0) { - return _GS_START_IN_; - }else { - uint256 price = _GS_START_IN_; - //TODO:gas - for(uint256 i = 0; i < nftAmount; i++) { - price = DecimalMath.mulFloor(price, _CR_IN_); - } - return price; - } + (uint256 price, ) = geometricCalc(_GS_START_IN_,_CR_IN_, _TOKEN_IDS_.length); + return price; } function getNFTRandomOutPrice() external view returns (uint256) { - uint256 nftAmount = _TOKEN_IDS_.length; - require(nftAmount != 0, "EMPTY"); - uint256 price = _GS_START_RANDOM_OUT_; - for(uint256 i = 0; i < nftAmount; i++) { - price = DecimalMath.mulFloor(price, _CR_RANDOM_OUT_); - } + require(_TOKEN_IDS_.length != 0, "EMPTY"); + + (uint256 price, ) = geometricCalc(_GS_START_RANDOM_OUT_,_CR_RANDOM_OUT_, _TOKEN_IDS_.length); return price; } function getNFTTargetOutPrice(address, uint256) external view returns (uint256) { - uint256 nftAmount = _TOKEN_IDS_.length; - require(nftAmount != 0, "EMPTY"); - uint256 price = _GS_START_TARGET_OUT_; - for(uint256 i = 0; i < nftAmount; i++) { - price = DecimalMath.mulFloor(price, _CR_TARGET_OUT_); - } + require(_TOKEN_IDS_.length != 0, "EMPTY"); + + (uint256 price, ) = geometricCalc(_GS_START_TARGET_OUT_,_CR_TARGET_OUT_, _TOKEN_IDS_.length); return price; } @@ -162,6 +147,28 @@ contract FilterModel01 is InitializableOwnable, IERC721Receiver { nftId = _TOKEN_IDS_[idx]; } + + function getTotalNFTInPrice(uint256 amount) external view returns (uint256 totalPrice) { + require(amount <= getAvaliableNFTIn(), "EXCEDD_IN_AMOUNT"); + + (uint256 base, ) = geometricCalc(_GS_START_IN_,_CR_IN_, _TOKEN_IDS_.length); + (, totalPrice) = geometricCalc(base, _CR_IN_, amount); + } + + function getTotalTargetNFTOutPrice(uint256 amount) external view returns (uint256 totalPrice) { + require(amount <= getAvaliableNFTOut(), "EXCEED_OUT_AMOUNT"); + + (uint256 base, ) = geometricCalc(_GS_START_TARGET_OUT_,_CR_TARGET_OUT_, _TOKEN_IDS_.length); + (, totalPrice) = geometricCalc(base, _CR_TARGET_OUT_, amount); + } + + function getTotalRandomNFTOutPrice(uint256 amount) external view returns (uint256 totalPrice) { + require(amount <= getAvaliableNFTOut(), "EXCEED_OUT_AMOUNT"); + + (uint256 base, ) = geometricCalc(_GS_START_RANDOM_OUT_,_CR_RANDOM_OUT_, _TOKEN_IDS_.length); + (, totalPrice) = geometricCalc(base, _CR_RANDOM_OUT_, amount); + } + // ================= Ownable ================ function transferOutERC721(address nftContract, address assetTo, uint256 nftId) external onlyOwner { require(nftContract == _NFT_COLLECTION_, "WRONG_NFT_COLLECTION"); @@ -257,4 +264,14 @@ contract FilterModel01 is InitializableOwnable, IERC721Receiver { ) external override returns (bytes4) { return IERC721Receiver.onERC721Received.selector; } + + // ============ Internal ============= + function geometricCalc(uint256 base, uint256 ratio, uint256 times) internal view returns(uint256 newBase, uint256 sum) { + sum = 0; + for(uint256 i = 0; i < times; i++) { + base = DecimalMath.mulFloor(base, ratio); + sum = sum.add(base); + } + newBase = base; + } } \ No newline at end of file diff --git a/contracts/NFTPool/intf/IFilterAdmin.sol b/contracts/NFTPool/intf/IFilterAdmin.sol index 542539c..cf1cb37 100644 --- a/contracts/NFTPool/intf/IFilterAdmin.sol +++ b/contracts/NFTPool/intf/IFilterAdmin.sol @@ -9,4 +9,14 @@ pragma solidity 0.6.9; interface IFilterAdmin { function _OWNER_() external returns (address); + + function init( + address _owner, + string memory _name, + string memory _symbol, + uint256 fee, + address mtFeeModel, + address defaultMaintainer, + address[] memory filters + ) external; } \ No newline at end of file diff --git a/contracts/SmartRoute/proxies/DODONFTPoolProxy.sol b/contracts/SmartRoute/proxies/DODONFTPoolProxy.sol index d641290..4d2a367 100644 --- a/contracts/SmartRoute/proxies/DODONFTPoolProxy.sol +++ b/contracts/SmartRoute/proxies/DODONFTPoolProxy.sol @@ -9,7 +9,19 @@ import {SafeMath} from "../../lib/SafeMath.sol"; import {InitializableOwnable} from "../../lib/InitializableOwnable.sol"; import {ICloneFactory} from "../../lib/CloneFactory.sol"; import {ReentrancyGuard} from "../../lib/ReentrancyGuard.sol"; +import {IFilterAdmin} from "../../NFTPool/intf/IFilterAdmin.sol"; +interface IFilter01 { + function init( + address filterAdmin, + address nftCollection, + bool[] memory switches, + uint256[] memory tokenRanges, + uint256[] memory nftAmounts, + uint256[] memory priceRules, + uint256[] memory spreadIds + ) external; +} contract DODONFTPoolProxy is ReentrancyGuard, InitializableOwnable { using SafeMath for uint256; @@ -37,17 +49,43 @@ contract DODONFTPoolProxy is ReentrancyGuard, InitializableOwnable { _DEFAULT_MAINTAINER_ = defaultMaintainer; } - //TODO:一笔交易 - - function createFilterAdmin( + function createNewNFTPool01( string memory name, string memory symbol, - uint256 fee - ) external returns(address) { + uint256 fee, + address nftCollection, + bool[] memory switches, + uint256[] memory tokenRanges, + uint256[] memory nftAmounts, + uint256[] memory priceRules, + uint256[] memory spreadIds + ) external returns(address newFilterAdmin) { + newFilterAdmin = ICloneFactory(_CLONE_FACTORY_).clone(_FILTER_ADMIN_TEMPLATE_); + address filter01 = createFilter01( + newFilterAdmin, + nftCollection, + switches, + tokenRanges, + nftAmounts, + priceRules, + spreadIds + ); + + address[] memory filters = new address[](1); + filters[0] = filter01; + + IFilterAdmin(newFilterAdmin).init( + msg.sender, + name, + symbol, + fee, + _NFT_POOL_FEE_MODEL_, + _DEFAULT_MAINTAINER_, + filters + ); } - function createFilter01( address filterAdmin, address nftCollection, @@ -56,11 +94,11 @@ contract DODONFTPoolProxy is ReentrancyGuard, InitializableOwnable { uint256[] memory nftAmounts, uint256[] memory priceRules, uint256[] memory spreadIds - ) external returns(address) { - + ) public returns(address newFilter01) { + newFilter01 = ICloneFactory(_CLONE_FACTORY_).clone(_FILTER_TEMPLATES_[1]); + IFilter01(newFilter01).init(filterAdmin, nftCollection, switches, tokenRanges, nftAmounts, priceRules, spreadIds); } - //====================== Ownable ======================== function changeDefaultMaintainer(address newMaintainer) external onlyOwner { _DEFAULT_MAINTAINER_ = newMaintainer;