// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract BankAccount {
uint private balance;
// Deposit function: adds an amount to the balance
function deposit(uint amount) public {
balance += amount;
}
// Withdraw function: deducts an amount from the balance
function withdraw(uint amount) public {
require(amount <= balance, 'Insufficient balance');
balance -= amount;
}
// View function to check balance
function getBalance() public view returns (uint) {
return balance;
}
// Pure function to calculate simple interest
function calculateInterest(uint principal, uint rate, uint time) public pure returns (uint) {
return (principal * rate * time) / 100;
}
}
这是我的合同,但是当我尝试编译它时,它显示:
Compiling .\contracts\BankAccount.sol
> Compilation warnings encountered:
Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
--> project:/contracts/BankAccount.sol
,Warning: Source file does not specify required compiler version! Consider adding "pragma solidity ^0.8.21;"
--> project:/contracts/BankAccount.sol
一切都是最新的,没有什么需要编译的。
我已经尝试了所有 pragma Solidity 版本,甚至是 0.8.21,但即使是我,结果仍然相同
我不确定你没有显示
hardhat.config.ts
文件,但我认为这是solidity编译器版本错误。
我的意思是 hardhat.config.ts
文件中的 Solidity 编译器版本应该与合同中的 Solidity 版本相同。
hardhat.config.ts
中指定的完全相同的版本。// SPDX-License-Identifier: MIT pragma solidity 0.8.21;
const config: HardhatUserConfig = { solidity: { version: "0.8.21", settings: { optimizer: { enabled: true, runs: 100, }, // viaIR: true, }, }, networks: { sepolia: {}, ... } ... }