paint-brush
Creating a Token With only 7 Lines of Code on RSK Testnet - A Step by Step Guideby@rootstock_io
504 reads
504 reads

Creating a Token With only 7 Lines of Code on RSK Testnet - A Step by Step Guide

by RootstockSeptember 4th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Using Truffle plus Open Zeppelin smart contracts, we can create a token with less than 10 lines of code. The RSK smart contract is a mintable ERC20 token. We need to create a smart contract and deploy it to the RSK testnet. The code is compiled using Node.js and NPM (Node Package Manager) The code will compile and deploy the smart contract to the testnet, and the migrations file will contain instructions to deploy the contract. To create our token, we need to import ERC-20Mintable from the library.

People Mentioned

Mention Thumbnail
Mention Thumbnail

Coin Mentioned

Mention Thumbnail
featured image - Creating a Token With only 7 Lines of Code on RSK Testnet - A Step by Step Guide
Rootstock HackerNoon profile picture

In this tutorial, I will show you step-by-step how to create a token with less than 10 lines of code, using Truffle plus Open Zeppelin smart contracts, and deploy it to the RSK testnet.

RSK is an open source platform for Ethereum compatible smart contracts based on the Bitcoin network. 

Overview

Here is a summary of the steps to be taken to build our token:

  • Initialize a project using Truffle;
  • Install Open Zeppelin smart contracts in the project folder;
  • Create a wallet mnemonic;
  • Configure Truffle to connect to RSK testnet;
  • Get some tR-BTC from a faucet;
  • Create smart contract of token;
  • Create deploy file at Truffle;
  • Deploy a smart contract on RSK Testnet using Truffle;
  • Interact with the smart contract at Truffle console.

Steps 1 to 5 are explained in detail in the tutorial link below:

Setup an RSK project using Truffle and Open Zeppelin

Requirements

  • POSIX compliant shell
  • Curl
  • Node.js and NPM (Node Package Manager)
  • Code editor: Visual Studio Code (VSCode) or any other editor of your choice
  • Truffle

All the requirements are explained in detail in the tutorial link below:

Setup an RSK project using Truffle and Open Zeppelin

Setup the project

Create a new folder named token.

mkdir token
cd token

Inside the folder token, do the steps below, following instructions from the tutorial 

Setup an RSK project using Truffle and Open Zeppelin

Creating the smart contract

Open the project in VS Code or other editor from your choice.

In the contracts folder, create a new file named Token.sol.

Token.sol with only 7 lines!

This smart contract is a mintable ERC20 token. This means that, in addition to the standard ERC20 specification, it has a function for issuing new tokens.

To learn more about it, go to EIP 20: ERC-20 Token Standard

Copy and paste the following code:

pragma solidity 0.5.2;
import '@openzeppelin/contracts/token/ERC20/ERC20Mintable.sol';
contract Token is ERC20Mintable {
       string public name = "My RSK token";
       string public symbol = "MRT";
       uint8 public decimals = 2;
}

Let’s review the above code.

To create our ERC20 Token, we will import ERC20Mintable from Open Zeppelin. This library itself imports several other libraries such as SafeMath.sol, the standards for this kind of token, and the capability to mint tokens.

Inside the token, we define some basic information about the token: name, symbol, and number of decimals for the precision.

To inherit the library’s attributes and functions, we simply define our contract as a ERC20Mintable using the is keyword in this way.

This is the final result:

Compile a smart contract

In your project folder, use the command below into the terminal:

truffle compile

It looked like this:

Deploy a smart contract

First of all, we need to create a new migrations file where Truffle will find it, containing instructions to deploy the smart contract.

Creating the token deployment file

The migrations folder has JavaScript files that help you deploy contracts to the network. These files are responsible for staging your deployment tasks, and they’re written under the assumption that your deployment needs will change over time. A history of previously run migrations is recorded on-chain through a special Migrations contract. (source: truffle: running-migrations)

In the migrations folder, create the file 2_deploy_contracts.js

Copy and paste this code.

var Token = artifacts.require("Token");
 
module.exports = function(deployer) {
  deployer.deploy(Token);
};

It will be like this:

Migrate

In your project folder, run this command:

truffle migrate

Wait a few minutes while the transactions for the smart contract deployments are sent to the blockchain.

The migrate command will compile the smart contract again if necessary.

First, it deploys the smart contract Migrations.sol, file generated by Truffle:

This is the transaction:

0xd29d03fc2b904545005ab6ed205f970575aef184ebecf14c9f0f6b6f45ec1bb3

And then it deploys our smart contract Token.sol:

This is the transaction:

0xbfff7cf431bb4af9e1b059dbd6eea935d7d20e52a770c467f38b97b479ba414a

Congratulations!

My RSK Token is now published on the RSK Testnet.

Save the contract address of token, it will be used shortly:

In the tutorial example:

tokenAddress = "0x095156af46597754926874dA15DB40e10113fb4d" 

Interact with the token using Truffle console

Go to the Truffle console, connected to RSK Tesnet.

In a new terminal, inside the token folder, run this command:

truffle console --network testnet

It takes a little longer to establish this connection when compared to the local node. This will open a new console.

Get your accounts

In the Truffle console, enter:

const accounts = await web3.eth.getAccounts()

To view each account:

accounts[0]
accounts[1]

Connect with your token

In the Truffle console, run this command:

const token = await Token.deployed()

Confirm if our instance is OK.

Enter the instance’s name: token, then ., without space hit the TAB button twice to trigger auto-complete as seen below. 

This will display the published address of the smart contract, and the transaction hash for its deployment, among other things, including all public variables and methods available.

token. [TAB] [TAB]

Check the total supply

To check if we have tokens already minted, call the totalSupply function:

(await token.totalSupply()).toString()

The returned value is 0, which is expected, since we did not perform any initial mint when we deployed the token.

Check the token balance

To check the balance of an account, call the balanceOf function. For example, to check the balance of account 0:

(await token.balanceOf(accounts[0])).toString()

The returned value is also 0, which is expected, since we did not make any initial mint when we deployed the token, and by definition no accounts can have any tokens yet.

Mint tokens

Run this command:

token.mint(accounts[0], 10000)

This command sent a transaction to mint 100 tokens for account 0.

To verify the transaction in the explorer, visit:

0x2162617b34ffcd55cf719cb998e69a33cf115c5d4d58b7ee639c1060fae81355

You can mint tokens for other accounts, for example, account 1:

token.mint(accounts[1], 10000)

You can also mint to a specific address, 0xa52515946DAABe072f446Cc014a4eaA93fb9Fd79:

token.mint("0xa52515946DAABe072f446Cc014a4eaA93fb9Fd79", 10000)

Reconfirm the token balance

Check the balance of account 0 again:

(await token.balanceOf(accounts[0])).toString()

The returned value is 10000, which is 100 with 2 decimal places of precision. This is exactly what we expected, as we issued 100 tokens

Also, you can get the balance of a specific address, for example, 0xa52515946DAABe072f446Cc014a4eaA93fb9Fd79:

(await token.balanceOf("0xa52515946DAABe072f446Cc014a4eaA93fb9Fd79")).toString()


Check the total supply (again)

Run this command to check the total supply again:

(await token.totalSupply()).toString()

The returned value is 30000, which is 300 with 2 decimal places of precision. After minting 100 tokens for 3 accounts, this is perfect!

Transfer tokens

I would like to transfer 40 tokens from account 0 to account 2. This can be done by calling the transfer function.

token.transfer(accounts[2], 4000, {from: accounts[0]})

Transaction:

0x529dbbe27e21770c21f4af34dbbbe23733af9be5c8c09b7dd4314fef743275a2

Account 2 had no tokens before the transfer, and now it should have 40. Let’s check the balance of account 2:

(await token.balanceOf(accounts[2])).toString()

Great! The balance of account 2 is correct.

Final considerations

Did you think that it would be so easy to use the Truffle framework connected to the RSK network and that it would be possible to create a token with less than 10 lines of code?

I showed you how to connect Truffle to the RSK network and deploy your own token with only 7 lines of code! Also, I hope that you saw how simple it is to use the Open Zeppelin libraries and that they work on the RSK network.

Our goal is to join forces and give options to people who believe in smart contracts based on Ethereum, and also believe in the power of Bitcoin, through RSK.

I hope this tutorial has been helpful and I’d appreciate your feedback. Share it if you like it :)

Author: Solange Gueiros - Reviewer: Brendan Graetz