Skip to content

All API Methods

Chain of Industry edited this page Sep 6, 2022 · 11 revisions

API Methods

All API methods are static and are available inside Manager.cs


Connect

This is required before making any blockchain transaction. It is used to initialize the WalletConnect socket used for Maiar connection.

/// <param name="OnWalletConnected">Callback triggered when user wallet connected</param>
/// <param name="OnWalletDisconnected">Callback triggered when user wallet disconnected</param>
/// <param name="qrImage">The image component that will display the QR for Maiar login</param>
public static void Connect(UnityAction<AccountDto> OnWalletConnected, UnityAction OnWalletDisconnected, Image qrImage)

Usage example:

//method call
ElrondUnityTools.Manager.Connect(OnConnected, OnDisconnected, qrImage);

private void OnConnected(AccountDto connectedAccount)
{
    //do actions with the connected wallet
}

private void OnDisconnected()
{
    //do actions when wallet disconected from the app
}

Is Wallet Connected

Simple check for connection status

/// <returns>true - if connection to the wallet is active</returns>
public static bool IsWalletConnected()

Usage example:

//method call
if (ElrondUnityTools.Manager.IsWalletConnected() == true)
{
    //do something if connection is active
}

Deep Link Login

Login from the same mobile device that has the Maiar app already installed. It will automatically open the Maiar app. The Connect method must be called first.

public static void DeepLinkLogin()

Usage example:

ElrondUnityTools.Manager.DeepLinkLogin();

Send EGLD Transaction

Send an EGLD transaction for signing to the Maiar wallet. After the signing, the transaction is automatically broadcasted to the blockchain.

/// <param name="destinationAddress">The erd address of the receiver</param>
/// <param name="amount">Amount of EGLD to send(in decimals)</param>
/// <param name="data">An optional custom message</param>
/// <param name="TransactionStatus">Callback to track the status of the transaction. At complete, the message will be the transaction hash</param>
public static void SendEGLDTransaction(string destinationAddress, string amount, string data, UnityAction<OperationStatus, string> TransactionStatus)

Usage example:

//Method call
ElrondUnityTools.Manager.SendEGLDTransaction("erd1jza9qqw0l24svfmm2u8wj24gdf84hksd5xrctk0s0a36leyqptgs5whlhf", "0.001", "You see this?", SigningStatusListener);

/// <summary>
/// Track the status of the signing transaction
/// </summary>
/// <param name="operationStatus"></param>
/// <param name="message">if the operation status is complete, the message is the txHash</param>
private void SigningStatusListener(ElrondUnityTools.OperationStatus operationStatus, string message)
{
    if (operationStatus == ElrondUnityTools.OperationStatus.Complete)
    {
        //save transaction hash
        //txHash = message;
        //check transaction status
    }
}

Send ESDT Transaction

Send an ESDT transaction for signing to the Maiar wallet. After the signing the transaction is automatically broadcasted to the blockchain.

/// <param name="destinationAddress">The erd address of the receiver</param>
/// <param name="token">Token to send</param>
/// <param name="amount">Amount of token to send(in decimals)</param>
/// <param name="TransactionStatus">Callback to track the status of the transaction. At complete, the message will be the transaction hash</param>
public static void SendESDTTransaction(string destinationAddress, ESDTToken token, string amount, UnityAction<OperationStatus, string> TransactionStatus)

Usage example:

//Method call
ElrondUnityTools.Manager.SendESDTTransaction("erd1jza9qqw0l24svfmm2u8wj24gdf84hksd5xrctk0s0a36leyqptgs5whlhf",ElrondUnityTools.SupportedESDTTokens.USDC, "0.1", SigningStatusListener);

/// <summary>
/// Track the status of the signing transaction
/// </summary>
/// <param name="operationStatus"></param>
/// <param name="message">if the operation status is complete, the message is the txHash</param>
private void SigningStatusListener(ElrondUnityTools.OperationStatus operationStatus, string message)
{
    if (operationStatus == ElrondUnityTools.OperationStatus.Complete)
    {
        //save transaction hash
        //txHash = message;
        //check transaction status
    }
}

Load wallet NFTs

Load metadata from all NFT properties from the connected wallet. From the metadata, the media files can be downloaded.

/// <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)

Usage example:

//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);
      }
}

Send NFT Transaction

Send an NFT to the destination address. After the signing, the transaction is automatically broadcasted to the blockchain.

/// <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:

//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
      }
}

Query a Smart Contract

To get any information about SC, a query needs to be performed.

/// <summary>
/// Make a smart contract query
/// </summary>
/// <param name="scAddress">The address of the smart contract</param>
/// <param name="methodName">The method to call</param>
/// <param name="args">The list of arguments</param>
/// <param name="QueryComplete">Callback to get the result of the query after finished</param>
public static void MakeSCQuery(string scAddress, string methodName, string[] args, UnityAction<OperationStatus, string, SCData> QueryComplete)
{
      ConnectionManager.Instance.MakeSCQuery(scAddress, methodName, args, QueryComplete);
}

Usage example:

//Method call. This will call the getSum method from the above SC
//If the method you are calling has no parameters, send null as a param 
ElrondUnityTools.Manager.MakeSCQuery("erd1qqqqqqqqqqqqqpgqmm2m825y2t9nya0yqeg3nqlh2q50e7pd0eqq98uw2e", "getSum", null, QueryComplete);

/// <summary>
/// Triggered when Smart contract query is complete
/// </summary>
/// <param name="operationStatus">Completed, In progress or Error</param>
/// <param name="message">additional message</param>
/// <param name="data">deserialized returned data</param>
private void QueryComplete(ElrondUnityTools.OperationStatus operationStatus, string message, ElrondUnityTools.SCData data)
{
      if (operationStatus == ElrondUnityTools.OperationStatus.Complete)
      {
            //the returned data is an array(I do not know at this point how to create a SC that returns an Array of data instead of a single element)
            string encodedText = data.returnData[0];

            //convert the received data to bytes
            byte[] bytes = Convert.FromBase64String(encodedText);

            //convert the bytes array so hex
            string hexString = Erdcsharp.Domain.Helper.Converter.ToHexString(bytes);

            //convert the hex string to your data type(int, float, string, etc) 
            //in this case the return data is an int
            int result = Convert.ToInt64(hexString, 16);
            Debug.Log(result);
      }
      else
      {
            Debug.LogError(message);
      }
}

Call a Smart Contract Method

A Smart Contract method call will execute a code inside the Smart Contract. The data payload of the transaction delivers these calls to the blockchain so it needs to be signed by the user.

/// <summary>
/// Call a smart contract method
/// </summary>
/// <param name="scAddress">The address of the smart contract</param>
/// <param name="methodName">The method to call</param>
/// <param name="gas">The gas required to execute the called SC method</param>
/// <param name="CallStatus">Callback to get the result of the call</param>
/// <param name="args">The list of arguments, can be numeric or string</param>
public static void CallSCMethod(string scAddress, string methodName, long gas, UnityAction<OperationStatus, string> CallStatus, params object[] args)
{
      ConnectionManager.Instance.CallSCMethod(scAddress, methodName, CallStatus, args);
}

Usage example:

//Method call
ElrondUnityTools.Manager.CallSCMethod("erd1qqqqqqqqqqqqqpgqmm2m825y2t9nya0yqeg3nqlh2q50e7pd0eqq98uw2e", "add", 1500000, CallStatus, 10);

/// <summary>
/// Listener for the call response
/// </summary>
/// <param name="operationStatus">Completed, In progress or Error</param>
/// <param name="message">additional message</param>
private void CallStatus(ElrondUnityTools.OperationStatus operationStatus, string message)
{
      if (operationStatus == ElrondUnityTools.OperationStatus.Complete)
      {
            //on operation status Complete the message is the transaction hash
            txHash = message;
            Debug.Log("Tx Hash: " + txHash);
            //check the CheckTransactionStatus page for more info
            ElrondUnityTools.Manager.CheckTransactionStatus(txHash, SCTransactionListener, 1);
      }
      if (operationStatus == ElrondUnityTools.OperationStatus.Error)
      {
            //do something
      }
}


/// <summary>
/// Listener for the SC transaction status 
/// </summary>
/// <param name="operationStatus">Completed, In progress or Error</param>
/// <param name="message">additional message</param>
private void SCTransactionListener(ElrondUnityTools.OperationStatus operationStatus, string message)
{
      if (operationStatus == ElrondUnityTools.OperationStatus.Complete)
      {
            if (message == "pending")
            {
                  ElrondUnityTools.Manager.CheckTransactionStatus(txHash, SCTransactionListener, 1);
            }
            else
            {
                  if (message == "success")
                  {
                        //do something 
                  }
            }
      }
}

Check Transaction Status

Check the status of a specific transaction (whether it is processed or not by the blockchain)

/// <param name="txHash">The hash of the transaction obtained after signing</param>
/// <param name="TransactionStatus">Callback to track the result</param>
/// <param name="delay">Time to wait before querying the tx status. A tx takes some time to process so some delays are good to limit the usage of the APIs</param>
public static void CheckTransactionStatus(string txHash, UnityAction<OperationStatus, string> TransactionStatus, float delay)

Usage example:

//Method call
ElrondUnityTools.Manager.CheckTransactionStatus(txHash, BlockchainTransactionListener, 1);

private void BlockchainTransactionListener(ElrondUnityTools.OperationStatus operationStatus, string message)
{
    if (operationStatus == ElrondUnityTools.OperationStatus.Complete)
    {
        if (message == "pending")
        {
            //check again later
            ElrondUnityTools.Manager.CheckTransactionStatus(txHash, BlockchainTransactionListener, 1);
        }
        else
        {
            if (message == "success")
            {
                //refresh account
            }
         }
    }
}

Disconnect

Close the connection with the wallet

public static void Disconnect()

Usage example:

ElrondUnityTools.Manager.Disconnect();
Clone this wiki locally