@explorins/pers-sdk-react-native 1.5.35 → 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/README.md +73 -13
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/useWeb3.d.ts +5 -53
- package/dist/hooks/useWeb3.d.ts.map +1 -1
- package/dist/hooks/useWeb3.js +82 -177
- package/dist/index.js +239 -176
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/hooks/index.ts +1 -1
- package/src/hooks/useWeb3.ts +98 -202
package/dist/index.js
CHANGED
|
@@ -31677,6 +31677,37 @@ class DonationManager {
|
|
|
31677
31677
|
*
|
|
31678
31678
|
* Provides a simplified API for common Web3 blockchain tasks while maintaining
|
|
31679
31679
|
* access to the full Web3 SDK for advanced use cases.
|
|
31680
|
+
*
|
|
31681
|
+
* ## Supported Token Standards
|
|
31682
|
+
*
|
|
31683
|
+
* - **ERC-20**: Fungible tokens (credits, points). Query balance with `getTokenBalance()`.
|
|
31684
|
+
* - **ERC-721**: Non-fungible tokens (unique NFTs). Supports enumeration - no tokenIds needed.
|
|
31685
|
+
* - **ERC-1155**: Multi-token standard (rewards, collectibles). Requires specific tokenIds.
|
|
31686
|
+
*
|
|
31687
|
+
* ## Recommended: Use Helper Methods
|
|
31688
|
+
*
|
|
31689
|
+
* For most use cases, use `getAccountOwnedTokensFromContract()` which automatically
|
|
31690
|
+
* handles token type detection and tokenId extraction:
|
|
31691
|
+
*
|
|
31692
|
+
* ```typescript
|
|
31693
|
+
* const result = await sdk.web3.getAccountOwnedTokensFromContract(walletAddress, token);
|
|
31694
|
+
* console.log(`Wallet owns ${result.totalOwned} tokens`);
|
|
31695
|
+
* ```
|
|
31696
|
+
*
|
|
31697
|
+
* ## Manual Approach (Advanced)
|
|
31698
|
+
*
|
|
31699
|
+
* For ERC-1155 tokens, tokenIds are extracted from `token.metadata[].tokenMetadataIncrementalId`:
|
|
31700
|
+
*
|
|
31701
|
+
* ```typescript
|
|
31702
|
+
* const tokenIds = token.metadata?.map(meta => meta.tokenMetadataIncrementalId.toString());
|
|
31703
|
+
* const collection = await sdk.web3.getTokenCollection({
|
|
31704
|
+
* accountAddress: walletAddress,
|
|
31705
|
+
* contractAddress: token.contractAddress,
|
|
31706
|
+
* abi: token.abi,
|
|
31707
|
+
* chainId: token.chainId,
|
|
31708
|
+
* tokenIds // Required for ERC-1155, optional for ERC-721
|
|
31709
|
+
* });
|
|
31710
|
+
* ```
|
|
31680
31711
|
*/
|
|
31681
31712
|
class Web3Manager {
|
|
31682
31713
|
constructor(apiClient) {
|
|
@@ -31764,6 +31795,132 @@ class Web3Manager {
|
|
|
31764
31795
|
getWeb3ApplicationService() {
|
|
31765
31796
|
return this.web3ApplicationService;
|
|
31766
31797
|
}
|
|
31798
|
+
// ==========================================
|
|
31799
|
+
// HELPER METHODS
|
|
31800
|
+
// ==========================================
|
|
31801
|
+
/**
|
|
31802
|
+
* Extract tokenIds from a TokenDTO's metadata.
|
|
31803
|
+
*
|
|
31804
|
+
* Extracts `tokenMetadataIncrementalId` from each metadata entry. This is
|
|
31805
|
+
* particularly useful for ERC-1155 tokens which require specific tokenIds
|
|
31806
|
+
* to query balances, but works with any token that has metadata.
|
|
31807
|
+
*
|
|
31808
|
+
* **Note:** For most use cases, prefer `getAccountOwnedTokensFromContract()`
|
|
31809
|
+
* which handles tokenId extraction automatically.
|
|
31810
|
+
*
|
|
31811
|
+
* @param token - Token definition containing metadata array
|
|
31812
|
+
* @returns Array of tokenId strings, or undefined if no metadata
|
|
31813
|
+
*
|
|
31814
|
+
* @example Manual token collection request
|
|
31815
|
+
* ```typescript
|
|
31816
|
+
* const rewardToken = (await sdk.tokens.getRewardTokens())[0];
|
|
31817
|
+
* const tokenIds = sdk.web3.extractTokenIds(rewardToken);
|
|
31818
|
+
*
|
|
31819
|
+
* if (tokenIds) {
|
|
31820
|
+
* const collection = await sdk.web3.getTokenCollection({
|
|
31821
|
+
* accountAddress: walletAddress,
|
|
31822
|
+
* contractAddress: rewardToken.contractAddress,
|
|
31823
|
+
* abi: rewardToken.abi,
|
|
31824
|
+
* chainId: rewardToken.chainId,
|
|
31825
|
+
* tokenIds
|
|
31826
|
+
* });
|
|
31827
|
+
* }
|
|
31828
|
+
* ```
|
|
31829
|
+
*
|
|
31830
|
+
* @see {@link getAccountOwnedTokensFromContract} - Recommended helper that handles this automatically
|
|
31831
|
+
*/
|
|
31832
|
+
extractTokenIds(token) {
|
|
31833
|
+
if (!token.metadata || token.metadata.length === 0) {
|
|
31834
|
+
return undefined;
|
|
31835
|
+
}
|
|
31836
|
+
return token.metadata.map(meta => meta.tokenMetadataIncrementalId.toString());
|
|
31837
|
+
}
|
|
31838
|
+
/**
|
|
31839
|
+
* Get owned tokens from a specific token contract for any blockchain address.
|
|
31840
|
+
*
|
|
31841
|
+
* **Recommended method** for querying token balances. Automatically handles:
|
|
31842
|
+
* - Token type detection (ERC-20, ERC-721, ERC-1155)
|
|
31843
|
+
* - TokenId extraction from metadata (uses `extractTokenIds()` internally for ERC-1155)
|
|
31844
|
+
* - Building the collection request
|
|
31845
|
+
* - Filtering to only tokens with balance > 0
|
|
31846
|
+
*
|
|
31847
|
+
* Works with any valid blockchain address - can query user wallets, external
|
|
31848
|
+
* wallets, contract addresses, or any other address holding tokens.
|
|
31849
|
+
*
|
|
31850
|
+
* @param accountAddress - Any valid blockchain address (wallet, contract, etc.)
|
|
31851
|
+
* @param token - Token definition (from getRewardTokens, getStatusTokens, etc.)
|
|
31852
|
+
* @param maxTokens - Maximum tokens to retrieve (default: 50)
|
|
31853
|
+
* @returns Promise resolving to collection result with owned tokens
|
|
31854
|
+
*
|
|
31855
|
+
* @example Query user's wallet
|
|
31856
|
+
* ```typescript
|
|
31857
|
+
* const userWallet = user.wallets[0].address;
|
|
31858
|
+
* const result = await sdk.web3.getAccountOwnedTokensFromContract(userWallet, rewardToken);
|
|
31859
|
+
* console.log(`User owns ${result.totalOwned} tokens`);
|
|
31860
|
+
* ```
|
|
31861
|
+
*
|
|
31862
|
+
* @example Query any external wallet
|
|
31863
|
+
* ```typescript
|
|
31864
|
+
* const externalWallet = '0x1234...abcd';
|
|
31865
|
+
* const result = await sdk.web3.getAccountOwnedTokensFromContract(externalWallet, rewardToken);
|
|
31866
|
+
* console.log(`Wallet owns ${result.totalOwned} tokens`);
|
|
31867
|
+
* ```
|
|
31868
|
+
*
|
|
31869
|
+
* @see {@link extractTokenIds} - Low-level helper used internally for ERC-1155
|
|
31870
|
+
* @see {@link buildCollectionRequest} - For manual request building
|
|
31871
|
+
*/
|
|
31872
|
+
async getAccountOwnedTokensFromContract(accountAddress, token, maxTokens = 50) {
|
|
31873
|
+
// For ERC-1155, extract tokenIds from metadata
|
|
31874
|
+
const tokenIds = token.type === NativeTokenTypes.ERC1155 ? this.extractTokenIds(token) : undefined;
|
|
31875
|
+
const collection = await this.getTokenCollection({
|
|
31876
|
+
accountAddress: accountAddress,
|
|
31877
|
+
contractAddress: token.contractAddress,
|
|
31878
|
+
abi: token.abi,
|
|
31879
|
+
chainId: token.chainId,
|
|
31880
|
+
tokenIds,
|
|
31881
|
+
maxTokens
|
|
31882
|
+
});
|
|
31883
|
+
// Filter to only owned tokens (hasBalance: true)
|
|
31884
|
+
// For ERC721, include all since they're enumerated
|
|
31885
|
+
const ownedTokens = token.type === NativeTokenTypes.ERC721
|
|
31886
|
+
? collection.tokens
|
|
31887
|
+
: collection.tokens.filter(t => t.hasBalance);
|
|
31888
|
+
return {
|
|
31889
|
+
token,
|
|
31890
|
+
ownedTokens,
|
|
31891
|
+
totalOwned: ownedTokens.length
|
|
31892
|
+
};
|
|
31893
|
+
}
|
|
31894
|
+
/**
|
|
31895
|
+
* Build a TokenCollectionRequest from a TokenDTO.
|
|
31896
|
+
*
|
|
31897
|
+
* Automatically handles ERC-1155 tokenId extraction from metadata.
|
|
31898
|
+
* Use this when you need more control over the request than
|
|
31899
|
+
* `getAccountOwnedTokensFromContract` provides.
|
|
31900
|
+
*
|
|
31901
|
+
* Works with any valid blockchain address - user wallets, external wallets, etc.
|
|
31902
|
+
*
|
|
31903
|
+
* @param accountAddress - Any valid blockchain address (wallet, contract, etc.)
|
|
31904
|
+
* @param token - Token definition
|
|
31905
|
+
* @param maxTokens - Maximum tokens to retrieve
|
|
31906
|
+
* @returns TokenCollectionRequest ready for getTokenCollection()
|
|
31907
|
+
*
|
|
31908
|
+
* @example
|
|
31909
|
+
* ```typescript
|
|
31910
|
+
* const request = sdk.web3.buildCollectionRequest(walletAddress, rewardToken);
|
|
31911
|
+
* const collection = await sdk.web3.getTokenCollection(request);
|
|
31912
|
+
* ```
|
|
31913
|
+
*/
|
|
31914
|
+
buildCollectionRequest(accountAddress, token, maxTokens = 50) {
|
|
31915
|
+
return {
|
|
31916
|
+
accountAddress: accountAddress,
|
|
31917
|
+
contractAddress: token.contractAddress,
|
|
31918
|
+
abi: token.abi,
|
|
31919
|
+
chainId: token.chainId,
|
|
31920
|
+
tokenIds: token.type === NativeTokenTypes.ERC1155 ? this.extractTokenIds(token) : undefined,
|
|
31921
|
+
maxTokens
|
|
31922
|
+
};
|
|
31923
|
+
}
|
|
31767
31924
|
}
|
|
31768
31925
|
|
|
31769
31926
|
/**
|
|
@@ -34456,48 +34613,9 @@ const useRedemptions = () => {
|
|
|
34456
34613
|
*
|
|
34457
34614
|
* Note: Wallet addresses should be obtained from `user.wallets` via `usePersSDK()`.
|
|
34458
34615
|
*
|
|
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
|
-
*
|
|
34498
34616
|
* @returns Web3 hook with methods for blockchain operations
|
|
34499
34617
|
*
|
|
34500
|
-
* @example
|
|
34618
|
+
* @example
|
|
34501
34619
|
* ```typescript
|
|
34502
34620
|
* function Web3Component() {
|
|
34503
34621
|
* const { user } = usePersSDK();
|
|
@@ -34618,138 +34736,6 @@ const useWeb3 = () => {
|
|
|
34618
34736
|
throw error;
|
|
34619
34737
|
}
|
|
34620
34738
|
}, [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]);
|
|
34753
34739
|
const resolveIPFSUrl = react.useCallback(async (url, chainId) => {
|
|
34754
34740
|
if (!isInitialized || !sdk) {
|
|
34755
34741
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
@@ -34802,8 +34788,86 @@ const useWeb3 = () => {
|
|
|
34802
34788
|
throw error;
|
|
34803
34789
|
}
|
|
34804
34790
|
}, [sdk, isInitialized]);
|
|
34791
|
+
// ==========================================
|
|
34792
|
+
// HELPER METHODS (delegating to core SDK)
|
|
34793
|
+
// ==========================================
|
|
34794
|
+
/**
|
|
34795
|
+
* Extract tokenIds from a TokenDTO's metadata.
|
|
34796
|
+
*
|
|
34797
|
+
* Extracts `tokenMetadataIncrementalId` from each metadata entry. This is
|
|
34798
|
+
* particularly useful for ERC-1155 tokens which require specific tokenIds
|
|
34799
|
+
* to query balances, but works with any token that has metadata.
|
|
34800
|
+
*
|
|
34801
|
+
* **Note:** For most use cases, prefer `getAccountOwnedTokensFromContract()`
|
|
34802
|
+
* which handles tokenId extraction automatically.
|
|
34803
|
+
*
|
|
34804
|
+
* @param token - Token definition containing metadata array
|
|
34805
|
+
* @returns Array of tokenId strings, or undefined if no metadata
|
|
34806
|
+
*
|
|
34807
|
+
* @see {@link getAccountOwnedTokensFromContract} - Recommended helper that handles this automatically
|
|
34808
|
+
*/
|
|
34809
|
+
const extractTokenIds = react.useCallback((token) => {
|
|
34810
|
+
// Pure function - delegates to core SDK (no initialization required)
|
|
34811
|
+
return sdk?.web3.extractTokenIds(token);
|
|
34812
|
+
}, [sdk]);
|
|
34813
|
+
/**
|
|
34814
|
+
* Get owned tokens from a specific token contract for any blockchain address.
|
|
34815
|
+
*
|
|
34816
|
+
* **Recommended method** for querying token balances. Automatically handles:
|
|
34817
|
+
* - Token type detection (ERC-20, ERC-721, ERC-1155)
|
|
34818
|
+
* - TokenId extraction from metadata (uses `extractTokenIds()` internally for ERC-1155)
|
|
34819
|
+
* - Building the collection request
|
|
34820
|
+
* - Filtering to only tokens with balance > 0
|
|
34821
|
+
*
|
|
34822
|
+
* Works with any valid blockchain address - can query user wallets, external
|
|
34823
|
+
* wallets, contract addresses, or any other address holding tokens.
|
|
34824
|
+
*
|
|
34825
|
+
* @param accountAddress - Any valid blockchain address (wallet, contract, etc.)
|
|
34826
|
+
* @param token - Token definition (from getRewardTokens, getStatusTokens, etc.)
|
|
34827
|
+
* @param maxTokens - Maximum tokens to retrieve (default: 50)
|
|
34828
|
+
* @returns Promise resolving to result with owned tokens
|
|
34829
|
+
* @throws Error if SDK is not initialized
|
|
34830
|
+
*
|
|
34831
|
+
* @example Query user's wallet
|
|
34832
|
+
* ```typescript
|
|
34833
|
+
* const { user } = usePersSDK();
|
|
34834
|
+
* const { getAccountOwnedTokensFromContract } = useWeb3();
|
|
34835
|
+
* const { getRewardTokens } = useTokens();
|
|
34836
|
+
*
|
|
34837
|
+
* const userWallet = user?.wallets?.[0]?.address;
|
|
34838
|
+
* const rewardTokens = await getRewardTokens();
|
|
34839
|
+
* const result = await getAccountOwnedTokensFromContract(userWallet, rewardTokens[0]);
|
|
34840
|
+
* console.log(`User owns ${result.totalOwned} tokens`);
|
|
34841
|
+
* ```
|
|
34842
|
+
*
|
|
34843
|
+
* @see {@link extractTokenIds} - Low-level helper used internally for ERC-1155
|
|
34844
|
+
* @see {@link buildCollectionRequest} - For manual request building
|
|
34845
|
+
*/
|
|
34846
|
+
const getAccountOwnedTokensFromContract = react.useCallback(async (accountAddress, token, maxTokens = 50) => {
|
|
34847
|
+
if (!isInitialized || !sdk) {
|
|
34848
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
34849
|
+
}
|
|
34850
|
+
return sdk.web3.getAccountOwnedTokensFromContract(accountAddress, token, maxTokens);
|
|
34851
|
+
}, [sdk, isInitialized]);
|
|
34852
|
+
/**
|
|
34853
|
+
* Build a TokenCollectionRequest from a TokenDTO.
|
|
34854
|
+
*
|
|
34855
|
+
* Automatically handles ERC-1155 tokenId extraction from metadata.
|
|
34856
|
+
* Use this when you need more control over the request than
|
|
34857
|
+
* `getAccountOwnedTokensFromContract` provides.
|
|
34858
|
+
*
|
|
34859
|
+
* @param accountAddress - Any valid blockchain address (wallet, contract, etc.)
|
|
34860
|
+
* @param token - Token definition
|
|
34861
|
+
* @param maxTokens - Maximum tokens to retrieve (default: 50)
|
|
34862
|
+
* @returns TokenCollectionRequest ready for getTokenCollection()
|
|
34863
|
+
*/
|
|
34864
|
+
const buildCollectionRequest = react.useCallback((accountAddress, token, maxTokens = 50) => {
|
|
34865
|
+
if (!sdk) {
|
|
34866
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
34867
|
+
}
|
|
34868
|
+
return sdk.web3.buildCollectionRequest(accountAddress, token, maxTokens);
|
|
34869
|
+
}, [sdk]);
|
|
34805
34870
|
return {
|
|
34806
|
-
// Core methods
|
|
34807
34871
|
getTokenBalance,
|
|
34808
34872
|
getTokenMetadata,
|
|
34809
34873
|
getTokenCollection,
|
|
@@ -34811,11 +34875,10 @@ const useWeb3 = () => {
|
|
|
34811
34875
|
fetchAndProcessMetadata,
|
|
34812
34876
|
getChainDataById,
|
|
34813
34877
|
getExplorerUrl,
|
|
34814
|
-
// Helper methods
|
|
34878
|
+
// Helper methods
|
|
34815
34879
|
extractTokenIds,
|
|
34816
|
-
|
|
34880
|
+
getAccountOwnedTokensFromContract,
|
|
34817
34881
|
buildCollectionRequest,
|
|
34818
|
-
// State
|
|
34819
34882
|
isAvailable: isInitialized && !!sdk?.web3,
|
|
34820
34883
|
};
|
|
34821
34884
|
};
|