You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Input: A→B: 100, B→C: 50, C→A: 30
Output: 3 net transfers (one per pair)
Key Properties
✅ Deterministic - Same input = same output
✅ Order-independent - Any order = same result
✅ Fee-preserving - All fees collected exactly
✅ Secure - Duplicate prevention, overflow protection
✅ Efficient - 50-100% gas savings
Best Practices
Batch size: 10-30 remittances optimal
Grouping: Group same-party pairs for max netting
Validation: Check status before batching
Monitoring: Track events for verification
Error handling: Handle all error codes
Events Emitted
// Per net transferemit_settlement_completed(sender, recipient, token, amount)// Per remittanceemit_remittance_completed(id, sender, agent, token, amount)
Common Patterns
Pattern 1: Validate Before Batch
// Check all remittances are pendingfor id in ids {let rem = get_remittance(&env, id)?;if rem.status != RemittanceStatus::Pending{returnErr(ContractError::InvalidStatus);}}
Pattern 2: Remove Duplicates
// Deduplicate IDsletmut seen = Vec::new(&env);letmut unique = Vec::new(&env);for entry in entries {if !seen.contains(&entry.remittance_id){
seen.push_back(entry.remittance_id);
unique.push_back(entry);}}
Pattern 3: Calculate Efficiency
fnnetting_efficiency(original:u32,net:u32) -> f64{((original - net)asf64 / original asf64)*100.0}
Gas per transfer: ~30,000 units
Batch overhead: ~50,000 units
Typical savings: 50-90%
Max batch size: 50 remittances
Testing
# Run net settlement tests
cargo test net_settlement
# Run specific test
cargo test test_net_settlement_simple_offset
# Run with output
cargo test -- --nocapture
Documentation
Full guide: NET_SETTLEMENT.md
API reference: NET_SETTLEMENT_API.md
Examples: examples/net-settlement-example.js
Summary: NET_SETTLEMENT_SUMMARY.md
Security Checklist
Duplicate prevention
Overflow protection
Authorization checks
Expiry validation
Fee preservation
Pause mechanism
Limits
Limit
Value
Min batch size
1
Max batch size
50
Max remittance amount
i128::MAX
Fee range
0-10000 bps
Algorithm Complexity
Operation
Complexity
Netting
O(n)
Validation
O(n)
Execution
O(m) where m ≤ n
Quick Checks
// Is batch valid?assert!(entries.len() > 0 && entries.len() <= 50);// Are IDs unique?let unique_count = entries.iter().collect::<HashSet<_>>().len();assert_eq!(unique_count, entries.len());// Are all pending?for entry in entries {let rem = get_remittance(&env, entry.remittance_id)?;assert_eq!(rem.status,RemittanceStatus::Pending);}
Version
Implementation: v1.0.0
Contract: Check get_version()
Last updated: 2026-02-20
Need more details? See full documentation in NET_SETTLEMENT.md