There are various ways to deploy Ethereum based smart contracts, some popular options are listed here:
- Use migrations of Truffle framework.
- Use Ethereum wallet and MetaMask.
- Using Remix IDE and MetaMask.
I prefer the last option, and would suggest the readers of this blog to follow the same, as it is most optimised as far as gas consumption is concerned and it can also be used to debug contracts.
In this tutorial we will deploy a sample contract, which is initially available on Remix, using MetaMask, on Ropsten test network.
Following is a tutorial you should follow to understand MetaMask and set up your own wallet on it, it is quite outdated but it is the perfect way for you to get the gist of things and get you started with MetaMask.
You will need to create a suitable password, which will be used to encrypt and create a seed phrase.
Keep the seed phrase somewhere secure and don’t share it with anyone, it can be used to retrieve your wallet.
Change your network by clicking on Main Network and switch to Ropsten Test Network.
Click on Buy, to get some free Ropsten Ethers.
Go to the Ropsten test faucet and request some Ethers, they will be mined in some time and will be added to your Ethereum account on MetaMask.
In some time your account will look like this, and you will be ready to deploy your first test contracts on Ropsten.
Using Remix IDE to deploy contracts:
Open the Remix IDE and you will see a pre-compiled contract file, namely ballot.sol. We will use this contract to understand how the deployment using Remix works.
There are three kinds of options available for deployment in Remix:
- Injected Web3 :
This will help you connect your MetaMask account to Remix and use the network you’re on for deployment. You can choose among “Ropsten”, “Rinkeby” and “Mainnet”. Ropsten and Rinkeby should be used for testing your contracts before deploying them.
- Web3 Provider:
You can use some explicit Web3 provider or can integrate it on your localhost and use it.
We will be using Injected Web3 as it is the easiest and widely used. So choose injected Web3 and proceed:
You will see your Ropsten account in the “Account” section of “Run” Subhead. This represents your current MetaMask account which will be used for all the required transactions. You can create more accounts on MetaMask and switch between them to connect different accounts to Remix. Please note that only one MetaMask account can be connected to remix at a time.
Enter number of proposals, which is a parameter required by constructor of Ballot contract and click create. You will see a MetaMask pop-up like the following:
This pop-up asks for a confirmation for the transaction you’re just about to create. I’ll explain the details mentioned and how to manipulate them one by one:
MetaMask calculates the maximum amount of gas that can be consumed by a transaction and displays it here, this amount of gas will be sent with the transaction, in case all the gas is not consumed during the transaction, remaining gas will return back to your account.
It calculates the latest price which is being used in the Ropsten test network, it depends on amount of traffic within Ropsten Network. You can increase it for faster results.
Max Transaction Fee:
It represent the maximum estimated mining fee for the transaction. Is an estimate of costs in USD and Ethers.
Sub total of all the costs in USD/Ethers.
Once you submit your transaction, you can track it’s status in MetaMask or Ropsten Etherscan.
Once the transaction is processed and is added to a block then you will see the contract at the bottom of “Run” section in Remix, as follows:
You can see all the public functions and state variables of the contract here. Blue represents that a function/variable is constant and it won’t consume any gas during execution. Constant functions are those which does not result in value change of state variables, and thus don’t consume any gas. Similarly Red represents a function which will consume gas and should be carefully used, when you use it a MetaMask pop-up to confirm transaction will appear. You can set the required parameters and submit your transaction. There are some other functions which accept Ethers, and can be used to send Ethers to a contract, it is of a darker red colour. You can specify the value and the unit you will be using in the “Value” section labelled under “Gas Limit”.
Understanding contract deployments is very necessary, and nascent developers make a lot of mistakes in this part leading to huge deployment costs and cumbersome testing mechanisms. This blog is a guide for beginners to avoid those mistakes and enjoy “happy development/deployment within solidity”.