Skip to content

Commit

Permalink
fix(plugin-evm): eth transfer action
Browse files Browse the repository at this point in the history
  • Loading branch information
FWangZil committed Dec 10, 2024
1 parent 622000e commit c66dbf5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
58 changes: 55 additions & 3 deletions packages/plugin-evm/src/actions/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import { ByteArray, parseEther, type Hex } from "viem";
import { WalletProvider } from "../providers/wallet";
import type { Transaction, TransferParams } from "../types";
import { transferTemplate } from "../templates";
import type { IAgentRuntime, Memory, State } from "@ai16z/eliza";
import {
composeContext,
generateObjectDEPRECATED,
HandlerCallback,
ModelClass,
type IAgentRuntime,
type Memory,
type State,
} from "@ai16z/eliza";

export { transferTemplate };
export class TransferAction {
Expand All @@ -15,6 +23,10 @@ export class TransferAction {
const walletClient = this.walletProvider.getWalletClient();
const [fromAddress] = await walletClient.getAddresses();

if (!params.data) {
params.data = "0x";
}

await this.walletProvider.switchChain(runtime, params.fromChain);

try {
Expand Down Expand Up @@ -57,11 +69,51 @@ export const transferAction = {
runtime: IAgentRuntime,
message: Memory,
state: State,
options: any
options: any,
callback?: HandlerCallback
) => {
console.log("Transfer action handler called");
const walletProvider = new WalletProvider(runtime);
const action = new TransferAction(walletProvider);
return action.transfer(runtime, options);

// Compose transfer context
const transferContext = composeContext({
state,
template: transferTemplate,
});

// Generate transfer content
const content = await generateObjectDEPRECATED({
runtime,
context: transferContext,
modelClass: ModelClass.LARGE,
});

const paramOptions: TransferParams = {
fromChain: content.fromChain,
toAddress: content.toAddress,
amount: content.amount,
data: content.data,
};

try {
const transferResp = await action.transfer(runtime, paramOptions);
if (callback) {
callback({
text: `Successfully transferred ${paramOptions.amount} tokens to ${paramOptions.toAddress}\nTransaction Hash: ${transferResp.hash}`,
});
}
return true;
} catch (error) {
console.error("Error during token transfer:", error);
if (callback) {
callback({
text: `Error transferring tokens: ${error.message}`,
content: { error: error.message },
});
}
return false;
}
},
template: transferTemplate,
validate: async (runtime: IAgentRuntime) => {
Expand Down
16 changes: 8 additions & 8 deletions packages/plugin-evm/src/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ export const transferTemplate = `Given the recent messages and wallet informatio
{{walletInfo}}
Extract the following information about the requested transfer:
- Chain to execute on (ethereum or base)
- Amount to transfer
- Recipient address
- Token symbol or address (if not native token)
- Chain to execute on: Must be one of ["ethereum", "base", "ethSepolia"]
- Amount to transfer: Must be a string representing the amount in ETH (e.g., "0.1")
- Recipient address: Must be a valid Ethereum address starting with "0x"
- Token symbol or address (if not native token): Optional, leave as null for ETH transfers
Respond with a JSON markdown block containing only the extracted values:
Respond with a JSON markdown block containing only the extracted values. All fields except 'token' are required:
\`\`\`json
{
"chain": "ethereum" | "base" | null,
"amount": string | null,
"toAddress": string | null,
"fromChain": "ethereum" | "base",
"amount": string,
"toAddress": string,
"token": string | null
}
\`\`\`
Expand Down

0 comments on commit c66dbf5

Please sign in to comment.