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

Contract
Address
Status

InferencePayment

0x4361115359E5C0a25c9b2f8Bb71184F010b768ea

✅ Deployed

NCNToken

0x38E2565e8905BeAf83C34b266592465C22A2f108

✅ Deployed

ValidatorRegistry

0x4F11D4cBE08A5aBa052C368C8477412af63a56f3

✅ Deployed

Network Configuration

Setting
Value

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

Initiates a payment request with escrow.

Parameters:

  • requestId - Unique request identifier

  • gateway - Gateway address

  • computeNode - Compute node address

  • computePrice - Payment to compute node (wei)

  • gatewayGas - Payment to gateway (wei)

  • validatorReward - Payment to validators (wei)

  • treasuryFee - Protocol fee (wei)

  • expiry - Expiry timestamp

  • validatorSignatures - M-of-N validator signatures

completeInferenceRequest

Completes a request and distributes payment.

Parameters:

  • requestId - Request to complete

  • computeSignature - Compute node's result signature

  • resultHash - SHA256 hash of result

  • validatorSignatures - Completion validator signatures

handleExpiredRequest

Refunds expired requests.

Events


Integration

JavaScript/TypeScript

Rust (ethers-rs)


Security

Implemented Protections

Protection
Description

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

  1. Private Key Management: Never expose private keys

  2. Signature Replay: Unique request IDs prevent replay

  3. Front-running: Time-bound signatures mitigate

  4. Oracle Dependency: Validators provide trust

  5. Upgrade Path: Consider proxy patterns for mainnet


Testing

Run Tests

Test Coverage

Test Files

File
Coverage

InferencePayment.t.sol

95%

NCNValidatorRegistry.t.sol

90%

SignatureDebug.t.sol

Helper tests


Gas Costs

Estimated Gas Usage

Function
Gas (approx)

initiateInferenceRequest

~150,000

completeInferenceRequest

~200,000

handleExpiredRequest

~80,000

Optimization Tips

  1. Batch multiple requests

  2. Use calldata for signatures

  3. Minimize storage writes

  4. Consider L2 deployment


Contract Verification

Verify on Etherscan


Last updated