-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriddle
109 lines (91 loc) · 3.01 KB
/
riddle
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
pragma solidity ^0.4.24;
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
//Le contract Ownable permet simplement de restreindre l'accès à certaines fonctions au créateur du contrat
contract Ownable {
address private _owner;
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
*/
constructor() internal {
_owner = msg.sender;
emit OwnershipTransferred(address(0), _owner);
}
/**
* @return the address of the owner.
*/
function owner() public view returns(address) {
return _owner;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(isOwner());
_;
}
/**
* @return true if `msg.sender` is the owner of the contract.
*/
function isOwner() public view returns(bool) {
return msg.sender == _owner;
}
/**
* @dev Allows the current owner to relinquish control of the contract.
* @notice Renouncing to ownership will leave the contract without an owner.
* It will not be possible to call the functions with the `onlyOwner`
* modifier anymore.
*/
function renounceOwnership() public onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function transferOwnership(address newOwner) public onlyOwner {
_transferOwnership(newOwner);
}
/**
* @dev Transfers control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function _transferOwnership(address newOwner) internal {
require(newOwner != address(0));
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
contract Riddle is Ownable {
event NewRiddle(string text, string answer, uint reward);
string text = "Enigme";
string answer = "42";
uint reward = 1;
Riddle[] public riddles;
//on commence par créer l'énigme
function setRiddle(string _text, string _answer, uint _reward) onlyOwner {
text = _text;
answer = _answer;
reward = _reward;
emit NewRiddle(_text, _answer, _reward);
}
//puis une fonction pour y répondre, et on reçoit la récompense si la réponse est juste
//on notera que Solidity ne permet pas de comparer deux chaînes de caractères, donc ce qu'on fait ici c'est qu'on compare leurs hashes par keccak256
function answerRiddle(string givenAnswer) public {
if (keccak256(abi.encodePacked(givenAnswer)) == keccak256(abi.encodePacked(answer))){
sendReward(msg.sender);
}
}
function sendReward(address _address) public payable {
_address.transfer(reward);
}
}