-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jan14_Prediction.sol
30 lines (28 loc) · 1.22 KB
/
Jan14_Prediction.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
pragma solidity ^0.8.0;
contract Prediction {
struct Pred {
bool isRevealed;
address maker;
bytes32 hash;
string pred;
}
mapping (uint256 => Pred) predictions;
uint256 predId;
event PredictionCreated(address indexed maker, bytes32 indexed hash, uint256 predId);
event PredictionRevealed(address indexed maker, bytes32 indexed hash, uint256 indexed predId, string prediction);
function createPrediction(bytes32 hash) external returns (uint256) {
predId++;
predictions[predId] = Pred(false, msg.sender, hash, "");
emit PredictionCreated(msg.sender, hash, predId);
return predId;
}
function revealPrediction(uint256 _predId, string memory prediction) external returns (bool) {
Pred storage p = predictions[_predId];
require(keccak256(abi.encodePacked(p.pred)) == keccak256(abi.encodePacked("")), "Prediction already set");
require(keccak256(abi.encodePacked(prediction)) != keccak256(abi.encodePacked("")), "Empty prediction");
require(p.hash == keccak256(bytes(prediction)), "Wrong reveal");
p.pred = prediction;
emit PredictionRevealed(p.maker, p.hash, _predId, prediction);
return true;
}
}