forked from blockscout/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
5,558 additions
and
153 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
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,50 @@ | ||
import { ApiPromise, WsProvider } from '@polkadot/api'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import React from 'react'; | ||
|
||
interface PolkadotApiContenxtType { | ||
isLoading: boolean; | ||
isError: boolean; | ||
isSuccess: boolean; | ||
api?: ApiPromise; | ||
} | ||
|
||
export const PolkadotApiContext = React.createContext<PolkadotApiContenxtType>({ isLoading: true, isError: false, isSuccess: false }); | ||
|
||
interface PolkadotApiProps { | ||
children: React.ReactNode; | ||
url?: string; | ||
} | ||
|
||
export function PolkadotApiProvider({ children, url }: PolkadotApiProps) { | ||
const { isLoading, isError, isSuccess, data } = useQuery({ | ||
queryKey: [ 'PolkadotApi', url ], | ||
queryFn: async() => { | ||
const provider = new WsProvider(url); | ||
const api = await ApiPromise.create({ provider }); | ||
|
||
return api; | ||
}, | ||
}); | ||
|
||
return ( | ||
<PolkadotApiContext.Provider value={{ | ||
isLoading, | ||
isSuccess, | ||
isError, | ||
api: data, | ||
}}> | ||
{ children } | ||
</PolkadotApiContext.Provider> | ||
); | ||
} | ||
|
||
export function usePolkadotApi() { | ||
const context = React.useContext(PolkadotApiContext); | ||
|
||
if (context?.isLoading === undefined) { | ||
throw new Error('usePolkadotApi must be used within a PolkadotApiProvider'); | ||
} | ||
|
||
return context; | ||
} |
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,12 @@ | ||
import { getEnvValue } from 'configs/app/utils'; | ||
|
||
import { usePolkadotApi } from './context'; | ||
|
||
export const useRegistry = () => { | ||
const { api, isSuccess } = usePolkadotApi(); | ||
|
||
return { | ||
decimals: isSuccess ? api?.registry.chainDecimals[0].toString() : getEnvValue('NEXT_PUBLIC_NETWORK_CURRENCY_DECIMALS'), | ||
symbol: isSuccess ? api?.registry.chainTokens[0] : (getEnvValue('NEXT_PUBLIC_NETWORK_CURRENCY_SYMBOL') || '18'), | ||
}; | ||
}; |
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
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
Oops, something went wrong.