0

I am implementing a factory pattern contract and on my constructor I want to deposit money into my child contract as soon as the child is rendered

--Factory--

function newChild() {
     Child child = new Child(money);
}

--Child Contract--

address payable public spender;

constructor(uint256 _money) {
   address(this).transfer(_money) /*{from: spender}*/;
}

My remix IDE is yelling at me that transfer is only valid on address of type payable. Obviously I am doing something quite wrong here so my question is what is the best way to send money immediately when I child is created?

Also I want to spend that money from a specific address lets call it the spender for now. How do I sent the money from that address

Thanks everyone!

1 Answer 1

0

A contract only can spend its own ether. It cannot access funds from another address.

The following code should do something similar.

// SPDX-License-Identifier: MIT
pragma solidity 0.7.4;

contract Factory {
    event NewChild(address child);

    // Make function payable to accept ethers on invocation
    function newChild(uint256 param) public payable {

        // Forward ethers received by function to Child
        Child cld = new Child{value: msg.value}(param);

        emit NewChild(address(cld));
    }
}

contract Child {
    uint256 public id;

    // Make constructor payable to receive ethers on creation
    constructor(uint256 param) public payable {
        id = param;
    }
}
0

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.