Deploy a Smart Contract on Somnia Network using Hardhat.
Last updated
Last updated
Various developer tools can be used to build on Somnia to enable the Somnia mission of empowering developers to build Mass applications. One such development tool is Hardhat. Hardhat is a development environment for the EVM i.e. Somnia. It consists of different components for editing, compiling, debugging, and deploying your smart contracts and dApps, all working together to create a complete development environment. This guide will teach you how to deploy a “Buy Me Coffee” Smart Contract to the Somia Network using Hardhat Development tools.
This guide is not an introduction to Solidity Programming; you are expected to understand Basic Solidity Programming.
To complete this guide, you will need MetaMask installed and the Somnia DevNet added to the list of Networks. If you have yet to install MetaMask, please follow this guide to Connect Your Wallet.
Hardhat is installed and set up on your local machine. See Guide.
Start a new Hardhat project by running the following command in your Terminal:
This will give you a series of prompts. Select the option to “Create a TypeScript Project (with Viem)”
This will install the required dependencies for your project. Once the installation is complete, open the project directory and check the directories where you will find the `contracts` directory. This is where the Smart Contract will be added.
Open the Smart Contracts folder and delete the default Lock.sol
file. Create a new file, BuyMeCoffee.sol
and paste the following code:
To compile your contracts, you need to customize the Solidity compiler options, open the hardhat.config.js
file and ensure the Solidity version is 0.8.28
and then run the command:
It will return the response:
This will compile the Solidity file and convert the Solidity code into machine-readable bytecode. By default, the compiled artifacts will be saved in the newly created artifacts
directory.
The next step is to deploy the contracts to the Somnia Network. In Hardhat, deployments are defined through Ignition Modules. These modules are abstractions that describe a deployment, specifically, JavaScript functions that process the file you want to deploy.
Open the ignition
directory inside the project root's directory, then enter the directory named modules
. Delete the Lock.ts
file. Create a deploy.ts
file and paste the following code:
Open the hardhat.config.js
file and update the network information by adding Somnia Network to the list of networks. Copy your Wallet Address Private Key from MetaMask, and add it to the accounts section. Ensure there are enough STT Token in the Wallet Address to pay for Gas. You can get some from the Somnia Faucet.
The "0xPRIVATE_KEY" is used to sign the Transaction from your EOA without permission. When deploying the smart contract, you must ensure the EOA that owns the Private Key is funded with enough STT Tokens to pay for gas. Follow this guide to get your Private Key on MetaMask.
Open a new terminal and deploy the smart contract to the Somnia Network. Run the command:
You will see a confirmation message asking if you want to deploy to the Somnia Network. Answer by hitting “y” on your keyboard. This will confirm the deployment of the Smart Contract to the Somnia Network.
Congratulations. 🎉 You have deployed your “BuyMeCoffee” Smart Contract to the Somnia Network using Hardhat. 🎉