Skip to content

Commit

Permalink
Merge pull request #7 from blockydevs/ELIZAAI-16-wallet-provider
Browse files Browse the repository at this point in the history
[ELIZAAI-16](feat): cosmos wallet provider with balances
  • Loading branch information
norbert-kulus-blockydevs authored Jan 2, 2025
2 parents 331c643 + 00cfb42 commit ab56e1a
Show file tree
Hide file tree
Showing 5 changed files with 2,619 additions and 2,450 deletions.
6 changes: 5 additions & 1 deletion packages/plugin-cosmos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
35 changes: 16 additions & 19 deletions packages/plugin-cosmos/src/actions/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import { FeeEstimator } from "../services/fee-estimator";
import { getNativeAssetByChainName } from "@chain-registry/utils";
import { assets } from "chain-registry";
import {
CosmosWalletProvider,
fetchChainDetails,
genCosmosChainsFromRuntime,
initWalletProvider,
CosmosWalletChainsData,
cosmosWalletProvider,
} from "../providers/wallet.ts";
import {
composeContext,
Expand All @@ -24,17 +22,17 @@ import { AssetList } from "@chain-registry/types";
import { Coin } from "@cosmjs/stargate";

export class TransferAction {
constructor(private cosmosWalletProvider: CosmosWalletProvider) {
this.cosmosWalletProvider = cosmosWalletProvider;
constructor(private cosmosChainsData: CosmosWalletChainsData) {
this.cosmosChainsData = cosmosChainsData;
}

async transfer(params: CosmosTransferParams): Promise<Transaction> {
const signingCosmWasmClient =
await this.cosmosWalletProvider.getSigningCosmWasmClient();
this.cosmosChainsData.getSigningCosmWasmClient(params.fromChain);

const wallet = await this.cosmosWalletProvider.getWallet();
const accounts = await wallet.getAccounts();
const senderAddress = accounts[0]?.address;
const senderAddress = await this.cosmosChainsData.getWalletAddress(
params.fromChain
);

if (!senderAddress) {
throw new Error("No sender address");
Expand All @@ -44,9 +42,9 @@ export class TransferAction {
throw new Error("No receiver address");
}

const chainAssets: AssetList = fetchChainDetails(
const chainAssets: AssetList = this.cosmosChainsData.getAssetsList(
params.fromChain
).chainAssets;
);

const formatedDenom = params.denomOrIbc.toString();

Expand Down Expand Up @@ -145,10 +143,8 @@ export const transferAction = {
};

try {
const walletProvider = await initWalletProvider(
_runtime,
paramOptions.fromChain
);
const walletProvider: CosmosWalletChainsData =
await cosmosWalletProvider.initWalletChainsData(_runtime);

const action = new TransferAction(walletProvider);

Expand Down Expand Up @@ -202,10 +198,11 @@ export const transferAction = {
},
template: transferTemplate,
validate: async (runtime: IAgentRuntime) => {
const recoveryPhrase = runtime.getSetting("COSMOS_RECOVERY_PHRASE");
const chains = genCosmosChainsFromRuntime(runtime);
const mnemonic = runtime.getSetting("COSMOS_RECOVERY_PHRASE");
const availableChains = runtime.getSetting("COSMOS_AVAILABLE_CHAINS");
const availableChainsArray = availableChains?.split(",");

return recoveryPhrase !== undefined && Object.keys(chains).length > 0;
return !(mnemonic && availableChains && availableChainsArray.length);
},
examples: [
[
Expand Down
7 changes: 3 additions & 4 deletions packages/plugin-cosmos/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { cosmosWalletProvider } from "./providers/wallet.ts";
import {cosmosWalletProvider} from "./providers/wallet.ts";
import type { Plugin } from "@ai16z/eliza";
import { balanceAction } from "./actions/walletProviderTestAction.ts";
import {transferAction} from "./actions/transfer.ts";
import { transferAction } from "./actions/transfer.ts";

export const cosmosPlugin: Plugin = {
name: "cosmos",
description: "Cosmos blockchain integration plugin",
providers: [cosmosWalletProvider],
evaluators: [],
services: [],
actions: [transferAction, balanceAction],
actions: [transferAction],
};

export default cosmosPlugin;
Loading

0 comments on commit ab56e1a

Please sign in to comment.