Steps to Set Up
Setting Up the Development Environment
1. Initialize the Project
Create a new directory for your project and initialize it with npm
:
mkdir MySolidityProject
cd MySolidityProject
npm init -y
This will generate a package.json
file for managing dependencies and scripts.
2. Install Solidity and Hardhat Dependencies
Hardhat is a powerful development environment for compiling, deploying, and testing Ethereum smart contracts. Install Hardhat along with ethers.js
for interacting with Ethereum, and the necessary plugins:
npm install --save-dev hardhat @nomiclabs/hardhat-ethers ethers
This command installs:
Hardhat: For development, testing, and deployment.
ethers.js: A library for interacting with Ethereum.
@nomiclabs/hardhat-ethers: A plugin that integrates Hardhat with
ethers.js
.
3. Create the Hardhat Configuration
Run Hardhat’s initialization command to generate the basic configuration and project files:
npx hardhat
You will be prompted to select a task. Choose Create a basic sample project
or Create an advanced project
, depending on your needs. This will generate:
hardhat.config.js
: Your Hardhat configuration file.Sample contract, test, and script files.
4. Set Up the Folder Structure
MySolidityProject/
├── contracts/ # Contains all the Solidity contracts
│ └── MyContract.sol # Example contract
├── scripts/ # Scripts for deployment and contract interaction
│ └── deploy.js # Script for contract deployment
├── test/ # Contains unit tests for smart contracts
│ └── test.js # Example test file
├── artifacts/ # Generated files (compiled contracts, ABIs, etc.)
├── cache/ # Cached files for faster compilation
├── node_modules/ # Installed npm packages
├── hardhat.config.js # Hardhat configuration file
└── package.json # Project dependencies and scripts
5. Creating a Sample Solidity Contract
Inside the contracts/
folder, create a simple Solidity contract, e.g., MyContract.sol
:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyContract {
uint256 public value;
function setValue(uint256 _value) public {
value = _value;
}
function getValue() public view returns (uint256) {
return value;
}
}
6. Creating a Deployment Script
In the scripts/
folder, create a deployment script (deploy.js
):
javascriptCopy codeasync function main() {
const [deployer] = await ethers.getSigners();
console.log("Deploying contracts with the account:", deployer.address);
const MyContract = await ethers.getContractFactory("MyContract");
const myContract = await MyContract.deploy();
console.log("MyContract deployed to:", myContract.address);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
7. Running the Deployment
To compile and deploy your contract locally:
Start a local Ethereum node using Hardhat:
npx hardhat node
Deploy your contract:
npx hardhat run scripts/deploy.js --network localhost
This will deploy your contract to a local test network.
8. Setting Up a Test File
In the test/
folder, create a test script (test.js
) using Hardhat's testing framework and ethers.js
:
const { expect } = require("chai");
describe("MyContract", function () {
it("Should set and get the correct value", async function () {
const MyContract = await ethers.getContractFactory("MyContract");
const myContract = await MyContract.deploy();
await myContract.deployed();
// Set a value
await myContract.setValue(42);
// Test the value
expect(await myContract.getValue()).to.equal(42);
});
});
9. Running the Tests
To run your tests:
npx hardhat test
10. Hardhat Network Configuration (Optional for Testnets)
To deploy on a real test network , configure your hardhat.config.js
:
require("@nomiclabs/hardhat-ethers");
module.exports = {
solidity: "0.8.0",
networks: {
phron: {
url: `https://testnet.phron.ai`,
},
},
};