-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbridge.ts
123 lines (111 loc) · 3.17 KB
/
bridge.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { useQuery } from '@tanstack/react-query';
import { Address } from 'wagmi';
import { GraphQLClient } from 'graphql-request';
import { useContract } from './useContract';
import { badgeABI, bridgeABI } from '@/abis';
import { BADGE_BRIDGE_ADDRESS, BADGE_BRIDGE_ADDRESS_BSC, BADGE_BRIDGE_ADDRESS_BSC_OLD, BADGE_BRIDGE_ADDRESS_OLD } from '@/constants/addresses';
import { polygon } from 'wagmi/chains';
import { GalxeBadge } from '@/constants';
import { fetchPowerLevel } from '@/lib/api-nest';
const nftQuery = `
query($address: String!) {
user(addr: $address) {
address
galxeBadges{
chainId
image
galxeCampaign {
cid
stringId
name
rarity
campaignType
}
contractAddress
tokenId
}
}
}
`;
const historyQuery = `
query($address: String!) {
user(addr: $address) {
bridgeTxs {
hash
chainId
timestamp
galxeBadges{
tokenId
image
galxeCampaign {
cid
name
stringId
}
}
}
}
}
`;
const PowerLevelQuery = `
query($address: String!) {
user(addr: $address) {
badgePL
}
}
`;
export type BridgeTxs = {
chainId: string;
hash: string;
timestamp: number;
galxeBadges: GalxeBadge[];
};
export type HistoryResult = {
user: {
bridgeTxs: BridgeTxs[];
};
};
const client = new GraphQLClient('https://badge-api.p12.games/graphql');
export const useBadgeNFT = (address?: Address) => {
return useQuery(['fetch_badge_nft', address], async () => {
if (!address) return [];
const variables = {
address: address,
};
const data = await client.request(nftQuery, variables);
return data;
});
};
export const useBadgeHistory = <T>(address?: Address) => {
return useQuery(['fetch_badge_history', address], async () => {
if (!address) return {} as T;
const variables = {
address: address,
};
const data = await client.request(historyQuery, variables);
return data as T;
});
};
export const usePowerLevel = <T>(address?: Address) => {
return useQuery(['fetch_power_level', address], async () => {
if (!address) return {} as T;
const variables = {
address: address,
};
const data = await client.request(PowerLevelQuery, variables);
return data as T;
});
};
export function useNFTContract({ token, chainId }: { token?: Address; chainId?: number }) {
return useContract(token, badgeABI, chainId);
}
export function useBridgeContract({ chainId }: { chainId?: number }) {
const address = chainId === polygon.id ? BADGE_BRIDGE_ADDRESS : BADGE_BRIDGE_ADDRESS_BSC;
return useContract(address, bridgeABI, chainId);
}
export const useFetchPowerLevel = (address?: Address) => {
return useQuery(['fetch_power_level_2', address], () => fetchPowerLevel(address), {
select: (data) => data.data,
enabled: !!address,
});
};