def sequencing_hook(self, db_session, parent_block, parent_sequenced_block): # Update Account according to AccountInfoSnapshot for account_info in AccountInfoSnapshot.query(db_session).filter_by(block_id=self.block.id): account = Account.query(db_session).get(account_info.account_id) if account: account.balance_total = account_info.balance_total account.balance_reserved = account_info.balance_reserved account.balance_free = account_info.balance_free account.nonce = account_info.nonce account.save(db_session)
def create_balance_snapshot(self, block_id, account_id, block_hash=None): if not block_hash: block_hash = self.substrate.get_block_hash(block_id) # Get balance for account try: account_info_data = self.substrate.get_runtime_state( module='System', storage_function='Account', params=['0x{}'.format(account_id)], block_hash=block_hash ).get('result') # Make sure no rows inserted before processing this record AccountInfoSnapshot.query(self.db_session).filter_by(block_id=block_id, account_id=account_id).delete() if account_info_data: account_info_obj = AccountInfoSnapshot( block_id=block_id, account_id=account_id, account_info=account_info_data, balance_free=account_info_data["data"]["free"], balance_reserved=account_info_data["data"]["reserved"], balance_total=account_info_data["data"]["free"] + account_info_data["data"]["reserved"], nonce=account_info_data["nonce"] ) else: account_info_obj = AccountInfoSnapshot( block_id=block_id, account_id=account_id, account_info=None, balance_free=None, balance_reserved=None, balance_total=None, nonce=None ) account_info_obj.save(self.db_session) except ValueError: pass
def serialize_item(self, item): data = item.serialize() # Get balance history account_info_snapshot = AccountInfoSnapshot.query( self.session).filter_by(account_id=item.id).order_by( AccountInfoSnapshot.block_id.desc())[:1000] data['attributes']['balance_history'] = [{ 'name': "Total balance", 'type': 'line', 'data': [[ item.block_id, float((item.balance_total or 0) / 10**settings.SUBSTRATE_TOKEN_DECIMALS) ] for item in reversed(account_info_snapshot)], }] if settings.USE_NODE_RETRIEVE_BALANCES == 'True': substrate = SubstrateInterface( url=settings.SUBSTRATE_RPC_URL, type_registry_preset=settings.TYPE_REGISTRY) if settings.SUBSTRATE_STORAGE_BALANCE == 'Account': storage_call = RuntimeStorage.query(self.session).filter_by( module_id='system', name='Account', ).order_by(RuntimeStorage.spec_version.desc()).first() if storage_call: account_data = substrate.get_storage( block_hash=None, module='System', function='Account', params=[item.id], return_scale_type=storage_call.type_value, hasher=storage_call.type_hasher, metadata_version=settings.SUBSTRATE_METADATA_VERSION) if account_data: data['attributes']['free_balance'] = account_data[ 'data']['free'] data['attributes']['reserved_balance'] = account_data[ 'data']['reserved'] data['attributes'][ 'misc_frozen_balance'] = account_data['data'][ 'miscFrozen'] data['attributes'][ 'fee_frozen_balance'] = account_data['data'][ 'feeFrozen'] data['attributes']['nonce'] = account_data['nonce'] elif settings.SUBSTRATE_STORAGE_BALANCE == 'Balances.Account': storage_call = RuntimeStorage.query(self.session).filter_by( module_id='balances', name='Account', ).order_by(RuntimeStorage.spec_version.desc()).first() if storage_call: account_data = substrate.get_storage( block_hash=None, module='Balances', function='Account', params=[item.id], return_scale_type=storage_call.type_value, hasher=storage_call.type_hasher, metadata_version=settings.SUBSTRATE_METADATA_VERSION) if account_data: data['attributes']['balance_free'] = account_data[ 'free'] data['attributes']['balance_reserved'] = account_data[ 'reserved'] data['attributes'][ 'misc_frozen_balance'] = account_data['miscFrozen'] data['attributes'][ 'fee_frozen_balance'] = account_data['feeFrozen'] data['attributes']['nonce'] = None else: storage_call = RuntimeStorage.query(self.session).filter_by( module_id='balances', name='FreeBalance', ).order_by(RuntimeStorage.spec_version.desc()).first() if storage_call: data['attributes']['free_balance'] = substrate.get_storage( block_hash=None, module='Balances', function='FreeBalance', params=[item.id], return_scale_type=storage_call.type_value, hasher=storage_call.type_hasher, metadata_version=settings.SUBSTRATE_METADATA_VERSION) storage_call = RuntimeStorage.query(self.session).filter_by( module_id='balances', name='ReservedBalance', ).order_by(RuntimeStorage.spec_version.desc()).first() if storage_call: data['attributes'][ 'reserved_balance'] = substrate.get_storage( block_hash=None, module='Balances', function='ReservedBalance', params=[item.id], return_scale_type=storage_call.type_value, hasher=storage_call.type_hasher, metadata_version=settings. SUBSTRATE_METADATA_VERSION) storage_call = RuntimeStorage.query(self.session).filter_by( module_id='system', name='AccountNonce', ).order_by(RuntimeStorage.spec_version.desc()).first() if storage_call: data['attributes']['nonce'] = substrate.get_storage( block_hash=None, module='System', function='AccountNonce', params=[item.id], return_scale_type=storage_call.type_value, hasher=storage_call.type_hasher, metadata_version=settings.SUBSTRATE_METADATA_VERSION) return data