-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathaggregator.ts
More file actions
782 lines (701 loc) · 22.6 KB
/
aggregator.ts
File metadata and controls
782 lines (701 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
import axios from "axios";
import type {
RatePayload,
RateResponse,
InstitutionProps,
PubkeyResponse,
VerifyAccountPayload,
InitiateKYCPayload,
InitiateKYCResponse,
KYCStatusResponse,
OrderDetailsResponse,
TransactionResponse,
TransactionCreateInput,
SaveTransactionResponse,
UpdateTransactionDetailsPayload,
UpdateTransactionStatusPayload,
APIToken,
RecipientDetails,
RecipientDetailsWithId,
SavedRecipientsResponse,
} from "../types";
import {
trackServerEvent,
trackBusinessEvent,
trackApiRequest,
trackApiResponse,
} from "../lib/server-analytics";
const AGGREGATOR_URL = process.env.NEXT_PUBLIC_AGGREGATOR_URL;
/**
* Fetches the current exchange rate for a given token and currency pair
* @param {RatePayload} params - The rate request parameters
* @param {string} params.token - The token symbol
* @param {number} [params.amount=1] - The amount to convert
* @param {string} params.currency - The target currency
* @param {string} [params.providerId] - Optional provider ID
* @param {string} [params.network] - Optional network identifier (e.g., "arbitrum-one", "polygon")
* @returns {Promise<RateResponse>} The rate response containing exchange rate and fees
* @throws {Error} If the API request fails or returns an error
*/
export const fetchRate = async ({
token,
amount = 1,
currency,
providerId,
network,
}: RatePayload): Promise<RateResponse> => {
const startTime = Date.now();
try {
// Track external API request
trackServerEvent("External API Request", {
service: "aggregator",
endpoint: "/rates",
method: "GET",
token,
amount,
currency,
provider_id: providerId,
network,
});
const endpoint = `${AGGREGATOR_URL}/rates/${token}/${amount}/${currency}`;
const params: Record<string, string> = {};
if (providerId) {
params.provider_id = providerId;
}
if (network) {
params.network = network;
}
const response = await axios.get(endpoint, { params });
const { data } = response;
// Track successful response
const responseTime = Date.now() - startTime;
trackApiResponse("/rates", "GET", 200, responseTime, {
service: "aggregator",
token,
amount,
currency,
provider_id: providerId,
network,
rate: data.data,
});
// Track business event
trackBusinessEvent("Rate Fetched", {
token,
amount,
currency,
provider_id: providerId,
network,
rate: data.data,
});
// Check the API response status first
if (data.status === "error") {
throw new Error(data.message || "Provider not found");
}
return data;
} catch (error) {
const responseTime = Date.now() - startTime;
// Track API error
trackServerEvent("External API Error", {
service: "aggregator",
endpoint: "/rates",
method: "GET",
token,
amount,
currency,
provider_id: providerId,
network,
error_message: error instanceof Error ? error.message : "Unknown error",
response_time_ms: responseTime,
});
if (axios.isAxiosError(error)) {
const message = error.response?.data?.message || error.message;
throw new Error(message);
}
console.error("Error fetching rate:", error);
throw error;
}
};
/**
* Fetches the list of supported institutions for a given currency
* @param {string} currency - The currency code to get institutions for
* @returns {Promise<InstitutionProps[]>} Array of supported institutions
* @throws {Error} If the API request fails
*/
export const fetchSupportedInstitutions = async (
currency: string,
): Promise<InstitutionProps[]> => {
try {
const response = await axios.get(
`${AGGREGATOR_URL}/institutions/${currency}`,
);
return response.data.data;
} catch (error) {
console.error("Error fetching supported institutions:", error);
throw error;
}
};
/**
* Fetches the aggregator's public key for encryption
* @returns {Promise<PubkeyResponse>} The public key response
* @throws {Error} If the API request fails
*/
export const fetchAggregatorPublicKey = async (): Promise<PubkeyResponse> => {
try {
const response = await axios.get(`${AGGREGATOR_URL}/pubkey`);
return response.data;
} catch (error) {
console.error("Error fetching aggregator public key:", error);
throw error;
}
};
/**
* Verifies an account number and returns the account name
* @param {VerifyAccountPayload} payload - The account verification payload
* @returns {Promise<string>} The account holder's name
* @throws {Error} If the API request fails
*/
export const fetchAccountName = async (
payload: VerifyAccountPayload,
): Promise<string> => {
const startTime = Date.now();
try {
// Track external API request
trackServerEvent("External API Request", {
service: "aggregator",
endpoint: "/verify-account",
method: "POST",
institution: payload.institution,
// account_identifier omitted to avoid PII in analytics
});
const response = await axios.post(
`${AGGREGATOR_URL}/verify-account`,
payload,
);
// Track successful response
const responseTime = Date.now() - startTime;
trackApiResponse("/verify-account", "POST", 200, responseTime, {
service: "aggregator",
institution: payload.institution,
// account_identifier omitted
// account_name omitted
});
// Track business event
trackBusinessEvent("Account Verification", {
institution: payload.institution,
});
return response.data.data;
} catch (error) {
const responseTime = Date.now() - startTime;
// Track API error
trackServerEvent("External API Error", {
service: "aggregator",
endpoint: "/verify-account",
method: "POST",
institution: payload.institution,
// account_identifier omitted
error_message: error instanceof Error ? error.message : "Unknown error",
response_time_ms: responseTime,
});
console.error("Error fetching account name:", error);
throw error;
}
};
/**
* Fetches details of an order by chain ID and order ID
* @param {number} chainId - The blockchain chain ID
* @param {string} orderId - The order ID
* @returns {Promise<OrderDetailsResponse>} The order details
* @throws {Error} If the API request fails
*/
export const fetchOrderDetails = async (
chainId: number,
orderId: string,
): Promise<OrderDetailsResponse> => {
try {
const response = await axios.get(
`${AGGREGATOR_URL}/orders/${chainId}/${orderId}`,
);
return response.data;
} catch (error) {
throw error;
}
};
/**
* Initiates the KYC process for a user
* @param {InitiateKYCPayload} payload - The KYC initiation payload
* @returns {Promise<InitiateKYCResponse>} The KYC initiation response
* @throws {Error} If the API request fails
*/
export const initiateKYC = async (
payload: InitiateKYCPayload,
): Promise<InitiateKYCResponse> => {
const startTime = Date.now();
try {
// Track external API request
trackServerEvent("External API Request", {
service: "aggregator",
endpoint: "/kyc",
method: "POST",
wallet_address: payload.walletAddress,
});
const response = await axios.post(`${AGGREGATOR_URL}/kyc`, payload);
// Track successful response
const responseTime = Date.now() - startTime;
trackApiResponse("/kyc", "POST", 200, responseTime, {
service: "aggregator",
wallet_address: payload.walletAddress,
// kyc_url omitted
});
// Track business event
trackBusinessEvent("KYC Initiated", {
wallet_address: payload.walletAddress,
});
return response.data;
} catch (error) {
const responseTime = Date.now() - startTime;
// Track API error
trackServerEvent("External API Error", {
service: "aggregator",
endpoint: "/kyc",
method: "POST",
wallet_address: payload.walletAddress,
error_message: error instanceof Error ? error.message : "Unknown error",
response_time_ms: responseTime,
});
throw error;
}
};
/**
* Fetches the KYC status for a wallet address
* @param {string} walletAddress - The wallet address to check
* @returns {Promise<KYCStatusResponse>} The KYC status response
* @throws {Error} If the API request fails
*/
export const fetchKYCStatus = async (
walletAddress: string,
): Promise<KYCStatusResponse> => {
const startTime = Date.now();
try {
// Track external API request
trackServerEvent("External API Request", {
service: "aggregator",
endpoint: "/kyc/status",
method: "GET",
wallet_address: walletAddress,
});
const response = await axios.get(`${AGGREGATOR_URL}/kyc/${walletAddress}`);
// Track successful response
const responseTime = Date.now() - startTime;
trackApiResponse("/kyc/status", "GET", 200, responseTime, {
service: "aggregator",
wallet_address: walletAddress,
kyc_status: response.data.data?.status,
});
// Track business event
trackBusinessEvent("KYC Status Checked", {
wallet_address: walletAddress,
kyc_status: response.data.data?.status,
});
return response.data;
} catch (error) {
const responseTime = Date.now() - startTime;
// Track API error
trackServerEvent("External API Error", {
service: "aggregator",
endpoint: "/kyc/status",
method: "GET",
wallet_address: walletAddress,
error_message: error instanceof Error ? error.message : "Unknown error",
response_time_ms: responseTime,
});
throw error;
}
};
/**
* Detects the user's location based on their IP address
* @returns {Promise<string>} The country code of the user's location
* @throws {Error} If the API request fails
*/
export const detectUserLocation = async (): Promise<string> => {
try {
const response = await axios.get("https://ipapi.co/json/");
return response.data.country_code;
} catch (error) {
console.error("Error detecting user location:", error);
return "";
}
};
/**
* Fetches transactions for a wallet address with pagination
* @param {string} address - The wallet address
* @param {string} accessToken - The access token for authentication
* @param {number} [page=1] - The page number
* @param {number} [limit=20] - The number of items per page
* @returns {Promise<TransactionResponse>} The transactions response
* @throws {Error} If the API request fails
*/
export async function fetchTransactions(
address: string,
accessToken: string,
page: number = 1,
limit: number = 20,
): Promise<TransactionResponse> {
const response = await axios.get<TransactionResponse>(
`/api/v1/transactions?page=${page}&limit=${limit}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
"x-wallet-address": address.toLowerCase(),
},
},
);
return response.data;
}
/**
* Saves a new transaction to the database
* @param {TransactionCreateInput} transaction - The transaction data to save
* @param {string} accessToken - The access token for authentication
* @returns {Promise<SaveTransactionResponse>} The save response
* @throws {Error} If the API request fails
*/
export async function saveTransaction(
transaction: TransactionCreateInput,
accessToken: string,
): Promise<SaveTransactionResponse> {
const response = await axios.post("/api/v1/transactions", transaction, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
return response.data;
}
/**
* Updates the status of a transaction
* @param {string} transactionId - The ID of the transaction to update
* @param {string} status - The new status to set
* @param {string} accessToken - The access token for authentication
* @param {string} walletAddress - The wallet address for authorization
* @returns {Promise<SaveTransactionResponse>} The update response
* @throws {Error} If the API request fails
*/
export async function updateTransactionStatus({
transactionId,
status,
accessToken,
walletAddress,
}: UpdateTransactionStatusPayload): Promise<SaveTransactionResponse> {
const finalStatus = ["validated", "settled"].includes(status)
? "completed"
: status;
const response = await axios.put(
`/api/v1/transactions/status/${transactionId}`,
{ status: finalStatus },
{
headers: {
Authorization: `Bearer ${accessToken}`,
"x-wallet-address": walletAddress.toLowerCase(),
},
},
);
return response.data;
}
/**
* Updates the details of a transaction including status, hash, and time spent
* @param {Object} params - The parameters object
* @param {string} params.transactionId - The ID of the transaction to update
* @param {string} params.status - The new status to set
* @param {string} [params.txHash] - The transaction hash (optional)
* @param {string} [params.timeSpent] - The time spent on the transaction (optional)
* @param {string} params.accessToken - The access token for authentication
* @param {string} params.walletAddress - The wallet address for authorization
* @returns {Promise<SaveTransactionResponse>} The update response
* @throws {Error} If the API request fails
*/
export async function updateTransactionDetails({
transactionId,
status,
txHash,
timeSpent,
accessToken,
walletAddress,
}: UpdateTransactionDetailsPayload): Promise<SaveTransactionResponse> {
const finalStatus = ["validated", "settled"].includes(status)
? "completed"
: status;
// Build the data object dynamically
const data: Record<string, any> = { status: finalStatus };
if (txHash !== undefined && txHash !== null && txHash !== "") {
data.txHash = txHash;
}
if (timeSpent !== undefined && timeSpent !== null && timeSpent !== "") {
data.timeSpent = timeSpent;
}
const response = await axios.put(
`/api/v1/transactions/${transactionId}`,
data,
{
headers: {
Authorization: `Bearer ${accessToken}`,
"x-wallet-address": walletAddress.toLowerCase(),
},
},
);
return response.data;
}
/**
* Reindexes a transaction on the Paycrest API with exponential retry
* @param {string} network - The network identifier (e.g., "base", "bnb-smart-chain", "polygon")
* @param {string} txHash - The transaction hash to reindex
* @param {number} retryCount - Current retry attempt (internal use)
* @param {number} maxRetries - Maximum number of retries (default: 3)
* @returns {Promise<any>} The reindex response
* @throws {Error} If the API request fails after all retries
*/
export async function reindexTransaction(
network: string,
txHash: string,
retryCount: number = 0,
maxRetries: number = 3,
): Promise<any> {
const startTime = Date.now();
try {
// Track external API request
trackServerEvent("External API Request", {
service: "aggregator",
endpoint: `/reindex/${network}/${txHash}`,
method: "GET",
network,
tx_hash: txHash,
retry_attempt: retryCount,
});
const endpoint = `${AGGREGATOR_URL}/reindex/${network}/${txHash}`;
const response = await axios.get(endpoint);
// Track successful response (2xx status)
const responseTime = Date.now() - startTime;
const status = response.status;
trackApiResponse(
`/reindex/${network}/${txHash}`,
"GET",
status,
responseTime,
{
service: "aggregator",
network,
tx_hash: txHash,
retry_attempt: retryCount,
},
);
// Track business event
trackBusinessEvent("Transaction Reindexed", {
network,
tx_hash: txHash,
retry_attempt: retryCount,
});
return response.data;
} catch (error: any) {
const responseTime = Date.now() - startTime;
const status = error.response?.status;
// Check if we should retry:
// 1. Network errors (no response) - retry (transient)
// 2. 5xx server errors - retry (transient)
// 3. 4xx client errors - do NOT retry (bad request, fail fast)
// Note: axios throws errors for status >= 400, so 2xx responses won't reach here
const isNetworkError = !error.response;
const is5xxError = status !== undefined && status >= 500;
// retryCount + 1 represents the next attempt number; ensure it doesn't exceed maxRetries
const shouldRetry =
(isNetworkError || is5xxError) && retryCount + 1 < maxRetries;
if (shouldRetry) {
const delay = Math.pow(2, retryCount) * 1000; // Exponential backoff: 1s, 2s, 4s
const errorType = isNetworkError ? "network error" : `status ${status}`;
console.debug(
`Reindex failed with ${errorType}, retrying in ${delay}ms (attempt ${retryCount + 1}/${maxRetries})`,
);
await new Promise((resolve) => setTimeout(resolve, delay)); // sleep for delay
return reindexTransaction(network, txHash, retryCount + 1, maxRetries);
}
// Track API error
trackApiResponse(
`/reindex/${network}/${txHash}`,
"GET",
status,
responseTime,
{
service: "aggregator",
network,
tx_hash: txHash,
error: error.message,
retry_attempt: retryCount,
},
);
// Re-throw error for caller to handle
throw error;
}
}
/**
* Fetches the list of supported tokens from the aggregator API
* @returns {Promise<APIToken[]>} Array of supported tokens from the API
* @throws {Error} If the API request fails
*/
export const fetchTokens = async (): Promise<APIToken[]> => {
try {
const response = await axios.get(`${AGGREGATOR_URL}/tokens`);
if (response.data?.data && Array.isArray(response.data.data)) {
return response.data.data;
}
return [];
} catch (error) {
console.error("Error fetching supported tokens from API:", error);
throw error;
}
};
/**
* Fetches saved recipients for a wallet address
* @param {string} accessToken - The access token for authentication
* @returns {Promise<RecipientDetailsWithId[]>} Array of saved recipients
* @throws {Error} If the API request fails
*/
export async function fetchSavedRecipients(
accessToken: string,
): Promise<RecipientDetailsWithId[]> {
const response = await axios.get<SavedRecipientsResponse>(
"/api/v1/recipients",
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);
if (!response.data.success) {
throw new Error(response.data.error || "Failed to fetch recipients");
}
return response.data.data;
}
/**
* Saves a new recipient
* @param {RecipientDetails} recipient - The recipient data to save
* @param {string} accessToken - The access token for authentication
* @returns {Promise<boolean>} Success status
* @throws {Error} If the API request fails
*/
export async function saveRecipient(
recipient: RecipientDetails,
accessToken: string,
): Promise<boolean> {
try {
const response = await axios.post("/api/v1/recipients", recipient, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
if (!response.data.success) {
throw new Error(response.data.error || "Failed to save recipient");
}
return true;
} catch (error) {
if (axios.isAxiosError(error)) {
const errorData = error.response?.data;
throw new Error(errorData?.error || error.message);
}
throw error;
}
}
/**
* Deletes a saved recipient
* @param {string} recipientId - The ID of the recipient to delete
* @param {string} accessToken - The access token for authentication
* @returns {Promise<boolean>} Success status
* @throws {Error} If the API request fails
*/
export async function deleteSavedRecipient(
recipientId: string,
accessToken: string,
): Promise<boolean> {
const response = await axios.delete(`/api/v1/recipients?id=${recipientId}`, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
if (!response.data.success) {
throw new Error(response.data.error || "Failed to delete recipient");
}
return true;
}
/**
* Migrates recipients from localStorage to Supabase
* @param {string} accessToken - The access token for authentication
* @returns {Promise<void>}
*/
export async function migrateLocalStorageRecipients(
accessToken: string,
): Promise<void> {
const migrationKey = `recipientsMigrated-${localStorage.getItem("userId")}`;
// Check if migration has already been done
if (localStorage.getItem(migrationKey)) {
return;
}
try {
const savedRecipients = localStorage.getItem("savedRecipients");
if (!savedRecipients) {
localStorage.setItem(migrationKey, "true");
return;
}
const recipients: RecipientDetails[] = JSON.parse(savedRecipients);
if (!Array.isArray(recipients) || recipients.length === 0) {
localStorage.setItem(migrationKey, "true");
return;
}
// First, fetch existing recipients from DB to check for duplicates
const existingRecipients = await fetchSavedRecipients(accessToken);
const existingKeys = new Set(
existingRecipients.map((r) => {
if (r.type === "wallet") {
return `wallet-${r.walletAddress}`;
} else {
return `${r.institutionCode}-${r.accountIdentifier}`;
}
}),
);
// Filter out duplicates - only migrate recipients that don't exist in DB
const recipientsToMigrate = recipients.filter((recipient) => {
const key = recipient.type === "wallet"
? `wallet-${recipient.walletAddress}`
: `${recipient.institutionCode}-${recipient.accountIdentifier}`;
return !existingKeys.has(key);
});
if (recipientsToMigrate.length === 0) {
console.log("All recipients already exist in cloud storage");
localStorage.removeItem("savedRecipients");
localStorage.setItem(migrationKey, "true");
return;
}
// Migrate only new recipients to Supabase using batch processing
const migrationPromises = recipientsToMigrate.map(async (recipient) => {
try {
await saveRecipient(recipient, accessToken);
return { success: true, recipient };
} catch (error) {
const recipientName = recipient.type === "wallet"
? recipient.walletAddress
: recipient.name;
console.error(`Failed to migrate recipient ${recipientName}:`, error);
return { success: false, recipient, error };
}
});
// Wait for all migrations to complete
const results = await Promise.all(migrationPromises);
const migratedCount = results.filter((r) => r.success).length;
const failedCount = results.filter((r) => !r.success).length;
if (migratedCount > 0) {
console.log(`Migrated ${migratedCount} recipients to cloud storage`);
}
if (failedCount > 0) {
console.warn(`Failed to migrate ${failedCount} recipients`);
}
localStorage.removeItem("savedRecipients");
localStorage.setItem(migrationKey, "true");
} catch (error) {
console.error("Error migrating recipients:", error);
// Don't throw - let the app continue even if migration fails
}
}