Compilation error in 8.7 version

Code is not working as per video

pragma solidity 0.8.7;

contract contractPayable{

mapping(address => uint) balance;
    
    event depositDone(uint amount, address indexed depositedTo);
    
    function deposit() public payable returns (uint)  {
        balance[msg.sender] += msg.value;
        emit depositDone(msg.value, msg.sender);
        return balance[msg.sender];
    }
    
    function withdraw(uint amount) public returns (uint){
        require(balance[msg.sender] >= amount);
        balance[msg.sender] -= amount;
      address payable toSend = 0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB; // compilation error 
        payable(toSend).transfer(amount); 
    }
    
    function getBalance() public view returns (uint){
        return balance[msg.sender];
    }
    
    function transfer(address recipient, uint amount) public {
        require(balance[msg.sender] >= amount, "Balance not sufficient");
        require(msg.sender != recipient, "Don't transfer money to yourself");
        
        uint previousSenderBalance = balance[msg.sender];
        
        _transfer(msg.sender, recipient, amount);
                
        assert(balance[msg.sender] == previousSenderBalance - amount);
    }
    
    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }


}```
1 Like

if your using remix you need to change your compiler version in the settings. to get here click the second tab as indicated in the image below and the compilers list is the first drowpown. make sure that the compiler you select matches the version of solidity that your using in your contract

Hi @Gaurav_Sahoo,

Address literals are non-payable by default, and you cannot assign a non-payable address to a payable address variable, without explicitly converting it to a payable address. The following will compile …

address payable toSend = payable(0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB);

Once you’ve corrected that, you will notice that the compiler now generates a warning for the withdraw() function header. That’s because you have declared a return value, but you haven’t added a return statement. The function can still operate effectively without returning a value, so you could just remove returns(unit) from the function header. But if you include it, you should also include a return statement in the function body.

Just let us know if you have any further questions.