forked from blockscout/frontend
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation for lsp7 support
- Loading branch information
Showing
13 changed files
with
183 additions
and
51 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,23 @@ | ||
import type { SearchIndex } from 'algoliasearch'; | ||
import algoliasearch from 'algoliasearch'; | ||
|
||
import { getEnvValue } from '../../configs/app/utils'; | ||
|
||
interface AlgoliaSearcher { | ||
profile: SearchIndex; | ||
asset: SearchIndex; | ||
} | ||
|
||
const algoliaEnvs = { | ||
appId: getEnvValue('NEXT_PUBLIC_ALGOLIA_APP_ID') || '', | ||
apiKey: getEnvValue('NEXT_PUBLIC_ALGOLIA_API_KEY') || '', | ||
profileIndex: getEnvValue('NEXT_PUBLIC_ALGOLIA_PROFILE_INDEX_NAME') || '', | ||
assetIndex: getEnvValue('NEXT_PUBLIC_ALGOLIA_ASSETS_INDEX_NAME') || '', | ||
}; | ||
|
||
const algoliaClient = algoliasearch(algoliaEnvs.appId, algoliaEnvs.apiKey); | ||
|
||
export const algoliaLSPSearch: AlgoliaSearcher = { | ||
profile: algoliaClient.initIndex(algoliaEnvs.profileIndex), | ||
asset: algoliaClient.initIndex(algoliaEnvs.assetIndex), | ||
}; |
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
import type { LSPResponse } from '../../types/api/lsp'; | ||
|
||
import { getEnvValue } from '../../configs/app/utils'; | ||
|
||
export const fetchLsp = async <T extends LSPResponse>(address: string) => { | ||
const upApiUrl = getEnvValue('NEXT_PUBLIC_UNIVERSAL_PROFILES_API_URL') || ''; | ||
const networkId = getEnvValue('NEXT_PUBLIC_NETWORK_ID') || '42'; | ||
const url = `${ upApiUrl }/v1/${ networkId }/address/${ address }`; | ||
|
||
try { | ||
const resp = await fetch(url); | ||
const json = await resp.json(); | ||
return json as T; | ||
} catch (err) { | ||
return undefined; | ||
} | ||
}; |
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,47 @@ | ||
import React from 'react'; | ||
|
||
import type { LSP7Response } from '../../types/api/lsp'; | ||
import type { SearchResultToken } from '../../types/api/search'; | ||
|
||
import type { Params as FetchParams } from 'lib/hooks/useFetch'; | ||
|
||
import { algoliaLSPSearch } from './algolia'; | ||
import { isUniversalProfileEnabled } from './isUniversalProfileEnabled'; | ||
import type { ResourceName, ResourcePathParams } from './resources'; | ||
|
||
export interface Params<R extends ResourceName> { | ||
pathParams?: ResourcePathParams<R>; | ||
queryParams?: Record<string, string | Array<string> | number | undefined>; | ||
fetchParams?: Pick<FetchParams, 'body' | 'method' | 'signal'>; | ||
} | ||
|
||
export default function useLSP7ApiFetch() { | ||
return React.useCallback(async(queryParams: string, | ||
) => { | ||
if (!isUniversalProfileEnabled()) { | ||
return [] as Array<SearchResultToken>; | ||
} | ||
try { | ||
const { hits } = await algoliaLSPSearch.asset.search(queryParams); | ||
return hits.map<SearchResultToken>((hit) => { | ||
const lsp = hit as unknown as LSP7Response; | ||
return { | ||
type: 'token', | ||
name: lsp.name, | ||
symbol: lsp.symbol, | ||
address: lsp.address, | ||
token_url: lsp.linkUrl, | ||
address_url: lsp.linkUrl, | ||
icon_url: null, | ||
token_type: 'LSP7', | ||
exchange_rate: null, | ||
total_supply: '0', | ||
is_verified_via_admin_panel: false, | ||
is_smart_contract_verified: false, | ||
}; | ||
}); | ||
} catch (error) { | ||
return error; | ||
} | ||
}, []); | ||
} |
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,21 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import type { ResourceError, ResourceName, ResourcePayload } from './resources'; | ||
import type { Params } from './useApiQuery'; | ||
import { getResourceKey } from './useApiQuery'; | ||
import useLSP7ApiFetch from './useLSP7ApiFetch'; | ||
|
||
export default function useLSP7Query<R extends ResourceName, E = unknown>( | ||
resource: R, | ||
{ queryOptions, pathParams, queryParams }: Params<R, E> = {}, | ||
) { | ||
const lspFetch = useLSP7ApiFetch(); | ||
return useQuery<ResourcePayload<R>, ResourceError<E>, ResourcePayload<R>>({ | ||
// eslint-disable-next-line @tanstack/query/exhaustive-deps | ||
queryKey: getResourceKey(resource, { pathParams, queryParams }), | ||
queryFn: async() => { | ||
return await lspFetch(queryParams?.q as string) as Promise<ResourcePayload<R>>; | ||
}, | ||
...queryOptions, | ||
}); | ||
} |
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,49 @@ | ||
export type LSP3Response = { | ||
type: string; | ||
hasProfileName: boolean; | ||
hasProfileImage: boolean; | ||
LSP3Profile: { | ||
name: string; | ||
profileImage: { | ||
[key: number]: { | ||
url: string; | ||
}; | ||
}; | ||
}; | ||
} | ||
|
||
export type LSP7Response = { | ||
address: string; | ||
updatedAtBlockNameAndSymbol: number; | ||
symbol: string; | ||
hasTokenSymbol: boolean; | ||
name: string; | ||
hasTokenName: boolean; | ||
LSP4Metadata: { | ||
description: string; | ||
icon: { | ||
[key: number]: { | ||
url: string; | ||
}; | ||
}; | ||
images: { | ||
[key: number]: { | ||
[key: number]: { | ||
url: string; | ||
}; | ||
}; | ||
}; | ||
}; | ||
assetImageUrl: string; | ||
assetImageRawUrl: string; | ||
hasAssetImage: boolean; | ||
iconImageUrl: string; | ||
iconImageRawUrl: string; | ||
hasIconImage: boolean; | ||
updatedAtBlockMetadata: number; | ||
description: string; | ||
objectID: string; | ||
linkUrl: string; | ||
} | ||
|
||
export type LSPResponse = LSP3Response | LSP7Response |
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 was deleted.
Oops, something went wrong.
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