Skip to content

Commit

Permalink
feat: add NFT knowledge
Browse files Browse the repository at this point in the history
  • Loading branch information
IkigaiLabsETH committed Dec 19, 2024
1 parent 398ce22 commit ab0df82
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 22 deletions.
43 changes: 22 additions & 21 deletions packages/plugin-evm/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
{
"name": "@ai16z/plugin-evm",
"version": "0.1.6-alpha.4",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"dependencies": {
"@ai16z/eliza": "workspace:*",
"@lifi/data-types": "5.15.5",
"@lifi/sdk": "3.4.1",
"@lifi/types": "16.3.0",
"tsup": "8.3.5",
"viem": "2.21.53"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"test": "vitest run"
},
"peerDependencies": {
"whatwg-url": "7.1.0"
}
"name": "@ai16z/plugin-evm",
"version": "0.1.6-alpha.4",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"dependencies": {
"@ai16z/eliza": "workspace:*",
"@lifi/data-types": "5.15.5",
"@lifi/sdk": "3.4.1",
"@lifi/types": "16.3.0",
"axios": "^1.6.2",
"tsup": "8.3.5",
"viem": "2.21.53"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"test": "vitest run"
},
"peerDependencies": {
"whatwg-url": "7.1.0"
}
}
4 changes: 3 additions & 1 deletion packages/plugin-evm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ export * from "./actions/bridge";
export * from "./actions/swap";
export * from "./actions/transfer";
export * from "./providers/wallet";
export * from "./providers/nft-collections";
export * from "./types";

import type { Plugin } from "@ai16z/eliza";
import { bridgeAction } from "./actions/bridge";
import { swapAction } from "./actions/swap";
import { transferAction } from "./actions/transfer";
import { evmWalletProvider } from "./providers/wallet";
import { nftCollectionProvider } from "./providers/nft-collections";

export const evmPlugin: Plugin = {
name: "evm",
description: "EVM blockchain integration plugin",
providers: [evmWalletProvider],
providers: [evmWalletProvider, nftCollectionProvider],
evaluators: [],
services: [],
actions: [transferAction, bridgeAction, swapAction],
Expand Down
85 changes: 85 additions & 0 deletions packages/plugin-evm/src/providers/nft-collections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Provider, IAgentRuntime, Memory, State } from "@ai16z/eliza";
import axios from "axios";

interface NFTCollection {
name: string;
totalSupply: number;
floorPrice: number;
volume24h: number;
}

const CACHE_TTL = 3600000; // 1 hour
let cachedCollections: NFTCollection[] | null = null;
let lastFetchTime = 0;

async function fetchWithRetry(
url: string,
options: any,
retries = 3
): Promise<any> {
try {
return await axios.get(url, options);
} catch (error) {
if (retries > 0) {
await new Promise((resolve) => setTimeout(resolve, 1000));
return fetchWithRetry(url, options, retries - 1);
}
throw error;
}
}

function sanitizeCollection(collection: any): NFTCollection {
return {
name: String(collection.name).slice(0, 100),
totalSupply: Math.max(0, parseInt(collection.totalSupply) || 0),
floorPrice: Math.max(
0,
parseFloat(collection.floorAsk?.price?.amount?.native) || 0
),
volume24h: Math.max(0, parseFloat(collection.volume?.["1day"]) || 0),
};
}

async function fetchCollectionsWithCache(): Promise<NFTCollection[]> {
const now = Date.now();
if (!cachedCollections || now - lastFetchTime > CACHE_TTL) {
const response = await fetchWithRetry(
"https://api.reservoir.tools/collections/v6",
{
headers: {
accept: "application/json",
"x-api-key": process.env.RESERVOIR_API_KEY,
},
}
);

cachedCollections = response.data.collections.map(sanitizeCollection);
lastFetchTime = now;
}
return cachedCollections;
}

function processCollections(collections: NFTCollection[]): string {
return collections
.sort((a, b) => b.volume24h - a.volume24h)
.slice(0, 10)
.map(
(collection) =>
`${collection.name}: Supply: ${collection.totalSupply}, Floor: ${collection.floorPrice.toFixed(
2
)} ETH, 24h Volume: ${collection.volume24h.toFixed(2)} ETH`
)
.join("\n");
}

export const nftCollectionProvider: Provider = {
get: async (runtime: IAgentRuntime, message: Memory, state?: State) => {
try {
const collections = await fetchCollectionsWithCache();
return `Current top NFT collections on Ethereum:\n${processCollections(collections)}`;
} catch (error) {
console.error("Error fetching NFT collections:", error);
return "Unable to fetch NFT collection data at the moment.";
}
},
};

0 comments on commit ab0df82

Please sign in to comment.