Skip to main content

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

Formula:

Core Data Structures

DistributionConfig

Note: Total must equal 10,000 BPS (100%)

PoolBalances


Contract Roles


Inflow Methods

handleSlash

Receive and distribute slashed stakes from Agents.sol.
Parameters:
  • agentId: Agent whose stake was slashed
  • asset: Asset address (address(0) for ETH)
  • amount: Slashed amount
Payment:
  • For ETH: msg.value must equal amount
  • For ERC20: Tokens must be transferred before calling
Behavior:
  • Splits amount according to distribution config (40/30/30)
  • Updates respective pool balances
Emits: 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.
Parameters:
  • agentId: Agent who exited early
  • asset: Asset address
  • amount: Penalty amount
Payment:
  • Same as handleSlash
Emits: 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).
Parameters:
  • asset: Asset address
  • amount: Deposit amount
Payment:
  • For ETH: msg.value must equal amount
  • For ERC20: Tokens must be transferred before calling
Behavior:
  • Adds full amount to rewards pool (no distribution split)
Emits: RewardsDeposited(address indexed source, address indexed asset, uint256 amount) Example (Manual Deposit):
Example (From Tasks.sol Dispute Resolution):

Outflow Methods

distributeReward

Distribute rewards from rewards pool to an agent.
Parameters:
  • agentId: Agent receiving reward
  • asset: Asset address
  • amount: Reward amount
  • recipient: Recipient address (address(0) = agent owner)
  • reason: Human-readable justification
Requirements:
  • Agent must be registered in Agents.sol
  • Rewards pool must have sufficient balance
Emits: RewardPaid(uint256 indexed agentId, address indexed asset, address indexed recipient, uint256 amount, string reason) Example:
Use Cases:
  • Performance bonuses for high-reputation agents
  • Ecosystem growth incentives
  • Bug bounty rewards
  • Competition prizes

withdrawTreasury

Withdraw from treasury pool (governance/development funds).
Parameters:
  • asset: Asset address
  • amount: Withdrawal amount
  • to: Recipient address
Requirements:
  • Treasury pool must have sufficient balance
Emits: PoolWithdrawn(bytes32 indexed pool, address indexed asset, address indexed to, uint256 amount) Example:

withdrawInsurance

Withdraw from insurance pool (risk coverage, emergency funds).
Parameters:
  • Same as withdrawTreasury
Emits: PoolWithdrawn(...) Example:

View Functions

poolBalances

Query pool balances for a specific asset.
Returns:
Example:

rewardsBalance

Query rewards pool balance for an asset.
Example:

Admin Functions

setDistribution

Update distribution configuration (must sum to 10,000 BPS).
Parameters:
  • treasuryBps: Treasury allocation (0-10000)
  • insuranceBps: Insurance allocation (0-10000)
  • rewardsBps: Rewards allocation (0-10000)
Requirements:
  • treasuryBps + insuranceBps + rewardsBps == 10000
Emits: DistributionUpdated(uint16 treasuryBps, uint16 insuranceBps, uint16 rewardsBps) Example:
Governance Considerations:
  • 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.
Parameters:
  • newAgents: New Agents contract address
Example:

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 use nonReentrant 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_ROLE can authorize upgrades

Gas Optimization

  1. Batch Rewards: Distribute multiple rewards in a single transaction
  2. Asset Grouping: Withdraw multiple assets in sequence to amortize setup costs
  3. Event Monitoring: Use indexed event parameters for efficient filtering

Governance Considerations

Distribution Adjustments

Scenario 1: Bear Market (Prioritize Treasury)
Scenario 2: Growth Phase (Prioritize Rewards)
Scenario 3: High Risk Period (Prioritize Insurance)


ABI & Deployment

ABI Location: /nexis-appchain/packages/contracts-bedrock/artifacts/contracts/Treasury.sol/Treasury.json Network Addresses:

Support & Resources