当前位置:网站首页>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 .
边栏推荐
- Principe du contrat évolutif - delegatecall
- flutter 问题总结
- MTK full dump grab
- Compilation errors and printout garbled problems caused by Chinese content in vs2019 code
- Bedtools tutorial
- 【多线程】主线程等待子线程执行完毕在执行并获取执行结果的方式记录(有注解代码无坑)
- Introduction to interface debugging tools
- Verilog and VHDL signed and unsigned number correlation operations
- Writing contract test cases based on hardhat
- 基于Hardhat编写合约测试用例
猜你喜欢
PLC-Recorder快速监控多个PLC位的技巧
ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
MySQL比较运算符IN问题求解
MySQL comparison operator in problem solving
RPA advanced (II) uipath application practice
Importerror: impossible d'importer le nom « graph» de « graphviz»
微信小程序利用百度api达成植物识别
The computer screen is black for no reason, and the brightness cannot be adjusted.
R HISTOGRAM EXAMPLE QUICK REFERENCE
Solve the problem of data blank in the quick sliding page of the uniapp list
随机推荐
MySQL basic statement
基于 Openzeppelin 的可升级合约解决方案的注意事项
LVM operation
MySQL linked list data storage query sorting problem
MySql存储过程游标遍历结果集
Some things configured from ros1 to ros2
VS2019代码中包含中文内容导致的编译错误和打印输出乱码问题
R HISTOGRAM EXAMPLE QUICK REFERENCE
Liftover for genome coordinate conversion
liftOver进行基因组坐标转换
[idea] use the plug-in to reverse generate code with one click
Is it safe to open a stock account online? I'm a novice, please guide me
PHP query distance according to longitude and latitude
Summary of flutter problems
MTK full dump grab
Multi line display and single line display of tqdm
RPA advanced (II) uipath application practice
QT获取某个日期是第几周
对毕业季即将踏入职场的年轻人的一点建议
Gaode draws lines according to the track