Skip to content

Commit

Permalink
Merge pull request #58 from gnosischain/dev
Browse files Browse the repository at this point in the history
feat: update envio indexer in dappnode deposit component
  • Loading branch information
Wagalidoom authored Oct 3, 2024
2 parents ed96280 + fb3c777 commit 14a17a8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 25 deletions.
4 changes: 2 additions & 2 deletions components/dappnodeDeposit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function PendingStatus({
reader.readAsText(acceptedFiles[0]);
}
},
[setDappnodeDepositData, errorMessage]
[setErrorMessage, setLoading, setDappnodeDepositData, setStep]
);

const { getRootProps, getInputProps } = useDropzone({
Expand Down Expand Up @@ -254,7 +254,7 @@ function Validation({
}) {
const onDeposit = useCallback(async () => {
await dappnodeDeposit();
}, [depositData]);
}, [dappnodeDeposit]);

return claimStatusPending ? (
<div className="flex flex-col items-center gap-4">
Expand Down
83 changes: 61 additions & 22 deletions hooks/use-dappnode-deposit.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<DepositDataJson[]>([]);
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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -226,7 +265,7 @@ function useDappnodeDeposit(contractConfig: ContractNetwork | undefined, address
console.error(err);
}
}
}, [address, deposits]);
}, [contractConfig, deposits, writeContractAsync]);

return {
depositSuccess,
Expand Down
2 changes: 1 addition & 1 deletion hooks/use-deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 14a17a8

Please sign in to comment.