Skip to content

Commit

Permalink
Merge pull request #965 from FWangZil/fix/plugin-evm
Browse files Browse the repository at this point in the history
fix: Fix Parameter Parsing in plugin-evm TransferAction and Return Transaction Hash
  • Loading branch information
odilitime authored Dec 16, 2024
2 parents 7d6d121 + 2263d76 commit ca5edca
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
53 changes: 38 additions & 15 deletions packages/plugin-evm/src/actions/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import type { Transaction, TransferParams } from "../types";
import { transferTemplate } from "../templates";

export { transferTemplate };

// Exported for tests
export class TransferAction {
constructor(private walletProvider: WalletProvider) {}

Expand All @@ -22,6 +24,12 @@ export class TransferAction {
`Transferring: ${params.amount} tokens to (${params.toAddress} on ${params.fromChain})`
);

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

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

const walletClient = this.walletProvider.getWalletClient(
params.fromChain
);
Expand Down Expand Up @@ -106,29 +114,44 @@ export const transferAction = {
options: any,
callback?: HandlerCallback
) => {
try {
const walletProvider = initWalletProvider(runtime);
const action = new TransferAction(walletProvider);
const transferDetails = await buildTransferDetails(
state,
runtime,
walletProvider
);
const tx = await action.transfer(transferDetails);
console.log("Transfer action handler called");
const walletProvider = initWalletProvider(runtime);
const action = new TransferAction(walletProvider);

// 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(paramOptions);
if (callback) {
callback({
text: `Successfully transferred ${formatEther(tx.value)} tokens to ${tx.to}\nTransaction hash: ${tx.hash}\nChain: ${transferDetails.fromChain}`,
text: `Successfully transferred ${paramOptions.amount} tokens to ${paramOptions.toAddress}\nTransaction Hash: ${transferResp.hash}`,
content: {
success: true,
hash: tx.hash,
amount: formatEther(tx.value),
recipient: tx.to,
chain: transferDetails.fromChain,
hash: transferResp.hash,
amount: formatEther(transferResp.value),
recipient: transferResp.to,
chain: content.fromChain,
},
});
}

return true;
} catch (error) {
console.error("Error during token transfer:", error);
Expand Down
12 changes: 7 additions & 5 deletions packages/plugin-evm/src/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ export const transferTemplate = `Given the recent messages and wallet informatio
{{walletInfo}}
Extract the following information about the requested transfer:
- Chain to execute on (like in viem/chains)
- Amount to transfer (only number without coin symbol)
- Recipient address
- Chain to execute on: Must be one of ["ethereum", "base", ...] (like in viem/chains)
- Amount to transfer: Must be a string representing the amount in ETH (only number without coin symbol, 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
{
"fromChain": SUPPORTED_CHAINS,
"amount": string,
"toAddress": string
"toAddress": string,
"token": string | null
}
\`\`\`
`;
Expand Down

0 comments on commit ca5edca

Please sign in to comment.