I. Dựng project cơ bản
1. Dựng project React
npx create-react-app demo-project
- Từ giờ, các lệnh cmd sẽ sử dụng trong folder demo-project
2. Cài đặt truffle
https://trufflesuite.com/
npm install truffle -g
- Tạo project react, cài đặt thêm truffle
truffle init
- Tại file ./truffle-config.js
1 2 3 4 5 |
development: { host: "127.0.0.1", port: 7545, // Đoạn này phải đổi sang cổng 7545 cho giống bên ganache network_id: "*", }, |
3. Cài đặt ứng dụng: Ganache
https://trufflesuite.com/ganache/
- Tạo workspace mới --> trỏ đến file truffle-config.js (trong project react)
- Chạy đoạn migrate đầu tiên
truffle migrate
Sử dụng truffle
1. Tạo file contract (solidity) tiếp theo trong thư mục contract
2. Compile ra file json
truffle compile
3. Tạo file migrate
- Tên file bắt đầu bằng con số: 1_, 2_
4. Migrate: đưa lên host hay gì gì đó
truffle migrate
5. Vào ứng dụng ganache kiếm tra
6. Khi thay đổi code file .sol cần migrate lại
truffle migrate --reset
-- Xem list accounts (trong console truffle)
accounts
Truy cập biến bằng truffle
1. Luôn bật ganache
2. Dùng terminal
truffle console
3. Bắt đầu dùng console
1 2 3 4 5 6 7 8 9 |
// Tạo biến const instance = await Faucet.deployed() // Show đối tượng instance // lấy biến trong đối tượng const snd = await instance.so_nguyen_duong() // xem biến snd snd.toString() |
4. Có thể dùng console với web3
1 2 3 4 5 6 7 8 |
//Faucet.abi là constract, địa chỉ đằng sau là địa chỉ của contract đó (xem phần contract trên ganache) const instance = new web3.eth.Contract(Faucet.abi, "0x24DB0ca0B537B89113Bfcd300D4Ee36167789682") //xem instance instance // lấy 1 biến const snd = await instance.methods.so_nguyen_duong().call() // xem biến đó snd |
Chuyển tiền bằng web3
Vẫn dùng console
web3.eth.sendTransaction({from: accounts[0], to: "0x6cF16A6f606E0dBDe888594515f148b83B348333", value: "10000000000000000000"})
web3.eth.sendTransaction({from: accounts[0], to: "0x6cF16A6f606E0dBDe888594515f148b83B348333", value: "10000000000000000000"})