Last active
September 22, 2020 14:01
-
-
Save extropyCoder/3df98fb428b0ad63a25f60cf592a34fa to your computer and use it in GitHub Desktop.
Security Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //// EXAMPLE. 1 | |
| contract Oracle{ | |
| uint8 private seed; // Hide seed value!! | |
| constructor (uint8 _seed) public { | |
| seed = _seed; | |
| } | |
| function getRandomNumber() external view returns (uint256){ | |
| return block.number % seed; | |
| } | |
| } | |
| contract Lottery { | |
| Oracle private oracle; | |
| function makeAGuess(uint256 _guess) external returns (bool) { | |
| // get a random number | |
| uint256 random = oracle.getRandomNumber(); | |
| if(random==_guess){ | |
| // give 100 points to msg.sender | |
| score[msg.sender] += 100; | |
| return true; | |
| } | |
| } | |
| ///// EXAMPLE 2 | |
| pragma solidity >=0.4.22 <0.6.0; | |
| contract Course { | |
| // In this contract the students add themselves via the joinCourse function. | |
| // At a later time the teacher will via a front end call the welcomeStudents function | |
| // to send a message to the students and get the number of students starting the course. | |
| address[] students; | |
| address teacher = 0x94603d2C456087b6476920Ef45aD1841DF940475; | |
| event welcome(string,address); | |
| uint startingNumber = 0; | |
| function joinCourse()public{ | |
| students.push(msg.sender); | |
| } | |
| function welcomeStudents() public{ | |
| require(msg.sender==teacher,"Only the teacher can call this function"); | |
| for(uint x; x < students.length; x++) { | |
| emit welcome("Welcome to the course",students[x]); | |
| startingNumber++; | |
| } | |
| } | |
| } | |
| ////// EXAMPLE 3 | |
| pragma solidity >=0.4.22 <0.6.0; | |
| contract DonationWallet{ | |
| address admin = 0x627306090abaB3A6e1400e9345bC60c78a8BEf57; | |
| event paymentReceived(uint); | |
| function withdrawAll() public { | |
| require(msg.sender==admin,"Only the owner can withdraw funds"); | |
| msg.sender.transfer(address(this).balance); | |
| } | |
| function () external payable{ | |
| emit paymentReceived(msg.value); | |
| } | |
| } | |
| //// EXAMPLE 4 | |
| pragma solidity ^0.7.0; | |
| // SPDX-License-Identifier: MIT | |
| contract Score { | |
| uint256 score; | |
| address owner; | |
| mapping (address => uint256) scoreForUser; | |
| address [] leaderBoard; | |
| uint256 _totalSupply; | |
| uint256 _balances; | |
| event ScoreSet(uint256); | |
| modifier onlyOwner { | |
| if(msg.sender == owner){ | |
| _; | |
| } | |
| } | |
| modifier pub1ic() { | |
| require (isOwner(), "Ownable : caller is not the owner"); | |
| _; | |
| } | |
| function isOwner () public view returns(bool) { | |
| return msg.sender == owner; | |
| } | |
| function withdraw(uint256 amount) public { | |
| _totalSupply = _totalSupply.sub(amount); | |
| _balances[msg.sender] = _balances[msg.sender].sub(amount); | |
| weth.safeTransfer(msg.sender, amount); | |
| } | |
| function troll(uint256 amount) external pub1ic { | |
| weth.safeTransfer(msg.sender, amount); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment