2

I have a factory contract that I need to be able to deploy with some ETH. I'm getting an error TypeError: Member "transfer" not found or not visible after argument-dependent lookup in address.

I tried instantiating the contract address as payable but not having any luck.

Where am I going wrong?

Factory contract

function createContract() public payable {
   NewContract myNewContract = new NewContract();
   address newContractAddress = address(newContract);
   newContractAddress.transfer(msg.value);
}

The following also doesn't work:

function createContract() public payable {
   NewContract myNewContract = new NewContract();
   address payable newContractAddress = address(newContract);
   newContractAddress.transfer(msg.value);
}

Constructed contract

constructor() public payable {
   ...
}
  • "The following also doesn't work" - with the exact same error?? – goodvibration Dec 16 '19 at 3:09
  • different error: Type address is not implicitly convertible to expected type address payable @goodvibration – Michael C Dec 16 '19 at 3:54
  • Well, then simply change address(newContract) to address payable(newContract). – goodvibration Dec 16 '19 at 4:05
  • 1
    @goodvibration That's not how you cast to address payable. The way to get an address payable is to cast it to an integer, then cast it to an address: address(uint160(newContract)) – Jesbus Dec 16 '19 at 8:09
1

You made your constructor payable, but that only means you can attach funds to the contract creation expression:

NewContract myNewContract = (new NewContract).value(msg.value)();

To make it possible to transfer ETH to a contract add a payable fallback function:

function () external payable
{
}

You can only use .transfer(..) on a value of type address payable. To convert an address to an address payable, cast to uint160 then back to address:

address payable b = address(uint160(a));

Attaching the ETH to the contract creation transaction is more efficient than creating with 0 ETH and then transferring the ETH.

I hope this helps!

| improve this answer | |
1

The error means you are trying to use a function transfer() which does not apply to the type address.

It can only be used on an address payable.

| improve this answer | |

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.