File size: 14,494 Bytes
cc183a1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | // SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {Test} from "forge-std/Test.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ERC6551Registry} from "erc6551-reference/ERC6551Registry.sol";
import {StableToken} from "../src/StableToken.sol";
import {BasicCollection} from "../src/BasicCollection.sol";
import {TokenAccountA} from "../src/TokenAccountA.sol";
import {TokenAccountB} from "../src/TokenAccountB.sol";
import {TokenAccountC} from "../src/TokenAccountC.sol";
import {TokenAccountBase} from "../src/TokenAccountBase.sol";
import {Rentable1155} from "../src/Rentable1155.sol";
import {IERC1155Receiver} from "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol";
import {CollectionBase} from "../src/CollectionBase.sol";
import {CollectionV1} from "../src/CollectionV1.sol";
import {CollectionV2} from "../src/CollectionV2.sol";
import {CollectionV3} from "../src/CollectionV3.sol";
contract TokenAccountVariantsTest is Test {
address alice = makeAddr("alice");
address bob = makeAddr("bob");
uint256 constant LOCK_UNTIL = 4_853_600_000;
StableToken usdc;
BasicCollection nft;
ERC6551Registry registry;
function setUp() public {
usdc = new StableToken(1_000_000e6);
nft = new BasicCollection();
registry = new ERC6551Registry();
nft.mint(alice, 100);
}
function _account(address impl, bytes32 salt) internal returns (address payable account) {
account = payable(registry.createAccount(impl, salt, block.chainid, address(nft), 100));
usdc.transfer(account, 100e6);
}
function test_A_ownerCanSweepThroughExecute() public {
address payable account = _account(address(new TokenAccountA()), bytes32(0));
assertEq(TokenAccountBase(account).owner(), alice);
vm.prank(alice);
TokenAccountA(account).execute(address(usdc), 0, abi.encodeCall(IERC20.transfer, (alice, 100e6)), 0);
assertEq(usdc.balanceOf(alice), 100e6);
assertEq(usdc.balanceOf(account), 0);
}
function test_A_controlFollowsTheNft() public {
address payable account = _account(address(new TokenAccountA()), bytes32(0));
vm.prank(alice);
nft.transferFrom(alice, bob, 100);
vm.prank(alice);
vm.expectRevert("invalid signer");
TokenAccountA(account).execute(address(usdc), 0, abi.encodeCall(IERC20.transfer, (alice, 100e6)), 0);
vm.prank(bob);
TokenAccountA(account).execute(address(usdc), 0, abi.encodeCall(IERC20.transfer, (bob, 100e6)), 0);
assertEq(usdc.balanceOf(bob), 100e6);
}
function _lockedAccount() internal returns (address payable account) {
account = _account(address(new TokenAccountB()), bytes32(0));
vm.prank(alice);
TokenAccountB(account).lock(LOCK_UNTIL);
}
function test_B_interfaceIdsAndSignerSurviveTheLock() public {
address payable account = _lockedAccount();
assertTrue(TokenAccountB(account).supportsInterface(0x01ffc9a7)); // ERC-165
assertTrue(TokenAccountB(account).supportsInterface(0x6faff5f1)); // IERC6551Account
assertTrue(TokenAccountB(account).supportsInterface(0x51945447)); // IERC6551Executable
assertEq(TokenAccountBase(account).isValidSigner(alice, ""), bytes4(0x523e3260));
assertEq(TokenAccountBase(account).isValidSigner(bob, ""), bytes4(0));
}
function test_B_executeRevertsDuringLockAndWorksAfterIt() public {
address payable account = _lockedAccount();
bytes memory sweep = abi.encodeCall(IERC20.transfer, (alice, 100e6));
vm.prank(alice);
vm.expectRevert("account is locked");
TokenAccountB(account).execute(address(usdc), 0, sweep, 0);
assertEq(usdc.balanceOf(account), 100e6);
vm.warp(LOCK_UNTIL);
vm.prank(alice);
TokenAccountB(account).execute(address(usdc), 0, sweep, 0);
assertEq(usdc.balanceOf(alice), 100e6);
assertEq(usdc.balanceOf(account), 0);
}
function testFuzz_B_executeRevertsForEveryCallerDuringLock(
address caller,
address to,
uint256 value,
bytes calldata data,
uint8 operation
) public {
address payable account = _lockedAccount();
vm.assume(caller != address(account));
vm.deal(caller, value);
vm.prank(caller);
vm.expectRevert("account is locked");
TokenAccountB(account).execute{value: value}(to, value, data, operation);
}
function testFuzz_B_lockCannotBeShortened(uint256 until) public {
address payable account = _lockedAccount();
until = bound(until, 0, LOCK_UNTIL);
vm.prank(alice);
vm.expectRevert("lock cannot be shortened");
TokenAccountB(account).lock(until);
assertEq(TokenAccountB(account).lockedUntil(), LOCK_UNTIL);
}
function test_B_lockCanBeExtended() public {
address payable account = _lockedAccount();
vm.prank(alice);
TokenAccountB(account).lock(LOCK_UNTIL + 1);
assertEq(TokenAccountB(account).lockedUntil(), LOCK_UNTIL + 1);
}
function testFuzz_B_onlyTheHolderCanLock(address caller) public {
address payable account = _account(address(new TokenAccountB()), bytes32(0));
vm.assume(caller != alice);
vm.prank(caller);
vm.expectRevert("invalid signer");
TokenAccountB(account).lock(LOCK_UNTIL);
}
function test_B_lockSurvivesTheNftTransfer() public {
address payable account = _lockedAccount();
vm.prank(alice);
nft.transferFrom(alice, bob, 100);
vm.prank(bob);
vm.expectRevert("account is locked");
TokenAccountB(account).execute(address(usdc), 0, abi.encodeCall(IERC20.transfer, (bob, 100e6)), 0);
}
function test_signerAndSignatureChecksFollowTheOwner() public {
(address holder, uint256 key) = makeAddrAndKey("holder");
nft.mint(holder, 7);
address payable account =
payable(registry.createAccount(address(new TokenAccountA()), bytes32(0), block.chainid, address(nft), 7));
assertEq(TokenAccountBase(account).isValidSigner(holder, ""), bytes4(0x523e3260));
assertEq(TokenAccountBase(account).isValidSigner(bob, ""), bytes4(0));
bytes32 digest = keccak256("message");
(uint8 v, bytes32 r, bytes32 s) = vm.sign(key, digest);
assertEq(TokenAccountBase(account).isValidSignature(digest, abi.encodePacked(r, s, v)), bytes4(0x1626ba7e));
}
function test_twoSaltsGiveTwoDistinctAccounts() public {
address impl = address(new TokenAccountA());
address payable first = _account(impl, bytes32(0));
address payable second = _account(impl, bytes32(uint256(1)));
assertTrue(first != second);
assertEq(usdc.balanceOf(first), 100e6);
assertEq(usdc.balanceOf(second), 100e6);
assertEq(TokenAccountBase(first).owner(), alice);
assertEq(TokenAccountBase(second).owner(), alice);
}
function test_C_supportsInterfaces() public {
address payable account = _account(address(new TokenAccountC()), bytes32(0));
assertTrue(TokenAccountC(account).supportsInterface(0x01ffc9a7)); // ERC-165
assertTrue(TokenAccountC(account).supportsInterface(0x6faff5f1)); // IERC6551Account
assertTrue(TokenAccountC(account).supportsInterface(0x51945447)); // IERC6551Executable
assertTrue(TokenAccountC(account).supportsInterface(type(IERC1155Receiver).interfaceId));
}
function test_C_receivesMintedAndTransferredUnits() public {
Rentable1155 rentable = new Rentable1155(true);
address payable account = _account(address(new TokenAccountC()), bytes32(0));
// TokenAccountC can receive minted ERC-1155 units
rentable.mint(account, 3, 10);
assertEq(rentable.balanceOf(account, 3), 10);
// TokenAccountC can receive transferred ERC-1155 units
rentable.mint(alice, 3, 5);
vm.prank(alice);
rentable.safeTransferFrom(alice, account, 3, 5, "");
assertEq(rentable.balanceOf(account, 3), 15);
// Negative check: TokenAccountB reverts when receiving ERC-1155 units
address payable accountB = _account(address(new TokenAccountB()), bytes32(uint256(2)));
vm.expectRevert();
rentable.mint(accountB, 3, 10);
}
function test_C_lockBlocksExecute() public {
address payable account = _account(address(new TokenAccountC()), bytes32(0));
vm.prank(alice);
TokenAccountC(account).lock(LOCK_UNTIL);
bytes memory sweep = abi.encodeCall(IERC20.transfer, (alice, 100e6));
vm.prank(alice);
vm.expectRevert("account is locked");
TokenAccountC(account).execute(address(usdc), 0, sweep, 0);
assertEq(usdc.balanceOf(account), 100e6);
vm.warp(LOCK_UNTIL);
vm.prank(alice);
TokenAccountC(account).execute(address(usdc), 0, sweep, 0);
assertEq(usdc.balanceOf(alice), 100e6);
assertEq(usdc.balanceOf(account), 0);
}
}
contract CollectionVariantsTest is Test {
address alice = makeAddr("alice");
address bob = makeAddr("bob");
address carol = makeAddr("carol");
uint256 constant ID = 42;
uint64 expiry;
function setUp() public {
vm.warp(1_700_000_000);
expiry = uint64(block.timestamp + 2 days);
}
function _lockedToken(CollectionBase c) internal returns (CollectionBase) {
c.mint(alice, ID);
vm.prank(alice);
c.setLocked(ID, true);
return c;
}
function test_V1_setUserAllowedWhileLocked() public {
CollectionBase c = _lockedToken(new CollectionV1());
assertTrue(c.locked(ID));
vm.prank(alice);
c.setUser(ID, bob, expiry);
assertEq(c.userOf(ID), bob);
assertEq(c.userExpires(ID), expiry);
assertEq(c.ownerOf(ID), alice);
assertTrue(c.locked(ID));
}
function test_V1_transferBlockedWhileLocked() public {
CollectionBase c = _lockedToken(new CollectionV1());
vm.prank(alice);
vm.expectRevert("token is locked");
c.transferFrom(alice, bob, ID);
}
function test_V1_ownerCanUnlock() public {
CollectionBase c = _lockedToken(new CollectionV1());
vm.prank(alice);
c.setLocked(ID, false);
assertFalse(c.locked(ID));
}
function test_V2_setUserRevertsWhileLocked() public {
CollectionBase c = _lockedToken(new CollectionV2());
vm.prank(alice);
vm.expectRevert("token is locked");
c.setUser(ID, bob, expiry);
assertEq(c.userOf(ID), address(0));
}
function test_V2_approvedOperatorAlsoReverts() public {
CollectionBase c = _lockedToken(new CollectionV2());
vm.prank(alice);
c.approve(bob, ID);
vm.prank(bob);
vm.expectRevert("token is locked");
c.setUser(ID, bob, expiry);
}
function testFuzz_V2_noUnlockPathForAnyCaller(address caller) public {
CollectionBase c = _lockedToken(new CollectionV2());
vm.prank(caller);
vm.expectRevert("lock is permanent");
c.setLocked(ID, false);
assertTrue(c.locked(ID));
}
function test_V2_transferBlockedSoLockCannotBeEscaped() public {
CollectionBase c = _lockedToken(new CollectionV2());
vm.prank(alice);
vm.expectRevert("token is locked");
c.transferFrom(alice, bob, ID);
}
function test_V1_V2_sameErcInterfaces() public {
CollectionBase a = new CollectionV1();
CollectionBase b = new CollectionV2();
bytes4[4] memory ids = [bytes4(0x80ac58cd), 0x5b5e139f, 0xad092b5c, 0xb45a3c0e];
for (uint256 i; i < ids.length; ++i) {
assertTrue(a.supportsInterface(ids[i]));
assertTrue(b.supportsInterface(ids[i]));
}
}
function test_V1_approvedCurrentUserCanSetUserWhileLocked() public {
CollectionBase c = _lockedToken(new CollectionV1());
vm.startPrank(alice);
c.setUser(ID, carol, uint64(block.timestamp + 30 days));
c.approve(carol, ID);
vm.stopPrank();
vm.prank(carol);
c.setUser(ID, bob, expiry);
assertEq(c.userOf(ID), bob);
assertEq(c.ownerOf(ID), alice);
assertTrue(c.locked(ID));
vm.prank(carol);
vm.expectRevert();
c.approve(bob, ID);
}
function test_V1_currentUserIsNotAuthorizedToSetUser() public {
CollectionBase c = _lockedToken(new CollectionV1());
vm.prank(alice);
c.setUser(ID, carol, uint64(block.timestamp + 30 days));
assertEq(c.userOf(ID), carol);
vm.prank(carol);
vm.expectRevert("caller is not owner nor approved");
c.setUser(ID, bob, expiry);
vm.prank(carol);
vm.expectRevert("only owner");
c.setLocked(ID, false);
vm.prank(carol);
vm.expectRevert();
c.approve(bob, ID);
vm.prank(carol);
vm.expectRevert();
c.transferFrom(alice, bob, ID);
}
function test_V1_clearsUserOnTransferWhileV3PreservesUser() public {
CollectionV1 v1 = new CollectionV1();
CollectionV3 v3 = new CollectionV3();
v1.mint(alice, ID);
v3.mint(alice, ID);
vm.startPrank(alice);
v1.setUser(ID, carol, expiry);
v3.setUser(ID, carol, expiry);
assertEq(v1.userOf(ID), carol);
assertEq(v3.userOf(ID), carol);
v1.transferFrom(alice, bob, ID);
v3.transferFrom(alice, bob, ID);
vm.stopPrank();
assertEq(v1.ownerOf(ID), bob);
assertEq(v3.ownerOf(ID), bob);
assertEq(v1.userOf(ID), address(0));
assertEq(v1.userExpires(ID), 0);
assertEq(v3.userOf(ID), carol);
assertEq(v3.userExpires(ID), expiry);
}
function test_V3_transferBlockedWhileLockedAndSameInterfaces() public {
CollectionV3 v3 = new CollectionV3();
v3.mint(alice, ID);
vm.prank(alice);
v3.setLocked(ID, true);
assertTrue(v3.locked(ID));
vm.prank(alice);
vm.expectRevert("token is locked");
v3.transferFrom(alice, bob, ID);
bytes4[4] memory ids = [bytes4(0x80ac58cd), 0x5b5e139f, 0xad092b5c, 0xb45a3c0e];
for (uint256 i; i < ids.length; ++i) {
assertTrue(v3.supportsInterface(ids[i]));
}
}
}
|