Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #129 from huff-language/reverts-on-erc721
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisGD committed Sep 1, 2023
2 parents 3211a84 + 55eec6e commit 4e2c9bd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/tokens/ERC721.huff
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
#define macro BALANCE_OF() = takes (0) returns (0) {
NON_PAYABLE() // []
0x04 calldataload // [account]
// revert if account is zero address
dup1 continue jumpi
ZERO_ADDRESS(0x00)
continue:
[BALANCE_LOCATION] LOAD_ELEMENT_FROM_KEYS(0x00) // [balance]
0x00 mstore // []
0x20 0x00 return // []
Expand All @@ -114,6 +118,10 @@
#define macro OWNER_OF() = takes (0) returns (0) {
0x04 calldataload // [tokenId]
[OWNER_LOCATION] LOAD_ELEMENT_FROM_KEYS(0x00) // [owner]
// revert if owner is zero address/not minted
dup1 continue jumpi
NOT_MINTED(0x00)
continue:
0x00 mstore // []
0x20 0x00 return // []
}
Expand Down
19 changes: 10 additions & 9 deletions test/tokens/ERC721.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ contract ERC721Test is Test {
// We can't burn a token we don't have
assertEq(token.balanceOf(address(this)), 0);
assertEq(token.getApproved(1337), address(0));

vm.expectRevert(bytes("NOT_MINTED"));
assertEq(token.ownerOf(1337), address(0));

vm.expectRevert(bytes("NOT_MINTED"));
token.burn(1337);

Expand All @@ -136,6 +139,8 @@ contract ERC721Test is Test {
token.burn(1337);
assertEq(token.balanceOf(address(this)), 0);
assertEq(token.getApproved(1337), address(0));

vm.expectRevert(bytes("NOT_MINTED"));
assertEq(token.ownerOf(1337), address(0));

vm.expectRevert(bytes("NOT_MINTED"));
Expand Down Expand Up @@ -459,8 +464,8 @@ contract ERC721Test is Test {
}

function testBalanceOfZeroAddress() public {
vm.expectRevert(bytes("ZERO_ADDRESS"));
uint256 bal = token.balanceOf(address(0));
assertEq(0, bal);
}

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

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

// vm.expectRevert("NOT_MINTED");
// token.ownerOf(id);

// vm.expectRevert("NOT_MINTED");
address owner = token.ownerOf(id);
assertEq(owner, address(0));
vm.expectRevert(bytes("NOT_MINTED"));
token.ownerOf(id);
}

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

// vm.expectRevert("NOT_MINTED");
vm.expectRevert(bytes("NOT_MINTED"));
address owner = token.ownerOf(id);
assertEq(owner, address(0));
}
Expand Down Expand Up @@ -819,7 +820,7 @@ contract ERC721Test is Test {
}

function testOwnerOfUnminted(uint256 id) public {
vm.expectRevert(bytes("NOT_MINTED"));
address owner = token.ownerOf(id);
assertEq(owner, address(0));
}
}

0 comments on commit 4e2c9bd

Please sign in to comment.