Skip to main content
edited tags
Link
Rob Hitchens
  • 55.6k
  • 11
  • 92
  • 147
Source Link

Return each struct value from a key inside a mapping, where the key is also a value from array inside another mapping

I have a project that I'm working on and I'm trying to figure out a way to do as the title says. I'm looking for the easiest and/or cheapest way(gas wise) and since I'm stuck I'm open to better ways to structure my code. The main idea is to create a dApp using truffle and metamask where once a submission is accepted the bountyPoster can send the initial amount to the accepted bountyHunter. These are the two requirements are being tough for me at the moment : "I am able to view a list of bounties that I have already posted. By clicking on a bounty, I can review submissions that have been proposed." My initial idea was to connect every new Bounty struct to it's bountyId , connect every new Submission to the bountyId it's submitting to and connect every bountyId with many submissions overall by their corresponding submissionId's. Then figure out a way to return the submissions inside the getBountySubmissions func. That's where I'm right now.

pragma solidity >=0.4.25 <0.6.0; contract BountyHunters { enum SubmissionStates {Accepted, Pending, Rejected} enum BountyStates { Open, Closed } address owner; uint public bountyId; uint public submissionId; mapping(address => uint[]) public myBounties; mapping(uint => Bounty) public newBounties; mapping(uint => uint[]) public mySubmissions; mapping(uint => Submissions) public subs; struct Bounty { uint bountyId; uint bountyAmount; address poster; string description; BountyStates bountyState; } struct Submissions { address bountyHunter; string description; SubmissionStates subState; } constructor() public payable { msg.sender == owner; } modifier onlyOP { require(newBounties[bountyId].poster == msg.sender, "You must be the owner of the post"); _; } modifier onlyNotOP { require(newBounties[bountyId].poster != msg.sender, "You are not allowed to submit to your own bounties."); _; } modifier isOpen { require(newBounties[bountyId].bountyState == BountyStates.Open); _; } function postNewBounty( string memory _description, uint _bountyAmount) public payable returns (bool) { require(_bountyAmount > 0); require(msg.sender.balance >= _bountyAmount); bountyId++; myBounties[msg.sender].push(bountyId) - 1; Bounty memory newBounty = Bounty(bountyId, _bountyAmount, msg.sender, _description, BountyStates.Open); newBounties[bountyId] = newBounty; return true; } function proposeSubmission(uint _bountyId, string memory _description) public onlyNotOP isOpen returns (bool success) { require(bountyId >= 0 && _bountyId <= bountyId); submissionId++; Submissions memory newSubb = Submissions(msg.sender, _description, SubmissionStates.Pending); mySubmissions[_bountyId].push(submissionId) - 1; subs[submissionId] = newSubb; return success; } function getBountySubmissions(uint _bountyId) public view onlyOP returns ( address bountyHunter, string memory description, SubmissionStates subState) { } 

}

Should I try looping through each submissionId and from there loop through each struct, or there is a better logical way to go with this? Do I have to loop through the structs here in the contract or there is an easier way when I reach deployment phase? Any help is much appreciated. Thank you for your time.