def buy_shares_check(tx, txs, out, DB): #make sure that we can only buy the shares of undecided markets. if not transactions.signature_check(tx): out[0]+='signature check' return False a=copy.deepcopy(tx) a.pop('signatures') half_way=tools.make_half_way(a) if tools.det_hash(half_way)>custom.buy_shares_target: out[0]+='insufficient POW' return False if not E_check(tx, 'buy', list): out[0]+='buy error' return False if not E_check(tx, 'PM_id', [str, unicode]): out[0]+='pm id error' return False pm=tools.db_get(tx['PM_id'], DB) if 'decisions' not in pm: out[0]+='that is not a prediction market yet' return False if len(tx['buy'])!=len(pm['shares_purchased']): out[0]+='buy length error' return False stop=True for i in tx['buy']: if i!=0: stop=False if stop: out[0]+='you need to buy a non-zero amount of at least one share' return False if 'price_limit' in tx: price = txs_tools.cost_to_buy_shares(tx) if price>tx['price_limit']: out[0]+='that is outside the price limit for that tx '+str(price) + ' is bigger than ' +str(tx) return False for purchase in tx['buy']: if type(purchase)!=int: return False for i in range(len(tx['buy'])): if tx['buy'][i]+pm['shares_purchased'][i]<0: out[0]+='PM cannot have negative shares' return False if not txs_tools.fee_check(tx, txs, DB): out[0]+='fee check error' return False for dec in pm['decisions']: decision = tools.db_get(dec, DB) bad=True if decision['state'] not in ['yes', 'no']: bad=False if bad: out[0]+='this PM is already expired. you cannot buy shares.' return False return True
def common_buy_shares(tx, num_states, brainwallet): privkey = tools.det_hash(brainwallet) pubkey = tools.privtopub(privkey) address = tools.make_address([pubkey], 1) tx['pubkeys'] = [pubkey] tx['count'] = tools.count(address, {}) cost = txs_tools.cost_to_buy_shares(tx) tx['price_limit'] = int(cost * 1.01) + 1 print( 'now for a little proof of work. This may take several minutes. The purpose of this pow is to make it more difficult for a front runner to steal your trade.' ) tx = tools.unpackage(tools.package(tx)) tx = tools.POW(tx) tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)] print('tx for copy/pasting into pushtx: ' + tools.package(tx).encode('base64')) return tx
def trade_shares(DB, args): #args = [ PM_id, buy ] privkey = tools.db_get('privkey') pubkey = tools.privtopub(privkey) address = tools.make_address([pubkey], 1) tx = { 'type': 'buy_shares', 'PM_id': args[0], 'buy': csv2vec(args[1]), 'pubkeys': [pubkey], 'count': tools.count(address, {}) } cost = txs_tools.cost_to_buy_shares(tx) tx['price_limit'] = int(cost * 1.01) + 1 tx = tools.unpackage(tools.package(tx)) tx = tools.POW(tx) tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)] return easy_add_transaction(tx, DB, privkey)
def buy_shares(tx, DB, add_block): address = addr(tx) acc = tools.db_get(address, DB) adjust_int(['count'], address, 1, DB, add_block) cost=txs_tools.cost_to_buy_shares(tx) fee=int(abs(cost*0.01)) adjust_int(['fees'], tx['PM_id'], fee, DB, add_block) adjust_int(['amount'], address, -fee, DB, add_block) adjust_int(['amount'], address, -cost, DB, add_block) all_zeros=[] for i in tx['buy']: all_zeros.append(0) dic={tx['PM_id']:all_zeros} if tx['PM_id'] not in acc['shares']:#initialize to zero adjust_dict(['shares'], address, False, dic, DB, add_block) for i in range(len(tx['buy'])): adjust_int(['shares_purchased', i], tx['PM_id'], tx['buy'][i], DB, add_block) adjust_int(['shares', tx['PM_id'], i], address, tx['buy'][i], DB, add_block) acc = tools.db_get(address, DB) if acc['shares'][tx['PM_id']]==all_zeros: adjust_dict(['shares'], address, True, dic, DB, add_block)
def build_buy_shares(): tx={'type':'buy_shares', 'PM_id':str(raw_input('What is the unique name for this prediction market?\n>'))} num_states=int(raw_input('how many states does this pm have?\n>')) tx['buy']=[] for i in range(num_states): tx['buy'].append(int(raw_input('how many shares do you want to buy of state '+str(i)+'? To sell states, use negative numbers.\n>'))) brainwallet=str(raw_input('What is your brainwallet\n>')) privkey=tools.det_hash(brainwallet) pubkey=tools.privtopub(privkey) address=tools.make_address([pubkey], 1) tx['pubkeys']=[pubkey] tx['count'] = tools.count(address, {}) cost=txs_tools.cost_to_buy_shares(tx) tx['price_limit']=int(cost*1.01) print('now for a little proof of work. This may take several minutes. The purpose of this pow is to make it more difficult for a front runner to steal your trade.') tx=tools.unpackage(tools.package(tx)) tx=tools.POW(tx) tx['signatures']=[tools.sign(tools.det_hash(tx), privkey)] print('tx for copy/pasting into pushtx: '+tools.package(tx).encode('base64')) return tx
def buy_shares(tx, DB): address = addr(tx) acc = tools.db_get(address, DB) adjust_int(['count'], address, 1, DB) #tx={'buy':[10, -5, 0, 0, 0], PM_id:''} this would buy 10 shares of the first state, and sell 5 of the second. cost=txs_tools.cost_to_buy_shares(tx, DB) fee=int(abs(cost*0.01)) adjust_int(['fees'], tx['PM_id'], fee, DB) adjust_int(['amount'], address, -fee, DB) adjust_int(['amount'], address, -cost, DB) all_zeros=[] for i in tx['buy']: all_zeros.append(0) dic={tx['PM_id']:all_zeros} if tx['PM_id'] not in acc['shares']:#initialize to zero adjust_dict(['shares'], address, False, dic, DB) for i in range(len(tx['buy'])): adjust_int(['shares_purchased', i], tx['PM_id'], tx['buy'][i], DB) adjust_int(['shares', tx['PM_id'], i], address, tx['buy'][i], DB) acc = tools.db_get(address, DB) if acc['shares'][tx['PM_id']]==all_zeros: adjust_dict(['shares'], address, True, dic, DB)
def price(DB, args): #args=[pm_id, [1,100,0,-20]] tx = {'PM_id': args[0], 'buy': csv2vec(args[1])} return txs_tools.cost_to_buy_shares(tx) * 1.01