7.区块链系列之hardhat框架部署合约

编程入门 行业动态 更新时间:2024-10-11 21:29:51

7.<a href=https://www.elefans.com/category/jswz/34/1769670.html style=区块链系列之hardhat框架部署合约"/>

7.区块链系列之hardhat框架部署合约

先前讲解的本地部署只能合约的方式编码较多,现在我们介绍目前比较流行的智能合约框架hardhat

1.环境准备
yarn init
yarn add --dev hardhat
yarn hardhat
npm install --save-dev @nomicfoundation/hardhat-toolbox
2. 新建并编译SimpleStorage.sol
  • 在hardhat框架conracts目录下新建SimpleStorage.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.8;contract SimpleStorage {uint256 favoriteNumber;struct People {uint256 favoriteNumber;string name;}// uint256[] public anArray;People[] public people;mapping(string => uint256) public nameToFavoriteNumber;function store(uint256 _favoriteNumber) public {favoriteNumber = _favoriteNumber;}function retrieve() public view returns (uint256) {return favoriteNumber;}function addPerson(string memory _name, uint256 _favoriteNumber) public {people.push(People(_favoriteNumber, _name));nameToFavoriteNumber[_name] = _favoriteNumber;}
}
  • 修改配置文件solidity版本为0.8.8
module.exports = {solidity: "0.8.8"
};
  • 开始编译yarn hardhat compile
(base) PS D:\blockchain\blockchain\hardhat-simple-storage-fcc> yarn hardhat compile
yarn run v1.22.19
$ D:\blockchain\blockchain\hardhat-simple-storage-fcc\node_modules\.bin\hardhat compile
Downloading compiler 0.8.8
Compiled 1 Solidity file successfully
Done in 5.70s.
  • 安装代码美化依赖
yarn add --dev prettier prettier-plugin-solidity

新建.prettierrc文件

{"tabWidth": 4,"useTabs": false,"semi": false,"singleQuote": false
}

新建.prettierignore文件

node_modules
package.json
img
artifacts
cache
coverage
.env
.*
README.md
coverage.json
3.修改deploy.js并部署

将以下代码覆盖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}`)
}// main
main().then(() => process.exit(0)).catch((error) => {console.error(error)process.exit(1)
})

执行 yarn hardhat run scripts/deploy.js部署合约

(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
Done in 3.67s.

在hardhat.config.js中,我们添加defaultNetwork: "hardhat",这是未指定网络时默认的配置

module.exports = {defaultNetwork: "hardhat",...
};

指定具体网络为hardhat

(base) PS D:\blockchain\blockchain\hardhat-simple-storage-fcc> yarn hardhat run scripts/deploy.js --network hardhat
yarn run v1.22.19
$ D:\blockchain\blockchain\hardhat-simple-storage-fcc\node_modules\.bin\hardhat run scripts/deploy.js --network hardhat
Deploying contract...
Deployed contract to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Done in 3.51s.
4. 部署至GOERLI测试网络
  • 新建.env文件

配置如下内容为测试网信息,详见之前文章

GOERLI_RPC_URL=XXXXX
PRIVATE_KEY=XXXXXXX

覆盖deploy.js内容如下

require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config()const GOERLI_RPC_URL = process.env.GOERLI_RPC_URL
const PRIVATE_KEY = process.env.PRIVATE_KEY/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {defaultNetwork: "hardhat",networks: {hardhat: {},goerli: {url: GOERLI_RPC_URL,accounts: [PRIVATE_KEY],// : 5}},solidity: "0.8.8"
};
// 安装依赖
npm install dotenv --save
(base) PS D:\blockchain\blockchain\hardhat-simple-storage-fcc> yarn hardhat run scripts/deploy.js --network goerli
yarn run v1.22.19
$ D:\blockchain\blockchain\hardhat-simple-storage-fcc\node_modules\.bin\hardhat run scripts/deploy.js --network goerli
Deploying contract...
Deployed contract to: 0x4fC6FAe2C80adFd7beF5F6AeF2d8E59F2f3e1265
Done in 30.15s.

访问该地址发现部署成功


欢迎关注公众号算法小生获取完整代码

更多推荐

7.区块链系列之hardhat框架部署合约

本文发布于:2024-02-27 09:05:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1705962.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区块   合约   框架   系列之   hardhat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!