当前位置: 首页 > news >正文

做网站所具备的的条件邢台网站推广

做网站所具备的的条件,邢台网站推广,北京列表网,西安网站制作服务商hardhat学习笔记会不定时填充内容。 初始化项目 yarn init 安装hardhat依赖 yarn add --dev hardhat 初始化 Hardhat yarn hardhat 代码格式化 yarn add --dev prettier prettier-plugin-solidity 项目中增加.prettierrc 与 .prettierignore 配置文件统一格式&#xff0…

hardhat学习笔记会不定时填充内容。

初始化项目 

yarn init

安装hardhat依赖 

yarn add --dev hardhat

 初始化 Hardhat

yarn hardhat

 代码格式化

yarn add --dev prettier prettier-plugin-solidity

  项目中增加.prettierrc 与 .prettierignore 配置文件统一格式(根据项目自行配制即可)

//.prettierrc
{"$schema": "https://json.schemastore.org/prettierrc","semi": false,"tabWidth": 2,"singleQuote": true,"printWidth": 100,"trailingComma": "none"
}
//.prettierrc
node_modules
package.json
img
artifacts
cache
coverage
.env
.*
README.md
coverage.json

编译合约

yarn hardhat compile

设置环境按量

安装依赖

yarn add --dev dotenv

在 .env 文件中,我们可以设置环境变量

RINKEBY_RPC_URL=https://rinkeby.infura.io/v3/key
RINKEBY_PRIVATE_KEY=0xkey
ETHERSCAN_API_KEY=key
COINMARKETCAP_API_KEY=key

  hardhat.config.js 读取环境变量

const INFURA_API_KEY = process.env.INFURA_API_KEY || "";
const RINKEBY_PRIVATE_KEY = process.env.RINKEBY_PRIVATE_KEY || "";
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY || "key";
const COINMARKETCAP_API_KEY = process.env.COINMARKETCAP_API_KEY || "key";

配置网络环境

现配置本地网络环境,测试账号还没申请下来

运行 yarn hardhat node 命令,启动一个本地可持续的网络

yarn hardhat node

配置 hardhat.config.js 

const config: HardhatUserConfig = {solidity: '0.8.24',defaultNetwork: 'hardhat',networks: {localhost: {url: 'http://localhost:8545'}// rinkeby: {//   url: `https://rinkeby.infura.io/v3/${INFURA_API_KEY}`,//   accounts: [`0x${RINKEBY_PRIVATE_KEY}`],// },}
}

部署脚本及关联合约

两个例子,一个hardhat自带的,一个网上找的,分别记录下。

deploy.ts

import { ethers } from 'hardhat'async function main() {const currentTimestampInSeconds = Math.round(Date.now() / 1000)const unlockTime = currentTimestampInSeconds + 60const lockedAmount = ethers.parseEther('0.001')const lock = await ethers.deployContract('Lock', [unlockTime], {value: lockedAmount})await lock.waitForDeployment()console.log(`Lock with ${ethers.formatEther(lockedAmount)}ETH and unlock timestamp ${unlockTime} deployed to ${lock.target}`)//简单合约 --------------------------------//获取合约工厂const simpleStorageFactory = await ethers.getContractFactory('SimpleStorage')//部署const simpleContract = await simpleStorageFactory.deploy()//等待部署完成await simpleContract.waitForDeployment()//输出合约地址simpleContract.getAddress().then((address) => console.log('SimpleStorage deployed to:address:', address))//获取当前值const value = await simpleContract.retrieve()console.log('SimpleStorage value:', value.toString())//设置值await simpleContract.store(13)//获取当前值const newValue = await simpleContract.retrieve()console.log('SimpleStorage new value:', newValue.toString())
}// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().then(() => process.exit(0)).catch((error) => {console.error(error)process.exitCode = 1})

Lock合约(Lock.sol)

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;// Uncomment this line to use console.log
// import "hardhat/console.sol";contract Lock {uint public unlockTime;address payable public owner;event Withdrawal(uint amount, uint when);constructor(uint _unlockTime) payable {require(block.timestamp < _unlockTime,"Unlock time should be in the future");unlockTime = _unlockTime;owner = payable(msg.sender);}function withdraw() public {// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);require(block.timestamp >= unlockTime, "You can't withdraw yet");require(msg.sender == owner, "You aren't the owner");emit Withdrawal(address(this).balance, block.timestamp);owner.transfer(address(this).balance);}
}

SimpleStorage合约(SimpleStorage.sol)

// SPDX-License-Identifier: UNKNOWN (this is a comment)pragma solidity ^0.8.24;contract SimpleStorage {uint256 favoriteNumber;bool favoriteBool;struct People {uint256 favoriteNumber;string name;}People public person = People({favoriteNumber: 2, name: 'Patrick'});People[] public people;mapping(string => uint256) public nameToFavoriteNumber;///存储值,是一个非view函数,会改变合约的状态function store(uint256 _favoriteNumber) public {favoriteNumber = _favoriteNumber;}///返回值,是一个view函数,不会改变合约的状态function retrieve() public view returns (uint256) {return favoriteNumber;}///增加人员function addPerson(string memory _name, uint256 _favoriteNumber) public {people.push(People(_favoriteNumber, _name));nameToFavoriteNumber[_name] = _favoriteNumber;}///返回人员function getPerson() public view returns (People[] memory) {return people;}
}

运行脚本

yarn hardhat run scripts/deploy.ts

http://www.yayakq.cn/news/332053/

相关文章:

  • 电子商务网站建设目的和意义台州网站制作建设
  • 做任务得佣金的网站搜索wordpress博客
  • 本地做的网站如何映射出去网站建设对图片有哪些要求
  • 定西网站建设公司网页设计软件html
  • 网站开发有哪些方向做网站亏本
  • 狮城app更多网站政务网站建设方案
  • 广告设计与制作网站西安到北京火车票查询
  • 江苏省城市建设信用手册网站易名域名交易
  • 做网站 绍兴南宁网站制作超薄网络
  • 企业网站的建设流程虚拟主机能干什么
  • 网站标题加后缀电线电缆技术支持中山网站建设
  • 网站的备案在哪备案吗建设网站的价值
  • 南京网站房地产福州专业网站设计公司
  • 成都三合一网站建设周期购那个网站做的比较好
  • 外国炫酷网站网址上海网站建设网络推广
  • 郑州网站哪家好五常网站建设
  • 图片网站建站系统郑州网站推广排名公司
  • 衡水做网站建设用手机如何制作简历
  • 免费企业网站源码室内设计网络课程
  • 北京网站优化推广效果vue做的网站有什么
  • 做短租有哪些网站自己设计t恤的平台
  • 合肥建设云个人服务平台昆明网站建设优化技术
  • 手机网站微信链接怎么做的国外有哪些网站可以做电商
  • 北京驾校网站建设英德市城乡建设局网站
  • 大型 交友 网站 建设 公司网站建设列表网
  • 网站用户粘度汕头外发加工网
  • flash网站引导页个人网上银行登录
  • 网站开发形象设计要求网站建设服务器环境配置
  • 哪个门户网站做推广好wordpress文章编辑软件
  • 广西网站建设企业如何做微信ppt模板下载网站