Skip to content

Commit

Permalink
feat: Added NFT's of a Contract + Updated ownership Data example
Browse files Browse the repository at this point in the history
  • Loading branch information
saszer committed Apr 18, 2022
1 parent 8f0391c commit ada6509
Show file tree
Hide file tree
Showing 13 changed files with 6,222 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Runtime/Internal/models/NFTDetails_model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace NFTPort
{
[Serializable]
public class NFTDetails_model : MonoBehaviour
public class NFTDetails_model
{
[Serializable]
public class Metadata
Expand Down
74 changes: 74 additions & 0 deletions Runtime/Internal/models/NFTs_OfAContract_model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

using System;
using System.Collections.Generic;

namespace NFTPort
{
[Serializable]
public class NFTs_OfAContract_model
{
[Serializable]
public class Attribute
{
public string trait_type;
public string value;
}

[Serializable]
public class Metadata
{
public string name;
public string description;
public string image;
public int edition;
public object date;
public List<Attribute> attributes;
public string compiler;
public string background_color;
public string external_url;
public string animation_url;
public string dna;
}

[Serializable]
public class FileInformation
{
public int height;
public int width;
public int file_size;
}

[Serializable]
public class Nft
{
public string name;
public string chain;
public string contract_address;
public string token_id;
public Metadata metadata;
public string metadata_url;
public string file_url;
public string cached_file_url;
public DateTime mint_date;
public FileInformation file_information;
public DateTime updated_date;
}

[Serializable]
public class Contract
{
public string name;
public string symbol;
public string type;
}

[Serializable]
public class Root
{
public string response;
public List<Nft> nfts;
public Contract contract;
public int total;
}
}
}
66 changes: 0 additions & 66 deletions Runtime/Internal/models/fromContract_model.cs

This file was deleted.

4 changes: 4 additions & 0 deletions Runtime/Internal/models/ownedbyAddress_model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
namespace NFTPort
{
[Serializable]
<<<<<<< Updated upstream:NFT_UnitySDK/Packages/com.nftport.nftport/Runtime/Internal/models/ownedbyAddress_model.cs
public class ownedbyAddress_model : MonoBehaviour
=======
public class NFTs_OwnedByAnAccount_model
>>>>>>> Stashed changes:NFT_UnitySDK/Packages/com.nftport.nftport/Runtime/Internal/models/NFTs_OwnedByAnAccount_model.cs
{
[Serializable]
public class Trait
Expand Down
217 changes: 217 additions & 0 deletions Runtime/NFTs_OfAContract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
using System.Collections;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;

namespace NFTPort
{
/// <summary>
/// NFTs of a contract / collections
/// </summary>
public class NFTs_OfAContract : MonoBehaviour
{
/// <summary>
/// Currently Supported chains for this endpoint.
/// </summary>
public enum Chains
{
ethereum,
polygon,
rinkeby
}

public enum Includes
{
Default,
metadata,
all
}

#region Parameter Defines

[SerializeField]
private Chains chain = Chains.ethereum;

[SerializeField]
private string contract_address = "Input Contract Address To Fetch NFT's from";

[Header("Optional:")]

[Header("Include optional data in the response.")]
[Tooltip("default is the default response, metadata includes NFT metadata and cached_file_url, and all includes extra information like file_information and mint_date in Retrieve NFT details.")]
[SerializeField]
Includes include = Includes.all;


private string RequestUriInit = "https://api.nftport.xyz/v0/nfts/";
private string WEB_URL;
private string _apiKey;
private bool destroyAtEnd = false;


private UnityAction<string> OnErrorAction;
private UnityAction<NFTs_OfAContract_model.Root> OnCompleteAction;

[Space(20)]
//[Header("Called After Successful API call")]
public UnityEvent afterSuccess;
//[Header("Called After Error API call")]
public UnityEvent afterError;

[Header("Run Component when this Game Object is Set Active")]
[SerializeField] private bool onEnable = false;
public bool debugErrorLog = true;
public bool debugLogRawApiResponse = false;

[Header("Gets filled with data and can be referenced:")]
public NFTs_OfAContract_model.Root ownedByContractModel;

#endregion


private void Awake()
{
Port.Initialise();
_apiKey = Port.GetUserApiKey();

}

private void OnEnable()
{
if (onEnable)
Run();
}

#region SetParams and Chain Functions

/// <summary>
/// Initialize creates a gameobject and assings this script as a component. This must be called if you are not refrencing the script any other way and it doesn't already exists in the scene.
/// </summary>
/// <param name="destroyAtEnd"> Optional bool parameter can set to false to avoid Spawned GameObject being destroyed after the Api process is complete. </param>
public static NFTs_OfAContract Initialize(bool destroyAtEnd = true)
{
var _this = new GameObject("NFTs Of a Contract").AddComponent<NFTs_OfAContract>();
_this.destroyAtEnd = destroyAtEnd;
_this.onEnable = false;
_this.debugErrorLog = false;
return _this;
}

/// <summary>
/// Set Contract Address to retrieve NFTs from as string
/// </summary>
/// <param name="contract_address"> as string.</param>
public NFTs_OfAContract SetContractAddress(string contract_address)
{
this.contract_address = contract_address;
return this;
}

/// <summary>
/// Blockchain from which to query NFTs.
/// </summary>
/// <param name="chain"> Choose from available 'Chains' enum</param>
public NFTs_OfAContract SetChain(Chains chain)
{
this.chain = chain;
return this;
}

/// <summary>
/// Include optional data in the response. default is the default response and metadata includes NFT metadata, like in Retrieve NFT details, and contract_information includes information of the NFT’s contract, Choose from Includes.
/// </summary>
/// <param name="include"> Choose from available 'Includes' enum </param>
public NFTs_OfAContract SetInclude(Includes include)
{
this.include = include;
return this;
}

/// <summary>
/// Action on succesfull API Fetch.
/// </summary>
/// <param name="NFTs_OwnedByAnAccount_model.Root"> Use: .OnComplete(NFTs=> NFTsOfUser = NFTs) , where NFTsOfUser = NFTs_OwnedByAnAccount_model.Root;</param>
/// <returns> NFTs_OwnedByAnAccount_model.Root </returns>
public NFTs_OfAContract OnComplete(UnityAction<NFTs_OfAContract_model.Root> action)
{
this.OnCompleteAction = action;
return this;
}

/// <summary>
/// Action on Error
/// </summary>
/// <param name="UnityAction action"> string.</param>
/// <returns> Information on Error as string text.</returns>
public NFTs_OfAContract OnError(UnityAction<string> action)
{
this.OnErrorAction = action;
return this;
}

#endregion


#region Run - API
/// <summary>
/// Runs the Api call and fills the corresponding model in the component on success.
/// </summary>
public NFTs_OfAContract_model.Root Run()
{
WEB_URL = BuildUrl();
StartCoroutine(CallAPIProcess());
return ownedByContractModel;
}

string BuildUrl()
{
WEB_URL = RequestUriInit + contract_address + "?chain=" + chain.ToString().ToLower() + "&include=" + include.ToString().ToLower();
return WEB_URL;
}

IEnumerator CallAPIProcess()
{
//Make request
UnityWebRequest request = UnityWebRequest.Get(WEB_URL);
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Authorization", _apiKey);

{
yield return request.SendWebRequest();
string jsonResult = System.Text.Encoding.UTF8.GetString(request.downloadHandler.data);

if(debugLogRawApiResponse)
Debug.Log(jsonResult);

if (request.error != null)
{
if(OnErrorAction!=null)
OnErrorAction($"Null data. Response code: {request.responseCode}. Result {jsonResult}");
if(debugErrorLog)
Debug.Log($"Null data. Response code: {request.responseCode}. Result {jsonResult}");
if(afterError!=null)
afterError.Invoke();
yield break;
}
else
{
//Fill Data Model from recieved class
ownedByContractModel = JsonConvert.DeserializeObject<NFTs_OfAContract_model.Root>(jsonResult);

if(OnCompleteAction!=null)
OnCompleteAction.Invoke(ownedByContractModel);

if(afterSuccess!=null)
afterSuccess.Invoke();
}
}
request.Dispose();
if(destroyAtEnd)
Destroy (this.gameObject);
}

#endregion
}

}
11 changes: 11 additions & 0 deletions Runtime/NFTs_OfAContract.cs.meta

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

Loading

0 comments on commit ada6509

Please sign in to comment.