Skip to content

Commit 4e2c9bd

Browse files
authored
Merge pull request #129 from huff-language/reverts-on-erc721
2 parents 3211a84 + 55eec6e commit 4e2c9bd

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/tokens/ERC721.huff

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@
104104
#define macro BALANCE_OF() = takes (0) returns (0) {
105105
NON_PAYABLE() // []
106106
0x04 calldataload // [account]
107+
// revert if account is zero address
108+
dup1 continue jumpi
109+
ZERO_ADDRESS(0x00)
110+
continue:
107111
[BALANCE_LOCATION] LOAD_ELEMENT_FROM_KEYS(0x00) // [balance]
108112
0x00 mstore // []
109113
0x20 0x00 return // []
@@ -114,6 +118,10 @@
114118
#define macro OWNER_OF() = takes (0) returns (0) {
115119
0x04 calldataload // [tokenId]
116120
[OWNER_LOCATION] LOAD_ELEMENT_FROM_KEYS(0x00) // [owner]
121+
// revert if owner is zero address/not minted
122+
dup1 continue jumpi
123+
NOT_MINTED(0x00)
124+
continue:
117125
0x00 mstore // []
118126
0x20 0x00 return // []
119127
}

test/tokens/ERC721.t.sol

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ contract ERC721Test is Test {
118118
// We can't burn a token we don't have
119119
assertEq(token.balanceOf(address(this)), 0);
120120
assertEq(token.getApproved(1337), address(0));
121+
122+
vm.expectRevert(bytes("NOT_MINTED"));
121123
assertEq(token.ownerOf(1337), address(0));
124+
122125
vm.expectRevert(bytes("NOT_MINTED"));
123126
token.burn(1337);
124127

@@ -136,6 +139,8 @@ contract ERC721Test is Test {
136139
token.burn(1337);
137140
assertEq(token.balanceOf(address(this)), 0);
138141
assertEq(token.getApproved(1337), address(0));
142+
143+
vm.expectRevert(bytes("NOT_MINTED"));
139144
assertEq(token.ownerOf(1337), address(0));
140145

141146
vm.expectRevert(bytes("NOT_MINTED"));
@@ -459,8 +464,8 @@ contract ERC721Test is Test {
459464
}
460465

461466
function testBalanceOfZeroAddress() public {
467+
vm.expectRevert(bytes("ZERO_ADDRESS"));
462468
uint256 bal = token.balanceOf(address(0));
463-
assertEq(0, bal);
464469
}
465470

466471
// function testFailOwnerOfUnminted() public view {
@@ -484,12 +489,8 @@ contract ERC721Test is Test {
484489

485490
assertEq(token.balanceOf(to), 0);
486491

487-
// vm.expectRevert("NOT_MINTED");
488-
// token.ownerOf(id);
489-
490-
// vm.expectRevert("NOT_MINTED");
491-
address owner = token.ownerOf(id);
492-
assertEq(owner, address(0));
492+
vm.expectRevert(bytes("NOT_MINTED"));
493+
token.ownerOf(id);
493494
}
494495

495496
function testApprove(address to, uint256 id) public {
@@ -512,7 +513,7 @@ contract ERC721Test is Test {
512513
assertEq(token.balanceOf(address(this)), 0);
513514
assertEq(token.getApproved(id), address(0));
514515

515-
// vm.expectRevert("NOT_MINTED");
516+
vm.expectRevert(bytes("NOT_MINTED"));
516517
address owner = token.ownerOf(id);
517518
assertEq(owner, address(0));
518519
}
@@ -819,7 +820,7 @@ contract ERC721Test is Test {
819820
}
820821

821822
function testOwnerOfUnminted(uint256 id) public {
823+
vm.expectRevert(bytes("NOT_MINTED"));
822824
address owner = token.ownerOf(id);
823-
assertEq(owner, address(0));
824825
}
825826
}

0 commit comments

Comments
 (0)