Skip to content

Truffle box configured to create an ERC20 token using Open Zeppelin smart contracts library with RSK Networks.

License

Notifications You must be signed in to change notification settings

solangegueiros/rsk-token-box

Repository files navigation

RSK Truffle Token Box

Truffle box with everything you need to start creating a token using Open Zeppelin smart contracts library in Truffle framework, connected to a RSK network. It includes network configurations for local node (regtest), Testnet and Mainnet.

Requirements

  1. NPM (Node Package Manager) and Node.js are needed, though both are usually installed at once.

Go to Node.js if you need to install it.

  1. Truffle

Install Truffle globally:

npm install -g truffle

Installation

  1. Create a new folder. For example, create the folder rsk-token. Navigate to the folder in the terminal.
mkdir rsk-token
cd rsk-token
  1. Run the unbox command. This also takes care of installing the necessary dependencies and it can take some time.
truffle unbox rsksmart/rsk-token-box

This is the result using Windows OS:

truffle unbox

Development console

Truffle has an interactive console that also spawns a development blockchain. This is very useful for compiling, deploying and testing locally.

  1. Run the development console. This command is successful if you see a list of 10 accounts, a mnemonic and the command prompt is now truffle(develop)>
truffle develop

You will now be in the truffle develop console with seeded accounts and their associated private keys listed.

C:\RSK\rsk-next>truffle develop

Truffle Develop started at http://127.0.0.1:8545/

Accounts:
(0) 0x1056f747cf4bc7710e178b2aeed4eb8c8506c728
(1) 0x45a71c00382c2898b5d6fae69a6f7bfe6edab80c
(2) 0x1596384706dc9ac4cca7f50279a4abe591d6c3fe
(3) 0x9576d0a496b645baa64f22aceb2328e7468d4113
(4) 0xd431572eef7d77584d944c1809398a155e89f830
(5) 0x92c111839718fe0800fadccc67068b40b8524a0f
(6) 0x6da22b5a027146619bfe6704957f7f36ff029c48
(7) 0x2c3a82d8c3993f8c80dcaf91025437bd057df867
(8) 0xc43ae7a44f7deb759177b7093f06512a0a9ff5d7
(9) 0xe61bf00cd7dce248449cfe58f23a4ef7d542bc0b

Private Keys:
(0) f32f32839fe27ad906b63eafb326f26fed95c231e3c5e33c7cdd08f62db63167
(1) ebef990088f27f6ef13b5e52a77d5dcc5a76862a701908c586d01b6fe93562b3
(2) 598ccae5e4436fedeb0e798c0d254789c55a63401ebfc3ae8ddde29634ddfcde
(3) 09934b80f391e0024b8cb00cd73790fdf64c4d0509e144766414fee317cd3f4e
(4) ac745b84b6574b5738d364b43e0d471c9d5107504acc709c90f6f091b78c751b
(5) 449654cde095f2349113ef12a93e139b4302bc95adb3619d08adf53dde9b8847
(6) c217f12a89c352fc70b5f1bd5742314b4fb1bb1e35cb779fdb3c2390106355db
(7) 1d4c74dfa4e99e161130c18cc63938bb120a128cefbf1b9188efc678bf5722cb
(8) 0f44e0becf2e090db498a1b747d2a758fcc81fb0241f350d61117a9c6b1fa82e
(9) 85218c5eec657470dafeb09e6f7101f91d21bfe822fbeeecfc9275f798662a63

Mnemonic: virtual valve razor retreat either turn possible student grief engage attract fiber

⚠️  Important ⚠️  : This mnemonic was created for you by Truffle. It is not secure.
Ensure you do not use it on production blockchains, or else you risk losing funds.

truffle(develop)>

Token.sol

Take a look at the smart contract Token.sol. You can check it out in folder contracts.

Token.sol

Token.sol has only 7 code 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 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.

  1. Compile and migrate the smart contract. Note inside the development console we don't preface commands with truffle.

To make sure you're in the development console, the command prompt must be truffle(develop)>

compile

The compile output should be similar to:

truffle compile

migrate

And the migrate output should be similar to:

truffle migrate

  1. Running contract tests.

This Truffle box also comes with the file TestToken.js which include some examples for testing the smart contract. You can check it out in the test folder.

There are many other tests which can be done to check an ERC20 token.

Run this command in the development console:

test

This test output should be similar to:

truffle test

Note the command varies slightly if you're in or outside of the development console.

// inside the development console.
test

// outside the development console.
truffle test

Interact with the token using Truffle console

  1. Get your accounts in Truffle console.

In the Truffle console, enter:

const accounts = await web3.eth.getAccounts()

Don’t worry about the undefined return, it is ok. See the addresses after it by entering the command below:

accounts

And to view each account:

accounts[0]
accounts[1]

Take a look in the results:

accounts

  1. Interact with the token using Truffle console.

First of all, connect with your token

const token = await Token.deployed()
  1. 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]

token tab tab

  1. 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.

  1. 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()

Take a look in the results of total supply and balanceOf:

total supply and balanceOf 0

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.

  1. Mint tokens

Run this command:

token.mint(accounts[0], 10000)

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

token.mint account 0

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

token.mint("0xa52515946DAABe072f446Cc014a4eaA93fb9Fd79", 10000)

token.mint address

  1. 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()

Take a look in the results:

balanceOf account 0 and address with 10000

  1. Check the total supply (again)

Check the total supply again:

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

totalSupply 20000

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

  1. Transfer tokens

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]})

token.transfer

Account 2 had no tokens before the transfer, and now it should have 40. Account 1 must have 60 tokens. Also the total supply will be the same.

Let’s check the balance of each account and the total supply:

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

Take a look in the results:

balances after transfer

Great! The balances of both accounts and the total supply are correct.

Exit Truffle console

In the Truffle console, enter this command to exit the terminal:

.exit

Using RSK networks

This Truffle box is already configured to connect to three RSK networks: regtest (local node), testnet and mainnet. We need only to do some items:

  • Install and run a local node
  • Setup an account & get R-BTC
  • RSK network gas price
  • Your wallet mnemonic
  • Choose the network in the app

RSK regtest (Local node)

To install and run a local node, follow these instructions.

Setup an account & get R-BTC

  • Get an address and learn how works the account based RSK addresses.
  • Paste the wallet mnemonic in the file .secret, located in the folder project, and save it.
  • For the RSK Testnet, get tR-BTC from our faucet.
  • For the RSK Mainnet, get R-BTC from an exchange.

Take a look truffle-config.js file to realize that we are using HDWalletProvider with RSK Networks derivations path:

  • RSK Testnet dpath: m/44’/37310’/0’/0
  • RSK Mainnet dpath: m/44’/137’/0’/0

For more information check RSKIP57.

Setup the gas price

Gas is the internal pricing for running a transaction or contract. When you send tokens, interact with a contract, send R-BTC, or do anything else on the blockchain, you must pay for that computation. That payment is calculated as gas. In RSK, this is paid in R-BTC. The minimumGasPrice is written in the block header by miners and establishes the minimum gas price that a transaction should have in order to be included in that block.

To update the minimumGasPrice in our project run this query using cURL:

Testnet

curl https://public-node.testnet.rsk.co/ -X POST -H "Content-Type: application/json" \
    --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest",false],"id":1}' \
    > .minimum-gas-price-testnet.json

Mainnet

curl https://public-node.rsk.co/ -X POST -H "Content-Type: application/json" \
    --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest",false],"id":1}' \
    > .minimum-gas-price-mainnet.json

This query saved the details of latest block to file .minimum-gas-price-testnet.json or .minimum-gas-price-mainnet.json, respectively.

In the truffle-config.js, we are reading the parameter minimumGasPrice in each json file.

For more information about the Gas and minimumGasPrice please go to the gas page.

Connect to RSK regtest (local node)

First ensure you have a local node running.

truffle console

Any network defined with the name development is considered the default network.

Connect to RSK Testnet or Mainnet

Run the development console for any RSK network.

# Console for Testnet
truffle console --network testnet

# Console for Mainnet
truffle console --network mainnet

Test the connection to RSK network

On any of the networks, run this commands in the Truffle console:

Block number

Shows the last block number.

(await web3.eth.getBlockNumber()).toString()

Network ID

To get the network ID, run this command:

(await web3.eth.net.getId()).toString()

For the local node, the network ID is 33.

getId local

List of network IDs:

  • mainnet: 30
  • testnet: 31
  • regtest (local node): 33

Compile and migrate the smart contracts.

We will do it running the below commands directly in the terminal, without using the truffle console, this is to show you an alternative.

On any of the networks, run this commands in a terminal (not in Truffle console):

truffle migrate

Where to go from here

Interact with the token published on an RSK network using Truffle console, doing the same steps which was done before:

  • Get your accounts
  • Connect with your token
  • Check the total supply or the token balance
  • Mint tokens
  • Transfer tokens

At this point, we have installed all requirements and created a token using Truffle framework and Open Zeppelin smart contracts library, connected to an RSK local node (Regtest), the RSK Testnet and the RSK Mainnet.

  • Find more documentation

Check out the RSK developers portal.

  • Do you have questions?

Ask in the RSK chat.

About

Truffle box configured to create an ERC20 token using Open Zeppelin smart contracts library with RSK Networks.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published