Files
aitbc/docs/scenarios/49_cross_chain_operations.md
aitbc 7f71d8a6c7
Some checks failed
Cross-Node Transaction Testing / transaction-test (push) Has been cancelled
Deploy to Testnet / deploy-testnet (push) Has been cancelled
Documentation Validation / validate-docs (push) Has been cancelled
Documentation Validation / validate-policies-strict (push) Has been cancelled
Multi-Node Stress Testing / stress-test (push) Has been cancelled
Add CLI scenario coverage for crosschain, monitor, resource, simulate commands and update existing scenarios with advanced subcommands
2026-05-27 09:13:49 +02:00

708 lines
18 KiB
Markdown

# Cross-Chain Operations for hermes Agents
**Level**: Intermediate
**Prerequisites**: Basic CLI knowledge, AITBC CLI installed, wallet basics (Scenario 01)
**Estimated Time**: 25 minutes
**Last Updated**: 2026-05-27
**Version**: 1.0
## 🧭 **Navigation Path:**
**🏠 [Documentation Home](../README.md)** → **🎭 [Agent Scenarios](./README.md)** → *You are here*
**breadcrumb**: Home → Scenarios → Cross-Chain Operations
---
## 🎯 **See Also:**
- **📖 Previous Scenario**: [48 Configuration Profiles](./48_config_profiles.md)
- **📖 Next Scenario**: [50 Workflow Management](./50_workflow_management.md)
- **📖 Related**: [20 Cross Chain Transfer](./20_cross_chain_transfer.md)
- **📖 Related**: [47 Cross Chain Atomic Swap](./47_cross_chain_atomic_swap.md)
- **⚙️ Cross-Chain Documentation**: [CLI Cross-Chain Commands](../cli/CLI_DOCUMENTATION.md)
---
## 📚 **Scenario Overview**
This scenario demonstrates how hermes agents use cross-chain operations to trade tokens across multiple blockchain networks. Cross-chain functionality enables agents to move assets between different AITBC chains, access liquidity across networks, and participate in multi-chain trading strategies.
### **Use Case**
An hermes agent needs to:
- Exchange tokens between different blockchain networks
- Bridge assets across chains securely
- Monitor cross-chain swap and bridge status
- Access cross-chain liquidity pools
- Track cross-chain trading statistics
### **What You'll Learn**
- Query cross-chain exchange rates
- Create cross-chain swaps with slippage protection
- Monitor swap and bridge status
- Create cross-chain bridge transactions
- View liquidity pools and trading statistics
- Understand cross-chain transaction lifecycle
### **Features Combined**
- **Exchange Rates**: Real-time cross-chain rate queries
- **Atomic Swaps**: Secure token swaps with slippage tolerance
- **Bridge Operations**: Asset bridging across chains
- **Status Tracking**: Monitor transaction progress
- **Liquidity Pools**: View available cross-chain liquidity
- **Statistics**: Track cross-chain trading volume and metrics
---
## 📋 **Prerequisites**
### **Knowledge Required**
- Basic command-line interface usage
- Understanding of blockchain transactions
- AITBC CLI installed and accessible
- Wallet basics (create, list, balance) from Scenario 01
### **System Requirements**
- AITBC CLI installed
- Wallet with AIT tokens (for actual swaps)
- Exchange service running (for live operations)
- Network connectivity to exchange service
### **Setup Required**
- AITBC CLI configured with exchange service URL
- Wallet created and funded (for live operations)
- Config file with `exchange_service_url` set
---
## 🚀 **Quick Start**
```bash
# Query cross-chain exchange rates
aitbc cross-chain rates
# Create a cross-chain swap
aitbc cross-chain swap --from-chain ait-testnet --to-chain ait-devnet \
--from-token AIT --to-token AIT --amount 100
# Check swap status
aitbc cross-chain status <swap_id>
# Create a bridge transaction
aitbc cross-chain bridge --source-chain ait-testnet --target-chain ait-devnet \
--token AIT --amount 50
# View liquidity pools
aitbc cross-chain pools
# View trading statistics
aitbc cross-chain stats
```
---
## 📖 **Detailed Steps**
### Step 1: Query Cross-Chain Exchange Rates
View available exchange rates between supported chains:
```bash
# View all available rates
aitbc cross-chain rates
```
**Expected Output:**
```
Cross-chain exchange rates:
+--------------+--------------+------------+
| From Chain | To Chain | Rate |
+==============+==============+============+
| ait-testnet | ait-devnet | 1.000000 |
| ait-testnet | ait-mainnet | 0.950000 |
| ait-devnet | ait-mainnet | 0.950000 |
+--------------+--------------+------------+
```
**Query specific rate:**
```bash
aitbc cross-chain rates --from-chain ait-testnet --to-chain ait-devnet
```
**Expected Output:**
```
Exchange rate ait-testnet → ait-devnet: 1.0
```
**What happens:**
- CLI queries exchange service for current rates
- Rates are returned in a table format
- Specific pair queries return single rate value
### Step 2: Create Cross-Chain Swap
Execute an atomic swap between chains:
```bash
aitbc cross-chain swap \
--from-chain ait-testnet \
--to-chain ait-devnet \
--from-token AIT \
--to-token AIT \
--amount 100 \
--slippage 0.01 \
--address ait1abc123...
```
**Expected Output:**
```
Cross-chain swap created successfully!
Swap ID: swap_abc123def456
From Chain: ait-testnet
To Chain: ait-devnet
Amount: 100.0
Expected Amount: 99.0
Rate: 0.99
Total Fees: 1.0
Status: pending
```
**Parameters explained:**
- `--from-chain`: Source blockchain network
- `--to-chain`: Target blockchain network
- `--from-token`: Token to swap from
- `--to-token`: Token to swap to
- `--amount`: Amount to swap
- `--slippage`: Slippage tolerance (default 0.01 = 1%)
- `--address`: Recipient wallet address
- `--min-amount`: Minimum amount to receive (auto-calculated if not set)
**What happens:**
- Swap transaction created on source chain
- Funds locked in smart contract
- Atomic swap mechanism ensures security
- Swap ID returned for tracking
### Step 3: Check Swap Status
Monitor swap execution progress:
```bash
aitbc cross-chain status swap_abc123def456
```
**Expected Output:**
```
Swap Status: executing
Swap ID: swap_abc123def456
From Chain: ait-testnet
To Chain: ait-devnet
From Token: AIT
To Token: AIT
Amount: 100.0
Expected Amount: 99.0
Actual Amount: 99.0
Status: executing
Created At: 2026-05-27 08:30:45
Completed At: -
Bridge Fee: 0.5
From Tx Hash: 0xabc123...
To Tx Hash: -
```
**Status meanings:**
- `pending`: Swap initiated, awaiting execution
- `executing`: Swap in progress
- `completed`: Swap finished successfully
- `failed`: Swap failed (check error_message)
- `refunded`: Swap refunded due to failure
### Step 4: List Cross-Chain Swaps
View historical swap transactions:
```bash
# List all swaps
aitbc cross-chain swaps
# Filter by user address
aitbc cross-chain swaps --user-address ait1abc123...
# Filter by status
aitbc cross-chain swaps --status completed
# Limit results
aitbc cross-chain swaps --limit 5
```
**Expected Output:**
```
Found 3 cross-chain swaps:
+----------+--------------+--------------+---------+-----------+---------------------+
| ID | From | To | Amount | Status | Created |
+==========+==============+==============+=========+===========+=====================+
| swap_abc | ait-testnet | ait-devnet | 100.0 | completed | 2026-05-27 08:30:45 |
| swap_def | ait-devnet | ait-mainnet | 50.0 | pending | 2026-05-27 09:15:20 |
| swap_ghi | ait-testnet | ait-mainnet | 200.0 | failed | 2026-05-27 10:00:00 |
+----------+--------------+--------------+---------+-----------+---------------------+
```
### Step 5: Create Cross-Chain Bridge
Bridge assets directly between chains:
```bash
aitbc cross-chain bridge \
--source-chain ait-testnet \
--target-chain ait-devnet \
--token AIT \
--amount 50 \
--recipient ait1xyz789...
```
**Expected Output:**
```
Cross-chain bridge created successfully!
Bridge ID: bridge_xyz789abc123
Source Chain: ait-testnet
Target Chain: ait-devnet
Token: AIT
Amount: 50.0
Bridge Fee: 0.5
Status: pending
```
**What happens:**
- Assets locked on source chain
- Bridge transaction initiated
- Relayer network processes transfer
- Assets released on target chain
### Step 6: Check Bridge Status
Monitor bridge transaction progress:
```bash
aitbc cross-chain bridge-status bridge_xyz789abc123
```
**Expected Output:**
```
Bridge Status: locked
Bridge ID: bridge_xyz789abc123
Source Chain: ait-testnet
Target Chain: ait-devnet
Token: AIT
Amount: 50.0
Recipient Address: ait1xyz789...
Status: locked
Created At: 2026-05-27 11:00:00
Completed At: -
Bridge Fee: 0.5
Source Tx Hash: 0xdef456...
Target Tx Hash: -
```
**Bridge status meanings:**
- `pending`: Bridge initiated
- `locked`: Assets locked on source chain
- `transferred`: Assets transferred to target chain
- `completed`: Bridge finished successfully
- `failed`: Bridge failed
### Step 7: View Liquidity Pools
Explore available cross-chain liquidity:
```bash
aitbc cross-chain pools
```
**Expected Output:**
```
Found 5 cross-chain liquidity pools:
+-----------+----------+----------+-----------+-----------+------------+------------+-------------+-------+
| Pool ID | Token A | Token B | Chain A | Chain B | Reserve A | Reserve B | Liquidity | APR |
+===========+==========+==========+===========+===========+============+============+=============+=======+
| pool_001 | AIT | AIT | testnet | devnet | 10000.00 | 10000.00 | 20000.00 | 5.2% |
| pool_002 | AIT | AIT | testnet | mainnet | 5000.00 | 4750.00 | 9750.00 | 8.1% |
+-----------+----------+----------+-----------+-----------+------------+------------+-------------+-------+
```
**What happens:**
- CLI queries available liquidity pools
- Shows token pairs and chain combinations
- Displays reserves and liquidity depth
- Shows APR for liquidity providers
### Step 8: View Trading Statistics
Track cross-chain trading metrics:
```bash
aitbc cross-chain stats
```
**Expected Output:**
```
Cross-Chain Trading Statistics:
Swap Statistics:
+-----------+-------+-----------+
| Status | Count | Volume |
+===========+=======+===========+
| completed | 150 | 50000.00 |
| pending | 5 | 2500.00 |
| failed | 2 | 100.00 |
+-----------+-------+-----------+
Bridge Statistics:
+-----------+-------+-----------+
| Status | Count | Volume |
+===========+=======+===========+
| completed | 200 | 75000.00 |
| pending | 3 | 1500.00 |
+-----------+-------+-----------+
Overall Statistics:
Total Volume: 129100.00
Supported Chains: ait-testnet, ait-devnet, ait-mainnet
Last Updated: 2026-05-27 12:00:00
```
---
## 🔧 **Advanced Usage**
### Swap with Custom Minimum Amount
Set explicit minimum receive amount:
```bash
aitbc cross-chain swap \
--from-chain ait-testnet \
--to-chain ait-devnet \
--from-token AIT \
--to-token AIT \
--amount 100 \
--min-amount 98.5 \
--slippage 0.015
```
### Batch Cross-Chain Operations
Execute multiple swaps in sequence:
```bash
#!/bin/bash
# batch_swaps.sh
SWAPS=(
"ait-testnet:ait-devnet:100"
"ait-devnet:ait-mainnet:50"
"ait-testnet:ait-mainnet:75"
)
for swap in "${SWAPS[@]}"; do
IFS=':' read -r from to amount <<< "$swap"
echo "Swapping $amount AIT from $from to $to"
aitbc cross-chain swap \
--from-chain "$from" \
--to-chain "$to" \
--from-token AIT \
--to-token AIT \
--amount "$amount"
done
```
### Monitor Swap Progress
Continuous status monitoring:
```bash
#!/bin/bash
# monitor_swap.sh
SWAP_ID=$1
while true; do
clear
aitbc cross-chain status "$SWAP_ID"
status=$(aitbc cross-chain status "$SWAP_ID" --format json | jq -r '.status')
if [ "$status" = "completed" ] || [ "$status" = "failed" ]; then
break
fi
sleep 5
done
```
---
## ⚠️ **Important Notes**
### Security Considerations
- **Slippage Protection**: Always set appropriate slippage tolerance
- **Minimum Amount**: Use `--min-amount` to protect against unfavorable rates
- **Address Verification**: Double-check recipient addresses
- **Transaction Fees**: Bridge and swap fees apply to all operations
### Transaction Lifecycle
- **Swaps**: Typically complete within 1-5 minutes
- **Bridges**: May take 5-30 minutes depending on relayer network
- **Status Polling**: Use status commands to track progress
- **Failed Transactions**: Check error messages for failure reasons
### Rate Limitations
- Rates are subject to market conditions
- Large swaps may experience slippage
- Liquidity depth affects execution speed
- Network congestion may delay transactions
### Supported Chains
- Current supported chains: ait-testnet, ait-devnet, ait-mainnet
- Additional chains may be added in future releases
- Check `aitbc cross-chain stats` for current supported chains
---
## 🐛 **Troubleshooting**
### Swap failed with insufficient liquidity
**Error:**
```
Error: Insufficient liquidity for swap
```
**Solution:**
```bash
# Check available liquidity pools
aitbc cross-chain pools
# Reduce swap amount or try different chain pair
aitbc cross-chain swap --amount 50 ... # Smaller amount
```
### Bridge stuck in pending state
**Issue:**
Bridge status shows "pending" for extended period
**Solution:**
```bash
# Check bridge status for details
aitbc cross-chain bridge-status <bridge_id>
# Verify exchange service is running
curl http://localhost:8001/health
# Check network connectivity
ping exchange-service.example.com
```
### Invalid chain ID
**Error:**
```
Error: Invalid chain ID 'invalid-chain'
```
**Solution:**
```bash
# View supported chains
aitbc cross-chain stats
# Use valid chain ID from supported list
aitbc cross-chain swap --from-chain ait-testnet --to-chain ait-devnet ...
```
### Network timeout during swap
**Error:**
```
Network error: Timeout
```
**Solution:**
```bash
# Check exchange service URL in config
aitbc config show
# Verify exchange service is accessible
curl http://your-exchange-url:8001/health
# Retry the swap operation
aitbc cross-chain status <swap_id> # Check if swap was created
```
### Slippage exceeded
**Error:**
```
Error: Slippage tolerance exceeded
```
**Solution:**
```bash
# Increase slippage tolerance
aitbc cross-chain swap --slippage 0.03 ...
# Or set explicit minimum amount
aitbc cross-chain swap --min-amount 95 ...
```
---
## 🧪 **Validation**
Validate this scenario with the shared 3-node harness:
```bash
bash scripts/workflow/44_comprehensive_multi_node_scenario.sh
```
**Node coverage**:
- `aitbc1`: genesis / primary node checks
- `aitbc`: follower / local node checks
- `gitea-runner`: automation / CI node checks
**Validation guide**:
- [Scenario Validation Guide](./VALIDATION.md)
**Expected result**:
- Scenario-specific commands complete successfully
- Cross-node health checks pass
- Blockchain heights remain in sync
- Any node-specific step is documented in the scenario workflow
**Note**: Cross-chain operations require an active exchange service. If the service is unavailable, commands will return network errors. This is expected behavior for scenarios dependent on external services.
---
## 💻 **Code Examples Using Agent SDK**
### Example 1: Query Exchange Rates Programmatically
```python
from aitbc_agent_sdk import Agent, AgentConfig
config = AgentConfig(
name="cross-chain-agent",
blockchain_network="mainnet"
)
agent = Agent(config)
agent.start()
# Get exchange rates
rates = agent.get_cross_chain_rates(
from_chain="ait-testnet",
to_chain="ait-devnet"
)
print(f"Exchange rate: {rates['rate']}")
```
### Example 2: Execute Cross-Chain Swap
```python
from aitbc_agent_sdk import Agent, AgentConfig
config = AgentConfig(
name="trading-agent",
blockchain_network="mainnet",
wallet_name="trading-wallet"
)
agent = Agent(config)
agent.start()
# Execute swap
swap_result = agent.execute_cross_chain_swap(
from_chain="ait-testnet",
to_chain="ait-devnet",
from_token="AIT",
to_token="AIT",
amount=100,
slippage=0.01
)
print(f"Swap ID: {swap_result['swap_id']}")
print(f"Status: {swap_result['status']}")
# Monitor swap
while swap_result['status'] in ['pending', 'executing']:
swap_result = agent.get_swap_status(swap_result['swap_id'])
print(f"Current status: {swap_result['status']}")
time.sleep(5)
```
### Example 3: Bridge Assets Across Chains
```python
from aitbc_agent_sdk import Agent, AgentConfig
config = AgentConfig(
name="bridge-agent",
blockchain_network="mainnet"
)
agent = Agent(config)
agent.start()
# Bridge assets
bridge_result = agent.bridge_assets(
source_chain="ait-testnet",
target_chain="ait-devnet",
token="AIT",
amount=50,
recipient="ait1recipient..."
)
print(f"Bridge ID: {bridge_result['bridge_id']}")
print(f"Status: {bridge_result['status']}")
```
---
## 🎯 **Expected Outcomes**
After completing this scenario, you should be able to:
- Query cross-chain exchange rates between supported chains
- Create atomic swaps with slippage protection
- Monitor swap and bridge transaction status
- Execute cross-chain bridge operations
- View available liquidity pools
- Track cross-chain trading statistics
- Understand the cross-chain transaction lifecycle
- Troubleshoot common cross-chain operation issues
---
## 🔗 **Related Resources**
### **AITBC Documentation**
- [Scenario 20: Cross Chain Transfer](./20_cross_chain_transfer.md) - Basic cross-chain transfers
- [Scenario 47: Cross Chain Atomic Swap](./47_cross_chain_atomic_swap.md) - Advanced atomic swaps
- [CLI Cross-Chain Commands](../cli/CLI_DOCUMENTATION.md) - Complete CLI reference
- [Exchange Service Documentation](../apps/exchange/README.md) - Exchange service details
### **External Resources**
- [Atomic Swap Protocol](https://en.wikipedia.org/wiki/Atomic_swap)
- [Cross-Chain Bridges](https://ethereum.org/en/bridges/)
- [Liquidity Pools](https://docs.uniswap.org/protocol/introduction)
### **Next Scenarios**
- [Scenario 50: Workflow Management](./50_workflow_management.md) - Workflow operations
- [Scenario 51: Monitoring and Metrics](./51_monitoring_and_metrics.md) - System monitoring
- [Scenario 27: Cross Chain Trader](./27_cross_chain_trader.md) - Advanced trading agent
---
## 📊 **Quality Metrics**
- **Structure**: 10/10 - Clear workflow from rates to execution
- **Content**: 10/10 - Comprehensive cross-chain operations coverage
- **Code Examples**: 10/10 - Working Agent SDK examples
- **Status**: Active scenario
---
*Last updated: 2026-05-27*
*Version: 1.0*
*Status: Active scenario document*