8.区块链系列之hardhat框架部署合约(二)

这篇具有很好参考价值的文章主要介绍了8.区块链系列之hardhat框架部署合约(二)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

现在我们来实践hardhat部署合约中的其他更多技术要点

1. 代码方式验证合约
  • 注册https://etherscan.io/, 如下图添加拷贝API_KEY

yarn hardhat 版本为2.18,区块链,区块链

  • 在.env文件中新增ETHERSCAN_API_KEY
ETHERSCAN_API_KEY=API_KEY【刚才注册的key】
  • hardhat.config.js中新增配置
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY

module.exports = {
  etherscan: {
    apiKey: ETHERSCAN_API_KEY
  }
};
  • 覆盖deploy.js
// imports
const { ethers, run, network } = require("hardhat")

// async main
async function main() {
  const SimpleStorageFactory = await ethers.getContractFactory("SimpleStorage")
  console.log("Deploying contract...")
  const simpleStorage = await SimpleStorageFactory.deploy()
  await simpleStorage.deployed()
  console.log(`Deployed contract to: ${simpleStorage.address}`)
   if (network.config.chainId === 5 && process.env.ETHERSCAN_API_KEY) {
    console.log("Waiting for block confirmations...")
    await simpleStorage.deployTransaction.wait(6)
    await verify(simpleStorage.address, [])
  }
}

const verify = async (contractAddress, args) => {
  console.log("Verifying contract...")
  try {
    await run("verify:verify", {
      address: contractAddress,
      constructorArguments: args,
    })
  } catch (e) {
    if (e.message.toLowerCase().includes("already verified")) {
      console.log("Already Verified!")
    } else {
      console.log(e)
    }
  }
}

// main
main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error)
    process.exit(1)
})
  • 验证合约

如果用的使用了clash代理的话开启Tun模式,否则可能会报Connect Timeout Error

yarn hardhat run scripts/deploy.js --network goerli
或
yarn hardhat verify --network goerli 0x2e3C64c769DAbAC7587dEa70cA23164EEE3d9596 --show-stack-traces

yarn run v1.22.19
$ D:\blockchain\blockchain\hardhat-simple-storage-fcc\node_modules\.bin\hardhat verify --network goerli 0x2e3C64c769DAbAC7587dEa70cA23164EEE3d9596 --show-stack-traces
Nothing to compile
Successfully submitted source code for contract
contracts/SimpleStorage.sol:SimpleStorage at 0x2e3C64c769DAbAC7587dEa70cA23164EEE3d9596
for verification on the block explorer. Waiting for verification result...

Successfully verified contract SimpleStorage on Etherscan.
https://goerli.etherscan.io/address/0x2e3C64c769DAbAC7587dEa70cA23164EEE3d9596#code
Done in 30.38s.
2. 通过hardhat与合约交互

在deploy.js的main方法中新增如下代码

async function main() {
  ......
  const currentValue = await simpleStorage.retrieve()
  console.log(`Current Value is: ${currentValue}`)

  // Update the current value
  const transactionResponse = await simpleStorage.store(7)
  await transactionResponse.wait(1)
  const updatedValue = await simpleStorage.retrieve()
  console.log(`Updated Value is: ${updatedValue}`)
}

运行及结果展示:

(base) PS D:\blockchain\blockchain\hardhat-simple-storage-fcc> yarn hardhat run scripts/deploy.js                          
yarn run v1.22.19
$ D:\blockchain\blockchain\hardhat-simple-storage-fcc\node_modules\.bin\hardhat run scripts/deploy.js
Deploying contract...
Deployed contract to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Current Value is: 0
Updated Value is: 7
Done in 3.62s.
3.自定义hardhat任务
  • 新建tasks/block-number.js
const { task } = require("hardhat/config")

task("block-number", "Prints the current block number").setAction(
  async (taskArgs, hre) => {
    const blockNumber = await hre.ethers.provider.getBlockNumber()
    console.log(`Current block number: ${blockNumber}`)
  }
)

module.exports = {}
  • 在hardhat.config.js中新增配置
require("./tasks/block-number")
  • 运行yarn hardhat命令
// 可以看到出现了任务
AVAILABLE TASKS:
  block-number          Prints the current block number
  • 运行yarn hardhat block-number --network goerli查看最新区块号
(base) PS D:\blockchain\blockchain\hardhat-simple-storage-fcc> yarn hardhat block-number --network goerli
yarn run v1.22.19
$ D:\blockchain\blockchain\hardhat-simple-storage-fcc\node_modules\.bin\hardhat block-number --network goerli
Current block number: 7779125
Done in 14.01s.
4.hardhat本地节点
  • 运行yarn hardhat node查看本地节点信息
(base) PS D:\blockchain\blockchain\hardhat-simple-storage-fcc> yarn hardhat node
yarn run v1.22.19
$ D:\blockchain\blockchain\hardhat-simple-storage-fcc\node_modules\.bin\hardhat node
Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/

Accounts
========

WARNING: These accounts, and their private keys, are publicly known.
Any funds sent to them on Mainnet or any other live network WILL BE LOST.

Account #0: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 (10000 ETH)
Private Key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

Account #1: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 (10000 ETH)
Private Key: 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
  • 在hardhat.config.js中新增网络配置
module.exports = {
  networks: {
    localhost: {
      url: "http://127.0.0.1:8545/",
      chainId: 31337
    }
  }
};
  • 新启动shell窗口并运行yarn hardhat run scripts/deploy.js --network localhost
(base) PS D:\blockchain\blockchain\hardhat-simple-storage-fcc> yarn hardhat run scripts/deploy.js --network localhost       
yarn run v1.22.19
$ D:\blockchain\blockchain\hardhat-simple-storage-fcc\node_modules\.bin\hardhat run scripts/deploy.js --network localhost
Deploying contract...
Deployed contract to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Current Value is: 0
Updated Value is: 7
  • 切换到node窗口可看到本地交易信息

yarn hardhat 版本为2.18,区块链,区块链

  • 在新启动的shell窗口中运行yarn hardhat console --network localhost我们即可在控制台与合约交互

yarn hardhat 版本为2.18,区块链,区块链

欢迎关注公众号算法小生或沈健的技术博客shenjian.online文章来源地址https://www.toymoban.com/news/detail-816327.html

到了这里,关于8.区块链系列之hardhat框架部署合约(二)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用Hardhat测试智能合约

    Hardhat是一个编译、部署、测试和调试以太坊应用的开发环境。它可以帮助开发人员管理和自动化构建智能合约和dApps过程中固有的重复性任务,并围绕这一工作流程轻松引入更多功能。这意味着hardhat在最核心的地方是编译、运行和测试智能合约。 Hardhat内置了Hardhat网络,这是

    2024年02月04日
    浏览(49)
  • 智能合约开发笔记-hardhat入门

    Hardhat是一个编译、部署、测试和调试以太坊应用的开发环境。 先安装nodejs环境; 然后打开命令行执行以下命令, 在项目目录pj_220509下安装hardhat环境: pj_220509目录下, 执行命令 npx hardhat  然后按提示安装相关的nodejs包,如下;完成安装; 装完后呀,可以在本地启动一个区块

    2023年04月11日
    浏览(25)
  • 基于Hardhat编写合约测试用例

    为智能合约编写自动化测试至关重要,毕竟写智能合约多多少少都会跟用户资金挂钩。 这里假设自己正在开发一个NFT交易平台,这个平台可以让用户售卖自己的NFT,包括ERC721和ERC1155,并且用户可以指定购买者需要支付指定的 ERC20 Token 购买。 我们先确定自己的测试功能和目标

    2024年02月02日
    浏览(29)
  • 区块链智能合约测试框架Foundry技术指南

    在区块链开发领域,智能合约的安全性和可靠性至关重要。鉴于区块链的不可变性,智能合约中的任何错误都可能导致不可逆转的后果,包括重大的财务损失。这凸显了彻底测试的关键重要性。Foundry 是一种 Solidity 测试框架,在这一领域中成为一个强大的工具,为开发人员提

    2024年02月03日
    浏览(37)
  • 如何使用hardhat进行合约uups模式升级

    id:BSN_2021 公众号:BSN研习社 背景: 在开发或维护solidity语言的智能合约时,经常会因为业务逻辑变动而变动合约内的逻辑,这就要考虑在不影响以前智能合约中已上链的数据的同时,修改或扩展新的业务逻辑,所以合约第一次开发时就需要考虑其本身支持可升级功能 目的:

    2024年02月16日
    浏览(23)
  • 使用hardhat验证智能合约(goeril测试网)

    使用openzeppelin写了个简单的Erc721合约,成功部署到goerli测试网,但是在验证的时候一直报错:

    2024年02月11日
    浏览(25)
  • 区块链DAPP开发 以太坊智能合约框架有哪些

    Truffle 是一个在以太坊进行 DApp 开发的世界级开发环境、测试框架。 使用 Truffle 开发有一以下优点: 内置智能合约编译,链接,部署和二进制(文件)管理。 可快速开发自动化智能合约测试框架。 可脚本化、可扩展的部署和迁移框架。 可管理多个不同的以太坊网络,可部署

    2024年02月02日
    浏览(34)
  • 基于Hardhat和Openzeppelin开发可升级合约(二)

    在本章我将开始介绍和演示 基于 Openzeppelin 的可升级合约解决方案 根据设计,智能合约是不可变的。但随着新的客户需求和产品设计的升级迭代,合约也需要升级。 Openzeppelin 的基础可升级合约解决方案是将合约数据与逻辑分离。 代理合约(Proxy) 负责转发交易到逻辑合约,

    2024年01月19日
    浏览(28)
  • 区块链java开发智能合约nf(部署第一个NFT智能合约)

    手把手教你区块链java开发智能合约nft-第二篇(部署第一个NFT智能合约) 刚搞区块链开发真的是太累了,自己摸石头过河,动不动就报错,网上搜索错误,一律看不出什么问题,或者报错的信息太少,问同事同事不鸟,问领导,领导也烦,无奈,对于英文不好的我,只能被迫

    2024年02月12日
    浏览(41)
  • 区块链合约安全系列(三):如何认识及预防公链合约中的自毁攻击

    id:BSN_2021 公众号:BSN 研习社 作者:红枣科技张雪良 背景:由于公链环境下所有的信息都是共享的,智能合约相当于是完全透明化,任何人都可以调用,外加一些利益的驱动,导致引发了很多hacker的攻击。其中self destruct攻击也是常见的攻击方式之一。 目标:将目标合约瘫痪

    2024年02月01日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包