当前位置:网站首页>Develop scalable contracts based on hardhat and openzeppelin (I)
Develop scalable contracts based on hardhat and openzeppelin (I)
2022-07-02 11:45:00 【Ni xiansen_】
be based on Hardhat and Openzeppelin Develop scalable contracts ( One )
This chapter mainly demonstrates how to use Hardhat Framework for rapid development of common contracts ( Not upgradeable ), Deploy on local node , And interact with the contract through the console . I hope this article can help readers get started quickly Hardhat frame
The upgradeable contract will begin in the next chapter
Third party Library
- Hardhat About Hardhat Installation and introduction of , Refer to another article of mine << Introduce the smart contract development framework -Hardhat>>
initialization
Create directory
* mkdir hardhat_upgradeable
* cd hardhat_upgradeable
* npx hardhat
888 888 888 888 888
888 888 888 888 888
888 888 888 888 888
8888888888 8888b. 888d888 .d88888 88888b. 8888b. 888888
888 888 "88b 888P" d88" 888 888 "88b "88b 888 888 888 .d888888 888 888 888 888 888 .d888888 888 888 888 888 888 888 Y88b 888 888 888 888 888 Y88b. 888 888 "Y888888 888 "Y88888 888 888 "Y888888 "Y888
Welcome to Hardhat v2.9.9
? What do you want to do? …
* Create a basic sample project
Create an advanced sample project
Create an advanced sample project that uses TypeScript
Create an empty hardhat.config.js
Quit
# Select first , Directly enter
? Hardhat project root: › /Users/lyon/Desktop/Solidity/hardhat_upgradeable
# Confirm the project directory , Or just enter
? Do you want to add a .gitignore? (Y/n) › y
# Prompt whether to create git Ignore files , Default y(yes), Directly enter
? Do you want to install this sample project's dependencies with npm (hardhat @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers)? (Y/n) › y
# Prompt whether you want to pass npm Installation dependency , Default y(yes), Or just enter
wait for npm Pull dependency to complete 
Project default directory

Non escalable contract
I hope to make readers familiar with based on Hardhat Development process , And you can write Scalable contracts Make a comparison when
Create a contract
# Delete the catalog demo contract file Greeter.sol
* rm contracts/Greeter.sol
# establish MyContract.sol
* touch contracts/MyContract.sol
Add the following contract code
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
contract MyContract {
int storageValue;
constructor(int initValue) {
storageValue = initValue;
}
function setValue(int newValue) public {
storageValue = newValue + 1;
}
function getValue() public view returns (int) {
return storageValue;
}
}
We assume that this is a wrong contract we developed , Because we are setting up new storageValue Numerical time , More +1( This is for demonstration, which can be done ).
compile
We first use Hardhat Tools to quickly compile contract files
Compile , It can quickly find and locate errors in the contract code
# compile
* npx hardhat compile
Compiled 1 Solidity file successfully
# Prompt successful compilation 1 individual solidity file , That's the contract we wrote
There is no problem with contract compilation , Then we can start the deployment test
Modify deployment script file
stay **/script** There is a sample-script.js file , Rename it to deploy.js, And replace the entire script content with the following code
const hre = require("hardhat");
async function main() {
// obtain MyContract contract
const MyContract = await hre.ethers.getContractFactory("MyContract");
// Deploy , Incoming initialization storageValue Value
const myContract = await MyContract.deploy(666);
// wait for MyContract Contract deployment completed
await myContract.deployed();
// Output MyContract Contract address
console.log("MyContract deployed to:", myContract.address);
}
// 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.exit(1);
});
Here our deployment script has been modified , Next, we start a local hardhat Local node , Then we deploy and test on the local node
Deployment and testing
Start the local node
* npx 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: 0xf39Fd6e51aad88Fxxxxxxxxxxxxxxx (10000 ETH)
Private Key: 0xac097xxxxxxxxxxxxxxx6b4d238ff944bacb478xxxxxxxxxxxxxxx
..........
Account #19: 0x8626f6940E2exxxxxxxxxxxxxxxxxxxx (10000 ETH)
Private Key: 0xdf570xxxxxxxxxxxxxxx27dafbffa9fc08a93xxxxxxxxxxxxxxx23656e
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.
# Here is the local node, which has been started successfully
Deploy
Open a new terminal , stay localhost Deploy smart contracts in the network
* npx hardhat run --network localhost scripts/deploy.js
Compiled 1 Solidity file successfully
MyContract deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
# 0x5FbDB2315678afecb367f032d93F642f64180aa3 It's ours MyContract Contract address
Next , We will pass Hardhat Console and on the local node MyContract Contract interaction
Contract interactions on local nodes
# Open console
* npx hardhat console --network localhost
Welcome to Node.js v16.15.1.
Type ".help" for more information.
>
# obtain MyContract contract
> const MyContract = await ethers.getContractFactory("MyContract")
undefined
# obtain myContract Contract example
> const myContract = await MyContract.attach("0x5FbDB2315678afecb367f032d93F642f64180aa3")
undefined
# call getValue Method
> await myContract.getValue()
BigNumber {
value: "666" }
# call setValue Method
> await myContract.setValue(111)
{
hash: '0x70f2e5baf434c3f15eb6618e54ecb7636a19c1d82f632cd27cd62f97c3d4c5fb',
type: 2,
accessList: [],
blockHash: '0xf1b93fcf0a7b7c1ddaaedf74ec42bbb4a6b54f7748ce78fe8632b7c68cbd36ca',
blockNumber: 2,
transactionIndex: 0,
confirmations: 1,
from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
gasPrice: BigNumber {
value: "767027553" },
maxPriorityFeePerGas: BigNumber {
value: "0" },
maxFeePerGas: BigNumber {
value: "970769246" },
gasLimit: BigNumber {
value: "26877" },
to: '0x5FbDB2315678afecb367f032d93F642f64180aa3',
value: BigNumber {
value: "0" },
nonce: 1,
data: '0x5093dc7d000000000000000000000000000000000000000000000000000000000000006f',
r: '0x35d5ecb1abf017d44f544e857546c781ce3af0fdc5d9d3b4c1ea7d18fcba34ca',
s: '0x43bf5017ca7554071e7ebbacb75fb0e0a4934b49fa87bf3847c0bb38c3c73e06',
v: 1,
creates: null,
chainId: 31337,
wait: [Function (anonymous)]
}
# Call again getValue Method validation setValue Execution results
> await myContract.getValue()
BigNumber {
value: "112" }
Here we demonstrate the process of connecting local nodes and interacting with contracts through the console , And verify whether the execution of the contract is successful
problem
As we mentioned earlier , We want to pass setValue Method to set a new storageValue value , But we wrote one more code in the contract +1( Although it was intended to demonstrate ). When our contract went online, we found this bug, If we want to modify now , Only one contract can be redeployed , Because we don't use scalable contracts .
Once the contract is linked, it cannot be modified , That's for sure .
If every contract goes wrong , If we all solve the problem by redeploying the contract, the cost will be too high .
- Disclose and replace the new contract address to users
- If there is a lot of data in the contract , We also need to do transfer
Next
that Next chapter Start , I will demonstrate how to make upgradeable changes to existing contracts , And complete the deployment and testing .
There is a problem , Or suggest leaving a message , thank you .
边栏推荐
- HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
- How to Create a Beautiful Plots in R with Summary Statistics Labels
- mmrotate旋转目标检测框架使用记录
- PHP query distance according to longitude and latitude
- CTF record
- 在连接mysql数据库的时候一直报错
- GGPlot Examples Best Reference
- php 根据经纬度查询距离
- PYQT5+openCV项目实战:微循环仪图片、视频记录和人工对比软件(附源码)
- 对毕业季即将踏入职场的年轻人的一点建议
猜你喜欢

ren域名有价值吗?值不值得投资?ren域名的应用范围有哪些?

Tdsql | difficult employment? Tencent cloud database micro authentication to help you

PYQT5+openCV项目实战:微循环仪图片、视频记录和人工对比软件(附源码)

Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic

Pit of the start attribute of enumrate

How to Create a Beautiful Plots in R with Summary Statistics Labels

2022年4月17日五心红娘团队收获双份喜报
![[cloud native] 2.5 kubernetes core practice (Part 2)](/img/87/826894d758392a0c7a60dd5fa09eef.png)
[cloud native] 2.5 kubernetes core practice (Part 2)

可昇級合約的原理-DelegateCall

ctf 记录
随机推荐
php 根据经纬度查询距离
webauthn——官方开发文档
电脑无缘无故黑屏,无法调节亮度。
How to Visualize Missing Data in R using a Heatmap
A white hole formed by antineutrons produced by particle accelerators
Skills of PLC recorder in quickly monitoring multiple PLC bits
ASTParser 解析含有emum 枚举方法的类文件的踩坑记
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
The selected cells in Excel form have the selection effect of cross shading
Precautions for scalable contract solution based on openzeppelin
Multi line display and single line display of tqdm
[idea] use the plug-in to reverse generate code with one click
The working day of the month is calculated from the 1st day of each month
liftOver进行基因组坐标转换
启牛商学院给的股票账户安全吗?能开户吗?
RPA advanced (II) uipath application practice
MySQL比较运算符IN问题求解
The difference between SQL left join main table restrictions written after on and where
Solve the problem of data blank in the quick sliding page of the uniapp list
【多线程】主线程等待子线程执行完毕在执行并获取执行结果的方式记录(有注解代码无坑)