Last active
August 29, 2015 14:10
-
-
Save loiluu/28b6a6453df09f1e47f0 to your computer and use it in GitHub Desktop.
This file contains 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
cf_code='''' | |
# storage[-1] = last campaign id | |
# campaigns: | |
# id: creator | |
# id+1: goal | |
# id+2: end timestamp | |
# id+3: pointer to place next donation | |
# id+4: total contribution counter | |
# id+2i+5: ith contributor | |
# id+2i+6: ith contribution | |
# Start a campaign, data [0, id, recipient, goal, time limit] | |
if msg.data[0] == 0: | |
id = msg.data[1] * 2 ^ 32 | |
if contract.storage[id]: | |
return(0) | |
contract.storage[id] = msg.data[2] # Campaign recipient | |
contract.storage[id + 1] = msg.data[3] # Goal | |
contract.storage[id + 2] = block.timestamp + msg.data[4] # Time limit | |
contract.storage[id + 3] = id + 5 # Index of next contribution | |
return(msg.data[1]) | |
# Contribute to a campaign [1, id] | |
elif msg.data[0] == 1: | |
id = msg.data[1] * 2^32 | |
# Update contribution total | |
total_contributed = contract.storage[id + 4] + msg.value | |
contract.storage[id + 4] = total_contributed | |
# Record new contribution | |
sub_index = contract.storage[id + 3] | |
contract.storage[sub_index] = msg.sender | |
contract.storage[sub_index + 1] = msg.value | |
contract.storage[id + 3] = sub_index + 2 | |
# Enough funding? | |
if total_contributed >= contract.storage[id + 1]: | |
send(contract.storage[id], total_contributed) | |
v = id | |
f = sub_index + 2 | |
while v < f: | |
contract.storage[v] = 0 | |
v += 1 | |
return(1) | |
# Expired? | |
if block.timestamp > contract.storage[id + 2]: | |
v = id | |
f = sub_index + 2 | |
while v < id + 5: | |
contract.storage[v] = 0 | |
v += 1 | |
while v < f: | |
send(contract.storage[v], contract.storage[v + 1]) | |
contract.storage[v] = 0 | |
contract.storage[v + 1] = 0 | |
v += 2 | |
return(2) | |
# Progress report [2, id] | |
elif msg.data[0] == 2: | |
return(contract.storage[msg.data[1] * 2^32] + 4) | |
''' | |
import serpent | |
from pyethereum import transactions, blocks, processblock, utils | |
import time | |
code = serpent.compile(cf_code) | |
key = utils.sha3('cow') | |
addr = utils.privtoaddr(key) | |
key2 = utils.sha3('cow2') | |
addr2 = utils.privtoaddr(key2) | |
key_host = utils.sha3('host') | |
add_host = utils.privtoaddr(key_host) | |
#initialize the block | |
genesis = blocks.genesis({addr: 10**18, addr2: 10**18, add_host: 10**18}) | |
#This is to initialize the contract | |
tx1 = transactions.contract(0,10**12,10000,0,code).sign(key_host) | |
result, contract = processblock.apply_transaction(genesis, tx1) | |
print genesis.get_balance(contract) | |
print 'balance of add %s is: %s' %(addr, str(genesis.get_balance(addr))) | |
print 'balance of add %s is: %s' %(addr2, str(genesis.get_balance(addr2))) | |
print genesis.to_dict() | |
#nonce, gasprice, startgas, to, value, data | |
print str(genesis.timestamp) | |
tx2 = transactions.Transaction(0, 10**12, 10000, contract, 10, serpent.encode_datalist([0, 1, addr, 10**16, 100])).sign(key) | |
result, ans = processblock.apply_transaction(genesis,tx2) | |
print "balance of contract: " , contract, genesis.get_balance(contract) | |
print 'Done creating campaign' | |
print 'balance of add %s is: %s' %(addr, str(genesis.get_balance(addr))) | |
print 'balance of add %s is: %s' %(addr2, str(genesis.get_balance(addr2))) | |
print genesis.to_dict() | |
for i in range(2): | |
genesis.finalize() | |
t = (genesis.timestamp or int(time.time())) + 60 | |
genesis = blocks.Block.init_from_parent(genesis, add_host, '', t) | |
#nonce, gasprice, startgas, to, value, data | |
tx3 = transactions.Transaction(0, 10**12, 10000, contract, 10**17, serpent.encode_datalist([1, 1])).sign(key2) | |
result, ans = processblock.apply_transaction(genesis,tx3) | |
print "balance of contract: " , genesis.get_balance(contract) | |
print result, serpent.decode_datalist(ans) | |
print 'Done crowfunding campaign' | |
print 'balance of add %s is: %s' %(addr, str(genesis.get_balance(addr))) | |
print 'balance of add %s is: %s' %(addr2, str(genesis.get_balance(addr2))) | |
print genesis.to_dict() | |
#################==================== | |
tx4 = transactions.Transaction(1, 10**12, 10000, contract, 10**17, serpent.encode_datalist([1, 2])).sign(key2) | |
result, ans = processblock.apply_transaction(genesis,tx4) | |
print "balance of contract: " , genesis.get_balance(contract) | |
print result, serpent.decode_datalist(ans) | |
print 'Keep sending the money' | |
print 'balance of add %s is: %s' %(addr, str(genesis.get_balance(addr))) | |
print 'balance of add %s is: %s' %(addr2, str(genesis.get_balance(addr2))) | |
print genesis.to_dict() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment