This is a tutorial project for a Solidity language. It includes a sample contract and a test suite.
- Node.js and npm are required to run this project.
$ node -v v18.15.0
Install the dependencies with npm install
.
Source: Hardhat, ITU Blockchain
$ mkdir solidity-tutorial
$ cd solidity-tutorial
$ npm init -y
$ npm install --save-dev hardhat
$ npx hardhat
# select "Create a TypeScript project"
$ npm install --save-dev @nomicfoundation/hardhat-toolbox
$ npm install @openzeppelin/contracts
$ npm install --save-dev typescript @types/node @types/mocha @types/chai
$ npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers
$ npm install --save-dev ts-node
$ npx hardhat compile
$ npx hardhat test
- Create a new file in the
scripts
folder calleddeploy.ts
. - Add the following code to the file:
import { ethers } from "hardhat";
async function main() {
const [deployer] = await ethers.getSigners();
const lockFactory = await ethers.getContractFactory("Lock");
// const lock = await lockFactory.deploy(1000, { value: ethers.utils.parseEther("0.001") });
const lock = await lockFactory.deploy("0x0000000");
console.log(`Lock deployed to ${lock.address}`);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
- Run the following command to deploy the contract:
# Deploying to Fuji Testnet
$ npm hardhat run scripts/deploy.ts --network fuji
- Run the following command to verify the contract:
# Verifying the contract on Fuji Testnet
$ npm hardhat verify --network fuji DEPLOYED_CONTRACT_ADDRESS "0x0000000"
Run the test suite with npm test
.
- 11-struct-enum.sol - added a struct and an enum.
- 11-struct-enum.sol - added modifier.
modifier: it is used to check the condition before executing the function. - 11-struc-enum.sol - added event.
event: it is used to log the event in the blockchain. - 12-bank-send-ether.sol - send ether to the contract.
- transfer: it is used to send ether and throw an error.
transfer(); // revert if failed
- send: it is used send ether and return a boolean value.
bool success = send();
- call: it is used to send ether with data.
(bool success,) = to.call{value: amount, gas: gas}(data);
- 12-bank-send-ether.sol - receive ether from the contract.
- receive: it is used to receive ether.
receive() external payable {}
- fallback: it is used to receive ether with data.
Note: Falback is called when the receive function is not defined or when the data is sent with the transaction.fallback() external payable {}
- 13-error.sol - added custom error.
- 14-library.sol - added library.
- 15-storage-memory-calldata.sol - added storage, memory and calldata.
- 16-inheritance-override.sol, 17-inheritance-super.sol, 18-inheritance-import-ownable.sol - added inheritance.
- 19-interaction.sol - added interaction with other contracts.
- 20-interface.sol, 21-interface-event.sol - added interface.
- Hardhat environment added.
- ERC20 token and lock contract added.
- Deployed the contract on Fuji Testnet.
- Go native binding added.