Skip to content

Module 5 – Smart contract on RSK (50 minutes)

herrerameri edited this page Nov 28, 2017 · 12 revisions

It's supposed that you have a RSK node running on your computer. If you don't have it, you can read this section to install one or this section to know how to compile and run a node.

Configure Truffle to connect to RSK Node

We've talked before about Truffle configuration and truffle.js, it's time to modify this configuration file to interact with our RSK node. Open the file and change the default port to 4444 (this is the RSK node default port):

module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 4444,
      network_id: "*" // Match any network id
    }
  }
};

Go to the command line and call Truffle console by typing truffle console.

We are now connected to our RSK node, let's try something easy:

web3.eth.syncing
```
If everything is ok, you will get a response with information about how it's going the syncing of your node in the RSK Testnet, similar to:
 
```
{ startingBlock: 363327,
  currentBlock: 380418,
  highestBlock: 768930 }
```

## Check our account

So far we didn't care too much about Gas or Bitcoins or anything like that, because in TestRPC we always had a new account with a big balance. Now we are a new client in the RSK Network depending if you we are working in a local network or over the Testnet we may not have a positive balance, but let's find this out.

``` javascript
var myAccount = web3.eth.coinbase;
myAccount;
'0x28fdc38c327f4a3bbdf9501fd3a01ac7228c7af7'
```

The command `web3.eth.coinbase` gives us the address of our current account, now let's get the balance:

``` javascript
web3.eth.getBalance(myAccount);
{ [String: '0'] s: 1, e: 0, c: [ 0 ] }
```

If you don't have funds (you're in the Testnet for instance) you can get Bitcoins in two ways:

- Mining some blocks
- Getting from a faucet

Of course we could mine some blocks but it may takes some time, so let's go to the easy way.

### Using a faucet to get some Bitcoins

There's a special URL intended to give you free Bitcoins to spend in the Testnet. Copy your account address and go to:

[http://faucet.rsk.co/](http://faucet.rsk.co/)

![img](img/Faucet1.png)

Enter your address and redeem free Bitcoins:

![img](img/Faucet2.png)

After a moment you should have some free credit to start publishing contracts :)
You can see your balance to be sure you have some SBTC with any of these commands:

```javascript
web3.eth.toWei(web3.eth.getBalance(myAccount), 'ether');
web3.eth.toWei(web3.eth.getBalance(web3.eth.coinbase), 'ether');
```

## Publishing a contract

```
**¡IMPORTANT!** To see your contract in the blockchain and interact with it, you need your RSK node to be synchronized up to date. 
```

We're going to repeat the same steps we did when we deployed in TestRPC but now in a real network, at this point Truffle is connected to one of the nodes.
We can also use the [Explorer app](https://explorer.rsk.co/) to check the status of our network.

If everything looks fine, we can try publishing a simple contract.

## Checking Truffle configuration
By default the miner1 account does not have funds, we need to specify the account to be used by Truffle when deploying contracts, open the *truffle.js* and set *gas* and *from* values. For gas you just need to set the value to something meanful, like 2500000. In *from* you need to specify your account address, the easier way to get this value is using the [Explorer app](https://github.com/rsksmart/utilities/tree/master/explorer).

````javascript
module.exports = {
	networks : {
		development : {
			gas : 2500000,
			from : "0xcd2a3d9f938e13cd947ec05abc5fe734df8dd826",
			host : "localhost",
			port : 4444,
			network_id : "*" // Match any network id
		}
	}
};

Now just migrate the current contract we've been working so far:

truffle migrate

img

Congratulations, you've deployed your first contract into a RSK network!

Interacting with the contract

The most important information about the contract is the address, you can see the address in the youContractName.json within the build/contracts directory

And of course, we can interact with our contract and change it's state just like we did before.

var store = null;
StoreSomeData.deployed().then(function(instance){store = instance;});

with the instance referece just call it:

store.get();

and change the state:

store.set(123).then(console.log);
{ tx: '0x3809925e7927c4a7a135e2ba4009a66f39942e3182fc313c1bdcde5f3149005a',
  receipt:
   { transactionHash: '0x3809925e7927c4a7a135e2ba4009a66f39942e3182fc313c1bdcde5f3149005a',
     transactionIndex: 0,
     blockHash: '0xa2f204cc944ae19c30f53e2813f65b66f2af8d27d6200584dac9c1b7b446feaf',
     blockNumber: 1023,
     cumulativeGasUsed: 41605,
     gasUsed: 41605,
     contractAddress: null,
     logs: [],
     from: '0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826',
     to: '0x83c5541a6c8d2dbad642f385d8d06ca9b6c731ee',
     root: '0x7433494a8ef15f786cdd93b83d4468582f83442c5d366b0e52b372bf19b98a16' },
  logs: [] }

After some time (because a new block must be created and the confirmed) we can query again:

store.get();

We can also look at the same details using the Montor tool.

Now we know the basics on how to interact with RSK nodes using Truffle let's jump to the Smart Contracts tutorial.

The articles below give more information about Solidity and Smart Contracts: