-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathuseTransaction.ts
144 lines (122 loc) · 3.87 KB
/
useTransaction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// TODO(2fd): looks to big, refactor?
import { useCallback, useEffect, useMemo, useState } from 'react'
import { ChainId } from '@dcl/schemas/dist/dapps/chain-id'
import { getTransaction } from 'decentraland-dapps/dist/modules/transaction/txUtils'
import { AnyTransaction } from 'decentraland-dapps/dist/modules/transaction/types'
import { isPending } from 'decentraland-dapps/dist/modules/transaction/utils'
import Time from '../utils/date/Time'
import rollbar from '../utils/development/rollbar'
import segment from '../utils/development/segment'
import sentry from '../utils/development/sentry'
import {
clearTransactions,
restoreTransactions,
storeTransactions,
} from '../utils/tx/storage'
import { Transaction } from '../utils/tx/type'
type TransactionState = Transaction<any>[]
const initialState: TransactionState = []
export default function useTransaction(
address?: string | null,
chainId?: ChainId | null
) {
const [transactions, setTransactions] =
useState<TransactionState>(initialState)
// re-store tranasctions
useEffect(() => {
if (address && chainId) {
setTransactions(restoreTransactions(address, chainId))
}
}, [address, chainId])
// track transactions
useEffect(() => {
let closed = false
let timer: number | null = null
async function updateTransactions() {
if (!address || !chainId) {
return
}
let txs = transactions || []
const updatedTransactions: Transaction[] = []
for (const tx of txs) {
if (isPending(tx.status)) {
const updatedTransaction = await getTransaction(
address,
tx.chainId,
tx.hash
)
if (updatedTransaction) {
const hasChanges = Object.keys(updatedTransaction).some(
(key: keyof AnyTransaction) =>
tx[key] !== updatedTransaction[key] &&
String(tx[key]) !== String(updatedTransaction[key])
)
if (hasChanges) {
updatedTransactions.push({
...tx,
...updatedTransaction,
})
}
}
}
}
if (updatedTransactions.length > 0 && !closed) {
txs = storeTransactions(address, chainId, updatedTransactions)
setTransactions(txs)
}
const pendingTransactions = txs.filter((tx) => isPending(tx.status))
if (pendingTransactions.length > 0 && !closed) {
timer = setTimeout(updateTransactions, Time.Second * 2) as any
}
}
updateTransactions()
return () => {
closed = true
if (timer) {
clearTimeout(timer)
}
}
}, [transactions])
const add = useCallback(
(hash: string, payload: Record<string, any> = {}) => {
if (address && chainId) {
getTransaction(address, chainId, hash)
.then((tx) => {
if (!tx) {
return
}
const newTransaction: Transaction = {
...tx,
timestamp: Date.now(),
chainId,
payload,
}
const txs = storeTransactions(address, chainId, [newTransaction])
setTransactions(txs)
})
.catch((err) => {
console.error(err)
rollbar((rollbar) => rollbar.error(err))
sentry((sentry) => sentry.captureException(err))
segment((analytics) =>
analytics.track('error', {
...err,
message: err.message,
stack: err.stack,
})
)
})
}
},
[transactions]
)
const clear = useCallback(() => {
if (!address || !chainId) {
return
}
setTransactions(initialState)
clearTransactions(address, chainId)
}, [transactions])
const actions = useMemo(() => ({ add, clear }), [add, clear])
return [transactions, actions] as const
}