1

I am currently learning Solidity and I have found this example on internet for crowdfunding app in which it calls another contract constructor:

// SPDX-License-Identifier: Unlicensed

pragma solidity >0.7.0 <=0.9.0;
import "hardhat/console.sol";

contract CampaignFactory {
    address[] public deployedCampaigns;

    function createCampaign(
        string memory campaignTitle, 
        uint requiredCampaignAmount) public
    {

        Campaign newCampaign = new Campaign(
            "campaignTitle1", 10000);
        

        deployedCampaigns.push(address(newCampaign));
        console.log("address of campaign: ",address(newCampaign));

        Campaign newCampaign1 = new Campaign(
            "campaignTitle", 1000);
        console.log("address of campaign1: ",address(newCampaign1));

    }
}


contract Campaign {
    string public title;
    uint public requiredAmount;

    constructor(
        string memory campaignTitle, 
        uint requiredCampaignAmount
    ) {
        title = campaignTitle;
        requiredAmount = requiredCampaignAmount;
    }

    function donate() public payable  {
        console.log("payable value: ",msg.value);
         requiredAmount += msg.value;
    }
}

I want to know that when we call the createCampaign function it creates two instances of Campagin contract twice with different values passed. When I have check the address of both the instances , they are different. So does it mean that CampaignFactory deploys the Campagin contract twice ?

1 Answer 1

2

Yes, each of them have their own unique contract address.

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.