-
Notifications
You must be signed in to change notification settings - Fork 0
Module 5 – Smart contract on RSK (50 minutes)
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.
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 }
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.
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:
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 Smart Bitcoins (SBTC) in two ways:
- Mining some blocks
- Getting them from a faucet
Of course we could mine some blocks but it may takes some time, so let's go for the easy way.
There's a special URL intended to give you free SBTC to spend in the Testnet. Copy your account address and go to:
Enter your address and redeem free SBTC:
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:
web3.eth.toWei(web3.eth.getBalance(myAccount), 'ether');
web3.eth.toWei(web3.eth.getBalance(web3.eth.coinbase), 'ether');
We can also use the Explorer app to check the status of the network.
**¡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.
If everything looks fine, we can try publishing. 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 meaningful, like 2500000. In from you need to specify your account address.
module.exports = {
networks : {
development : {
gas : 2500000,
from : "0xcd2a3d9f938e13cd947ec05abc5fe734df8dd826",
host : "localhost",
port : 4444,
network_id : "*" // Match any network id
}
}
};
Now just migrate the current contract:
truffle migrate
Congratulations, you've deployed your first contract into the RSK Testnet network!
In the Explorer app you can search for your address and you will see a tx with '0x0' as a destination: that is the contract.
Now you can interact with the contract in the Testnet network, using the same commands you have used in the previous module
In the next module, you can learn to make a simple DApp with the knowledge of the previous modules.