0

Not sure how to successfully convert a contract to a payable address..

ERC721 public nonFungibleContract; 

function withdrawBalance() external {
    address payable nftAddress = address(nonFungibleContract);
    nftAddress.send(address(this).balance);
}

TypeError: Type address is not implicitly convertible to expected type address payable.

1

There are 2 steps here:

  1. Convert contract type to address type:

address nftAddress = address(nonFungibleContract);

  1. Convert address type to payable address type:

address payable addr = address(uint160(nftAddress));

Your code:

ERC721 public nonFungibleContract; 

 function withdrawBalance() external {
     address nftAddress = address(nonFungibleContract);
     address payable addr = address(uint160(nftAddress));
     addr.send(address(this).balance);
}
-1

Your code should work fine, as long as the definition for ERC721 includes a payable fallback function, e.g.:

interface ERC721 {
    function() external payable;
    // ...
}

If ERC721 doesn't contain a payable fallback function, then you won't be able to send it ether this way anyway.

  • he is withdrawing the funds, not sending, hence why your solutions is incorrect. The warning says that in solidity 0.5 there is difference between an address and payable address and simple address has to be converted to payable address using the solution above. A function doesn't have to be payable if I'm taking out the funds from contract. – rstormsf Jan 31 at 8:09
  • @rstormsf Did you try actually running my code? It's correct. And yes, his code is sending funds to the ERC721 contract. (It's moving funds from one contract to another, which is why the term "wirhdraw" makes sense.) – smarx Jan 31 at 14:10
  • This is not terribly well documented, but you can find an explanation here: solidity.readthedocs.io/en/v0.5.3/types.html#contract-types. "Explicit conversion to and from the address payable type is only possible if the contract type has a payable fallback function. The conversion is still performed using address(x) and not using address payable(x)." – smarx Jan 31 at 17:22
  • I am terribly sorry. You are 100% correct. It will work just fine. I had recently came across return address(uint160(ecrecover(digest, uint8(v), r, s))); because ecrecover only returns address type and I had to use a workaround. I will update my vote in 12 hours – rstormsf Jan 31 at 20:27

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.