forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrikon.ts
155 lines (139 loc) · 4.6 KB
/
trikon.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
import { elizaLogger } from "@elizaos/core";
import {
type ActionExample,
type Content,
type HandlerCallback,
type IAgentRuntime,
type Memory,
ModelClass,
type State,
type Action,
} from "@elizaos/core";
import { composeContext } from "@elizaos/core";
import { generateObjectDeprecated } from "@elizaos/core";
export interface TransferContent extends Content {
recipient: string;
amount: string | number;
}
function isTransferContent(content: any): content is TransferContent {
elizaLogger.log("Content for transfer", content);
return (
typeof content.recipient === "string" &&
(typeof content.amount === "string" ||
typeof content.amount === "number")
);
}
const transferTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined.
Example response:
\`\`\`json
{
"recipient": "0x2badda48c062e861ef17a96a806c451fd296a49f45b272dee17f85b0e32663fd",
"amount": "1000"
}
\`\`\`
{{recentMessages}}
Given the recent messages, extract the following information about the requested token transfer:
- Recipient wallet address
- Amount to transfer
Respond with a JSON markdown block containing only the extracted values.`;
export default {
name: "SEND_TOKEN",
similes: [
"TRANSFER_TOKEN",
"TRANSFER_TOKENS",
"SEND_TOKENS",
"SEND_TRK",
"PAY",
],
validate: async (runtime: IAgentRuntime, message: Memory) => {
elizaLogger.log("Validating trikon transfer from user:", message.userId);
return false;
},
description: "Transfer tokens from the agent's wallet to another address",
handler: async (
runtime: IAgentRuntime,
message: Memory,
state: State,
_options: { [key: string]: unknown },
callback?: HandlerCallback
): Promise<boolean> => {
elizaLogger.log("Starting SEND_TOKEN handler...");
// Initialize or update state
if (!state) {
state = (await runtime.composeState(message)) as State;
} else {
state = await runtime.updateRecentMessageState(state);
}
// Compose transfer context
const transferContext = composeContext({
state,
template: transferTemplate,
});
// Generate transfer content
const content = await generateObjectDeprecated({
runtime,
context: transferContext,
modelClass: ModelClass.SMALL,
});
// Validate transfer content
if (!isTransferContent(content)) {
elizaLogger.error("Invalid content for TRANSFER_TOKEN action.");
if (callback) {
callback({
text: "Unable to process transfer request. Invalid content provided.",
content: { error: "Invalid transfer content" },
});
}
return false;
}
try {
// TODO: Implement Trikon-specific transfer logic here
elizaLogger.log(
`Would transfer ${content.amount} tokens to ${content.recipient}`
);
if (callback) {
callback({
text: `Transfer simulation successful for ${content.amount} TRK to ${content.recipient}`,
content: {
success: true,
amount: content.amount,
recipient: content.recipient,
},
});
}
return true;
} catch (error) {
elizaLogger.error("Error during token transfer:", error);
if (callback) {
callback({
text: `Error transferring tokens: ${error.message}`,
content: { error: error.message },
});
}
return false;
}
},
examples: [
[
{
user: "{{user1}}",
content: {
text: "Send 100 TRK tokens to 0xa385EEeFB533703dc4c811CB6Eb44cac2C14af07",
},
},
{
user: "{{user2}}",
content: {
text: "I'll send 100 TRK tokens now...",
action: "SEND_TOKEN",
},
},
{
user: "{{user2}}",
content: {
text: "Successfully sent 100 TRK tokens to 0xa385EEeFB533703dc4c811CB6Eb44cac2C14af07",
},
},
],
] as ActionExample[][],
} as Action;