@reality.eth/contracts 3.0.48 → 4.0.0-rc.1

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 (33) hide show
  1. package/abi/solc-0.8.20/RealityETH-4.0.abi.json +1 -1
  2. package/abi/solc-0.8.20/RealityETHFreezableExample_ERC20-4.0.abi.json +1 -0
  3. package/abi/solc-0.8.20/RealityETHReopenable-4.0.abi.json +1 -0
  4. package/abi/solc-0.8.20/RealityETHReopenable_ERC20-4.0.abi.json +1 -0
  5. package/abi/solc-0.8.20/RealityETH_ERC20-4.0.abi.json +1 -1
  6. package/bytecode/RealityETH-4.0.bin +1 -1
  7. package/bytecode/RealityETHFreezableExample_ERC20-4.0.bin +1 -0
  8. package/bytecode/RealityETHReopenable-4.0.bin +1 -0
  9. package/bytecode/RealityETHReopenable_ERC20-4.0.bin +1 -0
  10. package/bytecode/RealityETH_ERC20-4.0.bin +1 -1
  11. package/chains/supported.json +1 -1
  12. package/development/contracts/IBalanceHolder.sol +1 -0
  13. package/development/contracts/IRealityETH.sol +6 -2
  14. package/development/contracts/{IRealityETHCore.sol → IRealityETHCore_Common.sol} +4 -23
  15. package/development/contracts/IRealityETHCore_ERC20.sol +3 -124
  16. package/development/contracts/IRealityETHCore_Native.sol +12 -0
  17. package/development/contracts/IRealityETHCreateTemplateAndAskQuestion.sol +8 -0
  18. package/development/contracts/IRealityETHHistoryVerification.sol +8 -0
  19. package/development/contracts/IRealityETHReopenable.sol +22 -0
  20. package/development/contracts/IRealityETHReopenable_ERC20.sol +23 -0
  21. package/development/contracts/IRealityETH_ERC20.sol +5 -1
  22. package/development/contracts/RealityETH-4.0.sol +12 -614
  23. package/development/contracts/RealityETHCore_Common.sol +535 -0
  24. package/development/contracts/RealityETHFreezableExample_ERC20-4.0.sol +17 -0
  25. package/development/contracts/RealityETHFreezable_ERC20.sol +39 -0
  26. package/development/contracts/RealityETHReopenable-4.0.sol +119 -0
  27. package/development/contracts/RealityETHReopenable_ERC20-4.0.sol +122 -0
  28. package/development/contracts/RealityETH_ERC20-4.0.sol +13 -614
  29. package/generated/chains.json +1 -1
  30. package/package.json +2 -2
  31. package/tests/python/test.py +186 -5
  32. package/development/contracts/BalanceHolder_ERC20.sol +0 -21
  33. package/development/contracts/IBalanceHolder_ERC20.sol +0 -11
@@ -2,158 +2,11 @@
2
2
 
3
3
  pragma solidity ^0.8.20;
4
4
 
5
- import {IRealityETHCore} from "./IRealityETHCore.sol";
6
- import {BalanceHolder} from "./BalanceHolder.sol";
5
+ import {RealityETHCore_Common} from "./RealityETHCore_Common.sol";
6
+ import {IRealityETHCore_Native} from "./IRealityETHCore_Native.sol";
7
7
 
8
8
  // solhint-disable-next-line contract-name-camelcase
9
- contract RealityETH_v4_0 is BalanceHolder, IRealityETHCore {
10
- address private constant NULL_ADDRESS = address(0);
11
-
12
- // History hash when no history is created, or history has been cleared
13
- bytes32 private constant NULL_HASH = bytes32(0);
14
-
15
- // An unitinalized finalize_ts for a question will indicate an unanswered question.
16
- uint32 private constant UNANSWERED = 0;
17
-
18
- // Proportion withheld when you claim an earlier bond.
19
- uint256 private constant BOND_CLAIM_FEE_PROPORTION = 40; // One 40th ie 2.5%
20
-
21
- // Special value representing a question that was answered too soon.
22
- // bytes32(-2). By convention we use bytes32(-1) for "invalid", although the contract does not handle this.
23
- bytes32 private constant UNRESOLVED_ANSWER = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe;
24
-
25
- uint256 private nextTemplateID = 0;
26
- mapping(uint256 => uint256) public templates;
27
- mapping(uint256 => bytes32) public template_hashes;
28
- mapping(bytes32 => Question) public questions;
29
- mapping(bytes32 => Claim) public question_claims;
30
- mapping(address => uint256) public arbitrator_question_fees;
31
- mapping(bytes32 => bytes32) public reopened_questions;
32
- mapping(bytes32 => bool) public reopener_questions;
33
-
34
- modifier onlyArbitrator(bytes32 question_id) {
35
- if (msg.sender != questions[question_id].arbitrator) revert MsgSenderMustBeArbitrator();
36
- _;
37
- }
38
-
39
- modifier stateAny() {
40
- _;
41
- }
42
-
43
- modifier stateNotCreated(bytes32 question_id) {
44
- if (questions[question_id].timeout != 0) revert QuestionMustNotExist();
45
- _;
46
- }
47
-
48
- modifier stateOpen(bytes32 question_id) {
49
- if (questions[question_id].timeout == 0) revert QuestionMustExist();
50
- if (questions[question_id].is_pending_arbitration) revert QuestionMustNotBePendingArbitration();
51
- uint32 finalize_ts = questions[question_id].finalize_ts;
52
- if (finalize_ts != UNANSWERED && finalize_ts <= uint32(block.timestamp)) revert FinalizationDeadlineMustNotHavePassed();
53
- uint32 opening_ts = questions[question_id].opening_ts;
54
- if (opening_ts != 0 && opening_ts > uint32(block.timestamp)) revert OpeningDateMustHavePassed();
55
- _;
56
- }
57
-
58
- modifier statePendingArbitration(bytes32 question_id) {
59
- if (!questions[question_id].is_pending_arbitration) revert QuestionMustBePendingArbitration();
60
- _;
61
- }
62
-
63
- modifier stateOpenOrPendingArbitration(bytes32 question_id) {
64
- if (questions[question_id].timeout == 0) revert QuestionMustExist();
65
- uint32 finalize_ts = questions[question_id].finalize_ts;
66
- if (finalize_ts != UNANSWERED && finalize_ts <= uint32(block.timestamp)) revert FinalizationDealineMustNotHavePassed();
67
- uint32 opening_ts = questions[question_id].opening_ts;
68
- if (opening_ts != 0 && opening_ts > uint32(block.timestamp)) revert OpeningDateMustHavePassed();
69
- _;
70
- }
71
-
72
- modifier stateFinalized(bytes32 question_id) {
73
- if (!isFinalized(question_id)) revert QuestionMustBeFinalized();
74
- _;
75
- }
76
-
77
- modifier bondMustDoubleAndMatchMinimum(bytes32 question_id, uint256 tokens) {
78
- if (tokens == 0) revert BondMustBePositive();
79
- uint256 current_bond = questions[question_id].bond;
80
- if (current_bond == 0) {
81
- if (tokens < (questions[question_id].min_bond)) revert BondMustExceedTheMinimum();
82
- } else {
83
- if (tokens < (current_bond * 2)) revert BondMustBeDoubleAtLeastPreviousBond();
84
- }
85
- _;
86
- }
87
-
88
- modifier previousBondMustNotBeatMaxPrevious(bytes32 question_id, uint256 max_previous) {
89
- if (max_previous > 0) {
90
- if (questions[question_id].bond > max_previous) revert BondMustExceedMax_Previous();
91
- }
92
- _;
93
- }
94
-
95
- /* solhint-disable quotes */
96
- /// @notice Constructor, sets up some initial templates
97
- /// @dev Creates some generalized templates for different question types used in the DApp.
98
- constructor() {
99
- createTemplate('{"title": "%s", "type": "bool", "category": "%s", "lang": "%s"}');
100
- createTemplate('{"title": "%s", "type": "uint", "decimals": 18, "category": "%s", "lang": "%s"}');
101
- createTemplate('{"title": "%s", "type": "single-select", "outcomes": [%s], "category": "%s", "lang": "%s"}');
102
- createTemplate('{"title": "%s", "type": "multiple-select", "outcomes": [%s], "category": "%s", "lang": "%s"}');
103
- createTemplate('{"title": "%s", "type": "datetime", "category": "%s", "lang": "%s"}');
104
- }
105
- /* solhint-enable quotes */
106
-
107
- /// @notice Function for arbitrator to set an optional per-question fee.
108
- /// @dev The per-question fee, charged when a question is asked, is intended as an anti-spam measure.
109
- /// @param fee The fee to be charged by the arbitrator when a question is asked
110
- function setQuestionFee(uint256 fee) external stateAny {
111
- arbitrator_question_fees[msg.sender] = fee;
112
- emit LogSetQuestionFee(msg.sender, fee);
113
- }
114
-
115
- /// @notice Create a reusable template, which should be a JSON document.
116
- /// Placeholders should use gettext() syntax, eg %s.
117
- /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
118
- /// @param content The template content
119
- /// @return The ID of the newly-created template, which is created sequentially.
120
- function createTemplate(string memory content) public stateAny returns (uint256) {
121
- uint256 id = nextTemplateID;
122
- templates[id] = block.number;
123
- template_hashes[id] = keccak256(abi.encodePacked(content));
124
- emit LogNewTemplate(id, msg.sender, content);
125
- nextTemplateID = id + 1;
126
- return id;
127
- }
128
-
129
- /// @notice Create a new reusable template and use it to ask a question
130
- /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
131
- /// @param content The template content
132
- /// @param question A string containing the parameters that will be passed into the template to make the question
133
- /// @param arbitrator The arbitration contract that will have the final word on the answer if there is a dispute
134
- /// @param timeout How long the contract should wait after the answer is changed before finalizing on that answer
135
- /// @param opening_ts If set, the earliest time it should be possible to answer the question.
136
- /// @param nonce A user-specified nonce used in the question ID. Change it to repeat a question.
137
- /// @return The ID of the newly-created template, which is created sequentially.
138
- function createTemplateAndAskQuestion(
139
- string memory content,
140
- string memory question,
141
- address arbitrator,
142
- uint32 timeout,
143
- uint32 opening_ts,
144
- uint256 nonce
145
- )
146
- public
147
- payable
148
- returns (
149
- // stateNotCreated is enforced by the internal _askQuestion
150
- bytes32
151
- )
152
- {
153
- uint256 template_id = createTemplate(content);
154
- return askQuestion(template_id, question, arbitrator, timeout, opening_ts, nonce);
155
- }
156
-
9
+ contract RealityETH_v4_0 is RealityETHCore_Common, IRealityETHCore_Native {
157
10
  /// @notice Ask a new question and return the ID
158
11
  /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
159
12
  /// @param template_id The ID number of the template the question will use
@@ -229,45 +82,10 @@ contract RealityETH_v4_0 is BalanceHolder, IRealityETHCore {
229
82
  return question_id;
230
83
  }
231
84
 
232
- function _askQuestion(bytes32 question_id, bytes32 content_hash, address arbitrator, uint32 timeout, uint32 opening_ts, uint256 min_bond, uint256 tokens) internal stateNotCreated(question_id) {
233
- // A timeout of 0 makes no sense, and we will use this to check existence
234
- if (timeout == 0) revert TimeoutMustBePositive();
235
- if (timeout >= 365 days) revert TimeoutMustBeLessThan365Days();
236
-
237
- uint256 bounty = tokens;
238
-
239
- // The arbitrator can set a fee for asking a question.
240
- // This is intended as an anti-spam defence.
241
- // The fee is waived if the arbitrator is asking the question.
242
- // This allows them to set an impossibly high fee and make users proxy the question through them.
243
- // This would allow more sophisticated pricing, question whitelisting etc.
244
- if (arbitrator != NULL_ADDRESS && msg.sender != arbitrator) {
245
- uint256 question_fee = arbitrator_question_fees[arbitrator];
246
- if (bounty < question_fee) revert TokensProvidedMustCoverQuestionFee();
247
- bounty = bounty - question_fee;
248
- balanceOf[arbitrator] = balanceOf[arbitrator] + question_fee;
249
- }
250
-
251
- questions[question_id].content_hash = content_hash;
252
- questions[question_id].arbitrator = arbitrator;
253
- questions[question_id].opening_ts = opening_ts;
254
- questions[question_id].timeout = timeout;
255
-
256
- if (bounty > 0) {
257
- questions[question_id].bounty = bounty;
258
- emit LogFundAnswerBounty(question_id, bounty, bounty, msg.sender);
259
- }
260
-
261
- if (min_bond > 0) {
262
- questions[question_id].min_bond = min_bond;
263
- emit LogMinimumBond(question_id, min_bond);
264
- }
265
- }
266
-
267
85
  /// @notice Add funds to the bounty for a question
268
86
  /// @dev Add bounty funds after the initial question creation. Can be done any time until the question is finalized.
269
87
  /// @param question_id The ID of the question you wish to fund
270
- function fundAnswerBounty(bytes32 question_id) external payable stateOpen(question_id) {
88
+ function fundAnswerBounty(bytes32 question_id) external payable stateOpen(question_id) notFrozen {
271
89
  questions[question_id].bounty = questions[question_id].bounty + msg.value;
272
90
  emit LogFundAnswerBounty(question_id, msg.value, questions[question_id].bounty, msg.sender);
273
91
  }
@@ -281,7 +99,7 @@ contract RealityETH_v4_0 is BalanceHolder, IRealityETHCore {
281
99
  bytes32 question_id,
282
100
  bytes32 answer,
283
101
  uint256 max_previous
284
- ) external payable stateOpen(question_id) bondMustDoubleAndMatchMinimum(question_id, msg.value) previousBondMustNotBeatMaxPrevious(question_id, max_previous) {
102
+ ) external payable stateOpen(question_id) bondMustDoubleAndMatchMinimum(question_id, msg.value) previousBondMustNotBeatMaxPrevious(question_id, max_previous) notFrozen {
285
103
  _addAnswerToHistory(question_id, answer, msg.sender, msg.value);
286
104
  _updateCurrentAnswer(question_id, answer);
287
105
  }
@@ -297,436 +115,16 @@ contract RealityETH_v4_0 is BalanceHolder, IRealityETHCore {
297
115
  bytes32 answer,
298
116
  uint256 max_previous,
299
117
  address answerer
300
- ) external payable stateOpen(question_id) bondMustDoubleAndMatchMinimum(question_id, msg.value) previousBondMustNotBeatMaxPrevious(question_id, max_previous) {
301
- if (answerer == NULL_ADDRESS) revert AnswererMustBeNonZero();
118
+ ) external payable stateOpen(question_id) bondMustDoubleAndMatchMinimum(question_id, msg.value) previousBondMustNotBeatMaxPrevious(question_id, max_previous) notFrozen {
119
+ if (answerer == address(0)) revert AnswererMustBeNonZero();
302
120
  _addAnswerToHistory(question_id, answer, answerer, msg.value);
303
121
  _updateCurrentAnswer(question_id, answer);
304
122
  }
305
123
 
306
- function _addAnswerToHistory(bytes32 question_id, bytes32 answer, address answerer, uint256 bond) internal {
307
- bytes32 new_history_hash = keccak256(abi.encodePacked(questions[question_id].history_hash, answer, bond, answerer, false));
308
-
309
- // Update the current bond level, if there's a bond (ie anything except arbitration)
310
- if (bond > 0) {
311
- questions[question_id].bond = bond;
312
- }
313
- questions[question_id].history_hash = new_history_hash;
314
-
315
- emit LogNewAnswer(answer, question_id, new_history_hash, answerer, bond, block.timestamp, false);
316
- }
317
-
318
- function _updateCurrentAnswer(bytes32 question_id, bytes32 answer) internal {
319
- questions[question_id].best_answer = answer;
320
- questions[question_id].finalize_ts = uint32(block.timestamp) + questions[question_id].timeout;
321
- }
322
-
323
- // Like _updateCurrentAnswer but without advancing the timeout
324
- function _updateCurrentAnswerByArbitrator(bytes32 question_id, bytes32 answer) internal {
325
- questions[question_id].best_answer = answer;
326
- questions[question_id].finalize_ts = uint32(block.timestamp);
327
- }
328
-
329
- /// @notice Notify the contract that the arbitrator has been paid for a question, freezing it pending their decision.
330
- /// @dev The arbitrator contract is trusted to only call this if they've been paid, and tell us who paid them.
331
- /// @param question_id The ID of the question
332
- /// @param requester The account that requested arbitration
333
- /// @param max_previous If specified, reverts if a bond higher than this was submitted after you sent your transaction.
334
- function notifyOfArbitrationRequest(
335
- bytes32 question_id,
336
- address requester,
337
- uint256 max_previous
338
- ) external onlyArbitrator(question_id) stateOpen(question_id) previousBondMustNotBeatMaxPrevious(question_id, max_previous) {
339
- if (questions[question_id].finalize_ts <= UNANSWERED) revert QuestionMustAlreadyHaveAnAnswerWhenArbitrationIsRequested();
340
- questions[question_id].is_pending_arbitration = true;
341
- emit LogNotifyOfArbitrationRequest(question_id, requester);
342
- }
343
-
344
- /// @notice Cancel a previously-requested arbitration and extend the timeout
345
- /// @dev Useful when doing arbitration across chains that can't be requested atomically
346
- /// @param question_id The ID of the question
347
- function cancelArbitration(bytes32 question_id) external onlyArbitrator(question_id) statePendingArbitration(question_id) {
348
- questions[question_id].is_pending_arbitration = false;
349
- questions[question_id].finalize_ts = uint32(block.timestamp) + questions[question_id].timeout;
350
- emit LogCancelArbitration(question_id);
351
- }
352
-
353
- /// @notice Submit the answer for a question, for use by the arbitrator.
354
- /// @dev Doesn't require (or allow) a bond.
355
- /// If the current final answer is correct, the account should be whoever submitted it.
356
- /// If the current final answer is wrong, the account should be whoever paid for arbitration.
357
- /// However, the answerer stipulations are not enforced by the contract.
358
- /// @param question_id The ID of the question
359
- /// @param answer The answer, encoded into bytes32
360
- /// @param answerer The account credited with this answer for the purpose of bond claims
361
- function submitAnswerByArbitrator(bytes32 question_id, bytes32 answer, address answerer) public onlyArbitrator(question_id) statePendingArbitration(question_id) {
362
- if (answerer == NULL_ADDRESS) revert AnswererMustBeProvided();
363
- emit LogFinalize(question_id, answer);
364
-
365
- questions[question_id].is_pending_arbitration = false;
366
- _addAnswerToHistory(question_id, answer, answerer, 0);
367
- _updateCurrentAnswerByArbitrator(question_id, answer);
368
- }
369
-
370
- /// @notice Submit the answer for a question, for use by the arbitrator, working out the appropriate winner based on the last answer details.
371
- /// @dev Doesn't require (or allow) a bond.
372
- /// @param question_id The ID of the question
373
- /// @param answer The answer, encoded into bytes32
374
- /// @param payee_if_wrong The account to by credited as winner if the last answer given is wrong, usually the account that paid the arbitrator
375
- /// @param last_history_hash The history hash before the final one
376
- /// @param last_answer The last answer given
377
- /// @param last_answerer The address that supplied the last answer
378
- function assignWinnerAndSubmitAnswerByArbitrator(bytes32 question_id, bytes32 answer, address payee_if_wrong, bytes32 last_history_hash, bytes32 last_answer, address last_answerer) external {
379
- _verifyHistoryInputOrRevert(questions[question_id].history_hash, last_history_hash, last_answer, questions[question_id].bond, last_answerer);
380
-
381
- address payee = (questions[question_id].best_answer == answer) ? last_answerer : payee_if_wrong;
382
- submitAnswerByArbitrator(question_id, answer, payee);
383
- }
384
-
385
- /// @notice Report whether the answer to the specified question is finalized
386
- /// @param question_id The ID of the question
387
- /// @return Return true if finalized
388
- function isFinalized(bytes32 question_id) public view returns (bool) {
389
- uint32 finalize_ts = questions[question_id].finalize_ts;
390
- return (!questions[question_id].is_pending_arbitration && (finalize_ts > UNANSWERED) && (finalize_ts <= uint32(block.timestamp)));
391
- }
392
-
393
- /// @notice (Deprecated) Return the final answer to the specified question, or revert if there isn't one
394
- /// @param question_id The ID of the question
395
- /// @return The answer formatted as a bytes32
396
- function getFinalAnswer(bytes32 question_id) external view stateFinalized(question_id) returns (bytes32) {
397
- return questions[question_id].best_answer;
398
- }
399
-
400
- /// @notice Return the final answer to the specified question, or revert if there isn't one
401
- /// @param question_id The ID of the question
402
- /// @return The answer formatted as a bytes32
403
- function resultFor(bytes32 question_id) public view stateFinalized(question_id) returns (bytes32) {
404
- return questions[question_id].best_answer;
405
- }
406
-
407
- /// @notice Returns whether the question was answered before it had an answer, ie resolved to UNRESOLVED_ANSWER
408
- /// @param question_id The ID of the question
409
- function isSettledTooSoon(bytes32 question_id) public view returns (bool) {
410
- return (resultFor(question_id) == UNRESOLVED_ANSWER);
411
- }
412
-
413
- /// @notice Like resultFor(), but errors out if settled too soon, or returns the result of a replacement if it was reopened at the right time and settled
414
- /// @param question_id The ID of the question
415
- function resultForOnceSettled(bytes32 question_id) external view returns (bytes32) {
416
- bytes32 result = resultFor(question_id);
417
- if (result == UNRESOLVED_ANSWER) {
418
- // Try the replacement
419
- bytes32 replacement_id = reopened_questions[question_id];
420
- if (replacement_id == bytes32(0x0)) revert QuestionWasSettledTooSoonAndHasNotBeenReopened();
421
- // We only try one layer down rather than recursing to keep the gas costs predictable
422
- result = resultFor(replacement_id);
423
- if (result == UNRESOLVED_ANSWER) revert QuestionReplacementWasSettledTooSoonAndHasNotBeenReopened();
424
- }
425
- return result;
426
- }
427
-
428
- /// @notice Asks a new question reopening a previously-asked question that was settled too soon
429
- /// @dev A special version of askQuestion() that replaces a previous question that was settled too soon
430
- /// @param template_id The ID number of the template the question will use
431
- /// @param question A string containing the parameters that will be passed into the template to make the question
432
- /// @param arbitrator The arbitration contract that will have the final word on the answer if there is a dispute
433
- /// @param timeout How long the contract should wait after the answer is changed before finalizing on that answer
434
- /// @param opening_ts If set, the earliest time it should be possible to answer the question.
435
- /// @param nonce A user-specified nonce used in the question ID. Change it to repeat a question.
436
- /// @param min_bond The minimum bond that can be used to provide the first answer.
437
- /// @param reopens_question_id The ID of the question this reopens
438
- /// @return The ID of the newly-created question, created deterministically.
439
- function reopenQuestion(
440
- uint256 template_id,
441
- string memory question,
442
- address arbitrator,
443
- uint32 timeout,
444
- uint32 opening_ts,
445
- uint256 nonce,
446
- uint256 min_bond,
447
- bytes32 reopens_question_id
448
- )
449
- public
450
- payable
451
- returns (
452
- // stateNotCreated is enforced by the internal _askQuestion
453
- bytes32
454
- )
455
- {
456
- if (!isSettledTooSoon(reopens_question_id)) revert YouCanOnlyReopenQuestionsThatResolvedAsSettledTooSoon();
457
-
458
- bytes32 content_hash = keccak256(abi.encodePacked(template_id, opening_ts, question));
459
-
460
- // A reopening must exactly match the original question, except for the nonce and the creator
461
- if (content_hash != questions[reopens_question_id].content_hash) revert ContentHashMismatch();
462
- if (arbitrator != questions[reopens_question_id].arbitrator) revert ArbitratorMismatch();
463
- if (timeout != questions[reopens_question_id].timeout) revert TimeoutMismatch();
464
- if (opening_ts != questions[reopens_question_id].opening_ts) revert Opening_TsMismatch();
465
- if (min_bond != questions[reopens_question_id].min_bond) revert Min_BondMismatch();
466
-
467
- // If the the question was itself reopening some previous question, you'll have to re-reopen the previous question first.
468
- // This ensures the bounty can be passed on to the next attempt of the original question.
469
- if (reopener_questions[reopens_question_id]) revert QuestionIsAlreadyReopeningAPreviousQuestion();
470
-
471
- // A question can only be reopened once, unless the reopening was also settled too soon in which case it can be replaced
472
- bytes32 existing_reopen_question_id = reopened_questions[reopens_question_id];
473
-
474
- // Normally when we reopen a question we will take its bounty and pass it on to the reopened version.
475
- bytes32 take_bounty_from_question_id = reopens_question_id;
476
- // If the question has already been reopened but was again settled too soon, we can transfer its bounty to the next attempt.
477
- if (existing_reopen_question_id != bytes32(0)) {
478
- if (!isSettledTooSoon(existing_reopen_question_id)) revert QuestionHasAlreadyBeenReopened();
479
- // We'll overwrite the reopening with our new question and move the bounty.
480
- // Once that's done we'll detach the failed reopener and you'll be able to reopen that too if you really want, but without the bounty.
481
- reopener_questions[existing_reopen_question_id] = false;
482
- take_bounty_from_question_id = existing_reopen_question_id;
483
- }
484
-
485
- bytes32 question_id = askQuestionWithMinBond(template_id, question, arbitrator, timeout, opening_ts, nonce, min_bond);
486
-
487
- reopened_questions[reopens_question_id] = question_id;
488
- reopener_questions[question_id] = true;
489
-
490
- questions[question_id].bounty = questions[take_bounty_from_question_id].bounty + questions[question_id].bounty;
491
- questions[take_bounty_from_question_id].bounty = 0;
492
-
493
- emit LogReopenQuestion(question_id, reopens_question_id);
494
-
495
- return question_id;
496
- }
497
-
498
- /// @notice Return the final answer to the specified question, provided it matches the specified criteria.
499
- /// @dev Reverts if the question is not finalized, or if it does not match the specified criteria.
500
- /// @param question_id The ID of the question
501
- /// @param content_hash The hash of the question content (template ID + opening time + question parameter string)
502
- /// @param arbitrator The arbitrator chosen for the question (regardless of whether they are asked to arbitrate)
503
- /// @param min_timeout The timeout set in the initial question settings must be this high or higher
504
- /// @param min_bond The bond sent with the final answer must be this high or higher
505
- /// @return The answer formatted as a bytes32
506
- function getFinalAnswerIfMatches(bytes32 question_id, bytes32 content_hash, address arbitrator, uint32 min_timeout, uint256 min_bond) external view stateFinalized(question_id) returns (bytes32) {
507
- if (content_hash != questions[question_id].content_hash) revert ContentHashMustMatch();
508
- if (arbitrator != questions[question_id].arbitrator) revert ArbitratorMustMatch();
509
- if (min_timeout > questions[question_id].timeout) revert TimeoutMustBeLongEnough();
510
- if (min_bond > questions[question_id].bond) revert BondMustBeHighEnough();
511
- return questions[question_id].best_answer;
512
- }
513
-
514
- /// @notice Assigns the winnings (bounty and bonds) to everyone who gave the accepted answer
515
- /// Caller must provide the answer history, in reverse order
516
- /// @dev Works up the chain and assign bonds to the person who gave the right answer
517
- /// If someone gave the winning answer earlier, they must get paid from the higher bond
518
- /// That means we can't pay out the bond added at n until we have looked at n-1
519
- /// The first answer is authenticated by checking against the stored history_hash.
520
- /// One of the inputs to history_hash is the history_hash before it, so we use that to authenticate the next entry, etc
521
- /// Once we get to a null hash we'll know we're done and there are no more answers.
522
- /// Usually you would call the whole thing in a single transaction, but if not then the data is persisted to pick up later.
523
- /// @param question_id The ID of the question
524
- /// @param history_hashes Second-last-to-first, the hash of each history entry. (Final one should be empty).
525
- /// @param addrs Last-to-first, the address of each answerer
526
- /// @param bonds Last-to-first, the bond supplied with each answer
527
- /// @param answers Last-to-first, each answer supplied
528
- function claimWinnings(bytes32 question_id, bytes32[] memory history_hashes, address[] memory addrs, uint256[] memory bonds, bytes32[] memory answers) public stateFinalized(question_id) {
529
- if (history_hashes.length == 0) revert AtLeastOneHistoryHashEntryMustBeProvided();
530
-
531
- // These are only set if we split our claim over multiple transactions.
532
- address payee = question_claims[question_id].payee;
533
- uint256 last_bond = question_claims[question_id].last_bond;
534
- uint256 queued_funds = 0;
535
-
536
- // Starts as the hash of the final answer submitted. It'll be cleared when we're done.
537
- // If we're splitting the claim over multiple transactions, it'll be the hash where we left off last time
538
- bytes32 last_history_hash = questions[question_id].history_hash;
539
-
540
- bytes32 best_answer = questions[question_id].best_answer;
541
-
542
- uint256 i;
543
- for (i = 0; i < history_hashes.length; i++) {
544
- _verifyHistoryInputOrRevert(last_history_hash, history_hashes[i], answers[i], bonds[i], addrs[i]);
545
-
546
- queued_funds = queued_funds + last_bond;
547
- (queued_funds, payee) = _processHistoryItem(question_id, best_answer, queued_funds, payee, addrs[i], bonds[i], answers[i]);
548
-
549
- // Line the bond up for next time, when it will be added to somebody's queued_funds
550
- last_bond = bonds[i];
551
-
552
- // Burn (just leave in contract balance) a fraction of all bonds except the final one.
553
- // This creates a cost to increasing your own bond, which could be used to delay resolution maliciously
554
- if (last_bond != questions[question_id].bond) {
555
- last_bond = last_bond - last_bond / BOND_CLAIM_FEE_PROPORTION;
556
- }
557
-
558
- last_history_hash = history_hashes[i];
559
- }
560
-
561
- if (last_history_hash != NULL_HASH) {
562
- // We haven't yet got to the null hash (1st answer), ie the caller didn't supply the full answer chain.
563
- // Persist the details so we can pick up later where we left off later.
564
-
565
- // Pay out the latest payee, only keeping back last_bond which the next may have a claim on
566
- _payPayee(question_id, payee, queued_funds);
567
-
568
- question_claims[question_id].payee = payee;
569
- question_claims[question_id].last_bond = last_bond;
570
- } else {
571
- // There is nothing left below us so the payee can keep what remains
572
- _payPayee(question_id, payee, queued_funds + last_bond);
573
- delete question_claims[question_id];
574
- }
575
-
576
- questions[question_id].history_hash = last_history_hash;
577
- }
578
-
579
- function _payPayee(bytes32 question_id, address payee, uint256 value) internal {
580
- balanceOf[payee] = balanceOf[payee] + value;
581
- emit LogClaim(question_id, payee, value);
582
- }
583
-
584
- function _verifyHistoryInputOrRevert(bytes32 last_history_hash, bytes32 history_hash, bytes32 answer, uint256 bond, address addr) internal pure {
585
- if (last_history_hash != keccak256(abi.encodePacked(history_hash, answer, bond, addr, false))) {
586
- revert HistoryInputProvidedDidNotMatchTheExpectedHash();
587
- }
588
- }
589
-
590
- function _processHistoryItem(bytes32 question_id, bytes32 best_answer, uint256 queued_funds, address payee, address addr, uint256 bond, bytes32 answer) internal returns (uint256, address) {
591
- if (answer == best_answer) {
592
- if (payee == NULL_ADDRESS) {
593
- // The entry is for the first payee we come to, ie the winner.
594
- // They get the question bounty.
595
- payee = addr;
596
-
597
- if (best_answer != UNRESOLVED_ANSWER && questions[question_id].bounty > 0) {
598
- _payPayee(question_id, payee, questions[question_id].bounty);
599
- questions[question_id].bounty = 0;
600
- }
601
- } else if (addr != payee) {
602
- // Answerer has changed, ie we found someone lower down who needs to be paid
603
-
604
- // The lower answerer will take over receiving bonds from higher answerer.
605
- // They should also be paid the takeover fee, which is set at a rate equivalent to their bond.
606
- // (This is our arbitrary rule, to give consistent right-answerers a defence against high-rollers.)
607
-
608
- // There should be enough for the fee, but if not, take what we have.
609
- // There's an edge case involving weird arbitrator behaviour where we may be short.
610
- uint256 answer_takeover_fee = (queued_funds >= bond) ? bond : queued_funds;
611
- // Settle up with the old (higher-bonded) payee
612
- _payPayee(question_id, payee, queued_funds - answer_takeover_fee);
613
-
614
- // Now start queued_funds again for the new (lower-bonded) payee
615
- payee = addr;
616
- queued_funds = answer_takeover_fee;
617
- }
618
- }
619
-
620
- return (queued_funds, payee);
621
- }
622
-
623
- /// @notice Convenience function to assign bounties/bonds for multiple questions in one go, then withdraw all your funds.
624
- /// Caller must provide the answer history for each question, in reverse order
625
- /// @dev Can be called by anyone to assign bonds/bounties, but funds are only withdrawn for the user making the call.
626
- /// @param question_ids The IDs of the questions you want to claim for
627
- /// @param lengths The number of history entries you will supply for each question ID
628
- /// @param hist_hashes In a single list for all supplied questions, the hash of each history entry.
629
- /// @param addrs In a single list for all supplied questions, the address of each answerer
630
- /// @param bonds In a single list for all supplied questions, the bond supplied with each answer
631
- /// @param answers In a single list for all supplied questions, each answer supplied
632
- function claimMultipleAndWithdrawBalance(
633
- bytes32[] memory question_ids,
634
- uint256[] memory lengths,
635
- bytes32[] memory hist_hashes,
636
- address[] memory addrs,
637
- uint256[] memory bonds,
638
- bytes32[] memory answers
639
- )
640
- public
641
- stateAny // The finalization checks are done in the claimWinnings function
642
- {
643
- uint256 qi;
644
- uint256 i;
645
- for (qi = 0; qi < question_ids.length; qi++) {
646
- bytes32 qid = question_ids[qi];
647
- uint256 ln = lengths[qi];
648
- bytes32[] memory hh = new bytes32[](ln);
649
- address[] memory ad = new address[](ln);
650
- uint256[] memory bo = new uint256[](ln);
651
- bytes32[] memory an = new bytes32[](ln);
652
- uint256 j;
653
- for (j = 0; j < ln; j++) {
654
- hh[j] = hist_hashes[i];
655
- ad[j] = addrs[i];
656
- bo[j] = bonds[i];
657
- an[j] = answers[i];
658
- i++;
659
- }
660
- claimWinnings(qid, hh, ad, bo, an);
661
- }
662
- withdraw();
663
- }
664
-
665
- /// @notice Returns the questions's content hash, identifying the question content
666
- /// @param question_id The ID of the question
667
- function getContentHash(bytes32 question_id) public view returns (bytes32) {
668
- return questions[question_id].content_hash;
669
- }
670
-
671
- /// @notice Returns the arbitrator address for the question
672
- /// @param question_id The ID of the question
673
- function getArbitrator(bytes32 question_id) public view returns (address) {
674
- return questions[question_id].arbitrator;
675
- }
676
-
677
- /// @notice Returns the timestamp when the question can first be answered
678
- /// @param question_id The ID of the question
679
- function getOpeningTS(bytes32 question_id) public view returns (uint32) {
680
- return questions[question_id].opening_ts;
681
- }
682
-
683
- /// @notice Returns the timeout in seconds used after each answer
684
- /// @param question_id The ID of the question
685
- function getTimeout(bytes32 question_id) public view returns (uint32) {
686
- return questions[question_id].timeout;
687
- }
688
-
689
- /// @notice Returns the timestamp at which the question will be/was finalized
690
- /// @param question_id The ID of the question
691
- function getFinalizeTS(bytes32 question_id) public view returns (uint32) {
692
- return questions[question_id].finalize_ts;
693
- }
694
-
695
- /// @notice Returns whether the question is pending arbitration
696
- /// @param question_id The ID of the question
697
- function isPendingArbitration(bytes32 question_id) public view returns (bool) {
698
- return questions[question_id].is_pending_arbitration;
699
- }
700
-
701
- /// @notice Returns the current total unclaimed bounty
702
- /// @dev Set back to zero once the bounty has been claimed
703
- /// @param question_id The ID of the question
704
- function getBounty(bytes32 question_id) public view returns (uint256) {
705
- return questions[question_id].bounty;
706
- }
707
-
708
- /// @notice Returns the current best answer
709
- /// @param question_id The ID of the question
710
- function getBestAnswer(bytes32 question_id) public view returns (bytes32) {
711
- return questions[question_id].best_answer;
712
- }
713
-
714
- /// @notice Returns the history hash of the question
715
- /// @param question_id The ID of the question
716
- /// @dev Updated on each answer, then rewound as each is claimed
717
- function getHistoryHash(bytes32 question_id) public view returns (bytes32) {
718
- return questions[question_id].history_hash;
719
- }
720
-
721
- /// @notice Returns the highest bond posted so far for a question
722
- /// @param question_id The ID of the question
723
- function getBond(bytes32 question_id) public view returns (uint256) {
724
- return questions[question_id].bond;
725
- }
726
-
727
- /// @notice Returns the minimum bond that can answer the question
728
- /// @param question_id The ID of the question
729
- function getMinBond(bytes32 question_id) public view returns (uint256) {
730
- return questions[question_id].min_bond;
124
+ function withdraw() public override notFrozen {
125
+ uint256 bal = balanceOf[msg.sender];
126
+ balanceOf[msg.sender] = 0;
127
+ payable(msg.sender).transfer(bal);
128
+ emit LogWithdraw(msg.sender, bal);
731
129
  }
732
130
  }