non batched utxo fetch if server doesn't support batching

This commit is contained in:
julian 2023-04-17 08:27:44 -06:00
parent 93b040bc5f
commit ba77b40674

View file

@ -680,29 +680,46 @@ class ECashWallet extends CoinServiceAPI
try { try {
final fetchedUtxoList = <List<Map<String, dynamic>>>[]; final fetchedUtxoList = <List<Map<String, dynamic>>>[];
final Map<int, Map<String, List<dynamic>>> batches = {}; if (serverCanBatch) {
const batchSizeMax = 100; final Map<int, Map<String, List<dynamic>>> batches = {};
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].value,
_network,
);
batches[batchNumber]!.addAll({
scriptHash: [scriptHash]
});
if (i % batchSizeMax == batchSizeMax - 1) {
batchNumber++;
}
} }
final scripthash =
AddressUtils.convertToScriptHash(allAddresses[i].value, _network);
batches[batchNumber]!.addAll({
scripthash: [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.getBatchUTXOs(
await _electrumXClient.getBatchUTXOs(args: batches[i]!); args: batches[i]!,
for (final entry in response.entries) { );
if (entry.value.isNotEmpty) { for (final entry in response.entries) {
fetchedUtxoList.add(entry.value); if (entry.value.isNotEmpty) {
fetchedUtxoList.add(entry.value);
}
}
}
} else {
for (int i = 0; i < allAddresses.length; i++) {
final scriptHash = AddressUtils.convertToScriptHash(
allAddresses[i].value,
_network,
);
final utxos = await electrumXClient.getUTXOs(scripthash: scriptHash);
if (utxos.isNotEmpty) {
fetchedUtxoList.add(utxos);
} }
} }
} }