Skip to content

Commit

Permalink
fix: ERC-1155 batch portal integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
guidanoli committed Mar 20, 2024
1 parent 4e9d794 commit 0aac953
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions test/foundry/portals/ERC1155BatchPortal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
pragma solidity ^0.8.22;

import {IERC1155, ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import {ERC1155Holder} from "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";

import {ERC1155BatchPortal} from "contracts/portals/ERC1155BatchPortal.sol";
Expand All @@ -25,8 +24,6 @@ contract NormalToken is ERC1155 {
}
}

contract TokenHolder is ERC1155Holder {}

contract ERC1155BatchPortalTest is ERC165Test {
address _alice;
address _app;
Expand Down Expand Up @@ -77,17 +74,17 @@ contract ERC1155BatchPortalTest is ERC165Test {
vm.mockCall(address(_token), safeBatchTransferFrom, abi.encode());
vm.expectCall(address(_token), safeBatchTransferFrom, 1);

bytes memory payload = _encodePayload(
bytes memory payload = encodePayload(
tokenIds,
values,
baseLayerData,
execLayerData
);

bytes memory addInputCall = _encodeAddInput(payload);
bytes memory addInput = _encodeAddInput(payload);

vm.mockCall(address(_inputBox), addInputCall, abi.encode(bytes32(0)));
vm.expectCall(address(_inputBox), addInputCall, 1);
vm.mockCall(address(_inputBox), addInput, abi.encode(bytes32(0)));
vm.expectCall(address(_inputBox), addInput, 1);

vm.prank(_alice);
_portal.depositBatchERC1155Token(
Expand Down Expand Up @@ -116,16 +113,16 @@ contract ERC1155BatchPortalTest is ERC165Test {
vm.mockCall(address(_token), safeBatchTransferFrom, abi.encode());
vm.mockCallRevert(address(_token), safeBatchTransferFrom, errorData);

bytes memory payload = _encodePayload(
bytes memory payload = encodePayload(
tokenIds,
values,
baseLayerData,
execLayerData
);

bytes memory addInputCall = _encodeAddInput(payload);
bytes memory addInput = _encodeAddInput(payload);

vm.mockCall(address(_inputBox), addInputCall, abi.encode(bytes32(0)));
vm.mockCall(address(_inputBox), addInput, abi.encode(bytes32(0)));

vm.expectRevert(errorData);

Expand Down Expand Up @@ -156,27 +153,35 @@ contract ERC1155BatchPortalTest is ERC165Test {
}

_token = new NormalToken(_alice, tokenIds, supplies);
_app = address(new TokenHolder());

vm.startPrank(_alice);

// Allow the portal to withdraw tokens from Alice
_token.setApprovalForAll(address(_portal), true);

vm.mockCall(
address(_inputBox),
abi.encodeWithSelector(IInputBox.addInput.selector),
abi.encode(bytes32(0))
bytes memory payload = this.encodePayload(
tokenIds,
values,
baseLayerData,
execLayerData
);

bytes memory addInput = _encodeAddInput(payload);

vm.mockCall(address(_inputBox), addInput, abi.encode(bytes32(0)));

// balances before
for (uint256 i; i < numOfTokenIds; ++i) {
assertEq(_token.balanceOf(_alice, tokenIds[i]), supplies[i]);
assertEq(_token.balanceOf(_app, tokenIds[i]), 0);
assertEq(_token.balanceOf(address(_portal), tokenIds[i]), 0);
uint256 tokenId = tokenIds[i];
uint256 supply = supplies[i];
assertEq(_token.balanceOf(_alice, tokenId), supply);
assertEq(_token.balanceOf(_app, tokenId), 0);
assertEq(_token.balanceOf(address(_portal), tokenId), 0);
}

vm.expectEmit(true, true, true, true);
vm.expectCall(address(_inputBox), addInput, 1);

vm.expectEmit(true, true, true, true, address(_token));
emit IERC1155.TransferBatch(
address(_portal),
_alice,
Expand All @@ -197,21 +202,21 @@ contract ERC1155BatchPortalTest is ERC165Test {

// balances after
for (uint256 i; i < numOfTokenIds; ++i) {
assertEq(
_token.balanceOf(_alice, tokenIds[i]),
supplies[i] - values[i]
);
assertEq(_token.balanceOf(_app, tokenIds[i]), values[i]);
assertEq(_token.balanceOf(address(_portal), tokenIds[i]), 0);
uint256 tokenId = tokenIds[i];
uint256 value = values[i];
uint256 supply = supplies[i];
assertEq(_token.balanceOf(_alice, tokenId), supply - value);
assertEq(_token.balanceOf(_app, tokenId), value);
assertEq(_token.balanceOf(address(_portal), tokenId), 0);
}
}

function _encodePayload(
function encodePayload(
uint256[] calldata tokenIds,
uint256[] calldata values,
bytes calldata baseLayerData,
bytes calldata execLayerData
) internal view returns (bytes memory) {
) public view returns (bytes memory) {
return
InputEncoding.encodeBatchERC1155Deposit(
_token,
Expand Down

0 comments on commit 0aac953

Please sign in to comment.