How to use Remix and Metamask with RSK Network
Ever Wondered on how to configure Metamask to connect to RSK network?
RSK is the most secure contract platform in the world. RSK’s Contracts goal is to add value and functionality to the bitcoin contracts ecosystem by enabling smart contracts, near instant contracts payments, and higher scalability. In this tutorial I will show you step-by-step how to use Remix and Metamask, which are tools that were originally built for Ethereum, to create and deploy a simple smart contract on RSK’s Testnet
Overview
The steps we will cover in this article:
• Configure Metamask to connect to RSK testnet.
• Get some RBTCs testnet at faucet.
• Connect Remix with RSK Testnet.
• Create a smart contract at Remix.
• Compile it.
• Using Remix, deploy the smart contract at RSK Testnet.
• Know the RSK explorer.
• Interact with the smart contract.
• Check the transactions in MetaMask.
Requirements
• MetaMask.
• Remix
MetaMask
MetaMask is an extension for accessing Ethereum enabled distributed applications, or "Dapps" in your browser!
The extension injects the Ethereum web3 API into every website's javascript context, so that dapps can read from the blockchain.
Metamask is a kind of web wallet which facilitates transactions using yours accounts. It can be used with RSK networks too. It has versions for several browsers, like Chrome, Firefox, Opera and Brave.
• Go to metamask.io and install it.
• Create an account.
Write down your mnemonic, seed phrase, or backup phrase (all these terms mean the same), with 12 words. This is used to recover your account, in case you miss your password. The seed phrase is the most important thing in a wallet / account.
Remix
Remix is an online web tool. It is an IDE (Integrated Development Environment) used to write, compile, deploy and debug Solidity code. Can be connected with Metamask and used to deploy smart contracts to both the RSK Testnet and Mainnet.
Can be accessed at remix.ethereum.org
Connect MetaMask to RSK testnet
• Go to networks on MetaMask
• Select “Custom RPC"
• Then fill the gaps with the below details:
• Network Name
RSK Testnet
• New RPC URL
• ChainID (optional)
31
• Symbol (optional)
tRBTC
• Block Explorer URL (optional)
After Filling the form, then Click On Save
TestNet Faucet
You can get some Testnet RBTC at faucet.testnet.rsk.co.
Copy your address from Metamask
Enter your wallet address, pass the CAPTCHA and “Get test RBTC” at faucet.testnet.rsk.co.
Wait a few seconds…
Wait for the RBTCs token in your MetaMask
You can see the transaction hash, for example 0x105cb5cb19ba40384a8f2985816DA7883b076969cA7323a88a3531f762a41.
Now I have 0.01 RBTC because I sent the request twice!
Remix
Go to
In the home / welcome page, choose environment Solidity.
Remix Connect RSK Testnet
With the RSK network selected at Metamask
At Remix, on the left side, locate the button Deploy and run transactions. For now it is the 4th button
At Environment, choose Injected Web3
Injected Web3 connects Remix with active account in Metamask
ChainID 31 was defined at RSK Testnet custom network in MetaMask.
Create a smart contract
• Click on the second button on the left side - file explorer
• Click on the “file icon” and create a new file
• Name the new file anything. PS: In my case I named it “Storage.sol”
• And copy and paste the following code
pragma solidity >=0.7.0 <0.9.0;
contract Storage {
uint256 number;
function store(uint256 num) public {
number = num;
}
function retrieve() public view returns (uint256){
return number;
}
}
Now your Remix IDE looks like this….
Storage.sol
This smart contract has:
• A variable number to store a number
• A function store() to return the number stored at variable number
• A function retrieve() to change the number stored at variable number
Compile a smart contract
• In the 3rd button at left side click on Solidity compiler
• It is useful to enable auto-compile.
For now click on the button “Compile 1_Storage.sol”
Check the green sign at 3rd button with the message compilation successful
Deploy a smart contract at RSK testnet
In the left side panel, go to the button Deploy and run transactions. Actually, it is the 4th button
Currently we have only one smart contract, so it is automatically selected in the dropdown.
Click Deploy button.
It will open a MetaMask popup window, to confirm the transaction to create the smart contract Storage.sol
Click on confirm
At bottom right, we can check the message: creation of Storage pending....
Once it is confirmed, we can check it
Click on the transaction line or the debug button (at the right side) to see more details of the transaction:
In this example, the transaction hash is:0xb655d2cd0bc58e6e301c82bfd5d3f3d452340127161cc577f9b0cf6421e95c1e
RSK Explorer
The RSK explorer is the blockchain explorer for RSK transactions. We will use the Testnet explorer: explorer.testnet.rsk.co
Past the transaction hash copied from Remix IDE to the search field, at the top of the screen
This is the result:
You can verify my example at: 0xb655d2cd0bc58e6e301c82bfd5d3f3d452340127161cc577f9b0cf6421e95c1e
Interact with the smart contract
When a smart contract is deployed with Remix, we can see it in the left panel under deploy and run transactions:
These are the same functions we created in our smart contract!
The orange buttons are functions which will change some information stored on the blockchain, we call it state changes. This kind of function expends gas when used.
The blue buttons are functions which are read-only and it does not change anything stored on the blockchain. We do not need to expend gas when using them.
Retrieve
First of all, we will check the value stored at deploy.
Click on the retrieve button
We do not have any value stored, because we do not define anything at the moment when we deployed.
At bottom right, we can check that it was a call to Storage.retrieve() function:
Store
Put a value in the field at the right side of the store button, and click on the button.
In my case I inserted 2000 as the value
It will open a MetaMask popup window, to confirm the transaction to store a value
Click on confirm
At bottom right, we can verify that the transaction is pending, waiting for confirmation at blockchain:
After a few seconds, MetaMask will show when the transaction has been confirmed!
At bottom right, we have the transaction’s details
You can copy the transaction hash and verify at RSK explorer too: 0x66244df2a1c9ae96039251189dec1b34f01b9cc445dbf17657909907d5011ebc
Retrieve (again)
Now we have the value 2000 saved, and we can check it
Click on the retrieve button
And the value is correct!
Transactions in MetaMask
It is possible to verify all transactions in MetaMask
Final conclusions
Did you think that it would be so easy to use Remix and MetaMask to create a smart contract which can be used on both Ethereum or RSK networks?
I showed to you how we can use some Ethereum developer tools, and it is great to realize that they can be used on the RSK network as well.
My 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. Kindly Share it if you like it…..