이더리움에서 널리 사용하고있는 스마트컨트렉트 배포툴인 Truffle를 사용하고자 하시는 분들이 많이 있습니다. 클레이튼 또한 이더리움과 호환이 되기 때문에 Truffle를 사용가능합니다.
하지만 web3를 사용하는 기존 hdwallet-provider
에서 Caver를 사용하는 truffle-hdwallet-provider-klaytn
로 변경이 필요합니다. (참조: npmjs package)
그러기 위해서는 truffle-hdwallet-provider-klaytn
설치 및 truffle-config.js
를 수정해주셔야 합니
다.
1. truffle-hdwallet-provider-klaytn 설치
$ npm install truffle-hdwallet-provider-klaytn
2. truffle-config.js 설정
기본 truffle-config.js
파일에서 아래와 같이 truffle-hdwallet-provider-klaytn
을 추가 하십니다.
// const HDWalletProvider = require('@truffle/hdwallet-provider');
// const infuraKey = "fj4jll3k.....";
//
// const fs = require('fs');
// const mnemonic = fs.readFileSync(".secret").toString().trim();
const HDWalletProvider = require("truffle-hdwallet-provider-klaytn");
const privateKey = "0x4db5a...72c85";
const mnemonic = "mountains supernatural bird ...";
module.exports = {
/**
* Networks define how you connect to your ethereum client and let you set the
* defaults web3 uses to send transactions. If you don't specify one truffle
* will spin up a development blockchain for you on port 9545 when you
* run `develop` or `test`. You can ask a truffle command to use a specific
* network from the command line, e.g
*
* $ truffle test --network <network-name>
*/
networks: {
testnet_pk: {
provider: () => new HDWalletProvider(privateKey, "https://api.baobab.klaytn.net:8651"),
network_id: '1001', //Klaytn baobab testnet's network id
gas: '8500000',
gasPrice: null
},
testnet_mne: {
provider: () => new HDWalletProvider(mnemonic, "https://api.baobab.klaytn.net:8651"),
network_id: '1001', //Klaytn baobab testnet's network id
gas: '8500000',
gasPrice: null
},
...
3. Deploy 하기
이외에는 기존 truffle 사용과 동일하게 contract를 추가하신후 privatekey 또는 mnemonic에 따라 알맞은 네트워크를 통해 deploy 하시면 됩니다.
보다 자세한 내용을 링크를 참조 부탁드립니다.
> truffle deploy --reset --network testnet_pk
> truffle deploy --reset --network testnet_mne
4. 실행 결과 예시
> truffle deploy --reset --network testnet_pk
Compiling your contracts...
===========================
> Compiling ./contracts/ERC721/Address.sol
> Compiling ./contracts/ERC721/Counters.sol
> Compiling ./contracts/ERC721/ERC165.sol
> Compiling ./contracts/ERC721/ERC721.sol
> Compiling ./contracts/ERC721/ERC721Enumerable.sol
> Compiling ./contracts/ERC721/IERC165.sol
> Compiling ./contracts/ERC721/IERC721.sol
> Compiling ./contracts/ERC721/IERC721Enumerable.sol
> Compiling ./contracts/ERC721/IERC721Receiver.sol
> Compiling ./contracts/ERC721/SafeMath.sol
> Compiling ./contracts/Klaystagram.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /Users/ethan/WebstormProjects/caver_test/hello-klaytn/build/contracts
> Compiled successfully using:
- solc: 0.5.6+commit.b259423e.Emscripten.clang
Starting migrations...
======================
> Network name: 'testnet_pk'
> Network id: 1001
> Block gas limit: 0x0
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0x316c9c5eee6049c3d623289696b214739da2c3da81c5551109baff578f37646b
> Blocks: 0 Seconds: 0
> contract address: 0x3E246b87E03A6d6849AB99514c15B33142E6d7d2
> block number: 21302395
> block timestamp: 1582852343
> account: 0x9BD343f1c9E3Ae3a5D3e257A2091285DB6BA08F8
> balance: 143714.769073225
> gas used: 186071
> gas price: 25 gwei
> value sent: 0 ETH
> total cost: 0.004651775 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.004651775 ETH
2_deploy_contract.js
====================
Deploying 'Klaystagram'
-----------------------
> transaction hash: 0xa331d1594cce590571fe150f952231533d6e4b8197b2ffe024bda860528db8ea
> Blocks: 0 Seconds: 0
> contract address: 0x902db6E29a7A7cC588B109C60B915E4e923D9a73
> block number: 21302400
> block timestamp: 1582852348
> account: 0x9BD343f1c9E3Ae3a5D3e257A2091285DB6BA08F8
> balance: 143714.68396
> gas used: 3362528
> gas price: 25 gwei
> value sent: 0 ETH
> total cost: 0.0840632 ETH
⠋ Saving migration to chain.The abi of Klaystagram is recorded on deployedABI file
The deployed contract address * 0x902db6E29a7A7cC588B109C60B915E4e923D9a73 * is recorded on deployedAddress file
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.0840632 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.088714975 ETH