Building a Kin-powered app with Unity, Part 9

Will Gikandi
Kin Blog
Published in
3 min readAug 17, 2019

--

Sending Kin

In the previous tutorial, we looked “under the hood” of the wrapper to understand how accounts are created and funded by sending a request to the server. In this tutorial, we will finally get to send some Kin!

Create a UI for your app

Step 1: Add the code to handle the UI

We have understood how the code works in the server and client, so now we can create some UI controls to use it.

Open up your project and in the tutorial file, make the following additions:

public Text textBalance;public void SendKin(){
//send Kin to server
string memo = "tutorial Earn";
kinWrapper.SendKin(5m, memo);
}
public void RequestKin(){
//request Kin from server
string memo = "tutorial Earn";
kinWrapper.EarnKin(10m, memo);
}

Once we have the wrapper working, Earning and Spending Kin are as simple as the functions above. You can use them anywhere in your app.

To make a peer-to-peer payment — send Kin to an address that is not on your server — you can just use:

kinWrapper.SendKin(amount,memo,address);

We also need to keep the balance updated. Add the following changes (noted in bold):

void ListenKin(object eventData, string type){
if(type == "balance"){
textBalance.text = kinWrapper.Balance().ToString();
}
GameObject.Find("TutorialLog").GetComponent<Text>().text += "\n" + eventData.ToString();
}

Step 2: Add and connect the UI to the code

Add a button for Earn and Spend and a Text control for the listener.

Hierarchy window
Scene View

Drag the your balance control to your canvas’ Tutorial script.

Drag the canvas object into your button controls and connect each button to its function.

Sending/Spending Kin
Earning/Requesting Kin from server

Done! Compile and Test

You can now compile and run it on your phone. Click “Send Kin” and allow a few seconds for the balance to change. Then click “Earn Kin” to request Kin to be sent from your server.

How do the listeners work (inside the wrapper)?

Once initialized, Kin’s blockchain sends several events to our client. The SDK lets you listen for payments, account creations, and balance changes. Listeners can be added easily by adding the interfaces in addition to Monobehaviour.

public class KinWrapper : MonoBehaviour, IPaymentListener, IBalanceListener

You instantiate listeners in the wrapper as follows:

kinAccount.AddPaymentListener(this);
public void OnEvent(decimal balance){

}

Once the listener receives a balance change, it can save the balance to a PlayerPref and notify your code to update the user’s balance in the UI.

Conclusion

In this tutorial, we tied everything together to be able to Send and Request Kin to and from the server. Now, we can use the following simple functions to control Kin from anywhere within our app!!

Now that we understand how the mechanisms in the backend work, we have made working with the Kin blockchain into three functions: SendKin(), RequestKin(), and ListenKin().

In the final tutorial, we will wrap up and cover whitelisting transactions (how to send transactions with zero fees.)

--

--