npm install -g truffle
truffle init
npm install -g ganache-cli
ganache-cli version
ganache-cli version을 실행하면 아래의 화면이 출력되고 testRPC에 접속되어 가상의 블록체인 네트워크와 상호작용을 할 수 있다.
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*"
}
},
compilers: {
solc: {
version: "0.8.13"
}
}
};
"가나슈는 testRPC의 새로운 이름이다."
truffle suite 사용 시
truffle-config.js -> Port 7545로 수정
Ganache 실행 후 truffle-config.js 프로젝트 불러오기
이더리움넷에 스마트컨트랙트를 배포하기 위해서는
컴파일 -> 디플로이 과정을 거쳐야하는데 다음과 같다.
- contract/ .sol 컨트랙트 파일 생성
// SPDX-License-Identifier: MIT pragma solidity >=0.4.22 <0.9.0; contract Faucet { uint public funds = 1000; // 양수만 가능 int public counter = 0; uint32 public test = 4294967295; }
- truffle migrate
- migration/ .js 마이그레이션 파일 생성
const Migrations = artifacts.require("Migrations"); module.exports = function (deployer) { deployer.deploy(Migrations); };
truffle migrate 를 실행하면 컨트랙트와 마이그레이션이 진행되고
작성한 스마트컨트랙트가 testRPC에 배포되며 채굴 정보가 업데이트 된다.
Compiling your contracts...
===========================
✓ Fetching solc version list from solc-bin. Attempt #1
✓ Downloading compiler. Attempt #1.
> Compiling ./contracts/Migrations.sol
> Artifacts written to /Users/jidoil/study/solidity/build/contracts
> Compiled successfully using:
- solc: 0.8.13+commit.abaa5c0e.Emscripten.clang
Starting migrations...
======================
> Network name: 'development'
> Network id: 5777
> Block gas limit: 6721975 (0x6691b7)
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
✓ Transaction submitted successfully. Hash: 0xddb03a9a328d9eb41405c9a554b94e756e8207188f4f09027ad89d683cbe0e1a
> transaction hash: 0xddb03a9a328d9eb41405c9a554b94e756e8207188f4f09027ad89d683cbe0e1a
> Blocks: 0 Seconds: 0
> contract address: 0xE2BaE6Cf486E932409d74a509920ce42370656Ea
> block number: 1
> block timestamp: 1649288487
> account: 0x9972d837705931317eE4b11D353ceA312064F741
> balance: 99.99502292
> gas used: 248854 (0x3cc16)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00497708 ETH
✓ Saving migration to chain.
✓ Transaction submitted successfully. Hash: 0xfbbf7b6e89ff07ea5252fdf6d907d0df45787d269b24a578745773ac867b7fb9
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00497708 ETH
Summary
=======
> Total deployments: 1
> Final cost: 0.00497708 ETH
아래와 같이 테스트RPC에 스마트컨트랙트가 디플로이 된것을 확인 할 수 있다.