How to Create a Smart Contract on Ethereum
With the advent of blockchain technology, another technology has gained popularity: smart contracts. A smart contract is a self-executing contract in which the contents of the agreement are written directly into the lines of code. In simple terms, you can think of smart contracts as a collection of code and data that resides on the Ethereum network.
As a concept, smart contracts were proposed by Nick Szabo, a computer scientist and legal scholar in the 1990s. According to Szabo, who designed the virtual currency “Bit Gold” in 1998, smart contracts are transaction protocols that enforce and execute contract conditions.
Smart contracts are not controlled by a user, instead, they are self-executing programs on the Ethereum blockchain. Users can interact with smart contracts by submitting transactions that trigger a function encoded in the smart contract. Similar to regular contracts, smart contracts can define rules, but unlike normal contracts, the terms of the contract are enforced automatically through code and not through human intervention. A perfect analogy to describe a smart contract would be a digital vending machine. To get a snack from a vending machine you have to input an instruction, which automatically triggers a certain output.
The logic of the operation is programmed into the machine. Similar to a vending machine, a smart contract has logic programmed into its code. Also, a vending machine eliminates the need for operators to handle the machines. Likewise, smart contracts have the potential to replace intermediaries in many sectors.
Smart contracts have several benefits. For one, they are transparent. Participants can audit and view encrypted transaction logs. Smart contracts are also permissionless, meaning that anyone can write a smart contract and deploy it onto the Ethereum blockchain. More importantly, smart contracts are composable and can connect with other smart contracts within the Ethereum network.
To deploy a smart contract, you need to pay gas in the same way you would pay for an ETH transaction. The greater a smart contract’s complexity, the more expensive it is to run that smart contract on the public blockchain. A concrete example is given by Eric Lastname, a blockchain developer. According to him, the cost of deploying a simple smart contract is anywhere around $500. However, for a moderate-sized contract, you may have to spend north of $5,000 just on deployment costs.
Different smart contract platforms
While most smart contracts are built on Ethereum, other blockchains support smart contracts too. This includes Solana (SOL), Polkadot (DOT), BNB Chain, and more. However, Ethereum remains the most popular smart contract protocol. Information on the number of smart contracts deployed on Ethereum remains sketchy, but a 2020 study revealed there were over 1 million smart contracts on Ethereum. This number must have grown a lot since then. According to Yahoo Finance, the number of smart contracts deployed in Ethereum peaked in June 2021, at 2.5 million.
Having said that, there are different languages for smart contract deployment.
- Vyper is a Python-like programming language that is contract-oriented and targets the Ethereum virtual machine (EVM). Vyper was built to improve on the security issues of Solidity.
- Finally, Yul is an intermediate programming language that is used by Solidity and addresses the needs of different backends. Yul is already used in most Ethereum-based projects.
How to create a smart contract
Below is a step-by-step guide on how to create and deploy a smart contract on Ethereum.
Step 1: Connect to the Ethereum network.
You can connect to the Ethereum mainnet by downloading and installing a MetaMask wallet on your Chrome browser and enabling it. Once you have completed the setup, proceed to connect it to the Ethereum mainnet.
Step 2: Choose a test network.
You will find a list of test networks in your Metamask wallet. Select one, which will be used to test the smart contract you are building. The test networks include:
- Robsten Test network
- Rinkeby Test network
- Kovan Test network
- Goerli Test network.
Step 3: Fund your wallet with Testnet ETH.
Eventually, when you are ready to test your smart contract, you will need to have Testnet ETH in your wallet. Fortunately, the process of adding Testnet ETH in MetaMask is straightforward. Click on the “Deposit” and “Get Ether” buttons under the Test Faucet and proceed with the instructions.
Step 4: Use the Remix browser to write your smart contract.
You can use the editor in Remix browser IDE to write your smart contract in Solidity. Remix browser is the best option for writing smart contracts as it comes with several features and is usually used to write basic smart contracts.
Step 5: Create a .sol extension file.
Open the Remix Browser and click on the (+) icon on the left side to create a .sol extension. This makes any programmed file solidity-compatible.
Step 6: Complete your smart contract code.
Choose a version of the compiler from the Remix browser and compile the solidity smart contract code.
Step 7: Deploy the smart contract.
Deploy the smart contract on your selected Ethereum test network by clicking on the deploy button on the Remix browser. Once the transaction is complete, the address of the smart contract will appear on the right-hand side of the Remix browser.
Step 8: Make smart contract ready to go live.
To prepare a smart contract to go live on a blockchain, procedures such as testing or auditing are strongly recommended, besides other considerations. Once reassured that the smart contract is ready to hit the market, the smart contract can be deployed to the Ethereum mainnet.
For a complete visual walkthrough into how smart contracts work, watch below!