Skip to content

Commit

Permalink
feat: update envio indexer in dappnode deposit component
Browse files Browse the repository at this point in the history
  • Loading branch information
Wagalidoom committed Oct 3, 2024
1 parent 7e8e0cf commit 46146f8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
74 changes: 58 additions & 16 deletions hooks/use-dappnode-deposit.ts
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
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 46146f8

Please sign in to comment.