Truffle - Test net

반영환·2023년 5월 30일
0

BlockChainDev

목록 보기
2/11
post-thumbnail

Truffle depoly on test net

dApp 프레임워크인 truffle을 사용해서 sepolia 네트워크에 배포하는 방법을 알아보자.

사전 준비

  1. infura 이더리움 프로젝트 생성
  2. Node.js 설치
  3. Test Etherium Wallet Create ( By MetaMask )

Step

1. Truffle 설치

npm install -g truffle

NPM을 사용해서 truffle을 전역 설치 해준다.

전역 설치하지 않으면 실행이 되지 않는다.

2. MetaMask에 Test Net Ehter 입금

metamask test net ehter ballance append 검색

3. 프로젝트 폴더에 DotEnv 설치

npm install dotenv

4. Truffle 프로젝트 생성

truffle init
  • contracts/: Solidity contracts를 위한 폴더 입니다.
  • migrations/: 배포 스크립트 파일을 위한 폴더입니다.
  • test/: 앱과 컨트랙트의 테스트 파일을 위한 폴더입니다.
  • truffle-config.js : Truffle 설정 파일입니다.

5. hdwallet-provider 설치

hdwallet-package는 12단어 또는 24단어 니모닉에서 파생된 주소에 대한 트랜잭션을 서명할 수 있는 별도의 패키지이다. Metamask에 접근할 수 있게 해준다.

6. .env 파일 생성

INFURA_API_KEY = "https://ropsten.infura.io/v3/<Your-API-Key>"
MNEMONIC = "<Your-MetaMask-Secret-Recovery-Phrase>"

Your-API-Key : Infura의 이더리움 프로젝트 API key.
Your-MetaMask-Secret-Recovery-Phrase : MetaMask 지갑의 니모닉. hdwallet-provider의 거래에 사용됩니다.

7. smart contract 작성

// SPDX-License-Identifier: MIT
pragma solidity >=0.5.8;

contract Demo {
    event Echo(string message);

    function echo(string calldata message) external {
        emit Echo(message);
    }
}

8. truffle-config.js 설정

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

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*"
    },
    sepolia: {
      provider: () => new HDWalletProvider(MNEMONIC, INFURA_API_KEY),
      network_id: <Sepolia Chain Id In Here>,
      gas: 5500000
    }
  }
};

9. smart contract compile

truffle compile

성공시

Compiling your contracts...
===========================
> Compiling ./contracts/Demo.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /Users/user/truffleProject/build/contracts
> Compiled successfully using:
   - solc: 0.5.16+commit.9c3226ce.Emscripten.clang

10. deploy

truffle migrate --network sepolia

성공시

Starting migrations...
======================
> Network name:    'ropsten'
> Network id:      3
> Block gas limit: 30000000 (0x1c9c380)


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0xd437e9d1595e31228dcead9397caa1526ebe5ae8f7ec06a176b7aeed53916202
   > Blocks: 4            Seconds: 53
   > contract address:    0xBe72Ed0EB8Af6d52c0Be64Ca908f81E0100390C2
   > block number:        12818214
   > block timestamp:     1660875012
   > account:             0x5A2609D698DE041B1Ba77139A4229c8a161dDd9e
   > balance:             19.999374644998249006
   > gas used:            250142 (0x3d11e)
   > gas price:           2.500000007 gwei
   > value sent:          0 ETH
   > total cost:          0.000625355001750994 ETH

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:     0.000625355001750994 ETH


2_deploy_contract.js
====================

   Deploying 'Demo'
   ----------------
   > transaction hash:    0xbed1f55b5be802ac40e408d3ac517de67795afc1762aaa7450dd652921a557bd
   > Blocks: 2            Seconds: 29
   > contract address:    0xD06F174f4A41884AAf0958C72EEc49Ec386e03b3
   > block number:        12818218
   > block timestamp:     1660875060
   > account:             0x5A2609D698DE041B1Ba77139A4229c8a161dDd9e
   > balance:             19.998852204996786174
   > gas used:            163063 (0x27cf7)
   > gas price:           2.500000007 gwei
   > value sent:          0 ETH
   > total cost:          0.000407657501141441 ETH

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:     0.000407657501141441 ETH

Summary
=======
> Total deployments:   2
> Final cost:          0.001033012502892435 ETH

transaction hash 을 통해 EtherScan에서 블록 정보를 확인할 수 있다.

출처

Truffle로 SmartContract 배포하기

profile
최고의 오늘을 꿈꾸는 개발자

0개의 댓글

관련 채용 정보