Skip to content

Commit

Permalink
Merge pull request #240 from rainlanguage/2024-09-13-FlowERC1155Previ…
Browse files Browse the repository at this point in the history
…ewDefinedFlowIOForERC20SingleElementArrays

2024 09 13 flow erc1155 preview defined flow io for erc20 single element arrays
  • Loading branch information
thedavidmeister authored Sep 26, 2024
2 parents e05d866 + abe474d commit 1d1f09f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 66 deletions.
23 changes: 8 additions & 15 deletions test/concrete/flowBasic/FlowPreviewTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -146,27 +146,20 @@ contract FlowPreviewTest is FlowBasicTest {
uint256 erc20AmountIn,
uint256 erc20AmountOut
) external {
vm.assume(sentinel != erc20AmountIn);
vm.assume(sentinel != erc20AmountOut);

vm.label(alice, "alice");

(IFlowV5 flow,) = deployFlow();
assumeEtchable(alice, address(flow));

ERC20Transfer[] memory erc20Transfers = new ERC20Transfer[](2);
erc20Transfers[0] =
ERC20Transfer({token: address(iTokenA), from: address(flow), to: alice, amount: erc20AmountOut});
erc20Transfers[1] =
ERC20Transfer({token: address(iTokenA), from: alice, to: address(flow), amount: erc20AmountIn});

FlowTransferV1 memory flowTransfer =
FlowTransferV1(erc20Transfers, new ERC721Transfer[](0), new ERC1155Transfer[](0));
uint256[] memory stack = generateFlowStack(flowTransfer);

assertEq(
keccak256(abi.encode(flowTransfer)), keccak256(abi.encode(flow.stackToFlow(stack))), "wrong compare Structs"
(uint256[] memory stack, bytes32 transferHash) = mintAndBurnFlowStack(
alice,
20 ether,
10 ether,
5,
createTransfersERC20toERC20(alice, address(flow), erc20AmountIn, erc20AmountOut)
);

assertEq(transferHash, keccak256(abi.encode(flow.stackToFlow(stack))), "wrong compare Structs");
}

/**
Expand Down
25 changes: 25 additions & 0 deletions test/concrete/flowErc1155/FlowPreviewTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,31 @@ contract FlowPreviewTest is FlowERC1155Test {
assertEq(transferHash, keccak256(abi.encode(flow.stackToFlow(stack))), "wrong compare Structs");
}

/**
* @dev Tests the preview of defined Flow IO for ERC20
* using single-element arrays.
*/
function testFlowERC1155PreviewDefinedFlowIOForERC20SingleElementArrays(
address alice,
uint256 erc20AmountIn,
uint256 erc20AmountOut
) external {
vm.label(alice, "alice");

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

(uint256[] memory stack, bytes32 transferHash) = mintAndBurnFlowStack(
alice,
20 ether,
10 ether,
5,
createTransfersERC20toERC20(alice, address(flow), erc20AmountIn, erc20AmountOut)
);

assertEq(transferHash, keccak256(abi.encode(flow.stackToFlow(stack))), "wrong compare Structs");
}

/// Should preview empty flow io
function testFlowERC1155PreviewEmptyFlowIO(string memory uri, address alice, uint256 amount) public {
(IFlowERC1155V5 flow,) = deployIFlowERC1155V5({uri: uri});
Expand Down
33 changes: 8 additions & 25 deletions test/concrete/flowErc20/FlowPreviewTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,41 +142,24 @@ contract FlowPreviewTest is FlowERC20Test {
*/
/// forge-config: default.fuzz.runs = 100
function testFlowERC20PreviewDefinedFlowIOForERC20SingleElementArrays(
string memory name,
string memory symbol,
address alice,
uint256 erc20AmountIn,
uint256 erc20AmountOut
) external {
vm.assume(sentinel != erc20AmountIn);
vm.assume(sentinel != erc20AmountOut);

vm.label(alice, "alice");

(IFlowERC20V5 flow,) = deployFlowERC20(name, symbol);
(IFlowERC20V5 flow,) = deployFlowERC20("Flow ERC20", "F20");
assumeEtchable(alice, address(flow));

ERC20Transfer[] memory erc20Transfers = new ERC20Transfer[](2);
erc20Transfers[0] =
ERC20Transfer({token: address(iTokenA), from: address(flow), to: alice, amount: erc20AmountOut});
erc20Transfers[1] =
ERC20Transfer({token: address(iTokenA), from: alice, to: address(flow), amount: erc20AmountIn});

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

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

FlowERC20IOV1 memory flowERC20IO = FlowERC20IOV1(
mints, burns, FlowTransferV1(erc20Transfers, new ERC721Transfer[](0), new ERC1155Transfer[](0))
(uint256[] memory stack, bytes32 transferHash) = mintAndBurnFlowStack(
alice,
20 ether,
10 ether,
5,
createTransfersERC20toERC20(alice, address(flow), erc20AmountIn, erc20AmountOut)
);

uint256[] memory stack = generateFlowStack(flowERC20IO);

assertEq(
keccak256(abi.encode(flowERC20IO)), keccak256(abi.encode(flow.stackToFlow(stack))), "wrong compare Structs"
);
assertEq(transferHash, keccak256(abi.encode(flow.stackToFlow(stack))), "wrong compare Structs");
}

/**
Expand Down
35 changes: 9 additions & 26 deletions test/concrete/flowErc721/FlowPreviewTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,42 +142,25 @@ contract FlowPreviewTest is FlowERC721Test {
*/
/// forge-config: default.fuzz.runs = 100
function testFlowERC721PreviewDefinedFlowIOForERC20SingleElementArrays(
string memory symbol,
string memory baseURI,
address alice,
uint256 erc20AmountIn,
uint256 erc20AmountOut
) external {
vm.assume(sentinel != erc20AmountIn);
vm.assume(sentinel != erc20AmountOut);

vm.label(alice, "alice");

(IFlowERC721V5 flow,) = deployFlowERC721({name: symbol, symbol: symbol, baseURI: baseURI});
(IFlowERC721V5 flow,) =
deployFlowERC721({name: "FlowErc721", symbol: "FErc721", baseURI: "https://www.rainprotocol.xyz/nft/"});
assumeEtchable(alice, address(flow));

ERC20Transfer[] memory erc20Transfers = new ERC20Transfer[](2);
erc20Transfers[0] =
ERC20Transfer({token: address(iTokenA), from: address(flow), to: alice, amount: erc20AmountOut});
erc20Transfers[1] =
ERC20Transfer({token: address(iTokenA), from: alice, to: address(flow), amount: erc20AmountIn});

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

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

FlowERC721IOV1 memory flowERC721IO = FlowERC721IOV1(
mints, burns, FlowTransferV1(erc20Transfers, new ERC721Transfer[](0), new ERC1155Transfer[](0))
(uint256[] memory stack, bytes32 transferHash) = mintAndBurnFlowStack(
alice,
20 ether,
10 ether,
5,
createTransfersERC20toERC20(alice, address(flow), erc20AmountIn, erc20AmountOut)
);

uint256[] memory stack = generateFlowStack(flowERC721IO);

assertEq(
keccak256(abi.encode(flowERC721IO)), keccak256(abi.encode(flow.stackToFlow(stack))), "wrong compare Structs"
);
assertEq(transferHash, keccak256(abi.encode(flow.stackToFlow(stack))), "wrong compare Structs");
}

/**
Expand Down

0 comments on commit 1d1f09f

Please sign in to comment.