Skip to content

Commit ee43b45

Browse files
committedDec 11, 2024
fix: transfer action fix for EVM Plugin can't run any action elizaOS#735
1 parent b95bf03 commit ee43b45

File tree

3 files changed

+77
-33
lines changed

3 files changed

+77
-33
lines changed
 

‎packages/plugin-evm/src/actions/transfer.ts

+53-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { ByteArray, parseEther, type Hex } from "viem";
2-
import type { IAgentRuntime, Memory, State } from "@ai16z/eliza";
1+
import { ByteArray, formatEther, parseEther, type Hex } from "viem";
2+
import {
3+
composeContext,
4+
generateObjectDEPRECATED,
5+
HandlerCallback,
6+
ModelClass,
7+
type IAgentRuntime,
8+
type Memory,
9+
type State,
10+
} from "@ai16z/eliza";
311

4-
import { WalletProvider } from "../providers/wallet";
12+
import { initWalletProvider, WalletProvider } from "../providers/wallet";
513
import type { Transaction, TransferParams } from "../types";
614
import { transferTemplate } from "../templates";
715

@@ -54,14 +62,49 @@ export const transferAction = {
5462
runtime: IAgentRuntime,
5563
message: Memory,
5664
state: State,
57-
options: any
65+
options: any,
66+
callback?: HandlerCallback
5867
) => {
59-
const privateKey = runtime.getSetting(
60-
"EVM_PRIVATE_KEY"
61-
) as `0x${string}`;
62-
const walletProvider = new WalletProvider(privateKey);
63-
const action = new TransferAction(walletProvider);
64-
return action.transfer(options);
68+
try {
69+
const walletProvider = initWalletProvider(runtime);
70+
const action = new TransferAction(walletProvider);
71+
72+
const context = composeContext({
73+
state,
74+
template: transferTemplate,
75+
});
76+
77+
const transferDetails = await generateObjectDEPRECATED({
78+
runtime,
79+
context,
80+
modelClass: ModelClass.SMALL,
81+
});
82+
83+
const tx = await action.transfer(transferDetails);
84+
85+
if (callback) {
86+
callback({
87+
text: `Successfully transferred ${formatEther(tx.value)} tokens to ${tx.to}\nTransaction hash: ${tx.hash}`,
88+
content: {
89+
success: true,
90+
hash: tx.hash,
91+
amount: formatEther(tx.value),
92+
recipient: tx.to,
93+
},
94+
});
95+
}
96+
97+
return true;
98+
} catch (error) {
99+
console.error("Error during token transfer:", error);
100+
if (callback) {
101+
callback({
102+
text: `Error transferring tokens: ${error.message}`,
103+
content: { error: error.message },
104+
});
105+
}
106+
return false;
107+
}
65108
},
66109
template: transferTemplate,
67110
validate: async (runtime: IAgentRuntime) => {

‎packages/plugin-evm/src/providers/wallet.ts

+19-16
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,26 @@ export class WalletProvider {
3737
return this.chains[this.currentChain];
3838
}
3939

40-
getPublicClient(chainName: SupportedChain): PublicClient<HttpTransport, Chain, Account | undefined> {
40+
getPublicClient(
41+
chainName: SupportedChain
42+
): PublicClient<HttpTransport, Chain, Account | undefined> {
4143
const transport = this.createHttpTransport(chainName);
4244

4345
const publicClient = createPublicClient({
4446
chain: this.chains[chainName],
4547
transport,
46-
})
48+
});
4749
return publicClient;
4850
}
4951

50-
getWalletClient(chainName: SupportedChain):WalletClient {
52+
getWalletClient(chainName: SupportedChain): WalletClient {
5153
const transport = this.createHttpTransport(chainName);
5254

5355
const walletClient = createWalletClient({
5456
chain: this.chains[chainName],
5557
transport,
5658
account: this.account,
57-
})
59+
});
5860

5961
return walletClient;
6062
}
@@ -191,28 +193,29 @@ const genChainsFromRuntime = (
191193
return chains;
192194
};
193195

196+
export const initWalletProvider = (runtime: IAgentRuntime) => {
197+
const privateKey = runtime.getSetting("EVM_PRIVATE_KEY");
198+
if (!privateKey) {
199+
return null;
200+
}
201+
202+
const chains = genChainsFromRuntime(runtime);
203+
204+
return new WalletProvider(privateKey as `0x${string}`, chains);
205+
};
206+
194207
export const evmWalletProvider: Provider = {
195208
async get(
196209
runtime: IAgentRuntime,
197210
message: Memory,
198211
state?: State
199212
): Promise<string | null> {
200-
const privateKey = runtime.getSetting("EVM_PRIVATE_KEY");
201-
if (!privateKey) {
202-
return null;
203-
}
204-
205-
const chains = genChainsFromRuntime(runtime);
206-
207213
try {
208-
const walletProvider = new WalletProvider(
209-
privateKey as `0x${string}`,
210-
chains
211-
);
214+
const walletProvider = initWalletProvider(runtime);
212215
const address = walletProvider.getAddress();
213216
const balance = await walletProvider.getWalletBalance();
214217
const chain = walletProvider.getCurrentChain();
215-
return `EVM Wallet Address: ${address}\nBalance: ${balance} ETH\nChain ID: ${chain.id}, Name: ${chain.name}, Native Currency: ${chain.nativeCurrency}`;
218+
return `EVM Wallet Address: ${address}\nBalance: ${balance} ${chain.nativeCurrency.symbol}\nChain ID: ${chain.id}, Name: ${chain.name}`;
216219
} catch (error) {
217220
console.error("Error in EVM wallet provider:", error);
218221
return null;

‎packages/plugin-evm/src/templates/index.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@ export const transferTemplate = `Given the recent messages and wallet informatio
55
{{walletInfo}}
66
77
Extract the following information about the requested transfer:
8-
- Chain to execute on
9-
- Amount to transfer
8+
- Chain to execute on (like in viem/chains)
9+
- Amount to transfer (only number without coin symbol)
1010
- Recipient address
11-
- Token symbol or address (if not native token)
1211
1312
Respond with a JSON markdown block containing only the extracted values:
1413
1514
\`\`\`json
1615
{
17-
"chain": "ethereum" | "base" | "sepolia" | "bsc" | "arbitrum" | "avalanche" | "polygon" | "optimism" | "cronos" | "gnosis" | "fantom" | "klaytn" | "celo" | "moonbeam" | "aurora" | "harmonyOne" | "moonriver" | "arbitrumNova" | "mantle" | "linea" | "scroll" | "filecoin" | "taiko" | "zksync" | "canto" | null,
18-
"amount": string | null,
19-
"toAddress": string | null,
20-
"token": string | null
16+
"fromChain": "ethereum" | "base" | ...,
17+
"amount": string,
18+
"toAddress": string
2119
}
2220
\`\`\`
2321
`;

0 commit comments

Comments
 (0)
Please sign in to comment.