forked from scrudlabs/SmartHack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolidityWorkshopv812.sol
234 lines (187 loc) · 6.71 KB
/
solidityWorkshopv812.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
pragma solidity ^0.8.12;
//1 Simple token you can buy and send.
contract SimpleToken{
mapping(address => uint) public balances;
// @dev Buy token at the price of 1ETH/token.
function buyToken() external payable {
balances[msg.sender]+=msg.value / 1 ether;
}
/* @dev Send token.
* @param _recipient The recipient.
* @param _amount The amount to send.
*/
function sendToken(address _recipient, uint _amount) public {
require(balances[msg.sender]!=0); // You must have some tokens.
balances[msg.sender]-=_amount;
balances[_recipient]+=_amount;
}
}
// 2
contract VoteTwoChoices{
mapping(address => uint) public votingRights;
mapping(address => uint) public votesCast;
mapping(bytes32 => uint) public votesReceived;
// @dev Get 1 voting right per ETH sent.
function buyVotingRights() public payable {
votingRights[msg.sender]+=msg.value/(1 ether);
}
/* @dev Vote with nbVotes for a proposition.
* @param _nbVotes The number of votes to cast.
* @param _proposition The proposition to vote for.
*/
function vote(uint _nbVotes, bytes32 _proposition) public {
require(_nbVotes + votesCast[msg.sender]<=votingRights[msg.sender]); // Check you have enough voting rights.
votesCast[msg.sender]+=_nbVotes;
votesReceived[_proposition]+=_nbVotes;
}
}
//3
contract BuyToken {
mapping(address => uint) public balances;
uint public price=1;
address public owner=msg.sender;
/* @dev Buy tokens.
* @param _amount The amount to buy.
* @param _price The price to buy those in ETH.
*/
function buyToken(uint _amount, uint _price) public payable {
require(_price>=price); // The price is at least the current price.
require(_price * _amount * 1 ether <= msg.value); // You have paid at least the total price.
balances[msg.sender]+=_amount;
}
/* @dev Set the price, only the owner can do it.
* @param _price The new price.
*/
function setPrice(uint _price) public {
require(msg.sender==owner);
price=_price;
}
}
//4 Contract to store and redeem money.
contract Store {
struct Safe {
address owner;
uint amount;
}
Safe[] public safes;
// @dev Store some ETH.
function store() public payable {
safes.push(Safe({owner: msg.sender, amount: msg.value}));
}
// @dev Take back all the amount stored.
function take() public payable {
for (uint i; i<safes.length; ++i) {
Safe memory safe = safes[i];
if (safe.owner==msg.sender && safe.amount!=0) {
payable(msg.sender).transfer(safe.amount);
safe.amount=0;
}
}
}
}
//5 Count the total contribution of each user.
// Assume that the one creating the contract contributed 1ETH.
contract CountContribution{
mapping(address => uint) public contribution;
uint public totalContributions;
address owner=msg.sender;
// @dev Constructor, count a contribution of 1 ETH to the creator.
constructor() {
recordContribution(owner, 1 ether);
}
// @dev Contribute and record the contribution.
function contribute() public payable {
recordContribution(msg.sender, msg.value);
}
/* @dev Record a contribution. To be called by CountContribution and contribute.
* @param _user The user who contributed.
* @param _amount The amount of the contribution.
*/
function recordContribution(address _user, uint _amount) public {
contribution[_user]+=_amount;
totalContributions+=_amount;
}
}
//6
contract Token {
mapping(address => uint) public balances;
// @dev Buy token at the price of 1ETH/token.
function buyToken() public payable {
balances[msg.sender]+=msg.value / 1 ether;
}
/* @dev Send token.
* @param _recipient The recipient.
* @param _amount The amount to send.
*/
function sendToken(address _recipient, uint _amount) public {
require(balances[msg.sender]>=_amount); // You must have some tokens.
balances[msg.sender]-=_amount;
balances[_recipient]+=_amount;
}
/* @dev Send all tokens.
* @param _recipient The recipient.
*/
function sendAllTokens(address payable _recipient) public {
balances[_recipient]= balances[msg.sender];
balances[msg.sender]=0;
}
}
//7 You can buy some object.
// Further purchases are discounted.
// You need to pay basePrice / (1 + objectBought), where objectBought is the number of object you previously bought.
contract DiscountedBuy {
uint public basePrice = 1 ether;
mapping (address => uint) public objectBought;
// @dev Buy an object.
function buy() public payable {
require(msg.value * (1 + objectBought[msg.sender]) == basePrice);
objectBought[msg.sender]+=1;
}
/* @dev Return the price you'll need to pay.
* @return price The amount you need to pay in wei.
*/
function price() public view returns(uint _price) {
return basePrice/(1 + objectBought[msg.sender]);
}
}
//8 You choose Head or Tail and send 1 ETH.
// The next party send 1 ETH and try to guess what you chose.
// If it succeed it gets 2 ETH, else you get 2 ETH.
contract HeadOrTail {
bool public chosen; // True if head/tail has been chosen.
bool lastChoiceHead; // True if the choice is head.
address public lastParty; // The last party who chose.
/* @dev Must be sent 1 ETH.
* Choose head or tail to be guessed by the other player.
* @param _chooseHead True if head was chosen, false if tail was chosen.
*/
function choose(bool _chooseHead) public payable {
require(!chosen);
require(msg.value == 1 ether);
chosen=true;
lastChoiceHead=_chooseHead;
lastParty=msg.sender;
}
function guess(bool _guessHead) public payable {
require(chosen);
require(msg.value == 1 ether);
if (_guessHead == lastChoiceHead)
payable(msg.sender).transfer(2 ether);
else
payable(lastParty).transfer(2 ether);
chosen=false;
}
}
//9 You can store ETH in this contract and redeem them.
contract Vault {
mapping(address => uint) public balances;
// @dev Store ETH in the contract.
function store() public payable {
balances[msg.sender]+=msg.value;
}
// @dev Redeem your ETH.
function redeem() public payable{
msg.sender.call{value : (balances[msg.sender])};
balances[msg.sender]=0;
}
}