Skip to content

Commit d0fb057

Browse files
committed
Update Sui Token Transfer Action
``` You: transfer 100 sui to 0xaa000b3651bd1e57554ebd7308ca70df7c8c0e8e09d67123cc15c8a8a79342b3 ◎ LOGS Creating Memory a3cc6fec-88e8-079b-9b0f-67132847abb5 transfer 100 sui to 0xaa000b3651bd1e57554ebd7308ca70df7c8c0e8e09d67123cc15c8a8a79342b3 Validating sui transfer from user: 12dea96f-ec20-0935-a6ab-75692c994959 Validating sui transfer from user: 12dea96f-ec20-0935-a6ab-75692c994959 Cache hit for fetchPortfolioValue { totalUsd: '1.84213655964', totalSui: '1.8469386' } Cache hit for fetchPortfolioValue { totalUsd: '1.84213655964', totalSui: '1.8469386' } ["◎ Generating message response.."] ["◎ Generating text..."] ℹ INFORMATIONS Generating text with options: {"modelProvider":"openai","model":"small"} ℹ INFORMATIONS Selected model: gpt-4o-mini ◎ LOGS Creating Memory 100 SUI again? You’re really going all in. What’s the strategy? Trying to impress or just feeling extra generous today? ◎ LOGS Evaluating GET_FACTS ◎ LOGS Evaluating UPDATE_GOAL ["✓ Normalized action: sendtoken"] ["ℹ Executing handler for action: SEND_TOKEN"] ["◎ Starting SEND_TOKEN handler..."] Cache hit for fetchPortfolioValue { totalUsd: '1.84213655964', totalSui: '1.8469386' } Content for transfer { recipient: '0xaa000b3651bd1e57554ebd7308ca70df7c8c0e8e09d67123cc15c8a8a79342b3', amount: 100 } Transferring: 100 tokens (100000000000 base units) Transfer successful: 54ai65LEYMknCnbDJhffMXsHSgLoKVxGv1z796UwX7XL ["◎ Agent: 100 SUI again? You’re really going all in. What’s the strategy? Trying to impress or just feeling extra generous today? "] ["◎ Agent: Successfully transferred 100 SUI to 0xaa000b3651bd1e57554ebd7308ca70df7c8c0e8e09d67123cc15c8a8a79342b3, Transaction: 54ai65LEYMknCnbDJhffMXsHSgLoKVxGv1z796UwX7XL"] ```
1 parent af04019 commit d0fb057

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

packages/plugin-sui/src/actions/transfer.ts

+29-20
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
type Action,
1111
} from "@ai16z/eliza";
1212
import { composeContext } from "@ai16z/eliza";
13-
import { generateObjectDEPRECATED } from "@ai16z/eliza";
14-
13+
import { generateObjectV2 } from "@ai16z/eliza";
14+
import { z } from "zod";
1515

1616
import { SuiClient, getFullnodeUrl } from "@mysten/sui/client";
1717
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
@@ -27,7 +27,7 @@ export interface TransferContent extends Content {
2727
amount: string | number;
2828
}
2929

30-
function isTransferContent(content: any): content is TransferContent {
30+
function isTransferContent(content: Content): content is TransferContent {
3131
console.log("Content for transfer", content);
3232
return (
3333
typeof content.recipient === "string" &&
@@ -41,8 +41,8 @@ const transferTemplate = `Respond with a JSON markdown block containing only the
4141
Example response:
4242
\`\`\`json
4343
{
44-
"recipient": "0x2badda48c062e861ef17a96a806c451fd296a49f45b272dee17f85b0e32663fd",
45-
"amount": "1000"
44+
"recipient": "0xaa000b3651bd1e57554ebd7308ca70df7c8c0e8e09d67123cc15c8a8a79342b3",
45+
"amount": "1"
4646
}
4747
\`\`\`
4848
@@ -82,7 +82,7 @@ export default {
8282
return false;
8383
}
8484
*/
85-
return false;
85+
return true;
8686
},
8787
description: "Transfer tokens from the agent's wallet to another address",
8888
handler: async (
@@ -104,21 +104,30 @@ export default {
104104
state = await runtime.updateRecentMessageState(state);
105105
}
106106

107+
// Define the schema for the expected output
108+
const transferSchema = z.object({
109+
recipient: z.string(),
110+
amount: z.union([z.string(), z.number()]),
111+
});
112+
107113
// Compose transfer context
108114
const transferContext = composeContext({
109115
state,
110116
template: transferTemplate,
111117
});
112118

113-
// Generate transfer content
114-
const content = await generateObjectDEPRECATED({
119+
// Generate transfer content with the schema
120+
const content = await generateObjectV2({
115121
runtime,
116122
context: transferContext,
123+
schema: transferSchema,
117124
modelClass: ModelClass.SMALL,
118125
});
119126

127+
const transferContent = content.object as TransferContent;
128+
120129
// Validate transfer content
121-
if (!isTransferContent(content)) {
130+
if (!isTransferContent(transferContent)) {
122131
console.error("Invalid content for TRANSFER_TOKEN action.");
123132
if (callback) {
124133
callback({
@@ -131,21 +140,21 @@ export default {
131140

132141
try {
133142
const privateKey = runtime.getSetting("SUI_PRIVATE_KEY");
134-
const suiAccount = Ed25519Keypair.fromSecretKey(privateKey);
135-
const network = runtime.getSetting("SUI_NETWORK")
143+
const suiAccount = Ed25519Keypair.deriveKeypair(privateKey);
144+
const network = runtime.getSetting("SUI_NETWORK");
136145
const suiClient = new SuiClient({
137146
url: getFullnodeUrl(network as SuiNetwork),
138147
});
139148

140149
const adjustedAmount = BigInt(
141-
Number(content.amount) * Math.pow(10, SUI_DECIMALS)
150+
Number(transferContent.amount) * Math.pow(10, SUI_DECIMALS)
142151
);
143152
console.log(
144-
`Transferring: ${content.amount} tokens (${adjustedAmount} base units)`
153+
`Transferring: ${transferContent.amount} tokens (${adjustedAmount} base units)`
145154
);
146155
const tx = new Transaction();
147156
const [coin] = tx.splitCoins(tx.gas, [adjustedAmount]);
148-
tx.transferObjects([coin], content.recipient);
157+
tx.transferObjects([coin], transferContent.recipient);
149158
const executedTransaction =
150159
await suiClient.signAndExecuteTransaction({
151160
signer: suiAccount,
@@ -156,12 +165,12 @@ export default {
156165

157166
if (callback) {
158167
callback({
159-
text: `Successfully transferred ${content.amount} SUI to ${content.recipient}, Transaction: ${executedTransaction.digest}`,
168+
text: `Successfully transferred ${transferContent.amount} SUI to ${transferContent.recipient}, Transaction: ${executedTransaction.digest}`,
160169
content: {
161170
success: true,
162171
hash: executedTransaction.digest,
163-
amount: content.amount,
164-
recipient: content.recipient,
172+
amount: transferContent.amount,
173+
recipient: transferContent.recipient,
165174
},
166175
});
167176
}
@@ -184,20 +193,20 @@ export default {
184193
{
185194
user: "{{user1}}",
186195
content: {
187-
text: "Send 69 SUI tokens to 0x4f2e63be8e7fe287836e29cde6f3d5cbc96eefd0c0e3f3747668faa2ae7324b0",
196+
text: "Send 1 SUI tokens to 0x4f2e63be8e7fe287836e29cde6f3d5cbc96eefd0c0e3f3747668faa2ae7324b0",
188197
},
189198
},
190199
{
191200
user: "{{user2}}",
192201
content: {
193-
text: "I'll send 69 SUI tokens now...",
202+
text: "I'll send 1 SUI tokens now...",
194203
action: "SEND_TOKEN",
195204
},
196205
},
197206
{
198207
user: "{{user2}}",
199208
content: {
200-
text: "Successfully sent 69 SUI tokens to 0x4f2e63be8e7fe287836e29cde6f3d5cbc96eefd0c0e3f3747668faa2ae7324b0, Transaction: 0x39a8c432d9bdad993a33cc1faf2e9b58fb7dd940c0425f1d6db3997e4b4b05c0",
209+
text: "Successfully sent 1 SUI tokens to 0x4f2e63be8e7fe287836e29cde6f3d5cbc96eefd0c0e3f3747668faa2ae7324b0, Transaction: 0x39a8c432d9bdad993a33cc1faf2e9b58fb7dd940c0425f1d6db3997e4b4b05c0",
201210
},
202211
},
203212
],

0 commit comments

Comments
 (0)