@xyo-network/react-chain-provider 1.20.1 → 1.20.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/contexts/gateway/Provider.d.ts +8 -0
- package/dist/browser/contexts/gateway/Provider.d.ts.map +1 -0
- package/dist/browser/contexts/gateway/context.d.ts +8 -0
- package/dist/browser/contexts/gateway/context.d.ts.map +1 -0
- package/dist/browser/contexts/gateway/index.d.ts +5 -0
- package/dist/browser/contexts/gateway/index.d.ts.map +1 -0
- package/dist/browser/contexts/gateway/state.d.ts +13 -0
- package/dist/browser/contexts/gateway/state.d.ts.map +1 -0
- package/dist/browser/contexts/gateway/story/GatewayStats.d.ts +9 -0
- package/dist/browser/contexts/gateway/story/GatewayStats.d.ts.map +1 -0
- package/dist/browser/contexts/gateway/story/GatewayStatus.d.ts +3 -0
- package/dist/browser/contexts/gateway/story/GatewayStatus.d.ts.map +1 -0
- package/dist/browser/contexts/gateway/use.d.ts +4 -0
- package/dist/browser/contexts/gateway/use.d.ts.map +1 -0
- package/dist/browser/contexts/in-page-gateways/Provider.d.ts +8 -0
- package/dist/browser/contexts/in-page-gateways/Provider.d.ts.map +1 -0
- package/dist/browser/contexts/in-page-gateways/context.d.ts +8 -0
- package/dist/browser/contexts/in-page-gateways/context.d.ts.map +1 -0
- package/dist/browser/contexts/in-page-gateways/index.d.ts +5 -0
- package/dist/browser/contexts/in-page-gateways/index.d.ts.map +1 -0
- package/dist/browser/contexts/in-page-gateways/state.d.ts +10 -0
- package/dist/browser/contexts/in-page-gateways/state.d.ts.map +1 -0
- package/dist/browser/contexts/in-page-gateways/use.d.ts +4 -0
- package/dist/browser/contexts/in-page-gateways/use.d.ts.map +1 -0
- package/dist/browser/contexts/index.d.ts +2 -0
- package/dist/browser/contexts/index.d.ts.map +1 -1
- package/dist/browser/hooks/client/index.d.ts +2 -2
- package/dist/browser/hooks/client/index.d.ts.map +1 -1
- package/dist/browser/hooks/client/{useClient.d.ts → useClientFromWallet.d.ts} +3 -1
- package/dist/browser/hooks/client/useClientFromWallet.d.ts.map +1 -0
- package/dist/browser/hooks/client/useGatewayFromWallet.d.ts +6 -0
- package/dist/browser/hooks/client/useGatewayFromWallet.d.ts.map +1 -0
- package/dist/browser/hooks/gateway/index.d.ts +4 -0
- package/dist/browser/hooks/gateway/index.d.ts.map +1 -0
- package/dist/browser/hooks/gateway/useNetwork.d.ts +2 -0
- package/dist/browser/hooks/gateway/useNetwork.d.ts.map +1 -0
- package/dist/browser/hooks/gateway/useRunner.d.ts +2 -0
- package/dist/browser/hooks/gateway/useRunner.d.ts.map +1 -0
- package/dist/browser/hooks/gateway/useViewer.d.ts +2 -0
- package/dist/browser/hooks/gateway/useViewer.d.ts.map +1 -0
- package/dist/browser/hooks/index.d.ts +1 -0
- package/dist/browser/hooks/index.d.ts.map +1 -1
- package/dist/browser/hooks/useCurrentBlock.d.ts +33 -0
- package/dist/browser/hooks/useCurrentBlock.d.ts.map +1 -1
- package/dist/browser/hooks/useHttpRpcRunner.d.ts.map +1 -1
- package/dist/browser/hooks/viewer/useCheckRpc.d.ts.map +1 -1
- package/dist/browser/hooks/viewer/useHttpRpcViewer.d.ts +1 -0
- package/dist/browser/hooks/viewer/useHttpRpcViewer.d.ts.map +1 -1
- package/dist/browser/index.d.ts +1 -0
- package/dist/browser/index.d.ts.map +1 -1
- package/dist/browser/index.mjs +388 -187
- package/dist/browser/index.mjs.map +1 -1
- package/dist/browser/lib/buildGateway.d.ts +4 -0
- package/dist/browser/lib/buildGateway.d.ts.map +1 -0
- package/dist/browser/lib/index.d.ts +1 -0
- package/dist/browser/lib/index.d.ts.map +1 -1
- package/dist/browser/types/ContextGatewayType.d.ts +3 -0
- package/dist/browser/types/ContextGatewayType.d.ts.map +1 -0
- package/dist/browser/types/GatewayFromWallet.d.ts +8 -0
- package/dist/browser/types/GatewayFromWallet.d.ts.map +1 -0
- package/dist/browser/types/index.d.ts +3 -0
- package/dist/browser/types/index.d.ts.map +1 -0
- package/package.json +21 -19
- package/src/contexts/gateway/Provider.tsx +82 -0
- package/src/contexts/gateway/context.ts +5 -0
- package/src/contexts/gateway/index.ts +4 -0
- package/src/contexts/gateway/state.ts +15 -0
- package/src/contexts/gateway/story/GatewayStats.tsx +33 -0
- package/src/contexts/gateway/story/GatewayStatus.tsx +53 -0
- package/src/contexts/gateway/story/Provider.stories.tsx +38 -0
- package/src/contexts/gateway/story/ProviderWithWallet.stories.tsx +46 -0
- package/src/contexts/gateway/use.ts +5 -0
- package/src/contexts/in-page-gateways/Provider.tsx +82 -0
- package/src/contexts/in-page-gateways/context.ts +5 -0
- package/src/contexts/in-page-gateways/index.ts +4 -0
- package/src/contexts/in-page-gateways/state.ts +12 -0
- package/src/contexts/in-page-gateways/use.ts +5 -0
- package/src/contexts/index.ts +2 -0
- package/src/hooks/client/index.ts +2 -2
- package/src/hooks/client/permissions/usePermissions.ts +2 -2
- package/src/hooks/client/{useClient.ts → useClientFromWallet.ts} +4 -1
- package/src/hooks/client/{useGateway.ts → useGatewayFromWallet.ts} +7 -3
- package/src/hooks/gateway/index.ts +3 -0
- package/src/hooks/gateway/useNetwork.ts +6 -0
- package/src/hooks/gateway/useRunner.ts +6 -0
- package/src/hooks/gateway/useViewer.ts +6 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useConnectAccount.ts +2 -2
- package/src/hooks/useCurrentBlock.ts +13 -0
- package/src/hooks/useHttpRpcRunner.ts +1 -0
- package/src/hooks/viewer/useCheckRpc.ts +8 -17
- package/src/hooks/viewer/useHttpRpcViewer.ts +1 -0
- package/src/hooks/viewer/useViewerFromWallet.ts +2 -2
- package/src/index.ts +1 -0
- package/src/lib/buildGateway.ts +29 -0
- package/src/lib/index.ts +1 -0
- package/src/types/ContextGatewayType.ts +4 -0
- package/src/types/GatewayFromWallet.ts +8 -0
- package/src/types/index.ts +2 -0
- package/dist/browser/components/connected/ConnectAccountsStack.stories.d.ts +0 -6
- package/dist/browser/components/connected/ConnectAccountsStack.stories.d.ts.map +0 -1
- package/dist/browser/contexts/current-block/Provider.stories.d.ts +0 -7
- package/dist/browser/contexts/current-block/Provider.stories.d.ts.map +0 -1
- package/dist/browser/hooks/client/useClient.d.ts.map +0 -1
- package/dist/browser/hooks/client/useGateway.d.ts +0 -8
- package/dist/browser/hooks/client/useGateway.d.ts.map +0 -1
- package/dist/browser/hooks/viewer/UseStepRewardWeightTest.stories.d.ts +0 -12
- package/dist/browser/hooks/viewer/UseStepRewardWeightTest.stories.d.ts.map +0 -1
- package/dist/browser/hooks/viewer/useHttpRpcViewer.stories.d.ts +0 -9
- package/dist/browser/hooks/viewer/useHttpRpcViewer.stories.d.ts.map +0 -1
- package/dist/browser/hooks/viewer/useViewerFromWallet.stories.d.ts +0 -12
- package/dist/browser/hooks/viewer/useViewerFromWallet.stories.d.ts.map +0 -1
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/* eslint-disable react-hooks/set-state-in-effect */
|
|
2
|
+
/* eslint-disable react-hooks-extra/no-direct-set-state-in-use-effect */
|
|
3
|
+
import { isDefinedNotNull, isNull } from '@xylabs/sdk-js'
|
|
4
|
+
import { ErrorRender } from '@xyo-network/react-error'
|
|
5
|
+
import type { GatewayName } from '@xyo-network/xl1-sdk'
|
|
6
|
+
import type { PropsWithChildren } from 'react'
|
|
7
|
+
import React, {
|
|
8
|
+
useEffect, useMemo, useState,
|
|
9
|
+
} from 'react'
|
|
10
|
+
|
|
11
|
+
import { useGatewayFromWallet } from '../../hooks/index.ts'
|
|
12
|
+
import type { ContextGatewayType } from '../../types/index.ts'
|
|
13
|
+
import { useProvidedInPageGateways } from '../in-page-gateways/index.ts'
|
|
14
|
+
import { GatewayContext } from './context.ts'
|
|
15
|
+
import type { GatewayState } from './state.ts'
|
|
16
|
+
|
|
17
|
+
export interface GatewayProviderProps extends PropsWithChildren {
|
|
18
|
+
gatewayName?: GatewayName
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const GatewayProvider: React.FC<GatewayProviderProps> = ({ gatewayName, children }) => {
|
|
22
|
+
const [defaultGateway, setDefaultGateway] = useState<ContextGatewayType | null | undefined>()
|
|
23
|
+
const [gateways, setGateways] = useState<GatewayState['gateways']>({
|
|
24
|
+
inPageGateway: undefined,
|
|
25
|
+
walletGateway: undefined,
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
const {
|
|
29
|
+
gateway: gatewayFromWallet,
|
|
30
|
+
error: gatewayFromWalletError,
|
|
31
|
+
} = useGatewayFromWallet(gatewayName)
|
|
32
|
+
|
|
33
|
+
const {
|
|
34
|
+
gateways: allGateways,
|
|
35
|
+
errors: allGatewayErrors,
|
|
36
|
+
clearAll,
|
|
37
|
+
} = useProvidedInPageGateways(true)
|
|
38
|
+
|
|
39
|
+
const gatewayFromConfig = gatewayName ? allGateways[gatewayName] : undefined
|
|
40
|
+
const gatewayFromConfigError = gatewayName ? allGatewayErrors[gatewayName] : undefined
|
|
41
|
+
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (isNull(gatewayFromWallet)) {
|
|
44
|
+
// if no gateway from wallet, use the one from config
|
|
45
|
+
setDefaultGateway(gatewayFromConfig)
|
|
46
|
+
setGateways({
|
|
47
|
+
inPageGateway: gatewayFromConfig,
|
|
48
|
+
walletGateway: null,
|
|
49
|
+
})
|
|
50
|
+
} else if (isDefinedNotNull(gatewayFromWallet)) {
|
|
51
|
+
setDefaultGateway(gatewayFromWallet)
|
|
52
|
+
setGateways({
|
|
53
|
+
inPageGateway: gatewayFromConfig,
|
|
54
|
+
walletGateway: gatewayFromWallet,
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
}, [gatewayFromConfig, gatewayFromWallet])
|
|
58
|
+
|
|
59
|
+
const value = useMemo(() => {
|
|
60
|
+
const value: GatewayState = {
|
|
61
|
+
defaultGateway,
|
|
62
|
+
error: gatewayFromWalletError || gatewayFromConfigError,
|
|
63
|
+
gateways,
|
|
64
|
+
provided: true,
|
|
65
|
+
resetGatewaysFromConfig: clearAll,
|
|
66
|
+
}
|
|
67
|
+
return value
|
|
68
|
+
}, [
|
|
69
|
+
defaultGateway,
|
|
70
|
+
gatewayFromWalletError,
|
|
71
|
+
gatewayFromConfigError,
|
|
72
|
+
gateways,
|
|
73
|
+
clearAll,
|
|
74
|
+
])
|
|
75
|
+
|
|
76
|
+
return (
|
|
77
|
+
<GatewayContext value={value}>
|
|
78
|
+
<ErrorRender error={gatewayFromConfigError} />
|
|
79
|
+
{children}
|
|
80
|
+
</GatewayContext>
|
|
81
|
+
)
|
|
82
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ContextExState } from '@xylabs/react-shared'
|
|
2
|
+
|
|
3
|
+
import type { ContextGatewayType } from '../../types/index.ts'
|
|
4
|
+
|
|
5
|
+
export interface GatewayStateFields {
|
|
6
|
+
defaultGateway: ContextGatewayType | undefined | null
|
|
7
|
+
error: Error | null | undefined
|
|
8
|
+
gateways: {
|
|
9
|
+
inPageGateway: ContextGatewayType | undefined | null
|
|
10
|
+
walletGateway: ContextGatewayType | undefined | null
|
|
11
|
+
}
|
|
12
|
+
resetGatewaysFromConfig: () => void
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type GatewayState = ContextExState<GatewayStateFields>
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Stack, Typography } from '@mui/material'
|
|
2
|
+
import type { AccountInstance } from '@xyo-network/sdk-js'
|
|
3
|
+
import type { GatewayName } from '@xyo-network/xl1-sdk'
|
|
4
|
+
|
|
5
|
+
import { InPageGatewaysProvider } from '../../in-page-gateways/index.ts'
|
|
6
|
+
import { GatewayProvider } from '../Provider.tsx'
|
|
7
|
+
import { GatewayStatus } from './GatewayStatus.tsx'
|
|
8
|
+
|
|
9
|
+
export interface GatewayStatsProps {
|
|
10
|
+
account?: AccountInstance
|
|
11
|
+
id?: GatewayName
|
|
12
|
+
url?: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const GatewayStats: React.FC<GatewayStatsProps> = ({
|
|
16
|
+
account, id, url,
|
|
17
|
+
}) => {
|
|
18
|
+
return (
|
|
19
|
+
<InPageGatewaysProvider account={account}>
|
|
20
|
+
<GatewayProvider key={id} gatewayName={id}>
|
|
21
|
+
<Stack gap={1}>
|
|
22
|
+
<Typography variant="subtitle1">
|
|
23
|
+
<strong>{id}</strong>
|
|
24
|
+
</Typography>
|
|
25
|
+
<Typography variant="caption" color="text.secondary">
|
|
26
|
+
{url}
|
|
27
|
+
</Typography>
|
|
28
|
+
<GatewayStatus />
|
|
29
|
+
</Stack>
|
|
30
|
+
</GatewayProvider>
|
|
31
|
+
</InPageGatewaysProvider>
|
|
32
|
+
)
|
|
33
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Alert, Chip, Stack, Typography,
|
|
3
|
+
} from '@mui/material'
|
|
4
|
+
|
|
5
|
+
import { useProvidedGateway } from '../use.ts'
|
|
6
|
+
|
|
7
|
+
/** Inner component that consumes the GatewayContext to display load status */
|
|
8
|
+
export const GatewayStatus: React.FC = () => {
|
|
9
|
+
const { defaultGateway: gateway, error } = useProvidedGateway(false) ?? {}
|
|
10
|
+
|
|
11
|
+
if (error) {
|
|
12
|
+
return (
|
|
13
|
+
<Alert severity="error">
|
|
14
|
+
<Typography variant="body2">
|
|
15
|
+
Failed to load gateway:
|
|
16
|
+
{' '}
|
|
17
|
+
{error.message}
|
|
18
|
+
</Typography>
|
|
19
|
+
</Alert>
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (gateway === undefined) {
|
|
24
|
+
return (
|
|
25
|
+
<Alert severity="info">
|
|
26
|
+
<Typography variant="body2">Gateway loading...</Typography>
|
|
27
|
+
</Alert>
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (gateway === null) {
|
|
32
|
+
return (
|
|
33
|
+
<Alert severity="warning">
|
|
34
|
+
<Typography variant="body2">Gateway not available</Typography>
|
|
35
|
+
</Alert>
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<Alert severity="success">
|
|
41
|
+
<Stack direction="row" gap={1} alignItems="center">
|
|
42
|
+
<Typography variant="body2">Gateway loaded</Typography>
|
|
43
|
+
<Chip label="connected" color="success" size="small" />
|
|
44
|
+
{gateway?.connection?.viewer && (
|
|
45
|
+
<Chip label="viewer available" color="success" size="small" />
|
|
46
|
+
)}
|
|
47
|
+
{gateway?.connection.runner && (
|
|
48
|
+
<Chip label="runner available" color="success" size="small" />
|
|
49
|
+
)}
|
|
50
|
+
</Stack>
|
|
51
|
+
</Alert>
|
|
52
|
+
)
|
|
53
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Stack } from '@mui/material'
|
|
2
|
+
import type { Meta, StoryObj } from '@storybook/react-vite'
|
|
3
|
+
import { Account } from '@xyo-network/sdk-js'
|
|
4
|
+
import { DefaultNetworks } from '@xyo-network/xl1-sdk'
|
|
5
|
+
|
|
6
|
+
import type { GatewayProviderProps } from '../Provider.tsx'
|
|
7
|
+
import { GatewayProvider } from '../Provider.tsx'
|
|
8
|
+
import { GatewayStats } from './GatewayStats.tsx'
|
|
9
|
+
|
|
10
|
+
const meta: Meta<GatewayProviderProps> = {
|
|
11
|
+
title: 'provider/GatewayProvider',
|
|
12
|
+
component: GatewayProvider,
|
|
13
|
+
|
|
14
|
+
parameters: { docs: { description: { component: 'Wraps children with a `GatewayContext` populated from the given `gatewayName` and show the gateways from the default networks if available.' } } },
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default meta
|
|
18
|
+
|
|
19
|
+
const account = await Account.random()
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Convenience story that proves ALL default networks are represented.
|
|
23
|
+
* Each row corresponds to one entry in DefaultNetworks.
|
|
24
|
+
*/
|
|
25
|
+
const AllDefaultNetworks: StoryObj<GatewayProviderProps> = {
|
|
26
|
+
name: 'All Default Networks',
|
|
27
|
+
render: () => {
|
|
28
|
+
return (
|
|
29
|
+
<Stack gap={3} p={2}>
|
|
30
|
+
{DefaultNetworks.map(network => (
|
|
31
|
+
<GatewayStats key={network.id} id={network.id} url={network.url} account={account} />
|
|
32
|
+
))}
|
|
33
|
+
</Stack>
|
|
34
|
+
)
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { AllDefaultNetworks }
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Alert, Stack } from '@mui/material'
|
|
2
|
+
import type { Meta, StoryObj } from '@storybook/react-vite'
|
|
3
|
+
import { isDefinedNotNull } from '@xylabs/sdk-js'
|
|
4
|
+
import { IframeWalletWarningDecorator } from '@xyo-network/react-chain-shared'
|
|
5
|
+
import type { GatewayName } from '@xyo-network/xl1-sdk'
|
|
6
|
+
|
|
7
|
+
import { useClientFromWallet } from '../../../hooks/index.ts'
|
|
8
|
+
import type { GatewayProviderProps } from '../Provider.tsx'
|
|
9
|
+
import { GatewayProvider } from '../Provider.tsx'
|
|
10
|
+
import { GatewayStats } from './GatewayStats.tsx'
|
|
11
|
+
|
|
12
|
+
const meta: Meta<GatewayProviderProps> = {
|
|
13
|
+
title: 'provider/GatewayProvider',
|
|
14
|
+
component: GatewayProvider,
|
|
15
|
+
decorators: [IframeWalletWarningDecorator],
|
|
16
|
+
|
|
17
|
+
parameters: { docs: { description: { component: 'Wraps children with a `GatewayContext` populated from the given `gatewayName` and show the wallet-provided gateways if available.' } } },
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default meta
|
|
21
|
+
|
|
22
|
+
type Story = StoryObj<GatewayProviderProps>
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Convenience story that proves ALL client gateways are represented.
|
|
26
|
+
* Each row corresponds to one entry in the client's gateways.
|
|
27
|
+
*/
|
|
28
|
+
const AllClientGateways: Story = {
|
|
29
|
+
name: 'All Client Gateways',
|
|
30
|
+
render: () => {
|
|
31
|
+
const { client } = useClientFromWallet()
|
|
32
|
+
const foundClient = isDefinedNotNull(client)
|
|
33
|
+
return (
|
|
34
|
+
<Stack gap={3} p={2}>
|
|
35
|
+
<Alert severity={foundClient ? 'success' : 'error'}>
|
|
36
|
+
{foundClient ? 'Client found, displaying gateways:' : 'No client found. Please ensure your wallet is connected and has access to the current page.'}
|
|
37
|
+
</Alert>
|
|
38
|
+
{Object.keys(client?.gateways || {}).map(id => (
|
|
39
|
+
<GatewayStats key={id} id={id as GatewayName} />
|
|
40
|
+
))}
|
|
41
|
+
</Stack>
|
|
42
|
+
)
|
|
43
|
+
},
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { AllClientGateways }
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { AccountInstance } from '@xyo-network/sdk-js'
|
|
2
|
+
import type { GatewayName } from '@xyo-network/xl1-sdk'
|
|
3
|
+
import { DefaultNetworks } from '@xyo-network/xl1-sdk'
|
|
4
|
+
import type { PropsWithChildren } from 'react'
|
|
5
|
+
import React, {
|
|
6
|
+
startTransition,
|
|
7
|
+
useCallback, useEffect, useMemo, useState,
|
|
8
|
+
} from 'react'
|
|
9
|
+
|
|
10
|
+
import { buildGateway } from '../../lib/index.ts'
|
|
11
|
+
import type { ContextGatewayType } from '../../types/index.ts'
|
|
12
|
+
import { InPageGatewaysContext } from './context.ts'
|
|
13
|
+
import type { InPageGatewaysState } from './state.ts'
|
|
14
|
+
|
|
15
|
+
export interface InPageGatewaysProviderProps extends PropsWithChildren {
|
|
16
|
+
account?: AccountInstance
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const InPageGatewaysProvider: React.FC<InPageGatewaysProviderProps> = ({ account, children }) => {
|
|
20
|
+
const [gateways, setGateways] = useState<Partial<Record<GatewayName, ContextGatewayType>>>({})
|
|
21
|
+
const [errors, setErrors] = useState<Partial<Record<GatewayName, Error>>>({})
|
|
22
|
+
const [previousAccount, setPreviousAccount] = useState<AccountInstance | undefined>(account)
|
|
23
|
+
|
|
24
|
+
const clearAll = useCallback(() => {
|
|
25
|
+
setGateways({})
|
|
26
|
+
setErrors({})
|
|
27
|
+
}, [])
|
|
28
|
+
|
|
29
|
+
if (previousAccount !== account) {
|
|
30
|
+
clearAll()
|
|
31
|
+
setPreviousAccount(account)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
let cancelled = false
|
|
36
|
+
|
|
37
|
+
const buildAll = async () => {
|
|
38
|
+
const results = await Promise.allSettled(
|
|
39
|
+
DefaultNetworks.map(async (network) => {
|
|
40
|
+
const gateway = await buildGateway(network.id as GatewayName, account)
|
|
41
|
+
return { id: network.id as GatewayName, gateway }
|
|
42
|
+
}),
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
if (cancelled) return
|
|
46
|
+
|
|
47
|
+
const nextGateways: Partial<Record<GatewayName, ContextGatewayType>> = {}
|
|
48
|
+
const nextErrors: Partial<Record<GatewayName, Error>> = {}
|
|
49
|
+
|
|
50
|
+
for (const [index, result] of results.entries()) {
|
|
51
|
+
const networkId = DefaultNetworks[index].id as GatewayName
|
|
52
|
+
if (result.status === 'fulfilled') {
|
|
53
|
+
nextGateways[networkId] = result.value.gateway
|
|
54
|
+
} else {
|
|
55
|
+
nextErrors[networkId] = result.reason instanceof Error ? result.reason : new Error(String(result.reason))
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
startTransition(() => {
|
|
60
|
+
setGateways(nextGateways)
|
|
61
|
+
setErrors(nextErrors)
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
void buildAll()
|
|
66
|
+
return () => {
|
|
67
|
+
cancelled = true
|
|
68
|
+
}
|
|
69
|
+
}, [account])
|
|
70
|
+
|
|
71
|
+
const value = useMemo(() => {
|
|
72
|
+
const value: InPageGatewaysState = {
|
|
73
|
+
clearAll,
|
|
74
|
+
errors,
|
|
75
|
+
gateways,
|
|
76
|
+
provided: true,
|
|
77
|
+
}
|
|
78
|
+
return value
|
|
79
|
+
}, [clearAll, errors, gateways])
|
|
80
|
+
|
|
81
|
+
return <InPageGatewaysContext value={value}>{children}</InPageGatewaysContext>
|
|
82
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ContextExState } from '@xylabs/react-shared'
|
|
2
|
+
import type { GatewayName } from '@xyo-network/xl1-sdk'
|
|
3
|
+
|
|
4
|
+
import type { ContextGatewayType } from '../../types/index.ts'
|
|
5
|
+
|
|
6
|
+
export interface InPageGatewaysStateFields {
|
|
7
|
+
clearAll: () => void
|
|
8
|
+
errors: Partial<Record<GatewayName, Error>>
|
|
9
|
+
gateways: Partial<Record<GatewayName, ContextGatewayType>>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type InPageGatewaysState = ContextExState<InPageGatewaysStateFields>
|
package/src/contexts/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export * from './permissions/index.ts'
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
2
|
+
export * from './useClientFromWallet.ts'
|
|
3
|
+
export * from './useGatewayFromWallet.ts'
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useClientFromWallet } from '../useClientFromWallet.ts'
|
|
2
2
|
|
|
3
3
|
export const usePermissions = () => {
|
|
4
4
|
const {
|
|
5
5
|
client, isLoading, error, timedout,
|
|
6
|
-
} =
|
|
6
|
+
} = useClientFromWallet()
|
|
7
7
|
const permissions = client?.permissions
|
|
8
8
|
return {
|
|
9
9
|
permissions,
|
|
@@ -63,7 +63,7 @@ const subscribe = (listener: () => void, timeout?: number) => {
|
|
|
63
63
|
|
|
64
64
|
const getSnapshot = (): ClientState => currentState
|
|
65
65
|
|
|
66
|
-
export const
|
|
66
|
+
export const useClientFromWallet = (timeout?: number) => {
|
|
67
67
|
const subscribeWithTimeout = useCallback((listener: () => void) => subscribe(listener, timeout), [timeout])
|
|
68
68
|
const clientState = useSyncExternalStore(subscribeWithTimeout, getSnapshot)
|
|
69
69
|
|
|
@@ -91,3 +91,6 @@ export const useClient = (timeout?: number) => {
|
|
|
91
91
|
|
|
92
92
|
return clientState
|
|
93
93
|
}
|
|
94
|
+
|
|
95
|
+
/** @deprecated - use useClientFromWallet instead */
|
|
96
|
+
export const useClient = useClientFromWallet
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { isDefined, isNull } from '@xylabs/sdk-js'
|
|
2
2
|
import type { GatewayName } from '@xyo-network/xl1-sdk'
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import type { GatewayFromWallet } from '../../types/index.ts'
|
|
5
|
+
import { useClientFromWallet } from './useClientFromWallet.ts'
|
|
5
6
|
|
|
6
|
-
export const
|
|
7
|
+
export const useGatewayFromWallet = (gatewayName?: GatewayName, timeout?: number): GatewayFromWallet => {
|
|
7
8
|
const {
|
|
8
9
|
client, isLoading, error, timedout,
|
|
9
|
-
} =
|
|
10
|
+
} = useClientFromWallet(timeout)
|
|
10
11
|
|
|
11
12
|
const resolveGateway = () => {
|
|
12
13
|
// null client means we do not have to wait for timedout
|
|
@@ -24,3 +25,6 @@ export const useGateway = (gatewayName?: GatewayName, timeout?: number) => {
|
|
|
24
25
|
timedout,
|
|
25
26
|
}
|
|
26
27
|
}
|
|
28
|
+
|
|
29
|
+
/** @deprecated - useGatewayFromWallet */
|
|
30
|
+
export const useGateway = useGatewayFromWallet
|
package/src/hooks/index.ts
CHANGED
|
@@ -4,14 +4,14 @@ import type { GatewayName } from '@xyo-network/xl1-sdk'
|
|
|
4
4
|
import { MainNetwork } from '@xyo-network/xl1-sdk'
|
|
5
5
|
import { useCallback, useState } from 'react'
|
|
6
6
|
|
|
7
|
-
import { useAccountPermissions,
|
|
7
|
+
import { useAccountPermissions, useGatewayFromWallet } from './client/index.ts'
|
|
8
8
|
|
|
9
9
|
export const useConnectAccount = (gatewayName: GatewayName = MainNetwork.id, timeout?: number) => {
|
|
10
10
|
const [connectError, setConnectError] = useState<Error>()
|
|
11
11
|
|
|
12
12
|
const {
|
|
13
13
|
gateway, error, timedout,
|
|
14
|
-
} =
|
|
14
|
+
} = useGatewayFromWallet(gatewayName, timeout)
|
|
15
15
|
|
|
16
16
|
const [accountPermissions, accountPermissionsError] = useAccountPermissions()
|
|
17
17
|
|
|
@@ -3,6 +3,9 @@ import { usePromise } from '@xylabs/react-promise'
|
|
|
3
3
|
import { isDefinedNotNull } from '@xylabs/sdk-js'
|
|
4
4
|
import type { HydratedBlockWithHashMeta, XyoViewer } from '@xyo-network/xl1-sdk'
|
|
5
5
|
|
|
6
|
+
import { useViewerFromGateway } from './gateway/index.ts'
|
|
7
|
+
|
|
8
|
+
/* @deprecated - use useCurrentBlockFromGateway instead */
|
|
6
9
|
export const useCurrentBlock = (refresh = 1, viewer?: XyoViewer | null): [HydratedBlockWithHashMeta | undefined,
|
|
7
10
|
Error | undefined, UsePromiseState | undefined] => {
|
|
8
11
|
return usePromise(async () => {
|
|
@@ -12,3 +15,13 @@ export const useCurrentBlock = (refresh = 1, viewer?: XyoViewer | null): [Hydrat
|
|
|
12
15
|
}
|
|
13
16
|
}, [viewer, refresh])
|
|
14
17
|
}
|
|
18
|
+
|
|
19
|
+
export const useCurrentBlockFromGateway = (refresh = 1) => {
|
|
20
|
+
const viewer = useViewerFromGateway()
|
|
21
|
+
return usePromise(async () => {
|
|
22
|
+
if (isDefinedNotNull(viewer) && refresh > 0) {
|
|
23
|
+
const block = await viewer.currentBlock()
|
|
24
|
+
return block
|
|
25
|
+
}
|
|
26
|
+
}, [viewer, refresh])
|
|
27
|
+
}
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
AccountBalanceViewerRpcSchemas, buildJsonRpcProviderLocator, HttpRpcTransport, JsonRpcAccountBalanceViewer, SimpleTransactionViewer, XyoRunnerMoniker,
|
|
8
8
|
} from '@xyo-network/xl1-sdk'
|
|
9
9
|
|
|
10
|
+
/* @deprecated use useRunnerFromGateway */
|
|
10
11
|
export const useHttpRpcRunner = (url?: string) => {
|
|
11
12
|
return usePromise(async () => {
|
|
12
13
|
if (isUndefined(url)) {
|
|
@@ -2,13 +2,12 @@ import { usePromise } from '@xylabs/react-promise'
|
|
|
2
2
|
import {
|
|
3
3
|
delay, isDefined, isUndefined,
|
|
4
4
|
} from '@xylabs/sdk-js'
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
} from '@xyo-network/xl1-sdk'
|
|
5
|
+
import { basicRemoteViewerLocator } from '@xyo-network/chain-orchestration'
|
|
6
|
+
import type { JsonRpcXyoViewer } from '@xyo-network/xl1-sdk'
|
|
8
7
|
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
LocalNetwork,
|
|
9
|
+
SequenceNetwork,
|
|
10
|
+
XyoViewerMoniker,
|
|
12
11
|
} from '@xyo-network/xl1-sdk'
|
|
13
12
|
import { useEffect, useState } from 'react'
|
|
14
13
|
|
|
@@ -23,8 +22,8 @@ export const useCheckLocalRpc = () => {
|
|
|
23
22
|
}
|
|
24
23
|
|
|
25
24
|
export const useCheckSequenceRpc = () => {
|
|
26
|
-
const
|
|
27
|
-
return
|
|
25
|
+
const isSequenceRpc = useCheckRpc(sequenceRpcEndpoint)
|
|
26
|
+
return isSequenceRpc
|
|
28
27
|
}
|
|
29
28
|
|
|
30
29
|
export const useCheckRpc = (endpoint?: string) => {
|
|
@@ -34,15 +33,7 @@ export const useCheckRpc = (endpoint?: string) => {
|
|
|
34
33
|
const [viewer] = usePromise(async () => {
|
|
35
34
|
if (isUndefined(endpoint)) return
|
|
36
35
|
|
|
37
|
-
const
|
|
38
|
-
const locator = await buildJsonRpcProviderLocator({ transportFactory })
|
|
39
|
-
|
|
40
|
-
// Additional dependencies to get working viewer
|
|
41
|
-
locator.register(SimpleTransactionViewer.factory<SimpleTransactionViewer>(SimpleTransactionViewer.dependencies, {}))
|
|
42
|
-
locator.register(JsonRpcAccountBalanceViewer.factory<JsonRpcAccountBalanceViewer>(
|
|
43
|
-
JsonRpcAccountBalanceViewer.dependencies,
|
|
44
|
-
{ transport: await transportFactory(AccountBalanceViewerRpcSchemas) },
|
|
45
|
-
))
|
|
36
|
+
const locator = await basicRemoteViewerLocator(endpoint, { rpc: { protocol: 'http', url: endpoint } })
|
|
46
37
|
|
|
47
38
|
const viewer = await locator.getInstance<JsonRpcXyoViewer>(XyoViewerMoniker)
|
|
48
39
|
return viewer
|
|
@@ -55,6 +55,7 @@ const buildLocator = async (transportFactory: TransportFactory, dataLakeViewerPa
|
|
|
55
55
|
return locator
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
/** @deprecated - use useViewerFromGateway */
|
|
58
59
|
export const useHttpRpcViewer = (urlOrNetwork?: string | NetworkBootstrap): XyoViewer | undefined => {
|
|
59
60
|
const [resolvedViewer] = usePromise(async () => {
|
|
60
61
|
if (isUndefined(urlOrNetwork)) {
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
} from '@xylabs/sdk-js'
|
|
5
5
|
import type { GatewayName } from '@xyo-network/xl1-sdk'
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { useGatewayFromWallet } from '../client/index.ts'
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Creates a JsonRpcXyoViewer instance based on the provided networkId.
|
|
@@ -18,7 +18,7 @@ export const useViewerFromWallet = (
|
|
|
18
18
|
) => {
|
|
19
19
|
const {
|
|
20
20
|
gateway, error, isLoading, timedout,
|
|
21
|
-
} =
|
|
21
|
+
} = useGatewayFromWallet(networkId, timeout)
|
|
22
22
|
|
|
23
23
|
const result = usePromise(async () => {
|
|
24
24
|
await Promise.resolve() // ensure async
|
package/src/index.ts
CHANGED