From 110d95f9ff561fcfbac3f69d00a8487daff4b433 Mon Sep 17 00:00:00 2001 From: Diana Kocsis Date: Wed, 22 May 2024 22:59:27 -0400 Subject: [PATCH 1/8] remove unnecessary tests --- test/ERC6909Claims.t.sol | 338 --------------------------------------- 1 file changed, 338 deletions(-) diff --git a/test/ERC6909Claims.t.sol b/test/ERC6909Claims.t.sol index 5c8079bd7..d77064307 100644 --- a/test/ERC6909Claims.t.sol +++ b/test/ERC6909Claims.t.sol @@ -49,342 +49,4 @@ contract ERC6909ClaimsTest is Test, GasSnapshot { vm.expectRevert(); token.burnFrom(address(this), 1337, 100); } - - /// ---- Tests copied from solmate ---- /// - - function testMint() public { - token.mint(address(0xBEEF), 1337, 100); - snapLastCall("ERC6909Claims mint"); - - assertEq(token.balanceOf(address(0xBEEF), 1337), 100); - } - - function testBurn() public { - token.mint(address(0xBEEF), 1337, 100); - vm.prank(address(0xBEEF)); - token.burn(1337, 70); - snapLastCall("ERC6909Claims burn"); - - assertEq(token.balanceOf(address(0xBEEF), 1337), 30); - } - - function testSetOperator() public { - token.setOperator(address(0xBEEF), true); - - assertTrue(token.isOperator(address(this), address(0xBEEF))); - } - - function testApprove() public { - token.approve(address(0xBEEF), 1337, 100); - snapLastCall("ERC6909Claims approve"); - - assertEq(token.allowance(address(this), address(0xBEEF), 1337), 100); - } - - function testTransfer() public { - address sender = address(0xABCD); - - token.mint(sender, 1337, 100); - - vm.prank(sender); - - token.transfer(address(0xBEEF), 1337, 70); - snapLastCall("ERC6909Claims transfer"); - - assertEq(token.balanceOf(sender, 1337), 30); - assertEq(token.balanceOf(address(0xBEEF), 1337), 70); - } - - function testTransferFromWithApproval() public { - address sender = address(0xABCD); - address receiver = address(0xBEEF); - - token.mint(sender, 1337, 100); - - vm.prank(sender); - token.approve(address(this), 1337, 100); - - token.transferFrom(sender, receiver, 1337, 70); - snapLastCall("ERC6909Claims transferFrom with approval"); - - assertEq(token.allowance(sender, address(this), 1337), 30); - assertEq(token.balanceOf(sender, 1337), 30); - assertEq(token.balanceOf(receiver, 1337), 70); - } - - function testTransferFromWithInfiniteApproval() public { - address sender = address(0xABCD); - address receiver = address(0xBEEF); - - token.mint(sender, 1337, 100); - - vm.prank(sender); - token.approve(address(this), 1337, type(uint256).max); - - token.transferFrom(sender, receiver, 1337, 70); - snapLastCall("ERC6909Claims transferFrom with infinite approval"); - - assertEq(token.allowance(sender, address(this), 1337), type(uint256).max); - assertEq(token.balanceOf(sender, 1337), 30); - assertEq(token.balanceOf(receiver, 1337), 70); - } - - function testTransferFromAsOperator() public { - address sender = address(0xABCD); - address receiver = address(0xBEEF); - - token.mint(sender, 1337, 100); - - vm.prank(sender); - token.setOperator(address(this), true); - - token.transferFrom(sender, receiver, 1337, 70); - snapLastCall("ERC6909Claims transferFrom as operator"); - - assertEq(token.balanceOf(sender, 1337), 30); - assertEq(token.balanceOf(receiver, 1337), 70); - } - - function testFailMintBalanceOverflow() public { - token.mint(address(0xDEAD), 1337, type(uint256).max); - token.mint(address(0xDEAD), 1337, 1); - } - - function testFailTransferBalanceUnderflow() public { - address sender = address(0xABCD); - address receiver = address(0xBEEF); - - vm.prank(sender); - token.transferFrom(sender, receiver, 1337, 1); - } - - function testFailTransferBalanceOverflow() public { - address sender = address(0xABCD); - address receiver = address(0xBEEF); - - token.mint(sender, 1337, type(uint256).max); - - vm.prank(sender); - token.transferFrom(sender, receiver, 1337, type(uint256).max); - - token.mint(sender, 1337, 1); - - vm.prank(sender); - token.transferFrom(sender, receiver, 1337, 1); - } - - function testFailTransferFromBalanceUnderflow() public { - address sender = address(0xABCD); - address receiver = address(0xBEEF); - - vm.prank(sender); - token.transferFrom(sender, receiver, 1337, 1); - } - - function testFailTransferFromBalanceOverflow() public { - address sender = address(0xABCD); - address receiver = address(0xBEEF); - - token.mint(sender, 1337, type(uint256).max); - - vm.prank(sender); - token.transferFrom(sender, receiver, 1337, type(uint256).max); - - token.mint(sender, 1337, 1); - - vm.prank(sender); - token.transferFrom(sender, receiver, 1337, 1); - } - - function testFailTransferFromNotAuthorized() public { - address sender = address(0xABCD); - address receiver = address(0xBEEF); - - token.mint(sender, 1337, 100); - - token.transferFrom(sender, receiver, 1337, 100); - } - - function testMint(address receiver, uint256 id, uint256 amount) public { - token.mint(receiver, id, amount); - - assertEq(token.balanceOf(receiver, id), amount); - } - - function testBurn(address sender, uint256 id, uint256 amount) public { - token.mint(sender, id, amount); - vm.prank(sender); - token.burn(id, amount); - - assertEq(token.balanceOf(sender, id), 0); - } - - function testSetOperator(address operator, bool approved) public { - token.setOperator(operator, approved); - - assertEq(token.isOperator(address(this), operator), approved); - } - - function testApprove(address spender, uint256 id, uint256 amount) public { - token.approve(spender, id, amount); - - assertEq(token.allowance(address(this), spender, id), amount); - } - - function testTransfer(address sender, address receiver, uint256 id, uint256 mintAmount, uint256 transferAmount) - public - { - transferAmount = bound(transferAmount, 0, mintAmount); - - token.mint(sender, id, mintAmount); - - vm.prank(sender); - token.transfer(receiver, id, transferAmount); - - if (sender == receiver) { - assertEq(token.balanceOf(sender, id), mintAmount); - } else { - assertEq(token.balanceOf(sender, id), mintAmount - transferAmount); - assertEq(token.balanceOf(receiver, id), transferAmount); - } - } - - function testTransferFromWithApproval( - address sender, - address receiver, - uint256 id, - uint256 mintAmount, - uint256 transferAmount - ) public { - transferAmount = bound(transferAmount, 0, mintAmount); - - token.mint(sender, id, mintAmount); - - vm.prank(sender); - token.approve(address(this), id, mintAmount); - - token.transferFrom(sender, receiver, id, transferAmount); - - if (mintAmount == type(uint256).max) { - assertEq(token.allowance(sender, address(this), id), type(uint256).max); - } else { - if (sender != address(this)) { - assertEq(token.allowance(sender, address(this), id), mintAmount - transferAmount); - } else { - assertEq(token.allowance(sender, address(this), id), mintAmount); - } - } - - if (sender == receiver) { - assertEq(token.balanceOf(sender, id), mintAmount); - } else { - assertEq(token.balanceOf(sender, id), mintAmount - transferAmount); - assertEq(token.balanceOf(receiver, id), transferAmount); - } - } - - function testTransferFromWithInfiniteApproval( - address sender, - address receiver, - uint256 id, - uint256 mintAmount, - uint256 transferAmount - ) public { - transferAmount = bound(transferAmount, 0, mintAmount); - - token.mint(sender, id, mintAmount); - - vm.prank(sender); - token.approve(address(this), id, type(uint256).max); - - token.transferFrom(sender, receiver, id, transferAmount); - - assertEq(token.allowance(sender, address(this), id), type(uint256).max); - - if (sender == receiver) { - assertEq(token.balanceOf(sender, id), mintAmount); - } else { - assertEq(token.balanceOf(sender, id), mintAmount - transferAmount); - assertEq(token.balanceOf(receiver, id), transferAmount); - } - } - - function testTransferFromAsOperator( - address sender, - address receiver, - uint256 id, - uint256 mintAmount, - uint256 transferAmount - ) public { - transferAmount = bound(transferAmount, 0, mintAmount); - - token.mint(sender, id, mintAmount); - - vm.prank(sender); - token.setOperator(address(this), true); - - token.transferFrom(sender, receiver, id, transferAmount); - - if (sender == receiver) { - assertEq(token.balanceOf(sender, id), mintAmount); - } else { - assertEq(token.balanceOf(sender, id), mintAmount - transferAmount); - assertEq(token.balanceOf(receiver, id), transferAmount); - } - } - - function testFailTransferBalanceUnderflow(address sender, address receiver, uint256 id, uint256 amount) public { - amount = bound(amount, 1, type(uint256).max); - - vm.prank(sender); - token.transfer(receiver, id, amount); - } - - function testFailTransferBalanceOverflow(address sender, address receiver, uint256 id, uint256 amount) public { - amount = bound(amount, 1, type(uint256).max); - uint256 overflowAmount = type(uint256).max - amount + 1; - - token.mint(sender, id, amount); - - vm.prank(sender); - token.transfer(receiver, id, amount); - - token.mint(sender, id, overflowAmount); - - vm.prank(sender); - token.transfer(receiver, id, overflowAmount); - } - - function testFailTransferFromBalanceUnderflow(address sender, address receiver, uint256 id, uint256 amount) - public - { - amount = bound(amount, 1, type(uint256).max); - - vm.prank(sender); - token.transferFrom(sender, receiver, id, amount); - } - - function testFailTransferFromBalanceOverflow(address sender, address receiver, uint256 id, uint256 amount) public { - amount = bound(amount, 1, type(uint256).max); - uint256 overflowAmount = type(uint256).max - amount + 1; - - token.mint(sender, id, amount); - - vm.prank(sender); - token.transferFrom(sender, receiver, id, amount); - - token.mint(sender, id, overflowAmount); - - vm.prank(sender); - token.transferFrom(sender, receiver, id, overflowAmount); - } - - function testFailTransferFromNotAuthorized(address sender, address receiver, uint256 id, uint256 amount) public { - amount = bound(amount, 1, type(uint256).max); - - token.mint(sender, id, amount); - - vm.assume(sender != address(this)); - token.transferFrom(sender, receiver, id, amount); - } } From 4e870423df914def2dddd1ee6bc2b38eda376dec Mon Sep 17 00:00:00 2001 From: Diana Kocsis Date: Thu, 23 May 2024 11:43:28 -0400 Subject: [PATCH 2/8] rename --- ...ms.sol => ERC6909ClaimsImplementation.sol} | 7 +--- ....sol => ERC6909ClaimsImplementation.t.sol} | 37 ++++++++++++++----- 2 files changed, 29 insertions(+), 15 deletions(-) rename src/test/{MockERC6909Claims.sol => ERC6909ClaimsImplementation.sol} (78%) rename test/{ERC6909Claims.t.sol => ERC6909ClaimsImplementation.t.sol} (55%) diff --git a/src/test/MockERC6909Claims.sol b/src/test/ERC6909ClaimsImplementation.sol similarity index 78% rename from src/test/MockERC6909Claims.sol rename to src/test/ERC6909ClaimsImplementation.sol index 29415792d..01536da45 100644 --- a/src/test/MockERC6909Claims.sol +++ b/src/test/ERC6909ClaimsImplementation.sol @@ -6,7 +6,7 @@ import {ERC6909Claims} from "../ERC6909Claims.sol"; import {CurrencyLibrary, Currency} from "../types/Currency.sol"; /// @notice Mock contract for testing ERC6909Claims -contract MockERC6909Claims is ERC6909Claims { +contract ERC6909ClaimsImplementation is ERC6909Claims { using CurrencyLibrary for Currency; /// @notice mocked mint logic @@ -14,11 +14,6 @@ contract MockERC6909Claims is ERC6909Claims { _mint(to, id, amount); } - /// @notice mocked burn logic - function burn(uint256 id, uint256 amount) public { - _burn(msg.sender, id, amount); - } - /// @notice mocked burn logic without checking sender allowance function burnFrom(address from, uint256 id, uint256 amount) public { _burnFrom(from, id, amount); diff --git a/test/ERC6909Claims.t.sol b/test/ERC6909ClaimsImplementation.t.sol similarity index 55% rename from test/ERC6909Claims.t.sol rename to test/ERC6909ClaimsImplementation.t.sol index d77064307..fa641347c 100644 --- a/test/ERC6909Claims.t.sol +++ b/test/ERC6909ClaimsImplementation.t.sol @@ -3,19 +3,19 @@ pragma solidity ^0.8.15; import {Test} from "forge-std/Test.sol"; import {CurrencyLibrary, Currency} from "../src/types/Currency.sol"; -import {MockERC6909Claims} from "../src/test/MockERC6909Claims.sol"; +import {ERC6909ClaimsImplementation} from "../src/test/ERC6909ClaimsImplementation.sol"; import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol"; contract ERC6909ClaimsTest is Test, GasSnapshot { using CurrencyLibrary for Currency; - MockERC6909Claims token; + ERC6909ClaimsImplementation token; function setUp() public { - token = new MockERC6909Claims(); + token = new ERC6909ClaimsImplementation(); } - function test_burnFrom_withApproval(address sender, uint256 id, uint256 mintAmount, uint256 transferAmount) + function test_burnFrom_withApproval(address sender, uint256 id, uint256 mintAmount, uint256 burnAmount) public { token.mint(sender, id, mintAmount); @@ -23,25 +23,44 @@ contract ERC6909ClaimsTest is Test, GasSnapshot { vm.prank(sender); token.approve(address(this), id, mintAmount); - if (transferAmount > mintAmount) { + if (burnAmount > mintAmount) { vm.expectRevert(); } - token.burnFrom(sender, id, transferAmount); + token.burnFrom(sender, id, burnAmount); - if (transferAmount <= mintAmount) { + if (burnAmount <= mintAmount) { if (mintAmount == type(uint256).max) { assertEq(token.allowance(sender, address(this), id), type(uint256).max); } else { if (sender != address(this)) { - assertEq(token.allowance(sender, address(this), id), mintAmount - transferAmount); + assertEq(token.allowance(sender, address(this), id), mintAmount - burnAmount); } else { assertEq(token.allowance(sender, address(this), id), mintAmount); } } - assertEq(token.balanceOf(sender, id), mintAmount - transferAmount); + assertEq(token.balanceOf(sender, id), mintAmount - burnAmount); } } + function test_burnFrom_withOperator(address sender, uint256 id, uint256 mintAmount, uint256 burnAmount) + public + { + token.mint(sender, id, mintAmount); + + vm.prank(sender); + token.setOperator(address(this), true); + + if (burnAmount > mintAmount) { + vm.expectRevert(); + } + token.burnFrom(sender, id, burnAmount); + + if (burnAmount <= mintAmount) { + assertEq(token.balanceOf(sender, id), mintAmount - burnAmount); + } + assertEq(token.allowance(sender, address(this), id), 0); + } + function test_burnFrom_revertsWithNoApproval() public { token.mint(address(this), 1337, 100); From e8e3bd8590f188c776398b8640adbef498332cd0 Mon Sep 17 00:00:00 2001 From: Diana Kocsis Date: Thu, 23 May 2024 12:41:58 -0400 Subject: [PATCH 3/8] linters --- test/ERC6909ClaimsImplementation.t.sol | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/test/ERC6909ClaimsImplementation.t.sol b/test/ERC6909ClaimsImplementation.t.sol index fa641347c..561161486 100644 --- a/test/ERC6909ClaimsImplementation.t.sol +++ b/test/ERC6909ClaimsImplementation.t.sol @@ -15,9 +15,7 @@ contract ERC6909ClaimsTest is Test, GasSnapshot { token = new ERC6909ClaimsImplementation(); } - function test_burnFrom_withApproval(address sender, uint256 id, uint256 mintAmount, uint256 burnAmount) - public - { + function test_burnFrom_withApproval(address sender, uint256 id, uint256 mintAmount, uint256 burnAmount) public { token.mint(sender, id, mintAmount); vm.prank(sender); @@ -42,9 +40,7 @@ contract ERC6909ClaimsTest is Test, GasSnapshot { } } - function test_burnFrom_withOperator(address sender, uint256 id, uint256 mintAmount, uint256 burnAmount) - public - { + function test_burnFrom_withOperator(address sender, uint256 id, uint256 mintAmount, uint256 burnAmount) public { token.mint(sender, id, mintAmount); vm.prank(sender); From 24a7b46209e5c1a67a605fce148d92f8810f33ec Mon Sep 17 00:00:00 2001 From: Diana Kocsis Date: Thu, 23 May 2024 19:39:09 -0400 Subject: [PATCH 4/8] add back tests for erc6909 coverage --- src/test/ERC6909ClaimsImplementation.sol | 5 + test/ERC6909ClaimsImplementation.t.sol | 338 +++++++++++++++++++++++ 2 files changed, 343 insertions(+) diff --git a/src/test/ERC6909ClaimsImplementation.sol b/src/test/ERC6909ClaimsImplementation.sol index 01536da45..dbe930bd2 100644 --- a/src/test/ERC6909ClaimsImplementation.sol +++ b/src/test/ERC6909ClaimsImplementation.sol @@ -14,6 +14,11 @@ contract ERC6909ClaimsImplementation is ERC6909Claims { _mint(to, id, amount); } + /// @notice mocked burn logic + function burn(uint256 id, uint256 amount) public { + _burn(msg.sender, id, amount); + } + /// @notice mocked burn logic without checking sender allowance function burnFrom(address from, uint256 id, uint256 amount) public { _burnFrom(from, id, amount); diff --git a/test/ERC6909ClaimsImplementation.t.sol b/test/ERC6909ClaimsImplementation.t.sol index 561161486..3993b5331 100644 --- a/test/ERC6909ClaimsImplementation.t.sol +++ b/test/ERC6909ClaimsImplementation.t.sol @@ -64,4 +64,342 @@ contract ERC6909ClaimsTest is Test, GasSnapshot { vm.expectRevert(); token.burnFrom(address(this), 1337, 100); } + + /// ---- Tests copied from solmate ---- /// + + function testMint() public { + token.mint(address(0xBEEF), 1337, 100); + snapLastCall("ERC6909Claims mint"); + + assertEq(token.balanceOf(address(0xBEEF), 1337), 100); + } + + function testBurn() public { + token.mint(address(0xBEEF), 1337, 100); + vm.prank(address(0xBEEF)); + token.burn(1337, 70); + snapLastCall("ERC6909Claims burn"); + + assertEq(token.balanceOf(address(0xBEEF), 1337), 30); + } + + function testSetOperator() public { + token.setOperator(address(0xBEEF), true); + + assertTrue(token.isOperator(address(this), address(0xBEEF))); + } + + function testApprove() public { + token.approve(address(0xBEEF), 1337, 100); + snapLastCall("ERC6909Claims approve"); + + assertEq(token.allowance(address(this), address(0xBEEF), 1337), 100); + } + + function testTransfer() public { + address sender = address(0xABCD); + + token.mint(sender, 1337, 100); + + vm.prank(sender); + + token.transfer(address(0xBEEF), 1337, 70); + snapLastCall("ERC6909Claims transfer"); + + assertEq(token.balanceOf(sender, 1337), 30); + assertEq(token.balanceOf(address(0xBEEF), 1337), 70); + } + + function testTransferFromWithApproval() public { + address sender = address(0xABCD); + address receiver = address(0xBEEF); + + token.mint(sender, 1337, 100); + + vm.prank(sender); + token.approve(address(this), 1337, 100); + + token.transferFrom(sender, receiver, 1337, 70); + snapLastCall("ERC6909Claims transferFrom with approval"); + + assertEq(token.allowance(sender, address(this), 1337), 30); + assertEq(token.balanceOf(sender, 1337), 30); + assertEq(token.balanceOf(receiver, 1337), 70); + } + + function testTransferFromWithInfiniteApproval() public { + address sender = address(0xABCD); + address receiver = address(0xBEEF); + + token.mint(sender, 1337, 100); + + vm.prank(sender); + token.approve(address(this), 1337, type(uint256).max); + + token.transferFrom(sender, receiver, 1337, 70); + snapLastCall("ERC6909Claims transferFrom with infinite approval"); + + assertEq(token.allowance(sender, address(this), 1337), type(uint256).max); + assertEq(token.balanceOf(sender, 1337), 30); + assertEq(token.balanceOf(receiver, 1337), 70); + } + + function testTransferFromAsOperator() public { + address sender = address(0xABCD); + address receiver = address(0xBEEF); + + token.mint(sender, 1337, 100); + + vm.prank(sender); + token.setOperator(address(this), true); + + token.transferFrom(sender, receiver, 1337, 70); + snapLastCall("ERC6909Claims transferFrom as operator"); + + assertEq(token.balanceOf(sender, 1337), 30); + assertEq(token.balanceOf(receiver, 1337), 70); + } + + function testFailMintBalanceOverflow() public { + token.mint(address(0xDEAD), 1337, type(uint256).max); + token.mint(address(0xDEAD), 1337, 1); + } + + function testFailTransferBalanceUnderflow() public { + address sender = address(0xABCD); + address receiver = address(0xBEEF); + + vm.prank(sender); + token.transferFrom(sender, receiver, 1337, 1); + } + + function testFailTransferBalanceOverflow() public { + address sender = address(0xABCD); + address receiver = address(0xBEEF); + + token.mint(sender, 1337, type(uint256).max); + + vm.prank(sender); + token.transferFrom(sender, receiver, 1337, type(uint256).max); + + token.mint(sender, 1337, 1); + + vm.prank(sender); + token.transferFrom(sender, receiver, 1337, 1); + } + + function testFailTransferFromBalanceUnderflow() public { + address sender = address(0xABCD); + address receiver = address(0xBEEF); + + vm.prank(sender); + token.transferFrom(sender, receiver, 1337, 1); + } + + function testFailTransferFromBalanceOverflow() public { + address sender = address(0xABCD); + address receiver = address(0xBEEF); + + token.mint(sender, 1337, type(uint256).max); + + vm.prank(sender); + token.transferFrom(sender, receiver, 1337, type(uint256).max); + + token.mint(sender, 1337, 1); + + vm.prank(sender); + token.transferFrom(sender, receiver, 1337, 1); + } + + function testFailTransferFromNotAuthorized() public { + address sender = address(0xABCD); + address receiver = address(0xBEEF); + + token.mint(sender, 1337, 100); + + token.transferFrom(sender, receiver, 1337, 100); + } + + function testMint(address receiver, uint256 id, uint256 amount) public { + token.mint(receiver, id, amount); + + assertEq(token.balanceOf(receiver, id), amount); + } + + function testBurn(address sender, uint256 id, uint256 amount) public { + token.mint(sender, id, amount); + vm.prank(sender); + token.burn(id, amount); + + assertEq(token.balanceOf(sender, id), 0); + } + + function testSetOperator(address operator, bool approved) public { + token.setOperator(operator, approved); + + assertEq(token.isOperator(address(this), operator), approved); + } + + function testApprove(address spender, uint256 id, uint256 amount) public { + token.approve(spender, id, amount); + + assertEq(token.allowance(address(this), spender, id), amount); + } + + function testTransfer(address sender, address receiver, uint256 id, uint256 mintAmount, uint256 transferAmount) + public + { + transferAmount = bound(transferAmount, 0, mintAmount); + + token.mint(sender, id, mintAmount); + + vm.prank(sender); + token.transfer(receiver, id, transferAmount); + + if (sender == receiver) { + assertEq(token.balanceOf(sender, id), mintAmount); + } else { + assertEq(token.balanceOf(sender, id), mintAmount - transferAmount); + assertEq(token.balanceOf(receiver, id), transferAmount); + } + } + + function testTransferFromWithApproval( + address sender, + address receiver, + uint256 id, + uint256 mintAmount, + uint256 transferAmount + ) public { + transferAmount = bound(transferAmount, 0, mintAmount); + + token.mint(sender, id, mintAmount); + + vm.prank(sender); + token.approve(address(this), id, mintAmount); + + token.transferFrom(sender, receiver, id, transferAmount); + + if (mintAmount == type(uint256).max) { + assertEq(token.allowance(sender, address(this), id), type(uint256).max); + } else { + if (sender != address(this)) { + assertEq(token.allowance(sender, address(this), id), mintAmount - transferAmount); + } else { + assertEq(token.allowance(sender, address(this), id), mintAmount); + } + } + + if (sender == receiver) { + assertEq(token.balanceOf(sender, id), mintAmount); + } else { + assertEq(token.balanceOf(sender, id), mintAmount - transferAmount); + assertEq(token.balanceOf(receiver, id), transferAmount); + } + } + + function testTransferFromWithInfiniteApproval( + address sender, + address receiver, + uint256 id, + uint256 mintAmount, + uint256 transferAmount + ) public { + transferAmount = bound(transferAmount, 0, mintAmount); + + token.mint(sender, id, mintAmount); + + vm.prank(sender); + token.approve(address(this), id, type(uint256).max); + + token.transferFrom(sender, receiver, id, transferAmount); + + assertEq(token.allowance(sender, address(this), id), type(uint256).max); + + if (sender == receiver) { + assertEq(token.balanceOf(sender, id), mintAmount); + } else { + assertEq(token.balanceOf(sender, id), mintAmount - transferAmount); + assertEq(token.balanceOf(receiver, id), transferAmount); + } + } + + function testTransferFromAsOperator( + address sender, + address receiver, + uint256 id, + uint256 mintAmount, + uint256 transferAmount + ) public { + transferAmount = bound(transferAmount, 0, mintAmount); + + token.mint(sender, id, mintAmount); + + vm.prank(sender); + token.setOperator(address(this), true); + + token.transferFrom(sender, receiver, id, transferAmount); + + if (sender == receiver) { + assertEq(token.balanceOf(sender, id), mintAmount); + } else { + assertEq(token.balanceOf(sender, id), mintAmount - transferAmount); + assertEq(token.balanceOf(receiver, id), transferAmount); + } + } + + function testFailTransferBalanceUnderflow(address sender, address receiver, uint256 id, uint256 amount) public { + amount = bound(amount, 1, type(uint256).max); + + vm.prank(sender); + token.transfer(receiver, id, amount); + } + + function testFailTransferBalanceOverflow(address sender, address receiver, uint256 id, uint256 amount) public { + amount = bound(amount, 1, type(uint256).max); + uint256 overflowAmount = type(uint256).max - amount + 1; + + token.mint(sender, id, amount); + + vm.prank(sender); + token.transfer(receiver, id, amount); + + token.mint(sender, id, overflowAmount); + + vm.prank(sender); + token.transfer(receiver, id, overflowAmount); + } + + function testFailTransferFromBalanceUnderflow(address sender, address receiver, uint256 id, uint256 amount) + public + { + amount = bound(amount, 1, type(uint256).max); + + vm.prank(sender); + token.transferFrom(sender, receiver, id, amount); + } + + function testFailTransferFromBalanceOverflow(address sender, address receiver, uint256 id, uint256 amount) public { + amount = bound(amount, 1, type(uint256).max); + uint256 overflowAmount = type(uint256).max - amount + 1; + + token.mint(sender, id, amount); + + vm.prank(sender); + token.transferFrom(sender, receiver, id, amount); + + token.mint(sender, id, overflowAmount); + + vm.prank(sender); + token.transferFrom(sender, receiver, id, overflowAmount); + } + + function testFailTransferFromNotAuthorized(address sender, address receiver, uint256 id, uint256 amount) public { + amount = bound(amount, 1, type(uint256).max); + + token.mint(sender, id, amount); + + vm.assume(sender != address(this)); + token.transferFrom(sender, receiver, id, amount); + } } From 52b23257f33fa82ddf3a4ae12ba2018f48fd523c Mon Sep 17 00:00:00 2001 From: Diana Kocsis Date: Sat, 1 Jun 2024 17:07:17 +0200 Subject: [PATCH 5/8] fixes --- test/ERC6909ClaimsImplementation.t.sol | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/test/ERC6909ClaimsImplementation.t.sol b/test/ERC6909ClaimsImplementation.t.sol index 3993b5331..4ba2583de 100644 --- a/test/ERC6909ClaimsImplementation.t.sol +++ b/test/ERC6909ClaimsImplementation.t.sol @@ -5,6 +5,7 @@ import {Test} from "forge-std/Test.sol"; import {CurrencyLibrary, Currency} from "../src/types/Currency.sol"; import {ERC6909ClaimsImplementation} from "../src/test/ERC6909ClaimsImplementation.sol"; import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol"; +import {stdError} from "forge-std/StdError.sol"; contract ERC6909ClaimsTest is Test, GasSnapshot { using CurrencyLibrary for Currency; @@ -15,14 +16,14 @@ contract ERC6909ClaimsTest is Test, GasSnapshot { token = new ERC6909ClaimsImplementation(); } - function test_burnFrom_withApproval(address sender, uint256 id, uint256 mintAmount, uint256 burnAmount) public { + function test_fuzz_burnFrom_withApproval(address sender, uint256 id, uint256 mintAmount, uint256 burnAmount) public { token.mint(sender, id, mintAmount); vm.prank(sender); token.approve(address(this), id, mintAmount); if (burnAmount > mintAmount) { - vm.expectRevert(); + vm.expectRevert(stdError.arithmeticError); } token.burnFrom(sender, id, burnAmount); @@ -40,28 +41,31 @@ contract ERC6909ClaimsTest is Test, GasSnapshot { } } - function test_burnFrom_withOperator(address sender, uint256 id, uint256 mintAmount, uint256 burnAmount) public { + function test_fuzz_burnFrom_withOperator(address sender, uint256 id, uint256 mintAmount, uint256 burnAmount) public { token.mint(sender, id, mintAmount); + assertFalse(token.isOperator(sender, address(this))); + vm.prank(sender); token.setOperator(address(this), true); + assertTrue(token.isOperator(sender, address(this))); + if (burnAmount > mintAmount) { - vm.expectRevert(); + vm.expectRevert(stdError.arithmeticError); } token.burnFrom(sender, id, burnAmount); if (burnAmount <= mintAmount) { assertEq(token.balanceOf(sender, id), mintAmount - burnAmount); } - assertEq(token.allowance(sender, address(this), id), 0); } function test_burnFrom_revertsWithNoApproval() public { token.mint(address(this), 1337, 100); vm.prank(address(0xBEEF)); - vm.expectRevert(); + vm.expectRevert(stdError.arithmeticError); token.burnFrom(address(this), 1337, 100); } From eaccbc9fadcc0eb1e9d50b14a9a0b17fe13a5ccd Mon Sep 17 00:00:00 2001 From: Diana Kocsis Date: Sat, 1 Jun 2024 17:17:06 +0200 Subject: [PATCH 6/8] linters --- src/test/ERC6909ClaimsImplementation.sol | 6 +----- test/ERC6909ClaimsImplementation.t.sol | 8 ++++++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/ERC6909ClaimsImplementation.sol b/src/test/ERC6909ClaimsImplementation.sol index 501d7b6a5..8c64b0dbe 100644 --- a/src/test/ERC6909ClaimsImplementation.sol +++ b/src/test/ERC6909ClaimsImplementation.sol @@ -3,16 +3,12 @@ pragma solidity ^0.8.20; import {Test} from "forge-std/Test.sol"; import {ERC6909Claims} from "../ERC6909Claims.sol"; -import {Currency} from "../types/Currency.sol"; +import {Currency, CurrencyLibrary} from "../types/Currency.sol"; /// @notice Mock contract for testing ERC6909Claims -<<<<<<< HEAD:src/test/ERC6909ClaimsImplementation.sol contract ERC6909ClaimsImplementation is ERC6909Claims { using CurrencyLibrary for Currency; -======= -contract MockERC6909Claims is ERC6909Claims { ->>>>>>> main:src/test/MockERC6909Claims.sol /// @notice mocked mint logic function mint(address to, uint256 id, uint256 amount) public { _mint(to, id, amount); diff --git a/test/ERC6909ClaimsImplementation.t.sol b/test/ERC6909ClaimsImplementation.t.sol index 4ba2583de..904c12686 100644 --- a/test/ERC6909ClaimsImplementation.t.sol +++ b/test/ERC6909ClaimsImplementation.t.sol @@ -16,7 +16,9 @@ contract ERC6909ClaimsTest is Test, GasSnapshot { token = new ERC6909ClaimsImplementation(); } - function test_fuzz_burnFrom_withApproval(address sender, uint256 id, uint256 mintAmount, uint256 burnAmount) public { + function test_fuzz_burnFrom_withApproval(address sender, uint256 id, uint256 mintAmount, uint256 burnAmount) + public + { token.mint(sender, id, mintAmount); vm.prank(sender); @@ -41,7 +43,9 @@ contract ERC6909ClaimsTest is Test, GasSnapshot { } } - function test_fuzz_burnFrom_withOperator(address sender, uint256 id, uint256 mintAmount, uint256 burnAmount) public { + function test_fuzz_burnFrom_withOperator(address sender, uint256 id, uint256 mintAmount, uint256 burnAmount) + public + { token.mint(sender, id, mintAmount); assertFalse(token.isOperator(sender, address(this))); From da30a4d6cd5ce8ff929a6df2d454ec61a7f9c289 Mon Sep 17 00:00:00 2001 From: Diana Kocsis Date: Tue, 4 Jun 2024 18:15:18 +0200 Subject: [PATCH 7/8] remove unnecessary imports --- src/test/ERC6909ClaimsImplementation.sol | 3 --- test/ERC6909ClaimsImplementation.t.sol | 2 -- 2 files changed, 5 deletions(-) diff --git a/src/test/ERC6909ClaimsImplementation.sol b/src/test/ERC6909ClaimsImplementation.sol index 8c64b0dbe..e27bd6945 100644 --- a/src/test/ERC6909ClaimsImplementation.sol +++ b/src/test/ERC6909ClaimsImplementation.sol @@ -1,13 +1,10 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.20; -import {Test} from "forge-std/Test.sol"; import {ERC6909Claims} from "../ERC6909Claims.sol"; -import {Currency, CurrencyLibrary} from "../types/Currency.sol"; /// @notice Mock contract for testing ERC6909Claims contract ERC6909ClaimsImplementation is ERC6909Claims { - using CurrencyLibrary for Currency; /// @notice mocked mint logic function mint(address to, uint256 id, uint256 amount) public { diff --git a/test/ERC6909ClaimsImplementation.t.sol b/test/ERC6909ClaimsImplementation.t.sol index 904c12686..8ce760360 100644 --- a/test/ERC6909ClaimsImplementation.t.sol +++ b/test/ERC6909ClaimsImplementation.t.sol @@ -2,13 +2,11 @@ pragma solidity ^0.8.15; import {Test} from "forge-std/Test.sol"; -import {CurrencyLibrary, Currency} from "../src/types/Currency.sol"; import {ERC6909ClaimsImplementation} from "../src/test/ERC6909ClaimsImplementation.sol"; import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol"; import {stdError} from "forge-std/StdError.sol"; contract ERC6909ClaimsTest is Test, GasSnapshot { - using CurrencyLibrary for Currency; ERC6909ClaimsImplementation token; From 55273858681ef57cd8b081fe0c45e21b5eb94cc8 Mon Sep 17 00:00:00 2001 From: Diana Kocsis Date: Tue, 4 Jun 2024 18:17:15 +0200 Subject: [PATCH 8/8] fmt --- src/test/ERC6909ClaimsImplementation.sol | 1 - test/ERC6909ClaimsImplementation.t.sol | 1 - 2 files changed, 2 deletions(-) diff --git a/src/test/ERC6909ClaimsImplementation.sol b/src/test/ERC6909ClaimsImplementation.sol index e27bd6945..10a147676 100644 --- a/src/test/ERC6909ClaimsImplementation.sol +++ b/src/test/ERC6909ClaimsImplementation.sol @@ -5,7 +5,6 @@ import {ERC6909Claims} from "../ERC6909Claims.sol"; /// @notice Mock contract for testing ERC6909Claims contract ERC6909ClaimsImplementation is ERC6909Claims { - /// @notice mocked mint logic function mint(address to, uint256 id, uint256 amount) public { _mint(to, id, amount); diff --git a/test/ERC6909ClaimsImplementation.t.sol b/test/ERC6909ClaimsImplementation.t.sol index 8ce760360..1bb6f8def 100644 --- a/test/ERC6909ClaimsImplementation.t.sol +++ b/test/ERC6909ClaimsImplementation.t.sol @@ -7,7 +7,6 @@ import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol"; import {stdError} from "forge-std/StdError.sol"; contract ERC6909ClaimsTest is Test, GasSnapshot { - ERC6909ClaimsImplementation token; function setUp() public {