Welcome to Solidity 101, a comprehensive guide to understanding and developing smart contracts using the Solidity programming language. This project utilizes the Hardhat framework and supports Solidity version 0.8.28.
- Getting Started
- Installation
- Creating a Smart Contract
- Testing Smart Contracts
- Deploying Contracts
- Conclusion
This project is tailored towards developers who wish to learn about the basics of Solidity and smart contract development. Ensure you have the following installed:
- Node.js (version 12.x or newer)
- npm (Node package manager)
To set up the development environment, follow these steps:
- Clone the repository:
git clone https://github.com/Socialstranger/solidity101.git cd solidity101 - Install the required dependencies:
npm install
- Install Hardhat:
npx hardhat
- Create a new contract file in the
contractsdirectory:pragma solidity ^0.8.28; contract MyContract { string public greeting; constructor(string memory _greeting) { greeting = _greeting; } }
Testing is crucial. In the test directory, create a new test file:
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("MyContract", function () {
it("Should return the correct greeting", async function () {
const MyContract = await ethers.getContractFactory("MyContract");
const myContract = await MyContract.deploy("Hello, Solidity!");
await myContract.deployed();
expect(await myContract.greeting()).to.equal("Hello, Solidity!");
});
});To deploy your contract, create a deployment script in the scripts folder:
async function main() {
const MyContract = await ethers.getContractFactory("MyContract");
const myContract = await MyContract.deploy("Hello, Solidity!");
await myContract.deployed();
console.log(`Contract deployed to: ${myContract.address}`);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});This project aims to provide a basic understanding of Solidity, including smart contract creation, testing, and deployment using Hardhat. For more advanced topics, consider exploring further resources, tutorials, and the official Solidity documentation. Happy coding!