Table of Contents
In recent years, smart contracts have become increasingly popular, with Ethereum being the go-to platform for deploying them. Smart contracts are self-executing contracts with the terms of the agreement between buyer and seller being directly written into lines of code. This eliminates the need for intermediaries, allowing for faster and more secure transactions.
In this guide, we will take you through the steps involved in deploying a smart contract on Ethereum.
Deploying To A Local Network
A smart contract can be installed on a local network using an emulator, such as Ganache-cli. As everything is taking place on a local test network, it takes care of everything, and the user doesn’t have to worry about the security or the quantity of gas needed for transactions. The ganache provider must only be passed to the web3 instance as an argument (web3 facilitates the connection between the blockchain network and the js application).
Deploying To Actual Ethereum Network
- Before deploying a smart contract to an actual Ethereum network, make sure the account has some ether in it. Deploying a contract is like sending a transaction, and it needs some gas amount to process.
- Unlike deploying on a local network, transactions will take some time to complete (anywhere between 15 seconds to 5 minutes). Web3 is used to interact with the network the same way it is done in local deployment except to customize the provider that will be passed into the web3 instance.
- Instead of creating our own node that connects to the Ethereum network, one can use a developer platform with RPC endpoints like Infura or Alchemy. With one of these accounts, you have an API key that gives access to their Infura / Alchemy blockchain nodes that are already hosted on the Ethereum network.
- Simply sign up for Infura and get an endpoint that will be used in the code to deploy the smart contract. The tutorial below shows a smart contract being deployed with Infura.
Step 1- Install the required dependencies by running the following commands-
Step 2- Sign up for Infura and create a project on a particular Ethereum network to get access to the endpoint. The endpoint will be required to deploy the smart contract on the infura node that is already hosted on the Ethereum network. To create a project on infura-
- Click on create a new project.
- Give it a name.
- Select the network to deploy the smart contract on.
- A maximum of 3 projects can be created on infura for free.
Step 3- Access Bytecode and ABI now (Compile the smart contract). Solidity compiler produces a sizable amount of code, which can optionally be printed to the console. The following script only extracts the bytecode and interface that are pertinent (useful for deployment) from the output.
Step 4 Add Metamask extension to Google Chrome from the Chrome web store.
Step 5- After you have access to the bytecode and interface, all that is left to do is use the truffle-hdwallet-provider that was previously installed to build a provider with your own mnemonic phrase and Infura endpoint.
As a parameter, pass the provider when creating a web3 instance. Lastly, to deploy the smart contract, utilize the deploy method with bytecode as an input.
Now the contract is deployed on the network, its functionality can be tested using remix IDE or one can create an interface to interact with the smart contract on the network.
Interacting With Deployed Smart Contracts Using Remix IDE
Remix can be used to connect to actual Ethereum networks and interact with deployed smart contracts. It is the easiest way to interact with a deployed smart contract without having to make a fancy front end.
Step 1– Open Remix IDE in Chrome browser and copy the solidity code of the deployed smart contract and paste it into the ‘Ballot.sol’ file in the IDE. Switch to the solidity compiler by clicking on the icon on the sidebar and compile it.
Step 2- Navigate to Deploy and run transactions from the sidebar and select injected web3 from environment dropdown. It is the instance of web3 injected by metamask into your browser. It also has access to all the accounts.
Step 3– Instead of deploying the smart contract, copy the address of the already deployed smart contract in the ‘At Address’ field. This button is disabled until you put in a valid address.
Once the button is clicked, the list of functions from your smart contracts can be seen. One can interact with a deployed smart contract using these function buttons. Since the ‘example.sol’ has only one variable, manager. Clicking this button will give the address of the account it was deployed from as the output.
Deploying a smart contract on Ethereum can seem daunting at first, but it’s a relatively straightforward process once you have set up your development environment and familiarized yourself with the necessary tools and concepts.
By following the steps outlined in this guide, you should be able to deploy your first smart contract on Ethereum in no time. With the growing popularity of smart contracts and the Ethereum network, this is a skill that is likely to become increasingly valuable in the years to come.