"""Chain configuration setup prompt implementation."""
async def chain_config_setup() -> str:
"""
Guide for setting up chain configuration.
Helps users configure endpoints and troubleshoot connections.
"""
return """⚙️ **Chain Configuration Setup Guide**
Let's get you connected to Regen Network! This guide will help you configure your chain connection properly.
---
## 🌐 **Network Options**
### **Mainnet (Production)**
```python
mainnet_config = {
"chain_id": "regen-1",
"rest_endpoint": "https://rest.cosmos.directory/regen",
"rpc_endpoint": "https://rpc.cosmos.directory/regen",
"grpc_endpoint": "grpc.cosmos.directory:443",
"denom": "uregen"
}
```
### **Testnet (Development)**
```python
testnet_config = {
"chain_id": "regen-redwood-1",
"rest_endpoint": "https://testnet.rest.regen.network",
"rpc_endpoint": "https://testnet.rpc.regen.network",
"grpc_endpoint": "testnet.grpc.regen.network:443",
"denom": "uregen"
}
```
### **Local Node (Advanced)**
```python
local_config = {
"chain_id": "regen-local",
"rest_endpoint": "http://localhost:1317",
"rpc_endpoint": "http://localhost:26657",
"grpc_endpoint": "localhost:9090",
"denom": "uregen"
}
```
---
## 🔧 **Configuration Steps**
### **Step 1: Choose Your Network**
Decide which network to connect to:
- **Mainnet**: For production queries and real data
- **Testnet**: For development and testing
- **Local**: For custom node operators
### **Step 2: Test Endpoints**
```python
def test_endpoints(config):
\"\"\"Test if endpoints are accessible.\"\"\"
import requests
tests = {
'REST': config['rest_endpoint'] + '/cosmos/base/tendermint/v1beta1/node_info',
'RPC': config['rpc_endpoint'] + '/status'
}
results = {}
for name, url in tests.items():
try:
response = requests.get(url, timeout=5)
results[name] = 'OK' if response.status_code == 200 else f'Error: {response.status_code}'
except Exception as e:
results[name] = f'Failed: {str(e)}'
return results
# Test your configuration
test_results = test_endpoints(mainnet_config)
for endpoint, status in test_results.items():
print(f"{endpoint}: {status}")
```
### **Step 3: Verify Chain ID**
```python
def verify_chain_id(config):
\"\"\"Verify you're connected to the right chain.\"\"\"
import requests
url = config['rest_endpoint'] + '/cosmos/base/tendermint/v1beta1/node_info'
response = requests.get(url)
if response.status_code == 200:
node_info = response.json()
actual_chain_id = node_info.get('default_node_info', {}).get('network', '')
if actual_chain_id == config['chain_id']:
return {'status': 'success', 'chain_id': actual_chain_id}
else:
return {
'status': 'mismatch',
'expected': config['chain_id'],
'actual': actual_chain_id
}
return {'status': 'error', 'message': 'Could not fetch node info'}
```
---
## 🔍 **Connection Troubleshooting**
### **Common Issues & Solutions**
#### **Issue 1: Connection Timeout**
```
Error: Connection to rest.cosmos.directory timed out
```
**Solutions:**
- Check your internet connection
- Try alternative endpoints (see backup endpoints below)
- Verify firewall/proxy settings
- Increase timeout values
#### **Issue 2: Certificate Errors**
```
Error: SSL certificate verification failed
```
**Solutions:**
```python
# For testing only - not recommended for production
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
# Better solution: Update certificates
# pip install --upgrade certifi
```
#### **Issue 3: CORS Errors (Browser)**
```
Error: CORS policy blocked request
```
**Solutions:**
- Use a backend proxy server
- Configure CORS headers on your server
- Use official CORS-enabled endpoints
#### **Issue 4: Rate Limiting**
```
Error: 429 Too Many Requests
```
**Solutions:**
```python
import time
def rate_limited_request(url, delay=1):
\"\"\"Add delay between requests.\"\"\"
time.sleep(delay)
return requests.get(url)
# Or use exponential backoff
def retry_with_backoff(func, max_retries=3):
for i in range(max_retries):
try:
return func()
except Exception as e:
if i == max_retries - 1:
raise
time.sleep(2 ** i)
```
---
## 🔄 **Alternative Endpoints**
### **Backup REST Endpoints**
```python
backup_endpoints = [
"https://rest.cosmos.directory/regen",
"https://regen-mainnet-rest.allthatnode.com",
"https://regen.api.ping.pub",
"https://api-regen.cosmostation.io"
]
def find_working_endpoint(endpoints):
\"\"\"Find the first working endpoint.\"\"\"
for endpoint in endpoints:
try:
response = requests.get(
endpoint + '/cosmos/base/tendermint/v1beta1/node_info',
timeout=3
)
if response.status_code == 200:
return endpoint
except:
continue
return None
working_endpoint = find_working_endpoint(backup_endpoints)
print(f"Using endpoint: {working_endpoint}")
```
### **Public RPC Endpoints**
```python
rpc_endpoints = [
"https://rpc.cosmos.directory/regen",
"https://regen-mainnet-rpc.allthatnode.com",
"https://regen.rpc.ping.pub",
"https://rpc-regen.cosmostation.io"
]
```
---
## 📊 **Configuration Validation**
### **Complete Configuration Check**
```python
def validate_configuration(config):
\"\"\"Comprehensive configuration validation.\"\"\"
validation_results = {
'endpoints': {},
'chain_id': None,
'modules': {},
'overall': 'pending'
}
# Test endpoints
print("Testing endpoints...")
for endpoint_type in ['rest_endpoint', 'rpc_endpoint']:
if endpoint_type in config:
# Test connection
validation_results['endpoints'][endpoint_type] = 'configured'
# Verify chain ID
print("Verifying chain ID...")
chain_result = verify_chain_id(config)
validation_results['chain_id'] = chain_result
# Test module availability
print("Checking modules...")
modules_to_test = [
'/regen/ecocredit/v1/classes',
'/regen/ecocredit/v1/projects',
'/regen/ecocredit/marketplace/v1/sell-orders'
]
for module_path in modules_to_test:
try:
url = config['rest_endpoint'] + module_path
response = requests.get(url, timeout=5)
module_name = module_path.split('/')[2]
validation_results['modules'][module_name] = 'available' if response.status_code == 200 else 'error'
except:
validation_results['modules'][module_name] = 'failed'
# Determine overall status
if all(v == 'configured' for v in validation_results['endpoints'].values()):
if validation_results['chain_id'].get('status') == 'success':
if all(v == 'available' for v in validation_results['modules'].values()):
validation_results['overall'] = 'success'
else:
validation_results['overall'] = 'partial'
else:
validation_results['overall'] = 'chain_mismatch'
else:
validation_results['overall'] = 'connection_failed'
return validation_results
# Run validation
results = validate_configuration(mainnet_config)
print(f"Configuration Status: {results['overall']}")
```
---
## 🚀 **Quick Start Configuration**
### **Minimal Working Setup**
```python
# Simplest configuration that works
quick_config = {
"rest_endpoint": "https://rest.cosmos.directory/regen"
}
# That's it! You can now query:
# - get_credit_classes()
# - get_all_projects()
# - get_sell_orders()
# etc.
```
### **Environment Variables Setup**
```bash
# .env file
REGEN_CHAIN_ID=regen-1
REGEN_REST_ENDPOINT=https://rest.cosmos.directory/regen
REGEN_RPC_ENDPOINT=https://rpc.cosmos.directory/regen
```
```python
import os
from dotenv import load_dotenv
load_dotenv()
config = {
"chain_id": os.getenv("REGEN_CHAIN_ID"),
"rest_endpoint": os.getenv("REGEN_REST_ENDPOINT"),
"rpc_endpoint": os.getenv("REGEN_RPC_ENDPOINT")
}
```
---
## 📝 **Configuration Best Practices**
### **1. Use Configuration Files**
```python
# config.json
{
"mainnet": {
"chain_id": "regen-1",
"rest_endpoint": "https://rest.cosmos.directory/regen"
},
"testnet": {
"chain_id": "regen-redwood-1",
"rest_endpoint": "https://testnet.rest.regen.network"
}
}
# Load configuration
import json
def load_config(network='mainnet'):
with open('config.json', 'r') as f:
configs = json.load(f)
return configs.get(network, configs['mainnet'])
```
### **2. Implement Fallbacks**
```python
class ChainConfig:
def __init__(self, primary_endpoint, backup_endpoints=None):
self.primary = primary_endpoint
self.backups = backup_endpoints or []
self.current = primary_endpoint
def get_endpoint(self):
# Try primary first
if self.test_endpoint(self.current):
return self.current
# Try backups
for backup in self.backups:
if self.test_endpoint(backup):
self.current = backup
return backup
raise Exception("No working endpoints available")
def test_endpoint(self, endpoint):
# Implementation of endpoint test
pass
```
### **3. Cache Endpoint Status**
```python
import time
class EndpointCache:
def __init__(self, ttl=300): # 5 minutes TTL
self.cache = {}
self.ttl = ttl
def is_valid(self, endpoint):
if endpoint in self.cache:
timestamp, status = self.cache[endpoint]
if time.time() - timestamp < self.ttl:
return status
# Test endpoint and cache result
status = self.test_endpoint(endpoint)
self.cache[endpoint] = (time.time(), status)
return status
```
---
## ✅ **Configuration Checklist**
Before starting queries, ensure:
- [ ] **Endpoint Selection**: Chosen appropriate network (mainnet/testnet)
- [ ] **Connection Test**: All endpoints responding correctly
- [ ] **Chain ID Verification**: Connected to intended chain
- [ ] **Module Availability**: Required modules accessible
- [ ] **Error Handling**: Fallback endpoints configured
- [ ] **Performance**: Response times acceptable (<2 seconds)
- [ ] **Security**: Using HTTPS for production
---
## 🆘 **Need Help?**
### **Quick Diagnostics**
```python
def quick_diagnostics():
print("=== Regen Network Connection Diagnostics ===")
print(f"Python Version: {sys.version}")
print(f"Requests Version: {requests.__version__}")
# Test basic connectivity
try:
response = requests.get("https://rest.cosmos.directory/regen", timeout=5)
print(f"REST API: ✅ Connected (Status: {response.status_code})")
except Exception as e:
print(f"REST API: ❌ Failed ({str(e)})")
# Test chain info
try:
response = requests.get("https://rest.cosmos.directory/regen/cosmos/base/tendermint/v1beta1/node_info")
data = response.json()
print(f"Chain ID: {data.get('default_node_info', {}).get('network', 'Unknown')}")
print(f"Latest Block: {data.get('sync_info', {}).get('latest_block_height', 'Unknown')}")
except:
print("Chain Info: ❌ Could not fetch")
quick_diagnostics()
```
Ready to configure your connection? Start with the quick config and expand as needed!"""