[Contract Development Tool] Use hardhat to deploy the first contract in rinkeby

foreword

In this article, write a simple contract through solidity, and use the deployment tool hardhat to deploy the contract to the rinkeby test chain.

pre-step

The following materials are required before deploying the contract:

  • Alchemy App Key
  • The mnemonic phrase or the private key of the wallet when registering the wallet
  • Installation of the hardhat-deploy plugin

Alchemy’s App-key and how to register a wallet can be found on Baidu, there are many materials, so I won’t introduce it.

How to create a new hardhat project and how to install the plug-in hardhat-deploy can refer to:
https://hardhat.org/getting-started/

Directory Structure

insert image description here

Directories and files we need to know

contract: Store the written contract
deploy: Store the deployment script
utils/network.ts: Generally, it is a script to obtain the deployment chain and account information. This time, it is directly obtained in hardhat.config.ts, so this article is empty.
.env: A configuration file that stores mnemonic phrases, private keys, and app-keys.

the code

hardhat.config.ts
import {
    
    HardhatUserConfig} from "hardhat/types";
import 'hardhat-deploy';
// import {accounts} from './utils/network';

const ALCHEMY_RINKEBY = process.env.ALCHEMY_RINKEBY;
const MNEMONIC_AN = process.env.MNEMONIC_AN;

const buildConfig: HardhatUserConfig = {
    
    
  solidity: {
    
    
    compilers: [
      {
    
    
        version: '0.7.5',
        settings: {
    
    
          optimizer: {
    
    enabled: true, runs: 200},
          evmVersion: 'istanbul',
        },
      },
    ],
  },
  //如果部署的是其他测试链,应添加其他设置。
  networks: {
    
    
    rinkeby: {
    
    
      url: ALCHEMY_RINKEBY,
      // accounts : accounts(), //must mnemonic
      //看源码,这里我们输入 HardhatNetworkHDAccountsUserConfig 对象,即为通过助记词查找钱包地址。
      accounts: {
    
    
        mnemonic: MNEMONIC_AN,
      },
    },
  },

  //hardhat-deploy for local test
  namedAccounts: {
    
    
    deployer: {
    
    
      default: 0,
    },
  },
};

export default buildConfig;
.env file

insert image description here

001-Greeter-deploy
import {
    
    HardhatRuntimeEnvironment} from 'hardhat/types';
import {
    
    DeployFunction} from 'hardhat-deploy/types';

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
    
    
    const {
    
    deployments, getNamedAccounts} = hre;
    const {
    
    deploy} = deployments;

    const {
    
    deployer} = await getNamedAccounts();
	//这里建议打印部署者地址,检查是否是自己的地址
    console.log(deployer)

    await deploy('Greeter', {
    
    
        from: deployer,
        //输入constructor的参数
        args: ['we success!'],
        log: true,
    });
};

export default func;
func.tags = ['Greeter'];

After writing the code, execute
npx hardhat --network rinkeby deploy

Check your wallet address in https://rinkeby.etherscan.io/, and you can see the successfully deployed contract.

Guess you like

Origin blog.csdn.net/weixin_43742184/article/details/118056869