Connect AI Agents to EVM protocols
npm install @playai/evm-agent-kitbash
npm install evm-agent-kit
`
Quick Start
`typescript
import { EvmAgentKit, createEvmTools } from "evm-agent-kit";
// Initialize with private key and optional RPC URL
const agent = new EvmAgentKit(
"your-wallet-private-key",
"https://rpc-url.example.com",
{ OPENAI_API_KEY: "your-openai-api-key" } // optional config
);
// Create LangChain tools
const tools = createEvmTools(agent);
`
Usage Examples
$3
`typescript
const balance = await agent.getBalance("0x1234567890123456789012345678901234567890");
console.log("Token Balance:", balance);
`
$3
`typescript
const txHash = await agent.transfer(
"0x1234567890123456789012345678901234567890", // to address
1.5, // amount
"0xabcdef1234567890abcdef1234567890abcdef12" // token address (optional for native token)
);
console.log("Transfer Transaction:", txHash);
`
$3
`typescript
const result = await agent.createFourMemeToken(
FOUR_MEME_ADDRESSES.BSC, // Token Manager Address
0.1, // Fee in BNB
"My EVM Token", // name
"MET", // short name/symbol
"An awesome EVM token for testing", // description
"https://example.com/token-image.png", // image URL
Date.now() + 3600 * 1000, // launch time (1 hour from now)
"Meme", // category
"https://mytoken.com", // website (optional)
"https://twitter.com/mytoken", // Twitter URL (optional)
"https://t.me/mytoken" // Telegram URL (optional)
);
console.log("Token Creation Result:", result);
`
$3
`typescript
const priceData = await agent.getTokenPriceDataUsingCoingecko(
"0x1234567890123456789012345678901234567890", // Token address
"0xabcdef1234567890abcdef1234567890abcdef12" // Another token address
);
console.log("Token prices:", priceData);
`
$3
`typescript
const trendingTokens = await agent.getTrendingTokens();
console.log("Trending tokens:", trendingTokens);
`
$3
`typescript
const tvl = await agent.fetchProtocolTvl("uniswap");
console.log("Uniswap TVL:", tvl);
`
$3
Here's a complete example of creating a new tool in EVM Agent Kit:
#### 1. Create the Tool
src/tools/my_tool/my_function.ts:
`typescript
import { EvmAgentKit } from "../../agent";
import { Address } from "viem";
/**
* Description of what your tool does
* @param agent - EvmAgentKit instance
* @param param1 - Description of first parameter
* @param param2 - Description of second parameter
* @returns Promise resolving to the result
*/
export async function my_function(
agent: EvmAgentKit,
param1: string,
param2?: Address,
): Promise {
// Implementation of your tool
// Use agent.connection, agent.wallet, etc. to interact with blockchain
return result;
}
`
src/tools/my_tool/index.ts:
`typescript
export * from "./my_function";
`
#### 2. Create a LangChain Tool
src/langchain/my_tool/my_tool.ts:
`typescript
import { Tool } from "langchain/tools";
import { EvmAgentKit } from "../../agent";
export class MyTool extends Tool {
name = "my_tool_name";
description = Description of what your tool does.
;
constructor(private agent: EvmAgentKit) {
super();
}
protected async _call(input: string): Promise {
try {
const parsedInput = JSON.parse(input);
const { param1, param2 } = parsedInput;
if (!param1) {
throw new Error("param1 is required.");
}
const result = await this.agent.myFunction(param1, param2);
return JSON.stringify({ status: "success", data: result });
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
});
}
}
}
`
#### 3. Create an Action
src/actions/my_tool/my_action.ts:
`typescript
import { Action } from "../../types/action";
import { EvmAgentKit } from "../../agent";
import { z } from "zod";
export const myAction: Action = {
name: "MY_ACTION",
similes: ["my action", "perform my action", "do my action"],
description: "Description of what your action does.",
examples: [
[
{
input: { param1: "value1", param2: "value2" },
output: {
status: "success",
data: { result: "example result" },
message: "Action completed successfully"
},
explanation: "Explanation of what this example demonstrates.",
},
],
],
schema: z.object({
param1: z.string().describe("Description of param1"),
param2: z.string().optional().describe("Description of param2"),
}),
handler: async (agent: EvmAgentKit, input: Record) => {
const { param1, param2 } = input;
const result = await agent.myFunction(param1, param2);
return {
status: "success",
data: result,
message: "Action completed successfully",
};
},
};
`
#### 4. Add to Agent
src/agent/index.ts:
`typescript
import { my_function } from "../tools";
export class EvmAgentKit {
// Existing properties and methods...
async myFunction(param1: string, param2?: Address): Promise {
return my_function(this, param1, param2);
}
}
``