This document describes the agent-based architecture of the ChainCash protocol - a peer-to-peer money creation system built on the Ergo blockchain.
ChainCash implements a decentralized monetary system where different agents manage the lifecycle of digital notes backed by collateral and trust. The system enables self-sovereign banking where each participant can issue, transfer, and redeem digital currency.
File: contracts/onchain/reserve.es
Responsibilities:
- Holds ERG collateral backing issued notes
- Manages reserve owner's public key
- Handles note redemption with 2% fee
- Supports reserve top-up operations
- Issues new notes against collateral
- Maintains ownership history via AVL tree
Key Functions:
createReserve: Initialize new reserve with collateraltopUpReserve: Add more collateral to existing reserveissueNote: Create new notes backed by reserve collateralredeemNote: Redeem notes against reserve with fee
File: contracts/onchain/note.es
Responsibilities:
- Represents ChainCash notes (digital currency)
- Manages note ownership and transfers
- Maintains spending chain history in AVL tree
- Handles redemption operations
- Enforces signature verification for transfers
Key Functions:
createNote: Issue new note from reservetransferNote: Transfer note to new ownerredeemNote: Redeem note against reserveverifySpendingChain: Validate spending history
File: contracts/onchain/receipt.es
Responsibilities:
- Created during note redemption
- Allows re-redemption against earlier reserves
- Automatically burns after 3 years
- Tracks redemption position and history
Key Functions:
createReceipt: Generate receipt during redemptionreRedeem: Re-redeem against earlier reservesautoBurn: Self-destruct after expiration
File: src/main/scala/chaincash/offchain/WalletUtils.scala
Responsibilities:
- Manages user addresses and keys
- Fetches unspent boxes from blockchain
- Handles transaction fee calculations
- Tracks user's notes and reserves
Key Operations:
- Address management and key generation
- Box scanning and filtering
- Fee estimation and transaction building
File: src/main/scala/chaincash/offchain/ReserveUtils.scala
Responsibilities:
- Creates new reserves with collateral
- Associates reserves with public keys
- Handles reserve funding operations
- Manages reserve lifecycle
Key Operations:
- Reserve creation and funding
- Collateral management
- Reserve state tracking
File: src/main/scala/chaincash/offchain/NoteUtils.scala
Responsibilities:
- Creates new notes against reserves
- Handles note transfers between users
- Manages signature creation for spending
- Updates ownership history
Key Operations:
- Note issuance from reserves
- Transfer processing and validation
- Spending chain maintenance
File: src/main/scala/chaincash/offchain/TrackingUtils.scala
Responsibilities:
- Scans blockchain for new reserves and notes
- Maintains local database of system state
- Processes blocks and updates local state
- Tracks user-owned reserves and notes
Key Operations:
- Blockchain scanning and indexing
- State synchronization
- Event processing and persistence
File: src/main/scala/chaincash/offchain/server/model.scala
Responsibilities:
- Implements acceptance predicates for notes
- Manages trust-based and collateral-based reserves
- Processes exchange transactions
- Implements mutual credit clearing
Key Features:
- Client-side validation of note quality
- Customizable acceptance policies
- Trust scoring and reserve evaluation
- Exchange rate management
Reserve Agent → Note Agent
1. User funds reserve with collateral
2. Reserve Agent creates new notes
3. Note Agent manages note lifecycle
Note Agent → Note Agent
1. Current owner signs note transfer
2. New owner receives note with updated spending chain
3. Reserve Agent verifies transfer validity
Note Agent → Reserve Agent → Receipt Agent
1. Note holder initiates redemption
2. Reserve Agent processes redemption with fee
3. Receipt Agent creates redemption receipt
4. Receipt allows re-redemption against earlier reserves
- Notes are backed by all previous spenders in the chain
- Each spender adds their collateral or trust to the backing
- Redemption can occur against any reserve in the spending history
- Each participant acts as their own bank
- Customizable acceptance policies per user/server
- No central authority controlling issuance or acceptance
- Supports both collateral-based reserves (ERG backing)
- Supports trust-based reserves (reputation/credit)
- Mixed backing combining multiple reserve types
- Each receiver validates notes based on their own criteria
- No global consensus on note quality
- Decentralized acceptance decisions
- Agents communicate via transaction inputs/outputs
- State changes recorded on blockchain
- Immutable spending chain maintained in AVL trees
- Database state synchronization
- Blockchain event monitoring
- Local state management and caching
- AVL Trees: Used for maintaining spending chains and ownership history
- Registers: Store public keys, amounts, and metadata
- Tokens: Represent notes and receipts as first-class assets
- Signature verification for all transfers
- Collateral ratio enforcement
- Time-based expiration for receipts
- Fee mechanisms to prevent spam
- Client-side validation reduces blockchain load
- Local state management for performance
- Parallel processing of independent transactions
Test coverage includes:
- Reserve creation and funding
- Note issuance and transfers
- Redemption flows
- Spending chain validation
- Fee calculations
- Edge cases and error conditions
See test files in src/test/scala/chaincash/ for detailed agent testing.
This architecture enables a global monetary system with decentralized issuance where each participant can define their own acceptance rules while maintaining collective backing through the spending chain.