Skip to content

Commit 8e8577b

Browse files
committed
feat: answered too soon at index 1
1 parent a7a943a commit 8e8577b

File tree

2 files changed

+116
-57
lines changed

2 files changed

+116
-57
lines changed

contracts/deploy/00-reality-v2.ts

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,36 @@ import { HardhatRuntimeEnvironment } from "hardhat/types";
22
import { DeployFunction } from "hardhat-deploy/types";
33
import { HomeChains, isSkipped } from "./utils";
44

5-
const disputeTemplate = `{
6-
"$schema": "../NewDisputeTemplate.schema.json",
7-
"title": "Let's do this",
8-
"description": "We want to do this: %s",
9-
"question": "Does it comply with the policy?",
10-
"answers": [
11-
{
12-
"title": "Yes",
13-
"description": "Select this if you agree that it must be done."
14-
},
15-
{
16-
"title": "No",
17-
"description": "Select this if you do not agree that it must be done."
18-
}
19-
],
20-
"policyURI": "/ipfs/Qmdvk...rSD6cE/policy.pdf",
21-
"frontendUrl": "https://kleros-v2.netlify.app/#/cases/%s/overview",
22-
"arbitratorChainID": "421614",
23-
"arbitratorAddress": "0xD08Ab99480d02bf9C092828043f611BcDFEA917b",
24-
"category": "Others",
25-
"specification": "KIP001",
26-
"lang": "en_US"
27-
}
28-
`;
5+
const disputeTemplateFn = (chainId: number, arbitratorAddress: string) => `{
6+
"title": "A reality.eth question",
7+
"description": "A reality.eth question has been raised to arbitration.",
8+
"question": "{{ question }}",
9+
"type": "{{ type }}",
10+
"answers": [
11+
{
12+
"title": "Answered Too Soon",
13+
"description": "Answered Too Soon.",
14+
},
15+
{{# answers }}
16+
{
17+
"title": "{{ title }}",
18+
"description": "{{ description }}",
19+
}{{^ last }},{{/ last }}
20+
{{/ answers }}
21+
],
22+
"policyURI": "/ipfs/QmZ5XaV2RVgBADq5qMpbuEwgCuPZdRgCeu8rhGtJWLV6yz",
23+
"frontendUrl": "https://reality.eth.limo/app/#!/question/{{ realityAddress }}-{{ questionId }}",
24+
"arbitratorChainID": "${chainId}",
25+
"arbitratorAddress": "${arbitratorAddress}",
26+
"category": "Oracle",
27+
"lang": "en_US",
28+
"specification": "KIP99",
29+
"version": "1.0"
30+
}`;
2931

3032
// General court, 3 jurors
3133
const extraData =
32-
"0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003";
34+
"0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003";
3335

3436
const deploy: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
3537
const { deployments, getNamedAccounts, getChainId } = hre;
@@ -42,14 +44,16 @@ const deploy: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
4244

4345
const klerosCore = await deployments.get("KlerosCore");
4446
const disputeTemplateRegistry = await deployments.get("DisputeTemplateRegistry");
45-
47+
const disputeTemplate = disputeTemplateFn(chainId, klerosCore.address);
48+
const disputeTemplateMappings = "TODO";
49+
4650
await deploy("RealityV2", {
4751
from: deployer,
4852
args: [
4953
klerosCore.address,
5054
extraData,
5155
disputeTemplate,
52-
"disputeTemplateMapping: TODO",
56+
disputeTemplateMappings,
5357
disputeTemplateRegistry.address,
5458
600, // feeTimeout: 10 minutes
5559
],

contracts/src/RealityV2.sol

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22

33
/**
4-
* @authors: [@unknownunknown1]
4+
* @authors: [@unknownunknown1, @jaybuidl]
55
* @reviewers: []
66
* @auditors: []
77
* @bounties: []
@@ -12,23 +12,16 @@ pragma solidity 0.8.18;
1212

1313
import {IArbitrableV2, IArbitratorV2} from "@kleros/kleros-v2-contracts/arbitration/interfaces/IArbitrableV2.sol";
1414
import {EvidenceModule} from "@kleros/kleros-v2-contracts/arbitration/evidence/EvidenceModule.sol";
15-
import "@kleros/kleros-v2-contracts/arbitration/interfaces/IDisputeTemplateRegistry.sol";
16-
import "./interfaces/IRealitio.sol";
17-
import "./interfaces/IRealitioArbitrator.sol";
15+
import {IDisputeTemplateRegistry} from "@kleros/kleros-v2-contracts/arbitration/interfaces/IDisputeTemplateRegistry.sol";
16+
import {IRealitio} from "./interfaces/IRealitio.sol";
17+
import {IRealitioArbitrator} from "./interfaces/IRealitioArbitrator.sol";
1818

1919
/// @title RealitioProxyV2
2020
/// @dev Realitio proxy contract compatible with V2.
2121
contract RealitioProxyV2 is IRealitioArbitrator, IArbitrableV2 {
22-
uint256 public constant REFUSE_TO_ARBITRATE_REALITIO = type(uint256).max; // Constant that represents "Refuse to rule" in realitio format.
23-
24-
IRealitio public immutable override realitio; // Actual implementation of Realitio.
25-
string public override metadata; // Metadata for Realitio. See IRealitioArbitrator.
26-
27-
address public governor; // The address that can make changes to the parameters of the contract.
28-
IDisputeTemplateRegistry public templateRegistry; // The dispute template registry.
29-
uint256 public templateId; // Dispute template identifier.
30-
31-
uint256 private constant NUMBER_OF_RULING_OPTIONS = type(uint256).max; // The amount of non 0 choices the arbitrator can give.
22+
// ************************************* //
23+
// * Enums / Structs * //
24+
// ************************************* //
3225

3326
enum Status {
3427
None, // The question hasn't been requested arbitration yet.
@@ -54,15 +47,34 @@ contract RealitioProxyV2 is IRealitioArbitrator, IArbitrableV2 {
5447
EvidenceModule evidenceModule; // The evidence module for the arbitrator.
5548
}
5649

50+
// ************************************* //
51+
// * Storage * //
52+
// ************************************* //
53+
54+
uint256 private constant NUMBER_OF_RULING_OPTIONS = type(uint256).max; // Maximum, the number of choices in a Realitio question is unknown
55+
56+
IRealitio public immutable override realitio; // Actual implementation of Realitio.
57+
string public override metadata; // Metadata for Realitio. See IRealitioArbitrator.
58+
address public governor; // The address that can make changes to the parameters of the contract.
59+
IDisputeTemplateRegistry public templateRegistry; // The dispute template registry.
60+
uint256 public templateId; // Dispute template identifier.
5761
mapping(uint256 => ArbitrationRequest) public arbitrationRequests; // Maps a question identifier in uint256 to its arbitration details. Example: arbitrationRequests[uint256(questionID)]
5862
mapping(address => mapping(uint256 => uint256)) public arbitratorDisputeIDToQuestionID; // Maps a dispute ID to the ID of the question (converted into uint) with the disputed request in the form arbitratorDisputeIDToQuestionID[arbitrator][disputeID].
5963
ArbitrationParams[] public arbitrationParamsChanges;
6064

61-
modifier onlyGovernor() {
65+
// ************************************* //
66+
// * Function Modifiers * //
67+
// ************************************* //
68+
69+
modifier onlyGovernor() {
6270
if (governor != msg.sender) revert GovernorOnly();
6371
_;
6472
}
6573

74+
// ************************************* //
75+
// * Constructor * //
76+
// ************************************* //
77+
6678
/// @dev Constructor
6779
/// @param _governor The trusted governor of this contract.
6880
/// @param _realitio The address of the Realitio contract.
@@ -98,6 +110,10 @@ contract RealitioProxyV2 is IRealitioArbitrator, IArbitrableV2 {
98110
);
99111
}
100112

113+
// ************************************* //
114+
// * Governance * //
115+
// ************************************* //
116+
101117
/// @dev Changes the governor of the Reality proxy.
102118
/// @param _governor The address of the new governor.
103119
function changeGovernor(address _governor) external onlyGovernor {
@@ -129,32 +145,45 @@ contract RealitioProxyV2 is IRealitioArbitrator, IArbitrableV2 {
129145
function changeTemplateRegistry(
130146
IDisputeTemplateRegistry _templateRegistry,
131147
string memory _templateData,
132-
string memory _templateDataMappings)
133-
external onlyGovernor {
148+
string memory _templateDataMappings
149+
) external onlyGovernor {
134150
templateRegistry = _templateRegistry;
135151
templateId = templateRegistry.setDisputeTemplate("", _templateData, _templateDataMappings);
136152
}
137153

138154
/// @dev Changes the dispute template.
139155
/// @param _templateData The new template data for requests.
140156
/// @param _templateDataMappings The new data mappings json.
141-
function changeDisputeTemplate(string memory _templateData, string memory _templateDataMappings) external onlyGovernor {
157+
function changeDisputeTemplate(
158+
string memory _templateData,
159+
string memory _templateDataMappings
160+
) external onlyGovernor {
142161
templateId = templateRegistry.setDisputeTemplate("", _templateData, _templateDataMappings);
143162
}
144163

164+
// ************************************* //
165+
// * State Modifiers * //
166+
// ************************************* //
167+
145168
/// @dev Request arbitration from Kleros for given _questionID.
146169
/// @param _questionID The question identifier in Realitio contract.
147170
/// @param _maxPrevious If specified, reverts if a bond higher than this was submitted after you sent your transaction.
148171
/// @return disputeID ID of the resulting dispute in arbitrator.
149-
function requestArbitration(bytes32 _questionID, uint256 _maxPrevious) external payable returns (uint256 disputeID) {
172+
function requestArbitration(
173+
bytes32 _questionID,
174+
uint256 _maxPrevious
175+
) external payable returns (uint256 disputeID) {
150176
ArbitrationRequest storage arbitrationRequest = arbitrationRequests[uint256(_questionID)];
151-
if(arbitrationRequest.status != Status.None) revert ArbitrationAlreadyRequested();
177+
if (arbitrationRequest.status != Status.None) revert ArbitrationAlreadyRequested();
152178
uint256 arbitrationParamsIndex = arbitrationParamsChanges.length - 1;
153179
IArbitratorV2 arbitrator = arbitrationParamsChanges[arbitrationParamsIndex].arbitrator;
154180
bytes storage arbitratorExtraData = arbitrationParamsChanges[arbitrationParamsIndex].arbitratorExtraData;
155181

156182
// Notify Kleros
157-
disputeID = arbitrator.createDispute{value: msg.value}(NUMBER_OF_RULING_OPTIONS, arbitratorExtraData); /* If msg.value is greater than intended number of votes (specified in arbitratorExtraData),
183+
disputeID = arbitrator.createDispute{value: msg.value}(
184+
NUMBER_OF_RULING_OPTIONS,
185+
arbitratorExtraData
186+
); /* If msg.value is greater than intended number of votes (specified in arbitratorExtraData),
158187
Kleros will automatically spend excess for additional votes. */
159188
arbitratorDisputeIDToQuestionID[address(arbitrator)][disputeID] = uint256(_questionID);
160189

@@ -177,9 +206,9 @@ contract RealitioProxyV2 is IRealitioArbitrator, IArbitrableV2 {
177206
uint256 questionID = arbitratorDisputeIDToQuestionID[msg.sender][_disputeID];
178207
ArbitrationRequest storage arbitrationRequest = arbitrationRequests[questionID];
179208
IArbitratorV2 arbitrator = arbitrationParamsChanges[arbitrationRequest.arbitrationParamsIndex].arbitrator;
180-
181-
if(IArbitratorV2(msg.sender) != arbitrator) revert ArbitratorOnly();
182-
if(arbitrationRequest.status != Status.Disputed) revert StatusNotDisputed();
209+
210+
if (IArbitratorV2(msg.sender) != arbitrator) revert ArbitratorOnly();
211+
if (arbitrationRequest.status != Status.Disputed) revert StatusNotDisputed();
183212

184213
arbitrationRequest.ruling = _ruling;
185214
arbitrationRequest.status = Status.Ruled;
@@ -201,15 +230,24 @@ contract RealitioProxyV2 is IRealitioArbitrator, IArbitrableV2 {
201230
address _lastAnswerer
202231
) external {
203232
ArbitrationRequest storage arbitrationRequest = arbitrationRequests[uint256(_questionID)];
204-
if(arbitrationRequest.status != Status.Ruled) revert StatusNotRuled();
233+
if (arbitrationRequest.status != Status.Ruled) revert StatusNotRuled();
205234

206235
arbitrationRequest.status = Status.Reported;
207-
// Realitio ruling is shifted by 1 compared to Kleros.
208-
uint256 realitioRuling = arbitrationRequest.ruling != 0 ? arbitrationRequest.ruling - 1 : REFUSE_TO_ARBITRATE_REALITIO;
209-
210-
realitio.assignWinnerAndSubmitAnswerByArbitrator(_questionID, bytes32(realitioRuling), arbitrationRequest.requester, _lastHistoryHash, _lastAnswerOrCommitmentID, _lastAnswerer);
236+
uint256 realitioRuling = _klerosToRealitioRuling(arbitrationRequest.ruling);
237+
realitio.assignWinnerAndSubmitAnswerByArbitrator(
238+
_questionID,
239+
bytes32(realitioRuling),
240+
arbitrationRequest.requester,
241+
_lastHistoryHash,
242+
_lastAnswerOrCommitmentID,
243+
_lastAnswerer
244+
);
211245
}
212246

247+
// ************************************* //
248+
// * Public Views * //
249+
// ************************************* //
250+
213251
/// @dev Returns arbitration fee by calling arbitrationCost function in the arbitrator contract.
214252
/// @return fee Arbitration fee that needs to be paid.
215253
function getDisputeFee(bytes32) external view override returns (uint256 fee) {
@@ -219,9 +257,26 @@ contract RealitioProxyV2 is IRealitioArbitrator, IArbitrableV2 {
219257
return arbitrator.arbitrationCost(arbitratorExtraData);
220258
}
221259

260+
// ************************************* //
261+
// * Internal * //
262+
// ************************************* //
263+
264+
/// @dev Converts Kleros ruling to Realitio ruling.
265+
/// @param _klerosRuling The ruling from Kleros.
266+
/// @return The ruling in Realitio format.
267+
function _klerosToRealitioRuling(uint256 _klerosRuling) internal pure returns (uint256) {
268+
if (_klerosRuling == 0) return type(uint256).max; // Refuse to arbitrate
269+
if (_klerosRuling == 1) return type(uint256).max - 1; // Answered Too Soon
270+
return _klerosRuling - 1; // Normal answers are shifted by 1
271+
}
272+
273+
// ************************************* //
274+
// * Errors * //
275+
// ************************************* //
276+
222277
error StatusNotDisputed();
223278
error StatusNotRuled();
224279
error ArbitrationAlreadyRequested();
225280
error ArbitratorOnly();
226281
error GovernorOnly();
227-
}
282+
}

0 commit comments

Comments
 (0)