Skip to content

Commit 994b487

Browse files
committed
<ADD> chainbase plugin for eliza
1 parent bf5b72d commit 994b487

17 files changed

+846
-3
lines changed

.env.example

+4-1
Original file line numberDiff line numberDiff line change
@@ -535,4 +535,7 @@ AKASH_MANIFEST_MODE=auto
535535
# Default: Will use the SDL directory
536536
AKASH_MANIFEST_PATH=
537537
# Values: "strict" | "lenient" | "none" - Default: "strict"
538-
AKASH_MANIFEST_VALIDATION_LEVEL=strict
538+
AKASH_MANIFEST_VALIDATION_LEVEL=strict
539+
540+
# Chainbase
541+
CHAINBASE_API_KEY=demo # demo is a free tier key

agent/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"@elizaos/plugin-aptos": "workspace:*",
3737
"@elizaos/plugin-coingecko": "workspace:*",
3838
"@elizaos/plugin-coinmarketcap": "workspace:*",
39-
"@elizaos/plugin-coingecko": "workspace:*",
4039
"@elizaos/plugin-binance": "workspace:*",
4140
"@elizaos/plugin-avail": "workspace:*",
4241
"@elizaos/plugin-bootstrap": "workspace:*",
@@ -88,6 +87,7 @@
8887
"@elizaos/plugin-allora": "workspace:*",
8988
"@elizaos/plugin-opacity": "workspace:*",
9089
"@elizaos/plugin-akash": "workspace:*",
90+
"@elizaos/plugin-chainbase": "workspace:*",
9191
"readline": "1.3.0",
9292
"ws": "8.18.0",
9393
"yargs": "17.7.2"
@@ -99,4 +99,4 @@
9999
"ts-node": "10.9.2",
100100
"tsup": "8.3.5"
101101
}
102-
}
102+
}

agent/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import { OpacityAdapter } from "@elizaos/plugin-opacity";
9191
import { openWeatherPlugin } from "@elizaos/plugin-open-weather";
9292
import { stargazePlugin } from "@elizaos/plugin-stargaze";
9393
import { akashPlugin } from "@elizaos/plugin-akash";
94+
import { chainbasePlugin } from "@elizaos/plugin-chainbase";
9495
import Database from "better-sqlite3";
9596
import fs from "fs";
9697
import net from "net";
@@ -762,6 +763,7 @@ export async function createAgent(
762763
getSecret(character, "AKASH_WALLET_ADDRESS")
763764
? akashPlugin
764765
: null,
766+
getSecret(character, "CHAINBASE_API_KEY") ? chainbasePlugin : null,
765767
].filter(Boolean),
766768
providers: [],
767769
actions: [],

packages/plugin-chainbase/README.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Chainbase Plugin for Eliza
2+
3+
The Chainbase Plugin for Eliza bridges the gap between on-chain data and AI agents, enabling natural language interactions with blockchain data across multiple networks. This plugin leverages Chainbase's comprehensive blockchain data infrastructure to provide real-time insights and analytics.
4+
5+
## Description
6+
7+
This plugin serves as a powerful interface between Eliza AI agents and blockchain data, allowing users to query and analyze on-chain information using natural language. It transforms complex blockchain queries into actionable insights without requiring deep technical knowledge.
8+
9+
## Key Features
10+
11+
- **Multi-chain Data Access**: Access comprehensive data across multiple blockchain networks
12+
- **Natural Language Processing**: Convert natural language queries into blockchain data analytics and insights
13+
- **Real-time Data**: Get up-to-date blockchain information and analytics
14+
15+
## Supported Networks
16+
17+
- Ethereum
18+
- Polygon
19+
- BNB Smart Chain (BSC)
20+
- Avalanche
21+
- Arbitrum One
22+
- Optimism
23+
- Base
24+
- zkSync
25+
- Merlin
26+
27+
## Usage Examples
28+
29+
### On-chain Data Queries
30+
31+
```plaintext
32+
Query: "This address 0x8308964da9ed5d2e8012023d7c7ef02f9e6438c7 which tokens on Ethereum are held"
33+
```
34+
35+
This query will return the token holdings for the specified Ethereum address.
36+
37+
```plaintext
38+
Query: "List the top 10 Ethereum blocks by total gas used in the last 24 hours"
39+
```
40+
41+
This query will analyze and return gas usage statistics for recent Ethereum blocks.
42+
43+
```plaintext
44+
Query: "The address 0x8308964da9ed5d2e8012023d7c7ef02f9e6438c7 last 10 Ethereum token transfer"
45+
```
46+
47+
This query will fetch the most recent 10 token transfer events for the specified Ethereum address, including both incoming and outgoing transfers.
48+
49+
## Components
50+
51+
- **Actions**: Pre-configured blockchain data retrieval and analysis actions
52+
- **Providers**: Data providers for different blockchain networks
53+
- **Evaluators**: Analysis tools for blockchain data interpretation
54+
- **Services**: Specialized services for data processing and transformation
55+
56+
## Getting Started
57+
58+
To use this plugin, you'll need a Chainbase API key:
59+
60+
1. Visit [Chainbase Platform](https://console.chainbase.com) to create an account
61+
2. Once logged in, you can obtain a free API key from your dashboard
62+
3. Set your API key as the `CHAINBASE_API_KEY` environment variable
63+
64+
For development and testing purposes, you can use the API key "demo" to test the basic functionality.
65+
66+
For more detailed information about the available APIs and endpoints, please refer to the [Chainbase API Documentation](https://docs.chainbase.com/api-reference/overview).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import eslintGlobalConfig from "../../eslint.config.mjs";
2+
3+
export default [...eslintGlobalConfig];

packages/plugin-chainbase/link-to-docs

Whitespace-only changes.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@elizaos/plugin-chainbase",
3+
"version": "0.1.7-alpha.1",
4+
"main": "dist/index.js",
5+
"type": "module",
6+
"types": "dist/index.d.ts",
7+
"dependencies": {
8+
"@elizaos/core": "workspace:*"
9+
},
10+
"scripts": {
11+
"build": "tsup --format esm --dts",
12+
"dev": "tsup --format esm --dts --watch"
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
import {
2+
Action,
3+
IAgentRuntime,
4+
Memory,
5+
State,
6+
HandlerCallback,
7+
elizaLogger,
8+
generateText,
9+
ModelClass,
10+
} from "@elizaos/core";
11+
import { generateSQL, executeQuery } from "../libs/chainbase";
12+
import { responsePrompt } from "../templates";
13+
14+
const QUERY_PREFIX = "query onchain data:";
15+
16+
export const queryBlockChainData: Action = {
17+
name: "QUERY_BLOCKCHAIN_DATA",
18+
similes: ["ANALYZE_BLOCKCHAIN", "GET_CHAIN_DATA", "QUERY_ONCHAIN_DATA"],
19+
description:
20+
"Query blockchain data using natural language starting with 'query onchain data:'",
21+
22+
validate: async (runtime: IAgentRuntime, message: Memory) => {
23+
elizaLogger.log("Validating runtime for QUERY_BLOCKCHAIN_DATA...");
24+
return !!(
25+
runtime.character.settings.secrets?.CHAINBASE_API_KEY ||
26+
process.env.CHAINBASE_API_KEY
27+
);
28+
},
29+
30+
handler: async (
31+
runtime: IAgentRuntime,
32+
message: Memory,
33+
state?: State,
34+
options?: { [key: string]: unknown },
35+
callback?: HandlerCallback
36+
) => {
37+
try {
38+
const messageText = message.content.text.toLowerCase();
39+
40+
if (!messageText.includes(QUERY_PREFIX)) {
41+
callback({
42+
text: `Please use the format: ${QUERY_PREFIX} <your natural language query>`,
43+
});
44+
return;
45+
}
46+
47+
const queryText = message.content.text
48+
.slice(
49+
message.content.text.toLowerCase().indexOf(QUERY_PREFIX) +
50+
QUERY_PREFIX.length
51+
)
52+
.trim();
53+
54+
if (!queryText) {
55+
callback({
56+
text: `Please provide a specific query after '${QUERY_PREFIX}'`,
57+
});
58+
return;
59+
}
60+
61+
// Generate SQL from natural language
62+
const sql = await generateSQL(queryText);
63+
64+
// Execute query on Chainbase
65+
const result = await executeQuery(sql);
66+
67+
// Use generateText to format the response
68+
const formattedResponse = await generateText({
69+
runtime,
70+
context: responsePrompt(
71+
{
72+
sql,
73+
columns: result.columns,
74+
data: result.data,
75+
totalRows: result.totalRows,
76+
},
77+
queryText
78+
),
79+
modelClass: ModelClass.SMALL,
80+
});
81+
82+
callback({
83+
text: formattedResponse,
84+
});
85+
} catch (error) {
86+
elizaLogger.error("Error in queryChainbase action:", error);
87+
return "error";
88+
}
89+
},
90+
91+
examples: [
92+
[
93+
{
94+
user: "user",
95+
content: {
96+
text: "query onchain data: Calculate the average gas used per block on Ethereum in the last 100 blocks",
97+
action: "QUERY_BLOCKCHAIN_DATA",
98+
},
99+
},
100+
{
101+
user: "assistant",
102+
content: {
103+
text: "📊 Query Results...",
104+
},
105+
},
106+
],
107+
[
108+
{
109+
user: "user",
110+
content: {
111+
text: "query onchain data: Show me the top 10 active Ethereum addresses by transaction count in the last 1000 blocks",
112+
action: "QUERY_BLOCKCHAIN_DATA",
113+
},
114+
},
115+
{
116+
user: "assistant",
117+
content: {
118+
text: "📊 Query Results...",
119+
},
120+
},
121+
],
122+
[
123+
{
124+
user: "user",
125+
content: {
126+
text: "query onchain data: List Ethereum transactions with value greater than 1 ETH in the last 1000 blocks",
127+
action: "QUERY_BLOCKCHAIN_DATA",
128+
},
129+
},
130+
{
131+
user: "assistant",
132+
content: {
133+
text: "📊 Query Results...",
134+
},
135+
},
136+
],
137+
[
138+
{
139+
user: "user",
140+
content: {
141+
text: "query onchain data: Calculate the total ETH transaction fees collected in the last 100 Ethereum blocks",
142+
action: "QUERY_BLOCKCHAIN_DATA",
143+
},
144+
},
145+
{
146+
user: "assistant",
147+
content: {
148+
text: "📊 Query Results...",
149+
},
150+
},
151+
],
152+
[
153+
{
154+
user: "user",
155+
content: {
156+
text: "query onchain data: Show me the distribution of ETH transaction values in the last 1000 Ethereum transactions",
157+
action: "QUERY_BLOCKCHAIN_DATA",
158+
},
159+
},
160+
{
161+
user: "assistant",
162+
content: {
163+
text: "📊 Query Results...",
164+
},
165+
},
166+
],
167+
[
168+
{
169+
user: "user",
170+
content: {
171+
text: "query onchain data: Find Ethereum blocks that have more than 200 transactions in the last 24 hours",
172+
action: "QUERY_BLOCKCHAIN_DATA",
173+
},
174+
},
175+
{
176+
user: "assistant",
177+
content: {
178+
text: "📊 Query Results...",
179+
},
180+
},
181+
],
182+
[
183+
{
184+
user: "user",
185+
content: {
186+
text: "query onchain data: What's the average gas price trend on Ethereum mainnet in the last 1000 blocks",
187+
action: "QUERY_BLOCKCHAIN_DATA",
188+
},
189+
},
190+
{
191+
user: "assistant",
192+
content: {
193+
text: "📊 Query Results...",
194+
},
195+
},
196+
],
197+
[
198+
{
199+
user: "user",
200+
content: {
201+
text: "query onchain data: Show me Ethereum addresses that have both sent and received ETH in the last 100 blocks",
202+
action: "QUERY_BLOCKCHAIN_DATA",
203+
},
204+
},
205+
{
206+
user: "assistant",
207+
content: {
208+
text: "📊 Query Results...",
209+
},
210+
},
211+
],
212+
[
213+
{
214+
user: "user",
215+
content: {
216+
text: "query onchain data: List the top 10 Ethereum blocks by total gas used in the last 24 hours",
217+
action: "QUERY_BLOCKCHAIN_DATA",
218+
},
219+
},
220+
{
221+
user: "assistant",
222+
content: {
223+
text: "📊 Query Results...",
224+
},
225+
},
226+
],
227+
],
228+
};

0 commit comments

Comments
 (0)