-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added NFT's of a Contract + Updated ownership Data example
- Loading branch information
Showing
13 changed files
with
6,222 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.