Beispiel #1
0
 def _get_tokens_balance_and_price(
     self,
     address: ChecksumEthAddress,
     tokens: List[EthereumToken],
     balances: Dict[EthereumToken, FVal],
     token_usd_price: Dict[EthereumToken, Price],
     call_order: Optional[Sequence[NodeName]],
 ) -> None:
     ret = self._get_multitoken_account_balance(
         tokens=tokens,
         account=address,
         call_order=call_order,
     )
     for token_identifier, value in ret.items():
         token = EthereumToken.from_identifier(token_identifier)
         if token is None:  # should not happen
             log.warning(
                 f'Could not initialize token with identifier {token_identifier}. '
                 f'Should not happen. Skipping its token balance query', )
             continue
         balances[token] += value
         if token in token_usd_price:
             continue
         # else get the price
         try:
             usd_price = Inquirer().find_usd_price(token)
         except RemoteError:
             usd_price = Price(ZERO)
         token_usd_price[token] = usd_price
Beispiel #2
0
    def deserialize_from_db(
        cls,
        event_tuple: BalancerEventDBTuple,
    ) -> 'BalancerEvent':
        """May raise DeserializationError

        Event_tuple index - Schema columns
        ----------------------------------
        0 - tx_hash
        1 - log_index
        2 - address
        3 - timestamp
        4 - type
        5 - pool_address_token
        6 - lp_amount
        7 - usd_value
        8 - amount0
        9 - amount1
        10 - amount2
        11 - amount3
        12 - amount4
        13 - amount5
        14 - amount6
        15 - amount7
        """
        event_tuple_type = event_tuple[4]
        try:
            event_type = getattr(BalancerBPTEventType,
                                 event_tuple_type.upper())
        except AttributeError as e:
            raise DeserializationError(
                f'Unexpected event type: {event_tuple_type}.') from e

        pool_address_token = EthereumToken.from_identifier(
            event_tuple[5],
            form_with_incomplete_data=
            True,  # since some may not have decimals input correctly
        )
        if pool_address_token is None:
            raise DeserializationError(
                f'Balancer event pool token: {event_tuple[5]} not found in the DB.',
            )

        amounts: List[AssetAmount] = [
            deserialize_asset_amount(item) for item in event_tuple[8:16]
            if item is not None
        ]
        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_token=pool_address_token,
            lp_balance=Balance(
                amount=deserialize_asset_amount(event_tuple[6]),
                usd_value=deserialize_price(event_tuple[7]),
            ),
            amounts=amounts,
        )
Beispiel #3
0
def deserialize_ethereum_token_from_db(identifier: str) -> EthereumToken:
    """Takes an identifier and returns the <EthereumToken>"""
    ethereum_token = EthereumToken.from_identifier(identifier=identifier)
    if ethereum_token is None:
        raise DeserializationError(
            f'Could not initialize an ethereum token with identifier {identifier}',
        )

    return ethereum_token
Beispiel #4
0
def deserialize_adex_event_from_db(
    event_tuple: AdexEventDBTuple,
) -> Union[Bond, Unbond, UnbondRequest, ChannelWithdraw]:
    """Turns a tuple read from DB into an appropriate AdEx event.
    May raise a DeserializationError if something is wrong with the DB data.

    Event_tuple index - Schema columns
    ----------------------------------
    0 - tx_hash
    1 - address
    2 - identity_address
    3 - timestamp
    4 - type
    5 - pool_id
    6 - amount
    7 - usd_value
    8 - bond_id
    9 - nonce
    10 - slashed_at
    11 - unlock_at
    12 - channel_id
    13 - token
    14 - log_index
    """
    db_event_type = event_tuple[4]
    if db_event_type not in {str(event_type) for event_type in AdexEventType}:
        raise DeserializationError(
            f'Failed to deserialize event type. Unknown event: {db_event_type}.',
        )

    tx_hash = event_tuple[0]
    address = deserialize_ethereum_address(event_tuple[1])
    identity_address = deserialize_ethereum_address(event_tuple[2])
    timestamp = deserialize_timestamp(event_tuple[3])
    pool_id = event_tuple[5]
    amount = deserialize_asset_amount(event_tuple[6])
    usd_value = deserialize_asset_amount(event_tuple[7])
    value = Balance(amount=amount, usd_value=usd_value)

    if db_event_type == str(AdexEventType.BOND):
        if any(event_tuple[idx] is None for idx in (8, 9, 10)):
            raise DeserializationError(
                f'Failed to deserialize bond event. Unexpected data: {event_tuple}.',
            )

        return Bond(
            tx_hash=tx_hash,
            address=address,
            identity_address=identity_address,
            timestamp=timestamp,
            pool_id=pool_id,
            value=value,
            bond_id=event_tuple[8],  # type: ignore # type already checked
            nonce=event_tuple[9],  # type: ignore # type already checked
            slashed_at=deserialize_timestamp(
                event_tuple[10]),  # type: ignore # already checked
        )

    if db_event_type == str(AdexEventType.UNBOND):
        if any(event_tuple[idx] is None for idx in (8, )):
            raise DeserializationError(
                f'Failed to deserialize unbond event. Unexpected data: {event_tuple}.',
            )

        return Unbond(
            tx_hash=tx_hash,
            address=address,
            identity_address=identity_address,
            timestamp=timestamp,
            pool_id=pool_id,
            value=value,
            bond_id=event_tuple[8],  # type: ignore # type already checked
        )

    if db_event_type == str(AdexEventType.UNBOND_REQUEST):
        if any(event_tuple[idx] is None for idx in (8, 11)):
            raise DeserializationError(
                f'Failed to deserialize unbond request event. Unexpected data: {event_tuple}.',
            )

        return UnbondRequest(
            tx_hash=tx_hash,
            address=address,
            identity_address=identity_address,
            timestamp=timestamp,
            pool_id=pool_id,
            value=value,
            bond_id=event_tuple[8],  # type: ignore # type already checked
            unlock_at=deserialize_timestamp(
                event_tuple[11]),  # type: ignore # already checked
        )

    if db_event_type == str(AdexEventType.CHANNEL_WITHDRAW):
        if any(event_tuple[idx] is None for idx in (12, 13, 14)):
            raise DeserializationError(
                f'Failed to deserialize channel withdraw event. Unexpected data: {event_tuple}.',
            )
        token = EthereumToken.from_identifier(event_tuple[13])  # type: ignore
        if token is None:
            raise DeserializationError(
                f'Unknown token {event_tuple[13]} found while processing adex event. '
                f'Unexpected data: {event_tuple}', )

        return ChannelWithdraw(
            tx_hash=tx_hash,
            address=address,
            identity_address=identity_address,
            timestamp=timestamp,
            value=value,
            pool_id=pool_id,
            channel_id=event_tuple[12],  # type: ignore # type already checked
            token=token,
            log_index=event_tuple[14],  # type: ignore # type already checked
        )

    raise DeserializationError(
        f'Failed to deserialize event. Unexpected event type case: {db_event_type}.',
    )