diff --git a/test/foundry/dapp/Application.t.sol b/test/foundry/dapp/Application.t.sol index fd941552..ed18cd93 100644 --- a/test/foundry/dapp/Application.t.sol +++ b/test/foundry/dapp/Application.t.sol @@ -254,42 +254,31 @@ contract ApplicationTest is ERC165Test { ); _appContract.executeOutput(output, proof); - vm.prank(_tokenOwner); - _erc20Token.transfer(address(_appContract), _transferAmount); - - uint256 recipientBalance = _erc20Token.balanceOf(address(_recipient)); - uint256 appBalance = _erc20Token.balanceOf(address(_appContract)); - - _expectEmitOutputExecuted(output, proof); - _appContract.executeOutput(output, proof); + _testERC20Success(output, proof); + } - assertEq( - _erc20Token.balanceOf(address(_recipient)), - recipientBalance + _transferAmount, - "Recipient should have received the transfer amount" - ); + function testExecuteERC721TransferVoucher() external { + string memory name = "ERC721TransferVoucher"; + bytes memory output = _getOutput(name); + OutputValidityProof memory proof = _getProof(name); - assertEq( - _erc20Token.balanceOf(address(_appContract)), - appBalance - _transferAmount, - "Application contract should have the transfer amount deducted" - ); + _testERC721Transfer(output, proof); + } - assertTrue( - _wasOutputExecuted(proof), - "Output should be marked as executed" - ); + function testExecuteERC1155SingleTransferVoucher() external { + string memory name = "ERC1155SingleTransferVoucher"; + bytes memory output = _getOutput(name); + OutputValidityProof memory proof = _getProof(name); - _expectRevertOutputNotReexecutable(output); - _appContract.executeOutput(output, proof); + _testERC1155SingleTransfer(output, proof); } - function testExecuteERC721TransferVoucher() external { - string memory name = "ERC721TransferVoucher"; + function testExecuteERC1155BatchTransferVoucher() external { + string memory name = "ERC1155BatchTransferVoucher"; bytes memory output = _getOutput(name); OutputValidityProof memory proof = _getProof(name); - _testERC721Transfer(output, proof); + _testERC1155BatchTransfer(output, proof); } function testExecuteEmptyOutput() external { @@ -386,56 +375,7 @@ contract ApplicationTest is ERC165Test { bytes memory output = _getOutput(name); OutputValidityProof memory proof = _getProof(name); - vm.expectRevert( - abi.encodeWithSelector( - IERC1155Errors.ERC1155InsufficientBalance.selector, - address(_appContract), - 0, - _transferAmount, - _tokenId - ) - ); - _appContract.executeOutput(output, proof); - - vm.prank(_tokenOwner); - _erc1155SingleToken.safeTransferFrom( - _tokenOwner, - address(_appContract), - _tokenId, - _initialSupply, - "" - ); - - uint256 recipientBalance = _erc1155SingleToken.balanceOf( - _recipient, - _tokenId - ); - uint256 appBalance = _erc1155SingleToken.balanceOf( - address(_appContract), - _tokenId - ); - - _expectEmitOutputExecuted(output, proof); - _appContract.executeOutput(output, proof); - - assertEq( - _erc1155SingleToken.balanceOf(address(_appContract), _tokenId), - appBalance - _transferAmount, - "Application contract should have the transfer amount deducted" - ); - assertEq( - _erc1155SingleToken.balanceOf(_recipient, _tokenId), - recipientBalance + _transferAmount, - "Recipient should have received the transfer amount" - ); - - assertTrue( - _wasOutputExecuted(proof), - "Output should be marked as executed" - ); - - _expectRevertOutputNotReexecutable(output); - _appContract.executeOutput(output, proof); + _testERC1155SingleTransfer(output, proof); } function testERC1155BatchTransferToENS() external { @@ -443,66 +383,7 @@ contract ApplicationTest is ERC165Test { bytes memory output = _getOutput(name); OutputValidityProof memory proof = _getProof(name); - vm.expectRevert( - abi.encodeWithSelector( - IERC1155Errors.ERC1155InsufficientBalance.selector, - address(_appContract), - 0, - _transferAmounts[0], - _tokenIds[0] - ) - ); - _appContract.executeOutput(output, proof); - - vm.prank(_tokenOwner); - _erc1155BatchToken.safeBatchTransferFrom( - _tokenOwner, - address(_appContract), - _tokenIds, - _initialSupplies, - "" - ); - - uint256 batchLength = _initialSupplies.length; - uint256[] memory appBalances = new uint256[](batchLength); - uint256[] memory recipientBalances = new uint256[](batchLength); - for (uint256 i; i < batchLength; ++i) { - appBalances[i] = _erc1155BatchToken.balanceOf( - address(_appContract), - _tokenIds[i] - ); - recipientBalances[i] = _erc1155BatchToken.balanceOf( - _recipient, - _tokenIds[i] - ); - } - - _expectEmitOutputExecuted(output, proof); - _appContract.executeOutput(output, proof); - - for (uint256 i; i < _tokenIds.length; ++i) { - assertEq( - _erc1155BatchToken.balanceOf( - address(_appContract), - _tokenIds[i] - ), - appBalances[i] - _transferAmounts[i], - "Application contract should have the transfer amount deducted" - ); - assertEq( - _erc1155BatchToken.balanceOf(_recipient, _tokenIds[i]), - recipientBalances[i] + _transferAmounts[i], - "Recipient should have received the transfer amount" - ); - } - - assertTrue( - _wasOutputExecuted(proof), - "Output should be marked as executed" - ); - - _expectRevertOutputNotReexecutable(output); - _appContract.executeOutput(output, proof); + _testERC1155BatchTransfer(output, proof); } // ------- @@ -632,13 +513,51 @@ contract ApplicationTest is ERC165Test { 0, abi.encodeWithSignature( "safeTransferFrom(address,address,uint256)", - _appContract, + address(_appContract), _recipient, _tokenId ) ) ) ); + _nameOutput( + "ERC1155SingleTransferVoucher", + _addOutput( + _encodeVoucher( + address(_erc1155SingleToken), + 0, + abi.encodeCall( + IERC1155.safeTransferFrom, + ( + address(_appContract), + _recipient, + _tokenId, + _transferAmount, + "" + ) + ) + ) + ) + ); + _nameOutput( + "ERC1155BatchTransferVoucher", + _addOutput( + _encodeVoucher( + address(_erc1155BatchToken), + 0, + abi.encodeCall( + IERC1155.safeBatchTransferFrom, + ( + address(_appContract), + _recipient, + _tokenIds, + _transferAmounts, + "" + ) + ) + ) + ) + ); _finishInput(); _finishEpoch(); @@ -1100,4 +1019,126 @@ contract ApplicationTest is ERC165Test { _expectRevertOutputNotReexecutable(output); _appContract.executeOutput(output, proof); } + + function _testERC1155SingleTransfer( + bytes memory output, + OutputValidityProof memory proof + ) internal { + vm.expectRevert( + abi.encodeWithSelector( + IERC1155Errors.ERC1155InsufficientBalance.selector, + address(_appContract), + 0, + _transferAmount, + _tokenId + ) + ); + _appContract.executeOutput(output, proof); + + vm.prank(_tokenOwner); + _erc1155SingleToken.safeTransferFrom( + _tokenOwner, + address(_appContract), + _tokenId, + _initialSupply, + "" + ); + + uint256 recipientBalance = _erc1155SingleToken.balanceOf( + _recipient, + _tokenId + ); + uint256 appBalance = _erc1155SingleToken.balanceOf( + address(_appContract), + _tokenId + ); + + _expectEmitOutputExecuted(output, proof); + _appContract.executeOutput(output, proof); + + assertEq( + _erc1155SingleToken.balanceOf(address(_appContract), _tokenId), + appBalance - _transferAmount, + "Application contract should have the transfer amount deducted" + ); + assertEq( + _erc1155SingleToken.balanceOf(_recipient, _tokenId), + recipientBalance + _transferAmount, + "Recipient should have received the transfer amount" + ); + + assertTrue( + _wasOutputExecuted(proof), + "Output should be marked as executed" + ); + + _expectRevertOutputNotReexecutable(output); + _appContract.executeOutput(output, proof); + } + + function _testERC1155BatchTransfer( + bytes memory output, + OutputValidityProof memory proof + ) internal { + vm.expectRevert( + abi.encodeWithSelector( + IERC1155Errors.ERC1155InsufficientBalance.selector, + address(_appContract), + 0, + _transferAmounts[0], + _tokenIds[0] + ) + ); + _appContract.executeOutput(output, proof); + + vm.prank(_tokenOwner); + _erc1155BatchToken.safeBatchTransferFrom( + _tokenOwner, + address(_appContract), + _tokenIds, + _initialSupplies, + "" + ); + + uint256 batchLength = _initialSupplies.length; + uint256[] memory appBalances = new uint256[](batchLength); + uint256[] memory recipientBalances = new uint256[](batchLength); + for (uint256 i; i < batchLength; ++i) { + appBalances[i] = _erc1155BatchToken.balanceOf( + address(_appContract), + _tokenIds[i] + ); + recipientBalances[i] = _erc1155BatchToken.balanceOf( + _recipient, + _tokenIds[i] + ); + } + + _expectEmitOutputExecuted(output, proof); + _appContract.executeOutput(output, proof); + + for (uint256 i; i < _tokenIds.length; ++i) { + assertEq( + _erc1155BatchToken.balanceOf( + address(_appContract), + _tokenIds[i] + ), + appBalances[i] - _transferAmounts[i], + "Application contract should have the transfer amount deducted" + ); + assertEq( + _erc1155BatchToken.balanceOf(_recipient, _tokenIds[i]), + recipientBalances[i] + _transferAmounts[i], + "Recipient should have received the transfer amount" + ); + } + + assertTrue( + _wasOutputExecuted(proof), + "Output should be marked as executed" + ); + + _expectRevertOutputNotReexecutable(output); + _appContract.executeOutput(output, proof); + } } diff --git a/test/foundry/portals/ERC1155BatchPortal.t.sol b/test/foundry/portals/ERC1155BatchPortal.t.sol index 9b9c654d..0c3fe027 100644 --- a/test/foundry/portals/ERC1155BatchPortal.t.sol +++ b/test/foundry/portals/ERC1155BatchPortal.t.sol @@ -13,16 +13,7 @@ import {IPortal} from "contracts/portals/IPortal.sol"; import {InputEncoding} from "contracts/common/InputEncoding.sol"; import {ERC165Test} from "../util/ERC165Test.sol"; - -contract NormalToken is ERC1155 { - constructor( - address tokenOwner, - uint256[] memory tokenIds, - uint256[] memory supplies - ) ERC1155("BatchToken") { - _mintBatch(tokenOwner, tokenIds, supplies, ""); - } -} +import {SimpleBatchERC1155} from "../util/SimpleERC1155.sol"; contract ERC1155BatchPortalTest is ERC165Test { address _alice; @@ -137,7 +128,7 @@ contract ERC1155BatchPortalTest is ERC165Test { ); } - function testNormalToken( + function testSimpleBatchERC1155( uint256[] calldata supplies, bytes calldata baseLayerData, bytes calldata execLayerData @@ -152,7 +143,7 @@ contract ERC1155BatchPortalTest is ERC165Test { values[i] = bound(i, 0, supplies[i]); } - _token = new NormalToken(_alice, tokenIds, supplies); + _token = new SimpleBatchERC1155(_alice, tokenIds, supplies); vm.startPrank(_alice); diff --git a/test/foundry/portals/ERC1155SinglePortal.t.sol b/test/foundry/portals/ERC1155SinglePortal.t.sol index fd3570bd..b8ac2f0c 100644 --- a/test/foundry/portals/ERC1155SinglePortal.t.sol +++ b/test/foundry/portals/ERC1155SinglePortal.t.sol @@ -13,16 +13,7 @@ import {IPortal} from "contracts/portals/IPortal.sol"; import {InputEncoding} from "contracts/common/InputEncoding.sol"; import {ERC165Test} from "../util/ERC165Test.sol"; - -contract NormalToken is ERC1155 { - constructor( - address tokenOwner, - uint256 tokenId, - uint256 supply - ) ERC1155("NormalToken") { - _mint(tokenOwner, tokenId, supply, ""); - } -} +import {SimpleSingleERC1155} from "../util/SimpleERC1155.sol"; contract ERC1155SinglePortalTest is ERC165Test { address _alice; @@ -137,7 +128,7 @@ contract ERC1155SinglePortalTest is ERC165Test { ); } - function testNormalToken( + function testSimpleSingleERC1155( uint256 tokenId, uint256 supply, uint256 value, @@ -145,7 +136,7 @@ contract ERC1155SinglePortalTest is ERC165Test { bytes calldata execLayerData ) public { value = bound(value, 0, supply); - _token = new NormalToken(_alice, tokenId, supply); + _token = new SimpleSingleERC1155(_alice, tokenId, supply); vm.startPrank(_alice); diff --git a/test/foundry/portals/ERC20Portal.t.sol b/test/foundry/portals/ERC20Portal.t.sol index ee4f27f6..1e124679 100644 --- a/test/foundry/portals/ERC20Portal.t.sol +++ b/test/foundry/portals/ERC20Portal.t.sol @@ -13,15 +13,7 @@ import {IPortal} from "contracts/portals/IPortal.sol"; import {InputEncoding} from "contracts/common/InputEncoding.sol"; import {ERC165Test} from "../util/ERC165Test.sol"; - -contract NormalToken is ERC20 { - constructor( - address tokenOwner, - uint256 initialSupply - ) ERC20("NormalToken", "NORMAL") { - _mint(tokenOwner, initialSupply); - } -} +import {SimpleERC20} from "../util/SimpleERC20.sol"; contract ERC20PortalTest is ERC165Test { address _alice; @@ -115,14 +107,14 @@ contract ERC20PortalTest is ERC165Test { _portal.depositERC20Tokens(_token, _appContract, value, data); } - function testNormalToken( + function testSimpleERC20( uint256 supply, uint256 value, bytes calldata data ) public { value = bound(value, 0, supply); - NormalToken token = new NormalToken(_alice, supply); + SimpleERC20 token = new SimpleERC20(_alice, supply); bytes memory payload = _encodePayload(token, value, data); diff --git a/test/foundry/portals/ERC721Portal.t.sol b/test/foundry/portals/ERC721Portal.t.sol index 9fa11fcd..13d4890a 100644 --- a/test/foundry/portals/ERC721Portal.t.sol +++ b/test/foundry/portals/ERC721Portal.t.sol @@ -13,15 +13,7 @@ import {IPortal} from "contracts/portals/IPortal.sol"; import {InputEncoding} from "contracts/common/InputEncoding.sol"; import {ERC165Test} from "../util/ERC165Test.sol"; - -contract NormalToken is ERC721 { - constructor( - address tokenOwner, - uint256 tokenId - ) ERC721("NormalToken", "NORMAL") { - _safeMint(tokenOwner, tokenId); - } -} +import {SimpleERC721} from "../util/SimpleERC721.sol"; contract ERC721PortalTest is ERC165Test { address _alice; @@ -130,12 +122,12 @@ contract ERC721PortalTest is ERC165Test { ); } - function testNormalToken( + function testSimpleERC721( uint256 tokenId, bytes calldata baseLayerData, bytes calldata execLayerData ) public { - NormalToken token = new NormalToken(_alice, tokenId); + SimpleERC721 token = new SimpleERC721(_alice, tokenId); vm.startPrank(_alice);