@unisat/wallet-state 1.0.5 → 1.2.0

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 (290) hide show
  1. package/LICENSE +0 -5
  2. package/lib/actions/global.d.ts +2 -0
  3. package/lib/actions/global.d.ts.map +1 -0
  4. package/lib/context/ApprovalContext.d.ts +9 -0
  5. package/lib/context/ApprovalContext.d.ts.map +1 -0
  6. package/lib/context/DeviceContext.d.ts +17 -0
  7. package/lib/context/DeviceContext.d.ts.map +1 -0
  8. package/lib/context/I18nContext.d.ts +13 -0
  9. package/lib/context/I18nContext.d.ts.map +1 -0
  10. package/lib/context/NavigationContext.d.ts +218 -0
  11. package/lib/context/NavigationContext.d.ts.map +1 -0
  12. package/lib/context/PriceContext.d.ts +13 -0
  13. package/lib/context/PriceContext.d.ts.map +1 -0
  14. package/lib/context/StorageContext.d.ts +60 -0
  15. package/lib/context/StorageContext.d.ts.map +1 -0
  16. package/lib/context/ToolsContext.d.ts +17 -0
  17. package/lib/context/ToolsContext.d.ts.map +1 -0
  18. package/lib/context/WalletContext.d.ts +493 -0
  19. package/lib/context/WalletContext.d.ts.map +1 -0
  20. package/lib/context/index.d.ts +9 -0
  21. package/lib/context/index.d.ts.map +1 -0
  22. package/lib/hooks/accounts.d.ts +34 -0
  23. package/lib/hooks/accounts.d.ts.map +1 -0
  24. package/lib/hooks/base.d.ts +14 -0
  25. package/lib/hooks/base.d.ts.map +1 -0
  26. package/lib/hooks/browser.d.ts +32 -0
  27. package/lib/hooks/browser.d.ts.map +1 -0
  28. package/lib/hooks/discovery.d.ts +17 -0
  29. package/lib/hooks/discovery.d.ts.map +1 -0
  30. package/lib/hooks/global.d.ts +43 -0
  31. package/lib/hooks/global.d.ts.map +1 -0
  32. package/lib/hooks/index.d.ts +10 -0
  33. package/lib/hooks/index.d.ts.map +1 -0
  34. package/lib/hooks/keyrings.d.ts +5 -0
  35. package/lib/hooks/keyrings.d.ts.map +1 -0
  36. package/lib/hooks/settings.d.ts +43 -0
  37. package/lib/hooks/settings.d.ts.map +1 -0
  38. package/lib/hooks/transactions.d.ts +67 -0
  39. package/lib/hooks/transactions.d.ts.map +1 -0
  40. package/lib/hooks/ui.d.ts +112 -0
  41. package/lib/hooks/ui.d.ts.map +1 -0
  42. package/lib/index.d.mts +2072 -199
  43. package/lib/index.d.ts +31 -946
  44. package/lib/index.d.ts.map +1 -0
  45. package/lib/index.js +7176 -582
  46. package/lib/index.js.map +1 -1
  47. package/lib/index.mjs +6971 -525
  48. package/lib/index.mjs.map +1 -1
  49. package/lib/reducers/accounts.d.ts +49 -0
  50. package/lib/reducers/accounts.d.ts.map +1 -0
  51. package/lib/reducers/browser.d.ts +49 -0
  52. package/lib/reducers/browser.d.ts.map +1 -0
  53. package/lib/reducers/discovery.d.ts +22 -0
  54. package/lib/reducers/discovery.d.ts.map +1 -0
  55. package/lib/reducers/global.d.ts +35 -0
  56. package/lib/reducers/global.d.ts.map +1 -0
  57. package/lib/reducers/index.d.ts +9 -0
  58. package/lib/reducers/index.d.ts.map +1 -0
  59. package/lib/reducers/keyrings.d.ts +10 -0
  60. package/lib/reducers/keyrings.d.ts.map +1 -0
  61. package/lib/reducers/settings.d.ts +16 -0
  62. package/lib/reducers/settings.d.ts.map +1 -0
  63. package/lib/reducers/transactions.d.ts +61 -0
  64. package/lib/reducers/transactions.d.ts.map +1 -0
  65. package/lib/reducers/ui.d.ts +55 -0
  66. package/lib/reducers/ui.d.ts.map +1 -0
  67. package/lib/types/index.d.mts +7 -3
  68. package/lib/types/index.d.ts +3 -30
  69. package/lib/types/index.d.ts.map +1 -0
  70. package/lib/types/index.js +7 -1
  71. package/lib/types/index.js.map +1 -1
  72. package/lib/types/index.mjs +7 -2
  73. package/lib/types/index.mjs.map +1 -1
  74. package/lib/types/ui.d.ts +31 -0
  75. package/lib/types/ui.d.ts.map +1 -0
  76. package/lib/ui-hooks/index.d.ts +55 -0
  77. package/lib/ui-hooks/index.d.ts.map +1 -0
  78. package/lib/ui-hooks/useActionOverviewSectionLogic.d.ts +12 -0
  79. package/lib/ui-hooks/useActionOverviewSectionLogic.d.ts.map +1 -0
  80. package/lib/ui-hooks/useAddressTypeScreenLogic.d.ts +19 -0
  81. package/lib/ui-hooks/useAddressTypeScreenLogic.d.ts.map +1 -0
  82. package/lib/ui-hooks/useAlkanesBalanceCardLogic.d.ts +19 -0
  83. package/lib/ui-hooks/useAlkanesBalanceCardLogic.d.ts.map +1 -0
  84. package/lib/ui-hooks/useAlkanesCollectionListLogic.d.ts +11 -0
  85. package/lib/ui-hooks/useAlkanesCollectionListLogic.d.ts.map +1 -0
  86. package/lib/ui-hooks/useAlkanesListLogic.d.ts +14 -0
  87. package/lib/ui-hooks/useAlkanesListLogic.d.ts.map +1 -0
  88. package/lib/ui-hooks/useAlkanesNFTListLogic.d.ts +11 -0
  89. package/lib/ui-hooks/useAlkanesNFTListLogic.d.ts.map +1 -0
  90. package/lib/ui-hooks/useAlkanesNFTScreenLogic.d.ts +9 -0
  91. package/lib/ui-hooks/useAlkanesNFTScreenLogic.d.ts.map +1 -0
  92. package/lib/ui-hooks/useAlkanesTokenScreenLogic.d.ts +22 -0
  93. package/lib/ui-hooks/useAlkanesTokenScreenLogic.d.ts.map +1 -0
  94. package/lib/ui-hooks/useAmountInputLogic.d.ts +22 -0
  95. package/lib/ui-hooks/useAmountInputLogic.d.ts.map +1 -0
  96. package/lib/ui-hooks/useAnnouncementCardLogic.d.ts +11 -0
  97. package/lib/ui-hooks/useAnnouncementCardLogic.d.ts.map +1 -0
  98. package/lib/ui-hooks/useBRC20BalanceCardLogic.d.ts +26 -0
  99. package/lib/ui-hooks/useBRC20BalanceCardLogic.d.ts.map +1 -0
  100. package/lib/ui-hooks/useBRC20InscribeTransferLogic.d.ts +103 -0
  101. package/lib/ui-hooks/useBRC20InscribeTransferLogic.d.ts.map +1 -0
  102. package/lib/ui-hooks/useBRC20ListLogic.d.ts +14 -0
  103. package/lib/ui-hooks/useBRC20ListLogic.d.ts.map +1 -0
  104. package/lib/ui-hooks/useBRC20ProgListLogic.d.ts +14 -0
  105. package/lib/ui-hooks/useBRC20ProgListLogic.d.ts.map +1 -0
  106. package/lib/ui-hooks/useBRC20SendScreenLogic.d.ts +67 -0
  107. package/lib/ui-hooks/useBRC20SendScreenLogic.d.ts.map +1 -0
  108. package/lib/ui-hooks/useBRC20SingleStepScreenLogic.d.ts +36 -0
  109. package/lib/ui-hooks/useBRC20SingleStepScreenLogic.d.ts.map +1 -0
  110. package/lib/ui-hooks/useBRC20TokenScreenLogic.d.ts +75 -0
  111. package/lib/ui-hooks/useBRC20TokenScreenLogic.d.ts.map +1 -0
  112. package/lib/ui-hooks/useBalanceCardLogic.d.ts +26 -0
  113. package/lib/ui-hooks/useBalanceCardLogic.d.ts.map +1 -0
  114. package/lib/ui-hooks/useBtcDisplayLogic.d.ts +5 -0
  115. package/lib/ui-hooks/useBtcDisplayLogic.d.ts.map +1 -0
  116. package/lib/ui-hooks/useCAT20BalanceCardLogic.d.ts +20 -0
  117. package/lib/ui-hooks/useCAT20BalanceCardLogic.d.ts.map +1 -0
  118. package/lib/ui-hooks/useCAT20ListLogic.d.ts +14 -0
  119. package/lib/ui-hooks/useCAT20ListLogic.d.ts.map +1 -0
  120. package/lib/ui-hooks/useCAT20TokenScreenLogic.d.ts +19 -0
  121. package/lib/ui-hooks/useCAT20TokenScreenLogic.d.ts.map +1 -0
  122. package/lib/ui-hooks/useCAT721ListLogic.d.ts +11 -0
  123. package/lib/ui-hooks/useCAT721ListLogic.d.ts.map +1 -0
  124. package/lib/ui-hooks/useCAT721NFTScreenLogic.d.ts +9 -0
  125. package/lib/ui-hooks/useCAT721NFTScreenLogic.d.ts.map +1 -0
  126. package/lib/ui-hooks/useCreatePasswordScreenLogic.d.ts +27 -0
  127. package/lib/ui-hooks/useCreatePasswordScreenLogic.d.ts.map +1 -0
  128. package/lib/ui-hooks/useCreateWalletLogicImportWordsStep.d.ts +78 -0
  129. package/lib/ui-hooks/useCreateWalletLogicImportWordsStep.d.ts.map +1 -0
  130. package/lib/ui-hooks/useEditAccountNameScreenLogic.d.ts +18 -0
  131. package/lib/ui-hooks/useEditAccountNameScreenLogic.d.ts.map +1 -0
  132. package/lib/ui-hooks/useEditContactScreenLogic.d.ts +24 -0
  133. package/lib/ui-hooks/useEditContactScreenLogic.d.ts.map +1 -0
  134. package/lib/ui-hooks/useEditWalletNameScreenLogic.d.ts +15 -0
  135. package/lib/ui-hooks/useEditWalletNameScreenLogic.d.ts.map +1 -0
  136. package/lib/ui-hooks/useExportMnemonicsScreenLogic.d.ts +18 -0
  137. package/lib/ui-hooks/useExportMnemonicsScreenLogic.d.ts.map +1 -0
  138. package/lib/ui-hooks/useExportPrivateKeyScreenLogic.d.ts +17 -0
  139. package/lib/ui-hooks/useExportPrivateKeyScreenLogic.d.ts.map +1 -0
  140. package/lib/ui-hooks/useFeeRateBarLogic.d.ts +34 -0
  141. package/lib/ui-hooks/useFeeRateBarLogic.d.ts.map +1 -0
  142. package/lib/ui-hooks/useInfiniteList.d.ts +19 -0
  143. package/lib/ui-hooks/useInfiniteList.d.ts.map +1 -0
  144. package/lib/ui-hooks/useInscriptionListLogic.d.ts +11 -0
  145. package/lib/ui-hooks/useInscriptionListLogic.d.ts.map +1 -0
  146. package/lib/ui-hooks/useLockTimePageLogic.d.ts +11 -0
  147. package/lib/ui-hooks/useLockTimePageLogic.d.ts.map +1 -0
  148. package/lib/ui-hooks/useNotificationsLogic.d.ts +14 -0
  149. package/lib/ui-hooks/useNotificationsLogic.d.ts.map +1 -0
  150. package/lib/ui-hooks/useOrdinalsInscriptionScreenLogic.d.ts +27 -0
  151. package/lib/ui-hooks/useOrdinalsInscriptionScreenLogic.d.ts.map +1 -0
  152. package/lib/ui-hooks/useRunesBalanceCardLogic.d.ts +19 -0
  153. package/lib/ui-hooks/useRunesBalanceCardLogic.d.ts.map +1 -0
  154. package/lib/ui-hooks/useRunesListLogic.d.ts +14 -0
  155. package/lib/ui-hooks/useRunesListLogic.d.ts.map +1 -0
  156. package/lib/ui-hooks/useRunesTokenScreenLogic.d.ts +22 -0
  157. package/lib/ui-hooks/useRunesTokenScreenLogic.d.ts.map +1 -0
  158. package/lib/ui-hooks/useSecurityCardLogic.d.ts +1 -0
  159. package/lib/ui-hooks/useSecurityCardLogic.d.ts.map +1 -0
  160. package/lib/ui-hooks/useSendAlkanesNFTScreenLogic.d.ts +32 -0
  161. package/lib/ui-hooks/useSendAlkanesNFTScreenLogic.d.ts.map +1 -0
  162. package/lib/ui-hooks/useSendAlkanesScreenLogic.d.ts +37 -0
  163. package/lib/ui-hooks/useSendAlkanesScreenLogic.d.ts.map +1 -0
  164. package/lib/ui-hooks/useSendCAT20ScreenLogic.d.ts +44 -0
  165. package/lib/ui-hooks/useSendCAT20ScreenLogic.d.ts.map +1 -0
  166. package/lib/ui-hooks/useSendCAT721ScreenLogic.d.ts +40 -0
  167. package/lib/ui-hooks/useSendCAT721ScreenLogic.d.ts.map +1 -0
  168. package/lib/ui-hooks/useSendOrdinalsInscriptionScreenLogic.d.ts +22 -0
  169. package/lib/ui-hooks/useSendOrdinalsInscriptionScreenLogic.d.ts.map +1 -0
  170. package/lib/ui-hooks/useSendRunesScreenLogic.d.ts +30 -0
  171. package/lib/ui-hooks/useSendRunesScreenLogic.d.ts.map +1 -0
  172. package/lib/ui-hooks/useSettingsTabScreenLogic.d.ts +72 -0
  173. package/lib/ui-hooks/useSettingsTabScreenLogic.d.ts.map +1 -0
  174. package/lib/ui-hooks/useSignMessageLogic.d.ts +54 -0
  175. package/lib/ui-hooks/useSignMessageLogic.d.ts.map +1 -0
  176. package/lib/ui-hooks/useSignPsbtLogic.d.ts +82 -0
  177. package/lib/ui-hooks/useSignPsbtLogic.d.ts.map +1 -0
  178. package/lib/ui-hooks/useSplitOrdinalsInscriptionScreenLogic.d.ts +15 -0
  179. package/lib/ui-hooks/useSplitOrdinalsInscriptionScreenLogic.d.ts.map +1 -0
  180. package/lib/ui-hooks/useTxConfirmScreenLogic.d.ts +8 -0
  181. package/lib/ui-hooks/useTxConfirmScreenLogic.d.ts.map +1 -0
  182. package/lib/ui-hooks/useTxCreateScreenLogic.d.ts +28 -0
  183. package/lib/ui-hooks/useTxCreateScreenLogic.d.ts.map +1 -0
  184. package/lib/ui-hooks/useTxFailScreenLogic.d.ts +7 -0
  185. package/lib/ui-hooks/useTxFailScreenLogic.d.ts.map +1 -0
  186. package/lib/ui-hooks/useTxSuccessScreenLogic.d.ts +8 -0
  187. package/lib/ui-hooks/useTxSuccessScreenLogic.d.ts.map +1 -0
  188. package/lib/updater/accounts.d.ts +2 -0
  189. package/lib/updater/accounts.d.ts.map +1 -0
  190. package/lib/updater/index.d.ts +2 -0
  191. package/lib/updater/index.d.ts.map +1 -0
  192. package/lib/utils/bitcoin-utils.d.ts +7 -0
  193. package/lib/utils/bitcoin-utils.d.ts.map +1 -0
  194. package/lib/utils/eventBus.d.ts +12 -0
  195. package/lib/utils/eventBus.d.ts.map +1 -0
  196. package/lib/utils/password-utils.d.ts +12 -0
  197. package/lib/utils/password-utils.d.ts.map +1 -0
  198. package/lib/utils/ui-utils.d.ts +4 -0
  199. package/lib/utils/ui-utils.d.ts.map +1 -0
  200. package/package.json +19 -15
  201. package/src/.DS_Store +0 -0
  202. package/src/context/ApprovalContext.tsx +27 -0
  203. package/src/context/DeviceContext.tsx +36 -0
  204. package/src/context/I18nContext.tsx +14 -172
  205. package/src/context/NavigationContext.tsx +305 -0
  206. package/src/context/PriceContext.tsx +2 -2
  207. package/src/context/StorageContext.tsx +393 -0
  208. package/src/context/ToolsContext.tsx +50 -0
  209. package/src/context/WalletContext.tsx +170 -126
  210. package/src/context/index.ts +17 -2
  211. package/src/hooks/accounts.ts +11 -5
  212. package/src/hooks/browser.ts +11 -0
  213. package/src/hooks/global.ts +170 -7
  214. package/src/hooks/index.ts +1 -2
  215. package/src/hooks/settings.ts +38 -37
  216. package/src/hooks/transactions.ts +45 -151
  217. package/src/hooks/ui.ts +239 -35
  218. package/src/index.ts +33 -24
  219. package/src/reducers/accounts.ts +22 -2
  220. package/src/reducers/browser.ts +223 -0
  221. package/src/reducers/global.ts +67 -1
  222. package/src/reducers/index.ts +1 -0
  223. package/src/reducers/transactions.ts +0 -9
  224. package/src/reducers/ui.ts +144 -4
  225. package/src/types/index.ts +1 -1
  226. package/src/types/ui.ts +6 -1
  227. package/src/ui-hooks/index.ts +107 -0
  228. package/src/ui-hooks/useActionOverviewSectionLogic.ts +150 -0
  229. package/src/ui-hooks/useAddressTypeScreenLogic.ts +160 -0
  230. package/src/ui-hooks/useAlkanesBalanceCardLogic.ts +41 -0
  231. package/src/ui-hooks/useAlkanesCollectionListLogic.ts +68 -0
  232. package/src/ui-hooks/useAlkanesListLogic.ts +69 -0
  233. package/src/ui-hooks/useAlkanesNFTListLogic.ts +42 -0
  234. package/src/ui-hooks/useAlkanesNFTScreenLogic.ts +45 -0
  235. package/src/ui-hooks/useAlkanesTokenScreenLogic.ts +138 -0
  236. package/src/ui-hooks/useAmountInputLogic.ts +80 -0
  237. package/src/ui-hooks/useAnnouncementCardLogic.ts +91 -0
  238. package/src/ui-hooks/useBRC20BalanceCardLogic.ts +115 -0
  239. package/src/ui-hooks/useBRC20InscribeTransferLogic.ts +418 -0
  240. package/src/ui-hooks/useBRC20ListLogic.ts +79 -0
  241. package/src/ui-hooks/useBRC20ProgListLogic.ts +81 -0
  242. package/src/ui-hooks/useBRC20SendScreenLogic.ts +424 -0
  243. package/src/ui-hooks/useBRC20SingleStepScreenLogic.ts +208 -0
  244. package/src/ui-hooks/useBRC20TokenScreenLogic.ts +489 -0
  245. package/src/ui-hooks/useBalanceCardLogic.ts +164 -0
  246. package/src/ui-hooks/useBtcDisplayLogic.ts +16 -0
  247. package/src/ui-hooks/useCAT20BalanceCardLogic.ts +35 -0
  248. package/src/ui-hooks/useCAT20ListLogic.ts +83 -0
  249. package/src/ui-hooks/useCAT20TokenScreenLogic.ts +127 -0
  250. package/src/ui-hooks/useCAT721ListLogic.ts +68 -0
  251. package/src/ui-hooks/useCAT721NFTScreenLogic.ts +37 -0
  252. package/src/ui-hooks/useCreatePasswordScreenLogic.ts +92 -0
  253. package/src/ui-hooks/useCreateWalletLogicImportWordsStep.ts +299 -0
  254. package/src/ui-hooks/useEditAccountNameScreenLogic.ts +71 -0
  255. package/src/ui-hooks/useEditContactScreenLogic.ts +162 -0
  256. package/src/ui-hooks/useEditWalletNameScreenLogic.ts +60 -0
  257. package/src/ui-hooks/useExportMnemonicsScreenLogic.ts +75 -0
  258. package/src/ui-hooks/useExportPrivateKeyScreenLogic.ts +64 -0
  259. package/src/ui-hooks/useFeeRateBarLogic.ts +303 -0
  260. package/src/ui-hooks/useInfiniteList.ts +85 -0
  261. package/src/ui-hooks/useInscriptionListLogic.ts +68 -0
  262. package/src/ui-hooks/useLockTimePageLogic.ts +43 -0
  263. package/src/ui-hooks/useNotificationsLogic.ts +115 -0
  264. package/src/ui-hooks/useOrdinalsInscriptionScreenLogic.ts +130 -0
  265. package/src/ui-hooks/useRunesBalanceCardLogic.ts +44 -0
  266. package/src/ui-hooks/useRunesListLogic.ts +74 -0
  267. package/src/ui-hooks/useRunesTokenScreenLogic.ts +156 -0
  268. package/src/ui-hooks/useSecurityCardLogic.ts +0 -0
  269. package/src/ui-hooks/useSendAlkanesNFTScreenLogic.ts +160 -0
  270. package/src/ui-hooks/useSendAlkanesScreenLogic.ts +208 -0
  271. package/src/ui-hooks/useSendCAT20ScreenLogic.ts +297 -0
  272. package/src/ui-hooks/useSendCAT721ScreenLogic.ts +205 -0
  273. package/src/ui-hooks/useSendOrdinalsInscriptionScreenLogic.ts +152 -0
  274. package/src/ui-hooks/useSendRunesScreenLogic.ts +189 -0
  275. package/src/ui-hooks/useSettingsTabScreenLogic.ts +211 -0
  276. package/src/ui-hooks/useSignMessageLogic.ts +302 -0
  277. package/src/ui-hooks/useSignPsbtLogic.ts +517 -0
  278. package/src/ui-hooks/useSplitOrdinalsInscriptionScreenLogic.ts +109 -0
  279. package/src/ui-hooks/useTxConfirmScreenLogic.ts +47 -0
  280. package/src/ui-hooks/useTxCreateScreenLogic.ts +177 -0
  281. package/src/ui-hooks/useTxFailScreenLogic.ts +26 -0
  282. package/src/ui-hooks/useTxSuccessScreenLogic.ts +33 -0
  283. package/src/updater/accounts.ts +11 -11
  284. package/src/utils/bitcoin-utils.ts +17 -8
  285. package/src/utils/eventBus.ts +2 -1
  286. package/src/utils/password-utils.ts +78 -0
  287. package/src/utils/ui-utils.ts +28 -0
  288. package/src/hooks/approval.ts +0 -72
  289. package/src/hooks/i18n.ts +0 -53
  290. package/src/utils/i18n.ts +0 -41
@@ -0,0 +1,208 @@
1
+ import { bnUtils } from '@unisat/base-utils'
2
+ import { Inscription, SignedData, SignPsbtParams, ToSignData } from '@unisat/wallet-shared'
3
+ import { useEffect, useMemo, useRef, useState } from 'react'
4
+ import { useI18n, useNavigation, useTools, useWallet } from 'src/context'
5
+ import {
6
+ useCurrentAccount,
7
+ useFeeRateBar,
8
+ usePrepareSendAlkanesCallback,
9
+ usePushBitcoinTxCallback,
10
+ } from 'src/hooks'
11
+ import { isValidAddress } from 'src/utils/bitcoin-utils'
12
+
13
+ export enum SendAlkanesScreenStep {
14
+ CREATE_TX = 0,
15
+ SIGN_TX = 1,
16
+ }
17
+
18
+ export function useSendAlkanesScreenLogic() {
19
+ const nav = useNavigation()
20
+ const props = nav.getRouteState<'SendAlkanesScreen'>()
21
+
22
+ const { t } = useI18n()
23
+
24
+ const tokenBalance = props.tokenBalance
25
+
26
+ const tokenInfo = props.tokenInfo
27
+
28
+ const [inputAmount, setInputAmount] = useState('')
29
+ const [disabled, setDisabled] = useState(true)
30
+ const [toInfo, setToInfo] = useState<{
31
+ address: string
32
+ domain: string
33
+ inscription?: Inscription
34
+ }>({
35
+ address: '',
36
+ domain: '',
37
+ inscription: undefined,
38
+ })
39
+
40
+ const [availableBalance, setAvailableBalance] = useState(tokenBalance.amount)
41
+ const [error, setError] = useState('')
42
+ const [enableRBF, setEnableRBF] = useState(true)
43
+
44
+ const totalBalanceStr = useMemo(() => {
45
+ return bnUtils.toDecimalAmount(tokenBalance.amount, tokenBalance.divisibility)
46
+ }, [tokenBalance])
47
+ const availableBalanceStr = useMemo(() => {
48
+ return bnUtils.toDecimalAmount(availableBalance, tokenBalance?.divisibility)
49
+ }, [availableBalance, tokenBalance])
50
+
51
+ const currentAccount = useCurrentAccount()
52
+
53
+ const tools = useTools()
54
+
55
+ const { feeRate } = useFeeRateBar()
56
+
57
+ useEffect(() => {
58
+ const run = async () => {
59
+ const tokenSummary = await wallet.getAddressAlkanesTokenSummary(
60
+ currentAccount.address,
61
+ tokenBalance.alkaneid,
62
+ true
63
+ )
64
+ setAvailableBalance(tokenSummary.tokenBalance.available)
65
+ }
66
+
67
+ run()
68
+ }, [])
69
+
70
+ useEffect(() => {
71
+ setError('')
72
+ setDisabled(true)
73
+
74
+ if (!isValidAddress(toInfo.address)) {
75
+ return
76
+ }
77
+ if (!inputAmount) {
78
+ return
79
+ }
80
+
81
+ if (feeRate <= 0) {
82
+ return
83
+ }
84
+
85
+ const sendingAmount = bnUtils.fromDecimalAmount(inputAmount, tokenBalance.divisibility)
86
+
87
+ if (bnUtils.compareAmount(sendingAmount, '0') <= 0) {
88
+ return
89
+ }
90
+
91
+ if (bnUtils.compareAmount(sendingAmount, availableBalance)! > 0) {
92
+ setError(t('insufficient_balance'))
93
+ return
94
+ }
95
+
96
+ setDisabled(false)
97
+ }, [toInfo, inputAmount, availableBalance, feeRate])
98
+
99
+ const transferData = useRef<{
100
+ id: string
101
+ toSignData: ToSignData
102
+ }>({
103
+ id: '',
104
+ toSignData: null,
105
+ })
106
+
107
+ const [step, setStep] = useState(SendAlkanesScreenStep.CREATE_TX)
108
+
109
+ const wallet = useWallet()
110
+
111
+ useEffect(() => {
112
+ wallet.getEnableRBF().then(enableRBF => {
113
+ setEnableRBF(enableRBF)
114
+ })
115
+ }, [wallet])
116
+
117
+ const prepareSendAlkanes = usePrepareSendAlkanesCallback()
118
+ const pushBitcoinTx = usePushBitcoinTxCallback()
119
+
120
+ const onClickBack = () => {
121
+ nav.goBack()
122
+ }
123
+
124
+ const onClickNext = async () => {
125
+ tools.showLoading(true)
126
+ try {
127
+ const toSignData = await prepareSendAlkanes(
128
+ toInfo,
129
+ tokenBalance.alkaneid,
130
+ bnUtils.fromDecimalAmount(inputAmount, tokenBalance.divisibility),
131
+ feeRate,
132
+ 'ft',
133
+ enableRBF
134
+ )
135
+ if (toSignData) {
136
+ transferData.current.toSignData = toSignData
137
+ setStep(1)
138
+ }
139
+ } catch (e) {
140
+ setError((e as any).message)
141
+ } finally {
142
+ tools.showLoading(false)
143
+ }
144
+ }
145
+
146
+ const signPsbtParams: SignPsbtParams = {
147
+ data: {
148
+ toSignDatas: [transferData.current.toSignData],
149
+ },
150
+ }
151
+
152
+ const onSignPsbtHandleConfirm = async (signedDatas: SignedData[]) => {
153
+ tools.showLoading(true)
154
+ try {
155
+ const { success, txid, error } = await pushBitcoinTx(signedDatas[0].psbtHex)
156
+ if (success) {
157
+ nav.navigate('TxSuccessScreen', { txid })
158
+ } else {
159
+ throw new Error(error)
160
+ }
161
+ } catch (e) {
162
+ nav.navigate('TxFailScreen', { error: (e as any).message })
163
+ } finally {
164
+ tools.showLoading(false)
165
+ }
166
+ }
167
+
168
+ const onSignPsbtHandleCancel = () => {
169
+ setStep(SendAlkanesScreenStep.CREATE_TX)
170
+ }
171
+
172
+ const onSignPsbtHandleBack = () => {
173
+ setStep(SendAlkanesScreenStep.CREATE_TX)
174
+ }
175
+
176
+ const onEnableRBFChange = (value: boolean) => {
177
+ setEnableRBF(value)
178
+ wallet.setEnableRBF(value)
179
+ }
180
+
181
+ return {
182
+ step,
183
+ t,
184
+ tokenBalance,
185
+ tokenInfo,
186
+ toInfo,
187
+ totalBalanceStr,
188
+ availableBalanceStr,
189
+
190
+ inputAmount,
191
+ enableRBF,
192
+ disabled,
193
+ error,
194
+
195
+ // actions
196
+ setToInfo,
197
+ setInputAmount,
198
+ setEnableRBF: onEnableRBFChange,
199
+ onClickBack,
200
+ onClickNext,
201
+
202
+ // sign psbt actions
203
+ onSignPsbtHandleConfirm,
204
+ onSignPsbtHandleCancel,
205
+ onSignPsbtHandleBack,
206
+ signPsbtParams,
207
+ }
208
+ }
@@ -0,0 +1,297 @@
1
+ import { bnUtils } from '@unisat/base-utils'
2
+ import {
3
+ AddressCAT20UtxoSummary,
4
+ Inscription,
5
+ SignedData,
6
+ SignPsbtParams,
7
+ ToSignData,
8
+ } from '@unisat/wallet-shared'
9
+ import { AddressType } from '@unisat/wallet-types'
10
+ import BigNumber from 'bignumber.js'
11
+ import { useEffect, useMemo, useRef, useState } from 'react'
12
+ import { useI18n, useNavigation, useTools, useWallet } from 'src/context'
13
+ import { useCurrentAccount, useFeeRateBar, useNetworkType } from 'src/hooks'
14
+ import { getAddressType, isValidAddress } from 'src/utils/bitcoin-utils'
15
+
16
+ const MAX_TOKEN_INPUT = 4
17
+
18
+ export enum SendCAT20ScreenStep {
19
+ PREPARE = 0,
20
+ SIGN_COMMIT = 1,
21
+ WAITING = 2,
22
+ SIGN_REVEAL = 3,
23
+ }
24
+
25
+ export function useSendCAT20ScreenLogic() {
26
+ const nav = useNavigation()
27
+ const props = nav.getRouteState<'SendCAT20Screen'>()
28
+
29
+ const cat20Balance = props.cat20Balance
30
+
31
+ const cat20Info = props.cat20Info
32
+
33
+ const wallet = useWallet()
34
+
35
+ const { t } = useI18n()
36
+ const [inputAmount, setInputAmount] = useState('')
37
+ const [disabled, setDisabled] = useState(false)
38
+ const [toInfo, setToInfo] = useState<{
39
+ address: string
40
+ domain: string
41
+ inscription?: Inscription
42
+ }>({
43
+ address: '',
44
+ domain: '',
45
+ inscription: undefined,
46
+ })
47
+
48
+ const [tokenUtxoSummary, setTokenUtxoSummary] = useState<AddressCAT20UtxoSummary>({
49
+ totalUtxoCount: 0,
50
+ availableUtxoCount: 0,
51
+ availableTokenAmounts: [],
52
+ })
53
+ const [error, setError] = useState('')
54
+
55
+ const account = useCurrentAccount()
56
+
57
+ const networkType = useNetworkType()
58
+
59
+ const [showMergeBTCUTXOPopover, setShowMergeBTCUTXOPopover] = useState(false)
60
+ const tools = useTools()
61
+
62
+ useEffect(() => {
63
+ tools.showLoading(true)
64
+ wallet
65
+ .getAddressCAT20UtxoSummary(props.version, account.address, cat20Balance.tokenId)
66
+ .then(data => {
67
+ setTokenUtxoSummary(data)
68
+ })
69
+ .finally(() => {
70
+ tools.showLoading(false)
71
+ })
72
+ }, [])
73
+
74
+ const availableTokenAmount = useMemo(() => {
75
+ let amount = new BigNumber(0)
76
+ for (
77
+ let i = 0;
78
+ i < Math.min(tokenUtxoSummary.availableTokenAmounts.length, MAX_TOKEN_INPUT);
79
+ i++
80
+ ) {
81
+ amount = amount.plus(BigNumber(tokenUtxoSummary.availableTokenAmounts[i]))
82
+ }
83
+ return amount.toString()
84
+ }, [tokenUtxoSummary])
85
+
86
+ const { feeRate } = useFeeRateBar()
87
+
88
+ useEffect(() => {
89
+ setError('')
90
+ setDisabled(true)
91
+
92
+ if (!isValidAddress(toInfo.address)) {
93
+ return
94
+ }
95
+
96
+ const addressType = getAddressType(toInfo.address, networkType)
97
+ if (addressType !== AddressType.P2TR && addressType !== AddressType.P2WPKH) {
98
+ setError(t('the_recipient_must_be_p2tr_or_p2wpkh_address_type'))
99
+ return
100
+ }
101
+
102
+ if (!inputAmount) {
103
+ return
104
+ }
105
+
106
+ if (!feeRate) {
107
+ return
108
+ }
109
+
110
+ const amt = bnUtils.fromDecimalAmount(inputAmount, cat20Balance.decimals)
111
+ if (bnUtils.compareAmount(amt, '0') != 1) {
112
+ return
113
+ }
114
+
115
+ if (bnUtils.compareAmount(amt, availableTokenAmount) > 0) {
116
+ // insufficient balance
117
+ setError(t('insufficient_balance'))
118
+ return
119
+ }
120
+
121
+ setDisabled(false)
122
+ }, [toInfo, inputAmount, feeRate, availableTokenAmount])
123
+
124
+ const transferData = useRef<{
125
+ id: string
126
+ commitToSignData: ToSignData
127
+ revealToSignData: ToSignData
128
+ }>({
129
+ id: '',
130
+ commitToSignData: {
131
+ psbtHex: '',
132
+ toSignInputs: [],
133
+ autoFinalized: false,
134
+ },
135
+ revealToSignData: {
136
+ psbtHex: '',
137
+ toSignInputs: [],
138
+ autoFinalized: false,
139
+ },
140
+ })
141
+ const [step, setStep] = useState<SendCAT20ScreenStep>(SendCAT20ScreenStep.PREPARE)
142
+ const onClickNext = async () => {
143
+ tools.showLoading(true)
144
+ try {
145
+ const cat20Amount = bnUtils.fromDecimalAmount(inputAmount, cat20Balance.decimals)
146
+ const step1 = await wallet.transferCAT20Step1(
147
+ props.version,
148
+ toInfo.address,
149
+ cat20Balance.tokenId,
150
+ cat20Amount,
151
+ feeRate
152
+ )
153
+ if (step1) {
154
+ transferData.current.id = step1.id
155
+ transferData.current.commitToSignData = step1.toSignData
156
+ setStep(1)
157
+ }
158
+ } catch (e) {
159
+ const msg = (e as any).message
160
+ if (msg.includes('-307')) {
161
+ setShowMergeBTCUTXOPopover(true)
162
+ return
163
+ }
164
+ setError((e as any).message)
165
+ } finally {
166
+ tools.showLoading(false)
167
+ }
168
+ }
169
+
170
+ const onClickBack = () => {
171
+ if (step === SendCAT20ScreenStep.PREPARE) {
172
+ nav.goBack()
173
+ return
174
+ } else {
175
+ setStep(SendCAT20ScreenStep.PREPARE)
176
+ }
177
+ }
178
+
179
+ // sign commit psbt
180
+ const signCommitPsbtParams: SignPsbtParams = {
181
+ data: {
182
+ toSignDatas: [transferData.current.commitToSignData],
183
+ },
184
+ }
185
+
186
+ const onSignCommitPsbtHandleConfirm = async (signedDatas: SignedData[]) => {
187
+ try {
188
+ tools.showLoading(true)
189
+ const step2 = await wallet.transferCAT20Step2(
190
+ props.version,
191
+ transferData.current.id,
192
+ signedDatas[0].psbtHex
193
+ )
194
+
195
+ transferData.current.revealToSignData = step2.toSignData
196
+
197
+ setStep(SendCAT20ScreenStep.WAITING)
198
+ setTimeout(() => {
199
+ setStep(SendCAT20ScreenStep.SIGN_REVEAL)
200
+ }, 100)
201
+ } catch (e) {
202
+ console.log(e)
203
+ } finally {
204
+ tools.showLoading(false)
205
+ }
206
+ }
207
+
208
+ const onSignCommitPsbtHandleCancel = () => {
209
+ setStep(SendCAT20ScreenStep.PREPARE)
210
+ }
211
+
212
+ const onSignCommitPsbtHandleBack = () => {
213
+ setStep(SendCAT20ScreenStep.PREPARE)
214
+ }
215
+
216
+ // sign reveal psbt
217
+ const signRevealPsbtParams: SignPsbtParams = {
218
+ data: {
219
+ toSignDatas: [transferData.current.revealToSignData],
220
+ },
221
+ }
222
+
223
+ const onSignRevealPsbtHandleConfirm = async (signedDatas: SignedData[]) => {
224
+ tools.showLoading(true)
225
+ try {
226
+ const step3 = await wallet.transferCAT20Step3(
227
+ props.version,
228
+ transferData.current.id,
229
+ signedDatas[0].psbtHex
230
+ )
231
+ nav.navigate('TxSuccessScreen', { txid: step3.txid })
232
+ } catch (e) {
233
+ // tools.toastError((e as any).message);
234
+ nav.navigate('TxFailScreen', { error: (e as any).message })
235
+ } finally {
236
+ tools.showLoading(false)
237
+ }
238
+ }
239
+
240
+ const onSignRevealPsbtHandleCancel = () => {
241
+ setStep(SendCAT20ScreenStep.PREPARE)
242
+ }
243
+
244
+ const onSignRevealPsbtHandleBack = () => {
245
+ setStep(SendCAT20ScreenStep.PREPARE)
246
+ }
247
+
248
+ // merge UTXO
249
+ const shouldShowMerge = availableTokenAmount !== cat20Balance.amount
250
+
251
+ const onClickMergeUTXO = () => {
252
+ nav.navigate('MergeCAT20Screen', {
253
+ version: props.version,
254
+ cat20Balance: cat20Balance,
255
+ cat20Info: cat20Info,
256
+ })
257
+ }
258
+
259
+ return {
260
+ // info
261
+ cat20Info,
262
+ cat20Balance,
263
+ availableTokenAmount,
264
+ toInfo,
265
+ setToInfo,
266
+ inputAmount,
267
+ setInputAmount,
268
+ error,
269
+ disabled,
270
+ step,
271
+ showMergeBTCUTXOPopover,
272
+ setShowMergeBTCUTXOPopover,
273
+ shouldShowMerge,
274
+
275
+ // actions
276
+ onClickNext,
277
+ onClickBack,
278
+
279
+ // sign commit psbt actions
280
+ onSignCommitPsbtHandleConfirm,
281
+ onSignCommitPsbtHandleCancel,
282
+ onSignCommitPsbtHandleBack,
283
+ signCommitPsbtParams,
284
+
285
+ // sign reveal psbt actions
286
+ onSignRevealPsbtHandleConfirm,
287
+ onSignRevealPsbtHandleCancel,
288
+ onSignRevealPsbtHandleBack,
289
+ signRevealPsbtParams,
290
+
291
+ // merge UTXO action
292
+ onClickMergeUTXO,
293
+
294
+ // tools
295
+ t,
296
+ }
297
+ }
@@ -0,0 +1,205 @@
1
+ import { Inscription, SignedData, SignPsbtParams, ToSignData } from '@unisat/wallet-shared'
2
+ import { AddressType } from '@unisat/wallet-types'
3
+ import { useEffect, useRef, useState } from 'react'
4
+ import { useI18n, useNavigation, useTools, useWallet } from 'src/context'
5
+ import { useFeeRateBar, useNetworkType } from 'src/hooks'
6
+ import { getAddressType, isValidAddress } from 'src/utils/bitcoin-utils'
7
+
8
+ export enum SendCAT721ScreenStep {
9
+ PREPARE = 0,
10
+ SIGN_COMMIT = 1,
11
+ WAITING = 2,
12
+ SIGN_REVEAL = 3,
13
+ }
14
+ export function useSendCAT721ScreenLogic() {
15
+ const nav = useNavigation()
16
+ const { version, localId, collectionInfo } = nav.getRouteState<'SendCAT721Screen'>()
17
+
18
+ const wallet = useWallet()
19
+ const { t } = useI18n()
20
+
21
+ const [disabled, setDisabled] = useState(false)
22
+ const [toInfo, setToInfo] = useState<{
23
+ address: string
24
+ domain: string
25
+ inscription?: Inscription
26
+ }>({
27
+ address: '',
28
+ domain: '',
29
+ inscription: undefined,
30
+ })
31
+
32
+ const [error, setError] = useState('')
33
+
34
+ const networkType = useNetworkType()
35
+
36
+ const [showMergeBTCUTXOPopover, setShowMergeBTCUTXOPopover] = useState(false)
37
+ const tools = useTools()
38
+
39
+ const { feeRate } = useFeeRateBar()
40
+
41
+ useEffect(() => {
42
+ setError('')
43
+ setDisabled(true)
44
+
45
+ if (!isValidAddress(toInfo.address)) {
46
+ return
47
+ }
48
+
49
+ const addressType = getAddressType(toInfo.address, networkType)
50
+ if (addressType !== AddressType.P2TR && addressType !== AddressType.P2WPKH) {
51
+ setError(t('the_recipient_must_be_p2tr_or_p2wpkh_address_type'))
52
+ return
53
+ }
54
+
55
+ setDisabled(false)
56
+ }, [toInfo])
57
+
58
+ const transferData = useRef<{
59
+ id: string
60
+ commitToSignData: ToSignData
61
+ revealToSignData: ToSignData
62
+ }>({
63
+ id: '',
64
+ commitToSignData: {} as ToSignData,
65
+ revealToSignData: {} as ToSignData,
66
+ })
67
+ const [step, setStep] = useState<SendCAT721ScreenStep>(SendCAT721ScreenStep.PREPARE)
68
+ const onClickNext = async () => {
69
+ tools.showLoading(true)
70
+ try {
71
+ const step1 = await wallet.transferCAT721Step1(
72
+ version,
73
+ toInfo.address,
74
+ collectionInfo.collectionId,
75
+ localId,
76
+ feeRate
77
+ )
78
+ if (step1) {
79
+ transferData.current.id = step1.id
80
+ transferData.current.commitToSignData = step1.toSignData
81
+ setStep(1)
82
+ }
83
+ } catch (e) {
84
+ const msg = (e as any).message
85
+ if (msg.includes('-307')) {
86
+ setShowMergeBTCUTXOPopover(true)
87
+ return
88
+ }
89
+ setError((e as any).message)
90
+ } finally {
91
+ tools.showLoading(false)
92
+ }
93
+ }
94
+
95
+ const onClickBack = () => {
96
+ if (step === SendCAT721ScreenStep.PREPARE) {
97
+ nav.goBack()
98
+ return
99
+ } else {
100
+ setStep(SendCAT721ScreenStep.PREPARE)
101
+ }
102
+ }
103
+
104
+ // sign commit psbt
105
+ const signCommitPsbtParams: SignPsbtParams = {
106
+ data: {
107
+ toSignDatas: [transferData.current.commitToSignData],
108
+ },
109
+ }
110
+
111
+ const onSignCommitPsbtHandleConfirm = async (signedDatas: SignedData[]) => {
112
+ try {
113
+ tools.showLoading(true)
114
+ const step2 = await wallet.transferCAT721Step2(
115
+ version,
116
+ transferData.current.id,
117
+ signedDatas[0].psbtHex
118
+ )
119
+
120
+ transferData.current.revealToSignData = step2.toSignData
121
+
122
+ setStep(SendCAT721ScreenStep.WAITING)
123
+ setTimeout(() => {
124
+ setStep(SendCAT721ScreenStep.SIGN_REVEAL)
125
+ }, 100)
126
+ } catch (e) {
127
+ console.log(e)
128
+ } finally {
129
+ tools.showLoading(false)
130
+ }
131
+ }
132
+
133
+ const onSignCommitPsbtHandleCancel = () => {
134
+ setStep(SendCAT721ScreenStep.PREPARE)
135
+ }
136
+
137
+ const onSignCommitPsbtHandleBack = () => {
138
+ setStep(SendCAT721ScreenStep.PREPARE)
139
+ }
140
+
141
+ // sign reveal psbt
142
+ const signRevealPsbtParams: SignPsbtParams = {
143
+ data: {
144
+ toSignDatas: [transferData.current.revealToSignData],
145
+ },
146
+ }
147
+
148
+ const onSignRevealPsbtHandleConfirm = async (signedDatas: SignedData[]) => {
149
+ tools.showLoading(true)
150
+ try {
151
+ const step3 = await wallet.transferCAT721Step3(
152
+ version,
153
+ transferData.current.id,
154
+ signedDatas[0].psbtHex
155
+ )
156
+ nav.navigate('TxSuccessScreen', { txid: step3.txid })
157
+ } catch (e) {
158
+ // tools.toastError((e as any).message);
159
+ nav.navigate('TxFailScreen', { error: (e as any).message })
160
+ } finally {
161
+ tools.showLoading(false)
162
+ }
163
+ }
164
+
165
+ const onSignRevealPsbtHandleCancel = () => {
166
+ setStep(SendCAT721ScreenStep.PREPARE)
167
+ }
168
+
169
+ const onSignRevealPsbtHandleBack = () => {
170
+ setStep(SendCAT721ScreenStep.PREPARE)
171
+ }
172
+
173
+ return {
174
+ // info
175
+ version,
176
+ localId,
177
+ collectionInfo,
178
+ toInfo,
179
+ setToInfo,
180
+ error,
181
+ disabled,
182
+ step,
183
+ showMergeBTCUTXOPopover,
184
+ setShowMergeBTCUTXOPopover,
185
+
186
+ // actions
187
+ onClickNext,
188
+ onClickBack,
189
+
190
+ // sign commit psbt actions
191
+ onSignCommitPsbtHandleConfirm,
192
+ onSignCommitPsbtHandleCancel,
193
+ onSignCommitPsbtHandleBack,
194
+ signCommitPsbtParams,
195
+
196
+ // sign reveal psbt actions
197
+ onSignRevealPsbtHandleConfirm,
198
+ onSignRevealPsbtHandleCancel,
199
+ onSignRevealPsbtHandleBack,
200
+ signRevealPsbtParams,
201
+
202
+ // tools
203
+ t,
204
+ }
205
+ }