@ballkidz/defifa 0.0.3 → 0.0.5

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/STYLE_GUIDE.md ADDED
@@ -0,0 +1,467 @@
1
+ # Style Guide
2
+
3
+ How we write Solidity and organize repos across the Juicebox V6 ecosystem. `nana-core-v6` is the gold standard — when in doubt, match what it does.
4
+
5
+ **This repo's deviations:** `via_ir = true` (stack depth). Package scope: `@ballkidz/`.
6
+
7
+ ## File Organization
8
+
9
+ ```
10
+ src/
11
+ ├── Contract.sol # Main contracts in root
12
+ ├── abstract/ # Base contracts (JBPermissioned, JBControlled)
13
+ ├── enums/ # One enum per file
14
+ ├── interfaces/ # One interface per file, prefixed with I
15
+ ├── libraries/ # Pure/view logic, prefixed with JB
16
+ ├── periphery/ # Utility contracts (deadlines, price feeds)
17
+ └── structs/ # One struct per file, prefixed with JB
18
+ ```
19
+
20
+ One contract/interface/struct/enum per file. Name the file after the type it contains.
21
+
22
+ **Structs, enums, libraries, and interfaces always go in their subdirectories** (`src/structs/`, `src/enums/`, `src/libraries/`, `src/interfaces/`) — never inline in contract files or placed in `src/` root. This keeps type definitions discoverable and import paths consistent across repos.
23
+
24
+ ## Pragma Versions
25
+
26
+ ```solidity
27
+ // Contracts — pin to exact version
28
+ pragma solidity 0.8.26;
29
+
30
+ // Interfaces, structs, enums — caret for forward compatibility
31
+ pragma solidity ^0.8.0;
32
+
33
+ // Libraries — caret, may use newer features
34
+ pragma solidity ^0.8.17;
35
+ ```
36
+
37
+ ## Imports
38
+
39
+ Named imports only. Grouped by source, alphabetized within each group:
40
+
41
+ ```solidity
42
+ // External packages (alphabetized)
43
+ import {ERC2771Context} from "@openzeppelin/contracts/metatx/ERC2771Context.sol";
44
+ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
45
+ import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
46
+ import {mulDiv} from "@prb/math/src/Common.sol";
47
+
48
+ // Local: abstract contracts
49
+ import {JBPermissioned} from "./abstract/JBPermissioned.sol";
50
+
51
+ // Local: interfaces (alphabetized)
52
+ import {IJBController} from "./interfaces/IJBController.sol";
53
+ import {IJBDirectory} from "./interfaces/IJBDirectory.sol";
54
+ import {IJBMultiTerminal} from "./interfaces/IJBMultiTerminal.sol";
55
+
56
+ // Local: libraries (alphabetized)
57
+ import {JBConstants} from "./libraries/JBConstants.sol";
58
+ import {JBFees} from "./libraries/JBFees.sol";
59
+
60
+ // Local: structs (alphabetized)
61
+ import {JBAccountingContext} from "./structs/JBAccountingContext.sol";
62
+ import {JBSplit} from "./structs/JBSplit.sol";
63
+ ```
64
+
65
+ ## Contract Structure
66
+
67
+ Section banners divide the contract into a fixed ordering. Every contract with 50+ lines uses these banners:
68
+
69
+ ```solidity
70
+ /// @notice One-line description.
71
+ contract JBExample is JBPermissioned, IJBExample {
72
+ // A library that does X.
73
+ using SomeLib for SomeType;
74
+
75
+ //*********************************************************************//
76
+ // --------------------------- custom errors ------------------------- //
77
+ //*********************************************************************//
78
+
79
+ error JBExample_SomethingFailed(uint256 amount);
80
+
81
+ //*********************************************************************//
82
+ // ------------------------- public constants ------------------------ //
83
+ //*********************************************************************//
84
+
85
+ uint256 public constant override FEE = 25;
86
+
87
+ //*********************************************************************//
88
+ // ----------------------- internal constants ------------------------ //
89
+ //*********************************************************************//
90
+
91
+ uint256 internal constant _FEE_BENEFICIARY_PROJECT_ID = 1;
92
+
93
+ //*********************************************************************//
94
+ // --------------- public immutable stored properties ---------------- //
95
+ //*********************************************************************//
96
+
97
+ IJBDirectory public immutable override DIRECTORY;
98
+
99
+ //*********************************************************************//
100
+ // --------------------- public stored properties -------------------- //
101
+ //*********************************************************************//
102
+
103
+ //*********************************************************************//
104
+ // -------------------- internal stored properties ------------------- //
105
+ //*********************************************************************//
106
+
107
+ //*********************************************************************//
108
+ // -------------------------- constructor ---------------------------- //
109
+ //*********************************************************************//
110
+
111
+ //*********************************************************************//
112
+ // ---------------------- receive / fallback ------------------------- //
113
+ //*********************************************************************//
114
+
115
+ //*********************************************************************//
116
+ // --------------------------- modifiers ----------------------------- //
117
+ //*********************************************************************//
118
+
119
+ //*********************************************************************//
120
+ // ---------------------- external transactions ---------------------- //
121
+ //*********************************************************************//
122
+
123
+ //*********************************************************************//
124
+ // ----------------------- external views ---------------------------- //
125
+ //*********************************************************************//
126
+
127
+ //*********************************************************************//
128
+ // ----------------------- public transactions ----------------------- //
129
+ //*********************************************************************//
130
+
131
+ //*********************************************************************//
132
+ // ----------------------- internal helpers -------------------------- //
133
+ //*********************************************************************//
134
+
135
+ //*********************************************************************//
136
+ // ----------------------- internal views ---------------------------- //
137
+ //*********************************************************************//
138
+
139
+ //*********************************************************************//
140
+ // ----------------------- private helpers --------------------------- //
141
+ //*********************************************************************//
142
+ }
143
+ ```
144
+
145
+ **Section order:**
146
+ 1. `using` declarations
147
+ 2. Custom errors
148
+ 3. Public constants
149
+ 4. Internal constants
150
+ 5. Public immutable stored properties
151
+ 6. Internal immutable stored properties
152
+ 7. Public stored properties
153
+ 8. Internal stored properties
154
+ 9. Constructor
155
+ 10. `receive` / `fallback`
156
+ 11. Modifiers
157
+ 12. External transactions
158
+ 13. External views
159
+ 14. Public transactions
160
+ 15. Internal helpers
161
+ 16. Internal views
162
+ 17. Private helpers
163
+
164
+ Functions are alphabetized within each section.
165
+
166
+ **Events:** Events are declared in interfaces only, never in implementation contracts. Implementations inherit events from their interface and emit them unqualified. This keeps the ABI definition in one place and allows tests to use interface-qualified event expectations (e.g., `emit IJBController.LaunchProject(...)`).
167
+
168
+ ## Interface Structure
169
+
170
+ ```solidity
171
+ /// @notice One-line description.
172
+ interface IJBExample is IJBBase {
173
+ // Events (with full NatSpec)
174
+
175
+ /// @notice Emitted when X happens.
176
+ /// @param projectId The ID of the project.
177
+ /// @param amount The amount transferred.
178
+ event SomethingHappened(uint256 indexed projectId, uint256 amount);
179
+
180
+ // Views (alphabetized)
181
+
182
+ /// @notice The directory of terminals and controllers.
183
+ function DIRECTORY() external view returns (IJBDirectory);
184
+
185
+ // State-changing functions (alphabetized)
186
+
187
+ /// @notice Does the thing.
188
+ /// @param projectId The ID of the project.
189
+ /// @return result The result.
190
+ function doThing(uint256 projectId) external returns (uint256 result);
191
+ }
192
+ ```
193
+
194
+ **Rules:**
195
+ - Events first, then views, then state-changing functions
196
+ - No custom errors in interfaces — errors belong in the implementing contract
197
+ - Full NatSpec on every event, function, and parameter
198
+ - Alphabetized within each group
199
+
200
+ ## Naming
201
+
202
+ | Thing | Convention | Example |
203
+ |-------|-----------|---------|
204
+ | Contract | PascalCase | `JBMultiTerminal` |
205
+ | Interface | `I` + PascalCase | `IJBMultiTerminal` |
206
+ | Library | PascalCase | `JBCashOuts` |
207
+ | Struct | PascalCase | `JBRulesetConfig` |
208
+ | Enum | PascalCase | `JBApprovalStatus` |
209
+ | Enum value | PascalCase | `ApprovalExpected` |
210
+ | Error | `ContractName_ErrorName` | `JBMultiTerminal_FeeTerminalNotFound` |
211
+ | Public constant | `ALL_CAPS` | `FEE`, `MAX_FEE` |
212
+ | Internal constant | `_ALL_CAPS` | `_FEE_HOLDING_SECONDS` |
213
+ | Public immutable | `ALL_CAPS` | `DIRECTORY`, `PERMISSIONS` |
214
+ | Public/external function | `camelCase` | `cashOutTokensOf` |
215
+ | Internal/private function | `_camelCase` | `_processFee` |
216
+ | Internal storage | `_camelCase` | `_accountingContextForTokenOf` |
217
+ | Function parameter | `camelCase` | `projectId`, `cashOutCount` |
218
+
219
+ ## NatSpec
220
+
221
+ **Contracts:**
222
+ ```solidity
223
+ /// @notice One-line description of what the contract does.
224
+ contract JBExample is IJBExample {
225
+ ```
226
+
227
+ **Functions:**
228
+ ```solidity
229
+ /// @notice Records funds being added to a project's balance.
230
+ /// @param projectId The ID of the project which funds are being added to.
231
+ /// @param token The token being added.
232
+ /// @param amount The amount added, as a fixed point number with the same decimals as the terminal.
233
+ /// @return surplus The new surplus after adding.
234
+ function recordAddedBalanceFor(
235
+ uint256 projectId,
236
+ address token,
237
+ uint256 amount
238
+ ) external override returns (uint256 surplus) {
239
+ ```
240
+
241
+ **Structs:**
242
+ ```solidity
243
+ /// @custom:member duration The number of seconds the ruleset lasts for. 0 means it never expires.
244
+ /// @custom:member weight How many tokens to mint per unit paid (18 decimals).
245
+ /// @custom:member weightCutPercent How much weight decays each cycle (9 decimals).
246
+ struct JBRulesetConfig {
247
+ uint32 duration;
248
+ uint112 weight;
249
+ uint32 weightCutPercent;
250
+ }
251
+ ```
252
+
253
+ **Mappings:**
254
+ ```solidity
255
+ /// @notice Context describing how a token is accounted for by a project.
256
+ /// @custom:param projectId The ID of the project.
257
+ /// @custom:param token The address of the token.
258
+ mapping(uint256 projectId => mapping(address token => JBAccountingContext)) internal _accountingContextForTokenOf;
259
+ ```
260
+
261
+ ## Numbers
262
+
263
+ Use underscores for thousands separators:
264
+
265
+ ```solidity
266
+ uint256 internal constant _FEE_HOLDING_SECONDS = 2_419_200; // 28 days
267
+ uint32 public constant MAX_WEIGHT_CUT_PERCENT = 1_000_000_000;
268
+ uint256 public constant MAX_RESERVED_PERCENT = 10_000;
269
+ ```
270
+
271
+ ## Function Calls
272
+
273
+ Use named parameters for readability when calling functions with 3+ arguments:
274
+
275
+ ```solidity
276
+ PERMISSIONS.hasPermission({
277
+ operator: sender,
278
+ account: account,
279
+ projectId: projectId,
280
+ permissionId: permissionId,
281
+ includeRoot: true,
282
+ includeWildcardProjectId: true
283
+ });
284
+ ```
285
+
286
+ ## Multiline Signatures
287
+
288
+ ```solidity
289
+ function recordCashOutFor(
290
+ address holder,
291
+ uint256 projectId,
292
+ uint256 cashOutCount,
293
+ JBAccountingContext calldata accountingContext
294
+ )
295
+ external
296
+ override
297
+ returns (
298
+ JBRuleset memory ruleset,
299
+ uint256 reclaimAmount,
300
+ JBCashOutHookSpecification[] memory hookSpecifications
301
+ )
302
+ {
303
+ ```
304
+
305
+ Modifiers and return types go on their own indented lines.
306
+
307
+ ## Error Handling
308
+
309
+ - Validate inputs with explicit `revert` + custom error
310
+ - Use `try-catch` only for external calls to untrusted contracts (hooks, fee processing)
311
+ - Always include relevant context in error parameters
312
+
313
+ ```solidity
314
+ // Direct validation
315
+ if (amount > limit) revert JBTerminalStore_InadequateControllerPayoutLimit(amount, limit);
316
+
317
+ // External call to untrusted hook
318
+ try hook.afterPayRecordedWith(context) {} catch (bytes memory reason) {
319
+ emit HookAfterPayReverted(hook, context, reason, _msgSender());
320
+ }
321
+ ```
322
+
323
+ ---
324
+
325
+ ## DevOps
326
+
327
+ ### foundry.toml
328
+
329
+ Standard config for `@bananapus/core-v6`:
330
+
331
+ ```toml
332
+ [profile.default]
333
+ solc = '0.8.26'
334
+ evm_version = 'cancun'
335
+ optimizer_runs = 200
336
+ libs = ["node_modules", "lib"]
337
+ fs_permissions = [{ access = "read-write", path = "./"}]
338
+
339
+ [profile.ci_sizes]
340
+ optimizer_runs = 200
341
+
342
+ [fuzz]
343
+ runs = 4096
344
+
345
+ [invariant]
346
+ runs = 1024
347
+ depth = 100
348
+ fail_on_revert = false
349
+
350
+ [fmt]
351
+ number_underscore = "thousands"
352
+ multiline_func_header = "all"
353
+ wrap_comments = true
354
+ ```
355
+
356
+ This is the standard config with no deviations.
357
+
358
+ ### CI Workflows
359
+
360
+ Every repo has at minimum `test.yml` and `lint.yml`:
361
+
362
+ **test.yml:**
363
+ ```yaml
364
+ name: test
365
+ on:
366
+ pull_request:
367
+ branches: [main]
368
+ push:
369
+ branches: [main]
370
+ jobs:
371
+ forge-test:
372
+ runs-on: ubuntu-latest
373
+ steps:
374
+ - uses: actions/checkout@v4
375
+ with:
376
+ submodules: recursive
377
+ - uses: actions/setup-node@v4
378
+ with:
379
+ node-version: 22.4.x
380
+ - name: Install npm dependencies
381
+ run: npm install --omit=dev
382
+ - name: Install Foundry
383
+ uses: foundry-rs/foundry-toolchain@v1
384
+ - name: Run tests
385
+ run: forge test --fail-fast --summary --detailed --skip "*/script/**"
386
+ - name: Check contract sizes
387
+ run: FOUNDRY_PROFILE=ci_sizes forge build --sizes --skip "*/test/**" --skip "*/script/**" --skip SphinxUtils
388
+ ```
389
+
390
+ **lint.yml:**
391
+ ```yaml
392
+ name: lint
393
+ on:
394
+ pull_request:
395
+ branches: [main]
396
+ push:
397
+ branches: [main]
398
+ jobs:
399
+ forge-fmt:
400
+ runs-on: ubuntu-latest
401
+ steps:
402
+ - uses: actions/checkout@v4
403
+ - name: Install Foundry
404
+ uses: foundry-rs/foundry-toolchain@v1
405
+ - name: Check formatting
406
+ run: forge fmt --check
407
+ ```
408
+
409
+ ### package.json
410
+
411
+ ```json
412
+ {
413
+ "name": "@bananapus/core-v6",
414
+ "version": "x.x.x",
415
+ "license": "MIT",
416
+ "repository": { "type": "git", "url": "git+https://github.com/Bananapus/nana-core-v6.git" },
417
+ "engines": { "node": ">=20.0.0" },
418
+ "scripts": {
419
+ "test": "forge test",
420
+ "coverage": "forge coverage --match-path \"./src/*.sol\" --report lcov --report summary"
421
+ },
422
+ "dependencies": { ... },
423
+ "devDependencies": {
424
+ "@sphinx-labs/plugins": "^0.33.2"
425
+ }
426
+ }
427
+ ```
428
+
429
+ **Scoping:** `@bananapus/` for Bananapus repos, `@rev-net/` for revnet, `@croptop/` for croptop, `@bannynet/` for banny, `@ballkidz/` for defifa.
430
+
431
+ ### remappings.txt
432
+
433
+ Every repo has a `remappings.txt`. Minimal content:
434
+
435
+ ```
436
+ @sphinx-labs/contracts/=lib/sphinx/packages/contracts/contracts/foundry
437
+ ```
438
+
439
+ Additional mappings as needed for repo-specific dependencies.
440
+
441
+ ### Formatting
442
+
443
+ Run `forge fmt` before committing. The `[fmt]` config in `foundry.toml` enforces:
444
+ - Thousands separators on numbers (`1_000_000`)
445
+ - Multiline function headers when multiple parameters
446
+ - Wrapped comments at reasonable width
447
+
448
+ CI checks formatting via `forge fmt --check`.
449
+
450
+ ### Branching
451
+
452
+ - `main` is the primary branch
453
+ - Feature branches for PRs
454
+ - All PRs trigger test + lint workflows
455
+ - Submodule checkout with `--recursive` in CI
456
+
457
+ ### Dependencies
458
+
459
+ - Solidity dependencies via npm (`node_modules/`)
460
+ - `forge-std` as a git submodule in `lib/`
461
+ - Sphinx plugins as a devDependency
462
+ - Cross-repo references use `file:../sibling-repo` in local development
463
+ - Published versions use semver ranges (`^0.0.x`) for npm
464
+
465
+ ### Contract Size Checks
466
+
467
+ CI runs `FOUNDRY_PROFILE=ci_sizes forge build --sizes` to catch contracts approaching the 24KB limit. The `ci_sizes` profile uses `optimizer_runs = 200` for realistic size measurement even when the default profile has different optimizer settings.
package/foundry.toml CHANGED
@@ -1,29 +1,20 @@
1
1
  [profile.default]
2
2
  solc = '0.8.26'
3
- evm_version = 'paris'
4
- src = 'src'
5
- out = 'out'
6
- test = 'test'
7
- libs = ["node_modules", "lib"]
3
+ evm_version = 'cancun'
4
+ via_ir = true
8
5
  optimizer_runs = 200
9
- via-ir = true
10
- build_info = true
11
- extra_output = ['storageLayout']
6
+ libs = ["node_modules", "lib"]
12
7
  fs_permissions = [{ access = "read-write", path = "./"}]
13
8
 
14
9
  [rpc_endpoints]
15
10
  ethereum = "${RPC_ETHEREUM_MAINNET}"
16
- optimism = "${RPC_OPTIMISM_MAINNET}"
17
- arbitrum = "${RPC_ARBITRUM_MAINNET}"
18
- base = "${RPC_BASE_MAINNET}"
19
- arbitrum_sepolia = "${RPC_ARBITRUM_SEPOLIA}"
20
- ethereum_sepolia = "${RPC_ETHEREUM_SEPOLIA}"
21
- optimism_sepolia = "${RPC_OPTIMISM_SEPOLIA}"
22
- base_sepolia = "${RPC_BASE_SEPOLIA}"
11
+
12
+ [fuzz]
13
+ runs = 4096
23
14
 
24
15
  [invariant]
25
- runs = 256
26
- depth = 50
16
+ runs = 1024
17
+ depth = 100
27
18
  fail_on_revert = false
28
19
 
29
20
  [lint]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ballkidz/defifa",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "license": "MIT",
5
5
  "engines": {
6
6
  "node": ">=20.0.0"
@@ -13,9 +13,9 @@
13
13
  "url": "https://github.com/BallKidz/defifa-collection-deployer"
14
14
  },
15
15
  "dependencies": {
16
- "@bananapus/721-hook-v6": "^0.0.9",
16
+ "@bananapus/721-hook-v6": "^0.0.13",
17
17
  "@bananapus/address-registry-v6": "^0.0.4",
18
- "@bananapus/core-v6": "^0.0.10",
18
+ "@bananapus/core-v6": "^0.0.14",
19
19
  "@bananapus/permission-ids-v6": "^0.0.5",
20
20
  "@openzeppelin/contracts": "5.2.0",
21
21
  "@prb/math": "^4.1.1",
@@ -411,8 +411,8 @@ contract DefifaDeployer is IDefifaDeployer, IDefifaGamePhaseReporter, IDefifaGam
411
411
  }
412
412
 
413
413
  // Make sure the provided gameplay timestamps are sequential and that there is a mint duration.
414
- // slither-disable-next-line incorrect-equality
415
414
  if (
415
+ // slither-disable-next-line incorrect-equality
416
416
  launchProjectData.mintPeriodDuration == 0
417
417
  || launchProjectData.start
418
418
  < block.timestamp + launchProjectData.refundPeriodDuration + launchProjectData.mintPeriodDuration
@@ -532,6 +532,7 @@ contract DefifaHook is JB721Hook, Ownable, IDefifaHook {
532
532
  /// @notice Mint reserved tokens within the tier for the provided value.
533
533
  /// @param tierId The ID of the tier to mint within.
534
534
  /// @param count The number of reserved tokens to mint.
535
+ // slither-disable-next-line reentrancy-no-eth
535
536
  function mintReservesFor(uint256 tierId, uint256 count) public override {
536
537
  // Minting reserves must not be paused.
537
538
  if (JB721TiersRulesetMetadataResolver.mintPendingReservesPaused(
@@ -996,12 +997,18 @@ contract DefifaHook is JB721Hook, Ownable, IDefifaHook {
996
997
  }
997
998
  }
998
999
 
1000
+ // Resolve the recipient's delegate. If the recipient has no delegate set, auto-delegate to themselves to
1001
+ // prevent attestation units from being permanently lost.
1002
+ address _toDelegate = _tierDelegation[_to][_tierId];
1003
+ if (_toDelegate == address(0) && _to != address(0)) {
1004
+ _toDelegate = _to;
1005
+ _tierDelegation[_to][_tierId] = _to;
1006
+ emit DelegateChanged(_to, address(0), _to);
1007
+ }
1008
+
999
1009
  // Move delegated attestations.
1000
1010
  _moveTierDelegateAttestations({
1001
- _from: _tierDelegation[_from][_tierId],
1002
- _to: _tierDelegation[_to][_tierId],
1003
- _tierId: _tierId,
1004
- _amount: _amount
1011
+ _from: _tierDelegation[_from][_tierId], _to: _toDelegate, _tierId: _tierId, _amount: _amount
1005
1012
  });
1006
1013
  }
1007
1014
 
@@ -444,8 +444,8 @@ contract DefifaGovernorTest is JBTest, TestBaseWorkflow {
444
444
  }
445
445
 
446
446
  function testSetCashOutRatesAndRedeem_multipleTiers(uint8 nTiers, uint8[] calldata distribution) public {
447
- vm.assume(nTiers > 10 && nTiers < 100);
448
- vm.assume(distribution.length < nTiers);
447
+ nTiers = uint8(bound(uint256(nTiers), 11, 99));
448
+ vm.assume(distribution.length > 0 && distribution.length < nTiers);
449
449
 
450
450
  uint256 _sumDistribution;
451
451
  for (uint256 i = 0; i < distribution.length; i++) {