Hardhat
錯誤 HH600:編譯失敗 ParserError:應為“,”但得到“;”
當我在執行 yarn hardhat compile 時遇到此錯誤時,我正在學習 chainlink hackton 課程“DeFi Staking App: Code Again”連結:
ParserError: 應為 ‘,’ 但得到 ‘;’ –> 合約/Staking.sol:59:24: | 59 | 過去的獎勵;| ^
錯誤 HH600:編譯失敗
// SPDX-License-Identifier: MIT pragma solidity ^0.8.7; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; error Staking__TransferFailed(); error Stakig__NeedsMoteThanZero(); contract Staking { IERC20 public s_stakingToken; ERC20 public s_rewardToken; // somones address -> how much they staked mapping(address => uint256) public s_balances; // mapping of how much each address has been paid mapping(address => uint256) public s_userRewardPerTokenPaid; // a mapping of how much rewards each address has to claim mapping(address => uint256) public s_rewards; uint256 public constant REWARD_RATE =100; uint256 public s_totalSupply; uint256 public s_rewardPerTokenStored; uint256 public s_lastUpdateTime; modifier updateReward(address account){ s_rewardPerTokenStored = rewardPerToken(); s_lasrUpdateTime = block.timestamp; s_rewards[account] = earned(account); s_userRewardPerTokenPaid[account] = s_rewardPerTokenStored; _; } modifier moreThanZero(uint256 amount){ if( amount ==0){ revert Stakig__NeedsMoteThanZero(); } _; } constructor(address stakingToken, address rewardToken) { s_stakingToken = IER20(stakingToken); s_rewardToken = IER20(rewardToken); } function earned(address account) public view returns (uint256){ uint256 currentBlanace = s_balances[account]; // how much they have paid alredy uint256 amountPaid = s_userRewardPerToken[account]; uint256 currentRewardPerToken = rewardPerToken(); uint256 pastRewards = s_rewards[account]; uint256 _earned = ((currentBalance *(currentRewardPerToken - amountPaid)/1e18) + pastRewards; return _earned; } function rewardPerToken public view returns(uint256) { if (s_totalSupply ==0){ return s_rewardPerTokenStored; } return s_rewardPerTokenStored + (((block.timestamp -s_lastUpdateTime) * REWARD_RATE * 1e18/ s_totalSupply); } function stake(uint256 amount) external updateReward(msg.sender) moreThanZero(amount) { s_balances[msg.sender] = s_balances[msg.sender] + amount; s_totalSupply = s_totalSupply + amount; // emit event bool success = s_stakingToken.transferFrom(msg.sender, address(this), amount); // require(success, "Failed"); if(!success) { revert Staking__TransferFailed(); } }
}
function withdraw(uint256 amount) external updateReward(msg.sender) moreThanZero(amount) { s_balances[msg.sender] = s_balances[msg.sender] - amount; s_totalSupply = s_totalSupply - amount; bool success = s_stakingToken.transfer(msg.sender, amount); if(!success) { revert Staking__TransferFailed(); } } function claimReward() external updateReward(msg.sender) { uint256 reward = s_rewards[msg.sender]; bool succes = s_rewardToken.transfer(msg.sender, reward); if(!success){ revert Staking__TransferFailed(); } }
}
這裡:
function rewardPerToken public view returns(uint256) { if (s_totalSupply ==0){ return s_rewardPerTokenStored; } return s_rewardPerTokenStored + (((block.timestamp -s_lastUpdateTime) * REWARD_RATE * 1e18/ s_totalSupply); }
你忘記了函式括號。它應該看起來像
function rewardPerToken()