Xerial
  • Intro
    • ⚡Introduction
    • 🏁Getting Started
    • â„šī¸Before We Start: Important
  • 👷Build
    • Build Your Account
    • Create Your Assets
    • Inventory
    • Marketplace
    • User Flow
    • Best Practices
      • Attribute Guide
        • Attribute Use Cases
  • â˜„ī¸Xerial APIs
    • đŸ—ī¸API Authorization
    • 🛅Wallet API
      • Getting Started
      • Custodial Wallets
        • Create User
        • Create Wallet
        • Get Wallets
      • Wallet Management
        • Get Chain Native Currency Balance
        • Get Supported ERC20 Tokens Balances
        • Get Project Inventory
      • Transaction Proccessing
        • Transfer Xerial NFT
        • Transfer Chain Native Currency
        • Transfer ERC20 Token
        • Primary Purchase Xerial NFT
        • Secondary Purchase
        • List Xerial NFT
        • Delist Xerial NFT
        • Execute Custom Transaction
      • Errors Handling
        • Authentication Errors
        • Wallet Management Errors
        • Transaction Proccessing Errors
    • 🛒Marketplace API
      • Get Listed NFTs
      • Get Community Listed Items
      • Get Collections
      • Get Project Data
    • đŸ–ŧī¸NFT API
      • NFT Drop
    • 🌐Xerial Global Wallets
      • Get User Wallet Info
      • Get Global Inventory
      • User Authorization
      • Logout
  • đŸ› ī¸Xerial SDK
    • Wallet SDK
  • đŸŽī¸Gaming Toolkit
    • đŸ•šī¸Xerial Unity SDK Guide
      • âŦ‡ī¸Installation
      • âš™ī¸Configuration
      • Xerial Services
        • Set Up Xerial Services
        • Service Managers
          • Session Manager
          • Wallet Manager
            • UserData Class
              • Account Class
              • Wallet Class
              • Creating UserData Class
          • Inventory Manager
            • NFTs
              • Metadata
              • Attributes
            • NFTInventory Class
            • Creating NFT Inventory
          • MarketPlace Manager
    • Unreal Engine SDKs
Powered by GitBook
On this page
  • Usage
  • Deserializing NFTs from Project Inventory Data
  • NFT
  • Collection
  • Metadata
  • Contract
  • Attributes
  • Token
  • Store the NFT inventory
  1. Gaming Toolkit
  2. Xerial Unity SDK Guide
  3. Xerial Services
  4. Service Managers
  5. Inventory Manager

Creating NFT Inventory

After requesting the project inventory from Xerial services, the JSON response needs to be processed to create an instance of the UserData class.

Usage

using System.Collections.Generic;
using UnityEngine;
using SimpleJSON;
using Xerial;

public class NFTResponseHandler
{
    public void HandleNFTResponse(string response)
    {

        XerialServices.instance.inventoryManager.inventory = null;

        JSONArray jsonArray = JSON.Parse(response).AsArray;

        if (jsonArray != null && jsonArray.Count > 0)
        {
            JSONNode rootNode = jsonArray[0];
//Deserialize the object array            
        }
}

Deserializing NFTs from Project Inventory Data

To deserialize the NFTs from the JSON response within the project inventory data.

NFT

First, we retrieve the node containing NFT data from the JSON response received from the server. c

NFT nft = new NFT();

int quantity = rootNode["quantity"].AsInt;
nft.amount = quantity;

JSONNode collectionNode = rootNode["collection"];
nft.collection = new Collection();

JSONNode metadataNode = rootNode["metadata"];
nft.metadata = new Metadata();
 
JSONArray tokensArray = rootNode["tokenIds"].AsArray;
nft.tokens = new List<Token>();

Collection

nft.collection = new Collection();
nft.collection.Id = collectionNode["id"].Value;
nft.collection.TxHash = collectionNode["txHash"].Value;

Metadata

nft.metadata = new Metadata();
nft.metadata.name = metadataNode["name"].Value;
nft.metadata.description = metadataNode["description"].Value;
nft.metadata.image = metadataNode["image"].Value;
nft.metadata.externalUrl = metadataNode["externalUrl"].Value;

JSONNode contractnode = rootNode["metadata"]["contract"];

nft.metadata.contract = new Contract();

nft.metadata.attributes = new List<Attributes>();
foreach (JSONNode attributeNode in metadataNode["attributes"].AsArray)
{
    Attributes attribute = new Attributes();
    nft.metadata.attributes.Add(attribute);
}

Contract

JSONNode contractnode = rootNode["metadata"]["contract"];

nft.metadata.contract = new Contract();
nft.metadata.contract.address = contractnode["address"].Value;

Attributes

nft.metadata.attributes = new List<Attributes>();
foreach (JSONNode attributeNode in metadataNode["attributes"].AsArray)
{
    Attributes attribute = new Attributes();
    attribute.type = attributeNode["type"].Value;
    attribute.name = attributeNode["name"].Value;
    attribute.value = attributeNode["value"].Value;
    nft.metadata.attributes.Add(attribute);
}

Token

nft.tokens = new List<Token>();
foreach (JSONNode tokenNode in tokensArray)
{
    Token token = new Token();
    token.tokenId = tokenNode.AsInt;
    nft.tokens.Add(token);
}

Store the NFT inventory

Create NFT Instances: For each NFT entry in the JSON data, we add an instance of the NFT class to represent the NFT inside the inventory.

if (XerialServices.instance.inventoryManager.inventory != null)
{
    XerialServices.instance.inventoryManager.inventory.AddNFT(nft);
}
else
{
    XerialServices.instance.inventoryManager.inventory = new NFTInventory();
    XerialServices.instance.inventoryManager.inventory.AddNFT(nft);
}
PreviousNFTInventory ClassNextMarketPlace Manager

Last updated 1 year ago

đŸŽī¸
đŸ•šī¸