ATP CAIP
A Rust implementation of Chain Agnostic Improvement Proposals (CAIP) standards for blockchain interoperability. This crate provides standardized identifiers and primitives for working with multiple blockchain networks in a chain-agnostic manner.
Overview
ATP CAIP implements the following CAIP standards:
- CAIP-2: Chain ID specification for blockchain identification
- CAIP-10: Account ID specification for blockchain account identification
- CAIP-19: Asset ID specification for blockchain asset identification
The crate enables developers to build cross-chain applications by providing:
- Standardized identifiers for chains, accounts, and assets
- Type-safe financial primitives with decimal precision
- Trading pair representations for cross-chain and same-chain swaps
- Comprehensive error handling and validation
- Serialization support for configuration and API integration
Quick Start
use ;
use FromStr;
// Create chain identifiers
let ethereum = new?;
let solana = new?;
// Create asset identifiers
let eth = new?;
let usdc = new?;
let sol = new?;
// Create money amounts with proper decimals
let eth_amount = from_decimal_str?;
let usdc_amount = from_decimal_str?;
// Create trading pairs
let eth_usdc_pair = new;
let cross_chain_pair = new;
println!; // "1.5"
println!; // true
Core Types
Chain Identifiers (CAIP-2)
Chain IDs follow the format namespace:reference and identify specific blockchain networks:
use ChainId;
// Ethereum mainnet
let ethereum = new?;
assert_eq!;
// Solana mainnet
let solana = new?;
assert_eq!;
// Parse from string
let chain = from_str?;
assert_eq!;
assert_eq!;
Asset Identifiers (CAIP-19)
Asset IDs follow the format chain_id/asset_namespace:asset_reference:
use ;
let ethereum = new?;
// Native ETH
let eth = new?;
assert_eq!;
// USDC token
let usdc = new?;
assert_eq!;
// Chain-agnostic asset identifier
let asset_base = new?;
assert_eq!;
Account Identifiers (CAIP-10)
Account IDs follow the format chain_id:account_address:
use ;
let ethereum = new?;
let account = new?;
assert_eq!;
assert_eq!;
Financial Primitives
Money Type
The Money type provides precise decimal arithmetic for cryptocurrency amounts:
use Money;
use U256;
// Create from decimal string (human-readable)
let eth_amount = from_decimal_str?;
assert_eq!;
// Create from raw amount (base units)
let wei_amount = from_raw?;
assert_eq!;
// Arithmetic operations
let amount1 = from_decimal_str?;
let amount2 = from_decimal_str?;
let sum = amount1.add?;
assert_eq!;
let difference = amount1.sub?;
assert_eq!;
// Percentage calculations
let fee = amount1.percentage; // 3%
let basis_points_fee = amount1.basis_points; // 2.5%
// Convert to floating point (use with caution)
let float_value = amount1.to_f64;
// Check if zero
assert!;
assert!;
Asset Type
Combines an asset identifier with a money amount:
use ;
let ethereum = new?;
let eth_id = new?;
let eth_amount = from_decimal_str?;
let asset = new;
// Calculate USD value given price per token
let usd_value = asset.usd_value; // $3000 per ETH
assert_eq!; // 2.5 * 3000
Token Pairs
Represent trading relationships between two assets:
use ;
let ethereum = new?;
let solana = new?;
let eth = new?;
let sol = new?;
let pair = new;
// Check if cross-chain
assert!;
// Check if pair involves specific asset
assert!;
// Get the other asset in the pair
assert_eq!;
// Create reverse pair
let reverse_pair = pair.reverse;
assert_eq!;
assert_eq!;
// String representation
assert_eq!;
// Parse from string
let parsed_pair = from_pair_string?;
assert_eq!;
Namespace Enums
Predefined enums for common blockchain and asset namespaces:
use ;
// Chain namespaces
let evm_chains = Eip155;
let solana_chains = Solana;
let cosmos_chains = Cosmos;
assert_eq!;
// Asset namespaces
let native_tokens = Slip44;
let erc20_tokens = Erc20;
let nfts = Erc721;
let solana_tokens = Spl;
assert_eq!;
Cryptographic Curves
Support for different cryptographic signature algorithms:
use Curve;
let ethereum_curve = Secp256k1;
let solana_curve = Ed25519;
Error Handling
Comprehensive error types for validation and parsing failures:
use ;
// Invalid chain ID format
match new
// Invalid asset ID format
match from_str
// Decimal overflow
match new
// Invalid amount format
match from_decimal_str
Validation
All identifiers are validated against CAIP specification regex patterns:
- Chain ID:
^([-a-z0-9]{3,8}):([-a-zA-Z0-9]{1,32})$ - Account ID:
^([-a-z0-9]{3,8}):([-a-zA-Z0-9]{1,32}):([-a-zA-Z0-9]{1,128})$ - Asset ID:
^([-a-z0-9]{3,8}):([-a-zA-Z0-9]{1,32})/([-a-z0-9]{3,8}):([-a-zA-Z0-9]{1,64})$ - Asset ID Base:
^([-a-z0-9]{3,8}):([-a-zA-Z0-9]{1,64})$
Integration with ATP Chain Registry
This crate is designed to work seamlessly with atp-chain-registry:
// The chain registry uses CAIP types for configuration
use ChainRegistry;
use ;
let registry = default?;
// Find trading routes between assets
let from_asset = from_str?; // ETH
let to_asset = from_str?; // SOL
let routes = registry.find_trading_routes;
for route in routes
// Get chains supporting specific cryptographic curves
let secp256k1_chains = registry.get_chains_by_curve;
Serialization
All types support Serde serialization for configuration files and APIs:
use ;
use serde_json;
let chain = new?;
let json = to_string?;
println!; // "eip155:1"
let asset = new?;
let toml = to_string?;
// TokenPair with trading configuration
let mut pair = new;
pair.fee_percentage = Some;
pair.min_trade_amount = Some;
let config_json = to_string_pretty?;
Testing
Run the test suite to verify functionality:
The crate includes comprehensive tests covering:
- CAIP identifier validation and parsing
- Money arithmetic operations and edge cases
- TokenPair creation and manipulation
- Error handling scenarios
- Serialization round-trips
Examples
Cross-Chain Trading Scenario
use ;
// Set up chains
let ethereum = new?;
let solana = new?;
// Set up assets
let eth = new?;
let sol = new?;
// Create asset holdings
let eth_holding = new;
let sol_holding = new;
// Create trading pair
let trading_pair = new;
// Calculate portfolio value
let eth_price = 3000.0; // $3000 per ETH
let sol_price = 150.0; // $150 per SOL
let total_value = eth_holding.usd_value + sol_holding.usd_value;
println!; // $22,500.00
println!;
println!;
Multi-Chain Asset Management
use ;
use HashMap;
// Define supported chains
let chains = vec!;
// Define USDC on different chains
let mut usdc_assets = new;
// USDC on Ethereum
usdc_assets.insert;
// USDC on Polygon
usdc_assets.insert;
// USDC on Solana
usdc_assets.insert;
// Create balances
let mut balances = new;
balances.insert;
balances.insert;
balances.insert;
// Calculate total USDC across all chains
let total_usdc = balances.values
.try_fold?;
println!; // 2251.5
Dependencies
ethers-core: For U256 big integer supportserde: For serialization/deserializationregex: For CAIP format validationthiserror: For error handlinglazy_static: For compiled regex patterns
CAIP Standards References
License
This project is part of the SolverOS ecosystem. See the main repository for license information.