How to write and deploy your first smart contract
- General
How to write and deploy your first smart contract
Create and Deploy your Smart Contract
The first thing that we need to understand is what the Ethereum Virtual Machine (EVM) is. Its sole purpose is to act as a runtime environment for smart contracts based on Ethereum. Think of it as a global super computer that runs all the smart contracts. As the name suggests, the EVM is virtual and not a physical machine. You can read more about the EVM here.
The second concept we need to understand is what is gas. In the EVM, gas is a unit of measurement used to assign a fee to each transaction with a smart contract. Each computation that happens in the EVM requires gas. The more complex and tedious it is, the more gas is needed to execute the smart contract.
Every transaction specifies the gas price it is willing to pay in ether for each unit of gas, allowing the market to decide the relationship between the price of ether and the cost of computing operations (as measured in gas). It’s the combination of the two, total gas used multiplied by gas price paid, that results in the total fee paid by a transaction.
1 |
Fee for transaction = Total gas used * gas price; |
Read more about gas here.
Now that you have basic knowledge about what a smart contract is and how the smart contract runs, we can go straight into how we are going to make our very own smart contract!
Step 1: Connect to the Ethereum network
Install Metamask and select any test network and from its faucet add some dummy ethers. Once the dummy ethers are added to the wallet, you can start writing smart contracts on the Remix Browser IDE in the Solidity programming language.
You might also find the following test networks in your MetaMask wallet:
- Kovan Test Network
- Goerli Test Network
- Robsten Test Network
Step 2: Use editor remix to write the smart contract in Solidity
We will use Remix Browser IDE to write our Solidity code. The remix is the best option for writing smart contracts as it comes with a handful of features and offers a comprehensive development experience.
It is usually used for writing smaller-sized contracts. Remix’s features include:
- Warnings like gas cost, unsafe code, checks for overlapping variable names, whether functions can be constant or not
- Syntax and error highlighting
- Functions with injected Web3 objects
- Static analysis
- Integrated debugger
- Integrated testing and deployment environment
- Deploy directly to Mist or MetaMask
Let’s start writing smart contract code by visiting here.
Step 3: Create a smart contract with .sol extension file
Learn solidity and write any simple smart contract In this example using hello world smart contract. Open Remix Browser and click on the plus icon on the top left side, next to the browser to create a .sol extension file
1 2 3 4 5 6 7 |
// SPDX-License-Identifier: MIT pragma solidity 0.8.13; contract HelloWorld { function sayHelloWorld() public pure returns (string memory) { return "Hello World"; } } |
Step 4: Deploy your contract
Deploy the smart contract at the Ethereum test network by pressing the deploy button at the Remix window’s right-hand side. Wait until the transaction is complete.
After the transaction commits successfully, the address of the smart contract would be visible at the right-hand side of the remix window.
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s