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

feat: Add Dexscreener plugin #1584

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions agent/package.json
odilitime marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@elizaos/plugin-coingecko": "workspace:*",
"@elizaos/plugin-coinmarketcap": "workspace:*",
"@elizaos/plugin-binance": "workspace:*",
"@elizaos/plugin-dexscreener": "workspace:*",
"@elizaos/plugin-avail": "workspace:*",
"@elizaos/plugin-bootstrap": "workspace:*",
"@elizaos/plugin-cosmos": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import { coinmarketcapPlugin } from "@elizaos/plugin-coinmarketcap";
import { confluxPlugin } from "@elizaos/plugin-conflux";
import { createCosmosPlugin } from "@elizaos/plugin-cosmos";
import { cronosZkEVMPlugin } from "@elizaos/plugin-cronoszkevm";
import { dexscreenerPlugin } from "@elizaos/plugin-dexscreener";
import { echoChambersPlugin } from "@elizaos/plugin-echochambers";
import { evmPlugin } from "@elizaos/plugin-evm";
import { flowPlugin } from "@elizaos/plugin-flow";
Expand Down Expand Up @@ -825,7 +826,7 @@ export async function createAgent(
getSecret(character, "ABSTRACT_PRIVATE_KEY")
? abstractPlugin
: null,
getSecret(character, "B2_PRIVATE_KEY") ? b2Plugin: null,
getSecret(character, "B2_PRIVATE_KEY") ? b2Plugin : null,
getSecret(character, "BINANCE_API_KEY") &&
getSecret(character, "BINANCE_SECRET_KEY")
? binancePlugin
Expand Down
24 changes: 24 additions & 0 deletions packages/plugin-dexscreener/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# `@elizaos/plugin-dexscreener`

This plugin provides actions for interacting with dexscreener based on
https://docs.dexscreener.com/api/reference

---

## Installation

Just add it under your character profile in plugins as

```
"plugins": [
"@elizaos/plugin-dexscreener"
],
```

## Configuration

There is no need fo configuration for this plugin, API is free and limited per 60 req/min

## Usage

Results are saved to State object and agents can retrive it from there for use
3 changes: 3 additions & 0 deletions packages/plugin-dexscreener/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import eslintGlobalConfig from "../../eslint.config.mjs";

export default [...eslintGlobalConfig];
20 changes: 20 additions & 0 deletions packages/plugin-dexscreener/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@elizaos/plugin-dexscreener",
"version": "0.1.7-alpha.2",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"dependencies": {
"@elizaos/core": "workspace:*",
"tsup": "8.3.5"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"test": "vitest run",
"lint": "eslint --fix --cache ."
},
"peerDependencies": {
"whatwg-url": "7.1.0"
}
}
246 changes: 246 additions & 0 deletions packages/plugin-dexscreener/src/actions/getTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
import {
Action,
elizaLogger,
IAgentRuntime,
Memory,
HandlerCallback,
State,
getEmbeddingZeroVector,
} from "@elizaos/core";

interface TokenProfile {
url: string;
description?: string;
chainId: string;
tokenAddress: string;
}

const createTokenMemory = async (
runtime: IAgentRuntime,
state: State,
formattedOutput: string
): Promise<Memory> => {
const memory: Memory = {
userId: runtime.agentId,
agentId: runtime.agentId,
roomId: state.roomId,
content: { text: formattedOutput },
createdAt: Date.now(),
embedding: getEmbeddingZeroVector(),
};
await runtime.messageManager.createMemory(memory);
};

export const getLatestTokensAction: Action = {
name: "GET_LATEST_TOKENS",
description: "Get the latest tokens from DexScreener API",
validate: async (runtime: IAgentRuntime, _message: Memory) => {
elizaLogger.log("Validating runtime for GET_LATEST_TOKENS...");
return true;
},
handler: async (
runtime: IAgentRuntime,
_message: Memory,
state: State,
_options: any,
callback: HandlerCallback
) => {
elizaLogger.log("Starting GET_LATEST_TOKENS handler...");

const recentMessage = await runtime.messageManager.getMemories({
roomId: state.roomId,
count: 10,
unique: false,
});

try {
const response = await fetch(
"https://api.dexscreener.com/token-profiles/latest/v1",
{
method: "GET",
headers: {
accept: "application/json",
},
}
);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const tokens: TokenProfile[] = await response.json();

const formattedOutput = tokens
.map((token) => {
const description =
token.description || "No description available";
return `Chain: ${token.chainId}\nToken Address: ${token.tokenAddress}\nURL: ${token.url}\nDescription: ${description}\n\n`;
})
.join("");

state = (await runtime.composeState(
await createTokenMemory(runtime, state, formattedOutput)
)) as State;

callback(
{
text: formattedOutput,
},
[]
);
} catch (error) {
elizaLogger.error("Error fetching latest tokens:", error);
callback(
{
text: "Failed to fetch latest tokens. Please check the logs for more details.",
},
[]
);
}
},
examples: [],
similes: [
"GET_LATEST_TOKENS",
"FETCH_NEW_TOKENS",
"CHECK_RECENT_TOKENS",
"LIST_NEW_TOKENS",
],
};

export const getLatestBoostedTokensAction: Action = {
name: "GET_LATEST_BOOSTED_TOKENS",
description: "Get the latest boosted tokens from DexScreener API",
validate: async (runtime: IAgentRuntime, _message: Memory) => {
elizaLogger.log("Validating runtime for GET_LATEST_BOOSTED_TOKENS...");
return true;
},
handler: async (
runtime: IAgentRuntime,
_message: Memory,
state: State,
_options: any,
callback: HandlerCallback
) => {
elizaLogger.log("Starting GET_LATEST_BOOSTED_TOKENS handler...");

try {
const response = await fetch(
"https://api.dexscreener.com/token-boosts/latest/v1",
{
method: "GET",
headers: {
accept: "application/json",
},
}
);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const tokens: TokenProfile[] = await response.json();

const formattedOutput = tokens
.map((token) => {
const description =
token.description || "No description available";
return `Chain: ${token.chainId}\nToken Address: ${token.tokenAddress}\nURL: ${token.url}\nDescription: ${description}\n\n`;
})
.join("");

await createTokenMemory(runtime, state, formattedOutput);

callback(
{
text: formattedOutput,
},
[]
);
} catch (error) {
elizaLogger.error("Error fetching latest boosted tokens:", error);
callback(
{
text: "Failed to fetch latest boosted tokens. Please check the logs for more details.",
},
[]
);
}
},
examples: [],
similes: [
"GET_LATEST_BOOSTED_TOKENS",
"FETCH_NEW_BOOSTED_TOKENS",
"CHECK_RECENT_BOOSTED_TOKENS",
"LIST_NEW_BOOSTED_TOKENS",
],
};

export const getTopBoostedTokensAction: Action = {
name: "GET_TOP_BOOSTED_TOKENS",
description: "Get tokens with most active boosts from DexScreener API",
validate: async (runtime: IAgentRuntime, _message: Memory) => {
elizaLogger.log("Validating runtime for GET_TOP_BOOSTED_TOKENS...");
return true;
},
handler: async (
runtime: IAgentRuntime,
_message: Memory,
state: State,
_options: any,
callback: HandlerCallback
) => {
elizaLogger.log("Starting GET_TOP_BOOSTED_TOKENS handler...");

try {
const response = await fetch(
"https://api.dexscreener.com/token-boosts/top/v1",
{
method: "GET",
headers: {
accept: "application/json",
},
}
);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const tokens: TokenProfile[] = await response.json();

const formattedOutput = tokens
.map((token) => {
const description =
token.description || "No description available";
return `Chain: ${token.chainId}\nToken Address: ${token.tokenAddress}\nURL: ${token.url}\nDescription: ${description}\n\n`;
})
.join("");

state = (await runtime.composeState(
await createTokenMemory(runtime, state, formattedOutput)
)) as State;

callback(
{
text: formattedOutput,
},
[]
);
} catch (error) {
elizaLogger.error("Error fetching top boosted tokens:", error);
callback(
{
text: "Failed to fetch top boosted tokens. Please check the logs for more details.",
},
[]
);
}
},
examples: [],
similes: [
"GET_TOP_BOOSTED_TOKENS",
"FETCH_MOST_BOOSTED_TOKENS",
"CHECK_HIGHEST_BOOSTED_TOKENS",
"LIST_TOP_BOOSTED_TOKENS",
],
};
23 changes: 23 additions & 0 deletions packages/plugin-dexscreener/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export * from "./actions/getTokens";

import type { Plugin } from "@elizaos/core";
import {
getLatestTokensAction,
getLatestBoostedTokensAction,
getTopBoostedTokensAction,
} from "./actions/getTokens";

export const dexscreenerPlugin: Plugin = {
name: "dexscreener",
description: "dexscreener blockchain integration plugin",
providers: [],
evaluators: [],
services: [],
actions: [
getLatestTokensAction,
getLatestBoostedTokensAction,
getTopBoostedTokensAction,
],
};

export default dexscreenerPlugin;
15 changes: 15 additions & 0 deletions packages/plugin-dexscreener/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "../core/tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "./src",
"typeRoots": [
"./node_modules/@types",
"./src/types"
],
"declaration": true
},
"include": [
"src"
]
}
21 changes: 21 additions & 0 deletions packages/plugin-dexscreener/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/index.ts"],
outDir: "dist",
sourcemap: true,
clean: true,
format: ["esm"], // Ensure you're targeting CommonJS
external: [
"dotenv", // Externalize dotenv to prevent bundling
"fs", // Externalize fs to use Node.js built-in module
"path", // Externalize other built-ins if necessary
"@reflink/reflink",
"@node-llama-cpp",
"https",
"http",
"agentkeepalive",
"viem",
"@lifi/sdk",
],
});
Loading
Loading