Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize for gas #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)
32 changes: 15 additions & 17 deletions src/ERC6909.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions test/ERC6909.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down