Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix Parameter Parsing in plugin-evm TransferAction and Return Transaction Hash #965

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions packages/plugin-evm/src/actions/transfer.ts
shakkernerd marked this conversation as resolved.
Show resolved Hide resolved
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is options ignored now? Prefer an LLM options over explicitly passed ones?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that the previous way of directly passing options into action.transfer was in

async transfer(
runtime: IAgentRuntime,
params: TransferParams
): Promise<Transaction>

The parameters cannot be parsed out smoothly, so I refer to the writing method of plugin-solana

https://github.com/ai16z/eliza/blob/main/packages/plugin-solana/src/actions/transfer.ts#L116

Manually built parameters replace those passed to action.transfer.


// 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", ...]
- 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
Loading