Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix (CryptoCompare): Add missing entries #178

Merged
merged 1 commit into from
Jul 22, 2024

Conversation

OjusWiZard
Copy link
Contributor

@OjusWiZard OjusWiZard commented Jul 22, 2024

Used this script to generate the queries

import json
from pathlib import Path

from rotkehlchen.assets.types import AssetType
from rotkehlchen.constants.resolver import evm_address_to_identifier
from rotkehlchen.errors.serialization import DeserializationError
from rotkehlchen.globaldb.handler import GlobalDBHandler
from rotkehlchen.serialization.deserialize import deserialize_evm_address
from rotkehlchen.types import ChainID, EvmTokenKind
from rotkehlchen.logging import TRACE, add_logging_level

add_logging_level('TRACE', TRACE)


manually_checked = {
    'ADA',
    'STRK',
    'ETH2',
    'XTZ',
    'BCH',
    'DASH',
    'XRP',
    'NEAR',
    'CELO',
    'LTC',
    'AVAX',
    'DOGE',
    'XDAI',
    'DOT',
    'SUI',
    'BTC',
    'BSV',
    'WIF',
    'BONK',
    'JUP',
}
cc_chain_to_chain_id = {
    'BNB': 56,
    'ETH': 1,
    'FTM': 250,
    'AVAX': 43114,
    'GNO': 100,
    'ARB': 42161,
    'MATIC': 137,
    'BASECHAIN': 8453,
    'OP': 10,
}


def main():
    # cc.json is 17 mb of saved response from https://min-api.cryptocompare.com/data/all/coinlist
    with (Path(__file__).parent / 'cc.json').open('r') as f:
        cc_data = json.load(f)

    GlobalDBHandler(data_dir=Path(__file__).parent / 'data', sql_vm_instructions_cb=0)

    queries, check_manually = set(), set()
    for id, data in cc_data['Data'].items():
        identifier = None
        try:
            addresses = data['OtherSmartContractAddress'].split('|')
            decimals = data['DecimalPoints']
        except KeyError:
            identifier = id

        for cc_address in addresses:
            try:
                cc_chain, address = cc_address.split(':')
            except ValueError:
                if len(cc_address) > 42:
                    try:  # sometimes it's etherscan urls like ETH:http://...
                        parts = cc_address.split(':')
                        cc_chain = parts[0]
                        address = parts[-1][-42:-1]
                    except ValueError:
                        continue

            if cc_chain not in cc_chain_to_chain_id:
                identifier = id

            if identifier is None:
                try:
                    identifier = evm_address_to_identifier(
                        address=deserialize_evm_address(address),
                        chain_id=ChainID.deserialize(cc_chain_to_chain_id[cc_chain]),
                        token_type=EvmTokenKind.ERC20,
                    )
                except DeserializationError:
                    identifier = id

            if (db_token := GlobalDBHandler.get_asset_data(
                identifier=identifier,
                form_with_incomplete_data=False,
            )) is None:
                continue

            if db_token.cryptocompare is None or db_token.cryptocompare.lower() != id.lower():
                if not identifier.startswith('eip155'):
                    if identifier not in manually_checked:
                        check_manually.add(identifier)
                        continue

                full_insert = f'INSERT INTO assets(identifier, name, type) VALUES("{identifier}", "{db_token.name}", "{db_token.asset_type.serialize_for_db()}"); '
                if db_token.asset_type == AssetType.EVM_TOKEN:
                    full_insert += 'INSERT INTO evm_tokens(identifier, token_kind, chain, address, decimals, protocol) VALUES("{identifier}", "{token_kind}", {chain_id}, "{address}", {decimals}, "{protocol}"); '.format(
                        identifier=identifier,
                        token_kind='' if db_token.token_kind is None else db_token.token_kind.serialize_for_db(),
                        chain_id=db_token.chain_id.value if db_token.chain_id is not None else cc_chain_to_chain_id[cc_chain],
                        address=db_token.address if db_token.address is not None else deserialize_evm_address(address),
                        decimals=decimals if db_token.decimals is None else db_token.decimals,
                        protocol='' if db_token.protocol is None else db_token.protocol,
                    )

                full_insert += 'INSERT INTO common_asset_details(identifier, symbol, coingecko, cryptocompare, forked, started, swapped_for) VALUES("{identifier}", "{symbol}", "{coingecko}", "{cryptocompare}", {forked}, {started}, {swapped_for});'.format(
                    identifier=identifier,
                    symbol=db_token.symbol,
                    coingecko='' if db_token.coingecko is None else db_token.coingecko,
                    cryptocompare=id,
                    forked='NULL' if db_token.forked is None else f'"{db_token.forked}"',
                    started='NULL' if db_token.started is None else db_token.started,
                    swapped_for='NULL' if db_token.swapped_for is None else f'"{db_token.swapped_for}"',
                )

                queries.add(f"UPDATE common_asset_details SET cryptocompare = '{id}' WHERE identifier = '{identifier}';\n{full_insert}")

    print(f'Check manually ({len(check_manually)}): {check_manually}')

    with open(Path(__file__).parent / 'queries.sql', 'w') as f:
        f.write('\n'.join(queries))


if __name__ == '__main__':
    main()

Signed-off-by: OjusWiZard <ojuswimail@gmail.com>
@yabirgb yabirgb merged commit 5236f49 into rotki:develop Jul 22, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants