def deserialize_from_db( cls, trade_tuple: AMMSwapDBTuple, ) -> 'AMMSwap': """Turns a tuple read from DB into an appropriate Swap. May raise a DeserializationError if something is wrong with the DB data Trade_tuple index - Schema columns ---------------------------------- 0 - tx_hash 1 - log_index 2 - address 3 - from_address 4 - to_address 5 - timestamp 6 - location 7 - is_token0_unknown 8 - token0_address 9 - token0_symbol 10 - token0_name 11 - token0_decimals 12 - is_token1_unknown 13 - token1_address 14 - token1_symbol 15 - token1_name 16 - token1_decimals 17 - amount0_in 18 - amount1_in 19 - amount0_out 20 - amount1_out """ address = deserialize_ethereum_address(trade_tuple[2]) from_address = deserialize_ethereum_address(trade_tuple[3]) to_address = deserialize_ethereum_address(trade_tuple[4]) is_token0_unknown = trade_tuple[7] is_token1_unknown = trade_tuple[12] token0: Union[EthereumToken, UnknownEthereumToken] token1: Union[EthereumToken, UnknownEthereumToken] if is_token0_unknown: token0 = deserialize_unknown_ethereum_token_from_db( ethereum_address=trade_tuple[8], symbol=trade_tuple[9], name=trade_tuple[10], decimals=trade_tuple[11], ) else: token0 = deserialize_ethereum_token_from_db( identifier=trade_tuple[9]) if is_token1_unknown: token1 = deserialize_unknown_ethereum_token_from_db( ethereum_address=trade_tuple[13], symbol=trade_tuple[14], name=trade_tuple[15], decimals=trade_tuple[16], ) else: token1 = deserialize_ethereum_token_from_db( identifier=trade_tuple[14]) return cls( tx_hash=trade_tuple[0], log_index=trade_tuple[1], address=address, from_address=from_address, to_address=to_address, timestamp=deserialize_timestamp(trade_tuple[5]), location=deserialize_location_from_db(trade_tuple[6]), token0=token0, token1=token1, amount0_in=deserialize_asset_amount(trade_tuple[17]), amount1_in=deserialize_asset_amount(trade_tuple[18]), amount0_out=deserialize_asset_amount(trade_tuple[19]), amount1_out=deserialize_asset_amount(trade_tuple[20]), )
def deserialize_from_db( cls, event_tuple: LiquidityPoolEventDBTuple, ) -> 'LiquidityPoolEvent': """Turns a tuple read from DB into an appropriate LiquidityPoolEvent. May raise a DeserializationError if something is wrong with the DB data Event_tuple index - Schema columns ---------------------------------- 0 - tx_hash 1 - log_index 2 - address 3 - timestamp 4 - type 5 - pool_address 6 - is_token0_unknown 7 - token0_address 8 - token0_symbol 9 - token0_name 10 - token0_decimals 11 - is_token1_unknown 12 - token1_address 13 - token1_symbol 14 - token1_name 15 - token1_decimals 16 - amount0 17 - amount1 18 - usd_price 19 - lp_amount """ db_event_type = event_tuple[4] if db_event_type not in {str(event_type) for event_type in EventType}: raise DeserializationError( f'Failed to deserialize event type. Unknown event: {db_event_type}.', ) if db_event_type == str(EventType.MINT): event_type = EventType.MINT elif db_event_type == str(EventType.BURN): event_type = EventType.BURN else: raise ValueError(f'Unexpected event type case: {db_event_type}.') is_token0_unknown = event_tuple[6] is_token1_unknown = event_tuple[11] token0: Union[EthereumToken, UnknownEthereumToken] token1: Union[EthereumToken, UnknownEthereumToken] if is_token0_unknown: token0 = deserialize_unknown_ethereum_token_from_db( ethereum_address=event_tuple[7], symbol=event_tuple[8], name=event_tuple[9], decimals=event_tuple[10], ) else: token0 = deserialize_ethereum_token_from_db( identifier=event_tuple[8]) if is_token1_unknown: token1 = deserialize_unknown_ethereum_token_from_db( ethereum_address=event_tuple[12], symbol=event_tuple[13], name=event_tuple[14], decimals=event_tuple[15], ) else: token1 = deserialize_ethereum_token_from_db( identifier=event_tuple[13]) return cls( tx_hash=event_tuple[0], log_index=event_tuple[1], address=string_to_ethereum_address(event_tuple[2]), timestamp=deserialize_timestamp(event_tuple[3]), event_type=event_type, pool_address=string_to_ethereum_address(event_tuple[5]), token0=token0, token1=token1, amount0=deserialize_asset_amount(event_tuple[16]), amount1=deserialize_asset_amount(event_tuple[17]), usd_price=deserialize_price(event_tuple[18]), lp_amount=deserialize_asset_amount(event_tuple[19]), )
def deserialize_from_db( cls, pool_tuple: BalancerEventPoolDBTuple, ) -> 'BalancerEventPool': """May raise DeserializationError Event_tuple index - Schema columns ---------------------------------- 0 - address 1 - tokens_number 2 - is_token0_unknown 3 - token0_address 4 - token0_symbol 5 - token0_name 6 - token0_decimals 7 - token0_weight ...token columns are repeated for token 0 to 7 44 - is_token7_unknown 45 - token7_address 46 - token7_symbol 47 - token7_name 48 - token7_decimals 49 - token7_weight """ address = string_to_ethereum_address(pool_tuple[0]) tokens_number = pool_tuple[1] pool_tokens: List[BalancerBPTEventPoolToken] = [] # Deserialize only existing tokens (not null) using `tokens_number` for idx in range(tokens_number): idx_start = 2 + idx * DB_TOKEN_NUMBER_COLUMNS idx_stop = idx_start + DB_TOKEN_NUMBER_COLUMNS ( is_token_unknown, token_address, token_symbol, token_name, token_decimals, token_weight, ) = pool_tuple[idx_start:idx_stop] token: Union[EthereumToken, UnknownEthereumToken] if not is_token_unknown: token = deserialize_ethereum_token_from_db( identifier=cast(str, token_symbol)) else: token = deserialize_unknown_ethereum_token_from_db( ethereum_address=cast(str, token_address), symbol=cast(str, token_symbol), name=None if token_name is None else cast(str, token_name), decimals=None if token_decimals is None else cast( int, token_decimals), ) pool_token = BalancerBPTEventPoolToken( token=token, weight=deserialize_asset_amount(cast(str, token_weight)), ) pool_tokens.append(pool_token) return cls(address=address, tokens=pool_tokens)