@xyo-network/react-chain-provider 1.20.14 → 1.20.16
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/README.md +9 -3302
- package/dist/browser/hooks/account/BalanceHistoryItemFormatted.d.ts +1 -0
- package/dist/browser/hooks/account/BalanceHistoryItemFormatted.d.ts.map +1 -1
- package/dist/browser/hooks/account/helpers/formatAccountBalanceHistory.d.ts.map +1 -1
- package/dist/browser/hooks/gateway/useNetwork.d.ts +1 -1
- package/dist/browser/hooks/gateway/useNetwork.d.ts.map +1 -1
- package/dist/browser/hooks/gateway/useRunner.d.ts +1 -1
- package/dist/browser/hooks/gateway/useRunner.d.ts.map +1 -1
- package/dist/browser/hooks/gateway/useViewer.d.ts +1 -1
- package/dist/browser/hooks/gateway/useViewer.d.ts.map +1 -1
- package/dist/browser/index.mjs +3 -0
- package/dist/browser/index.mjs.map +1 -1
- package/package.json +155 -45
- package/src/components/connected/ConnectAccountsStack.stories.tsx +0 -16
- package/src/components/connected/ConnectAccountsStack.tsx +0 -92
- package/src/components/connected/account/Connected.tsx +0 -19
- package/src/components/connected/account/index.ts +0 -1
- package/src/components/connected/index.ts +0 -2
- package/src/components/index.ts +0 -1
- package/src/contexts/current-block/Provider.stories.tsx +0 -38
- package/src/contexts/current-block/Provider.tsx +0 -36
- package/src/contexts/current-block/context.ts +0 -5
- package/src/contexts/current-block/index.ts +0 -5
- package/src/contexts/current-block/state.ts +0 -16
- package/src/contexts/current-block/use.ts +0 -5
- package/src/contexts/current-block/usePollCurrentBlock.ts +0 -61
- package/src/contexts/gateway/Provider.tsx +0 -81
- package/src/contexts/gateway/context.ts +0 -5
- package/src/contexts/gateway/index.ts +0 -4
- package/src/contexts/gateway/state.ts +0 -15
- package/src/contexts/gateway/story/GatewayStats.tsx +0 -33
- package/src/contexts/gateway/story/GatewayStatus.tsx +0 -53
- package/src/contexts/gateway/story/Provider.stories.tsx +0 -38
- package/src/contexts/gateway/story/ProviderWithWallet.stories.tsx +0 -43
- package/src/contexts/gateway/use.ts +0 -5
- package/src/contexts/in-page-gateways/Provider.tsx +0 -86
- package/src/contexts/in-page-gateways/context.ts +0 -5
- package/src/contexts/in-page-gateways/index.ts +0 -4
- package/src/contexts/in-page-gateways/state.ts +0 -12
- package/src/contexts/in-page-gateways/use.ts +0 -5
- package/src/contexts/index.ts +0 -3
- package/src/global.d.ts +0 -9
- package/src/hooks/account/BalanceHistoryItemFormatted.ts +0 -17
- package/src/hooks/account/helpers/formatAccountBalanceHistory.ts +0 -44
- package/src/hooks/account/helpers/index.ts +0 -1
- package/src/hooks/account/index.ts +0 -3
- package/src/hooks/account/useAccountBalanceHistory.ts +0 -51
- package/src/hooks/client/helpers/findCaveat.ts +0 -2
- package/src/hooks/client/helpers/index.ts +0 -1
- package/src/hooks/client/index.ts +0 -3
- package/src/hooks/client/permissions/index.ts +0 -2
- package/src/hooks/client/permissions/usePermissions.ts +0 -2
- package/src/hooks/client/permissions/usePermissionsAccounts.ts +0 -2
- package/src/hooks/client/useClientFromWallet.ts +0 -2
- package/src/hooks/client/useGatewayFromWallet.ts +0 -2
- package/src/hooks/gateway/index.ts +0 -3
- package/src/hooks/gateway/useNetwork.ts +0 -6
- package/src/hooks/gateway/useRunner.ts +0 -6
- package/src/hooks/gateway/useViewer.ts +0 -6
- package/src/hooks/helpers/getXyoClient.ts +0 -2
- package/src/hooks/helpers/index.ts +0 -2
- package/src/hooks/helpers/transaction/Confirmation.ts +0 -125
- package/src/hooks/helpers/transaction/index.ts +0 -1
- package/src/hooks/index.ts +0 -11
- package/src/hooks/useAddressBalance.ts +0 -86
- package/src/hooks/useConfirmTransactionBase.ts +0 -64
- package/src/hooks/useConnectAccount.ts +0 -36
- package/src/hooks/useCurrentBlock.ts +0 -27
- package/src/hooks/useNetwork.ts +0 -13
- package/src/hooks/useSigner.ts +0 -9
- package/src/hooks/viewer/UseStepRewardWeightTest.stories.tsx +0 -87
- package/src/hooks/viewer/ViewerWithDataLake.ts +0 -51
- package/src/hooks/viewer/index.ts +0 -2
- package/src/hooks/viewer/useCheckRpc.ts +0 -61
- package/src/index.ts +0 -5
- package/src/lib/balanceForRange.ts +0 -16
- package/src/lib/buildGateway.ts +0 -29
- package/src/lib/findMinimumBlock.ts +0 -9
- package/src/lib/index.ts +0 -3
- package/src/story/GatewayDecorator.tsx +0 -36
- package/src/story/index.ts +0 -1
- package/src/types/ContextGatewayType.ts +0 -4
- package/src/types/GatewayFromWallet.ts +0 -2
- package/src/types/index.ts +0 -2
|
@@ -1,15 +0,0 @@
|
|
|
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>
|
|
@@ -1,33 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
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 }
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { Alert, Stack } from '@mui/material'
|
|
2
|
-
import type { Meta, StoryFn } from '@storybook/react-vite'
|
|
3
|
-
import { isDefinedNotNull, isNull } 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
|
-
const TestClientGateways: React.FC = () => {
|
|
23
|
-
const { client } = useClientFromWallet()
|
|
24
|
-
const foundClient = isDefinedNotNull(client)
|
|
25
|
-
const notFoundClient = isNull(client)
|
|
26
|
-
return (
|
|
27
|
-
<Stack gap={3} p={2}>
|
|
28
|
-
<Alert severity={foundClient ? 'success' : 'error'}>
|
|
29
|
-
{foundClient ? 'Client found, displaying gateways:' : ''}
|
|
30
|
-
{notFoundClient ? 'No client found. Please ensure your wallet is connected and has access to the current page.' : ''}
|
|
31
|
-
</Alert>
|
|
32
|
-
{Object.keys(client?.gateways || {}).map(id => (
|
|
33
|
-
<GatewayStats key={id} id={id as GatewayName} />
|
|
34
|
-
))}
|
|
35
|
-
</Stack>
|
|
36
|
-
)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const Template: StoryFn = args => <TestClientGateways {...args} />
|
|
40
|
-
|
|
41
|
-
const AllClientGateways = Template.bind({})
|
|
42
|
-
|
|
43
|
-
export { AllClientGateways }
|
|
@@ -1,86 +0,0 @@
|
|
|
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
|
-
// This can come back once we homogenize the type for XyoGateway.
|
|
25
|
-
// Right now, SimpleXyoClient takes XyoGatewayRunners but ideally it would take XyoGateway.
|
|
26
|
-
// const clientInPage = useMemo(() => new SimpleXyoClient(gateways, {}), [gateways])
|
|
27
|
-
|
|
28
|
-
const clearAll = useCallback(() => {
|
|
29
|
-
setGateways({})
|
|
30
|
-
setErrors({})
|
|
31
|
-
}, [])
|
|
32
|
-
|
|
33
|
-
if (previousAccount !== account) {
|
|
34
|
-
clearAll()
|
|
35
|
-
setPreviousAccount(account)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
useEffect(() => {
|
|
39
|
-
let cancelled = false
|
|
40
|
-
|
|
41
|
-
const buildAll = async () => {
|
|
42
|
-
const results = await Promise.allSettled(
|
|
43
|
-
DefaultNetworks.map(async (network) => {
|
|
44
|
-
const gateway = await buildGateway(network.id as GatewayName, account)
|
|
45
|
-
return { id: network.id as GatewayName, gateway }
|
|
46
|
-
}),
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
if (cancelled) return
|
|
50
|
-
|
|
51
|
-
const nextGateways: Partial<Record<GatewayName, ContextGatewayType>> = {}
|
|
52
|
-
const nextErrors: Partial<Record<GatewayName, Error>> = {}
|
|
53
|
-
|
|
54
|
-
for (const [index, result] of results.entries()) {
|
|
55
|
-
const networkId = DefaultNetworks[index].id as GatewayName
|
|
56
|
-
if (result.status === 'fulfilled') {
|
|
57
|
-
nextGateways[networkId] = result.value.gateway
|
|
58
|
-
} else {
|
|
59
|
-
nextErrors[networkId] = result.reason instanceof Error ? result.reason : new Error(String(result.reason))
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
startTransition(() => {
|
|
64
|
-
setGateways(nextGateways)
|
|
65
|
-
setErrors(nextErrors)
|
|
66
|
-
})
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
void buildAll()
|
|
70
|
-
return () => {
|
|
71
|
-
cancelled = true
|
|
72
|
-
}
|
|
73
|
-
}, [account])
|
|
74
|
-
|
|
75
|
-
const value = useMemo(() => {
|
|
76
|
-
const value: InPageGatewaysState = {
|
|
77
|
-
clearAll,
|
|
78
|
-
errors,
|
|
79
|
-
gateways,
|
|
80
|
-
provided: true,
|
|
81
|
-
}
|
|
82
|
-
return value
|
|
83
|
-
}, [clearAll, errors, gateways])
|
|
84
|
-
|
|
85
|
-
return <InPageGatewaysContext value={value}>{children}</InPageGatewaysContext>
|
|
86
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
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
DELETED
package/src/global.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { Address, Hash } from '@xylabs/sdk-js'
|
|
2
|
-
import type { AccountBalanceHistoryItem } from '@xyo-network/xl1-sdk'
|
|
3
|
-
|
|
4
|
-
export type BalanceTransactionType = 'send' | 'receive'
|
|
5
|
-
|
|
6
|
-
export interface AccountBalanceHistoryItemFormatted {
|
|
7
|
-
amount: bigint
|
|
8
|
-
blockNumber: number
|
|
9
|
-
debug: AccountBalanceHistoryItem
|
|
10
|
-
from: Address
|
|
11
|
-
key: string
|
|
12
|
-
timestamp: number
|
|
13
|
-
to: Address
|
|
14
|
-
transferHash: Hash
|
|
15
|
-
txHash: Hash | undefined
|
|
16
|
-
type: BalanceTransactionType
|
|
17
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import type { Address, Hex } from '@xylabs/sdk-js'
|
|
2
|
-
import { hexToBigInt } from '@xylabs/sdk-js'
|
|
3
|
-
import { PayloadBuilder } from '@xyo-network/sdk-js'
|
|
4
|
-
import type { AccountBalanceHistoryItem } from '@xyo-network/xl1-sdk'
|
|
5
|
-
|
|
6
|
-
import type { AccountBalanceHistoryItemFormatted } from '../BalanceHistoryItemFormatted.ts'
|
|
7
|
-
|
|
8
|
-
export const formatAccountBalanceHistory = async (address: Address, history: AccountBalanceHistoryItem[]): Promise<AccountBalanceHistoryItemFormatted[]> => {
|
|
9
|
-
let results: AccountBalanceHistoryItemFormatted[] = []
|
|
10
|
-
for (const item of history) {
|
|
11
|
-
const [blockBw, txBw, transfer] = item
|
|
12
|
-
const transferHash = await PayloadBuilder.hash(transfer)
|
|
13
|
-
if (address === transfer.from) {
|
|
14
|
-
for (const [to, amount] of Object.entries(transfer.transfers)) {
|
|
15
|
-
results.push({
|
|
16
|
-
amount: hexToBigInt(amount as Hex),
|
|
17
|
-
blockNumber: blockBw.block,
|
|
18
|
-
from: transfer.from,
|
|
19
|
-
timestamp: blockBw.$epoch,
|
|
20
|
-
key: globalThis.crypto.randomUUID(),
|
|
21
|
-
to: to as Address,
|
|
22
|
-
transferHash,
|
|
23
|
-
txHash: txBw?._hash,
|
|
24
|
-
debug: item,
|
|
25
|
-
type: 'send',
|
|
26
|
-
})
|
|
27
|
-
}
|
|
28
|
-
} else {
|
|
29
|
-
results.push({
|
|
30
|
-
amount: hexToBigInt(transfer.transfers[address] as Hex),
|
|
31
|
-
blockNumber: blockBw.block,
|
|
32
|
-
from: transfer.from,
|
|
33
|
-
key: globalThis.crypto.randomUUID(),
|
|
34
|
-
timestamp: blockBw.$epoch,
|
|
35
|
-
to: address,
|
|
36
|
-
transferHash,
|
|
37
|
-
txHash: txBw?._hash,
|
|
38
|
-
debug: item,
|
|
39
|
-
type: 'receive',
|
|
40
|
-
})
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return results
|
|
44
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './formatAccountBalanceHistory.ts'
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import {
|
|
3
|
-
type Address,
|
|
4
|
-
isUndefined,
|
|
5
|
-
} from '@xylabs/sdk-js'
|
|
6
|
-
import type {
|
|
7
|
-
AccountBalanceHistoryItem, XL1BlockRange, XyoViewer,
|
|
8
|
-
} from '@xyo-network/xl1-sdk'
|
|
9
|
-
|
|
10
|
-
import { balanceForRange, findMinimumBlock } from '../../lib/index.ts'
|
|
11
|
-
import { formatAccountBalanceHistory } from './helpers/index.ts'
|
|
12
|
-
|
|
13
|
-
export const useAccountBalanceHistory = (address?: Address, viewer?: XyoViewer, maxPage = 10, blockRange?: XL1BlockRange, refresh?: number) => {
|
|
14
|
-
return usePromise(async () => {
|
|
15
|
-
if (isUndefined(viewer) || isUndefined(address)) return
|
|
16
|
-
|
|
17
|
-
// Pagination Values
|
|
18
|
-
let page = 1
|
|
19
|
-
let lastBlock: number
|
|
20
|
-
const pagedHistory: AccountBalanceHistoryItem[] = []
|
|
21
|
-
const resolvedStartBlock = blockRange ? blockRange[0] : 0
|
|
22
|
-
|
|
23
|
-
// Fetch first page to get starting lastBlock
|
|
24
|
-
// If blockRange is provided, use that for the first fetch, otherwise the api will use [0, currentBlock]
|
|
25
|
-
const history = await viewer.account.balance.accountBalanceHistory(address, blockRange ? { range: blockRange } : {})
|
|
26
|
-
if (history.length > 0) {
|
|
27
|
-
pagedHistory.push(...history)
|
|
28
|
-
// Set lastBlock to one less than the lowest block in the first page
|
|
29
|
-
lastBlock = findMinimumBlock(history)
|
|
30
|
-
while (page < maxPage) {
|
|
31
|
-
// Fetch next page
|
|
32
|
-
const nextHistory = await viewer.account.balance.accountBalanceHistory(address, { range: [resolvedStartBlock, lastBlock] as XL1BlockRange })
|
|
33
|
-
// Break if no more history
|
|
34
|
-
if (nextHistory.length === 0) break
|
|
35
|
-
// Update lastBlock and append to pagedHistory
|
|
36
|
-
lastBlock = findMinimumBlock(nextHistory)
|
|
37
|
-
pagedHistory.push(...nextHistory)
|
|
38
|
-
// Increment page
|
|
39
|
-
// Even if we are going to go over and break the loop, we still want to increment to show the answer was truncated
|
|
40
|
-
page++
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
const formattedHistory = await formatAccountBalanceHistory(address, pagedHistory)
|
|
44
|
-
|
|
45
|
-
return {
|
|
46
|
-
history: formattedHistory,
|
|
47
|
-
balance: balanceForRange(address, pagedHistory),
|
|
48
|
-
truncated: page >= maxPage,
|
|
49
|
-
}
|
|
50
|
-
}, [address, viewer, refresh, blockRange, maxPage])
|
|
51
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './findCaveat.ts'
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import type { Hash, Promisable } from '@xylabs/sdk-js'
|
|
2
|
-
import {
|
|
3
|
-
assertEx, delay, forget, isDefined, isNull,
|
|
4
|
-
} from '@xylabs/sdk-js'
|
|
5
|
-
import type { SignedHydratedTransaction, XyoViewer } from '@xyo-network/xl1-sdk'
|
|
6
|
-
|
|
7
|
-
export type ConfirmationStatusUpdate = {
|
|
8
|
-
blockNumber: number
|
|
9
|
-
confirmed: boolean
|
|
10
|
-
error?: Error
|
|
11
|
-
progress?: number
|
|
12
|
-
remainingBlocks?: number
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export type TransactionConfirmationParams = {
|
|
16
|
-
onStatusUpdate?: (status: ConfirmationStatusUpdate) => void
|
|
17
|
-
transaction: SignedHydratedTransaction
|
|
18
|
-
txHash: Hash
|
|
19
|
-
viewer: XyoViewer
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export class TransactionConfirmationStatus {
|
|
23
|
-
private readonly _params: TransactionConfirmationParams
|
|
24
|
-
private active = false
|
|
25
|
-
|
|
26
|
-
constructor(params: TransactionConfirmationParams) {
|
|
27
|
-
this._params = params
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
get exp(): number {
|
|
31
|
-
return assertEx(this.transaction[0].exp, () => 'exp is not found, transaction is not set')
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
get nbf(): number {
|
|
35
|
-
return assertEx(this.transaction[0].nbf, () => 'nbf is not found, transaction is not set')
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get onStatusUpdate(): TransactionConfirmationParams['onStatusUpdate'] {
|
|
39
|
-
return this._params.onStatusUpdate
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
get params(): TransactionConfirmationParams {
|
|
43
|
-
return this._params
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
get transaction(): SignedHydratedTransaction {
|
|
47
|
-
return this.params.transaction
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
get txHash(): Hash {
|
|
51
|
-
return this.params.txHash
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
get viewer(): XyoViewer {
|
|
55
|
-
return this.params.viewer
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
static create(params: TransactionConfirmationParams): Promisable<TransactionConfirmationStatus> {
|
|
59
|
-
return new TransactionConfirmationStatus(params)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
async start(): Promise<void> {
|
|
63
|
-
this.active = true
|
|
64
|
-
try {
|
|
65
|
-
const currentBlockNumber = await this.viewer.currentBlockNumber()
|
|
66
|
-
while (isDefined(currentBlockNumber) && this.active) {
|
|
67
|
-
await this.checkBlock(currentBlockNumber)
|
|
68
|
-
// check block every second to see if the transaction is confirmed
|
|
69
|
-
await delay(1000)
|
|
70
|
-
}
|
|
71
|
-
} catch (error) {
|
|
72
|
-
await this.stop()
|
|
73
|
-
forget(this.onStatusUpdate?.({
|
|
74
|
-
error: error instanceof Error ? error : new Error('Unknown error occurred'),
|
|
75
|
-
confirmed: false,
|
|
76
|
-
blockNumber: 0,
|
|
77
|
-
progress: 0,
|
|
78
|
-
remainingBlocks: 0,
|
|
79
|
-
}))
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
stop(): Promisable<void> {
|
|
84
|
-
this.active = false
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
private async checkBlock(blockNumber: number): Promise<void> {
|
|
88
|
-
if (blockNumber < this.nbf) {
|
|
89
|
-
await this.stop()
|
|
90
|
-
forget(this.onStatusUpdate?.({
|
|
91
|
-
error: new Error(`Block number: ${blockNumber} is before ${this.nbf}`),
|
|
92
|
-
confirmed: false,
|
|
93
|
-
blockNumber,
|
|
94
|
-
progress: 0,
|
|
95
|
-
remainingBlocks: this.exp - blockNumber,
|
|
96
|
-
}))
|
|
97
|
-
}
|
|
98
|
-
if (blockNumber > this.exp) {
|
|
99
|
-
forget(this.onStatusUpdate?.({
|
|
100
|
-
confirmed: false, blockNumber, progress: 100, remainingBlocks: 0,
|
|
101
|
-
}))
|
|
102
|
-
return
|
|
103
|
-
}
|
|
104
|
-
if (blockNumber < this.exp) {
|
|
105
|
-
await this.checkConfirmation(blockNumber)
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
private async checkConfirmation(blockNumber: number): Promise<void> {
|
|
110
|
-
const remainingBlocks = this.exp - blockNumber
|
|
111
|
-
const progress = Math.round(((blockNumber - this.nbf) / (this.exp - this.nbf)) * 100)
|
|
112
|
-
|
|
113
|
-
const tx = await this.viewer.transaction.byHash?.(this.txHash)
|
|
114
|
-
if (isNull(tx)) {
|
|
115
|
-
this.onStatusUpdate?.({
|
|
116
|
-
confirmed: false, blockNumber, progress, remainingBlocks,
|
|
117
|
-
})
|
|
118
|
-
} else {
|
|
119
|
-
this.onStatusUpdate?.({
|
|
120
|
-
confirmed: true, blockNumber, progress, remainingBlocks,
|
|
121
|
-
})
|
|
122
|
-
await this.stop()
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './Confirmation.ts'
|
package/src/hooks/index.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export * from './account/index.ts'
|
|
2
|
-
export * from './client/index.ts'
|
|
3
|
-
export * from './gateway/index.ts'
|
|
4
|
-
export * from './helpers/index.ts'
|
|
5
|
-
export * from './useAddressBalance.ts'
|
|
6
|
-
export * from './useConfirmTransactionBase.ts'
|
|
7
|
-
export * from './useConnectAccount.ts'
|
|
8
|
-
export * from './useCurrentBlock.ts'
|
|
9
|
-
export * from './useNetwork.ts'
|
|
10
|
-
export * from './useSigner.ts'
|
|
11
|
-
export * from './viewer/index.ts'
|