본문 바로가기

BlockChain Developer/Public Blockchain

Hardhat - Upgradable Contract & Verify Contract

1. Project Settiing

# Initialize
npm init -y

# install hardhat
npm i hardhat --dev

# hardhat initialize
npx hardhat

# init openzeppelin
npm install @openzeppelin/hardhat-upgrades --dev

# For Verify Contract
npm install @nomiclabs/hardhat-etherscan --dev

 

2. Hardhat 기본 사용법

# View Accounts
npx hardhat accounts

# Compile Solidity
npx hardhat compile

# TESTING
npx hardhat test ./test/mycontract.test.js

# TESTNETWORK START (ex: GETH)
npx hardhat node

# RUN DEPLOY CONTRACT
npx hardhat run --network localhost ./scripts/MyContract.deploy.js

# Open Console
npx hardhat console --network localhost

3. USE Contract In Console

# GET CONTRACT
const f = await ethers.getContractFactory("MyContract")

# Set Contract with address;
const mc = await f.attach("0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9")

# Run Contract Function
(await mc.get()).toString()

4. Make Upgradable Contract

// hardhat.config.js

require("@nomiclabs/hardhat-waffle");
// Upgradable Contract Lib
require("@openzeppelin/hardhat-upgrades");
// Verify Contract
require("@nomiclabs/hardhat-etherscan");

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
  const accounts = await hre.ethers.getSigners();

  for (const account of accounts) {
    console.log(account.address);
  }
});

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  networks: {
    rinkeby: {
      url: "https://rinkeby.infura.io/v3/e9da57059982446d8514371157fdd050",
      accounts: ["e5db84f8d6393e84959782e0afe6d27786370c5295cbc5bb5a6596f87c5c873c"]
    }
  },
  solidity: "0.8.2",
  etherscan: {
    apiKey: "X83XAQV5TJQ3QMPSEIJ5CX6UAKCJCJJXIY"
  }
};

 

Deploy Version1 File

// MyContract.deploy.js
const hre = require("hardhat");

async function main() {
    const MyContract = await hre.ethers.getContractFactory("MyContract");
    const mc = await upgrades.deployProxy(MyContract, [1], { initializer : "set" });
    console.log(`--My Contract--`);
    console.log(MyContract);

    console.log(`--MC--`);
    console.log(mc);
    console.log(`Deployed To : ${mc.address}`);
    
}
  
main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });

 

Deploy Version2 File

const { upgrades } = require("hardhat");
const hre = require("hardhat");

async function main() {
	// Version 1 Contract Address === proxyContractAddress;
    const proxyContractAddress = "0x359c1FfCb0114d86501929f68642244d8A49C7fB";
    const MyContract_V2 = await hre.ethers.getContractFactory("MyContract_V2");
    const mc2 = await upgrades.upgradeProxy(proxyContractAddress, MyContract_V2);
    
    console.log(`-- Upgraded Proxy Contract Address --`);
    console.log(mc2.address);
}
  
main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });

 

 

 

5. Verify Contract

5.1 컨트렉트를 배포한다.

5.2 컨트렉트 Verify

npx hardhat verify --network rinkeby "0x359c1FfCb0114d86501929f68642244d8A49C7fB" --show-stack-traces

 

'BlockChain Developer > Public Blockchain' 카테고리의 다른 글

공동소유 NFT (Fractional NFT)  (0) 2022.08.22
[Solditiy] CEI & Mutex Pattern  (0) 2022.08.18
Blockchain Dev Initial Setting  (0) 2022.04.21
Install IPFS IN Ubuntu  (0) 2022.04.19
[Ethereum] GSN  (0) 2022.03.15