@bananapus/core-v6 0.0.52 → 0.0.54
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.
- package/CHANGELOG.md +0 -37
- package/foundry.toml +0 -1
- package/package.json +1 -1
- package/src/JBMultiTerminal.sol +297 -539
- package/src/interfaces/IJBCashOutTerminal.sol +0 -68
- package/src/interfaces/IJBFeeTerminal.sol +1 -1
- package/src/libraries/JBConstants.sol +3 -6
- package/src/libraries/JBFees.sol +20 -25
- package/src/libraries/JBRulesetMetadataResolver.sol +12 -20
- package/src/structs/JBRulesetMetadata.sol +1 -4
- package/test/helpers/JBTest.sol +3 -6
- package/src/libraries/JBCashOutHookSpecsLib.sol +0 -181
- package/src/libraries/JBHeldFeesLib.sol +0 -288
|
@@ -9,39 +9,6 @@ import {JBRuleset} from "../structs/JBRuleset.sol";
|
|
|
9
9
|
|
|
10
10
|
/// @notice A terminal that can be cashed out from.
|
|
11
11
|
interface IJBCashOutTerminal is IJBTerminal {
|
|
12
|
-
/// @notice Atomically cash out a holder's tokens of one project and add the reclaim to another project's
|
|
13
|
-
/// balance (no project tokens minted on the destination side).
|
|
14
|
-
/// @dev Equivalent to calling `cashOutTokensOf` followed by `addToBalanceOf` on the destination project,
|
|
15
|
-
/// except the source-side cash out fee is skipped (the equivalent fee is bound on the destination
|
|
16
|
-
/// project's side instead). Held-fee return is hardcoded to `false` on the destination side — this
|
|
17
|
-
/// entrypoint is for value top-up only, not fee unlock.
|
|
18
|
-
/// @dev The destination terminal is whichever terminal the directory has registered as the beneficiary
|
|
19
|
-
/// project's primary terminal for `tokenToReclaim` (which may itself be a router that swaps before adding
|
|
20
|
-
/// to balance). Cashout-side hooks (if specified by the data hook) execute additively.
|
|
21
|
-
/// @dev Round-trip fee preservation is enforced by snapshotting the beneficiary project's
|
|
22
|
-
/// accounting-context balances on this terminal before and after the routing, and crediting
|
|
23
|
-
/// `_feeFreeSurplusOf` by the per-token delta on each context that grew. The beneficiary project's current
|
|
24
|
-
/// ruleset can set `pauseCrossProjectFeeFreeInflows` to opt out.
|
|
25
|
-
/// @param holder The address whose project tokens are being burned.
|
|
26
|
-
/// @param projectId The ID of the project whose project tokens are being burned.
|
|
27
|
-
/// @param cashOutCount The number of project tokens to burn.
|
|
28
|
-
/// @param tokenToReclaim The terminal token reclaimed from the source project's surplus.
|
|
29
|
-
/// @param beneficiaryProjectId The destination project receiving the reclaim.
|
|
30
|
-
/// @param cashOutMetadata Forwarded to the source project's data hook and any cashout hook specifications.
|
|
31
|
-
/// @param addToBalanceMetadata Forwarded to the destination project's `addToBalanceOf` event.
|
|
32
|
-
/// @return reclaimAmount The gross reclaim amount returned by the store.
|
|
33
|
-
function addToBalanceAfterCashOutTokensOf(
|
|
34
|
-
address holder,
|
|
35
|
-
uint256 projectId,
|
|
36
|
-
uint256 cashOutCount,
|
|
37
|
-
address tokenToReclaim,
|
|
38
|
-
uint256 beneficiaryProjectId,
|
|
39
|
-
bytes calldata cashOutMetadata,
|
|
40
|
-
bytes calldata addToBalanceMetadata
|
|
41
|
-
)
|
|
42
|
-
external
|
|
43
|
-
returns (uint256 reclaimAmount);
|
|
44
|
-
|
|
45
12
|
/// @notice A cash out was processed for a project.
|
|
46
13
|
/// @param rulesetId The ID of the ruleset during the cash out.
|
|
47
14
|
/// @param rulesetCycleNumber The cycle number of the ruleset during the cash out.
|
|
@@ -129,39 +96,4 @@ interface IJBCashOutTerminal is IJBTerminal {
|
|
|
129
96
|
)
|
|
130
97
|
external
|
|
131
98
|
returns (uint256 reclaimAmount);
|
|
132
|
-
|
|
133
|
-
/// @notice Atomically cash out a holder's tokens of one project and pay the reclaim into another. Equivalent
|
|
134
|
-
/// to calling `cashOutTokensOf` followed by `pay` on the destination project, except the source-side cash out
|
|
135
|
-
/// fee is skipped (the equivalent fee is bound on the destination project's side instead).
|
|
136
|
-
/// @dev The destination terminal is whichever terminal the directory has registered as the beneficiary project's
|
|
137
|
-
/// primary terminal for `tokenToReclaim` (which may itself be a router that swaps before paying). Cashout-side
|
|
138
|
-
/// hooks (if specified by the data hook) execute additively.
|
|
139
|
-
/// @dev Round-trip fee preservation is enforced by snapshotting the beneficiary project's accounting-context
|
|
140
|
-
/// balances on this terminal before and after the routing, and crediting `_feeFreeSurplusOf` by the per-token
|
|
141
|
-
/// delta on each context that grew. The beneficiary project's current ruleset can set
|
|
142
|
-
/// `pauseCrossProjectFeeFreeInflows` to opt out.
|
|
143
|
-
/// @param holder The address whose project tokens are being burned.
|
|
144
|
-
/// @param projectId The ID of the project whose project tokens are being burned.
|
|
145
|
-
/// @param cashOutCount The number of project tokens to burn.
|
|
146
|
-
/// @param tokenToReclaim The terminal token reclaimed from the source project's surplus.
|
|
147
|
-
/// @param beneficiaryProjectId The destination project.
|
|
148
|
-
/// @param beneficiary The address that receives the newly minted tokens of the destination project.
|
|
149
|
-
/// @param minTokensOut The minimum number of destination-project tokens that must be minted; reverts otherwise.
|
|
150
|
-
/// @param cashOutMetadata Forwarded to the source project's data hook and any cashout hook specifications.
|
|
151
|
-
/// @param payMetadata Forwarded to the destination project's pay flow.
|
|
152
|
-
/// @return reclaimAmount The gross reclaim amount returned by the store.
|
|
153
|
-
/// @return beneficiaryTokenCount The number of destination-project tokens minted to `beneficiary`.
|
|
154
|
-
function payAfterCashOutTokensOf(
|
|
155
|
-
address holder,
|
|
156
|
-
uint256 projectId,
|
|
157
|
-
uint256 cashOutCount,
|
|
158
|
-
address tokenToReclaim,
|
|
159
|
-
uint256 beneficiaryProjectId,
|
|
160
|
-
address beneficiary,
|
|
161
|
-
uint256 minTokensOut,
|
|
162
|
-
bytes calldata cashOutMetadata,
|
|
163
|
-
bytes calldata payMetadata
|
|
164
|
-
)
|
|
165
|
-
external
|
|
166
|
-
returns (uint256 reclaimAmount, uint256 beneficiaryTokenCount);
|
|
167
99
|
}
|
|
@@ -27,7 +27,7 @@ interface IJBFeeTerminal is IJBTerminal {
|
|
|
27
27
|
/// @param projectId The ID of the project the fee was held for.
|
|
28
28
|
/// @param token The token the fee is denominated in.
|
|
29
29
|
/// @param amount The amount from which the fee was calculated.
|
|
30
|
-
/// @param fee The fee
|
|
30
|
+
/// @param fee The fee numerator used to calculate the held fee, out of `JBConstants.MAX_FEE`.
|
|
31
31
|
/// @param beneficiary The address that will receive project tokens when the fee is processed.
|
|
32
32
|
/// @param caller The address that triggered the fee hold.
|
|
33
33
|
event HoldFee(
|
|
@@ -18,12 +18,9 @@ library JBConstants {
|
|
|
18
18
|
/// @notice The denominator for split percentages (9-decimal precision). A split of 1,000,000,000 = 100%.
|
|
19
19
|
uint32 public constant SPLITS_TOTAL_PERCENT = 1_000_000_000;
|
|
20
20
|
|
|
21
|
-
/// @notice The fee denominator. The protocol fee is `
|
|
21
|
+
/// @notice The fee denominator. The protocol fee is `STANDARD_FEE / MAX_FEE`.
|
|
22
22
|
uint16 public constant MAX_FEE = 1000;
|
|
23
23
|
|
|
24
|
-
/// @notice The fee numerator. The protocol fee is `
|
|
25
|
-
uint16 public constant
|
|
26
|
-
|
|
27
|
-
/// @notice The project ID that receives protocol fees. Should be the first project launched at deployment.
|
|
28
|
-
uint256 public constant FEE_BENEFICIARY_PROJECT_ID = 1;
|
|
24
|
+
/// @notice The standard protocol fee numerator. The protocol fee is `STANDARD_FEE / MAX_FEE` = 2.5%.
|
|
25
|
+
uint16 public constant STANDARD_FEE = 25;
|
|
29
26
|
}
|
package/src/libraries/JBFees.sol
CHANGED
|
@@ -3,53 +3,48 @@ pragma solidity 0.8.28;
|
|
|
3
3
|
|
|
4
4
|
import {mulDiv} from "@prb/math/src/Common.sol";
|
|
5
5
|
|
|
6
|
-
import {JBConstants} from "
|
|
6
|
+
import {JBConstants} from "./JBConstants.sol";
|
|
7
7
|
|
|
8
8
|
/// @notice Fee calculations.
|
|
9
9
|
library JBFees {
|
|
10
|
-
/// @notice Returns the fee amount that, when added to `amountAfterFee`, produces the gross amount needed to yield
|
|
11
|
-
/// `amountAfterFee` after the fee is deducted.
|
|
12
|
-
/// @dev Use this to back-calculate the fee from a desired post-fee payout.
|
|
13
|
-
/// @param amountAfterFee The desired post-fee amount, as a fixed point number.
|
|
14
|
-
/// @param feePercent The fee percent, out of `JBConstants.MAX_FEE`.
|
|
15
|
-
/// @return The fee amount, as a fixed point number with the same number of decimals as the provided `amount`.
|
|
16
|
-
function feeAmountResultingIn(uint256 amountAfterFee, uint256 feePercent) internal pure returns (uint256) {
|
|
17
|
-
return mulDiv(amountAfterFee, JBConstants.MAX_FEE, JBConstants.MAX_FEE - feePercent) - amountAfterFee;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
10
|
/// @notice Returns the fee that would be taken from `amountBeforeFee`.
|
|
21
11
|
/// @dev Use this to forward-calculate the fee from a known pre-fee amount.
|
|
22
12
|
/// @dev Fee rounding error is bounded by N-1 wei (N = number of splits). Economically
|
|
23
13
|
/// insignificant. Rounds down (mulDiv floors), so the fee beneficiary may receive up to 1 wei less per split.
|
|
24
14
|
/// @param amountBeforeFee The amount before the fee is applied, as a fixed point number.
|
|
25
15
|
/// @param feePercent The fee percent, out of `JBConstants.MAX_FEE`.
|
|
26
|
-
/// @return The fee amount, as a fixed point number with the same number of decimals as
|
|
16
|
+
/// @return The fee amount, as a fixed point number with the same number of decimals as `amountBeforeFee`.
|
|
27
17
|
function feeAmountFrom(uint256 amountBeforeFee, uint256 feePercent) internal pure returns (uint256) {
|
|
28
18
|
return mulDiv(amountBeforeFee, feePercent, JBConstants.MAX_FEE);
|
|
29
19
|
}
|
|
30
20
|
|
|
31
|
-
/// @notice
|
|
32
|
-
///
|
|
33
|
-
///
|
|
34
|
-
/// @
|
|
35
|
-
///
|
|
36
|
-
///
|
|
21
|
+
/// @notice Returns the fee amount that, when added to `amountAfterFee`, produces the gross amount needed to yield
|
|
22
|
+
/// `amountAfterFee` after the fee is deducted.
|
|
23
|
+
/// @dev Use this to back-calculate the fee from a desired post-fee payout.
|
|
24
|
+
/// @param amountAfterFee The desired post-fee amount, as a fixed point number.
|
|
25
|
+
/// @param feePercent The fee percent, out of `JBConstants.MAX_FEE`.
|
|
26
|
+
/// @return The fee amount, as a fixed point number with the same number of decimals as `amountAfterFee`.
|
|
27
|
+
function feeAmountResultingIn(uint256 amountAfterFee, uint256 feePercent) internal pure returns (uint256) {
|
|
28
|
+
return mulDiv(amountAfterFee, JBConstants.MAX_FEE, JBConstants.MAX_FEE - feePercent) - amountAfterFee;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// @notice Returns the standard protocol fee taken from `amountBeforeFee`.
|
|
32
|
+
/// @dev The standard fee is `25 / 1_000`, pre-reduced to `1 / 40`. If `JBConstants.STANDARD_FEE` or
|
|
33
|
+
/// `JBConstants.MAX_FEE` changes, this denominator must be reduced again.
|
|
37
34
|
/// @param amountBeforeFee The amount before the fee is applied, as a fixed point number.
|
|
38
35
|
/// @return The fee amount, as a fixed point number with the same number of decimals as `amountBeforeFee`.
|
|
39
36
|
function standardFeeAmountFrom(uint256 amountBeforeFee) internal pure returns (uint256) {
|
|
37
|
+
// `JBConstants.STANDARD_FEE / JBConstants.MAX_FEE` is currently `1 / 40`.
|
|
40
38
|
return amountBeforeFee / 40;
|
|
41
39
|
}
|
|
42
40
|
|
|
43
|
-
/// @notice
|
|
44
|
-
///
|
|
45
|
-
///
|
|
46
|
-
/// @dev Pre-reduced: `mulDiv(amount, 1000, 1000 - 25) - amount` ≡ `mulDiv(amount, 40, 39) - amount`
|
|
47
|
-
/// (gcd(1000, 975) = 25). `mulDiv` (not raw `*`) preserves overflow-safety for the rare wildly-large
|
|
48
|
-
/// inputs and matches the rounding behavior of `feeAmountResultingIn` exactly. If `JBConstants.FEE` or
|
|
49
|
-
/// `JBConstants.MAX_FEE` changes, the constants `40` and `39` here MUST be reduced again.
|
|
41
|
+
/// @notice Back-calculates the standard protocol fee from a known post-fee amount.
|
|
42
|
+
/// @dev `1 / (1 - 25 / 1_000) - 1` reduces to `40 / 39 - 1`. If `JBConstants.STANDARD_FEE` or
|
|
43
|
+
/// `JBConstants.MAX_FEE` changes, these constants must be reduced again.
|
|
50
44
|
/// @param amountAfterFee The desired post-fee amount, as a fixed point number.
|
|
51
45
|
/// @return The fee amount that, when added to `amountAfterFee`, yields the gross pre-fee amount.
|
|
52
46
|
function standardFeeAmountResultingIn(uint256 amountAfterFee) internal pure returns (uint256) {
|
|
47
|
+
// Use `mulDiv` instead of `amountAfterFee * 40 / 39` to preserve overflow safety.
|
|
53
48
|
return mulDiv(amountAfterFee, 40, 39) - amountAfterFee;
|
|
54
49
|
}
|
|
55
50
|
}
|
|
@@ -71,25 +71,20 @@ library JBRulesetMetadataResolver {
|
|
|
71
71
|
return ((ruleset.metadata >> 79) & 1) == 1;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
function pauseCrossProjectFeeFreeInflows(JBRuleset memory ruleset) internal pure returns (bool) {
|
|
75
|
-
return ((ruleset.metadata >> 80) & 1) == 1;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
74
|
function useDataHookForPay(JBRuleset memory ruleset) internal pure returns (bool) {
|
|
79
|
-
return (ruleset.metadata >>
|
|
75
|
+
return (ruleset.metadata >> 80) & 1 == 1;
|
|
80
76
|
}
|
|
81
77
|
|
|
82
78
|
function useDataHookForCashOut(JBRuleset memory ruleset) internal pure returns (bool) {
|
|
83
|
-
return (ruleset.metadata >>
|
|
79
|
+
return (ruleset.metadata >> 81) & 1 == 1;
|
|
84
80
|
}
|
|
85
81
|
|
|
86
82
|
function dataHook(JBRuleset memory ruleset) internal pure returns (address) {
|
|
87
|
-
return address(uint160(ruleset.metadata >>
|
|
83
|
+
return address(uint160(ruleset.metadata >> 82));
|
|
88
84
|
}
|
|
89
85
|
|
|
90
86
|
function metadata(JBRuleset memory ruleset) internal pure returns (uint16) {
|
|
91
|
-
|
|
92
|
-
return uint16(ruleset.metadata >> 243);
|
|
87
|
+
return uint16(ruleset.metadata >> 242);
|
|
93
88
|
}
|
|
94
89
|
|
|
95
90
|
/// @notice Pack the funding cycle metadata.
|
|
@@ -130,16 +125,14 @@ library JBRulesetMetadataResolver {
|
|
|
130
125
|
if (rulesetMetadata.holdFees) packed |= 1 << 78;
|
|
131
126
|
// scopeCashOutsToLocalBalances in bit 79.
|
|
132
127
|
if (rulesetMetadata.scopeCashOutsToLocalBalances) packed |= 1 << 79;
|
|
133
|
-
//
|
|
134
|
-
if (rulesetMetadata.
|
|
135
|
-
// use
|
|
136
|
-
if (rulesetMetadata.
|
|
137
|
-
//
|
|
138
|
-
|
|
139
|
-
//
|
|
140
|
-
packed |= uint256(
|
|
141
|
-
// metadata in bits 243-255 (13 bits).
|
|
142
|
-
packed |= (uint256(rulesetMetadata.metadata) & 0x1FFF) << 243;
|
|
128
|
+
// use pay data source in bit 80.
|
|
129
|
+
if (rulesetMetadata.useDataHookForPay) packed |= 1 << 80;
|
|
130
|
+
// use cash out data source in bit 81.
|
|
131
|
+
if (rulesetMetadata.useDataHookForCashOut) packed |= 1 << 81;
|
|
132
|
+
// data source address in bits 82-241.
|
|
133
|
+
packed |= uint256(uint160(address(rulesetMetadata.dataHook))) << 82;
|
|
134
|
+
// metadata in bits 242-255 (14 bits).
|
|
135
|
+
packed |= (uint256(rulesetMetadata.metadata) & 0x3FFF) << 242;
|
|
143
136
|
}
|
|
144
137
|
|
|
145
138
|
/// @notice Expand the funding cycle metadata.
|
|
@@ -162,7 +155,6 @@ library JBRulesetMetadataResolver {
|
|
|
162
155
|
ownerMustSendPayouts: ownerMustSendPayouts(ruleset),
|
|
163
156
|
holdFees: holdFees(ruleset),
|
|
164
157
|
scopeCashOutsToLocalBalances: scopeCashOutsToLocalBalances(ruleset),
|
|
165
|
-
pauseCrossProjectFeeFreeInflows: pauseCrossProjectFeeFreeInflows(ruleset),
|
|
166
158
|
useDataHookForPay: useDataHookForPay(ruleset),
|
|
167
159
|
useDataHookForCashOut: useDataHookForCashOut(ruleset),
|
|
168
160
|
dataHook: dataHook(ruleset),
|
|
@@ -23,12 +23,10 @@ pragma solidity ^0.8.0;
|
|
|
23
23
|
/// @custom:member holdFees If `true`, fees are accumulated but not processed until a future ruleset (or manually).
|
|
24
24
|
/// @custom:member scopeCashOutsToLocalBalances If `true`, omnichain cash-out calculations use only the local chain's
|
|
25
25
|
/// balances (not cross-chain aggregates).
|
|
26
|
-
/// @custom:member pauseCrossProjectFeeFreeInflows If `true`, the project cannot be targeted by
|
|
27
|
-
/// `payAfterCashOutTokensOf` calls during this ruleset.
|
|
28
26
|
/// @custom:member useDataHookForPay If `true`, the data hook is called before recording payments.
|
|
29
27
|
/// @custom:member useDataHookForCashOut If `true`, the data hook is called before recording cash outs.
|
|
30
28
|
/// @custom:member dataHook Contract called before pay/cash-out to potentially override token counts or add hooks.
|
|
31
|
-
/// @custom:member metadata
|
|
29
|
+
/// @custom:member metadata 14 bits of application-specific metadata (upper 2 bits are ignored).
|
|
32
30
|
struct JBRulesetMetadata {
|
|
33
31
|
uint16 reservedPercent;
|
|
34
32
|
uint16 cashOutTaxRate;
|
|
@@ -45,7 +43,6 @@ struct JBRulesetMetadata {
|
|
|
45
43
|
bool ownerMustSendPayouts;
|
|
46
44
|
bool holdFees;
|
|
47
45
|
bool scopeCashOutsToLocalBalances;
|
|
48
|
-
bool pauseCrossProjectFeeFreeInflows;
|
|
49
46
|
bool useDataHookForPay;
|
|
50
47
|
bool useDataHookForCashOut;
|
|
51
48
|
address dataHook;
|
package/test/helpers/JBTest.sol
CHANGED
|
@@ -68,8 +68,7 @@ contract JBTest is Test {
|
|
|
68
68
|
useDataHookForPay: false,
|
|
69
69
|
useDataHookForCashOut: false,
|
|
70
70
|
dataHook: address(0),
|
|
71
|
-
metadata: 0
|
|
72
|
-
pauseCrossProjectFeeFreeInflows: false
|
|
71
|
+
metadata: 0
|
|
73
72
|
});
|
|
74
73
|
|
|
75
74
|
uint256 packed = _rulesMetadata.packRulesetMetadata();
|
|
@@ -107,8 +106,7 @@ contract JBTest is Test {
|
|
|
107
106
|
useDataHookForPay: false,
|
|
108
107
|
useDataHookForCashOut: false,
|
|
109
108
|
dataHook: address(0),
|
|
110
|
-
metadata: 0
|
|
111
|
-
pauseCrossProjectFeeFreeInflows: false
|
|
109
|
+
metadata: 0
|
|
112
110
|
});
|
|
113
111
|
}
|
|
114
112
|
|
|
@@ -132,8 +130,7 @@ contract JBTest is Test {
|
|
|
132
130
|
useDataHookForPay: false,
|
|
133
131
|
useDataHookForCashOut: false,
|
|
134
132
|
dataHook: address(0),
|
|
135
|
-
metadata: 0
|
|
136
|
-
pauseCrossProjectFeeFreeInflows: false
|
|
133
|
+
metadata: 0
|
|
137
134
|
});
|
|
138
135
|
|
|
139
136
|
uint256 packed = _rulesMetadata.packRulesetMetadata();
|
|
@@ -1,181 +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 {JBRuleset} from "../structs/JBRuleset.sol";
|
|
12
|
-
import {JBTokenAmount} from "../structs/JBTokenAmount.sol";
|
|
13
|
-
import {JBConstants} from "./JBConstants.sol";
|
|
14
|
-
import {JBFees} from "./JBFees.sol";
|
|
15
|
-
|
|
16
|
-
/// @notice Cash-out hook specification fulfillment for `JBMultiTerminal`. Extracted to reduce terminal
|
|
17
|
-
/// bytecode size, mirroring the `JBHeldFeesLib` pattern.
|
|
18
|
-
/// @dev Called via DELEGATECALL — `address(this)` inside library code is the terminal's address, so token
|
|
19
|
-
/// approvals and ETH-bearing hook calls operate on the terminal's balance and allowances. Events are
|
|
20
|
-
/// emitted from the terminal address. The `caller` field of `HookAfterRecordCashOut` uses raw `msg.sender`
|
|
21
|
-
/// (matching `JBHeldFeesLib`'s precedent) — this means meta-transactions through a trusted forwarder will
|
|
22
|
-
/// surface the forwarder address in the event slot, but the actual hook execution semantics are unaffected.
|
|
23
|
-
library JBCashOutHookSpecsLib {
|
|
24
|
-
// A library that adds default safety checks to ERC20 functionality.
|
|
25
|
-
using SafeERC20 for IERC20;
|
|
26
|
-
|
|
27
|
-
//*********************************************************************//
|
|
28
|
-
// ------------------------------ events ----------------------------- //
|
|
29
|
-
//*********************************************************************//
|
|
30
|
-
|
|
31
|
-
/// @notice A cash out hook was called after a cash out was recorded.
|
|
32
|
-
/// @param hook The cash out hook that was called.
|
|
33
|
-
/// @param context The context passed to the hook.
|
|
34
|
-
/// @param specificationAmount The amount specified for the hook.
|
|
35
|
-
/// @param fee The fee taken from the hook's amount.
|
|
36
|
-
/// @param caller The address that called the cash out function.
|
|
37
|
-
event HookAfterRecordCashOut(
|
|
38
|
-
IJBCashOutHook indexed hook,
|
|
39
|
-
JBAfterCashOutRecordedContext context,
|
|
40
|
-
uint256 specificationAmount,
|
|
41
|
-
uint256 fee,
|
|
42
|
-
address caller
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
//*********************************************************************//
|
|
46
|
-
// --------------------------- custom errors ------------------------- //
|
|
47
|
-
//*********************************************************************//
|
|
48
|
-
|
|
49
|
-
/// @notice Thrown when a hook returns without consuming the full forwarded ERC-20 amount.
|
|
50
|
-
error JBMultiTerminal_TemporaryAllowanceNotConsumed(address token, address spender, uint256 allowance);
|
|
51
|
-
|
|
52
|
-
//*********************************************************************//
|
|
53
|
-
// ----------------------- external functions ------------------------ //
|
|
54
|
-
//*********************************************************************//
|
|
55
|
-
|
|
56
|
-
/// @notice Iterates `specifications`, calling each non-noop hook with the right ETH/ERC-20 setup, and
|
|
57
|
-
/// accumulates the fee-eligible amount across non-feeless hooks.
|
|
58
|
-
/// @dev For each spec: if the hook is feeless, it gets the full spec amount; otherwise the hook gets
|
|
59
|
-
/// `amount - feeAmountFrom(amount)` and the gross spec amount is added to the eligible-for-fees total
|
|
60
|
-
/// (the caller takes the fee separately via `_takeFeeFrom`). Cross-token semantics: the hook context's
|
|
61
|
-
/// `forwardedAmount` carries the post-fee amount in the same token as `beneficiaryReclaimAmount`.
|
|
62
|
-
/// @param feelessAddresses Registry of fee-exempt addresses (consulted per-hook).
|
|
63
|
-
/// @param projectId The project being cashed out from.
|
|
64
|
-
/// @param beneficiaryReclaimAmount The token amount reference (token, decimals, currency, gross value).
|
|
65
|
-
/// @param holder The account whose project tokens were burned.
|
|
66
|
-
/// @param cashOutCount The number of project tokens burned.
|
|
67
|
-
/// @param metadata Bytes forwarded to each hook as `cashOutMetadata`.
|
|
68
|
-
/// @param ruleset The ruleset active during the cash out.
|
|
69
|
-
/// @param cashOutTaxRate The cash out tax rate applied.
|
|
70
|
-
/// @param beneficiary The address forwarded as the hook context's `beneficiary` (typically the user-supplied
|
|
71
|
-
/// recipient or `address(this)` for cross-project flows where the terminal custodies the reclaim mid-flow).
|
|
72
|
-
/// @param specifications The hook specifications returned by the data hook.
|
|
73
|
-
/// @return amountEligibleForFees Total spec amounts (gross) from non-feeless hooks, used by the caller to
|
|
74
|
-
/// charge fees in a single pass.
|
|
75
|
-
function fulfill(
|
|
76
|
-
IJBFeelessAddresses feelessAddresses,
|
|
77
|
-
uint256 projectId,
|
|
78
|
-
JBTokenAmount memory beneficiaryReclaimAmount,
|
|
79
|
-
address holder,
|
|
80
|
-
uint256 cashOutCount,
|
|
81
|
-
bytes memory metadata,
|
|
82
|
-
JBRuleset memory ruleset,
|
|
83
|
-
uint256 cashOutTaxRate,
|
|
84
|
-
address payable beneficiary,
|
|
85
|
-
JBCashOutHookSpecification[] memory specifications
|
|
86
|
-
)
|
|
87
|
-
external
|
|
88
|
-
returns (uint256 amountEligibleForFees)
|
|
89
|
-
{
|
|
90
|
-
JBAfterCashOutRecordedContext memory context = JBAfterCashOutRecordedContext({
|
|
91
|
-
holder: holder,
|
|
92
|
-
projectId: projectId,
|
|
93
|
-
rulesetId: ruleset.id,
|
|
94
|
-
cashOutCount: cashOutCount,
|
|
95
|
-
reclaimedAmount: beneficiaryReclaimAmount,
|
|
96
|
-
forwardedAmount: beneficiaryReclaimAmount,
|
|
97
|
-
cashOutTaxRate: cashOutTaxRate,
|
|
98
|
-
beneficiary: beneficiary,
|
|
99
|
-
hookMetadata: "",
|
|
100
|
-
cashOutMetadata: metadata
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
for (uint256 i; i < specifications.length;) {
|
|
104
|
-
JBCashOutHookSpecification memory specification = specifications[i];
|
|
105
|
-
|
|
106
|
-
// A noop specification is informational only and doesn't trigger the hook.
|
|
107
|
-
if (specification.noop) {
|
|
108
|
-
unchecked {
|
|
109
|
-
++i;
|
|
110
|
-
}
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// Get the fee for the specified amount.
|
|
115
|
-
uint256 specificationAmountFee = feelessAddresses.isFeelessFor({
|
|
116
|
-
addr: address(specification.hook), projectId: projectId
|
|
117
|
-
})
|
|
118
|
-
? 0
|
|
119
|
-
: JBFees.standardFeeAmountFrom({amountBeforeFee: specification.amount});
|
|
120
|
-
|
|
121
|
-
// Add the specification's amount to the amount eligible for fees.
|
|
122
|
-
if (specificationAmountFee != 0) {
|
|
123
|
-
amountEligibleForFees += 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: beneficiaryReclaimAmount.token,
|
|
131
|
-
decimals: beneficiaryReclaimAmount.decimals,
|
|
132
|
-
currency: beneficiaryReclaimAmount.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.
|
|
139
|
-
// Keep a reference to the amount that'll be paid as a `msg.value`.
|
|
140
|
-
uint256 payValue = _beforeTransferTo({
|
|
141
|
-
to: address(specification.hook), token: beneficiaryReclaimAmount.token, amount: specification.amount
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
// Fulfill the specification.
|
|
145
|
-
specification.hook.afterCashOutRecordedWith{value: payValue}(context);
|
|
146
|
-
|
|
147
|
-
// Revoke the temporary pull allowance now that the hook call has finished.
|
|
148
|
-
_afterTransferTo({to: address(specification.hook), token: beneficiaryReclaimAmount.token});
|
|
149
|
-
|
|
150
|
-
emit HookAfterRecordCashOut({
|
|
151
|
-
hook: specification.hook,
|
|
152
|
-
context: context,
|
|
153
|
-
specificationAmount: specification.amount,
|
|
154
|
-
fee: specificationAmountFee,
|
|
155
|
-
caller: msg.sender
|
|
156
|
-
});
|
|
157
|
-
unchecked {
|
|
158
|
-
++i;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
//*********************************************************************//
|
|
164
|
-
// ----------------------- private helpers --------------------------- //
|
|
165
|
-
//*********************************************************************//
|
|
166
|
-
|
|
167
|
-
/// @notice Native-token transfers use `msg.value` (return the amount). ERC20 transfers grant a temporary
|
|
168
|
-
/// pull allowance and return 0 (the recipient is expected to consume it within the same call).
|
|
169
|
-
function _beforeTransferTo(address to, address token, uint256 amount) private returns (uint256) {
|
|
170
|
-
if (token == JBConstants.NATIVE_TOKEN) return amount;
|
|
171
|
-
IERC20(token).forceApprove({spender: to, value: amount});
|
|
172
|
-
return 0;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/// @notice Asserts the recipient consumed the temporary ERC20 allowance. No-op for native token.
|
|
176
|
-
function _afterTransferTo(address to, address token) private view {
|
|
177
|
-
if (token == JBConstants.NATIVE_TOKEN) return;
|
|
178
|
-
uint256 allowance = IERC20(token).allowance({owner: address(this), spender: to});
|
|
179
|
-
if (allowance != 0) revert JBMultiTerminal_TemporaryAllowanceNotConsumed(token, to, allowance);
|
|
180
|
-
}
|
|
181
|
-
}
|