Minting Custom Tokens with CKB Studio

Jonathan Caras
NervOS CKB Israel
Published in
11 min readMay 5, 2020

CKB Studio Tutorial #3

This is Part 3 of my tutorials on getting started building on NervOS network.

If you don’t know what Nervos is check out our introduction on why we invested in Nervos.

We are using CKB Studio created by Obsidian. If you have never used CKB Studio, you will want to check out Part 1, which covers installing the IDE and Part 2 which covers sending data (“Hello World!”) to a local instance of the blockchain.

Step 1: Getting everything up and running

Fire up CKB Studio, and make sure you are up to date. We will be working with v0.4.

Make sure you start Docker.

You may need to upgrade Docker, and this may need your admin password.

After updating Docker, I was able to update my version of CKB Studio to v0.4.0.

At first glance you may not notice any differences between this version of CKB Studio and previous ones, but v0.4 introduced the powerful new User Defined Token Templates!

Step 2: Building the UDT Template

Click “Project” in the top left corner of the screen, and select “Create Project…”

In the screen that pops up you will notice a dropdown menu underneath “Template”.

Select “Simple UDT”.

What is a Simple UDT or sUDT?

Just like how Ethereum has the ERC-20 standard, the Nervos community has created a standard which allows anyone to mint their own token and create logic around that token.

This standard will evolve over time and the first version is referred to as the Simple User Defined Token. If you want to see how this standard was created you can look at the Nervos Talk Channel for the draft spec.

One of the Nervos Core Devs, Tannr, wrote a great piece on the approach to designing the standard of UDTs, which have a few fundamental differences from ERC-20s.

UDTs require users to hold CKBytes, the native tokens of Nervos. This helps prevent state bloat, and the famous tragedy of the commons in public blockchains. In addition, UDTs were standardised as part of the cell model, which treats user defined tokens as first class citizens.

If you don’t know what any of that means, it’s ok, just know that it means that Nervos is awesome and very different than the other “Ethereum Killers” you might be familiar with.

— — — —

Next up, let’s name the project. I named mine “UDT_Tutorial_1”

Click Build to compile the script we will add to the blockchain.

Now you have a whole bunch of files, this is the source code of the UDT Cell. In a future post I hope to go through what all this is, but thats for another time.

By pressing build, you added a new file, “simple_udt.o”, this is a compiled output file of the UDT script.

Tap on the file, you can see the script has been successfully compiled.

Step 3: Run the Nervos Blockchain

Before we continue, we need to run the Nervos Blockchain on our local machine.

Tap the Network button on the top right, and if you did my past tutorials, you will see the instance “HelloWorld we created last time. We are going to make a new instance so that it’s easier to manually look through blocks.

Click New Instance

I gave it the name “UDT_Tutorial_1”, and used the CKB Version 0.31

The miner is set as the user we created in the last tutorial.

Click “Create”

Now click “Start” on the new instance we created.

Check that the miner is running (I have had it not start in the past)

Step 4: Load the UDT Script to the blockchain

Hop over to the TX Constructor.

Down in the bottom left is the “Live Cells” area.

You should see a tab with the address of your miner. If not, click the green key at the very bottom left, and copy the first address listed in the keypair. Paste that into the text field above the “Live Cells” area.

Now click “Show empty cells” you may need to hit the refresh button on the left.

You should see the cells appear with block rewards of 2009.8 CKB

Click “+New Cell” and select “File”

Click File under the Data tab. Navigate to the “simple_udt.o” file you created. I used Spotlight on my mac to find mine. Click “Open”

Notice how the Capacity automatically updated based on how much of the blockchain’s state this cell will be using. This usage of CKB is state rent of a form, and is one of the most compelling elements of the Nervos Network. This is what ties the value of CKB to the value of the tokens issued on Nervos. The more tokens issued, the more CKB is locked, the lower the supply, and potentially the higher the value of the native token, thus increasing the competition of mining, and increasing the security of the network.

Press “Preview”

Now we see some of those cells we earned via mining, the ones full if CKB, have already been added to our Inputs section.

The Outputs section contains the CKB we will use to load the file to the network along with the change we will get as a UTXO.

Press “Push Transaction”

Our Transaction contains the raw data of the file we selected.

Tap the check box on our public key, and then press “Sign transaction”

This adds the signature to the witnesses section. Now press “Push Transaction”.

Step 5: Add a reference to the UDT Script

Go back to the Explorer and look for a block that has extra data. There is our UDT Script! (you may need to refresh or click “Load More” to find it)

We can take a look at the details of the block, go back to the TX Constructor, and scroll through the Live Cells, again you may need to refresh and load more. This was a little hard to find, but with enough searching its there. It will say “Simple UDT” and have a different icon.

If you double click on it, you should see the Cell Detail, which contains the raw data of the script.

Notice the option to “Add cell to reference”

Go to Finder on your Mac, and look for a file called “cells.json”. I searched for it with Spotlight. Open this up in your text editor of choice, I am using BBEdit.

Notice you have a reference to the “Simple UDT”, This is how CBK Studio knows how to name the contract you submitted.

Go back to CKB Studio and click “Add cell to reference” you should see the title change to “In reference”

Look at your cells.json file in your editor, and you will see that the reference has been created. You can close this file now.

Step 6: Mint your UDT

On the right side of the Tx Constructor you will find a tiny icon to “Mint UDT”

You can now mint as many tokens as you like. But before we do that, let’s name the token. Tap the little pencil icon next to “(Unknown)”.

I gave my token the Symbol “MFU” and the name “MyFirstUDT”. Click Update.

Everything looks good.

Now we need to create an amount of tokens to mint. I’m going to select a random number, let’s say…. 21 million.

We want to send these tokens to ourselves, so open up the address book on the lower right, and select the account of our miner.

Press “Preview”

You can search for a file called “udts.json”, there you will find the reference of the UDT you just created.

Now it looks all set, we see in the Type section under Outputs, the type of token is the hash to our UDT and the Data section is the amount of tokens.

Press “Push Transaction”

The Transaction is displayed.

As before, we select which address we will use to sign the transaction. Click “Sign Transaction”

And “Push Transaction”

Go to the Explorer and look for the new transaction we created.

Step 7: Create a new address

First we will need to add a new address. Click the key in the bottom left of the Tx Constructor.

This opens the Keypair Manager. Click create.

This creates a new address, you will need to copy the address and save it somewhere, we will need it soon.

Press “Save”

Press “Update”

I saved my new address in a text file, you can paste it where ever it will be easy to find later.

ckt1qyqzeyupdzh9j0etr7slmx2re86ggjsequwqqaq5en

Step 8: Transfer the UDT

Copy to clipboard the new address you created.

Next to the place where you clicked Mint, press the “Transfer Button”.

On the drop down menu, select the UDT you created.

Paste in the address of the recipient. This is the address you just copied.

Let’s try sending 22,000,000. This is more than the tokens we own, and in fact, more than the total tokens minted. You should get an error when you press “Preview”

Now let’s try sending 1000 MFU tokens.

Tap “Preview”.

You will see the Transaction Constructor all filled out.

Click the pencil icon next to the word data on the top line, next to the number 1000.

Notice that the Data contained in this cell will be in a format of a 128 bit unsigned integer.

Our number 1000 will be recorded in Hex as 0xe803……

Press “Confirm”

Notice the change will be sent back to you. I have done a number of transactions while making this tutorial, so my change will be different than yours.

Now press “Push Transaction” (if you get an error message that you don’t have sufficient tokens, try waiting 20 seconds and try again)

Scroll down to “outputsData” and confirm that you are sending 1000 tokens aka 0xe803…”

Check the box, sign and push the transaction.

Step 9: Verify the UDT tokens arrived at the recipient

Now go to the Explorer and add a new tab with the address of the recipient, in my case ckt1qyqzeyupdzh9j0etr7slmx2re86ggjsequwqqaq5en

You will see a cell they received with the 100 MFUs. Notice the data section matches the Hex we expected to see.

Now go to the Tx Explorer and add the recipient address to the Live Cells. Make sure “Show empty cells” is enabled

Double tap on the cell

And there you have it! 1000 MFU tokens accounted for!

Thanks for following this tutorial until the end. You can reach me at @madcapslaugh on Twitter or Telegram. Let me know if you have any trouble with this tutorial.

Big thanks to Zehao Li from Obsidian Labs, Matt and Tannr from Nervos and members of the CKB Studio chat group for helping me with this tutorial.

Now that there are custom tokens on Nervos, it is important for a decentralised exchange and stablecoins to be launched so that tokens can be traded.

We interviewed a team building a Dapp called UDTSwap, similar to Uniswap.

See you in the next tutorial.

--

--