|
4 | 4 |
|
5 | 5 | [](https://opensource.org/licenses/MIT) |
6 | 6 |  |
| 7 | +[](https://www.npmjs.com/package/@polkadot-agent-kit/sdk) |
7 | 8 | <br/> |
8 | 9 | <br/> |
9 | 10 | **Polkadot AI Agent Kit** is an open-source library designed to build AI Agents that interact with the Polkadot ecosystem. The library provides tools for wallet management, cross-chain transactions, NFT interactions, DeFi integrations, and interactions with Asset Hub and parachains such as People Chain and Kusama Chain. The project will be archived under the **[OpenGuild](https://github.com/openguild-labs)** organization upon completion. |
10 | 11 |
|
11 | | -- NPM package can be found here: https://www.npmjs.com/package/@polkadot-agent-kit/sdk |
| 12 | +## 📦 Monorepo Structure |
12 | 13 |
|
13 | | -## 📦 Project Overview |
| 14 | +This project is organized as a monorepo with the following packages: |
14 | 15 |
|
15 | | -### Objectives |
16 | | -- **Simplify** the development of AI Agents on Polkadot. |
17 | | -- Provide **extensive tools** for interacting with Substrate, parachains, XCM, NFTs, and DeFi protocols. |
18 | | -- Support **secure wallet management** through Proxy or MPC solutions. |
19 | | -- Integrate an **AI interface** for controlling the Agent using natural language. |
| 16 | +| Package | Description | Key Features | |
| 17 | +|---------|-------------|--------------| |
| 18 | +| **[`packages/common/`](packages/common/)** | **Shared utilities and type definitions** | • Chain configurations and supported networks<br/>• TypeScript interfaces and type definitions<br/>• Utility functions for chain validation and filtering<br/>• Common constants and enums used across packages | |
| 19 | +| **[`packages/core/`](packages/core/)** | **Core Polkadot API functionality** | • PolkadotApi implementation with multi-chain support<br/>• Balance checking and native token transfers<br/>• Cross-chain (XCM) transaction handling<br/>• Dynamic chain initialization and management<br/>• Transaction utilities and signing logic | |
| 20 | +| **[`packages/llm/`](packages/llm/)** | **LangChain integration for AI agents** | • LangChain-compatible tools for blockchain operations<br/>• AI agent interfaces and abstractions<br/>• Tool definitions for balance checking, transfers, and XCM<br/>• Dynamic chain initialization tools for AI agents | |
| 21 | +| **[`packages/sdk/`](packages/sdk/)** | **Main SDK interface** | • PolkadotAgentKit - the primary class for developers<br/>• High-level API that combines core and LLM functionality<br/>• Comprehensive examples and documentation<br/>• Integration tests and usage patterns | |
20 | 22 |
|
21 | | -## ✨ Features |
| 23 | +## 🔗 Examples |
22 | 24 |
|
23 | | -| Feature | Description | |
24 | | -|----------|-------| |
25 | | -| **Substrate Connection** | Support HTTP/WebSocket connections to Polkadot, Kusama, and parachains. | |
26 | | -| **Wallet Management** | Use **pallet-proxy** for delegation or MPC solutions for multi-layer security. | |
27 | | -| **Cross-Chain Transactions** | Transfer tokens via XCM between Relay Chain, Asset Hub, and parachains. | |
28 | | -| **NFT Interactions** | Manage and interact with NFTs across parachains (e.g., minting, transferring, and querying NFTs). | |
29 | | -| **DeFi Integrations** | Interact with DeFi protocols for staking, swapping, and yield farming. | |
30 | | -| **OpenGov Interaction** | Create/vote on proposals on People Chain and manage governance. | |
31 | | -| **AI Interface** | Control the Agent using natural language commands (e.g., *"Swap 100 USDC to DOT and transfer to Coretime Chain"*). | |
| 25 | +Explore real-world implementations: |
32 | 26 |
|
33 | | -## 🛠️ Phases & Deliverables |
| 27 | +- **[Telegram Bot Example](examples/telegram-bot/)** |
34 | 28 |
|
35 | | -### **Phase 1: Core Library** |
36 | | -| Tool | Description | |
37 | | -|---------|-------| |
38 | | -| **Substrate Connection** | Basic connection to Substrate nodes via Polkadot.js API. | |
39 | | -| **Wallet Management** | Integrate Proxy Accounts and authentication via SR25519/Ed25519. | |
40 | | -| **XCM Transaction Tools** | Support token transfers via XCM between parachains. | |
41 | | -| **Parachain Modules** | Interact with Asset Hub (Assets), People Chain (Governance), and Coretime Chain. | |
42 | | -| **NFT Tools** | Provide tools for minting, transferring, and querying NFTs across parachains. | |
43 | | -| **DeFi Tools** | Enable interactions with DeFi protocols for staking, swapping, and yield farming. | |
| 29 | +## 🚀 How to Run the Monorepo |
44 | 30 |
|
45 | | -### **Phase 2: AI Interface** |
46 | | -| Deliverable | Description | |
47 | | -|-------------|-------| |
48 | | -| **Prompt Interface** | Control the Agent using commands such as: | |
49 | | -| ```await agent.prompt("Vote Proposal #123 on OpenGov")``` | | |
50 | | -| ```await agent.prompt("Transfer 10 DOT from Asset Hub to Coretime Chain")``` | | |
51 | | -| ```await agent.prompt("Mint an NFT on Asset Hub")``` | | |
52 | | -| ```await agent.prompt("Stake 100 DOT on a DeFi protocol")``` | | |
53 | | -| **Natural Language Processing** | Integrate AI models to analyze natural language requests. | |
| 31 | +### Prerequisites |
| 32 | +- Node.js 18+ |
| 33 | +- pnpm (recommended package manager) |
54 | 34 |
|
55 | | -## Chain Configuration and Setup |
56 | | - |
57 | | -This project provides a flexible, interactive system to install Polkadot chains. The `.papi/` directory is included in `.gitignore` to prevent tracking of chain descriptors, which means each developer needs to set up their desired chains after cloning the repository. |
58 | | - |
59 | | -### Quick Setup |
60 | | - |
61 | | -After cloning the project, the easiest way to setup chains is: |
62 | | - |
63 | | -1. Install dependencies: |
64 | | - ```bash |
65 | | - pnpm install |
66 | | - ``` |
67 | | - |
68 | | -2. Run the interactive chain setup script: |
69 | | - ```bash |
70 | | - pnpm papi |
71 | | - ``` |
72 | | -3. Run build package |
73 | | - ```bash |
74 | | - pnpm run build |
75 | | - ``` |
76 | | -4. Test Telegram AI bot |
77 | | - ```bash |
78 | | - cd examples/telegram-bot && pnpm i && pnpm run dev |
79 | | - ``` |
80 | | - |
81 | | -This will launch an interactive CLI menu that allows you to: |
82 | | -- Choose from common predefined chains |
83 | | -- Add custom chains |
84 | | -- Install from a configuration file |
85 | | -- Save your configuration for later use |
86 | | - |
87 | | -### Command Line Options |
88 | | - |
89 | | -The setup script supports various command line options for more flexibility: |
| 35 | +### Setup and Development Commands |
90 | 36 |
|
91 | 37 | ```bash |
92 | | -# Show all available options |
93 | | -pnpm run setup -- --help |
94 | | - |
95 | | -# List all common predefined chains |
96 | | -pnpm run setup -- --list |
97 | | - |
98 | | -# Install specific chains directly |
99 | | -pnpm run setup -- --install west,west_asset_hub |
100 | | - |
101 | | -# Install chains from a configuration file |
102 | | -pnpm run setup -- --config ./my-chains.json |
103 | | -``` |
104 | | - |
105 | | -### Custom Chain Configuration |
106 | | - |
107 | | -You can create your own chain configuration file with this format: |
108 | | - |
109 | | -```json |
110 | | -{ |
111 | | - "chains": [ |
112 | | - { |
113 | | - "id": "chain_id", |
114 | | - "name": "chain_name_in_system", |
115 | | - "description": "Description of the chain (optional)" |
116 | | - } |
117 | | - ] |
118 | | -} |
119 | | -``` |
120 | | - |
121 | | -The setup script will automatically detect: |
122 | | -1. A `chains.config.json` file in the project root directory |
123 | | -2. A custom path specified via the `--config` option |
124 | | -3. An environment variable `CHAINS_CONFIG_PATH` pointing to your config file |
125 | | - |
126 | | -### Why is `.papi/` in `.gitignore`? |
127 | | - |
128 | | -The `.papi/` directory contains descriptors for Polkadot chains. These descriptors: |
129 | | -1. Can be quite large |
130 | | -2. Can be generated automatically |
131 | | -3. May differ between development environments |
132 | | -4. May be updated frequently |
133 | | - |
134 | | -By placing this directory in `.gitignore`, we avoid tracking unnecessary files and allow each developer to set up chains according to their specific needs. |
135 | | - |
136 | | -### Troubleshooting |
137 | | - |
138 | | -If you encounter errors during chain installation: |
139 | | - |
140 | | -1. Make sure you're using the Node.js version specified in `package.json` (Node.js 22). |
141 | | -2. Delete the `.papi/` directory and try again from scratch. |
142 | | -3. For specific chains, you can try installing them manually: |
143 | | - ```bash |
144 | | - npx papi add [chain_id] -n [chain_name] |
145 | | - ``` |
146 | | -4. Ensure you have a stable internet connection when running the setup script. |
147 | | - |
148 | | -## Dynamic Chain Registry |
149 | | - |
150 | | -The Polkadot AI Agent Kit has been enhanced with a dynamic chain registry system, allowing the library to support any Substrate-based parachain, not just predefined ones. This makes the library more flexible and future-proof. |
151 | | - |
152 | | -### Automatic Initialization |
153 | | - |
154 | | -The SDK now automatically initializes chain descriptors when imported, making it work like a standard SDK without manual setup: |
155 | | - |
156 | | -```typescript |
157 | | -// Just import the SDK - chain descriptors are loaded automatically |
158 | | -import { PolkadotAgentKit } from 'polkadot-agent-kit'; |
159 | | - |
160 | | -// Create an agent - no initialization needed |
161 | | -const agent = new PolkadotAgentKit({ |
162 | | - privateKey: process.env.PRIVATE_KEY, |
163 | | - chains: [ |
164 | | - { name: 'westend', url: 'wss://westend-rpc.polkadot.io' } |
165 | | - ] |
166 | | -}); |
167 | | - |
168 | | -// Use the agent immediately |
169 | | -const connection = await agent.getConnection('westend'); |
170 | | -``` |
171 | | - |
172 | | -If you need to control the initialization timing, you can use the explicit initialization function: |
173 | | - |
174 | | -```typescript |
175 | | -import { initializeSDK, PolkadotAgentKit } from 'polkadot-agent-kit'; |
176 | | - |
177 | | -// Initialize SDK explicitly (optional, it also initializes automatically) |
178 | | -await initializeSDK(); |
179 | | - |
180 | | -// Now create your agent |
181 | | -const agent = new PolkadotAgentKit({ /* config */ }); |
182 | | -``` |
| 38 | +# 1. Clone the repository |
| 39 | +git clone https://github.com/openguild-labs/polkadot-agent-kit.git |
| 40 | +cd polkadot-agent-kit |
183 | 41 |
|
184 | | -### How It Works |
| 42 | +# 2. Install all dependencies |
| 43 | +pnpm install |
185 | 44 |
|
186 | | -1. **Chain Descriptors**: Chain descriptors are registered at runtime, instead of being hardcoded imports. |
| 45 | +# 3. Generate Polkadot API types (required before building) |
| 46 | +pnpm papi |
187 | 47 |
|
188 | | -2. **Registering Chains**: You can register any Substrate-based chain using our registry system: |
| 48 | +# 4. Build all packages |
| 49 | +pnpm run build |
189 | 50 |
|
190 | | - ```typescript |
191 | | - import { registerChainDescriptor } from 'polkadot-agent-kit/chain/chainInit'; |
192 | | - import { someChainDescriptor } from '@polkadot-api/descriptors'; |
193 | | - |
194 | | - // Register a new chain |
195 | | - registerChainDescriptor('my_custom_chain', someChainDescriptor); |
196 | | - ``` |
| 51 | +# 5. Run unit tests across all packages |
| 52 | +pnpm run test |
197 | 53 |
|
198 | | -3. **Using Registered Chains**: Once registered, you can use the chain with the agent: |
| 54 | +# 6. Run end-to-end tests |
| 55 | +pnpm run test:e2e |
199 | 56 |
|
200 | | - ```typescript |
201 | | - const agent = new PolkadotAgentKit({ |
202 | | - privateKey: process.env.PRIVATE_KEY, |
203 | | - chains: [ |
204 | | - { name: 'my_custom_chain', url: 'wss://my-custom-chain-rpc.example.com' } |
205 | | - ] |
206 | | - }); |
207 | | - |
208 | | - // Use the chain like any other |
209 | | - const { api } = await agent.getConnection('my_custom_chain'); |
210 | | - ``` |
211 | | - |
212 | | -4. **Default Chains**: The library still initializes common chains by default for convenience: |
213 | | - |
214 | | - - westend2 (Westend Relay Chain) |
215 | | - - westend2_asset_hub (Westend Asset Hub) |
216 | | - - polkadot (Polkadot Relay Chain) |
217 | | - - polkadot_asset_hub (Polkadot Asset Hub) |
218 | | - - kusama (Kusama Relay Chain) |
219 | | - - kusama_asset_hub (Kusama Asset Hub) |
220 | | - |
221 | | -### Custom Chain Descriptors |
222 | | - |
223 | | -For custom chains, you need to: |
224 | | - |
225 | | -1. Import the descriptor from `@polkadot-api/descriptors` or create a custom one |
226 | | -2. Register it with `registerChainDescriptor(chainName, descriptor)` |
227 | | - |
228 | | -This approach ensures the library remains flexible while working correctly with the package structure of Polkadot API. |
229 | | - |
230 | | -### Chain Naming Conventions |
231 | | - |
232 | | -For compatibility reasons, the library supports multiple naming conventions for the same chains: |
233 | | - |
234 | | -| Chain Type | Supported Names | |
235 | | -|------------|----------------| |
236 | | -| Westend Relay Chain | `westend`, `westend2` | |
237 | | -| Westend Asset Hub | `westend_asset_hub`, `westend2_asset_hub` | |
238 | | -| Kusama Relay Chain | `kusama`, `ksmcc3` | |
239 | | -| Kusama Asset Hub | `kusama_asset_hub`, `ksmcc3_asset_hub` | |
240 | | - |
241 | | -When connecting to chains, you can use either naming convention. We recommend using the more descriptive names (`westend`, `westend_asset_hub`, etc.) for better readability. |
242 | | - |
243 | | -### Automatic Reconnection |
244 | | - |
245 | | -The SDK includes an automatic reconnection system to handle network disruptions and chain reorganizations. When connection issues like `ChainHead disjointed` errors occur, the system will: |
246 | | - |
247 | | -1. Detect the connection problem automatically |
248 | | -2. Attempt to reconnect with exponential backoff (starting at 1 second, doubling each time) |
249 | | -3. Try up to 5 reconnection attempts before giving up |
250 | | -4. Maintain your existing API objects with the same references |
251 | | - |
252 | | -This provides better resilience against temporary network issues and chain reorganizations without requiring application-level error handling in most cases. |
253 | | - |
254 | | -Example of a connection that automatically recovers: |
255 | | - |
256 | | -```typescript |
257 | | -// Create an agent as usual |
258 | | -const agent = new PolkadotAgentKit({ |
259 | | - privateKey: process.env.PRIVATE_KEY, |
260 | | - chains: [{ name: 'westend', url: 'wss://westend-rpc.polkadot.io' }] |
261 | | -}); |
262 | | - |
263 | | -// Get a connection - it will automatically reconnect if needed |
264 | | -const { api } = await agent.getConnection('westend'); |
265 | | - |
266 | | -// Even if the connection drops temporarily, it will reconnect in the background |
267 | | -// and your code can continue to use the same API object |
| 57 | +# 7. Run integration tests (requires Ollama) |
| 58 | +pnpm run test:integration |
268 | 59 | ``` |
269 | 60 |
|
270 | | -## Key Types and Mnemonic Support |
271 | | - |
272 | | -The Polkadot AI Agent Kit now supports both Sr25519 and Ed25519 key types with dynamic configuration, as well as mnemonic phrase key generation. |
273 | | - |
274 | | -### Key Type Configuration |
275 | | - |
276 | | -You can specify which key type to use when creating your agent: |
277 | | - |
278 | | -```typescript |
279 | | -// Create an agent with Sr25519 keys |
280 | | -const agent = new PolkadotAgentKit({ |
281 | | - privateKey: process.env.PRIVATE_KEY, |
282 | | - keyType: 'Sr25519', // Use Sr25519 for signing (default is Ed25519) |
283 | | - chains: [ |
284 | | - { name: 'westend', url: 'wss://westend-rpc.polkadot.io' } |
285 | | - ] |
286 | | -}); |
287 | | -``` |
288 | | - |
289 | | -### Mnemonic Phrase Support |
290 | | - |
291 | | -You can now create an agent using a mnemonic phrase instead of a raw private key: |
292 | | - |
293 | | -```typescript |
294 | | -// Create an agent from a mnemonic phrase |
295 | | -const agent = new PolkadotAgentKit({ |
296 | | - mnemonic: 'word1 word2 word3 ... word12', // Your 12 or 24 word mnemonic |
297 | | - derivationPath: '', // Optional derivation path (default: '') |
298 | | - keyType: 'Sr25519', // Optional key type (default: Ed25519) |
299 | | - chains: [ |
300 | | - { name: 'westend', url: 'wss://westend-rpc.polkadot.io' } |
301 | | - ] |
302 | | -}); |
303 | | -``` |
304 | | - |
305 | | -### Using Both Key Types and Delegation |
306 | | - |
307 | | -You can mix key types and use both private keys and mnemonics with delegation: |
308 | | - |
309 | | -```typescript |
310 | | -// Advanced configuration with different key types |
311 | | -const agent = new PolkadotAgentKit({ |
312 | | - // Main account with mnemonic |
313 | | - mnemonic: 'word1 word2 word3 ... word12', |
314 | | - derivationPath: '//0', |
315 | | - keyType: 'Sr25519', |
316 | | - |
317 | | - // Delegate account with private key |
318 | | - delegatePrivateKey: '0x1234...', |
319 | | - delegateKeyType: 'Ed25519', |
320 | | - |
321 | | - // Or delegate with mnemonic |
322 | | - // delegateMnemonic: 'word1 word2 word3 ... word12', |
323 | | - // delegateDerivationPath: '//1', |
324 | | - // delegateKeyType: 'Sr25519', |
325 | | - |
326 | | - chains: [ |
327 | | - { name: 'westend', url: 'wss://westend-rpc.polkadot.io' } |
328 | | - ] |
329 | | -}); |
330 | | -``` |
331 | | - |
332 | | -### Available Key Types |
333 | | - |
334 | | -| Key Type | Description | |
335 | | -|----------|-------------| |
336 | | -| `Ed25519` | Edwards-curve Digital Signature Algorithm (EdDSA) with 255-bit curve. Default key type. | |
337 | | -| `Sr25519` | Schnorrkel/Ristretto signatures on the Ristretto group on curve25519. Used by Polkadot ecosystem for account keys. | |
338 | | - |
339 | | ---- |
340 | | - |
341 | 61 | ## 🤝 Contributing |
342 | 62 |
|
343 | 63 | We welcome contributions from the community! Please: |
|
0 commit comments