[Reboot 이타인클럽] #2 컨트랙트 빌드 with Truffle
이타인클럽 도움 주고 받기를 위한 ERC 토큰을 만들어 보려고 합니다.
이전글 - [Reboot 이타인클럽] #1 ERC777 토큰 컨트랙트 테스트
도움 주고 받기 앱 helpus
- 구글 플레이: https://play.google.com/store/apps/details?id=club.etain.helpus
- 애플 앱 스토어: https://apps.apple.com/us/app/helpus-instant-help-in-town/id1496615309
Truffle로 스마트 컨트랙트 빌드
이전에 아래와 같이 샘플 토큰을 만들었습니다. 별거 없죠.
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import "../node_modules/@openzeppelin/contracts/token/ERC777/ERC777.sol";
contract EtainClubToken is ERC777 {
// constructor
constructor(
uint256 initialSupply,
address[] memory defaultOperators
)
ERC777("EtainClub", "ECT", defaultOperators)
public
{
_mint(msg.sender, initialSupply, "", "");
}
}
이걸 truffle로 빌드해 보겠습니다.
먼저 로컬 블록체인을 사용하기 위해 다음과 같이 truffle-config.js 파일을 열어서 수정합니다.
module.exports = {
networks: {
developoment: { // Whatever network our local node connects to
network_id: "*", // Match any network id
host: "localhost",
port: 8545,
},
ganache: { // Ganache local test RPC blockchain
network_id: "5777",
host: "localhost",
port: 7545,
gas: 6721975,
},
}
};
이렇게 하고 빌드를 하면 에러가 발생합니다!
$ truffle compile
Compiling your contracts...
===========================
> Compiling ./contracts/EtainToken.sol
> Compiling ./contracts/Migrations.sol
/home/etain/devel/erc-test/contracts/EtainToken.sol:3:1: ParserError: Source file requires different compiler version (current compiler is 0.5.16+commit.9c3226ce.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
pragma solidity ^0.6.0;
^---------------------^
Error: Truffle is currently using solc 0.5.16, but one or more of your contracts specify "pragma solidity ^0.6.0".
Please update your truffle config or pragma statement(s).
(See https://truffleframework.com/docs/truffle/reference/configuration#compiler-configuration for information on
configuring Truffle to use a specific solc compiler version.)
Compilation failed. See above.
Truffle v5.1.32 (core: 5.1.32)
이유는 최신 truffle이 solidity 0.6.0 버전을 지원하지 않기 때문입니다. openzeppelin 코드가 0.6.0 이상의 버전을 필요로 하기 때문에 꼭 0.6.0 이상을 사용해야만 하는 상황입니다.
$ truffle version
Truffle v5.1.32 (core: 5.1.32)
Solidity - ^0.5.16 (solc-js)
Node v12.14.0
Web3.js v1.2.1
사실은 지원안한다기 보다는 공식 릴리스 버전에 포함된 solidity complier는 0.5.16 버전인 것입니다.
다음과 같이 truffle-config.js 파일을 열어서 solidity 버전을 설정합니다.
module.exports = {
networks: {
developoment: { // Whatever network our local node connects to
network_id: "*", // Match any network id
host: "localhost",
port: 8545,
},
ganache: { // Ganache local test RPC blockchain
network_id: "5777",
host: "localhost",
port: 7545,
gas: 6721975,
},
},
compilers: {
solc: {
version: "^0.6.0"
}
}
};
이렇게 해주면 truffle이 적당한 solidity 버전을 다운로드하여 컴파일 합니다.
$ truffle compile
Compiling your contracts...
===========================
✔ Fetching solc version list from solc-bin. Attempt #1
✔ Downloading compiler. Attempt #1.
> Compiling ./contracts/EtainToken.sol
> Compiling ./contracts/Migrations.sol
> Compiling ./node_modules/@openzeppelin/contracts/GSN/Context.sol
> Compiling ./node_modules/@openzeppelin/contracts/introspection/IERC1820Registry.sol
> Compiling ./node_modules/@openzeppelin/contracts/math/SafeMath.sol
> Compiling ./node_modules/@openzeppelin/contracts/token/ERC20/IERC20.sol
> Compiling ./node_modules/@openzeppelin/contracts/token/ERC777/ERC777.sol
> Compiling ./node_modules/@openzeppelin/contracts/token/ERC777/IERC777.sol
> Compiling ./node_modules/@openzeppelin/contracts/token/ERC777/IERC777Recipient.sol
> Compiling ./node_modules/@openzeppelin/contracts/token/ERC777/IERC777Sender.sol
> Compiling ./node_modules/@openzeppelin/contracts/utils/Address.sol
✔ Fetching solc version list from solc-bin. Attempt #1
> Artifacts written to /home/etain/devel/erc-test/build/contracts
> Compiled successfully using:
- solc: 0.6.10+commit.00c0fcaf.Emscripten.clang
위 첫줄을 보면 다음과 같이 solidity compiler를 다운로드 하는 것을 볼 수 있습니다.
✔ Fetching solc version list from solc-bin. Attempt #1
✔ Downloading compiler. Attempt #1.
마지막으로 truffle version을 다시 확인해 보겠습니다.
Truffle v5.1.32 (core: 5.1.32)
Solidity - ^0.6.0 (solc-js)
Node v12.14.0
Web3.js v1.2.1
짜잔, solidity 버전이 ^0.6.0으로 변경되어 있습니다.
다음에는 컨트랙트를 로컬 블록체인에 deploy하고 테스트 하는 부분을 설명해 보겠습니다.