Skip to content

Instantly share code, notes, and snippets.

@ohadcn
Created October 22, 2018 07:48
Show Gist options
  • Save ohadcn/4d77fe0723f5a52b64d70faec7175931 to your computer and use it in GitHub Desktop.
Save ohadcn/4d77fe0723f5a52b64d70faec7175931 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.4.25+commit.59dbf8f1.js&optimize=false&gist=
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
pragma solidity ^0.4.23;
// pragma experimental ABIEncoderV2;
library HandLib {
// enum HAND_TYPES { HIGH_CARD, ONE_PAIR, TWO_PAIRS, THREE, STRAIGHT, FLUSH, FULL_HOUSE, FOUR, STRAIGHT_FLUSH}
// mapping(HAND_TYPES => uint8) handRank { HIGH_CARD: 0, ONE_PAIR: 1, TWO_PAIRS: 2, THREE: 3, STRAIGHT: 4, FLUSH: 5, FULL_HOUSE: 5, FOUR: 6, STRAIGHT_FLUSH: 7}
function getCardRank(uint8 card) private pure returns (uint8 rank) {
require(card < 52);
rank = (card % 13);
}
function getCardSuit(uint8 card) pure private returns (uint8 suit) {
suit = card / 13;
}
function getHighCard(uint8[5] hand) pure private returns (uint8 high) {
high = getCardRank(hand[0]);
for (uint8 i = 1; i < 5; i++) {
uint8 card = getCardRank(hand[i]);
if(card > high) {
high = card;
}
}
}
function getHighCardExcept(uint8[5] hand, uint8 except) pure private returns (uint8 high) {
high = getCardRank(hand[0]);
for (uint8 i = 1; i < 5; i++) {
uint8 card = getCardRank(hand[i]);
if(card != except && (card > high || high == except )) {
high = card;
}
}
}
function verifyPair(uint8[5] hand, uint8 handRank) pure private returns (bool res) {
bool found = false;
for (uint8 i = 0; i < 5; i++) {
uint8 card = getCardRank(hand[i]);
if(card == handRank) {
if(found){
res = true;
return;
}
found = true;
}
}
res = false;
}
function verifyThree(uint8[5] hand, uint8 handRank) pure private returns (bool res) {
uint8 found = 0;
for (uint8 i = 0; i < 5; i++) {
uint8 card = getCardRank(hand[i]);
if(card == handRank) {
if(found == 2){
res = true;
return;
}
found++;
}
}
res = false;
}
function verifyFour(uint8[5] hand, uint8 handRank) pure private returns (bool res) {
uint8 found = 0;
for (uint8 i = 0; i < 5; i++) {
uint8 card = getCardRank(hand[i]);
if(card == handRank) {
if(found == 3) {
res = true;
return;
}
found++;
}
}
res = false;
}
function verifyFlush(uint8[5] hand, uint8 /* handRank*/) pure private returns (bool res) {
uint8 suit = getCardSuit(hand[0]);
for (uint8 i = 1; i < 5; i++) {
require(getCardSuit(hand[i]) == suit);
}
res = true;
}
function verifyStright(uint8[5] hand, uint8 handRank) pure private returns (bool res) {
bool[5] memory found;
for (uint8 j = 0; j < 5; j++) {
found[j] = false;
}
for (uint8 i = 0; i < 5; i++) {
uint8 card = getCardRank(hand[i]);
uint8 diff = handRank - card;
if(handRank == 3 && card == 12)
diff = 4;
require(4 >= diff && diff >= 0 );
require(!found[diff]);
found[diff] = true;
}
res = true;
}
function verifyStrightFlush(uint8[5] hand, uint8 handRank) pure private returns (bool res) {
verifyStright(hand, handRank);
verifyFlush(hand, handRank);
res = true;
}
function verifyFullHouse(uint8[5] hand, uint8 handRank) pure private returns (bool res) {
if(verifyThree(hand, handRank) &&
verifyPair(hand, getHighCardExcept(hand, handRank))) {
res = true;
} else
res = false;
}
function verifyTwoPairs(uint8[5] hand, uint8 handRank) pure private returns (bool res) {
verifyPair(hand, handRank);
uint8[2] memory cards;
for(uint8 j = 0; j < 2; j++) {
cards[j] = 15;
}
for(uint i = 0; i < 5; i++) {
uint8 rank = getCardRank(hand[i]);
if(rank == handRank)
continue;
if(rank == cards[0] || rank == cards[1]) {
res = true;
return;
}
cards[(cards[0] == 15) ? 0 : 1] = rank;
}
res = false;
}
function verifyHand(uint8[5] hand, uint8 handType, uint8 handRank) pure public returns (bool res) {
require(handRank < 13);
res = true;
if(handType == 0)
require(getHighCard(hand) == handRank);
else if(handType == 1)
require(verifyPair(hand, handRank));
else if(handType == 2)
require(verifyTwoPairs(hand, handRank));
else if(handType == 3)
require(verifyThree(hand, handRank));
else if(handType == 4)
require(verifyStright(hand, handRank));
else if(handType == 5)
require(verifyFlush(hand, handRank));
else if(handType == 6)
require(verifyFullHouse(hand, handRank));
else if(handType == 7)
require(verifyFour(hand, handRank));
else if(handType == 8)
require(verifyStrightFlush(hand, handRank));
else
res = false;
}
}
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
//pragma solidity ^0.4.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract CveDateViews is usingOraclize {
string public cveCount;
string public url;
string public prod;
string public vendor; // date
event newOraclizeQuery(string description);
event newCveViewsCount(string cveCount);
function CveDateViews(string _vendor, string _prod) {
//url = strConcat('json(https://cve.circl.lu/api/search/' , vendor , '/',prod, ')..[?(@.cvss>7)].Published');
// url = strConcat('json(https://cve.circl.lu/api/search/' , vendor , '/',prod, ').0.Published.3');
prod = _prod;
vendor = _vendor;
update();
//$..[?(@.cvss>8)].Published
//oraclize_query(60, "URL", "json(https://api.kraken.com/0/public/Ticker?pair=ETHXBT).result.XETHXXBT.c.0");
}
function __callback(bytes32 myid, string result) {
if (msg.sender != oraclize_cbAddress()) throw;
cveCount = result;
newCveViewsCount(cveCount);
// do something with cveCount. like sending report to augur if viewsCount > X?
}
function update() payable {
if (oraclize_getPrice("URL") > this.balance) {
newOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
} else {
newOraclizeQuery("Oraclize query was sent, standing by for the answer..");
oraclize_query('URL', strConcat('json(https://cve.circl.lu/api/search/' , vendor , '/',prod, ').0.Published'));
}
}
}
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
pragma solidity ^0.4.11;
// Standard token interface (ERC 20)
// https://github.com/ethereum/EIPs/issues/20
contract ERC20 {
// Functions:
/// @return total amount of tokens
uint256 public totalSupply;
/// @param _owner The address from which the balance will be retrieved
/// @return The balance
function balanceOf(address _owner) view returns (uint256);
/// @notice send `_value` token to `_to` from `msg.sender`
/// @param _to The address of the recipient
/// @param _value The amount of token to be transferred
/// @return Whether the transfer was successful or not
function transfer(address _to, uint256 _value) returns (bool);
/// @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from`
/// @param _from The address of the sender
/// @param _to The address of the recipient
/// @param _value The amount of token to be transferred
/// @return Whether the transfer was successful or not
function transferFrom(address _from, address _to, uint256 _value) returns (bool);
/// @notice `msg.sender` approves `_addr` to spend `_value` tokens
/// @param _spender The address of the account able to transfer the tokens
/// @param _value The amount of wei to be approved for transfer
/// @return Whether the approval was successful or not
function approve(address _spender, uint256 _value) returns (bool);
/// @param _owner The address of the account owning tokens
/// @param _spender The address of the account able to transfer the tokens
/// @return Amount of remaining tokens allowed to spent
function allowance(address _owner, address _spender) view returns (uint256);
// Events:
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
pragma solidity ^0.4.23;
import "./table.sol";
contract Hand {
// enum HandState {
// SHUFFLE_COMMIT: 0, GIVE_CARDS: 1, PRE_FLOP:2 , FLOP: 3, PRE_TURN: 4, TURN: 5, PRE_RIVER: 6, RIVER: 7, POST_RIVER: 8, SHOW: 9
// }
Table table;
uint8 state;
uint8 turn;
uint8 checks;
uint curVal;
mapping(address => bytes32) cardShuffle;
mapping(address => uint) pot;
mapping(address => uint8[2]) userCards;
mapping(address => uint8[2]) userCardsRank;
modifier onlyplayer(uint8 reqState, uint8 player) {
if (msg.sender == table.players[player] && state == reqState)
_;
}
function put(uint ammount, address player) private {
require(ammount < table.balances[player]);
prev = table.balances[player];
require(prev > ammount);
require(ammount >= curVal || (ammount - prev) == table.balances[player]);
table.balances[player] -= ammount - prev;
pot[address] = ammount;
}
constructor(Table _table, address _bigBlind, address _smallBlind, uint8 _startingPlayer) public {
table = _table;
put((table.blind / 2), _smallBlind);
put(table.blind, _bigBlind);
turn = _startingPlayer;
}
function commitShuffle(bytes32 shuffleHash) public {
require(msg.sender == table.players[turn]);
require(state == 0);
cardShuffle[msg.sender] = shuffleHash;
turn = (turn + 1) % table.playersOnline;
checks++;
if(checks == table.playersOnline) {
state++;
checks = 0;
}
}
function acceptCards() public onlyplayer(1, turn) {
turn = (turn + 1) % table.playersOnline;
checks++;
if(checks == table.playersOnline) {
state++;
checks = 0;
}
}
function nextPlayer() {
turn = (turn + 1) % table.playersOnline;
while(userCards[table.players[turn]][0] == 100) {
turn = (turn + 1) % table.playersOnline;
checks++;
}
if(checks == table.playersOnline) {
state++;
}
}
function bet(uint val) public { //can be used for call / raise / bet
require((state % 2) == 0);
require(state != 0);
require(msg.sender == table.players[turn]);
put(val, msg.sender);
checks = 0;
nextPlayer();
}
function fold() {
require((state % 2) == 0);
require(state != 0);
require(msg.sender == table.players[turn]);
userCards[msg.sender][0] = 100;
checks++;
nextPlayer();
}.
function submitCards(bytes32[5][] proof1, uint8 proof1Path, uint8 card1, bytes32[5][] proof2, uint8 proof2Path, uint8 card2) {
nmuj
}
}
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
/*
Kraken-based ETH/XBT price ticker
This contract keeps in storage an updated ETH/XBT price,
which is updated every ~60 seconds.
*/
//pragma solidity ^0.4.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract KrakenPriceTicker is usingOraclize {
string public ETHXBT;
event newOraclizeQuery(string description);
event newKrakenPriceTicker(string price);
function KrakenPriceTicker() {
//oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS);
//update();
}
function __callback(bytes32 myid, string result, bytes proof) {
if (msg.sender != oraclize_cbAddress()) throw;
ETHXBT = result;
newKrakenPriceTicker(ETHXBT);
update();
}
function update() payable {
if (oraclize.getPrice("URL") > this.balance) {
newOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
} else {
newOraclizeQuery("Oraclize query was sent, standing by for the answer..");
oraclize_query(60, "URL", "json(https://api.kraken.com/0/public/Ticker?pair=ETHXBT).result.XETHXXBT.c.0");
}
}
}
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
contract Owned {
/**
* Contract owner address
*/
address public owner;
/**
* @dev Delegate contract to another person
* @param _owner New owner address
*/
function setOwner(address _owner) onlyOwner {
owner = _owner;
}
/**
* @dev Owner check modifier
*/
modifier onlyOwner { if (msg.sender != owner) revert(); _; }
}
contract Destroyable {
address public hammer;
/**
* @dev Hammer setter
* @param _hammer New hammer address
*/
function setHammer(address _hammer) onlyHammer {
hammer = _hammer;
}
/**
* @dev Destroy contract and scrub a data
* @notice Only hammer can call it
*/
function destroy() onlyHammer {
selfdestruct(msg.sender);
}
/**
* @dev Hammer check modifier
*/
modifier onlyHammer { if (msg.sender != hammer) revert(); _; }
}
/**
* @title Generic owned destroyable contract
*/
contract Object is Owned, Destroyable {
constructor() {
owner = msg.sender;
hammer = msg.sender;
}
}
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
pragma solidity ^0.4.23;
import "./card.sol";
contract Poker {
mapping(address => CardLib.Card[2]) hands;
address house;
constructor() public {
house = msg.sender;
hands[house][0] = CardLib.num2card(15);
hands[house][1] = CardLib.num2card(25);
}
}
pragma solidity ^0.4.23;
contract Table {
uint blind;
uint minBuyIn;
address[] players;
uint8 playersOnline;
mapping(address => uint) balances;
mapping(address => uint) lockedBalances; //on hand
uint WithdrawRequestTimeout;
mapping(address => uint) balancesWithdrawRequests;
mapping(address => uint) balancesWithdrawTime;
event Deposit(address _from, uint value);
event WithdrawRequest(address _from, uint value);
event Withdraw(address _from, uint value);
uint handCount;
constructor(uint8 _maxPlayers, uint _blind, uint _minBuyIn, uint _WithdrawRequestTimeout) public payable {
require(_minBuyIn > 0);
require(_blind < _minBuyIn);
players.length = _maxPlayers;
balances.length = _maxPlayers;
minBuyIn = _minBuyIn;
blind = _blind;
players[0] = msg.sender;
balances[msg.sender] = msg.value;
WithdrawRequestTimeout = _WithdrawRequestTimeout;
playersOnline = 1;
}
function() payable {
if(balances[msg.sender] == 0){
// TODO
}
balances[msg.sender] += msg.value;
if (msg.value > 0)
Deposit(msg.sender, msg.value);
}
function sit() payable {
balances[msg.sender] += msg.value;
if (msg.value > 0)
Deposit(msg.sender, msg.value);
require(balances[msg.sender] > minBuyIn );
require(players[_id] == address(0));
players[playersOnline] = msg.sender;
playersOnline++;
}
}
// pragma solidity ^0.4.23;
contract Test {
bytes32 public val;
constructor(uint256 i) payable public {
uint256 ha = uint256(sha256(i));
val = sha256(i);
}
function Withdraw() public {
selfdestruct(msg.sender);
}
function getVal() public view returns (bytes32) {
return val;
}
}
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
import "./erc20.sol";
import "./Object.sol";
contract Token is Object, ERC20 {
/* Short description of token */
string public name;
string public symbol;
/* Total count of tokens exist */
uint256 public totalSupply;
/* Fixed point position */
uint256 public decimals;
/* Token approvement system */
mapping(address => uint256) balances;
mapping(address => mapping(address => uint256)) allowances;
/* Token constructor */
constructor(string _name, string _symbol, uint8 _decimals, uint256 _count) {
name = _name;
symbol = _symbol;
decimals = _decimals;
totalSupply = _count;
balances[msg.sender] = _count;
}
/**
* @dev Get balance of plain address
* @param _owner is a target address
* @return amount of tokens on balance
*/
function balanceOf(address _owner) view returns (uint256) {
return balances[_owner];
}
/**
* @dev Take allowed tokens
* @param _owner The address of the account owning tokens
* @param _spender The address of the account able to transfer the tokens
* @return Amount of remaining tokens allowed to spent
*/
function allowance(address _owner, address _spender) public view returns (uint256) {
return allowances[_owner][_spender];
}
/**
* @dev Transfer self tokens to given address
* @param _to destination address
* @param _value amount of token values to send
* @notice `_value` tokens will be sended to `_to`
* @return `true` when transfer done
*/
function transfer(address _to, uint256 _value) returns (bool) {
if (balances[msg.sender] >= _value) {
balances[msg.sender] -= _value;
balances[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
return false;
}
/**
* @dev Transfer with approvement mechainsm
* @param _from source address, `_value` tokens shold be approved for `sender`
* @param _to destination address
* @param _value amount of token values to send
* @notice from `_from` will be sent `_value` tokens to `_to`
* @return `true` when transfer is done
*/
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
var avail = allowances[_from][msg.sender] > balances[_from] ? balances[_from] : allowances[_from][msg.sender];
if (avail >= _value) {
allowances[_from][msg.sender] -= _value;
balances[_from] -= _value;
balances[_to] += _value;
Transfer(_from, _to, _value);
return true;
}
return false;
}
/**
* @dev Give to target address ability for self token manipulation without sending
* @param _spender target address (future requester)
* @param _value amount of token values for approving
*/
function approve(address _spender, uint256 _value) public returns (bool) {
allowances[msg.sender][_spender] += _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
/**
* @dev Reset count of tokens approved for given address
* @param _spender target address (future requester)
*/
function unapprove(address _spender) public {
allowances[msg.sender][_spender] = 0;
}
}
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment