코드코코

[솔리디티][디앱] lottery - 환경설정 본문

블록체인/솔리디티

[솔리디티][디앱] lottery - 환경설정

코드코코 2022. 2. 13. 17:47

환경설정

  1. nodejs
  2. vscode
  3. truffle - 컴파일 하고 사용하기 쉽게 함.
  4. ganache-cli
  5. vscode - solidity extension
  6. metamask

 

npm -g install truffle

npm -g ganache-cli

 

작업위치

C:\Users\coco\Desktop\coco\studying_solidity\lotteryDapp>

 

truffle init

C:\Users\coco\Desktop\coco\studying_solidity\lotteryDapp>truffle init

Starting init...
================

> Copying project files to C:\Users\coco\Desktop\coco\studying_solidity\lotteryDapp

Init successful, sweet!

Try our scaffold commands to get started:
  $ truffle create contract YourContractName # scaffold a contract
  $ truffle create test YourTestName         # scaffold a test

http://trufflesuite.com/docs

 

contracts :모든 스마트 컨트랙트 파일

migrations : 배포관련 스크립트

test : 단위 테스트, 인터그레이트 테스트 같은 테스트 코드

 

테스트 코드 사용 2가지 방법

-솔리디티 자체의 랭귀지 자체 사용 테스트 형식,

-js를 통해 외부에서 사용하는 인터그레이션 테스트 형식.

 

파일 생성 및 작성

 

truffle compile

C:\Users\coco\Desktop\coco\studying_solidity\lotteryDapp>truffle compile

Compiling your contracts...
===========================
> Compiling .\contracts\Lottery.sol
> Compiling .\contracts\Migrations.sol
> Compilation warnings encountered:

    Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
--> project:/contracts/Lottery.sol


> Artifacts written to C:\Users\coco\Desktop\coco\studying_solidity\lotteryDapp\build\contracts
> Compiled successfully using:
   - solc: 0.8.11+commit.d7f03943.Emscripten.clang

- build : 컴파일 한 결과들이 json 파일형식으로 생성됨.

 

Lottey.json

- bytecode : 블록체인 네트워크에 배포될 때 사용됨

- 소스맵과 여러가지 메타정보들을 포함함.

 

 

ganache-cli -d -m tutorial

- 배포하기 위해서는 우리가 사용할 블록체인 네트워크가 필요: ganache-cli 사용

- 항상 같은 주소와 같은 개인키를 가져오게 됨

C:\Users\coco>ganache-cli -d -m tutorial
Ganache CLI v6.12.2 (ganache-core: 2.13.2)

Available Accounts
==================
(0) 0xF76c9B7012c0A3870801eaAddB93B6352c8893DB (100 ETH)
(1) 0x84F43c88Dc56510D043D5EE922D57549959c4C3C (100 ETH)
(2) 0x7eC36bc569C4B6fD3dDD7B7ebc9e1eDb2e736339 (100 ETH)
(3) 0x7f98395E0F0470B4a6FBC7E8828897ABBD093a00 (100 ETH)
(4) 0xfca6a8A66E8fbcd2fCc1532f5eeEDD34bF4A7e97 (100 ETH)
(5) 0x4De841B154641C4d0bceA2cb1aFC9d86779ffd0A (100 ETH)
(6) 0x77041c20FE4B1240d8b13C29dC8F541eFC576c08 (100 ETH)
(7) 0x68126B29d36451F0e2FDc6bFeCB404594099B669 (100 ETH)
(8) 0x9fcBaD0d39d77393cA6f4FE33e0b5f138E9DF59A (100 ETH)
(9) 0xcD4a6eCa58EdD41012Cf0429E59E885Bcb052Ba1 (100 ETH)

Private Keys
==================
(0) 0x8d22a0aa9c43da157ebc24bc7d70c26d198381e042ab93434757752e3f0ee8e5
(1) 0xfc953f0b751d43d825c8bdbe0cb96b5399785d63d665a8a08d4402d1744651eb
(2) 0xb29daad2e0a03bbe925d600a2096a9e4a36cc4135a5698663399049d358260da
(3) 0xd0a21a0c8bb03a589e01461470b8db428124bfd0674b2105480ea63b477d7f0f
(4) 0x2dffbad13afbc666a9c593920be3de59504923b2761cbe9a74b5b1c535fdcd45
(5) 0x9dad1cf63f92916dcb1d1e6162b32ef91622a3465e00b3cb73fe900ae6050272
(6) 0xda5ea07f6dd402aafeae6a079bef862e18ed2c2da2e7a8001434d7c19f3e9b8d
(7) 0xe9f7be5224efe8f29f849373fece147b84d041af027b91938ba6beae7c2233fd
(8) 0xaa6da5dce2bd70e453a2823a182f619d9cc1d919a044c0ade83707199c6e0018
(9) 0x800dcf712a622efcf37deff66439f1257055d50a79e2fd6414c5e8c9fd09c274

HD Wallet
==================
Mnemonic:      tutorial
Base HD Path:  m/44'/60'/0'/0/{account_index}

Gas Price
==================
20000000000

Gas Limit
==================
6721975

Call Gas Limit
==================
9007199254740991

Listening on 127.0.0.1:8545
>

migrations/2_deploy_smart_contract.js 파일 생성, 내용작성

 

 

 

truffle migrate

배포할수 있게 함

C:\Users\coco\Desktop\coco\studying_solidity\lotteryDapp>truffle migrate

Compiling your contracts...
===========================
> Compiling .\contracts\Lottery.sol
> Compiling .\contracts\Migrations.sol
> Compilation warnings encountered:

    Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
--> project:/contracts/Lottery.sol


> Artifacts written to C:\Users\coco\Desktop\coco\studying_solidity\lotteryDapp\build\contracts
> Compiled successfully using:
   - solc: 0.8.11+commit.d7f03943.Emscripten.clang

> Something went wrong while attempting to connect to the network at http://127.0.0.1:7545. Check your network configuration.

Could not connect to your Ethereum client with the following parameters:
    - host       > 127.0.0.1
    - port       > 7545
    - network_id > 5777
Please check that your Ethereum client:
    - is running
    - is accepting RPC connections (i.e., "--rpc" or "--http" option is used in geth)
    - is accessible over the network
    - is properly configured in your Truffle configuration file (truffle-config.js)

Truffle v5.4.32 (core: 5.4.32)
Node v16.14.0

배포가 안된 이유

- 어디에다 배포할 지 설정을 안 해 줌.

- truffle-config.js 에서 설정이 필요

 

truffle-config.js

- 트러플 환경에서 어떤 셋팅을 사용할 건지에 대한 설정을 할 수 있음.

- 개발환경 사용을 위해 해당 부분 주석 해제.

- 기본적으로 배포되는 환경정보를 담고 있음.

 

truffle migrate

C:\Users\coco\Desktop\coco\studying_solidity\lotteryDapp>truffle migrate

Compiling your contracts...
===========================
> Compiling .\contracts\Lottery.sol
> Compiling .\contracts\Migrations.sol
> Compilation warnings encountered:

    Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
--> project:/contracts/Lottery.sol


> Artifacts written to C:\Users\coco\Desktop\coco\studying_solidity\lotteryDapp\build\contracts
> Compiled successfully using:
   - solc: 0.8.11+commit.d7f03943.Emscripten.clang



Starting migrations...
======================
> Network name:    'development'
> Network id:      1644740725728
> Block gas limit: 6721975 (0x6691b7)


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

   Deploying 'Migrations'
   ----------------------
   ⠋ Blocks: 0            Seconds: 0   > transaction hash:    0x95e92fc455102809554b596cc0a8fc037c4a6e8b76568d081b6c380082c3d7fc
   > Blocks: 0            Seconds: 0
   > contract address:    0xC8cbDe94d91791d7Fd2282c04C021271449eFc4D
   > block number:        1
   > block timestamp:     1644741316
   > account:             0xF76c9B7012c0A3870801eaAddB93B6352c8893DB
   > balance:             99.99502316
   > gas used:            248842 (0x3cc0a)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00497684 ETH


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


2_deploy_smart_contract.js
==========================

   Deploying 'Lottery'
   -------------------
   ⠋ Blocks: 0            Seconds: 0   > transaction hash:    0x9f17cbfbd9c55cc8398603334f47e8ef91f06a7f47fbf313875ed67f763e9c8d
   > Blocks: 0            Seconds: 0
   > contract address:    0xC89C4883D9206f011cC10AeB06558845BCe8Ddfd
   > block number:        3
   > block timestamp:     1644741317
   > account:             0xF76c9B7012c0A3870801eaAddB93B6352c8893DB
   > balance:             99.99283158
   > gas used:            67066 (0x105fa)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00134132 ETH


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

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

 

truffle migrate --reset

-  truffle migrate은 변경된 부분에 대해서만 하기 때문에, 배포 후 다시 배포할 때는 뒤에 --reset 옵션을 넣어줌.

 

 

contracts/Migrations.sol

-스마트 컨트랙트의 버전관리 역할

-트러플에서 사용하는 툴 혹은 컨트랙트, 내가 몇번째 디플로이먼트 스크립트까지 사용했는지 확인가능.

-건드리지 않는 게 좋음