Skip to content

Commit

Permalink
Merge pull request #27 from rainlanguage/2024-07-30-FlowAbstractUtils
Browse files Browse the repository at this point in the history
Added utilities for testing flow
  • Loading branch information
thedavidmeister authored Jul 31, 2024
2 parents 4427e71 + 4e9d5be commit a86384f
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions test/abstract/FlowUtilsAbstractTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: CAL
pragma solidity ^0.8.18;

import {Test, Vm} from "forge-std/Test.sol";
import {ERC20Transfer, ERC721Transfer, ERC1155Transfer, RAIN_FLOW_SENTINEL} from "src/interface/unstable/IFlowV5.sol";
import {Sentinel} from "rain.solmem/lib/LibStackSentinel.sol";

abstract contract FlowUtilsAbstractTest is Test {
function generateTokenTransferStack(
ERC1155Transfer[] memory erc1155Transfers,
ERC721Transfer[] memory erc721Transfers,
ERC20Transfer[] memory erc20Transfers
) internal pure returns (uint256[] memory stack) {
uint256 totalItems =
1 + (erc1155Transfers.length * 5) + 1 + (erc721Transfers.length * 4) + 1 + (erc20Transfers.length * 4);
stack = new uint256[](totalItems);
uint256 index = 0;

uint256 separator = Sentinel.unwrap(RAIN_FLOW_SENTINEL);

stack[index++] = separator;
for (uint256 i = 0; i < erc1155Transfers.length; i++) {
stack[index++] = uint256(uint160(erc1155Transfers[i].token));
stack[index++] = uint256(uint160(erc1155Transfers[i].from));
stack[index++] = uint256(uint160(erc1155Transfers[i].to));
stack[index++] = erc1155Transfers[i].id;
stack[index++] = erc1155Transfers[i].amount;
}

stack[index++] = separator;
for (uint256 i = 0; i < erc721Transfers.length; i++) {
stack[index++] = uint256(uint160(erc721Transfers[i].token));
stack[index++] = uint256(uint160(erc721Transfers[i].from));
stack[index++] = uint256(uint160(erc721Transfers[i].to));
stack[index++] = erc721Transfers[i].id;
}

stack[index++] = separator;
for (uint256 i = 0; i < erc20Transfers.length; i++) {
stack[index++] = uint256(uint160(erc20Transfers[i].token));
stack[index++] = uint256(uint160(erc20Transfers[i].from));
stack[index++] = uint256(uint160(erc20Transfers[i].to));
stack[index++] = erc20Transfers[i].amount;
}

return stack;
}

function findEvent(Vm.Log[] memory logs, bytes32 eventSignature) internal pure returns (Vm.Log memory) {
for (uint256 i = 0; i < logs.length; i++) {
if (logs[i].topics[0] == eventSignature) {
return logs[i];
}
}
revert("Event not found!");
}
}

0 comments on commit a86384f

Please sign in to comment.