0

enter image description here

The receive function shows an error while compiling in truffle.

//SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.5.0 <0.9.0;

contract Lottery {
    address payable[] public players;
    address public manager;

    constructor() {
        manager = msg.sender;
    }
    

//Error Function

    receive() external payable{
        require(msg.value == 0.1 ether);
        players.push(payable(msg.sender));
    }

    function getBalance() public view returns (uint256) {
        require(msg.sender == manager);
        return address(this).balance;
    }

    function random() internal view returns (uint256) {
        return
            uint256(
                keccak256(
                    abi.encodePacked(
                        block.difficulty,
                        block.timestamp,
                        players.length
                    )
                )
            );
    }

    function pickWinner() public {
        require(msg.sender == manager);
        require(players.length >= 3);

        uint256 r = random();
        address payable winner;

        uint256 index = r % players.length;

        winner = players[index];

        winner.transfer(getBalance());

        players = new address payable[](0);
    }

}

ParserError: Expected identifier but got '(' receive() external payable{ ^

2
  • 1
    This contract is fine, no errors. Your issue comes from another place, please post the full compilation result.
    – Kof
    May 4, 2022 at 20:12
  • @Kof I've added the compilation error to go through. May 5, 2022 at 6:32

1 Answer 1

2

You're probably using an old version of solidity to compile the contract, receive is a keyword of the language that was added around 0.6.x.

https://blog.soliditylang.org/2020/03/26/fallback-receive-split/

To use version 0.8.x for instance, you first have to update your code -

pragma solidity >=0.8.0 <0.9.0;

In your truffle config file, you can specify the solidity version to use -

  compilers: {
    solc: {
      version: "^0.8.0"
    }
  }

See Truffle Configuration docs here.

5
  • I changed my solidity version to 0.8.0 but when I check it with truffle version it still shows a solc version of 0.5.16. I reinstalled truffle but the version remains the same. Soo no luck.. May 5, 2022 at 14:44
  • Added how to set solc version to my answer
    – Kof
    May 5, 2022 at 14:51
  • I already changed it to 0.8.0 in config file. But now it shows Source file requires different compiler version (current compiler is 0.5.16+commit.9c3226ce.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version pragma solidity ^0.8.0; I have reinstalled solc and truffle many times but the version still sows 0.5.16 for some reason. May 5, 2022 at 15:03
  • That's because your code requires support of version >=0.5.0 as defined in your 3rd line of code
    – Kof
    May 5, 2022 at 15:19
  • I actually had an inner directory with a config file where I didn't update my truffle version. Works fine now, thanks! May 6, 2022 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.