Basic Setup to Write & Test a Smart Contract

Basic Setup to Write & Test a Smart Contract

How to setup your dev environment in 3 steps

ยท

3 min read

This guide is a quick recap (plus few additions) of the dev environment setup from 30DaysOfWeb30 by WBW3. If you are about to start the curriculum this article should help you initialize your environment and find resources to troubleshoot any issue.

Let's start. Open your IDE (i.e. VSCode) and run the terminal.

Step 1 : nvm

nvm (node version manager) allows you to quickly install and use different versions of node via the command line.

Install or update nvm in your machine by running the script, following cURL or Wget command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Verify that nvm has been installed:command -v nvm
You should get nvm

Running into any error? Check out the complete installation guide for nvm, including troubleshooting, available here. Or alternatively you can use nodenv, learn more here.

To continue to the next step you need nvm installed.

Step 2 : Node.js and npm

Node.js is a runtime environment that executes JavaScript outside the browser, enabling developers to build full-stack JS apps

npm (node package manager) is the command-line interface to Node.js packages. Read more here

Install Node.js and npm by running the following commands in your terminal:

nvm install --lts
nvm use --lts

Verify that both node and npm have been installed:nvm current
You should get the version of node (example: v16.17.0).

You can also verify the versions installed with:

nodenpm
node -vnpm -v

๐Ÿ“Œ Create a Node.js project
To add basic project info in your packages.json file, run the following in terminal. You can continue by clicking enter and adjust them later in the file.

npm init

Step 3 : Hardhat

Hardhat is an Ethereum development environment. It facilitates performing frequent tasks, such as running tests, automatically checking code for mistakes or interacting with a smart contract.

Install hardhat with

npm install --save-dev hardhat

Verify hardhat is installed and version: npm hardhat -v

๐Ÿ“Œ Create a Hardhat project
Once Hardhat is installed, run this command and follow the instructions (you can simply select Create a javascript project and press enter for all the other prompts):

npx hardhat

Now your project folder should look something like this:

|-- contracts
|-- node_modules
|-- scripts
|-- tests
 .gitignore
 hardhat-config.js
 package-lock.json
 package.json
 README.md

And you're ready to go and write your smart contract under the contracts folder.

Testing

Anytime you want to test your smart contract, you'll first compile it by running

npx hardhat compile

and then test out the script to make sure it's working as expected

npm run script

Let me know how the setup went for you and if you found this short guide useful.

ย