@spacesops/wdk-react-native-core 1.0.0-beta.72 → 1.0.0-beta.73
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.
|
@@ -6,11 +6,13 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { useQuery } from '@tanstack/react-query';
|
|
8
8
|
import { useShallow } from 'zustand/react/shallow';
|
|
9
|
+
import { AddressService } from '../services/addressService';
|
|
9
10
|
import { TransactionService } from '../services/transactionService';
|
|
10
11
|
import { getWalletStore } from '../store/walletStore';
|
|
11
12
|
import { getWorkletStore } from '../store/workletStore';
|
|
12
13
|
import { isIndexerConfigured } from '../store/indexerConfigStore';
|
|
13
14
|
import { resolveWalletId } from '../utils/storeHelpers';
|
|
15
|
+
import { logWarn } from '../utils/logger';
|
|
14
16
|
import { DEFAULT_QUERY_GC_TIME_MS, DEFAULT_QUERY_STALE_TIME_MS, } from '../utils/constants';
|
|
15
17
|
export const transactionQueryKeys = {
|
|
16
18
|
all: ['transactions'],
|
|
@@ -19,9 +21,12 @@ export const transactionQueryKeys = {
|
|
|
19
21
|
function resolveTokenConfigs(tokenConfigs) {
|
|
20
22
|
return typeof tokenConfigs === 'function' ? tokenConfigs() : tokenConfigs;
|
|
21
23
|
}
|
|
22
|
-
function addressesFingerprint(addresses, accountIndex) {
|
|
24
|
+
function addressesFingerprint(addresses, accountIndex, tokenConfigs) {
|
|
23
25
|
const parts = [];
|
|
24
|
-
for (const network of Object.keys(
|
|
26
|
+
for (const network of Object.keys(tokenConfigs).sort()) {
|
|
27
|
+
if (!tokenConfigs[network]?.indexerBlockchain) {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
25
30
|
const address = addresses[network]?.[accountIndex];
|
|
26
31
|
if (address) {
|
|
27
32
|
parts.push(`${network}:${address.toLowerCase()}`);
|
|
@@ -29,13 +34,33 @@ function addressesFingerprint(addresses, accountIndex) {
|
|
|
29
34
|
}
|
|
30
35
|
return parts.join('|');
|
|
31
36
|
}
|
|
37
|
+
async function ensureIndexerAddresses(walletId, accountIndex, tokenConfigs) {
|
|
38
|
+
for (const [network, networkTokens] of Object.entries(tokenConfigs)) {
|
|
39
|
+
if (!networkTokens.indexerBlockchain) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const walletStore = getWalletStore();
|
|
43
|
+
const cached = walletStore.getState().addresses[walletId]?.[network]?.[accountIndex];
|
|
44
|
+
if (cached) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
await AddressService.getAddress(network, accountIndex, walletId);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
logWarn(`[useWalletTransactions] Could not resolve ${network} address for wallet ${walletId}:`, error);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
32
55
|
async function fetchWalletTransactionsForAccount(walletId, accountIndex, tokenConfigs) {
|
|
56
|
+
const configs = resolveTokenConfigs(tokenConfigs);
|
|
57
|
+
await ensureIndexerAddresses(walletId, accountIndex, configs);
|
|
33
58
|
const walletStore = getWalletStore();
|
|
34
59
|
const addresses = walletStore.getState().addresses[walletId] ?? {};
|
|
35
60
|
return TransactionService.resolveWalletTransactions({
|
|
36
61
|
addresses,
|
|
37
62
|
accountIndex,
|
|
38
|
-
tokenConfigs:
|
|
63
|
+
tokenConfigs: configs,
|
|
39
64
|
});
|
|
40
65
|
}
|
|
41
66
|
/**
|
|
@@ -46,16 +71,18 @@ export function useWalletTransactions(accountIndex, tokenConfigs, options) {
|
|
|
46
71
|
const walletStore = getWalletStore();
|
|
47
72
|
const isInitialized = workletStore.getState().isInitialized;
|
|
48
73
|
const walletId = resolveWalletId(options?.walletId);
|
|
74
|
+
const tokenConfigsObj = resolveTokenConfigs(tokenConfigs);
|
|
49
75
|
const addresses = walletStore(useShallow((state) => state.addresses[walletId] ?? {}));
|
|
50
|
-
const addressKey = addressesFingerprint(addresses, accountIndex);
|
|
76
|
+
const addressKey = addressesFingerprint(addresses, accountIndex, tokenConfigsObj);
|
|
51
77
|
const indexerReady = isIndexerConfigured();
|
|
78
|
+
const hasIndexerNetworks = Object.values(tokenConfigsObj).some((networkTokens) => Boolean(networkTokens?.indexerBlockchain));
|
|
52
79
|
return useQuery({
|
|
53
80
|
queryKey: [...transactionQueryKeys.byWallet(walletId, accountIndex), addressKey],
|
|
54
81
|
queryFn: () => fetchWalletTransactionsForAccount(walletId, accountIndex, tokenConfigs),
|
|
55
82
|
enabled: (options?.enabled !== false) &&
|
|
56
83
|
isInitialized &&
|
|
57
84
|
indexerReady &&
|
|
58
|
-
|
|
85
|
+
hasIndexerNetworks,
|
|
59
86
|
staleTime: options?.staleTime ?? DEFAULT_QUERY_STALE_TIME_MS,
|
|
60
87
|
gcTime: DEFAULT_QUERY_GC_TIME_MS,
|
|
61
88
|
refetchInterval: options?.refetchInterval,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTransactions.js","sourceRoot":"","sources":["../../src/hooks/useTransactions.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAElD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAA;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAA;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,EACL,wBAAwB,EACxB,2BAA2B,GAC5B,MAAM,oBAAoB,CAAA;AAU3B,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,GAAG,EAAE,CAAC,cAAc,CAAU;IAC9B,QAAQ,EAAE,CAAC,QAAgB,EAAE,YAAoB,EAAE,EAAE,CACnD,CA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AAC,aAAa,EAAE,EAAE,CAAC,OAAO,CAAC,aAAa,EAAE,iBAAiB,CAAC,CAC7D,CAAA;IAED,OAAO,QAAQ,CAAC;QACd,QAAQ,EAAE,CAAC,GAAG,oBAAoB,CAAC,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC,EAAE,UAAU,CAAC;QAChF,OAAO,EAAE,GAAG,EAAE,CAAC,iCAAiC,CAAC,QAAQ,EAAE,YAAY,EAAE,YAAY,CAAC;QACtF,OAAO,EACL,CAAC,OAAO,EAAE,OAAO,KAAK,KAAK,CAAC;YAC5B,aAAa;YACb,YAAY;YACZ,kBAAkB;QACpB,SAAS,EAAE,OAAO,EAAE,SAAS,IAAI,2BAA2B;QAC5D,MAAM,EAAE,wBAAwB;QAChC,eAAe,EAAE,OAAO,EAAE,eAAe;KAC1C,CAAC,CAAA;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spacesops/wdk-react-native-core",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.73",
|
|
4
4
|
"description": "Core functionality for React Native wallets - wallet management, balance fetching, and worklet operations",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -8,16 +8,18 @@
|
|
|
8
8
|
import { useQuery } from '@tanstack/react-query'
|
|
9
9
|
import { useShallow } from 'zustand/react/shallow'
|
|
10
10
|
|
|
11
|
+
import { AddressService } from '../services/addressService'
|
|
11
12
|
import { TransactionService } from '../services/transactionService'
|
|
12
13
|
import { getWalletStore } from '../store/walletStore'
|
|
13
14
|
import { getWorkletStore } from '../store/workletStore'
|
|
14
15
|
import { isIndexerConfigured } from '../store/indexerConfigStore'
|
|
15
16
|
import { resolveWalletId } from '../utils/storeHelpers'
|
|
17
|
+
import { logWarn } from '../utils/logger'
|
|
16
18
|
import {
|
|
17
19
|
DEFAULT_QUERY_GC_TIME_MS,
|
|
18
20
|
DEFAULT_QUERY_STALE_TIME_MS,
|
|
19
21
|
} from '../utils/constants'
|
|
20
|
-
import type { TokenConfigProvider, WalletTransaction } from '../types'
|
|
22
|
+
import type { TokenConfigProvider, TokenConfigs, WalletTransaction } from '../types'
|
|
21
23
|
|
|
22
24
|
export interface WalletTransactionsQueryOptions {
|
|
23
25
|
enabled?: boolean
|
|
@@ -38,10 +40,14 @@ function resolveTokenConfigs(tokenConfigs: TokenConfigProvider) {
|
|
|
38
40
|
|
|
39
41
|
function addressesFingerprint(
|
|
40
42
|
addresses: Record<string, Record<number, string>>,
|
|
41
|
-
accountIndex: number
|
|
43
|
+
accountIndex: number,
|
|
44
|
+
tokenConfigs: TokenConfigs
|
|
42
45
|
): string {
|
|
43
46
|
const parts: string[] = []
|
|
44
|
-
for (const network of Object.keys(
|
|
47
|
+
for (const network of Object.keys(tokenConfigs).sort()) {
|
|
48
|
+
if (!tokenConfigs[network]?.indexerBlockchain) {
|
|
49
|
+
continue
|
|
50
|
+
}
|
|
45
51
|
const address = addresses[network]?.[accountIndex]
|
|
46
52
|
if (address) {
|
|
47
53
|
parts.push(`${network}:${address.toLowerCase()}`)
|
|
@@ -50,18 +56,48 @@ function addressesFingerprint(
|
|
|
50
56
|
return parts.join('|')
|
|
51
57
|
}
|
|
52
58
|
|
|
59
|
+
async function ensureIndexerAddresses(
|
|
60
|
+
walletId: string,
|
|
61
|
+
accountIndex: number,
|
|
62
|
+
tokenConfigs: TokenConfigs
|
|
63
|
+
): Promise<void> {
|
|
64
|
+
for (const [network, networkTokens] of Object.entries(tokenConfigs)) {
|
|
65
|
+
if (!networkTokens.indexerBlockchain) {
|
|
66
|
+
continue
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const walletStore = getWalletStore()
|
|
70
|
+
const cached = walletStore.getState().addresses[walletId]?.[network]?.[accountIndex]
|
|
71
|
+
if (cached) {
|
|
72
|
+
continue
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
await AddressService.getAddress(network, accountIndex, walletId)
|
|
77
|
+
} catch (error) {
|
|
78
|
+
logWarn(
|
|
79
|
+
`[useWalletTransactions] Could not resolve ${network} address for wallet ${walletId}:`,
|
|
80
|
+
error
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
53
86
|
async function fetchWalletTransactionsForAccount(
|
|
54
87
|
walletId: string,
|
|
55
88
|
accountIndex: number,
|
|
56
89
|
tokenConfigs: TokenConfigProvider
|
|
57
90
|
): Promise<WalletTransaction[]> {
|
|
91
|
+
const configs = resolveTokenConfigs(tokenConfigs)
|
|
92
|
+
await ensureIndexerAddresses(walletId, accountIndex, configs)
|
|
93
|
+
|
|
58
94
|
const walletStore = getWalletStore()
|
|
59
95
|
const addresses = walletStore.getState().addresses[walletId] ?? {}
|
|
60
96
|
|
|
61
97
|
return TransactionService.resolveWalletTransactions({
|
|
62
98
|
addresses,
|
|
63
99
|
accountIndex,
|
|
64
|
-
tokenConfigs:
|
|
100
|
+
tokenConfigs: configs,
|
|
65
101
|
})
|
|
66
102
|
}
|
|
67
103
|
|
|
@@ -77,11 +113,15 @@ export function useWalletTransactions(
|
|
|
77
113
|
const walletStore = getWalletStore()
|
|
78
114
|
const isInitialized = workletStore.getState().isInitialized
|
|
79
115
|
const walletId = resolveWalletId(options?.walletId)
|
|
116
|
+
const tokenConfigsObj = resolveTokenConfigs(tokenConfigs)
|
|
80
117
|
const addresses = walletStore(
|
|
81
118
|
useShallow((state) => state.addresses[walletId] ?? {})
|
|
82
119
|
)
|
|
83
|
-
const addressKey = addressesFingerprint(addresses, accountIndex)
|
|
120
|
+
const addressKey = addressesFingerprint(addresses, accountIndex, tokenConfigsObj)
|
|
84
121
|
const indexerReady = isIndexerConfigured()
|
|
122
|
+
const hasIndexerNetworks = Object.values(tokenConfigsObj).some(
|
|
123
|
+
(networkTokens) => Boolean(networkTokens?.indexerBlockchain)
|
|
124
|
+
)
|
|
85
125
|
|
|
86
126
|
return useQuery({
|
|
87
127
|
queryKey: [...transactionQueryKeys.byWallet(walletId, accountIndex), addressKey],
|
|
@@ -90,7 +130,7 @@ export function useWalletTransactions(
|
|
|
90
130
|
(options?.enabled !== false) &&
|
|
91
131
|
isInitialized &&
|
|
92
132
|
indexerReady &&
|
|
93
|
-
|
|
133
|
+
hasIndexerNetworks,
|
|
94
134
|
staleTime: options?.staleTime ?? DEFAULT_QUERY_STALE_TIME_MS,
|
|
95
135
|
gcTime: DEFAULT_QUERY_GC_TIME_MS,
|
|
96
136
|
refetchInterval: options?.refetchInterval,
|