From 46146f8f0f8674857f20eb5a482030cf63b46751 Mon Sep 17 00:00:00 2001 From: Wagalidoom Date: Thu, 3 Oct 2024 14:12:12 +0200 Subject: [PATCH 1/3] feat: update envio indexer in dappnode deposit component --- hooks/use-dappnode-deposit.ts | 74 +++++++++++++++++++++++++++-------- hooks/use-deposit.ts | 2 +- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/hooks/use-dappnode-deposit.ts b/hooks/use-dappnode-deposit.ts index 7a67098..76b4fd4 100644 --- a/hooks/use-dappnode-deposit.ts +++ b/hooks/use-dappnode-deposit.ts @@ -12,6 +12,7 @@ import { getPublicClient } from "wagmi/actions"; import { config } from "@/wagmi"; import { fetchDeposit } from "@/utils/fetchEvents"; import { DEPOSIT_TOKEN_AMOUNT_OLD, MAX_BATCH_DEPOSIT } from "@/utils/constants"; +import { gql, useApolloClient } from "@apollo/client"; export type DepositDataJson = { pubkey: string; @@ -30,12 +31,34 @@ export type DappnodeUser = [ totalStakeAmount: bigint // uint256 ]; +const GET_DEPOSIT_EVENTS = gql` + query MyQuery($pubkeys: [String!], $chainId: Int!) { + SBCDepositContract_DepositEvent( + where: { + pubkey: { + _in: $pubkeys + }, + chainId: {_eq: $chainId} + } + ) { + id + amount + db_write_timestamp + index + withdrawal_credentials + pubkey + } + } +`; + function useDappnodeDeposit(contractConfig: ContractNetwork | undefined, address: `0x${string}` | undefined, chainId: number) { const [deposits, setDeposits] = useState([]); const [hasDuplicates, setHasDuplicates] = useState(false); const [isBatch, setIsBatch] = useState(false); const [filename, setFilename] = useState(""); const client = getPublicClient(config, { chainId: chainId as 100 }); + + const apolloClient = useApolloClient(); const { data: user }: { data: DappnodeUser | undefined } = useReadContract({ abi: dappnodeIncentiveABI, @@ -103,29 +126,48 @@ function useDappnodeDeposit(contractConfig: ContractNetwork | undefined, address ); } - const { deposits: existingDeposits, lastBlock: fromBlock } = - await loadCachedDeposits( - chainId, - contractConfig.depositStartBlockNumber - ); + const pksFromFile = deposits.map((d) => `0x${d.pubkey}`); + const { data } = await apolloClient.query({ + query: GET_DEPOSIT_EVENTS, + variables: { + pubkeys: pksFromFile, + chainId: chainId, + }, + }); + + const existingDeposits = data.SBCDepositContract_DepositEvent.map((d: { pubkey: string }) => d.pubkey); - const events = await fetchDeposit( - contractConfig.addresses.deposit, - fromBlock, - client - ); + // const { deposits: existingDeposits, lastBlock: fromBlock } = + // await loadCachedDeposits( + // chainId, + // contractConfig.depositStartBlockNumber + // ); - let pks = events.map((e) => e.args.pubkey); - pks = pks.concat(existingDeposits); - console.log(pks); - console.log(`Found ${pks.length} existing deposits`); + // const events = await fetchDeposit( + // contractConfig.addresses.deposit, + // fromBlock, + // client + // ); + + // let pks = events.map((e) => e.args.pubkey); + // pks = pks.concat(existingDeposits); + // console.log(pks); + // console.log(`Found ${pks.length} existing deposits`); for (const deposit of deposits) { - if (!pks.includes(`0x${deposit.pubkey}`)) { - console.log("new deposit", deposit.pubkey); + if (!existingDeposits.includes(`0x${deposit.pubkey}`)) { + console.log('new deposit', deposit.pubkey); newDeposits.push(deposit); } } + + // for (const deposit of deposits) { + // if (!pks.includes(`0x${deposit.pubkey}`)) { + // console.log("new deposit", deposit.pubkey); + // newDeposits.push(deposit); + // } + // } + hasDuplicates = newDeposits.length !== deposits.length; if (newDeposits.length === 0) { diff --git a/hooks/use-deposit.ts b/hooks/use-deposit.ts index c22505a..9496a68 100644 --- a/hooks/use-deposit.ts +++ b/hooks/use-deposit.ts @@ -107,7 +107,7 @@ function useDeposit(contractConfig: ContractNetwork | undefined, address: `0x${s chainId: chainId, }, }); - console.log(data); + const existingDeposits = data.SBCDepositContract_DepositEvent.map((d: { pubkey: string }) => d.pubkey); for (const deposit of deposits) { From c5b0c637912db3aab365f1a5a9d15317e908ddf3 Mon Sep 17 00:00:00 2001 From: Wagalidoom Date: Thu, 3 Oct 2024 15:53:07 +0200 Subject: [PATCH 2/3] fix: remove eslint warning --- components/dappnodeDeposit.tsx | 4 ++-- hooks/use-dappnode-deposit.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/dappnodeDeposit.tsx b/components/dappnodeDeposit.tsx index cdfa66b..2714d97 100644 --- a/components/dappnodeDeposit.tsx +++ b/components/dappnodeDeposit.tsx @@ -194,7 +194,7 @@ function PendingStatus({ reader.readAsText(acceptedFiles[0]); } }, - [setDappnodeDepositData, errorMessage] + [setErrorMessage, setLoading, setDappnodeDepositData, setStep] ); const { getRootProps, getInputProps } = useDropzone({ @@ -254,7 +254,7 @@ function Validation({ }) { const onDeposit = useCallback(async () => { await dappnodeDeposit(); - }, [depositData]); + }, [dappnodeDeposit]); return claimStatusPending ? (
diff --git a/hooks/use-dappnode-deposit.ts b/hooks/use-dappnode-deposit.ts index 76b4fd4..f12821c 100644 --- a/hooks/use-dappnode-deposit.ts +++ b/hooks/use-dappnode-deposit.ts @@ -206,7 +206,7 @@ function useDappnodeDeposit(contractConfig: ContractNetwork | undefined, address return { deposits: newDeposits, hasDuplicates, _isBatch }; }, - [address, contractConfig, deposits, user] + [apolloClient, chainId, contractConfig, user] ); const setDappnodeDepositData = useCallback( @@ -268,7 +268,7 @@ function useDappnodeDeposit(contractConfig: ContractNetwork | undefined, address console.error(err); } } - }, [address, deposits]); + }, [contractConfig, deposits, writeContractAsync]); return { depositSuccess, From fb3c777d578d2f615148692a09a53b4265539d10 Mon Sep 17 00:00:00 2001 From: Wagalidoom Date: Thu, 3 Oct 2024 18:11:27 +0200 Subject: [PATCH 3/3] fix: remove unusued imports --- hooks/use-dappnode-deposit.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/hooks/use-dappnode-deposit.ts b/hooks/use-dappnode-deposit.ts index f12821c..4d2fb4f 100644 --- a/hooks/use-dappnode-deposit.ts +++ b/hooks/use-dappnode-deposit.ts @@ -1,16 +1,13 @@ import { useCallback, useState } from "react"; import { - useAccount, useReadContract, useWriteContract, useWaitForTransactionReceipt, } from "wagmi"; -import CONTRACTS, { ContractNetwork } from "@/utils/contracts"; +import { ContractNetwork } from "@/utils/contracts"; import dappnodeIncentiveABI from "@/utils/abis/dappnodeIncentive"; -import { loadCachedDeposits } from "@/utils/deposit"; import { getPublicClient } from "wagmi/actions"; import { config } from "@/wagmi"; -import { fetchDeposit } from "@/utils/fetchEvents"; import { DEPOSIT_TOKEN_AMOUNT_OLD, MAX_BATCH_DEPOSIT } from "@/utils/constants"; import { gql, useApolloClient } from "@apollo/client";