Skip to content

Instantly share code, notes, and snippets.

@chriseth
chriseth / SimpleStorage.sol
Created October 19, 2015 22:46
SimpleStorage
contract SimpleStorage {
uint storedData;
function set(uint x) {
storedData = x;
}
function get() constant returns (uint retVal) {
return storedData;
}
}
@chriseth
chriseth / Coin.sol
Created October 19, 2015 23:05
Coin
contract Coin {
// The keyword "public" makes those variables
// readable from outside.
address public minter;
mapping (address => uint) public balances;
// Events allow light clients to react on
// changes efficiently.
event Sent(address from, address to, uint amount);
@chriseth
chriseth / OpenAuction.sol
Last active November 3, 2015 09:47
Open Auction
contract SimpleAuction {
// Parameters of the auction. Times are either
// absolute unix timestamps (seconds since 1970-01-01)
// ore time periods in seconds.
address public beneficiary;
uint public auctionStart;
uint public biddingTime;
// Current state of the auction.
address public highestBidder;
contract AccessRestriction {
// These will be assigned at the construction
// phase, where `msg.sender` is the account
// creating this contract.
address public owner = msg.sender;
uint public creationTime = now;
// Modifiers can be used to change
// the body of a function.
// If this modifier is used, it will
@chriseth
chriseth / StateMachine.sol
Created October 20, 2015 20:56
State Machine
contract StateMachine {
enum Stages {
AcceptingBlindedBids,
RevealBids,
AnotherStage,
AreWeDoneYet,
Finished
}
// This is the current stage.
Stages public stage = Stages.AcceptingBlindedBids;
@chriseth
chriseth / Purchase.sol
Last active May 2, 2021 13:27
Purchase
// This contract can be used to hold money while an item is in transit
// during purchase.
// This protocol is a variation of a protocol by Oleg Andreev which is
// described at
// https://gatecoin.com/blog/2015/10/blockchain2-disrupting-disrutors/
//
// Assume Bob wants to buy an item worth x Ether from Alice.
// Alice creates this contract and and sends 2x Ether to the contract
// together with its creation transaction.
// If Bob does not react, Alice can get her money back.
@chriseth
chriseth / BinarySearch.sol
Last active August 3, 2022 19:22
Verified binary search in sorted array
contract BinarySearch {
///@why3
/// requires { arg_data.length < UInt256.max_uint256 }
/// requires { 0 <= to_int arg_begin <= to_int arg_end <= arg_data.length }
/// requires { forall i j: int. 0 <= i <= j < arg_data.length -> to_int arg_data[i] <= to_int arg_data[j] }
/// variant { to_int arg_end - to_int arg_begin }
/// ensures {
/// to_int result < UInt256.max_uint256 -> (to_int arg_begin <= to_int result < to_int arg_end && to_int arg_data[to_int result] = to_int arg_value)
/// }
/// ensures {
@chriseth
chriseth / DoubleOrNothing.sol
Created October 31, 2015 22:20
DoubleOrNothing
contract Alarm {
function deposit();
function scheduleCall(address contractAddress, bytes4 abiSignature, bytes32
dataHash, uint targetBlock, uint8 gracePeriod);
}
contract BitcoinBridge {
function queuePayment(bytes bitcoinAddress) returns(bool successful);
}
@chriseth
chriseth / Ballot.sol
Last active May 30, 2016 14:00
Ballot
/// @title Voting with delegation.
contract Ballot {
// This declares a new complex type which will
// be used for variables later.
// It will represent a single voter.
struct Voter {
uint weight; // weight is accumulated by delegation
bool voted; // if true, that person already voted
address delegate; // person delegated to
uint vote; // index of the voted proposal
@chriseth
chriseth / BlindAuction.sol
Last active November 3, 2015 15:12
Blind Auction
contract BlindAuction
{
struct Bid
{
bytes32 blindedBid;
uint deposit;
}
address public beneficiary;
uint public auctionStart;
uint public biddingEnd;