def get_transactions(height, end, date): begin_height = height block = blockexplorer.get_block_height(height)[0] curr_date = time.strftime('%Y-%m-%d', time.localtime(block.received_time)) new_date = curr_date date_transactions = [] # search between specified range for transactions while height < end: try: print(end - height) height = height + 1 for block in blockexplorer.get_block_height(height): new_date = time.strftime('%Y-%m-%d', time.localtime(block.received_time)) print(new_date) # keep list of all transactions date_transactions = date_transactions + block.transactions except: time.sleep(10) height = height - 1 input_nodes = nx.Graph() #print(date_transactions[0:5]) lines = [] lines.append('tx_hash,in_out,address,time,value') for trns in date_transactions: # if block doesn't have any inputs, skip curr_nodes = [] for inpt in trns.inputs: try: input_nodes.add_node(inpt.address) #add edges between all nodes in transaction input_nodes.add_edges_from([(inpt.address, out_addr) for out_addr in curr_nodes]) curr_nodes.append(inpt.address) #print('input' + str(inpt.address)) row = str(trns.hash) + ',' + 'in,' + str( inpt.address) + ',' + str(trns.time) + ',' + str( inpt.value) #print(row) lines.append(row) except AttributeError: pass for outpt in trns.outputs: try: row = str(trns.hash) + ',' + 'out,' + str( outpt.address) + ',' + str(trns.time) + ',' + str( outpt.value) lines.append(row) # if any attributes aren't found, as is the case with coinbase transactions, discard row except AttributeError: pass print(date) with open('transactions_' + str(date) + '.csv', 'w') as file_out: file_out.write('\n'.join(lines))
def load_num_blocks(block_height, num_blocks): """ Function loads num blocks starting with block of height block_height in blockchain :param block_height: starting block :param num_blocks: number of blocks to parse :return: void """ curr_block_height = block_height for i in range(num_blocks): # print("loading block of height %d" % curr_block_height) try: message = "loading block of height %d" % curr_block_height print(message) block = blockexplorer.get_block_height(curr_block_height)[0] wait_and_load_no_log(block, 60, 1) if block.n_tx < 50: print("sleeping...") time.sleep(10) curr_block_height += 1 except Exception as e: print("error in retrieving block: %s" % str(e)) print("trying again...")
def results(query): search_results = None type = None if len(query) > 63: try: search_results = be.get_block(query, api_code=app.api_code) type = 'Block Info' except APIException as e: print('An API error has occurred ' + str(e)) elif len(query) > 33: try: search_results = be.get_address(query) type = 'Address Info' except APIException as e: print('Error ' + str(e)) else: type = 'Block Height Info' try: try: n = int(query) search_results = be.get_block_height(n, api_code=app.api_code) except (ValueError, TypeError) as err: search_results = str('Invalid query expression. ' + str(err)) flash(search_results, 'danger') return redirect(url_for('index')) except APIException as e: print('Error ' + str(e)) return render_template('results.html', query=query, search_results=search_results, type=type, current_time=datetime.now().strftime('LLL'))
def load_from_block(block_height, log_file): """ Function moves forward and loads blocks one by one, starting from given block height. In the case that a block contains less than 50 transactions, functions sleeps for 10 seconds to avoid spamming blockexplorer api endpoint :param block_height: :return: void """ curr_block_height = block_height while True: # print("loading block of height %d" % curr_block_height) try: message = "loading block of height %d" % curr_block_height write_to_file(message, log_file) block = blockexplorer.get_block_height(curr_block_height)[0] wait_and_load(block, 60, 1, log_file) if block.n_tx < 50: write_to_file("sleeping...", log_file) time.sleep(10) curr_block_height += 1 except Exception as e: write_to_file("error in retrieving block: %s" % str(e), log_file) write_to_file("trying again...", log_file)
def get_time(height): try: return blockexplorer.get_block_height(height)[0].received_time except KeyError: return get_time(height + 1) except APIException: time.sleep(20) return get_time(height)
def handle(self, *args, **options): import blockchain blockchain.util.TIMEOUT = 60 Last = None CurrencyInstance = Currency.objects.get(title="BTC") AccountList = PoolAccounts.objects.filter(currency_id=2) OwnAccounts = {} print "get all addresses" for i in AccountList: if i.address == '' or i.address is None or i.user is None: continue OwnAccounts[i.address] = i.user while True: try: Last = VolatileConsts.objects.get(Name="last_btc_process_block") except: print "there is no previouse info" sys.exit(0) PrevValue = int(Last.Value) time.sleep(1) NewBlock = PrevValue + 1 LatestBlock = blockexplorer.get_latest_block() print "start process new block %i %i" % (NewBlock, LatestBlock.height) if LatestBlock.height == NewBlock or LatestBlock.height < NewBlock: print "there is uncofirmed block do nothing" sys.exit(0) # try: for Block in blockexplorer.get_block_height(NewBlock): for Trans in Block.transactions: for output in Trans.outputs: if OwnAccounts.has_key(output.address): if is_out(Trans.inputs, OwnAccounts): break else: Decimal = sato2Dec(output.value) try: TransObj = CryptoTransfers.objects.get(crypto_txid=Trans.hash) print "trans %s is existed to %s amnt %s %i" % ( TransObj.crypto_txid, TransObj.user.username, TransObj.amnt, TransObj.id) except CryptoTransfers.DoesNotExist: print "trans %s to save %s amnt %s" % (Trans.hash, output.address, Decimal) TransObj = CryptoTransfers(crypto_txid=Trans.hash, status="processing", amnt=Decimal, currency=CurrencyInstance, account=output.address, user=OwnAccounts[output.address], confirms=0 ) TransObj.sign_record(crypton.settings.CRYPTO_SALT) TransObj.save() Last.Value = NewBlock Last.save()
def pullBlock(block, errors=0): try: return blockexplorer.get_block_height(block, api_code=load_api_code()) except Exception as e: print("Exception pulling... sleeping " + str(1 + errors) + "seconds. Details below:") print(e) time.sleep(1 + errors) return pullBlock(block, errors + 1)
def get_latest_saved_block(): with open(FILENAME, 'r') as f: reader = csv.reader(f) for row in reader: pass # row has contents of the last line latest_block_height = row[0] blocks = blockexplorer.get_block_height(latest_block_height, api_code = API_CODE) for block in blocks: if block.main_chain: return block return None
def _download_block(height): from blockchain import blockexplorer, util util.TIMEOUT = 30 # time out after 5 seconds block = blockexplorer.get_block_height(height)[0] n_tx = len([tx for tx in block.transactions]) n_outs = len([txo for tx in block.transactions for txo in tx.outputs]) print( f"- Block with height {height} arrived at {time.ctime(block.time)} with " f"{n_tx} transactions and a total of {n_outs} outputs.", flush=True) return {"t": block.time, "n_tx": n_tx, "n_outs": n_outs}
def tx(hash): try: tx = be.get_tx(hash, api_code=app.api_code) blk = be.get_block_height(tx.block_height, api_code=app.api_code) except APIException as e: message = 'There has been an API Error.' flash(message, 'danger') return redirect(url_for('index')) return render_template('transaction.html', tx=tx, block=blk, current_time=datetime.now().strftime('LLL'))
def get_latest_saved_block(): with open(FILENAME, 'r') as f: reader = csv.reader(f) for row in reader: pass # row has contents of the last line latest_block_height = row[0] blocks = blockexplorer.get_block_height(latest_block_height, api_code=API_CODE) for block in blocks: if block.main_chain: return block return None
def blockexplorer(self): block = blockexplorer.get_block( '000000000000000016f9a2c3e0f4c1245ff24856a79c34806969f5084f410680') tx = blockexplorer.get_tx( 'd4af240386cdacab4ca666d178afc88280b620ae308ae8d2585e9ab8fc664a94') blocks = blockexplorer.get_block_height(2570) address = blockexplorer.get_address( '1HS9RLmKvJ7D1ZYgfPExJZQZA1DMU3DEVd') xpub = None #blockexplorer.get_xpub('xpub6CmZamQcHw2TPtbGmJNEvRgfhLwitarvzFn3fBYEEkFTqztus7W7CNbf48Kxuj1bRRBmZPzQocB6qar9ay6buVkQk73ftKE1z4tt9cPHWRn') addresses = None # blockexplorer.get_multi_address('1HS9RLmKvJ7D1ZYgfPExJZQZA1DMU3DEVd', 'xpub6CmZamQcHw2TPtbGmJNEvRgfhLwitarvzFn3fBYEEkFTqztus7W7CNbf48Kxuj1bRRBmZPzQocB6qar9ay6buVkQk73ftKE1z4tt9cPHWRn') outs = blockexplorer.get_unspent_outputs( '1HS9RLmKvJ7D1ZYgfPExJZQZA1DMU3DEVd') latest_block = blockexplorer.get_latest_block() txs = blockexplorer.get_unconfirmed_tx() blocks_by_name = None #blockexplorer.get_blocks(pool_name = 'Discus Fish')
def get_transactions(height): fail = True while fail: try: block = blockexplorer.get_block_height(height)[0] fail = False except: fail = True curr_date = time.strftime('%Y-%m-%d', time.localtime(block.received_time)) new_date = curr_date date_transactions = [] #while curr_date == new_date: while True: height = height + 1 for block in blockexplorer.get_block_height(height): new_date = time.strftime('%Y-%m-%d-%H-%m-%s', time.localtime(block.received_time)) print(block.received_time) #dt = "%d-%02d-%02d-%02d-%02d-%02d"%(block_datetime.year, block_datetime.month, block_datetime.day, block_datetime.hour, block_datetime.minute, block_datetime.second) #field specification: ["in", transaction_key, referent_transaction_key, index, public_key, date] date_transactions = date_transactions + block.transactions #print(len(date_transactions)) return
def get_block_height(height): try: blocks = be.get_block_height(height, api_code=app.api_code) if blocks: block_height = blocks[0].height stats = get_stats() block_count = len(blocks) except APIException as e: print('Error ' + str(e)) return render_template('blocks_by_height.html', blocks=blocks, stats=stats, block_count=block_count, block_height=block_height, current_time=datetime.now().strftime('LLL'))
def compressionLab(encodedFile): fileReceived = encodedFile; byteSize = 4 initLoop = 0 endLoop = byteSize length = len(fileReceived) / byteSize print(length) blockchainCoordinates = [None] * length latestBlockHeight = (blockexplorer.get_latest_block()).height for blockElement in range(latestBlockHeight, 0, -1): blocks = blockexplorer.get_block_height(blockElement) print(blockElement) for element in range(0, length): blockchainCoordinates[element] = turnIntoTransactions(latestBlockHeight, blocks, fileReceived[initLoop:endLoop]) initLoop += byteSize endLoop += byteSize if endLoop >= len(fileReceived): initLoop = 0 endLoop = byteSize
cur.execute(query) conn.commit() cur.close() conn.close() # block heights lowerBlock = 399720 upperBlock = 399820 for thisHeight in range(lowerBlock, upperBlock + 1): blockDataToPostgres(thisHeight) #%% testFlag = 0 if testFlag == 1: blocks = blockexplorer.get_block_height(399455) block = blocks[0] txIndex = 10 tx = vars(block.transactions[txIndex]) allInput = [ vars(i) for i in tx['inputs']] allOutput = [ vars(i) for i in tx['outputs']]
def get_bit(height): blks = blex.get_block_height(height) if len(blks) == 1: return blks[0].bits else: return None
def to_btc(val): return val/100000000 datas = [] alturas = [] volumes_btc = [] volume_dolar = [] carteiras = [] qtd = 300 for x in range(0, qtd): block = blockexplorer.get_block_height(x) altura = block[0].height print(altura) alturas.append(altura) for transaction in block[0].transactions: timestamp = transaction.time data = datetime.datetime.fromtimestamp(timestamp) data = data.strftime('%Y-%m-%d %H:%M:%S') datas.append(data) # isso aqui é pra pegar apenas as transaçoes válidas if transaction.block_height != -1: for saida in transaction.outputs: endereco = saida.address carteiras.append(endereco) value = to_btc(saida.value) volumes_btc.append(value)
def get_size(height): blks = blex.get_block_height(height) if len(blks) == 1: return blks[0].size else: return None
def get_no_tx(height): blks = blex.get_block_height(height) if len(blks) == 1: return blks[0].n_tx else: return None
print "there is no previouse info" sys.exit(0) PrevValue = int(Last.Value) NewBlock = PrevValue + 1 LatestBlock = blockexplorer.get_latest_block() NewBlock = PrevValue + 1 if LatestBlock.height == NewBlock: print "start process new block %i %i" % (NewBlock, LatestBlock.height) if LatestBlock.height == NewBlock or LatestBlock.height < NewBlock: print "there is uncofirmed block do nothing" sys.exit(0) # try: for Block in blockexplorer.get_block_height(NewBlock): print "block get" for Trans in Block.transactions: for output in Trans.outputs : if OwnAccounts.has_key( output.address): if is_out(Trans.inputs, OwnAccounts ): break else: Decimal = sato2Dec(output.value) try : TransObj = CryptoTransfers.objects.get(crypto_txid = Trans.hash) print "trans %s is existed to %s amnt %s" % (TransObj.crypto_txid, TransObj.user.username, TransObj.amnt) except CryptoTransfers.DoesNotExist: print "trans %s to save %s amnt %s" % (Trans.hash, output.address, Decimal) TransObj = CryptoTransfers(crypto_txid = Trans.hash, status="processing",
def load_single_block(block_hash): print("loading single block...") block2 = blockexplorer.get_block(block_hash) block = blockexplorer.get_block_height(559684)[0] print("block hash: " + str(block2.hash)) print("block hash from height: " + str(block.hash))
def blockDataToPostgres(blockHeight): conn = psycopg2.connect("dbname=mettinger user=postgres") cur = conn.cursor() block = blockexplorer.get_block_height(blockHeight)[0] txs = block.transactions numTxs = len(txs) # coinbase transaction? for i in range(1,numTxs): thisTransaction = vars(txs[i]) # process inputs inputs = thisTransaction['inputs'] numInputs = len(inputs) for j in range(numInputs): thisInput = vars(inputs[j]) tx_in_n = j prev_tx_index = thisInput['tx_index'] prev_tx_n = thisInput['n'] tx_index = thisTransaction['tx_index'] unlocking_script = thisInput['script'] address = thisInput['address'] amount = thisInput['value'] query = ''' INSERT INTO transactions_in (block_height, tx_in_n, prev_tx_index, prev_tx_n, tx_index, unlocking_script, address, amount) VALUES (%s, %s, %s, %s, %s, '%s', '%s',%s) ''' % (blockHeight, tx_in_n, prev_tx_index, prev_tx_n, tx_index, unlocking_script, address, amount) cur.execute(query) # process outpouts outputs = thisTransaction['outputs'] numOutputs = len(outputs) for j in range(numOutputs): thisOutput = vars(outputs[j]) tx_out_n = thisOutput['n'] amount = thisOutput['value'] tx_index = thisTransaction['tx_index'] script = thisOutput['script'] address = thisOutput['address'] query = ''' INSERT INTO transactions_out (block_height, tx_out_n, amount, tx_index, script, address) VALUES (%s, %s, %s, %s,'%s', '%s') ''' % (blockHeight, tx_out_n, amount, tx_index, script, address) cur.execute(query) conn.commit() cur.close() conn.close()
import os from blockchain import blockexplorer import pandas as pd blocks_heights = range(100) if not os.path.isfile('./cash_flows.pkl'): # get blocks and corresponding transaction hash list blocks = [] for block_height in blocks_heights: blocks += blockexplorer.get_block_height(block_height) transactions_hash_list = [] for block in blocks: transactions_hash_list += [tx.hash for tx in block.transactions] # get corresponding transactions data transactions = [] for tx_id in transactions_hash_list: transactions.append(blockexplorer.get_tx(tx_id)) # save corresponding cash flows per user cash_flows = [] for transaction in transactions: # get transaction cash flows for input_ in transaction.inputs: if hasattr(input_, 'address'): cash_flows.append( [input_.address, transaction.time, -input_.value]) for output_ in transaction.outputs:
from blockchain import blockexplorer import time import numpy as np all_addresses = set() #from 24/May to ... for block_count in range(577477, 578719): try: bl = blockexplorer.get_block_height(block_count)[0] print block_count, bl.time, bl.hash for tx in bl.transactions[1:]: if len(tx.inputs) < 50 and len(tx.outputs) < 50 and sum( o.value for o in tx.outputs) > 10**6: for i in tx.inputs: all_addresses.add(i.address) #be gentle with blockchain.com... time.sleep(1) except: print 'Erro!' print len(all_addresses) np.save('suspect_wallets.npy', [a for a in all_addresses])
from pymongo import MongoClient import time client = MongoClient('localhost', 27017) db = client.blockchain_db if __name__ == '__main__': accounts = {} transactions_limit = 1000000 db.transactions.drop() height = blockexplorer.get_latest_block().height count_transactions = 0 while count_transactions < transactions_limit: blocks = blockexplorer.get_block_height(height) for block in blocks: print(f'Height: {block.height}') for transaction in block.transactions: print(f'Transaction: {count_transactions+1}') json_transaction = transaction.__dict__ json_transaction['inputs'] = [ input.__dict__ for input in json_transaction['inputs'] if hasattr(input, 'address') ] json_transaction['outputs'] = [ output.__dict__ for output in json_transaction['outputs'] if output.address is not None ] db.transactions.insert_one(json_transaction) count_transactions += 1
def set_time(step=3): # blocks = blockexplorer.get_blocks() # # for block in blocks: # value = time.localtime(block.time) # print (time.strftime("%Y-%m-%d %H:%M:%S", value)) # print (time.strftime("%a %b %d %H:%M:%S %Y", value)) current_time = time.localtime(time.time()) print('Now is ' + time.strftime("%Y-%m-%d %H:%M:%S", current_time)) # str_start_time = input('Please enter the start time') # # str_end_time = input('Please enter the end time') str_start_time_init = '2017-02-10 15:10:00' str_end_time_init = '2017-02-10 16:30:00' print('The time: ', str_start_time_init + ' - ', str_end_time_init) tuple_start_time_whole = time.strptime(str_start_time_init, '%Y-%m-%d %H:%M:%S') tuple_end_time_whole = time.strptime(str_end_time_init, '%Y-%m-%d %H:%M:%S') unix_start_time_whole = int(time.mktime(tuple_start_time_whole)) unix_end_time_whole = int(time.mktime(tuple_end_time_whole)) str_start_time = str_start_time_init[0:10] + ' 17:25:23' str_end_time = str_end_time_init[0:10] + ' 17:25:23' tuple_start_time = time.strptime(str_start_time, '%Y-%m-%d %H:%M:%S') tuple_end_time = time.strptime(str_end_time, '%Y-%m-%d %H:%M:%S') unix_start_time = int(time.mktime(tuple_start_time)) unix_end_time = int(time.mktime(tuple_end_time)) print(unix_end_time, unix_start_time) blocks = blockexplorer.get_blocks(time=(unix_start_time * 1000 + 455)) # for block in blocks: # value = time.localtime(block.time) # print(block.time) # print (time.strftime("%Y-%m-%d %H:%M:%S", value)) # print (time.strftime("%a %b %d %H:%M:%S %Y", value)) # # # blocks = blockexplorer.get_blocks() # # # # for block in blocks: # # print (block.time) # # # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(1487841923))) start_height = blocks[0].height block = blocks[0] if block.time < unix_start_time_whole: while (block.time < unix_start_time_whole): start_height += step blockss = blockexplorer.get_block_height(start_height) block = blockss[0] print(block.time, unix_start_time_whole) else: while (block.time > unix_start_time_whole): start_height += step blockss = blockexplorer.get_block_height(start_height) block = blockss[0] print(block.time, unix_start_time_whole) print(start_height) blocks = blockexplorer.get_blocks(time=(unix_end_time * 1000 + 455)) end_height = blocks[0].height block = blocks[0] if block.time < unix_end_time_whole: while (block.time < unix_end_time_whole): end_height += step blockss = blockexplorer.get_block_height(end_height) block = blockss[0] print(block.time, unix_end_time_whole) else: while (block.time > unix_end_time_whole): end_height += step blockss = blockexplorer.get_block_height(end_height) block = blockss[0] print(block.time, unix_end_time_whole) print(end_height) return start_height, end_height