forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer.ts
91 lines (84 loc) · 3.04 KB
/
transfer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import type { IAgentRuntime, Memory, State } from "@ai16z/eliza";
import { ByteArray, parseEther, type Hex } from "viem";
import { WalletProvider } from "../providers/wallet";
import { transferTemplate } from "../templates";
import type { Transaction, TransferParams } from "../types";
export { transferTemplate };
export class TransferAction {
constructor(private walletProvider: WalletProvider) {}
async transfer(
runtime: IAgentRuntime,
params: TransferParams
): Promise<Transaction> {
const walletClient = this.walletProvider.getWalletClient();
const [fromAddress] = await walletClient.getAddresses();
await this.walletProvider.switchChain(runtime, params.fromChain);
try {
const hash = await walletClient.sendTransaction({
account: fromAddress,
to: params.toAddress,
value: parseEther(params.amount),
data: params.data as Hex,
kzg: {
blobToKzgCommitment: function (blob: ByteArray): ByteArray {
throw new Error("Function not implemented.");
},
computeBlobKzgProof: function (
blob: ByteArray,
commitment: ByteArray
): ByteArray {
throw new Error("Function not implemented.");
},
},
chain: undefined,
});
return {
hash,
from: fromAddress,
to: params.toAddress,
value: parseEther(params.amount),
data: params.data as Hex,
};
} catch (error) {
throw new Error(`Transfer failed: ${error.message}`);
}
}
}
export const transferAction = {
name: "transfer",
description: "Transfer tokens between addresses on the same chain",
handler: async (
runtime: IAgentRuntime,
message: Memory,
state: State,
options: any
) => {
const walletProvider = new WalletProvider(runtime);
const action = new TransferAction(walletProvider);
return action.transfer(runtime, options);
},
template: transferTemplate,
validate: async (runtime: IAgentRuntime) => {
const privateKey = runtime.getSetting("EVM_PRIVATE_KEY");
return typeof privateKey === "string" && privateKey.startsWith("0x");
},
examples: [
[
{
user: "assistant",
content: {
text: "I'll help you transfer 1 ETH to 0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
action: "SEND_TOKENS",
},
},
{
user: "user",
content: {
text: "Transfer 1 ETH to 0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
action: "SEND_TOKENS",
},
},
],
],
similes: ["SEND_TOKENS", "TOKEN_TRANSFER", "MOVE_TOKENS"],
};