Complete Android BApp Example using Fee Delegation

TL;DR, if you want to skip to code directly, here are links :link:

A while ago, I presented an Android BApp example which uses Klaytn Fee Delegation. Although the app couldn’t be simpler and had no special things, it actually attracted many people’s attention since it was the first Android example and used fee delegation (it was also the first hands-on experience for many people).

Due to high demand, I’m sharing the example online :clap: :tada:

The Android app is called Count Android or, simply, Count, and all it does is count the number stored in a contract. Here’s the contract I used for the example:

pragma solidity ^0.5.6;

contract Count {
   uint public count = 0;
   address public lastParticipant;

   function plus() public {
      lastParticipant = msg.sender;

   function minus() public {
      lastParticipant = msg.sender;

This contract is pre-deployed on Baobab at 0xb4bF60383C64D47F2E667f2fE8F7ED0c9380f770, and Count changes this contract’s storage variable count via functions plus and minus. You can check the contract transaction history from here.

Count is available on Github, and you can fork/clone/download it freely. Once you have the code on your computer, you can run the app on the emulator comes with Android Studio. If you are new to Android, follow the instruction to setup your Android emulator.

Once you open the app, enter your private key and hit START. FYI, since we are going to change the contract value deployed on Baobab, your private key should tie to an account on Baobab as well.

This is the screen you’ll see when you provide a valid private key. From the top, the app shows

  1. Address derived from the submitted private key
  2. KLAY balance of the account (of the derived address)
  3. Payer configuration
  4. Current Count
  5. Buttons you can click to invoke plus or minus

You can run plus or minus function by clicking the button, so the number in the middle changes as you click buttons. Assuming Use Payer is disabled, the app does the followings when you click a button:

  1. Encodes a transaction corresponding to the button you clicked
  2. Signs the transaction
  3. Sends the signed transaction to a Baobab endpoint
  4. Waits for the receipt
  5. Displays the result

Because including a transaction in a block costs transaction fees :moneybag: know as gas. Clicking a button won’t successfully increase or decrease the count if your balance is zero because your transaction won’t be accepted by Klaytn nodes. This is where fee delegation is useful.

Take a look at the following image:

To use fee delegation, enable Use Payer by clicking the checkbox and providing the payer URL. This example assumes you have downloaded Payer Express and run on localhost (hence, pointing to – the reserved IP address for Android Emulators points to host machine). With a Payer configured with an account holding sufficient KLAY balance, changing count won’t charge you gas fee :money_with_wings:

As mentioned above, source code for both Count Android and Payer Express are publicly available on Github. Try them and ask any questions via comments down below :wink:

1 Like