3

I'm taking the course from lynda.com "Ethereum: Building Blockchain Decentralized Apps (DApps)" but there's an issue with the contract we're given, and it won't compile:

pragma solidity ^0.5.0;

contract ApprovalContract {
    address payable public sender;
    address payable public receiver;
    address public constant approver = 0x3999BA5D247c800E5Ef3d4de66618741E3675379 ;

function deposit(address _receiver) external payable {

    require (msg.value > 0);
    sender = msg.sender;
    receiver = _receiver;   
    }

function viewApprover () external pure returns(address) {
    return(approver);
    }

function approve() external {
        require(msg.sender == approver);
        receiver.transfer(address(this).balance);
        
    }
}

Here the picture from the result of trying to compile the solidity contract:

enter image description here

3

Should be

function deposit(address payable _receiver) external payable {

From Solidity documentation:

The address type comes in two flavours, which are largely identical:

address: Holds a 20 byte value (size of an Ethereum address).

address payable: Same as address, but with the additional members transfer and send.

The idea behind this distinction is that address payable is an address you can send Ether to, while a plain address cannot be sent Ether.

As long as address has fewer methods than address payable you cannot directly assign address value to address payable variable. Tough you may explicitly cast adderess to address payable (not needed in your case).

3
  • It's works, OMG, it took me 4h for this – johnho.91 Apr 4 '19 at 9:46
  • 4
    How would you cast address to address payable? – NYC Tech Engineer Feb 20 '20 at 23:16
  • 3
    cast address to address payable using payable(msg.sender) – Eslam Mahmoud Apr 16 at 21:52

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