Sử dụng Truffle cơ bản

Author: - Posted: 22/04/22 - Update: 07/08/22

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

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

4. Có thể dùng console với web3

Chuyển tiền bằng web3
Vẫn dùng console

web3.eth.sendTransaction({from: accounts[0], to: "0x6cF16A6f606E0dBDe888594515f148b83B348333", value: "10000000000000000000"})