0

I have a struct called Meeting that contains an array of addresses. I want to check that the address doesn't exist before it RSVPs to a meeting, so I created a mapping called hasRSVPed. (I could loop through the array of addresses, but that is inefficient).

I am trying to push the created Meeting into an array of Meetings, but how do I do that if the Meeting contains a mapping?

I get the following error when I tried this

TypeError: Uninitialized mapping. Mappings cannot be created dynamically, you have to assign them from a state variable.
 struct Meeting {
        address meeting_creator;
        uint256 meetingID;
        string name;
        string description;
        uint256 start_date;
        uint256 end_date;
        uint256 max_tickets;
        uint256 ticket_price;
        uint256 current_tickets_sold;
        address[] attendees;
        mapping(address => bool) hasRSVPed;

    }

Meeting[] public meetingArray;

function create_meeting(string memory _name, string memory _description, uint _startTime, uint _endTime, uint _maxTickets,
    uint256 _ticketPrice) public returns (uint256 meetingID) {

        address[] memory adr;
        mapping(address => bool) memory RSVPed;

        totalMeetings += 1;
        meetingID = totalMeetings;

        meetingArray.push(Meeting(msg.sender, meetingID, _name, _description, _startTime, _endTime, _maxTickets, _ticketPrice, 0, adr, RSVPed));

        Meeting storage m = meetings[totalMeetings];
        m.meeting_creator = msg.sender;
        m.meetingID = meetingID;
        m.name = _name;
        m.description = _description;
        m.start_date = _startTime;
        m.end_date = _endTime;
        m.max_tickets = _maxTickets;
        m.ticket_price = _ticketPrice;
        m.current_tickets_sold = 0;
        m.attendees = adr;
        m.hasRSVPed = RSVPed;
}
2
  • Please, can you explain better your smart contract purpose? May 26, 2022 at 21:20
  • Sure, I'm trying to create a decentralized EventBrite where one can create Meetings and attend meetings by staking coins
    – Emily Kuo
    May 27, 2022 at 2:58

1 Answer 1

0

You are trying to push a dynamic mapping to an array. As far as I can tell, this may not be possible because array is itself dynamic in nature in Solidity. So, pushing a mapping whose size cannot be determined at first, solidity cannot calculate the storage slot in EVM properly. Or, it could incur a gas cost thats too high.

So I would suggest using a mapping(uint => bool) hasRSVPed instead which tracks if a meeting with a unique id has been RSVPed or not.

I have a small snippet of the code used by you with implementation of the code I pointed out.

pragma solidity ^0.8.0;
contract meeting {
    uint totalMeetings;
    struct Meeting {
        address meeting_creator;
    }
    mapping(uint => bool) hasRSVPed;
    mapping(uint => Meeting) meetings;

Meeting[] private meetingArray;

function create_meeting(string memory _name, string memory _description, uint _startTime, uint _endTime, uint _maxTickets,
    uint256 _ticketPrice) public returns (uint256 meetingID) {

        address[] memory adr;
        totalMeetings += 1;
        Meeting storage m = meetings[totalMeetings];
        m.meeting_creator = msg.sender;
        hasRSVPed[totalMeetings] = true;
}
2
  • I see, but I want multiple addresses to 'RSVP' to a meeting, hence why I put an array of addresses inside the Meeting struct. How do I use a mapping to check that the address is not in the array of a specific Meeting?
    – Emily Kuo
    May 27, 2022 at 15:14
  • try this then: mapping(address => mapping(uint => bool)); So, addresses are mapped to meetings. So that address and its meeting id is mapped to bool
    – Mr.SsS
    May 27, 2022 at 16:17

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.