skka3134

skka3134

email
telegram

hardhat contract development

  1. First, you need a tool for scientific internet access, which costs 6 yuan per month and is very useful.
    https://mxwljsq.com/user/shop
  2. Install WSL, WSL is the Windows Subsystem for Linux, open PowerShell with administrator privileges, and do it all at once.
wsl --install
  1. Install vscode, the tool for writing code, install the plugin, click on the small icon in the lower left corner, and connect to WSL.
    image
    image
  2. Install curl, curl is a command-line file transfer tool.
mkdir folder
cd folder
sudo apt-get install curl
  1. Install nvm, nvm can be used for version control of nodejs.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
  1. Install nodejs, nodejs is the environment for JavaScript.
nvm install --lts

If you are using hardhat or editing solidity files in vscode, you need to install the plugin, the yellow one supports hardhat.

solidity

image
Choose a development tool. The current mainstream development tools are: Remix, Truffle, Hardhat, Foundry.
Remix: The simplest development tool, very suitable for beginners to use, it has a complete GUI function, which can reduce a lot of code.
Truffle: The oldest development tool, compared with Remix, it is installed locally and does not require network support, and it also has a GUI.
Hardhat: It is currently the most popular development framework, also installed locally, without GUI, and more flexible than Truffle.
Foundry: It is an emerging development framework, used for the best flexibility. For beginners, if you only focus on the contract itself and do not need to learn JavaScript, you only need to learn the solidity language.
Steps to use hardhat:

  1. Initialize a project.
npm init
  1. Install hardhat (the old version's interaction and contract deployment methods may have been invalidated).
npm install hardhat
  1. Initialize the hardhat project.
npx hardhat init
  1. Install dependencies.
    @openzeppelin/contracts erc20, erc21 standards, etc.
    @openzeppelin/contracts-upgradeable upgradeable erc20, erc721, etc.
    @chainlink/contracts used for random number requests, contract automation execution, etc.
    dotenv stores important information.
    @nomiclabs/hardhat-ethers used for contract deployment.
    @openzeppelin/hardhat-upgrades used for deploying proxy contracts.
    @openzeppelin/hardhat-defender used for contract security protection.
    @nomiclabs/hardhat-etherscan automatically verifies on the blockchain browser after deploying the contract.
    chai contract testing.
    @openzeppelin/test-helpers openzeppelin team's contract testing.
npm install @openzeppelin/contracts
npm install @openzeppelin/contracts-upgradeable
npm install @chainlink/contracts
npm install dotenv
npm install @nomicfoundation/hardhat-toolbox
npm install @nomiclabs/hardhat-ethers
npm install @openzeppelin/hardhat-upgrades 
npm install @openzeppelin/hardhat-defender
npm install @nomiclabs/hardhat-etherscan 
npm install chai 
npm install @openzeppelin/test-helpers
  1. Create a .env file to store critical information.
MNEMONIC=""
ALCHEMY_API_KEY=""
etherscanKey=""
DEFENDER_TEAM_API_KEY=""
DEFENDER_TEAM_API_SECRET_KEY=""
  1. Modify hardhat.config.js
require('dotenv').config()
require("@nomicfoundation/hardhat-toolbox");
require('@nomiclabs/hardhat-ethers');
require('@openzeppelin/hardhat-upgrades');
require("@openzeppelin/hardhat-defender");
require("@nomiclabs/hardhat-etherscan");
module.exports = {
  solidity: {
    version: '0.8.17',
    settings: {
      optimizer: {
        enabled: true,
        runs: 200,
      },
    },
  },
  networks: {
    sepolia: {
      url: `https://eth-sepolia.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`,
      accounts: [process.env.MNEMONIC]
    },
  },
   etherscan: {
     apiKey: process.env.etherscanKey,
   },
   defender: {
     apiKey: process.env.DEFENDER_TEAM_API_KEY,
     apiSecret: process.env.DEFENDER_TEAM_API_SECRET_KEY,
   },
};
  1. Use the Wizard to develop contracts https://docs.openzeppelin.com/contracts/4.x/wizard
  1. Compile the contract.
npx hardhat compile 
  1. Deploy the contract script (ethers6.0 may not be applicable).
async function main() {
  const [deployer] = await ethers.getSigners();
  console.log("Deploying contracts with the account:", deployer.address);
  const DragonToken = await ethers.deployContract("DragonToken");
  console.log("Token address:", await DragonToken.getAddress());
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
  1. Start the hardhat local network.
npx hardhat node
  1. Deploy to the hardhat local network.
npx hardhat run ./scripts/deploy.js --network localhost 
  1. Deploy to the sepolia testnet.
npx hardhat run ./scripts/deploy.js --network sepolia 
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.