0

I am working on a smart contract for a quiz application, Basically, I want everyone to be able to create a quiz and for everyone to be able to answer. I am struggling on saving a list of quizzes with the following structs

struct Option {
    string option;
}

struct Question {
    string question;
    string answer;
    Option[] options;
}

struct Quiz {
    address author;
    string title;
    string description;
    Question[] questions;
}

Survey[] private surveys;

function createQuiz(string memory _title, string memory _description, string[] _questions, string[][] _options) {}

Basically, I want to store a survey and a list of questions inside that survey. How can I go about doing that?

1 Answer 1

0

I kept this minimal to help point you in the right direction. I'd be a little leery about the complexity of the object to pass in from the client. The contract would be more lengthy if questions and surveys were assembled in smaller steps, but that might produce a preferable result.

When you pass in the Survey it contains Questions as defined in the struct. I removed the Options struct because it only had one element.

// SPDX-License-Identifier: UNLICENSED

pragma solidity 0.8.14;

contract Surveys {

    struct Question {
        string question;
        string answer;
        string[] options;
    }

    struct Survey {
        address author;
        string title;
        string description;
        Question[] questions;
    }

    Survey[] private surveys;

    event CreateSurvey(Survey survey);

    function createSurvey(Survey calldata survey) public {
        require(msg.sender == survey.author, "can't ghost write");
        surveys.push(survey);
        emit CreateSurvey(survey);
    }

}

Hope it helps.

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.