false Transaction mined but execution failed in Remix IDE

317 views Asked by At

I am getting false Transaction mined but execution failed error from the constructor function in the contract. I am just saving the address from which the contract is deployed in manager variable and not sending any money to the account.

   // SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.20;

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

    constructor() public {
        manager = msg.sender;
    }

    function enter()public payable{
        require(msg.value > 0.1 ether);
        players.push(msg.sender);
    }

//Random number generator function.
    function random()private view returns (uint){
        return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players)));
    }

    function pickWinner()public{
        uint index = random() % players.length;
        address contractAddress = address(this);
        payable(players[index]).transfer(contractAddress.balance);
    }

}

After solidity v0.7, it says that visibility of the constructor is not required, so i tried by removing the publicvisibility. I also tried with payable visibility to the constructor() but still getting the same error.

3

There are 3 answers

4
Anh Dang Vien On

try this one below

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.20;

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

    constructor() {
        manager = msg.sender;
    }

    function enter() public payable {
        require(msg.value > 0.1 ether);
        players.push(msg.sender);
    }

    // Random number generator function.
    function random() private view returns (uint) {
        return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players)));
    }

    function pickWinner() public {
        uint index = random() % players.length;
        address payable winner = payable(players[index]);
        uint contractBalance = address(this).balance;
        winner.transfer(contractBalance);
        players = new address[](0); // Reset the player array for a new round
    }
}
0
Rakshith On

Make sure that the evm(Ethereum Virtual Machine) version and VM(Virtual Machine)/Environment is the same.

1
user23385337 On

Common checkpoint in such cases is to make sure that VM(Virtual Machine)/Environment and EVM(Ethereum Virtual Machine) version should be same.