|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | +pragma solidity >=0.8.22; |
| 3 | + |
| 4 | +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 5 | +import { ud21x18, UD21x18 } from "@prb/math/src/UD21x18.sol"; |
| 6 | +import { ud60x18 } from "@prb/math/src/UD60x18.sol"; |
| 7 | +import { Broker, ISablierFlow } from "@sablier/flow/src/interfaces/ISablierFlow.sol"; |
| 8 | + |
| 9 | +/// @notice The `Batch` contract, inherited in SablierFlow, allows multiple function calls to be batched together. This |
| 10 | +/// enables any possible combination of functions to be executed within a single transaction. |
| 11 | +/// @dev For some functions to work, `msg.sender` must have approved this contract to spend USDC. |
| 12 | +contract FlowBatchable { |
| 13 | + // Sepolia addresses |
| 14 | + IERC20 public constant USDC = IERC20(0xf08A50178dfcDe18524640EA6618a1f965821715); |
| 15 | + ISablierFlow public constant FLOW = ISablierFlow(0x5Ae8c13f6Ae094887322012425b34b0919097d8A); |
| 16 | + |
| 17 | + /// @dev A function to adjust the rate per second and deposit into a stream in a single transaction. |
| 18 | + function adjustRatePerSecondAndDeposit(uint256 streamId) external { |
| 19 | + UD21x18 newRatePerSecond = ud21x18(0.0001e18); |
| 20 | + uint128 depositAmount = 1000e6; |
| 21 | + |
| 22 | + // Transfer to this contract the amount to deposit in the stream. |
| 23 | + USDC.transferFrom(msg.sender, address(this), depositAmount); |
| 24 | + |
| 25 | + // Approve the Sablier contract to spend USDC. |
| 26 | + USDC.approve(address(FLOW), depositAmount); |
| 27 | + |
| 28 | + // Fetch the stream recipient. |
| 29 | + address recipient = FLOW.getRecipient(streamId); |
| 30 | + |
| 31 | + // The call data declared as bytes. |
| 32 | + bytes[] memory calls = new bytes[](2); |
| 33 | + calls[0] = abi.encodeCall(FLOW.adjustRatePerSecond, (streamId, newRatePerSecond)); |
| 34 | + calls[1] = abi.encodeCall(FLOW.deposit, (streamId, depositAmount, msg.sender, recipient)); |
| 35 | + |
| 36 | + FLOW.batch(calls); |
| 37 | + } |
| 38 | + |
| 39 | + /// @dev A function to create a stream and deposit via a broker in a single transaction. |
| 40 | + function createAndDepositViaBroker() external returns (uint256 streamId) { |
| 41 | + address sender = msg.sender; |
| 42 | + address recipient = address(0xCAFE); |
| 43 | + UD21x18 ratePerSecond = ud21x18(0.0001e18); |
| 44 | + uint128 depositAmount = 1000e6; |
| 45 | + |
| 46 | + // The broker struct. |
| 47 | + Broker memory broker = Broker({ |
| 48 | + account: address(0xDEAD), |
| 49 | + fee: ud60x18(0.0001e18) // the fee percentage |
| 50 | + }); |
| 51 | + |
| 52 | + // Transfer to this contract the amount to deposit in the stream. |
| 53 | + USDC.transferFrom(msg.sender, address(this), depositAmount); |
| 54 | + |
| 55 | + // Approve the Sablier contract to spend USDC. |
| 56 | + USDC.approve(address(FLOW), depositAmount); |
| 57 | + |
| 58 | + streamId = FLOW.nextStreamId(); |
| 59 | + |
| 60 | + // The call data declared as bytes |
| 61 | + bytes[] memory calls = new bytes[](2); |
| 62 | + calls[0] = abi.encodeCall(FLOW.create, (sender, recipient, ratePerSecond, USDC, true)); |
| 63 | + calls[1] = abi.encodeCall(FLOW.depositViaBroker, (streamId, depositAmount, sender, recipient, broker)); |
| 64 | + |
| 65 | + // Execute multiple calls in a single transaction using the prepared call data. |
| 66 | + FLOW.batch(calls); |
| 67 | + } |
| 68 | + |
| 69 | + /// @dev A function to create multiple streams in a single transaction. |
| 70 | + function createMultiple() external returns (uint256[] memory streamIds) { |
| 71 | + address sender = msg.sender; |
| 72 | + address firstRecipient = address(0xCAFE); |
| 73 | + address secondRecipient = address(0xBEEF); |
| 74 | + UD21x18 firstRatePerSecond = ud21x18(0.0001e18); |
| 75 | + UD21x18 secondRatePerSecond = ud21x18(0.0002e18); |
| 76 | + |
| 77 | + // The call data declared as bytes |
| 78 | + bytes[] memory calls = new bytes[](2); |
| 79 | + calls[0] = abi.encodeCall(FLOW.create, (sender, firstRecipient, firstRatePerSecond, USDC, true)); |
| 80 | + calls[1] = abi.encodeCall(FLOW.create, (sender, secondRecipient, secondRatePerSecond, USDC, true)); |
| 81 | + |
| 82 | + // Prepare the `streamIds` array to return them |
| 83 | + uint256 nextStreamId = FLOW.nextStreamId(); |
| 84 | + streamIds = new uint256[](2); |
| 85 | + streamIds[0] = nextStreamId; |
| 86 | + streamIds[1] = nextStreamId + 1; |
| 87 | + |
| 88 | + // Execute multiple calls in a single transaction using the prepared call data. |
| 89 | + FLOW.batch(calls); |
| 90 | + } |
| 91 | + |
| 92 | + /// @dev A function to create multiple streams and deposit via a broker into all the stream in a single transaction. |
| 93 | + function createMultipleAndDepositViaBroker() external returns (uint256[] memory streamIds) { |
| 94 | + address sender = msg.sender; |
| 95 | + address firstRecipient = address(0xCAFE); |
| 96 | + address secondRecipient = address(0xBEEF); |
| 97 | + UD21x18 ratePerSecond = ud21x18(0.0001e18); |
| 98 | + uint128 depositAmount = 1000e6; |
| 99 | + |
| 100 | + // Transfer the deposit amount of USDC tokens to this contract for both streams |
| 101 | + USDC.transferFrom(msg.sender, address(this), 2 * depositAmount); |
| 102 | + |
| 103 | + // Approve the Sablier contract to spend USDC. |
| 104 | + USDC.approve(address(FLOW), 2 * depositAmount); |
| 105 | + |
| 106 | + // The broker struct |
| 107 | + Broker memory broker = Broker({ |
| 108 | + account: address(0xDEAD), |
| 109 | + fee: ud60x18(0.0001e18) // the fee percentage |
| 110 | + }); |
| 111 | + |
| 112 | + uint256 nextStreamId = FLOW.nextStreamId(); |
| 113 | + streamIds = new uint256[](2); |
| 114 | + streamIds[0] = nextStreamId; |
| 115 | + streamIds[1] = nextStreamId + 1; |
| 116 | + |
| 117 | + // We need to have 4 different function calls, 2 for creating streams and 2 for depositing via broker |
| 118 | + bytes[] memory calls = new bytes[](4); |
| 119 | + calls[0] = abi.encodeCall(FLOW.create, (sender, firstRecipient, ratePerSecond, USDC, true)); |
| 120 | + calls[1] = abi.encodeCall(FLOW.create, (sender, secondRecipient, ratePerSecond, USDC, true)); |
| 121 | + calls[2] = abi.encodeCall(FLOW.depositViaBroker, (streamIds[0], depositAmount, sender, firstRecipient, broker)); |
| 122 | + calls[3] = abi.encodeCall(FLOW.depositViaBroker, (streamIds[1], depositAmount, sender, secondRecipient, broker)); |
| 123 | + |
| 124 | + // Execute multiple calls in a single transaction using the prepared call data. |
| 125 | + FLOW.batch(calls); |
| 126 | + } |
| 127 | + |
| 128 | + /// @dev A function to pause a stream and withdraw the maximum available funds. |
| 129 | + function pauseAndWithdrawMax(uint256 streamId) external { |
| 130 | + // The call data declared as bytes. |
| 131 | + bytes[] memory calls = new bytes[](2); |
| 132 | + calls[0] = abi.encodeCall(FLOW.pause, (streamId)); |
| 133 | + calls[1] = abi.encodeCall(FLOW.withdrawMax, (streamId, address(0xCAFE))); |
| 134 | + |
| 135 | + // Execute multiple calls in a single transaction using the prepared call data. |
| 136 | + FLOW.batch(calls); |
| 137 | + } |
| 138 | + |
| 139 | + /// @dev A function to void a stream and withdraw what is left. |
| 140 | + function voidAndWithdrawMax(uint256 streamId) external { |
| 141 | + // The call data declared as bytes |
| 142 | + bytes[] memory calls = new bytes[](2); |
| 143 | + calls[0] = abi.encodeCall(FLOW.void, (streamId)); |
| 144 | + calls[1] = abi.encodeCall(FLOW.withdrawMax, (streamId, address(0xCAFE))); |
| 145 | + |
| 146 | + // Execute multiple calls in a single transaction using the prepared call data. |
| 147 | + FLOW.batch(calls); |
| 148 | + } |
| 149 | + |
| 150 | + /// @dev A function to withdraw maximum available funds from multiple streams in a single transaction. |
| 151 | + function withdrawMaxMultiple(uint256[] calldata streamIds) external { |
| 152 | + uint256 count = streamIds.length; |
| 153 | + |
| 154 | + // Iterate over the streamIds and prepare the call data for each stream. |
| 155 | + bytes[] memory calls = new bytes[](count); |
| 156 | + for (uint256 i = 0; i < count; ++i) { |
| 157 | + address recipient = FLOW.getRecipient(streamIds[i]); |
| 158 | + calls[i] = abi.encodeCall(FLOW.withdrawMax, (streamIds[i], recipient)); |
| 159 | + } |
| 160 | + |
| 161 | + // Execute multiple calls in a single transaction using the prepared call data. |
| 162 | + FLOW.batch(calls); |
| 163 | + } |
| 164 | +} |
0 commit comments