-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhook.ts
More file actions
215 lines (186 loc) · 6.37 KB
/
hook.ts
File metadata and controls
215 lines (186 loc) · 6.37 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import {
RuntimeEvent,
RuntimeEventList,
useGetRuntimeEvents,
useGetRuntimeEvmTokensAddress,
useGetRuntimeEvmTokensAddressHolders,
useGetRuntimeEvmTokensAddressNfts,
useGetRuntimeAccountsAddressNfts,
useGetRuntimeEvmTokensAddressNftsId,
RuntimeEventType,
GetRuntimeEventsParams,
} from '../../../oasis-nexus/api'
import { AppErrors } from '../../../types/errors'
import { RuntimeScope } from '../../../types/searchScope'
import { useSearchParamsPagination } from '../../components/Table/useSearchParamsPagination'
import { NUMBER_OF_ITEMS_ON_SEPARATE_PAGE } from '../../../config'
import { useComprehensiveSearchParamsPagination } from '../../components/Table/useComprehensiveSearchParamsPagination'
interface UseTokenInfoParams {
/** Defaults to true */
enabled?: boolean
useCaching?: boolean
swallowError?: boolean
}
export const useTokenInfo = (scope: RuntimeScope, address: string, params: UseTokenInfoParams = {}) => {
const { network, layer } = scope
const { enabled, useCaching, swallowError = false } = params
const query = useGetRuntimeEvmTokensAddress(network, layer, address, {
query: {
enabled,
staleTime: useCaching ? 3600000 : undefined,
},
request: swallowError ? ({ swallowError } as any) : undefined,
})
const token = query.data?.data
const { isLoading, isError, isFetched } = query
return {
token,
isLoading: isLoading && enabled !== false, // By default, we get true for isLoading on disabled queries. We don't want that.
isError,
isFetched,
}
}
export const useNFTInstanceTransfers = (
scope: RuntimeScope,
params: { nft_id: string; contract_address: string },
) => {
return useTokenTransfers(scope, { nft_id: params.nft_id, contract_address: params.contract_address })
}
export const useTokenTransfers = (
scope: undefined | RuntimeScope,
params: undefined | GetRuntimeEventsParams,
querySearchParamName?: string,
) => {
if (params && Object.values(params).some(value => value === undefined || value === null)) {
throw new Error('Must set params=undefined while some values are unavailable')
}
if (params && !scope) {
throw new Error('Must set params=undefined while scope is unavailable')
}
const mockScopeForDisabledQuery = { network: 'mainnet', layer: 'sapphire' } as const
const pagination = useComprehensiveSearchParamsPagination<RuntimeEvent, RuntimeEventList>({
paramName: querySearchParamName ?? 'page',
pageSize: NUMBER_OF_ITEMS_ON_SEPARATE_PAGE,
})
const query = useGetRuntimeEvents(
scope?.network ?? mockScopeForDisabledQuery.network,
scope?.layer ?? mockScopeForDisabledQuery.layer, // This is OK since consensus has been handled separately
{
...pagination.paramsForQuery,
type: RuntimeEventType.evmlog,
// The following is the hex-encoded signature for Transfer(address,address,uint256)
evm_log_signature: 'ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
...params,
},
{
query: {
enabled: !!params,
},
},
)
const { isFetched, isLoading, data } = query
const results = pagination.getResults(data?.data)
if (isFetched && pagination.selectedPage > 1 && !results.data?.length) {
throw AppErrors.PageDoesNotExist
}
return {
isLoading,
isFetched,
results: pagination.getResults(data?.data),
}
}
export const useTokenHolders = (scope: RuntimeScope, address: string) => {
const { network, layer } = scope
const pagination = useSearchParamsPagination('page')
const offset = (pagination.selectedPage - 1) * NUMBER_OF_ITEMS_ON_SEPARATE_PAGE
const query = useGetRuntimeEvmTokensAddressHolders(network, layer, address, {
limit: NUMBER_OF_ITEMS_ON_SEPARATE_PAGE,
offset: offset,
})
const { isFetched, isLoading, data } = query
const holders = data?.data.holders
if (isFetched && pagination.selectedPage > 1 && !holders?.length) {
throw AppErrors.PageDoesNotExist
}
const totalCount = data?.data.total_count
const isTotalCountClipped = data?.data.is_total_count_clipped
return {
isLoading,
isFetched,
holders,
pagination,
totalCount,
isTotalCountClipped,
}
}
// specific to NFT gallery only
const NUMBER_OF_INVENTORY_ITEMS = 15
export const useTokenInventory = (scope: RuntimeScope, address: string) => {
const { network, layer } = scope
const pagination = useSearchParamsPagination('page')
const offset = (pagination.selectedPage - 1) * NUMBER_OF_INVENTORY_ITEMS
const query = useGetRuntimeEvmTokensAddressNfts(network, layer, address, {
limit: NUMBER_OF_INVENTORY_ITEMS,
offset: offset,
})
const { isFetched, isLoading, data } = query
const inventory = data?.data.evm_nfts
if (isFetched && pagination.selectedPage > 1 && !inventory?.length) {
throw AppErrors.PageDoesNotExist
}
const totalCount = data?.data.total_count
const isTotalCountClipped = data?.data.is_total_count_clipped
return {
isLoading,
isFetched,
inventory,
pagination: {
...pagination,
isTotalCountClipped,
rowsPerPage: NUMBER_OF_INVENTORY_ITEMS,
},
totalCount,
}
}
export const useAccountTokenInventory = (scope: RuntimeScope, address: string, tokenAddress: string) => {
const { network, layer } = scope
const pagination = useSearchParamsPagination('page')
const offset = (pagination.selectedPage - 1) * NUMBER_OF_INVENTORY_ITEMS
const query = useGetRuntimeAccountsAddressNfts(network, layer, address, {
limit: NUMBER_OF_INVENTORY_ITEMS,
offset: offset,
token_address: tokenAddress,
})
const { isFetched, isLoading, data } = query
const inventory = data?.data.evm_nfts
if (isFetched && pagination.selectedPage > 1 && !inventory?.length) {
throw AppErrors.PageDoesNotExist
}
const totalCount = data?.data.total_count
const isTotalCountClipped = data?.data.is_total_count_clipped
return {
isLoading,
isFetched,
inventory,
pagination: {
...pagination,
rowsPerPage: NUMBER_OF_INVENTORY_ITEMS,
},
isTotalCountClipped,
totalCount,
}
}
export const useNFTInstance = (scope: RuntimeScope, address: string, id: string) => {
const { network, layer } = scope
const query = useGetRuntimeEvmTokensAddressNftsId(network, layer, address, id)
const { data, isError, isFetched, isLoading } = query
if (isError) {
throw AppErrors.InvalidAddress
}
const nft = data?.data
return {
isLoading,
isFetched,
nft,
}
}