Smart Contracts
NCN Network v2 uses Solidity smart contracts deployed on Ethereum-compatible blockchains for payment handling and validator management.
Overview
┌─────────────────────────────────────────────────────────────────────────────┐
│ NCN Smart Contracts │
│ │
│ ┌───────────────────────────────────────────────────────────────────┐ │
│ │ InferencePayment Contract │ │
│ │ │ │
│ │ • Escrow-based payment system │ │
│ │ • Validator signature verification │ │
│ │ • Multi-party payment distribution │ │
│ │ • Automatic refunds on expiry │ │
│ │ │ │
│ │ Functions: │ │
│ │ - initiateInferenceRequest() │ │
│ │ - completeInferenceRequest() │ │
│ │ - handleExpiredRequest() │ │
│ │ - markRequestFailed() │ │
│ │ │ │
│ └───────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────────────────────────────────────────┐ │
│ │ NCNToken Contract │ │
│ │ │ │
│ │ • ERC20 token for payments │ │
│ │ • Standard transfer/approve functions │ │
│ │ │ │
│ └───────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────────────────────────────────────────┐ │
│ │ NCNValidatorRegistry Contract │ │
│ │ │ │
│ │ • On-chain validator registration │ │
│ │ • Stake management │ │
│ │ • Slashing functionality │ │
│ │ │ │
│ └───────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘Deployed Contracts
Forknet Testnet
InferencePayment
0x4361115359E5C0a25c9b2f8Bb71184F010b768ea
✅ Deployed
NCNToken
0x38E2565e8905BeAf83C34b266592465C22A2f108
✅ Deployed
ValidatorRegistry
0x4F11D4cBE08A5aBa052C368C8477412af63a56f3
✅ Deployed
Network Configuration
Network
Forknet Testnet
Chain ID
828 (Agglayer connected)
RPC URL
https://testnet-rpc-1.forknet.io
Contract Documentation
Main payment contract:
Payment initiation with validator signatures
Payment distribution on completion
Expiry handling and refunds
Admin functions
ERC20 payment token:
Standard ERC20 interface
Testnet faucet (if available)
On-chain validator management:
Validator registration with stake
Slashing mechanism
Stake withdrawal
Contract deployment guide:
Local deployment
Testnet deployment
Mainnet considerations
Contract ABI documentation:
Function signatures
Event definitions
Type encodings
Quick Start
Prerequisites
Install Foundry:
Build Contracts
Deploy to Local Network
InferencePayment Contract
Core Functions
initiateInferenceRequest
initiateInferenceRequestInitiates a payment request with escrow.
Parameters:
requestId- Unique request identifiergateway- Gateway addresscomputeNode- Compute node addresscomputePrice- Payment to compute node (wei)gatewayGas- Payment to gateway (wei)validatorReward- Payment to validators (wei)treasuryFee- Protocol fee (wei)expiry- Expiry timestampvalidatorSignatures- M-of-N validator signatures
completeInferenceRequest
completeInferenceRequestCompletes a request and distributes payment.
Parameters:
requestId- Request to completecomputeSignature- Compute node's result signatureresultHash- SHA256 hash of resultvalidatorSignatures- Completion validator signatures
handleExpiredRequest
handleExpiredRequestRefunds expired requests.
Events
Integration
JavaScript/TypeScript
Rust (ethers-rs)
Security
Implemented Protections
ReentrancyGuard
Prevents reentrancy attacks
Signature Verification
ECDSA signature validation
Expiry Checks
Time-bound requests
Access Control
Function-level permissions
Pausable
Emergency pause capability
Security Considerations
Private Key Management: Never expose private keys
Signature Replay: Unique request IDs prevent replay
Front-running: Time-bound signatures mitigate
Oracle Dependency: Validators provide trust
Upgrade Path: Consider proxy patterns for mainnet
Testing
Run Tests
Test Coverage
Test Files
InferencePayment.t.sol
95%
NCNValidatorRegistry.t.sol
90%
SignatureDebug.t.sol
Helper tests
Gas Costs
Estimated Gas Usage
initiateInferenceRequest
~150,000
completeInferenceRequest
~200,000
handleExpiredRequest
~80,000
Optimization Tips
Batch multiple requests
Use calldata for signatures
Minimize storage writes
Consider L2 deployment
Contract Verification
Verify on Etherscan
Related Documentation
Inference Payment - Detailed payment contract docs
Deployment - Deployment guide
ABI Reference - Contract ABIs
Payment Flow - Payment architecture
Last updated
