@unisat/wallet-state 1.0.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.
@@ -0,0 +1,282 @@
1
+ import {
2
+ Account,
3
+ AddressSummary,
4
+ AppSummary,
5
+ BitcoinBalanceV2,
6
+ Inscription,
7
+ InscriptionSummary,
8
+ TxHistoryItem,
9
+ } from '@/shared/types'
10
+ import { createSlice } from '@reduxjs/toolkit'
11
+
12
+ import { updateVersion } from '../global/actions'
13
+
14
+ export interface AccountsState {
15
+ accounts: Account[]
16
+ current: Account
17
+ loading: boolean
18
+ balanceMap: {
19
+ [key: string]: {
20
+ amount: string
21
+ btc_amount: string
22
+ confirm_btc_amount: string
23
+ pending_btc_amount: string
24
+ inscription_amount: string
25
+ expired: boolean
26
+ }
27
+ }
28
+ balanceV2Map: {
29
+ [key: string]: BitcoinBalanceV2
30
+ }
31
+ historyMap: {
32
+ [key: string]: {
33
+ list: TxHistoryItem[]
34
+ expired: boolean
35
+ }
36
+ }
37
+ inscriptionsMap: {
38
+ [key: string]: {
39
+ list: Inscription[]
40
+ expired: boolean
41
+ }
42
+ }
43
+ appSummary: AppSummary
44
+ inscriptionSummary: InscriptionSummary
45
+ addressSummary: AddressSummary
46
+ }
47
+
48
+ const initialAccount = {
49
+ type: '',
50
+ address: '',
51
+ brandName: '',
52
+ alianName: '',
53
+ displayBrandName: '',
54
+ index: 0,
55
+ balance: 0,
56
+ pubkey: '',
57
+ key: '',
58
+ flag: 0,
59
+ }
60
+
61
+ export const initialState: AccountsState = {
62
+ accounts: [],
63
+ current: initialAccount,
64
+ loading: false,
65
+ balanceMap: {},
66
+ balanceV2Map: {},
67
+ historyMap: {},
68
+ inscriptionsMap: {},
69
+ appSummary: {
70
+ apps: [],
71
+ },
72
+ inscriptionSummary: {
73
+ mintedList: [],
74
+ },
75
+ addressSummary: {
76
+ totalSatoshis: 0,
77
+ btcSatoshis: 0,
78
+ assetSatoshis: 0,
79
+ inscriptionCount: 0,
80
+ brc20Count: 0,
81
+ brc20Count5Byte: 0,
82
+ brc20Count6Byte: 0,
83
+ loading: true,
84
+ address: '',
85
+ runesCount: 0,
86
+ },
87
+ }
88
+
89
+ const slice = createSlice({
90
+ name: 'accounts',
91
+ initialState,
92
+ reducers: {
93
+ pendingLogin(state) {
94
+ state.loading = true
95
+ },
96
+ setCurrent(state, action: { payload: Account }) {
97
+ const { payload } = action
98
+ state.current = payload || initialAccount
99
+ },
100
+ setAccounts(state, action: { payload: Account[] }) {
101
+ const { payload } = action
102
+ state.accounts = payload
103
+ },
104
+ setBalance(
105
+ state,
106
+ action: {
107
+ payload: {
108
+ address: string
109
+ amount: string
110
+ btc_amount: string
111
+ inscription_amount: string
112
+ confirm_btc_amount: string
113
+ pending_btc_amount: string
114
+ }
115
+ }
116
+ ) {
117
+ const {
118
+ payload: {
119
+ address,
120
+ amount,
121
+ btc_amount,
122
+ inscription_amount,
123
+ confirm_btc_amount,
124
+ pending_btc_amount,
125
+ },
126
+ } = action
127
+ state.balanceMap[address] = state.balanceMap[address] || {
128
+ amount: '0',
129
+ btc_amount: '0',
130
+ inscription_amount: '0',
131
+ confirm_btc_amount: '0',
132
+ pending_btc_amount: '0',
133
+ expired: true,
134
+ }
135
+ state.balanceMap[address].amount = amount
136
+ state.balanceMap[address].btc_amount = btc_amount
137
+ state.balanceMap[address].inscription_amount = inscription_amount
138
+ state.balanceMap[address].confirm_btc_amount = confirm_btc_amount
139
+ state.balanceMap[address].pending_btc_amount = pending_btc_amount
140
+ state.balanceMap[address].expired = false
141
+ },
142
+ setBalanceV2(
143
+ state,
144
+ action: {
145
+ payload: {
146
+ address: string
147
+ balance: BitcoinBalanceV2
148
+ }
149
+ }
150
+ ) {
151
+ const {
152
+ payload: {
153
+ balance: { availableBalance, unavailableBalance, totalBalance },
154
+ address,
155
+ },
156
+ } = action
157
+ state.balanceV2Map[address] = state.balanceV2Map[address] || {
158
+ availableBalance: 0,
159
+ unavailableBalance: 0,
160
+ totalBalance: 0,
161
+ }
162
+ state.balanceV2Map[address].availableBalance = availableBalance
163
+ state.balanceV2Map[address].unavailableBalance = unavailableBalance
164
+ state.balanceV2Map[address].totalBalance = totalBalance
165
+ },
166
+ setAddressSummary(state, action: { payload: any }) {
167
+ state.addressSummary = action.payload
168
+ },
169
+ expireBalance(state) {
170
+ const balance = state.balanceMap[state.current.address]
171
+ if (balance) {
172
+ balance.expired = true
173
+ }
174
+ },
175
+ setHistory(state, action: { payload: { address: string; list: TxHistoryItem[] } }) {
176
+ const {
177
+ payload: { address, list },
178
+ } = action
179
+ state.historyMap[address] = state.historyMap[address] || {
180
+ list: [],
181
+ expired: true,
182
+ }
183
+ state.historyMap[address].list = list
184
+ state.historyMap[address].expired = false
185
+ },
186
+ expireHistory(state) {
187
+ const history = state.historyMap[state.current.address]
188
+ if (history) {
189
+ history.expired = true
190
+ }
191
+ },
192
+ setInscriptions(state, action: { payload: { address: string; list: Inscription[] } }) {
193
+ const {
194
+ payload: { address, list },
195
+ } = action
196
+ state.inscriptionsMap[address] = state.inscriptionsMap[address] || {
197
+ list: [],
198
+ expired: true,
199
+ }
200
+ state.inscriptionsMap[address].list = list
201
+ state.inscriptionsMap[address].expired = false
202
+ },
203
+ expireInscriptions(state) {
204
+ const inscriptions = state.inscriptionsMap[state.current.address]
205
+ if (inscriptions) {
206
+ inscriptions.expired = true
207
+ }
208
+ },
209
+ setCurrentAccountName(state, action: { payload: string }) {
210
+ const { payload } = action
211
+ state.current.alianName = payload
212
+ const account = state.accounts.find(v => v.address === state.current.address)
213
+ if (account) {
214
+ account.alianName = payload
215
+ }
216
+ },
217
+ setCurrentAddressFlag(state, action: { payload: number }) {
218
+ const { payload } = action
219
+ state.current.flag = payload
220
+ const account = state.accounts.find(v => v.address === state.current.address)
221
+ if (account) {
222
+ account.flag = payload
223
+ }
224
+ },
225
+ setInscriptionSummary(state, action: { payload: InscriptionSummary }) {
226
+ const { payload } = action
227
+ state.inscriptionSummary = payload
228
+ },
229
+ setAppSummary(state, action: { payload: AppSummary }) {
230
+ const { payload } = action
231
+ state.appSummary = payload
232
+ },
233
+ rejectLogin(state) {
234
+ state.loading = false
235
+ },
236
+ reset(state) {
237
+ return initialState
238
+ },
239
+ updateAccountName(
240
+ state,
241
+ action: {
242
+ payload: Account
243
+ }
244
+ ) {
245
+ const account = action.payload
246
+ if (state.current.key === account.key) {
247
+ state.current.alianName = account.alianName
248
+ }
249
+ state.accounts.forEach(v => {
250
+ if (v.key === account.key) {
251
+ v.alianName = account.alianName
252
+ }
253
+ })
254
+ },
255
+ },
256
+ extraReducers: builder => {
257
+ builder.addCase(updateVersion, state => {
258
+ // todo
259
+ if (!state.addressSummary) {
260
+ state.addressSummary = {
261
+ totalSatoshis: 0,
262
+ btcSatoshis: 0,
263
+ assetSatoshis: 0,
264
+ inscriptionCount: 0,
265
+ brc20Count: 0,
266
+ brc20Count5Byte: 0,
267
+ brc20Count6Byte: 0,
268
+ loading: true,
269
+ address: '',
270
+ runesCount: 0,
271
+ }
272
+ }
273
+
274
+ if (!state.balanceV2Map) {
275
+ state.balanceV2Map = {}
276
+ }
277
+ })
278
+ },
279
+ })
280
+
281
+ export const accountActions = slice.actions
282
+ export default slice.reducer
@@ -0,0 +1,52 @@
1
+ import { createSlice } from '@reduxjs/toolkit'
2
+
3
+ import { updateVersion } from '../global/actions'
4
+
5
+ export type TabOption = 'home' | 'discover' | 'settings'
6
+
7
+ export interface GlobalState {
8
+ tab: TabOption
9
+ isUnlocked: boolean
10
+ isReady: boolean
11
+ isBooted: boolean
12
+ }
13
+
14
+ export const initialState: GlobalState = {
15
+ tab: 'home',
16
+ isUnlocked: false,
17
+ isReady: false,
18
+ isBooted: false,
19
+ }
20
+
21
+ const slice = createSlice({
22
+ name: 'global',
23
+ initialState,
24
+ reducers: {
25
+ reset(state) {
26
+ return initialState
27
+ },
28
+ update(
29
+ state,
30
+ action: {
31
+ payload: {
32
+ tab?: TabOption
33
+ isUnlocked?: boolean
34
+ isReady?: boolean
35
+ isBooted?: boolean
36
+ }
37
+ }
38
+ ) {
39
+ const { payload } = action
40
+ state = Object.assign({}, state, payload)
41
+ return state
42
+ },
43
+ },
44
+ extraReducers: builder => {
45
+ builder.addCase(updateVersion, state => {
46
+ // todo
47
+ })
48
+ },
49
+ })
50
+
51
+ export const globalActions = slice.actions
52
+ export default slice.reducer
@@ -0,0 +1,10 @@
1
+ // Redux slices exports
2
+ // Individual slice exports will be added as we migrate existing reducers
3
+
4
+ // Export slice creators that will be populated from existing code
5
+ export * from './accounts';
6
+ export * from './transactions';
7
+ export * from './settings';
8
+ export * from './global';
9
+ export * from './keyrings';
10
+ export * from './ui';
@@ -0,0 +1,81 @@
1
+ import { Account, AddressType, WalletKeyring } from '@/shared/types'
2
+ import { createSlice } from '@reduxjs/toolkit'
3
+
4
+ import { updateVersion } from '../global/actions'
5
+
6
+ export interface KeyringsState {
7
+ keyrings: WalletKeyring[]
8
+ current: WalletKeyring
9
+ }
10
+
11
+ const initialKeyring: WalletKeyring = {
12
+ key: '',
13
+ index: 0,
14
+ type: '',
15
+ addressType: AddressType.P2TR,
16
+ accounts: [],
17
+ alianName: '',
18
+ hdPath: '',
19
+ }
20
+
21
+ export const initialState: KeyringsState = {
22
+ keyrings: [],
23
+ current: initialKeyring,
24
+ }
25
+
26
+ const slice = createSlice({
27
+ name: 'keyrings',
28
+ initialState,
29
+ reducers: {
30
+ setCurrent(state, action: { payload: WalletKeyring }) {
31
+ const { payload } = action
32
+ state.current = payload || initialKeyring
33
+ },
34
+ setKeyrings(state, action: { payload: WalletKeyring[] }) {
35
+ const { payload } = action
36
+ state.keyrings = payload
37
+ },
38
+
39
+ reset(state) {
40
+ return initialState
41
+ },
42
+
43
+ updateKeyringName(state, action: { payload: WalletKeyring }) {
44
+ const keyring = action.payload
45
+ if (state.current.key === keyring.key) {
46
+ state.current.alianName = keyring.alianName
47
+ }
48
+ state.keyrings.forEach(v => {
49
+ if (v.key === keyring.key) {
50
+ v.alianName = keyring.alianName
51
+ }
52
+ })
53
+ },
54
+
55
+ updateAccountName(state, action: { payload: Account }) {
56
+ const account = action.payload
57
+
58
+ state.current.accounts.forEach(v => {
59
+ if (v.key === account.key) {
60
+ v.alianName = account.alianName
61
+ }
62
+ })
63
+
64
+ state.keyrings.forEach(v => {
65
+ v.accounts.forEach(w => {
66
+ if (w.key === account.key) {
67
+ w.alianName = account.alianName
68
+ }
69
+ })
70
+ })
71
+ },
72
+ },
73
+ extraReducers: builder => {
74
+ builder.addCase(updateVersion, state => {
75
+ // todo
76
+ })
77
+ },
78
+ })
79
+
80
+ export const keyringsActions = slice.actions
81
+ export default slice.reducer
@@ -0,0 +1,71 @@
1
+ import { ChainType, DEFAULT_LOCKTIME_ID } from '@/shared/constant'
2
+ import { NetworkType, WalletConfig } from '@/shared/types'
3
+ import { createSlice } from '@reduxjs/toolkit'
4
+
5
+ import { updateVersion } from '../global/actions'
6
+
7
+ export interface SettingsState {
8
+ locale: string
9
+ networkType: NetworkType
10
+ chainType: ChainType
11
+ walletConfig: WalletConfig
12
+ skippedVersion: string
13
+ autoLockTimeId: number
14
+ developerMode: boolean
15
+ }
16
+
17
+ export const initialState: SettingsState = {
18
+ locale: 'English',
19
+ networkType: NetworkType.MAINNET,
20
+ chainType: ChainType.BITCOIN_MAINNET,
21
+ walletConfig: {
22
+ version: '',
23
+ moonPayEnabled: true,
24
+ statusMessage: '',
25
+ endpoint: '',
26
+ chainTip: '',
27
+ disableUtxoTools: true,
28
+ },
29
+ skippedVersion: '',
30
+ autoLockTimeId: DEFAULT_LOCKTIME_ID,
31
+ developerMode: false,
32
+ }
33
+
34
+ const slice = createSlice({
35
+ name: 'settings',
36
+ initialState,
37
+ reducers: {
38
+ reset(state) {
39
+ return initialState
40
+ },
41
+ updateSettings(
42
+ state,
43
+ action: {
44
+ payload: {
45
+ locale?: string
46
+ networkType?: NetworkType
47
+ walletConfig?: WalletConfig
48
+ skippedVersion?: string
49
+ chainType?: ChainType
50
+ autoLockTimeId?: number
51
+ developerMode?: boolean
52
+ }
53
+ }
54
+ ) {
55
+ const { payload } = action
56
+ state = Object.assign({}, state, payload)
57
+ return state
58
+ },
59
+ },
60
+ extraReducers: builder => {
61
+ builder.addCase(updateVersion, state => {
62
+ // todo
63
+ if (!state.networkType) {
64
+ state.networkType = NetworkType.MAINNET
65
+ }
66
+ })
67
+ },
68
+ })
69
+
70
+ export const settingsActions = slice.actions
71
+ export default slice.reducer