-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-mint-events.ts
More file actions
63 lines (54 loc) · 1.55 KB
/
use-mint-events.ts
File metadata and controls
63 lines (54 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"use client";
import { useQueryClient } from "@tanstack/react-query";
import { watchContractEvent } from "@wagmi/core";
import { useEffect } from "react";
import { useAccount } from "wagmi";
import { wagmiWsConfig } from "@/app/_config/wagmi";
import { forgeContractConfig } from "@/app/_contracts/forge-contract-config";
import { useTokens } from "../use-tokens";
/**
* useMintEvents
*
* on Forge mint-event (Forge__MintToken)
* - Refetch minted token balance
* - Recomputes forgeability of forged-tokens
*/
export const useMintEvents = () => {
const queryClient = useQueryClient();
const { address } = useAccount();
const { reCheckForgeability } = useTokens();
useEffect(() => {
if (
!address ||
!forgeContractConfig ||
!forgeContractConfig.address ||
!forgeContractConfig.abi
)
return;
const unwatch = watchContractEvent(wagmiWsConfig, {
address: forgeContractConfig.address,
abi: forgeContractConfig.abi,
// event Forge__MintToken(address indexed user, uint256 indexed tokenId, uint256 indexed amount);
eventName: "Forge__MintToken",
onLogs(logs) {
const log = logs[0] as unknown as {
args: {
user: string;
tokenId: bigint;
amount: bigint;
};
};
const { tokenId: mintedTokenId } = log.args;
queryClient.invalidateQueries({
queryKey: ["balance", `${mintedTokenId}-${address}`],
});
// Recomputes forgeability of forged-tokens
reCheckForgeability();
},
});
return () => {
unwatch();
};
}, [address, queryClient, reCheckForgeability]);
return null;
};