From 7b34ac6060f086eee17ff9b4d002aada78241ee4 Mon Sep 17 00:00:00 2001 From: David Colon <38386583+Da-Colon@users.noreply.github.com> Date: Fri, 22 Mar 2024 23:44:22 -0400 Subject: [PATCH] reduce the number of requests by adding load key --- .../DAO/loaders/governance/useERC20Claim.ts | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/hooks/DAO/loaders/governance/useERC20Claim.ts b/src/hooks/DAO/loaders/governance/useERC20Claim.ts index c3ad1ff3a6..16032bcb62 100644 --- a/src/hooks/DAO/loaders/governance/useERC20Claim.ts +++ b/src/hooks/DAO/loaders/governance/useERC20Claim.ts @@ -1,4 +1,4 @@ -import { useEffect, useCallback } from 'react'; +import { useEffect, useCallback, useRef } from 'react'; import { useFractal } from '../../../../providers/App/AppProvider'; import { FractalGovernanceAction } from '../../../../providers/App/governance/action'; import useSafeContracts from '../../../safe/useSafeContracts'; @@ -15,13 +15,19 @@ export function useERC20Claim() { } = useFractal(); const baseContracts = useSafeContracts(); const loadTokenClaimContract = useCallback(async () => { - if (!baseContracts || !votesTokenContractAddress) return; + if (!baseContracts || !votesTokenContractAddress) { + return; + } const { claimingMasterCopyContract } = baseContracts; + const votesTokenContract = baseContracts.votesTokenMasterCopyContract.asProvider.attach(votesTokenContractAddress); + const approvalFilter = votesTokenContract.filters.Approval(); const approvals = await votesTokenContract.queryFilter(approvalFilter); - if (!approvals.length) return; + if (!approvals.length) { + return; + } const possibleTokenClaimContract = claimingMasterCopyContract.asProvider.attach( approvals[0].args[1], ); @@ -39,11 +45,20 @@ export function useERC20Claim() { payload: possibleTokenClaimContract, }); }, [baseContracts, votesTokenContractAddress, action]); + const loadKey = useRef(); useEffect(() => { - if (daoAddress) { + if ( + daoAddress && + votesTokenContractAddress && + daoAddress + votesTokenContractAddress !== loadKey.current + ) { + loadKey.current = daoAddress + votesTokenContractAddress; loadTokenClaimContract(); } - }, [loadTokenClaimContract, daoAddress]); + if (!daoAddress || !votesTokenContractAddress) { + loadKey.current = undefined; + } + }, [loadTokenClaimContract, daoAddress, votesTokenContractAddress]); return; }