@explorins/pers-sdk-react-native 1.3.2 → 1.5.1
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 +276 -123
- package/dist/hooks/index.d.ts +8 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +7 -0
- package/dist/hooks/useAnalytics.d.ts +96 -0
- package/dist/hooks/useAnalytics.d.ts.map +1 -0
- package/dist/hooks/useAnalytics.js +73 -0
- package/dist/hooks/useAuth.d.ts +52 -7
- package/dist/hooks/useAuth.d.ts.map +1 -1
- package/dist/hooks/useAuth.js +250 -3
- package/dist/hooks/useBusiness.d.ts +45 -3
- package/dist/hooks/useBusiness.d.ts.map +1 -1
- package/dist/hooks/useBusiness.js +182 -28
- package/dist/hooks/useCampaigns.d.ts +11 -4
- package/dist/hooks/useCampaigns.d.ts.map +1 -1
- package/dist/hooks/useCampaigns.js +101 -39
- package/dist/hooks/useDonations.d.ts +33 -0
- package/dist/hooks/useDonations.d.ts.map +1 -0
- package/dist/hooks/useDonations.js +62 -0
- package/dist/hooks/useFiles.d.ts +50 -0
- package/dist/hooks/useFiles.d.ts.map +1 -0
- package/dist/hooks/useFiles.js +140 -0
- package/dist/hooks/usePurchases.d.ts +9 -0
- package/dist/hooks/usePurchases.d.ts.map +1 -0
- package/dist/hooks/usePurchases.js +56 -0
- package/dist/hooks/useRedemptions.d.ts +10 -5
- package/dist/hooks/useRedemptions.d.ts.map +1 -1
- package/dist/hooks/useRedemptions.js +94 -66
- package/dist/hooks/useTenants.d.ts +10 -0
- package/dist/hooks/useTenants.d.ts.map +1 -0
- package/dist/hooks/useTenants.js +68 -0
- package/dist/hooks/useTokens.d.ts +36 -2
- package/dist/hooks/useTokens.d.ts.map +1 -1
- package/dist/hooks/useTokens.js +180 -19
- package/dist/hooks/useTransactions.d.ts +45 -3
- package/dist/hooks/useTransactions.d.ts.map +1 -1
- package/dist/hooks/useTransactions.js +152 -31
- package/dist/hooks/useUserStatus.d.ts +9 -0
- package/dist/hooks/useUserStatus.d.ts.map +1 -0
- package/dist/hooks/useUserStatus.js +57 -0
- package/dist/hooks/useUsers.d.ts +17 -0
- package/dist/hooks/useUsers.d.ts.map +1 -0
- package/dist/hooks/useUsers.js +120 -0
- package/dist/hooks/useWeb3.d.ts +69 -6
- package/dist/hooks/useWeb3.d.ts.map +1 -1
- package/dist/hooks/useWeb3.js +177 -17
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12199 -796
- package/dist/index.js.map +1 -1
- package/dist/polyfills/index.d.ts +1 -1
- package/dist/polyfills/index.d.ts.map +1 -1
- package/dist/polyfills/index.js +155 -9
- package/dist/polyfills/web3-polyfills.d.ts +7 -0
- package/dist/polyfills/web3-polyfills.d.ts.map +1 -0
- package/dist/polyfills/web3-polyfills.js +85 -0
- package/dist/providers/PersSDKProvider.d.ts +28 -18
- package/dist/providers/PersSDKProvider.d.ts.map +1 -1
- package/dist/providers/PersSDKProvider.js +63 -129
- package/dist/providers/react-native-auth-provider.d.ts +19 -64
- package/dist/providers/react-native-auth-provider.d.ts.map +1 -1
- package/dist/providers/react-native-auth-provider.js +57 -175
- package/package.json +171 -157
- package/src/hooks/index.ts +11 -1
- package/src/hooks/useAnalytics.ts +150 -0
- package/src/hooks/useAuth.ts +286 -27
- package/src/hooks/useBusiness.ts +193 -30
- package/src/hooks/useCampaigns.ts +121 -43
- package/src/hooks/useDonations.ts +68 -0
- package/src/hooks/useFiles.ts +160 -0
- package/src/hooks/usePurchases.ts +69 -0
- package/src/hooks/useRedemptions.ts +109 -68
- package/src/hooks/useTenants.ts +77 -0
- package/src/hooks/useTokens.ts +189 -21
- package/src/hooks/useTransactions.ts +170 -34
- package/src/hooks/useUserStatus.ts +65 -0
- package/src/hooks/useUsers.ts +133 -0
- package/src/hooks/useWeb3.ts +201 -21
- package/src/index.ts +21 -17
- package/src/polyfills/index.ts +163 -10
- package/src/polyfills/web3-polyfills.ts +98 -0
- package/src/providers/PersSDKProvider.tsx +114 -178
- package/src/providers/react-native-auth-provider.ts +65 -208
- package/dist/index.esm.js +0 -1057
- package/dist/index.esm.js.map +0 -1
- package/dist/polyfills/index.simple.d.ts +0 -2
- package/dist/polyfills/index.simple.d.ts.map +0 -1
- package/dist/polyfills/index.simple.js +0 -17
- package/src/polyfills/index.simple.ts +0 -22
- package/src/types/external-modules.d.ts +0 -13
|
@@ -1,129 +1,170 @@
|
|
|
1
1
|
import { useCallback } from 'react';
|
|
2
2
|
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
import type {
|
|
4
|
+
RedemptionCreateRequestDTO,
|
|
5
|
+
RedemptionDTO,
|
|
6
|
+
RedemptionRedeemDTO,
|
|
7
|
+
RedemptionRedeemRequestResponseDTO,
|
|
8
|
+
RedemptionTypeDTO,
|
|
9
|
+
TokenUnitCreateRequestDTO
|
|
10
|
+
} from '@explorins/pers-shared';
|
|
3
11
|
|
|
4
12
|
export const useRedemptions = () => {
|
|
5
|
-
const {
|
|
13
|
+
const { sdk, isInitialized, isAuthenticated } = usePersSDK();
|
|
6
14
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (!isInitialized) {
|
|
15
|
+
const getActiveRedemptions = useCallback(async (): Promise<RedemptionDTO[]> => {
|
|
16
|
+
if (!isInitialized || !sdk) {
|
|
10
17
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
11
18
|
}
|
|
12
|
-
if (!redemptions?.createRedemption) {
|
|
13
|
-
throw new Error('createRedemption method not available - admin access required');
|
|
14
|
-
}
|
|
15
19
|
|
|
16
20
|
try {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
console.log('✅ Redemption offer created successfully:', result);
|
|
21
|
+
const result = await sdk.redemptions.getActiveRedemptions();
|
|
22
|
+
console.log('Active redemptions fetched successfully:', result);
|
|
20
23
|
return result;
|
|
21
24
|
} catch (error) {
|
|
22
|
-
console.error('
|
|
25
|
+
console.error('Failed to fetch active redemptions:', error);
|
|
23
26
|
throw error;
|
|
24
27
|
}
|
|
25
|
-
}, [
|
|
28
|
+
}, [sdk, isInitialized]);
|
|
26
29
|
|
|
27
|
-
const
|
|
28
|
-
if (!isInitialized) {
|
|
30
|
+
const getUserRedemptions = useCallback(async (): Promise<RedemptionRedeemDTO[]> => {
|
|
31
|
+
if (!isInitialized || !sdk) {
|
|
29
32
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
30
33
|
}
|
|
31
|
-
if (!
|
|
32
|
-
console.warn('
|
|
34
|
+
if (!isAuthenticated) {
|
|
35
|
+
console.warn('SDK not authenticated. getUserRedemptions requires authentication.');
|
|
33
36
|
return [];
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
try {
|
|
37
|
-
const result = await redemptions.
|
|
38
|
-
console.log('
|
|
40
|
+
const result = await sdk.redemptions.getUserRedemptions();
|
|
41
|
+
console.log('User redemptions fetched successfully:', result);
|
|
39
42
|
return result;
|
|
40
43
|
} catch (error) {
|
|
41
|
-
console.error('
|
|
44
|
+
console.error('Failed to fetch user redemptions:', error);
|
|
42
45
|
throw error;
|
|
43
46
|
}
|
|
44
|
-
}, [
|
|
47
|
+
}, [sdk, isInitialized, isAuthenticated]);
|
|
45
48
|
|
|
46
|
-
const
|
|
47
|
-
if (!isInitialized) {
|
|
49
|
+
const redeemOffer = useCallback(async (redemptionId: string): Promise<RedemptionRedeemRequestResponseDTO> => {
|
|
50
|
+
if (!isInitialized || !sdk) {
|
|
48
51
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
49
52
|
}
|
|
50
53
|
if (!isAuthenticated) {
|
|
51
|
-
|
|
52
|
-
return [];
|
|
54
|
+
throw new Error('SDK not authenticated. redeemOffer requires authentication.');
|
|
53
55
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
console.log('Redeeming redemption:', redemptionId);
|
|
59
|
+
const result = await sdk.redemptions.redeemOffer(redemptionId);
|
|
60
|
+
|
|
61
|
+
// Cross-platform: Handle signature URLs for redemptions
|
|
62
|
+
if (result?.senderTransaction?.actionable?.actionUrl) {
|
|
63
|
+
try {
|
|
64
|
+
const { Linking } = require('react-native');
|
|
65
|
+
console.log('Opening signature URL:', result.senderTransaction.actionable.actionUrl);
|
|
66
|
+
await Linking.openURL(result.senderTransaction.actionable.actionUrl);
|
|
67
|
+
} catch (linkingError) {
|
|
68
|
+
console.error('Failed to open signature URL:', linkingError);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
console.log('Redemption processed successfully:', result);
|
|
73
|
+
return result;
|
|
74
|
+
} catch (error) {
|
|
75
|
+
console.error('Failed to redeem redemption:', error);
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
}, [sdk, isInitialized, isAuthenticated]);
|
|
79
|
+
|
|
80
|
+
// Admin methods
|
|
81
|
+
const createRedemption = useCallback(async (redemptionData: RedemptionCreateRequestDTO): Promise<RedemptionDTO> => {
|
|
82
|
+
if (!isInitialized || !sdk) {
|
|
83
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
57
84
|
}
|
|
58
85
|
|
|
59
86
|
try {
|
|
60
|
-
|
|
61
|
-
|
|
87
|
+
console.log('Creating redemption offer with data:', redemptionData);
|
|
88
|
+
const result = await sdk.redemptions.createRedemption(redemptionData);
|
|
89
|
+
console.log('Redemption offer created successfully:', result);
|
|
62
90
|
return result;
|
|
63
91
|
} catch (error) {
|
|
64
|
-
console.error('
|
|
92
|
+
console.error('Failed to create redemption offer:', error);
|
|
65
93
|
throw error;
|
|
66
94
|
}
|
|
67
|
-
}, [
|
|
95
|
+
}, [sdk, isInitialized]);
|
|
68
96
|
|
|
69
|
-
const
|
|
70
|
-
if (!isInitialized) {
|
|
97
|
+
const getAllRedemptions = useCallback(async (active?: boolean): Promise<RedemptionDTO[]> => {
|
|
98
|
+
if (!isInitialized || !sdk) {
|
|
71
99
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
72
100
|
}
|
|
73
|
-
|
|
74
|
-
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
const result = await sdk.redemptions.getAllRedemptions(active);
|
|
104
|
+
console.log('All redemptions fetched successfully:', result);
|
|
105
|
+
return result;
|
|
106
|
+
} catch (error) {
|
|
107
|
+
console.error('Failed to fetch all redemptions:', error);
|
|
108
|
+
throw error;
|
|
75
109
|
}
|
|
76
|
-
|
|
77
|
-
|
|
110
|
+
}, [sdk, isInitialized]);
|
|
111
|
+
|
|
112
|
+
const getRedemptionTypes = useCallback(async (): Promise<RedemptionTypeDTO[]> => {
|
|
113
|
+
if (!isInitialized || !sdk) {
|
|
114
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
78
115
|
}
|
|
79
116
|
|
|
80
117
|
try {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
// React Native specific: Handle signature URLs for redemptions
|
|
85
|
-
if (result?.actionable?.actionUrl) {
|
|
86
|
-
try {
|
|
87
|
-
const { Linking } = require('react-native');
|
|
88
|
-
console.log('🔗 Opening redemption signature URL:', result.actionable.actionUrl);
|
|
89
|
-
await Linking.openURL(result.actionable.actionUrl);
|
|
90
|
-
} catch (linkingError) {
|
|
91
|
-
console.error('❌ Failed to open redemption signature URL:', linkingError);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
console.log('✅ Redemption processed successfully:', result);
|
|
118
|
+
const result = await sdk.redemptions.getRedemptionTypes();
|
|
119
|
+
console.log('Redemption types fetched successfully:', result);
|
|
96
120
|
return result;
|
|
97
121
|
} catch (error) {
|
|
98
|
-
console.error('
|
|
122
|
+
console.error('Failed to fetch redemption types:', error);
|
|
99
123
|
throw error;
|
|
100
124
|
}
|
|
101
|
-
}, [
|
|
125
|
+
}, [sdk, isInitialized]);
|
|
102
126
|
|
|
103
|
-
const
|
|
104
|
-
if (!isInitialized) {
|
|
127
|
+
const updateRedemption = useCallback(async (redemptionId: string, redemptionData: RedemptionCreateRequestDTO): Promise<RedemptionDTO> => {
|
|
128
|
+
if (!isInitialized || !sdk) {
|
|
105
129
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
106
130
|
}
|
|
107
|
-
|
|
108
|
-
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
const result = await sdk.redemptions.updateRedemption(redemptionId, redemptionData);
|
|
134
|
+
console.log('Redemption updated successfully:', result);
|
|
135
|
+
return result;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
console.error('Failed to update redemption:', error);
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
}, [sdk, isInitialized]);
|
|
141
|
+
|
|
142
|
+
const toggleRedemptionStatus = useCallback(async (redemptionId: string): Promise<RedemptionDTO> => {
|
|
143
|
+
if (!isInitialized || !sdk) {
|
|
144
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
109
145
|
}
|
|
110
146
|
|
|
111
147
|
try {
|
|
112
|
-
const result = await redemptions.
|
|
113
|
-
console.log('
|
|
148
|
+
const result = await sdk.redemptions.toggleRedemptionStatus(redemptionId);
|
|
149
|
+
console.log('Redemption status toggled successfully:', result);
|
|
114
150
|
return result;
|
|
115
151
|
} catch (error) {
|
|
116
|
-
console.error('
|
|
152
|
+
console.error('Failed to toggle redemption status:', error);
|
|
117
153
|
throw error;
|
|
118
154
|
}
|
|
119
|
-
}, [
|
|
155
|
+
}, [sdk, isInitialized]);
|
|
120
156
|
|
|
121
157
|
return {
|
|
122
|
-
createRedemption,
|
|
123
158
|
getActiveRedemptions,
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
159
|
+
getUserRedemptions,
|
|
160
|
+
redeemOffer,
|
|
161
|
+
getRedemptionTypes,
|
|
162
|
+
createRedemption,
|
|
163
|
+
getAllRedemptions,
|
|
164
|
+
updateRedemption,
|
|
165
|
+
toggleRedemptionStatus,
|
|
166
|
+
isAvailable: isInitialized && !!sdk?.redemptions,
|
|
128
167
|
};
|
|
129
|
-
};
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
export type RedemptionHook = ReturnType<typeof useRedemptions>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
import type { TenantPublicDTO, TenantClientConfigDTO, AdminDTO } from '@explorins/pers-shared';
|
|
4
|
+
|
|
5
|
+
export const useTenants = () => {
|
|
6
|
+
const { sdk, isInitialized } = usePersSDK();
|
|
7
|
+
|
|
8
|
+
const getTenantInfo = useCallback(async (): Promise<TenantPublicDTO> => {
|
|
9
|
+
if (!isInitialized || !sdk) {
|
|
10
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const result = await sdk.tenants.getTenantInfo();
|
|
15
|
+
console.log('Tenant info fetched successfully:', result);
|
|
16
|
+
return result;
|
|
17
|
+
} catch (error) {
|
|
18
|
+
console.error('Failed to fetch tenant info:', error);
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
}, [sdk, isInitialized]);
|
|
22
|
+
|
|
23
|
+
const getClientConfig = useCallback(async (): Promise<TenantClientConfigDTO> => {
|
|
24
|
+
if (!isInitialized || !sdk) {
|
|
25
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
const result = await sdk.tenants.getClientConfig();
|
|
30
|
+
console.log('Client config fetched successfully:', result);
|
|
31
|
+
return result;
|
|
32
|
+
} catch (error) {
|
|
33
|
+
console.error('Failed to fetch client config:', error);
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}, [sdk, isInitialized]);
|
|
37
|
+
|
|
38
|
+
const getLoginToken = useCallback(async (): Promise<string> => {
|
|
39
|
+
if (!isInitialized || !sdk) {
|
|
40
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const result = await sdk.tenants.getLoginToken();
|
|
45
|
+
console.log('Login token fetched successfully');
|
|
46
|
+
return result;
|
|
47
|
+
} catch (error) {
|
|
48
|
+
console.error('Failed to fetch login token:', error);
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
}, [sdk, isInitialized]);
|
|
52
|
+
|
|
53
|
+
const getAdmins = useCallback(async (): Promise<AdminDTO[]> => {
|
|
54
|
+
if (!isInitialized || !sdk) {
|
|
55
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
const result = await sdk.tenants.getAdmins();
|
|
60
|
+
console.log('Admins fetched successfully:', result);
|
|
61
|
+
return result;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
console.error('Failed to fetch admins:', error);
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}, [sdk, isInitialized]);
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
getTenantInfo,
|
|
70
|
+
getClientConfig,
|
|
71
|
+
getLoginToken,
|
|
72
|
+
getAdmins,
|
|
73
|
+
isAvailable: isInitialized && !!sdk?.tenants,
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export type TenantHook = ReturnType<typeof useTenants>;
|
package/src/hooks/useTokens.ts
CHANGED
|
@@ -1,53 +1,221 @@
|
|
|
1
1
|
import { useCallback } from 'react';
|
|
2
2
|
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
import type { TokenDTO } from '@explorins/pers-shared';
|
|
3
4
|
|
|
5
|
+
/**
|
|
6
|
+
* React hook for token operations in the PERS SDK
|
|
7
|
+
*
|
|
8
|
+
* Provides methods for fetching various types of tokens including credit, reward, and status tokens.
|
|
9
|
+
* Supports both general token queries and specific contract-based token retrieval.
|
|
10
|
+
*
|
|
11
|
+
* @returns Token hook with methods for token management
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* function TokensComponent() {
|
|
16
|
+
* const { getTokens, getActiveCreditToken, getRewardTokens } = useTokens();
|
|
17
|
+
*
|
|
18
|
+
* const loadTokens = async () => {
|
|
19
|
+
* try {
|
|
20
|
+
* const tokens = await getTokens();
|
|
21
|
+
* const creditToken = await getActiveCreditToken();
|
|
22
|
+
* console.log('Loaded tokens:', tokens);
|
|
23
|
+
* console.log('Active credit token:', creditToken);
|
|
24
|
+
* } catch (error) {
|
|
25
|
+
* console.error('Failed to load tokens:', error);
|
|
26
|
+
* }
|
|
27
|
+
* };
|
|
28
|
+
*
|
|
29
|
+
* return <button onClick={loadTokens}>Load Tokens</button>;
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
4
33
|
export const useTokens = () => {
|
|
5
|
-
const {
|
|
34
|
+
const { sdk, isInitialized, isAuthenticated } = usePersSDK();
|
|
6
35
|
|
|
7
36
|
if (!isAuthenticated && isInitialized) {
|
|
8
37
|
console.warn('SDK not authenticated. Some token operations may fail.');
|
|
9
38
|
}
|
|
10
39
|
|
|
11
|
-
|
|
12
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Retrieves all tokens available to the current user
|
|
42
|
+
*
|
|
43
|
+
* @returns Promise resolving to array of tokens
|
|
44
|
+
* @throws Error if SDK is not initialized
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* const { getTokens } = useTokens();
|
|
49
|
+
* const tokens = await getTokens();
|
|
50
|
+
* console.log('Available tokens:', tokens);
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
const getTokens = useCallback(async (): Promise<TokenDTO[]> => {
|
|
54
|
+
if (!isInitialized || !sdk) {
|
|
13
55
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
14
56
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
const result = await sdk.tokens.getTokens();
|
|
60
|
+
console.log('Tokens fetched successfully:', result);
|
|
61
|
+
return result;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
console.error('Failed to fetch tokens:', error);
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}, [sdk, isInitialized]);
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Retrieves the currently active credit token
|
|
70
|
+
*
|
|
71
|
+
* @returns Promise resolving to active credit token
|
|
72
|
+
* @throws Error if SDK is not initialized
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const { getActiveCreditToken } = useTokens();
|
|
77
|
+
* const creditToken = await getActiveCreditToken();
|
|
78
|
+
* console.log('Active credit token:', creditToken.name);
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
const getActiveCreditToken = useCallback(async (): Promise<TokenDTO> => {
|
|
82
|
+
if (!isInitialized || !sdk) {
|
|
83
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
18
84
|
}
|
|
19
85
|
|
|
20
86
|
try {
|
|
21
|
-
const result = await tokens.
|
|
22
|
-
console.log('
|
|
87
|
+
const result = await sdk.tokens.getActiveCreditToken();
|
|
88
|
+
console.log('Active credit token fetched successfully:', result);
|
|
23
89
|
return result;
|
|
24
90
|
} catch (error) {
|
|
25
|
-
console.error('
|
|
91
|
+
console.error('Failed to fetch active credit token:', error);
|
|
26
92
|
throw error;
|
|
27
93
|
}
|
|
28
|
-
}, [
|
|
94
|
+
}, [sdk, isInitialized]);
|
|
29
95
|
|
|
30
|
-
|
|
31
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Retrieves all reward tokens available in the system
|
|
98
|
+
*
|
|
99
|
+
* @returns Promise resolving to array of reward tokens
|
|
100
|
+
* @throws Error if SDK is not initialized
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const { getRewardTokens } = useTokens();
|
|
105
|
+
* const rewardTokens = await getRewardTokens();
|
|
106
|
+
* console.log('Reward tokens:', rewardTokens);
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
const getRewardTokens = useCallback(async (): Promise<TokenDTO[]> => {
|
|
110
|
+
if (!isInitialized || !sdk) {
|
|
32
111
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
33
112
|
}
|
|
34
|
-
|
|
35
|
-
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
const result = await sdk.tokens.getRewardTokens();
|
|
116
|
+
console.log('Reward tokens fetched successfully:', result);
|
|
117
|
+
return result;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
console.error('Failed to fetch reward tokens:', error);
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
}, [sdk, isInitialized]);
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Retrieves all available token types in the system
|
|
126
|
+
*
|
|
127
|
+
* @returns Promise resolving to array of token types
|
|
128
|
+
* @throws Error if SDK is not initialized
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* const { getTokenTypes } = useTokens();
|
|
133
|
+
* const types = await getTokenTypes();
|
|
134
|
+
* console.log('Token types:', types);
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
const getTokenTypes = useCallback(async (): Promise<any> => {
|
|
138
|
+
if (!isInitialized || !sdk) {
|
|
139
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
36
140
|
}
|
|
37
141
|
|
|
38
142
|
try {
|
|
39
|
-
const result = await tokens.
|
|
40
|
-
console.log('
|
|
143
|
+
const result = await sdk.tokens.getTokenTypes();
|
|
144
|
+
console.log('Token types fetched successfully:', result);
|
|
41
145
|
return result;
|
|
42
146
|
} catch (error) {
|
|
43
|
-
console.error('
|
|
147
|
+
console.error('Failed to fetch token types:', error);
|
|
44
148
|
throw error;
|
|
45
149
|
}
|
|
46
|
-
}, [
|
|
150
|
+
}, [sdk, isInitialized]);
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Retrieves all status tokens (tokens that represent user status/achievements)
|
|
154
|
+
*
|
|
155
|
+
* @returns Promise resolving to array of status tokens
|
|
156
|
+
* @throws Error if SDK is not initialized
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const { getStatusTokens } = useTokens();
|
|
161
|
+
* const statusTokens = await getStatusTokens();
|
|
162
|
+
* console.log('Status tokens:', statusTokens);
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
const getStatusTokens = useCallback(async (): Promise<TokenDTO[]> => {
|
|
166
|
+
if (!isInitialized || !sdk) {
|
|
167
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
try {
|
|
171
|
+
const result = await sdk.tokens.getStatusTokens();
|
|
172
|
+
console.log('Status tokens fetched successfully:', result);
|
|
173
|
+
return result;
|
|
174
|
+
} catch (error) {
|
|
175
|
+
console.error('Failed to fetch status tokens:', error);
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
}, [sdk, isInitialized]);
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Retrieves a specific token by its contract address and optional token ID
|
|
182
|
+
*
|
|
183
|
+
* @param contractAddress - The contract address of the token
|
|
184
|
+
* @param contractTokenId - Optional specific token ID within the contract
|
|
185
|
+
* @returns Promise resolving to the token data
|
|
186
|
+
* @throws Error if SDK is not initialized
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```typescript
|
|
190
|
+
* const { getTokenByContract } = useTokens();
|
|
191
|
+
* const token = await getTokenByContract('0x123...', 'token-1');
|
|
192
|
+
* console.log('Token from contract:', token);
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
const getTokenByContract = useCallback(async (contractAddress: string, contractTokenId?: string | null): Promise<TokenDTO> => {
|
|
196
|
+
if (!isInitialized || !sdk) {
|
|
197
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
try {
|
|
201
|
+
const result = await sdk.tokens.getTokenByContract(contractAddress, contractTokenId);
|
|
202
|
+
console.log('Token by contract fetched successfully:', result);
|
|
203
|
+
return result;
|
|
204
|
+
} catch (error) {
|
|
205
|
+
console.error('Failed to fetch token by contract:', error);
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
}, [sdk, isInitialized]);
|
|
47
209
|
|
|
48
210
|
return {
|
|
49
211
|
getTokens,
|
|
50
|
-
|
|
51
|
-
|
|
212
|
+
getActiveCreditToken,
|
|
213
|
+
getRewardTokens,
|
|
214
|
+
getTokenTypes,
|
|
215
|
+
getStatusTokens,
|
|
216
|
+
getTokenByContract,
|
|
217
|
+
isAvailable: isInitialized && !!sdk?.tokens,
|
|
52
218
|
};
|
|
53
|
-
};
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
export type TokenHook = ReturnType<typeof useTokens>;
|