Solidity Error Address / Address Payable

by ph4nt0m   Last Updated June 12, 2019 11:28 AM

I'm currently learning solidity programming to create a basic smart contract based on a 2018 tutorial where solidity was using version 4. I'm currently using truffle v5.0.21. I understand that there was an update from "address" to "address payable", but i'm still not able to get it to work after referencing to similar threads. Appreciate any help. Thanks!

Code for ApprovalContract.sol:

pragma solidity >=0.4.18;

contract ApprovalContract {

    address public sender;
    address public receiver;
    address public constant approver = 0xAFXXX;


    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);
    }

}

Error that comes out on CMD:

//start of error code

TypeError: "send" and "transfer" are only available for objects of type "address payable", not "address". receiver.transfer(address(this).balance); ^---------------^

Compilation failed. See above.

//end of error code



Answers 1


That's because receiver and _receiver need to be payable. I've set an address of the Javascript VM of Remix as the approver.

pragma solidity ^0.5.0;

contract ApprovalContract {

    address public sender;
    address payable public receiver;
    address public constant approver = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;

    function deposit(address payable _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);
    }
}
alberto
alberto
June 12, 2019 11:06 AM

Related Questions


Updated November 01, 2017 07:28 AM

Updated July 18, 2018 11:28 AM

Updated September 12, 2018 13:28 PM

Updated December 06, 2017 02:28 AM