Your first smart contract
In this tutorial, you'll learn to create your first smart contract with Hardhat โ a full-featured development environment for contract compilation, deployment and verification.
In this tutorial we'll guide you:
Setting up your Node.js environment for U2U smart contract development
Creating and configuring a Hardhat project
The basics of a Solidity smart contract that implements a time-locked wallet
Writing automated tests for your contract using Hardhat
...
You should get familiar to:
Write code in Javascript
Operate a terminal
Use git
Understand the basics of how smart contracts work
Initialization
First, weโll need to create a folder for our project. Navigate to your command line and type:
Create an empty Node.js project:
Add Hardhat as a dependency to the project:
Then use Hardhat to initialize your project:
Then it will appear as follow, please select Create a Typescript project:
It will ask more questions, just follow them and finish setting up your sample project, below are example:
Hardhat has scaffolded a project contains Lock
contract. Our sample project should contain these folders and files:
These are the default paths for a Hardhat project.
contracts/
is where the source files for your contracts should be.test/
is where your tests should go.scripts/
is where simple automation scripts go.
Write your first smart contract
Open up contracts
folder, you'll see sample contract file Lock.sol
that Hardhat has initialized:
Now let's take a glance at Lock.sol
:
This Solidity smart contract is called "Lock" and is designed to hold and manage funds until a specified unlock time has passed. Let's break down the key components and functionality of this contract:
State Variables:
uint public unlockTime
: This variable stores the timestamp when the contract will allow funds to be withdrawn. It's publicly accessible, so anyone can check when the funds will become available.address payable public owner
: This variable holds the Ethereum address of the owner of the contract, who has the authority to withdraw funds.
Events:
event Withdrawal(uint amount, uint when)
: This event is emitted when a withdrawal is made. It logs the amount withdrawn and the timestamp at which the withdrawal occurred.
Constructor:
constructor(uint _unlockTime) payable
: This constructor is executed when the contract is deployed. It takes a single argument,_unlockTime
, which represents the timestamp in the future when the funds can be withdrawn. Therequire
statement ensures that the unlock time is indeed in the future. Themsg.sender
is set as the owner of the contract, and the constructor can receive an initial amount of Ether.
Function -
withdraw
:function withdraw() public
: This function allows the owner to withdraw the funds held in the contract. It enforces the following conditions:The current block timestamp (
block.timestamp
) must be greater than or equal to theunlockTime
, ensuring that funds can only be withdrawn after the specified unlock time.The sender (i.e.,
msg.sender
) must be the owner of the contract.
If these conditions are met, the function emits the
Withdrawal
event to log the withdrawal details, and it transfers the entire balance of the contract to the owner's address usingowner.transfer(address(this).balance)
.
Additionally, there is a commented-out line (console.log
) that can be used for debugging purposes when working with a development environment like Hardhat. It's used to print information to the console but is typically commented out in production code.
What's next?
Finally you have your own smart contract. The next step is to test it carefully before deploy it to blockchain. Please refer to next section at Test your smart contract.
Last updated