1

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.

1 Answer 1

0

You should not resort to iteration. In fact, you must not.

It will help to think of these as unordered sets. There is a crud pattern (create, retrieve, update, delete) that can be consistently applied across your various concerns. The relational concerns (related posts, etc) are themselves also sets. You can store sets inside of sets, so there is a reusable pattern.

Have a look at this library and if things are not clear, the previous installments in the series.

https://medium.com/robhitchens/solidity-crud-epilogue-e563e794fde

Hope it helps.

1
  • Thank you @Rob , worked like a charm. Took me some time to go through your articles and library and test them but it was definitely worth it. I found your CRUD pattern to be so far the best pattern to use when working with sets. Great job, keep up the good work ! Commented Apr 29, 2019 at 17:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.