当前位置:网站首页>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 .
边栏推荐
- GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
- HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
- MySQL basic statement
- Rest (XOR) position and thinking
- 基于Hardhat和Openzeppelin开发可升级合约(二)
- GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
- Gaode draws lines according to the track
- mysql 基本语句
- Order by注入
- 2022年4月17日五心红娘团队收获双份喜报
猜你喜欢

Cluster Analysis in R Simplified and Enhanced

K-Means Clustering Visualization in R: Step By Step Guide

webauthn——官方开发文档
![[cloud native] 2.5 kubernetes core practice (Part 2)](/img/87/826894d758392a0c7a60dd5fa09eef.png)
[cloud native] 2.5 kubernetes core practice (Part 2)

念念不忘,必有回响 | 悬镜诚邀您参与OpenSCA用户有奖调研

数字化转型挂帅复产复工,线上线下全融合重建商业逻辑

Seriation in R: How to Optimally Order Objects in a Data Matrice

CentOS8之mysql基本用法

Jinshanyun - 2023 Summer Internship

从攻击面视角,看信创零信任方案实践
随机推荐
从ros1到ros2配置的一些东西
ROS lacks catkin_ pkg
What is the relationship between digital transformation of manufacturing industry and lean production
微信小程序利用百度api达成植物识别
CTF record
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
【IDEA】使用插件一键逆向生成代码
在连接mysql数据库的时候一直报错
基于Hardhat编写合约测试用例
PLC-Recorder快速监控多个PLC位的技巧
SSRF
ros缺少xacro的包
Visualization of chip SEQ data by deeptools
Homer forecast motif
2022年遭“挤爆”的三款透明LED显示屏
[idea] use the plug-in to reverse generate code with one click
C file and folder operation
Tdsql | difficult employment? Tencent cloud database micro authentication to help you
Basic usage of MySQL in centos8
ASTParser 解析含有emum 枚举方法的类文件的踩坑记