Skip to content

Commit 95bbc92

Browse files
committed
feat: handle token deploy
1 parent 294e798 commit 95bbc92

File tree

1 file changed

+97
-1
lines changed

1 file changed

+97
-1
lines changed

packages/plugin-solana-agentkit/src/actions/createToken.ts

+97-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {
22
ActionExample,
3+
composeContext,
34
Content,
45
elizaLogger,
6+
generateObjectDeprecated,
57
HandlerCallback,
68
IAgentRuntime,
79
Memory,
@@ -10,6 +12,8 @@ import {
1012
type Action,
1113
} from "@elizaos/core";
1214

15+
import { SolanaAgentKit } from "solana-agent-kit";
16+
1317
export interface CreateTokenContent extends Content {
1418
name: string;
1519
uri: string;
@@ -32,6 +36,30 @@ function isCreateTokenContent(
3236
);
3337
}
3438

39+
const createTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined.
40+
41+
Example response:
42+
\`\`\`json
43+
{
44+
"name": "Example Token",
45+
"symbol": "EXMPL",
46+
"uri": "https://raw.githubusercontent.com/solana-developers/opos-asset/main/assets/CompressedCoil/image.png",
47+
"decimals": 18,
48+
"initialSupply": 1000000,
49+
}
50+
\`\`\`
51+
52+
{{recentMessages}}
53+
54+
Given the recent messages, extract the following information about the requested token transfer:
55+
- Token name
56+
- Token symbol
57+
- Token uri
58+
- Token decimals
59+
- Token initialSupply
60+
61+
Respond with a JSON markdown block containing only the extracted values.`;
62+
3563
export default {
3664
name: "CREATE_TOKEN",
3765
similes: ["DEPLOY_TOKEN"],
@@ -45,7 +73,75 @@ export default {
4573
callback?: HandlerCallback
4674
): Promise<boolean> => {
4775
elizaLogger.log("Starting CREATE_TOKEN handler...");
48-
return true;
76+
// Initialize or update state
77+
if (!state) {
78+
state = (await runtime.composeState(message)) as State;
79+
} else {
80+
state = await runtime.updateRecentMessageState(state);
81+
}
82+
83+
// Compose transfer context
84+
const transferContext = composeContext({
85+
state,
86+
template: createTemplate,
87+
});
88+
89+
// Generate transfer content
90+
const content = await generateObjectDeprecated({
91+
runtime,
92+
context: transferContext,
93+
modelClass: ModelClass.LARGE,
94+
});
95+
96+
// Validate transfer content
97+
if (!isCreateTokenContent(runtime, content)) {
98+
elizaLogger.error("Invalid content for CREATE_TOKEN action.");
99+
if (callback) {
100+
callback({
101+
text: "Unable to process create token request. Invalid content provided.",
102+
content: { error: "Invalid creat token content" },
103+
});
104+
}
105+
return false;
106+
}
107+
108+
elizaLogger.log("Init solana agent kit...");
109+
const solanaPrivatekey = runtime.getSetting("SOLANA_PRIVATE_KEY");
110+
const rpc = runtime.getSetting("RPC_URL");
111+
const openAIKey = runtime.getSetting("OPENAI_API_KEY");
112+
const solanaAgentKit = new SolanaAgentKit(
113+
solanaPrivatekey,
114+
rpc,
115+
openAIKey
116+
);
117+
try {
118+
const deployedAddress = solanaAgentKit.deployToken(
119+
content.name,
120+
content.uri,
121+
content.symbol,
122+
content.decimals,
123+
content.initialSupply
124+
);
125+
elizaLogger.log("Create successful: ", deployedAddress);
126+
if (callback) {
127+
callback({
128+
text: `Successfully create token ${content.name}`,
129+
content: {
130+
success: true,
131+
},
132+
});
133+
}
134+
return true;
135+
} catch (error) {
136+
if (callback) {
137+
elizaLogger.error("Error during create token: ", error);
138+
callback({
139+
text: `Error creating token: ${error.message}`,
140+
content: { error: error.message },
141+
});
142+
}
143+
return false;
144+
}
49145
},
50146
examples: [
51147
[

0 commit comments

Comments
 (0)