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
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:
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 feefee_quote_id- Valid for 10 minutesconfig_hash- Anti-tampering hashexpires_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
initiateInferenceRequestRequirements:
Caller has approved token spend
M-of-N validator signatures valid
Expiry timestamp in future
Request ID not already used
completeInferenceRequest
completeInferenceRequestRequirements:
Request exists and is pending
Compute node signature valid
M-of-N validator signatures valid
Called by original gateway
handleExpiredRequest
handleExpiredRequestBehavior:
If request expired, refund to original payer
Anyone can call (gas incentive)
Events
Failure Handling
Compute Failure
If computation fails:
Gateway calls
markRequestFailed(admin function)Or request expires after timeout
Client receives automatic refund
Validator Rejection
If validators reject completion:
Gateway cannot call
completeInferenceRequestRequest expires
Client refunded automatically
Transaction Failure
If blockchain transaction fails:
Gateway retries with exponential backoff
After max retries, request marked failed
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
ReentrancyGuardon all payment functionsChecks-Effects-Interactions pattern
No external calls before state updates
Token Flow Diagram
Configuration
Gateway Payment Settings
Pricing Configuration
Monitoring
Key Metrics
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
Validation & Consensus - Validator mechanism
Smart Contracts - Contract details
Security Model - Security architecture
Last updated
