This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from MXCzkEVM/tx_records
feat: Tx records
- Loading branch information
Showing
21 changed files
with
378 additions
and
180 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
lib/common/components/recent_transactions/domain/transactions_repository.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'package:datadashwallet/common/components/recent_transactions/entity/transaction_history_model.dart'; | ||
import 'package:mxc_logic/mxc_logic.dart'; | ||
import 'package:datadashwallet/core/core.dart'; | ||
|
||
class TransactionsHistoryRepository extends ControlledCacheRepository { | ||
@override | ||
final String zone = 'transaction-history'; | ||
|
||
late final Field<List<TransactionHistoryModel>> transactionsHistory = | ||
fieldWithDefault<List<TransactionHistoryModel>>('items', [], | ||
serializer: (b) => b | ||
.map((e) => { | ||
'chainId': e.chainId, | ||
'txList': e.txList.map((e) => e.toMap()).toList() | ||
}) | ||
.toList(), | ||
deserializer: (b) => (b as List) | ||
.map((e) => TransactionHistoryModel( | ||
chainId: e['chainId'], | ||
txList: (e['txList'] as List) | ||
.map((e) => TransactionModel.fromMap(e)) | ||
.toList(), | ||
)) | ||
.toList()); | ||
|
||
List<TransactionHistoryModel> get items => transactionsHistory.value; | ||
|
||
void addItem(TransactionHistoryModel item) => | ||
transactionsHistory.value = [...transactionsHistory.value, item]; | ||
|
||
void addItemTx(TransactionModel item, int index) { | ||
final newList = transactionsHistory.value; | ||
newList[index].txList.insert(0, item); | ||
transactionsHistory.value = newList; | ||
} | ||
|
||
void updateItem(TransactionHistoryModel item, int index) { | ||
final newList = transactionsHistory.value; | ||
newList.removeAt(index); | ||
newList.insert(index, item); | ||
transactionsHistory.value = newList; | ||
} | ||
|
||
void updateItemTx(TransactionModel item, int index, int txIndex) { | ||
final newList = transactionsHistory.value; | ||
|
||
newList[index].txList[txIndex] = item; | ||
|
||
transactionsHistory.value = newList; | ||
} | ||
|
||
void removeItem(TransactionHistoryModel item) => | ||
transactionsHistory.value = transactionsHistory.value | ||
.where((e) => e.chainId != item.chainId) | ||
.toList(); | ||
|
||
void removeAll() => transactionsHistory.value = []; | ||
} |
86 changes: 86 additions & 0 deletions
86
lib/common/components/recent_transactions/domain/transactions_use_case.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import 'package:datadashwallet/core/core.dart'; | ||
import 'package:mxc_logic/mxc_logic.dart'; | ||
import '../entity/transaction_history_model.dart'; | ||
import 'transactions_repository.dart'; | ||
|
||
class TransactionsHistoryUseCase extends ReactiveUseCase { | ||
TransactionsHistoryUseCase(this._repository, this._web3Repository); | ||
|
||
final Web3Repository _web3Repository; | ||
|
||
final TransactionsHistoryRepository _repository; | ||
|
||
late final ValueStream<List<TransactionHistoryModel>> transactionsHistory = | ||
reactiveField(_repository.transactionsHistory); | ||
|
||
List<TransactionHistoryModel> getTransactionsHistory() => _repository.items; | ||
|
||
List<String> updatingTxList = []; | ||
|
||
void addItem(TransactionHistoryModel item) { | ||
_repository.addItem(item); | ||
update(transactionsHistory, _repository.items); | ||
} | ||
|
||
void updateItem(TransactionHistoryModel item, int index) { | ||
_repository.updateItem(item, index); | ||
update(transactionsHistory, _repository.items); | ||
} | ||
|
||
void updateItemTx(TransactionModel item, int selectedNetworkChainId) { | ||
final txHistory = transactionsHistory.value | ||
.firstWhere((element) => element.chainId == selectedNetworkChainId); | ||
|
||
final index = transactionsHistory.value.indexOf(txHistory); | ||
final txIndex = txHistory.txList.indexWhere( | ||
(element) => element.hash == item.hash, | ||
); | ||
|
||
if (txIndex == -1) { | ||
_repository.addItemTx(item, index); | ||
} else { | ||
_repository.updateItemTx(item, index, txIndex); | ||
} | ||
|
||
update(transactionsHistory, _repository.items); | ||
} | ||
|
||
void removeAll() { | ||
_repository.removeAll(); | ||
update(transactionsHistory, _repository.items); | ||
} | ||
|
||
void removeItem(TransactionHistoryModel item) { | ||
_repository.removeItem(item); | ||
update(transactionsHistory, _repository.items); | ||
} | ||
|
||
void spyOnTransaction(TransactionModel item, int chainId) { | ||
if (!updatingTxList.contains(item.hash)) { | ||
updatingTxList.add(item.hash); | ||
final stream = _web3Repository.tokenContract.spyTransaction(item.hash); | ||
stream.onData((succeeded) { | ||
if (succeeded) { | ||
final updatedItem = item.copyWith(status: TransactionStatus.done); | ||
updateItemTx(updatedItem, chainId); | ||
updatingTxList.remove(item.hash); | ||
stream.cancel(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
void checkForPendingTransactions(int chainId) { | ||
final index = transactionsHistory.value | ||
.indexWhere((element) => element.chainId == chainId); | ||
|
||
if (index != -1) { | ||
final chainTx = transactionsHistory.value[index]; | ||
final pendingTxList = chainTx.txList | ||
.where((element) => element.status == TransactionStatus.failed); | ||
for (TransactionModel pendingTx in pendingTxList) { | ||
spyOnTransaction(pendingTx, chainId); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
lib/common/components/recent_transactions/entity/transaction_history_model.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import 'package:mxc_logic/mxc_logic.dart'; | ||
|
||
class TransactionHistoryModel { | ||
TransactionHistoryModel({ | ||
required this.chainId, | ||
required this.txList, | ||
}); | ||
|
||
int chainId; | ||
List<TransactionModel> txList; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/common/components/recent_transactions/widgets/recent_transaction_item.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.