index.py (1450B)
1 # standard imports 2 import logging 3 import json 4 5 # externa imports 6 from hexathon import ( 7 uniform as hex_uniform, 8 strip_0x, 9 ) 10 11 logg = logging.getLogger(__name__) 12 13 14 class AddressIndex: 15 16 def __init__(self, rpc, store): 17 self.rpc = rpc 18 self.store = store 19 self.addresses = {} 20 21 22 def load_address_tx(self, address): 23 address = hex_uniform(strip_0x(address)) 24 if self.addresses.get(address) == None: 25 self.addresses[address] = [] 26 txs = {} 27 for tx_hash in self.store.get_address_tx(address): 28 j = self.store.get_tx(tx_hash) 29 tx = json.loads(j) 30 logg.debug('tx {}'.format(tx)) 31 32 block_number = None 33 try: 34 block_number = int(tx['block_number'], 16) 35 except: 36 block_number = int(tx['block_number']) 37 38 tx_index = None 39 try: 40 tx_index = int(tx['transaction_index'], 16) 41 except: 42 tx_index = int(tx['transaction_index']) 43 44 k = '{}.{}'.format(block_number, tx_index) 45 46 txs[float(k)] = tx 47 48 ks = list(txs.keys()) 49 ks.sort() 50 ks.reverse() 51 for k in ks: 52 self.addresses[address].append(txs[k]) 53 54 return len(ks) 55 56 57 def get_address(self, address): 58 address = hex_uniform(strip_0x(address)) 59 return self.addresses[address]