@unisat/wallet-state 1.0.0 → 1.0.2

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 (40) hide show
  1. package/lib/index.d.mts +934 -0
  2. package/lib/index.d.ts +934 -0
  3. package/lib/index.js +2225 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/index.mjs +2124 -0
  6. package/lib/index.mjs.map +1 -0
  7. package/package.json +19 -5
  8. package/src/actions/global.ts +5 -0
  9. package/src/context/I18nContext.tsx +191 -0
  10. package/src/context/PriceContext.tsx +81 -0
  11. package/src/context/WalletContext.tsx +703 -0
  12. package/src/context/index.ts +3 -0
  13. package/src/hooks/accounts.ts +23 -21
  14. package/src/hooks/approval.ts +72 -0
  15. package/src/hooks/base.ts +6 -0
  16. package/src/hooks/discovery.ts +29 -0
  17. package/src/hooks/global.ts +129 -5
  18. package/src/hooks/i18n.ts +53 -0
  19. package/src/hooks/index.ts +9 -15
  20. package/src/hooks/keyrings.ts +14 -5
  21. package/src/hooks/settings.ts +318 -5
  22. package/src/hooks/transactions.ts +44 -38
  23. package/src/hooks/ui.ts +133 -5
  24. package/src/index.ts +42 -5
  25. package/src/{slices → reducers}/accounts.ts +12 -12
  26. package/src/reducers/discovery.ts +73 -0
  27. package/src/reducers/global.ts +51 -0
  28. package/src/{slices → reducers}/keyrings.ts +7 -6
  29. package/src/{slices → reducers}/settings.ts +5 -5
  30. package/src/{slices → reducers}/transactions.ts +4 -5
  31. package/src/{slices → reducers}/ui.ts +4 -5
  32. package/src/updater/accounts.ts +107 -0
  33. package/src/updater/index.ts +1 -0
  34. package/src/utils/bitcoin-utils.ts +81 -0
  35. package/src/utils/eventBus.ts +49 -0
  36. package/src/utils/i18n.ts +41 -0
  37. package/src/slices/global.ts +0 -52
  38. package/src/slices/index.ts +0 -10
  39. package/src/store/index.ts +0 -43
  40. package/src/types/index.ts +0 -37
@@ -0,0 +1,81 @@
1
+ import { createContext, ReactNode, useCallback, useContext, useEffect, useState } from 'react'
2
+
3
+ import { CoinPrice } from '@unisat/wallet-shared'
4
+ import { useWallet } from './WalletContext'
5
+
6
+ import { useChain, useChainType } from '../hooks/settings'
7
+
8
+ interface PriceContextType {
9
+ isLoadingCoinPrice: boolean
10
+ coinPrice: CoinPrice
11
+ refreshCoinPrice: () => void
12
+ }
13
+
14
+ const PriceContext = createContext<PriceContextType>({} as PriceContextType)
15
+
16
+ export function usePrice() {
17
+ const context = useContext(PriceContext)
18
+ if (!context) {
19
+ throw Error('Feature flag hooks can only be used by children of BridgeProvider.')
20
+ } else {
21
+ return context
22
+ }
23
+ }
24
+
25
+ let isRequestingCoinPrice = false
26
+ let refreshCoinPriceTime = 0
27
+
28
+ export function PriceProvider({ children }: { children: ReactNode }) {
29
+ const wallet = useWallet()
30
+ const chainType = useChainType()
31
+ const chain = useChain()
32
+ const [isLoadingCoinPrice, setIsLoadingCoinPrice] = useState(false)
33
+ const [coinPrice, setCoinPrice] = useState<CoinPrice>({
34
+ btc: 0,
35
+ fb: 0,
36
+ })
37
+
38
+ const refreshCoinPrice = useCallback(() => {
39
+ if (chain.showPrice === false) {
40
+ return
41
+ }
42
+ if (isRequestingCoinPrice) {
43
+ return
44
+ }
45
+ // 30s cache
46
+ if (Date.now() - refreshCoinPriceTime < 30 * 1000) {
47
+ return
48
+ }
49
+ isRequestingCoinPrice = true
50
+ setIsLoadingCoinPrice(true)
51
+
52
+ wallet
53
+ .getCoinPrice()
54
+ .then(res => {
55
+ refreshCoinPriceTime = Date.now()
56
+ setCoinPrice(res)
57
+ })
58
+ .catch(e => {
59
+ setCoinPrice({
60
+ btc: 0,
61
+ fb: 0,
62
+ })
63
+ })
64
+ .finally(() => {
65
+ setIsLoadingCoinPrice(false)
66
+ isRequestingCoinPrice = false
67
+ })
68
+ }, [chainType, chain])
69
+
70
+ useEffect(() => {
71
+ refreshCoinPrice()
72
+ }, [refreshCoinPrice])
73
+
74
+ const value = {
75
+ isLoadingCoinPrice,
76
+ coinPrice,
77
+ refreshCoinPrice,
78
+ }
79
+
80
+ return <PriceContext.Provider value={value}>{children}</PriceContext.Provider>
81
+ }