|
1 | 1 | import {
|
2 |
| - CosmosWalletProvider, |
3 |
| - genCosmosChainsFromRuntime, |
4 |
| - initWalletProvider, |
| 2 | + CosmosWalletProvider, |
| 3 | + genCosmosChainsFromRuntime, |
| 4 | + initWalletProvider, |
5 | 5 | } from "../providers/wallet.ts";
|
6 | 6 | import {
|
7 |
| - composeContext, |
8 |
| - generateObjectDeprecated, |
9 |
| - HandlerCallback, |
10 |
| - IAgentRuntime, |
11 |
| - Memory, |
12 |
| - ModelClass, |
13 |
| - State, |
| 7 | + composeContext, |
| 8 | + generateObjectDeprecated, |
| 9 | + HandlerCallback, |
| 10 | + IAgentRuntime, |
| 11 | + Memory, |
| 12 | + ModelClass, |
| 13 | + State, |
14 | 14 | } from "@ai16z/eliza";
|
15 | 15 | import { balanceTemplate } from "../templates";
|
16 | 16 | import { z } from "zod";
|
17 | 17 |
|
18 | 18 | export class BalanceAction {
|
19 |
| - constructor(private cosmosWalletProvider: CosmosWalletProvider) { |
20 |
| - this.cosmosWalletProvider = cosmosWalletProvider; |
21 |
| - } |
| 19 | + constructor(private cosmosWalletProvider: CosmosWalletProvider) { |
| 20 | + this.cosmosWalletProvider = cosmosWalletProvider; |
| 21 | + } |
22 | 22 |
|
23 |
| - async getBalance() { |
24 |
| - try { |
25 |
| - const activeChain = this.cosmosWalletProvider.getActiveChain(); |
26 |
| - const address = this.cosmosWalletProvider.getAddress(); |
27 |
| - const balance = await this.cosmosWalletProvider.getWalletBalance(); |
| 23 | + async getBalance() { |
| 24 | + try { |
| 25 | + const activeChain = this.cosmosWalletProvider.getActiveChain(); |
| 26 | + const address = this.cosmosWalletProvider.getAddress(); |
| 27 | + const balance = await this.cosmosWalletProvider.getWalletBalance(); |
28 | 28 |
|
29 |
| - return `Address: ${address}\nBalance: ${JSON.stringify(balance, null, 2)}, chain name: ${activeChain}`; |
30 |
| - } catch (error) { |
31 |
| - console.error("Error in Cosmos wallet provider:", error); |
| 29 | + return `Address: ${address}\nBalance: ${JSON.stringify(balance, null, 2)}, chain name: ${activeChain}`; |
| 30 | + } catch (error) { |
| 31 | + console.error("Error in Cosmos wallet provider:", error); |
32 | 32 |
|
33 |
| - return null; |
| 33 | + return null; |
| 34 | + } |
34 | 35 | }
|
35 |
| - } |
36 | 36 | }
|
37 | 37 |
|
38 | 38 | export const balanceAction = {
|
39 |
| - name: "COSMOS_WALLET_BALANCE", |
40 |
| - description: "Action for fetching wallet balance on given chain", |
41 |
| - handler: async ( |
42 |
| - _runtime: IAgentRuntime, |
43 |
| - _message: Memory, |
44 |
| - state: State, |
45 |
| - _options: { [key: string]: unknown }, |
46 |
| - _callback: HandlerCallback |
47 |
| - ) => { |
48 |
| - console.log("COSMOS_WALLET_BALANCE action handler called"); |
| 39 | + name: "COSMOS_WALLET_BALANCE", |
| 40 | + description: "Action for fetching wallet balance on given chain", |
| 41 | + handler: async ( |
| 42 | + _runtime: IAgentRuntime, |
| 43 | + _message: Memory, |
| 44 | + state: State, |
| 45 | + _options: { [key: string]: unknown }, |
| 46 | + _callback: HandlerCallback |
| 47 | + ) => { |
| 48 | + console.log("COSMOS_WALLET_BALANCE action handler called"); |
49 | 49 |
|
50 |
| - // Compose transfer context |
51 |
| - const transferContext = composeContext({ |
52 |
| - state: state, |
53 |
| - template: balanceTemplate, |
54 |
| - templatingEngine: "handlebars", |
55 |
| - }); |
| 50 | + // Compose transfer context |
| 51 | + const transferContext = composeContext({ |
| 52 | + state: state, |
| 53 | + template: balanceTemplate, |
| 54 | + templatingEngine: "handlebars", |
| 55 | + }); |
56 | 56 |
|
57 |
| - // Generate transfer content |
58 |
| - const content = await generateObjectDeprecated({ |
59 |
| - runtime: _runtime, |
60 |
| - context: transferContext, |
61 |
| - modelClass: ModelClass.SMALL, |
62 |
| - }); |
| 57 | + // Generate transfer content |
| 58 | + const content = await generateObjectDeprecated({ |
| 59 | + runtime: _runtime, |
| 60 | + context: transferContext, |
| 61 | + modelClass: ModelClass.SMALL, |
| 62 | + }); |
63 | 63 |
|
64 |
| - const balanceContentValidator = z.object({ |
65 |
| - chainName: z.string(), |
66 |
| - }); |
| 64 | + const balanceContentValidator = z.object({ |
| 65 | + chainName: z.string(), |
| 66 | + }); |
67 | 67 |
|
68 |
| - const transferContent = balanceContentValidator.parse(content); |
| 68 | + const transferContent = balanceContentValidator.parse(content); |
69 | 69 |
|
70 |
| - const { chainName } = transferContent; |
| 70 | + const { chainName } = transferContent; |
71 | 71 |
|
72 |
| - try { |
73 |
| - const walletProvider = await initWalletProvider( |
74 |
| - _runtime, |
75 |
| - chainName |
76 |
| - ); |
77 |
| - const action = new BalanceAction(walletProvider); |
78 |
| - const responseText = await action.getBalance(); |
| 72 | + try { |
| 73 | + const walletProvider = await initWalletProvider( |
| 74 | + _runtime, |
| 75 | + chainName |
| 76 | + ); |
| 77 | + const action = new BalanceAction(walletProvider); |
| 78 | + const responseText = await action.getBalance(); |
79 | 79 |
|
80 |
| - await _callback({ |
81 |
| - text: responseText, |
82 |
| - }); |
83 |
| - } catch (error) { |
84 |
| - await _callback({ |
85 |
| - text: error.message, |
86 |
| - }); |
87 |
| - console.error("Error in Cosmos wallet provider:", error); |
88 |
| - } |
| 80 | + await _callback({ |
| 81 | + text: responseText, |
| 82 | + }); |
| 83 | + } catch (error) { |
| 84 | + await _callback({ |
| 85 | + text: error.message, |
| 86 | + }); |
| 87 | + console.error("Error in Cosmos wallet provider:", error); |
| 88 | + } |
89 | 89 |
|
90 |
| - return; |
91 |
| - }, |
92 |
| - validate: async (runtime: IAgentRuntime) => { |
93 |
| - const recoveryPhrase = runtime.getSetting("COSMOS_RECOVERY_PHRASE"); |
94 |
| - const chains = genCosmosChainsFromRuntime(runtime); |
| 90 | + return; |
| 91 | + }, |
| 92 | + validate: async (runtime: IAgentRuntime) => { |
| 93 | + const recoveryPhrase = runtime.getSetting("COSMOS_RECOVERY_PHRASE"); |
| 94 | + const chains = genCosmosChainsFromRuntime(runtime); |
95 | 95 |
|
96 |
| - return recoveryPhrase !== undefined && Object.keys(chains).length > 0; |
97 |
| - }, |
98 |
| - examples: [ |
99 |
| - [ |
100 |
| - { |
101 |
| - user: "{{user1}}", |
102 |
| - content: { |
103 |
| - text: "Show me balance of my cosmos wallet for chain mantrachaintestnet2", |
104 |
| - }, |
105 |
| - }, |
106 |
| - { |
107 |
| - user: "{{user2}}", |
108 |
| - content: { |
109 |
| - text: "", |
110 |
| - action: "COSMOS_WALLET_BALANCE", |
111 |
| - }, |
112 |
| - }, |
113 |
| - ], |
114 |
| - [ |
115 |
| - { |
116 |
| - user: "{{user1}}", |
117 |
| - content: { |
118 |
| - text: "Show me balance of my cosmos wallet for chain mantrachaintestnet2 use COSMOS_WALLET_BALANCE action", |
119 |
| - }, |
120 |
| - }, |
121 |
| - { |
122 |
| - user: "{{user2}}", |
123 |
| - content: { |
124 |
| - text: "", |
125 |
| - action: "COSMOS_WALLET_BALANCE", |
126 |
| - }, |
127 |
| - }, |
128 |
| - ], |
129 |
| - [ |
130 |
| - { |
131 |
| - user: "{{user1}}", |
132 |
| - content: { |
133 |
| - text: "Show me balance of my wallet for chain mantrachaintestnet2 on cosmos", |
134 |
| - }, |
135 |
| - }, |
136 |
| - { |
137 |
| - user: "{{user2}}", |
138 |
| - content: { |
139 |
| - text: "", |
140 |
| - action: "COSMOS_WALLET_BALANCE", |
141 |
| - }, |
142 |
| - }, |
143 |
| - ], |
144 |
| - [ |
145 |
| - { |
146 |
| - user: "{{user1}}", |
147 |
| - content: { |
148 |
| - text: "What is my balance on the chain mantrachaintestnet2 on cosmos", |
149 |
| - }, |
150 |
| - }, |
151 |
| - { |
152 |
| - user: "{{user2}}", |
153 |
| - content: { |
154 |
| - text: "", |
155 |
| - action: "COSMOS_WALLET_BALANCE", |
156 |
| - }, |
157 |
| - }, |
| 96 | + return recoveryPhrase !== undefined && Object.keys(chains).length > 0; |
| 97 | + }, |
| 98 | + examples: [ |
| 99 | + [ |
| 100 | + { |
| 101 | + user: "{{user1}}", |
| 102 | + content: { |
| 103 | + text: "Show me balance of my cosmos wallet for chain mantrachaintestnet2", |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + user: "{{user2}}", |
| 108 | + content: { |
| 109 | + text: "", |
| 110 | + action: "COSMOS_WALLET_BALANCE", |
| 111 | + }, |
| 112 | + }, |
| 113 | + ], |
| 114 | + [ |
| 115 | + { |
| 116 | + user: "{{user1}}", |
| 117 | + content: { |
| 118 | + text: "Show me balance of my cosmos wallet for chain mantrachaintestnet2 use COSMOS_WALLET_BALANCE action", |
| 119 | + }, |
| 120 | + }, |
| 121 | + { |
| 122 | + user: "{{user2}}", |
| 123 | + content: { |
| 124 | + text: "", |
| 125 | + action: "COSMOS_WALLET_BALANCE", |
| 126 | + }, |
| 127 | + }, |
| 128 | + ], |
| 129 | + [ |
| 130 | + { |
| 131 | + user: "{{user1}}", |
| 132 | + content: { |
| 133 | + text: "Show me balance of my wallet for chain mantrachaintestnet2 on cosmos", |
| 134 | + }, |
| 135 | + }, |
| 136 | + { |
| 137 | + user: "{{user2}}", |
| 138 | + content: { |
| 139 | + text: "", |
| 140 | + action: "COSMOS_WALLET_BALANCE", |
| 141 | + }, |
| 142 | + }, |
| 143 | + ], |
| 144 | + [ |
| 145 | + { |
| 146 | + user: "{{user1}}", |
| 147 | + content: { |
| 148 | + text: "What is my balance on the chain mantrachaintestnet2 on cosmos", |
| 149 | + }, |
| 150 | + }, |
| 151 | + { |
| 152 | + user: "{{user2}}", |
| 153 | + content: { |
| 154 | + text: "", |
| 155 | + action: "COSMOS_WALLET_BALANCE", |
| 156 | + }, |
| 157 | + }, |
| 158 | + ], |
158 | 159 | ],
|
159 |
| - ], |
160 |
| - similes: ["COSMOS_BALANCE", "COSMOS_WALLET_TOKENS"], |
| 160 | + similes: ["COSMOS_BALANCE", "COSMOS_WALLET_TOKENS"], |
161 | 161 | };
|
0 commit comments