Skip to content

Send NFT Transaction

Chain of Industry edited this page Aug 18, 2022 · 10 revisions

Send NFT Transaction Instructions

Overview

This section shows you how to load all NFTs from the connected wallet and send an NFT/SFT transaction to the Maiar app for signing and broadcasting it on the blockchain. To be able to send NFTs, a user needs to be already connected. See the Connect Maiar section on how to connect the user wallet.

Load wallet NFTs

To load all NFTs from the connected wallet this method should be called:

/// <param name="LoadNFTsComplete">Callback triggered on load finish. Receives the metadata for each NFT as parameter</param>
public static void LoadWalletNFTs(UnityAction<OperationStatus, string, NFTMetadata[]> LoadNFTsComplete)

This will load metadata from all NFTs from the connected wallet. From the metadata the media files can be downloaded, and all NFT properties can be extracted and used.

To be more clear here is a full example usage:

//Method call
ElrondUnityTools.Manager.LoadWalletNFTs(LoadNFTComplete);

/// <summary>
/// Listener triggered when NFT metadata is loaded
/// </summary>
/// <param name="operationStatus">Completed, In progress or Error</param>
/// <param name="message">additional message</param>
/// <param name="allNfts">All metadata properties serialized as NFTMetadata type</param>
private void LoadNFTComplete(ElrondUnityTools.OperationStatus operationStatus, string message, ElrondUnityTools.NFTMetadata[] allNfts)
{
      if (operationStatus == ElrondUnityTools.OperationStatus.Complete)
      {
            //after all metadata is loaded display the NFTs
            for (int i = 0; i < allNfts.Length; i++)
            {
                  DisplayNft(allNfts[i]);
            }
      }
      else
      {
            Debug.Log(operationStatus + " " + message);
      }
}

All NFT properties are available inside NFTMetadata.cs

Here is a simple example on how to download and display the NFT thumbnail

private void DisplayNft(ElrondUnityTools.NFTMetadata nFTMetadata)
{
      //load and display the NFT thumbnail
      //imageHolder is the Image component used to display the image
      StartCoroutine(LoadImage(nFTMetadata.media[0].thumbnailUrl, imageHolder));
}

/// <summary>
/// Load the NFT Thumbnail from the url
/// </summary>
/// <param name="imageURL"></param>
/// <param name="displayComponent">image component to display the downloaded thumbnail picture</param>
/// <returns></returns>
private IEnumerator LoadImage(string imageURL, Image displayComponent)
{
      UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(imageURL);
      yield return webRequest.SendWebRequest();

      switch (webRequest.result)
      {
            case UnityWebRequest.Result.Success:
                  Texture2D imageTex = ((DownloadHandlerTexture)webRequest.downloadHandler).texture;
                  Sprite newSprite = Sprite.Create(imageTex, new Rect(0, 0, imageTex.width, imageTex.height), new Vector2(.5f, .5f));
                  displayComponent.sprite = newSprite;
            break;
            default:
                  Debug.LogError(webRequest.error);
            break;
      }
}

Send NFT to external wallet

To send an NFT transaction the following API method will be used:

/// <param name="destinationAddress">the address to send the NFT to</param>
/// <param name="collectionIdentifier">the collection ID</param>
/// <param name="nftNonce">nonce of the NFT, you can get it from metadata</param>
/// <param name="quantity">number of units to send</param>
/// <param name="TransactionStatus">Callback to check the transaction status</param>
public static void SendNFT(string destinationAddress, string collectionIdentifier, int nftNonce, int quantity, UnityAction<OperationStatus, string> TransactionStatus)

Usage example:

Check the following image for a better understanding on how to get the parameters:
NFT Params

The nonce parameter shown is in hex format and it is needed in decimal format so use an external convertor like this to get it:
Hex to Decimal Convertor
0de8 in decimal = 3560

//Method call
ElrondUnityTools.Manager.SendNFT("erd1jza9qqw0l24svfmm2u8wj24gdf84hksd5xrctk0s0a36leyqptgs5whlhf", "ADT-8daf0d", 3560, 1, CompleteListener);


/// <summary>
/// Track the status of the send NFT transaction
/// </summary>
/// <param name="operationStatus">Completed, In progress or Error</param>
/// <param name="message">if the operation status is complete, the message is the txHash</param>
private void CompleteListener(ElrondUnityTools.OperationStatus operationStatus, string message)
{
      if (operationStatus == ElrondUnityTools.OperationStatus.Complete)
      {
           //save transaction hash
           //txHash = message;
           //check transaction status
      }
}

Recommended next: Interacting with Smart Contracts

Clone this wiki locally