Skip to content

Commit

Permalink
Merge pull request #216 from rainlanguage/112-porting-legacy-tests-fr…
Browse files Browse the repository at this point in the history
…om-flowerc1155flow--test-case-should-support-transferpreflight-hook

112 porting legacy tests from flowerc1155flow  test case should support transferpreflight hook
  • Loading branch information
thedavidmeister authored Oct 3, 2024
2 parents 0e0a43a + 652d7b3 commit 254650c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 50 deletions.
7 changes: 4 additions & 3 deletions test/abstract/FlowERC1155Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract contract FlowERC1155Test is FlowTest {
internal
returns (IFlowERC1155V5 flowErc1155, EvaluableV2 memory evaluable)
{
(flowErc1155, evaluable) = deployIFlowERC1155V5(address(0), uri);
(flowErc1155, evaluable) = deployIFlowERC1155V5(address(uint160(uint256(keccak256("expression")))), uri);
}

function deployIFlowERC1155V5(address expression, string memory uri)
Expand All @@ -33,8 +33,9 @@ abstract contract FlowERC1155Test is FlowTest {
address[] memory expressions = new address[](1);
expressions[0] = expression;
uint256[] memory constants = new uint256[](0);
(IFlowERC1155V5 flowErc1155, EvaluableV2[] memory evaluables) =
deployIFlowERC1155V5(expressions, address(1), constants.matrixFrom(), uri);
(IFlowERC1155V5 flowErc1155, EvaluableV2[] memory evaluables) = deployIFlowERC1155V5(
expressions, address(uint160(uint256(keccak256("configExpression")))), constants.matrixFrom(), uri
);
return (flowErc1155, evaluables[0]);
}

Expand Down
77 changes: 76 additions & 1 deletion test/concrete/flowErc1155/Erc1155FlowTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,83 @@ contract Erc1155FlowTest is FlowERC1155Test {
using LibEvaluable for EvaluableV2;
using SignContextLib for Vm;
using LibUint256Matrix for uint256[];
using LibUint256Array for uint256[];
using Address for address;

/**
* @notice Tests the support for the transferPreflight hook.
*/
/// forge-config: default.fuzz.runs = 100
function testFlowERC1155SupportsTransferPreflightHook(address alice, uint128 amount, uint256 id) external {
vm.assume(alice != address(0));
vm.assume(amount != 0);

(IFlowERC1155V5 flow, EvaluableV2 memory evaluable) = deployIFlowERC1155V5("https://www.rainprotocol.xyz/nft/");
assumeEtchable(alice, address(flow));

// Mint tokens to Alice
{
(uint256[] memory stack,) = mintFlowStack(alice, amount, id, transferEmpty());
interpreterEval2MockCall(stack, new uint256[](0));
}

uint256[] memory ids = new uint256[](1);
ids[0] = id;

uint256[] memory amounts = new uint256[](1);
amounts[0] = amount;

uint256[][] memory context = LibContextWrapper.buildAndSetContext(
LibUint256Matrix.matrixFrom(
LibUint256Array.arrayFrom(
uint256(uint160(alice)), uint256(uint160(alice)), uint256(uint160(address(flow)))
),
ids,
amounts
),
new SignedContextV1[](0),
address(alice),
address(flow)
);

{
interpreterEval2ExpectCall(
address(flow),
LibEncodedDispatch.encode2(
address(uint160(uint256(keccak256("configExpression")))),
FLOW_ERC1155_HANDLE_TRANSFER_ENTRYPOINT,
FLOW_ERC1155_HANDLE_TRANSFER_MAX_OUTPUTS
),
context
);

flow.flow(evaluable, new uint256[](0), new SignedContextV1[](0));

vm.startPrank(alice);
IERC1155(address(flow)).safeTransferFrom(alice, address(flow), id, amount, "");
vm.stopPrank();
}

{
interpreterEval2RevertCall(
address(flow),
LibEncodedDispatch.encode2(
address(uint160(uint256(keccak256("configExpression")))),
FLOW_ERC1155_HANDLE_TRANSFER_ENTRYPOINT,
FLOW_ERC1155_HANDLE_TRANSFER_MAX_OUTPUTS
),
context
);

flow.flow(evaluable, new uint256[](0), new SignedContextV1[](0));

vm.expectRevert("REVERT_EVAL2_CALL");
vm.startPrank(alice);
IERC1155(address(flow)).safeTransferFrom(alice, address(flow), id, amount, "");
vm.stopPrank();
}
}

/// Tests the flow between ERC721 and ERC1155 on the good path.
/// forge-config: default.fuzz.runs = 100
function testFlowERC1155FlowERC721ToERC1155(
Expand Down Expand Up @@ -171,7 +246,7 @@ contract Erc1155FlowTest is FlowERC1155Test {
mints[0] = ERC1155SupplyChange({account: alice, id: erc721InTokenId, amount: amount});

ERC1155SupplyChange[] memory burns = new ERC1155SupplyChange[](1);
burns[0] = ERC1155SupplyChange({account: alice, id: erc721InTokenId, amount: 0 ether});
burns[0] = ERC1155SupplyChange({account: alice, id: erc721InTokenId, amount: amount});

uint256[] memory stack = generateFlowStack(
FlowERC1155IOV1(
Expand Down
41 changes: 11 additions & 30 deletions test/concrete/flowErc20/Erc20FlowTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,14 @@ contract Erc20FlowTest is FlowERC20Test {
* @notice Tests the support for the transferPreflight hook.
*/
/// forge-config: default.fuzz.runs = 100
function testFlowERC20SupportsTransferPreflightHook(
address alice,
uint128 amount,
address expressionA,
address expressionB
) external {
function testFlowERC20SupportsTransferPreflightHook(address alice, uint128 amount) external {
vm.assume(alice != address(0));
vm.assume(sentinel != amount);
vm.assume(expressionA != expressionB);

address[] memory expressions = new address[](1);
expressions[0] = expressionA;

(IFlowERC20V5 flow, EvaluableV2[] memory evaluables) =
deployFlowERC20(expressions, expressionB, new uint256[][](1), "Flow ERC20", "F20");
(IFlowERC20V5 flow, EvaluableV2 memory evaluable) = deployFlowERC20("Flow ERC20", "F20");
assumeEtchable(alice, address(flow));

{
ERC20SupplyChange[] memory mints = new ERC20SupplyChange[](1);
mints[0] = ERC20SupplyChange({account: alice, amount: amount});

ERC20SupplyChange[] memory burns = new ERC20SupplyChange[](1);
burns[0] = ERC20SupplyChange({account: alice, amount: 0 ether});

uint256[] memory stack = generateFlowStack(
FlowERC20IOV1(
mints,
burns,
FlowTransferV1(new ERC20Transfer[](0), new ERC721Transfer[](0), new ERC1155Transfer[](0))
)
);
(uint256[] memory stack,) = mintFlowStack(alice, amount, 0, transferEmpty());
interpreterEval2MockCall(stack, new uint256[](0));
}

Expand All @@ -85,12 +62,14 @@ contract Erc20FlowTest is FlowERC20Test {
interpreterEval2ExpectCall(
address(flow),
LibEncodedDispatch.encode2(
expressionB, FLOW_ERC20_HANDLE_TRANSFER_ENTRYPOINT, FLOW_ERC20_HANDLE_TRANSFER_MAX_OUTPUTS
address(uint160(uint256(keccak256("configExpression")))),
FLOW_ERC20_HANDLE_TRANSFER_ENTRYPOINT,
FLOW_ERC20_HANDLE_TRANSFER_MAX_OUTPUTS
),
context
);

flow.flow(evaluables[0], new uint256[](0), new SignedContextV1[](0));
flow.flow(evaluable, new uint256[](0), new SignedContextV1[](0));

vm.startPrank(alice);
IERC20(address(flow)).transfer(address(flow), amount);
Expand All @@ -101,12 +80,14 @@ contract Erc20FlowTest is FlowERC20Test {
interpreterEval2RevertCall(
address(flow),
LibEncodedDispatch.encode2(
expressionB, FLOW_ERC20_HANDLE_TRANSFER_ENTRYPOINT, FLOW_ERC20_HANDLE_TRANSFER_MAX_OUTPUTS
address(uint160(uint256(keccak256("configExpression")))),
FLOW_ERC20_HANDLE_TRANSFER_ENTRYPOINT,
FLOW_ERC20_HANDLE_TRANSFER_MAX_OUTPUTS
),
context
);

flow.flow(evaluables[0], new uint256[](0), new SignedContextV1[](0));
flow.flow(evaluable, new uint256[](0), new SignedContextV1[](0));

vm.startPrank(alice);
vm.expectRevert("REVERT_EVAL2_CALL");
Expand Down
24 changes: 8 additions & 16 deletions test/concrete/flowErc721/Erc721FlowTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,15 @@ contract Erc721FlowTest is FlowERC721Test {
/// forge-config: default.fuzz.runs = 100
function testFlowERC721SupportsTransferPreflightHook(address alice, uint256 tokenIdA, uint256 tokenIdB) external {
vm.assume(alice != address(0));
vm.assume(sentinel != tokenIdA);
vm.assume(sentinel != tokenIdB);
vm.assume(tokenIdA != tokenIdB);
vm.assume(!alice.isContract());

(IFlowERC721V5 flow, EvaluableV2 memory evaluable) = deployFlowERC721({name: "", symbol: "", baseURI: ""});
assumeEtchable(alice, address(flow));

{
ERC721SupplyChange[] memory mints = new ERC721SupplyChange[](2);
mints[0] = ERC721SupplyChange({account: alice, id: tokenIdA});
mints[1] = ERC721SupplyChange({account: alice, id: tokenIdB});

uint256[] memory stack = generateFlowStack(
FlowERC721IOV1(
mints,
new ERC721SupplyChange[](0),
FlowTransferV1(new ERC20Transfer[](0), new ERC721Transfer[](0), new ERC1155Transfer[](0))
)
);
(uint256[] memory stack,) = mintFlowStack(alice, 0, tokenIdA, transferEmpty());
interpreterEval2MockCall(stack, new uint256[](0));
IFlowERC721V5(flow).flow(evaluable, new uint256[](0), new SignedContextV1[](0));
}

{
Expand All @@ -89,13 +77,17 @@ contract Erc721FlowTest is FlowERC721Test {
contextTransferA
);

IFlowERC721V5(flow).flow(evaluable, new uint256[](0), new SignedContextV1[](0));

vm.startPrank(alice);
IERC721(address(flow)).transferFrom({from: alice, to: address(flow), tokenId: tokenIdA});
vm.stopPrank();
}

{
(uint256[] memory stack,) = mintFlowStack(alice, 0, tokenIdB, transferEmpty());
interpreterEval2MockCall(stack, new uint256[](0));
IFlowERC721V5(flow).flow(evaluable, new uint256[](0), new SignedContextV1[](0));
}

{
uint256[][] memory contextTransferB = LibContextWrapper.buildAndSetContext(
LibUint256Array.arrayFrom(uint256(uint160(address(alice))), uint256(uint160(address(flow))), tokenIdB)
Expand Down

0 comments on commit 254650c

Please sign in to comment.