user1506104
user1506104

Reputation: 7106

Transfer contract's value from one to another

I have a contract CampaignFactory below that creates instances of the contract Campaign using the function createCampaign. This function is marked as payable.

pragma solidity >=0.4.16 <0.9.0;
contract CampaignFactory {
    Campaign[] public deployedCampaigns;
    
    function createCampaign(uint minimum) public payable {
        Campaign newCampaign = new Campaign(minimum, msg.sender);
        deployedCampaigns.push(newCampaign);
    }
}

Now, what I need to do is if a user calls the createCampaign function, I need to transfer all those ethers sent in by the user to the created campaign contract. How do I do that?

Upvotes: 0

Views: 162

Answers (1)

Sergei Sevriugin
Sergei Sevriugin

Reputation: 498

Hope this work:

function createCampaign(uint minimum) public payable {
            uint256 amount = msg.value;
    
            Campaign newCampaign = new Campaign(minimum, msg.sender);
            deployedCampaigns.push(newCampaign);
            newCampaign.transfer(amount);
        }

Upvotes: 2

Related Questions