Skip to content

Commit e5737c6

Browse files
Merge pull request #13 from blockydevs/ELIZAAI-16-unit-tests
Elizaai 16 unit tests
2 parents 101c445 + ef9819f commit e5737c6

29 files changed

+3856
-2694
lines changed

.env.example

+4
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,7 @@ STORY_PRIVATE_KEY= # Story private key
332332
STORY_API_BASE_URL= # Story API base URL
333333
STORY_API_KEY= # Story API key
334334
PINATA_JWT= # Pinata JWT for uploading files to IPFS
335+
336+
# Cosmos
337+
COSMOS_RECOVERY_PHRASE= # 12 words recovery phrase (need to be in quotes, because of spaces)
338+
COSMOS_AVAILABLE_CHAINS= # mantrachaintestnet2,cosmos # Array of chains

agent/src/index.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
} from "@ai16z/plugin-coinbase";
4242
import { confluxPlugin } from "@ai16z/plugin-conflux";
4343
import { evmPlugin } from "@ai16z/plugin-evm";
44+
import { createCosmosPlugin } from "@ai16z/plugin-cosmos";
4445
import { storyPlugin } from "@ai16z/plugin-story";
4546
import { flowPlugin } from "@ai16z/plugin-flow";
4647
import { imageGenerationPlugin } from "@ai16z/plugin-image-generation";
@@ -210,11 +211,11 @@ export async function loadCharacters(
210211
export function getTokenForProvider(
211212
provider: ModelProviderName,
212213
character: Character
213-
):string {
214+
): string {
214215
switch (provider) {
215216
// no key needed for llama_local
216217
case ModelProviderName.LLAMALOCAL:
217-
return ''
218+
return "";
218219
case ModelProviderName.OPENAI:
219220
return (
220221
character.settings?.secrets?.OPENAI_API_KEY ||
@@ -310,9 +311,9 @@ export function getTokenForProvider(
310311
settings.AKASH_CHAT_API_KEY
311312
);
312313
default:
313-
const errorMessage = `Failed to get token - unsupported model provider: ${provider}`
314-
elizaLogger.error(errorMessage)
315-
throw new Error(errorMessage)
314+
const errorMessage = `Failed to get token - unsupported model provider: ${provider}`;
315+
elizaLogger.error(errorMessage);
316+
throw new Error(errorMessage);
316317
}
317318
}
318319

@@ -510,6 +511,9 @@ export async function createAgent(
510511
getSecret(character, "WALLET_PUBLIC_KEY")?.startsWith("0x"))
511512
? evmPlugin
512513
: null,
514+
getSecret(character, "COSMOS_RECOVERY_PHRASE") &&
515+
getSecret(character, "COSMOS_AVAILABLE_CHAINS") &&
516+
createCosmosPlugin(),
513517
(getSecret(character, "SOLANA_PUBLIC_KEY") ||
514518
(getSecret(character, "WALLET_PUBLIC_KEY") &&
515519
!getSecret(character, "WALLET_PUBLIC_KEY")?.startsWith(

characters/cosmosHelper.character.json

+10-26
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
"clients": [],
44
"modelProvider": "groq",
55
"settings": {
6-
"secrets": {},
76
"voice": {
87
"model": "en_US-male-medium"
98
},
109
"chains": {
1110
"cosmos": ["axelar", "carbon", "mantrachaintestnet2"]
1211
}
1312
},
14-
"plugins": ["@ai16z/plugin-cosmos","@ai16z/plugin-bootstrap"],
13+
"plugins": [],
1514
"bio": [
1615
"Expert in Cosmos ecosystem.",
1716
"Knowledgeable in CosmWasm and Stargate.",
@@ -24,37 +23,22 @@
2423
"Supports projects and interactions within the Cosmos ecosystem."
2524
],
2625
"knowledge": [
27-
"knows EXACT cost to families under Kamala ($29,000)",
28-
"understands REAL border numbers (worse than reported)",
29-
"saw what really happened in Minneapolis 2020",
30-
"remembers who begged for help (and when)",
31-
"knows why Iran's president targeting us",
32-
"understands Secret Service allocation (and why they do it)",
33-
"knows REAL rally numbers (they hide them)",
34-
"saw the TRUTH about China Virus response",
35-
"understands states' rights better than anyone",
36-
"knows why they're letting in illegal guns",
37-
"remembers when America was AFFORDABLE",
38-
"understands the REAL election interference",
39-
"knows why they're scared of WorldLibertyFi",
40-
"saw what they did to women's sports",
41-
"understands the REAL Middle East situation",
42-
"knows why missiles flying everywhere now",
43-
"remembers perfect peace under Trump presidency",
44-
"understands Democrat election strategy (letting in MILLIONS)",
45-
"knows Kamala's REAL tax plans (coming for everything)",
46-
"saw what they did to Minneapolis (and other cities)"
26+
"Knows how Cosmos blockchain works",
27+
"Knows what actions should he call for token transfer, swapping or bridging",
28+
"Knows that users might want to do specific actions multiple times and should help them by doing it again.",
29+
"Should always ask for confirmation before calling an COSMOS_TRANSFER, COSMOS_BRIDGE, COSMOS_SWAP actions.",
30+
"Should call actions COSMOS_TRANSFER, COSMOS_BRIDGE, COSMOS_SWAP only after previous confirmation."
4731
],
4832
"messageExamples": [
4933
[
5034
{
5135
"user": "{{user1}}",
52-
"content": { "text": "Can you explain the Cosmos Hub?" }
36+
"content": { "text": "Show my balances of my wallet on {{mantrachaintestnet2}}" }
5337
},
5438
{
5539
"user": "CosmosHelper",
5640
"content": {
57-
"text": "The Cosmos Hub is the central blockchain in the Cosmos ecosystem, facilitating interoperability between connected blockchains."
41+
"text": "Your balances on chain {{mantrachaintestnet2}} are: \n - 13456.124 OM\n - 1222 ONDO\n 0.122122 USDY"
5842
}
5943
}
6044
],
@@ -97,12 +81,12 @@
9781
[
9882
{
9983
"user": "{{user1}}",
100-
"content": { "text": "What are validators?" }
84+
"content": { "text": "Make transfer 0.0001 OM to mantra13248w8dtnn07sxc3gq4l3ts4rvfyat6fks0ecj on mantrachaintestnet2" }
10185
},
10286
{
10387
"user": "CosmosHelper",
10488
"content": {
105-
"text": "Validators are responsible for securing the network by validating transactions and producing new blocks. They earn rewards through staking."
89+
"text": "Sure, your transfer i being processed."
10690
}
10791
}
10892
]

packages/plugin-cosmos/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
"bignumber.js": "9.1.2",
1414
"chain-registry": "^1.69.68",
1515
"tsup": "8.3.5",
16-
"zod": "3.23.8"
16+
"zod": "3.23.8",
17+
"interchain": "^1.10.4"
1718
},
1819
"scripts": {
1920
"build": "tsup --format esm --dts",
2021
"dev": "tsup --format esm --dts --watch",
2122
"lint": "eslint --fix --cache .",
2223
"test": "vitest run"
24+
},
25+
"devDependencies": {
26+
"@chain-registry/types": "^0.50.44"
2327
}
2428
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
import {
2+
composeContext,
3+
generateObjectDeprecated,
4+
HandlerCallback,
5+
IAgentRuntime,
6+
Memory,
7+
ModelClass,
8+
State,
9+
} from "@ai16z/eliza";
10+
import { initWalletChainsData } from "../../providers/wallet/utils";
11+
import { cosmosTransferTemplate } from "../../templates";
12+
import { CosmosTransferActionService } from "./services/cosmos-transfer-action-service";
13+
import type { CosmosTransferParams } from "./types";
14+
import type {
15+
ICosmosPluginOptions,
16+
ICosmosWalletChains,
17+
} from "../../shared/interfaces";
18+
19+
export const createTransferAction = (pluginOptions: ICosmosPluginOptions) => ({
20+
name: "COSMOS_TRANSFER",
21+
description: "Transfer tokens between addresses on the same chain",
22+
handler: async (
23+
_runtime: IAgentRuntime,
24+
_message: Memory,
25+
state: State,
26+
_options: { [key: string]: unknown },
27+
_callback?: HandlerCallback
28+
) => {
29+
const cosmosTransferContext = composeContext({
30+
state: state,
31+
template: cosmosTransferTemplate,
32+
templatingEngine: "handlebars",
33+
});
34+
35+
const cosmosTransferContent = await generateObjectDeprecated({
36+
runtime: _runtime,
37+
context: cosmosTransferContext,
38+
modelClass: ModelClass.SMALL,
39+
});
40+
41+
const paramOptions: CosmosTransferParams = {
42+
chainName: cosmosTransferContent.chainName,
43+
symbol: cosmosTransferContent.symbol,
44+
amount: cosmosTransferContent.amount,
45+
toAddress: cosmosTransferContent.toAddress,
46+
};
47+
48+
try {
49+
const walletProvider: ICosmosWalletChains =
50+
await initWalletChainsData(_runtime);
51+
52+
const action = new CosmosTransferActionService(walletProvider);
53+
54+
const customAssets = (pluginOptions?.customChainData ?? []).map(
55+
(chainData) => chainData.assets
56+
);
57+
58+
const transferResp = await action.execute(
59+
paramOptions,
60+
customAssets
61+
);
62+
63+
if (_callback) {
64+
await _callback({
65+
text: `Successfully transferred ${paramOptions.amount} tokens to ${paramOptions.toAddress}\nGas paid: ${transferResp.gasPaid}\nTransaction Hash: ${transferResp.txHash}`,
66+
content: {
67+
success: true,
68+
hash: transferResp.txHash,
69+
amount: paramOptions.amount,
70+
recipient: transferResp.to,
71+
chain: cosmosTransferContent.fromChain,
72+
},
73+
});
74+
75+
const newMemory: Memory = {
76+
userId: _message.agentId,
77+
agentId: _message.agentId,
78+
roomId: _message.roomId,
79+
content: {
80+
text: `Transaction ${paramOptions.amount} ${paramOptions.symbol} to address ${paramOptions.toAddress} on chain ${paramOptions.toAddress} was successfully transfered.\n Gas paid: ${transferResp.gasPaid}. Tx hash: ${transferResp.txHash}`,
81+
},
82+
};
83+
84+
await _runtime.messageManager.createMemory(newMemory);
85+
}
86+
return true;
87+
} catch (error) {
88+
console.error("Error during token transfer:", error);
89+
90+
if (_callback) {
91+
await _callback({
92+
text: `Error transferring tokens: ${error.message}`,
93+
content: { error: error.message },
94+
});
95+
}
96+
97+
const newMemory: Memory = {
98+
userId: _message.agentId,
99+
agentId: _message.agentId,
100+
roomId: _message.roomId,
101+
content: {
102+
text: `Transaction ${paramOptions.amount} ${paramOptions.symbol} to address ${paramOptions.toAddress} on chain ${paramOptions.toAddress} was unsuccessful.`,
103+
},
104+
};
105+
106+
await _runtime.messageManager.createMemory(newMemory);
107+
108+
return false;
109+
}
110+
},
111+
template: cosmosTransferTemplate,
112+
validate: async (runtime: IAgentRuntime) => {
113+
const mnemonic = runtime.getSetting("COSMOS_RECOVERY_PHRASE");
114+
const availableChains = runtime.getSetting("COSMOS_AVAILABLE_CHAINS");
115+
const availableChainsArray = availableChains?.split(",");
116+
117+
return !(mnemonic && availableChains && availableChainsArray.length);
118+
},
119+
examples: [
120+
[
121+
{
122+
user: "{{user1}}",
123+
content: {
124+
text: "Make transfer {{0.0001 OM}} to {{mantra1pcnw46km8m5amvf7jlk2ks5std75k73aralhcf}} on {{mantrachaintestnet2}}",
125+
action: "COSMOS_TRANSFER",
126+
},
127+
},
128+
{
129+
user: "{{user2}}",
130+
content: {
131+
text: "Do you confirm the transfer action?",
132+
action: "COSMOS_TRANSFER",
133+
},
134+
},
135+
{
136+
user: "{{user1}}",
137+
content: {
138+
text: "Yes",
139+
action: "COSMOS_TRANSFER",
140+
},
141+
},
142+
{
143+
user: "{{user2}}",
144+
content: {
145+
text: "",
146+
action: "COSMOS_TRANSFER",
147+
},
148+
},
149+
],
150+
[
151+
{
152+
user: "{{user1}}",
153+
content: {
154+
text: "Send {{10 OSMO}} to {{osmo13248w8dtnn07sxc3gq4l3ts4rvfyat6f4qkdd6}} on {{osmosistestnet}}",
155+
action: "COSMOS_TRANSFER",
156+
},
157+
},
158+
{
159+
user: "{{user2}}",
160+
content: {
161+
text: "Do you confirm the transfer action?",
162+
action: "COSMOS_TRANSFER",
163+
},
164+
},
165+
{
166+
user: "{{user1}}",
167+
content: {
168+
text: "Yes",
169+
action: "COSMOS_TRANSFER",
170+
},
171+
},
172+
{
173+
user: "{{user2}}",
174+
content: {
175+
text: "",
176+
action: "COSMOS_TRANSFER",
177+
},
178+
},
179+
],
180+
[
181+
{
182+
user: "{{user1}}",
183+
content: {
184+
text: "Send {{0.0001 OM}} on {{mantrachaintestnet2}} to {{mantra1pcnw46km8m5amvf7jlk2ks5std75k73aralhcf}}.",
185+
action: "COSMOS_TRANSFER",
186+
},
187+
},
188+
{
189+
user: "{{user2}}",
190+
content: {
191+
text: "Do you confirm the transfer action?",
192+
action: "COSMOS_TRANSFER",
193+
},
194+
},
195+
{
196+
user: "{{user1}}",
197+
content: {
198+
text: "Yes",
199+
action: "COSMOS_TRANSFER",
200+
},
201+
},
202+
{
203+
user: "{{user2}}",
204+
content: {
205+
text: "",
206+
action: "COSMOS_TRANSFER",
207+
},
208+
},
209+
],
210+
],
211+
similes: [
212+
"COSMOS_SEND_TOKENS",
213+
"COSMOS_TOKEN_TRANSFER",
214+
"COSMOS_MOVE_TOKENS",
215+
],
216+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { z } from "zod";
2+
3+
export const cosmosTransferParamsSchema = z.object({
4+
chainName: z.string(),
5+
symbol: z.string(),
6+
amount: z.string(),
7+
toAddress: z.string(),
8+
});

0 commit comments

Comments
 (0)