diff --git a/.gas-snapshot b/.gas-snapshot index b48b480..ae5272d 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -8,10 +8,10 @@ ERC6909Test:testBalanceOf() (gas: 17504) ERC6909Test:testSetOperator() (gas: 42429) ERC6909Test:testSupportsInterface() (gas: 6703) ERC6909Test:testTotalSupply() (gas: 9874) -ERC6909Test:testTransfer() (gas: 48795) -ERC6909Test:testTransferFrom() (gas: 65198) -ERC6909Test:testTransferFromCallerIsOperator() (gas: 78223) -ERC6909Test:testTransferFromCallerIsSpender() (gas: 49295) -ERC6909Test:testTransferFromInsufficientBalance() (gas: 23767) -ERC6909Test:testTransferFromInsufficientPermission() (gas: 29792) -ERC6909Test:testTransferInsufficientBalance() (gas: 23357) \ No newline at end of file +ERC6909Test:testTransfer() (gas: 48420) +ERC6909Test:testTransferFrom() (gas: 64584) +ERC6909Test:testTransferFromCallerIsOperator() (gas: 77803) +ERC6909Test:testTransferFromCallerIsSpender() (gas: 48875) +ERC6909Test:testTransferFromInsufficientBalance() (gas: 22824) +ERC6909Test:testTransferFromInsufficientPermission() (gas: 28868) +ERC6909Test:testTransferInsufficientBalance() (gas: 22441) \ No newline at end of file diff --git a/src/ERC6909.sol b/src/ERC6909.sol index c34b4b9..936f016 100644 --- a/src/ERC6909.sol +++ b/src/ERC6909.sol @@ -4,16 +4,6 @@ pragma solidity ^0.8.19; import "src/interfaces/IERC6909.sol"; contract ERC6909 is IERC6909 { - /// @dev Thrown when owner balance for id is insufficient. - /// @param owner The address of the owner. - /// @param id The id of the token. - error InsufficientBalance(address owner, uint256 id); - - /// @dev Thrown when spender allowance for id is insufficient. - /// @param spender The address of the spender. - /// @param id The id of the token. - error InsufficientPermission(address spender, uint256 id); - /// @notice The total supply of each id. mapping(uint256 id => uint256 amount) public totalSupply; @@ -31,9 +21,14 @@ contract ERC6909 is IERC6909 { /// @param id The id of the token. /// @param amount The amount of the token. function transfer(address receiver, uint256 id, uint256 amount) public { - if (balanceOf[msg.sender][id] < amount) revert InsufficientBalance(msg.sender, id); balanceOf[msg.sender][id] -= amount; - balanceOf[receiver][id] += amount; + + // Cannot overflow because the sum of all user + // balances can't exceed the max uint256 value. + unchecked { + balanceOf[receiver][id] += amount; + } + emit Transfer(msg.sender, receiver, id, amount); } @@ -44,14 +39,17 @@ contract ERC6909 is IERC6909 { /// @param amount The amount of the token. function transferFrom(address sender, address receiver, uint256 id, uint256 amount) public { if (sender != msg.sender && !isOperator[sender][msg.sender]) { - if (allowance[sender][msg.sender][id] < amount) { - revert InsufficientPermission(msg.sender, id); - } allowance[sender][msg.sender][id] -= amount; } - if (balanceOf[sender][id] < amount) revert InsufficientBalance(sender, id); + balanceOf[sender][id] -= amount; - balanceOf[receiver][id] += amount; + + // Cannot overflow because the sum of all user + // balances can't exceed the max uint256 value. + unchecked { + balanceOf[receiver][id] += amount; + } + emit Transfer(sender, receiver, id, amount); } diff --git a/test/ERC6909.t.sol b/test/ERC6909.t.sol index 9f9ff41..8861f67 100644 --- a/test/ERC6909.t.sol +++ b/test/ERC6909.t.sol @@ -131,7 +131,7 @@ contract ERC6909Test is Test { function testTransferInsufficientBalance() public { assertEq(erc6909.balanceOf(alice, tokenId), 1); assertEq(erc6909.balanceOf(bob, tokenId), 0); - vm.expectRevert(abi.encodeWithSelector(InsufficientBalance.selector, alice, tokenId)); + vm.expectRevert(); vm.prank(alice); erc6909.transfer(bob, tokenId, 2); @@ -140,7 +140,7 @@ contract ERC6909Test is Test { function testTransferFromInsufficientBalance() public { assertEq(erc6909.balanceOf(alice, tokenId), 1); assertEq(erc6909.balanceOf(bob, tokenId), 0); - vm.expectRevert(abi.encodeWithSelector(InsufficientBalance.selector, alice, tokenId)); + vm.expectRevert(); vm.prank(alice); erc6909.transferFrom(alice, bob, tokenId, 2); @@ -150,7 +150,7 @@ contract ERC6909Test is Test { assertEq(erc6909.balanceOf(alice, tokenId), 1); assertEq(erc6909.balanceOf(bob, tokenId), 0); assertEq(erc6909.allowance(alice, bob, tokenId), 0); - vm.expectRevert(abi.encodeWithSelector(InsufficientPermission.selector, bob, tokenId)); + vm.expectRevert(); vm.prank(bob); erc6909.transferFrom(alice, bob, tokenId, 1);