- 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": {}
}
- khởi tạo blockchain:
geth --datadir . init genesis.json
- 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
- tạo account đầu tiên:
geth --datadir . account new
- khởi động geth console:
geth attach http://localhost:8545/
- 1 chạy lệnh để kiểm tra balance:
eth.getBalance(eth.accounts[0])
- 2 chạy lệnh để unlock account:
personal.unlockAccount(eth.accounts[0])
- 3 chạy lệnh để set ethbase:
miner.setEtherbase(eth.accounts[0])
- 4 start miner:
miner.start()
- tạo thư mục truffle:
mkdir truffle
cd truffle
- 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:
- 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;
}
}
- Cập nhât file
truffle-config.js
module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*",
gas: 1192085,
from: "0x3c31b4b9d6c24bad29498f99aaa6914231362c7f"
}
}
};
- Chạy lệnh:
truffle compile
- 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])
- Gọi đến contract thông qua truffle, chạy lệnh:
truffle console
- 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!'