2

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

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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