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