How to deploy a Smart Contract in RSK

This tutorial aims to help you install the required environment, code your first smart contract and deploy it to RSK network.


If you find something unclear, or have ideas on how to improve this or new tutorials? Found this useful? Want to help building more tutorials? Have ideas?, feel free to contact me!

Environment setup

This is very hard to standardize, there are several tools available, and more are coming every day, lot of compatibility issues, versions, different OSs, so, I will try to give (IMHO) the most easy to install and compatible environment I can.

Linux MacOS Windows
1. Visual Studio Code Debian/Ubuntu

Red Hat/Fedora/SUSE

macOS 10.9+ Win 7, 8, 10
2. VS Solidity Ext.
3. Truffle
4. Jaxx wallet



RSK has two networks, one is a development or test network (also known as testnet) and a production ready network (the mainnet).

The software can also run on your own node, you can find how to deploy a node and connect it to testnet or mainnet.

In order to interact and see the network status, here you can find the links:

Tool \ Network Testnet Mainnet
Network status
Network explorer
Faucet N/A

The Testnet has a faucet that provides SBTC to run your Smart Contracts within the sandbox for free, the Mainnet does not, that would be like giving money for free in the real world Smile.

You will need to install all the required software specified in the previous section.

I present you your new IDE, Visual Studio Code, this is the initial screen:

demo 1


New project

Let’s start coding! Open Visual Studio Code. You can see the interface in the screenshot.

The first task is setting the environment folder. Its just a new empty folder that we are gonna use to host all our project files.

Click on “File” –> “Open folder…” and choose a convenient location.

As we are going to use the Command Line Interface that is not showed by default. Click in “View” –> “Integrated Terminal” to open it.

demo 2

The folder should be empty in order to start fresh.

To start coding, truffle help us by creating a template project with some magic on it.

On the command interface type:

truffle init

demo 3

This creates some folders and files as a template, we will focus our attention in.

  • “contracts” folder: where our contracts must be (*.sol files).
  • “migrations” folder: javascript migration code, this is executed on the deploy.
  • “truffle.js” file: truffle configuration file, we are going to configure our network to deploy the contract.

You should see the file tree as follows:

demo 4

Lets create our first Smart Contract just creating a new file called “HelloWorld.sol” inside the “contracts” folder.

demo 5

Open the file and paste the following code as in the image bellow:

pragma solidity ^0.4.4;

contract HelloWorld {
string greeting;
function HelloWorld() public {
greeting = “Hello smart world!”;

function getGreeting () public constant returns (string) {
return greeting;

function setGreeting(string _newGreeting) public returns (bool success) {
greeting = _newGreeting;
return true;

demo 6

Its a simple Smart Contract with only one variable that can be asked or replaced using the functions “getGreeting()” and “setGreeting(“some message”)” respectively.



Now we have our code done, let’s prepare the project to compile and deploy it.

Open the “1_initial_migration.js” file in the “migrations” folder, and add lines 2 and 6. The file should end like this:

var Migrations = artifacts.require(“./Migrations.sol”);

var HelloWorld = artifacts.require(“./HelloWorld.sol”);

module.exports = function(deployer) {




demo 7

This tells truffle how to deploy our code.

Next step is to compile the Smart Contracts running this command in the terminal:

truffle compile

demo 8

This operation compiles the Smart Contracts and creates one JSON file for each one. Open and see what’s inside our compiled Smart Contract (“HelloWorld.json” inside the new created “build/contracts” folder). Do you find some things interesting?

demo 9


In-memory blockchain

In order to avoid gas usage and long waits during our development process, truffle implements an in-memory blockchain that allow us to deploy and interact with our smart contracts.

To start the in-memory blockchain and connect the terminal to it, run this command:

truffle deploy

demo 10

This command returns lot of information. Some to highlight are the address:port where the blockchain is running ( in my case, this may vary, but don’t panic).

We can see also 10 account addresses with the corresponding Private Keys and the famous 12 words; Its convenient to write this down for future usage. Don’t send any money to this accounts, they are living just in your in-memory blockchain.



Firstly, we are going to deploy our compiled Smart Contracts to our in-memory blockchain.

If you started this tutorial from this step, I recommend to go back one and open the in-memory blockchain console.

Once in the truffle develop console, just type and execute this command to deploy the contract:


demo 11

If you don’t see a result similar to the one in the screenshot, force the deploy using this command:

migrate –reset

The command result gives:

  • Transaction Id
  • Contract address (important to call the contract)

That is all! You have your Smart Contract deployed!


Test your smart contract

With the Smart Contract deployed, now we need to test or interact with it. Truffle execute the migration file “1_initial_migration.js” mentioned before. It’s just javascript, so, we can see line by line what is running. In the second line we create a global variable called HelloWorld that contains the Smart Contract, type in the console and see the result:


Now, we are going to interact with it, type line by line the following commands as showed in the image below:

var helloWorldContract

HelloWorld.deployed().then(function(contract){ helloWorldContract = contract; })

This three lines creates a variable “helloWorldContract”, then reference the Smart Contract to this variable, and finally we use the variable to call the “getGreeting” function in the contract.

demo 12

The next step is writing/modify the greeting by calling the “setGreeting” function of the Smart Contract, typing and running the command:

helloWorldContract.setGreeting(“RSK loves Tokyo!”).then(console.log)

The result is quite different from the other calls, and the time between the call and answer may vary if you are running local or connected to a remote network. Because we are setting a variable, we need to run the Smart Contract through a transaction to the blockchain. The command result is the transaction receipt. You can see the blockNumber, transactionHash and gasUsed among other info.

If you are running this on a remote network (testnet or mainnet), you will find this transaction details in the explorer (see tools section).

demo 13

Now, the last step is calling the “getGreeting” function again to check our new greeting message!

demo 14

As you know, the Smart Contracts deployed to remote networks are executed by the real nodes participating in the network. It’s normal that the transaction have some delay in the execution, and maybe you will see the same first greeting message, give the network some time and run the command again.


Remote deploy

The last step is deploying our Smart Contract to a remote network. To do so, you need a node to interact with the network, refer yourself to the tools section in this tutorial to see how to install a node, or ask me the address if you are following this tutorial during a workshop or meetup. (join the RSK Ambassador Program)

Truffle needs to know about the network, so, let’s configure the network as showed below:

module.exports = {

// See <>

// to customize your Truffle configuration!

networks: {

aleRSK: {

gas : 2500000,

gasPrice : 1,

from : “0xsomeaddress”,

host: “some host”,

port: 4444,

network_id: “*” // Match any network id




Copy this code to the “truffle.js” configuration file.

demo 15

After that, run the migrate command as we learnt in the previous sections.

truffle migrate –reset –network aleRSK

Replace “aleRSK” with the name you want. Be sure its the same configured before in the “truffle.js” file.

Open the console connected to the remote network:

truffle console –network aleRSK

After that, test it using the same steps as in the “testing your smart contract” section.

demo 16

If you are here you’ve showed passion about the technology. Join our Global Community Slack and join the RSK Ambassador Program!

[Facebook] [Google] [LinkedIn] [Twitter] [Windows Live] [Email]
Tagged with: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *