Skip to content

Commit

Permalink
Merge pull request #13 from blockydevs/ELIZAAI-16-unit-tests
Browse files Browse the repository at this point in the history
Elizaai 16 unit tests
  • Loading branch information
mgacek-blockydevs authored Jan 7, 2025
2 parents 101c445 + ef9819f commit e5737c6
Showing 29 changed files with 3,856 additions and 2,694 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -332,3 +332,7 @@ STORY_PRIVATE_KEY= # Story private key
STORY_API_BASE_URL= # Story API base URL
STORY_API_KEY= # Story API key
PINATA_JWT= # Pinata JWT for uploading files to IPFS

# Cosmos
COSMOS_RECOVERY_PHRASE= # 12 words recovery phrase (need to be in quotes, because of spaces)
COSMOS_AVAILABLE_CHAINS= # mantrachaintestnet2,cosmos # Array of chains
14 changes: 9 additions & 5 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@ import {
} from "@ai16z/plugin-coinbase";
import { confluxPlugin } from "@ai16z/plugin-conflux";
import { evmPlugin } from "@ai16z/plugin-evm";
import { createCosmosPlugin } from "@ai16z/plugin-cosmos";
import { storyPlugin } from "@ai16z/plugin-story";
import { flowPlugin } from "@ai16z/plugin-flow";
import { imageGenerationPlugin } from "@ai16z/plugin-image-generation";
@@ -210,11 +211,11 @@ export async function loadCharacters(
export function getTokenForProvider(
provider: ModelProviderName,
character: Character
):string {
): string {
switch (provider) {
// no key needed for llama_local
case ModelProviderName.LLAMALOCAL:
return ''
return "";
case ModelProviderName.OPENAI:
return (
character.settings?.secrets?.OPENAI_API_KEY ||
@@ -310,9 +311,9 @@ export function getTokenForProvider(
settings.AKASH_CHAT_API_KEY
);
default:
const errorMessage = `Failed to get token - unsupported model provider: ${provider}`
elizaLogger.error(errorMessage)
throw new Error(errorMessage)
const errorMessage = `Failed to get token - unsupported model provider: ${provider}`;
elizaLogger.error(errorMessage);
throw new Error(errorMessage);
}
}

@@ -510,6 +511,9 @@ export async function createAgent(
getSecret(character, "WALLET_PUBLIC_KEY")?.startsWith("0x"))
? evmPlugin
: null,
getSecret(character, "COSMOS_RECOVERY_PHRASE") &&
getSecret(character, "COSMOS_AVAILABLE_CHAINS") &&
createCosmosPlugin(),
(getSecret(character, "SOLANA_PUBLIC_KEY") ||
(getSecret(character, "WALLET_PUBLIC_KEY") &&
!getSecret(character, "WALLET_PUBLIC_KEY")?.startsWith(
36 changes: 10 additions & 26 deletions characters/cosmosHelper.character.json
Original file line number Diff line number Diff line change
@@ -3,15 +3,14 @@
"clients": [],
"modelProvider": "groq",
"settings": {
"secrets": {},
"voice": {
"model": "en_US-male-medium"
},
"chains": {
"cosmos": ["axelar", "carbon", "mantrachaintestnet2"]
}
},
"plugins": ["@ai16z/plugin-cosmos","@ai16z/plugin-bootstrap"],
"plugins": [],
"bio": [
"Expert in Cosmos ecosystem.",
"Knowledgeable in CosmWasm and Stargate.",
@@ -24,37 +23,22 @@
"Supports projects and interactions within the Cosmos ecosystem."
],
"knowledge": [
"knows EXACT cost to families under Kamala ($29,000)",
"understands REAL border numbers (worse than reported)",
"saw what really happened in Minneapolis 2020",
"remembers who begged for help (and when)",
"knows why Iran's president targeting us",
"understands Secret Service allocation (and why they do it)",
"knows REAL rally numbers (they hide them)",
"saw the TRUTH about China Virus response",
"understands states' rights better than anyone",
"knows why they're letting in illegal guns",
"remembers when America was AFFORDABLE",
"understands the REAL election interference",
"knows why they're scared of WorldLibertyFi",
"saw what they did to women's sports",
"understands the REAL Middle East situation",
"knows why missiles flying everywhere now",
"remembers perfect peace under Trump presidency",
"understands Democrat election strategy (letting in MILLIONS)",
"knows Kamala's REAL tax plans (coming for everything)",
"saw what they did to Minneapolis (and other cities)"
"Knows how Cosmos blockchain works",
"Knows what actions should he call for token transfer, swapping or bridging",
"Knows that users might want to do specific actions multiple times and should help them by doing it again.",
"Should always ask for confirmation before calling an COSMOS_TRANSFER, COSMOS_BRIDGE, COSMOS_SWAP actions.",
"Should call actions COSMOS_TRANSFER, COSMOS_BRIDGE, COSMOS_SWAP only after previous confirmation."
],
"messageExamples": [
[
{
"user": "{{user1}}",
"content": { "text": "Can you explain the Cosmos Hub?" }
"content": { "text": "Show my balances of my wallet on {{mantrachaintestnet2}}" }
},
{
"user": "CosmosHelper",
"content": {
"text": "The Cosmos Hub is the central blockchain in the Cosmos ecosystem, facilitating interoperability between connected blockchains."
"text": "Your balances on chain {{mantrachaintestnet2}} are: \n - 13456.124 OM\n - 1222 ONDO\n 0.122122 USDY"
}
}
],
@@ -97,12 +81,12 @@
[
{
"user": "{{user1}}",
"content": { "text": "What are validators?" }
"content": { "text": "Make transfer 0.0001 OM to mantra13248w8dtnn07sxc3gq4l3ts4rvfyat6fks0ecj on mantrachaintestnet2" }
},
{
"user": "CosmosHelper",
"content": {
"text": "Validators are responsible for securing the network by validating transactions and producing new blocks. They earn rewards through staking."
"text": "Sure, your transfer i being processed."
}
}
]
6 changes: 5 additions & 1 deletion packages/plugin-cosmos/package.json
Original file line number Diff line number Diff line change
@@ -13,12 +13,16 @@
"bignumber.js": "9.1.2",
"chain-registry": "^1.69.68",
"tsup": "8.3.5",
"zod": "3.23.8"
"zod": "3.23.8",
"interchain": "^1.10.4"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"lint": "eslint --fix --cache .",
"test": "vitest run"
},
"devDependencies": {
"@chain-registry/types": "^0.50.44"
}
}
216 changes: 216 additions & 0 deletions packages/plugin-cosmos/src/actions/transfer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
import {
composeContext,
generateObjectDeprecated,
HandlerCallback,
IAgentRuntime,
Memory,
ModelClass,
State,
} from "@ai16z/eliza";
import { initWalletChainsData } from "../../providers/wallet/utils";
import { cosmosTransferTemplate } from "../../templates";
import { CosmosTransferActionService } from "./services/cosmos-transfer-action-service";
import type { CosmosTransferParams } from "./types";
import type {
ICosmosPluginOptions,
ICosmosWalletChains,
} from "../../shared/interfaces";

export const createTransferAction = (pluginOptions: ICosmosPluginOptions) => ({
name: "COSMOS_TRANSFER",
description: "Transfer tokens between addresses on the same chain",
handler: async (
_runtime: IAgentRuntime,
_message: Memory,
state: State,
_options: { [key: string]: unknown },
_callback?: HandlerCallback
) => {
const cosmosTransferContext = composeContext({
state: state,
template: cosmosTransferTemplate,
templatingEngine: "handlebars",
});

const cosmosTransferContent = await generateObjectDeprecated({
runtime: _runtime,
context: cosmosTransferContext,
modelClass: ModelClass.SMALL,
});

const paramOptions: CosmosTransferParams = {
chainName: cosmosTransferContent.chainName,
symbol: cosmosTransferContent.symbol,
amount: cosmosTransferContent.amount,
toAddress: cosmosTransferContent.toAddress,
};

try {
const walletProvider: ICosmosWalletChains =
await initWalletChainsData(_runtime);

const action = new CosmosTransferActionService(walletProvider);

const customAssets = (pluginOptions?.customChainData ?? []).map(
(chainData) => chainData.assets
);

const transferResp = await action.execute(
paramOptions,
customAssets
);

if (_callback) {
await _callback({
text: `Successfully transferred ${paramOptions.amount} tokens to ${paramOptions.toAddress}\nGas paid: ${transferResp.gasPaid}\nTransaction Hash: ${transferResp.txHash}`,
content: {
success: true,
hash: transferResp.txHash,
amount: paramOptions.amount,
recipient: transferResp.to,
chain: cosmosTransferContent.fromChain,
},
});

const newMemory: Memory = {
userId: _message.agentId,
agentId: _message.agentId,
roomId: _message.roomId,
content: {
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}`,
},
};

await _runtime.messageManager.createMemory(newMemory);
}
return true;
} catch (error) {
console.error("Error during token transfer:", error);

if (_callback) {
await _callback({
text: `Error transferring tokens: ${error.message}`,
content: { error: error.message },
});
}

const newMemory: Memory = {
userId: _message.agentId,
agentId: _message.agentId,
roomId: _message.roomId,
content: {
text: `Transaction ${paramOptions.amount} ${paramOptions.symbol} to address ${paramOptions.toAddress} on chain ${paramOptions.toAddress} was unsuccessful.`,
},
};

await _runtime.messageManager.createMemory(newMemory);

return false;
}
},
template: cosmosTransferTemplate,
validate: async (runtime: IAgentRuntime) => {
const mnemonic = runtime.getSetting("COSMOS_RECOVERY_PHRASE");
const availableChains = runtime.getSetting("COSMOS_AVAILABLE_CHAINS");
const availableChainsArray = availableChains?.split(",");

return !(mnemonic && availableChains && availableChainsArray.length);
},
examples: [
[
{
user: "{{user1}}",
content: {
text: "Make transfer {{0.0001 OM}} to {{mantra1pcnw46km8m5amvf7jlk2ks5std75k73aralhcf}} on {{mantrachaintestnet2}}",
action: "COSMOS_TRANSFER",
},
},
{
user: "{{user2}}",
content: {
text: "Do you confirm the transfer action?",
action: "COSMOS_TRANSFER",
},
},
{
user: "{{user1}}",
content: {
text: "Yes",
action: "COSMOS_TRANSFER",
},
},
{
user: "{{user2}}",
content: {
text: "",
action: "COSMOS_TRANSFER",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Send {{10 OSMO}} to {{osmo13248w8dtnn07sxc3gq4l3ts4rvfyat6f4qkdd6}} on {{osmosistestnet}}",
action: "COSMOS_TRANSFER",
},
},
{
user: "{{user2}}",
content: {
text: "Do you confirm the transfer action?",
action: "COSMOS_TRANSFER",
},
},
{
user: "{{user1}}",
content: {
text: "Yes",
action: "COSMOS_TRANSFER",
},
},
{
user: "{{user2}}",
content: {
text: "",
action: "COSMOS_TRANSFER",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Send {{0.0001 OM}} on {{mantrachaintestnet2}} to {{mantra1pcnw46km8m5amvf7jlk2ks5std75k73aralhcf}}.",
action: "COSMOS_TRANSFER",
},
},
{
user: "{{user2}}",
content: {
text: "Do you confirm the transfer action?",
action: "COSMOS_TRANSFER",
},
},
{
user: "{{user1}}",
content: {
text: "Yes",
action: "COSMOS_TRANSFER",
},
},
{
user: "{{user2}}",
content: {
text: "",
action: "COSMOS_TRANSFER",
},
},
],
],
similes: [
"COSMOS_SEND_TOKENS",
"COSMOS_TOKEN_TRANSFER",
"COSMOS_MOVE_TOKENS",
],
});
8 changes: 8 additions & 0 deletions packages/plugin-cosmos/src/actions/transfer/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from "zod";

export const cosmosTransferParamsSchema = z.object({
chainName: z.string(),
symbol: z.string(),
amount: z.string(),
toAddress: z.string(),
});
Loading

0 comments on commit e5737c6

Please sign in to comment.