6 minutes
Categorize NFT Marketplaces Using Enums
Use Enums to make your code cleaner and less error-prone. Here’s a full example of using Enums on NFT marketplaces.
What are Enums?
Enums, or enumeration types, are a specific data type that allows you to define a set of specific, allowed values.
Example of Enums in Your Schema
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.
You can define enums in your schema, and once defined, you can use the string representation of the enum values to set an enum field on an entity.
Here’s what an enum definition might look like in your schema, based on the example above:
1enum TokenStatus {2 OriginalOwner3 SecondOwner4 ThirdOwner5}
This means that when you use the TokenStatus
type in your schema, you expect it to be exactly one of predefined values: OriginalOwner
, SecondOwner
, or ThirdOwner
, ensuring consistency and validity.
To learn more about enums, check out Creating a Subgraph and GraphQL documentation.
Benefits of Using Enums
- Clarity: Enums provide meaningful names for values, making data easier to understand.
- Validation: Enums enforce strict value definitions, preventing invalid data entries.
- Maintainability: When you need to change or add new categories, enums allow you to do this in a focused manner.
Without Enums
If you choose to define the type as a string instead of using an Enum, your code might look like this:
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}
In this schema, TokenStatus
is a simple string with no specific, allowed values.
Why is this a problem?
- There’s no restriction of
TokenStatus
values, so any string can be accidentally assigned. This makes it hard to ensure that only valid statuses likeOriginalOwner
,SecondOwner
, orThirdOwner
are set. - It’s easy to make typos such as
Orgnalowner
instead ofOriginalOwner
, making the data and potential queries unreliable.
With Enums
Instead of assigning free-form strings, you can define an enum for TokenStatus
with specific values: OriginalOwner
, SecondOwner
, or ThirdOwner
. Using an enum ensures only allowed values are used.
Enums provide type safety, minimize typo risks, and ensure consistent and reliable results.
Defining Enums for NFT Marketplaces
Note: The following guide uses the CryptoCoven NFT smart contract.
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}
Using Enums for NFT Marketplaces
Once defined, enums can be used throughout your Subgraph to categorize transactions or events.
For example, when logging NFT sales, you can specify the marketplace involved in the trade using the enum.
Implementing a Function for NFT Marketplaces
Here’s how you can implement a function to retrieve the marketplace name from the enum as a string:
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}
Best Practices for Using Enums
- Consistent Naming: Use clear, descriptive names for enum values to improve readability.
- Centralized Management: Keep enums in a single file for consistency. This makes enums easier to update and ensures they are the single source of truth.
- Documentation: Add comments to enum to clarify their purpose and usage.
Using Enums in Queries
Enums in queries help you improve data quality and make your results easier to interpret. They function as filters and response elements, ensuring consistency and reducing errors in marketplace values.
Specifics
- Filtering with Enums: Enums provide clear filters, allowing you to confidently include or exclude specific marketplaces.
- Enums in Responses: Enums guarantee that only recognized marketplace names are returned, making the results standardized and accurate.
Sample Queries
Query 1: Account With The Highest NFT Marketplace Interactions
This query does the following:
- It finds the account with the highest unique NFT marketplace interactions, which is great for analyzing cross-marketplace activity.
- The marketplaces field uses the marketplace enum, ensuring consistent and validated marketplace values in the response.
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}
Returns
This response provides account details and a list of unique marketplace interactions with enum values for standardized clarity:
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}
Query 2: Most Active Marketplace for CryptoCoven transactions
This query does the following:
- It identifies the marketplace with the highest volume of CryptoCoven transactions.
- It uses the marketplace enum to ensure that only valid marketplace types appear in the response, adding reliability and consistency to your data.
1{2 marketplaceInteractions(first: 1, orderBy: transactionCount, orderDirection: desc) {3 marketplace4 transactionCount5 }6}
Result 2
The expected response includes the marketplace and the corresponding transaction count, using the enum to indicate the marketplace type:
1{2 "data": {3 "marketplaceInteractions": [4 {5 "marketplace": "Unknown",6 "transactionCount": "222"7 }8 ]9 }10}
Query 3: Marketplace Interactions with High Transaction Counts
This query does the following:
- It retrieves the top four marketplaces with over 100 transactions, excluding “Unknown” marketplaces.
- It uses enums as filters to ensure that only valid marketplace types are included, increasing accuracy.
1{2 marketplaceInteractions(3 first: 44 orderBy: transactionCount5 orderDirection: desc6 where: { transactionCount_gt: "100", marketplace_not: "Unknown" }7 ) {8 marketplace9 transactionCount10 }11}
Result 3
Expected output includes the marketplaces that meet the criteria, each represented by an enum value:
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}
Additional Resources
For additional information, check out this guide’s repo.