Docs
搜索⌘ K
  • 主页
  • 关于 The Graph
  • 支持的网络
  • 协议合约
  • 子图
    • 子流
      • 代币 API
        • Hypergraph
          • AI Suite
            • 索引
              • 资源
                子图 > 操作指南

                8 分钟

                使用枚举对NFT市场进行分类

                使用枚举使代码更清晰,更不容易出错。这是一个在NFT市场上使用枚举的完整示例。

                枚举是什么?

                枚举或枚举类型是一种特定的数据类型,允许您定义一组特定的、允许的值。

                模式中枚举的示例

                If you’re building a subgraph to track the ownership history of tokens on a marketplace, each token might go through different ownerships, such as OriginalOwner, SecondOwner, and ThirdOwner. By using enums, you can define these specific ownerships, ensuring only predefined values are assigned.

                您可以在架构中定义枚举,定义后,您可以使用枚举值的字符串表示形式在实体上设置枚举字段。

                基于上面的示例,以下是枚举定义在模式中的样子:

                1enum TokenStatus {2  OriginalOwner3  SecondOwner4  ThirdOwner5}

                这意味着,当您在模式中使用TokenStatus类型时,您希望它恰好是预定义值之一:OriginalOwner、SecondOwner或ThirdOwner,以确保一致性和有效性。

                要了解更多关于枚举的信息,请查看创建子图 和 GraphQL文档⁠。

                使用枚举的好处

                • 清晰度: 枚举为值提供有意义的名称,使数据更容易理解。
                • 有效性: 枚举强制执行严格的值定义,防止无效的数据条目。
                • 可维护性: 当您需要更改或添加新类别时,枚举允许您以专注的方式完成此操作。

                无枚举

                如果你选择将类型定义为字符串而不是使用枚举,你的代码可能看起来像这样:

                1type Token @entity {2  id: ID!3  tokenId: BigInt!4  owner: Bytes! # Owner of the token5  tokenStatus: String! # String field to track token status6  timestamp: BigInt!7}

                在此模式中,TokenStatus是一个简单的字符串,没有特定的、允许的值。

                为什么这是个问题?

                • TokenStatus值没有限制,因此任何字符串都可能被意外分配。这使得很难确保只设置有效的状态,如OriginalOwner、SecondOwner或ThirdOwner。
                • 很容易出现拼写错误,例如Orgnalowner而不是OriginalOwner,从而使数据和潜在查询不可靠。

                带枚举

                您可以为TokenStatus定义一个具有特定值的枚举,而不是分配自由格式的字符串:OriginalOwner、SecondOwner或ThirdOwner。使用枚举可确保只使用允许的值。

                枚举提供类型安全性,最大限度地减少拼写错误风险,并确保一致可靠的结果。

                定义NFT市场的枚举

                注意:以下指南使用CryptoCoven NFT智能合约。

                To define enums for the various marketplaces where NFTs are traded, use the following in your subgraph schema:

                1# Enum for Marketplaces that the CryptoCoven contract interacted with(likely a Trade/Mint)2enum Marketplace {3  OpenSeaV1 # Represents when a CryptoCoven NFT is traded on the marketplace4  OpenSeaV2 # Represents when a CryptoCoven NFT is traded on the OpenSeaV2 marketplace5  SeaPort # Represents when a CryptoCoven NFT is traded on the SeaPort marketplace6  LooksRare # Represents when a CryptoCoven NFT is traded on the LookRare marketplace7  # ...and other marketplaces8}

                NFT市场使用枚举

                Once defined, enums can be used throughout your subgraph to categorize transactions or events.

                例如,在记录NFT销售时,您可以使用枚举指定交易中涉及的市场。

                实现NFT市场功能

                以下是如何实现一个函数,以字符串形式从枚举中检索市场名称:

                1export function getMarketplaceName(marketplace: Marketplace): string {2  // Using if-else statements to map the enum value to a string3  if (marketplace === Marketplace.OpenSeaV1) {4    return 'OpenSeaV1' // If the marketplace is OpenSea, return its string representation5  } else if (marketplace === Marketplace.OpenSeaV2) {6    return 'OpenSeaV2'7  } else if (marketplace === Marketplace.SeaPort) {8    return 'SeaPort' // If the marketplace is SeaPort, return its string representation9  } else if (marketplace === Marketplace.LooksRare) {10    return 'LooksRare' // If the marketplace is LooksRare, return its string representation11    // ... and other market places12  }13}

                使用枚举的最佳实践

                • **一致命名:**为枚举值使用清晰的、描述性的名称,以提高可读性。
                • 集中管理: 将枚举保存在单个文件中以保持一致性。这使得枚举更容易更新,并确保它们是唯一的事实来源。
                • 文档: 在枚举中添加注释,以阐明其目的和用法。

                在查询中使用枚举

                查询中的枚举有助于提高数据质量,并使结果更容易解释。它们充当过滤器和响应元素,确保一致性并减少市场价值中的错误。

                详情

                • **使用枚举进行筛选:**枚举提供清晰的筛选,使您能够自信地包含或排除特定的市场。
                • **响应中的枚举:**枚举保证只返回可识别的市场名称,使结果标准化和准确。

                示例查询

                问题1:NFT市场互动最高的账户

                此查询执行以下操作:

                • 它找到了具有最高独特NFT市场互动的帐户,这对于分析跨市场活动非常有用。
                • 市场字段使用市场枚举,确保响应中一致且经过验证的市场值。
                1{2  accounts(first: 1, orderBy: uniqueMarketplacesCount, orderDirection: desc) {3    id4    sendCount5    receiveCount6    totalSpent7    uniqueMarketplacesCount8    marketplaces {9      marketplace # This field returns the enum value representing the marketplace10    }11  }12}

                返回

                此响应提供了帐户详细信息和具有枚举值的独特市场交互列表,以实现标准化的清晰度:

                1{2  "data": {3    "accounts": [4      {5        "id": "0xb3abc96cb9a61576c03c955d75b703a890a14aa0",6        "sendCount": "44",7        "receiveCount": "44",8        "totalSpent": "1197500000000000000",9        "uniqueMarketplacesCount": "7",10        "marketplaces": [11          {12            "marketplace": "OpenSeaV1"13          },14          {15            "marketplace": "OpenSeaV2"16          },17          {18            "marketplace": "GenieSwap"19          },20          {21            "marketplace": "CryptoCoven"22          },23          {24            "marketplace": "Unknown"25          },26          {27            "marketplace": "LooksRare"28          },29          {30            "marketplace": "NFTX"31          }32        ]33      }34    ]35  }36}

                问题2:CryptoCoven交易最活跃的市场

                此查询执行以下操作:

                • 它确定了CryptoCoven交易量最大的市场。
                • 它使用市场枚举来确保响应中只显示有效的市场类型,从而为您的数据增加可靠性和一致性。
                1{2  marketplaceInteractions(first: 1, orderBy: transactionCount, orderDirection: desc) {3    marketplace4    transactionCount5  }6}

                结果2

                预期的响应包括市场和相应的交易计数,使用枚举指示市场类型:

                1{2  "data": {3    "marketplaceInteractions": [4      {5        "marketplace": "Unknown",6        "transactionCount": "222"7      }8    ]9  }10}

                问题3:交易量高的市场互动

                此查询执行以下操作:

                • 它检索了交易量超过100的前四大市场,不包括“未知”市场。
                • 它使用枚举作为筛子,以确保只包含有效的市场类型,从而提高准确性。
                1{2  marketplaceInteractions(3    first: 44    orderBy: transactionCount5    orderDirection: desc6    where: { transactionCount_gt: "100", marketplace_not: "Unknown" }7  ) {8    marketplace9    transactionCount10  }11}

                结果3

                预期产出包括符合标准的市场,每个市场由一个枚举值表示:

                1{2  "data": {3    "marketplaceInteractions": [4      {5        "marketplace": "NFTX",6        "transactionCount": "201"7      },8      {9        "marketplace": "OpenSeaV1",10        "transactionCount": "148"11      },12      {13        "marketplace": "CryptoCoven",14        "transactionCount": "117"15      },16      {17        "marketplace": "OpenSeaV1",18        "transactionCount": "111"19      }20    ]21  }22}

                其他资源

                如需更多信息,请查看本指南的库存⁠。

                ⁠在GitHub上编辑⁠

                传输到The GraphHow to Secure API Keys Using Next.js Server Components
                在此页面上
                • 枚举是什么?
                • 模式中枚举的示例
                • 使用枚举的好处
                • 无枚举
                • 带枚举
                • 定义NFT市场的枚举
                • NFT市场使用枚举
                • 实现NFT市场功能
                • 使用枚举的最佳实践
                • 在查询中使用枚举
                • 示例查询
                • 其他资源
                The GraphStatusTestnetBrand AssetsForum安全Privacy PolicyTerms of Service