@coinbase/agentkit 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +305 -0
- package/dist/action-providers/actionDecorator.d.ts +69 -0
- package/dist/action-providers/actionDecorator.js +96 -0
- package/dist/action-providers/actionProvider.d.ts +48 -0
- package/dist/action-providers/actionProvider.js +62 -0
- package/dist/action-providers/basename/basenameActionProvider.d.ts +30 -0
- package/dist/action-providers/basename/basenameActionProvider.js +109 -0
- package/dist/action-providers/basename/basenameActionProvider.test.d.ts +1 -0
- package/dist/action-providers/basename/basenameActionProvider.test.js +146 -0
- package/dist/action-providers/basename/constants.d.ts +52 -0
- package/dist/action-providers/basename/constants.js +81 -0
- package/dist/action-providers/basename/index.d.ts +2 -0
- package/dist/action-providers/basename/index.js +18 -0
- package/dist/action-providers/basename/schemas.d.ts +14 -0
- package/dist/action-providers/basename/schemas.js +14 -0
- package/dist/action-providers/cdp/cdpApiActionProvider.d.ts +41 -0
- package/dist/action-providers/cdp/cdpApiActionProvider.js +111 -0
- package/dist/action-providers/cdp/cdpApiActionProvider.test.d.ts +1 -0
- package/dist/action-providers/cdp/cdpApiActionProvider.test.js +134 -0
- package/dist/action-providers/cdp/cdpWalletActionProvider.d.ts +58 -0
- package/dist/action-providers/cdp/cdpWalletActionProvider.js +218 -0
- package/dist/action-providers/cdp/cdpWalletActionProvider.test.d.ts +1 -0
- package/dist/action-providers/cdp/cdpWalletActionProvider.test.js +259 -0
- package/dist/action-providers/cdp/constants.d.ts +31 -0
- package/dist/action-providers/cdp/constants.js +34 -0
- package/dist/action-providers/cdp/index.d.ts +3 -0
- package/dist/action-providers/cdp/index.js +19 -0
- package/dist/action-providers/cdp/schemas.d.ts +91 -0
- package/dist/action-providers/cdp/schemas.js +77 -0
- package/dist/action-providers/customActionProvider.d.ts +30 -0
- package/dist/action-providers/customActionProvider.js +66 -0
- package/dist/action-providers/erc20/constants.d.ts +135 -0
- package/dist/action-providers/erc20/constants.js +191 -0
- package/dist/action-providers/erc20/erc20ActionProvider.d.ts +38 -0
- package/dist/action-providers/erc20/erc20ActionProvider.js +118 -0
- package/dist/action-providers/erc20/erc20ActionProvider.test.d.ts +1 -0
- package/dist/action-providers/erc20/erc20ActionProvider.test.js +117 -0
- package/dist/action-providers/erc20/index.d.ts +1 -0
- package/dist/action-providers/erc20/index.js +17 -0
- package/dist/action-providers/erc20/schemas.d.ts +27 -0
- package/dist/action-providers/erc20/schemas.js +26 -0
- package/dist/action-providers/erc721/constants.d.ts +236 -0
- package/dist/action-providers/erc721/constants.js +301 -0
- package/dist/action-providers/erc721/erc721ActionProvider.d.ts +46 -0
- package/dist/action-providers/erc721/erc721ActionProvider.js +164 -0
- package/dist/action-providers/erc721/erc721ActionProvider.test.d.ts +1 -0
- package/dist/action-providers/erc721/erc721ActionProvider.test.js +137 -0
- package/dist/action-providers/erc721/index.d.ts +1 -0
- package/dist/action-providers/erc721/index.js +17 -0
- package/dist/action-providers/erc721/schemas.d.ts +46 -0
- package/dist/action-providers/erc721/schemas.js +44 -0
- package/dist/action-providers/farcaster/farcasterActionProvider.d.ts +57 -0
- package/dist/action-providers/farcaster/farcasterActionProvider.js +142 -0
- package/dist/action-providers/farcaster/farcasterActionProvider.test.d.ts +1 -0
- package/dist/action-providers/farcaster/farcasterActionProvider.test.js +143 -0
- package/dist/action-providers/farcaster/index.d.ts +2 -0
- package/dist/action-providers/farcaster/index.js +18 -0
- package/dist/action-providers/farcaster/schemas.d.ts +15 -0
- package/dist/action-providers/farcaster/schemas.js +20 -0
- package/dist/action-providers/index.d.ts +13 -0
- package/dist/action-providers/index.js +29 -0
- package/dist/action-providers/morpho/constants.d.ts +16 -0
- package/dist/action-providers/morpho/constants.js +27 -0
- package/dist/action-providers/morpho/index.d.ts +2 -0
- package/dist/action-providers/morpho/index.js +18 -0
- package/dist/action-providers/morpho/morphoActionProvider.d.ts +39 -0
- package/dist/action-providers/morpho/morphoActionProvider.js +147 -0
- package/dist/action-providers/morpho/morphoActionProvider.test.d.ts +1 -0
- package/dist/action-providers/morpho/morphoActionProvider.test.js +126 -0
- package/dist/action-providers/morpho/schemas.d.ts +36 -0
- package/dist/action-providers/morpho/schemas.js +47 -0
- package/dist/action-providers/pyth/index.d.ts +2 -0
- package/dist/action-providers/pyth/index.js +18 -0
- package/dist/action-providers/pyth/pythActionProvider.d.ts +33 -0
- package/dist/action-providers/pyth/pythActionProvider.js +121 -0
- package/dist/action-providers/pyth/schemas.d.ts +21 -0
- package/dist/action-providers/pyth/schemas.js +20 -0
- package/dist/action-providers/twitter/index.d.ts +2 -0
- package/dist/action-providers/twitter/index.js +18 -0
- package/dist/action-providers/twitter/schemas.d.ts +38 -0
- package/dist/action-providers/twitter/schemas.js +44 -0
- package/dist/action-providers/twitter/twitterActionProvider.d.ts +82 -0
- package/dist/action-providers/twitter/twitterActionProvider.js +204 -0
- package/dist/action-providers/twitter/twitterActionProvider.test.d.ts +1 -0
- package/dist/action-providers/twitter/twitterActionProvider.test.js +185 -0
- package/dist/action-providers/wallet/index.d.ts +1 -0
- package/dist/action-providers/wallet/index.js +17 -0
- package/dist/action-providers/wallet/schemas.d.ts +19 -0
- package/dist/action-providers/wallet/schemas.js +19 -0
- package/dist/action-providers/wallet/walletActionProvider.d.ts +44 -0
- package/dist/action-providers/wallet/walletActionProvider.js +127 -0
- package/dist/action-providers/wallet/walletActionProvider.test.d.ts +1 -0
- package/dist/action-providers/wallet/walletActionProvider.test.js +116 -0
- package/dist/action-providers/weth/constants.d.ts +19 -0
- package/dist/action-providers/weth/constants.js +29 -0
- package/dist/action-providers/weth/index.d.ts +1 -0
- package/dist/action-providers/weth/index.js +17 -0
- package/dist/action-providers/weth/schemas.d.ts +8 -0
- package/dist/action-providers/weth/schemas.js +10 -0
- package/dist/action-providers/weth/wethActionProvider.d.ts +30 -0
- package/dist/action-providers/weth/wethActionProvider.js +89 -0
- package/dist/action-providers/weth/wethActionProvider.test.d.ts +1 -0
- package/dist/action-providers/weth/wethActionProvider.test.js +92 -0
- package/dist/action-providers/wow/constants.d.ts +15 -0
- package/dist/action-providers/wow/constants.js +844 -0
- package/dist/action-providers/wow/index.d.ts +2 -0
- package/dist/action-providers/wow/index.js +18 -0
- package/dist/action-providers/wow/schemas.d.ts +43 -0
- package/dist/action-providers/wow/schemas.js +47 -0
- package/dist/action-providers/wow/uniswap/constants.d.ts +3 -0
- package/dist/action-providers/wow/uniswap/constants.js +100 -0
- package/dist/action-providers/wow/uniswap/utils.d.ts +82 -0
- package/dist/action-providers/wow/uniswap/utils.js +226 -0
- package/dist/action-providers/wow/utils.d.ts +27 -0
- package/dist/action-providers/wow/utils.js +63 -0
- package/dist/action-providers/wow/wowActionProvider.d.ts +46 -0
- package/dist/action-providers/wow/wowActionProvider.js +223 -0
- package/dist/action-providers/wow/wowActionProvider.test.d.ts +1 -0
- package/dist/action-providers/wow/wowActionProvider.test.js +287 -0
- package/dist/agentkit.d.ts +45 -0
- package/dist/agentkit.js +60 -0
- package/dist/analytics/index.d.ts +1 -0
- package/dist/analytics/index.js +17 -0
- package/dist/analytics/sendAnalyticsEvent.d.ts +31 -0
- package/dist/analytics/sendAnalyticsEvent.js +51 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +22 -0
- package/dist/network/index.d.ts +1 -0
- package/dist/network/index.js +17 -0
- package/dist/network/network.d.ts +13 -0
- package/dist/network/network.js +41 -0
- package/dist/network/types.d.ts +17 -0
- package/dist/network/types.js +2 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.js +43 -0
- package/dist/utils.test.d.ts +1 -0
- package/dist/utils.test.js +50 -0
- package/dist/wallet-providers/cdpWalletProvider.d.ts +218 -0
- package/dist/wallet-providers/cdpWalletProvider.js +362 -0
- package/dist/wallet-providers/evmWalletProvider.d.ts +51 -0
- package/dist/wallet-providers/evmWalletProvider.js +14 -0
- package/dist/wallet-providers/index.d.ts +4 -0
- package/dist/wallet-providers/index.js +20 -0
- package/dist/wallet-providers/viemWalletProvider.d.ts +89 -0
- package/dist/wallet-providers/viemWalletProvider.js +187 -0
- package/dist/wallet-providers/walletProvider.d.ts +48 -0
- package/dist/wallet-providers/walletProvider.js +41 -0
- package/package.json +65 -0
package/README.md
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
# Agentkit
|
|
2
|
+
|
|
3
|
+
AgentKit is a framework for easily enabling AI agents to take actions onchain. It is designed to be framework-agnostic, so you can use it with any AI framework, and wallet-agnostic, so you can use it with any wallet.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [Agentkit](#agentkit)
|
|
8
|
+
- [Getting Started](#getting-started)
|
|
9
|
+
- [Installation](#installation)
|
|
10
|
+
- [Usage](#usage)
|
|
11
|
+
- [Create an AgentKit instance](#1-create-an-agentkit-instance)
|
|
12
|
+
- [Create an AgentKit instance with a specified wallet provider](#2-create-an-agentkit-instance-with-a-specified-wallet-provider)
|
|
13
|
+
- [Create an AgentKit instance with a specified action providers](#3-create-an-agentkit-instance-with-a-specified-action-providers)
|
|
14
|
+
- [Wallet Providers](#wallet-providers)
|
|
15
|
+
- [CdpWalletProvider](#cdpwalletprovider)
|
|
16
|
+
- [Network Configuration](#network-configuration)
|
|
17
|
+
- [Configuring from an existing CDP API Wallet](#configuring-from-an-existing-cdp-api-wallet)
|
|
18
|
+
- [Configuring from a mnemonic phrase](#configuring-from-a-mnemonic-phrase)
|
|
19
|
+
- [Exporting a wallet](#exporting-a-wallet)
|
|
20
|
+
- [Importing a wallet from WalletData JSON string](#importing-a-wallet-from-walletdata-json-string)
|
|
21
|
+
- [ViemWalletProvider](#viemwalletprovider)
|
|
22
|
+
- [Contributing](#contributing)
|
|
23
|
+
|
|
24
|
+
## Getting Started
|
|
25
|
+
|
|
26
|
+
*Prerequisites*:
|
|
27
|
+
- [Node.js 18+](https://nodejs.org/en/download/)
|
|
28
|
+
- [CDP Secret API Key](https://docs.cdp.coinbase.com/get-started/docs/cdp-api-keys#creating-secret-api-keys)
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm install @coinbase/agentkit
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
1. Create an AgentKit instance. If no wallet or action providers are specified, the agent will use the `CdpWalletProvider` and `WalletProvider` action provider.
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
const agentKit = await AgentKit.from({
|
|
42
|
+
cdpApiKeyName: "CDP API KEY NAME",
|
|
43
|
+
cdpApiKeyPrivate: "CDP API KEY PRIVATE KEY",
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
2. Create an AgentKit instance with a specified wallet provider.
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { CdpWalletProvider } from "@coinbase/agentkit";
|
|
51
|
+
|
|
52
|
+
const walletProvider = await CdpWalletProvider.configureWithWallet({
|
|
53
|
+
apiKeyName: "CDP API KEY NAME",
|
|
54
|
+
apiKeyPrivate: "CDP API KEY PRIVATE KEY",
|
|
55
|
+
networkId: "base-mainnet",
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const agentKit = await AgentKit.from({
|
|
59
|
+
walletProvider,
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
3. Create an AgentKit instance with a specified action providers.
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { cdpApiActionProvider, pythActionProvider } from "@coinbase/agentkit";
|
|
67
|
+
|
|
68
|
+
const agentKit = await AgentKit.from({
|
|
69
|
+
walletProvider,
|
|
70
|
+
actionProviders: [
|
|
71
|
+
cdpApiActionProvider({
|
|
72
|
+
apiKeyName: "CDP API KEY NAME",
|
|
73
|
+
apiKeyPrivate: "CDP API KEY PRIVATE KEY",
|
|
74
|
+
}),
|
|
75
|
+
pythActionProvider(),
|
|
76
|
+
],
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
4. Use the agent's actions with a framework extension. For example, using LangChain + OpenAI.
|
|
81
|
+
|
|
82
|
+
*Prerequisites*:
|
|
83
|
+
- [OpenAI API Key](https://help.openai.com/en/articles/4936850-where-do-i-find-my-openai-api-key)
|
|
84
|
+
- Set `OPENAI_API_KEY` environment variable.
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
npm install @langchain @langchain/langgraph @langchain/openai
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { getLangChainTools } from "@coinbase/agentkit-langchain";
|
|
92
|
+
import { createReactAgent } from "@langchain/langgraph/prebuilt";
|
|
93
|
+
import { ChatOpenAI } from "@langchain/openai";
|
|
94
|
+
|
|
95
|
+
const tools = await getLangChainTools(agentKit);
|
|
96
|
+
|
|
97
|
+
const llm = new ChatOpenAI({
|
|
98
|
+
model: "gpt-4o-mini",
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const agent = createReactAgent({
|
|
102
|
+
llm,
|
|
103
|
+
tools,
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Creating an Action Provider
|
|
108
|
+
|
|
109
|
+
Action providers are used to define the actions that an agent can take. They are defined as a class that extends the `ActionProvider` abstract class.
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { ActionProvider, WalletProvider, Network } from "@coinbase/agentkit";
|
|
113
|
+
|
|
114
|
+
// Define an action provider that uses a wallet provider.
|
|
115
|
+
class MyActionProvider extends ActionProvider<WalletProvider> {
|
|
116
|
+
constructor() {
|
|
117
|
+
super("my-action-provider", []);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Define if the action provider supports the given network
|
|
121
|
+
supportsNetwork = (network: Network) => true;
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Adding Actions to your Action Provider
|
|
126
|
+
|
|
127
|
+
Actions are defined as instance methods on the action provider class with the `@CreateAction` decorator. Actions can use a wallet provider or not and always return a Promise that resolves to a string.
|
|
128
|
+
|
|
129
|
+
1. Define the action schema. Action schemas are defined using the `zod` library.
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import { z } from "zod";
|
|
133
|
+
|
|
134
|
+
export const MyActionSchema = z.object({
|
|
135
|
+
myField: z.string(),
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
2. Define the action.
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { ActionProvider, WalletProvider, Network, CreateAction } from "@coinbase/agentkit";
|
|
143
|
+
|
|
144
|
+
class MyActionProvider extends ActionProvider<WalletProvider> {
|
|
145
|
+
constructor() {
|
|
146
|
+
super("my-action-provider", []);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
@CreateAction({
|
|
150
|
+
name: "my-action",
|
|
151
|
+
description: "My action description",
|
|
152
|
+
schema: MyActionSchema,
|
|
153
|
+
})
|
|
154
|
+
async myAction(args: z.infer<typeof MyActionSchema>): Promise<string> {
|
|
155
|
+
return args.myField;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
supportsNetwork = (network: Network) => true;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export const myActionProvider = () => new MyActionProvider();
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
#### Adding Actions to your Action Provider that use a Wallet Provider
|
|
165
|
+
|
|
166
|
+
Actions that use a wallet provider can be defined as instance methods on the action provider class with the `@CreateAction` decorator that have a `WalletProvider` as the first parameter.
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
class MyActionProvider extends ActionProvider<WalletProvider> {
|
|
170
|
+
constructor() {
|
|
171
|
+
super("my-action-provider", []);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
@CreateAction({
|
|
175
|
+
name: "my-action",
|
|
176
|
+
description: "My action description",
|
|
177
|
+
schema: MyActionSchema,
|
|
178
|
+
})
|
|
179
|
+
async myAction(walletProvider: WalletProvider, args: z.infer<typeof MyActionSchema>): Promise<string> {
|
|
180
|
+
return walletProvider.signMessage(args.myField);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
supportsNetwork = (network: Network) => true;
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Adding an Action Provider to your AgentKit instance.
|
|
188
|
+
|
|
189
|
+
This gives your agent access to the actions defined in the action provider.
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
const agentKit = new AgentKit({
|
|
193
|
+
cdpApiKeyName: "CDP API KEY NAME",
|
|
194
|
+
cdpApiKeyPrivate: "CDP API KEY PRIVATE KEY",
|
|
195
|
+
actionProviders: [myActionProvider()],
|
|
196
|
+
});
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Wallet Providers
|
|
200
|
+
|
|
201
|
+
Wallet providers give an agent access to a wallet. AgentKit currently supports the following wallet providers:
|
|
202
|
+
|
|
203
|
+
EVM:
|
|
204
|
+
- [CdpWalletProvider](./src/wallet-providers/cdpWalletProvider.ts)
|
|
205
|
+
- [ViemWalletProvider](./src/wallet-providers/viemWalletProvider.ts)
|
|
206
|
+
|
|
207
|
+
### CdpWalletProvider
|
|
208
|
+
|
|
209
|
+
The `CdpWalletProvider` is a wallet provider that uses the Coinbase Developer Platform (CDP) [API Wallet](https://docs.cdp.coinbase.com/wallet-api/docs/welcome).
|
|
210
|
+
|
|
211
|
+
#### Network Configuration
|
|
212
|
+
|
|
213
|
+
The `CdpWalletProvider` can be configured to use a specific network by passing the `networkId` parameter to the `configureWithWallet` method. The `networkId` is the ID of the network you want to use. You can find a list of [supported networks on the CDP API docs](https://docs.cdp.coinbase.com/cdp-apis/docs/networks).
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
import { CdpWalletProvider } from "@coinbase/agentkit";
|
|
217
|
+
|
|
218
|
+
const walletProvider = await CdpWalletProvider.configureWithWallet({
|
|
219
|
+
apiKeyName: "CDP API KEY NAME",
|
|
220
|
+
apiKeyPrivate: "CDP API KEY PRIVATE KEY",
|
|
221
|
+
networkId: "base-mainnet",
|
|
222
|
+
});
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
#### Configuring from an existing CDP API Wallet
|
|
226
|
+
|
|
227
|
+
If you already have a CDP API Wallet, you can configure the `CdpWalletProvider` by passing the `wallet` parameter to the `configureWithWallet` method.
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
import { CdpWalletProvider } from "@coinbase/agentkit";
|
|
231
|
+
import { Wallet } from "@coinbase/coinbase-sdk";
|
|
232
|
+
const walletProvider = await CdpWalletProvider.configureWithWallet({
|
|
233
|
+
wallet,
|
|
234
|
+
apiKeyName: "CDP API KEY NAME",
|
|
235
|
+
apiKeyPrivate: "CDP API KEY PRIVATE KEY",
|
|
236
|
+
});
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
#### Configuring from a mnemonic phrase
|
|
240
|
+
|
|
241
|
+
The `CdpWalletProvider` can be configured from a mnemonic phrase by passing the `mnemonicPhrase` parameter to the `configureWithWallet` method.
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
import { CdpWalletProvider } from "@coinbase/agentkit";
|
|
245
|
+
|
|
246
|
+
const walletProvider = await CdpWalletProvider.configureWithWallet({
|
|
247
|
+
mnemonicPhrase: "MNEMONIC PHRASE",
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
#### Exporting a wallet
|
|
252
|
+
|
|
253
|
+
The `CdpWalletProvider` can export a wallet by calling the `exportWallet` method.
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
import { CdpWalletProvider } from "@coinbase/agentkit";
|
|
257
|
+
|
|
258
|
+
const walletProvider = await CdpWalletProvider.configureWithWallet({
|
|
259
|
+
mnemonicPhrase: "MNEMONIC PHRASE",
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
const walletData = await walletProvider.exportWallet();
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
#### Importing a wallet from `WalletData` JSON string
|
|
266
|
+
|
|
267
|
+
The `CdpWalletProvider` can import a wallet from a `WalletData` JSON string by passing the `cdpWalletData` parameter to the `configureWithWallet` method.
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
import { CdpWalletProvider } from "@coinbase/agentkit";
|
|
271
|
+
|
|
272
|
+
const walletProvider = await CdpWalletProvider.configureWithWallet({
|
|
273
|
+
cdpWalletData: "WALLET DATA JSON STRING",
|
|
274
|
+
apiKeyName: "CDP API KEY NAME",
|
|
275
|
+
apiKeyPrivate: "CDP API KEY PRIVATE KEY",
|
|
276
|
+
});
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### ViemWalletProvider
|
|
280
|
+
|
|
281
|
+
The `ViemWalletProvider` is a wallet provider that uses the [Viem library](https://viem.sh/docs/getting-started). It is useful for interacting with any EVM-compatible chain.
|
|
282
|
+
|
|
283
|
+
```typescript
|
|
284
|
+
import { ViemWalletProvider } from "@coinbase/agentkit";
|
|
285
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
286
|
+
import { baseSepolia } from "viem/chains";
|
|
287
|
+
import { http } from "viem/transports";
|
|
288
|
+
import { createWalletClient } from "viem";
|
|
289
|
+
|
|
290
|
+
const account = privateKeyToAccount(
|
|
291
|
+
"0x4c0883a69102937d6231471b5dbb6208ffd70c02a813d7f2da1c54f2e3be9f38",
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
const client = createWalletClient({
|
|
295
|
+
account,
|
|
296
|
+
chain: baseSepolia,
|
|
297
|
+
transport: http(),
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
const walletProvider = new ViemWalletProvider(client);
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
## Contributing
|
|
304
|
+
|
|
305
|
+
See [CONTRIBUTING.md](../../CONTRIBUTING.md) for more information.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import "reflect-metadata";
|
|
3
|
+
/**
|
|
4
|
+
* Parameters for the create action decorator
|
|
5
|
+
*/
|
|
6
|
+
export interface CreateActionDecoratorParams {
|
|
7
|
+
/**
|
|
8
|
+
* The name of the action
|
|
9
|
+
*/
|
|
10
|
+
name: string;
|
|
11
|
+
/**
|
|
12
|
+
* The description of the action
|
|
13
|
+
*/
|
|
14
|
+
description: string;
|
|
15
|
+
/**
|
|
16
|
+
* The schema of the action
|
|
17
|
+
*/
|
|
18
|
+
schema: z.ZodSchema;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Metadata key for the action decorator
|
|
22
|
+
*/
|
|
23
|
+
export declare const ACTION_DECORATOR_KEY: unique symbol;
|
|
24
|
+
/**
|
|
25
|
+
* Metadata for AgentKit actions
|
|
26
|
+
*/
|
|
27
|
+
export interface ActionMetadata {
|
|
28
|
+
/**
|
|
29
|
+
* The name of the action
|
|
30
|
+
*/
|
|
31
|
+
name: string;
|
|
32
|
+
/**
|
|
33
|
+
* The description of the action
|
|
34
|
+
*/
|
|
35
|
+
description: string;
|
|
36
|
+
/**
|
|
37
|
+
* The schema of the action
|
|
38
|
+
*/
|
|
39
|
+
schema: z.ZodSchema;
|
|
40
|
+
/**
|
|
41
|
+
* The function to invoke the action
|
|
42
|
+
*/
|
|
43
|
+
invoke: (...args: any[]) => any;
|
|
44
|
+
/**
|
|
45
|
+
* The wallet provider to use for the action
|
|
46
|
+
*/
|
|
47
|
+
walletProvider: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A map of action names to their metadata
|
|
51
|
+
*/
|
|
52
|
+
export type StoredActionMetadata = Map<string, ActionMetadata>;
|
|
53
|
+
/**
|
|
54
|
+
* Decorator to embed metadata on class methods to indicate they are actions accessible to the agent
|
|
55
|
+
*
|
|
56
|
+
* @param params - The parameters for the action decorator
|
|
57
|
+
* @returns A decorator function
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* class MyActionProvider extends ActionProvider {
|
|
62
|
+
* @CreateAction({ name: "my_action", description: "My action", schema: myActionSchema })
|
|
63
|
+
* public myAction(args: z.infer<typeof myActionSchema>) {
|
|
64
|
+
* // ...
|
|
65
|
+
* }
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export declare function CreateAction(params: CreateActionDecoratorParams): (target: object, propertyKey: string, descriptor: PropertyDescriptor) => object;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ACTION_DECORATOR_KEY = void 0;
|
|
4
|
+
exports.CreateAction = CreateAction;
|
|
5
|
+
const wallet_providers_1 = require("../wallet-providers");
|
|
6
|
+
const analytics_1 = require("../analytics");
|
|
7
|
+
require("reflect-metadata");
|
|
8
|
+
/**
|
|
9
|
+
* Metadata key for the action decorator
|
|
10
|
+
*/
|
|
11
|
+
exports.ACTION_DECORATOR_KEY = Symbol("agentkit:action");
|
|
12
|
+
/**
|
|
13
|
+
* Decorator to embed metadata on class methods to indicate they are actions accessible to the agent
|
|
14
|
+
*
|
|
15
|
+
* @param params - The parameters for the action decorator
|
|
16
|
+
* @returns A decorator function
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* class MyActionProvider extends ActionProvider {
|
|
21
|
+
* @CreateAction({ name: "my_action", description: "My action", schema: myActionSchema })
|
|
22
|
+
* public myAction(args: z.infer<typeof myActionSchema>) {
|
|
23
|
+
* // ...
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
function CreateAction(params) {
|
|
29
|
+
return (target, propertyKey, descriptor) => {
|
|
30
|
+
const prefixedActionName = `${target.constructor.name}_${params.name}`;
|
|
31
|
+
const originalMethod = descriptor.value;
|
|
32
|
+
const { isWalletProvider } = validateActionMethodArguments(target, propertyKey);
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
34
|
+
descriptor.value = function (...args) {
|
|
35
|
+
let walletMetrics = {};
|
|
36
|
+
if (isWalletProvider) {
|
|
37
|
+
walletMetrics = {
|
|
38
|
+
wallet_provider: args[0].getName(),
|
|
39
|
+
wallet_address: args[0].getAddress(),
|
|
40
|
+
network_id: args[0].getNetwork().networkId,
|
|
41
|
+
chain_id: args[0].getNetwork().chainId,
|
|
42
|
+
protocol_family: args[0].getNetwork().protocolFamily,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
(0, analytics_1.sendAnalyticsEvent)({
|
|
46
|
+
name: "agent_action_invocation",
|
|
47
|
+
action: "invoke_action",
|
|
48
|
+
component: "agent_action",
|
|
49
|
+
action_name: prefixedActionName,
|
|
50
|
+
class_name: target.constructor.name,
|
|
51
|
+
method_name: propertyKey,
|
|
52
|
+
...walletMetrics,
|
|
53
|
+
});
|
|
54
|
+
return originalMethod.apply(this, args);
|
|
55
|
+
};
|
|
56
|
+
const existingMetadata = Reflect.getMetadata(exports.ACTION_DECORATOR_KEY, target.constructor) || new Map();
|
|
57
|
+
const metaData = {
|
|
58
|
+
name: prefixedActionName,
|
|
59
|
+
description: params.description,
|
|
60
|
+
schema: params.schema,
|
|
61
|
+
invoke: descriptor.value,
|
|
62
|
+
walletProvider: isWalletProvider,
|
|
63
|
+
};
|
|
64
|
+
existingMetadata.set(propertyKey, metaData);
|
|
65
|
+
Reflect.defineMetadata(exports.ACTION_DECORATOR_KEY, existingMetadata, target.constructor);
|
|
66
|
+
return target;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Validates the arguments of an action method
|
|
71
|
+
*
|
|
72
|
+
* @param target - The target object
|
|
73
|
+
* @param propertyKey - The property key
|
|
74
|
+
* @returns An object containing the wallet provider flag
|
|
75
|
+
*/
|
|
76
|
+
function validateActionMethodArguments(target, propertyKey) {
|
|
77
|
+
const className = target instanceof Object ? target.constructor.name : undefined;
|
|
78
|
+
const params = Reflect.getMetadata("design:paramtypes", target, propertyKey);
|
|
79
|
+
if (params == null) {
|
|
80
|
+
throw new Error(`Failed to get parameters for action method ${propertyKey} on class ${className}`);
|
|
81
|
+
}
|
|
82
|
+
if (params.length > 2) {
|
|
83
|
+
throw new Error(`Action method ${propertyKey} on class ${className} has more than 2 parameters`);
|
|
84
|
+
}
|
|
85
|
+
const walletProviderParam = params.find(param => {
|
|
86
|
+
if (!param || !param.prototype) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
if (param === wallet_providers_1.WalletProvider)
|
|
90
|
+
return true;
|
|
91
|
+
return param.prototype instanceof wallet_providers_1.WalletProvider;
|
|
92
|
+
});
|
|
93
|
+
return {
|
|
94
|
+
isWalletProvider: !!walletProviderParam,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { WalletProvider } from "../wallet-providers";
|
|
3
|
+
import { Network } from "../network";
|
|
4
|
+
/**
|
|
5
|
+
* Action is the interface for all actions.
|
|
6
|
+
*/
|
|
7
|
+
export interface Action<TActionSchema extends z.ZodSchema = z.ZodSchema> {
|
|
8
|
+
name: string;
|
|
9
|
+
description: string;
|
|
10
|
+
schema: TActionSchema;
|
|
11
|
+
invoke: (args: z.infer<TActionSchema>) => Promise<string>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* ActionProvider is the abstract base class for all action providers.
|
|
15
|
+
*
|
|
16
|
+
* @abstract
|
|
17
|
+
*/
|
|
18
|
+
export declare abstract class ActionProvider<TWalletProvider extends WalletProvider = WalletProvider> {
|
|
19
|
+
/**
|
|
20
|
+
* The name of the action provider.
|
|
21
|
+
*/
|
|
22
|
+
readonly name: string;
|
|
23
|
+
/**
|
|
24
|
+
* The action providers to combine.
|
|
25
|
+
*/
|
|
26
|
+
readonly actionProviders: ActionProvider<TWalletProvider>[];
|
|
27
|
+
/**
|
|
28
|
+
* The constructor for the action provider.
|
|
29
|
+
*
|
|
30
|
+
* @param name - The name of the action provider.
|
|
31
|
+
* @param actionProviders - The action providers to combine.
|
|
32
|
+
*/
|
|
33
|
+
constructor(name: string, actionProviders: ActionProvider<TWalletProvider>[]);
|
|
34
|
+
/**
|
|
35
|
+
* Gets the actions of the action provider bound to the given wallet provider.
|
|
36
|
+
*
|
|
37
|
+
* @param walletProvider - The wallet provider.
|
|
38
|
+
* @returns The actions of the action provider.
|
|
39
|
+
*/
|
|
40
|
+
getActions(walletProvider: TWalletProvider): Action[];
|
|
41
|
+
/**
|
|
42
|
+
* Checks if the action provider supports the given network.
|
|
43
|
+
*
|
|
44
|
+
* @param network - The network to check.
|
|
45
|
+
* @returns True if the action provider supports the network, false otherwise.
|
|
46
|
+
*/
|
|
47
|
+
abstract supportsNetwork(network: Network): boolean;
|
|
48
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ActionProvider = void 0;
|
|
4
|
+
const actionDecorator_1 = require("./actionDecorator");
|
|
5
|
+
/**
|
|
6
|
+
* ActionProvider is the abstract base class for all action providers.
|
|
7
|
+
*
|
|
8
|
+
* @abstract
|
|
9
|
+
*/
|
|
10
|
+
class ActionProvider {
|
|
11
|
+
/**
|
|
12
|
+
* The constructor for the action provider.
|
|
13
|
+
*
|
|
14
|
+
* @param name - The name of the action provider.
|
|
15
|
+
* @param actionProviders - The action providers to combine.
|
|
16
|
+
*/
|
|
17
|
+
constructor(name,
|
|
18
|
+
// Update parameter type to match property type
|
|
19
|
+
actionProviders) {
|
|
20
|
+
this.name = name;
|
|
21
|
+
this.actionProviders = actionProviders;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Gets the actions of the action provider bound to the given wallet provider.
|
|
25
|
+
*
|
|
26
|
+
* @param walletProvider - The wallet provider.
|
|
27
|
+
* @returns The actions of the action provider.
|
|
28
|
+
*/
|
|
29
|
+
getActions(walletProvider) {
|
|
30
|
+
const actions = [];
|
|
31
|
+
const actionProviders = [this, ...this.actionProviders];
|
|
32
|
+
for (const actionProvider of actionProviders) {
|
|
33
|
+
const actionsMetadataMap = Reflect.getMetadata(actionDecorator_1.ACTION_DECORATOR_KEY, actionProvider.constructor);
|
|
34
|
+
if (!actionsMetadataMap) {
|
|
35
|
+
if (!(actionProvider instanceof ActionProvider)) {
|
|
36
|
+
console.warn(`Warning: ${actionProvider} is not an instance of ActionProvider.`);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
console.warn(`Warning: ${actionProvider} has no actions.`);
|
|
40
|
+
}
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
for (const actionMetadata of actionsMetadataMap.values()) {
|
|
44
|
+
actions.push({
|
|
45
|
+
name: actionMetadata.name,
|
|
46
|
+
description: actionMetadata.description,
|
|
47
|
+
schema: actionMetadata.schema,
|
|
48
|
+
invoke: schemaArgs => {
|
|
49
|
+
const args = [];
|
|
50
|
+
if (actionMetadata.walletProvider) {
|
|
51
|
+
args[0] = walletProvider;
|
|
52
|
+
}
|
|
53
|
+
args.push(schemaArgs);
|
|
54
|
+
return actionMetadata.invoke.apply(actionProvider, args);
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return actions;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.ActionProvider = ActionProvider;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ActionProvider } from "../actionProvider";
|
|
3
|
+
import { Network } from "../../network";
|
|
4
|
+
import { RegisterBasenameSchema } from "./schemas";
|
|
5
|
+
import { EvmWalletProvider } from "../../wallet-providers";
|
|
6
|
+
/**
|
|
7
|
+
* Action provider for registering Basenames.
|
|
8
|
+
*/
|
|
9
|
+
export declare class BasenameActionProvider extends ActionProvider {
|
|
10
|
+
/**
|
|
11
|
+
* Constructs a new BasenameActionProvider.
|
|
12
|
+
*/
|
|
13
|
+
constructor();
|
|
14
|
+
/**
|
|
15
|
+
* Registers a Basename.
|
|
16
|
+
*
|
|
17
|
+
* @param wallet - The wallet to use for the registration.
|
|
18
|
+
* @param args - The arguments for the registration.
|
|
19
|
+
* @returns A string indicating the success or failure of the registration.
|
|
20
|
+
*/
|
|
21
|
+
register(wallet: EvmWalletProvider, args: z.infer<typeof RegisterBasenameSchema>): Promise<string>;
|
|
22
|
+
/**
|
|
23
|
+
* Checks if the Basename action provider supports the given network.
|
|
24
|
+
*
|
|
25
|
+
* @param network - The network to check.
|
|
26
|
+
* @returns True if the Basename action provider supports the network, false otherwise.
|
|
27
|
+
*/
|
|
28
|
+
supportsNetwork: (network: Network) => boolean;
|
|
29
|
+
}
|
|
30
|
+
export declare const basenameActionProvider: () => BasenameActionProvider;
|