@reality.eth/contracts 3.0.1 → 3.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +9 -3
  2. package/chains/chainid.network.json +7691 -1349
  3. package/chains/deployments/1/ETH/RealityETH-3.0.json +4 -1
  4. package/chains/deployments/1/SWISE/RealityETH_ERC20-3.0.json +7 -0
  5. package/chains/deployments/10/OETH/RealityETH-3.0.json +6 -0
  6. package/chains/deployments/100/GNO/RealityETH_ERC20-3.0.json +7 -0
  7. package/chains/deployments/100/SWISE/RealityETH_ERC20-3.0.json +7 -0
  8. package/chains/deployments/100/XDAI/RealityETH-3.0.json +3 -1
  9. package/chains/deployments/1337702/ETH/RealityETH-3.0.json +6 -0
  10. package/chains/deployments/4/ETH/RealityETH-3.0.json +3 -1
  11. package/chains/deployments/42/ETH/RealityETH-3.0.json +2 -1
  12. package/chains/deployments/56/DEXE/RealityETH_ERC20-3.0.json +7 -0
  13. package/chains/deployments/69/OETH/RealityETH-3.0.json +6 -0
  14. package/chains/deployments/77/XDAI/RealityETH-2.1-rc1.json +1 -3
  15. package/chains/deployments/777/CTH/RealityETH-3.0.json +6 -0
  16. package/chains/supported.json +23 -3
  17. package/development/contracts/Arbitrator.sol +7 -6
  18. package/development/contracts/BalanceHolder.sol +4 -2
  19. package/development/contracts/{BalanceHolderERC20.sol → BalanceHolder_ERC20.sol} +3 -2
  20. package/development/contracts/IArbitrator.sol +4 -7
  21. package/development/contracts/IArbitratorForeignProxy.sol +12 -0
  22. package/development/contracts/IBalanceHolder.sol +3 -1
  23. package/development/contracts/IBalanceHolder_ERC20.sol +11 -0
  24. package/development/contracts/IERC20.sol +1 -1
  25. package/development/contracts/IOwned.sol +1 -1
  26. package/development/contracts/IRealityETH.sol +63 -0
  27. package/development/contracts/IRealityETH_ERC20.sol +67 -0
  28. package/development/contracts/Owned.sol +4 -2
  29. package/development/contracts/RealityETH-3.0.sol +4 -82
  30. package/development/contracts/RealityETH_ERC20-3.0.sol +3 -3
  31. package/generated/chains.json +95 -22
  32. package/generated/contracts.json +89 -7
  33. package/generated/tokens.json +34 -2
  34. package/index.js +9 -4
  35. package/package.json +3 -3
  36. package/scripts/deploy.js +14 -3
  37. package/scripts/fetch_and_reformat_chains_json.js +31 -0
  38. package/scripts/{format_abi.js → format_json.js} +0 -0
  39. package/scripts/generate_chains_json.js +3 -0
  40. package/scripts/send_funds.js +52 -0
  41. package/tokens/CTH.json +7 -0
  42. package/tokens/DEXE.json +7 -0
  43. package/tokens/ETH.json +2 -1
  44. package/tokens/GNO.json +1 -0
  45. package/tokens/OETH.json +8 -0
  46. package/tokens/SWISE.json +8 -0
  47. package/development/contracts/IRealitio.sol +0 -44
  48. package/development/contracts/MultiSigWallet.sol +0 -391
  49. package/development/contracts/Realitio.sol +0 -756
  50. package/development/contracts/RealitioERC20.sol +0 -825
  51. package/development/contracts/RealitioSafeMath256.sol +0 -36
  52. package/development/contracts/RealitioSafeMath32.sol +0 -17
  53. package/development/contracts/Realitio_v2_1.sol +0 -819
  54. package/development/contracts/SplitterWallet.sol +0 -142
  55. package/tests/python/test_wallet.py +0 -185
@@ -1,756 +0,0 @@
1
- // SPDX-License-Identifier: GPL-3.0-only
2
-
3
- pragma solidity ^0.8.6;
4
-
5
- import './RealitioSafeMath256.sol';
6
- import './RealitioSafeMath32.sol';
7
- import './BalanceHolder.sol';
8
-
9
-
10
- contract Realitio is BalanceHolder {
11
-
12
- using RealitioSafeMath256 for uint256;
13
- using RealitioSafeMath32 for uint32;
14
-
15
- address constant NULL_ADDRESS = address(0);
16
-
17
- // History hash when no history is created, or history has been cleared
18
- bytes32 constant NULL_HASH = bytes32(0);
19
-
20
- // An unitinalized finalize_ts for a question will indicate an unanswered question.
21
- uint32 constant UNANSWERED = 0;
22
-
23
- // An unanswered reveal_ts for a commitment will indicate that it does not exist.
24
- uint256 constant COMMITMENT_NON_EXISTENT = 0;
25
-
26
- // Commit->reveal timeout is 1/8 of the question timeout (rounded down).
27
- uint32 constant COMMITMENT_TIMEOUT_RATIO = 8;
28
-
29
- event LogSetQuestionFee(
30
- address arbitrator,
31
- uint256 amount
32
- );
33
-
34
- event LogNewTemplate(
35
- uint256 indexed template_id,
36
- address indexed user,
37
- string question_text
38
- );
39
-
40
- event LogNewQuestion(
41
- bytes32 indexed question_id,
42
- address indexed user,
43
- uint256 template_id,
44
- string question,
45
- bytes32 indexed content_hash,
46
- address arbitrator,
47
- uint32 timeout,
48
- uint32 opening_ts,
49
- uint256 nonce,
50
- uint256 created
51
- );
52
-
53
- event LogFundAnswerBounty(
54
- bytes32 indexed question_id,
55
- uint256 bounty_added,
56
- uint256 bounty,
57
- address indexed user
58
- );
59
-
60
- event LogNewAnswer(
61
- bytes32 answer,
62
- bytes32 indexed question_id,
63
- bytes32 history_hash,
64
- address indexed user,
65
- uint256 bond,
66
- uint256 ts,
67
- bool is_commitment
68
- );
69
-
70
- event LogAnswerReveal(
71
- bytes32 indexed question_id,
72
- address indexed user,
73
- bytes32 indexed answer_hash,
74
- bytes32 answer,
75
- uint256 nonce,
76
- uint256 bond
77
- );
78
-
79
- event LogNotifyOfArbitrationRequest(
80
- bytes32 indexed question_id,
81
- address indexed user
82
- );
83
-
84
- event LogFinalize(
85
- bytes32 indexed question_id,
86
- bytes32 indexed answer
87
- );
88
-
89
- event LogClaim(
90
- bytes32 indexed question_id,
91
- address indexed user,
92
- uint256 amount
93
- );
94
-
95
- struct Question {
96
- bytes32 content_hash;
97
- address arbitrator;
98
- uint32 opening_ts;
99
- uint32 timeout;
100
- uint32 finalize_ts;
101
- bool is_pending_arbitration;
102
- uint256 bounty;
103
- bytes32 best_answer;
104
- bytes32 history_hash;
105
- uint256 bond;
106
- }
107
-
108
- // Stored in a mapping indexed by commitment_id, a hash of commitment hash, question, bond.
109
- struct Commitment {
110
- uint32 reveal_ts;
111
- bool is_revealed;
112
- bytes32 revealed_answer;
113
- }
114
-
115
- // Only used when claiming more bonds than fits into a transaction
116
- // Stored in a mapping indexed by question_id.
117
- struct Claim {
118
- address payee;
119
- uint256 last_bond;
120
- uint256 queued_funds;
121
- }
122
-
123
- uint256 nextTemplateID = 0;
124
- mapping(uint256 => uint256) public templates;
125
- mapping(uint256 => bytes32) public template_hashes;
126
- mapping(bytes32 => Question) public questions;
127
- mapping(bytes32 => Claim) public question_claims;
128
- mapping(bytes32 => Commitment) public commitments;
129
- mapping(address => uint256) public arbitrator_question_fees;
130
-
131
- modifier onlyArbitrator(bytes32 question_id) {
132
- require(msg.sender == questions[question_id].arbitrator, "msg.sender must be arbitrator");
133
- _;
134
- }
135
-
136
- modifier stateAny() {
137
- _;
138
- }
139
-
140
- modifier stateNotCreated(bytes32 question_id) {
141
- require(questions[question_id].timeout == 0, "question must not exist");
142
- _;
143
- }
144
-
145
- modifier stateOpen(bytes32 question_id) {
146
- require(questions[question_id].timeout > 0, "question must exist");
147
- require(!questions[question_id].is_pending_arbitration, "question must not be pending arbitration");
148
- uint32 finalize_ts = questions[question_id].finalize_ts;
149
- require(finalize_ts == UNANSWERED || finalize_ts > uint32(block.timestamp), "finalization deadline must not have passed");
150
- uint32 opening_ts = questions[question_id].opening_ts;
151
- require(opening_ts == 0 || opening_ts <= uint32(block.timestamp), "opening date must have passed");
152
- _;
153
- }
154
-
155
- modifier statePendingArbitration(bytes32 question_id) {
156
- require(questions[question_id].is_pending_arbitration, "question must be pending arbitration");
157
- _;
158
- }
159
-
160
- modifier stateOpenOrPendingArbitration(bytes32 question_id) {
161
- require(questions[question_id].timeout > 0, "question must exist");
162
- uint32 finalize_ts = questions[question_id].finalize_ts;
163
- require(finalize_ts == UNANSWERED || finalize_ts > uint32(block.timestamp), "finalization dealine must not have passed");
164
- uint32 opening_ts = questions[question_id].opening_ts;
165
- require(opening_ts == 0 || opening_ts <= uint32(block.timestamp), "opening date must have passed");
166
- _;
167
- }
168
-
169
- modifier stateFinalized(bytes32 question_id) {
170
- require(isFinalized(question_id), "question must be finalized");
171
- _;
172
- }
173
-
174
- modifier bondMustDouble(bytes32 question_id) {
175
- require(msg.value > 0, "bond must be positive");
176
- require(msg.value >= (questions[question_id].bond.mul(2)), "bond must be double at least previous bond");
177
- _;
178
- }
179
-
180
- modifier previousBondMustNotBeatMaxPrevious(bytes32 question_id, uint256 max_previous) {
181
- if (max_previous > 0) {
182
- require(questions[question_id].bond <= max_previous, "bond must exceed max_previous");
183
- }
184
- _;
185
- }
186
-
187
- /// @notice Constructor, sets up some initial templates
188
- /// @dev Creates some generalized templates for different question types used in the DApp.
189
- constructor() {
190
- createTemplate('{"title": "%s", "type": "bool", "category": "%s", "lang": "%s"}');
191
- createTemplate('{"title": "%s", "type": "uint", "decimals": 18, "category": "%s", "lang": "%s"}');
192
- createTemplate('{"title": "%s", "type": "single-select", "outcomes": [%s], "category": "%s", "lang": "%s"}');
193
- createTemplate('{"title": "%s", "type": "multiple-select", "outcomes": [%s], "category": "%s", "lang": "%s"}');
194
- createTemplate('{"title": "%s", "type": "datetime", "category": "%s", "lang": "%s"}');
195
- }
196
-
197
- /// @notice Function for arbitrator to set an optional per-question fee.
198
- /// @dev The per-question fee, charged when a question is asked, is intended as an anti-spam measure.
199
- /// @param fee The fee to be charged by the arbitrator when a question is asked
200
- function setQuestionFee(uint256 fee)
201
- stateAny()
202
- external {
203
- arbitrator_question_fees[msg.sender] = fee;
204
- emit LogSetQuestionFee(msg.sender, fee);
205
- }
206
-
207
- /// @notice Create a reusable template, which should be a JSON document.
208
- /// Placeholders should use gettext() syntax, eg %s.
209
- /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
210
- /// @param content The template content
211
- /// @return The ID of the newly-created template, which is created sequentially.
212
- function createTemplate(string memory content)
213
- stateAny()
214
- public returns (uint256) {
215
- uint256 id = nextTemplateID;
216
- templates[id] = block.number;
217
- template_hashes[id] = keccak256(abi.encodePacked(content));
218
- emit LogNewTemplate(id, msg.sender, content);
219
- nextTemplateID = id.add(1);
220
- return id;
221
- }
222
-
223
- /// @notice Create a new reusable template and use it to ask a question
224
- /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
225
- /// @param content The template content
226
- /// @param question A string containing the parameters that will be passed into the template to make the question
227
- /// @param arbitrator The arbitration contract that will have the final word on the answer if there is a dispute
228
- /// @param timeout How long the contract should wait after the answer is changed before finalizing on that answer
229
- /// @param opening_ts If set, the earliest time it should be possible to answer the question.
230
- /// @param nonce A user-specified nonce used in the question ID. Change it to repeat a question.
231
- /// @return The ID of the newly-created template, which is created sequentially.
232
- function createTemplateAndAskQuestion(
233
- string memory content,
234
- string memory question, address arbitrator, uint32 timeout, uint32 opening_ts, uint256 nonce
235
- )
236
- // stateNotCreated is enforced by the internal _askQuestion
237
- public payable returns (bytes32) {
238
- uint256 template_id = createTemplate(content);
239
- return askQuestion(template_id, question, arbitrator, timeout, opening_ts, nonce);
240
- }
241
-
242
- /// @notice Ask a new question and return the ID
243
- /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
244
- /// @param template_id The ID number of the template the question will use
245
- /// @param question A string containing the parameters that will be passed into the template to make the question
246
- /// @param arbitrator The arbitration contract that will have the final word on the answer if there is a dispute
247
- /// @param timeout How long the contract should wait after the answer is changed before finalizing on that answer
248
- /// @param opening_ts If set, the earliest time it should be possible to answer the question.
249
- /// @param nonce A user-specified nonce used in the question ID. Change it to repeat a question.
250
- /// @return The ID of the newly-created question, created deterministically.
251
- function askQuestion(uint256 template_id, string memory question, address arbitrator, uint32 timeout, uint32 opening_ts, uint256 nonce)
252
- // stateNotCreated is enforced by the internal _askQuestion
253
- public payable returns (bytes32) {
254
-
255
- require(templates[template_id] > 0, "template must exist");
256
-
257
- bytes32 content_hash = keccak256(abi.encodePacked(template_id, opening_ts, question));
258
- bytes32 question_id = keccak256(abi.encodePacked(content_hash, arbitrator, timeout, msg.sender, nonce));
259
-
260
- _askQuestion(question_id, content_hash, arbitrator, timeout, opening_ts);
261
- emit LogNewQuestion(question_id, msg.sender, template_id, question, content_hash, arbitrator, timeout, opening_ts, nonce, block.timestamp);
262
-
263
- return question_id;
264
- }
265
-
266
- function _askQuestion(bytes32 question_id, bytes32 content_hash, address arbitrator, uint32 timeout, uint32 opening_ts)
267
- stateNotCreated(question_id)
268
- internal {
269
-
270
- // A timeout of 0 makes no sense, and we will use this to check existence
271
- require(timeout > 0, "timeout must be positive");
272
- require(timeout < 365 days, "timeout must be less than 365 days");
273
- require(arbitrator != NULL_ADDRESS, "arbitrator must be set");
274
-
275
- uint256 bounty = msg.value;
276
-
277
- // The arbitrator can set a fee for asking a question.
278
- // This is intended as an anti-spam defence.
279
- // The fee is waived if the arbitrator is asking the question.
280
- // This allows them to set an impossibly high fee and make users proxy the question through them.
281
- // This would allow more sophisticated pricing, question whitelisting etc.
282
- if (msg.sender != arbitrator) {
283
- uint256 question_fee = arbitrator_question_fees[arbitrator];
284
- require(bounty >= question_fee, "ETH provided must cover question fee");
285
- bounty = bounty.sub(question_fee);
286
- balanceOf[arbitrator] = balanceOf[arbitrator].add(question_fee);
287
- }
288
-
289
- questions[question_id].content_hash = content_hash;
290
- questions[question_id].arbitrator = arbitrator;
291
- questions[question_id].opening_ts = opening_ts;
292
- questions[question_id].timeout = timeout;
293
- questions[question_id].bounty = bounty;
294
-
295
- }
296
-
297
- /// @notice Add funds to the bounty for a question
298
- /// @dev Add bounty funds after the initial question creation. Can be done any time until the question is finalized.
299
- /// @param question_id The ID of the question you wish to fund
300
- function fundAnswerBounty(bytes32 question_id)
301
- stateOpen(question_id)
302
- external payable {
303
- questions[question_id].bounty = questions[question_id].bounty.add(msg.value);
304
- emit LogFundAnswerBounty(question_id, msg.value, questions[question_id].bounty, msg.sender);
305
- }
306
-
307
- /// @notice Submit an answer for a question.
308
- /// @dev Adds the answer to the history and updates the current "best" answer.
309
- /// May be subject to front-running attacks; Substitute submitAnswerCommitment()->submitAnswerReveal() to prevent them.
310
- /// @param question_id The ID of the question
311
- /// @param answer The answer, encoded into bytes32
312
- /// @param max_previous If specified, reverts if a bond higher than this was submitted after you sent your transaction.
313
- function submitAnswer(bytes32 question_id, bytes32 answer, uint256 max_previous)
314
- stateOpen(question_id)
315
- bondMustDouble(question_id)
316
- previousBondMustNotBeatMaxPrevious(question_id, max_previous)
317
- external payable {
318
- _addAnswerToHistory(question_id, answer, msg.sender, msg.value, false);
319
- _updateCurrentAnswer(question_id, answer, questions[question_id].timeout);
320
- }
321
-
322
- // @notice Verify and store a commitment, including an appropriate timeout
323
- // @param question_id The ID of the question to store
324
- // @param commitment The ID of the commitment
325
- function _storeCommitment(bytes32 question_id, bytes32 commitment_id)
326
- internal
327
- {
328
- require(commitments[commitment_id].reveal_ts == COMMITMENT_NON_EXISTENT, "commitment must not already exist");
329
-
330
- uint32 commitment_timeout = questions[question_id].timeout / COMMITMENT_TIMEOUT_RATIO;
331
- commitments[commitment_id].reveal_ts = uint32(block.timestamp).add(commitment_timeout);
332
- }
333
-
334
- /// @notice Submit the hash of an answer, laying your claim to that answer if you reveal it in a subsequent transaction.
335
- /// @dev Creates a hash, commitment_id, uniquely identifying this answer, to this question, with this bond.
336
- /// The commitment_id is stored in the answer history where the answer would normally go.
337
- /// Does not update the current best answer - this is left to the later submitAnswerReveal() transaction.
338
- /// @param question_id The ID of the question
339
- /// @param answer_hash The hash of your answer, plus a nonce that you will later reveal
340
- /// @param max_previous If specified, reverts if a bond higher than this was submitted after you sent your transaction.
341
- /// @param _answerer If specified, the address to be given as the question answerer. Defaults to the sender.
342
- /// @dev Specifying the answerer is useful if you want to delegate the commit-and-reveal to a third-party.
343
- function submitAnswerCommitment(bytes32 question_id, bytes32 answer_hash, uint256 max_previous, address _answerer)
344
- stateOpen(question_id)
345
- bondMustDouble(question_id)
346
- previousBondMustNotBeatMaxPrevious(question_id, max_previous)
347
- external payable {
348
-
349
- bytes32 commitment_id = keccak256(abi.encodePacked(question_id, answer_hash, msg.value));
350
- address answerer = (_answerer == NULL_ADDRESS) ? msg.sender : _answerer;
351
- _storeCommitment(question_id, commitment_id);
352
- _addAnswerToHistory(question_id, commitment_id, answerer, msg.value, true);
353
-
354
- }
355
-
356
- /// @notice Submit the answer whose hash you sent in a previous submitAnswerCommitment() transaction
357
- /// @dev Checks the parameters supplied recreate an existing commitment, and stores the revealed answer
358
- /// Updates the current answer unless someone has since supplied a new answer with a higher bond
359
- /// msg.sender is intentionally not restricted to the user who originally sent the commitment;
360
- /// For example, the user may want to provide the answer+nonce to a third-party service and let them send the tx
361
- /// NB If we are pending arbitration, it will be up to the arbitrator to wait and see any outstanding reveal is sent
362
- /// @param question_id The ID of the question
363
- /// @param answer The answer, encoded as bytes32
364
- /// @param nonce The nonce that, combined with the answer, recreates the answer_hash you gave in submitAnswerCommitment()
365
- /// @param bond The bond that you paid in your submitAnswerCommitment() transaction
366
- function submitAnswerReveal(bytes32 question_id, bytes32 answer, uint256 nonce, uint256 bond)
367
- stateOpenOrPendingArbitration(question_id)
368
- external {
369
-
370
- bytes32 answer_hash = keccak256(abi.encodePacked(answer, nonce));
371
- bytes32 commitment_id = keccak256(abi.encodePacked(question_id, answer_hash, bond));
372
-
373
- require(!commitments[commitment_id].is_revealed, "commitment must not have been revealed yet");
374
- require(commitments[commitment_id].reveal_ts > uint32(block.timestamp), "reveal deadline must not have passed");
375
-
376
- commitments[commitment_id].revealed_answer = answer;
377
- commitments[commitment_id].is_revealed = true;
378
-
379
- if (bond == questions[question_id].bond) {
380
- _updateCurrentAnswer(question_id, answer, questions[question_id].timeout);
381
- }
382
-
383
- emit LogAnswerReveal(question_id, msg.sender, answer_hash, answer, nonce, bond);
384
-
385
- }
386
-
387
- function _addAnswerToHistory(bytes32 question_id, bytes32 answer_or_commitment_id, address answerer, uint256 bond, bool is_commitment)
388
- internal
389
- {
390
- bytes32 new_history_hash = keccak256(abi.encodePacked(questions[question_id].history_hash, answer_or_commitment_id, bond, answerer, is_commitment));
391
-
392
- // Update the current bond level, if there's a bond (ie anything except arbitration)
393
- if (bond > 0) {
394
- questions[question_id].bond = bond;
395
- }
396
- questions[question_id].history_hash = new_history_hash;
397
-
398
- emit LogNewAnswer(answer_or_commitment_id, question_id, new_history_hash, answerer, bond, block.timestamp, is_commitment);
399
- }
400
-
401
- function _updateCurrentAnswer(bytes32 question_id, bytes32 answer, uint32 timeout_secs)
402
- internal {
403
- questions[question_id].best_answer = answer;
404
- questions[question_id].finalize_ts = uint32(block.timestamp).add(timeout_secs);
405
- }
406
-
407
- /// @notice Notify the contract that the arbitrator has been paid for a question, freezing it pending their decision.
408
- /// @dev The arbitrator contract is trusted to only call this if they've been paid, and tell us who paid them.
409
- /// @param question_id The ID of the question
410
- /// @param requester The account that requested arbitration
411
- /// @param max_previous If specified, reverts if a bond higher than this was submitted after you sent your transaction.
412
- function notifyOfArbitrationRequest(bytes32 question_id, address requester, uint256 max_previous)
413
- onlyArbitrator(question_id)
414
- stateOpen(question_id)
415
- previousBondMustNotBeatMaxPrevious(question_id, max_previous)
416
- external {
417
- require(questions[question_id].bond > 0, "Question must already have an answer when arbitration is requested");
418
- questions[question_id].is_pending_arbitration = true;
419
- emit LogNotifyOfArbitrationRequest(question_id, requester);
420
- }
421
-
422
- /// @notice Submit the answer for a question, for use by the arbitrator.
423
- /// @dev Doesn't require (or allow) a bond.
424
- /// If the current final answer is correct, the account should be whoever submitted it.
425
- /// If the current final answer is wrong, the account should be whoever paid for arbitration.
426
- /// However, the answerer stipulations are not enforced by the contract.
427
- /// @param question_id The ID of the question
428
- /// @param answer The answer, encoded into bytes32
429
- /// @param answerer The account credited with this answer for the purpose of bond claims
430
- function submitAnswerByArbitrator(bytes32 question_id, bytes32 answer, address answerer)
431
- onlyArbitrator(question_id)
432
- statePendingArbitration(question_id)
433
- external {
434
-
435
- require(answerer != NULL_ADDRESS, "answerer must be provided");
436
- emit LogFinalize(question_id, answer);
437
-
438
- questions[question_id].is_pending_arbitration = false;
439
- _addAnswerToHistory(question_id, answer, answerer, 0, false);
440
- _updateCurrentAnswer(question_id, answer, 0);
441
-
442
- }
443
-
444
- /// @notice Report whether the answer to the specified question is finalized
445
- /// @param question_id The ID of the question
446
- /// @return Return true if finalized
447
- function isFinalized(bytes32 question_id)
448
- view public returns (bool) {
449
- uint32 finalize_ts = questions[question_id].finalize_ts;
450
- return ( !questions[question_id].is_pending_arbitration && (finalize_ts > UNANSWERED) && (finalize_ts <= uint32(block.timestamp)) );
451
- }
452
-
453
- /// @notice (Deprecated) Return the final answer to the specified question, or revert if there isn't one
454
- /// @param question_id The ID of the question
455
- /// @return The answer formatted as a bytes32
456
- function getFinalAnswer(bytes32 question_id)
457
- stateFinalized(question_id)
458
- external view returns (bytes32) {
459
- return questions[question_id].best_answer;
460
- }
461
-
462
- /// @notice Return the final answer to the specified question, or revert if there isn't one
463
- /// @param question_id The ID of the question
464
- /// @return The answer formatted as a bytes32
465
- function resultFor(bytes32 question_id)
466
- stateFinalized(question_id)
467
- external view returns (bytes32) {
468
- return questions[question_id].best_answer;
469
- }
470
-
471
-
472
- /// @notice Return the final answer to the specified question, provided it matches the specified criteria.
473
- /// @dev Reverts if the question is not finalized, or if it does not match the specified criteria.
474
- /// @param question_id The ID of the question
475
- /// @param content_hash The hash of the question content (template ID + opening time + question parameter string)
476
- /// @param arbitrator The arbitrator chosen for the question (regardless of whether they are asked to arbitrate)
477
- /// @param min_timeout The timeout set in the initial question settings must be this high or higher
478
- /// @param min_bond The bond sent with the final answer must be this high or higher
479
- /// @return The answer formatted as a bytes32
480
- function getFinalAnswerIfMatches(
481
- bytes32 question_id,
482
- bytes32 content_hash, address arbitrator, uint32 min_timeout, uint256 min_bond
483
- )
484
- stateFinalized(question_id)
485
- external view returns (bytes32) {
486
- require(content_hash == questions[question_id].content_hash, "content hash must match");
487
- require(arbitrator == questions[question_id].arbitrator, "arbitrator must match");
488
- require(min_timeout <= questions[question_id].timeout, "timeout must be long enough");
489
- require(min_bond <= questions[question_id].bond, "bond must be high enough");
490
- return questions[question_id].best_answer;
491
- }
492
-
493
- /// @notice Assigns the winnings (bounty and bonds) to everyone who gave the accepted answer
494
- /// Caller must provide the answer history, in reverse order
495
- /// @dev Works up the chain and assign bonds to the person who gave the right answer
496
- /// If someone gave the winning answer earlier, they must get paid from the higher bond
497
- /// That means we can't pay out the bond added at n until we have looked at n-1
498
- /// The first answer is authenticated by checking against the stored history_hash.
499
- /// One of the inputs to history_hash is the history_hash before it, so we use that to authenticate the next entry, etc
500
- /// Once we get to a null hash we'll kblock.timestamp we're done and there are no more answers.
501
- /// Usually you would call the whole thing in a single transaction, but if not then the data is persisted to pick up later.
502
- /// @param question_id The ID of the question
503
- /// @param history_hashes Second-last-to-first, the hash of each history entry. (Final one should be empty).
504
- /// @param addrs Last-to-first, the address of each answerer or commitment sender
505
- /// @param bonds Last-to-first, the bond supplied with each answer or commitment
506
- /// @param answers Last-to-first, each answer supplied, or commitment ID if the answer was supplied with commit->reveal
507
- function claimWinnings(
508
- bytes32 question_id,
509
- bytes32[] memory history_hashes, address[] memory addrs, uint256[] memory bonds, bytes32[] memory answers
510
- )
511
- stateFinalized(question_id)
512
- public {
513
-
514
- require(history_hashes.length > 0, "at least one history hash entry must be provided");
515
-
516
- // These are only set if we split our claim over multiple transactions.
517
- address payee = question_claims[question_id].payee;
518
- uint256 last_bond = question_claims[question_id].last_bond;
519
- uint256 queued_funds = question_claims[question_id].queued_funds;
520
-
521
- // Starts as the hash of the final answer submitted. It'll be cleared when we're done.
522
- // If we're splitting the claim over multiple transactions, it'll be the hash where we left off last time
523
- bytes32 last_history_hash = questions[question_id].history_hash;
524
-
525
- bytes32 best_answer = questions[question_id].best_answer;
526
-
527
- uint256 i;
528
- for (i = 0; i < history_hashes.length; i++) {
529
-
530
- // Check input against the history hash, and see which of 2 possible values of is_commitment fits.
531
- bool is_commitment = _verifyHistoryInputOrRevert(last_history_hash, history_hashes[i], answers[i], bonds[i], addrs[i]);
532
-
533
- queued_funds = queued_funds.add(last_bond);
534
- (queued_funds, payee) = _processHistoryItem(
535
- question_id, best_answer, queued_funds, payee,
536
- addrs[i], bonds[i], answers[i], is_commitment);
537
-
538
- // Line the bond up for next time, when it will be added to somebody's queued_funds
539
- last_bond = bonds[i];
540
- last_history_hash = history_hashes[i];
541
-
542
- }
543
-
544
- if (last_history_hash != NULL_HASH) {
545
- // We haven't yet got to the null hash (1st answer), ie the caller didn't supply the full answer chain.
546
- // Persist the details so we can pick up later where we left off later.
547
-
548
- // If we kblock.timestamp who to pay we can go ahead and pay them out, only keeping back last_bond
549
- // (We always kblock.timestamp who to pay unless all we saw were unrevealed commits)
550
- if (payee != NULL_ADDRESS) {
551
- _payPayee(question_id, payee, queued_funds);
552
- queued_funds = 0;
553
- }
554
-
555
- question_claims[question_id].payee = payee;
556
- question_claims[question_id].last_bond = last_bond;
557
- question_claims[question_id].queued_funds = queued_funds;
558
- } else {
559
- // There is nothing left below us so the payee can keep what remains
560
- _payPayee(question_id, payee, queued_funds.add(last_bond));
561
- delete question_claims[question_id];
562
- }
563
-
564
- questions[question_id].history_hash = last_history_hash;
565
-
566
- }
567
-
568
- function _payPayee(bytes32 question_id, address payee, uint256 value)
569
- internal {
570
- balanceOf[payee] = balanceOf[payee].add(value);
571
- emit LogClaim(question_id, payee, value);
572
- }
573
-
574
- function _verifyHistoryInputOrRevert(
575
- bytes32 last_history_hash,
576
- bytes32 history_hash, bytes32 answer, uint256 bond, address addr
577
- )
578
- internal pure returns (bool) {
579
- if (last_history_hash == keccak256(abi.encodePacked(history_hash, answer, bond, addr, true)) ) {
580
- return true;
581
- }
582
- if (last_history_hash == keccak256(abi.encodePacked(history_hash, answer, bond, addr, false)) ) {
583
- return false;
584
- }
585
- revert("History input provided did not match the expected hash");
586
- }
587
-
588
- function _processHistoryItem(
589
- bytes32 question_id, bytes32 best_answer,
590
- uint256 queued_funds, address payee,
591
- address addr, uint256 bond, bytes32 answer, bool is_commitment
592
- )
593
- internal returns (uint256, address) {
594
-
595
- // For commit-and-reveal, the answer history holds the commitment ID instead of the answer.
596
- // We look at the referenced commitment ID and switch in the actual answer.
597
- if (is_commitment) {
598
- bytes32 commitment_id = answer;
599
- // If it's a commit but it hasn't been revealed, it will always be considered wrong.
600
- if (!commitments[commitment_id].is_revealed) {
601
- delete commitments[commitment_id];
602
- return (queued_funds, payee);
603
- } else {
604
- answer = commitments[commitment_id].revealed_answer;
605
- delete commitments[commitment_id];
606
- }
607
- }
608
-
609
- if (answer == best_answer) {
610
-
611
- if (payee == NULL_ADDRESS) {
612
-
613
- // The entry is for the first payee we come to, ie the winner.
614
- // They get the question bounty.
615
- payee = addr;
616
- queued_funds = queued_funds.add(questions[question_id].bounty);
617
- questions[question_id].bounty = 0;
618
-
619
- } else if (addr != payee) {
620
-
621
- // Answerer has changed, ie we found someone lower down who needs to be paid
622
-
623
- // The lower answerer will take over receiving bonds from higher answerer.
624
- // They should also be paid the takeover fee, which is set at a rate equivalent to their bond.
625
- // (This is our arbitrary rule, to give consistent right-answerers a defence against high-rollers.)
626
-
627
- // There should be enough for the fee, but if not, take what we have.
628
- // There's an edge case involving weird arbitrator behaviour where we may be short.
629
- uint256 answer_takeover_fee = (queued_funds >= bond) ? bond : queued_funds;
630
-
631
- // Settle up with the old (higher-bonded) payee
632
- _payPayee(question_id, payee, queued_funds.sub(answer_takeover_fee));
633
-
634
- // Now start queued_funds again for the new (lower-bonded) payee
635
- payee = addr;
636
- queued_funds = answer_takeover_fee;
637
-
638
- }
639
-
640
- }
641
-
642
- return (queued_funds, payee);
643
-
644
- }
645
-
646
- /// @notice Convenience function to assign bounties/bonds for multiple questions in one go, then withdraw all your funds.
647
- /// Caller must provide the answer history for each question, in reverse order
648
- /// @dev Can be called by anyone to assign bonds/bounties, but funds are only withdrawn for the user making the call.
649
- /// @param question_ids The IDs of the questions you want to claim for
650
- /// @param lengths The number of history entries you will supply for each question ID
651
- /// @param hist_hashes In a single list for all supplied questions, the hash of each history entry.
652
- /// @param addrs In a single list for all supplied questions, the address of each answerer or commitment sender
653
- /// @param bonds In a single list for all supplied questions, the bond supplied with each answer or commitment
654
- /// @param answers In a single list for all supplied questions, each answer supplied, or commitment ID
655
- function claimMultipleAndWithdrawBalance(
656
- bytes32[] memory question_ids, uint256[] memory lengths,
657
- bytes32[] memory hist_hashes, address[] memory addrs, uint256[] memory bonds, bytes32[] memory answers
658
- )
659
- stateAny() // The finalization checks are done in the claimWinnings function
660
- public {
661
-
662
- uint256 qi;
663
- uint256 i;
664
- for (qi = 0; qi < question_ids.length; qi++) {
665
- bytes32 qid = question_ids[qi];
666
- uint256 ln = lengths[qi];
667
- bytes32[] memory hh = new bytes32[](ln);
668
- address[] memory ad = new address[](ln);
669
- uint256[] memory bo = new uint256[](ln);
670
- bytes32[] memory an = new bytes32[](ln);
671
- uint256 j;
672
- for (j = 0; j < ln; j++) {
673
- hh[j] = hist_hashes[i];
674
- ad[j] = addrs[i];
675
- bo[j] = bonds[i];
676
- an[j] = answers[i];
677
- i++;
678
- }
679
- claimWinnings(qid, hh, ad, bo, an);
680
- }
681
- withdraw();
682
- }
683
-
684
- /// @notice Returns the questions's content hash, identifying the question content
685
- /// @param question_id The ID of the question
686
- function getContentHash(bytes32 question_id)
687
- public view returns(bytes32) {
688
- return questions[question_id].content_hash;
689
- }
690
-
691
- /// @notice Returns the arbitrator address for the question
692
- /// @param question_id The ID of the question
693
- function getArbitrator(bytes32 question_id)
694
- public view returns(address) {
695
- return questions[question_id].arbitrator;
696
- }
697
-
698
- /// @notice Returns the timestamp when the question can first be answered
699
- /// @param question_id The ID of the question
700
- function getOpeningTS(bytes32 question_id)
701
- public view returns(uint32) {
702
- return questions[question_id].opening_ts;
703
- }
704
-
705
- /// @notice Returns the timeout in seconds used after each answer
706
- /// @param question_id The ID of the question
707
- function getTimeout(bytes32 question_id)
708
- public view returns(uint32) {
709
- return questions[question_id].timeout;
710
- }
711
-
712
- /// @notice Returns the timestamp at which the question will be/was finalized
713
- /// @param question_id The ID of the question
714
- function getFinalizeTS(bytes32 question_id)
715
- public view returns(uint32) {
716
- return questions[question_id].finalize_ts;
717
- }
718
-
719
- /// @notice Returns whether the question is pending arbitration
720
- /// @param question_id The ID of the question
721
- function isPendingArbitration(bytes32 question_id)
722
- public view returns(bool) {
723
- return questions[question_id].is_pending_arbitration;
724
- }
725
-
726
- /// @notice Returns the current total unclaimed bounty
727
- /// @dev Set back to zero once the bounty has been claimed
728
- /// @param question_id The ID of the question
729
- function getBounty(bytes32 question_id)
730
- public view returns(uint256) {
731
- return questions[question_id].bounty;
732
- }
733
-
734
- /// @notice Returns the current best answer
735
- /// @param question_id The ID of the question
736
- function getBestAnswer(bytes32 question_id)
737
- public view returns(bytes32) {
738
- return questions[question_id].best_answer;
739
- }
740
-
741
- /// @notice Returns the history hash of the question
742
- /// @param question_id The ID of the question
743
- /// @dev Updated on each answer, then rewound as each is claimed
744
- function getHistoryHash(bytes32 question_id)
745
- public view returns(bytes32) {
746
- return questions[question_id].history_hash;
747
- }
748
-
749
- /// @notice Returns the highest bond posted so far for a question
750
- /// @param question_id The ID of the question
751
- function getBond(bytes32 question_id)
752
- public view returns(uint256) {
753
- return questions[question_id].bond;
754
- }
755
-
756
- }