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 7a67098..4d2fb4f 100644
--- a/hooks/use-dappnode-deposit.ts
+++ b/hooks/use-dappnode-deposit.ts
@@ -1,17 +1,15 @@
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";
export type DepositDataJson = {
pubkey: string;
@@ -30,12 +28,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 +123,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) {
@@ -164,7 +203,7 @@ function useDappnodeDeposit(contractConfig: ContractNetwork | undefined, address
return { deposits: newDeposits, hasDuplicates, _isBatch };
},
- [address, contractConfig, deposits, user]
+ [apolloClient, chainId, contractConfig, user]
);
const setDappnodeDepositData = useCallback(
@@ -226,7 +265,7 @@ function useDappnodeDeposit(contractConfig: ContractNetwork | undefined, address
console.error(err);
}
}
- }, [address, deposits]);
+ }, [contractConfig, deposits, writeContractAsync]);
return {
depositSuccess,
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) {