|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.11; |
| 3 | + |
| 4 | +import "@std/Test.sol"; |
| 5 | +import "@ds-test/test.sol"; |
| 6 | + |
| 7 | +import { DropSinglePhase1155 } from "contracts/extension/DropSinglePhase1155.sol"; |
| 8 | + |
| 9 | +contract MyDropSinglePhase1155 is DropSinglePhase1155 { |
| 10 | + bool condition; |
| 11 | + |
| 12 | + function setCondition(bool _condition) external { |
| 13 | + condition = _condition; |
| 14 | + } |
| 15 | + |
| 16 | + function _canSetClaimConditions() internal override returns (bool) { |
| 17 | + return condition; |
| 18 | + } |
| 19 | + |
| 20 | + function collectPriceOnClaim( |
| 21 | + address _primarySaleRecipient, |
| 22 | + uint256 _quantityToClaim, |
| 23 | + address _currency, |
| 24 | + uint256 _pricePerToken |
| 25 | + ) internal override {} |
| 26 | + |
| 27 | + function transferTokensOnClaim( |
| 28 | + address _to, |
| 29 | + uint256 _tokenId, |
| 30 | + uint256 _quantityBeingClaimed |
| 31 | + ) internal override {} |
| 32 | +} |
| 33 | + |
| 34 | +contract ExtensionDropSinglePhase1155 is DSTest, Test { |
| 35 | + MyDropSinglePhase1155 internal ext; |
| 36 | + |
| 37 | + event TokensClaimed( |
| 38 | + address indexed claimer, |
| 39 | + address indexed receiver, |
| 40 | + uint256 indexed tokenId, |
| 41 | + uint256 quantityClaimed |
| 42 | + ); |
| 43 | + event ClaimConditionUpdated( |
| 44 | + uint256 indexed tokenId, |
| 45 | + MyDropSinglePhase1155.ClaimCondition condition, |
| 46 | + bool resetEligibility |
| 47 | + ); |
| 48 | + |
| 49 | + function setUp() public { |
| 50 | + ext = new MyDropSinglePhase1155(); |
| 51 | + } |
| 52 | + |
| 53 | + /*/////////////////////////////////////////////////////////////// |
| 54 | + Claim Tests |
| 55 | + //////////////////////////////////////////////////////////////*/ |
| 56 | + |
| 57 | + /** |
| 58 | + * note: Testing revert condition; not allowed to claim again before wait time is over. |
| 59 | + */ |
| 60 | + function test_revert_claimCondition_waitTimeInSecondsBetweenClaims() public { |
| 61 | + ext.setCondition(true); |
| 62 | + vm.warp(1); |
| 63 | + |
| 64 | + address receiver = address(0x123); |
| 65 | + address claimer = address(0x345); |
| 66 | + bytes32[] memory proofs = new bytes32[](0); |
| 67 | + uint256 _tokenId = 0; |
| 68 | + |
| 69 | + MyDropSinglePhase1155.AllowlistProof memory alp; |
| 70 | + alp.proof = proofs; |
| 71 | + |
| 72 | + MyDropSinglePhase1155.ClaimCondition[] memory conditions = new MyDropSinglePhase1155.ClaimCondition[](1); |
| 73 | + conditions[0].maxClaimableSupply = 100; |
| 74 | + conditions[0].quantityLimitPerTransaction = 100; |
| 75 | + conditions[0].waitTimeInSecondsBetweenClaims = type(uint256).max; |
| 76 | + |
| 77 | + ext.setClaimConditions(_tokenId, conditions[0], false); |
| 78 | + |
| 79 | + vm.prank(claimer, claimer); |
| 80 | + ext.claim(receiver, _tokenId, 1, address(0), 0, alp, ""); |
| 81 | + |
| 82 | + vm.expectRevert("cant claim yet"); |
| 83 | + vm.prank(claimer, claimer); |
| 84 | + ext.claim(receiver, _tokenId, 1, address(0), 0, alp, ""); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * note: Testing revert condition; exceed max claimable supply. |
| 89 | + */ |
| 90 | + function test_revert_claimCondition_exceedMaxClaimableSupply() public { |
| 91 | + ext.setCondition(true); |
| 92 | + vm.warp(1); |
| 93 | + |
| 94 | + address receiver = address(0x123); |
| 95 | + address claimer1 = address(0x345); |
| 96 | + address claimer2 = address(0x567); |
| 97 | + bytes32[] memory proofs = new bytes32[](0); |
| 98 | + uint256 _tokenId = 0; |
| 99 | + |
| 100 | + MyDropSinglePhase1155.AllowlistProof memory alp; |
| 101 | + alp.proof = proofs; |
| 102 | + |
| 103 | + MyDropSinglePhase1155.ClaimCondition[] memory conditions = new MyDropSinglePhase1155.ClaimCondition[](1); |
| 104 | + conditions[0].maxClaimableSupply = 100; |
| 105 | + conditions[0].quantityLimitPerTransaction = 100; |
| 106 | + conditions[0].waitTimeInSecondsBetweenClaims = type(uint256).max; |
| 107 | + |
| 108 | + ext.setClaimConditions(_tokenId, conditions[0], false); |
| 109 | + |
| 110 | + vm.prank(claimer1, claimer1); |
| 111 | + ext.claim(receiver, _tokenId, 100, address(0), 0, alp, ""); |
| 112 | + |
| 113 | + vm.expectRevert("exceeds max supply"); |
| 114 | + vm.prank(claimer2, claimer2); |
| 115 | + ext.claim(receiver, _tokenId, 1, address(0), 0, alp, ""); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * note: Testing quantity limit restriction when no allowlist present. |
| 120 | + */ |
| 121 | + function test_fuzz_claim_noAllowlist(uint256 x) public { |
| 122 | + ext.setCondition(true); |
| 123 | + vm.assume(x != 0); |
| 124 | + vm.warp(1); |
| 125 | + |
| 126 | + address receiver = address(0x123); |
| 127 | + address claimer = address(0x345); |
| 128 | + bytes32[] memory proofs = new bytes32[](0); |
| 129 | + uint256 _tokenId = 0; |
| 130 | + |
| 131 | + MyDropSinglePhase1155.AllowlistProof memory alp; |
| 132 | + alp.proof = proofs; |
| 133 | + alp.maxQuantityInAllowlist = x; |
| 134 | + |
| 135 | + MyDropSinglePhase1155.ClaimCondition[] memory conditions = new MyDropSinglePhase1155.ClaimCondition[](1); |
| 136 | + conditions[0].maxClaimableSupply = 500; |
| 137 | + conditions[0].quantityLimitPerTransaction = 100; |
| 138 | + conditions[0].waitTimeInSecondsBetweenClaims = type(uint256).max; |
| 139 | + |
| 140 | + ext.setClaimConditions(_tokenId, conditions[0], false); |
| 141 | + |
| 142 | + vm.prank(claimer, claimer); |
| 143 | + vm.expectRevert("Invalid quantity"); |
| 144 | + ext.claim(receiver, _tokenId, 101, address(0), 0, alp, ""); |
| 145 | + |
| 146 | + ext.setClaimConditions(_tokenId, conditions[0], true); |
| 147 | + |
| 148 | + vm.prank(claimer, claimer); |
| 149 | + vm.expectRevert("Invalid quantity"); |
| 150 | + ext.claim(receiver, _tokenId, 101, address(0), 0, alp, ""); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * note: Testing revert condition; can't claim if not in whitelist. |
| 155 | + */ |
| 156 | + function test_revert_claimCondition_merkleProof() public { |
| 157 | + ext.setCondition(true); |
| 158 | + string[] memory inputs = new string[](3); |
| 159 | + |
| 160 | + inputs[0] = "node"; |
| 161 | + inputs[1] = "src/test/scripts/generateRoot.ts"; |
| 162 | + inputs[2] = "1"; |
| 163 | + |
| 164 | + bytes memory result = vm.ffi(inputs); |
| 165 | + bytes32 root = abi.decode(result, (bytes32)); |
| 166 | + |
| 167 | + inputs[1] = "src/test/scripts/getProof.ts"; |
| 168 | + result = vm.ffi(inputs); |
| 169 | + bytes32[] memory proofs = abi.decode(result, (bytes32[])); |
| 170 | + |
| 171 | + vm.warp(1); |
| 172 | + |
| 173 | + address claimer = address(0x92Bb439374a091c7507bE100183d8D1Ed2c9dAD3); |
| 174 | + uint256 _tokenId = 0; |
| 175 | + |
| 176 | + MyDropSinglePhase1155.AllowlistProof memory alp; |
| 177 | + alp.proof = proofs; |
| 178 | + alp.maxQuantityInAllowlist = 1; |
| 179 | + |
| 180 | + MyDropSinglePhase1155.ClaimCondition[] memory conditions = new MyDropSinglePhase1155.ClaimCondition[](1); |
| 181 | + conditions[0].maxClaimableSupply = 100; |
| 182 | + conditions[0].quantityLimitPerTransaction = 100; |
| 183 | + conditions[0].waitTimeInSecondsBetweenClaims = type(uint256).max; |
| 184 | + conditions[0].merkleRoot = root; |
| 185 | + |
| 186 | + ext.setClaimConditions(_tokenId, conditions[0], false); |
| 187 | + |
| 188 | + vm.prank(claimer, claimer); |
| 189 | + ext.claim(claimer, _tokenId, 1, address(0), 0, alp, ""); |
| 190 | + |
| 191 | + vm.prank(address(4), address(4)); |
| 192 | + vm.expectRevert("not in allowlist"); |
| 193 | + ext.claim(address(4), _tokenId, 1, address(0), 0, alp, ""); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * note: Testing state changes; reset eligibility of claim conditions and claiming again for same condition id. |
| 198 | + */ |
| 199 | + function test_state_claimCondition_resetEligibility_waitTimeInSecondsBetweenClaims() public { |
| 200 | + ext.setCondition(true); |
| 201 | + vm.warp(1); |
| 202 | + |
| 203 | + address receiver = address(0x123); |
| 204 | + address claimer = address(0x345); |
| 205 | + bytes32[] memory proofs = new bytes32[](0); |
| 206 | + uint256 _tokenId = 0; |
| 207 | + |
| 208 | + MyDropSinglePhase1155.AllowlistProof memory alp; |
| 209 | + alp.proof = proofs; |
| 210 | + |
| 211 | + MyDropSinglePhase1155.ClaimCondition[] memory conditions = new MyDropSinglePhase1155.ClaimCondition[](1); |
| 212 | + conditions[0].maxClaimableSupply = 100; |
| 213 | + conditions[0].quantityLimitPerTransaction = 100; |
| 214 | + conditions[0].waitTimeInSecondsBetweenClaims = type(uint256).max; |
| 215 | + |
| 216 | + ext.setClaimConditions(_tokenId, conditions[0], false); |
| 217 | + |
| 218 | + vm.prank(claimer, claimer); |
| 219 | + ext.claim(receiver, _tokenId, 1, address(0), 0, alp, ""); |
| 220 | + |
| 221 | + ext.setClaimConditions(_tokenId, conditions[0], true); |
| 222 | + |
| 223 | + vm.prank(claimer, claimer); |
| 224 | + ext.claim(receiver, _tokenId, 1, address(0), 0, alp, ""); |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * note: Testing event emission on setClaimConditions. |
| 229 | + */ |
| 230 | + function test_event_setClaimConditions() public { |
| 231 | + ext.setCondition(true); |
| 232 | + vm.warp(1); |
| 233 | + |
| 234 | + bytes32[] memory proofs = new bytes32[](0); |
| 235 | + uint256 _tokenId = 0; |
| 236 | + |
| 237 | + MyDropSinglePhase1155.AllowlistProof memory alp; |
| 238 | + alp.proof = proofs; |
| 239 | + |
| 240 | + MyDropSinglePhase1155.ClaimCondition[] memory conditions = new MyDropSinglePhase1155.ClaimCondition[](1); |
| 241 | + conditions[0].maxClaimableSupply = 100; |
| 242 | + conditions[0].quantityLimitPerTransaction = 100; |
| 243 | + conditions[0].waitTimeInSecondsBetweenClaims = type(uint256).max; |
| 244 | + |
| 245 | + vm.expectEmit(true, true, true, true); |
| 246 | + emit ClaimConditionUpdated(_tokenId, conditions[0], false); |
| 247 | + |
| 248 | + ext.setClaimConditions(_tokenId, conditions[0], false); |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * note: Testing event emission on claim. |
| 253 | + */ |
| 254 | + function test_event_claim() public { |
| 255 | + ext.setCondition(true); |
| 256 | + vm.warp(1); |
| 257 | + |
| 258 | + address receiver = address(0x123); |
| 259 | + address claimer = address(0x345); |
| 260 | + bytes32[] memory proofs = new bytes32[](0); |
| 261 | + uint256 _tokenId = 0; |
| 262 | + |
| 263 | + MyDropSinglePhase1155.AllowlistProof memory alp; |
| 264 | + alp.proof = proofs; |
| 265 | + |
| 266 | + MyDropSinglePhase1155.ClaimCondition[] memory conditions = new MyDropSinglePhase1155.ClaimCondition[](1); |
| 267 | + conditions[0].maxClaimableSupply = 100; |
| 268 | + conditions[0].quantityLimitPerTransaction = 100; |
| 269 | + conditions[0].waitTimeInSecondsBetweenClaims = type(uint256).max; |
| 270 | + |
| 271 | + ext.setClaimConditions(_tokenId, conditions[0], false); |
| 272 | + |
| 273 | + vm.startPrank(claimer, claimer); |
| 274 | + |
| 275 | + vm.expectEmit(true, true, true, true); |
| 276 | + emit TokensClaimed(claimer, receiver, _tokenId, 1); |
| 277 | + |
| 278 | + ext.claim(receiver, _tokenId, 1, address(0), 0, alp, ""); |
| 279 | + } |
| 280 | +} |
0 commit comments