keynesol-shared 1.0.0

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.
Files changed (154) hide show
  1. package/README.md +118 -0
  2. package/dist/components/Common/ErrorBoundary.d.ts +23 -0
  3. package/dist/components/Common/ErrorBoundary.d.ts.map +1 -0
  4. package/dist/components/Common/ErrorBoundary.js +93 -0
  5. package/dist/components/Common/ErrorBoundary.jsx +103 -0
  6. package/dist/components/Common/ErrorMessage.d.ts +8 -0
  7. package/dist/components/Common/ErrorMessage.d.ts.map +1 -0
  8. package/dist/components/Common/ErrorMessage.js +36 -0
  9. package/dist/components/Common/ErrorMessage.jsx +40 -0
  10. package/dist/components/Common/Loading.d.ts +8 -0
  11. package/dist/components/Common/Loading.d.ts.map +1 -0
  12. package/dist/components/Common/Loading.js +41 -0
  13. package/dist/components/Common/Loading.jsx +44 -0
  14. package/dist/components/Common/LoadingIndicator.d.ts +17 -0
  15. package/dist/components/Common/LoadingIndicator.d.ts.map +1 -0
  16. package/dist/components/Common/LoadingIndicator.js +95 -0
  17. package/dist/components/Common/LoadingIndicator.jsx +108 -0
  18. package/dist/components/Common/ProgramStatus.d.ts +3 -0
  19. package/dist/components/Common/ProgramStatus.d.ts.map +1 -0
  20. package/dist/components/Common/ProgramStatus.js +26 -0
  21. package/dist/components/Common/ProgramStatus.jsx +27 -0
  22. package/dist/components/Common/Skeleton.d.ts +39 -0
  23. package/dist/components/Common/Skeleton.d.ts.map +1 -0
  24. package/dist/components/Common/Skeleton.js +53 -0
  25. package/dist/components/Common/Skeleton.jsx +67 -0
  26. package/dist/components/Common/SkeletonScreen.d.ts +18 -0
  27. package/dist/components/Common/SkeletonScreen.d.ts.map +1 -0
  28. package/dist/components/Common/SkeletonScreen.js +98 -0
  29. package/dist/components/Common/SkeletonScreen.jsx +108 -0
  30. package/dist/components/Common/index.d.ts +11 -0
  31. package/dist/components/Common/index.d.ts.map +1 -0
  32. package/dist/components/Common/index.js +10 -0
  33. package/dist/components/Wallet/TransactionStatus.d.ts +11 -0
  34. package/dist/components/Wallet/TransactionStatus.d.ts.map +1 -0
  35. package/dist/components/Wallet/TransactionStatus.js +97 -0
  36. package/dist/components/Wallet/TransactionStatus.jsx +106 -0
  37. package/dist/components/Wallet/WalletBalance.d.ts +4 -0
  38. package/dist/components/Wallet/WalletBalance.d.ts.map +1 -0
  39. package/dist/components/Wallet/WalletBalance.js +82 -0
  40. package/dist/components/Wallet/WalletBalance.jsx +86 -0
  41. package/dist/components/Wallet/WalletButton.d.ts +3 -0
  42. package/dist/components/Wallet/WalletButton.d.ts.map +1 -0
  43. package/dist/components/Wallet/WalletButton.js +51 -0
  44. package/dist/components/Wallet/WalletButton.jsx +53 -0
  45. package/dist/components/Wallet/WalletConnectionModal.d.ts +8 -0
  46. package/dist/components/Wallet/WalletConnectionModal.d.ts.map +1 -0
  47. package/dist/components/Wallet/WalletConnectionModal.js +150 -0
  48. package/dist/components/Wallet/WalletConnectionModal.jsx +170 -0
  49. package/dist/components/Wallet/WalletProvider.d.ts +9 -0
  50. package/dist/components/Wallet/WalletProvider.d.ts.map +1 -0
  51. package/dist/components/Wallet/WalletProvider.js +70 -0
  52. package/dist/components/Wallet/WalletProvider.jsx +75 -0
  53. package/dist/components/Wallet/index.d.ts +9 -0
  54. package/dist/components/Wallet/index.d.ts.map +1 -0
  55. package/dist/components/Wallet/index.js +8 -0
  56. package/dist/components/index.d.ts +7 -0
  57. package/dist/components/index.d.ts.map +1 -0
  58. package/dist/components/index.js +6 -0
  59. package/dist/hooks/index.d.ts +10 -0
  60. package/dist/hooks/index.d.ts.map +1 -0
  61. package/dist/hooks/index.js +9 -0
  62. package/dist/hooks/useCache.d.ts +16 -0
  63. package/dist/hooks/useCache.d.ts.map +1 -0
  64. package/dist/hooks/useCache.js +67 -0
  65. package/dist/hooks/usePolling.d.ts +16 -0
  66. package/dist/hooks/usePolling.d.ts.map +1 -0
  67. package/dist/hooks/usePolling.js +79 -0
  68. package/dist/hooks/useProgram.d.ts +14 -0
  69. package/dist/hooks/useProgram.d.ts.map +1 -0
  70. package/dist/hooks/useProgram.js +88 -0
  71. package/dist/hooks/useTokenBalance.d.ts +16 -0
  72. package/dist/hooks/useTokenBalance.d.ts.map +1 -0
  73. package/dist/hooks/useTokenBalance.js +100 -0
  74. package/dist/hooks/useVaults.d.ts +23 -0
  75. package/dist/hooks/useVaults.d.ts.map +1 -0
  76. package/dist/hooks/useVaults.js +98 -0
  77. package/dist/index.d.ts +12 -0
  78. package/dist/index.d.ts.map +1 -0
  79. package/dist/index.js +17 -0
  80. package/dist/services/index.d.ts +7 -0
  81. package/dist/services/index.d.ts.map +1 -0
  82. package/dist/services/index.js +6 -0
  83. package/dist/services/reconciliationService.d.ts +76 -0
  84. package/dist/services/reconciliationService.d.ts.map +1 -0
  85. package/dist/services/reconciliationService.js +216 -0
  86. package/dist/services/syncService.d.ts +51 -0
  87. package/dist/services/syncService.d.ts.map +1 -0
  88. package/dist/services/syncService.js +218 -0
  89. package/dist/types/index.d.ts +201 -0
  90. package/dist/types/index.d.ts.map +1 -0
  91. package/dist/types/index.js +1 -0
  92. package/dist/utils/cacheManager.d.ts +73 -0
  93. package/dist/utils/cacheManager.d.ts.map +1 -0
  94. package/dist/utils/cacheManager.js +232 -0
  95. package/dist/utils/errorHandler.d.ts +76 -0
  96. package/dist/utils/errorHandler.d.ts.map +1 -0
  97. package/dist/utils/errorHandler.js +267 -0
  98. package/dist/utils/index.d.ts +12 -0
  99. package/dist/utils/index.d.ts.map +1 -0
  100. package/dist/utils/index.js +11 -0
  101. package/dist/utils/performanceMonitor.d.ts +75 -0
  102. package/dist/utils/performanceMonitor.d.ts.map +1 -0
  103. package/dist/utils/performanceMonitor.js +197 -0
  104. package/dist/utils/rpcRetry.d.ts +12 -0
  105. package/dist/utils/rpcRetry.d.ts.map +1 -0
  106. package/dist/utils/rpcRetry.js +47 -0
  107. package/dist/utils/supabase.d.ts +198 -0
  108. package/dist/utils/supabase.d.ts.map +1 -0
  109. package/dist/utils/supabase.js +50 -0
  110. package/dist/utils/toastService.d.ts +52 -0
  111. package/dist/utils/toastService.d.ts.map +1 -0
  112. package/dist/utils/toastService.js +139 -0
  113. package/dist/utils/tokenUtils.d.ts +33 -0
  114. package/dist/utils/tokenUtils.d.ts.map +1 -0
  115. package/dist/utils/tokenUtils.js +66 -0
  116. package/dist/utils/validation.d.ts +35 -0
  117. package/dist/utils/validation.d.ts.map +1 -0
  118. package/dist/utils/validation.js +83 -0
  119. package/package.json +45 -0
  120. package/src/components/Common/ErrorBoundary.tsx +135 -0
  121. package/src/components/Common/ErrorMessage.tsx +52 -0
  122. package/src/components/Common/Loading.tsx +56 -0
  123. package/src/components/Common/LoadingIndicator.tsx +143 -0
  124. package/src/components/Common/ProgramStatus.tsx +37 -0
  125. package/src/components/Common/Skeleton.tsx +83 -0
  126. package/src/components/Common/SkeletonScreen.tsx +166 -0
  127. package/src/components/Common/index.ts +10 -0
  128. package/src/components/Wallet/TransactionStatus.tsx +138 -0
  129. package/src/components/Wallet/WalletBalance.tsx +94 -0
  130. package/src/components/Wallet/WalletButton.tsx +65 -0
  131. package/src/components/Wallet/WalletConnectionModal.tsx +193 -0
  132. package/src/components/Wallet/WalletProvider.tsx +104 -0
  133. package/src/components/Wallet/index.ts +8 -0
  134. package/src/components/index.ts +6 -0
  135. package/src/hooks/index.ts +10 -0
  136. package/src/hooks/useCache.ts +87 -0
  137. package/src/hooks/usePolling.ts +98 -0
  138. package/src/hooks/useProgram.ts +93 -0
  139. package/src/hooks/useTokenBalance.ts +113 -0
  140. package/src/hooks/useVaults.ts +122 -0
  141. package/src/index.ts +23 -0
  142. package/src/services/index.ts +6 -0
  143. package/src/services/reconciliationService.ts +246 -0
  144. package/src/services/syncService.ts +238 -0
  145. package/src/types/index.ts +233 -0
  146. package/src/utils/cacheManager.ts +286 -0
  147. package/src/utils/errorHandler.ts +336 -0
  148. package/src/utils/index.ts +12 -0
  149. package/src/utils/performanceMonitor.ts +222 -0
  150. package/src/utils/rpcRetry.ts +55 -0
  151. package/src/utils/supabase.ts +253 -0
  152. package/src/utils/toastService.ts +166 -0
  153. package/src/utils/tokenUtils.ts +75 -0
  154. package/src/utils/validation.ts +107 -0
@@ -0,0 +1,238 @@
1
+ /**
2
+ * Data Synchronization Service
3
+ * Synchronizes blockchain events to Supabase
4
+ * Requirements: 10.1, 10.2, 10.3, 10.4, 10.5
5
+ */
6
+ import { Connection, PublicKey } from '@solana/web3.js';
7
+ import { getSupabaseClient, isSupabaseConfigured, SupabaseConfig } from '../utils/supabase';
8
+ import { errorHandler } from '../utils/errorHandler';
9
+ import { cacheManager } from '../utils/cacheManager';
10
+
11
+ interface SyncConfig {
12
+ enabled: boolean;
13
+ interval: number; // milliseconds
14
+ batchSize: number;
15
+ supabaseConfig?: SupabaseConfig;
16
+ }
17
+
18
+ class SyncService {
19
+ private connection: Connection | null = null;
20
+ private syncInterval: NodeJS.Timeout | null = null;
21
+ private config: SyncConfig = {
22
+ enabled: false,
23
+ interval: 60000, // 1 minute
24
+ batchSize: 100,
25
+ };
26
+
27
+ /**
28
+ * Initialize sync service
29
+ */
30
+ initialize(connection: Connection, supabaseConfig?: SupabaseConfig): void {
31
+ this.connection = connection;
32
+ this.config.supabaseConfig = supabaseConfig;
33
+ this.config.enabled = isSupabaseConfigured(supabaseConfig);
34
+
35
+ if (this.config.enabled) {
36
+ this.startSync();
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Start periodic synchronization
42
+ */
43
+ startSync(): void {
44
+ if (this.syncInterval) {
45
+ return; // Already running
46
+ }
47
+
48
+ // Initial sync
49
+ this.syncAll();
50
+
51
+ // Periodic sync
52
+ this.syncInterval = setInterval(() => {
53
+ this.syncAll();
54
+ }, this.config.interval);
55
+ }
56
+
57
+ /**
58
+ * Stop synchronization
59
+ */
60
+ stopSync(): void {
61
+ if (this.syncInterval) {
62
+ clearInterval(this.syncInterval);
63
+ this.syncInterval = null;
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Sync all data types
69
+ */
70
+ async syncAll(): Promise<void> {
71
+ if (!this.config.enabled || !isSupabaseConfigured(this.config.supabaseConfig)) {
72
+ return;
73
+ }
74
+
75
+ try {
76
+ await Promise.all([
77
+ this.syncTransactions(),
78
+ this.syncRewards(),
79
+ this.syncPlatformMetrics(),
80
+ this.syncVaultHistory(),
81
+ ]);
82
+ } catch (error) {
83
+ errorHandler.handleError(error as Error, 'SyncService');
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Sync transactions from blockchain to Supabase
89
+ */
90
+ async syncTransactions(): Promise<void> {
91
+ if (!this.connection || !isSupabaseConfigured(this.config.supabaseConfig)) {
92
+ return;
93
+ }
94
+
95
+ try {
96
+ const supabase = getSupabaseClient(this.config.supabaseConfig);
97
+
98
+ // Get latest transaction signature from Supabase
99
+ const { data: latestTx } = await supabase
100
+ .from('transactions')
101
+ .select('signature, timestamp')
102
+ .order('timestamp', { ascending: false })
103
+ .limit(1)
104
+ .single();
105
+
106
+ // In a real implementation, you would:
107
+ // 1. Query blockchain for new transactions since latestTx.timestamp
108
+ // 2. Parse transaction data
109
+ // 3. Insert into Supabase
110
+
111
+ // For now, this is a placeholder
112
+ console.log('Syncing transactions...');
113
+ } catch (error) {
114
+ errorHandler.handleError(error as Error, 'SyncService.syncTransactions');
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Sync rewards from blockchain to Supabase
120
+ */
121
+ async syncRewards(): Promise<void> {
122
+ if (!this.connection || !isSupabaseConfigured(this.config.supabaseConfig)) {
123
+ return;
124
+ }
125
+
126
+ try {
127
+ const supabase = getSupabaseClient(this.config.supabaseConfig);
128
+
129
+ // Get latest reward timestamp from Supabase
130
+ const { data: latestReward } = await supabase
131
+ .from('rewards')
132
+ .select('timestamp')
133
+ .order('timestamp', { ascending: false })
134
+ .limit(1)
135
+ .single();
136
+
137
+ // In a real implementation, you would:
138
+ // 1. Query blockchain for new rewards since latestReward.timestamp
139
+ // 2. Parse reward data
140
+ // 3. Insert into Supabase
141
+
142
+ console.log('Syncing rewards...');
143
+ } catch (error) {
144
+ errorHandler.handleError(error as Error, 'SyncService.syncRewards');
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Sync platform metrics
150
+ */
151
+ async syncPlatformMetrics(): Promise<void> {
152
+ if (!this.connection || !isSupabaseConfigured(this.config.supabaseConfig)) {
153
+ return;
154
+ }
155
+
156
+ try {
157
+ const supabase = getSupabaseClient(this.config.supabaseConfig);
158
+
159
+ // Calculate current platform metrics
160
+ // This would typically involve:
161
+ // 1. Fetching all vaults
162
+ // 2. Calculating TVL, user count, etc.
163
+ // 3. Inserting into platform_metrics table
164
+
165
+ const metrics = {
166
+ timestamp: new Date().toISOString(),
167
+ total_value_locked: 0,
168
+ total_users: 0,
169
+ active_vaults: 0,
170
+ average_apr: 0,
171
+ current_epoch: 0,
172
+ volume_24h: 0,
173
+ };
174
+
175
+ await (supabase.from('platform_metrics') as any).insert(metrics);
176
+
177
+ // Invalidate cache
178
+ cacheManager.invalidate('platformMetrics');
179
+
180
+ console.log('Syncing platform metrics...');
181
+ } catch (error) {
182
+ errorHandler.handleError(error as Error, 'SyncService.syncPlatformMetrics');
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Sync vault history
188
+ */
189
+ async syncVaultHistory(): Promise<void> {
190
+ if (!this.connection || !isSupabaseConfigured(this.config.supabaseConfig)) {
191
+ return;
192
+ }
193
+
194
+ try {
195
+ const supabase = getSupabaseClient(this.config.supabaseConfig);
196
+
197
+ // In a real implementation, you would:
198
+ // 1. Fetch all vaults from blockchain
199
+ // 2. For each vault, create history entry
200
+ // 3. Insert into vault_history table
201
+
202
+ console.log('Syncing vault history...');
203
+ } catch (error) {
204
+ errorHandler.handleError(error as Error, 'SyncService.syncVaultHistory');
205
+ }
206
+ }
207
+
208
+ /**
209
+ * Manually trigger sync for a specific transaction
210
+ */
211
+ async syncTransaction(signature: string): Promise<void> {
212
+ if (!this.connection || !isSupabaseConfigured(this.config.supabaseConfig)) {
213
+ return;
214
+ }
215
+
216
+ try {
217
+ // Fetch transaction from blockchain
218
+ const tx = await this.connection!.getTransaction(signature, {
219
+ maxSupportedTransactionVersion: 0,
220
+ });
221
+
222
+ if (!tx) {
223
+ throw new Error('Transaction not found');
224
+ }
225
+
226
+ // Parse and insert into Supabase
227
+ // This would require parsing the transaction logs
228
+ // to extract stake/unstake/reward information
229
+
230
+ console.log('Syncing transaction:', signature);
231
+ } catch (error) {
232
+ errorHandler.handleError(error as Error, 'SyncService.syncTransaction');
233
+ }
234
+ }
235
+ }
236
+
237
+ // Export singleton instance
238
+ export const syncService = new SyncService();
@@ -0,0 +1,233 @@
1
+ import { PublicKey } from '@solana/web3.js';
2
+ import { BN } from '@coral-xyz/anchor';
3
+
4
+ // Core platform types
5
+ export interface PredictionPlatform {
6
+ admin: PublicKey;
7
+ totalVaults: BN;
8
+ currentEpoch: BN;
9
+ platformFee: number;
10
+ earlyUnstakeFee: number;
11
+ isPaused: boolean;
12
+ }
13
+
14
+ // Blockchain Vault type (from Anchor program)
15
+ export interface BlockchainVault {
16
+ id: BN;
17
+ aprRate: BN;
18
+ totalStaked: BN;
19
+ stakerCount: BN;
20
+ currentEpoch: BN;
21
+ isActive: boolean;
22
+ tokenMint: PublicKey;
23
+ currentRewardPool: BN;
24
+ totalRewardsDistributed: BN;
25
+ createdAt: BN;
26
+ lastUpdated: BN;
27
+ }
28
+
29
+ export interface UserStake {
30
+ user: PublicKey;
31
+ vaultId: BN;
32
+ amount: BN;
33
+ epochJoined: BN;
34
+ lastRewardEpoch: BN;
35
+ pendingRewards: BN;
36
+ stakedAt: BN;
37
+ cooldownEnd?: BN;
38
+ }
39
+
40
+ export interface Epoch {
41
+ id: BN;
42
+ vaultId: BN;
43
+ startTime: BN;
44
+ endTime?: BN;
45
+ totalRewards: BN;
46
+ participants: BN;
47
+ isActive: boolean;
48
+ rewardRate: BN;
49
+ }
50
+
51
+ // Frontend state types
52
+ export interface WalletState {
53
+ connected: boolean;
54
+ publicKey: PublicKey | null;
55
+ balance: number;
56
+ connecting: boolean;
57
+ disconnecting: boolean;
58
+ }
59
+
60
+ export interface VaultDisplayData {
61
+ id: number;
62
+ aprRate: number;
63
+ totalStaked: number;
64
+ stakerCount: number;
65
+ userStake: number;
66
+ currentRewardPool: number;
67
+ isActive: boolean;
68
+ loading: boolean;
69
+ historicalAPR: APRHistoryPoint[];
70
+ }
71
+
72
+ export interface APRHistoryPoint {
73
+ date: string;
74
+ apr: number;
75
+ totalStaked: number;
76
+ participants: number;
77
+ }
78
+
79
+ export interface UserPortfolio {
80
+ totalStaked: number;
81
+ totalRewards: number;
82
+ activeVaults: number;
83
+ portfolioValue: number;
84
+ performanceMetrics: PerformanceData;
85
+ }
86
+
87
+ export interface PerformanceData {
88
+ totalReturn: number;
89
+ annualizedReturn: number;
90
+ sharpeRatio: number;
91
+ maxDrawdown: number;
92
+ winRate: number;
93
+ }
94
+
95
+ export interface TransactionHistory {
96
+ id: string;
97
+ type: 'stake' | 'unstake' | 'claim' | 'reward';
98
+ amount: number;
99
+ vaultId: number;
100
+ timestamp: Date;
101
+ signature: string;
102
+ status: 'pending' | 'confirmed' | 'failed';
103
+ }
104
+
105
+ export interface PlatformStats {
106
+ totalValueLocked: number;
107
+ totalUsers: number;
108
+ totalVaults: number;
109
+ averageAPR: number;
110
+ currentEpoch: number;
111
+ totalRewardsDistributed: number;
112
+ }
113
+
114
+ // UI component types
115
+ export interface StakeModalProps {
116
+ isOpen: boolean;
117
+ onClose: () => void;
118
+ vault: VaultDisplayData;
119
+ onStake: (amount: number) => Promise<void>;
120
+ }
121
+
122
+ export interface UnstakeModalProps {
123
+ isOpen: boolean;
124
+ onClose: () => void;
125
+ vault: VaultDisplayData;
126
+ userStake: number;
127
+ onUnstake: (amount: number, immediate: boolean) => Promise<void>;
128
+ }
129
+
130
+ export interface VaultCardProps {
131
+ vault: VaultDisplayData;
132
+ onStake: () => void;
133
+ onUnstake: () => void;
134
+ onViewDetails: () => void;
135
+ }
136
+
137
+ // Error types
138
+ export interface PlatformError {
139
+ code: string;
140
+ message: string;
141
+ details?: any;
142
+ }
143
+
144
+ // Theme types
145
+ export interface Theme {
146
+ colors: {
147
+ primary: string;
148
+ secondary: string;
149
+ background: string;
150
+ surface: string;
151
+ border: string;
152
+ text: string;
153
+ textSecondary: string;
154
+ success: string;
155
+ warning: string;
156
+ error: string;
157
+ accent: string;
158
+ };
159
+ spacing: {
160
+ xs: string;
161
+ sm: string;
162
+ md: string;
163
+ lg: string;
164
+ xl: string;
165
+ xxl: string;
166
+ };
167
+ borderRadius: {
168
+ sm: string;
169
+ md: string;
170
+ lg: string;
171
+ xl: string;
172
+ };
173
+ shadows: {
174
+ sm: string;
175
+ md: string;
176
+ lg: string;
177
+ xl: string;
178
+ };
179
+ breakpoints?: {
180
+ mobile: string;
181
+ tablet: string;
182
+ desktop: string;
183
+ wide: string;
184
+ ultrawide: string;
185
+ };
186
+ }
187
+
188
+ // API response types
189
+ export interface ApiResponse<T> {
190
+ success: boolean;
191
+ data?: T;
192
+ error?: PlatformError;
193
+ timestamp: string;
194
+ }
195
+
196
+ // Sustainability types
197
+ export interface SustainabilityMetrics {
198
+ energyConsumption: number;
199
+ carbonFootprint: number;
200
+ resourceEfficiency: number;
201
+ renewablePercentage: number;
202
+ sustainabilityScore: number;
203
+ }
204
+
205
+ // Property test types for correctness validation
206
+ export interface PropertyTestConfig {
207
+ iterations: number;
208
+ seed?: number;
209
+ timeout?: number;
210
+ verbose?: boolean;
211
+ }
212
+
213
+ export interface PropertyTestResult {
214
+ property: string;
215
+ passed: boolean;
216
+ iterations: number;
217
+ counterexample?: any;
218
+ error?: string;
219
+ }
220
+
221
+ // Real-time sync types
222
+ export interface SyncState {
223
+ lastSync: Date;
224
+ syncing: boolean;
225
+ syncError?: string;
226
+ pendingUpdates: number;
227
+ }
228
+
229
+ // Export utility types
230
+ export type VaultId = number;
231
+ export type EpochId = number;
232
+ export type UserId = string;
233
+ export type TransactionSignature = string;