-
Notifications
You must be signed in to change notification settings - Fork 9
/
oasis-account-names.ts
147 lines (136 loc) · 4.89 KB
/
oasis-account-names.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import axios from 'axios'
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import {
Layer,
useGetConsensusAccountsAddresses,
useGetRuntimeAccountsAddresses,
} from '../../oasis-nexus/api'
import { Network } from '../../types/network'
import {
AccountData,
AccountMap,
AccountMetadata,
AccountMetadataInfo,
AccountNameSearchConsensusMatch,
AccountNameSearchMatch,
AccountNameSearchResults,
AccountNameSearchRuntimeMatch,
} from './named-accounts'
import { hasTextMatch } from '../components/HighlightedText/text-matching'
const dataSources: Record<Network, Partial<Record<Layer, string>>> = {
[Network.mainnet]: {
[Layer.consensus]:
'https://raw.githubusercontent.com/oasisprotocol/nexus/main/account-names/mainnet_consensus.json',
[Layer.emerald]:
'https://raw.githubusercontent.com/oasisprotocol/nexus/main/account-names/mainnet_paratime.json',
[Layer.sapphire]:
'https://raw.githubusercontent.com/oasisprotocol/nexus/main/account-names/mainnet_paratime.json',
},
[Network.testnet]: {
[Layer.consensus]:
'https://raw.githubusercontent.com/oasisprotocol/nexus/main/account-names/testnet_consensus.json',
[Layer.emerald]:
'https://raw.githubusercontent.com/oasisprotocol/nexus/main/account-names/testnet_paratime.json',
[Layer.sapphire]:
'https://raw.githubusercontent.com/oasisprotocol/nexus/main/account-names/testnet_paratime.json',
},
}
const getOasisAccountsMetadata = async (network: Network, layer: Layer): Promise<AccountData> => {
const url = dataSources[network][layer]
if (!url) throw new Error('No data available for this layer')
const response = await axios.get(url)
if (response.status !== 200) throw new Error("Couldn't load names")
if (!response.data) throw new Error("Couldn't load names")
// console.log('Response data is', response.data)
const map: AccountMap = new Map()
const list: AccountMetadata[] = []
Array.from(response.data).forEach((entry: any) => {
const metadata: AccountMetadata = {
address: entry.Address,
name: entry.Name,
description: entry.Description,
}
// Register the metadata in its native form
list.push(metadata)
map.set(metadata.address, metadata)
})
return {
map,
list,
}
}
export const useOasisAccountsMetadata = (
network: Network,
layer: Layer,
queryOptions: UseQueryOptions<AccountData, unknown, AccountData, string[]>,
) => {
return useQuery(['oasisAccounts', network, layer], () => getOasisAccountsMetadata(network, layer), {
staleTime: Infinity,
...queryOptions,
})
}
export const useOasisAccountMetadata = (
network: Network,
layer: Layer,
address: string,
queryOptions: UseQueryOptions<AccountData, unknown, AccountData, string[]>,
): AccountMetadataInfo => {
const { isLoading, isError, error, data: allData } = useOasisAccountsMetadata(network, layer, queryOptions)
if (isError) {
console.log('Failed to load Oasis account metadata', error)
}
return {
metadata: allData?.map.get(address),
isLoading,
isError,
}
}
export const useSearchForOasisAccountsByName = (
network: Network,
layer: Layer,
nameFragment: string,
queryOptions: { enabled: boolean } & UseQueryOptions<AccountData, unknown, AccountData, string[]>,
): AccountNameSearchResults => {
const {
isLoading: isMetadataLoading,
isError: isMetadataError,
error: metadataError,
data: namedAccounts,
} = useOasisAccountsMetadata(network, layer, { useErrorBoundary: false, ...queryOptions })
if (isMetadataError) {
console.log('Failed to load Oasis account metadata', metadataError)
}
const textMatcher =
nameFragment && queryOptions.enabled
? (account: AccountMetadata) => hasTextMatch(account.name, [nameFragment])
: () => false
const matches =
namedAccounts?.list.filter(textMatcher).map(
(account): AccountNameSearchMatch => ({
network,
layer,
address: account.address,
}),
) ?? []
const consensusMatches = layer === Layer.consensus ? (matches as AccountNameSearchConsensusMatch[]) : []
const runtimeMatches = layer === Layer.consensus ? [] : (matches as AccountNameSearchRuntimeMatch[])
const {
isLoading: areConsensusAccountsLoading,
isError: areConsensusAccountsError,
data: consensusResults,
} = useGetConsensusAccountsAddresses(consensusMatches, {
enabled: queryOptions.enabled && !isMetadataLoading && !isMetadataError,
})
const {
isLoading: areRuntimeAccountsLoading,
isError: areRuntimeAccountsError,
data: runtimeResults,
} = useGetRuntimeAccountsAddresses(runtimeMatches, {
enabled: queryOptions.enabled && !isMetadataLoading && !isMetadataError,
})
return {
isLoading: isMetadataLoading || areConsensusAccountsLoading || areRuntimeAccountsLoading,
isError: isMetadataError || areConsensusAccountsError || areRuntimeAccountsError,
results: [...consensusResults, ...runtimeResults],
}
}