最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

compilation - `pragma solidity` is giving error, even though I added the latest version - Stack Overflow

programmeradmin0浏览0评论
// 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;
    }
}

Here is my contract but when I tried to compile it, it shows:

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  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

Everything is up to date, there is nothing to compile.

i have tried all the pragma solidity version even 0.8.21 but still its even me the same result

// 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;
    }
}

Here is my contract but when I tried to compile it, it shows:

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. 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

Everything is up to date, there is nothing to compile.

i have tried all the pragma solidity version even 0.8.21 but still its even me the same result

Share Improve this question edited Nov 23, 2024 at 17:21 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Nov 20, 2024 at 7:49 JabirPapaJabirPapa 111 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 2

I am not sure you didn't show the hardhat.config.ts file, but I think this is the solidity compiler version error. I mean solidity compiler version in hardhat.config.ts file should be the same as solidity version in the contract.

In your BankAccount contract file, you use the exact same version as specified in hardhat.config.ts.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

Your hardhat config file already has the correct version setting;

const config: HardhatUserConfig = {
  solidity: {
    version: "0.8.21",
    settings: {
      optimizer: {
        enabled: true,
        runs: 100,
      },
      // viaIR: true,
    },
  },
  networks: {
    sepolia: {},
    ...
    }
   ...
}
发布评论

评论列表(0)

  1. 暂无评论