@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,86 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import type { Address } from '@xylabs/sdk-js'
|
|
3
|
-
import { isUndefined, isUndefinedOrNull } from '@xylabs/sdk-js'
|
|
4
|
-
import type { AttoXL1, XyoViewer } from '@xyo-network/xl1-sdk'
|
|
5
|
-
import { ShiftedBigInt } from '@xyo-network/xl1-sdk'
|
|
6
|
-
import {
|
|
7
|
-
useMemo, useRef, useState,
|
|
8
|
-
} from 'react'
|
|
9
|
-
|
|
10
|
-
export const useAddressBalance = (
|
|
11
|
-
// address to get balance for
|
|
12
|
-
address?: Address,
|
|
13
|
-
// viewer to use for fetching balance
|
|
14
|
-
viewer?: XyoViewer,
|
|
15
|
-
// refresh balance trigger
|
|
16
|
-
refresh?: number,
|
|
17
|
-
// reset balance trigger - clears state immediately
|
|
18
|
-
reset?: number,
|
|
19
|
-
) => {
|
|
20
|
-
const [balancesResult, setBalancesResult] = useState<AttoXL1 | null>()
|
|
21
|
-
const [loading, setLoading] = useState(false)
|
|
22
|
-
const [balancesError, setBalancesError] = useState<Error>()
|
|
23
|
-
|
|
24
|
-
const [previousReset, setPreviousReset] = useState<number | undefined>(reset)
|
|
25
|
-
if (reset !== previousReset) {
|
|
26
|
-
setPreviousReset(reset)
|
|
27
|
-
setBalancesResult(undefined)
|
|
28
|
-
setBalancesError(undefined)
|
|
29
|
-
setLoading(false)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const balancePromiseRef = useRef<Promise<AttoXL1> | null>(null)
|
|
33
|
-
usePromise(async () => {
|
|
34
|
-
if (isUndefined(viewer) || isUndefined(address)) return
|
|
35
|
-
|
|
36
|
-
setLoading(true)
|
|
37
|
-
setBalancesError(undefined)
|
|
38
|
-
|
|
39
|
-
// Create and store the current promise
|
|
40
|
-
const currentPromise = viewer.account.balance.accountBalance(address) as Promise<AttoXL1>
|
|
41
|
-
balancePromiseRef.current = currentPromise
|
|
42
|
-
|
|
43
|
-
try {
|
|
44
|
-
const result = await currentPromise
|
|
45
|
-
// Only update state if this is still the latest promise
|
|
46
|
-
if (balancePromiseRef.current === currentPromise) {
|
|
47
|
-
setBalancesResult(result as AttoXL1)
|
|
48
|
-
setLoading(false)
|
|
49
|
-
}
|
|
50
|
-
} catch (err) {
|
|
51
|
-
// Only update state if this is still the latest promise
|
|
52
|
-
if (balancePromiseRef.current === currentPromise) {
|
|
53
|
-
console.error('Error in queued call:', err)
|
|
54
|
-
setBalancesResult(undefined)
|
|
55
|
-
setBalancesError(err as Error)
|
|
56
|
-
setLoading(false)
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}, [address, viewer, refresh])
|
|
60
|
-
|
|
61
|
-
const shiftedBigInt = useMemo(() => {
|
|
62
|
-
if (typeof balancesResult !== 'bigint') return
|
|
63
|
-
return new ShiftedBigInt(balancesResult, {
|
|
64
|
-
places: 18, maxDecimal: 18, maxCharacters: 9, minDecimals: 1, locale: navigator.language,
|
|
65
|
-
})
|
|
66
|
-
}, [balancesResult])
|
|
67
|
-
|
|
68
|
-
const balanceIntlFriendly = useMemo(() => {
|
|
69
|
-
return shiftedBigInt?.toFullString()
|
|
70
|
-
}, [shiftedBigInt])
|
|
71
|
-
|
|
72
|
-
const shortBalanceIntlFriendly = useMemo(() => {
|
|
73
|
-
return isUndefinedOrNull(balancesResult)
|
|
74
|
-
? undefined
|
|
75
|
-
: (balancesResult < 1_000_000_000_000n && balancesResult > 0n) ? '<0.00001' : shiftedBigInt?.toShortString()
|
|
76
|
-
}, [balancesResult, shiftedBigInt])
|
|
77
|
-
|
|
78
|
-
return {
|
|
79
|
-
address,
|
|
80
|
-
balanceForAddress: balancesResult,
|
|
81
|
-
balanceIntlFriendly,
|
|
82
|
-
shortBalanceIntlFriendly,
|
|
83
|
-
error: balancesError,
|
|
84
|
-
loading,
|
|
85
|
-
}
|
|
86
|
-
}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import type { Hash } from '@xylabs/sdk-js'
|
|
3
|
-
import {
|
|
4
|
-
forget, isDefined, isHash,
|
|
5
|
-
} from '@xylabs/sdk-js'
|
|
6
|
-
import { PayloadBuilder } from '@xyo-network/sdk-js'
|
|
7
|
-
import type {
|
|
8
|
-
SignedHydratedTransaction, XyoRunner, XyoViewer,
|
|
9
|
-
} from '@xyo-network/xl1-sdk'
|
|
10
|
-
import { useEffect, useState } from 'react'
|
|
11
|
-
|
|
12
|
-
import type { ConfirmationStatusUpdate, TransactionConfirmationParams } from './helpers/index.ts'
|
|
13
|
-
import { TransactionConfirmationStatus } from './helpers/index.ts'
|
|
14
|
-
|
|
15
|
-
export const useConfirmTransactionBase = (
|
|
16
|
-
// The transaction to confirm
|
|
17
|
-
transaction?: SignedHydratedTransaction,
|
|
18
|
-
// Callback for when the transaction is broadcast to the network
|
|
19
|
-
onBroadcast?: (hash: Hash) => void,
|
|
20
|
-
// Optional callback for status updates during confirmation
|
|
21
|
-
onStatusUpdate?: TransactionConfirmationParams['onStatusUpdate'],
|
|
22
|
-
// Runner to use for broadcasting transactions
|
|
23
|
-
runner?: XyoRunner,
|
|
24
|
-
// Viewer to use for checking transaction status
|
|
25
|
-
viewer?: XyoViewer,
|
|
26
|
-
) => {
|
|
27
|
-
const [status, setStatus] = useState<ConfirmationStatusUpdate>()
|
|
28
|
-
const onStatusUpdateLocal = (newStatus: ConfirmationStatusUpdate) => {
|
|
29
|
-
setStatus(newStatus)
|
|
30
|
-
onStatusUpdate?.(newStatus)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const [transactionConfirmationStatus, transactionConfirmationError] = usePromise(async () => {
|
|
34
|
-
const hasRunner = isDefined(runner)
|
|
35
|
-
if (transaction && viewer) {
|
|
36
|
-
const hash = hasRunner ? (await runner.broadcastTransaction(transaction)) : await PayloadBuilder.hash(transaction[0])
|
|
37
|
-
if (isHash(hash)) {
|
|
38
|
-
if (hasRunner && isDefined(onBroadcast)) onBroadcast(hash)
|
|
39
|
-
const params: TransactionConfirmationParams = {
|
|
40
|
-
onStatusUpdate: onStatusUpdateLocal,
|
|
41
|
-
transaction,
|
|
42
|
-
txHash: hash,
|
|
43
|
-
viewer,
|
|
44
|
-
}
|
|
45
|
-
const instance = await TransactionConfirmationStatus.create(params)
|
|
46
|
-
await instance.start()
|
|
47
|
-
return instance
|
|
48
|
-
} else {
|
|
49
|
-
throw new Error('unexpected return type from broadcast transaction')
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}, [transaction, runner, viewer])
|
|
53
|
-
|
|
54
|
-
useEffect(() => {
|
|
55
|
-
return () => {
|
|
56
|
-
if (transactionConfirmationStatus) {
|
|
57
|
-
// Stop the transaction confirmation status instance when the component unmounts
|
|
58
|
-
forget(transactionConfirmationStatus.stop())
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}, [transactionConfirmationStatus])
|
|
62
|
-
|
|
63
|
-
return { status, transactionConfirmationError }
|
|
64
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { type Address, asAddress } from '@xylabs/sdk-js'
|
|
2
|
-
import { assertEx } from '@xylabs/sdk-js'
|
|
3
|
-
import type { GatewayName } from '@xyo-network/xl1-sdk'
|
|
4
|
-
import { MainNetwork } from '@xyo-network/xl1-sdk'
|
|
5
|
-
import { useCallback, useState } from 'react'
|
|
6
|
-
|
|
7
|
-
import { useAccountPermissions, useGatewayFromWallet } from './client/index.ts'
|
|
8
|
-
|
|
9
|
-
export const useConnectAccount = (gatewayName: GatewayName = MainNetwork.id, timeout?: number) => {
|
|
10
|
-
const [connectError, setConnectError] = useState<Error>()
|
|
11
|
-
|
|
12
|
-
const {
|
|
13
|
-
gateway, error, timedout,
|
|
14
|
-
} = useGatewayFromWallet(gatewayName, timeout)
|
|
15
|
-
|
|
16
|
-
const [accountPermissions, accountPermissionsError] = useAccountPermissions()
|
|
17
|
-
|
|
18
|
-
const [address, setAddress] = useState<Address>()
|
|
19
|
-
|
|
20
|
-
const connectSigner = useCallback(async () => {
|
|
21
|
-
try {
|
|
22
|
-
setConnectError(undefined)
|
|
23
|
-
const assertedGateway = assertEx(gateway, () => `Gateway ${gatewayName} is not available`)
|
|
24
|
-
const signer = assertedGateway.signer
|
|
25
|
-
const address = await signer.address()
|
|
26
|
-
setAddress(address)
|
|
27
|
-
return address
|
|
28
|
-
} catch (e) {
|
|
29
|
-
setConnectError(e as Error)
|
|
30
|
-
}
|
|
31
|
-
}, [gateway, gatewayName])
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
address: asAddress(accountPermissions?.[0] ?? address), connectSigner, error: error ?? accountPermissionsError ?? connectError, gateway, timedout,
|
|
35
|
-
}
|
|
36
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { UsePromiseState } from '@xylabs/react-promise'
|
|
2
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
3
|
-
import { isDefinedNotNull } from '@xylabs/sdk-js'
|
|
4
|
-
import type { HydratedBlockWithHashMeta, XyoViewer } from '@xyo-network/xl1-sdk'
|
|
5
|
-
|
|
6
|
-
import { useViewerFromGateway } from './gateway/index.ts'
|
|
7
|
-
|
|
8
|
-
/* @deprecated - use useCurrentBlockFromGateway instead */
|
|
9
|
-
export const useCurrentBlock = (refresh = 1, viewer?: XyoViewer | null): [HydratedBlockWithHashMeta | undefined,
|
|
10
|
-
Error | undefined, UsePromiseState | undefined] => {
|
|
11
|
-
return usePromise(async () => {
|
|
12
|
-
if (isDefinedNotNull(viewer) && refresh > 0) {
|
|
13
|
-
const block = await viewer.currentBlock()
|
|
14
|
-
return block
|
|
15
|
-
}
|
|
16
|
-
}, [viewer, refresh])
|
|
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
|
-
}
|
package/src/hooks/useNetwork.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { isUndefined } from '@xylabs/sdk-js'
|
|
2
|
-
import type { DefaultNetworkIds } from '@xyo-network/xl1-sdk'
|
|
3
|
-
import { SimpleXyoNetwork } from '@xyo-network/xl1-sdk'
|
|
4
|
-
import { useMemo } from 'react'
|
|
5
|
-
|
|
6
|
-
export const useNetwork = (id?: DefaultNetworkIds) => {
|
|
7
|
-
const network = useMemo(() => {
|
|
8
|
-
if (isUndefined(id)) return
|
|
9
|
-
return new SimpleXyoNetwork(id)
|
|
10
|
-
}, [id])
|
|
11
|
-
|
|
12
|
-
return network
|
|
13
|
-
}
|
package/src/hooks/useSigner.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import type { AccountInstance } from '@xyo-network/sdk-js'
|
|
3
|
-
import {
|
|
4
|
-
SimpleXyoSigner, type XyoConnection, type XyoSigner,
|
|
5
|
-
} from '@xyo-network/xl1-sdk'
|
|
6
|
-
|
|
7
|
-
export const useSigner = (_provider?: XyoConnection, account?: AccountInstance) => {
|
|
8
|
-
return usePromise<XyoSigner | undefined>(async () => account ? await SimpleXyoSigner.create({ account }) : undefined, [account])
|
|
9
|
-
}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Alert,
|
|
3
|
-
Stack,
|
|
4
|
-
} from '@mui/material'
|
|
5
|
-
import type { Meta, StoryFn } from '@storybook/react-vite'
|
|
6
|
-
import { ButtonEx } from '@xylabs/react-button'
|
|
7
|
-
import {
|
|
8
|
-
isDefined, isDefinedNotNull, isUndefined,
|
|
9
|
-
} from '@xylabs/sdk-js'
|
|
10
|
-
import {
|
|
11
|
-
asXL1BlockNumber,
|
|
12
|
-
XL1Amount, XL1Places,
|
|
13
|
-
} from '@xyo-network/xl1-sdk'
|
|
14
|
-
import React, { useState } from 'react'
|
|
15
|
-
|
|
16
|
-
import { LocalGatewayDecorator } from '../../story/index.ts'
|
|
17
|
-
import { useViewerFromGateway } from '../gateway/index.ts'
|
|
18
|
-
|
|
19
|
-
const STUB_BLOCK = asXL1BlockNumber(92_444, true)
|
|
20
|
-
const STUB_STEP = 3
|
|
21
|
-
const STUB_POSITION = 3
|
|
22
|
-
|
|
23
|
-
const wallet = globalThis.xyo?.client !== undefined
|
|
24
|
-
const inIframe = globalThis.self !== window.top
|
|
25
|
-
|
|
26
|
-
const UseStepRewardWeightTest: React.FC = () => {
|
|
27
|
-
const viewer = useViewerFromGateway()
|
|
28
|
-
const [error, setError] = useState<Error | null>(null)
|
|
29
|
-
const [result, setResult] = useState<number | null | undefined>()
|
|
30
|
-
const [loading, setLoading] = useState(false)
|
|
31
|
-
|
|
32
|
-
const handleClick = async () => {
|
|
33
|
-
if (isDefinedNotNull(viewer)) {
|
|
34
|
-
try {
|
|
35
|
-
setLoading(true)
|
|
36
|
-
const weight = await viewer.networkStakeStepRewardPositionWeight({ block: STUB_BLOCK, step: STUB_STEP }, STUB_POSITION)
|
|
37
|
-
setError(null)
|
|
38
|
-
const normalizedWeight = new XL1Amount(weight).to(XL1Places.xl1)
|
|
39
|
-
setResult(Number(normalizedWeight))
|
|
40
|
-
setLoading(false)
|
|
41
|
-
} catch (err) {
|
|
42
|
-
setError(err as Error)
|
|
43
|
-
setResult(undefined)
|
|
44
|
-
setLoading(false)
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return (
|
|
50
|
-
<Stack gap={2} alignItems="center">
|
|
51
|
-
{inIframe
|
|
52
|
-
? (
|
|
53
|
-
<Alert severity="warning">
|
|
54
|
-
This story is running in an iframe. Please run it in a standalone browser window to test the wallet extension.
|
|
55
|
-
</Alert>
|
|
56
|
-
)
|
|
57
|
-
: null}
|
|
58
|
-
{isUndefined(wallet)
|
|
59
|
-
? (
|
|
60
|
-
<Alert severity="warning">
|
|
61
|
-
No wallet extension found. Please install the Xyo Wallet Chrome Extension.
|
|
62
|
-
</Alert>
|
|
63
|
-
)
|
|
64
|
-
: null}
|
|
65
|
-
<ButtonEx loading={loading} variant="contained" onClick={() => void handleClick()} disabled={!isDefined(viewer)}>
|
|
66
|
-
Get Reward Weight Test
|
|
67
|
-
</ButtonEx>
|
|
68
|
-
<pre>
|
|
69
|
-
{JSON.stringify(result, null, 2)}
|
|
70
|
-
</pre>
|
|
71
|
-
{error ? <Alert severity="error">{error.message}</Alert> : null}
|
|
72
|
-
</Stack>
|
|
73
|
-
)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export default {
|
|
77
|
-
title: 'provider/useViewer/networkStakeStepRewardPositionWeight',
|
|
78
|
-
component: UseStepRewardWeightTest,
|
|
79
|
-
decorators: [LocalGatewayDecorator],
|
|
80
|
-
} as Meta
|
|
81
|
-
|
|
82
|
-
export const Template: StoryFn<typeof UseStepRewardWeightTest> = args => <UseStepRewardWeightTest {...args} />
|
|
83
|
-
|
|
84
|
-
const Default = Template.bind({})
|
|
85
|
-
Default.args = {}
|
|
86
|
-
|
|
87
|
-
export { Default }
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { type Hash, isNull } from '@xylabs/sdk-js'
|
|
2
|
-
import { PayloadBuilder } from '@xyo-network/sdk-js'
|
|
3
|
-
import type {
|
|
4
|
-
DataLakeViewer, SignedHydratedBlockWithHashMeta, SignedHydratedTransactionWithHashMeta, XL1BlockNumber,
|
|
5
|
-
} from '@xyo-network/xl1-sdk'
|
|
6
|
-
import {
|
|
7
|
-
addDataLakePayloads,
|
|
8
|
-
addDataLakePayloadsToPayloads, DataLakeViewerMoniker, JsonRpcXyoViewer,
|
|
9
|
-
} from '@xyo-network/xl1-sdk'
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* A Viewer with data lake capabilities, it will attempt to fill in missing payloads from the
|
|
13
|
-
* data lake viewer if available.
|
|
14
|
-
*
|
|
15
|
-
* THIS IS AN EXPERIMENTAL CLASS! Only intended to prove the data lake viewer functionality works.
|
|
16
|
-
* In the future, it should be refactored to augment any viewer with data lake capabilities,
|
|
17
|
-
* and not rely on inheritance from JsonRpcXyoViewer which has the 'creatable' pattern baked in.
|
|
18
|
-
*/
|
|
19
|
-
export class ViewerWithDataLake extends JsonRpcXyoViewer {
|
|
20
|
-
protected dataLakeViewer: DataLakeViewer | undefined
|
|
21
|
-
|
|
22
|
-
override async blocksByHash(hash: Hash, limit?: number): Promise<SignedHydratedBlockWithHashMeta[]> {
|
|
23
|
-
const blocks = await super.blocksByHash(hash, limit)
|
|
24
|
-
return await Promise.all(blocks.map(async block => (await addDataLakePayloads(block, this.dataLakeViewer))[0]))
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
override async blocksByNumber(blockNumber: XL1BlockNumber, limit?: number): Promise<SignedHydratedBlockWithHashMeta[]> {
|
|
28
|
-
const blocks = await super.blocksByNumber(blockNumber, limit)
|
|
29
|
-
return await Promise.all(blocks.map(async block => (await addDataLakePayloads(block, this.dataLakeViewer))[0]))
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
override async createHandler() {
|
|
33
|
-
await super.createHandler()
|
|
34
|
-
this.dataLakeViewer = await this.locator.tryGetInstance<DataLakeViewer>(DataLakeViewerMoniker)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
override async payloadsByHash(hashes: Hash[]) {
|
|
38
|
-
const payloads = await super.payloadsByHash(hashes)
|
|
39
|
-
return (await addDataLakePayloadsToPayloads(hashes, payloads, this.dataLakeViewer))[0]
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
override async transactionByHash(hash: Hash): Promise<SignedHydratedTransactionWithHashMeta | null> {
|
|
43
|
-
const transaction = await super.transaction.byHash(hash)
|
|
44
|
-
if (!this.dataLakeViewer) return transaction ? [await PayloadBuilder.addHashMeta(transaction[0]), await PayloadBuilder.addHashMeta(transaction[1])] : null
|
|
45
|
-
|
|
46
|
-
return isNull(transaction)
|
|
47
|
-
? transaction
|
|
48
|
-
: (await addDataLakePayloads([await PayloadBuilder.addHashMeta(transaction[0]),
|
|
49
|
-
await PayloadBuilder.addHashMeta(transaction[1])], this.dataLakeViewer))[0]
|
|
50
|
-
}
|
|
51
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import {
|
|
3
|
-
delay, isDefined, isUndefined,
|
|
4
|
-
} from '@xylabs/sdk-js'
|
|
5
|
-
import { basicRemoteViewerLocator } from '@xyo-network/chain-orchestration'
|
|
6
|
-
import type { JsonRpcXyoViewer } from '@xyo-network/xl1-sdk'
|
|
7
|
-
import {
|
|
8
|
-
LocalNetwork,
|
|
9
|
-
SequenceNetwork,
|
|
10
|
-
XyoViewerMoniker,
|
|
11
|
-
} from '@xyo-network/xl1-sdk'
|
|
12
|
-
import { useEffect, useState } from 'react'
|
|
13
|
-
|
|
14
|
-
const INTERVAL = 5000 // 5 seconds
|
|
15
|
-
|
|
16
|
-
const localRpcEndpoint: string = `${LocalNetwork.url}/rpc`
|
|
17
|
-
const sequenceRpcEndpoint: string = `${SequenceNetwork.url}/rpc`
|
|
18
|
-
|
|
19
|
-
export const useCheckLocalRpc = () => {
|
|
20
|
-
const isLocalProducer = useCheckRpc(localRpcEndpoint)
|
|
21
|
-
return isLocalProducer
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const useCheckSequenceRpc = () => {
|
|
25
|
-
const isSequenceRpc = useCheckRpc(sequenceRpcEndpoint)
|
|
26
|
-
return isSequenceRpc
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export const useCheckRpc = (endpoint?: string) => {
|
|
30
|
-
const [isLocalProducer, setIsLocalProducer] = useState(false)
|
|
31
|
-
const [error, setError] = useState<Error>()
|
|
32
|
-
|
|
33
|
-
const [viewer] = usePromise(async () => {
|
|
34
|
-
if (isUndefined(endpoint)) return
|
|
35
|
-
|
|
36
|
-
const locator = await basicRemoteViewerLocator(endpoint, { rpc: { protocol: 'http', url: endpoint } })
|
|
37
|
-
|
|
38
|
-
const viewer = await locator.getInstance<JsonRpcXyoViewer>(XyoViewerMoniker)
|
|
39
|
-
return viewer
|
|
40
|
-
}, [endpoint])
|
|
41
|
-
|
|
42
|
-
useEffect(() => {
|
|
43
|
-
if (isUndefined(viewer)) return
|
|
44
|
-
|
|
45
|
-
void (async () => {
|
|
46
|
-
setError(undefined)
|
|
47
|
-
while (!isLocalProducer) {
|
|
48
|
-
try {
|
|
49
|
-
const block = await viewer.currentBlock()
|
|
50
|
-
setIsLocalProducer(isDefined(block))
|
|
51
|
-
} catch (err) {
|
|
52
|
-
setError(err as Error)
|
|
53
|
-
setIsLocalProducer(false)
|
|
54
|
-
}
|
|
55
|
-
await delay(INTERVAL)
|
|
56
|
-
}
|
|
57
|
-
})()
|
|
58
|
-
}, [isLocalProducer, viewer])
|
|
59
|
-
|
|
60
|
-
return { isLocalProducer, error }
|
|
61
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { Address } from '@xylabs/sdk-js'
|
|
2
|
-
import {
|
|
3
|
-
hexToBigInt, isHex, toHex,
|
|
4
|
-
} from '@xylabs/sdk-js'
|
|
5
|
-
import type { AccountBalanceHistoryItem, AttoXL1 } from '@xyo-network/xl1-sdk'
|
|
6
|
-
|
|
7
|
-
export const balanceForRange = (address: Address, results: AccountBalanceHistoryItem[]): [AttoXL1, AttoXL1] => {
|
|
8
|
-
const totalReceivedBalance = results?.reduce((a, [_block, _tx, transfer]) => {
|
|
9
|
-
return a + hexToBigInt(transfer.transfers[address] ?? toHex(0))
|
|
10
|
-
}, 0n)
|
|
11
|
-
// eslint-disable-next-line unicorn/no-array-reduce
|
|
12
|
-
const totalSentBalance = results?.reduce((a, [_block, _tx, transfer]) => {
|
|
13
|
-
return (transfer.from === address) ? a + Object.values(transfer.transfers).reduce((a, v) => a + (isHex(v) ? hexToBigInt(v) : 0n), 0n) : a
|
|
14
|
-
}, 0n)
|
|
15
|
-
return [totalReceivedBalance, totalSentBalance] as [AttoXL1, AttoXL1]
|
|
16
|
-
}
|
package/src/lib/buildGateway.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { assertEx, isDefined } from '@xylabs/sdk-js'
|
|
2
|
-
import { basicRemoteRunnerLocator, basicRemoteViewerLocator } from '@xyo-network/chain-orchestration'
|
|
3
|
-
import type { AccountInstance } from '@xyo-network/sdk-js'
|
|
4
|
-
import type {
|
|
5
|
-
GatewayName, RemoteConfig, SimpleXyoGateway,
|
|
6
|
-
} from '@xyo-network/xl1-sdk'
|
|
7
|
-
import {
|
|
8
|
-
DefaultNetworks, NetworkDataLakeUrls, XyoGatewayMoniker,
|
|
9
|
-
} from '@xyo-network/xl1-sdk'
|
|
10
|
-
|
|
11
|
-
export const buildGateway = async (gatewayName: GatewayName, account?: AccountInstance) => {
|
|
12
|
-
const network = DefaultNetworks.find(network => network.id === gatewayName)
|
|
13
|
-
const resolvedNetwork = assertEx(network, () => `No network found for id ${gatewayName}`)
|
|
14
|
-
|
|
15
|
-
const remoteConfig: RemoteConfig = {
|
|
16
|
-
rpc: {
|
|
17
|
-
protocol: 'http',
|
|
18
|
-
url: `${resolvedNetwork.url}/rpc`,
|
|
19
|
-
},
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const dataLakeEndpoint = NetworkDataLakeUrls[gatewayName]
|
|
23
|
-
|
|
24
|
-
const locator = isDefined(account)
|
|
25
|
-
? await basicRemoteRunnerLocator(gatewayName, remoteConfig, account, dataLakeEndpoint)
|
|
26
|
-
: await basicRemoteViewerLocator(gatewayName, remoteConfig, dataLakeEndpoint)
|
|
27
|
-
|
|
28
|
-
return await locator.getInstance<SimpleXyoGateway>(XyoGatewayMoniker)
|
|
29
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { AccountBalanceHistoryItem } from '@xyo-network/xl1-sdk'
|
|
2
|
-
|
|
3
|
-
export const findMinimumBlock = (history: AccountBalanceHistoryItem[]): number => {
|
|
4
|
-
// since taking Math.min of an empty array returns Infinity
|
|
5
|
-
if (history.length === 0) return 0
|
|
6
|
-
const blockNumbers = history.map(([blockBw]) => blockBw.block)
|
|
7
|
-
const min = Math.min(...blockNumbers)
|
|
8
|
-
return Math.max(min - 1, 0)
|
|
9
|
-
}
|
package/src/lib/index.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { Decorator } from '@storybook/react-vite'
|
|
2
|
-
import {
|
|
3
|
-
LocalNetwork, MainNetwork, SequenceNetwork,
|
|
4
|
-
} from '@xyo-network/xl1-sdk'
|
|
5
|
-
|
|
6
|
-
import { GatewayProvider, InPageGatewaysProvider } from '../contexts/index.ts'
|
|
7
|
-
|
|
8
|
-
export const MainnetGatewayDecorator: Decorator = (Story, ...args) => {
|
|
9
|
-
return (
|
|
10
|
-
<InPageGatewaysProvider>
|
|
11
|
-
<GatewayProvider gatewayName={MainNetwork.id}>
|
|
12
|
-
<Story {...args} />
|
|
13
|
-
</GatewayProvider>
|
|
14
|
-
</InPageGatewaysProvider>
|
|
15
|
-
)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const SequenceGatewayDecorator: Decorator = (Story, ...args) => {
|
|
19
|
-
return (
|
|
20
|
-
<InPageGatewaysProvider>
|
|
21
|
-
<GatewayProvider gatewayName={SequenceNetwork.id}>
|
|
22
|
-
<Story {...args} />
|
|
23
|
-
</GatewayProvider>
|
|
24
|
-
</InPageGatewaysProvider>
|
|
25
|
-
)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const LocalGatewayDecorator: Decorator = (Story, ...args) => {
|
|
29
|
-
return (
|
|
30
|
-
<InPageGatewaysProvider>
|
|
31
|
-
<GatewayProvider gatewayName={LocalNetwork.id}>
|
|
32
|
-
<Story {...args} />
|
|
33
|
-
</GatewayProvider>
|
|
34
|
-
</InPageGatewaysProvider>
|
|
35
|
-
)
|
|
36
|
-
}
|
package/src/story/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './GatewayDecorator.tsx'
|
package/src/types/index.ts
DELETED