|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.16; |
| 3 | + |
| 4 | +import '@openzeppelin/contracts/access/Ownable.sol'; |
| 5 | + |
| 6 | +import './libraries/AddressSet.sol'; |
| 7 | + |
| 8 | +/// @custom:security-contact security@golden.com |
| 9 | +contract GoldenProtocol is Ownable { |
| 10 | + using AddressSet for AddressSet.Set; |
| 11 | + // This is not going to scale well... how to keep track of questions without this? |
| 12 | + AddressSet.Set _questions; |
| 13 | + string[] entities; |
| 14 | + uint256 public minimumVotes; |
| 15 | + |
| 16 | + constructor(uint256 _minimumVotes) Ownable() { |
| 17 | + minimumVotes = _minimumVotes; |
| 18 | + } |
| 19 | + |
| 20 | + function setMinimumVotes(uint256 _minimumVotes) public onlyOwner { |
| 21 | + minimumVotes = _minimumVotes; |
| 22 | + } |
| 23 | + |
| 24 | + function createQuestion( |
| 25 | + bytes16 subjectUUID, |
| 26 | + bytes16 predicateUUID, |
| 27 | + uint256 bounty |
| 28 | + ) public returns (address) { |
| 29 | + // TODO: Creating new contracts is going to be very expensive. |
| 30 | + // Is there a cheaper alternative to encapsulate the question logic? |
| 31 | + GoldenProtocolQuestion newQuestion = new GoldenProtocolQuestion( |
| 32 | + address(this), |
| 33 | + msg.sender, |
| 34 | + subjectUUID, |
| 35 | + predicateUUID, |
| 36 | + bounty |
| 37 | + ); |
| 38 | + address newQuestionAddress = address(newQuestion); |
| 39 | + _questions.insert(newQuestionAddress); |
| 40 | + return newQuestionAddress; |
| 41 | + } |
| 42 | + |
| 43 | + // TODO: This is not going to scale well... how to keep track of questions without this? |
| 44 | + function questions() public view returns (address[] memory) { |
| 45 | + return _questions.keyList; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +contract GoldenProtocolQuestion { |
| 50 | + using AddressSet for AddressSet.Set; |
| 51 | + |
| 52 | + address public owner; |
| 53 | + address public asker; |
| 54 | + uint256 public bounty; |
| 55 | + bytes16 public subjectUUID; |
| 56 | + bytes16 public predicateUUID; |
| 57 | + string public answer; |
| 58 | + |
| 59 | + AddressSet.Set answerers; |
| 60 | + AddressSet.Set verifiers; |
| 61 | + |
| 62 | + // Mapping of answerer address to their answer |
| 63 | + mapping(address => string) answerByAnswerer; |
| 64 | + |
| 65 | + // Mapping of voter address to the index of an answer |
| 66 | + mapping(address => uint256) answerIndexByVerifier; |
| 67 | + |
| 68 | + // Mapping of answerer address to their vote count (score) |
| 69 | + mapping(address => uint256) voteCountByAnswerer; |
| 70 | + |
| 71 | + // Helper struct for consensus/payout algorithms |
| 72 | + struct Answer { |
| 73 | + address answerer; |
| 74 | + string answer; |
| 75 | + uint256 voteCount; |
| 76 | + } |
| 77 | + |
| 78 | + constructor( |
| 79 | + address _owner, |
| 80 | + address _asker, |
| 81 | + bytes16 _subjectUUID, |
| 82 | + bytes16 _predicateUUID, |
| 83 | + uint256 _bounty |
| 84 | + ) { |
| 85 | + owner = _owner; |
| 86 | + asker = _asker; |
| 87 | + subjectUUID = _subjectUUID; |
| 88 | + predicateUUID = _predicateUUID; |
| 89 | + bounty = _bounty; |
| 90 | + } |
| 91 | + |
| 92 | + modifier onlyAsker() { |
| 93 | + require(msg.sender == asker, 'GoldenProtocolQuestion: onlyAsker'); |
| 94 | + _; |
| 95 | + } |
| 96 | + |
| 97 | + function addAnswer(string calldata _answer) public { |
| 98 | + address answerer = msg.sender; |
| 99 | + answerByAnswerer[answerer] = _answer; |
| 100 | + answerers.upsert(answerer); |
| 101 | + voteCountByAnswerer[answerer] = 0; |
| 102 | + } |
| 103 | + |
| 104 | + function answers() public view returns (string[] memory) { |
| 105 | + string[] memory _answers = new string[](answerers.count()); |
| 106 | + for (uint256 i = 0; i < answerers.count(); i++) { |
| 107 | + _answers[i] = answerByAnswerer[answerers.keyAtIndex(i)]; |
| 108 | + } |
| 109 | + return _answers; |
| 110 | + } |
| 111 | + |
| 112 | + function upvote(uint256 index) public { |
| 113 | + require( |
| 114 | + answerers.count() > index, |
| 115 | + 'GoldenProtocolQuestion: there is no answer at that index' |
| 116 | + ); |
| 117 | + address answerer = answerers.keyAtIndex(index); |
| 118 | + address verifier = msg.sender; |
| 119 | + voteCountByAnswerer[answerer] += 1; |
| 120 | + answerIndexByVerifier[verifier] = index; |
| 121 | + verifiers.upsert(verifier); |
| 122 | + } |
| 123 | + |
| 124 | + function votes() public view returns (uint256[] memory) { |
| 125 | + uint256[] memory _votes = new uint256[](answerers.count()); |
| 126 | + for (uint256 i = 0; i < answerers.count(); i++) { |
| 127 | + _votes[i] = voteCountByAnswerer[answerers.keyAtIndex(i)]; |
| 128 | + } |
| 129 | + return _votes; |
| 130 | + } |
| 131 | + |
| 132 | + function topAnswer() public view returns (Answer memory) { |
| 133 | + uint256 maxVotes = 0; |
| 134 | + uint256 maxVotesIndex = 0; |
| 135 | + for (uint256 i = 0; i < answerers.count(); i++) { |
| 136 | + uint256 voteCount = voteCountByAnswerer[answerers.keyAtIndex(i)]; |
| 137 | + if (voteCount >= maxVotes) { |
| 138 | + maxVotes = voteCount; |
| 139 | + maxVotesIndex = i; |
| 140 | + } |
| 141 | + } |
| 142 | + address answerer = answerers.keyAtIndex(maxVotesIndex); |
| 143 | + return (Answer(answerer, answerByAnswerer[answerer], maxVotes)); |
| 144 | + } |
| 145 | + |
| 146 | + // TODO: Pay out the bounty to the best answer, skim a small fee for the voters and protocol |
| 147 | + function payout() public onlyAsker { |
| 148 | + Answer memory _topAnswer = topAnswer(); |
| 149 | + GoldenProtocol protocol = GoldenProtocol(owner); |
| 150 | + require( |
| 151 | + protocol.minimumVotes() <= _topAnswer.voteCount, |
| 152 | + 'GoldenProtocolQuestion: payout: minimumVotes not met' |
| 153 | + ); |
| 154 | + address payable answerer = payable(_topAnswer.answerer); |
| 155 | + answerer.transfer(bounty); |
| 156 | + } |
| 157 | + |
| 158 | + // Utils |
| 159 | + function hashAnswer(address answerer, string memory value) |
| 160 | + public |
| 161 | + pure |
| 162 | + returns (uint256) |
| 163 | + { |
| 164 | + return uint256(keccak256(abi.encode(answerer, value))); |
| 165 | + } |
| 166 | +} |
0 commit comments