2025-05-17 01:03:02 +02:00
import ' dart:async ' ;
2024-11-09 19:59:47 +01:00
import ' package:cw_monero/api/account_list.dart ' ;
2021-12-24 14:52:08 +02:00
import ' package:cw_monero/api/structs/pending_transaction.dart ' ;
import ' package:cw_monero/api/transaction_history.dart '
as monero_transaction_history ;
import ' package:cw_core/crypto_currency.dart ' ;
2022-11-03 14:45:50 -04:00
import ' package:cw_core/amount_converter.dart ' ;
2022-01-17 21:46:49 +06:00
2021-12-24 14:52:08 +02:00
import ' package:cw_core/pending_transaction.dart ' ;
2024-11-26 14:43:56 +01:00
import ' package:cw_monero/api/wallet.dart ' ;
2025-05-15 18:48:43 +02:00
import ' package:cw_monero/monero_wallet.dart ' ;
2021-12-24 14:52:08 +02:00
class DoubleSpendException implements Exception {
DoubleSpendException ( ) ;
@ override
String toString ( ) = >
' This transaction cannot be committed. This can be due to many reasons including the wallet not being synced, there is not enough XMR in your available balance, or previous transactions are not yet fully processed. ' ;
}
class PendingMoneroTransaction with PendingTransaction {
2025-05-15 18:48:43 +02:00
PendingMoneroTransaction ( this . pendingTransactionDescription , this . wallet ) ;
2021-12-24 14:52:08 +02:00
final PendingTransactionDescription pendingTransactionDescription ;
2025-05-15 18:48:43 +02:00
final MoneroWalletBase wallet ;
2021-12-24 14:52:08 +02:00
@ override
String get id = > pendingTransactionDescription . hash ;
2022-07-28 18:03:16 +01:00
@ override
String get hex = > pendingTransactionDescription . hex ;
String get txKey = > pendingTransactionDescription . txKey ;
2021-12-24 14:52:08 +02:00
@ override
2022-11-03 14:45:50 -04:00
String get amountFormatted = > AmountConverter . amountIntToString (
CryptoCurrency . xmr , pendingTransactionDescription . amount ) ;
2021-12-24 14:52:08 +02:00
@ override
2022-11-03 14:45:50 -04:00
String get feeFormatted = > AmountConverter . amountIntToString (
CryptoCurrency . xmr , pendingTransactionDescription . fee ) ;
2021-12-24 14:52:08 +02:00
2024-11-09 19:59:47 +01:00
bool shouldCommitUR ( ) = > isViewOnly ;
2021-12-24 14:52:08 +02:00
@ override
Future < void > commit ( ) async {
try {
2025-05-24 21:21:49 +02:00
await monero_transaction_history . commitTransactionFromPointerAddress (
2024-11-09 19:59:47 +01:00
address: pendingTransactionDescription . pointerAddress ,
useUR: false ) ;
} catch ( e ) {
final message = e . toString ( ) ;
if ( message . contains ( ' Reason: double spend ' ) ) {
throw DoubleSpendException ( ) ;
}
rethrow ;
}
2024-11-26 14:43:56 +01:00
storeSync ( force: true ) ;
2025-05-17 01:03:02 +02:00
unawaited ( ( ) async {
await Future . delayed ( const Duration ( milliseconds: 250 ) ) ;
await wallet . fetchTransactions ( ) ;
} ( ) ) ;
2024-11-09 19:59:47 +01:00
}
@ override
Future < String ? > commitUR ( ) async {
try {
2025-05-24 21:21:49 +02:00
final ret = await monero_transaction_history . commitTransactionFromPointerAddress (
2024-11-09 19:59:47 +01:00
address: pendingTransactionDescription . pointerAddress ,
useUR: true ) ;
2025-05-15 18:48:43 +02:00
storeSync ( force: true ) ;
2025-05-17 01:03:02 +02:00
unawaited ( ( ) async {
await Future . delayed ( const Duration ( milliseconds: 250 ) ) ;
await wallet . fetchTransactions ( ) ;
} ( ) ) ;
2024-11-09 19:59:47 +01:00
return ret ;
2021-12-24 14:52:08 +02:00
} catch ( e ) {
final message = e . toString ( ) ;
if ( message . contains ( ' Reason: double spend ' ) ) {
throw DoubleSpendException ( ) ;
}
rethrow ;
}
}
}