Micropayment Channels
Off-chain state channels for high-frequency, low-value payments. Essential for per-token AI billing and streaming payment use cases.
Overview
Micropayment channels allow two parties to exchange many small payments off-chain, settling only the final balance on-chain. This reduces gas costs and enables real-time per-token billing for AI inference.
Channel Lifecycle
- Open: Sender deposits TNZO into escrow, creates channel
- Update: Off-chain state updates with signed balance changes
- Close: Either party submits final state on-chain
- Settle: Escrowed funds distributed per final state
Key Properties
- Unlimited off-chain updates during channel lifetime
- On-chain settlement only on channel close
- Nonce-based ordering prevents replay of old states
- Time-locked dispute resolution window
- Automatic close on channel expiry
Use Case: Per-Token AI Billing
When a user requests AI inference, a micropayment channel is opened with an initial TNZO deposit. As tokens stream from the model, the channel state is updated with each token, charging the exact amount. When inference completes, the channel is closed and the final balance settles on-chain.
// Per-token billing flow 1. User opens channel: deposit 10 TNZO 2. Model generates token 1: state update → 9.999 TNZO remaining 3. Model generates token 2: state update → 9.998 TNZO remaining ... (hundreds of updates, all off-chain) 4. Inference complete: close channel 5. On-chain settlement: user receives 8.5 TNZO refund Provider receives 1.5 TNZO payment
Code Examples
Open a Channel
use tenzro_settlement::MicropaymentChannelManager;
let manager = MicropaymentChannelManager::new(storage);
// Open channel with 10 TNZO deposit
let channel = manager.open_channel(
sender,
receiver,
Amount::tnzo(10.0),
Duration::from_secs(3600), // 1 hour TTL
)?;
println!("Channel ID: {}", channel.id);
println!("Escrowed: {} TNZO", channel.escrowed_amount);Update State (Off-Chain)
// Each token generation triggers a state update
let new_balance = current_balance - token_cost;
let nonce = nonce + 1;
// Both parties sign the new state
let signature = wallet.sign_state_update(
&channel.id,
new_balance,
nonce,
)?;
manager.update_state(
&channel.id,
new_balance,
nonce,
signature,
)?;Close and Settle
// Close channel and settle on-chain
let receipt = manager.close_channel(&channel.id)?;
println!("Settlement tx: {}", receipt.tx_hash);
println!("Sender refund: {} TNZO", receipt.sender_amount);
println!("Receiver payment: {} TNZO", receipt.receiver_amount);
println!("Network fee: {} TNZO", receipt.fee);Channel Parameters
| Parameter | Default |
|---|---|
| Min Deposit | 0.1 TNZO |
| Max Duration | 24 hours |
| Dispute Window | 1 hour |
| Network Fee | 0.5% |