def process_polo_loans(data, start_ts, end_ts): new_data = list() for loan in reversed(data): close_time = createTimeStamp(loan['close'], formatstr="%Y-%m-%d %H:%M:%S") open_time = createTimeStamp(loan['open'], formatstr="%Y-%m-%d %H:%M:%S") if open_time < start_ts: continue if close_time > end_ts: break loan_data = { 'open_time': open_time, 'close_time': close_time, 'currency': loan['currency'], 'fee': FVal(loan['fee']), 'earned': FVal(loan['earned']), 'amount_lent': FVal(loan['amount']), } log.debug('processing poloniex loan', **make_sensitive(loan_data)) new_data.append(loan_data) new_data.sort(key=lambda loan: loan['open_time']) return new_data
def add_to_allevents( self, event_type: EventType, paid_in_profit_currency: FVal, paid_asset: Union[Asset, EmptyStr], paid_in_asset: FVal, received_asset: Union[Asset, EmptyStr], received_in_asset: FVal, taxable_received_in_profit_currency: FVal, timestamp: Timestamp, is_virtual: bool = False, taxable_amount: FVal = ZERO, taxable_bought_cost: FVal = ZERO, ) -> None: row = len(self.all_events_csv) + 2 if event_type == EV_BUY: net_profit_or_loss = FVal(0) # no profit by buying net_profit_or_loss_csv = '0' elif event_type == EV_SELL: if taxable_amount == 0: net_profit_or_loss = FVal(0) else: net_profit_or_loss = taxable_received_in_profit_currency - taxable_bought_cost net_profit_or_loss_csv = '=IF(D{}=0,0,K{}-L{})'.format(row, row, row) elif event_type in (EV_TX_GAS_COST, EV_ASSET_MOVE, EV_LOAN_SETTLE): net_profit_or_loss = paid_in_profit_currency net_profit_or_loss_csv = '=-J{}'.format(row) elif event_type in (EV_INTEREST_PAYMENT, EV_MARGIN_CLOSE): net_profit_or_loss = taxable_received_in_profit_currency net_profit_or_loss_csv = '=K{}'.format(row) else: raise ValueError('Illegal event type "{}" at add_to_allevents'.format(event_type)) entry = { 'type': event_type, 'paid_in_profit_currency': paid_in_profit_currency, 'paid_asset': paid_asset, 'paid_in_asset': paid_in_asset, 'taxable_amount': taxable_amount, 'taxable_bought_cost_in_profit_currency': taxable_bought_cost, 'received_asset': received_asset, 'taxable_received_in_profit_currency': taxable_received_in_profit_currency, 'received_in_asset': received_in_asset, 'net_profit_or_loss': net_profit_or_loss, 'time': timestamp, 'is_virtual': is_virtual, } log.debug('csv event', **make_sensitive(entry)) self.all_events.append(entry) new_entry = entry.copy() new_entry['net_profit_or_loss'] = net_profit_or_loss_csv new_entry['time'] = timestamp_to_date(timestamp, formatstr='%d/%m/%Y %H:%M:%S') new_entry['paid_in_{}'.format(self.profit_currency)] = paid_in_profit_currency key = f'taxable_received_in_{self.profit_currency}' new_entry[key] = taxable_received_in_profit_currency new_entry['taxable_bought_cost_in_{}'.format(self.profit_currency)] = taxable_bought_cost del new_entry['paid_in_profit_currency'] del new_entry['taxable_received_in_profit_currency'] del new_entry['taxable_bought_cost_in_profit_currency'] self.all_events_csv.append(new_entry)
def process_polo_loans( msg_aggregator: MessagesAggregator, data: List[Dict], start_ts: Timestamp, end_ts: Timestamp, ) -> List[Loan]: """Takes in the list of loans from poloniex as returned by the return_lending_history api call, processes it and returns it into our loan format """ new_data = [] for loan in reversed(data): log.debug('processing poloniex loan', **make_sensitive(loan)) try: close_time = deserialize_timestamp_from_poloniex_date( loan['close']) open_time = deserialize_timestamp_from_poloniex_date(loan['open']) if open_time < start_ts: continue if close_time > end_ts: continue our_loan = Loan( location=Location.POLONIEX, open_time=open_time, close_time=close_time, currency=asset_from_poloniex(loan['currency']), fee=deserialize_fee(loan['fee']), earned=deserialize_asset_amount(loan['earned']), amount_lent=deserialize_asset_amount(loan['amount']), ) except UnsupportedAsset as e: msg_aggregator.add_warning( f'Found poloniex loan with unsupported asset' f' {e.asset_name}. Ignoring it.', ) continue except UnknownAsset as e: msg_aggregator.add_warning( f'Found poloniex loan with unknown asset' f' {e.asset_name}. Ignoring it.', ) continue except (DeserializationError, KeyError) as e: msg = str(e) if isinstance(e, KeyError): msg = f'Missing key entry for {msg}.' msg_aggregator.add_error( 'Deserialization error while reading a poloniex loan. Check ' 'logs for more details. Ignoring it.', ) log.error( 'Deserialization error while reading a poloniex loan', loan=loan, error=msg, ) continue new_data.append(our_loan) new_data.sort(key=lambda loan: loan.open_time) return new_data
def process_polo_loans( self, data: List[Dict], start_ts: Timestamp, end_ts: Timestamp, ) -> List[Dict]: """Takes in the list of loans from poloniex as returned by the returnLendingHistory api call, processes it and returns it into our loan format """ new_data = list() for loan in reversed(data): close_time = createTimeStamp(loan['close'], formatstr="%Y-%m-%d %H:%M:%S") open_time = createTimeStamp(loan['open'], formatstr="%Y-%m-%d %H:%M:%S") if open_time < start_ts: continue if close_time > end_ts: break try: loan_data = { 'open_time': open_time, 'close_time': close_time, 'currency': asset_from_poloniex(loan['currency']), 'fee': FVal(loan['fee']), 'earned': FVal(loan['earned']), 'amount_lent': FVal(loan['amount']), } except UnsupportedAsset as e: self.msg_aggregator.add_warning( f'Found poloniex loan with unsupported asset' f' {e.asset_name}. Ignoring it.', ) continue except UnknownAsset as e: self.msg_aggregator.add_warning( f'Found poloniex loan with unknown asset' f' {e.asset_name}. Ignoring it.', ) continue log.debug('processing poloniex loan', **make_sensitive(loan_data)) new_data.append(loan_data) new_data.sort(key=lambda loan: loan['open_time']) return new_data
def add_to_allevents( self, event_type: EventType, location: Location, paid_in_profit_currency: FVal, paid_asset: Union[Asset, EmptyStr], paid_in_asset: FVal, received_asset: Union[Asset, EmptyStr], received_in_asset: FVal, taxable_received_in_profit_currency: FVal, total_received_in_profit_currency: FVal, timestamp: Timestamp, is_virtual: bool = False, taxable_amount: FVal = ZERO, taxable_bought_cost: FVal = ZERO, total_bought_cost: FVal = ZERO, cost_basis_info: Optional['CostBasisInfo'] = None, ) -> None: row = len(self.all_events_csv) + 2 if event_type == EV_BUY: net_profit_or_loss = ZERO # no profit by buying net_profit_or_loss_csv = '0' elif event_type == EV_SELL: if taxable_amount == 0: net_profit_or_loss = ZERO else: net_profit_or_loss = taxable_received_in_profit_currency - taxable_bought_cost net_profit_or_loss_csv = f'=IF(E{row}=0,0,L{row}-M{row})' elif event_type in (EV_TX_GAS_COST, EV_ASSET_MOVE, EV_LOAN_SETTLE): net_profit_or_loss = paid_in_profit_currency net_profit_or_loss_csv = '=-K{}'.format(row) elif event_type in (EV_INTEREST_PAYMENT, EV_MARGIN_CLOSE, EV_DEFI, EV_LEDGER_ACTION): net_profit_or_loss = taxable_received_in_profit_currency net_profit_or_loss_csv = '=L{}'.format(row) else: raise ValueError('Illegal event type "{}" at add_to_allevents'.format(event_type)) exported_paid_asset = ( paid_asset if isinstance(paid_asset, str) else str(paid_asset) ) exported_received_asset = ( received_asset if isinstance(received_asset, str) else str(received_asset) ) entry = { 'type': event_type, 'location': str(location), 'paid_in_profit_currency': paid_in_profit_currency, 'paid_asset': exported_paid_asset, 'paid_in_asset': paid_in_asset, 'taxable_amount': taxable_amount, 'taxable_bought_cost_in_profit_currency': taxable_bought_cost, 'received_asset': exported_received_asset, 'taxable_received_in_profit_currency': taxable_received_in_profit_currency, 'received_in_asset': received_in_asset, 'net_profit_or_loss': net_profit_or_loss, 'time': timestamp, 'cost_basis': cost_basis_info.serialize() if cost_basis_info else None, 'is_virtual': is_virtual, } log.debug('csv event', **make_sensitive(entry)) self.all_events.append(entry) new_entry = entry.copy() new_entry['net_profit_or_loss'] = net_profit_or_loss_csv new_entry['time'] = self.timestamp_to_date(timestamp) new_entry[f'paid_in_{self.profit_currency.symbol}'] = paid_in_profit_currency key = f'taxable_received_in_{self.profit_currency.symbol}' new_entry[key] = taxable_received_in_profit_currency key = f'taxable_bought_cost_in_{self.profit_currency.symbol}' new_entry[key] = taxable_bought_cost del new_entry['cost_basis'] # deleting and re-adding is for appending it to end of dict new_entry['cost_basis'] = cost_basis_info.to_string(self.timestamp_to_date) if cost_basis_info else '' # noqa: E501 key = f'total_bought_cost_in_{self.profit_currency.symbol}' new_entry[key] = total_bought_cost key = f'total_received_in_{self.profit_currency.symbol}' new_entry[key] = total_received_in_profit_currency del new_entry['paid_in_profit_currency'] del new_entry['taxable_received_in_profit_currency'] del new_entry['taxable_bought_cost_in_profit_currency'] self.all_events_csv.append(new_entry)