@explorins/pers-sdk-react-native 1.5.33 → 1.5.35
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/README.md +105 -45
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/useWeb3.d.ts +55 -1
- package/dist/hooks/useWeb3.d.ts.map +1 -1
- package/dist/hooks/useWeb3.js +179 -1
- package/dist/index.js +180 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/hooks/index.ts +2 -1
- package/src/hooks/useWeb3.ts +206 -1
package/dist/index.js
CHANGED
|
@@ -2629,6 +2629,8 @@ exports.ApiKeyType = void 0;
|
|
|
2629
2629
|
ApiKeyType["ADMIN_JWT_REFRESH_TOKEN"] = "ADMIN_JWT_REFRESH_TOKEN";
|
|
2630
2630
|
ApiKeyType["USER_JWT_ACCESS_TOKEN"] = "USER_JWT_ACCESS_TOKEN";
|
|
2631
2631
|
ApiKeyType["USER_JWT_REFRESH_TOKEN"] = "USER_JWT_REFRESH_TOKEN";
|
|
2632
|
+
ApiKeyType["BUSINESS_JWT_ACCESS_TOKEN"] = "BUSINESS_JWT_ACCESS_TOKEN";
|
|
2633
|
+
ApiKeyType["BUSINESS_JWT_REFRESH_TOKEN"] = "BUSINESS_JWT_REFRESH_TOKEN";
|
|
2632
2634
|
ApiKeyType["BLOCKCHAIN_WRITER_JWT"] = "BLOCKCHAIN_WRITER_JWT";
|
|
2633
2635
|
ApiKeyType["BLOCKCHAIN_READER_JWT"] = "BLOCKCHAIN_READER_JWT";
|
|
2634
2636
|
ApiKeyType["TRANSACTION_JWT_ACCESS_TOKEN"] = "TRANSACTION_JWT_ACCESS_TOKEN";
|
|
@@ -34454,9 +34456,48 @@ const useRedemptions = () => {
|
|
|
34454
34456
|
*
|
|
34455
34457
|
* Note: Wallet addresses should be obtained from `user.wallets` via `usePersSDK()`.
|
|
34456
34458
|
*
|
|
34459
|
+
* ## ERC-1155 Token Handling
|
|
34460
|
+
*
|
|
34461
|
+
* **IMPORTANT**: ERC-1155 tokens require specific `tokenIds` to query balances.
|
|
34462
|
+
* Unlike ERC-721, you cannot enumerate owned tokens - you must know which tokenId to check.
|
|
34463
|
+
*
|
|
34464
|
+
* The tokenIds come from `TokenDTO.metadata[].tokenMetadataIncrementalId`.
|
|
34465
|
+
*
|
|
34466
|
+
* ### Option 1: Use the helper method (Recommended)
|
|
34467
|
+
* ```typescript
|
|
34468
|
+
* const { getUserOwnedTokensFromContract } = useWeb3();
|
|
34469
|
+
* const { getRewardTokens } = useTokens();
|
|
34470
|
+
*
|
|
34471
|
+
* const rewardTokens = await getRewardTokens();
|
|
34472
|
+
* const result = await getUserOwnedTokensFromContract(walletAddress, rewardTokens[0]);
|
|
34473
|
+
* console.log('Owned rewards:', result.ownedTokens);
|
|
34474
|
+
* ```
|
|
34475
|
+
*
|
|
34476
|
+
* ### Option 2: Manual tokenIds extraction
|
|
34477
|
+
* ```typescript
|
|
34478
|
+
* const { getTokenCollection, extractTokenIds } = useWeb3();
|
|
34479
|
+
* const { getRewardTokens } = useTokens();
|
|
34480
|
+
*
|
|
34481
|
+
* const rewardTokens = await getRewardTokens();
|
|
34482
|
+
* const rewardToken = rewardTokens[0];
|
|
34483
|
+
*
|
|
34484
|
+
* // Extract tokenIds for ERC-1155
|
|
34485
|
+
* const tokenIds = extractTokenIds(rewardToken);
|
|
34486
|
+
*
|
|
34487
|
+
* const collection = await getTokenCollection({
|
|
34488
|
+
* accountAddress: walletAddress,
|
|
34489
|
+
* contractAddress: rewardToken.contractAddress,
|
|
34490
|
+
* abi: rewardToken.abi,
|
|
34491
|
+
* chainId: rewardToken.chainId,
|
|
34492
|
+
* tokenIds: tokenIds // Required for ERC-1155!
|
|
34493
|
+
* });
|
|
34494
|
+
*
|
|
34495
|
+
* const ownedTokens = collection.tokens.filter(t => t.hasBalance);
|
|
34496
|
+
* ```
|
|
34497
|
+
*
|
|
34457
34498
|
* @returns Web3 hook with methods for blockchain operations
|
|
34458
34499
|
*
|
|
34459
|
-
* @example
|
|
34500
|
+
* @example Basic Usage
|
|
34460
34501
|
* ```typescript
|
|
34461
34502
|
* function Web3Component() {
|
|
34462
34503
|
* const { user } = usePersSDK();
|
|
@@ -34577,6 +34618,138 @@ const useWeb3 = () => {
|
|
|
34577
34618
|
throw error;
|
|
34578
34619
|
}
|
|
34579
34620
|
}, [sdk, isInitialized]);
|
|
34621
|
+
/**
|
|
34622
|
+
* Extract tokenIds from a TokenDTO for use with ERC-1155 contracts.
|
|
34623
|
+
*
|
|
34624
|
+
* ERC-1155 tokens require specific tokenIds to query balances. This helper
|
|
34625
|
+
* extracts the `tokenMetadataIncrementalId` from each metadata entry.
|
|
34626
|
+
*
|
|
34627
|
+
* @param token - Token definition containing metadata array
|
|
34628
|
+
* @returns Array of tokenId strings, or undefined if no metadata
|
|
34629
|
+
*
|
|
34630
|
+
* @example
|
|
34631
|
+
* ```typescript
|
|
34632
|
+
* const { extractTokenIds, getTokenCollection } = useWeb3();
|
|
34633
|
+
* const { getRewardTokens } = useTokens();
|
|
34634
|
+
*
|
|
34635
|
+
* const rewardToken = (await getRewardTokens())[0];
|
|
34636
|
+
* const tokenIds = extractTokenIds(rewardToken);
|
|
34637
|
+
*
|
|
34638
|
+
* if (tokenIds) {
|
|
34639
|
+
* const collection = await getTokenCollection({
|
|
34640
|
+
* accountAddress: walletAddress,
|
|
34641
|
+
* contractAddress: rewardToken.contractAddress,
|
|
34642
|
+
* abi: rewardToken.abi,
|
|
34643
|
+
* chainId: rewardToken.chainId,
|
|
34644
|
+
* tokenIds: tokenIds
|
|
34645
|
+
* });
|
|
34646
|
+
* }
|
|
34647
|
+
* ```
|
|
34648
|
+
*/
|
|
34649
|
+
const extractTokenIds = react.useCallback((token) => {
|
|
34650
|
+
if (!token.metadata || token.metadata.length === 0) {
|
|
34651
|
+
return undefined;
|
|
34652
|
+
}
|
|
34653
|
+
return token.metadata.map(meta => meta.tokenMetadataIncrementalId.toString());
|
|
34654
|
+
}, []);
|
|
34655
|
+
/**
|
|
34656
|
+
* Get user's owned tokens from a specific token contract.
|
|
34657
|
+
*
|
|
34658
|
+
* This is a convenience method that automatically handles:
|
|
34659
|
+
* - ERC-1155 tokenId extraction from metadata
|
|
34660
|
+
* - Building the collection request
|
|
34661
|
+
* - Filtering to only tokens with balance > 0
|
|
34662
|
+
*
|
|
34663
|
+
* **This is the recommended way to get user's owned reward/status tokens.**
|
|
34664
|
+
*
|
|
34665
|
+
* @param walletAddress - User's wallet address
|
|
34666
|
+
* @param token - Token definition (from getRewardTokens, getStatusTokens, etc.)
|
|
34667
|
+
* @param maxTokens - Maximum tokens to retrieve (default: 50)
|
|
34668
|
+
* @returns Promise resolving to result with owned tokens
|
|
34669
|
+
* @throws Error if SDK is not initialized
|
|
34670
|
+
*
|
|
34671
|
+
* @example Get User's Owned Rewards
|
|
34672
|
+
* ```typescript
|
|
34673
|
+
* const { user } = usePersSDK();
|
|
34674
|
+
* const { getRewardTokens } = useTokens();
|
|
34675
|
+
* const { getUserOwnedTokensFromContract } = useWeb3();
|
|
34676
|
+
*
|
|
34677
|
+
* const walletAddress = user?.wallets?.[0]?.address;
|
|
34678
|
+
* const rewardTokens = await getRewardTokens();
|
|
34679
|
+
*
|
|
34680
|
+
* for (const token of rewardTokens) {
|
|
34681
|
+
* const result = await getUserOwnedTokensFromContract(walletAddress, token);
|
|
34682
|
+
*
|
|
34683
|
+
* console.log(`${token.name}: ${result.totalOwned} owned`);
|
|
34684
|
+
* result.ownedTokens.forEach(owned => {
|
|
34685
|
+
* console.log(` - TokenId ${owned.tokenId}: ${owned.balance}`);
|
|
34686
|
+
* console.log(` Name: ${owned.metadata?.name}`);
|
|
34687
|
+
* console.log(` Image: ${owned.metadata?.imageUrl}`);
|
|
34688
|
+
* });
|
|
34689
|
+
* }
|
|
34690
|
+
* ```
|
|
34691
|
+
*/
|
|
34692
|
+
const getUserOwnedTokensFromContract = react.useCallback(async (walletAddress, token, maxTokens = 50) => {
|
|
34693
|
+
if (!isInitialized || !sdk) {
|
|
34694
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
34695
|
+
}
|
|
34696
|
+
try {
|
|
34697
|
+
// For ERC-1155, extract tokenIds from metadata
|
|
34698
|
+
const tokenIds = token.type === NativeTokenTypes.ERC1155 ? extractTokenIds(token) : undefined;
|
|
34699
|
+
const collection = await sdk.web3.getTokenCollection({
|
|
34700
|
+
accountAddress: walletAddress,
|
|
34701
|
+
contractAddress: token.contractAddress,
|
|
34702
|
+
abi: token.abi,
|
|
34703
|
+
chainId: token.chainId,
|
|
34704
|
+
tokenIds,
|
|
34705
|
+
maxTokens
|
|
34706
|
+
});
|
|
34707
|
+
// Filter to only owned tokens (hasBalance: true)
|
|
34708
|
+
// For ERC721, include all since they're enumerated
|
|
34709
|
+
const ownedTokens = token.type === NativeTokenTypes.ERC721
|
|
34710
|
+
? collection.tokens
|
|
34711
|
+
: collection.tokens.filter(t => t.hasBalance);
|
|
34712
|
+
return {
|
|
34713
|
+
token,
|
|
34714
|
+
ownedTokens,
|
|
34715
|
+
totalOwned: ownedTokens.length
|
|
34716
|
+
};
|
|
34717
|
+
}
|
|
34718
|
+
catch (error) {
|
|
34719
|
+
console.error('Failed to get user owned tokens:', error);
|
|
34720
|
+
throw error;
|
|
34721
|
+
}
|
|
34722
|
+
}, [sdk, isInitialized, extractTokenIds]);
|
|
34723
|
+
/**
|
|
34724
|
+
* Build a TokenCollectionRequest from a TokenDTO.
|
|
34725
|
+
*
|
|
34726
|
+
* Automatically handles ERC-1155 tokenId extraction from metadata.
|
|
34727
|
+
* Use this when you need more control over the request than
|
|
34728
|
+
* `getUserOwnedTokensFromContract` provides.
|
|
34729
|
+
*
|
|
34730
|
+
* @param walletAddress - User's wallet address
|
|
34731
|
+
* @param token - Token definition
|
|
34732
|
+
* @param maxTokens - Maximum tokens to retrieve (default: 50)
|
|
34733
|
+
* @returns TokenCollectionRequest ready for getTokenCollection()
|
|
34734
|
+
*
|
|
34735
|
+
* @example
|
|
34736
|
+
* ```typescript
|
|
34737
|
+
* const { buildCollectionRequest, getTokenCollection } = useWeb3();
|
|
34738
|
+
*
|
|
34739
|
+
* const request = buildCollectionRequest(walletAddress, rewardToken);
|
|
34740
|
+
* const collection = await getTokenCollection(request);
|
|
34741
|
+
* ```
|
|
34742
|
+
*/
|
|
34743
|
+
const buildCollectionRequest = react.useCallback((walletAddress, token, maxTokens = 50) => {
|
|
34744
|
+
return {
|
|
34745
|
+
accountAddress: walletAddress,
|
|
34746
|
+
contractAddress: token.contractAddress,
|
|
34747
|
+
abi: token.abi,
|
|
34748
|
+
chainId: token.chainId,
|
|
34749
|
+
tokenIds: token.type === NativeTokenTypes.ERC1155 ? extractTokenIds(token) : undefined,
|
|
34750
|
+
maxTokens
|
|
34751
|
+
};
|
|
34752
|
+
}, [extractTokenIds]);
|
|
34580
34753
|
const resolveIPFSUrl = react.useCallback(async (url, chainId) => {
|
|
34581
34754
|
if (!isInitialized || !sdk) {
|
|
34582
34755
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
@@ -34630,6 +34803,7 @@ const useWeb3 = () => {
|
|
|
34630
34803
|
}
|
|
34631
34804
|
}, [sdk, isInitialized]);
|
|
34632
34805
|
return {
|
|
34806
|
+
// Core methods
|
|
34633
34807
|
getTokenBalance,
|
|
34634
34808
|
getTokenMetadata,
|
|
34635
34809
|
getTokenCollection,
|
|
@@ -34637,6 +34811,11 @@ const useWeb3 = () => {
|
|
|
34637
34811
|
fetchAndProcessMetadata,
|
|
34638
34812
|
getChainDataById,
|
|
34639
34813
|
getExplorerUrl,
|
|
34814
|
+
// Helper methods for ERC-1155 tokens
|
|
34815
|
+
extractTokenIds,
|
|
34816
|
+
getUserOwnedTokensFromContract,
|
|
34817
|
+
buildCollectionRequest,
|
|
34818
|
+
// State
|
|
34640
34819
|
isAvailable: isInitialized && !!sdk?.web3,
|
|
34641
34820
|
};
|
|
34642
34821
|
};
|