-
Notifications
You must be signed in to change notification settings - Fork 5
/
identity.ts
43 lines (40 loc) · 1.2 KB
/
identity.ts
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
import { useCallback, useMemo } from 'react';
import { useQuery } from 'react-query';
import { queryClient } from '../../masa-query-client';
import { Masa } from '@masa-finance/masa-sdk';
import { BigNumber } from 'ethers';
export const useIdentity = (
masa: Masa | null,
walletAddress: string | undefined
): {
identity:
| {
identityId?: BigNumber;
address?: string;
}
| undefined;
handlePurchaseIdentity: () => void;
status: string;
isLoading: boolean;
error: unknown;
} => {
const queryKey: string = useMemo(() => {
return `identity-${walletAddress}-${masa?.config.network}`;
}, [walletAddress, masa]);
const {
data: identity,
status,
isLoading,
error,
} = useQuery(queryKey, () => masa?.identity.load(walletAddress), {
enabled: !!masa && !!walletAddress,
onSuccess: (identity: { identityId?: BigNumber; address?: string }) => {
console.log({ identity });
},
});
const handlePurchaseIdentity = useCallback(async () => {
await masa?.identity.create();
await queryClient.invalidateQueries(`identity-${walletAddress}`);
}, [masa, walletAddress]);
return { identity, handlePurchaseIdentity, status, isLoading, error };
};