0

I have been developing a personal project and I am getting this error:

transact to Lottery.(receive) errored: VM error: revert.

Can someone help me?

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.9;

contract Lottery {
    address payable[] public players;
    address public manager;
    
    constructor() {
        manager = msg.sender;
    }
    
    receive() external payable {
        require(msg.value == 0.1 ether);
        players.push(payable(msg.sender));
    }
    
    function getBalance() public view returns(uint) {
        require(msg.sender == manager);
        return address(this).balance;
    }
    
     function random() public view returns(uint) {
        return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players.length)));
    }
}
1
  • What were you doing when the error appeared? Calling a function, deploying the contract. Were testing using remix javascript vm or an external web3 provider?
    – Ismael
    Commented Oct 26, 2021 at 21:13

1 Answer 1

0

You should not run anything in the receive function. You want to make sure that the contract can receive ether without running out of gas. Sending ether is probably the cheapest operation to do with an floor gas of 21000 unit.

This function should remain empty and just allow your contract to receive ether.You should use a function like this.

function depositEther(uint256 _amount) external payable {
     require(msg.value == 0.1 ether);  // will work because you can receive
     players.push(payable(msg.sender));

}
receive() external payable {
    // do nothing let your contract revieve ether
}

This might not fix your error because we don't know what your players array looks like but it's the right way to receive ether.

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.