Skip to content

Commit

Permalink
examples/simple: fix openzeppelin remappings and fix OZ ERC721 transf…
Browse files Browse the repository at this point in the history
…erFrom test (#344)
  • Loading branch information
karmacoma-eth committed Aug 14, 2024
1 parent 96bf260 commit 96a44a7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
1 change: 1 addition & 0 deletions examples/simple/remappings.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
multicaller/=src/multicaller/
openzeppelin/=../../tests/lib/openzeppelin-contracts/contracts/
36 changes: 20 additions & 16 deletions examples/tokens/ERC721/test/ERC721Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,23 @@ abstract contract ERC721Test is SymTest, Test {
}

function check_transferFrom(address caller, address from, address to, address other, uint256 tokenId, uint256 otherTokenId) public virtual {
// not realistic in practice
// and in particular msg.sender == address(0) disables auth for OpenZeppelin ERC721
vm.assume(caller != address(0));

// consider other address
require(other != from);
require(other != to);
vm.assume(other != from);
vm.assume(other != to);

// consider other token ids
require(otherTokenId != tokenId);
vm.assume(otherTokenId != tokenId);

// record their current balance
uint256 oldBalanceFrom = IERC721(token).balanceOf(from);
uint256 oldBalanceTo = IERC721(token).balanceOf(to);
uint256 oldBalanceOther = IERC721(token).balanceOf(other);

// record their current owner
// record their current owner
address oldOwner = IERC721(token).ownerOf(tokenId);
address oldOtherTokenOwner = IERC721(token).ownerOf(otherTokenId);

Expand All @@ -81,26 +85,26 @@ abstract contract ERC721Test is SymTest, Test {
}

// ensure requirements of transfer
assert(from == oldOwner);
assert(approved);
assertEq(from, oldOwner);
assertTrue(approved);

// ensure the owner is updated correctly
assert(IERC721(token).ownerOf(tokenId) == to);
assert(IERC721(token).getApproved(tokenId) == address(0)); // ensure the approval is reset
assertEq(IERC721(token).ownerOf(tokenId), to);
assertEq(IERC721(token).getApproved(tokenId), address(0)); // ensure the approval is reset

// ensure the other token's owner is unchanged
assert(IERC721(token).ownerOf(otherTokenId) == oldOtherTokenOwner);
assertEq(IERC721(token).ownerOf(otherTokenId), oldOtherTokenOwner);

// balance update
if (from != to) {
assert(IERC721(token).balanceOf(from) < oldBalanceFrom);
assert(IERC721(token).balanceOf(from) == oldBalanceFrom - 1);
assert(IERC721(token).balanceOf(to) > oldBalanceTo);
assert(IERC721(token).balanceOf(to) == oldBalanceTo + 1);
assertLt(IERC721(token).balanceOf(from), oldBalanceFrom);
assertEq(IERC721(token).balanceOf(from), oldBalanceFrom - 1);
assertGt(IERC721(token).balanceOf(to), oldBalanceTo);
assertEq(IERC721(token).balanceOf(to), oldBalanceTo + 1);
} else {
assert(IERC721(token).balanceOf(from) == oldBalanceFrom);
assert(IERC721(token).balanceOf(to) == oldBalanceTo);
assertEq(IERC721(token).balanceOf(from), oldBalanceFrom);
assertEq(IERC721(token).balanceOf(to), oldBalanceTo);
}
assert(IERC721(token).balanceOf(other) == oldBalanceOther);
assertEq(IERC721(token).balanceOf(other), oldBalanceOther);
}
}
6 changes: 2 additions & 4 deletions examples/tokens/ERC721/test/OpenZeppelinERC721.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ contract OpenZeppelinERC721Test is ERC721Test {
tokenIds[4] = 5;

// account0: {token0, token1}, account1: {token2}, account2: {token3}
vm.prank(deployer);
vm.startPrank(deployer);
token_.transferFrom(deployer, accounts[0], tokenIds[0]);
vm.prank(deployer);
token_.transferFrom(deployer, accounts[0], tokenIds[1]);
vm.prank(deployer);
token_.transferFrom(deployer, accounts[1], tokenIds[2]);
vm.prank(deployer);
token_.transferFrom(deployer, accounts[2], tokenIds[3]);
vm.stopPrank();

vm.prank(accounts[0]);
token_.approve(accounts[2], tokenIds[0]);
Expand Down

0 comments on commit 96a44a7

Please sign in to comment.