Building a Kin-powered app with Unity, Part 8

Will Gikandi
Aug 17, 2019 · 3 min read

Checking account status and requesting funding

In the previous tutorial, we built a client that successfully communicates with the server to be funded. In this tutorial, we need to understand the SDK code that:

  1. Creates a local account
  2. Checks if the local account is onboarded
  3. Sends an onboarding request to the server

Declaring the Kin objects

The Kin SDK uses a client and account object. The client class allows the account to talk to the blockchain, regardless of device (Android/iOS). The account handles most of the functions we will be using.

Using the SDK starts by declaring both client and account objects. You can see all this code by opening your Wrapper.cs file.

using Kin; //declare the namespace....private KinClient kinClient; //kin object
private KinAccount kinAccount; //kin object

Creating a local Kin account

kinClient = new KinClient(Kin.Environment.Test, "appId");
if (!kinClient.HasAccount())
{
kinAccount = kinClient.AddAccount();
}else{
kinAccount = kinClient.GetAccount(0);
}

The snippet above checks whether the client already has a local account (public/private key pair). If not, it calls the SDK to create a local account.

Checking if an account has been onboarded

kinAccount.GetStatus(GetStatusCallback)
void GetStatusCallback(KinException ex, AccountStatus status)
{
if (status == AccountStatus.Created)
{
...
}
else
{
StartCoroutine(RequestPayment(10.00M, "Inital funding", FundAccountCallback, true));
}
}

kinAccount.GetStatus above queries Kin’s blockchain to see whether the account is onboarded. Since this is an online query, it gets the response through a callback.

If the account above has not been onboarded, the client calls a function to request the server to onboard it and fund it with 10 Kin. (We have already looked at the server’s code in previous tutorials.)

Initialization steps

All off these processes are handled by the function:

InitializeKin()

The process is summarized in the flowchart below:

To make sure most of these checks are run only once, the initializeKin() function saves the steps on the client using Unity’s UserPrefs. For example, once the account is onboarded, the wrapper runs the following code:

PlayerPrefs.SetInt("UserAccountOnboarded", 1); //save this so we don't have to check next time

Make sure you can see how the InitializeKin() function works — by saving checkpoints to prevent unnecessary calls the next time the client is started up. To summarize the code, with some checks removed (such as checking for an internet connection,) the code is as follows:

void Start(){
kinClient = new KinClient(environment, appId);
kinAccount = FetchAccount();
}

Above code initializes the client and account, then fetches the account handle.

KinAccount FetchAccount(){
KinAccount ka = null;
if (!kinClient.HasAccount()){
ka = kinClient.AddAccount();
}else{
ka = kinClient.GetAccount(0);
}
return (ka);
}

Finally, InitializeKin() and the various functions it calls save “checkpoints” of the process, so that once an account is onboarded, all of these checks are skipped in the future.

void InitializeKin(){
if (PlayerPrefs.GetInt("UserAccountOnboarded", 0) == 0){
kinAccount.GetStatus(GetStatusCallback);
}else if (fetchedUserBalance == false){
kinAccount.GetBalance(GetBalanceCallback);
}else if (listenersActive == false){
AddListeners();
}
}

Conclusion

We have gone through the more difficult parts of setting up the SDK and initializing and funding an account. In the next tutorial, we will send Kin from the client.

Kin Blog

Kin is money. Earn, spend, and transfer value across an ecosystem of apps and services. Get paid for developing engaging user experiences with Kin.

Will Gikandi

Written by

Kin Blog

Kin Blog

Kin is money. Earn, spend, and transfer value across an ecosystem of apps and services. Get paid for developing engaging user experiences with Kin.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade