Code Monkey home page Code Monkey logo

hardhat-demo's Introduction

初始化项目

yarn init

yarn add hardhat

yarn hardhat

yarn hardhat run scripts/deploy.js

测试网 sepolia

  • RPC_URL chainId 在这里找:Sepolia RPC URL List

  • 获取不到环境变量???需要引入dotenvrequire('dotenv').config()

  • 运行 yarn hardhat run scripts/deploy.js --network sepolia 得到: Lock with 0.001ETH and unlock timestamp 1689089487 deployed to 0x285F56139C6D76309067991010716557dee76A34

  • https://sepolia.etherscan.io/ 可通过合约地址查询

  • .sol 中使用 console.log

        import "hardhat/console.sol";
        console.log("block timestamp is %o", block.timestamp);
  • 部署:

        // 方式一:
        hre.ethers.deployContract()
        // 方式一:
        const SimpleStorage=await ethers.getContractFactory('SimpleStorage')
        const simpleStorage=await SimpleStorage.deploy();

验证合约

  • 安装hardhat-verifyyarn add --dev @nomicfoundation/hardhat-verify

自定义hardhat任务

  • 新建 task 文件,然后在 hardhat.config.js中引入
  • yarn hardhat block-number 或者 yarn hardhat block-number --network sepolia 运行任务

类似 ganache 的本地节点

  • yarn hardhat node
    1. 本地节点不会在每次运行是重置
    2. 比测试网快的多
  • 然后再运行:yarn hardhat run ./scripts/deploy2.js --network localhost

hardhat控制台

  • yarn hardhat node 然后 yarn hardhat console --network localhost
  • 然后直接运行deploy.js中语句,不需要导入方法(但是可能回报语法错误,不好用)

测试

  • 清除缓存:yarn hardhat clean
  • 在test文件夹下编写测试文件,运行yarn hardhat test
  • 指定运行测试用例:
    1. yarn hardhat test --grep 更新后
    2. it.only(...)

hardhat-gas-reporter

  • 测试花费多少gas
  • yarn add hardhat-gas-reporter --dev,然后在hardhat.config.js中配置
  • 运行yarn hardhat test即可
  • coinmarketcap在这里获取:https://pro.coinmarketcap.com/

solidity-coverage

  • 查测试用例覆盖率yarn hardhat coverage

note

6-6 连 sepolia 会报错 error:: ConnectTimeoutError: Connect Timeout Error

hardhat-demo's People

Contributors

fulinpeng avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.