-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: identity from ppl chain when available
- Loading branch information
Showing
11 changed files
with
188 additions
and
34 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,99 @@ | ||
import React, { useMemo } from 'react' | ||
import { ApiPromise, WsProvider } from '@polkadot/api' | ||
import { useState, useEffect, createContext, useContext } from 'react' | ||
import { useNetwork } from './NetworkContext' | ||
import '@polkadot/api-augment' | ||
|
||
type ApiContextProps = { | ||
children: React.ReactNode | React.ReactNode[] | ||
} | ||
|
||
export interface IApiContext { | ||
pplApi?: false | ApiPromise | ||
pplChainInfo?: ChainInfoHuman | ||
} | ||
|
||
export interface ChainInfoHuman { | ||
ss58Format: number | ||
tokenDecimals: number | ||
tokenSymbol: string | ||
} | ||
|
||
interface RawChainInfoHuman { | ||
ss58Format: string | ||
tokenDecimals: string[] | ||
tokenSymbol: string[] | ||
} | ||
|
||
const PplApiContext = createContext<IApiContext | undefined>(undefined) | ||
|
||
const PplApiContextProvider = ({ children }: ApiContextProps) => { | ||
const { selectedNetworkInfo } = useNetwork() | ||
const [chainInfo, setChainInfo] = useState<ChainInfoHuman | undefined>() | ||
const [pplApiPromise, setPplApiPromise] = useState<ApiPromise | undefined>() | ||
const [isPplApiReady, setIsPplApiReady] = useState(false) | ||
const provider = useMemo( | ||
() => | ||
!!selectedNetworkInfo?.pplChainRpcUrl && new WsProvider(selectedNetworkInfo?.pplChainRpcUrl), | ||
[selectedNetworkInfo] | ||
) | ||
|
||
useEffect(() => { | ||
if (!provider) return | ||
|
||
// console.log('---> connecting to', provider.endpoint) | ||
setIsPplApiReady(false) | ||
const pplApi = new ApiPromise({ provider }) | ||
pplApi.isReady.then((newApi) => setPplApiPromise(newApi)).catch(console.error) | ||
|
||
return () => { | ||
// console.log('<---disconnecting') | ||
setIsPplApiReady(false) | ||
!!pplApi && pplApi.disconnect() | ||
setPplApiPromise(undefined) | ||
} | ||
|
||
// prevent an infinite loop | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [provider]) | ||
|
||
useEffect(() => { | ||
if (!pplApiPromise) return | ||
|
||
pplApiPromise.isReady | ||
.then((pplApi) => { | ||
setIsPplApiReady(true) | ||
|
||
const info = pplApi.registry.getChainProperties() | ||
const raw = info?.toHuman() as unknown as RawChainInfoHuman | ||
setChainInfo({ | ||
// some parachains such as interlay have a comma in the format, e.g: "2,042" | ||
ss58Format: Number(raw?.ss58Format.replace(',', '')) || 0, | ||
tokenDecimals: Number(raw?.tokenDecimals[0]) || 0, | ||
tokenSymbol: raw?.tokenSymbol[0] || '' | ||
}) | ||
}) | ||
.catch(console.error) | ||
}, [pplApiPromise]) | ||
|
||
return ( | ||
<PplApiContext.Provider | ||
value={{ | ||
pplApi: isPplApiReady && pplApiPromise, | ||
pplChainInfo: chainInfo | ||
}} | ||
> | ||
{children} | ||
</PplApiContext.Provider> | ||
) | ||
} | ||
|
||
const usePplApi = () => { | ||
const context = useContext(PplApiContext) | ||
if (context === undefined) { | ||
throw new Error('usePplApi must be used within a PplApiContextProvider') | ||
} | ||
return context | ||
} | ||
|
||
export { PplApiContextProvider, usePplApi } |
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,14 @@ | ||
import { useMemo } from 'react' | ||
import { useNetwork } from '../contexts/NetworkContext' | ||
import { useIdenityApi } from './useIdentityApi' | ||
|
||
export const useHasIdentityFeature = () => { | ||
const { api } = useIdenityApi() | ||
const { selectedNetworkInfo } = useNetwork() | ||
const hasIdentityPallet = useMemo(() => !!api && !!api.tx?.identity?.setIdentity, [api]) | ||
const hasPplChain = useMemo(() => !!selectedNetworkInfo?.pplChainRpcUrl, [selectedNetworkInfo]) | ||
return { | ||
hasPplChain, | ||
hasIdentityPallet | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ApiPromise } from '@polkadot/api' | ||
import { useState, useEffect } from 'react' | ||
import { useApi } from '../contexts/ApiContext' | ||
import { ChainInfoHuman, usePplApi } from '../contexts/PeopleChainApiContext' | ||
|
||
export const useIdenityApi = () => { | ||
const { api, chainInfo } = useApi() | ||
const { pplApi, pplChainInfo } = usePplApi() | ||
const [apiToUse, setApiToUse] = useState<ApiPromise | null>(null) | ||
const [chainInfoToUse, setChainInfoToUse] = useState<ChainInfoHuman | undefined>(undefined) | ||
|
||
useEffect(() => { | ||
if (!pplApi && !api) { | ||
return | ||
} | ||
|
||
if (pplApi) { | ||
setApiToUse(pplApi) | ||
setChainInfoToUse(pplChainInfo) | ||
} else if (api) { | ||
setApiToUse(api) | ||
setChainInfoToUse(chainInfo) | ||
} | ||
}, [api, chainInfo, pplApi, pplChainInfo]) | ||
|
||
useEffect(() => { | ||
if (!pplApi && !api) { | ||
return | ||
} | ||
|
||
if (pplApi) { | ||
setApiToUse(pplApi) | ||
} else if (api) { | ||
setApiToUse(api) | ||
} | ||
}, [api, pplApi]) | ||
|
||
return { api: apiToUse, chainInfo: chainInfoToUse } | ||
} |
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