From 0a2f710a87a91b66e8fce815a97363bb968bfa29 Mon Sep 17 00:00:00 2001 From: Alaa Elattar Date: Tue, 20 May 2025 13:45:09 +0300 Subject: [PATCH 1/4] check when op_low_reserve error --- packages/stellar_client/lib/src/client.dart | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/stellar_client/lib/src/client.dart b/packages/stellar_client/lib/src/client.dart index 0634a02d..22592b5c 100644 --- a/packages/stellar_client/lib/src/client.dart +++ b/packages/stellar_client/lib/src/client.dart @@ -667,9 +667,14 @@ class Client { final SubmitTransactionResponse response = await _sdk.submitTransaction(transaction); if (!response.success) { + if (response.extras!.resultCodes!.operationsResultCodes! + .contains('op_low_reserve')) { + throw Exception('Transaction failed due to low reserve.'); + } logger.e('Transaction failed with result: ${response.resultXdr}'); return false; } + return true; } catch (error) { throw Exception('Transaction failed due to: ${error.toString()}'); From 390789e876c3899b5a89fcb71aa4374bb318c85b Mon Sep 17 00:00:00 2001 From: Alaa Elattar Date: Tue, 20 May 2025 13:46:36 +0300 Subject: [PATCH 2/4] include cancel, update orders --- packages/stellar_client/lib/src/client.dart | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/stellar_client/lib/src/client.dart b/packages/stellar_client/lib/src/client.dart index 22592b5c..fafee0de 100644 --- a/packages/stellar_client/lib/src/client.dart +++ b/packages/stellar_client/lib/src/client.dart @@ -711,6 +711,10 @@ class Client { final SubmitTransactionResponse response = await _sdk.submitTransaction(transaction); if (!response.success) { + if (response.extras!.resultCodes!.operationsResultCodes! + .contains('op_low_reserve')) { + throw Exception('Transaction failed due to low reserve.'); + } logger.e('Transaction failed with result: ${response.resultXdr}'); return false; } @@ -760,6 +764,10 @@ class Client { final SubmitTransactionResponse response = await _sdk.submitTransaction(transaction); if (!response.success) { + if (response.extras!.resultCodes!.operationsResultCodes! + .contains('op_low_reserve')) { + throw Exception('Transaction failed due to low reserve.'); + } logger.e('Transaction failed with result: ${response.resultXdr}'); return false; } From 54377197732fb9e11054da30d2bf0c54ee9109e5 Mon Sep 17 00:00:00 2001 From: Alaa Elattar Date: Tue, 20 May 2025 13:52:35 +0300 Subject: [PATCH 3/4] use ? instead of ! --- packages/stellar_client/lib/src/client.dart | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/stellar_client/lib/src/client.dart b/packages/stellar_client/lib/src/client.dart index fafee0de..f91869b3 100644 --- a/packages/stellar_client/lib/src/client.dart +++ b/packages/stellar_client/lib/src/client.dart @@ -667,8 +667,9 @@ class Client { final SubmitTransactionResponse response = await _sdk.submitTransaction(transaction); if (!response.success) { - if (response.extras!.resultCodes!.operationsResultCodes! - .contains('op_low_reserve')) { + if (response.extras?.resultCodes?.operationsResultCodes + ?.contains('op_low_reserve') == + true) { throw Exception('Transaction failed due to low reserve.'); } logger.e('Transaction failed with result: ${response.resultXdr}'); @@ -711,8 +712,9 @@ class Client { final SubmitTransactionResponse response = await _sdk.submitTransaction(transaction); if (!response.success) { - if (response.extras!.resultCodes!.operationsResultCodes! - .contains('op_low_reserve')) { + if (response.extras?.resultCodes?.operationsResultCodes + ?.contains('op_low_reserve') == + true) { throw Exception('Transaction failed due to low reserve.'); } logger.e('Transaction failed with result: ${response.resultXdr}'); @@ -764,8 +766,9 @@ class Client { final SubmitTransactionResponse response = await _sdk.submitTransaction(transaction); if (!response.success) { - if (response.extras!.resultCodes!.operationsResultCodes! - .contains('op_low_reserve')) { + if (response.extras?.resultCodes?.operationsResultCodes + ?.contains('op_low_reserve') == + true) { throw Exception('Transaction failed due to low reserve.'); } logger.e('Transaction failed with result: ${response.resultXdr}'); From ec1bc33ef387f339d84de6df60de3260de892a58 Mon Sep 17 00:00:00 2001 From: Alaa Elattar Date: Tue, 20 May 2025 14:58:40 +0300 Subject: [PATCH 4/4] implement error check type --- .../stellar_client/lib/models/exceptions.dart | 36 +++++++++++++++++ packages/stellar_client/lib/src/client.dart | 39 +++++++++---------- .../stellar_client/lib/stellar_client.dart | 1 + 3 files changed, 55 insertions(+), 21 deletions(-) create mode 100644 packages/stellar_client/lib/models/exceptions.dart diff --git a/packages/stellar_client/lib/models/exceptions.dart b/packages/stellar_client/lib/models/exceptions.dart new file mode 100644 index 00000000..cb4ece1a --- /dev/null +++ b/packages/stellar_client/lib/models/exceptions.dart @@ -0,0 +1,36 @@ +class StellarBalanceException implements Exception { + final String message; + final String? resultXdr; + final bool isLowReserve; + + StellarBalanceException({ + required this.message, + this.resultXdr, + this.isLowReserve = false, + }); + + @override + String toString() { + if (resultXdr != null) { + return 'StellarBalanceException: $message (XDR: $resultXdr)'; + } + return 'StellarBalanceException: $message'; + } + + static StellarBalanceException fromOperationResult( + List? operationCodes, + String? resultXdr, + ) { + if (operationCodes?.contains('op_low_reserve') == true) { + return StellarBalanceException( + message: 'Transaction failed due to low reserve', + resultXdr: resultXdr, + isLowReserve: true, + ); + } + return StellarBalanceException( + message: 'Transaction failed', + resultXdr: resultXdr, + ); + } +} diff --git a/packages/stellar_client/lib/src/client.dart b/packages/stellar_client/lib/src/client.dart index f91869b3..77f3fef1 100644 --- a/packages/stellar_client/lib/src/client.dart +++ b/packages/stellar_client/lib/src/client.dart @@ -667,16 +667,15 @@ class Client { final SubmitTransactionResponse response = await _sdk.submitTransaction(transaction); if (!response.success) { - if (response.extras?.resultCodes?.operationsResultCodes - ?.contains('op_low_reserve') == - true) { - throw Exception('Transaction failed due to low reserve.'); - } - logger.e('Transaction failed with result: ${response.resultXdr}'); - return false; + throw StellarBalanceException.fromOperationResult( + response.extras?.resultCodes?.operationsResultCodes, + response.resultXdr, + ); } return true; + } on StellarBalanceException catch (_) { + rethrow; } catch (error) { throw Exception('Transaction failed due to: ${error.toString()}'); } @@ -712,15 +711,14 @@ class Client { final SubmitTransactionResponse response = await _sdk.submitTransaction(transaction); if (!response.success) { - if (response.extras?.resultCodes?.operationsResultCodes - ?.contains('op_low_reserve') == - true) { - throw Exception('Transaction failed due to low reserve.'); - } - logger.e('Transaction failed with result: ${response.resultXdr}'); - return false; + throw StellarBalanceException.fromOperationResult( + response.extras?.resultCodes?.operationsResultCodes, + response.resultXdr, + ); } return true; + } on StellarBalanceException catch (_) { + rethrow; } catch (error) { throw Exception('Transaction failed due to: ${error.toString()}'); } @@ -766,15 +764,14 @@ class Client { final SubmitTransactionResponse response = await _sdk.submitTransaction(transaction); if (!response.success) { - if (response.extras?.resultCodes?.operationsResultCodes - ?.contains('op_low_reserve') == - true) { - throw Exception('Transaction failed due to low reserve.'); - } - logger.e('Transaction failed with result: ${response.resultXdr}'); - return false; + throw StellarBalanceException.fromOperationResult( + response.extras?.resultCodes?.operationsResultCodes, + response.resultXdr, + ); } return true; + } on StellarBalanceException catch (_) { + rethrow; } catch (error) { throw Exception('Transaction failed due to: ${error.toString()}'); } diff --git a/packages/stellar_client/lib/stellar_client.dart b/packages/stellar_client/lib/stellar_client.dart index e476efd0..d16e6194 100644 --- a/packages/stellar_client/lib/stellar_client.dart +++ b/packages/stellar_client/lib/stellar_client.dart @@ -5,6 +5,7 @@ import 'dart:typed_data'; import 'package:stellar_client/models/balance.dart'; import 'package:stellar_client/models/currency.dart' as currency; +import 'package:stellar_client/models/exceptions.dart'; import 'package:stellar_client/models/transaction_data.dart'; import 'package:stellar_client/models/vesting_account.dart'; import 'package:stellar_client/models/transaction.dart';