How to Simply Deploy a Smart Contract on Ethereum?
Smart contracts are blocks of code that reside on the blockchain. It is like an Ethereum account but there is a critical difference between an external account and a smart contract. Unlike a smart contract, an external account can connect to multiple Ethereum networks (Goerli testnet, mainnet, etc.) whereas a smart contract is only specific to one individual network (the network it is deployed on). When a smart contract is deployed, it creates an instance (contract account) on the network. One can create multiple instances of a smart contract on the network or multiple networks. Deployment of a smart contract is done by sending a transaction to the network with bytecode.
Deploying To A Local Network
An emulator can be used to deploy a smart contract on a local network eg. Ganache-cli. It takes care of everything and the user doesn’t have to worry about the security and the gas amount required for transactions since everything is happening on a local test network. All one has to do is pass the ganache provider as an argument to the web3 instance(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 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 below tutorial shows a smart contract being deployed with Infura. For a smart contract tutorial using tools like Alchemy (ethers.js, Hardhat, Solidity, and Metamask), refer to this basic tutorial – “Hello World Smart Contract“.
example.sol- Below is the sample solidity code used for testing. All it does is set a public variable as the address of the sender.
Step 1- Install the required dependencies by running the following commands-
npm i email@example.com firstname.lastname@example.org email@example.com
Make sure to install the same versions for the following scripts to run successfully.
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 – Get access to Bytecode and ABI (Compile the smart contract). Solidity compiler gives a huge piece of code as output, one can print the output to console if required. Only the relevant part (relevant for deployment) i.e., bytecode and interface are extracted from the output in the following script.
Step 4 – Add Metamask extension to google chrome from the chrome web store.
Step 5 – Once have access to the bytecode and interface, all that is required is to create a provider with own mnemonic phrase and infura endpoint using the truffle-hdwallet-provider that was installed earlier. Create a web3 instance and pass the provider as an argument. Finally, use the deploy method with bytecode as an argument to deploy the smart contract.
Contract is deployed to 0x8716443863c87ee791C1ee15289e61503Ad4443c
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 Contract 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 frontend.
Step 1- Open Remix IDE in chrome browser and copy the solidity code of the deployed smart contract and paste it in the Ballot.sol file in the IDE. Switch to the solidity compiler by clicking on the “S” 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.
Before Mainnet Deployment | Goerli Testnet
Before deploying to the Ethereum mainnet, we recommend deploying to the Goerli testnet. Note that with the recent Ethereum merge, Goerli is the only Ethereum-supported testnet. Other testnets like Rinkeby, Kovan, and Ropsten have been deprecated.
By deploying on Goerli testnet first, you’re able to test your smart contract without it costing real money (in the form of real ETH). In order to deploy to Goerli testnet (https://eth-goerli.g.alchemy.com/v2/Alchemy-API-key or https://goerli.infura.io/v3/Infura-API-key), you’ll need test Goerli Ether (“Goerli testETH”). You get Goerli testETH from Goerli faucets – for example, Alchemy provides a free Goerli faucet where you can get more testETH every day: goerlifaucet.com.
Please Login to comment...