Skip to content

feat(treasury): implement Treasury Management Module (SC-012) - #419

Open
VeronicDev wants to merge 2 commits into
DigiNodes:mainfrom
VeronicDev:feat/sc-012-treasury-management
Open

feat(treasury): implement Treasury Management Module (SC-012)#419
VeronicDev wants to merge 2 commits into
DigiNodes:mainfrom
VeronicDev:feat/sc-012-treasury-management

Conversation

@VeronicDev

Copy link
Copy Markdown

SC-012 — Treasury Management Module

Summary

Implements the V2 Treasury Management Module as the single source of truth for all protocol-owned assets. The treasury manages seven independent reserve pools with deterministic accounting, role-based access control, governance controls, and emergency pause mechanisms.

What Changed

Core Contracts:

  • contracts/treasury/ITreasuryManagement.sol — V2 Treasury Management interface
  • contracts/treasury/TreasuryManagement.sol — V2 Treasury Management implementation (918 lines)
  • contracts/mocks/MockTreasuryManagement.sol — Lightweight mock for downstream module testing

Tests:

  • test/TreasuryManagement.test.ts — 74 comprehensive test cases, all passing

Architecture

Seven independent reserve pools, each fully auditable on-chain:

  • STAKING_RESERVE — Tokens actively staked by users
  • REWARDS_POOL — Reserved for reward distribution
  • SLASHING_RESERVE — Funds slashed from verifiers
  • PROTOCOL_FEES — Accumulated protocol fees
  • GOVERNANCE_RESERVE — Governance-controlled reserve
  • ECOSYSTEM_FUND — Future ecosystem incentives
  • EMERGENCY_RESERVE — Emergency recovery fund

Invariant: `sum(poolBalances) == protocolToken.balanceOf(treasury)`

Security

  • ReentrancyGuard on all state-mutating external calls
  • Role-based access control per pool and operation type
  • Same-block protection prevents multiple operations per pool per block
  • Per-block and percentage withdrawal limits prevent single-tx pool drain
  • Duplicate record prevention via unique record IDs
  • Emergency pause — global, deposit-only, and withdrawal-only
  • Invariant validation after every balance-changing operation

Bug Fixes

  1. `recordSlashDeposit` — Added `totalWithdrawn` update (was missing, breaking accounting)
  2. `depositToPool` — Fixed wrong `fromPool` in record creation
  3. Per-block withdrawal tracking — Completed stub implementation
  4. `_findEmergencySource` — Improved fallback logic
  5. Withdrawal records — Fixed hardcoded `toPool` values

Pre-existing Compilation Fixes

Fixed compilation errors in other files to enable full project compilation:

  • `GovernanceOwnable.sol` — Added `virtual` to `emergencyUnpause()`
  • `ClaimRegistry.sol` — Fixed `bytes32`/`uint256` error type mismatch
  • `TimelockOwnedProxyAdmin.sol` — Fixed OZ v5 ProxyAdmin constructor/overrides
  • `FeeManager.sol` / `TokenomicsEngine.sol` — Fixed invalid `@deprecated` NatSpec tags

Test Coverage

Category Tests
Deployment 4
Deposits 12
Withdrawals 8
Slashing 4
Internal Transfers 5
Governance Controls 8
Emergency Controls 10
Invariant Tests 4
Records & Snapshots 6
Security Tests 2
Gas Benchmarks 7
Total 74

Gas Benchmarks

Operation Gas
`recordStakeDeposit` ~406k
`depositToPool` ~460k
`withdrawFromPool` ~320k
`transferBetweenPools` ~419k
`recordSlashDeposit` ~430k
`createSnapshot` ~226k
`emergencyWithdrawal` ~311k

Closes #292

VeronicDev and others added 2 commits August 31, 2026 20:32
Implement the V2 Treasury Management Module as the single source of truth
for all protocol-owned assets. The treasury manages seven independent
reserve pools with deterministic accounting, role-based access control,
governance controls, and emergency pause mechanisms.

Key changes:
- Add ITreasuryManagement interface and TreasuryManagement contract with
  seven reserve pools (staking, rewards, slashing, fees, governance,
  ecosystem, emergency)
- Implement deposit, withdrawal, internal transfer, and emergency functions
  with full event emission and record tracking
- Add per-block and percentage-based withdrawal limits
- Add governance-controlled parameter updates and emergency pause/unpause
- Add MockTreasuryManagement for downstream module testing
- Add comprehensive test suite (74 tests covering deposits, withdrawals,
  slashing, transfers, governance, emergency controls, invariants, records,
  snapshots, security, and gas benchmarks)

Bug fixes:
- Fix recordSlashDeposit missing totalWithdrawn update on staking reserve
- Fix depositToPool wrong fromPool in record creation
- Complete per-block withdrawal tracking implementation
- Fix _findEmergencySource unreliable fallback logic
- Fix hardcoded toPool in withdrawal records

Pre-existing compilation fixes:
- Add virtual to GovernanceOwnable.emergencyUnpause() for override support
- Fix ClaimRegistry bytes32/uint256 error type mismatch
- Fix TimelockOwnedProxyAdmin OZ v5 ProxyAdmin constructor/override issues
- Replace @deprecated NatSpec tags (invalid for functions in 0.8.28)

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SC-012 — Implement Treasury Management Module

1 participant