Exemplo n.º 1
0
    def query_exchange_trades(
        self,
        name: Optional[str],
        from_timestamp: Timestamp,
        to_timestamp: Timestamp,
        async_query: bool,
    ) -> Response:
        if async_query:
            return self._query_async(
                command='_query_exchange_trades',
                name=name,
                from_timestamp=from_timestamp,
                to_timestamp=to_timestamp,
            )

        response = self._query_exchange_trades(
            name=name,
            from_timestamp=from_timestamp,
            to_timestamp=to_timestamp,
        )
        trades = response['result']
        msg = response['message']

        if trades is None:
            return api_response(wrap_in_fail_result(msg),
                                status_code=HTTPStatus.CONFLICT)

        # If it's a single exchange query, it's a list, otherwise it's a dict
        result = process_result_list(trades) if name else process_result(
            trades)
        return api_response(_wrap_in_ok_result(result), HTTPStatus.OK)
Exemplo n.º 2
0
 def query_owned_assets(self) -> Response:
     result = process_result_list(
         self.rotkehlchen.data.db.query_owned_assets())
     return api_response(
         _wrap_in_ok_result(result),
         status_code=HTTPStatus.OK,
     )
Exemplo n.º 3
0
 def add_ignored_assets(self, assets: List[Asset]) -> Response:
     result, msg = self.rotkehlchen.data.add_ignored_assets(assets=assets)
     if result is None:
         return api_response(wrap_in_fail_result(msg),
                             status_code=HTTPStatus.CONFLICT)
     result_dict = _wrap_in_result(process_result_list(result), msg)
     return api_response(result_dict, status_code=HTTPStatus.OK)
Exemplo n.º 4
0
    def query_my_trades(self, symbol):
        # Ignore all other options and just use the symbol
        result = []
        for trade in self.trades_list:
            if trade['symbol'] == symbol:
                result.append(trade)

        return process_result_list(result)
Exemplo n.º 5
0
def _query_borrowing_aave_history_test(setup: BalancesTestSetup,
                                       server: APIServer) -> None:
    with ExitStack() as stack:
        # patch ethereum/etherscan to not autodetect tokens
        setup.enter_ethereum_patches(stack)
        response = requests.get(api_url_for(
            server,
            "aavehistoryresource",
        ))
        result = assert_proper_response_with_result(response)

    assert len(result) == 1
    assert len(result[AAVE_TEST_ACC_3]) == 4
    events = result[AAVE_TEST_ACC_3]['events']
    total_earned_interest = result[AAVE_TEST_ACC_3]['total_earned_interest']
    total_lost = result[AAVE_TEST_ACC_3]['total_lost']
    total_earned_liquidations = result[AAVE_TEST_ACC_3][
        'total_earned_liquidations']

    assert len(total_earned_interest) >= 1
    assert len(total_earned_interest[A_AWBTC_V1.identifier]) == 2
    assert FVal(total_earned_interest[A_AWBTC_V1.identifier]
                ['amount']) >= FVal('0.00000833')
    assert FVal(
        total_earned_interest[A_AWBTC_V1.identifier]['usd_value']) >= ZERO

    assert len(total_earned_liquidations) == 1
    assert len(total_earned_liquidations['ETH']) == 2
    assert FVal(total_earned_liquidations['ETH']['amount']) >= FVal(
        '9.251070299427409111')
    assert FVal(total_earned_liquidations['ETH']['usd_value']) >= ZERO

    assert len(total_lost) == 3
    eth_lost = total_lost['ETH']
    assert len(eth_lost) == 2
    assert FVal(eth_lost['amount']) >= FVal('0.004452186358507873')
    assert FVal(eth_lost['usd_value']) >= ZERO
    busd_lost = total_lost[A_BUSD.identifier]
    assert len(busd_lost) == 2
    assert FVal(busd_lost['amount']) >= FVal('21.605824443625747553')
    assert FVal(busd_lost['usd_value']) >= ZERO
    wbtc_lost = total_lost[A_WBTC.identifier]
    assert len(wbtc_lost) == 2
    assert FVal(wbtc_lost['amount']) >= FVal('0.41590034')  # ouch
    assert FVal(wbtc_lost['usd_value']) >= ZERO

    expected_events = process_result_list(
        expected_aave_liquidation_test_events)

    assert_serialized_lists_equal(
        a=events[:len(expected_events)],
        b=expected_events,
        ignore_keys=None,
    )
Exemplo n.º 6
0
    def query_value_distribution_data(self, distribution_by: str) -> Response:
        data: Union[List[AssetBalance], List[LocationData]]
        if distribution_by == 'location':
            data = self.rotkehlchen.data.db.get_latest_location_value_distribution(
            )
        else:
            # Can only be 'asset'. Checked by the marshmallow encoding
            data = self.rotkehlchen.data.db.get_latest_asset_value_distribution(
            )

        result = process_result_list(data)
        return api_response(_wrap_in_ok_result(result),
                            status_code=HTTPStatus.OK)
Exemplo n.º 7
0
def _query_simple_aave_history_test(
    setup: BalancesTestSetup,
    server: APIServer,
    async_query: bool,
    use_graph: bool,
) -> None:
    with ExitStack() as stack:
        # patch ethereum/etherscan to not autodetect tokens
        setup.enter_ethereum_patches(stack)
        response = requests.get(api_url_for(
            server,
            "aavehistoryresource",
        ),
                                json={'async_query': async_query})
        if async_query:
            task_id = assert_ok_async_response(response)
            # Big timeout since this test can take a long time
            outcome = wait_for_async_task(server, task_id, timeout=600)
            assert outcome['message'] == ''
            result = outcome['result']
        else:
            result = assert_proper_response_with_result(response)

    assert len(result) == 1
    assert len(result[AAVE_TEST_ACC_2]) == 4
    events = result[AAVE_TEST_ACC_2]['events']
    total_earned_interest = result[AAVE_TEST_ACC_2]['total_earned_interest']
    total_lost = result[AAVE_TEST_ACC_2]['total_lost']
    total_earned_liquidations = result[AAVE_TEST_ACC_2][
        'total_earned_liquidations']
    assert len(total_lost) == 0
    assert len(total_earned_liquidations) == 0
    assert len(total_earned_interest) == 1
    assert len(total_earned_interest[A_ADAI_V1.identifier]) == 2
    assert FVal(total_earned_interest[A_ADAI_V1.identifier]['amount']) >= FVal(
        '24.207179802347627414')  # noqa: E501
    assert FVal(
        total_earned_interest[A_ADAI_V1.identifier]['usd_value']) >= FVal(
            '24.580592532348742989192')  # noqa: E501

    expected_events = process_result_list(expected_aave_deposit_test_events)
    if use_graph:
        expected_events = expected_events[:7] + expected_events[8:]

    assert_serialized_lists_equal(
        a=events[:len(expected_events)],
        b=expected_events,
        ignore_keys=['log_index', 'block_number'] if use_graph else None,
    )
Exemplo n.º 8
0
def assert_asset_movements(
    expected: List[AssetMovement],
    to_check_list: List[Any],
    deserialized: bool,
    movements_to_check: Optional[Tuple[int, ...]] = None,
) -> None:
    if deserialized:
        expected = process_result_list([x.serialize() for x in expected])

    if movements_to_check is None:
        assert len(to_check_list) == len(expected)
        assert all(x in to_check_list for x in expected)
    else:
        assert all(expected[x] in to_check_list for x in movements_to_check)
        assert len(to_check_list) == len(movements_to_check)
Exemplo n.º 9
0
    def query_timed_balances_data(
        self,
        asset: Asset,
        from_timestamp: Timestamp,
        to_timestamp: Timestamp,
    ) -> Response:
        data = self.rotkehlchen.data.db.query_timed_balances(
            from_ts=from_timestamp,
            to_ts=to_timestamp,
            asset=asset,
        )

        result = process_result_list(data)
        return api_response(_wrap_in_ok_result(result),
                            status_code=HTTPStatus.OK)
Exemplo n.º 10
0
def test_query_aave_history(rotkehlchen_api_server, ethereum_accounts):  # pylint: disable=unused-argument  # noqa: E501
    """Check querying the aave histoy endpoint works. Uses real data.

    Since this actually queries real blockchain data for aave it is a very slow test
    due to the sheer amount of log queries
    """
    rotki = rotkehlchen_api_server.rest_api.rotkehlchen
    setup = setup_balances(rotki,
                           ethereum_accounts=ethereum_accounts,
                           btc_accounts=None)
    # Since this test is slow we don't run both async and sync in the same test run
    # Instead we randomly choose one. Eventually both cases will be covered.
    async_query = random.choice([True, False])

    with ExitStack() as stack:
        # patch ethereum/etherscan to not autodetect tokens
        setup.enter_ethereum_patches(stack)
        response = requests.get(api_url_for(
            rotkehlchen_api_server,
            "aavehistoryresource",
        ),
                                json={'async_query': async_query})
        if async_query:
            task_id = assert_ok_async_response(response)
            # Big timeout since this test can take a long time
            outcome = wait_for_async_task(rotkehlchen_api_server,
                                          task_id,
                                          timeout=600)
            assert outcome['message'] == ''
            result = outcome['result']
        else:
            result = assert_proper_response_with_result(response)

    assert len(result) == 1
    assert len(result[AAVE_TEST_ACC_2]) == 2
    events = result[AAVE_TEST_ACC_2]['events']
    total_earned = result[AAVE_TEST_ACC_2]['total_earned']
    assert len(total_earned) == 1
    assert len(total_earned['aDAI']) == 2
    assert FVal(
        total_earned['aDAI']['amount']) >= FVal('24.207179802347627414')
    assert FVal(
        total_earned['aDAI']['usd_value']) >= FVal('24.580592532348742989192')

    expected_events = process_result_list(expected_aave_test_events)
    assert len(events) >= 16
    assert events[:16] == expected_events
Exemplo n.º 11
0
def test_eth2_deposits_serialization():
    addr1 = make_ethereum_address()
    addr2 = make_ethereum_address()
    deposits = [
        Eth2Deposit(
            from_address=addr1,
            pubkey='0xb016e31f633a21fbe42a015152399361184f1e2c0803d89823c224994af74a561c4ad8cfc94b18781d589d03e952cd5b',  # noqa: E501
            withdrawal_credentials='0x004c7691c2085648f394ffaef851f3b1d51b95f7263114bc923fc5338f5fc499',  # noqa: E501
            value=Balance(FVal(32), FVal(64)),
            deposit_index=9,
            tx_hash='0xd9eca1c2a0c5ff2f25071713432b21cc4d0ff2e8963edc63a48478e395e08db1',
            log_index=22,
            timestamp=Timestamp(int(1604506685)),
        ), Eth2Deposit(
            from_address=addr2,
            pubkey='0xa8ff5fc88412d080a297683c25a791ef77eb52d75b265fabab1f2c2591bb927c35818ac6289bc6680ab252787d0ebab3',  # noqa: E501
            withdrawal_credentials='0x00cfe1c10347d642a8b8daf86d23bcb368076972691445de2cf517ff43765817',  # noqa: E501
            value=Balance(FVal(32), FVal(64)),
            deposit_index=1650,
            tx_hash='0x6905f4d1843fb8c003c1fbbc2c8e6c5f9792f4f44ddb1122553412ee0b128da7',
            log_index=221,
            timestamp=Timestamp(int(1605043544)),
        ),
    ]

    serialized = process_result_list(deposits)
    assert serialized == [
        {
            'from_address': addr1,
            'pubkey': '0xb016e31f633a21fbe42a015152399361184f1e2c0803d89823c224994af74a561c4ad8cfc94b18781d589d03e952cd5b',  # noqa: E501
            'withdrawal_credentials': '0x004c7691c2085648f394ffaef851f3b1d51b95f7263114bc923fc5338f5fc499',  # noqa: E501
            'value': {'amount': '32', 'usd_value': '64'},
            'deposit_index': 9,
            'tx_hash': '0xd9eca1c2a0c5ff2f25071713432b21cc4d0ff2e8963edc63a48478e395e08db1',
            'log_index': 22,
            'timestamp': 1604506685,
        }, {
            'from_address': addr2,
            'pubkey': '0xa8ff5fc88412d080a297683c25a791ef77eb52d75b265fabab1f2c2591bb927c35818ac6289bc6680ab252787d0ebab3',  # noqa: E501
            'withdrawal_credentials': '0x00cfe1c10347d642a8b8daf86d23bcb368076972691445de2cf517ff43765817',  # noqa: E501
            'value': {'amount': '32', 'usd_value': '64'},
            'deposit_index': 1650,
            'tx_hash': '0x6905f4d1843fb8c003c1fbbc2c8e6c5f9792f4f44ddb1122553412ee0b128da7',
            'log_index': 221,
            'timestamp': 1605043544,
        },
    ]
Exemplo n.º 12
0
def _query_simple_aave_history_test_v2(
    setup: BalancesTestSetup,
    server: APIServer,
    async_query: bool,
) -> None:
    with ExitStack() as stack:
        # patch ethereum/etherscan to not autodetect tokens
        setup.enter_ethereum_patches(stack)
        response = requests.get(api_url_for(
            server,
            "aavehistoryresource",
        ),
                                json={'async_query': async_query})
        if async_query:
            task_id = assert_ok_async_response(response)
            # Big timeout since this test can take a long time
            outcome = wait_for_async_task(server, task_id, timeout=600)
            assert outcome['message'] == ''
            result = outcome['result']
        else:
            result = assert_proper_response_with_result(response)

    assert len(result) == 1
    assert len(result[AAVE_V2_TEST_ACC]) == 4
    events = result[AAVE_V2_TEST_ACC]['events']
    total_earned_interest = result[AAVE_V2_TEST_ACC]['total_earned_interest']
    total_lost = result[AAVE_V2_TEST_ACC]['total_lost']
    total_earned_liquidations = result[AAVE_V2_TEST_ACC][
        'total_earned_liquidations']
    assert len(total_lost) == 1
    assert len(total_earned_liquidations) == 0
    assert len(total_earned_interest) == 1
    assert len(total_earned_interest[
        '_ceth_0xa06bC25B5805d5F8d82847D191Cb4Af5A3e873E0']) == 2
    assert FVal(total_earned_interest[
        '_ceth_0xa06bC25B5805d5F8d82847D191Cb4Af5A3e873E0']['amount']) >= FVal(
            '0.09')  # noqa: E501
    assert FVal(total_earned_interest[
        '_ceth_0xa06bC25B5805d5F8d82847D191Cb4Af5A3e873E0']
                ['usd_value']) >= FVal('0.09248')  # noqa: E501

    assert_serialized_lists_equal(
        a=events[:len(expected_aave_v2_events)],
        b=process_result_list(expected_aave_v2_events),
        ignore_keys=None,
    )
Exemplo n.º 13
0
def test_query_compound_history(rotkehlchen_api_server, ethereum_accounts):  # pylint: disable=unused-argument  # noqa: E501
    """Check querying the compound history endpoint works. Uses real data"""
    rotki = rotkehlchen_api_server.rest_api.rotkehlchen
    setup = setup_balances(
        rotki,
        ethereum_accounts=ethereum_accounts,
        eth_balances=['1000000', '2000000', '33000030003', '42323213'],
        token_balances={},
        btc_accounts=None,
        original_queries=['zerion'],
    )
    # Since this test can be a bit slow we don't run both async and sync in the same test run
    # Instead we randomly choose one. Eventually both cases will be covered.
    async_query = random.choice([True, False])

    with ExitStack() as stack:
        # patch ethereum/etherscan to not autodetect tokens
        setup.enter_ethereum_patches(stack)
        response = requests.get(api_url_for(
            rotkehlchen_api_server,
            "compoundhistoryresource",
        ), json={'async_query': async_query})
        if async_query:
            task_id = assert_ok_async_response(response)
            # Timeout of 120 since this test can take a long time
            outcome = wait_for_async_task(rotkehlchen_api_server, task_id, timeout=120)
            assert outcome['message'] == ''
            result = outcome['result']
        else:
            result = assert_proper_response_with_result(response)

    assert len(result) == 5
    expected_events = process_result_list(EXPECTED_EVENTS)
    # Check only 22 first events, since this is how many there were in the time of
    # the writing of the test. Also don't check events for one of the addresses
    # as it's added later, has many events and it's only to see we handle repay correctly
    to_check_events = [
        x for x in result['events'] if x['address'] != '0x65304d6aff5096472519ca86a6a1fea31cb47Ced'
    ]
    assert to_check_events[:22] == expected_events
    # Check one repay event
    other_events = [
        x for x in result['events'] if x['address'] == '0x65304d6aff5096472519ca86a6a1fea31cb47Ced'
    ]
    assert other_events[12]['event_type'] == 'repay'
    expected_hash = '0x48a3e2ef8a746383deac34d74f2f0ea0451b2047701fbed4b9d769a782888eea'
    assert other_events[12]['tx_hash'] == expected_hash
    assert other_events[12]['value']['amount'] == '0.55064402'

    # Check interest profit mappings
    profit_0 = result['interest_profit']['0x2B888954421b424C5D3D9Ce9bB67c9bD47537d12']
    assert FVal(profit_0['DAI']['amount']) > FVal(9)
    profit_1 = result['interest_profit']['0xC440f3C87DC4B6843CABc413916220D4f4FeD117']
    assert FVal(profit_1['USDC']['amount']) > FVal(2)
    profit_2 = result['interest_profit']['0xF59D4937BF1305856C3a267bB07791507a3377Ee']
    assert FVal(profit_2['DAI']['amount']) > FVal('0.3')

    # Check debt loss mappings
    debt_0 = result['debt_loss']['0xC440f3C87DC4B6843CABc413916220D4f4FeD117']
    assert FVal(debt_0['cUSDC']['amount']) > FVal('84')
    assert FVal(debt_0['ETH']['amount']) > FVal('0.000012422')

    # Check liquidation profit mappings
    lprofit_0 = result['liquidation_profit']['0xC440f3C87DC4B6843CABc413916220D4f4FeD117']
    assert FVal(lprofit_0['ETH']['amount']) > FVal('0.000012')

    # Check rewards mappings
    rewards_0 = result['rewards']['0xC440f3C87DC4B6843CABc413916220D4f4FeD117']
    assert FVal(rewards_0['COMP']['amount']) > FVal('0.000036')
    rewards_1 = result['rewards']['0xF59D4937BF1305856C3a267bB07791507a3377Ee']
    assert FVal(rewards_1['COMP']['amount']) > FVal('0.003613')