Truffle 실습2 ( local 다른 컨트랙트 import, Goerli 테스트넷에 베포하기)

유지민·2022년 12월 14일
0

truffle

목록 보기
2/2

다른 컨트랙트 import해보기

A.sol, B.sol을 각각 만든후 상태변수 선언 후 C.sol에서 둘의 상태변수 더한값 리턴하기

A.sol, B.sol 작성 후 C.sol작성

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
import "./A.sol";
import "./B.sol";

contract C {
    A a;
    B b;
    constructor(address _a, address _b) {
        a = A(_a);
        b = B(_b);
    }

    function getAB() public view returns(uint) {
        return(a.a() + b.b());
    }
}

이때 constructor에 들어갈 두 주소는 Deploy용 js파일에서 작성한다.

3_C_sol.js

const c = artifacts.require("C");

module.exports = function (deployer) {
  deployer.deploy(c, '0x698c6A73Aa1E5e3670723ea2e335aBB8DCA0e418', '0x69FD2f51be24FB2F4f7043a39bEccDe4B1df7B27');
};

먼저 A, B를 Deploy한후 C를 Migrate하면 된다. 그럼 A, B만 Migrate하는 방법은

truffle migrate --f n --to m

하면 n번부터 m번까지 Migrate하겠다는 뜻.
그래서 deploy용 js파일을 작성할때는 접두어로 순서를 쓰라고 공식문서에도 나와있다고 한다.
만약 주소를 저렇게 하드코딩으로 쓰지 않고 자동으로 불러오고 싶으면?

const A = artifacts.require("A");   //require안에 아이는 constract명
const B = artifacts.require("B");
const C = artifacts.require("C");   //정보를 가지고 와서

module.exports = async function (deployer) {
  const a = await A.deployed();
  const b = await B.deployed();
  return deployer.deploy(C, a.address, b.address);  || awiat deployer.deploy(C, a.address, b.address);  //베포(deployer.deploy)
};

이런식으로 require로 정보를 가져와서 deployed된 아리를 가져와 .address로 작성하면 된다.
마지막 return deployer.deploy(C, a.address, b.address); 이나
awiat deployer.deploy(C, a.address, b.address); 둘다 상관없다는데
통일성을 위해 await로 써주자

하나의 deploy용 js파일에서 다른지갑 주소로 베포하기

5_ab_sol.js 파일 생성 후

const a = artifacts.require("A");
const b = artifacts.require("B");

module.exports = function (deployer, network, accounts) {
  deployer.deploy(a);
  deployer.deploy(b, { from: accounts[1] });
};

Goerli 테스트넷에 베포하기

  1. infura.io 접속 후 api key 확인
  2. goerli etherscan 접속
  3. metamask 제일 첫 지갑에 충분한 테스트 이더 있는지 확인
npm install dotenv	//환경 변수 관리를 위해 설치
npm install @truffle/hdwallet-provider	//개인지갑 설정 위헤

후에 .env파일을 만든뒤

INFURA_API_KEY = "https://goerli.infura.io/v3/인퓨라 API KEY"
MNEMONIC = "개인지갑 시드복사한거"

이제 truffle-config.js안에 몇가지를 수정해준다.

44번째쯤 줄에

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

// const HDWalletProvider = require('@truffle/hdwallet-provider');

를 주석 해제 후

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

로 수정

88번째쯤 줄

    // goerli: {
    //   provider: () => new HDWalletProvider(MNEMONIC, `https://goerli.infura.io/v3/${PROJECT_ID}`),
    //   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 )
    // },

를 주석 해제 후 수정

    goerli: {
      provider: () => new HDWalletProvider(MNEMONIC, INFURA_API_KEY), //지갑, network
      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 )
    },

수정후 베포할때

truffle migrate --network goerli

처럼 --network 네트워크이름 을 추가해준다.

profile
개발 취준생

0개의 댓글