Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update envio indexer in dappnode deposit component #58

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
78 changes: 60 additions & 18 deletions hooks/use-dappnode-deposit.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove unused imports:

useAccount
fetchDeposit
CONTRACTS
loadCachedDeposits

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch thank you! 👍

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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 +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) {
Expand Down Expand Up @@ -164,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(
Expand Down Expand Up @@ -226,7 +268,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
Loading