@bananapus/core-v6 0.0.53 → 0.0.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,176 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- pragma solidity 0.8.28;
3
-
4
- import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
5
- import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
6
-
7
- import {IJBCashOutHook} from "../interfaces/IJBCashOutHook.sol";
8
- import {IJBFeelessAddresses} from "../interfaces/IJBFeelessAddresses.sol";
9
- import {JBAfterCashOutRecordedContext} from "../structs/JBAfterCashOutRecordedContext.sol";
10
- import {JBCashOutHookSpecification} from "../structs/JBCashOutHookSpecification.sol";
11
- import {JBTokenAmount} from "../structs/JBTokenAmount.sol";
12
- import {JBConstants} from "./JBConstants.sol";
13
- import {JBFees} from "./JBFees.sol";
14
-
15
- /// @notice Cash-out hook specification fulfillment for `JBMultiTerminal`. Extracted to reduce terminal
16
- /// bytecode size, mirroring the `JBHeldFeesLib` pattern.
17
- /// @dev Called via DELEGATECALL — `address(this)` inside library code is the terminal's address, so token
18
- /// approvals and ETH-bearing hook calls operate on the terminal's balance and allowances. Events are
19
- /// emitted from the terminal address. The `caller` field of `HookAfterRecordCashOut` uses raw `msg.sender`
20
- /// (matching `JBHeldFeesLib`'s precedent) — this means meta-transactions through a trusted forwarder will
21
- /// surface the forwarder address in the event slot, but the actual hook execution semantics are unaffected.
22
- library JBCashOutHookSpecsLib {
23
- // A library that adds default safety checks to ERC20 functionality.
24
- using SafeERC20 for IERC20;
25
-
26
- //*********************************************************************//
27
- // ------------------------------ events ----------------------------- //
28
- //*********************************************************************//
29
-
30
- /// @notice A cash out hook was called after a cash out was recorded.
31
- /// @param hook The cash out hook that was called.
32
- /// @param context The context passed to the hook.
33
- /// @param specificationAmount The amount specified for the hook.
34
- /// @param fee The fee taken from the hook's amount.
35
- /// @param caller The address that called the cash out function.
36
- event HookAfterRecordCashOut(
37
- IJBCashOutHook indexed hook,
38
- JBAfterCashOutRecordedContext context,
39
- uint256 specificationAmount,
40
- uint256 fee,
41
- address caller
42
- );
43
-
44
- //*********************************************************************//
45
- // --------------------------- custom errors ------------------------- //
46
- //*********************************************************************//
47
-
48
- /// @notice Thrown when a hook returns without consuming the full forwarded ERC-20 amount.
49
- error JBMultiTerminal_TemporaryAllowanceNotConsumed(address token, address spender, uint256 allowance);
50
-
51
- //*********************************************************************//
52
- // ----------------------- external functions ------------------------ //
53
- //*********************************************************************//
54
-
55
- /// @notice Iterates `specifications`, calling each non-noop hook with the right ETH/ERC-20 setup, and
56
- /// accumulates the fee-eligible amount across non-feeless hooks.
57
- /// @dev For each spec: if the hook is feeless, it gets the full spec amount; otherwise the hook gets
58
- /// `amount - feeAmountFrom(amount)` and the gross spec amount is added to the eligible-for-fees total
59
- /// (the caller takes the fee separately via `_takeFeeFrom`). Cross-token semantics: the hook context's
60
- /// `forwardedAmount` carries the post-fee amount in the same token as `context.reclaimedAmount`.
61
- /// @dev The caller builds `context` once (`JBAfterCashOutRecordedContext`) and passes it in; the
62
- /// library mutates `context.forwardedAmount` and `context.hookMetadata` per spec. Bundling the
63
- /// caller-side state into the existing hook-context struct keeps the call site under solc 0.8.28's
64
- /// non-via-ir 16-slot Yul stack ceiling (the prior 10-arg shape tripped it from
65
- /// `JBMultiTerminal._cashOutTokensOf`).
66
- /// @param feelessAddresses Registry of fee-exempt addresses (consulted per-hook).
67
- /// @param context Cash-out context to forward into each hook. `context.reclaimedAmount` doubles as the
68
- /// token-amount reference for per-spec transfers; `context.forwardedAmount` / `context.hookMetadata`
69
- /// are rewritten per iteration.
70
- /// @param specifications The hook specifications returned by the data hook.
71
- /// @return amountEligibleForFees Total spec amounts (gross) from non-feeless hooks, used by the caller to
72
- /// charge fees in a single pass.
73
- function fulfill(
74
- IJBFeelessAddresses feelessAddresses,
75
- JBAfterCashOutRecordedContext memory context,
76
- JBCashOutHookSpecification[] memory specifications
77
- )
78
- external
79
- returns (uint256 amountEligibleForFees)
80
- {
81
- // Loop body is extracted into a helper to keep `fulfill`'s stack frame shallow enough for solc 0.8.28
82
- // to compile *without* `via_ir`.
83
- for (uint256 i; i < specifications.length;) {
84
- amountEligibleForFees += _fulfillOne(feelessAddresses, context, specifications[i]);
85
- unchecked {
86
- ++i;
87
- }
88
- }
89
- }
90
-
91
- /// @notice Fulfill a single hook specification: charge the protocol fee (if non-feeless), set up the hook
92
- /// call context, transfer the post-fee amount to the hook, and emit the after-cash-out event.
93
- /// @dev Returns the gross spec amount when the hook is non-feeless (so the caller can accumulate it into
94
- /// `amountEligibleForFees`), or `0` when the hook is feeless / noop. Mutates `context.forwardedAmount`
95
- /// and `context.hookMetadata` so the caller's next iteration can overwrite them.
96
- /// @param feelessAddresses Registry of fee-exempt addresses (consulted to skip the per-hook fee).
97
- /// @param context Cash-out context forwarded into the hook. `context.reclaimedAmount` is used as the
98
- /// token-amount reference for the per-spec transfer; `context.forwardedAmount` and
99
- /// `context.hookMetadata` are overwritten by this call.
100
- /// @param specification The hook specification to fulfill (hook address, amount, metadata, noop flag).
101
- /// @return grossSpecAmount The gross spec amount when the hook is non-feeless (for caller accumulation
102
- /// into `amountEligibleForFees`); `0` for feeless or noop specifications.
103
- function _fulfillOne(
104
- IJBFeelessAddresses feelessAddresses,
105
- JBAfterCashOutRecordedContext memory context,
106
- JBCashOutHookSpecification memory specification
107
- )
108
- private
109
- returns (uint256 grossSpecAmount)
110
- {
111
- // A noop specification is informational only and doesn't trigger the hook.
112
- if (specification.noop) return 0;
113
-
114
- // Get the fee for the specified amount.
115
- uint256 specificationAmountFee = feelessAddresses.isFeelessFor({
116
- addr: address(specification.hook), projectId: context.projectId
117
- })
118
- ? 0
119
- : JBFees.standardFeeAmountFrom({amountBeforeFee: specification.amount});
120
-
121
- // Surface the gross spec amount to the caller so it can be accumulated into `amountEligibleForFees`.
122
- if (specificationAmountFee != 0) {
123
- grossSpecAmount = specification.amount;
124
- specification.amount -= specificationAmountFee;
125
- }
126
-
127
- // Pass the correct token `forwardedAmount` to the hook.
128
- context.forwardedAmount = JBTokenAmount({
129
- value: specification.amount,
130
- token: context.reclaimedAmount.token,
131
- decimals: context.reclaimedAmount.decimals,
132
- currency: context.reclaimedAmount.currency
133
- });
134
-
135
- // Pass the correct metadata from the data hook's specification.
136
- context.hookMetadata = specification.metadata;
137
-
138
- // Trigger any inherited pre-transfer logic. Keep a reference to the amount that'll be paid as a `msg.value`.
139
- uint256 payValue = _beforeTransferTo({
140
- to: address(specification.hook), token: context.reclaimedAmount.token, amount: specification.amount
141
- });
142
-
143
- // Fulfill the specification.
144
- specification.hook.afterCashOutRecordedWith{value: payValue}(context);
145
-
146
- // Revoke the temporary pull allowance now that the hook call has finished.
147
- _afterTransferTo({to: address(specification.hook), token: context.reclaimedAmount.token});
148
-
149
- emit HookAfterRecordCashOut({
150
- hook: specification.hook,
151
- context: context,
152
- specificationAmount: specification.amount,
153
- fee: specificationAmountFee,
154
- caller: msg.sender
155
- });
156
- }
157
-
158
- //*********************************************************************//
159
- // ----------------------- private helpers --------------------------- //
160
- //*********************************************************************//
161
-
162
- /// @notice Native-token transfers use `msg.value` (return the amount). ERC20 transfers grant a temporary
163
- /// pull allowance and return 0 (the recipient is expected to consume it within the same call).
164
- function _beforeTransferTo(address to, address token, uint256 amount) private returns (uint256) {
165
- if (token == JBConstants.NATIVE_TOKEN) return amount;
166
- IERC20(token).forceApprove({spender: to, value: amount});
167
- return 0;
168
- }
169
-
170
- /// @notice Asserts the recipient consumed the temporary ERC20 allowance. No-op for native token.
171
- function _afterTransferTo(address to, address token) private view {
172
- if (token == JBConstants.NATIVE_TOKEN) return;
173
- uint256 allowance = IERC20(token).allowance({owner: address(this), spender: to});
174
- if (allowance != 0) revert JBMultiTerminal_TemporaryAllowanceNotConsumed(token, to, allowance);
175
- }
176
- }
@@ -1,288 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- pragma solidity 0.8.28;
3
-
4
- import {IJBDirectory} from "../interfaces/IJBDirectory.sol";
5
- import {IJBTerminal} from "../interfaces/IJBTerminal.sol";
6
- import {IJBTerminalStore} from "../interfaces/IJBTerminalStore.sol";
7
- import {JBFee} from "../structs/JBFee.sol";
8
- import {JBConstants} from "./JBConstants.sol";
9
- import {JBFees} from "./JBFees.sol";
10
-
11
- /// @notice Local callback into the terminal's `executeProcessFee(...)`. Kept here because the function is an
12
- /// implementation detail of the library/terminal pair, not a shared public interface.
13
- interface IJBHeldFeesExecutor {
14
- /// @notice Sends a fee amount from a project to the fee-receiving terminal under an external CALL boundary,
15
- /// so the library can wrap the call in `try/catch` (revert in the fee route is forgiven, not propagated).
16
- /// @param projectId The project paying the fee.
17
- /// @param token The token the fee is denominated in.
18
- /// @param amount The fee amount.
19
- /// @param beneficiary The address that receives any platform tokens minted by the fee payment.
20
- /// @param feeTerminal The terminal that'll receive the fee.
21
- function executeProcessFee(
22
- uint256 projectId,
23
- address token,
24
- uint256 amount,
25
- address beneficiary,
26
- IJBTerminal feeTerminal
27
- )
28
- external;
29
- }
30
-
31
- /// @notice Held-fee bookkeeping for `JBMultiTerminal`. Extracted to reduce terminal bytecode size.
32
- /// @dev Called via DELEGATECALL — storage refs (`heldFeesOf`, `nextHeldFeeIndexOf`) point at the terminal's
33
- /// storage. `address(this)` inside library code is the terminal's address. Events are therefore emitted from
34
- /// the terminal.
35
- library JBHeldFeesLib {
36
- //*********************************************************************//
37
- // ------------------------------ events ----------------------------- //
38
- //*********************************************************************//
39
-
40
- /// @notice Emitted when a fee is sent to the fee-receiving terminal.
41
- /// @param projectId The project the fee was for.
42
- /// @param token The token the fee was paid in.
43
- /// @param amount The fee amount.
44
- /// @param wasHeld Whether the fee was previously held (true) or processed inline (false).
45
- /// @param beneficiary The address that received any platform tokens minted by the fee payment.
46
- /// @param caller The address that triggered the fee processing.
47
- event ProcessFee(
48
- uint256 indexed projectId,
49
- address indexed token,
50
- uint256 amount,
51
- bool wasHeld,
52
- address beneficiary,
53
- address caller
54
- );
55
-
56
- /// @notice Emitted when a fee payment reverts and the amount is returned to the project's balance.
57
- /// @param projectId The project the fee was for.
58
- /// @param token The token the fee was paid in.
59
- /// @param feeProjectId The ID of the fee-receiving project.
60
- /// @param amount The fee amount returned.
61
- /// @param reason The revert reason from the fee route.
62
- /// @param caller The address that triggered the fee processing.
63
- event FeeReverted(
64
- uint256 indexed projectId,
65
- address indexed token,
66
- uint256 indexed feeProjectId,
67
- uint256 amount,
68
- bytes reason,
69
- address caller
70
- );
71
-
72
- /// @notice Emitted when held fees are returned to the project's balance (e.g. on an `addToBalanceOf`).
73
- /// @param projectId The project whose held fees were returned.
74
- /// @param token The token the held fees were denominated in.
75
- /// @param amount The amount used as the basis for the return calculation.
76
- /// @param returnedFees The amount of fees actually returned.
77
- /// @param leftoverAmount Any leftover from the basis amount that did not match a held fee.
78
- /// @param caller The address that triggered the return.
79
- event ReturnHeldFees(
80
- uint256 indexed projectId,
81
- address indexed token,
82
- uint256 amount,
83
- uint256 returnedFees,
84
- uint256 leftoverAmount,
85
- address caller
86
- );
87
-
88
- //*********************************************************************//
89
- // ----------------------- internal functions ------------------------ //
90
- //*********************************************************************//
91
-
92
- /// @notice Processes up to `count` unlocked held fees for `(projectId, token)`, forwarding each to the fee
93
- /// terminal and reclaiming the storage slot once the queue is drained.
94
- /// @dev Re-reads `nextHeldFeeIndexOf[projectId][token]` from storage each iteration to be reentrancy-safe
95
- /// against any nested call that may have advanced the index. The entry is deleted and the index advanced
96
- /// BEFORE the external call so a reverting fee route cannot be replayed.
97
- /// @param heldFeesOf Storage ref to the terminal's per-(project,token) held-fee queue.
98
- /// @param nextHeldFeeIndexOf Storage ref to the next-index cursor for each (project,token) queue.
99
- /// @param directory The terminal's directory, used to locate the fee-receiving terminal.
100
- /// @param store The terminal's store, used to credit the fee back to the project on a failed route.
101
- /// @param projectId The project to process held fees for.
102
- /// @param token The token the held fees are denominated in.
103
- /// @param count The maximum number of held fees to process.
104
- function processHeldFees(
105
- mapping(uint256 => mapping(address => JBFee[])) storage heldFeesOf,
106
- mapping(uint256 => mapping(address => uint256)) storage nextHeldFeeIndexOf,
107
- IJBDirectory directory,
108
- IJBTerminalStore store,
109
- uint256 projectId,
110
- address token,
111
- uint256 count
112
- )
113
- external
114
- {
115
- // Resolve the fee-receiving terminal once outside the loop.
116
- IJBTerminal feeTerminal =
117
- directory.primaryTerminalOf({projectId: JBConstants.FEE_BENEFICIARY_PROJECT_ID, token: token});
118
-
119
- for (uint256 i; i < count;) {
120
- uint256 currentIndex = nextHeldFeeIndexOf[projectId][token];
121
-
122
- // Queue exhausted — break early so we can run the array cleanup below.
123
- if (currentIndex >= heldFeesOf[projectId][token].length) break;
124
-
125
- JBFee memory heldFee = heldFeesOf[projectId][token][currentIndex];
126
-
127
- // Fees unlock sequentially; if the head isn't ready, nothing later is either.
128
- // forge-lint: disable-next-line(block-timestamp)
129
- if (heldFee.unlockTimestamp > block.timestamp) break;
130
-
131
- // Delete + advance index BEFORE the external call (reentrancy safety: a reentrant
132
- // `processHeldFeesOf` cannot re-process the same entry).
133
- delete heldFeesOf[projectId][token][currentIndex];
134
- nextHeldFeeIndexOf[projectId][token] = currentIndex + 1;
135
-
136
- processFee({
137
- store: store,
138
- projectId: projectId,
139
- token: token,
140
- amount: JBFees.standardFeeAmountFrom({amountBeforeFee: heldFee.amount}),
141
- beneficiary: heldFee.beneficiary,
142
- feeTerminal: feeTerminal,
143
- wasHeld: true
144
- });
145
-
146
- unchecked {
147
- ++i;
148
- }
149
- }
150
-
151
- // Reclaim the array storage slot once the queue has been fully drained.
152
- if (
153
- nextHeldFeeIndexOf[projectId][token] >= heldFeesOf[projectId][token].length
154
- && heldFeesOf[projectId][token].length > 0
155
- ) {
156
- delete heldFeesOf[projectId][token];
157
- delete nextHeldFeeIndexOf[projectId][token];
158
- }
159
- }
160
-
161
- /// @notice Sends a fee to the fee-receiving terminal under a try/catch boundary.
162
- /// @dev Routed through `IJBHeldFeesExecutor(address(this)).executeProcessFee(...)` so the call is a real
163
- /// external CALL (try/catch semantics require it). Under DELEGATECALL `address(this)` is the terminal, so
164
- /// this becomes a call to the terminal's `executeProcessFee` whose `msg.sender == address(this)` check
165
- /// passes. On revert the amount is forgiven and added back to the project's balance — by design, a broken
166
- /// fee route should not permanently lock project funds.
167
- /// @param store The terminal's store, used for the forgive-on-revert credit.
168
- /// @param projectId The project paying the fee.
169
- /// @param token The token the fee is denominated in.
170
- /// @param amount The fee amount.
171
- /// @param beneficiary The address that receives any platform tokens minted by the fee payment.
172
- /// @param feeTerminal The terminal that'll receive the fee.
173
- /// @param wasHeld Whether the fee was previously held (true) or processed inline (false).
174
- function processFee(
175
- IJBTerminalStore store,
176
- uint256 projectId,
177
- address token,
178
- uint256 amount,
179
- address beneficiary,
180
- IJBTerminal feeTerminal,
181
- bool wasHeld
182
- )
183
- public
184
- {
185
- try IJBHeldFeesExecutor(address(this))
186
- .executeProcessFee({
187
- projectId: projectId, token: token, amount: amount, beneficiary: beneficiary, feeTerminal: feeTerminal
188
- }) {
189
- emit ProcessFee({
190
- projectId: projectId,
191
- token: token,
192
- amount: amount,
193
- wasHeld: wasHeld,
194
- beneficiary: beneficiary,
195
- caller: msg.sender
196
- });
197
- } catch (bytes memory reason) {
198
- // Forgive the fee, credit it back to the project, and surface the failure for off-chain observability.
199
- emit FeeReverted({
200
- projectId: projectId,
201
- token: token,
202
- feeProjectId: JBConstants.FEE_BENEFICIARY_PROJECT_ID,
203
- amount: amount,
204
- reason: reason,
205
- caller: msg.sender
206
- });
207
-
208
- store.recordAddedBalanceFor({projectId: projectId, token: token, amount: amount});
209
- }
210
- }
211
-
212
- /// @notice Returns held fees up to `amount` in FIFO order from the project's held-fee queue.
213
- /// @dev Walks the queue from `nextHeldFeeIndexOf` forward. Whole entries are consumed when `leftoverAmount`
214
- /// covers their post-fee amount; the final entry can be partially consumed by shrinking its `.amount` in
215
- /// place. The fee implied by the partial-consume branch uses `feeAmountResultingIn` (inverse of
216
- /// `feeAmountFrom`) so the credited fee aligns with the amount that was actually returned. Dust amounts
217
- /// below the floor produce `feeAmount == 0`.
218
- /// @param heldFeesOf Storage ref to the terminal's per-(project,token) held-fee queue.
219
- /// @param nextHeldFeeIndexOf Storage ref to the next-index cursor for each (project,token) queue.
220
- /// @param projectId The project to return held fees for.
221
- /// @param token The token the held fees are denominated in.
222
- /// @param amount The reference amount to base the return on (typically the inbound addToBalance amount).
223
- /// @return returnedFees The total fee amount returned to the project.
224
- function returnHeldFees(
225
- mapping(uint256 => mapping(address => JBFee[])) storage heldFeesOf,
226
- mapping(uint256 => mapping(address => uint256)) storage nextHeldFeeIndexOf,
227
- uint256 projectId,
228
- address token,
229
- uint256 amount
230
- )
231
- external
232
- returns (uint256 returnedFees)
233
- {
234
- uint256 startIndex = nextHeldFeeIndexOf[projectId][token];
235
- uint256 numberOfHeldFees = heldFeesOf[projectId][token].length;
236
-
237
- // Empty queue — nothing to return.
238
- if (startIndex >= numberOfHeldFees) return 0;
239
-
240
- uint256 leftoverAmount = amount;
241
- uint256 count = numberOfHeldFees - startIndex;
242
- uint256 newStartIndex = startIndex;
243
-
244
- for (uint256 i; i < count;) {
245
- JBFee memory heldFee = heldFeesOf[projectId][token][startIndex + i];
246
-
247
- if (leftoverAmount == 0) {
248
- break;
249
- } else {
250
- // Recompute the fee charged on the stored gross amount so partial returns stay aligned.
251
- uint256 feeAmount = JBFees.standardFeeAmountFrom({amountBeforeFee: heldFee.amount});
252
- uint256 amountPaidOut = heldFee.amount - feeAmount;
253
-
254
- if (leftoverAmount >= amountPaidOut) {
255
- // Whole entry consumed: credit its full fee, advance the cursor past it.
256
- unchecked {
257
- leftoverAmount -= amountPaidOut;
258
- returnedFees += feeAmount;
259
- }
260
- newStartIndex = startIndex + i + 1;
261
- } else {
262
- // Partial entry: shrink the stored entry by what we consumed (incl. its own fee).
263
- feeAmount = JBFees.standardFeeAmountResultingIn({amountAfterFee: leftoverAmount});
264
- unchecked {
265
- heldFeesOf[projectId][token][startIndex + i].amount -= (leftoverAmount + feeAmount);
266
- returnedFees += feeAmount;
267
- }
268
- leftoverAmount = 0;
269
- }
270
- }
271
-
272
- unchecked {
273
- ++i;
274
- }
275
- }
276
-
277
- if (startIndex != newStartIndex) nextHeldFeeIndexOf[projectId][token] = newStartIndex;
278
-
279
- emit ReturnHeldFees({
280
- projectId: projectId,
281
- token: token,
282
- amount: amount,
283
- returnedFees: returnedFees,
284
- leftoverAmount: leftoverAmount,
285
- caller: msg.sender
286
- });
287
- }
288
- }