@bananapus/core-v6 0.0.18 → 0.0.19

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.
@@ -0,0 +1,415 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity 0.8.26;
3
+
4
+ import {JBTerminalStore} from "../../../../src/JBTerminalStore.sol";
5
+ import {IJBPayHook} from "../../../../src/interfaces/IJBPayHook.sol";
6
+ import {IJBPrices} from "../../../../src/interfaces/IJBPrices.sol";
7
+ import {IJBRulesetApprovalHook} from "../../../../src/interfaces/IJBRulesetApprovalHook.sol";
8
+ import {IJBRulesetDataHook} from "../../../../src/interfaces/IJBRulesetDataHook.sol";
9
+ import {IJBRulesets} from "../../../../src/interfaces/IJBRulesets.sol";
10
+ import {IJBToken} from "../../../../src/interfaces/IJBToken.sol";
11
+ import {JBConstants} from "../../../../src/libraries/JBConstants.sol";
12
+ import {JBRulesetMetadataResolver} from "../../../../src/libraries/JBRulesetMetadataResolver.sol";
13
+ import {JBBeforePayRecordedContext} from "../../../../src/structs/JBBeforePayRecordedContext.sol";
14
+ import {JBPayHookSpecification} from "../../../../src/structs/JBPayHookSpecification.sol";
15
+ import {JBRuleset} from "../../../../src/structs/JBRuleset.sol";
16
+ import {JBRulesetMetadata} from "../../../../src/structs/JBRulesetMetadata.sol";
17
+ import {JBTokenAmount} from "../../../../src/structs/JBTokenAmount.sol";
18
+ import {mulDiv} from "@prb/math/src/Common.sol";
19
+ import {JBTerminalStoreSetup} from "./JBTerminalStoreSetup.sol";
20
+
21
+ contract TestPreviewPayFrom_Local is JBTerminalStoreSetup {
22
+ using JBRulesetMetadataResolver for JBRuleset;
23
+
24
+ uint64 _projectId = 1;
25
+ uint256 _defaultValue = 1e18;
26
+ uint8 _defaultDecimals = 18;
27
+
28
+ // Mocks
29
+ IJBToken _token = IJBToken(makeAddr("token"));
30
+ IJBRulesetDataHook _dataHook = IJBRulesetDataHook(makeAddr("dataHook"));
31
+ IJBPayHook _payHook = IJBPayHook(makeAddr("payHook"));
32
+ address _terminal = makeAddr("terminal");
33
+
34
+ uint32 _currency = uint32(uint160(address(_token)));
35
+ uint32 _nativeCurrency = uint32(uint160(JBConstants.NATIVE_TOKEN));
36
+
37
+ function setUp() public {
38
+ super.terminalStoreSetup();
39
+ }
40
+
41
+ function test_WhenCurrentRulesetCycleNumberIsZero() external {
42
+ // it will revert INVALID_RULESET
43
+
44
+ JBTokenAmount memory _tokenAmount = JBTokenAmount({
45
+ token: address(_token), value: _defaultValue, decimals: _defaultDecimals, currency: _currency
46
+ });
47
+
48
+ JBRuleset memory _returnedRuleset = JBRuleset({
49
+ cycleNumber: 0,
50
+ id: uint48(block.timestamp),
51
+ basedOnId: 0,
52
+ start: uint48(block.timestamp),
53
+ duration: uint32(block.timestamp + 1000),
54
+ weight: 1e18,
55
+ weightCutPercent: 0,
56
+ approvalHook: IJBRulesetApprovalHook(address(0)),
57
+ metadata: 0
58
+ });
59
+
60
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
61
+
62
+ vm.expectRevert(abi.encodeWithSelector(JBTerminalStore.JBTerminalStore_RulesetNotFound.selector, _projectId));
63
+ _store.previewPayFrom({
64
+ terminal: _terminal,
65
+ payer: address(this),
66
+ amount: _tokenAmount,
67
+ projectId: _projectId,
68
+ beneficiary: address(this),
69
+ metadata: ""
70
+ });
71
+ }
72
+
73
+ function test_WhenCurrentRulesetPausePayEqTrue() external {
74
+ // it will revert RULESET_PAYMENT_PAUSED
75
+
76
+ JBTokenAmount memory _tokenAmount = JBTokenAmount({
77
+ token: address(_token), value: _defaultValue, decimals: _defaultDecimals, currency: _currency
78
+ });
79
+
80
+ JBRulesetMetadata memory _metadata = JBRulesetMetadata({
81
+ reservedPercent: 0,
82
+ cashOutTaxRate: JBConstants.MAX_CASH_OUT_TAX_RATE,
83
+ baseCurrency: uint32(uint160(JBConstants.NATIVE_TOKEN)),
84
+ pausePay: true,
85
+ pauseCreditTransfers: false,
86
+ allowOwnerMinting: false,
87
+ allowSetCustomToken: false,
88
+ allowTerminalMigration: false,
89
+ allowSetTerminals: false,
90
+ ownerMustSendPayouts: false,
91
+ allowSetController: false,
92
+ allowAddAccountingContext: true,
93
+ allowAddPriceFeed: false,
94
+ holdFees: false,
95
+ useTotalSurplusForCashOuts: false,
96
+ useDataHookForPay: false,
97
+ useDataHookForCashOut: false,
98
+ dataHook: address(0),
99
+ metadata: 0
100
+ });
101
+
102
+ uint256 _packedMetadata = JBRulesetMetadataResolver.packRulesetMetadata(_metadata);
103
+
104
+ JBRuleset memory _returnedRuleset = JBRuleset({
105
+ cycleNumber: uint48(block.timestamp),
106
+ id: uint48(block.timestamp),
107
+ basedOnId: 0,
108
+ start: uint48(block.timestamp),
109
+ duration: uint32(block.timestamp + 1000),
110
+ weight: 1e18,
111
+ weightCutPercent: 0,
112
+ approvalHook: IJBRulesetApprovalHook(address(0)),
113
+ metadata: _packedMetadata
114
+ });
115
+
116
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
117
+
118
+ vm.expectRevert(JBTerminalStore.JBTerminalStore_RulesetPaymentPaused.selector);
119
+ _store.previewPayFrom({
120
+ terminal: _terminal,
121
+ payer: address(this),
122
+ amount: _tokenAmount,
123
+ projectId: _projectId,
124
+ beneficiary: address(this),
125
+ metadata: ""
126
+ });
127
+ }
128
+
129
+ function test_MatchesRecordPaymentFrom_NoDataHook() external {
130
+ // it returns the same tokenCount as recordPaymentFrom for a simple payment without a data hook
131
+
132
+ JBTokenAmount memory _tokenAmount = JBTokenAmount({
133
+ token: address(_token), value: _defaultValue, decimals: _defaultDecimals, currency: _currency
134
+ });
135
+
136
+ JBRulesetMetadata memory _metadata = JBRulesetMetadata({
137
+ reservedPercent: 0,
138
+ cashOutTaxRate: JBConstants.MAX_CASH_OUT_TAX_RATE,
139
+ baseCurrency: uint32(uint160(address(_token))),
140
+ pausePay: false,
141
+ pauseCreditTransfers: false,
142
+ allowOwnerMinting: false,
143
+ allowSetCustomToken: false,
144
+ allowTerminalMigration: false,
145
+ allowSetTerminals: false,
146
+ ownerMustSendPayouts: false,
147
+ allowSetController: false,
148
+ allowAddAccountingContext: true,
149
+ allowAddPriceFeed: false,
150
+ holdFees: false,
151
+ useTotalSurplusForCashOuts: false,
152
+ useDataHookForPay: false,
153
+ useDataHookForCashOut: false,
154
+ dataHook: address(0),
155
+ metadata: 0
156
+ });
157
+
158
+ uint256 _packedMetadata = JBRulesetMetadataResolver.packRulesetMetadata(_metadata);
159
+
160
+ JBRuleset memory _returnedRuleset = JBRuleset({
161
+ cycleNumber: uint48(block.timestamp),
162
+ id: uint48(block.timestamp),
163
+ basedOnId: 0,
164
+ start: uint48(block.timestamp),
165
+ duration: uint32(block.timestamp + 1000),
166
+ weight: 1e18,
167
+ weightCutPercent: 0,
168
+ approvalHook: IJBRulesetApprovalHook(address(0)),
169
+ metadata: _packedMetadata
170
+ });
171
+
172
+ // Mock for preview call
173
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
174
+
175
+ (, uint256 previewTokenCount, JBPayHookSpecification[] memory previewSpecs) = _store.previewPayFrom({
176
+ terminal: address(this),
177
+ payer: address(this),
178
+ amount: _tokenAmount,
179
+ projectId: _projectId,
180
+ beneficiary: address(this),
181
+ metadata: ""
182
+ });
183
+
184
+ // Mock for record call
185
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
186
+
187
+ (, uint256 recordTokenCount, JBPayHookSpecification[] memory recordSpecs) = _store.recordPaymentFrom({
188
+ payer: address(this), amount: _tokenAmount, projectId: _projectId, beneficiary: address(this), metadata: ""
189
+ });
190
+
191
+ assertEq(previewTokenCount, recordTokenCount);
192
+ assertEq(previewSpecs.length, recordSpecs.length);
193
+ }
194
+
195
+ function test_MatchesRecordPaymentFrom_WithDataHook() external {
196
+ // it returns the same tokenCount and hook specs as recordPaymentFrom with a data hook
197
+
198
+ JBTokenAmount memory _tokenAmount = JBTokenAmount({
199
+ token: address(_token), value: _defaultValue, decimals: _defaultDecimals, currency: _currency
200
+ });
201
+
202
+ JBRulesetMetadata memory _metadata = JBRulesetMetadata({
203
+ reservedPercent: 0,
204
+ cashOutTaxRate: JBConstants.MAX_CASH_OUT_TAX_RATE,
205
+ baseCurrency: uint32(uint160(address(_token))),
206
+ pausePay: false,
207
+ pauseCreditTransfers: false,
208
+ allowOwnerMinting: false,
209
+ allowSetCustomToken: false,
210
+ allowTerminalMigration: false,
211
+ allowSetTerminals: false,
212
+ ownerMustSendPayouts: false,
213
+ allowSetController: false,
214
+ allowAddAccountingContext: true,
215
+ allowAddPriceFeed: false,
216
+ holdFees: false,
217
+ useTotalSurplusForCashOuts: false,
218
+ useDataHookForPay: true,
219
+ useDataHookForCashOut: false,
220
+ dataHook: address(_dataHook),
221
+ metadata: 0
222
+ });
223
+
224
+ uint256 _packedMetadata = JBRulesetMetadataResolver.packRulesetMetadata(_metadata);
225
+
226
+ JBRuleset memory _returnedRuleset = JBRuleset({
227
+ cycleNumber: uint48(block.timestamp),
228
+ id: uint48(block.timestamp),
229
+ basedOnId: 0,
230
+ start: uint48(block.timestamp),
231
+ duration: uint32(block.timestamp + 1000),
232
+ weight: 1e18,
233
+ weightCutPercent: 0,
234
+ approvalHook: IJBRulesetApprovalHook(address(0)),
235
+ metadata: _packedMetadata
236
+ });
237
+
238
+ JBPayHookSpecification[] memory _spec = new JBPayHookSpecification[](1);
239
+ _spec[0] = JBPayHookSpecification({hook: _payHook, amount: _defaultValue / 2, metadata: ""});
240
+
241
+ // The data hook context will use the terminal address passed to preview / msg.sender for record.
242
+ // Since we call both from address(this), they match.
243
+ JBBeforePayRecordedContext memory _context = JBBeforePayRecordedContext({
244
+ terminal: address(this),
245
+ payer: address(this),
246
+ amount: _tokenAmount,
247
+ projectId: _projectId,
248
+ rulesetId: uint48(block.timestamp),
249
+ beneficiary: address(this),
250
+ weight: _returnedRuleset.weight,
251
+ reservedPercent: _returnedRuleset.reservedPercent(),
252
+ metadata: ""
253
+ });
254
+
255
+ // Mock for preview call
256
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
257
+ mockExpect(
258
+ address(_dataHook),
259
+ abi.encodeCall(IJBRulesetDataHook.beforePayRecordedWith, (_context)),
260
+ abi.encode(1e18 / 2, _spec)
261
+ );
262
+
263
+ (, uint256 previewTokenCount, JBPayHookSpecification[] memory previewSpecs) = _store.previewPayFrom({
264
+ terminal: address(this),
265
+ payer: address(this),
266
+ amount: _tokenAmount,
267
+ projectId: _projectId,
268
+ beneficiary: address(this),
269
+ metadata: ""
270
+ });
271
+
272
+ // Mock for record call
273
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
274
+ mockExpect(
275
+ address(_dataHook),
276
+ abi.encodeCall(IJBRulesetDataHook.beforePayRecordedWith, (_context)),
277
+ abi.encode(1e18 / 2, _spec)
278
+ );
279
+
280
+ (, uint256 recordTokenCount, JBPayHookSpecification[] memory recordSpecs) = _store.recordPaymentFrom({
281
+ payer: address(this), amount: _tokenAmount, projectId: _projectId, beneficiary: address(this), metadata: ""
282
+ });
283
+
284
+ assertEq(previewTokenCount, recordTokenCount);
285
+ assertEq(previewSpecs.length, recordSpecs.length);
286
+ assertEq(previewSpecs[0].amount, recordSpecs[0].amount);
287
+ }
288
+
289
+ function test_DoesNotModifyState() external {
290
+ // it does not change balanceOf
291
+
292
+ JBTokenAmount memory _tokenAmount = JBTokenAmount({
293
+ token: address(_token), value: _defaultValue, decimals: _defaultDecimals, currency: _currency
294
+ });
295
+
296
+ JBRulesetMetadata memory _metadata = JBRulesetMetadata({
297
+ reservedPercent: 0,
298
+ cashOutTaxRate: JBConstants.MAX_CASH_OUT_TAX_RATE,
299
+ baseCurrency: uint32(uint160(address(_token))),
300
+ pausePay: false,
301
+ pauseCreditTransfers: false,
302
+ allowOwnerMinting: false,
303
+ allowSetCustomToken: false,
304
+ allowTerminalMigration: false,
305
+ allowSetTerminals: false,
306
+ ownerMustSendPayouts: false,
307
+ allowSetController: false,
308
+ allowAddAccountingContext: true,
309
+ allowAddPriceFeed: false,
310
+ holdFees: false,
311
+ useTotalSurplusForCashOuts: false,
312
+ useDataHookForPay: false,
313
+ useDataHookForCashOut: false,
314
+ dataHook: address(0),
315
+ metadata: 0
316
+ });
317
+
318
+ uint256 _packedMetadata = JBRulesetMetadataResolver.packRulesetMetadata(_metadata);
319
+
320
+ JBRuleset memory _returnedRuleset = JBRuleset({
321
+ cycleNumber: uint48(block.timestamp),
322
+ id: uint48(block.timestamp),
323
+ basedOnId: 0,
324
+ start: uint48(block.timestamp),
325
+ duration: uint32(block.timestamp + 1000),
326
+ weight: 1e18,
327
+ weightCutPercent: 0,
328
+ approvalHook: IJBRulesetApprovalHook(address(0)),
329
+ metadata: _packedMetadata
330
+ });
331
+
332
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
333
+
334
+ uint256 balanceBefore = _store.balanceOf(_terminal, _projectId, address(_token));
335
+
336
+ _store.previewPayFrom({
337
+ terminal: _terminal,
338
+ payer: address(this),
339
+ amount: _tokenAmount,
340
+ projectId: _projectId,
341
+ beneficiary: address(this),
342
+ metadata: ""
343
+ });
344
+
345
+ uint256 balanceAfter = _store.balanceOf(_terminal, _projectId, address(_token));
346
+
347
+ assertEq(balanceBefore, balanceAfter);
348
+ }
349
+
350
+ function test_WithDifferentBaseCurrency() external {
351
+ // it returns the correct tokenCount when baseCurrency differs from payment currency
352
+
353
+ JBTokenAmount memory _tokenAmount = JBTokenAmount({
354
+ token: address(_token), value: _defaultValue, decimals: _defaultDecimals, currency: _currency
355
+ });
356
+
357
+ JBRulesetMetadata memory _metadata = JBRulesetMetadata({
358
+ reservedPercent: 0,
359
+ cashOutTaxRate: JBConstants.MAX_CASH_OUT_TAX_RATE,
360
+ baseCurrency: _nativeCurrency,
361
+ pausePay: false,
362
+ pauseCreditTransfers: false,
363
+ allowOwnerMinting: false,
364
+ allowSetCustomToken: false,
365
+ allowTerminalMigration: false,
366
+ allowSetTerminals: false,
367
+ ownerMustSendPayouts: false,
368
+ allowSetController: false,
369
+ allowAddAccountingContext: true,
370
+ allowAddPriceFeed: false,
371
+ holdFees: false,
372
+ useTotalSurplusForCashOuts: false,
373
+ useDataHookForPay: false,
374
+ useDataHookForCashOut: false,
375
+ dataHook: address(0),
376
+ metadata: 0
377
+ });
378
+
379
+ uint256 _packedMetadata = JBRulesetMetadataResolver.packRulesetMetadata(_metadata);
380
+
381
+ JBRuleset memory _returnedRuleset = JBRuleset({
382
+ cycleNumber: uint48(block.timestamp),
383
+ id: uint48(block.timestamp),
384
+ basedOnId: 0,
385
+ start: uint48(block.timestamp),
386
+ duration: uint32(block.timestamp + 1000),
387
+ weight: 1e18,
388
+ weightCutPercent: 0,
389
+ approvalHook: IJBRulesetApprovalHook(address(0)),
390
+ metadata: _packedMetadata
391
+ });
392
+
393
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
394
+
395
+ // mock call to JBPrices pricePerUnitOf
396
+ mockExpect(
397
+ address(prices),
398
+ abi.encodeCall(IJBPrices.pricePerUnitOf, (_projectId, _currency, _nativeCurrency, 18)),
399
+ abi.encode(2e18)
400
+ );
401
+
402
+ uint256 expectedCount = mulDiv(_defaultValue, 1e18, 2e18);
403
+
404
+ (, uint256 tokenCount,) = _store.previewPayFrom({
405
+ terminal: _terminal,
406
+ payer: address(this),
407
+ amount: _tokenAmount,
408
+ projectId: _projectId,
409
+ beneficiary: address(this),
410
+ metadata: ""
411
+ });
412
+
413
+ assertEq(tokenCount, expectedCount);
414
+ }
415
+ }