티스토리 뷰

728x90

Truffle

트러플은 스마트 컨트랙트의 테스트, 컴파일, 배포 등을 위해 사용하는 도구이다. Hardhat과 달리 로컬 블록체인 네트워크는 제공하지 않는다. 로컬 네트워크가 필요하다면 ganache 등과 함께 사용할 수 있다.

init

npx truffle init

아래와 같은 디렉토리와 파일이 생성된다.

contracts\
migrations\
test\
truffle-config.js

컴파일(Compile)

npx truffle compile

contracts 디렉토리 안의 솔리디티 파일들을 컴파일해서 결과를 build 디렉토리 안에 넣어준다.

배포(Deploy)

npx truffle migrate --reset --network dev

네트워크 이름은 truffle-config.js 파일에서 설정한다.

// truffle-config.js
const { projectId, mnemonic, API_KEY } = require("./secrets.json");
const HDWalletProvider = require("@truffle/hdwallet-provider");

module.exports = {
  api_keys: {
    etherscan: API_KEY,
  },
  networks: {
    development: {
      host: "127.0.0.1", // Localhost (default: none)
      port: 7545, // Standard Ethereum port (default: none)
      network_id: "*", // Any network (default: none)
    },
    dev: {
      host: "127.0.0.1", // Localhost (default: none)
      port: 8545, // Standard Ethereum port (default: none)
      network_id: "*", // Any network (default: none)
    },
    ropsten: {
      provider: () =>
        new HDWalletProvider(
          mnemonic,
          `https://ropsten.infura.io/v3/${projectId}`
        ),
      network_id: 3, // Ropsten's id
      gas: 5500000, // Ropsten has a lower block limit than mainnet
      confirmations: 2, // # of confs 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 )
    },
  },
  compilers: {
    solc: {
      version: "0.8.10", // Fetch exact version from solc-bin (default: truffle's version)
    },
  },
}

Hardhat

하드햇은 스마트 컨트랙트의 테스트, 컴파일, 배포 등을 위한 도구이며 로컬 블록체인 네트워크도 제공한다.

테스트(Test)

npx hardhat test

test 디렉토리 안에 미리 작성해두고 위 명령어를 통해 테스트 스크립트를 실행시킨다.

컴파일(Compile)

npx hardhat compile

로컬 네트워크(노드) 실행

npx hardhat node

배포(Deploy)

npx hardhat run scripts/deploy.js --network hardhat

네트워크 이름 등의 설정은 hardhat.config.js에서 한다.

// hardhat.config.js
require('@nomiclabs/hardhat-waffle');
const fs = require('fs');
const keyData = fs.readFileSync('./p-key.txt', {
  encoding: 'utf8',
  flag: 'r',
});
const projectId = fs.readFileSync('./p-id.txt', {
  encoding: 'utf8',
  flag: 'r',
});

module.exports = {
  defaultNetwork: 'hardhat',
  networks: {
    hardhat: {
      chainId: 1337, // config standard
    },
    mumbai: {
      url: `https://polygon-mumbai.infura.io/v3/${projectId}`,
      accounts: [keyData],
    },
    mainnet: {
      url: `https://mainnet.infura.io/v3/${projectId}`,
      accounts: [keyData],
    },
    ropsten: {
      url: `https://ropsten.infura.io/v3/${projectId}`,
      accounts: [`0x${keyData}`],
    },
  },
  solidity: {
    version: '0.8.10',
    settings: {
      optimizer: {
        enabled: true,
        runs: 200,
      },
    },
  },
};
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함