1. tạo file genesis.json
{ "config": { "chainId": 15, "homesteadBlock": 0, "eip150Block": 0, "eip155Block": 0, "eip158Block": 0, "byzantiumBlock": 0, "constantinopleBlock": 0 }, "difficulty": "400000", "gasLimit": "2100000", "alloc": {} }
2. khởi tạo blockchain: geth --datadir . init genesis.json
3. khởi động node geth --syncmode fast --allow-insecure-unlock --cache 512 --ipcpath ~/Library/Ethereum/geth.ipc --networkid 1234 --datadir . --http --http.api "personal,eth,net,web3,miner" --http.corsdomain "*" --nodiscover
4. tạo account đầu tiên: geth --datadir . account new
5. khởi động geth console: geth attach http://localhost:8545/
5.1 chạy lệnh để kiểm tra balance: eth.getBalance(eth.accounts[0])
5.2 chạy lệnh để unlock account: personal.unlockAccount(eth.accounts[0])
5.3 chạy lệnh để set ethbase: miner.setEtherbase(eth.accounts[0])
5.4 start miner: miner.start()
6. tạo thư mục truffle: mkdir truffle
7. cd truffle
8. khởi tạo module, chạy lệnh: truffle init
. Kết quả chúng ta có thự mục với cấu trúc:
9. 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; } }
10. Cập nhât file truffle-config.js
module.exports = { networks: { development: { host: "localhost", port: 8545, network_id: "*", gas: 1192085, from: "0x3c31b4b9d6c24bad29498f99aaa6914231362c7f" } } };
11. Chạy lệnh:
truffle compile
12. Chạy lệnh:
truffle migrate
Nếu gặp lỗi này: "Migrations" -- Returned error: authentication needed: password or unlock.
thì chạy lại lệnh: personal.unlockAccount(eth.accounts[0])
13. Gọi đến contract thông qua truffle, chạy lệnh: truffle console
14. Gọi thử contract vừa tạo:
let contract = await Migrations.deployed() contract.Hello() contract.message.call()
Kết quả chúng ta có:
truffle(development)> contract.Hello() { tx: '0xdf5fa89e5e71fac06ae50cdf4e44ac91e0c726d89176d838cc4b7efbb9e23402', ..... logs: [] }
truffle(development)> contract.message() 'Hello World!'
Các link tham khảo:
1. https://medium.com/datawallet-blog/how-to-deploy-a-local-private-ethereum-blockchain-c2b497f068f4
2. https://www.edureka.co/blog/ethereum-smart-contract-project
3. https://github.com/ethereum/go-ethereum/issues/20676
4. https://www.trufflesuite.com/docs/truffle/getting-started/interacting-with-your-contracts
5. https://ethereum.stackexchange.com/questions/87245/error-returned-error-invalid-opcode-shr/103943
6. https://stackoverflow.com/questions/67366053/geth-private-network-return-error-invalid-opcode-selfbalance-when-executing-a