Skip to content

check when op_low_reserve error #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/stellar_client/lib/models/exceptions.dart
Original file line number Diff line number Diff line change
@@ -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<String?>? 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,
);
}
}
25 changes: 19 additions & 6 deletions packages/stellar_client/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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()}');
}
Expand Down Expand Up @@ -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()}');
}
Expand Down Expand Up @@ -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()}');
}
Expand Down
1 change: 1 addition & 0 deletions packages/stellar_client/lib/stellar_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down