@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,825 +0,0 @@
1
- // SPDX-License-Identifier: GPL-3.0-only
2
-
3
- pragma solidity ^0.8.6;
4
-
5
-
6
- import './RealitioSafeMath256.sol';
7
- import './RealitioSafeMath32.sol';
8
- import './BalanceHolderERC20.sol';
9
-
10
- contract RealitioERC20 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, uint256 tokens) {
175
- require(tokens > 0, "bond must be positive");
176
- require(tokens >= (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
- function setToken(IERC20 _token)
188
- public
189
- {
190
- require(token == IERC20(address(0x0)), "Token can only be initialized once");
191
- token = _token;
192
- }
193
-
194
- /// @notice Constructor, sets up some initial templates
195
- /// @dev Creates some generalized templates for different question types used in the DApp.
196
- constructor() {
197
- createTemplate('{"title": "%s", "type": "bool", "category": "%s", "lang": "%s"}');
198
- createTemplate('{"title": "%s", "type": "uint", "decimals": 18, "category": "%s", "lang": "%s"}');
199
- createTemplate('{"title": "%s", "type": "single-select", "outcomes": [%s], "category": "%s", "lang": "%s"}');
200
- createTemplate('{"title": "%s", "type": "multiple-select", "outcomes": [%s], "category": "%s", "lang": "%s"}');
201
- createTemplate('{"title": "%s", "type": "datetime", "category": "%s", "lang": "%s"}');
202
- }
203
-
204
- /// @notice Function for arbitrator to set an optional per-question fee.
205
- /// @dev The per-question fee, charged when a question is asked, is intended as an anti-spam measure.
206
- /// @param fee The fee to be charged by the arbitrator when a question is asked
207
- function setQuestionFee(uint256 fee)
208
- stateAny()
209
- external {
210
- arbitrator_question_fees[msg.sender] = fee;
211
- emit LogSetQuestionFee(msg.sender, fee);
212
- }
213
-
214
- /// @notice Create a reusable template, which should be a JSON document.
215
- /// Placeholders should use gettext() syntax, eg %s.
216
- /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
217
- /// @param content The template content
218
- /// @return The ID of the newly-created template, which is created sequentially.
219
- function createTemplate(string memory content)
220
- stateAny()
221
- public returns (uint256) {
222
- uint256 id = nextTemplateID;
223
- templates[id] = block.number;
224
- template_hashes[id] = keccak256(abi.encodePacked(content));
225
- emit LogNewTemplate(id, msg.sender, content);
226
- nextTemplateID = id.add(1);
227
- return id;
228
- }
229
-
230
- /// @notice Create a new reusable template and use it to ask a question
231
- /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
232
- /// @param content The template content
233
- /// @param question A string containing the parameters that will be passed into the template to make the question
234
- /// @param arbitrator The arbitration contract that will have the final word on the answer if there is a dispute
235
- /// @param timeout How long the contract should wait after the answer is changed before finalizing on that answer
236
- /// @param opening_ts If set, the earliest time it should be possible to answer the question.
237
- /// @param nonce A user-specified nonce used in the question ID. Change it to repeat a question.
238
- /// @return The ID of the newly-created template, which is created sequentially.
239
- function createTemplateAndAskQuestion(
240
- string memory content,
241
- string memory question, address arbitrator, uint32 timeout, uint32 opening_ts, uint256 nonce
242
- )
243
- // stateNotCreated is enforced by the internal _askQuestion
244
- public returns (bytes32) {
245
- uint256 template_id = createTemplate(content);
246
- return askQuestion(template_id, question, arbitrator, timeout, opening_ts, nonce);
247
- }
248
-
249
- /// @notice Ask a new question without a bounty and return the ID
250
- /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
251
- /// @dev Calling without the token param will only work if there is no arbitrator-set question fee.
252
- /// @dev This has the same function signature as askQuestion() in the non-ERC20 version, which is optionally payable.
253
- /// @param template_id The ID number of the template the question will use
254
- /// @param question A string containing the parameters that will be passed into the template to make the question
255
- /// @param arbitrator The arbitration contract that will have the final word on the answer if there is a dispute
256
- /// @param timeout How long the contract should wait after the answer is changed before finalizing on that answer
257
- /// @param opening_ts If set, the earliest time it should be possible to answer the question.
258
- /// @param nonce A user-specified nonce used in the question ID. Change it to repeat a question.
259
- /// @return The ID of the newly-created question, created deterministically.
260
- function askQuestion(uint256 template_id, string memory question, address arbitrator, uint32 timeout, uint32 opening_ts, uint256 nonce)
261
- // stateNotCreated is enforced by the internal _askQuestion
262
- public returns (bytes32) {
263
-
264
- require(templates[template_id] > 0, "template must exist");
265
-
266
- bytes32 content_hash = keccak256(abi.encodePacked(template_id, opening_ts, question));
267
- bytes32 question_id = keccak256(abi.encodePacked(content_hash, arbitrator, timeout, msg.sender, nonce));
268
-
269
- _askQuestion(question_id, content_hash, arbitrator, timeout, opening_ts, 0);
270
- emit LogNewQuestion(question_id, msg.sender, template_id, question, content_hash, arbitrator, timeout, opening_ts, nonce, block.timestamp);
271
-
272
- return question_id;
273
- }
274
-
275
- /// @notice Ask a new question with a bounty and return the ID
276
- /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
277
- /// @param template_id The ID number of the template the question will use
278
- /// @param question A string containing the parameters that will be passed into the template to make the question
279
- /// @param arbitrator The arbitration contract that will have the final word on the answer if there is a dispute
280
- /// @param timeout How long the contract should wait after the answer is changed before finalizing on that answer
281
- /// @param opening_ts If set, the earliest time it should be possible to answer the question.
282
- /// @param nonce A user-specified nonce used in the question ID. Change it to repeat a question.
283
- /// @param tokens The combined initial question bounty and question fee
284
- /// @return The ID of the newly-created question, created deterministically.
285
- function askQuestionERC20(uint256 template_id, string memory question, address arbitrator, uint32 timeout, uint32 opening_ts, uint256 nonce, uint256 tokens)
286
- // stateNotCreated is enforced by the internal _askQuestion
287
- public returns (bytes32) {
288
-
289
- _deductTokensOrRevert(tokens);
290
-
291
- require(templates[template_id] > 0, "template must exist");
292
-
293
- bytes32 content_hash = keccak256(abi.encodePacked(template_id, opening_ts, question));
294
- bytes32 question_id = keccak256(abi.encodePacked(content_hash, arbitrator, timeout, msg.sender, nonce));
295
-
296
- _askQuestion(question_id, content_hash, arbitrator, timeout, opening_ts, tokens);
297
- emit LogNewQuestion(question_id, msg.sender, template_id, question, content_hash, arbitrator, timeout, opening_ts, nonce, block.timestamp);
298
-
299
- return question_id;
300
- }
301
-
302
- function _deductTokensOrRevert(uint256 tokens)
303
- internal {
304
-
305
- if (tokens == 0) {
306
- return;
307
- }
308
-
309
- uint256 bal = balanceOf[msg.sender];
310
-
311
- // Deduct any tokens you have in your internal balance first
312
- if (bal > 0) {
313
- if (bal >= tokens) {
314
- balanceOf[msg.sender] = bal.sub(tokens);
315
- return;
316
- } else {
317
- tokens = tokens.sub(bal);
318
- balanceOf[msg.sender] = 0;
319
- }
320
- }
321
- // Now we need to charge the rest from
322
- require(token.transferFrom(msg.sender, address(this), tokens), "Transfer of tokens failed, insufficient approved balance?");
323
- return;
324
-
325
- }
326
-
327
- function _askQuestion(bytes32 question_id, bytes32 content_hash, address arbitrator, uint32 timeout, uint32 opening_ts, uint256 tokens)
328
- stateNotCreated(question_id)
329
- internal {
330
-
331
- uint256 bounty = tokens;
332
-
333
- // A timeout of 0 makes no sense, and we will use this to check existence
334
- require(timeout > 0, "timeout must be positive");
335
- require(timeout < 365 days, "timeout must be less than 365 days");
336
- require(arbitrator != NULL_ADDRESS, "arbitrator must be set");
337
-
338
- // The arbitrator can set a fee for asking a question.
339
- // This is intended as an anti-spam defence.
340
- // The fee is waived if the arbitrator is asking the question.
341
- // This allows them to set an impossibly high fee and make users proxy the question through them.
342
- // This would allow more sophisticated pricing, question whitelisting etc.
343
- if (msg.sender != arbitrator) {
344
- uint256 question_fee = arbitrator_question_fees[arbitrator];
345
- require(bounty >= question_fee, "Tokens provided must cover question fee");
346
- bounty = bounty.sub(question_fee);
347
- balanceOf[arbitrator] = balanceOf[arbitrator].add(question_fee);
348
- }
349
-
350
- questions[question_id].content_hash = content_hash;
351
- questions[question_id].arbitrator = arbitrator;
352
- questions[question_id].opening_ts = opening_ts;
353
- questions[question_id].timeout = timeout;
354
- questions[question_id].bounty = bounty;
355
-
356
- }
357
-
358
- /// @notice Add funds to the bounty for a question
359
- /// @dev Add bounty funds after the initial question creation. Can be done any time until the question is finalized.
360
- /// @param question_id The ID of the question you wish to fund
361
- /// @param tokens The number of tokens to fund
362
- function fundAnswerBountyERC20(bytes32 question_id, uint256 tokens)
363
- stateOpen(question_id)
364
- external {
365
- _deductTokensOrRevert(tokens);
366
- questions[question_id].bounty = questions[question_id].bounty.add(tokens);
367
- emit LogFundAnswerBounty(question_id, tokens, questions[question_id].bounty, msg.sender);
368
- }
369
-
370
- /// @notice Submit an answer for a question.
371
- /// @dev Adds the answer to the history and updates the current "best" answer.
372
- /// May be subject to front-running attacks; Substitute submitAnswerCommitment()->submitAnswerReveal() to prevent them.
373
- /// @param question_id The ID of the question
374
- /// @param answer The answer, encoded into bytes32
375
- /// @param max_previous If specified, reverts if a bond higher than this was submitted after you sent your transaction.
376
- /// @param tokens The amount of tokens to submit
377
- function submitAnswerERC20(bytes32 question_id, bytes32 answer, uint256 max_previous, uint256 tokens)
378
- stateOpen(question_id)
379
- bondMustDouble(question_id, tokens)
380
- previousBondMustNotBeatMaxPrevious(question_id, max_previous)
381
- external {
382
- _deductTokensOrRevert(tokens);
383
- _addAnswerToHistory(question_id, answer, msg.sender, tokens, false);
384
- _updateCurrentAnswer(question_id, answer, questions[question_id].timeout);
385
- }
386
-
387
- // @notice Verify and store a commitment, including an appropriate timeout
388
- // @param question_id The ID of the question to store
389
- // @param commitment The ID of the commitment
390
- function _storeCommitment(bytes32 question_id, bytes32 commitment_id)
391
- internal
392
- {
393
- require(commitments[commitment_id].reveal_ts == COMMITMENT_NON_EXISTENT, "commitment must not already exist");
394
-
395
- uint32 commitment_timeout = questions[question_id].timeout / COMMITMENT_TIMEOUT_RATIO;
396
- commitments[commitment_id].reveal_ts = uint32(block.timestamp).add(commitment_timeout);
397
- }
398
-
399
- /// @notice Submit the hash of an answer, laying your claim to that answer if you reveal it in a subsequent transaction.
400
- /// @dev Creates a hash, commitment_id, uniquely identifying this answer, to this question, with this bond.
401
- /// The commitment_id is stored in the answer history where the answer would normally go.
402
- /// Does not update the current best answer - this is left to the later submitAnswerReveal() transaction.
403
- /// @param question_id The ID of the question
404
- /// @param answer_hash The hash of your answer, plus a nonce that you will later reveal
405
- /// @param max_previous If specified, reverts if a bond higher than this was submitted after you sent your transaction.
406
- /// @param _answerer If specified, the address to be given as the question answerer. Defaults to the sender.
407
- /// @param tokens Number of tokens sent
408
- /// @dev Specifying the answerer is useful if you want to delegate the commit-and-reveal to a third-party.
409
- function submitAnswerCommitmentERC20(bytes32 question_id, bytes32 answer_hash, uint256 max_previous, address _answerer, uint256 tokens)
410
- stateOpen(question_id)
411
- bondMustDouble(question_id, tokens)
412
- previousBondMustNotBeatMaxPrevious(question_id, max_previous)
413
- external {
414
-
415
- _deductTokensOrRevert(tokens);
416
-
417
- bytes32 commitment_id = keccak256(abi.encodePacked(question_id, answer_hash, tokens));
418
- address answerer = (_answerer == NULL_ADDRESS) ? msg.sender : _answerer;
419
-
420
- _storeCommitment(question_id, commitment_id);
421
- _addAnswerToHistory(question_id, commitment_id, answerer, tokens, true);
422
-
423
- }
424
-
425
- /// @notice Submit the answer whose hash you sent in a previous submitAnswerCommitment() transaction
426
- /// @dev Checks the parameters supplied recreate an existing commitment, and stores the revealed answer
427
- /// Updates the current answer unless someone has since supplied a new answer with a higher bond
428
- /// msg.sender is intentionally not restricted to the user who originally sent the commitment;
429
- /// For example, the user may want to provide the answer+nonce to a third-party service and let them send the tx
430
- /// NB If we are pending arbitration, it will be up to the arbitrator to wait and see any outstanding reveal is sent
431
- /// @param question_id The ID of the question
432
- /// @param answer The answer, encoded as bytes32
433
- /// @param nonce The nonce that, combined with the answer, recreates the answer_hash you gave in submitAnswerCommitment()
434
- /// @param bond The bond that you paid in your submitAnswerCommitment() transaction
435
- function submitAnswerReveal(bytes32 question_id, bytes32 answer, uint256 nonce, uint256 bond)
436
- stateOpenOrPendingArbitration(question_id)
437
- external {
438
-
439
- bytes32 answer_hash = keccak256(abi.encodePacked(answer, nonce));
440
- bytes32 commitment_id = keccak256(abi.encodePacked(question_id, answer_hash, bond));
441
-
442
- require(!commitments[commitment_id].is_revealed, "commitment must not have been revealed yet");
443
- require(commitments[commitment_id].reveal_ts > uint32(block.timestamp), "reveal deadline must not have passed");
444
-
445
- commitments[commitment_id].revealed_answer = answer;
446
- commitments[commitment_id].is_revealed = true;
447
-
448
- if (bond == questions[question_id].bond) {
449
- _updateCurrentAnswer(question_id, answer, questions[question_id].timeout);
450
- }
451
-
452
- emit LogAnswerReveal(question_id, msg.sender, answer_hash, answer, nonce, bond);
453
-
454
- }
455
-
456
- function _addAnswerToHistory(bytes32 question_id, bytes32 answer_or_commitment_id, address answerer, uint256 bond, bool is_commitment)
457
- internal
458
- {
459
- bytes32 new_history_hash = keccak256(abi.encodePacked(questions[question_id].history_hash, answer_or_commitment_id, bond, answerer, is_commitment));
460
-
461
- // Update the current bond level, if there's a bond (ie anything except arbitration)
462
- if (bond > 0) {
463
- questions[question_id].bond = bond;
464
- }
465
- questions[question_id].history_hash = new_history_hash;
466
-
467
- emit LogNewAnswer(answer_or_commitment_id, question_id, new_history_hash, answerer, bond, block.timestamp, is_commitment);
468
- }
469
-
470
- function _updateCurrentAnswer(bytes32 question_id, bytes32 answer, uint32 timeout_secs)
471
- internal {
472
- questions[question_id].best_answer = answer;
473
- questions[question_id].finalize_ts = uint32(block.timestamp).add(timeout_secs);
474
- }
475
-
476
- /// @notice Notify the contract that the arbitrator has been paid for a question, freezing it pending their decision.
477
- /// @dev The arbitrator contract is trusted to only call this if they've been paid, and tell us who paid them.
478
- /// @param question_id The ID of the question
479
- /// @param requester The account that requested arbitration
480
- /// @param max_previous If specified, reverts if a bond higher than this was submitted after you sent your transaction.
481
- function notifyOfArbitrationRequest(bytes32 question_id, address requester, uint256 max_previous)
482
- onlyArbitrator(question_id)
483
- stateOpen(question_id)
484
- previousBondMustNotBeatMaxPrevious(question_id, max_previous)
485
- external {
486
- require(questions[question_id].bond > 0, "Question must already have an answer when arbitration is requested");
487
- questions[question_id].is_pending_arbitration = true;
488
- emit LogNotifyOfArbitrationRequest(question_id, requester);
489
- }
490
-
491
- /// @notice Submit the answer for a question, for use by the arbitrator.
492
- /// @dev Doesn't require (or allow) a bond.
493
- /// If the current final answer is correct, the account should be whoever submitted it.
494
- /// If the current final answer is wrong, the account should be whoever paid for arbitration.
495
- /// However, the answerer stipulations are not enforced by the contract.
496
- /// @param question_id The ID of the question
497
- /// @param answer The answer, encoded into bytes32
498
- /// @param answerer The account credited with this answer for the purpose of bond claims
499
- function submitAnswerByArbitrator(bytes32 question_id, bytes32 answer, address answerer)
500
- onlyArbitrator(question_id)
501
- statePendingArbitration(question_id)
502
- external {
503
-
504
- require(answerer != NULL_ADDRESS, "answerer must be provided");
505
- emit LogFinalize(question_id, answer);
506
-
507
- questions[question_id].is_pending_arbitration = false;
508
- _addAnswerToHistory(question_id, answer, answerer, 0, false);
509
- _updateCurrentAnswer(question_id, answer, 0);
510
-
511
- }
512
-
513
- /// @notice Report whether the answer to the specified question is finalized
514
- /// @param question_id The ID of the question
515
- /// @return Return true if finalized
516
- function isFinalized(bytes32 question_id)
517
- view public returns (bool) {
518
- uint32 finalize_ts = questions[question_id].finalize_ts;
519
- return ( !questions[question_id].is_pending_arbitration && (finalize_ts > UNANSWERED) && (finalize_ts <= uint32(block.timestamp)) );
520
- }
521
-
522
- /// @notice (Deprecated) Return the final answer to the specified question, or revert if there isn't one
523
- /// @param question_id The ID of the question
524
- /// @return The answer formatted as a bytes32
525
- function getFinalAnswer(bytes32 question_id)
526
- stateFinalized(question_id)
527
- external view returns (bytes32) {
528
- return questions[question_id].best_answer;
529
- }
530
-
531
- /// @notice Return the final answer to the specified question, or revert if there isn't one
532
- /// @param question_id The ID of the question
533
- /// @return The answer formatted as a bytes32
534
- function resultFor(bytes32 question_id)
535
- stateFinalized(question_id)
536
- external view returns (bytes32) {
537
- return questions[question_id].best_answer;
538
- }
539
-
540
-
541
- /// @notice Return the final answer to the specified question, provided it matches the specified criteria.
542
- /// @dev Reverts if the question is not finalized, or if it does not match the specified criteria.
543
- /// @param question_id The ID of the question
544
- /// @param content_hash The hash of the question content (template ID + opening time + question parameter string)
545
- /// @param arbitrator The arbitrator chosen for the question (regardless of whether they are asked to arbitrate)
546
- /// @param min_timeout The timeout set in the initial question settings must be this high or higher
547
- /// @param min_bond The bond sent with the final answer must be this high or higher
548
- /// @return The answer formatted as a bytes32
549
- function getFinalAnswerIfMatches(
550
- bytes32 question_id,
551
- bytes32 content_hash, address arbitrator, uint32 min_timeout, uint256 min_bond
552
- )
553
- stateFinalized(question_id)
554
- external view returns (bytes32) {
555
- require(content_hash == questions[question_id].content_hash, "content hash must match");
556
- require(arbitrator == questions[question_id].arbitrator, "arbitrator must match");
557
- require(min_timeout <= questions[question_id].timeout, "timeout must be long enough");
558
- require(min_bond <= questions[question_id].bond, "bond must be high enough");
559
- return questions[question_id].best_answer;
560
- }
561
-
562
- /// @notice Assigns the winnings (bounty and bonds) to everyone who gave the accepted answer
563
- /// Caller must provide the answer history, in reverse order
564
- /// @dev Works up the chain and assign bonds to the person who gave the right answer
565
- /// If someone gave the winning answer earlier, they must get paid from the higher bond
566
- /// That means we can't pay out the bond added at n until we have looked at n-1
567
- /// The first answer is authenticated by checking against the stored history_hash.
568
- /// One of the inputs to history_hash is the history_hash before it, so we use that to authenticate the next entry, etc
569
- /// Once we get to a null hash we'll kblock.timestamp we're done and there are no more answers.
570
- /// Usually you would call the whole thing in a single transaction, but if not then the data is persisted to pick up later.
571
- /// @param question_id The ID of the question
572
- /// @param history_hashes Second-last-to-first, the hash of each history entry. (Final one should be empty).
573
- /// @param addrs Last-to-first, the address of each answerer or commitment sender
574
- /// @param bonds Last-to-first, the bond supplied with each answer or commitment
575
- /// @param answers Last-to-first, each answer supplied, or commitment ID if the answer was supplied with commit->reveal
576
- function claimWinnings(
577
- bytes32 question_id,
578
- bytes32[] memory history_hashes, address[] memory addrs, uint256[] memory bonds, bytes32[] memory answers
579
- )
580
- stateFinalized(question_id)
581
- public {
582
-
583
- require(history_hashes.length > 0, "at least one history hash entry must be provided");
584
-
585
- // These are only set if we split our claim over multiple transactions.
586
- address payee = question_claims[question_id].payee;
587
- uint256 last_bond = question_claims[question_id].last_bond;
588
- uint256 queued_funds = question_claims[question_id].queued_funds;
589
-
590
- // Starts as the hash of the final answer submitted. It'll be cleared when we're done.
591
- // If we're splitting the claim over multiple transactions, it'll be the hash where we left off last time
592
- bytes32 last_history_hash = questions[question_id].history_hash;
593
-
594
- bytes32 best_answer = questions[question_id].best_answer;
595
-
596
- uint256 i;
597
- for (i = 0; i < history_hashes.length; i++) {
598
-
599
- // Check input against the history hash, and see which of 2 possible values of is_commitment fits.
600
- bool is_commitment = _verifyHistoryInputOrRevert(last_history_hash, history_hashes[i], answers[i], bonds[i], addrs[i]);
601
-
602
- queued_funds = queued_funds.add(last_bond);
603
- (queued_funds, payee) = _processHistoryItem(
604
- question_id, best_answer, queued_funds, payee,
605
- addrs[i], bonds[i], answers[i], is_commitment);
606
-
607
- // Line the bond up for next time, when it will be added to somebody's queued_funds
608
- last_bond = bonds[i];
609
- last_history_hash = history_hashes[i];
610
-
611
- }
612
-
613
- if (last_history_hash != NULL_HASH) {
614
- // We haven't yet got to the null hash (1st answer), ie the caller didn't supply the full answer chain.
615
- // Persist the details so we can pick up later where we left off later.
616
-
617
- // If we kblock.timestamp who to pay we can go ahead and pay them out, only keeping back last_bond
618
- // (We always kblock.timestamp who to pay unless all we saw were unrevealed commits)
619
- if (payee != NULL_ADDRESS) {
620
- _payPayee(question_id, payee, queued_funds);
621
- queued_funds = 0;
622
- }
623
-
624
- question_claims[question_id].payee = payee;
625
- question_claims[question_id].last_bond = last_bond;
626
- question_claims[question_id].queued_funds = queued_funds;
627
- } else {
628
- // There is nothing left below us so the payee can keep what remains
629
- _payPayee(question_id, payee, queued_funds.add(last_bond));
630
- delete question_claims[question_id];
631
- }
632
-
633
- questions[question_id].history_hash = last_history_hash;
634
-
635
- }
636
-
637
- function _payPayee(bytes32 question_id, address payee, uint256 value)
638
- internal {
639
- balanceOf[payee] = balanceOf[payee].add(value);
640
- emit LogClaim(question_id, payee, value);
641
- }
642
-
643
- function _verifyHistoryInputOrRevert(
644
- bytes32 last_history_hash,
645
- bytes32 history_hash, bytes32 answer, uint256 bond, address addr
646
- )
647
- internal pure returns (bool) {
648
- if (last_history_hash == keccak256(abi.encodePacked(history_hash, answer, bond, addr, true)) ) {
649
- return true;
650
- }
651
- if (last_history_hash == keccak256(abi.encodePacked(history_hash, answer, bond, addr, false)) ) {
652
- return false;
653
- }
654
- revert("History input provided did not match the expected hash");
655
- }
656
-
657
- function _processHistoryItem(
658
- bytes32 question_id, bytes32 best_answer,
659
- uint256 queued_funds, address payee,
660
- address addr, uint256 bond, bytes32 answer, bool is_commitment
661
- )
662
- internal returns (uint256, address) {
663
-
664
- // For commit-and-reveal, the answer history holds the commitment ID instead of the answer.
665
- // We look at the referenced commitment ID and switch in the actual answer.
666
- if (is_commitment) {
667
- bytes32 commitment_id = answer;
668
- // If it's a commit but it hasn't been revealed, it will always be considered wrong.
669
- if (!commitments[commitment_id].is_revealed) {
670
- delete commitments[commitment_id];
671
- return (queued_funds, payee);
672
- } else {
673
- answer = commitments[commitment_id].revealed_answer;
674
- delete commitments[commitment_id];
675
- }
676
- }
677
-
678
- if (answer == best_answer) {
679
-
680
- if (payee == NULL_ADDRESS) {
681
-
682
- // The entry is for the first payee we come to, ie the winner.
683
- // They get the question bounty.
684
- payee = addr;
685
- queued_funds = queued_funds.add(questions[question_id].bounty);
686
- questions[question_id].bounty = 0;
687
-
688
- } else if (addr != payee) {
689
-
690
- // Answerer has changed, ie we found someone lower down who needs to be paid
691
-
692
- // The lower answerer will take over receiving bonds from higher answerer.
693
- // They should also be paid the takeover fee, which is set at a rate equivalent to their bond.
694
- // (This is our arbitrary rule, to give consistent right-answerers a defence against high-rollers.)
695
-
696
- // There should be enough for the fee, but if not, take what we have.
697
- // There's an edge case involving weird arbitrator behaviour where we may be short.
698
- uint256 answer_takeover_fee = (queued_funds >= bond) ? bond : queued_funds;
699
-
700
- // Settle up with the old (higher-bonded) payee
701
- _payPayee(question_id, payee, queued_funds.sub(answer_takeover_fee));
702
-
703
- // Now start queued_funds again for the new (lower-bonded) payee
704
- payee = addr;
705
- queued_funds = answer_takeover_fee;
706
-
707
- }
708
-
709
- }
710
-
711
- return (queued_funds, payee);
712
-
713
- }
714
-
715
- /// @notice Convenience function to assign bounties/bonds for multiple questions in one go, then withdraw all your funds.
716
- /// Caller must provide the answer history for each question, in reverse order
717
- /// @dev Can be called by anyone to assign bonds/bounties, but funds are only withdrawn for the user making the call.
718
- /// @param question_ids The IDs of the questions you want to claim for
719
- /// @param lengths The number of history entries you will supply for each question ID
720
- /// @param hist_hashes In a single list for all supplied questions, the hash of each history entry.
721
- /// @param addrs In a single list for all supplied questions, the address of each answerer or commitment sender
722
- /// @param bonds In a single list for all supplied questions, the bond supplied with each answer or commitment
723
- /// @param answers In a single list for all supplied questions, each answer supplied, or commitment ID
724
- function claimMultipleAndWithdrawBalance(
725
- bytes32[] memory question_ids, uint256[] memory lengths,
726
- bytes32[] memory hist_hashes, address[] memory addrs, uint256[] memory bonds, bytes32[] memory answers
727
- )
728
- stateAny() // The finalization checks are done in the claimWinnings function
729
- public {
730
-
731
- uint256 qi;
732
- uint256 i;
733
- for (qi = 0; qi < question_ids.length; qi++) {
734
- bytes32 qid = question_ids[qi];
735
- uint256 ln = lengths[qi];
736
- bytes32[] memory hh = new bytes32[](ln);
737
- address[] memory ad = new address[](ln);
738
- uint256[] memory bo = new uint256[](ln);
739
- bytes32[] memory an = new bytes32[](ln);
740
- uint256 j;
741
- for (j = 0; j < ln; j++) {
742
- hh[j] = hist_hashes[i];
743
- ad[j] = addrs[i];
744
- bo[j] = bonds[i];
745
- an[j] = answers[i];
746
- i++;
747
- }
748
- claimWinnings(qid, hh, ad, bo, an);
749
- }
750
- withdraw();
751
- }
752
-
753
- /// @notice Returns the questions's content hash, identifying the question content
754
- /// @param question_id The ID of the question
755
- function getContentHash(bytes32 question_id)
756
- public view returns(bytes32) {
757
- return questions[question_id].content_hash;
758
- }
759
-
760
- /// @notice Returns the arbitrator address for the question
761
- /// @param question_id The ID of the question
762
- function getArbitrator(bytes32 question_id)
763
- public view returns(address) {
764
- return questions[question_id].arbitrator;
765
- }
766
-
767
- /// @notice Returns the timestamp when the question can first be answered
768
- /// @param question_id The ID of the question
769
- function getOpeningTS(bytes32 question_id)
770
- public view returns(uint32) {
771
- return questions[question_id].opening_ts;
772
- }
773
-
774
- /// @notice Returns the timeout in seconds used after each answer
775
- /// @param question_id The ID of the question
776
- function getTimeout(bytes32 question_id)
777
- public view returns(uint32) {
778
- return questions[question_id].timeout;
779
- }
780
-
781
- /// @notice Returns the timestamp at which the question will be/was finalized
782
- /// @param question_id The ID of the question
783
- function getFinalizeTS(bytes32 question_id)
784
- public view returns(uint32) {
785
- return questions[question_id].finalize_ts;
786
- }
787
-
788
- /// @notice Returns whether the question is pending arbitration
789
- /// @param question_id The ID of the question
790
- function isPendingArbitration(bytes32 question_id)
791
- public view returns(bool) {
792
- return questions[question_id].is_pending_arbitration;
793
- }
794
-
795
- /// @notice Returns the current total unclaimed bounty
796
- /// @dev Set back to zero once the bounty has been claimed
797
- /// @param question_id The ID of the question
798
- function getBounty(bytes32 question_id)
799
- public view returns(uint256) {
800
- return questions[question_id].bounty;
801
- }
802
-
803
- /// @notice Returns the current best answer
804
- /// @param question_id The ID of the question
805
- function getBestAnswer(bytes32 question_id)
806
- public view returns(bytes32) {
807
- return questions[question_id].best_answer;
808
- }
809
-
810
- /// @notice Returns the history hash of the question
811
- /// @param question_id The ID of the question
812
- /// @dev Updated on each answer, then rewound as each is claimed
813
- function getHistoryHash(bytes32 question_id)
814
- public view returns(bytes32) {
815
- return questions[question_id].history_hash;
816
- }
817
-
818
- /// @notice Returns the highest bond posted so far for a question
819
- /// @param question_id The ID of the question
820
- function getBond(bytes32 question_id)
821
- public view returns(uint256) {
822
- return questions[question_id].bond;
823
- }
824
-
825
- }