Skip to content

Instantly share code, notes, and snippets.

@chriseth
chriseth / GamblingContract.js
Created December 18, 2014 21:07
Solidity Gambling Contract
contract Gamble {
function Gamble() {
owner = msg.sender;
address nameReg = 0x72ba7d8e73fe8eb666ea66babc8116a41bfb10e2;
nameReg.callstring32string32("register", "UltimateGamble");
}
function gamble(uint luckyNumber) {
value = value+ msg.value;
hash randomness = sha3(block.prevhash ^ hash(msg.sender) ^ luckyNumber);
if (msg.value >= 1000000000000000000 && ((randomness & 0xff) < hash(0x20))) {

Blind Auction

Bidding period

Everyone can place a nonce-hashed bid together with a value transfer.

Revealing Period

Every bidder can reveal their nonce and thus the bid. If the previously transferred value is smaller than the bid, the person is refunded and the bid discarded.

@chriseth
chriseth / contract.sol
Created June 4, 2015 13:17
Solidity decimal conversion
contract c {
uint a;
function charAt(bytes32 b, uint char) returns (bytes1) {
return bytes1(uint8(uint(b) / (2**((31 - char) * 8))));
}
function parseDecimal(bytes32 byteString) returns (uint r) {
uint n = uint(byteString);
for (uint b = 0; b < 32; b ++)
{
var c = uint8(charAt(byteString, b));
@chriseth
chriseth / backtrace
Created June 4, 2015 15:19
geth crash
$ geth --rpc --rpccorsdomain "http://ethereum-dapp-wallet.meteor.com" --unlock 0x1a062d36074ad50bc573d5839bf41f85c4f21c0e --verbosity 0 console
Unlocking account 0x1a062d36074ad50bc573d5839bf41f85c4f21c0e | Attempt 1/3
Passphrase:
Account '0x1a062d36074ad50bc573d5839bf41f85c4f21c0e' unlocked.
> panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x57e9f8]
goroutine 250 [running]:
github.com/ethereum/go-ethereum/xeth.(*logQueue).add(0x0, 0xc238ae7ba0, 0x1, 0x1)
/build/ethereum-bLfdxX/ethereum-0.9.26+567SNAPSHOT20150603174306trusty/obj-x86_64-linux-gnu/src/github.com/ethereum/go-ethereum/xeth/xeth.go:1004 +0x148
@chriseth
chriseth / trace
Last active August 29, 2015 14:22
wallet trace
0x83ad0
{
"id": 6,
"jsonrpc": "2.0",
"result": {
"0x0000000000000000000000000000000000000000000000000000000000000000": "0x01",
"0x0000000000000000000000000000000000000000000000000000000000000001": "0x01",
"0x0000000000000000000000000000000000000000000000000000000000000003": "0x1a062d36074ad50bc573d5839bf41f85c4f21c0e",
"0x0000000000000000000000000000000000000000000000000000000000000107": "0x40cf",
"0xa81d9100f82f3c918fd53b3a8571dbb25c834d01a2a44729eeb074a7509daad7": "0x01"
@chriseth
chriseth / crowdfunding.js
Created July 7, 2015 14:20
Mini-Crowdfunding
contract Crowdfunding {
address recipient; uint goal; uint deadline;
struct Contribution { address contributor; uint amount; }
Contribution[] contributions;
uint contributed;
function Crowdfunding(address _recipient, uint256 _goal, uint256 _deadline) {
recipient = _recipient;
goal = _goal;
deadline = _deadline;
}
contract Ballot {
enum Vote {
None,
Direct { proposal: Proposal },
Delegated { to: address }
}
struct Voter { vote: Vote; weight: uint; }
using Proposal = uint8;
chairperson: address;
numProposals: uint8;
@chriseth
chriseth / WalletCopy.js
Last active August 29, 2015 14:25
WalletCopy
/*
Use the following binary code to deploy a "copy" of a wallet contract. Replace the string
"XYXYXYREPLACEBYACTUALWALLETADDRESSXYXYXY" by the hex address of the actual wallet contract,
be sure to include any leading zeros, i.e. do not change the length while replacing.
606060405260405161014a38038061014a8339810160409081528151608051920180516001908101815533600160a060020a0316600381905560009081526101026020529283205591905b825181101560d0578281815181101560025790602001906020020151600160a060020a0316600260005082600201610100811015600257909001600050819055508060020161010260005060008584815181101560025790602001906020020151600160a060020a0316815260200190815260200160002060005081905550600101604a565b8160006000508190555062015180420461010760005081905550505050604f806100fb6000396000f30060606040523615600a575b604d73XYXYXYREPLACEBYACTUALWALLETADDRESSXYXYXY6000366060808383808284378201915050925050506000604051808303816000866161da5a03f2505050565b00
*/
contract WalletCopy {
function WalletCopy(address[] _owners, uint _required) {
@chriseth
chriseth / copy.lll
Created July 31, 2015 10:39
Contract "copy" using calldata
{
(def 'original 0xcafe)
(calldatacopy 0 0 (asm calldatasize))
(callcode allgas original 0 0 (asm calldatasize) 0 32)
(return 0 32)
}
@chriseth
chriseth / verify.js
Last active October 1, 2015 13:59
Bytecode verifier
// Usage: node verify.js file_with_source_code file_with_hex_bytecode compiler_binary...
var fs = require('fs');
var source = fs.readFileSync(process.argv[2]).toString();
var binary = fs.readFileSync(process.argv[3]).toString().replace(/\s+/, '');
if (binary.substring(0, 2) == '0x')
binary = binary.substring(2);
var matchFound = false;
process.argv.forEach(function(compilerFile, i) {