From 09a4cbfc9c49a13247e177934a8a50e74b0a7614 Mon Sep 17 00:00:00 2001 From: mingda Date: Fri, 10 Sep 2021 19:49:29 +0800 Subject: [PATCH] fix ERC1155 random --- contracts/NFTPool/impl/BaseFilterV1.sol | 10 ++++------ contracts/NFTPool/impl/FilterERC1155V1.sol | 10 +++++++++- contracts/NFTPool/impl/FilterERC721V1.sol | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/contracts/NFTPool/impl/BaseFilterV1.sol b/contracts/NFTPool/impl/BaseFilterV1.sol index ab646e5..e984d99 100644 --- a/contracts/NFTPool/impl/BaseFilterV1.sol +++ b/contracts/NFTPool/impl/BaseFilterV1.sol @@ -173,12 +173,10 @@ contract BaseFilterV1 is InitializableOwnable, ReentrancyGuard { return a1.mul(qn.sub(qm)).div(q.sub(DecimalMath.ONE)); } - function _getRandomOutId() public view returns (uint256 index) { - index = - uint256( - keccak256(abi.encodePacked(tx.origin, blockhash(block.number - 1), gasleft())) - ) % - _TOTAL_NFT_AMOUNT_; + function _getRandomNum() public view returns (uint256 randomNum) { + randomNum = uint256( + keccak256(abi.encodePacked(tx.origin, blockhash(block.number - 1), gasleft())) + ); } // ================= Ownable ================ diff --git a/contracts/NFTPool/impl/FilterERC1155V1.sol b/contracts/NFTPool/impl/FilterERC1155V1.sol index 609b86e..fc230e8 100644 --- a/contracts/NFTPool/impl/FilterERC1155V1.sol +++ b/contracts/NFTPool/impl/FilterERC1155V1.sol @@ -79,7 +79,15 @@ contract FilterERC1155V1 is IERC1155Receiver, BaseFilterV1 { (uint256 rawPay, ) = queryNFTRandomOut(amount); paid = IFilterAdmin(_OWNER_).burnFragFrom(msg.sender, rawPay); for (uint256 i = 0; i < amount; i++) { - _transferOutERC1155(to, _getRandomOutId(), 1); + uint256 randomNum = _getRandomOutId() % _TOTAL_NFT_AMOUNT_; + uint256 sum; + for (uint256 j = 0; j < _NFT_IDS_.length; j++) { + sum += _NFT_RESERVE_[_NFT_IDS_[j]]; + if (sum >= randomNum) { + _transferOutERC1155(to, _NFT_IDS_[j], 1); + break; + } + } } } diff --git a/contracts/NFTPool/impl/FilterERC721V1.sol b/contracts/NFTPool/impl/FilterERC721V1.sol index bfbc0da..08da766 100644 --- a/contracts/NFTPool/impl/FilterERC721V1.sol +++ b/contracts/NFTPool/impl/FilterERC721V1.sol @@ -87,7 +87,7 @@ contract FilterERC721V1 is IERC721Receiver, BaseFilterV1 { (uint256 rawPay, ) = queryNFTRandomOut(amount); paid = IFilterAdmin(_OWNER_).burnFragFrom(msg.sender, rawPay); for (uint256 i = 0; i < amount; i++) { - _transferOutERC721(to, _getRandomOutId()); + _transferOutERC721(to, _getRandomNum() % _TOTAL_NFT_AMOUNT_); } _TOTAL_NFT_AMOUNT_ = _NFT_IDS_.length; }