Solidity Project Tutorial

Build Status

This is a tutorial project for a Solidity language. It includes a sample contract and a test suite.

Source Code

Installation

  1. Node.js and npm are required to run this project.
    $ node -v
    v18.15.0

Install the dependencies with npm install.

Steps for creating a project

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

Deploying the contract

  1. Create a new file in the scripts folder called deploy.ts.
  2. 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);
  });
  1. Run the following command to deploy the contract:
# Deploying to Fuji Testnet
$ npm hardhat run scripts/deploy.ts --network fuji
  1. Run the following command to verify the contract:
# Verifying the contract on Fuji Testnet
$ npm hardhat verify --network fuji DEPLOYED_CONTRACT_ADDRESS "0x0000000"
  1. https://testnet.snowtrace.io/contract/0x0000000

Testing

Run the test suite with npm test.

Changes

  1. 11-struct-enum.sol - added a struct and an enum.
  2. 11-struct-enum.sol - added modifier.
    modifier: it is used to check the condition before executing the function.
  3. 11-struc-enum.sol - added event.
    event: it is used to log the event in the blockchain.
  4. 12-bank-send-ether.sol - send ether to the contract.
    1. transfer: it is used to send ether and throw an error.
     transfer(); // revert if failed
    1. send: it is used send ether and return a boolean value.
     bool success = send();
    1. call: it is used to send ether with data.
     (bool success,) = to.call{value: amount, gas: gas}(data);
  5. 12-bank-send-ether.sol - receive ether from the contract.
    1. receive: it is used to receive ether.
     receive() external payable {}
    1. fallback: it is used to receive ether with data.
     fallback() external payable {}
    Note: Falback is called when the receive function is not defined or when the data is sent with the transaction.
  6. 13-error.sol - added custom error.
  7. 14-library.sol - added library.
  8. 15-storage-memory-calldata.sol - added storage, memory and calldata.
  9. 16-inheritance-override.sol, 17-inheritance-super.sol, 18-inheritance-import-ownable.sol - added inheritance.
  10. 19-interaction.sol - added interaction with other contracts.
  11. 20-interface.sol, 21-interface-event.sol - added interface.
  12. Hardhat environment added.
  13. ERC20 token and lock contract added.
  14. Deployed the contract on Fuji Testnet.
  15. Go native binding added.