Skip to content

All API Methods

Chain of Industry edited this page Dec 1, 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
MultiversXUnityTools.Manager.Connect(OnConnected, OnDisconnected, qrImage);

private void OnConnected(Account 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 (MultiversXUnityTools.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:

MultiversXUnityTools.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
MultiversXUnityTools.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(MultiversXUnityTools.OperationStatus operationStatus, string message)
{
    if (operationStatus == MultiversXUnityTools.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, Token token, string amount, UnityAction<OperationStatus, string> TransactionStatus)

Usage example:

//Method call
MultiversXUnityTools.Manager.SendESDTTransaction("erd1jza9qqw0l24svfmm2u8wj24gdf84hksd5xrctk0s0a36leyqptgs5whlhf",MultiversXUnityTools.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(MultiversXUnityTools.OperationStatus operationStatus, string message)
{
    if (operationStatus == MultiversXUnityTools.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
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);
      }
}

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, ulong nftNonce, int quantity, UnityAction<OperationStatus, string> TransactionStatus)

Usage example:

//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)
      {
           //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>
/// <typeparam name="T">Query response type</typeparam>
/// <param name="scAddress">The address of the smart contract</param>
/// <param name="methodName">The method to call</param>
/// <param name="QueryComplete">Callback to get the result of the query after finished</param>
/// <param name="outputType">Type of expected result</param>
/// <param name="args">The list of arguments</param>        
public static void MakeSCQuery<T>(string scAddress, string methodName, UnityAction<OperationStatus, string, T> QueryComplete, TypeValue outputType, params IBinaryType[] args) where T : IBinaryType

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 
MultiversXUnityTools.Manager.MakeSCQuery<NumericValue>("erd1qqqqqqqqqqqqqpgqmm2m825y2t9nya0yqeg3nqlh2q50e7pd0eqq98uw2e", "getSum", QueryComplete, TypeValue.BigUintTypeValue);

/// <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(OperationStatus operationStatus, string message, NumericValue data)
{
      if (operationStatus == OperationStatus.Complete)
      {
            scResultText.text = "Current sum: " + data;
      }
      else
      {
            scResultText.text = 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:</param>
/// Address
/// BooleanValue
/// BytesValue
/// EnumValue
/// MultiValue
/// NumericValue
/// OptionValue
/// StructValue
/// TokenIdentifierValue
public static void CallSCMethod(string scAddress, string methodName, long gas, UnityAction<OperationStatus, string> CallStatus, params IBinaryType[] args)

Usage example:

//Method call
MultiversXUnityTools.Manager.CallSCMethod("erd1qqqqqqqqqqqqqpgqmm2m825y2t9nya0yqeg3nqlh2q50e7pd0eqq98uw2e", "add", 1500000, CallStatus, NumericValue.BigIntValue(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(OperationStatus operationStatus, string message)
{
      if (operationStatus == OperationStatus.Complete)
      {
            txHash = message;
            scResultText.text = $"Pending TX: {txHash}";
            Manager.CheckTransactionStatus(txHash, SCTransactionListener, 1);
      }
      if (operationStatus == OperationStatus.Error)
      {
            //do something
            scResultText.text = operationStatus + " " + message;
      }
}

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
MultiversXUnityTools.Manager.CheckTransactionStatus(txHash, TransactionProcessed, 1);

/// <summary>
/// Listener for the transaction status response
/// </summary>
/// <param name="operationStatus">Completed, In progress or Error</param>
/// <param name="message">additional message</param>
private void TransactionProcessed(OperationStatus operationStatus, string message)
{
      if (operationStatus == OperationStatus.Complete)
      {
            //after a transaction is processed, refresh account balance
            Manager.RefreshAccount(RefreshDone);
      }
      else
      {
           //do something if failed
      }
}

Get Endpoint URL

Get the corresponding url based on selected API

/// <summary>
/// Returns the URL associated with Endpoint
/// </summary>
/// <param name="endpoint">endpoint defined inside settings window</param>
/// <returns></returns>
public static string GetEndpointUrl(EndpointNames endpoint)

Usage example:
For an endpoint defined in settings window like this:

Endpoint

This is the call:

string url = Manager.GetEndpointUrl(EndpointNames.GetAccounts).Replace("{address}", "erd1jza9qqw0l24svfmm2u8wj24gdf84hksd5xrctk0s0a36leyqptgs5whlhf");

url will be: https://devnet-api.elrond.com/accounts/erd1jza9qqw0l24svfmm2u8wj24gdf84hksd5xrctk0s0a36leyqptgs5whlhf


Make a GET request

/// <summary>
/// Generic API Get request
/// </summary>
/// <typeparam name="T">Return type</typeparam>
/// <param name="url">Get API url</param>
/// <param name="CompleteMethod">Complete listener (operation status, error message, return data</param>
public static void GetRequest<T>(string url, UnityAction<OperationStatus, string, T> CompleteMethod)

Here is a simple example used to get the info about an erd address.

//construct the url for the Get request
string url = "https://devnet-api.elrond.com/accounts/erd1jza9qqw0l24svfmm2u8wj24gdf84hksd5xrctk0s0a36leyqptgs5whlhf";

//make the Get request
Manager.GetRequest<AccountDto>(url, CompleteMethodGet);

//receive the result inside Complete Method
private void CompleteMethodGet(OperationStatus operationStatus, string message, AccountDto result)
{
      if (operationStatus == OperationStatus.Complete)
      {
            Debug.Log(result.Address);
      }
      else
      {
            Debug.LogError(message + " " + result);
      }
}

Make a POST request

Post requests are used to send information to the blockchain and receive a response.

/// <summary>
/// Make a POST request to MultiversX APIs
/// </summary>
/// <param name="url">Post url</param>
/// <param name="jsonData">json data to send</param>
/// <param name="CompleteMethod">Complete listener (operation status, error message, return data)</param>
public static void PostRequest<T>(string url, string jsonData, UnityAction<OperationStatus, string, T> CompleteMethod)

Here is a simple example on how to get the gas cost for a transaction before submitting it to the blockchain:

//construct the url
string url = "https://devnet-gateway.elrond.com/transaction/cost";

//construct the params as a json string
string json = "{" +
              "\"nonce\":0," +
              "\"sender\":\"erd1lgp3ezf2wfkejnu0sm5y9g4x3ad05gr8lfc0g69vvdwwj0wjv0gscv2w4s\"," +
              "\"receiver\":\"erd1jza9qqw0l24svfmm2u8wj24gdf84hksd5xrctk0s0a36leyqptgs5whlhf\"," +
              "\"value\":\"1000000000000000\"," +
              "\"gasPrice\":1000000000," +
              "\"gasLimit\":89000," +
              "\"data\":\"WW91IHNlZSB0aGlzPw==\"," +
              "\"chainId\":\"D\"," +
              "\"version\":1," + "\"signature\":\"72ddcb105778051ea2a6f92b3869e2110d50f708708a2a3fe842014c062152c8aff78dae39868d97d25831915d3c60f4acfc749dfa8bdfa395f3769d2e231a05\"" +
              "}";

//Make the Post request 
MultiversXUnityTools.Manager.PostRequest(url, json, CompleteMethod);

//receive the result inside Complete Method
private void CompleteMethod(OperationStatus operationStatus, string message, TransactionCostDataDto result)
{
      if (operationStatus == OperationStatus.Complete)
      {
            Debug.Log(result.TxGasUnits);
      }
      else
      {
            Debug.LogError(message + " " + result.ReturnMessage);
      }
}

Disconnect

Close the connection with the wallet

public static void Disconnect()

Usage example:

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