def __init__(self, from_block=0, provider=http_provider): #self.web3 = Web3(IPCProvider(provider)) self.web3 = Web3(HTTPProvider(provider)) self.from_block = from_block self.client = self.conection_stor() self.balance = ClientBalance(settings.balanceurl) #self.web3.personal.unlockAccount('0xe630f8dd65f9be1ef2588da43c83b697a270d12e', "eth2018") self.web3.middleware_stack.inject(geth_poa_middleware, layer=0)
def __init__(self, from_block=0, to_block=-1, db_host=None, db_name=None): self.from_block = from_block self.to_block = to_block self.coinid = coin_id self.qtum = AuthServiceProxy(qtum_server) self.client = ClientBalance(settings.balanceurl) self.db = TablePars(db_host, db_name) self.storge = ClientStorge(settings.storageurl)
def __init__(self, from_block=0, qtum_host=qtum_host_def, db_name=None, db_host=None): self.qtum = AuthServiceProxy(qtum_host) self.from_block = from_block self.client = self.conection_stor() self.balance = ClientBalance(settings.balanceurl) self.db = TablePars(db_host, db_name) self.coinid_put = coin_id_put
def __init__(self, from_block=0, host=None, coin_id=None, db_host=None, db_name=None, balance_host=None): self.from_block = from_block self.connect = Web3(IPCProvider(host)) self.coin_id = coin_id self.connect.middleware_stack.inject(geth_poa_middleware, layer=0) self.db = TablePars(db_host, db_name) self.balance = ClientBalance(settings.balanceurl)
class Parsing(): """ Parsing all transaction in all blocks """ def __init__(self, from_block=0, host=None, coin_id=None, db_host=None, db_name=None, balance_host=None): self.from_block = from_block self.connect = Web3(IPCProvider(host)) self.coin_id = coin_id self.connect.middleware_stack.inject(geth_poa_middleware, layer=0) self.db = TablePars(db_host, db_name) self.balance = ClientBalance(settings.balanceurl) def get_transaction_in_block(self, block=None): # get list transaction in block try: if not block: block = self.from_block block = self.connect.eth.getBlock(block) transactions = block.transactions return transactions except Exception as e: info = "Invalid get_transaction_in_block: {e}".format(e=e) # print(info) def get_raw_transaction(self, transaction_blocks=None): # get raw transaction try: if not transaction_blocks: transaction_blocks = self.get_transaction_in_block() transaction_list = [] for transaction_block in transaction_blocks: try: transaction_data = self.connect.eth.getTransaction( transaction_block) transaction_list += [transaction_data] except Exception as e: info = "Invalid get_raw_transaction: {e}\ntransaction_block: {transaction_block}".format( e=e, transaction_block=transaction_block) # print(info) return transaction_list except Exception as e: info = "Invalid get_raw_transaction: {e}".format(e=e) # print(info) def decode_raw_transaction(self, encoded_datas=None): # decode raw transaction try: if not encoded_datas: try: encoded_datas = self.get_raw_transaction() except: pass for encoded_data in encoded_datas: try: txid_hex = encoded_data["hash"] txid = txid_hex.hex() from_a = encoded_data["from"] to = encoded_data["to"] value = int(encoded_data["value"] * 10**(-10)) # value == 10**18 // **(-10) to_a = to.lower() result = self.db.check_address(address=to_a, coinid=self.coin_id) if result: self.vout_logic(to, value) except Exception as e: pass except: pass def vout_logic(self, to, value): try: result = self.balance.inc_balance(address=to, amount=value, coinid=self.coin_id) except: pass def get_block_count(self): # Get count block return self.connect.eth.blockNumber
class SearchTransaction(): def __init__(self, from_block=0, provider=http_provider): #self.web3 = Web3(IPCProvider(provider)) self.web3 = Web3(HTTPProvider(provider)) self.from_block = from_block self.client = self.conection_stor() self.balance = ClientBalance(settings.balanceurl) #self.web3.personal.unlockAccount('0xe630f8dd65f9be1ef2588da43c83b697a270d12e', "eth2018") self.web3.middleware_stack.inject(geth_poa_middleware, layer=0) def abi_to_params(self, abi, output_types): decode_hex = codecs.getdecoder("hex_codec") encode_hex = codecs.getencoder("hex_codec") data = decode_hex(abi)[0] return decode_abi(output_types, data) def search_transaction(self, txid, vouts): signatures = { '8c3ce5d7': [ 'makeCid({0[0]}, {0[1]}, {0[2]}, {0[3]}, {0[4]})', ['string', 'address', 'string', 'uint256', 'uint256'], self.new_cid ], '65d72416': [ 'newOffer({0[0]}, {0[1]}, {0[2]}, {0[3]}, {0[4]})', ['uint256', 'address', 'uint256', 'uint256', 'string'], self.new_offer ], '715c084b': [ 'sellContent({0[0]}, {0[1]}, {0[2]})', ['uint256', 'address', 'string', 'string'], self.confirm_balance ], 'bbfd5e53': [ 'changeOwner({0[0]}, {0[1]}, {0[2]})', ['uint256', 'address', 'string', 'string'], self.confirm_balance ], '41309af4': [ "newReview({0[0]}, {0[1]}, {0[2]}, {0[3]})", ["uint256", "address", "string"], self.update_review ], } list_data = [] asm_data = vouts hex_address = asm_data[:8] data = asm_data[8:] signatures_list = signatures[hex_address] signatures_list_type = signatures_list[1] decode = self.abi_to_params(data, signatures_list_type) new_decode = self.change_decode(signatures_list_type, decode) data_write = [txid] + new_decode method = signatures_list[2] method_call = method(data_write) list_data += [data] return list_data def change_decode(self, signatures_list_type, decode): decode = list(decode) if "address" in signatures_list_type: index_adr = signatures_list_type.index("address") decode_index_adr = decode[index_adr] new_adr = decode_index_adr[2:] decode[index_adr] = new_adr if "string" in signatures_list_type: index_str = signatures_list_type.index("string") decode_index_str = decode[index_str] new_str = decode_index_str.decode() decode[index_str] = new_str return decode def get_transaction_in_block(self, block=None): # get list transaction in block try: if not block: block = self.from_block block = self.web3.eth.getBlock(block) transactions = block.transactions return transactions except Exception as e: pass def get_raw_transaction(self, transaction_blocks=None): # get raw transaction try: if not transaction_blocks: transaction_blocks = self.get_transaction_in_block() transaction_list = [] for transaction_block in transaction_blocks: try: transaction_data = dict( self.web3.eth.getTransaction(transaction_block)) transaction_block = binascii.hexlify( transaction_block).decode('utf-8') transaction_data["tx_hash"] = transaction_block transaction_list += [transaction_data] except Exception as e: print(e) return transaction_list except: pass def decode_raw_transaction(self, address_smart_contract, encoded_datas=None): # decode raw transaction try: if not encoded_datas: try: encoded_datas = self.get_raw_transaction() except: pass for encoded_data in encoded_datas: try: to = encoded_data["to"] if to == address_smart_contract: txid = encoded_data["tx_hash"] vout = encoded_data["input"][2:] result = self.search_transaction(txid, vout) except: pass except: pass def new_cid(self, data): tx_hash = data[0] cid = data[1] result = self.client.update_users_content(txid=tx_hash, coin_id=coin_id) def new_offer(self, data): txid = data[0] cid = data[1] address = data[2] offer_type = data[3] price = data[4] self.client.update_offer(txid, coin_id=coin_id) result = self.client.mailed_confirm(cid=cid, buyer_address=address, offer_type=offer_type, price=price, coin_id=coin_id) def confirm_balance(self, data): tx_hash = data[0] cid = data[1] address = data[2] result = self.balance.confirm_balance(txid=tx_hash, cid=cid, buyer_address=address, coinid=coin_id) def update_review(self, data): txid = data[0] result = self.client.update_review(txid=txid, coin_id=coin_id) def conection_stor(self): while True: try: cli = ClientStorge(storghost) return cli except: sleep(1) def run(self, from_i, address_smart_contract): while True: getlastblock = self.web3.eth.blockNumber if getlastblock >= from_i: pars = SearchTransaction(from_i) result = pars.decode_raw_transaction(address_smart_contract) print(from_i) from_i += 1 else: sleep(1)
class ParsingBlock(): """ Parsing all transaction in all blocks """ def __init__(self, from_block=0, to_block=-1, db_host=None, db_name=None): self.from_block = from_block self.to_block = to_block self.coinid = coin_id self.qtum = AuthServiceProxy(qtum_server) self.client = ClientBalance(settings.balanceurl) self.db = TablePars(db_host, db_name) self.storge = ClientStorge(settings.storageurl) def block_hash_num(self, block=None): # get block hash try: if not block: block = self.from_block block_hash = self.qtum.getblockhash(block) return block_hash except: pass def get_transaction_in_block(self, block_hash=None): # get list transaction in block try: if not block_hash: block_hash = self.block_hash_num() block = self.qtum.getblock(block_hash) list_tx = block["tx"] return list_tx except: pass def get_raw_transaction(self, transaction_blocks=None): # get raw transaction try: if not transaction_blocks: transaction_blocks = self.get_transaction_in_block() transaction_list = [] for transaction_block in transaction_blocks: try: transaction_data = self.qtum.getrawtransaction( transaction_block) transaction_list += [transaction_data] except JSONRPCException: try: transaction_data = self.qtum.gettransaction( transaction_block) transaction_list += [transaction_data] except JSONRPCException: pass return transaction_list except: pass def transaction_in(self, vin): # parsing input try: list_address = [] for vin_i in vin: try: txid = vin_i["txid"] vout_num = vin_i["vout"] encoded_datas = self.get_raw_transaction([txid]) for i in encoded_datas: try: transaction_data = self.qtum.decoderawtransaction( i) vout_prev = transaction_data["vout"] vout_prev_data = vout_prev[vout_num] value_dec = vout_prev_data["value"] script_pub_key = vout_prev_data["scriptPubKey"] addresses = script_pub_key["addresses"] value_int = int(value_dec * (10**8)) for adr in addresses: list_address += [{adr: value_int}] except: pass except: pass return list_address except: pass def transaction_out(self, vout, txid): # parsing output try: list_address = [False] for vout_i in vout: try: script_pub_key = vout_i["scriptPubKey"] types = script_pub_key["type"] if types == "call" and self.coinid == coin_id: asm = script_pub_key["asm"] asm_split = asm.split() gasLimit = asm_split[1] gasPrice = asm_split[2] asm_data = asm_split[3] hex_address = asm_data[:8] smart_contr_address = asm_split[4] if smart_contr_address in address_smart_contract and hex_address == sign_transfer: data = asm_data[8:] signatures_list_type = ['address', 'uint'] try: decode = self.abi_to_params( data, signatures_list_type) new_decode = self.change_decode( signatures_list_type, decode) address_token = new_decode[0] value_int = new_decode[1] address_token = Qtum.hex_to_qtum_address( address_token, mainnet=mainnet_status) result = self.db.check_address( address=address_token, coinid=coin_id_put) result_keys = result.keys() if "address" in result_keys: update_data_1 = self.client.inc_balance( address_token, value_int, coin_id_put) self.storge.log_transaction( **{ "coinid": coin_id_put, "blocknumber": self.from_block, "blockhash": self.block_hash_num(), "vin": [], "vout": [{ address_token: value_int }], "txid": txid, "gasLimit": gasLimit, "gasPrice": gasPrice }) except Exception as e: # print(e) pass addresses = script_pub_key["addresses"] value = vout_i["value"] value_int = int(value * (10**8)) for adr in addresses: data = self.db.check_address(adr, self.coinid) result_keys = data.keys() if "address" in result_keys: update_data_1 = self.client.inc_balance( adr, value_int, coin_id) list_address[0] = True list_address += [{adr: value_int}] except: pass return list_address except: pass def decode_raw_transaction(self, encoded_datas=None): # decode raw transaction try: if not encoded_datas: encoded_datas = self.get_raw_transaction() for encoded_data in encoded_datas: try: transaction_data = self.qtum.decoderawtransaction( encoded_data) # vin = transaction_data["vin"] vout = transaction_data["vout"] txid = transaction_data["txid"] # self.transaction_in(vin) vout_res = self.transaction_out(vout, txid) if vout_res[0]: vin_res = self.transaction_in(transaction_data["vin"]) self.storge.log_transaction( **{ "coinid": self.coinid, "blocknumber": self.from_block, "blockhash": self.block_hash_num(), "vin": vin_res, "vout": vout_res[1:], "txid": txid }) except: pass except: pass def change_decode(self, signatures_list_type, decode): decode = list(decode) if "address" in signatures_list_type: index_adr = signatures_list_type.index("address") decode_index_adr = decode[index_adr] new_adr = decode_index_adr[2:] decode[index_adr] = new_adr if "string" in signatures_list_type: index_str = signatures_list_type.index("string") decode_index_str = decode[index_str] new_str = decode_index_str.decode() decode[index_str] = new_str return decode def abi_to_params(self, abi, output_types): decode_hex = codecs.getdecoder("hex_codec") encode_hex = codecs.getencoder("hex_codec") data = decode_hex(abi)[0] return decode_abi(output_types, data) def get_block_count(self): # Get count documents in db return self.qtum.getblockcount()
class SearchTransaction(): def __init__(self, from_block=0, qtum_host=qtum_host_def, db_name=None, db_host=None): self.qtum = AuthServiceProxy(qtum_host) self.from_block = from_block self.client = self.conection_stor() self.balance = ClientBalance(settings.balanceurl) self.db = TablePars(db_host, db_name) self.coinid_put = coin_id_put def abi_to_params(self, abi, output_types): decode_hex = codecs.getdecoder("hex_codec") encode_hex = codecs.getencoder("hex_codec") data = decode_hex(abi)[0] return decode_abi(output_types, data) def search_transaction(self, txid, address_smart_contract, vouts): signatures = { '8c3ce5d7': [ 'makeCid({0[0]}, {0[1]}, {0[2]}, {0[3]}, {0[4]})', ['string', 'address', 'string', 'uint256', 'uint256'], self.new_cid ], """'a9059cbb': ["", ['address', 'uint'], self.balance_put, []]""" '65d72416': [ 'newOffer({0[0]}, {0[1]}, {0[2]}, {0[3]}, {0[4]})', ['uint256', 'address', 'uint256', 'uint256', 'string'], self.new_offer ], '715c084b': [ 'sellContent({0[0]}, {0[1]}, {0[2]})', ['uint256', 'address', 'string', 'string'], self.confirm_balance ], 'bbfd5e53': [ 'changeOwner({0[0]}, {0[1]}, {0[2]})', ['uint256', 'address', 'string', 'string'], self.confirm_balance ], '41309af4': [ "newReview({0[0]}, {0[1]}, {0[2]}, {0[3]})", ["uint256", "address", "string"], self.update_review ] } list_data = [] for vout in vouts: script_pub_key = vout["scriptPubKey"] types = script_pub_key["type"] if types == "call": asm = script_pub_key["asm"] asm_split = asm.split() asm_data = asm_split[3] smart_contr_address = asm_split[4] if smart_contr_address in address_smart_contract: hex_address = asm_data[:8] data = asm_data[8:] signatures_list = signatures[hex_address] signatures_list_type = signatures_list[1] try: decode = self.abi_to_params(data, signatures_list_type) new_decode = self.change_decode( signatures_list_type, decode) data_write = [txid] + new_decode if len(signatures_list) > 3: data_write += asm_split[:3] method = signatures_list[2] method_call = method(data_write) list_data += data except Exception as e: print(e) return list_data def change_decode(self, signatures_list_type, decode): decode = list(decode) if "address" in signatures_list_type: index_adr = signatures_list_type.index("address") decode_index_adr = decode[index_adr] new_adr = decode_index_adr[2:] decode[index_adr] = new_adr if "string" in signatures_list_type: index_str = signatures_list_type.index("string") decode_index_str = decode[index_str] new_str = decode_index_str.decode() decode[index_str] = new_str return decode def block_hash_num(self, block=None): # get block hash try: if not block: block = self.from_block block_hash = self.qtum.getblockhash(block) return block_hash except: pass def get_transaction_in_block(self, block_hash=None): # get list transaction in block try: if not block_hash: block_hash = self.block_hash_num() block = self.qtum.getblock(block_hash) list_tx = block["tx"] return list_tx except: pass def get_raw_transaction(self, transaction_blocks=None): # get raw transaction try: if not transaction_blocks: transaction_blocks = self.get_transaction_in_block() transaction_list = [] for transaction_block in transaction_blocks: try: transaction_data = [ self.qtum.getrawtransaction(transaction_block) ] transaction_data += [transaction_block] transaction_list += [transaction_data] except JSONRPCException: try: transaction_data = [ self.qtum.gettransaction(transaction_block) ] transaction_data += [transaction_block] transaction_list += [transaction_data] except JSONRPCException: pass # print(transaction_block) return transaction_list except: pass def decode_raw_transaction(self, address_smart_contract, encoded_datas=None): # decode raw transaction try: if not encoded_datas: try: encoded_datas = self.get_raw_transaction() except: pass for encoded_data in encoded_datas: try: transaction_data = self.qtum.decoderawtransaction( encoded_data[0]) txid = encoded_data[1] vout = transaction_data["vout"] result = self.search_transaction(txid, address_smart_contract, vout) except: pass except: pass def balance_put(self, data): txid = data[0] address = data[1] address = Qtum.hex_to_qtum_address(address, mainnet=mainnet_status) amount = data[2] gasLimit = data[4] gasPrice = data[5] result = self.db.check_address(address, self.coinid_put) if result: update_data_1 = self.balance.inc_balance(address, amount, self.coinid_put) self.client.log_transaction( **{ "coinid": self.coinid_put, "blocknumber": self.from_block, "blockhash": self.block_hash_num(), "vin": [], "vout": [{ address: amount }], "txid": txid, "gasLimit": gasLimit, "gasPrice": gasPrice }) def new_cid(self, data): tx_hash = data[0] cid = data[1] result = self.client.update_users_content(txid=tx_hash, coin_id=coin_id) def new_offer(self, data): txid = data[0] cid = data[1] address = data[2] offer_type = data[3] price = data[4] self.client.update_offer(txid, coin_id=coin_id) mail = self.client.mailed_confirm(cid=cid, buyer_address=address, offer_type=offer_type, price=price, coin_id=coin_id) def confirm_balance(self, data): tx_hash = data[0] cid = data[1] address = data[2] result = self.balance.confirm_balance(txid=tx_hash, cid=cid, buyer_address=address, coinid=coin_id) def update_review(self, data): txid = data[0] self.client.update_review(txid, coin_id=coin_id) def conection_stor(self): while True: try: cli = ClientStorge(storghost) return cli except: sleep(1)