From ba77b40674b47f61769aaa650d55615dc619bb39 Mon Sep 17 00:00:00 2001 From: julian Date: Mon, 17 Apr 2023 08:27:44 -0600 Subject: [PATCH] non batched utxo fetch if server doesn't support batching --- lib/services/coins/ecash/ecash_wallet.dart | 59 ++++++++++++++-------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/lib/services/coins/ecash/ecash_wallet.dart b/lib/services/coins/ecash/ecash_wallet.dart index d7c1b08db..9ee72eccf 100644 --- a/lib/services/coins/ecash/ecash_wallet.dart +++ b/lib/services/coins/ecash/ecash_wallet.dart @@ -680,29 +680,46 @@ class ECashWallet extends CoinServiceAPI try { final fetchedUtxoList = >>[]; - final Map>> batches = {}; - const batchSizeMax = 100; - int batchNumber = 0; - for (int i = 0; i < allAddresses.length; i++) { - if (batches[batchNumber] == null) { - batches[batchNumber] = {}; + if (serverCanBatch) { + final Map>> batches = {}; + const batchSizeMax = 100; + int batchNumber = 0; + for (int i = 0; i < allAddresses.length; i++) { + 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++) { - final response = - await _electrumXClient.getBatchUTXOs(args: batches[i]!); - for (final entry in response.entries) { - if (entry.value.isNotEmpty) { - fetchedUtxoList.add(entry.value); + for (int i = 0; i < batches.length; i++) { + final response = await _electrumXClient.getBatchUTXOs( + args: batches[i]!, + ); + for (final entry in response.entries) { + 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); } } }