How to Create a Smart Contract?

Ali
5 min readAug 5, 2021

A glorious guide to creating smart contracts and dapps.

To create a smart contract is super easy, you just need to have basic knowledge in any of the programming languages used for writing down a smart contract. There are different programming languages used for writing down smart contracts, solidity is the most commonly used programming language among them.

1. Smart Contracts

Let’s discuss briefly a smart contract, a smart contract is nothing but similar to a real-life contract implements without third-party involvement and adopts the properties of a blockchain. Oh, now Blockchain? what’s blockchain? Blockchain is just like a decentralized database without permission for updating and deletion. Once the data is store in the blockchain, it can never be changed. A smart contract after deploying on the blockchain becomes unchangeable, deploying means it is stored on the blockchain, as we already discussed, a blockchain is a distributed database, the smart contract after deployment is stored among all the nodes, such as BSC (Binance Smart Chain, a blockchain) has 21 nodes, so if you deploy on BSC, your smart contract will be stored on all the nodes (21 nodes) of BSC. If you want to make any changes to your smart contract, you’ll have to write down a new smart contract and make the desired changes and deploy it again on the blockchain. Once it is deployed on the blockchain, a contract address will be assigned to it and we can interact with the deployed smart contract using the smart contract address and ABI( Application Binary Interface). Each smart contract would be assigned a unique contract address.

2. A smart contract to store and load the string

Let me show you how to get the address and ABI for a smart contract. It’s needed when you want to integrate your smart contract with an HTML page using javascript, known as web3. We’ll discuss it in the next post like how you can interact with your smart contract using a webpage (written in HTML, CSS, and use javascript and web3 libraries).

3. ABI for a smart contract

ABI (Application Binary Interface) lets you encode and decode parameters to ABI for function calls to the EVM (Ethereum Virtual Machine). Here we have the ABI file for our smart contract “sotre&load”, that we got after compiling it. We can see it represents function name same as our smart contract functions “store” and “load”, load function does not take any input, so the input field is empty and the store does not return any value so the output is empty.

4. An ABI of a smart contract “store&load”
5. Getting contract address for a smart contract

There is another way also. Go to Etherscan and copy the contract address from there. You can access Etherscan by clicking on the link given in the output panel. It would seem like shown in the figure below.

6. Getting contract address from EtherScan

So, let’s talk about what are the required tools for writing the smart contract. Once we store smart contracts or make transactions to the smart contracts on the blockchain, they can be accessed from anywhere in the world. As we already discussed the smart contract and all the transactions are stored among all the nodes. Now, the question is that what’s their benefit to store the transaction??? Did you also get this question into your mind? The answer here is “They get money for storing the smart contract and transactions”. Whoever stored data on the blockchain, will have to pay some gas fee according to the data that is being stored. There are some wallets that are used to pay the fee. If you are reading this post for just learning purposes, here is good news for you... If you just want to practice deploying your smart contract and transaction for learning purposes. There are some blockchains freely available for that and called test networks. There are two types of networks: main net and testnet. Mainnet is used to deploy on the main blockchain such as Ethereum blockchain Mainnet, and the testnet is used for testing purposes before deploying it to the mainnet, for deploying on the test network you don’t need to pay the real money, just dummy gas is provided by each blockchain, which is for free.

I think that’s enough about this. Let’s create a smart contract now!

So, here is a tool, www.chainide.com, that is very easy to use, especially for beginners. Simply jump over there and start writing down your first smart contract, Hurray!!

A smart contract to store and load the string to/from blockchain

Let’s for this example, we create a simple smart contract that saves your first name on the blockchain and then shows it back.

Like other programming languages, solidity also has different data types for storing different types of data, functions, if-else conditions, etc., I mean to say, you don’t need to worry, everything is almost the same. Here we a simple smart contract to store and load your first name.

Once I deploy it using ChainIDE, I will get an ABI and Contract address for this smart contract. But before deploying it, we need to pay the transaction fee that is also known as the gas fee. For that, we will use the MetaMask wallet. You can simply download the MetaMask from the given link, and then go to the Ropsten test network because we are going to use the ropsten test network to deploy our smart contract. Once you have everything ready, simply compile the source code, get ABI, and deploy your smart contract. That’s all. Is it Difficult? I don’t think so, what you think please tell in the comment section.

7. Ropsten Test Network for deploying and making transactions to the smart contract

Now, it's time to interact with the deployed smart contract. We don’t need to code any further, ChainIDE has an interactive panel for that, simply go there and use your deployed smart contract.

5. Interact with the deployed smart contract

Practice, and if you face any problem, feel free to ask in the comment section. Thanks…

To learn more about blockchain and smart contracts, reach us

Find us on the following:
Forum: https://forum.chainide.com/
Twitter: https://twitter.com/MatrixDapp
Telegram: https://t.me/joinchat/Q48BNwB-f8RlZTJl
Facebook: https://www.facebook.com/tian.ling.9

YouTube: https://www.youtube.com/channel/UCgvPUHayWfxAGiJCI2xOzNg

--

--

Ali

Ali Arshad, student of computer science, interested in blockchain.