Để deploy contract lên mạng Ropsten test net, chúng ta cần thực hiện các bước sau:
1. Tạo project truffle
1.1. tạo thư mục truffle: mkdir truffle
1.2. cd truffle
1.3. truffle init
2. Cập nhật file Migrations.sol
// SPDX-License-Identifier: MIT pragma solidity >=0.4.22 <0.9.0; contract Migrations { string public message; uint public last_completed_migration; function Hello() public{ message = "Hello World!"; } function setCompleted(uint completed) public{ last_completed_migration = completed; } }
3. Tạo 1 tài khoản trên metamask và hãy lưu lại seedphase vào file .secret
vào trong project truffle
4. Request eth từ các nhà hảo tâm, nếu bạn cần hãy để lại tài ở comment khoản và mình sẽ chuyển cho bạn
5. Tạo một dự án tại https://infura.io/
6. Bỏ comment và sửa đoạn này trong file truffle-config.js
ropsten: { provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/{projectId vừa tạo}`), network_id: 3, gas: 5500000, confirmations: 2, timeoutBlocks: 200, skipDryRun: true }
7. Compile contract: truffle compile
8. Deploy contract: truffle migrate --network ropsten
9. Kết quả chúng ta nhận được:
1_initial_migration.js ====================== Deploying 'Migrations' ---------------------- > transaction hash: 0x5f95932408cfcb54c0522637d17ce6f2fd2ce1ef5829139a3c169fbce91f9f71 Summary ======= > Total deployments: 1 > Final cost: 0.000418759097796939 ETH
Tham khảo:
1. https://www.trufflesuite.com/guides/using-infura-custom-provider
2. https://www.geeksforgeeks.org/deploying-smart-contract-on-test-main-network-using-truffle/
Sharing