-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Cross-chain region transfers * both directions work * conditional * remove unused * remove network specific url for now * connect only when experimental
- Loading branch information
Showing
13 changed files
with
261 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React, { useContext, useEffect, useReducer } from 'react'; | ||
|
||
import { ApiState } from '@/contexts/apis/types'; | ||
import { useToast } from '@/contexts/toast'; | ||
|
||
import { connect, disconnect, initialState, reducer } from '../common'; | ||
import { EXPERIMENTAL, WS_REGIONX_CHAIN } from '../consts'; | ||
|
||
const types = { | ||
CoreIndex: 'u32', | ||
CoreMask: 'Vec<u8>', | ||
Timeslice: 'u32', | ||
RegionId: { | ||
begin: 'Timeslice', | ||
core: 'CoreIndex', | ||
mask: 'CoreMask', | ||
}, | ||
RegionRecord: { | ||
end: 'Timeslice', | ||
owner: 'AccountId', | ||
paid: 'Option<Balance>', | ||
}, | ||
}; | ||
|
||
const defaultValue = { | ||
state: { ...initialState }, | ||
disconnectRegionX: (): void => { | ||
/** */ | ||
}, | ||
}; | ||
|
||
const RegionXApiContext = React.createContext(defaultValue); | ||
|
||
const RegionXApiContextProvider = (props: any) => { | ||
const [state, dispatch] = useReducer(reducer, initialState); | ||
const { toastError, toastSuccess } = useToast(); | ||
|
||
useEffect(() => { | ||
state.apiError && toastError(`Failed to connect to RegionX chain`); | ||
}, [state.apiError, toastError]); | ||
|
||
useEffect(() => { | ||
state.apiState === ApiState.READY && | ||
state.name && | ||
toastSuccess(`Successfully connected to ${state.name}`); | ||
}, [state.apiState, state.name, toastSuccess]); | ||
|
||
const disconnectRegionX = () => disconnect(state); | ||
|
||
useEffect(() => { | ||
if (!EXPERIMENTAL) return; | ||
// TODO: currently we use the RegionX chain only when the experimental flag is on. | ||
// | ||
// For this reason we don't have different urls based on the network. However, this | ||
// should be updated once this is in production. | ||
connect(state, WS_REGIONX_CHAIN, dispatch, false, types); | ||
}, [state]); | ||
|
||
return ( | ||
<RegionXApiContext.Provider value={{ state, disconnectRegionX }}> | ||
{props.children} | ||
</RegionXApiContext.Provider> | ||
); | ||
}; | ||
|
||
const useRegionXApi = () => useContext(RegionXApiContext); | ||
|
||
export { RegionXApiContextProvider, useRegionXApi }; |
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,40 @@ | ||
import { ApiPromise } from '@polkadot/api'; | ||
import { Region } from 'coretime-utils'; | ||
|
||
import { parseHNString } from '@/utils/functions'; | ||
|
||
export const fetchRegions = async ( | ||
regionxApi: ApiPromise | null | ||
): Promise<Array<Region>> => { | ||
if (!regionxApi) { | ||
return []; | ||
} | ||
try { | ||
const regionEntries = await regionxApi.query.regions.regions.entries(); | ||
|
||
const regions: Array<Region> = regionEntries | ||
.map(([key, value]) => { | ||
const keyTuple: any = key.toHuman(undefined, true); | ||
const { begin, core, mask } = keyTuple[0] as any; | ||
const { owner, record: _record } = value.toHuman() as any; | ||
|
||
const regionId = { | ||
begin: parseHNString(begin.toString()), | ||
core: parseHNString(core.toString()), | ||
mask: mask, | ||
}; | ||
return new Region( | ||
regionId, | ||
/*dummy data*/ { | ||
end: 0, | ||
owner, | ||
paid: null, | ||
} | ||
); | ||
}) | ||
.filter((entry) => !!entry) as Array<Region>; | ||
return regions; | ||
} catch (_) { | ||
return []; | ||
} | ||
}; |
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.