non batched tx history fetching if server doesn't support batching

This commit is contained in:
julian 2023-04-17 09:28:02 -06:00
parent 9c5c0cb179
commit 9df5bc4f36

View file

@ -936,38 +936,61 @@ class ECashWallet extends CoinServiceAPI
} }
Future<List<Map<String, dynamic>>> _fetchHistory( Future<List<Map<String, dynamic>>> _fetchHistory(
List<String> allAddresses) async { List<String> allAddresses,
) async {
try { try {
List<Map<String, dynamic>> allTxHashes = []; List<Map<String, dynamic>> allTxHashes = [];
final Map<int, Map<String, List<dynamic>>> batches = {}; if (serverCanBatch) {
final Map<String, String> requestIdToAddressMap = {}; final Map<int, Map<String, List<dynamic>>> batches = {};
const batchSizeMax = 100; final Map<String, String> requestIdToAddressMap = {};
int batchNumber = 0; const batchSizeMax = 100;
for (int i = 0; i < allAddresses.length; i++) { int batchNumber = 0;
if (batches[batchNumber] == null) { for (int i = 0; i < allAddresses.length; i++) {
batches[batchNumber] = {}; if (batches[batchNumber] == null) {
batches[batchNumber] = {};
}
final scriptHash = AddressUtils.convertToScriptHash(
allAddresses[i],
_network,
);
final id = Logger.isTestEnv ? "$i" : const Uuid().v1();
requestIdToAddressMap[id] = allAddresses[i];
batches[batchNumber]!.addAll({
id: [scriptHash]
});
if (i % batchSizeMax == batchSizeMax - 1) {
batchNumber++;
}
} }
final scripthash =
AddressUtils.convertToScriptHash(allAddresses[i], _network);
final id = Logger.isTestEnv ? "$i" : const Uuid().v1();
requestIdToAddressMap[id] = allAddresses[i];
batches[batchNumber]!.addAll({
id: [scripthash]
});
if (i % batchSizeMax == batchSizeMax - 1) {
batchNumber++;
}
}
for (int i = 0; i < batches.length; i++) { for (int i = 0; i < batches.length; i++) {
final response = final response =
await _electrumXClient.getBatchHistory(args: batches[i]!); await _electrumXClient.getBatchHistory(args: batches[i]!);
for (final entry in response.entries) { for (final entry in response.entries) {
for (int j = 0; j < entry.value.length; j++) { for (int j = 0; j < entry.value.length; j++) {
entry.value[j]["address"] = requestIdToAddressMap[entry.key]; entry.value[j]["address"] = requestIdToAddressMap[entry.key];
if (!allTxHashes.contains(entry.value[j])) { if (!allTxHashes.contains(entry.value[j])) {
allTxHashes.add(entry.value[j]); allTxHashes.add(entry.value[j]);
}
}
}
}
} else {
for (int i = 0; i < allAddresses.length; i++) {
final scriptHash = AddressUtils.convertToScriptHash(
allAddresses[i],
_network,
);
final response = await electrumXClient.getHistory(
scripthash: scriptHash,
);
for (int j = 0; j < response.length; j++) {
response[j]["address"] = allAddresses[i];
if (!allTxHashes.contains(response[j])) {
allTxHashes.add(response[j]);
} }
} }
} }