A smart contract is a set of programs that runs by itself. It works on if-this-then-that
concept. You provide conditions in the smart contract code, and when the conditions are met, it automatically executes. It does not require any human intervention. In this article, we will explore how to build and deploy your first basic smart contract with Hardhat and Typescript.
A smart contract is-
- Automated – means it runs automatically when a condition is fulfilled
- Immutable – means you can not change anything in that once it is deployed
- Transparent – everyone on the network can access its code and see the transactions
- Trustless – removes the need for any manual management
A brief about Hardhat
Hardhat provides a development environment to build ethereum based smart contracts. It is a local lab for blockchain developers. You can deploy, test, and execute smart contracts before deploying them to the real environment.
Write your first smart contract with Solidity
Now that we have a basic understanding of Smart Contracts and Hardhat, let’s write our first simple smart contract.
By the way, smart contracts are written in the Solidity programming language. We will write a first contract and compile it using Hardhat. The compiled binary will be deployed to the blockchain network.
// SPDX-License-Identifier: MIT
// contracts/DevContract.sol
pragma solidity ^0.8.19;
contract DevContract {
string public message;
constructor(string memory _message) {
message = _message;
}
function setMessage(string memory _newMessage) public {
message = _newMessage;
}
}
A straightforward smart contract is written above, where a class with the name DevContract
is created with a constructor, a public property and a public method. It stores a message on the blockchain. You can access the current message with message
and update any new message with setMessage
.
Do you notice the first comment in the code – // SPDX-License-Identifier: MIT
? License identifier is required to be present in the Solidity smart contract to avoid the warning –
Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing “SPDX-License-Identifier: <SPDX-License>” to each source file. Use “SPDX-License-Identifier: UNLICENSED” for non-open-source code. Please see https://spdx.org for more information.
Build and deploy your first smart contract with Hardhat and Typescript
Your first smart contract is written in the Solidity programming language. We need to write our Typescript code to compile and deploy the contract to the blockchain network.
Let’s create a directory structure with initial files for our Hardhat TypeScript application.
hardhat-contract-deploy/
├── contracts/
│ └── DevContract.sol
├── app.ts
├── hardhat.config.ts
├── tsconfig.json
├── package.json
Run npm init -y
which will create a package.json
file with initial code.
Once package.json
file is created with initial code, run the command to install dev dependencies that will deploy the smart contract to the blockchain.
npm i -D @nomicfoundation/hardhat-toolbox hardhat ts-node typescript
The above command will install the hardhat and hardhat toolbox packages as dev dependencies along with ts-node and typescript.
Put the DevContract.sol
smart contract inside /contracts
folder which we created earlier. Copy and paste the following contents into the respective files.
// app.ts
import hre from "hardhat";
async function main() {
const HelloWorld = await hre.ethers.getContractFactory("DevContract");
const dev = await HelloWorld.deploy("Hey StackBlogger!");
await dev.waitForDeployment();
console.log("Contract deployed at:", await dev.getAddress());
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
The above code deploys the DevContract
smart contract to the local hardhat blockchain. We import the hre
(Hardhat Runtime Environment) from hardhat
package. It compiles the smart contract if not already compiled, and it deploys the contract to the local blockchain with an initial message as Hey StackBlogger!. Since the deployment process is asynchronous so we need to wait till the deployment is complete. Once it is complete, we get the deployed address using getAddress()
method.
// hardhat.config.ts
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
const config: HardhatUserConfig = {
solidity: "0.8.19"
};
export default config;
When you work with Hardhat for smart contracts deployment, it requires a config file to be present at the root. The name of the file should be hardhat.config.ts. We do not need to explicitly consume any of its value, but hardhat internally does that.
This tells the hardhat compiler to use the solidity
0.8.19 version for smart contracts compilation, deployments, etc.
// tsconfig.json
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"lib": ["es2020"],
"outDir": "./dist",
"rootDir": "./",
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true
},
"include": [
"./**/*"
],
"exclude": [
"node_modules",
"dist"
]
}
Deploy the smart contract to the Hardhat local blockchain
Now that we are ready with the code, we need to deploy the smart contract to the local blockchain. Run the command to deploy.
npx hardhat run app.ts
It will compile the smart contract and deploy it to an address on the blockchain. It will print the address on the console. Here is a screenshot of the console that shows the smart contract deployed address to the local hardhat blockchain network.

Conclusion
In this article, we explored how to write a very simple smart contract in Solidity
programming language and how to build and deploy your first smart contract with Hardhat and TypeScript to the local blockchain network.
In the next article, we will explore how to set and get different values on blockchain using a smart contract.
Meanwhile, you can explore my Node.Js
articles that are written to help developers.