From f4ec7f4f168a42bc7f47b92ae4a42aa3a8ffa8b3 Mon Sep 17 00:00:00 2001 From: sosaucily Date: Fri, 3 May 2024 11:54:46 +0200 Subject: [PATCH 1/2] feat: fetch all vaults and filter on funded --- src/app/hooks/use-ethereum.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/app/hooks/use-ethereum.ts b/src/app/hooks/use-ethereum.ts index 891382f6..77a7b887 100644 --- a/src/app/hooks/use-ethereum.ts +++ b/src/app/hooks/use-ethereum.ts @@ -174,13 +174,21 @@ export function useEthereum(): UseEthereumReturnType { } async function getAllFundedVaults(ethereumNetwork: EthereumNetwork): Promise { + const FUNDED_STATUS = 1; try { const dlcManagerContract = await getDefaultProvider(ethereumNetwork, 'DLCManager'); - const vaults: RawVault[] = await dlcManagerContract.getFundedDLCs(0, 10000); - const filteredVaults = vaults.filter( - vault => vault.uuid != '0x0000000000000000000000000000000000000000000000000000000000000000' - ); - return filteredVaults; + const numToFetch = 50; + let numFetched = 0; + let fundedVaults: RawVault[] = []; + let fetchedVaults: RawVault[] = []; + do { + fetchedVaults = await dlcManagerContract.getAllDLCs(numFetched, numFetched + numToFetch); + fundedVaults = fundedVaults.concat( + fetchedVaults.filter(vault => vault.status == FUNDED_STATUS) + ); + numFetched += numToFetch; + } while (fetchedVaults.length == numToFetch); + return fundedVaults; } catch (error) { throw new EthereumError(`Could not fetch Funded Vaults: ${error}`); } From 61f3be4a34288034f40d89014533c09303f64d06 Mon Sep 17 00:00:00 2001 From: sosaucily Date: Fri, 3 May 2024 12:09:34 +0200 Subject: [PATCH 2/2] feat: cleanup function --- src/app/hooks/use-ethereum.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/app/hooks/use-ethereum.ts b/src/app/hooks/use-ethereum.ts index 77a7b887..346808b8 100644 --- a/src/app/hooks/use-ethereum.ts +++ b/src/app/hooks/use-ethereum.ts @@ -178,16 +178,18 @@ export function useEthereum(): UseEthereumReturnType { try { const dlcManagerContract = await getDefaultProvider(ethereumNetwork, 'DLCManager'); const numToFetch = 50; - let numFetched = 0; - let fundedVaults: RawVault[] = []; - let fetchedVaults: RawVault[] = []; - do { - fetchedVaults = await dlcManagerContract.getAllDLCs(numFetched, numFetched + numToFetch); - fundedVaults = fundedVaults.concat( - fetchedVaults.filter(vault => vault.status == FUNDED_STATUS) + let totalFetched = 0; + const fundedVaults: RawVault[] = []; + // eslint-disable-next-line no-constant-condition + while (true) { + const fetchedVaults: RawVault[] = await dlcManagerContract.getAllDLCs( + totalFetched, + totalFetched + numToFetch ); - numFetched += numToFetch; - } while (fetchedVaults.length == numToFetch); + fundedVaults.push(...fetchedVaults.filter(vault => vault.status === FUNDED_STATUS)); + totalFetched += numToFetch; + if (fetchedVaults.length !== numToFetch) break; + } return fundedVaults; } catch (error) { throw new EthereumError(`Could not fetch Funded Vaults: ${error}`);