def get_validator_daily_stats( self, validator_index: int, from_ts: Optional[Timestamp] = None, to_ts: Optional[Timestamp] = None, ) -> List[ValidatorDailyStats]: """Gets all DB entries for daily staking stats of a validator""" cursor = self.db.conn.cursor() querystr = ('SELECT validator_index,' ' timestamp,' ' start_usd_price,' ' end_usd_price,' ' pnl,' ' start_amount,' ' end_amount,' ' missed_attestations,' ' orphaned_attestations,' ' proposed_blocks,' ' missed_blocks,' ' orphaned_blocks,' ' included_attester_slashings,' ' proposer_attester_slashings,' ' deposits_number,' ' amount_deposited ' ' FROM eth2_daily_staking_details ' ' WHERE validator_index = ? ') querystr, bindings = form_query_to_filter_timestamps( query=querystr, timestamp_attribute='timestamp', from_ts=from_ts, to_ts=to_ts, ) results = cursor.execute(querystr, (validator_index, *bindings)) return [ValidatorDailyStats.deserialize_from_db(x) for x in results]
def get_eth2_deposits( self, from_ts: Optional[Timestamp] = None, to_ts: Optional[Timestamp] = None, address: Optional[ChecksumEthAddress] = None, ) -> List[Eth2Deposit]: """Returns a list of Eth2Deposit filtered by time and address""" cursor = self.db.conn.cursor() query = ( 'SELECT ' 'tx_hash, ' 'tx_index, ' 'from_address, ' 'timestamp, ' 'pubkey, ' 'withdrawal_credentials, ' 'amount, ' 'usd_value ' 'FROM eth2_deposits ' ) # Timestamp filters are omitted, done via `form_query_to_filter_timestamps` filters = [] if address is not None: filters.append(f'from_address="{address}" ') if filters: query += 'WHERE ' query += 'AND '.join(filters) query, bindings = form_query_to_filter_timestamps(query, 'timestamp', from_ts, to_ts) results = cursor.execute(query, bindings) return [Eth2Deposit.deserialize_from_db(deposit_tuple) for deposit_tuple in results]
def get_ledger_actions( self, from_ts: Optional[Timestamp], to_ts: Optional[Timestamp], location: Optional[Location], ) -> List[LedgerAction]: cursor = self.db.conn.cursor() query = ( 'SELECT identifier,' ' timestamp,' ' type,' ' location,' ' amount,' ' asset,' ' link,' ' notes FROM ledger_actions ' ) if location is not None: query += f'WHERE location="{location.serialize_for_db()}" ' query, bindings = form_query_to_filter_timestamps(query, 'timestamp', from_ts, to_ts) results = cursor.execute(query, bindings) actions = [] for result in results: try: action = LedgerAction( identifier=result[0], timestamp=deserialize_timestamp(result[1]), action_type=deserialize_ledger_action_type_from_db(result[2]), location=deserialize_location_from_db(result[3]), amount=deserialize_asset_amount(result[4]), asset=Asset(result[5]), link=result[6], notes=result[7], ) except DeserializationError as e: self.msg_aggregator.add_error( f'Error deserializing Ledger Action from the DB. Skipping it.' f'Error was: {str(e)}', ) continue except UnknownAsset as e: self.msg_aggregator.add_error( f'Error deserializing Ledger Action from the DB. Skipping it. ' f'Unknown asset {e.asset_name} found', ) continue actions.append(action) return actions
def test_form_query_to_filter_timestamps( query_in, timestamp_attribute, from_ts, to_ts, expected_query_out, expected_bindings, ): query_out, bindings = form_query_to_filter_timestamps( query=query_in, timestamp_attribute=timestamp_attribute, from_ts=from_ts, to_ts=to_ts, ) assert query_out == expected_query_out assert bindings == expected_bindings