Example #1
0
def add_ethereum_token_to_db(token_data: EthereumToken) -> EthereumToken:
    """Adds an ethereum token to the DB and returns it

    May raise:
    - InputError if token already exists in the DB
    """
    globaldb = GlobalDBHandler()
    globaldb.add_asset(
        asset_id=token_data.identifier,
        asset_type=AssetType.ETHEREUM_TOKEN,
        data=token_data,
    )
    # This can, but should not raise UnknownAsset, DeserializationError
    return EthereumToken(token_data.ethereum_address,
                         form_with_incomplete_data=True)
Example #2
0
def import_assets_from_file(
    path: Path,
    msg_aggregator: 'MessagesAggregator',
    db_handler: 'DBHandler',
) -> None:
    """
    Import assets from the file at the defined path.
    This function can raise:
    - ValidationError: If the format of the file is not correct
    - InputError: If the version of the file is not valid for the current
    globaldb version
    """
    globaldb = GlobalDBHandler()
    with open(path) as f:
        data = ExportedAssetsSchema().loads(f.read())

    if int(data['version']) != GLOBAL_DB_VERSION:
        raise InputError(
            f'Provided file is for a different version of rotki. File version: '
            f'{data["version"]} rotki version: {GLOBAL_DB_VERSION}',
        )
    if data['assets'] is None:
        raise InputError('The imported file is missing a valid list of assets')

    identifiers = []
    for asset_data in data['assets']:
        # Check if we already have the asset with that name and symbol. It is possible that
        # we have added a missing asset. Using check_asset_exists for non ethereum tokens and
        # for ethereum tokens comparing by identifier. The edge case of a non-ethereum token
        # with same name and symbol will make this fail.
        asset_type = asset_data['asset_type']
        asset_ref: Union[Optional[List[str]], Optional[AssetData]]
        if asset_type == AssetType.ETHEREUM_TOKEN:
            asset_ref = globaldb.get_asset_data(
                identifier=asset_data['identifier'],
                form_with_incomplete_data=True,
            )
        else:
            asset_ref = globaldb.check_asset_exists(
                asset_type=asset_type,
                name=asset_data['name'],
                symbol=asset_data['symbol'],
            )
        if asset_ref is not None:
            msg_aggregator.add_warning(
                f'Tried to import existing asset {asset_data["identifier"]} with '
                f'name {asset_data["name"]}',
            )
            continue

        try:
            globaldb.add_asset(
                asset_id=asset_data['identifier'],
                asset_type=asset_type,
                data=asset_data['extra_information'],
            )
        except InputError as e:
            log.error(
                f'Failed to import asset with {asset_data["identifier"]=}',
                f'{asset_type=} and {asset_data=}. {str(e)}',
            )
            msg_aggregator.add_error(
                f'Failed to save import with identifier '
                f'{asset_data["identifier"]}. Check logs for more details',
            )
            continue
        identifiers.append(asset_data['identifier'])

    db_handler.add_asset_identifiers(identifiers)