当前位置:网站首页>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 .
边栏推荐
- 每月1号开始计算当月工作日
- 揭露数据不一致的利器 —— 实时核对系统
- Basic usage of MySQL in centos8
- GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
- PLC-Recorder快速监控多个PLC位的技巧
- Some suggestions for young people who are about to enter the workplace in the graduation season
- Always report errors when connecting to MySQL database
- Jinshanyun - 2023 Summer Internship
- ctf 记录
- PX4 Position_ Control RC_ Remoter import
猜你喜欢
JS——每次调用从数组里面随机取一个数,且不能与上一次为同一个
vant tabs组件选中第一个下划线位置异常
6方面带你认识LED软膜屏 LED软膜屏尺寸|价格|安装|应用
可升级合约的原理-DelegateCall
What is the relationship between digital transformation of manufacturing industry and lean production
A sharp tool for exposing data inconsistencies -- a real-time verification system
HOW TO ADD P-VALUES TO GGPLOT FACETS
【云原生】2.5 Kubernetes 核心实战(下)
excel表格中选中单元格出现十字带阴影的选中效果
JS -- take a number randomly from the array every call, and it cannot be the same as the last time
随机推荐
Is it safe to open a stock account online? I'm a novice, please guide me
Is the Ren domain name valuable? Is it worth investing? What is the application scope of Ren domain name?
MySql存储过程游标遍历结果集
制造业数字化转型和精益生产什么关系
Eight sorting summaries
Summary of flutter problems
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
通过券商经理的开户二维码开股票账户安全吗?还是去证券公司开户安全?
[multithreading] the main thread waits for the sub thread to finish executing, and records the way to execute and obtain the execution result (with annotated code and no pit)
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
Importerror: impossible d'importer le nom « graph» de « graphviz»
Introduction to interface debugging tools
CentOS8之mysql基本用法
Astparser parsing class files with enum enumeration methods
Multi line display and single line display of tqdm
ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
MySQL comparison operator in problem solving
Tick Data and Resampling
Seriation in R: How to Optimally Order Objects in a Data Matrice
ASTParser 解析含有emum 枚举方法的类文件的踩坑记