This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
Currently, two official plugins are available:
- @vitejs/plugin-react uses Babel for Fast Refresh
- @vitejs/plugin-react-swc uses SWC for Fast Refresh
If you are developing a production application, we recommend using TypeScript with type-aware lint rules enabled. Check out the TS template for information on how to integrate TypeScript and typescript-eslint in your project.
MetaVote 允許投票者創建簽名,由主席代付 gas 費用執行投票交易。
// 安裝依賴
npm install ethers
// 引入合約 ABI
const ballotContract = new ethers.Contract(contractAddress, ABI, signer);// 獲取用戶當前 nonce
const nonce = await ballotContract.getUserNonce(userAddress);
// 獲取網絡信息
const network = await provider.getNetwork();
const chainId = network.chainId;const domain = {
name: "Ballot",
version: "1",
chainId: chainId,
verifyingContract: contractAddress,
};
const types = {
MetaVote: [
{ name: "voter", type: "address" },
{ name: "proposal", type: "uint256" },
{ name: "nonce", type: "uint256" },
],
};
const value = {
voter: userAddress,
proposal: proposalIndex,
nonce: nonce,
};
const signature = await signer._signTypedData(domain, types, value);// 主席調用 metaVote
const tx = await ballotContract
.connect(chairperson)
.metaVote(userAddress, proposalIndex, nonce, signature);- Nonce 管理:每次簽名後 nonce 會自動增加,防止重放攻擊
- 時間窗口:只能在投票期間內執行
- 權限檢查:用戶必須有投票權且未投票
- 簽名驗證:使用 EIP-712 標準確保簽名安全
InvalidNonce():nonce 不匹配InvalidSignature():簽名無效NoRightToVote():無投票權或已投票NotWithinVotingPeriod():不在投票時間內
參見 metaVoteExample.js 文件中的完整實作範例。