Skip to content

Latest commit

 

History

History
222 lines (161 loc) · 5.79 KB

File metadata and controls

222 lines (161 loc) · 5.79 KB

Smart Contracts

Rust smart contracts for Agentis built with the Odra Framework v2.4.0 on the Casper Network.


Prerequisites

  • Rust (latest stable toolchain)

    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    rustup update stable
  • wasm32 target (required for Casper contracts)

    rustup target add wasm32-unknown-unknown
  • WASM tools (binaryen & wabt for optimization)

    # Run from project root
    ./setup.sh
    
    # Add to PATH (printed by script)
    export PATH="$(pwd)/tools/binaryen/bin:$(pwd)/tools/wabt/bin:$PATH"
  • Casper Client (optional, for manual deployments)

    cargo install casper-client

Contracts Overview

Contract File Description
AgentNFT src/agent_nft.rs CEP-78 NFT for agent identity, metadata, and leveling
AgentMarketplace src/agent_marketplace.rs Trustless escrow for agent trading
AgentCredits src/agent_credits.rs CEP-18 fungible tokens for session credits
RevenueShare src/revenue_share.rs Automated 80/20 revenue splitting

Build

Build all contracts to WASM:

cargo odra build

Output files are generated in wasm/ directory.


Test

Run the test suite:

cargo odra test

For verbose output:

cargo odra test -- --nocapture

Deploy

Local (OdraMock)

For local testing with the Odra mock VM:

cargo run --bin deploy_local --features local

Testnet

Deploy to Casper Testnet:

  1. Create environment file:

    cp .env.example .env

    Edit .env with your values:

    ODRA_CASPER_LIVENET_NODE_ADDRESS=https://node.testnet.casper.network/rpc
    ODRA_CASPER_LIVENET_CHAIN_NAME=casper-test
    ODRA_CASPER_LIVENET_SECRET_KEY_PATH=./keys/secret_key.pem
    ODRA_CASPER_LIVENET_EVENTS_URL=https://node.testnet.casper.network/events/main
  2. Generate keys (if you don't have them):

    casper-client keygen ./keys
  3. Fund your account with testnet CSPR from the faucet

  4. Run deployment:

    cargo run --bin deploy_testnet --features livenet
  5. Save the output — the script prints deployed contract hashes needed for frontend configuration.


Contract Addresses

After deployment, update the frontend src/constants/contracts.ts with your contract package hashes:

export const CONTRACTS = {
  network: "testnet",
  chainName: "casper-test",
  rpcUrl: "https://node.testnet.casper.network/rpc",

  addresses: {
    AgentNFT: "your-package-hash-here",
    AgentMarketplace: "your-package-hash-here",
    AgentCredits: "your-package-hash-here",
    RevenueShare: "your-package-hash-here",
  },
} as const;

Deploy Script Binaries

Binary Feature Description
deploy_local local Deploy to OdraMock for testing
deploy_testnet livenet Deploy to Casper Testnet/Mainnet
ai_nft_casper_cli CLI for contract interactions
ai_nft_casper_build_contract Build WASM binaries
ai_nft_casper_build_schema Generate contract schemas

Post-Deployment Setup

After deploying all contracts, configure inter-contract references:

  1. Set RevenueShare on AgentCredits:

    casper-client put-deploy \
      --node-address $CASPER_NODE_URL \
      --chain-name $CASPER_CHAIN_NAME \
      --secret-key $CASPER_SECRET_KEY \
      --payment-amount 1000000000 \
      --session-hash $CREDITS_CONTRACT_HASH \
      --session-entry-point "set_revenue_share_contract" \
      --session-arg "new_contract:key='$REVENUE_SHARE_CONTRACT_HASH'"
  2. Set AgentNFT on RevenueShare:

    casper-client put-deploy \
      --node-address $CASPER_NODE_URL \
      --chain-name $CASPER_CHAIN_NAME \
      --secret-key $CASPER_SECRET_KEY \
      --payment-amount 1000000000 \
      --session-hash $REVENUE_SHARE_CONTRACT_HASH \
      --session-entry-point "set_agent_nft" \
      --session-arg "new_agent_nft:key='$NFT_CONTRACT_HASH'"
  3. Set Authorized Spender (backend wallet for credit operations):

    casper-client put-deploy \
      --node-address $CASPER_NODE_URL \
      --chain-name $CASPER_CHAIN_NAME \
      --secret-key $CASPER_SECRET_KEY \
      --payment-amount 1000000000 \
      --session-hash $CREDITS_CONTRACT_HASH \
      --session-entry-point "set_authorized_spender" \
      --session-arg "spender:key='account-hash-$BACKEND_ACCOUNT_HASH'" \
      --session-arg "authorized:bool='true'"

Project Structure

contract/
├── src/
│   ├── lib.rs                  # Module exports
│   ├── agent_nft.rs            # NFT contract
│   ├── agent_marketplace.rs    # Trading contract
│   ├── agent_credits.rs        # Credits contract
│   ├── revenue_share.rs        # Revenue routing
│   └── tests.rs                # Unit tests
├── bin/
│   ├── build_contract.rs       # WASM build script
│   ├── build_schema.rs         # Schema generator
│   ├── cli.rs                  # CLI interface
│   ├── deploy_local.rs         # Local deployment
│   └── deploy_testnet.rs       # Testnet deployment
├── Cargo.toml                  # Dependencies
└── Odra.toml                   # Contract definitions