Deploy and Verify a Contract to the Rinkeby Testnet — The Easy Way
This article aims to show you how to:
- Create a wallet and fill it with testnet Ether.
- Deploy a smart contract to the Rinkeby testnet.
- Verify the contract on Etherscan.
- Interact with and send transactions to the blockchain via the published contract.
What is Deploying, why Verify?
Deploying is the process in which a smart contract (solidity file) is written to the blockchain. It will stay there forever unless the code has a function that allows it to self destruct. Everyone can see this contract and interact with it once it has been deployed. The deployed contract is also immutable, meaning that it is impossible for it to be changed once it has been added to the blockchain. After deploying a contract, the next step is to verify it.
Verifying a contract is the process of checking if the generated bytecode actually matches the source code that was written. This is necessary for the community to trust the contract and to ensure that the code will do exactly what it claims to do. Security and transparency are the most important features when it comes to utilizing the blockchain. Once verified, the contract’s source code can be seen and publicly checked by anyone. A trusted site used to verify contracts is Etherscan.io.
What is the Rinkeby Testnet?
The Testnet is an alternative from the main blockchain, to be used for testing. Testnet Ether coins are separate and distinct from actual Ether coins, and are never supposed to have any value. This allows application developers or Ethereum testers to experiment, without having to use real tokens or worrying about breaking the main Ethereum blockchain. There are many different types of testnets that differ mostly by Consensus Algorithm. The testnet we will be deploying to is Rinkeby.
Rinkeby is an Ethereum Testnet. Rinkeby uses a PoA (proof-of-authority) algorithm to achieve consensus. Rinkeby is run by centralized nodes that could be shutdown at anytime. This is one reason why it’s only used for testing. Balances do not get transferred from one network to another. You cannot send Ether from the mainnet to testnets, the balance does not carry over.
Obtaining a Contract
I’ll be using my very own Greeter Contract. The Greeter contract allows those interacting with it to change the original string, read the current string that’s set, and kill the contract if necessary. You can access the greeter via cloning or downloading the zip.
git clone https://github.com/EbenezerGH/Greeter-Contract.git
Download Zip: https://github.com/EbenezerGH/GreeterContract/archive/master.zip
Source Code: https://raw.githubusercontent.com/EbenezerGH/Greeter-Contract/master/flattened/Greeter_flat.sol
Create a Wallet with Metamask on the Rinkeby Testnet
- First we will need to download the official Metamask browser extension.
- Once that process is complete, change from Main Ethereum Network to Rinkeby Test Network, it’s time to get some Ether.
Request Ether from the Rinkeby Faucet
Since testnet Ether doesn’t really hold any real monetary value, it’s possible to request Ether in increments. You give a public key to a faucet and it will distribute Ether to that address as long as you verify a social media profile. Verification of a social media profile is used as a method to prevent spam. My go-to Rinkeby faucet is https://www.rinkeby.io/#faucet.
- On a social media profile of your choice, make a post with your public address. MAKE SURE THE POST CONTAINS YOUR PUBLIC ADDRESS, NOT YOUR PRIVATE KEY.
- Copy and paste the URL to this post inside of the Give me Ether Field.
3. Confirm that the transaction went through by checking your public address on the Rinkeby Etherscan page https://rinkeby.etherscan.io/ . It may take some time depending on how congested the network is.
What is Etherscan?
Etherscan is the leading BlockExplorer for the Ethereum Blockchain. A BlockExplorer is basically a search engine that allows users to easily lookup, confirm and validate transactions that have taken place on the Ethereum Blockchain. We are independently operated and developed by a team of individuals who are truly passionate and excited about the kinds of decentralized information and infrastructure applications that Ethereum makes possible.
And just like you, we are “users” of the Ethereum Blockchain. Etherscan is not funded, operated or managed by the Ethereum Foundation but instead exists as an independent entity. The Ethereum Blockchain has a public ledger (like a decentralized database) which Etherscan.io indexes and then makes available this information through our site. Our mission is to facilitate Blockchain transparency by indexing and making searchable all transactions on the Ethereum Blockchain in the most transparent and accessible way possible.
Etherscan is not wallet service provider, we do not store your private keys and we have no control over the transactions that take place over the Ethereum network.
In addition, we also provide a set of load balanced robust API Services that can be used to either build decentralized applications or to serve as data feeds for Ethereum Blockchain information.
Now, We can Deploy the Contract!
We will use Remix IDE to deploy our contract.
Remix can be used as a browser-based compiler and IDE. It enables users to build Ethereum contracts with Solidity language and to debug transactions.
Head to http://remix.ethereum.org and copy and paste the source code of the Greeter_flat.sol into the window. You can delete any code that’s already in the IDE window.
Note: It is important to copy the source file exactly as is. Any changes to the code (even a whitespace) can cause for an error when verifying the byte code later on.
There are many different compile versions, but I recommend picking one closest to the solidity file’s pragma level. My pragma version is
pragma solidity ^0.4.23 . So I will pick a version that does not exceed 4.23.
Looks like my code compiled with only 3 warnings now it’s time to run it.
Most of the information should be filled out on the run screen. Metamask Automatically Injects a web3 script that contains your wallet information into the browser. The 3000000 gas limit is the max amount that I am willing to pay for the transaction. A standard Ethereum transaction costs 21000. The more computational work that needs to be done, the more you should raise your gas limit.
Note: We will opt out of enabling optimizations for this tutorial.
Click Deploy, we will add a string when we’re sending transactions later.
You should then see a confirmation screen. Confirm the transaction then check the logs in the IDE.
When you send tokens, interact with a contract, send ETH, or do anything else on the blockchain, you must pay for that computation. That payment is calculated in Gas and gas is paid in ETH.
The contract has been successfully deployed on the testnet. Going to the URL in the log redirects to the transaction hash. You can see your transaction information on Etherscan. Before we can interact with the contract, we need to verify it.
Verify The Contract
Search your public address on Etherscan and go to the transaction that deployed the contract. Under the code tab you’ll see Verify and Publish. Click and it will redirect you to the screen below.
Copy and paste the whole flattened solidity file exactly as it is written. Fill in the name of the parent contract for contract name, and specify the compiler that was used. We did not add any libraries so we can scroll to the bottom and verify.
The ABI is basically how you can encode Solidity contract calls for the EVM.
The Bytecode is the low level representation of the solidity file that was written.
Write to the Contract
Go back to the contract and click Address and you should see new tab options. Transactions, Code, Read Contract, Write Contract, Events.
Go to Write Contract and click Connect with Metamask. Write a greeting string, confirm the transaction and you’re done!
Unless you can read hexadecimal I recommend you select decode input data. You should see your message displayed in plain text.
View the transaction from the below link.
The only difference between publishing from the mainnet and testnet is real Ether. Using Metamask and Etherscan, you can just select a different network other than Rinkeby. I enjoy using Metamask and remix because they are super lightweight tools and I don’t have to download the whole blockchain. They’re both powerful and capable of much more than was demonstrated.
There are many other ways to deploy a contract to the blockchain. There are also many more intricacies that vary per use case. I tried to be as general as possible and use a very simple contract. I also chose not to change too many optional parameters.