-
Notifications
You must be signed in to change notification settings - Fork 1
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
10 changed files
with
231 additions
and
47 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
CONTRACT_IDENTITY="AddressOfIdentityContract" | ||
CONTRACT_ADDRESS_BOOK="AddressOfAddressBookContract" | ||
CONTRACT_ADDRESS_BOOK="AddressOfAddressBookContract" | ||
RELAY_CHAIN="kusama_or_polkadot" |
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,7 @@ | ||
type RELAY_CHAIN_OPTION = 'polkadot' | 'kusama'; | ||
const RELAY_CHAIN_ENDPOINTS = { | ||
polkadot: "wss://polkadot.api.onfinality.io/public-ws", | ||
kusama: "wss://kusama.api.onfinality.io/public-ws" | ||
}; | ||
export const RELAY_CHAIN = (process.env.RELAY_CHAIN || 'polkadot') as RELAY_CHAIN_OPTION; | ||
export const RELAY_CHAIN_ENDPOINT = RELAY_CHAIN_ENDPOINTS[RELAY_CHAIN]; |
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,105 @@ | ||
import { ApiPromise, WsProvider } from '@polkadot/api'; | ||
import jsonrpc from '@polkadot/types/interfaces/jsonrpc'; | ||
import { DefinitionRpcExt } from '@polkadot/types/types'; | ||
import React, { useContext, useEffect, useReducer } from 'react'; | ||
|
||
import { RELAY_CHAIN_ENDPOINT } from '@/consts'; | ||
|
||
import { useToast } from '../Toast'; | ||
|
||
/// | ||
// Initial state for `useReducer` | ||
|
||
type State = { | ||
socket: string; | ||
jsonrpc: Record<string, Record<string, DefinitionRpcExt>>; | ||
api: any; | ||
apiError: any; | ||
apiState: any; | ||
}; | ||
|
||
const initialState: State = { | ||
// These are the states | ||
socket: RELAY_CHAIN_ENDPOINT, | ||
jsonrpc: { ...jsonrpc }, | ||
api: null, | ||
apiError: null, | ||
apiState: null, | ||
}; | ||
|
||
/// | ||
// Reducer function for `useReducer` | ||
|
||
const reducer = (state: any, action: any) => { | ||
switch (action.type) { | ||
case 'CONNECT_INIT': | ||
return { ...state, apiState: 'CONNECT_INIT' }; | ||
case 'CONNECT': | ||
return { ...state, api: action.payload, apiState: 'CONNECTING' }; | ||
case 'CONNECT_SUCCESS': | ||
return { ...state, apiState: 'READY' }; | ||
case 'CONNECT_ERROR': | ||
return { ...state, apiState: 'ERROR', apiError: action.payload }; | ||
default: | ||
throw new Error(`Unknown type: ${action.type}`); | ||
} | ||
}; | ||
|
||
/// | ||
// Connecting to the Substrate node | ||
|
||
const connect = (state: any, dispatch: any) => { | ||
const { apiState, socket, jsonrpc } = state; | ||
// We only want this function to be performed once | ||
if (apiState) return; | ||
|
||
dispatch({ type: 'CONNECT_INIT' }); | ||
|
||
const provider = new WsProvider(socket); | ||
const _api = new ApiPromise({ provider, rpc: jsonrpc }); | ||
|
||
// Set listeners for disconnection and reconnection event. | ||
_api.on('connected', () => { | ||
dispatch({ type: 'CONNECT', payload: _api }); | ||
// `ready` event is not emitted upon reconnection and is checked explicitly here. | ||
_api.isReady.then(() => dispatch({ type: 'CONNECT_SUCCESS' })); | ||
}); | ||
_api.on('ready', () => dispatch({ type: 'CONNECT_SUCCESS' })); | ||
_api.on('error', (err) => dispatch({ type: 'CONNECT_ERROR', payload: err })); | ||
}; | ||
|
||
const defaultValue = { | ||
state: initialState, | ||
}; | ||
|
||
const RelayApiContext = React.createContext(defaultValue); | ||
|
||
const RelayApiContextProvider = (props: any) => { | ||
const [state, dispatch] = useReducer(reducer, initialState); | ||
const { toastError, toastSuccess } = useToast(); | ||
|
||
useEffect(() => { | ||
state.apiError && | ||
toastError( | ||
`Failed to connect to relay chain: error = ${state.apiError.toString()}` | ||
); | ||
}, [state.apiError]); | ||
|
||
useEffect(() => { | ||
state.apiState === 'READY' && | ||
toastSuccess('Successfully connected to relay chain'); | ||
}, [state.apiState]); | ||
|
||
useEffect(() => { | ||
connect(state, dispatch); | ||
}, [process.env.RELAY_CHAIN_ENDPOINT]); | ||
|
||
return ( | ||
<RelayApiContext.Provider value={{ state }}> | ||
{props.children} | ||
</RelayApiContext.Provider> | ||
); | ||
}; | ||
const useRelayApi = () => useContext(RelayApiContext); | ||
|
||
export { RelayApiContextProvider, useRelayApi }; |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.identity-container { | ||
.identityContainer { | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
|
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,9 @@ | ||
.transferContainer { | ||
display: flex; | ||
flex-direction: column; | ||
background: white; | ||
margin-left: auto; | ||
margin-right: auto; | ||
min-width: 500px; | ||
padding: 24px 16px; | ||
} |