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

Marketplace API sample calls #943

Merged
merged 13 commits into from
May 8, 2024

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using System.Threading.Tasks;
using ChainSafe.Gaming.UnityPackage;
using ChainSafe.Gaming.UnityPackage.Model;
using Scripts.EVM.Remote;
using UnityEngine;

namespace Scripts.EVM.Marketplace
{
public class Marketplace
{
#region Methods

#region MarketplaceCalls

/// <summary>
/// Gets all items in a project.
/// Path: https://api.gaming.chainsafe.io/v1/projects/{projectID}/items
/// </summary>
/// <returns>MarketplaceModel.Root</returns>
public static async Task<MarketplaceModel.Root> GetProjectItems()
{
string path = $"/items?chainId={Web3Accessor.Web3.ChainConfig.ChainId}";
var response = await CSServer.GetData<MarketplaceModel.Root>(path);
return response;
}

/// <summary>
/// Gets all items in a marketplace.
/// Path: https://api.gaming.chainsafe.io/v1/projects/{projectID}/marketplaces/{marketplaceID}/items
/// </summary>
/// <param name="marketplaceId">MarketplaceID to query</param>
/// <returns>MarketplaceModel.Root</returns>
public static async Task<MarketplaceModel.Root> GetMarketplaceItems(string marketplaceId)
{
string path = $"/marketplaces/{marketplaceId}/items";
var response = await CSServer.GetData<MarketplaceModel.Root>(path);
return response;
}

/// <summary>
/// Gets items listed by token id.
/// Path: https://api.gaming.chainsafe.io/v1/projects/{projectID}/marketplaces/{marketplaceID}/items/{itemID}
/// </summary>
/// <param name="marketplaceId">MarketplaceID to query</param>
/// <param name="tokenId">TokenID to query</param>
/// <returns>MarketplaceModel.Item</returns>
public static async Task<MarketplaceModel.Item> GetItem(string marketplaceId, string tokenId)
{
string path = $"/marketplaces/{marketplaceId}/items/{tokenId}";
var response = await CSServer.GetData<MarketplaceModel.Item>(path);
return response;
}

#endregion

#region TokenCalls

/// <summary>
/// Gets all tokens in a project.
/// Path: https://api.gaming.chainsafe.io/v1/projects/{projectID}/tokens
/// </summary>
/// <returns>NftTokenModel.Root</returns>
public static async Task<NftTokenModel.Root> GetProjectTokens()
{
string path = $"/tokens?chainId={Web3Accessor.Web3.ChainConfig.ChainId}";
var response = await CSServer.GetData<NftTokenModel.Root>(path);
return response;
}

/// <summary>
/// Gets all tokens in a 721 collection.
/// Path: https://api.gaming.chainsafe.io/v1/projects/{projectID}/collections/{collectionID}/tokens
/// </summary>
/// <param name="collectionId721">CollectionID721 to query</param>
/// <returns>NftTokenModel.Root</returns>
public static async Task<NftTokenModel.Root> GetCollectionTokens721(string collectionId721)
{
string path = $"/collections/{collectionId721}/tokens";
var response = await CSServer.GetData<NftTokenModel.Root>(path);
return response;
}

/// <summary>
/// Gets all tokens in a 1155 collection.
/// Path https://api.gaming.chainsafe.io/v1/projects/{projectID}/collections/{collectionID}/tokens
/// </summary>
/// <param name="collectionId1155">CollectionID1155 to query</param>
/// <returns>NftTokenModel.Root</returns>
public static async Task<NftTokenModel.Root> GetCollectionTokens1155(string collectionId1155)
{
string path = $"/collections/{collectionId1155}/tokens";
var response = await CSServer.GetData<NftTokenModel.Root>(path);
return response;
}

/// <summary>
/// Gets the information of a token in a collection via id.
/// Path: https://api.gaming.chainsafe.io/v1/projects/{projectID}/collections/{collectionID}/tokens/:tokenID
/// Token id is optional
/// </summary>
/// <param name="collectionId">CollectionID to query</param>
/// <param name="tokenId">TokenID to query</param>
/// <returns>NftTokenModel.Token</returns>
public static async Task<NftTokenModel.Token> GetCollectionToken(string collectionId, string tokenId)
{
string path = $"/collections/{collectionId}/tokens/{tokenId}";
var response = await CSServer.GetData<NftTokenModel.Token>(path);
return response;
}

/// <summary>
/// Gets the owners of a token id in a collection.
/// Path: https://api.gaming.chainsafe.io/v1/projects/{projectID}/collections/{collectionID}/tokens/{tokenID}/owners
/// </summary>
/// <param name="collectionId">CollectionID to query</param>
/// <param name="tokenId">TokenID to query</param>
/// <returns>NftTokenModel.Token</returns>
public static async Task<MarketplaceModel.Root> GetTokenOwners(string collectionId, string tokenId)
{
string path = $"/collections/{collectionId}/tokens/{tokenId}/owners";
var response = await CSServer.GetData<MarketplaceModel.Root>(path);
return response;
}

#region Utilities

/// <summary>
/// Prints json properties in the console on new lines.
/// </summary>
/// <param name="obj">The object to print out</param>
public static void PrintObject(object obj)
{
var properties = obj.GetType().GetProperties();
foreach (var property in properties)
{
var value = property.GetValue(obj);
Debug.Log($"{property.Name}: {value}");
}
}

#endregion

#endregion

#endregion
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Threading.Tasks;
using ChainSafe.Gaming.UnityPackage;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Networking;

namespace Scripts.EVM.Remote
{
public class CSServer
{
#region Fields

private static readonly string host = "https://api.gaming.chainsafe.io/v1/projects/";

#endregion

#region Methods

/// <summary>
/// Unity web request helper function to retrieve data.
/// </summary>
/// <param name="_path"></param>
/// <returns></returns>
public static async Task<T> GetData<T>(string _path)
{
using UnityWebRequest webRequest = UnityWebRequest.Get($"{host}{Web3Accessor.Web3.ProjectConfig.ProjectId}{_path}");
await webRequest.SendWebRequest();
if (webRequest.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Error: Your project ID doesn't have a marketplace or the token ID doesn't exist, please go to dashboard and create items " + webRequest.error);
return default;
}
var json = webRequest.downloadHandler.text;
var response = JsonConvert.DeserializeObject<T>(json);
return response;
}

#endregion

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

40 changes: 0 additions & 40 deletions Packages/io.chainsafe.web3-unity/Runtime/Scripts/Model/BuyNFT.cs

This file was deleted.

This file was deleted.

Loading
Loading