Search code examples
solidityfactory-pattern

How to execute a factory pattern when the child contract does not have a constructor?


// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

contract ConcertTicketReservation {

    struct reservation {
        address Id;
        string name;
        string seat;
        Class choice;
    }

    uint256 reservationsCount = 0;
    mapping (uint => reservation) public reservations; //to store reservations
    
    enum Class{VIP, NON_VIP } //two types of seats

    Class choice = Class.NON_VIP;

    function setVIP() public { //set to vip
        choice = Class.VIP;
    }
    function setNON_VIP() public {
        choice = Class.NON_VIP;
    }

    function getChoice() public view returns (string memory) {  //for my ease, to see what value has been chosen
        if(choice == Class.NON_VIP) {
            return "NON_VIP";
        } else if(choice == Class.VIP) {
            return "VIP";
        }
        return "NON_VIP";
    }

    uint non_vip_price = 0.005 ether; //price
    uint vip_price = 0.01 ether;

    event Received(address, uint);

    function pay() internal { //pay function
        uint moneyToReturn;
        if(reservations[reservationsCount].choice == Class.NON_VIP) {
            require (msg.value >= non_vip_price);
            emit Received(msg.sender, msg.value);
            moneyToReturn = msg.value - non_vip_price; 
        } else if(reservations[reservationsCount].choice == Class.VIP) {
            require (msg.value >= vip_price);
            emit Received(msg.sender, msg.value);
            moneyToReturn = msg.value - vip_price; 
        }
        if(moneyToReturn > 0) //return amount left
                payable(msg.sender).transfer(moneyToReturn);
    }
     
    //user enters details and pays
    function makeReservation (string memory _name, string memory _seat) public  payable{
        reservationsCount += 1;
        reservations[reservationsCount] = reservation(msg.sender, _name, _seat, choice);
        pay();
    }

    /*------------------------------------------------------------*/
    address owner = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;

    modifier Owner {
        require(msg.sender == owner, "not eligible");
            _;
    }
}

I'm new to solidity and this is an assigned task. I have finished all tasks except one which is to implement a factory pattern on this, what I don't get is how do you implement a factory pattern when a child contract has no constructor we were explicitly told not to use a constructor to update values of a struct we had to use a function. Also we have to use the custom modifier created in the child contract in the parent contract is there a way to do this? --one solution that came to me was to declare the modifier in the factory contract but then can I use it in the child contract?


Solution

  • You can deploy a new contract using the factory pattern even without constructor.

    contract ConcertTicketReservationFactory {
        function create() public {
            // deploys the `ConcertTicketReservation` contract to a new address
            ConcertTicketReservation reservationContract = new ConcertTicketReservation();
    
            // get the newly deployed contract address
            address reservationContractAddress = address(reservationContract);
    
            // or execute its function
            string memory choice = reservationContract.getChoice();
    
        }
    }
    

    The factory contract can't read modifiers of the instance or use them directly. You'll need to copy the modifier source code to the factory contract, if you want to use it there as well.