-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontract.sol
483 lines (317 loc) · 10.1 KB
/
contract.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
// Function contract
////////////////// Start ///////////////
contract funcBehaviour {
uint age = 30;
function getAge() public view returns (uint){
return age;
}
function GetNumber() public pure returns (uint){
uint Number = 150;
return Number;
}
uint public recieveamount; //1
function GetAmount() public payable
{
recieveamount = recieveamount + msg.value; //1
}
}
///////////////// END /////////////////////
// Inheritance contract
//////////// start //////////////////
contract Student {
uint public age;
string public name;
function setAge(uint _age) public {
age = _age;
}
function setName(string memory _name) public {
name = _name;
}
}
contract Teacher is Student {
string public setDesigna;
function setDesignation(string memory _set) public {
setDesigna = _set;
}
}
////////////// END ////////////////////////
// Modifier Contract
////////////////// Start////////////////////
contract modifierExample {
address public owner;
uint public sum;
constructor() {
owner = msg.sender;
}
modifier OnlyOwner(){
require(msg.sender == owner , "You are not the Owner");
_;
}
function add() public OnlyOwner returns (uint)
{
sum = sum + 1;
return sum;
}
}
/////////////////// END //////////////////////
// Array Contract
////////////// Start ////////////////////
contract Array{
uint[] public dynamicArray; // dynamic
uint[] public myArray2 = [1,2,3];
uint[10] public myFixedArray; //fixed
function push(uint _value) public {
dynamicArray.push(_value);
}
function pop() public {
dynamicArray.pop();
}
function getLength() public view returns (uint) {
return dynamicArray.length;
}
function remove(uint _index) public {
delete dynamicArray[_index];
pop();
}
}
/////////////// END ////////////////////
// Struct Contract
//////////////// Start /////////////////
contract stuctsExample {
struct Teachers {
string name;
uint age;
}
struct Students {
string name;
uint age;
uint rollnumber;
bool isPass;
}
Teachers[] public teacher;
Students[] public student;
function createTeacherRecord(string memory _name, uint _age) public {
teacher.push(Teachers(_name,_age));
}
function createStudentRecord(string memory name, uint age , uint rollnumber, bool isPass) public {
student.push(Students(name , age , rollnumber, isPass));
}
function getStudentRecord(uint _index) public view returns(string memory, uint , uint , bool){
Students storage studentObj = student[_index];
return (studentObj.name,studentObj.age,studentObj.rollnumber,studentObj.isPass);
}
}
/////////////// END /////////////////////
// Mapping contract
/////////////// Start //////////////////
contract mappingExample {
mapping(uint => address) public myAddress;
function add(uint id, address _addr) public {
myAddress[id] = _addr;
}
}
//////////////// End /////////////////
// Enum Contract
/////////////// Start /////////////
contract enumExample {
enum Status {
Pending,
Approved,
Reject
}
Status public status;
string public statusCheck;
function Approved() public {
require(status == Status.Pending);
status = Status.Approved;
statusCheck = "Approved";
}
function Reject() public {
require(status == Status.Approved);
status = Status.Reject;
statusCheck = "Reject";
}
}
///////////////// END ///////////////////
// Sending and reciving ETH Contract
////////////////// Start ///////////////////
// 1 transfer
// 2 send
// 3 call
contract ReceiveEther
{
fallback() external payable {
// custom function code
}
receive() external payable {
// custom function code
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
contract sendEther {
bytes public data;
function sendTransfer(address payable _to) public payable {
_to.transfer(msg.value);
}
function sendEtherViaSend(address payable _to) public payable{
bool sent = _to.send(msg.value);
require(sent , "Failed to send Ether");
}
function sentViaCall(address payable _to) public payable {
(bool sent , bytes memory _data) = _to.call{value:msg.value,gas:2000}("");
data = _data;
require(sent , "Failed to send Ether");
}
}
///////////////////// END //////////////////////////
// Wallet development Contract (Using event)
///////////// Start //////////////////
contract walletDevelopment {
event Deposit(address depositAddress , uint amount , uint balance);
event Withdraw(uint amount , uint balance);
event Transfer(address to , uint amount , uint balance);
address payable public owner;
constructor() public payable {
owner = payable(msg.sender);
}
function deposit(uint _amount) public payable{
require(msg.value > 0 , "Please sent ether");
emit Deposit(msg.sender , _amount , address(this).balance);
}
modifier onlyOwner() {
require(msg.sender == owner , "Your are not the onwer");
_;
}
function withdraw(uint _amount) public onlyOwner {
owner.transfer(_amount);
emit Withdraw(_amount , address(this).balance);
}
function transfer(address payable _to , uint _amount) public onlyOwner {
_to.transfer(_amount);
emit Transfer(_to , _amount , address(this).balance);
}
function getBalance() public view returns(uint){
return address(this).balance;
}
}
///////////////////// END ///////////////////
// Hash Function
/////////////////// START ///////////////////
contract HashFunction {
function hash(string memory _text , uint _num , address _addr)
public pure returns(bytes32)
{
return keccak256(abi.encodePacked(_text , _num , _addr));
}
//0xe5d11b08737f5dbf924278d835533b2b1e65c2fe1b5b119c5fdd21555547b9c4
//0x1edf4aae368e845d5d1cd28aec0624c467d538ecc7e5660765ed2afedca37aca
//0x40120e57c637c5d216032d19c65bc64e072b32cfe0a6b4f0e6c65ddbaf251fff
function collision(string memory _text , string memory _secText)
public pure returns(bytes32)
{
// AA = BBB == AABBB
return keccak256(abi.encode(_text , _secText));
}
}
contract GuessTheMagicWorld{
//0x541111248b45b7a8dc3f5579f630e74cb01456ea6ac067d3f4d793245a255155
function hash(string memory _text)
public pure returns(bytes32)
{
return keccak256(abi.encodePacked(_text));
}
bytes32 public answer = 0x541111248b45b7a8dc3f5579f630e74cb01456ea6ac067d3f4d793245a255155;
function guess(string memory _word) public view returns (bool) {
return keccak256(abi.encodePacked(_word)) == answer;
}
}
////////////////////// END /////////////////////////////
// Call Other contract function
///////////////////////////// START /////////////////////////
contract Callme {
uint public x;
uint public value;
uint public addv1;
function setX(uint _x) public returns(uint) {
x = _x;
return x;
}
function add(uint _a, uint _b) public returns(uint) {
addv1 = _a + _b;
return addv1;
}
function setXandSendEther(uint _x) public payable returns (uint , uint) {
x = _x;
value = msg.value;
return (x , value);
}
//0xd2a5bC10698FD955D1Fe6cb468a17809A08fd005
}
contract Caller {
function setValueOFContractCallme(Callme _call , uint _x) public {
uint x = _call.setX(_x);
uint x1 = _call.add(_x , 1);
}
function addValueOfContractCallme(Callme _call , uint _x, uint _y) public {
// add (1 , 2)
}
function setXandSendEther(Callme _call , uint _x) public payable {
}
}
////////////////////// END //////////////////////////////////
// Player State Contract ///
//////////////////// START ///////////////////////
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "@openzeppelin/contracts/access/Ownable.sol";
contract playerState is Ownable{
struct Player {
string hash;
address playerAddress;
uint score;
}
uint public playerCount;
mapping(uint => Player) public playerMapping;
mapping(address => bool) public whitelistedAddresses;
event AddingNewPlayer (
uint playerId,
address playerAddress
);
modifier isWhitelisted() {
require(whitelistedAddresses[msg.sender], "Unauthorized Address");
_;
}
function addWhitelistedAddress(address addWhitelistedAddress) external onlyOwner {
whitelistedAddresses[addWhitelistedAddress] = true;
}
function removeWhitelistedAddress(address addWhitelistedAddress) external onlyOwner{
whitelistedAddresses[addWhitelistedAddress] = false;
}
function createNewPlayer(
string memory _hash,
address _playerAddress,
uint _score
) external
isWhitelisted
{
playerCount++;
playerMapping[playerCount] = Player(_hash, _playerAddress, _score);
emit AddingNewPlayer(playerCount,_playerAddress);
}
function UpdatingPlayer(
uint playerId,
string memory _hash,
address _playerAddress,
uint _score
)
external
{
playerMapping[playerId].hash = _hash;
playerMapping[playerId].playerAddress = _playerAddress;
playerMapping[playerId].score = _score;
}
}