Triển khai NFT chuẩn ERC721 (OpenZeppelin) sử dụng Truffle Suite + Ganache.
- Node.js (v16+ khuyến nghị)
- npm hoặc yarn
- Truffle:
npm install -g truffle - Ganache:
npm install -g ganache(hoặc dùng Ganache GUI)
truffle-nft/
├── contracts/ # Smart contract (NFT.sol)
├── migrations/ # Script deploy (migration files)
├── test/ # Unit test (Mocha/Chai)
├── truffle-config.js # Cấu hình network, compiler
└── .env # Biến môi trường (KHÔNG commit)
cd truffle-nft
npm installTạo file .env:
MNEMONIC="your twelve word seed phrase here"
SEPOLIA_RPC_URL=https://sepolia.infura.io/v3/<project_id>
ETHERSCAN_API_KEY=<your_key>Mở terminal riêng, chạy Ganache:
npx ganacheCompile contract:
truffle compileMigrate (deploy) lên mạng local:
truffle migrate --network developmenttruffle testBao gồm test event assertion (kiểm tra Transfer, Approval event khi mint/transfer NFT).
Cách 1 — console.log trong Solidity (qua Ganache):
Thêm import "hardhat/console.sol";-tương tự hoặc dùng thư viện debug hỗ trợ Truffle, log ra trong quá trình chạy test.
Cách 2 — Truffle Debugger (chain forking):
truffle debug <transaction_hash> --network developmentLưu ý: Goerli đã deprecated, dùng Sepolia thay thế.
Dùng HDWallet Provider (đã cấu hình sẵn trong truffle-config.js):
truffle migrate --network sepoliaHoặc dùng Truffle Dashboard (deploy an toàn hơn, không cần lưu private key):
truffle dashboard
# mở tab mới:
truffle migrate --network dashboardtruffle console --network sepolialet instance = await MyNFT.deployed();
await instance.mint(myAddress, tokenId);MIT