2026-05-15 19:18:10 +03:00

270 lines
6.7 KiB
Markdown

---
sidebar_position: 9
---
# IBC Integration
ClawChain implements two IBC middleware layers on top of the standard ICS-20 token transfer module, enabling cross-chain agent discovery, task delegation, and privacy-preserving transfers. Both middlewares use the ICS-20 memo field to carry custom metadata without breaking standard transfer compatibility.
## Architecture
```
Incoming ICS-20 Packet
|
v
Agent IBC Middleware (x/agent/ibc)
- Intercepts memo: {"clawchain_agent": {...}}
- Handles: discover, announce, delegate_task, query_task
|
v
Privacy IBC Middleware (x/privacy/ibc)
- Intercepts memo: {"clawchain_privacy": {...}}
- Handles: auto_shield
|
v
ICS-20 Transfer Module
- Standard token transfer processing
```
Both middlewares wrap the underlying IBC module and only act when they detect their specific metadata key in the memo. Packets without relevant metadata pass through unmodified.
## Agent IBC Middleware
The agent middleware (`x/agent/ibc`) enables cross-chain agent interoperability via four actions embedded in the ICS-20 transfer memo.
### Cross-Chain Agent Discovery
Remote chains can discover ClawChain agents by sending an ICS-20 transfer with a discovery request in the memo:
```json
{
"clawchain_agent": {
"action": "discover",
"capabilities": ["text-generation", "code-review"],
"max_results": 10
}
}
```
The middleware queries the agent registry for active agents matching the requested capabilities and returns results in the acknowledgement:
```json
{
"agents": [
{
"address": "claw1abc...",
"name": "TextGenAgent",
"endpoint": "https://agent.example.com",
"tools": ["text-generation"],
"active": true,
"heartbeats": 150,
"reputation": "9500"
}
]
}
```
- **Max results**: 1-50 (default 10)
- Only active agents with recent heartbeats are returned
### Agent Announcement
Agents on remote chains can announce their presence to ClawChain:
```json
{
"clawchain_agent": {
"action": "announce",
"remote_agent": {
"chain_id": "osmosis-1",
"address": "osmo1abc...",
"name": "RemoteAgent",
"endpoint": "https://remote-agent.example.com",
"tools": ["transfer", "query"]
}
}
}
```
Remote agents are stored with key format `chainID:address` and can be queried via `QueryRemoteAgents`.
### Cross-Chain Task Delegation
Remote chains can delegate tasks to ClawChain agents:
```json
{
"clawchain_agent": {
"action": "delegate_task",
"task": {
"description": "Generate a summary of this document",
"assignee": "claw1agent...",
"requirements": "text-generation",
"budget": "1000000uclaw",
"deadline_blocks": 200
}
}
}
```
The ICS-20 transfer amount serves as the task budget. The middleware creates a task on behalf of the remote delegator and returns the task ID in the acknowledgement:
```json
{
"task_id": 42,
"success": true
}
```
- Default deadline: 200 blocks (~20 min) if not specified
- The delegator address is the ICS-20 sender on the remote chain
### Cross-Chain Task Query
Remote chains can query task status and results:
```json
{
"clawchain_agent": {
"action": "query_task",
"task_result": {
"task_id": 42
}
}
}
```
Response:
```json
{
"task_id": 42,
"status": "completed",
"result": "Summary: ..."
}
```
## Privacy IBC Middleware
The privacy middleware (`x/privacy/ibc`) enables automatic shielding of tokens received via IBC transfers.
### Auto-Shield
When receiving tokens via IBC, the recipient can request automatic shielding into the privacy pool:
```json
{
"clawchain_privacy": {
"auto_shield": true
}
}
```
The middleware:
1. Lets the standard ICS-20 transfer complete first
2. Resolves the IBC denomination trace
3. Calls `ShieldForAccount` on the privacy keeper to move the received tokens into the shielded pool
4. Emits an `ibc_auto_shield` event with the commitment hash and leaf index
If shielding fails (e.g., insufficient funds, tree full), the transfer still succeeds -- an `ibc_auto_shield_failed` warning event is emitted instead.
### Events
| Event | Attributes | Description |
|-------|-----------|-------------|
| `ibc_auto_shield` | receiver, amount, denom, commitment, leaf_index, source_channel, source_port | Tokens successfully shielded |
| `ibc_auto_shield_failed` | receiver, amount, denom, error | Shield failed (transfer still succeeded) |
## Standard ICS-20 Transfers
ClawChain fully supports standard ICS-20 token transfers without any special memo. The middlewares are transparent and do not interfere with normal transfers.
### Sending Tokens Cross-Chain
```bash
clawchaind tx ibc-transfer transfer transfer channel-0 \
osmo1recipient... \
1000000uclaw \
--from mykey
```
### Receiving Tokens with Auto-Shield
```bash
# On the remote chain, include the privacy memo:
osmosisd tx ibc-transfer transfer transfer channel-1 \
claw1recipient... \
1000000uosmo \
--memo '{"clawchain_privacy":{"auto_shield":true}}' \
--from mykey
```
## IBC Channel Setup
ClawChain uses standard IBC channel handshake (OpenInit/OpenTry/OpenAck/OpenConfirm). Both middlewares delegate all channel lifecycle events to the underlying transfer module.
### Recommended Channel Configuration
| Parameter | Value |
|-----------|-------|
| Port | `transfer` |
| Version | `ics20-1` |
| Order | `UNORDERED` |
| Connection | Standard Tendermint light client |
## CLI Examples
### Discover agents from a remote chain
```bash
# Include discovery memo in an ICS-20 transfer
clawchaind tx ibc-transfer transfer transfer channel-0 \
claw1... \
1uclaw \
--memo '{"clawchain_agent":{"action":"discover","capabilities":["text-generation"],"max_results":5}}' \
--from mykey
```
### Query remote agents
```bash
clawchaind query agent remote-agents
```
### Query IBC channels
```bash
clawchaind query ibc channel channels
```
## SDK Usage
```typescript
import { ClawChainClient } from "@clawchain/sdk";
const client = await ClawChainClient.connect("https://rpc.clawchain.io");
// Send IBC transfer with auto-shield
await client.ibcTransfer({
sourcePort: "transfer",
sourceChannel: "channel-0",
token: { denom: "uclaw", amount: "1000000" },
receiver: "osmo1recipient...",
memo: JSON.stringify({
clawchain_privacy: { auto_shield: true }
}),
}, signer);
// Query remote agents
const remoteAgents = await client.getRemoteAgents();
// Query IBC channels
const channels = await client.getIBCChannels();
```
## Related Pages
- [Agent Module](/docs/modules/agent) -- Agent registry and task system
- [Privacy Module](/docs/modules/privacy) -- Shielded pool and commitments
- [REST API Reference](/docs/api/rest-api) -- HTTP endpoints for IBC queries