Để deploy contract lên mạng Ropsten test net, chúng ta cần thực hiện các bước sau:
- Tạo project truffle
- 1. tạo thư mục truffle: <code>mkdir truffle</code>
- 2. <code>cd truffle</code>
- 3. <code>truffle init</code>
Cập nhật file <code>Migrations.sol</code>
// 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;
}
}
- Tạo 1 tài khoản trên metamask và hãy lưu lại seedphase vào file <code>.secret</code> vào trong project <code>truffle</code>
- 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
- Tạo một dự án tại <a href="https://infura.io/" target="_blank">https://infura.io/
Bỏ comment và sửa đoạn này trong file <code>truffle-config.js</code>
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
}
- Compile contract: <code>truffle compile</code>
- Deploy contract: <code>truffle migrate --network ropsten</code>
- Kết quả chúng ta nhận được:
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: <a href="https://ropsten.etherscan.io/tx/0x5f95932408cfcb54c0522637d17ce6f2fd2ce1ef5829139a3c169fbce91f9f71" target="_blank">0x5f95932408cfcb54c0522637d17ce6f2fd2ce1ef5829139a3c169fbce91f9f71</a>
Summary
=======
> Total deployments: 1
> Final cost: 0.000418759097796939 ETH
Tham khảo:
- <a href="https://www.trufflesuite.com/guides/using-infura-custom-provider" target="_blank">https://www.trufflesuite.com/guides/using-infura-custom-provider
- <a href="https://www.geeksforgeeks.org/deploying-smart-contract-on-test-main-network-using-truffle/" target="_blank">https://www.geeksforgeeks.org/deploying-smart-contract-on-test-main-network-using-truffle/