Treasury.sol
Overview
The Treasury.sol contract is the central economic management system for the Nexis Appchain. It implements a 40/30/30 distribution model that automatically allocates incoming funds (from slashing, penalties, and direct deposits) across three strategic pools: Treasury (40%), Insurance (30%), and Rewards (30%). Contract Location:/nexis-appchain/packages/contracts-bedrock/contracts/Treasury.sol
Key Features
- Automated Distribution: All inflows automatically split across three pools
- Multi-Asset Support: Manage ETH and ERC20 tokens separately
- Role-Based Access: Granular control over deposits, withdrawals, and reward distribution
- Slashing Integration: Receive and distribute slashed stakes from Agents.sol
- Penalty Collection: Handle early withdrawal penalties with automatic pooling
- Reward Distribution: Controlled payout system for agent incentives
Architecture
Distribution Model
40/30/30 Split
| Pool | Allocation | Purpose | Access | 
|---|---|---|---|
| Treasury | 40% | Protocol development, operations, governance | WITHDRAW_ROLE | 
| Insurance | 30% | Risk coverage, emergency funds, user protection | WITHDRAW_ROLE | 
| Rewards | 30% | Agent incentives, performance bonuses, ecosystem growth | REWARDS_ROLE | 
Core Data Structures
DistributionConfig
PoolBalances
Contract Roles
| Role | Bytes32 Identifier | Description | 
|---|---|---|
| DEFAULT_ADMIN_ROLE | 0x00 | Administrative control over contract configuration | 
| REWARDS_ROLE | keccak256("REWARDS_ROLE") | Distribute rewards from rewards pool | 
| WITHDRAW_ROLE | keccak256("WITHDRAW_ROLE") | Withdraw from treasury and insurance pools | 
| INFLOW_ROLE | keccak256("INFLOW_ROLE") | Handle slashing and penalty inflows (typically Agents.sol) | 
Inflow Methods
handleSlash
Receive and distribute slashed stakes from Agents.sol.- agentId: Agent whose stake was slashed
- asset: Asset address (- address(0)for ETH)
- amount: Slashed amount
- For ETH: msg.valuemust equalamount
- For ERC20: Tokens must be transferred before calling
- Splits amountaccording to distribution config (40/30/30)
- Updates respective pool balances
SlashHandled(uint256 indexed agentId, address indexed asset, uint256 amount, uint256 treasuryShare, uint256 insuranceShare, uint256 rewardsShare)
Example (Called by Agents.sol):
handleEarlyExitPenalty
Receive and distribute early withdrawal penalties.- agentId: Agent who exited early
- asset: Asset address
- amount: Penalty amount
- Same as handleSlash
EarlyExitHandled(uint256 indexed agentId, address indexed asset, uint256 amount, uint256 treasuryShare, uint256 insuranceShare, uint256 rewardsShare)
Example (Called by Agents.sol):
recordRewardDeposit
Record direct deposits to rewards pool (e.g., from dispute resolutions, fundraising).- asset: Asset address
- amount: Deposit amount
- For ETH: msg.valuemust equalamount
- For ERC20: Tokens must be transferred before calling
- Adds full amountto rewards pool (no distribution split)
RewardsDeposited(address indexed source, address indexed asset, uint256 amount)
Example (Manual Deposit):
Outflow Methods
distributeReward
Distribute rewards from rewards pool to an agent.- agentId: Agent receiving reward
- asset: Asset address
- amount: Reward amount
- recipient: Recipient address (- address(0)= agent owner)
- reason: Human-readable justification
- Agent must be registered in Agents.sol
- Rewards pool must have sufficient balance
RewardPaid(uint256 indexed agentId, address indexed asset, address indexed recipient, uint256 amount, string reason)
Example:
- Performance bonuses for high-reputation agents
- Ecosystem growth incentives
- Bug bounty rewards
- Competition prizes
withdrawTreasury
Withdraw from treasury pool (governance/development funds).- asset: Asset address
- amount: Withdrawal amount
- to: Recipient address
- Treasury pool must have sufficient balance
PoolWithdrawn(bytes32 indexed pool, address indexed asset, address indexed to, uint256 amount)
Example:
withdrawInsurance
Withdraw from insurance pool (risk coverage, emergency funds).- Same as withdrawTreasury
PoolWithdrawn(...)
Example:
View Functions
poolBalances
Query pool balances for a specific asset.rewardsBalance
Query rewards pool balance for an asset.Admin Functions
setDistribution
Update distribution configuration (must sum to 10,000 BPS).- treasuryBps: Treasury allocation (0-10000)
- insuranceBps: Insurance allocation (0-10000)
- rewardsBps: Rewards allocation (0-10000)
- treasuryBps + insuranceBps + rewardsBps == 10000
DistributionUpdated(uint16 treasuryBps, uint16 insuranceBps, uint16 rewardsBps)
Example:
- Changes apply to future inflows only (existing balances unchanged)
- Consider community governance for distribution changes
- Document rationale for changes on-chain or IPFS
setAgents
Update Agents.sol contract reference.- newAgents: New Agents contract address
Events Reference
DistributionUpdated
SlashHandled
EarlyExitHandled
RewardsDeposited
RewardPaid
PoolWithdrawn
Economic Flow Diagrams
Slashing Flow
Early Exit Flow
Reward Distribution Flow
Integration Examples
Monitor Treasury Activity
Treasury Dashboard
Automated Reward Distribution
Security Considerations
Reentrancy Protection
All withdrawal and distribution functions usenonReentrant modifier to prevent reentrancy attacks.
Role-Based Access Control
- Separation of Duties: Withdraw role cannot distribute rewards, and vice versa
- INFLOW_ROLE: Typically granted only to Agents.sol
- Multi-sig Recommended: Use Gnosis Safe or similar for admin roles
Balance Integrity
- Distribution always sums to 100% (enforced by BPS check)
- Pool balances tracked per asset independently
- No cross-asset contamination
Upgrade Safety
- UUPS proxy pattern for upgrades
- Only DEFAULT_ADMIN_ROLEcan authorize upgrades
Gas Optimization
- Batch Rewards: Distribute multiple rewards in a single transaction
- Asset Grouping: Withdraw multiple assets in sequence to amortize setup costs
- Event Monitoring: Use indexed event parameters for efficient filtering
Governance Considerations
Distribution Adjustments
Scenario 1: Bear Market (Prioritize Treasury)Related Contracts
- Agents.sol - Sends slashed stakes and penalties to treasury
- Tasks.sol - Sends disputed task rewards to treasury
- Subscriptions.sol - May integrate for protocol fee collection
ABI & Deployment
ABI Location:/nexis-appchain/packages/contracts-bedrock/artifacts/contracts/Treasury.sol/Treasury.json
Network Addresses:
| Network | Contract Address | Explorer | 
|---|---|---|
| Nexis Mainnet | 0x... | View Contract | 
| Nexis Testnet | 0x... | View Contract | 
Support & Resources
- GitHub: nexis-network/nexis-appchain
- Discord: Nexis Community
- Documentation: nex-t1.ai