Smart contracts for decentralized agent registration and staking on the Cardano blockchain
- Overview
- Features
- Architecture
- Getting Started
- Contract Specifications
- Development
- Testing
- Deployment
- Contributing
- Resources
DAgent Contractor is a decentralized platform built on Cardano that enables secure agent registration and stake management. Written in Aiken, these smart contracts provide a robust foundation for managing agent relationships and token staking with full on-chain validation.
- β Trustless Operations: All transactions validated on-chain
- β Flexible Staking: Support for multiple stake operations (create, transfer, pull)
- β Agent Registry: Decentralized agent registration and management
- β Plutus V3: Built with the latest Cardano smart contract platform
- β Type Safety: Written in Aiken for compile-time guarantees
- Create Stakes: Add new stakes linking clients to providers
- Transfer Stakes: Move stakes between users atomically
- Pull Stakes: Withdraw specific amounts from stake positions
- Batch Operations: Pull all stakes for a user in one transaction
- Real-time Tracking: Automatic total stake and count calculations
- State Validation: Built-in invariant checking for data integrity
- Decentralized Registry: On-chain agent registration
- Immutable Records: Tamper-proof agent information storage
- Query Support: Retrieve agent details and stakes on-chain
dagent-contractor-cardano/
βββ validators/ # Smart contract validators
β βββ stake/ # Staking contract
β β βββ stake.ak # Main stake validator
β β βββ stake_test.ak # Test suite
β βββ agents/ # Agent registration
β βββ agents.ak # Agent validator
βββ lib/ # Supporting libraries
β βββ stake/
β βββ types.ak # Data type definitions
β βββ transition.ak # State transition logic
β βββ check.ak # Validation helpers
βββ build/ # Compiled artifacts
The staking contract manages stake relationships between clients and providers:
pub type Stake {
client: ByteArray, // Client address
provider: ByteArray, // Provider address
amount: Int, // Staked amount
user_id: ByteArray, // Unique user identifier
}Supported Actions:
CreateStake- Add new stake entryTransferStake- Move stake between usersPullStake- Withdraw partial stakePullAllStake- Withdraw all user stakesGetAddressStake- Query stake by addressGetTotalStake- Get total staked amountGetTotalStakeCount- Get total stake count
- Aiken v1.1.19 or later
- Cardano Node (for deployment)
- Git
-
Clone the repository
git clone https://github.com/d-agent/dagent-contract-cardano.git cd dagent-contract-cardano -
Build the contracts
aiken build
-
Verify installation
aiken check
pub type StakeDatum {
stakes: List<Stake>, // All stake records
total_stake: Int, // Sum of all stakes
total_count: Int, // Number of stakes
}All state transitions enforce invariants:
- Amounts must be positive
- Total stake must equal sum of individual stakes
- Total count must match list length
- Stakes cannot be duplicated
Creating a Stake:
CreateStake {
client: "addr_client...",
provider: "addr_provider...",
amount: 1000000, // 1 ADA (in lovelace)
user_id: "user_123"
}Transferring Stakes:
TransferStake {
from_user_id: "user_123",
to_user_id: "user_456",
amount: 500000 // 0.5 ADA
}Write validators in the validators/ folder and supporting functions in the lib/ folder using .ak as a file extension.
Edit aiken.toml for network configuration:
[config.default]
network_id = 41 # Testnet: 41, Mainnet: 1Compile all contracts:
aiken buildBuild artifacts will be generated in the build/ directory.
Execute all test suites:
aiken checkRun specific test modules:
aiken check -m stakeRun tests matching a pattern:
aiken check -m transferTests are written inline using the test keyword:
use stake/transition
test create_stake_increases_count() {
let initial = StakeDatum { stakes: [], total_stake: 0, total_count: 0 }
let result = transition.create_stake(
initial,
CreateStake { client: "c1", provider: "p1", amount: 100, user_id: "u1" }
)
result.total_count == 1
}Create HTML documentation for the contracts:
aiken docsAfter building, contract blueprints are available in:
build/packages/dagent-dagent-contractor-cardano/
- Build for target network (configure
aiken.toml) - Generate blueprint with
aiken build - Submit using Cardano CLI or transaction builder
- Reference contract hash in transactions
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Write tests for your changes
- Ensure all tests pass (
aiken check) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow Aiken style guidelines
- Add tests for new functionality
- Document public functions
- Keep validators focused and minimal
- Aiken Documentation - Learn Aiken language
- Cardano Developers - Cardano development resources
- Plutus - Plutus platform documentation
- Aiken Standard Library - Built-in functions reference