forked from InsurNiffy/niff-Stellar-shurance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaim.ts
More file actions
87 lines (79 loc) · 2.65 KB
/
Copy pathclaim.ts
File metadata and controls
87 lines (79 loc) · 2.65 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
import { IpfsUploadResponse } from '../types/claim';
import { getConfig } from '@/config/env';
const { apiUrl: API_BASE_URL } = getConfig();
export interface Claim {
id: number;
policyId: string;
creatorAddress: string;
status: 'PENDING' | 'APPROVED' | 'REJECTED' | 'PAID';
amount: string;
description?: string;
evidenceHash?: string;
createdAt: string;
updatedAt: string;
}
export interface BuildClaimTransactionResponse {
unsignedXdr: string;
minResourceFee: string;
baseFee: string;
totalEstimatedFee: string;
totalEstimatedFeeXlm: string;
authRequirements: Array<{ address: string; isContract: boolean }>;
}
export class ClaimAPI {
private static async handleResponse<T>(response: Response): Promise<T> {
if (!response.ok) {
const errorData = await response.json().catch(() => ({
message: 'An unexpected error occurred',
}));
throw new Error(errorData.message || 'API Error');
}
return response.json();
}
static async buildTransaction(data: {
holder: string;
policyId: number;
amount: string;
details: string;
imageUrls: string[];
}): Promise<BuildClaimTransactionResponse> {
const response = await fetch(`${API_BASE_URL}/api/claims/build-transaction`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
});
return this.handleResponse<BuildClaimTransactionResponse>(response);
}
static async submitTransaction(transactionXdr: string): Promise<{ claimId: number; transactionHash: string }> {
const response = await fetch(`${API_BASE_URL}/api/claims/submit`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ transactionXdr }),
});
return this.handleResponse<{ claimId: number; transactionHash: string }>(response);
}
static async getClaim(claimId: number): Promise<Claim> {
const response = await fetch(`${API_BASE_URL}/api/claims/${claimId}`);
return this.handleResponse<Claim>(response);
}
static async pollClaimStatus(
claimId: number,
maxAttempts = 20,
interval = 3000
): Promise<Claim> {
let attempts = 0;
while (attempts < maxAttempts) {
try {
const claim = await this.getClaim(claimId);
// In a real app, we might wait for the status to change from a temporary one
// but for now, if we get the claim, it's a good sign.
return claim;
} catch (error) {
if (attempts === maxAttempts - 1) throw error;
await new Promise((resolve) => setTimeout(resolve, interval));
attempts++;
}
}
throw new Error('Claim confirmation timeout');
}
}