[Solidity] 230714 study
1. Introduction
- 그렇게 작성한 smart contract를 어떻게 deploy할 것인가?
- Truffle이 smart contract compilation 뿐 아니라 ABI generation도 해준다
- 다음 명령어로 다운로드
npm install truffle -g-g옵션은 globally available한 상태로 install 하기 위한 옵션이다
2. Getting Started with Truffle
Truffle’s Default Directory Structure
-
truffle init을 돌리면 생성되는 파일과 폴더들은 다음과 같다- contracts
- smart contract들이 모두 들어 있는 폴더.
Migrations.sol은truffle init에 의해 자동적으로 생성된다
- migrations
- migration은 Truffle에게 smart contract를 어떻게 deploy할 지 알려주는 JS 파일이다
- test
- unit test를 위한 JS file, solidity file이 저장된다
- smart contract를 deploy한 후에는 변경이 불가능하기 때문에, deploy 전 test는 필수다
- truffle.js & truffle-config.js
-
기본적인 directory structure를 변경하면 작동하지 않을 가능성이 있다.
- 기본 구조에 익숙해지면 다른 사람의 프로젝트를 이해하기 쉬워지므로 적응하도록 하자
Truffle-hdwallet-provider
- Infura를 이용하면 Ethereum 체인 위에 우리 코드를 deploy 할 수 있다
- 하지만 Infura는 private key를 보관해주지 않기 때문에 지갑 관리자가 필요하다
npm install truffle-hdwallet-provider로 설치
3. Compiling the source code
- compile 하면
pure,view로 쓸 수 있을 것 같은 경우에는 알려줌 - to save gas truffle compile로 compile
4. Migrations
- Ganache에서 테스트 해 볼 수 있다. 그런데 이 테스트 배우는 것도 시간이 필요하니 우선 deployment에만 집중해보자
5. Configuration Files
- deploy할 때는 Truffle에게 우리가 deploy할 network에 대해 명시해줘야 하므로, config file을 만져줘야 한다
6. Deploying Our Smart Contract
- migration은 복잡해 보이지만 Truffle에게 smart contract가 어떻게 변하는지 알려주는 방법일 뿐이다
truffle migrate --network rinkeby와 같이 migrate
7. Use Truffle with Loom!
npm install loom-truffle-provider- Loom: multichain interoperability platform
8. Deploy to Loom Testnet
-
loom_testnet: { provider: function() { const privateKey = 'YOUR_PRIVATE_KEY' const chainId = 'extdev-plasma-us1'; const writeUrl = 'http://extdev-plasma-us1.dappchains.com:80/rpc'; const readUrl = 'http://extdev-plasma-us1.dappchains.com:80/query'; return new LoomTruffleProvider(chainId, writeUrl, readUrl, privateKey); }, network_id: '9545242630824' }
9. Deploy to Loom (cont’d)
truffle migrate --network loom_testnet
10. Deploy to Basechain
-
과정
- 새로운 private key 발급
- 새 private key를 보안을 고려해 truffle에게 전달
truffle.js를 수정해 Truffle에게 basechain을 전달- basechain에 deploy
-
basechain: { provider: function() { const chainId = 'default'; const writeUrl = 'http://basechain.dappchains.com/rpc'; const readUrl = 'http://basechain.dappchains.com/query'; const privateKeyPath = path.join(__dirname, 'mainnet_private_key'); const loomTruffleProvider = getLoomProviderWithPrivateKey(privateKeyPath, chainId, writeUrl, readUrl); return loomTruffleProvider; }, network_id: '*' }