Skip to content

Send NFT Transaction

Chain of Industry edited this page Dec 1, 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 loads metadata from all NFTs from the connected wallet. The media files can be downloaded from the metadata, and all NFT properties can be extracted and used.

To be more clear, here is a full example of usage:

//Method call
MultiversXUnityTools.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(MultiversXUnityTools.OperationStatus operationStatus, string message, MultiversXUnityTools.NFTMetadata[] allNfts)
{
      if (operationStatus == MultiversXUnityTools.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 of how to download and display the NFT thumbnail

private void DisplayNft(MultiversXUnityTools.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 an external wallet

To send an NFT transaction, the following API method is 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, ulong nftNonce, int quantity, UnityAction<OperationStatus, string> TransactionStatus)

Usage example:

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

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

//Method call
MultiversXUnityTools.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(MultiversXUnityTools.OperationStatus operationStatus, string message)
{
      if (operationStatus == MultiversXUnityTools.OperationStatus.Complete)
      {
           // store the transaction hash for later use
           txHash = message;
           //check the status of the transaction. More details in Check Transaction Status section
           MultiversXUnityTools.Manager.CheckTransactionStatus(txHash, BlockchainTransactionListener, 6);
      }
}

After SendNFT is called, the Maiar app receives a notification to sign the transaction. If the user approves the transaction, the signature will be automatically applied, and the transaction will be sent to the Elrond Blockchain for processing. In this case, an operationStatus of Complete will be received. If the user declines to sign or an error occurs, an operationStatus of Error will be received.

This concludes the send NFT transaction Unity tutorial.

Recommended next: Interacting with Smart Contracts