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

DB for NFT knowledge #212

Open
IkigaiLabsETH opened this issue Dec 19, 2024 · 0 comments
Open

DB for NFT knowledge #212

IkigaiLabsETH opened this issue Dec 19, 2024 · 0 comments
Assignees

Comments

@IkigaiLabsETH
Copy link
Owner

To store knowledge about NFT collections and integrate it with the Eliza framework, the best approach is to use PostgreSQL with the pgvector extension. Here's why this is the optimal solution:

Database Choice: PostgreSQL

PostgreSQL is the ideal choice for storing NFT collection data and integrating with Eliza for several reasons:

  1. Vector Search Capabilities: PostgreSQL with pgvector supports efficient vector similarity searches, which is crucial for embedding-based queries on NFT metadata[1].

  2. JSONB Support: PostgreSQL's JSONB type allows flexible storage of NFT metadata, accommodating varying attributes across collections[1].

  3. Scalability: PostgreSQL offers robust scalability options, including connection pooling and high performance, which are essential for handling large NFT datasets[1].

  4. ACID Compliance: PostgreSQL ensures data integrity through full ACID compliance, critical for maintaining accurate NFT ownership and transaction records[5].

  5. Object-Oriented Features: PostgreSQL's object-oriented capabilities allow for creating custom types and table inheritance, which can be useful for modeling different NFT collection structures[5].

Implementation with Eliza

To implement this solution using Eliza's database adapters:

  1. Use the PostgresDatabaseAdapter:
import { PostgresDatabaseAdapter } from "@ai16z/adapter-postgres";

const db = new PostgresDatabaseAdapter({
  connectionString: process.env.DATABASE_URL,
  max: 20, // Connection pool size
  idleTimeoutMillis: 30000,
  connectionTimeoutMillis: 2000,
});
  1. Create a schema optimized for NFT data:
CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE nft_collections (
  id UUID PRIMARY KEY,
  name TEXT NOT NULL,
  description TEXT,
  creator_address TEXT NOT NULL,
  total_supply INTEGER NOT NULL,
  floor_price NUMERIC,
  volume_24h NUMERIC,
  metadata JSONB,
  embedding vector(1536)
);

CREATE INDEX nft_embedding_idx ON
nft_collections USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100);
  1. Implement memory operations for NFT data:
async createNFTCollection(collection: NFTCollection) {
  await db.createMemory({
    id: collection.id,
    type: "nft_collections",
    content: {
      name: collection.name,
      description: collection.description,
      creator_address: collection.creatorAddress,
      total_supply: collection.totalSupply,
      floor_price: collection.floorPrice,
      volume_24h: collection.volume24h,
      metadata: collection.metadata
    },
    embedding: collection.embedding,
    userId: collection.creatorAddress,
    roomId: "global", // or a specific room for NFT collections
    agentId: "nft_agent",
    createdAt: Date.now(),
    unique: true,
  });
}
  1. Implement vector search for similar NFT collections:
async searchSimilarNFTCollections(embedding: number[], limit: number = 10) {
  const memories = await db.searchMemories({
    tableName: "nft_collections",
    roomId: "global",
    embedding: embedding,
    match_threshold: 0.7,
    match_count: limit,
    unique: true,
  });
  return memories.map(memory => ({
    ...memory.content,
    similarity: memory.similarity
  }));
}

By using PostgreSQL with pgvector and integrating it with Eliza's database adapters, you can efficiently store, retrieve, and search NFT collection data while leveraging the framework's built-in functionality for memory management and vector operations.

Citations:
[1] https://ai16z.github.io/eliza/docs/packages/adapters/
[2] https://vocal.media/theChain/choosing-the-perfect-database-for-your-nft-marketplace
[3] https://www.reddit.com/r/nextjs/comments/uhz3gu/im_trying_to_choose_between_supabase_and/
[4] https://www.reddit.com/r/ethdev/comments/1eul2f2/to_create_an_nft_sql_database_is_it_best_to/
[5] https://strapi.io/blog/relational-databases-postgresql-vs-mariadb-vs-mysql-vs-sqlite
[6] https://stackoverflow.com/questions/48844682/best-database-type-to-store-data-that-will-be-used-with-a-blockchain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants