def update_transaction(response: Dict, transaction: Transaction, error_msg: str = None): """ Updates the transaction depending on whether or not the transaction was successfully executed on the Stellar network and `process_withdrawal` completed without raising an exception. If the Horizon response indicates the response was not successful or an exception was raised while processing the withdrawal, we mark the status as `error`. If the Stellar transaction succeeded, we mark it as `completed`. :param error_msg: a description of the error that has occurred. :param response: a response body returned from Horizon for the transaction :param transaction: a database model object representing the transaction """ if error_msg or not response["successful"]: transaction.status = Transaction.STATUS.error transaction.status_message = error_msg else: transaction.completed_at = now() transaction.status = Transaction.STATUS.completed transaction.status_eta = 0 transaction.amount_out = transaction.amount_in - transaction.amount_fee transaction.stellar_transaction_id = response["id"] transaction.save()
def submit(cls, transaction: Transaction) -> bool: valid_statuses = [ Transaction.STATUS.pending_user_transfer_start, Transaction.STATUS.pending_external, Transaction.STATUS.pending_anchor, Transaction.STATUS.pending_trust, ] if transaction.status not in valid_statuses: raise ValueError( f"Unexpected transaction status: {transaction.status}, expecting " f"{' or '.join(valid_statuses)}.") transaction.status = Transaction.STATUS.pending_anchor transaction.save() logger.info(f"Initiating Stellar deposit for {transaction.id}") maybe_make_callback(transaction) if transaction.envelope_xdr: try: envelope = TransactionEnvelope.from_xdr( transaction.envelope_xdr, settings.STELLAR_NETWORK_PASSPHRASE) except Exception: cls._handle_error(transaction, "Failed to decode transaction envelope") return False else: distribution_acc, _ = get_account_obj( Keypair.from_public_key( transaction.asset.distribution_account)) envelope = cls.create_deposit_envelope(transaction, distribution_acc) envelope.sign(transaction.asset.distribution_seed) transaction.status = Transaction.STATUS.pending_stellar transaction.save() logger.info(f"Transaction {transaction.id} now pending_stellar") maybe_make_callback(transaction) try: response = settings.HORIZON_SERVER.submit_transaction(envelope) except BaseHorizonError as e: cls._handle_error(transaction, f"{e.__class__.__name__}: {e.message}") return False if not response.get("successful"): cls._handle_error( transaction, f"Stellar transaction failed when submitted to horizon: {response['result_xdr']}", ) return False elif transaction.claimable_balance_supported: transaction.claimable_balance_id = cls.get_balance_id(response) transaction.envelope_xdr = response["envelope_xdr"] transaction.paging_token = response["paging_token"] transaction.stellar_transaction_id = response["id"] transaction.status = Transaction.STATUS.completed transaction.completed_at = datetime.datetime.now(datetime.timezone.utc) transaction.amount_out = round( Decimal(transaction.amount_in) - Decimal(transaction.amount_fee), transaction.asset.significant_decimals, ) transaction.save() logger.info(f"Transaction {transaction.id} completed.") maybe_make_callback(transaction) return True