-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Load the Pontus-X account names - Load metadata for Oasis Consensus, Emerald and Sapphire - Support displaying account names - Support for searching for accounts by name - Implement highlighting matches in account names
- Loading branch information
Showing
16 changed files
with
455 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Initial support for displaying account names |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export type AccountMetadata = { | ||
name?: string | ||
description?: string | ||
} | ||
|
||
export type AccountMetadataInfo = { | ||
metadata?: AccountMetadata | ||
loading: boolean | ||
} | ||
|
||
export type AccountMap = Map<string, AccountMetadata> | ||
|
||
export type AccountEntry = { address: string } & AccountMetadata | ||
|
||
export type AccountData = { | ||
map: AccountMap | ||
list: AccountEntry[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import axios from 'axios' | ||
import { useQuery } from '@tanstack/react-query' | ||
import { Layer } from '../../oasis-nexus/api' | ||
import { Network } from '../../types/network' | ||
import { findTextMatch } from '../components/HighlightedText/text-matching' | ||
import * as process from 'process' | ||
import { AccountData, AccountEntry, AccountMap, AccountMetadata, AccountMetadataInfo } from './named-accounts' | ||
|
||
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 = (network: Network, layer: Layer) => | ||
new Promise<AccountData>((resolve, reject) => { | ||
const url = dataSources[network][layer] | ||
if (!url) { | ||
reject('No data available for this layer') | ||
} else { | ||
axios.get(url).then(response => { | ||
if (response.status !== 200) reject("Couldn't load names") | ||
if (!response.data) reject("Couldn't load names") | ||
// console.log('Response data is', response.data) | ||
const map: AccountMap = new Map() | ||
const list: AccountEntry[] = [] | ||
Array.from(response.data).forEach((entry: any) => { | ||
const address = entry.Address | ||
const metadata: AccountMetadata = { | ||
name: entry.Name, | ||
description: entry.Description, | ||
} | ||
// Register the metadata in its native form | ||
list.push({ | ||
address, | ||
...metadata, | ||
}) | ||
map.set(address, metadata) | ||
}) | ||
resolve({ | ||
map, | ||
list, | ||
}) | ||
}, reject) | ||
} | ||
}) | ||
|
||
export const useOasisAccountsMetadata = (network: Network, layer: Layer, enabled: boolean) => { | ||
return useQuery(['oasisAccounts', network, layer], () => getOasisAccountsMetadata(network, layer), { | ||
enabled, | ||
staleTime: Infinity, | ||
}) | ||
} | ||
|
||
export const useOasisAccountMetadata = ( | ||
network: Network, | ||
layer: Layer, | ||
address: string, | ||
enabled: boolean, | ||
): AccountMetadataInfo => { | ||
// When running jest tests, we don't want to load from Pontus-X. | ||
if (process.env.NODE_ENV === 'test') { | ||
return { | ||
metadata: { | ||
name: undefined, | ||
}, | ||
loading: false, | ||
} | ||
} | ||
// This is not a condition that can change while the app is running, so it's OK. | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
const { isLoading, error, data: allData } = useOasisAccountsMetadata(network, layer, enabled) | ||
if (error) { | ||
console.log('Failed to load Oasis account metadata', error) | ||
} | ||
return { | ||
metadata: allData?.map.get(address), | ||
loading: isLoading, | ||
} | ||
} | ||
|
||
export const useSearchForOasisAccountsByName = ( | ||
network: Network, | ||
layer: Layer, | ||
nameFragment: string, | ||
enabled: boolean, | ||
) => { | ||
const { isLoading, error, data: allData } = useOasisAccountsMetadata(network, layer, enabled) | ||
if (error) { | ||
console.log('Failed to load Oasis account metadata', error) | ||
} | ||
|
||
const textMatcher = | ||
nameFragment && enabled | ||
? (entry: AccountEntry): boolean => { | ||
return !!findTextMatch(entry.name, [nameFragment]) | ||
} | ||
: () => false | ||
return { | ||
results: (allData?.list || []).filter(textMatcher).map(entry => ({ | ||
network, | ||
layer, | ||
address: entry.address, | ||
})), | ||
isLoading, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import axios from 'axios' | ||
import { useQuery } from '@tanstack/react-query' | ||
import { Layer } from '../../oasis-nexus/api' | ||
import { Network } from '../../types/network' | ||
import { findTextMatch } from '../components/HighlightedText/text-matching' | ||
import * as process from 'process' | ||
import { AccountData, AccountEntry, AccountMap, AccountMetadataInfo } from './named-accounts' | ||
|
||
const DATA_SOURCE_URL = 'https://raw.githubusercontent.com/deltaDAO/mvg-portal/main/pontusxAddresses.json' | ||
|
||
const getPontusXAccountsMetadata = () => | ||
new Promise<AccountData>((resolve, reject) => { | ||
axios.get(DATA_SOURCE_URL).then(response => { | ||
if (response.status !== 200) reject("Couldn't load names") | ||
if (!response.data) reject("Couldn't load names") | ||
const map: AccountMap = new Map() | ||
const list: AccountEntry[] = [] | ||
Object.entries(response.data).forEach(([address, name]) => { | ||
map.set(address, { name: name as string }) | ||
const normalizedEntry: AccountEntry = { | ||
name: name as string, | ||
address, | ||
} | ||
list.push(normalizedEntry) | ||
}) | ||
resolve({ | ||
map, | ||
list, | ||
}) | ||
}, reject) | ||
}) | ||
|
||
export const usePontusXAccountsMetadata = (enabled: boolean) => { | ||
return useQuery(['pontusXNames'], getPontusXAccountsMetadata, { | ||
enabled, | ||
staleTime: Infinity, | ||
}) | ||
} | ||
|
||
export const usePontusXAccountMetadata = (address: string, enabled: boolean): AccountMetadataInfo => { | ||
// When running jest tests, we don't want to load from Pontus-X. | ||
if (process.env.NODE_ENV === 'test') { | ||
return { | ||
metadata: undefined, | ||
loading: false, | ||
} | ||
} | ||
// This is not a condition that can change while the app is running, so it's OK. | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
const { isLoading, error, data: allData } = usePontusXAccountsMetadata(enabled) | ||
if (error) { | ||
console.log('Failed to load Pontus-X account names', error) | ||
} | ||
return { | ||
metadata: allData?.map.get(address), | ||
loading: isLoading, | ||
} | ||
} | ||
|
||
export const useSearchForPontusXAccountsByName = ( | ||
network: Network, | ||
nameFragment: string, | ||
enabled: boolean, | ||
) => { | ||
const { isLoading, error, data: allNames } = usePontusXAccountsMetadata(enabled) | ||
if (error) { | ||
console.log('Failed to load Pontus-X account names', error) | ||
} | ||
|
||
const textMatcher = | ||
nameFragment && enabled | ||
? (entry: AccountEntry): boolean => { | ||
return !!findTextMatch(entry.name, [nameFragment]) | ||
} | ||
: () => false | ||
return { | ||
results: (allNames?.list || []).filter(textMatcher).map(entry => ({ | ||
network, | ||
layer: Layer.pontusx, | ||
address: entry.address, | ||
})), | ||
isLoading, | ||
} | ||
} |
Oops, something went wrong.