forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmintNFTAction.ts
320 lines (306 loc) · 10.3 KB
/
mintNFTAction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import {
Action,
composeContext,
elizaLogger,
generateObject,
HandlerCallback,
IAgentRuntime,
Memory,
ModelClass,
State,
} from "@elizaos/core";
import { createNFT } from "../handlers/createNFT.ts";
import { verifyNFT } from "../handlers/verifyNFT.ts";
import { sleep } from "../index.ts";
import WalletSolana from "../provider/wallet/walletSolana.ts";
import { PublicKey } from "@solana/web3.js";
import { mintNFTTemplate } from "../templates.ts";
import { MintNFTContent, MintNFTSchema } from "../types.ts";
function isMintNFTContent(content: any): content is MintNFTContent {
return typeof content.collectionAddress === "string";
}
const mintNFTAction: Action = {
name: "MINT_NFT",
similes: [
"NFT_MINTING",
"NFT_CREATION",
"CREATE_NFT",
"GENERATE_NFT",
"MINT_TOKEN",
"CREATE_TOKEN",
"MAKE_NFT",
"TOKEN_GENERATION",
],
description: "Mint NFTs for the collection",
validate: async (runtime: IAgentRuntime, _message: Memory) => {
const awsAccessKeyIdOk = !!runtime.getSetting("AWS_ACCESS_KEY_ID");
const awsSecretAccessKeyOk = !!runtime.getSetting(
"AWS_SECRET_ACCESS_KEY"
);
const awsRegionOk = !!runtime.getSetting("AWS_REGION");
const awsS3BucketOk = !!runtime.getSetting("AWS_S3_BUCKET");
const solanaAdminPrivateKeyOk = !!runtime.getSetting(
"SOLANA_ADMIN_PRIVATE_KEY"
);
const solanaAdminPublicKeyOk = !!runtime.getSetting(
"SOLANA_ADMIN_PUBLIC_KEY"
);
return (
awsAccessKeyIdOk ||
awsSecretAccessKeyOk ||
awsRegionOk ||
awsS3BucketOk ||
solanaAdminPrivateKeyOk ||
solanaAdminPublicKeyOk
);
},
handler: async (
runtime: IAgentRuntime,
message: Memory,
state: State,
options: { [key: string]: unknown },
callback: HandlerCallback
) => {
try {
elizaLogger.log("Composing state for message:", message);
if (!state) {
state = (await runtime.composeState(message)) as State;
} else {
state = await runtime.updateRecentMessageState(state);
}
// Compose transfer context
const transferContext = composeContext({
state,
template: mintNFTTemplate,
});
const res = await generateObject({
runtime,
context: transferContext,
modelClass: ModelClass.LARGE,
schema: MintNFTSchema,
});
const content = res.object;
elizaLogger.log("Generate Object:", content);
if (!isMintNFTContent(content)) {
elizaLogger.error("Invalid content for MINT_NFT action.");
if (callback) {
callback({
text: "Unable to process mint request. Invalid content provided.",
content: { error: "Invalid mint content" },
});
}
return false;
}
const publicKey = runtime.getSetting("SOLANA_PUBLIC_KEY");
const privateKey = runtime.getSetting("SOLANA_PRIVATE_KEY");
const wallet = new WalletSolana(
new PublicKey(publicKey),
privateKey
);
const collectionInfo = await wallet.fetchDigitalAsset(
content.collectionAddress
);
elizaLogger.log("Collection Info", collectionInfo);
const metadata = collectionInfo.metadata;
if (metadata.collection?.["value"]) {
callback({
text: `Unable to process mint request. Invalid collection address ${content.collectionAddress}.`,
content: { error: "Invalid collection address." },
});
return false;
}
if (metadata) {
const nftRes = await createNFT({
runtime,
collectionName: metadata.name,
collectionAddress: content.collectionAddress,
collectionAdminPublicKey: metadata.updateAuthority,
collectionFee: metadata.sellerFeeBasisPoints,
tokenId: 1,
});
elizaLogger.log("NFT Address:", nftRes);
if (nftRes) {
callback({
text: `Congratulations to you! 🎉🎉🎉 \nCollection Address: ${content.collectionAddress}\n NFT Address: ${nftRes.address}\n NFT Link: ${nftRes.link}`, //caption.description,
attachments: [],
});
await sleep(15000);
await verifyNFT({
runtime,
collectionAddress: content.collectionAddress,
NFTAddress: nftRes.address,
});
} else {
callback({
text: `Mint NFT Error in ${content.collectionAddress}.`,
content: { error: "Mint NFT Error." },
});
return false;
}
} else {
callback({
text: "Unable to process mint request. Invalid collection address.",
content: { error: "Invalid collection address." },
});
return false;
}
return [];
} catch (e: any) {
elizaLogger.log(e);
throw e;
}
},
examples: [
[
{
user: "{{user1}}",
content: {
text: "mint nft for collection: D8j4ubQ3MKwmAqiJw83qT7KQNKjhsuoC7zJJdJa5BkvS",
},
},
{
user: "{{agentName}}",
content: {
text: "I've minted a new NFT in your specified collection.",
action: "MINT_NFT",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Could you create an NFT in collection D8j4ubQ3MKwmAqiJw83qT7KQNKjhsuoC7zJJdJa5BkvS?",
},
},
{
user: "{{agentName}}",
content: {
text: "Successfully minted your NFT in the specified collection.",
action: "MINT_NFT",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Please mint a new token in D8j4ubQ3MKwmAqiJw83qT7KQNKjhsuoC7zJJdJa5BkvS collection",
},
},
{
user: "{{agentName}}",
content: {
text: "Your NFT has been minted in the collection successfully.",
action: "MINT_NFT",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Generate NFT for D8j4ubQ3MKwmAqiJw83qT7KQNKjhsuoC7zJJdJa5BkvS",
},
},
{
user: "{{agentName}}",
content: {
text: "I've generated and minted your NFT in the collection.",
action: "MINT_NFT",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "I want to mint an NFT in collection D8j4ubQ3MKwmAqiJw83qT7KQNKjhsuoC7zJJdJa5BkvS",
},
},
{
user: "{{agentName}}",
content: {
text: "Your NFT has been successfully minted in the collection.",
action: "MINT_NFT",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Create a new NFT token in D8j4ubQ3MKwmAqiJw83qT7KQNKjhsuoC7zJJdJa5BkvS collection",
},
},
{
user: "{{agentName}}",
content: {
text: "The NFT has been created in your specified collection.",
action: "MINT_NFT",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Issue an NFT for collection D8j4ubQ3MKwmAqiJw83qT7KQNKjhsuoC7zJJdJa5BkvS",
},
},
{
user: "{{agentName}}",
content: {
text: "I've issued your NFT in the requested collection.",
action: "MINT_NFT",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Make a new NFT in D8j4ubQ3MKwmAqiJw83qT7KQNKjhsuoC7zJJdJa5BkvS",
},
},
{
user: "{{agentName}}",
content: {
text: "Your new NFT has been minted in the collection.",
action: "MINT_NFT",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Can you mint an NFT for D8j4ubQ3MKwmAqiJw83qT7KQNKjhsuoC7zJJdJa5BkvS collection?",
},
},
{
user: "{{agentName}}",
content: {
text: "I've completed minting your NFT in the collection.",
action: "MINT_NFT",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Add a new NFT to collection D8j4ubQ3MKwmAqiJw83qT7KQNKjhsuoC7zJJdJa5BkvS",
},
},
{
user: "{{agentName}}",
content: {
text: "A new NFT has been added to your collection.",
action: "MINT_NFT",
},
},
],
],
} as Action;
export default mintNFTAction;