Created
January 29, 2021 15:50
-
-
Save PatrickAlphaC/151e7093d524e6f98a15f53c11483988 to your computer and use it in GitHub Desktop.
mapping_mapping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: MIT | |
| pragma solidity ^0.6.6; | |
| contract Questions { | |
| uint256 public questionIdCounter; | |
| mapping(uint256 => Question) public questions; | |
| struct Question { | |
| uint256 answerCounter; | |
| mapping(uint256 => Answer) answers; | |
| } | |
| struct Answer { | |
| address answerer; | |
| } | |
| constructor() public{ | |
| questionIdCounter = 0; | |
| } | |
| function askQuestion() public { | |
| questions[questionIdCounter] = Question({answerCounter: 0}); | |
| questionIdCounter = questionIdCounter + 1; | |
| } | |
| function answerQuestion(uint256 questionId) public { | |
| // Add new answer | |
| questions[questionId].answers[questions[questionId].answerCounter] = Answer({answerer: msg.sender}); | |
| // update answer counter | |
| questions[questionId].answerCounter = questions[questionId].answerCounter + 1; | |
| } | |
| function getAnswer(uint256 questionId, uint256 answerId) public returns (address){ | |
| return questions[questionId].answers[answerId].answerer; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment