From 813190d78c2946866a74d024eae6abac8295591a Mon Sep 17 00:00:00 2001 From: Catrya <140891948+Catrya@users.noreply.github.com> Date: Tue, 5 Aug 2025 12:07:02 -0600 Subject: [PATCH 1/2] fix: handle out_of_range_sats_amount error with session cleanup and retry mechanism - Add specific handling for out_of_range_sats_amount cant_do messages - Implement session cleanup to remove temporary sessions on retry - Add reset functionality in AddOrderNotifier for failed order attempts - Generate new requestId and trade key for each retry attempt --- .../notfiers/abstract_mostro_notifier.dart | 11 ++++++++ .../order/notfiers/add_order_notifier.dart | 26 +++++++++++++++++++ lib/shared/notifiers/session_notifier.dart | 12 +++++++++ 3 files changed, 49 insertions(+) diff --git a/lib/features/order/notfiers/abstract_mostro_notifier.dart b/lib/features/order/notfiers/abstract_mostro_notifier.dart index 1b90c9773..9c35db047 100644 --- a/lib/features/order/notfiers/abstract_mostro_notifier.dart +++ b/lib/features/order/notfiers/abstract_mostro_notifier.dart @@ -219,6 +219,17 @@ class AbstractMostroNotifier extends StateNotifier { break; case Action.cantDo: final cantDo = event.getPayload(); + + // Handle specific case of out_of_range_sats_amount + if (cantDo?.cantDoReason.toString() == 'out_of_range_sats_amount') { + logger.i('Received out_of_range_sats_amount, cleaning up session for retry'); + + // Clean up temporary session if it exists by requestId + if (event.requestId != null) { + ref.read(sessionNotifierProvider.notifier).cleanupRequestSession(event.requestId!); + } + } + ref.read(notificationProvider.notifier).showInformation( event.action, values: { diff --git a/lib/features/order/notfiers/add_order_notifier.dart b/lib/features/order/notfiers/add_order_notifier.dart index 458029c91..2094c19da 100644 --- a/lib/features/order/notfiers/add_order_notifier.dart +++ b/lib/features/order/notfiers/add_order_notifier.dart @@ -5,6 +5,7 @@ import 'package:mostro_mobile/data/models.dart'; import 'package:mostro_mobile/shared/providers.dart'; import 'package:mostro_mobile/features/order/notfiers/abstract_mostro_notifier.dart'; import 'package:mostro_mobile/features/order/providers/order_notifier_provider.dart'; +import 'package:mostro_mobile/features/order/models/order_state.dart'; import 'package:mostro_mobile/services/mostro_service.dart'; class AddOrderNotifier extends AbstractMostroNotifier { @@ -40,6 +41,12 @@ class AddOrderNotifier extends AbstractMostroNotifier { } } else if (msg.payload is CantDo) { handleEvent(msg); + + // Reset for retry if out_of_range_sats_amount + final cantDo = msg.getPayload(); + if (cantDo?.cantDoReason.toString() == 'out_of_range_sats_amount') { + _resetForRetry(); + } } } }, @@ -76,4 +83,23 @@ class AddOrderNotifier extends AbstractMostroNotifier { await mostroService.submitOrder(message); state = state.updateWith(message); } + + /// Reset notifier state for retry after out_of_range_sats_amount error + void _resetForRetry() { + logger.i('Resetting AddOrderNotifier for retry after out_of_range_sats_amount'); + + // Generate new requestId for next attempt + requestId = _requestIdFromOrderId(orderId); + + // Reset state to initial clean state + state = OrderState( + action: Action.newOrder, + status: Status.pending, + order: null, + ); + + // Re-subscribe with new requestId + subscription?.close(); + subscribe(); + } } diff --git a/lib/shared/notifiers/session_notifier.dart b/lib/shared/notifiers/session_notifier.dart index 587eef9f9..6df09e611 100644 --- a/lib/shared/notifiers/session_notifier.dart +++ b/lib/shared/notifiers/session_notifier.dart @@ -160,6 +160,18 @@ class SessionNotifier extends StateNotifier> { state = sessions; } + /// Clean up temporary session by requestId + /// Used when order creation fails and needs retry + void cleanupRequestSession(int requestId) { + final session = _requestIdToSession.remove(requestId); + if (session != null) { + // Remove from state list if it was a temporary session + final updatedSessions = sessions.where((s) => s != session).toList(); + state = updatedSessions; + _logger.d('Cleaned up temporary session for requestId: $requestId'); + } + } + NostrKeyPairs calculateSharedKey( String tradePrivateKey, String counterpartyPublicKey) { try { From 200371376bcdfc3222970ec59be31c2386b14552 Mon Sep 17 00:00:00 2001 From: Catrya <140891948+Catrya@users.noreply.github.com> Date: Tue, 5 Aug 2025 12:31:46 -0600 Subject: [PATCH 2/2] use direct enum comparison for CantDoReason checks --- lib/features/order/notfiers/abstract_mostro_notifier.dart | 2 +- lib/features/order/notfiers/add_order_notifier.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/features/order/notfiers/abstract_mostro_notifier.dart b/lib/features/order/notfiers/abstract_mostro_notifier.dart index 9c35db047..d7e11cc10 100644 --- a/lib/features/order/notfiers/abstract_mostro_notifier.dart +++ b/lib/features/order/notfiers/abstract_mostro_notifier.dart @@ -221,7 +221,7 @@ class AbstractMostroNotifier extends StateNotifier { final cantDo = event.getPayload(); // Handle specific case of out_of_range_sats_amount - if (cantDo?.cantDoReason.toString() == 'out_of_range_sats_amount') { + if (cantDo?.cantDoReason == CantDoReason.outOfRangeSatsAmount) { logger.i('Received out_of_range_sats_amount, cleaning up session for retry'); // Clean up temporary session if it exists by requestId diff --git a/lib/features/order/notfiers/add_order_notifier.dart b/lib/features/order/notfiers/add_order_notifier.dart index 2094c19da..c6eb36fd8 100644 --- a/lib/features/order/notfiers/add_order_notifier.dart +++ b/lib/features/order/notfiers/add_order_notifier.dart @@ -44,7 +44,7 @@ class AddOrderNotifier extends AbstractMostroNotifier { // Reset for retry if out_of_range_sats_amount final cantDo = msg.getPayload(); - if (cantDo?.cantDoReason.toString() == 'out_of_range_sats_amount') { + if (cantDo?.cantDoReason == CantDoReason.outOfRangeSatsAmount) { _resetForRetry(); } }