@explorins/pers-sdk-react-native 1.5.34 → 1.5.36

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@explorins/pers-sdk-react-native",
3
- "version": "1.5.34",
3
+ "version": "1.5.36",
4
4
  "description": "React Native SDK for PERS Platform - Tourism Loyalty System with Blockchain Transaction Signing and WebAuthn Authentication",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -37,8 +37,8 @@
37
37
  "author": "eXplorins",
38
38
  "license": "MIT",
39
39
  "dependencies": {
40
- "@explorins/pers-sdk": "^1.6.42",
41
- "@explorins/pers-shared": "^2.1.52",
40
+ "@explorins/pers-sdk": "^1.6.45",
41
+ "@explorins/pers-shared": "^2.1.54",
42
42
  "@explorins/pers-signer": "^1.0.33",
43
43
  "@explorins/web3-ts": "^0.3.77",
44
44
  "buffer": "^6.0.3",
@@ -25,4 +25,5 @@ export type {
25
25
  StatusUpdateData,
26
26
  OnStatusUpdateFn,
27
27
  SigningStatus as SigningStatusType
28
- } from './useTransactionSigner';
28
+ } from './useTransactionSigner';
29
+ export type { AccountOwnedTokensResult, Web3Hook } from './useWeb3';
@@ -8,6 +8,11 @@ import type {
8
8
  TokenMetadata
9
9
  } from '@explorins/pers-sdk/web3';
10
10
  import type { ChainData } from '@explorins/pers-sdk/web3-chain';
11
+ import type { TokenDTO } from '@explorins/pers-shared';
12
+ import type { AccountOwnedTokensResult } from '@explorins/pers-sdk';
13
+
14
+ // Re-export for convenience
15
+ export type { AccountOwnedTokensResult } from '@explorins/pers-sdk';
11
16
 
12
17
  /**
13
18
  * React hook for Web3 operations in the PERS SDK
@@ -202,6 +207,98 @@ export const useWeb3 = () => {
202
207
  }
203
208
  }, [sdk, isInitialized]);
204
209
 
210
+ // ==========================================
211
+ // HELPER METHODS (delegating to core SDK)
212
+ // ==========================================
213
+
214
+ /**
215
+ * Extract tokenIds from a TokenDTO's metadata.
216
+ *
217
+ * Extracts `tokenMetadataIncrementalId` from each metadata entry. This is
218
+ * particularly useful for ERC-1155 tokens which require specific tokenIds
219
+ * to query balances, but works with any token that has metadata.
220
+ *
221
+ * **Note:** For most use cases, prefer `getAccountOwnedTokensFromContract()`
222
+ * which handles tokenId extraction automatically.
223
+ *
224
+ * @param token - Token definition containing metadata array
225
+ * @returns Array of tokenId strings, or undefined if no metadata
226
+ *
227
+ * @see {@link getAccountOwnedTokensFromContract} - Recommended helper that handles this automatically
228
+ */
229
+ const extractTokenIds = useCallback((token: TokenDTO): string[] | undefined => {
230
+ // Pure function - delegates to core SDK (no initialization required)
231
+ return sdk?.web3.extractTokenIds(token);
232
+ }, [sdk]);
233
+
234
+ /**
235
+ * Get owned tokens from a specific token contract for any blockchain address.
236
+ *
237
+ * **Recommended method** for querying token balances. Automatically handles:
238
+ * - Token type detection (ERC-20, ERC-721, ERC-1155)
239
+ * - TokenId extraction from metadata (uses `extractTokenIds()` internally for ERC-1155)
240
+ * - Building the collection request
241
+ * - Filtering to only tokens with balance > 0
242
+ *
243
+ * Works with any valid blockchain address - can query user wallets, external
244
+ * wallets, contract addresses, or any other address holding tokens.
245
+ *
246
+ * @param accountAddress - Any valid blockchain address (wallet, contract, etc.)
247
+ * @param token - Token definition (from getRewardTokens, getStatusTokens, etc.)
248
+ * @param maxTokens - Maximum tokens to retrieve (default: 50)
249
+ * @returns Promise resolving to result with owned tokens
250
+ * @throws Error if SDK is not initialized
251
+ *
252
+ * @example Query user's wallet
253
+ * ```typescript
254
+ * const { user } = usePersSDK();
255
+ * const { getAccountOwnedTokensFromContract } = useWeb3();
256
+ * const { getRewardTokens } = useTokens();
257
+ *
258
+ * const userWallet = user?.wallets?.[0]?.address;
259
+ * const rewardTokens = await getRewardTokens();
260
+ * const result = await getAccountOwnedTokensFromContract(userWallet, rewardTokens[0]);
261
+ * console.log(`User owns ${result.totalOwned} tokens`);
262
+ * ```
263
+ *
264
+ * @see {@link extractTokenIds} - Low-level helper used internally for ERC-1155
265
+ * @see {@link buildCollectionRequest} - For manual request building
266
+ */
267
+ const getAccountOwnedTokensFromContract = useCallback(async (
268
+ accountAddress: string,
269
+ token: TokenDTO,
270
+ maxTokens: number = 50
271
+ ): Promise<AccountOwnedTokensResult> => {
272
+ if (!isInitialized || !sdk) {
273
+ throw new Error('SDK not initialized. Call initialize() first.');
274
+ }
275
+
276
+ return sdk.web3.getAccountOwnedTokensFromContract(accountAddress, token, maxTokens);
277
+ }, [sdk, isInitialized]);
278
+
279
+ /**
280
+ * Build a TokenCollectionRequest from a TokenDTO.
281
+ *
282
+ * Automatically handles ERC-1155 tokenId extraction from metadata.
283
+ * Use this when you need more control over the request than
284
+ * `getAccountOwnedTokensFromContract` provides.
285
+ *
286
+ * @param accountAddress - Any valid blockchain address (wallet, contract, etc.)
287
+ * @param token - Token definition
288
+ * @param maxTokens - Maximum tokens to retrieve (default: 50)
289
+ * @returns TokenCollectionRequest ready for getTokenCollection()
290
+ */
291
+ const buildCollectionRequest = useCallback((
292
+ accountAddress: string,
293
+ token: TokenDTO,
294
+ maxTokens: number = 50
295
+ ): TokenCollectionRequest => {
296
+ if (!sdk) {
297
+ throw new Error('SDK not initialized. Call initialize() first.');
298
+ }
299
+ return sdk.web3.buildCollectionRequest(accountAddress, token, maxTokens);
300
+ }, [sdk]);
301
+
205
302
  return {
206
303
  getTokenBalance,
207
304
  getTokenMetadata,
@@ -210,6 +307,10 @@ export const useWeb3 = () => {
210
307
  fetchAndProcessMetadata,
211
308
  getChainDataById,
212
309
  getExplorerUrl,
310
+ // Helper methods
311
+ extractTokenIds,
312
+ getAccountOwnedTokensFromContract,
313
+ buildCollectionRequest,
213
314
  isAvailable: isInitialized && !!sdk?.web3,
214
315
  };
215
316
  };