From 9f35b9ada1e84e6e78ef46776d4c87c944412014 Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Wed, 19 Jun 2024 11:09:24 +0200 Subject: [PATCH] feat: modify getContractFundedVaults functions --- src/network-handlers/ethereum-handler.ts | 16 +++++++++++----- .../read-only-ethereum-handler.ts | 16 +++++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/network-handlers/ethereum-handler.ts b/src/network-handlers/ethereum-handler.ts index 75dab5e..0e55dc1 100644 --- a/src/network-handlers/ethereum-handler.ts +++ b/src/network-handlers/ethereum-handler.ts @@ -133,19 +133,25 @@ export class EthereumHandler { let totalFetched = 0; const fundedVaults: RawVault[] = []; - while (true) { + let shouldContinue = true; + while (shouldContinue) { const fetchedVaults: RawVault[] = await this.ethereumContracts.dlcManagerContract.getAllDLCs( totalFetched, totalFetched + amount ); - fundedVaults.push(...fetchedVaults.filter(vault => vault.status === VaultState.Funded)); + const filteredVaults = fetchedVaults.filter(vault => vault.status === VaultState.Funded); + fundedVaults.push(...filteredVaults); + totalFetched += amount; - if (fetchedVaults.length !== amount) break; + shouldContinue = fetchedVaults.length === amount; } + return fundedVaults; - } catch (error: any) { - throw new EthereumError(`Could not fetch Funded Vaults: ${error}`); + } catch (error) { + throw new EthereumError( + `Could not fetch Funded Vaults: ${error instanceof Error ? error.message : error}` + ); } } } diff --git a/src/network-handlers/read-only-ethereum-handler.ts b/src/network-handlers/read-only-ethereum-handler.ts index f809be3..4e2383f 100644 --- a/src/network-handlers/read-only-ethereum-handler.ts +++ b/src/network-handlers/read-only-ethereum-handler.ts @@ -81,19 +81,25 @@ export class ReadOnlyEthereumHandler { let totalFetched = 0; const fundedVaults: RawVault[] = []; - while (true) { + let shouldContinue = true; + while (shouldContinue) { const fetchedVaults: RawVault[] = await this.ethereumContracts.dlcManagerContract.getAllDLCs( totalFetched, totalFetched + amount ); - fundedVaults.push(...fetchedVaults.filter(vault => vault.status === VaultState.Funded)); + const filteredVaults = fetchedVaults.filter(vault => vault.status === VaultState.Funded); + fundedVaults.push(...filteredVaults); + totalFetched += amount; - if (fetchedVaults.length !== amount) break; + shouldContinue = fetchedVaults.length === amount; } + return fundedVaults; - } catch (error: any) { - throw new EthereumError(`Could not fetch Funded Vaults: ${error}`); + } catch (error) { + throw new EthereumError( + `Could not fetch Funded Vaults: ${error instanceof Error ? error.message : error}` + ); } } }