Payment Flow

This document details the blockchain-based payment system in NCN Network v2, including fee calculation, transaction flow, and smart contract interactions.


Overview

NCN Network uses an escrow-based payment model where clients deposit funds before computation, and payments are distributed after validated completion.

┌─────────────────────────────────────────────────────────────────────────────┐
│                           Payment Flow Overview                              │
│                                                                             │
│   1. Request        2. Validate      3. Pay           4. Compute           │
│   ┌─────────┐       ┌─────────┐      ┌─────────┐      ┌─────────┐          │
│   │ Client  │──────▶│Validators│─────▶│Blockchain│─────▶│ Compute │          │
│   │         │       │ (M-of-N) │      │ (Escrow) │      │  Node   │          │
│   └─────────┘       └─────────┘      └─────────┘      └─────────┘          │
│                                                              │              │
│   8. Result         7. Validate      6. Complete     5. Sign │              │
│   ┌─────────┐       ┌─────────┐      ┌─────────┐      ┌──────▼──┐          │
│   │ Client  │◀──────│Validators│◀─────│Blockchain│◀─────│ Result  │          │
│   │         │       │ (M-of-N) │      │(Distribute)│     │         │          │
│   └─────────┘       └─────────┘      └─────────┘      └─────────┘          │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

Payment Tree

Every inference request has an associated Payment Tree that defines how funds are distributed:

Structure

Distribution Breakdown

Recipient
Purpose
Typical %

Compute Node

Payment for model execution

80%

Gateway

Gas costs and routing fee

10%

Validators

Consensus participation reward

5%

Treasury

Protocol development fund

5%

Example Payment Tree

For a 1 ETH total payment:


Fee Calculation

Compute Price

The compute price is calculated based on:

Factor
Description
Example

base_price

Minimum fee per request

0.001 ETH

token_count

Input/output tokens

1000 tokens

price_per_token

Per-token rate

0.0001 ETH

execution_time

Estimated compute time

5 seconds

time_rate

Per-second rate

0.001 ETH/s

Gateway Gas

Covers:

  • Transaction submission

  • Event monitoring

  • Result relay

Validator Reward

Split equally among participating validators.

Treasury Fee

Fixed percentage (default: 5%) for protocol development.


Payment Lifecycle

Phase 1: Fee Estimation

Before payment, clients can estimate fees:

Response includes:

  • fee_wei - Total estimated fee

  • fee_quote_id - Valid for 10 minutes

  • config_hash - Anti-tampering hash

  • expires_at - Quote expiration

Phase 2: Preprocessing Validation

Validators verify request setup:

Validators verify:

  • Gateway is registered and reputable

  • Compute node is available and capable

  • Payment amounts are correctly calculated

  • Request parameters are valid

Phase 3: Payment Initiation

Client pays on blockchain:

Smart contract verifies:

  • M-of-N validator signatures

  • Correct payment amount

  • Request not expired

  • Token allowance sufficient

Phase 4: Execution

After payment confirmation, compute proceeds:

Phase 5: Completion Validation

Validators verify computation:

Validators verify:

  • Compute node signature is valid

  • Result format is correct

  • Computation time is reasonable

  • Result hash matches

Phase 6: Payment Distribution

Gateway finalizes on blockchain:


Smart Contract Interface

InferencePayment Contract

initiateInferenceRequest

Requirements:

  • Caller has approved token spend

  • M-of-N validator signatures valid

  • Expiry timestamp in future

  • Request ID not already used

completeInferenceRequest

Requirements:

  • Request exists and is pending

  • Compute node signature valid

  • M-of-N validator signatures valid

  • Called by original gateway

handleExpiredRequest

Behavior:

  • If request expired, refund to original payer

  • Anyone can call (gas incentive)

Events


Failure Handling

Compute Failure

If computation fails:

  1. Gateway calls markRequestFailed (admin function)

  2. Or request expires after timeout

  3. Client receives automatic refund

Validator Rejection

If validators reject completion:

  1. Gateway cannot call completeInferenceRequest

  2. Request expires

  3. Client refunded automatically

Transaction Failure

If blockchain transaction fails:

  1. Gateway retries with exponential backoff

  2. After max retries, request marked failed

  3. Client refunded if funds escrowed


Security Considerations

Double-Spend Prevention

  • Request IDs are unique (UUID v4)

  • Each request ID can only be initiated once

  • Completion only possible for initiated requests

Signature Verification

  • All signatures verified on-chain

  • ECDSA signatures with secp256k1

  • Validators must be registered in contract

Expiry Protection

  • Preprocessing validations expire (10 min)

  • Payment requests expire (configurable, default 1 hour)

  • Automatic refund after expiry

Reentrancy Protection

  • ReentrancyGuard on all payment functions

  • Checks-Effects-Interactions pattern

  • No external calls before state updates


Token Flow Diagram


Configuration

Gateway Payment Settings

Pricing Configuration


Monitoring

Key Metrics

Metric
Description

payments_initiated_total

Total payments initiated

payments_completed_total

Successfully distributed

payments_refunded_total

Refunded payments

payment_amount_wei_total

Total wei processed

payment_distribution_time_ms

Time to distribute

Events to Monitor


Next Steps

Last updated