@xyo-network/react-chain-provider 1.16.2 → 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.
- package/dist/browser/components/account/BalanceHistoryFlexbox.d.ts +11 -0
- package/dist/browser/components/account/BalanceHistoryFlexbox.d.ts.map +1 -0
- package/dist/browser/components/account/BalanceHistoryFlexbox.stories.d.ts +6 -0
- package/dist/browser/components/account/BalanceHistoryFlexbox.stories.d.ts.map +1 -0
- package/dist/browser/components/account/BalanceHistoryTable.d.ts +8 -0
- package/dist/browser/components/account/BalanceHistoryTable.d.ts.map +1 -0
- package/dist/browser/components/account/index.d.ts +3 -0
- package/dist/browser/components/account/index.d.ts.map +1 -0
- package/dist/browser/components/index.d.ts +1 -0
- package/dist/browser/components/index.d.ts.map +1 -1
- package/dist/browser/hooks/index.d.ts +1 -0
- package/dist/browser/hooks/index.d.ts.map +1 -1
- package/dist/browser/hooks/useAccountBalanceHistory.d.ts +11 -0
- package/dist/browser/hooks/useAccountBalanceHistory.d.ts.map +1 -0
- package/dist/browser/index.d.ts +1 -0
- package/dist/browser/index.d.ts.map +1 -1
- package/dist/browser/index.mjs +255 -55
- package/dist/browser/index.mjs.map +1 -1
- package/dist/browser/types/account/BalanceHistoryItemRow.d.ts +14 -0
- package/dist/browser/types/account/BalanceHistoryItemRow.d.ts.map +1 -0
- package/dist/browser/types/account/Table.d.ts +7 -0
- package/dist/browser/types/account/Table.d.ts.map +1 -0
- package/dist/browser/types/account/index.d.ts +3 -0
- package/dist/browser/types/account/index.d.ts.map +1 -0
- package/dist/browser/types/index.d.ts +2 -0
- package/dist/browser/types/index.d.ts.map +1 -0
- package/package.json +9 -6
- package/src/components/account/BalanceHistoryFlexbox.stories.tsx +25 -0
- package/src/components/account/BalanceHistoryFlexbox.tsx +67 -0
- package/src/components/account/BalanceHistoryTable.tsx +81 -0
- package/src/components/account/index.ts +2 -0
- package/src/components/index.ts +1 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useAccountBalanceHistory.ts +104 -0
- package/src/index.ts +1 -0
- package/src/types/account/BalanceHistoryItemRow.ts +14 -0
- package/src/types/account/Table.ts +14 -0
- package/src/types/account/index.ts +2 -0
- 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
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -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 @@
|
|
|
1
|
+
export * from './account/index.ts'
|