diff --git a/packages/stellar_client/lib/models/exceptions.dart b/packages/stellar_client/lib/models/exceptions.dart new file mode 100644 index 0000000..cb4ece1 --- /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 0634a02..77f3fef 100644 --- a/packages/stellar_client/lib/src/client.dart +++ b/packages/stellar_client/lib/src/client.dart @@ -667,10 +667,15 @@ class Client { final SubmitTransactionResponse response = await _sdk.submitTransaction(transaction); if (!response.success) { - 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()}'); } @@ -706,10 +711,14 @@ class Client { final SubmitTransactionResponse response = await _sdk.submitTransaction(transaction); if (!response.success) { - 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()}'); } @@ -755,10 +764,14 @@ class Client { final SubmitTransactionResponse response = await _sdk.submitTransaction(transaction); if (!response.success) { - 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 e476efd..d16e619 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';