-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathhooks.ts
More file actions
244 lines (230 loc) · 7.21 KB
/
hooks.ts
File metadata and controls
244 lines (230 loc) · 7.21 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import React, { useCallback, useEffect } from 'react'
import { bytes, Uint64LE } from '@ckb-lumos/lumos/codec'
import { type CKBComponents } from '@ckb-lumos/lumos/rpc'
import { getSUDTAccountList } from 'services/remote'
import { NeuronWalletActions, useDispatch } from 'states'
import {
epochParser,
getSUDTAmount,
isSuccessResponse,
nftFormatter,
PresetScript,
shannonToCKBFormatter,
sporeFormatter,
sudtValueToAmount,
toUint128Le,
useDialogWrapper,
useDidMount,
} from 'utils'
import { TFunction } from 'i18next'
import { MILLISECONDS, MILLISECONDS_PER_DAY } from 'utils/const'
import { AssetInfo, ChequeAssetInfo, NFTType } from '.'
export interface SpecialAssetCell {
blockHash: string
blockNumber: string
capacity: string
customizedAssetInfo: AssetInfo
daoData: string | null
data: string
lock: {
args: string
codeHash: string
hashType: 'type' | 'data' | 'data1' | 'data2'
}
lockHash: string
multiSignBlake160: string
outPoint: {
index: string
txHash: string
}
status: 'live' | 'dead'
timestamp: string
type: CKBComponents.Script | null
}
export const useMigrate = () => {
const { openDialog, dialogRef, closeDialog } = useDialogWrapper()
const onDocumentClick = useCallback(
(e: any) => {
if (!dialogRef?.current?.children?.[0]?.contains(e.target) && dialogRef?.current?.open) {
closeDialog()
}
},
[closeDialog, dialogRef]
)
useDidMount(() => {
document.addEventListener('click', onDocumentClick, false)
return () => document.removeEventListener('click', onDocumentClick, false)
})
return {
openDialog,
dialogRef,
closeDialog,
}
}
export const useClickMigrate = ({
closeMigrateDialog,
openMigrateToNewAccountDialog,
openMigrateToExistAccountDialog,
}: {
closeMigrateDialog: () => void
openMigrateToNewAccountDialog: () => void
openMigrateToExistAccountDialog: () => void
}) => {
return useCallback(
(e: React.BaseSyntheticEvent) => {
const {
dataset: { type },
} = e.currentTarget
closeMigrateDialog()
switch (type) {
case 'new-account':
openMigrateToNewAccountDialog()
break
case 'exist-account':
openMigrateToExistAccountDialog()
break
default:
break
}
},
[closeMigrateDialog, openMigrateToNewAccountDialog, openMigrateToExistAccountDialog]
)
}
export const useGetAssetAccounts = (walletID: string) => {
const dispatch = useDispatch()
useEffect(() => {
getSUDTAccountList({ walletID })
.then(res => {
if (isSuccessResponse(res)) {
return res.result
}
throw new Error(res.message.toString())
})
.then((list: Controller.GetSUDTAccountList.Response) => {
dispatch({
type: NeuronWalletActions.GetSUDTAccountList,
payload: list,
})
})
.catch((err: Error) => console.error(err))
}, [walletID, dispatch])
}
interface SpecialAssetColumnInfoProps {
epoch: string
bestKnownBlockTimestamp: number
tokenInfoList: Array<Controller.GetTokenInfoList.TokenInfo>
t: TFunction
}
export const useSpecialAssetColumnInfo = ({
epoch,
bestKnownBlockTimestamp,
tokenInfoList,
t,
}: SpecialAssetColumnInfoProps) => {
return useCallback(
(item: SpecialAssetCell) => {
const { timestamp, customizedAssetInfo: assetInfo, capacity, lock, type, data } = item
const datetime = +timestamp
let targetTime: undefined | number
let status:
| 'user-defined-asset'
| 'locked-asset'
| 'claim-asset'
| 'withdraw-asset'
| 'transfer-nft'
| 'user-defined-token' = 'user-defined-asset'
let epochsInfo: Record<'target' | 'current', number> | undefined
let amount: string = `${shannonToCKBFormatter(capacity)} CKB`
switch (assetInfo.lock) {
case PresetScript.Locktime: {
const targetEpochInfo = epochParser(bytes.hexify(Uint64LE.pack(`0x${lock.args.slice(-16)}`)))
const currentEpochInfo = epochParser(epoch)
const targetEpochFraction =
Number(targetEpochInfo.length) > 0 ? Number(targetEpochInfo.index) / Number(targetEpochInfo.length) : 1
epochsInfo = {
target: Number(targetEpochInfo.number) + Math.min(targetEpochFraction, 1),
current: Number(currentEpochInfo.number) + Number(currentEpochInfo.index) / Number(currentEpochInfo.length),
}
targetTime = bestKnownBlockTimestamp + (epochsInfo.target - epochsInfo.current) * MILLISECONDS
if (epochsInfo.target > epochsInfo.current) {
status = 'locked-asset'
} else {
status = 'claim-asset'
}
break
}
case PresetScript.Cheque: {
status = (assetInfo as ChequeAssetInfo).data === 'claimable' ? 'claim-asset' : 'withdraw-asset'
if (status === 'withdraw-asset') {
targetTime = datetime + MILLISECONDS_PER_DAY
}
try {
const tokenInfo = tokenInfoList.find(info => info.tokenID === type?.args)
if (!tokenInfo) {
throw new Error('Token info not found')
}
const balance = BigInt(toUint128Le(data)).toString()
amount = `${sudtValueToAmount(balance, tokenInfo.decimal)} ${tokenInfo.symbol}`
} catch {
amount = 'sUDT Token'
}
break
}
default: {
// ignore
}
}
const isLockedCheque = status === 'withdraw-asset' && targetTime && Date.now() < targetTime
const isNFTTransferable = assetInfo.type === NFTType.NFT && assetInfo.data === 'transferable'
const isNFTClassOrIssuer = assetInfo.type === NFTType.NFTClass || assetInfo.type === NFTType.NFTIssuer
const isSpore = assetInfo.type === NFTType.Spore
let sporeClusterInfo: { name: string; description: string } | undefined
switch (assetInfo.type) {
case NFTType.NFT: {
amount = nftFormatter(type?.args)
status = 'transfer-nft'
break
}
case NFTType.Spore: {
if (type) {
// every spore cell is transferable
status = 'transfer-nft'
sporeClusterInfo = JSON.parse(item.customizedAssetInfo.data)
amount = sporeFormatter({ args: type.args, data: item.data, clusterName: sporeClusterInfo?.name })
}
break
}
case NFTType.NFTClass:
case NFTType.NFTIssuer:
case 'Unknown': {
amount = t('special-assets.unknown-asset')
break
}
case PresetScript.XUDT:
case PresetScript.SUDT: {
status = 'user-defined-token'
const tokenInfo = tokenInfoList.find(info => info.tokenID === type?.args)
const amountInfo = getSUDTAmount({ tokenInfo, data })
amount = amountInfo.amount
break
}
default: {
break
}
}
return {
amount,
status,
targetTime,
isLockedCheque,
isNFTTransferable,
isNFTClassOrIssuer,
epochsInfo,
isSpore,
sporeClusterInfo,
udtType: assetInfo.type,
}
},
[epoch, bestKnownBlockTimestamp, tokenInfoList, t]
)
}