Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add balance action in evm plugin #1

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions packages/plugin-evm/src/actions/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { composeContext, generateObjectDeprecated, ModelClass, type IAgentRuntime, type Memory, type State } from "@elizaos/core";

import { initWalletProvider } from "../providers/wallet";
import { SupportedChain, AddressParams } from "../types";
import { getAddressTemplate } from "../templates";
import { formatEther } from "viem";

export { getAddressTemplate }

const buildAddressDetails = async (
state: State,
runtime: IAgentRuntime,
): Promise<AddressParams> => {
const context = composeContext({
state,
template: getAddressTemplate,
});

const addressDetails = (await generateObjectDeprecated({
runtime,
context: context,
modelClass: ModelClass.SMALL,
})) as AddressParams;

return addressDetails;
}

export const getBalanceAction = {
name: "getBalance",
description: "Get the balance of a provided wallet address",
handler: async (
runtime: IAgentRuntime,
_message: Memory,
state: State,
_options: any,
callback?: (response: any) => void
) => {
const walletProvider = initWalletProvider(runtime);
const currentChain = walletProvider.getCurrentChain().name.toLowerCase() as SupportedChain;
const { address } = await buildAddressDetails(state, runtime);

try {

Check warning on line 42 in packages/plugin-evm/src/actions/common.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
const client = walletProvider.getPublicClient(currentChain);

Check warning on line 43 in packages/plugin-evm/src/actions/common.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
const balance = await client.getBalance({ address });
const formattedBalance = formatEther(balance);

if (callback) {
callback({
text: `Balance for address ${address}: ${formattedBalance}`,
content: { balance: formattedBalance },
});
}
return true;
} catch (error) {
console.error("Error getting balance:", error);
if (callback) {
callback({
text: `Error getting balance: ${error.message}`,
content: { error: error.message },
});
}
return false;
}
},
template: getAddressTemplate,
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 check the balance for 0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
action: "GET_BALANCE",
},
},
{
user: "user",
content: {
text: "What's the balance of 0x742d35Cc6634C0532925a3b844Bc454e4438f44e?",
action: "GET_BALANCE",
},
},
],
],
similes: ["CHECK_BALANCE", "BALANCE_INQUIRY", "ACCOUNT_BALANCE"],
};

export const getBlockAction = {
name: "getBlockNumber",
description: "Get the current block number",
handler: async (
runtime: IAgentRuntime,
_message: Memory,
state: State,
_options: any,
callback?: (response: any) => void
) => {
const walletProvider = initWalletProvider(runtime);
const currentChain = walletProvider.getCurrentChain().name.toLowerCase() as SupportedChain;

try {
const client = walletProvider.getPublicClient(currentChain);
const blockNumber = Number(await client.getBlockNumber());

Check warning on line 107 in packages/plugin-evm/src/actions/common.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
if (callback) {

Check warning on line 108 in packages/plugin-evm/src/actions/common.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
callback({
text: `Current block number: ${blockNumber}`,
content: { blockNumber },
});
}
return true;
} catch (error) {
console.error("Error getting block number:", error);
if (callback) {
callback({
text: `Error getting block number: ${error.message}`,
content: { error: error.message },
});
}
return false;
}
},
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 fetch the latest block number",
action: "GET_BLOCK",
},
},
{
user: "user",
content: {
text: "What's the current block number?",
action: "GET_BLOCK",
},
},
],
],
similes: ["BLOCK_NUMBER", "FETCH_BLOCK", "BLOCK_INFO"],
};
3 changes: 2 additions & 1 deletion packages/plugin-evm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import { bridgeAction } from "./actions/bridge";
import { swapAction } from "./actions/swap";
import { transferAction } from "./actions/transfer";
import { evmWalletProvider } from "./providers/wallet";
import { getBalanceAction, getBlockAction } from "./actions/common";

export const evmPlugin: Plugin = {
name: "evm",
description: "EVM blockchain integration plugin",
providers: [evmWalletProvider],
evaluators: [],
services: [],
actions: [transferAction, bridgeAction, swapAction],
actions: [transferAction, bridgeAction, swapAction, getBalanceAction, getBlockAction],
};

export default evmPlugin;
16 changes: 16 additions & 0 deletions packages/plugin-evm/src/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,19 @@ Respond with a JSON markdown block containing only the extracted values. Use nul
}
\`\`\`
`;

export const getAddressTemplate = `Given the recent messages below:

{{recentMessages}}

Extract the following information about the requested address:
- Wallet address

Respond with a JSON markdown block containing only the extracted values:

\`\`\`json
{
"address": string
}
\`\`\`
`;
4 changes: 4 additions & 0 deletions packages/plugin-evm/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export interface BridgeParams {
toAddress?: Address;
}

export interface AddressParams {
address: Address;
}

// Plugin configuration
export interface EvmPluginConfig {
rpcUrl?: {
Expand Down
Loading