def test_process_polo_loans(): raw_data = [TEST_POLO_LOAN_1, TEST_POLO_LOAN_2] msg_aggregator = MessagesAggregator() loans = process_polo_loans(msg_aggregator, raw_data, 0, 1564262858) assert len(loans) == 2 assert isinstance(loans[0], Loan) assert loans[0].open_time == Timestamp(1485237904) assert loans[0].close_time == Timestamp(1485252304) assert isinstance(loans[0].currency, Asset) assert loans[0].currency == A_DASH assert loans[0].fee == FVal('0.00015') assert loans[0].earned == FVal('0.003') assert loans[0].amount_lent == FVal('2') assert isinstance(loans[1], Loan) assert loans[1].open_time == Timestamp(1487012821) assert loans[1].close_time == Timestamp(1487027104) assert isinstance(loans[1].currency, Asset) assert loans[1].currency == A_DASH assert loans[1].fee == FVal('0.00011') assert loans[1].earned == FVal('0.0035') assert loans[1].amount_lent == FVal('2') # Test different start/end timestamps loans = process_polo_loans(msg_aggregator, raw_data, 1485252305, 1564262858) assert len(loans) == 1 assert loans[0].close_time == Timestamp(1487027104) loans = process_polo_loans(msg_aggregator, raw_data, 0, 1487012820) assert len(loans) == 1 assert loans[0].close_time == Timestamp(1485252304)
def populate_history_cb( trades_history: List[Trade], margin_history: List[MarginPosition], result_asset_movements: List[AssetMovement], exchange_specific_data: Any, ) -> None: """This callback will run for succesfull exchange history query We don't include ledger actions here since we simply gather all of them at the end """ history.extend(trades_history) history.extend(margin_history) history.extend(result_asset_movements) if exchange_specific_data: # This can only be poloniex at the moment polo_loans_data = exchange_specific_data history.extend( process_polo_loans( msg_aggregator=self.msg_aggregator, data=polo_loans_data, # We need to have history of loans since before the range start_ts=Timestamp(0), end_ts=end_ts, ))
def accounting_history_process( accountant, start_ts: Timestamp, end_ts: Timestamp, history_list: List[Dict], margin_list: List[MarginPosition] = None, loans_list: List[Dict] = None, asset_movements_list: List[AssetMovement] = None, eth_transaction_list: List[Dict] = None, defi_events_list: List[DefiEvent] = None, ) -> Dict[str, Any]: trade_history: Sequence[Union[Trade, MarginPosition]] # For filtering the taxable actions list we start with 0 ts so that we have the # full history available trade_history = trades_from_dictlist( given_trades=history_list, start_ts=Timestamp(0), end_ts=end_ts, location='accounting_history_process for tests', msg_aggregator=accountant.msg_aggregator, ) # if present, append margin positions to trade history if margin_list: trade_history.extend(margin_list) # type: ignore asset_movements = [] if asset_movements_list: asset_movements = asset_movements_list loan_history = [] if loans_list: loan_history = process_polo_loans( msg_aggregator=accountant.msg_aggregator, data=loans_list, start_ts=Timestamp(0), end_ts=end_ts, ) eth_transactions = [] if eth_transaction_list: eth_transactions = eth_transaction_list defi_events = [] if defi_events_list: defi_events = defi_events_list result = accountant.process_history( start_ts=start_ts, end_ts=end_ts, trade_history=trade_history, loan_history=loan_history, asset_movements=asset_movements, eth_transactions=eth_transactions, defi_events=defi_events, ) return result
def accounting_history_process( accountant, start_ts: Timestamp, end_ts: Timestamp, history_list: List[Dict], margin_list: List[MarginPosition] = None, loans_list: List[Dict] = None, asset_movements_list: List[Dict] = None, eth_transaction_list: List[Dict] = None, ) -> Dict[str, Any]: # For filtering the taxable actions list we start with 0 ts so that we have the # full history available trade_history = trades_from_dictlist( given_trades=history_list, start_ts=0, end_ts=end_ts, location='accounting_history_process for tests', msg_aggregator=accountant.msg_aggregator, ) # if present, append margin positions to trade history if margin_list: trade_history.extend(margin_list) asset_movements = list() if asset_movements_list: asset_movements = asset_movements_from_dictlist( given_data=asset_movements_list, start_ts=0, end_ts=end_ts, ) loan_history = list() if loans_list: loan_history = process_polo_loans( msg_aggregator=accountant.msg_aggregator, data=loans_list, start_ts=0, end_ts=end_ts, ) eth_transactions = list() if eth_transaction_list: eth_transactions = transactions_from_dictlist( given_transactions=eth_transaction_list, start_ts=0, end_ts=end_ts, ) result = accountant.process_history( start_ts=start_ts, end_ts=end_ts, trade_history=trade_history, loan_history=loan_history, asset_movements=asset_movements, eth_transactions=eth_transactions, ) return result
def test_process_polo_loans_unexpected_data(): """Test that with unexpected data the offending loan is skipped and an error generated""" msg_aggregator = MessagesAggregator() broken_loan = TEST_POLO_LOAN_1.copy() broken_loan['close'] = 'xx2017-xxs07-22 21:18:37' loans = process_polo_loans(msg_aggregator, [broken_loan, TEST_POLO_LOAN_2], 0, 1564262858) assert len(loans) == 1 assert loans[0].close_time == Timestamp(1487027104) assert len(msg_aggregator.consume_errors()) == 1 broken_loan = TEST_POLO_LOAN_1.copy() broken_loan['open'] = 'xx2017-xxs07-22 21:18:37' loans = process_polo_loans(msg_aggregator, [broken_loan, TEST_POLO_LOAN_2], 0, 1564262858) assert len(loans) == 1 assert loans[0].close_time == Timestamp(1487027104) assert len(msg_aggregator.consume_errors()) == 1 broken_loan = TEST_POLO_LOAN_1.copy() broken_loan['fee'] = 'sdad' loans = process_polo_loans(msg_aggregator, [broken_loan, TEST_POLO_LOAN_2], 0, 1564262858) assert len(loans) == 1 assert loans[0].close_time == Timestamp(1487027104) assert len(msg_aggregator.consume_errors()) == 1 broken_loan = TEST_POLO_LOAN_1.copy() broken_loan['earned'] = None loans = process_polo_loans(msg_aggregator, [broken_loan, TEST_POLO_LOAN_2], 0, 1564262858) assert len(loans) == 1 assert loans[0].close_time == Timestamp(1487027104) assert len(msg_aggregator.consume_errors()) == 1 broken_loan = TEST_POLO_LOAN_1.copy() broken_loan['amount'] = ['something'] loans = process_polo_loans(msg_aggregator, [broken_loan, TEST_POLO_LOAN_2], 0, 1564262858) assert len(loans) == 1 assert loans[0].close_time == Timestamp(1487027104) assert len(msg_aggregator.consume_errors()) == 1 # And finally test that missing an expected entry is also handled broken_loan = TEST_POLO_LOAN_1.copy() del broken_loan['amount'] loans = process_polo_loans(msg_aggregator, [broken_loan, TEST_POLO_LOAN_2], 0, 1564262858) assert len(loans) == 1 assert loans[0].close_time == Timestamp(1487027104) assert len(msg_aggregator.consume_errors()) == 1
def populate_history_cb( trades_history: List[Trade], margin_history: List[MarginPosition], result_asset_movements: List[AssetMovement], exchange_specific_data: Any, ) -> None: """This callback will run for succesfull exchange history query""" history.extend(trades_history) history.extend(margin_history) asset_movements.extend(result_asset_movements) if exchange_specific_data: # This can only be poloniex at the moment polo_loans_data = exchange_specific_data polo_loans.extend(process_polo_loans( msg_aggregator=self.msg_aggregator, data=polo_loans_data, start_ts=start_ts, end_ts=end_ts, ))
def populate_history_cb( result_history: Union[List[Trade], List[MarginPosition]], result_asset_movements: List[AssetMovement], exchange_specific_data: Any, ) -> None: """This callback will run for succesfull exchange history query""" history.extend(result_history) asset_movements.extend(result_asset_movements) if exchange_specific_data: # This can only be poloniex at the moment polo_loans_data = exchange_specific_data polo_loans.extend( process_polo_loans( msg_aggregator=self.msg_aggregator, data=polo_loans_data, start_ts=start_ts, end_ts=end_ts, )) loansfile_path = os.path.join(self.user_directory, LOANS_HISTORYFILE) write_history_data_in_file(polo_loans, loansfile_path, start_ts, end_ts)
def accounting_history_process( accountant, start_ts: Timestamp, end_ts: Timestamp, history_list: List[Dict], margin_list: List[MarginPosition] = None, loans_list: List[Dict] = None, asset_movements_list: List[AssetMovement] = None, eth_transaction_list: List[Dict] = None, defi_events_list: List[DefiEvent] = None, ledger_actions_list: List[LedgerAction] = None, ) -> Tuple[Dict[str, Any], List[Dict[str, Any]]]: trade_history: Sequence[Union[Trade, MarginPosition]] # For filtering the taxable actions list we start with 0 ts so that we have the # full history available trade_history = trades_from_dictlist( given_trades=history_list, start_ts=Timestamp(0), end_ts=end_ts, location='accounting_history_process for tests', msg_aggregator=accountant.msg_aggregator, ) # if present, append margin positions to trade history if margin_list: trade_history.extend(margin_list) # type: ignore asset_movements = [] if asset_movements_list: asset_movements = asset_movements_list loan_history = [] if loans_list: loan_history = process_polo_loans( msg_aggregator=accountant.msg_aggregator, data=loans_list, start_ts=Timestamp(0), end_ts=end_ts, ) eth_transactions = [] if eth_transaction_list: eth_transactions = eth_transaction_list defi_events = [] if defi_events_list: defi_events = defi_events_list ledger_actions = [] if ledger_actions_list: ledger_actions = ledger_actions_list report_id = accountant.process_history( start_ts=start_ts, end_ts=end_ts, trade_history=trade_history, loan_history=loan_history, asset_movements=asset_movements, eth_transactions=eth_transactions, defi_events=defi_events, ledger_actions=ledger_actions, ) dbpnl = DBAccountingReports(accountant.csvexporter.database) report = dbpnl.get_reports(report_id=report_id, with_limit=False)[0][0] events = dbpnl.get_report_data( filter_=ReportDataFilterQuery.make(report_id=1), with_limit=False, )[0] return report, events