Breaking down the Aleo example: Token (advanced version)

Kaylej
3 min readApr 13, 2023

--

Let’s break down the operation of a more complex token model on the Aleo blockchain. Previously, we looked at how a simple token can be implemented. In this article, we will look at an example of a token where we can manage our anonymity, i.e. we will learn how we can separate public and private transfers.

Let’s start with the contract constructor:

It is important to note that unlike the simple version of the token, this time we use mapping to be able to output some information publicly. In our case it is the address-balance mapping.

Next we will define functions for minting and transferring. We will have a total of 6 functions: private and public minting, private and public transfers, then a transfer from the secret sender to the public receiver, and finally a transfer from the public sender to the secret receiver.

Let’s start with minting:

In the public mint we go straight to the finalize block and there we add the balance to the address in the mapping account. Note that the mint function will only work if the recipient account has a zero balance (no records).

In private mint, things are a little different. Since the content made in “finalize” will be visible to all users of the blockchain, we are limited to only the transition (off-chain part of the contract):

Public transfer: the same as in public minting, but don’t forget to decrement the sender’s balance. The function will not work if the sender does not have enough tokens for the transfer.

In the private transfer, we also stick to the transition block. With the first operation we calculate the balance of the sender after the transfer (and a proof cannot be generated for this function if the difference goes over unsigned int (here it is u64), i.e. if it tries to become negative).

This is followed by a sender balance update and then a recipient balance update:

Transfer from the secret sender to the public recipient: Here we see much the same as in the previous function. We decrement the sender’s balance in transition, but add the recipient’s balance in finalize by applying increment to the account mapping:

Transferring from public to secret is the same thing, but in reverse:

Original blog post: link

--

--