0

This is my first contract (working from Lynda course). Even when I use the supplied code I still age this error for the following code with I try to compile:

TypeError: Type address is not implicitly convertible to expected type address payable. receiver = _receiver; ^-------^

Compilation failed. See above.

pragma solidity ^0.5.16;

contract ApprovalContract {

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

  function ApprovalContracts() public {
    // tbd
  }

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

}

2 Answers 2

0

The Error:

TypeError: Type address is not implicitly convertible to expected type address payable. receiver = _receiver; ^-------^

The compiler reports such errors whenever you assign a variable of type address to a variable of type address payable

In Solidity:

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

  • address: Holds a 20-byte value (size of an Ethereum address).
  • address payable: Same as an 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

Address Data Type in Soldity

In your code:

address payable public receiver;
receiver = _receiver;

inside deposit function, here _receiver is of type address and not address payable

In order to fix this error, change deposit functions signature to:

function deposit(address payable _receiver) external payable {
    // Logic remains the same
}
0

Add payable to this line receiver.transfer(address(this).balance);

make sure you are add brackets to the receiver

Dont repeat a function name same as contract name, use constructor instead

Here the code should be according to solidity version 0.8.9

 pragma solidity ^0.8.9;

 contract ApprovalContract {

 address public sender;
 address payable public receiver;
 address constant public approver = ; //address

 constructor() {
 // tbd
 }

 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);
 payable(receiver).transfer(address(this).balance);
 }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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