Creating and Deploying a Solidity Smart Contract on a Blockchain

Hemant Juyal
Coinmonks
Published in
7 min readJun 10, 2022

--

If you are in the world of Blockchain and Cryptocurrencies then you would have heard about Smart Contract and If you haven’t then it’s ok as I will cover in an easy way what is a Smart Contract and how to create one and deploy it on a blockchain.

But as a First step you have to be in sync with me like this ;-)

Building Smart Contract: First Step

For newbies,

A blockchain is a (form) of distributed ledger having growing list of blocks (or records) which are linked together cryptographically.

In simple words we can describe a Smart Contract as a computer code that comprises a set of rules and executes on a blockchain.

Smart Contract: A typical Tenancy Agreement
Smart Contract: A typical Tenancy Agreement

Let’s understand it in a better way with the an example. In our real world the way we have a printed Tenancy Agreement or Tenancy Contract that comprises of all the agreement clauses and have to be adhered by both the signing parties. In blockchain world we can have the same Tenancy Contract in its pure digital form through a Smart Contract. While stating pure digital form means in form of a computer program or code where this computer code inherently holds the programmatic representation of all the Tenancy Contract clauses agreed by the involved parties and the super interesting part is no one can alter this contract not even the involved signing parties.

It can be Illustrated as

Smart Contract: A typical Tenant Agreement / Contract flow
Smart Contract: A typical Tenant Agreement / Contract flow
Smart Contract: A typical Smart Contract flow
Smart Contract: A typical Smart Contract flow on Blockchain

Now we have good understanding about Smart Contract lets move to some real stuff i.e. How to create a smart contract and get it deployed. As I have mentioned it’s a computer program that automatically executes so we will use some coding and leverage some tools to build and deploy our Smart Contract.

We will be using Solidity, Hardhat, Alchemy and Metamask. Don’t worry we will know about these as we move forward.

For newbies

Solidity: Solidity is an object-oriented programming language for implementing smart contracts on blockchain platforms. It’s influenced by C++, JavaScript and Python.

Hardhat: Hardhat is a development environment to compile, deploy, test, and debug Ethereum based softwares.

Alchemy: Alchemy develops and markets tools and infrastructure services for blockchain companies. Alchemy has wide range of products that provides a powerful blockchain developer platform providing a suite of developer tools.

Metamask: MetaMask is a crypto wallet that allows users to store and swap Ether and other Ether-related tokens.

Let’s quickly setup these on our machine / system to develop and deploy our Smart Contract. Lets do it

Installing Metamask

Go to MetaMask and install MetaMask for your browser (through available browser extensions) and follow the mentioned easy steps to create your MetaMask wallet Account.

For newbies, MetaMask is a crypto wallet that allows users to store and swap Ether and other Ether-related tokens.

Smart Contract: Install MetaMask for your browser: Go to MetaMask Download page
Smart Contract: Install MetaMask for your browser: Go to MetaMask Download page

Setting up MetaMask
After creating our wallet we will set-it up by adding ether from a faucet as to deploy our smart contract to the test network, we’ll need some fake ETH. Why because to deploy and execute a code on Blockchain require a Gas Fee that typically gets paid using cryptocurrencies and tokens. In our case we will paying the Gas fee using ETH.

Follow the below steps into your MetaMask Wallet

  1. Change the Network to Goerli Test Network
Smart Contract: Step1 MetaMask Wallet set-up
Smart Contract: Step1 MetaMask Wallet set-up

2. Go to Goreli Faucet and enter your MetaMask wallet address and send some test Eth

Smart Contract: Step2 send test eth to you MetaMask Wallet
Smart Contract: Step2 send test eth to you MetaMask Wallet

After sending test Eth your MetaMask wallet should start reflecting the updated amount.

Smart Contract:
Smart Contract: MetaMask wallet with test Eth

Using Alchemy

To run our Smart Contract we will use Alchemy through which we will be able to communicate with Ethereum chain and has developer tools for analytics and monitoring. Use sign-up to create free account

Smart Contract: Create app and API key on Alchemy
Smart Contract: Create app on Alchemy

Once you are done with the sign-up process then create an app by selecting giving app name something like ‘SmartContract-GreetingsMessage’ and Chain as Ethereum and Network as Goreli

You can view the API keys from the view section once the app is created. Keep it handy as we will be using it in doing our Smart Contract configuration.

Smart Contract: View API key on Alchemy
Smart Contract: View API key on Alchemy

Smart Contract Project Setup

Check out the greetings smart-contract project code from Github into your local directory. You will see following project structure. Don’t worry I will explain the important files.

Note: If you have Github installed on your local machine then follow the official installer page and install it on your system

Smart Contract: Github project set-up
Smart Contract: Github project set-up

We will use node and CLI terminal to execute our commands. Install LTS version of node if you haven’t installed it already on your machine.

For newbies, Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine and LTS release status is “long-term support”, which typically guarantees that critical bugs will be fixed for a total of 30 months.

Run the following command from the root folder of the project and it will install all the project dependencies

npm install

The contract folder has a file GreetingMessages.sol which is a Solidity file and has the set of logic. It’s a simple Smart Contract that will be initialised with a default greeting message and has an update function to update the messages.

Smart Contract: GreetingMessages .sol smart contract

.env Configuration
Within the Github cloned project root folder, create a new file and name it as ‘.env’ which will be having our env configuration parameters that will be required. Enter the relevant details

#.env file configuration for deploying our Smart ContractAPI_URL = Copy HTTP value from your Alchemy app key details section
API_KEY = Copy API_KEY value from your Alchemy app key details section
PRIVATE_KEY = Your MetaMask wallet private key
Smart Contract: View API key on Alchemy
Smart Contract: View API key and HTTP configuration on Alchemy

Refer to How to export your MetaMask account’s private key to follow the steps and to get the value of `PRIVATE_KEY` configuration

Smart Contract: Export you MetaMask wallet private keys
Smart Contract: Export you MetaMask wallet private keys

Fully configured .env file will look something like this

Smart Contract: .env file configuration

Deploy your Smart Contract

Run the following command from project root folder to compile our solidity code

npx hardhat compile

If all the node packages are installed properly as per the previous steps, then you will received success message stating something like “Compiled 1 Solidity file successfully”

To deploy the compile code, navigate to scripts/deploy.js and you will find the code as

A ContractFactory in ethers.js is an abstraction used to deploy new smart contracts, so GreetingMessages here is a factory for instances of our GreetingMessages contract. Calling deployon a ContractFactory will start the deployment.

Run the command to execute the deployment process

npx hardhat run scripts/deploy.js

On successful deployment you will receive message as ‘Contract deployed to address: 0xeD0B03aD1C49……

Note: I have mentioned the partial address that I have received during my command execution on CLI terminal. Your local code execution will give you a different address as per your configuration. So make a note of this

Deployed Smart Contract Validation
Above command output shows that deployed on the Blockchain successfully. Copy this address and save it to a file as we will be referring to it in other scenarios that will be covered in the next stage. To validate the deployed Smart Contract, go to Goreli Testnet Explorer and enter your contract address. You will find the GreetingMessages contract creation entry as part of the contract details thus our deployed contract is validated.

Smart Contract: Deployed contract details
Smart Contract: Deployed contract details

Congratulations!!! we have deployed our smart contract successfully. Time for some celebration ;-)

Smart Contract: Deployment successful and now it’s time for celebration
Smart Contract: Deployment successful and now it’s time for celebration

Images Credit: Paramount Pictures and Leonardo DiCaprio

Glossary:

Gas: Gas is a unit of account within the EVM used in the calculation of a transaction fee, which is the amount of ETH a transaction’s sender must pay to the miner who includes the transaction in the blockchain. From Wikipedia

NFT: NFTs or Non-fungible tokens typically contain references to digital files such as photos, videos, and audio. From Wikipedia

--

--

Hemant Juyal
Coinmonks

I am a Technologist with a passion for innovation, I always love staying on top of the most recent advancements and developments in the field of technology.