当前位置:网站首页>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 .
边栏推荐
- Is it safe to open a stock account through the QR code of the securities manager? Or is it safe to open an account in a securities company?
- Programmer growth Chapter 6: how to choose a company?
- 制造业数字化转型和精益生产什么关系
- 基于Hardhat和Openzeppelin开发可升级合约(一)
- SQLite modify column type
- ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
- PHP query distance according to longitude and latitude
- spritejs
- MySQL比较运算符IN问题求解
- Rest (XOR) position and thinking
猜你喜欢
随机推荐
Verilog 和VHDL有符号数和无符号数相关运算
MySQL basic statement
Principle of scalable contract delegatecall
TDSQL|就业难?腾讯云数据库微认证来帮你
电脑无缘无故黑屏,无法调节亮度。
deepTools对ChIP-seq数据可视化
tqdm的多行显示与单行显示
R HISTOGRAM EXAMPLE QUICK REFERENCE
Visualization of chip SEQ data by deeptools
A sharp tool for exposing data inconsistencies -- a real-time verification system
Supermarket (heap overload
ren域名有价值吗?值不值得投资?ren域名的应用范围有哪些?
Bedtools tutorial
Summary of flutter problems
map集合赋值到数据库
mysql 基本语句
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
php 根据经纬度查询距离
Seriation in R: How to Optimally Order Objects in a Data Matrice









