@xyo-network/react-chain-provider 1.16.1 → 1.16.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.
Files changed (61) hide show
  1. package/dist/browser/components/account/BalanceHistoryFlexbox.d.ts +11 -0
  2. package/dist/browser/components/account/BalanceHistoryFlexbox.d.ts.map +1 -0
  3. package/dist/browser/components/account/BalanceHistoryFlexbox.stories.d.ts +6 -0
  4. package/dist/browser/components/account/BalanceHistoryFlexbox.stories.d.ts.map +1 -0
  5. package/dist/browser/components/account/BalanceHistoryTable.d.ts +8 -0
  6. package/dist/browser/components/account/BalanceHistoryTable.d.ts.map +1 -0
  7. package/dist/browser/components/account/index.d.ts +3 -0
  8. package/dist/browser/components/account/index.d.ts.map +1 -0
  9. package/dist/browser/components/index.d.ts +1 -0
  10. package/dist/browser/components/index.d.ts.map +1 -1
  11. package/dist/browser/hooks/client/helpers/findCaveat.d.ts +1 -2
  12. package/dist/browser/hooks/client/helpers/findCaveat.d.ts.map +1 -1
  13. package/dist/browser/hooks/client/useGateway.d.ts +1 -1
  14. package/dist/browser/hooks/index.d.ts +1 -0
  15. package/dist/browser/hooks/index.d.ts.map +1 -1
  16. package/dist/browser/hooks/useAccountBalanceHistory.d.ts +11 -0
  17. package/dist/browser/hooks/useAccountBalanceHistory.d.ts.map +1 -0
  18. package/dist/browser/hooks/useAddressBalance.d.ts +1 -3
  19. package/dist/browser/hooks/useAddressBalance.d.ts.map +1 -1
  20. package/dist/browser/hooks/useConnectAccount.d.ts +3 -7
  21. package/dist/browser/hooks/useConnectAccount.d.ts.map +1 -1
  22. package/dist/browser/hooks/useNetwork.d.ts +2 -2
  23. package/dist/browser/hooks/useNetwork.d.ts.map +1 -1
  24. package/dist/browser/hooks/useSigner.d.ts +1 -1
  25. package/dist/browser/hooks/useSigner.d.ts.map +1 -1
  26. package/dist/browser/hooks/viewer/UseStepRewardWeightTest.stories.d.ts.map +1 -1
  27. package/dist/browser/hooks/viewer/useViewerFromWallet.d.ts +1 -1
  28. package/dist/browser/hooks/viewer/useViewerFromWallet.d.ts.map +1 -1
  29. package/dist/browser/hooks/viewer/useViewerFromWallet.stories.d.ts.map +1 -1
  30. package/dist/browser/index.d.ts +1 -0
  31. package/dist/browser/index.d.ts.map +1 -1
  32. package/dist/browser/index.mjs +266 -72
  33. package/dist/browser/index.mjs.map +1 -1
  34. package/dist/browser/types/account/BalanceHistoryItemRow.d.ts +14 -0
  35. package/dist/browser/types/account/BalanceHistoryItemRow.d.ts.map +1 -0
  36. package/dist/browser/types/account/Table.d.ts +7 -0
  37. package/dist/browser/types/account/Table.d.ts.map +1 -0
  38. package/dist/browser/types/account/index.d.ts +3 -0
  39. package/dist/browser/types/account/index.d.ts.map +1 -0
  40. package/dist/browser/types/index.d.ts +2 -0
  41. package/dist/browser/types/index.d.ts.map +1 -0
  42. package/package.json +17 -14
  43. package/src/components/account/BalanceHistoryFlexbox.stories.tsx +25 -0
  44. package/src/components/account/BalanceHistoryFlexbox.tsx +67 -0
  45. package/src/components/account/BalanceHistoryTable.tsx +81 -0
  46. package/src/components/account/index.ts +2 -0
  47. package/src/components/index.ts +1 -0
  48. package/src/hooks/client/helpers/findCaveat.ts +3 -2
  49. package/src/hooks/index.ts +1 -0
  50. package/src/hooks/useAccountBalanceHistory.ts +104 -0
  51. package/src/hooks/useConnectAccount.ts +1 -1
  52. package/src/hooks/useNetwork.ts +2 -2
  53. package/src/hooks/useSigner.ts +4 -3
  54. package/src/hooks/viewer/UseStepRewardWeightTest.stories.tsx +1 -3
  55. package/src/hooks/viewer/useViewerFromWallet.stories.tsx +1 -3
  56. package/src/hooks/viewer/useViewerFromWallet.ts +6 -9
  57. package/src/index.ts +1 -0
  58. package/src/types/account/BalanceHistoryItemRow.ts +14 -0
  59. package/src/types/account/Table.ts +14 -0
  60. package/src/types/account/index.ts +2 -0
  61. package/src/types/index.ts +1 -0
@@ -0,0 +1,104 @@
1
+ import {
2
+ type Address, type Hex,
3
+ hexToBigInt,
4
+ toHex,
5
+ } from '@xylabs/hex'
6
+ import { usePromise } from '@xylabs/react-promise'
7
+ import { isUndefined } from '@xylabs/typeof'
8
+ import type { AttoXL1, XL1BlockRange } from '@xyo-network/xl1-protocol'
9
+ import type { AccountBalanceHistoryItem, XyoViewer } from '@xyo-network/xl1-protocol-sdk'
10
+
11
+ import type { AccountBalanceHistoryItemRow } from '../types/index.ts'
12
+
13
+ export const formatAccountBalanceHistory = (address: Address, history: AccountBalanceHistoryItem[]): AccountBalanceHistoryItemRow[] => {
14
+ let results: AccountBalanceHistoryItemRow[] = []
15
+ for (const item of history) {
16
+ const [blockBw, txBw, transfer] = item
17
+ if (address === transfer.from) {
18
+ for (const [to, amount] of Object.entries(transfer.transfers)) {
19
+ results.push({
20
+ amount: hexToBigInt(amount as Hex),
21
+ blockNumber: blockBw.block,
22
+ from: transfer.from,
23
+ timestamp: blockBw.$epoch,
24
+ key: globalThis.crypto.randomUUID(),
25
+ to: to as Address,
26
+ txHash: txBw?._hash,
27
+ debug: item,
28
+ type: 'send',
29
+ })
30
+ }
31
+ } else {
32
+ results.push({
33
+ amount: hexToBigInt(transfer.transfers[address] as Hex),
34
+ blockNumber: blockBw.block,
35
+ from: transfer.from,
36
+ key: globalThis.crypto.randomUUID(),
37
+ timestamp: blockBw.$epoch,
38
+ to: address,
39
+ txHash: txBw?._hash,
40
+ debug: item,
41
+ type: 'receive',
42
+ })
43
+ }
44
+ }
45
+ return results
46
+ }
47
+
48
+ const findMinimumBlock = (history: AccountBalanceHistoryItem[]): number => {
49
+ // since taking Math.min of an empty array returns Infinity
50
+ if (history.length === 0) return 0
51
+ const blockNumbers = history.map(([blockBw]) => blockBw.block)
52
+ const min = Math.min(...blockNumbers)
53
+ return Math.max(min - 1, 0)
54
+ }
55
+
56
+ const balanceForRange = (address: Address, results: AccountBalanceHistoryItem[]): [AttoXL1, AttoXL1] => {
57
+ const totalReceivedBalance = results?.reduce((a, [_block, _tx, transfer]) => {
58
+ return a + hexToBigInt(transfer.transfers[address] ?? toHex(0))
59
+ }, 0n)
60
+ // eslint-disable-next-line unicorn/no-array-reduce
61
+ const totalSentBalance = results?.reduce((a, [_block, _tx, transfer]) => {
62
+ return (transfer.from === address) ? a + Object.values(transfer.transfers).reduce((a, v) => a + (v ? hexToBigInt(v) : 0n), 0n) : a
63
+ }, 0n)
64
+ return [totalReceivedBalance, totalSentBalance] as [AttoXL1, AttoXL1]
65
+ }
66
+
67
+ export const useAccountBalanceHistory = (address?: Address, viewer?: XyoViewer, refresh?: number) => {
68
+ return usePromise(async () => {
69
+ if (isUndefined(viewer) || isUndefined(address)) return
70
+
71
+ // Pagination Values
72
+ let page = 1
73
+ let lastBlock: number
74
+ const maxPage = 10
75
+ const pagedHistory: AccountBalanceHistoryItem[] = []
76
+
77
+ // Fetch first page to get starting lastBlock
78
+ const history = await viewer.accountBalanceHistory(address)
79
+ if (history.length > 0) {
80
+ pagedHistory.push(...history)
81
+ // Set lastBlock to one less than the lowest block in the first page
82
+ lastBlock = findMinimumBlock(history)
83
+ while (page < maxPage) {
84
+ // Fetch next page
85
+ const nextHistory = await viewer.accountBalanceHistory(address, [0, lastBlock] as XL1BlockRange)
86
+ // Break if no more history
87
+ if (nextHistory.length === 0) break
88
+ // Update lastBlock and append to pagedHistory
89
+ lastBlock = findMinimumBlock(nextHistory)
90
+ pagedHistory.push(...nextHistory)
91
+ // Increment page
92
+ // Even if we are going to go over and break the loop, we still want to increment to show the answer was truncated
93
+ page++
94
+ }
95
+ }
96
+ const formattedHistory = formatAccountBalanceHistory(address, pagedHistory)
97
+
98
+ return {
99
+ history: formattedHistory,
100
+ balance: balanceForRange(address, pagedHistory),
101
+ truncated: page >= maxPage,
102
+ }
103
+ }, [address, viewer, refresh])
104
+ }
@@ -20,7 +20,7 @@ export const useConnectAccount = (gatewayName: GatewayName = MainNetwork.id, tim
20
20
  const connectSigner = useCallback(async () => {
21
21
  try {
22
22
  const assertedGateway = assertEx(gateway, () => `Gateway ${gatewayName} is not available`)
23
- const signer = await assertedGateway.signer()
23
+ const signer = await assertedGateway.signer
24
24
  const address = await signer.address()
25
25
  setAddress(address)
26
26
  return address
@@ -1,12 +1,12 @@
1
1
  import { isUndefined } from '@xylabs/typeof'
2
2
  import type { DefaultNetworkIds } from '@xyo-network/xl1-protocol'
3
- import { MemoryXyoNetwork } from '@xyo-network/xl1-rpc'
3
+ import { SimpleXyoNetwork } from '@xyo-network/xl1-protocol-sdk'
4
4
  import { useMemo } from 'react'
5
5
 
6
6
  export const useNetwork = (id?: DefaultNetworkIds) => {
7
7
  const network = useMemo(() => {
8
8
  if (isUndefined(id)) return
9
- return new MemoryXyoNetwork(id)
9
+ return new SimpleXyoNetwork(id)
10
10
  }, [id])
11
11
 
12
12
  return network
@@ -1,8 +1,9 @@
1
1
  import type { AccountInstance } from '@xyo-network/account-model'
2
- import type { XyoConnection, XyoSigner } from '@xyo-network/xl1-protocol-sdk'
3
- import { MemoryXyoSigner } from '@xyo-network/xl1-rpc'
2
+ import {
3
+ SimpleXyoSigner, type XyoConnection, type XyoSigner,
4
+ } from '@xyo-network/xl1-protocol-sdk'
4
5
  import { useMemo } from 'react'
5
6
 
6
7
  export const useSigner = (_provider?: XyoConnection, account?: AccountInstance) => {
7
- return useMemo<XyoSigner | undefined>(() => account ? new MemoryXyoSigner(account) : undefined, [account])
8
+ return useMemo<XyoSigner | undefined>(() => account ? new SimpleXyoSigner(account) : undefined, [account])
8
9
  }
@@ -8,7 +8,6 @@ import {
8
8
  isDefined, isDefinedNotNull, isUndefined,
9
9
  } from '@xylabs/typeof'
10
10
  import { LocalNetwork } from '@xyo-network/chain-network-model'
11
- import { ErrorRender } from '@xyo-network/react-error'
12
11
  import {
13
12
  asXL1BlockNumber, XL1Amount, XL1Places,
14
13
  } from '@xyo-network/xl1-protocol'
@@ -25,7 +24,7 @@ const inIframe = globalThis.self !== window.top
25
24
 
26
25
  const UseStepRewardWeightTest: React.FC = () => {
27
26
  const defaultDestination = LocalNetwork.id
28
- const [viewer, viewerError] = useViewerFromWallet(defaultDestination)
27
+ const viewer = useViewerFromWallet(defaultDestination)
29
28
  const [error, setError] = useState<Error | null>(null)
30
29
  const [result, setResult] = useState<number | null | undefined>()
31
30
  const [loading, setLoading] = useState(false)
@@ -56,7 +55,6 @@ const UseStepRewardWeightTest: React.FC = () => {
56
55
  </Alert>
57
56
  )
58
57
  : null}
59
- <ErrorRender error={viewerError} />
60
58
  {isUndefined(wallet)
61
59
  ? (
62
60
  <Alert severity="warning">
@@ -4,7 +4,6 @@ import { ButtonEx } from '@xylabs/react-button'
4
4
  import { isDefined, isDefinedNotNull } from '@xylabs/typeof'
5
5
  import { MainNetwork } from '@xyo-network/chain-network-model'
6
6
  import { isXyoGlobal } from '@xyo-network/react-chain-model'
7
- import { ErrorRender } from '@xyo-network/react-error'
8
7
  import type { HydratedBlock } from '@xyo-network/xl1-protocol'
9
8
  import React, { useState } from 'react'
10
9
 
@@ -15,7 +14,7 @@ const inIframe = globalThis.self !== window.top
15
14
 
16
15
  const UseViewerFromWalletStoryTemplate: React.FC = () => {
17
16
  const defaultDestination = MainNetwork.id
18
- const [viewer, viewerError] = useViewerFromWallet(defaultDestination)
17
+ const viewer = useViewerFromWallet(defaultDestination)
19
18
  const [error, setError] = useState<Error | null>(null)
20
19
  const [result, setResult] = useState<HydratedBlock | null | undefined>()
21
20
  const [loading, setLoading] = useState(false)
@@ -45,7 +44,6 @@ const UseViewerFromWalletStoryTemplate: React.FC = () => {
45
44
  </Alert>
46
45
  )
47
46
  : null}
48
- <ErrorRender error={viewerError} />
49
47
  {hasXyoGlobal
50
48
  ? null
51
49
  : (
@@ -1,4 +1,3 @@
1
- import { usePromise } from '@xylabs/react-promise'
2
1
  import { isDefined, isDefinedNotNull } from '@xylabs/typeof'
3
2
  import type { GatewayName } from '@xyo-network/xl1-protocol'
4
3
 
@@ -15,12 +14,10 @@ export const useViewerFromWallet = (networkId?: GatewayName) => {
15
14
  gateway, error, isLoading,
16
15
  } = useGateway(networkId)
17
16
 
18
- return usePromise(async () => {
19
- if (isDefinedNotNull(error)) return null
20
- if (isLoading) return
21
- if (isDefined(gateway) && isDefined(networkId)) {
22
- const connection = await gateway?.connection()
23
- return connection?.viewer
24
- }
25
- }, [gateway, networkId])
17
+ if (isDefinedNotNull(error)) return null
18
+ if (isLoading) return
19
+ if (isDefined(gateway) && isDefined(networkId)) {
20
+ const connection = gateway?.connection
21
+ return connection?.viewer
22
+ }
26
23
  }
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './components/index.ts'
2
2
  export * from './hooks/index.ts'
3
+ export * from './types/index.ts'
@@ -0,0 +1,14 @@
1
+ import type { Address, Hash } from '@xylabs/hex'
2
+ import type { AccountBalanceHistoryItem } from '@xyo-network/xl1-protocol-sdk'
3
+
4
+ export interface AccountBalanceHistoryItemRow {
5
+ amount: bigint
6
+ blockNumber: number
7
+ debug: AccountBalanceHistoryItem
8
+ from: Address
9
+ key: string
10
+ timestamp: number
11
+ to: Address
12
+ txHash: Hash | undefined
13
+ type: 'send' | 'receive'
14
+ }
@@ -0,0 +1,14 @@
1
+ import type { AccountBalanceHistoryItemRow } from './BalanceHistoryItemRow.ts'
2
+
3
+ export type TableHeadingLabels = 'Tx Hash' | 'BlockNumber' | 'Timestamp' | 'From' | 'To' | 'Amount' | 'Debug'
4
+ export type TableHeadings = { [key in keyof AccountBalanceHistoryItemRow]: TableHeadingLabels }
5
+
6
+ export const TableHeadingLabels: TableHeadingLabels[] = [
7
+ 'Tx Hash',
8
+ 'BlockNumber',
9
+ 'Timestamp',
10
+ 'From',
11
+ 'To',
12
+ 'Amount',
13
+ 'Debug',
14
+ ] as const
@@ -0,0 +1,2 @@
1
+ export * from './BalanceHistoryItemRow.ts'
2
+ export * from './Table.ts'
@@ -0,0 +1 @@
1
+ export * from './account/index.ts'