SECURITY/Blockchain

[Ethereum] Truffle μ—μ„œ Ropsten 에 deploy (migrate) ν•˜κΈ°

\b\t 2022. 2. 7. 16:37

 

truffle ν”„λ‘œμ νŠΈμ—μ„œ ganache μ—λ§Œ λ°°ν¬ν•΄μ„œ ν…ŒμŠ€νŠΈν–ˆμ—ˆλŠ”λ°,

μ΄λ²ˆμ—λŠ” ropsten μ΄λΌλŠ” ν…ŒμŠ€νŠΈ λ„€νŠΈμ›Œν¬μ— 배포해보도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€.

 

 

1. infura 계정 생성 및 Ropsten API Key 생성

ropsten 을 μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” λ¨Όμ € infura 계정을 μƒμ„±ν•΄μ„œ API Key λ₯Ό μ–»μ–΄μ•Ό ν•©λ‹ˆλ‹€.

infura 계정을 μƒμ„±ν•œ ν›„, μƒˆ ν”„λ‘œμ νŠΈλ₯Ό λ§Œλ“€λ©΄ Project ID λ₯Ό 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

λ”°λΌμ„œ https://ropsten.infura.io/v3/{PROJECT_ID} 둜 ropsten 을 μ‚¬μš©ν•˜κ²Œ λ©λ‹ˆλ‹€.

 

 

 

2. Truffle ν”„λ‘œμ νŠΈμ— Ropsten network μ„€μ •

ropsten 은 infura μ—μ„œ μ œκ³΅ν•˜λŠ” 블둝체인 λ„€νŠΈμ›Œν¬λ‘œ, λ‹€μŒμ—μ„œ λ³Ό 수 μžˆλ“― HDWallet 으둜 μ ‘κ·Όν•΄μ•Ό ν•©λ‹ˆλ‹€.

 

 

HDWallet 을 μœ„ν•΄ 이λ₯Ό μ‰½κ²Œ μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•΄μ£ΌλŠ” @truffle/hdwallet-provider 을 μ‚¬μš©ν•˜λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€.

@truffle/hdwallet-provider 을 μ„€μΉ˜ν•œ ν›„,

$ npm install @truffle/hdwallet-provider

 

ropsten μ—μ„œ wallet 을 λ§Œλ“€μ–΄μ£Όκ³  truffle-config.js 에 λ‹€μŒκ³Ό 같이 μž‘μ„±ν•©λ‹ˆλ‹€.

μ €μ˜ 경우, web3 λ₯Ό μ‚¬μš©ν•˜μ—¬ ropsten μ—μ„œ 계정을 λ§Œλ“€μ–΄μ£Όκ³  mnemonic 에 private key λ₯Ό λ„£μ–΄μ£Όμ—ˆμŠ΅λ‹ˆλ‹€.

const HDWalletProvider = require('@truffle/hdwallet-provider');
const mnemonic = "{PRIVATE_KEY}";

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",     // Localhost (default: none)
      port: 7545,            // Standard Ethereum port (default: none)
      network_id: "*",       // Any network (default: none)
    },
    ropsten: {
     provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/{PROJECT_ID}`),
     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 )
    },
  },

  // Configure your compilers
  compilers: {
    solc: {
      version: "0.8.10",  
    }
  },
};
  • PRIVATE_KEY 와 PROJECT_ID μž‘μ„±

 

μ΄λ•Œ, constract λ₯Ό λ°°ν¬ν•˜λ €λ©΄ ν•΄λ‹Ή 계정에 이더가 μžˆμ–΄μ•Ό ν•˜λ―€λ‘œ μ €λŠ” 배포에 μ‚¬μš©ν•  계정에 ν…ŒμŠ€νŠΈ 이더λ₯Ό λ°›μ•˜μŠ΅λ‹ˆλ‹€.

 

μœ„μ˜ 두 μ‚¬μ΄νŠΈμ—μ„œ 계정 address λ₯Ό λ„£μœΌλ©΄ 이더λ₯Ό λ³΄λ‚΄μ€λ‹ˆλ‹€. (μ–Όλ§ˆλ‚˜ κ±Έλ¦΄μ§€λŠ” λͺ¨λ₯΄μ§€λ§Œ μ €λŠ” 금방 μ™”μŠ΅λ‹ˆλ‹€)

 

 

3. Ropsten 에 배포

계정 섀정이 잘 λ˜μ—ˆμœΌλ©΄, λ‹€μŒκ³Ό 같이 배포해쀄 수 μžˆμŠ΅λ‹ˆλ‹€.

$ turffle migrate --network ropsten

 

만일 계정에 이더가 μΆ©λΆ„ν•˜μ§€ μ•ŠμœΌλ©΄ λ‹€μŒκ³Ό 같이 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

 

 

이더λ₯Ό ν™•μΈν•˜κ³  μ‹ΆμœΌλ©΄ truffle console 둜 확인해볼 수 μžˆμŠ΅λ‹ˆλ‹€.

$ truffle console --network ropsten
truffle(ropsten)> web3.eth.getBalance("{ADRRESS}")
'999374614924453492'

 

이더λ₯Ό μ±„μ›Œμ£Όλ©΄ λ‹€μŒκ³Ό 같이 migrate λ₯Ό μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

 

μ—­μ‹œ ganache 둜 λ‘œμ»¬μ—μ„œ μ‹€ν–‰ν•  λ•ŒλŠ” λ°”λ‘œ 배포가 λ˜μ—ˆλŠ”λ°, ropsten 은 훨씬 μ‹œκ°„μ΄ 많이 κ±Έλ ΈμŠ΅λ‹ˆλ‹€.

 

 

4. web3 둜 ropsten 에 배포된 contract μ°Έμ‘°ν•˜κΈ°

μš°μ„ , npm 으둜 web3 λ₯Ό μ„€μΉ˜ν•΄μ€λ‹ˆλ‹€. (truffle console μ—μ„œλ„ web3 λ₯Ό μ‚¬μš©ν•΄μ„œ ν•  수 μžˆμ§€λ§Œ, λ…λ¦½λœ web3 λ₯Ό μ‚¬μš©ν•΄λ³΄κΈ° μœ„ν•΄μ„œ μ΄λ ‡κ²Œ ν–ˆμŠ΅λ‹ˆλ‹€)

$ npm install web3

 

κ·Έ ν›„ node λ₯Ό μ‹€ν–‰μ‹œν‚¨ λ‹€μŒ infura PROJECT ID 둜 web3 객체λ₯Ό λ§Œλ“€μ–΄μ€λ‹ˆλ‹€.

$ node
> const Web3 = require('web3')
> const web3 = new Web3('https://ropsten.infura.io/v3/{PROJECT_ID}')
> let contract = new web3.eth.Contract([
    {
      "inputs": [],
      "name": "count",
      "outputs": [
        {
          "internalType": "int256",
          "name": "",
          "type": "int256"
        }
      ],
      "stateMutability": "view",
      "type": "function",
      "constant": true
    },
  ], "0x99AF...")
  • μ›ν•˜λŠ” 블둝체인 λ„€νŠΈμ›Œν¬λ‘œ web3 객체λ₯Ό λ§Œλ“€μ–΄μ€λ‹ˆλ‹€. μ—¬κΈ°μ„œλŠ” ropsten 을 μ‚¬μš©ν•  κ²ƒμ΄λ―€λ‘œ, PROJECT_ID 둜 endpoint λ₯Ό μ§€μ •ν•˜μ—¬ μ„€μ •ν•΄μ£Όμ—ˆμŠ΅λ‹ˆλ‹€.
  • new web3.eth.Contract 둜 contract 객체λ₯Ό λ§Œλ“€μ–΄μ€λ‹ˆλ‹€. μΈμžλŠ” λ‹€μŒκ³Ό μœ„μ™€ ABI, contract address κ°€ λ“€μ–΄κ°€λ©΄ λ©λ‹ˆλ‹€. (let contract = new web3.eth.Contract(<ABI>, <Contract Address>))
  • truffle μ—μ„œ ν•΄λ‹Ή contract λ₯Ό 배포할 λ•Œ compile ν›„ build/contracts/ 에 json ν˜•νƒœλ‘œ compile κ²°κ³Όλ₯Ό μ£ΌλŠ”λ°, κ·Έ νŒŒμΌμ— ABI κ°€ μžˆμŠ΅λ‹ˆλ‹€.

 

이제 ν•΄λ‹Ή 객체둜 contract 에 μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

 

λ°°ν¬ν•œ smart constract κ°€ counter κΈ°λŠ₯을 κ°–λŠ” κ²ƒμ΄λ―€λ‘œ getCount() 둜 값을 확인해보도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€.

λ‹€μŒκ³Ό 같이 contract 객체의 methods λ“€λ‘œ ν•¨μˆ˜λ“€μ„ μ°Έμ‘°ν•˜μ—¬ 호좜, μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

 

 

μ΄μƒμœΌλ‘œ Ropsten λ„€νŠΈμ›Œν¬μ— 배포 및 ν˜ΈμΆœμ„ ν•΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

 

 


ref. https://medium.com/0xcode/interacting-with-smart-contracts-using-web3-js-part-ii-c1ef7566d1c5