Skip to content

Instantly share code, notes, and snippets.

@PatrickAlphaC
Created January 29, 2021 15:50
Show Gist options
  • Select an option

  • Save PatrickAlphaC/151e7093d524e6f98a15f53c11483988 to your computer and use it in GitHub Desktop.

Select an option

Save PatrickAlphaC/151e7093d524e6f98a15f53c11483988 to your computer and use it in GitHub Desktop.
mapping_mapping
// 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