Skip to content

Commit 488ca66

Browse files
authored
check when op_low_reserve error (#159)
* check when op_low_reserve error * include cancel, update orders * use ? instead of ! * implement error check type
1 parent 66b6d64 commit 488ca66

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class StellarBalanceException implements Exception {
2+
final String message;
3+
final String? resultXdr;
4+
final bool isLowReserve;
5+
6+
StellarBalanceException({
7+
required this.message,
8+
this.resultXdr,
9+
this.isLowReserve = false,
10+
});
11+
12+
@override
13+
String toString() {
14+
if (resultXdr != null) {
15+
return 'StellarBalanceException: $message (XDR: $resultXdr)';
16+
}
17+
return 'StellarBalanceException: $message';
18+
}
19+
20+
static StellarBalanceException fromOperationResult(
21+
List<String?>? operationCodes,
22+
String? resultXdr,
23+
) {
24+
if (operationCodes?.contains('op_low_reserve') == true) {
25+
return StellarBalanceException(
26+
message: 'Transaction failed due to low reserve',
27+
resultXdr: resultXdr,
28+
isLowReserve: true,
29+
);
30+
}
31+
return StellarBalanceException(
32+
message: 'Transaction failed',
33+
resultXdr: resultXdr,
34+
);
35+
}
36+
}

packages/stellar_client/lib/src/client.dart

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -667,10 +667,15 @@ class Client {
667667
final SubmitTransactionResponse response =
668668
await _sdk.submitTransaction(transaction);
669669
if (!response.success) {
670-
logger.e('Transaction failed with result: ${response.resultXdr}');
671-
return false;
670+
throw StellarBalanceException.fromOperationResult(
671+
response.extras?.resultCodes?.operationsResultCodes,
672+
response.resultXdr,
673+
);
672674
}
675+
673676
return true;
677+
} on StellarBalanceException catch (_) {
678+
rethrow;
674679
} catch (error) {
675680
throw Exception('Transaction failed due to: ${error.toString()}');
676681
}
@@ -706,10 +711,14 @@ class Client {
706711
final SubmitTransactionResponse response =
707712
await _sdk.submitTransaction(transaction);
708713
if (!response.success) {
709-
logger.e('Transaction failed with result: ${response.resultXdr}');
710-
return false;
714+
throw StellarBalanceException.fromOperationResult(
715+
response.extras?.resultCodes?.operationsResultCodes,
716+
response.resultXdr,
717+
);
711718
}
712719
return true;
720+
} on StellarBalanceException catch (_) {
721+
rethrow;
713722
} catch (error) {
714723
throw Exception('Transaction failed due to: ${error.toString()}');
715724
}
@@ -755,10 +764,14 @@ class Client {
755764
final SubmitTransactionResponse response =
756765
await _sdk.submitTransaction(transaction);
757766
if (!response.success) {
758-
logger.e('Transaction failed with result: ${response.resultXdr}');
759-
return false;
767+
throw StellarBalanceException.fromOperationResult(
768+
response.extras?.resultCodes?.operationsResultCodes,
769+
response.resultXdr,
770+
);
760771
}
761772
return true;
773+
} on StellarBalanceException catch (_) {
774+
rethrow;
762775
} catch (error) {
763776
throw Exception('Transaction failed due to: ${error.toString()}');
764777
}

packages/stellar_client/lib/stellar_client.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'dart:typed_data';
55

66
import 'package:stellar_client/models/balance.dart';
77
import 'package:stellar_client/models/currency.dart' as currency;
8+
import 'package:stellar_client/models/exceptions.dart';
89
import 'package:stellar_client/models/transaction_data.dart';
910
import 'package:stellar_client/models/vesting_account.dart';
1011
import 'package:stellar_client/models/transaction.dart';

0 commit comments

Comments
 (0)