Unity SDK TezosAPI object
The Unity SDK class Tezos.API.TezosAPI
provides methods for many Tezos-related tasks, including connecting to wallets, sending transactions to Tezos, and getting information about about the Tezos blockchain, such as what tokens accounts or contracts control.
Properties
None.
Initialization methods
WaitUntilSDKInitialized()
Waits until the SDK is fully initialized. Use this method at startup before trying to connect to wallets or use other features of the SDK.
public static async UniTask WaitUntilSDKInitialized()
Wallet connection methods
ConnectWallet()
Sends a request to a user's wallet to connect a Beacon or WalletConnect wallet to the application.
To connect social wallets, use SocialLogIn()
.
public static async UniTask<WalletProviderData> ConnectWallet(WalletProviderData walletProviderData);
If a wallet is already connected, this method either throws an exception (if a social wallet is connected) or returns the current connection information (if a Beacon or WalletConnect wallet is connected).
This method triggers the WalletConnected
or WalletConnectionFailed
events, depending on whether the connection was successful or not.
When the WalletType
field of the WalletProviderData
parameter is set to WalletType.BEACON
, this method automatically picks the correct way to connect to wallets:
- In WebGL applications, it uses the
TezosSDK.Beacon.BeaconConnectorWebGl
class to trigger the browser to connect to a wallet app in a browser plugin. - In all other applications, it uses the
TezosSDK.Beacon.BeaconConnectorDotNet
class to generate a QR code to connect to a wallet app on a mobile device or use a "deep link" to connect to a wallet on the same mobile device that is running the application.
When the WalletType
field of the WalletProviderData
parameter is set to WalletType.WALLETCONNECT
, this method opens the WalletConnect SDK's popup window, which provides deep links and a QR code to connect EVM wallets.
For more information about connecting to wallets, see Connecting accounts.
SocialLogIn()
Initiates a social login session and returns information about the connection.
public static async UniTask<SocialProviderData> SocialLogIn(SocialProviderData socialProviderData);
This method triggers the SocialLoggedIn
event.
Disconnect()
Disconnects the currently connected wallet and returns true if a wallet was connected or false if no wallet was connected.
public static async UniTask<bool> Disconnect()
This method triggers the WalletDisconnected
or SocialLoggedOut
event, depending on the type of wallet connection.
Wallet information methods
IsConnected()
Returns true if any kind of wallet is connected to the application and false if not.
public static bool IsConnected()
This method returns true if a Beacon, WalletConnect, or social wallet is connected.
To check for Beacon and WalletConnect connections specifically, use IsWalletConnected()
.
To check for social wallets specifically, use IsSocialLoggedIn()
.
GetConnectionAddress()
Returns the connected address or an empty string if no wallet is connected.
public static string GetConnectionAddress()
IsWalletConnected()
Returns true if a Beacon or WalletConnect wallet is connected.
public static bool IsWalletConnected()
IsSocialLoggedIn()
Returns true if a social wallet is connected.
public static bool IsSocialLoggedIn()
GetWalletConnectionData()
Retrieves information about the current wallet connection.
public static WalletProviderData GetWalletConnectionData()
GetSocialLoginData()
Retrieves information about the current social wallet connection.
public static SocialProviderData GetSocialLoginData();
GetWalletProvider()
Returns the internal object that the SDK uses to represent the connection to Beacon and WalletConnect wallets.
public static IWalletProvider GetWalletProvider<T>()
To use this method you must specify the type of wallet provider that the Unity application is using.
Example for WebGL applications:
BeaconWebGLProvider walletProvider = TezosAPI.GetWalletProvider<BeaconWebGLProvider>();
Debug.Log(walletProvider.WalletType);
Example for mobile applications:
BeaconMobileProvider walletProvider = TezosAPI.GetWalletProvider<BeaconMobileProvider>();
Debug.Log(walletProvider.WalletType);
GetSocialProvider()
Returns the internal object that the SDK uses to represent the connection to social wallets.
public static ISocialLoginProvider GetSocialProvider<T>()
Example:
KukaiMobileProvider walletProvider = TezosAPI.GetSocialProvider<KukaiMobileProvider>();
Debug.Log(walletProvider.WalletType);
Tezos information methods
GetBalance()
Fetches the balance of the connected account in mutez, as a string.
public static async UniTask<string> GetBalance()
Example:
public void RunGetBalance()
{
try
{
var balance = ulong.Parse(await TezosAPI.GetBalance());
float convertedBalance = balance / 1000000f;
Debug.Log($"Balance: {balance} tez");
}
catch (Exception e)
{
Debug.LogError($"Balance fetch error: {e.Message}");
}
}
ReadView()
Returns the response from a contract view.
public static UniTask<T> ReadView<T>(string contractAddress, string entrypoint, string input)
Note that the input
parameter must be a Michelson-encoded object, as in the following example, which passes a string parameter to the view:
Example:
var result = await TezosAPI.ReadView<string>("KT1K46vZTMEe8bnacFvFQfgHtNDKniEauRMJ", "simple", "\"String value\"");
Debug.Log("View response: " + result);
GetTokens()
Returns the tokens for a given contract or account address as a list of TokenData
objects.
public static UniTask<T> GetTokens<T>(
string address,
int limit = 100
)
This example gets information about the tokens in a contract:
var tokenList = await TezosAPI.GetTokens<List<TokenData>>("KT1HP6uMwf829cDgwynZJ4rDvjLCZmfYjja1", 5);
foreach (TokenData token in tokenList)
{
Debug.Log($"Token ID: {token.TokenId} has {token.HoldersCount} owners");
}
This example gets the tokens that a user account holds:
var tokenList = await TezosAPI.GetTokens<List<TokenData>>("tz1QCVQinE8iVj1H2fckqx6oiM85CNJSK9Sx", 5);
foreach (TokenData token in tokenList)
{
Debug.Log($"Contract: {token.Contract.Address} ID: {token.TokenId}");
}
GetTokenMetadata()
Gets the metadata for the specified token.
public static UniTask<JsonElement> GetTokenMetadata(
string contractAddress,
uint tokenId
);
Transaction methods
RequestOperation()
Sends a Tezos transaction and returns an object with the hash of the transaction.
public static async UniTask<OperationResponse> RequestOperation(OperationRequest operationRequest)
This method triggers the OperationResulted
event.
For examples, see Calling contracts.
GetOperationStatus()
public static UniTask<bool> GetOperationStatus(string operationHash)
Returns true if the specified operation was successful, false if it failed, or null (or HTTP 204) if it doesn't exist.
RequestSignPayload()
Prompts the connected wallet to sign a payload and returns the signed payload.
public static async UniTask<SignPayloadResponse> RequestSignPayload(SignPayloadRequest operationRequest)
This method triggers the SigningResulted
event.
Example:
private async void Start()
{
TezosAPI.SigningResulted += SigningResulted;
await TezosAPI.WaitUntilSDKInitialized();
}
public async void SignPayloadClick()
{
try
{
var payload = "Hello World!";
var bytes = Encoding.UTF8.GetBytes(payload);
var hexPayload = BitConverter.ToString(bytes);
hexPayload = hexPayload.Replace("-", "");
hexPayload = "05" + hexPayload;
var result = await TezosAPI.RequestSignPayload(
new SignPayloadRequest
{
Payload = hexPayload,
SigningType = SignPayloadType.MICHELINE
}
);
Debug.Log($"Signature: {result.Signature}");
}
catch (Exception e)
{
Debug.Log($"{e.Message}");
Debug.Log($"{e.StackTrace}");
}
}
public void SigningResulted(SignPayloadResponse response)
{
Debug.Log("SigningResulted");
Debug.Log(response);
}