def verify_otctrade_data( data: ExternalTrade, ) -> Tuple[Optional[typing.Trade], str]: """Takes in the trade data dictionary, validates it and returns a trade instance""" for field in otc_fields: if field not in data: return None, '{} was not provided'.format(field) if data[field] in ('', None) and field not in otc_optional_fields: return None, '{} was empty'.format(field) if field in otc_numerical_fields and not is_number(data[field]): return None, '{} should be a number'.format(field) pair = data['otc_pair'] assert isinstance(pair, str) first = get_pair_position(pair, 'first') second = get_pair_position(pair, 'second') trade_type = cast(str, data['otc_type']) amount = FVal(data['otc_amount']) rate = FVal(data['otc_rate']) fee = FVal(data['otc_fee']) fee_currency = cast(typing.Asset, data['otc_fee_currency']) try: assert isinstance(data['otc_timestamp'], str) timestamp = createTimeStamp(data['otc_timestamp'], formatstr='%d/%m/%Y %H:%M') except ValueError as e: return None, 'Could not process the given datetime: {}'.format(e) log.debug( 'Creating OTC trade data', sensitive_log=True, pair=pair, trade_type=trade_type, amount=amount, rate=rate, fee=fee, fee_currency=fee_currency, ) if data['otc_fee_currency'] not in (first, second): return None, 'Trade fee currency should be one of the two in the currency pair' if data['otc_type'] not in ('buy', 'sell'): return None, 'Trade type can only be buy or sell' trade = typing.Trade( time=timestamp, location='external', pair=cast(str, pair), trade_type=trade_type, amount=amount, rate=rate, fee=fee, fee_currency=fee_currency, link=cast(str, data['otc_link']), notes=cast(str, data['otc_notes']), ) return trade, ''
def check_otctrade_data_valid(data): for field in otc_fields: if field not in data: return None, '{} was not provided'.format(field) if data[field] in ('', None) and field not in otc_optional_fields: return None, '{} was empty'.format(field) if field in otc_numerical_fields and not is_number(data[field]): return None, '{} should be a number'.format(field) pair = data['otc_pair'] first = get_pair_position(pair, 'first') second = get_pair_position(pair, 'second') if data['otc_fee_currency'] not in (first, second): return None, 'Trade fee currency should be one of the two in the currency pair' if data['otc_type'] not in ('buy', 'sell'): return None, 'Trade type can only be buy or sell' try: timestamp = createTimeStamp(data['otc_time'], formatstr='%d/%m/%Y %H:%M') except ValueError as e: return None, 'Could not process the given datetime: {}'.format(e) return timestamp, ''