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

GMS-1616: Additional erc1155 unit tests #226

Merged
merged 6 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion contracts/token/erc1155/abstract/ERC1155Permit.Sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import "@openzeppelin/contracts/interfaces/IERC1271.sol";
import "solidity-bytes-utils/contracts/BytesLib.sol";
import "./IERC1155Permit.sol";

import {IImmutableERC1155Errors} from "../../../errors/Errors.sol";

abstract contract ERC1155Permit is ERC1155Burnable, EIP712, IERC1155Permit, IImmutableERC1155Errors {
Expand Down Expand Up @@ -90,7 +91,7 @@ abstract contract ERC1155Permit is ERC1155Burnable, EIP712, IERC1155Permit, IImm
returns (bool)
{
return
interfaceId == type(IERC1155Permit).interfaceId || // 0x5604e225
interfaceId == type(IERC1155Permit).interfaceId || // 0x9e3ae8e4
super.supportsInterface(interfaceId);
}

Expand Down
78 changes: 78 additions & 0 deletions test/token/erc1155/ImmutableERC1155.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {Sign} from "../../utils/Sign.sol";
import {DeployOperatorAllowlist} from "../../utils/DeployAllowlistProxy.sol";
import {MockWallet} from "../../../contracts/mocks/MockWallet.sol";
import {MockWalletFactory} from "../../../contracts/mocks/MockWalletFactory.sol";
import {MockEIP1271Wallet} from "../../../contracts/mocks/MockEIP1271Wallet.sol";

contract ImmutableERC1155Test is Test {
ImmutableERC1155 public immutableERC1155;
Expand All @@ -20,6 +21,7 @@ contract ImmutableERC1155Test is Test {
MockWallet public mockWalletModule;
MockWallet public scw;
MockWallet public anotherScw;
MockEIP1271Wallet public eip1271Wallet;
address[] private operatorAddrs;

uint256 deployerPrivateKey = 1;
Expand Down Expand Up @@ -72,6 +74,8 @@ contract ImmutableERC1155Test is Test {
scmf.deploy(address(mockWalletModule), anotherSalt);
anotherScwAddress = scmf.getAddress(address(mockWalletModule), anotherSalt);
anotherScw = MockWallet(anotherScwAddress);

eip1271Wallet = new MockEIP1271Wallet(owner);
}

function _sign(
Expand Down Expand Up @@ -129,6 +133,10 @@ contract ImmutableERC1155Test is Test {
assertEq(immutableERC1155.baseURI(), "test-base-uri");
}

function test_DeploymentShouldSetUri() public {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this test different from line 134?

Copy link
Contributor Author

@bruno-imx bruno-imx Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of those cases of 'you optimize what you measure'.
The forge unit test coverage tool requires a unit test to deploy the contract in order to 'mark' that bit of code as 'tested'

So this is a unit test that deploys the contract and checks that the constructor is correct - in this case, it seems like a big workaround & unnecessary

This unit test validates the uri(uint256 index) function that invokes baseURI() - ignoring the index

assertEq(immutableERC1155.uri(0), immutableERC1155.baseURI());
}

function test_DeploymentAllowlistShouldGiveAdminToOwner() public {
bytes32 adminRole = operatorAllowlist.DEFAULT_ADMIN_ROLE();
assertTrue(operatorAllowlist.hasRole(adminRole, owner));
Expand Down Expand Up @@ -207,6 +215,23 @@ contract ImmutableERC1155Test is Test {
immutableERC1155.permit(owner, spender, true, 1 days, sig);
}

function test_PermitRevertsWhenInvalidSignature() public {
bytes memory sig = bytes("invalid_sig");

vm.expectRevert(IImmutableERC1155Errors.InvalidSignature.selector);

immutableERC1155.permit(owner, spender, true, 1 days, sig);
}

function test_PermitSuccess_UsingSmartContractWalletAsOwner() public {
bytes memory sig = _sign(ownerPrivateKey, address(eip1271Wallet), owner, true, 0, 1 days);

immutableERC1155.permit(address(eip1271Wallet), owner, true, 1 days, sig);

assertEq(immutableERC1155.isApprovedForAll(address(eip1271Wallet), owner), true);
assertEq(immutableERC1155.nonces(address(eip1271Wallet)), 1);
}

/*
* Mints
*/
Expand Down Expand Up @@ -374,5 +399,58 @@ contract ImmutableERC1155Test is Test {
assertEq(immutableERC1155.balanceOf(owner, 1), 5);
assertEq(immutableERC1155.balanceOf(owner, 2), 7);
assertEq(immutableERC1155.totalSupply(1), 5);
assertTrue(immutableERC1155.exists(1));
}

/*
* SupportsInterface
*/
function test_SupportsInterface() public {
assertTrue(immutableERC1155.supportsInterface(0x9e3ae8e4));
}

function test_SupportsInterface_delegatesToSuper() public {
assertTrue(immutableERC1155.supportsInterface(0x01ffc9a7)); //IERC165
}

/*
* Royalties
*/
function test_setDefaultRoyaltyReceiver() public {
vm.prank(owner);
address newFeeReceiver = vm.addr(anotherPrivateKey);
immutableERC1155.setDefaultRoyaltyReceiver(newFeeReceiver, 500);
(address receiver, uint256 royaltyAmount) = immutableERC1155.royaltyInfo(1, 20000);
assertEq(receiver, newFeeReceiver);
// 1000 = 20000 (salePrice) * 500 (new royalty amount) / 10000 (feeDenominator)
assertEq(1000, royaltyAmount);
}

function test_setNFTRoyaltyReceiver() public {
vm.prank(minter);
address newFeeReceiver = vm.addr(anotherPrivateKey);
uint256 tokenID = 10;
immutableERC1155.setNFTRoyaltyReceiver(tokenID, newFeeReceiver, 100);
(address receiver, uint256 royaltyAmount) = immutableERC1155.royaltyInfo(tokenID, 10000);
assertEq(receiver, newFeeReceiver);
// 100 = 10000 (salePrice) * 100 (new royalty amount) / 10000 (feeDenominator)
assertEq(100, royaltyAmount);
}

function test_setNFTRoyaltyReceiverBatch() public {
vm.prank(minter);
address newFeeReceiver = vm.addr(anotherPrivateKey);
uint256[] memory tokenIDs = new uint256[](3);
tokenIDs[0] = 20;
tokenIDs[1] = 21;
tokenIDs[2] = 22;

immutableERC1155.setNFTRoyaltyReceiverBatch(tokenIDs, newFeeReceiver, 100);

for (uint i = 0; i < tokenIDs.length; i++) {
(address receiver, uint256 royaltyAmount) = immutableERC1155.royaltyInfo(tokenIDs[i], 10000);
assertEq(receiver, newFeeReceiver);
assertEq(100, royaltyAmount);
}
}
}