Truffle - 스마트 컨트랙트 배포 및 에러 정리

jhcha·2023년 8월 27일
0

DApp

목록 보기
7/9
npm install truffle
truffle init

Truffle init으로 프로젝트를 생성하고 스마트 컨트랙트를 배포하는 과정 중 발생하는 에러를 정리해본다.

1. HDWalletProvider is not defined

ReferenceError: HDWalletProvider is not defined

npm install truffle-hdwallet-provider

truffle-config.js
node_modules에 생성된 truffle-hdwallet-provider 경로로 재설정

// const HDWalletProvider = require('@truffle/hdwallet-provider');
const HDWalletProvider = require('truffle-hdwallet-provider');
require('dotenv').config();
const { MNEMONIC, INFURA_ENDPOINT_URL } = process.env;

2. ReferenceError: MNEMONIC is not defined

ReferenceError: MNEMONIC is not defined

프로젝트 .env에 MNEMONIC, INFURA_ENDPOINT_URL 설정해도 인식을 못해서 발생한 에러
메타마스크 비밀키 확인 방법은 다음 공식 사이트의 설명에 따르면 된다.
https://support.metamask.io/hc/en-us/articles/360015290032

npm install dotenv

종속성 설치하고, truffle-config.js 파일에서 다음과 같이 설정한다.

require('dotenv').config();
const { MNEMONIC, INFURA_ENDPOINT_URL } = process.env;

3. Error: There was a timeout while attempting to connect to the network at undefined.

Error: There was a timeout while attempting to connect to the network at undefined.

truffle-config.js 파일에서 다음과 같이 networkCheckTimeout 값을 설정해서 오류를 해결했다.

    goerli: {
      networkCheckTimeout: 100000,
      provider: () => new HDWalletProvider(MNEMONIC, INFURA_ENDPOINT_URL),
      network_id: 5,       // Goerli's id
      confirmations: 2,    // # of confirmations to wait between deployments. (default: 0)
      timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
      skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
    },

4. Error: The network id does not match the one returned by the network.

Error: The network id specified in the truffle config (5) does not match the one returned by the network (1). Ensure that both the network and the provider are properly configured.

goerli network id는 5인데, 설정한 provider infura endpoint url은 메인넷으로 연결해서 발생한 에러.

    goerli: {
      // INFURA_ENDPOINT_URL: https://goerli.infura.io/v3/${PROJECT_ID}
      provider: () => new HDWalletProvider(MNEMONIC, INFURA_ENDPOINT_URL),
      ),
      // Goerli's id
      network_id: 5,
    },

ethereum mainnet은 network id가 1이다. truffle-config.js에 설정한 provider와 network id가 매칭하는지 확인해서 수정하면 된다.

migrate test

truffle migrate --network goerli


스마트 컨트랙트가 잘 배포되었다.

truffle console --network goerli
> HelloWorld


위와 같이, 스마트 컨트랙트 배포된 것도 확인할 수 있다.
정리하자면 이더리움 테스트넷 Goerli 네트워크에 Infura Provider를 설정하고, Truffle로 스마트 컨트랙트를 배포를 테스트했다.

0개의 댓글

관련 채용 정보