forked from StellarSend/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
467 lines (407 loc) · 15.6 KB
/
Copy pathapi.ts
File metadata and controls
467 lines (407 loc) · 15.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
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from 'axios'
import type {
AccountInfo,
AccountStats,
ApiError,
Quote,
QuoteRequest,
SendPaymentRequest,
SendPaymentResult,
TransactionFilters,
TransactionPage,
PaginationParams,
Subscription,
CreateSubscriptionRequest,
BatchPaymentRequest,
BatchPaymentResult,
PaymentRequest,
CreatePaymentRequestPayload,
Escrow,
CreateEscrowRequest,
} from '@/types'
// ─── Config ───────────────────────────────────────────────────────────────────
const BASE_URL = (import.meta.env.VITE_API_URL as string) || 'http://localhost:8080'
// ─── Normalized API errors ────────────────────────────────────────────────────
/**
* A real `Error` subclass carrying the same `code`/`details` fields as
* `ApiError`, rejected by every request through `apiClient` in place of a
* plain `{ code, message, details }` object literal. The plain-object
* rejection made `error instanceof Error` — used by the global React Query
* retry predicate in `App.tsx` (and assumed by every hook's `useQuery<T,
* Error>` type parameter) — always false for backend-routed failures,
* silently defeating the "don't retry 404s" check (#60).
*/
export class ApiRequestError extends Error implements ApiError {
code: string
details?: Record<string, string[]>
constructor(apiError: ApiError) {
super(apiError.message)
this.name = 'ApiRequestError'
this.code = apiError.code
this.details = apiError.details
}
}
/**
* Turns a rejected axios response into an `ApiRequestError`. Exported
* separately from the interceptor so it can be unit-tested directly instead
* of relying on axios's internal interceptor-handler storage.
*
* `code` prefers the backend's own `data.code`; when the backend doesn't
* send one, a 404 status is normalized to the stable `'NOT_FOUND'` code
* (HTTP status is a reliable, backend-convention-independent signal, unlike
* substring-matching `message`) rather than falling through to
* `'UNKNOWN_ERROR'`.
*/
export function normalizeApiError(error: AxiosError<ApiError>): ApiRequestError {
const message =
error.response?.data?.message || error.message || 'An unexpected error occurred'
const code =
error.response?.data?.code || (error.response?.status === 404 ? 'NOT_FOUND' : 'UNKNOWN_ERROR')
return new ApiRequestError({
code,
message,
details: error.response?.data?.details,
})
}
// ─── Axios instance ───────────────────────────────────────────────────────────
const createApiClient = (): AxiosInstance => {
const client = axios.create({
baseURL: BASE_URL,
timeout: 30_000,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
})
// Request interceptor — attach auth token if present
client.interceptors.request.use(
(config) => {
const token = localStorage.getItem('stellarsend_token')
if (token && config.headers) {
config.headers.Authorization = `Bearer ${token}`
}
return config
},
(error) => Promise.reject(error),
)
// Response interceptor — normalise errors
client.interceptors.response.use(
(response) => response,
(error: AxiosError<ApiError>) => Promise.reject(normalizeApiError(error)),
)
return client
}
export const apiClient: AxiosInstance = createApiClient()
// ─── Helper ───────────────────────────────────────────────────────────────────
async function request<T>(config: AxiosRequestConfig): Promise<T> {
const response = await apiClient.request<{ data: T }>(config)
// Handle both { data: T } and plain T shapes
return (response.data as unknown as { data: T }).data ?? (response.data as unknown as T)
}
// ─── Account ──────────────────────────────────────────────────────────────────
export const accountApi = {
getAccount: (publicKey: string) =>
request<AccountInfo>({ method: 'GET', url: `/accounts/${publicKey}` }),
getStats: (publicKey: string) =>
request<AccountStats>({ method: 'GET', url: `/accounts/${publicKey}/stats` }),
}
// ─── Quotes ───────────────────────────────────────────────────────────────────
export const quoteApi = {
getQuote: (params: QuoteRequest) =>
request<Quote>({ method: 'POST', url: '/quotes', data: params }),
refreshQuote: (quoteId: string) =>
request<Quote>({ method: 'POST', url: `/quotes/${quoteId}/refresh` }),
}
// ─── Payments ─────────────────────────────────────────────────────────────────
export const paymentApi = {
send: (payload: SendPaymentRequest) =>
request<SendPaymentResult>({ method: 'POST', url: '/payments', data: payload }),
getStatus: (txHash: string) =>
request<SendPaymentResult>({ method: 'GET', url: `/payments/${txHash}` }),
buildTransaction: (params: {
sourceAccount: string
destinationAccount: string
amount: string
assetCode: string
assetIssuer: string | null
memo?: string
usePathPayment: boolean
sendAssetCode?: string
sendAssetIssuer?: string | null
path?: Array<{ assetCode: string; assetIssuer: string | null }>
slippageTolerance?: string
}) =>
request<{ xdr: string; fee: string }>({
method: 'POST',
url: '/payments/build',
data: params,
}),
}
// ─── Subscriptions (recurring payments) ───────────────────────────────────────
// Assumed backend surface — paths may need small adjustments once the backend
// team's actual routes land, but the shapes here follow the same
// request/response conventions as quoteApi/paymentApi above.
export const subscriptionApi = {
list: (publicKey: string) =>
request<Subscription[]>({
method: 'GET',
url: '/api/subscriptions',
params: { publicKey },
}),
get: (subscriptionId: string) =>
request<Subscription>({
method: 'GET',
url: `/api/subscriptions/${subscriptionId}`,
}),
/** Ask the backend to build the unsigned XDR for the first-payment / authorization transaction. */
buildCreateTransaction: (payload: CreateSubscriptionRequest) =>
request<{ xdr: string; fee: string }>({
method: 'POST',
url: '/api/subscriptions/build',
data: payload,
}),
/** Submit the Freighter-signed XDR to register the recurring schedule. */
create: (payload: CreateSubscriptionRequest & { signedXdr: string }) =>
request<Subscription>({
method: 'POST',
url: '/api/subscriptions',
data: payload,
}),
buildCancelTransaction: (subscriptionId: string) =>
request<{ xdr: string; fee: string }>({
method: 'POST',
url: `/api/subscriptions/${subscriptionId}/cancel/build`,
}),
cancel: (subscriptionId: string, signedXdr?: string) =>
request<Subscription>({
method: 'POST',
url: `/api/subscriptions/${subscriptionId}/cancel`,
data: { signedXdr },
}),
}
// ─── Batch / split payments ───────────────────────────────────────────────────
export const batchPaymentApi = {
build: (params: {
sourcePublicKey: string
assetCode: string
assetIssuer: string | null
recipients: { destinationAddress: string; amount: string; memo?: string }[]
}) =>
request<{ xdr: string; fee: string }>({
method: 'POST',
url: '/api/payments/batch/build',
data: params,
}),
send: (payload: BatchPaymentRequest) =>
request<BatchPaymentResult>({
method: 'POST',
url: '/api/payments/batch',
data: payload,
}),
getStatus: (batchId: string) =>
request<BatchPaymentResult>({
method: 'GET',
url: `/api/payments/batch/${batchId}`,
}),
}
// ─── Payment requests / invoicing ─────────────────────────────────────────────
export const paymentRequestApi = {
create: (payload: CreatePaymentRequestPayload) =>
request<PaymentRequest>({
method: 'POST',
url: '/api/payment-requests',
data: payload,
}),
get: (requestId: string) =>
request<PaymentRequest>({
method: 'GET',
url: `/api/payment-requests/${requestId}`,
}),
list: (publicKey: string) =>
request<PaymentRequest[]>({
method: 'GET',
url: '/api/payment-requests',
params: { publicKey },
}),
cancel: (requestId: string) =>
request<PaymentRequest>({
method: 'POST',
url: `/api/payment-requests/${requestId}/cancel`,
}),
}
// ─── Escrow / conditional transfers ───────────────────────────────────────────
export const escrowApi = {
list: (publicKey: string) =>
request<Escrow[]>({
method: 'GET',
url: '/api/escrows',
params: { publicKey },
}),
get: (escrowId: string) =>
request<Escrow>({
method: 'GET',
url: `/api/escrows/${escrowId}`,
}),
buildCreateTransaction: (payload: CreateEscrowRequest) =>
request<{ xdr: string; fee: string }>({
method: 'POST',
url: '/api/escrows/build',
data: payload,
}),
create: (payload: CreateEscrowRequest & { signedXdr: string }) =>
request<Escrow>({
method: 'POST',
url: '/api/escrows',
data: payload,
}),
buildReleaseTransaction: (escrowId: string) =>
request<{ xdr: string; fee: string }>({
method: 'POST',
url: `/api/escrows/${escrowId}/release/build`,
}),
release: (escrowId: string, signedXdr: string) =>
request<Escrow>({
method: 'POST',
url: `/api/escrows/${escrowId}/release`,
data: { signedXdr },
}),
buildRefundTransaction: (escrowId: string) =>
request<{ xdr: string; fee: string }>({
method: 'POST',
url: `/api/escrows/${escrowId}/refund/build`,
}),
refund: (escrowId: string, signedXdr: string) =>
request<Escrow>({
method: 'POST',
url: `/api/escrows/${escrowId}/refund`,
data: { signedXdr },
}),
}
// ─── Transactions ─────────────────────────────────────────────────────────────
export const transactionApi = {
list: (
publicKey: string,
pagination: PaginationParams,
filters?: TransactionFilters,
) =>
request<TransactionPage>({
method: 'GET',
url: `/accounts/${publicKey}/transactions`,
params: {
page: pagination.page,
page_size: pagination.pageSize,
cursor: pagination.cursor,
...filters,
},
}),
get: (txHash: string) =>
request<import('@/types').Transaction>({
method: 'GET',
url: `/transactions/${txHash}`,
}),
}
// ─── Stellar Horizon fallback ─────────────────────────────────────────────────
// Used when the backend is unavailable — calls Horizon directly.
const HORIZON_TESTNET = 'https://horizon-testnet.stellar.org'
const HORIZON_MAINNET = 'https://horizon.stellar.org'
/** Resolves the Horizon host for `network` — always thread the caller's
* currently selected network through here rather than hardcoding a host;
* a hardcoded mainnet URL is what made the now-removed `useStellarAccount`
* hook silently 404 on valid testnet accounts (#21). */
export function horizonUrl(network: 'testnet' | 'mainnet') {
return network === 'testnet' ? HORIZON_TESTNET : HORIZON_MAINNET
}
/** Fetches full account state from Horizon for the given `network`. This is
* the network-aware source of truth `useWallet`'s account state is built
* on — prefer it (or that hook) over any new ad-hoc Horizon call. */
export async function fetchAccountFromHorizon(
publicKey: string,
network: 'testnet' | 'mainnet',
): Promise<AccountInfo> {
const url = `${horizonUrl(network)}/accounts/${publicKey}`
const res = await fetch(url)
if (!res.ok) {
if (res.status === 404) throw new Error('Account not found on Stellar network')
throw new Error(`Horizon error: ${res.status}`)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const raw: any = await res.json()
return {
publicKey: raw.account_id,
sequence: raw.sequence,
subentryCount: raw.subentry_count,
lastModifiedLedger: raw.last_modified_ledger,
thresholds: {
lowThreshold: raw.thresholds.low_threshold,
medThreshold: raw.thresholds.med_threshold,
highThreshold: raw.thresholds.high_threshold,
},
flags: {
authRequired: raw.flags.auth_required,
authRevocable: raw.flags.auth_revocable,
authImmutable: raw.flags.auth_immutable,
},
balances: (raw.balances || []).map(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(b: any): import('@/types').Balance => ({
asset: {
code: b.asset_type === 'native' ? 'XLM' : b.asset_code,
issuer: b.asset_type === 'native' ? null : b.asset_issuer,
name: b.asset_type === 'native' ? 'Stellar Lumens' : b.asset_code,
decimals: 7,
},
balance: b.balance,
buyingLiabilities: b.buying_liabilities || '0',
sellingLiabilities: b.selling_liabilities || '0',
limit: b.limit,
}),
),
}
}
export async function fetchTransactionsFromHorizon(
publicKey: string,
network: 'testnet' | 'mainnet',
limit = 20,
cursor?: string,
): Promise<TransactionPage> {
const base = `${horizonUrl(network)}/accounts/${publicKey}/transactions`
const params = new URLSearchParams({
limit: String(limit),
order: 'desc',
include_failed: 'true',
})
if (cursor) params.set('cursor', cursor)
const res = await fetch(`${base}?${params}`)
if (!res.ok) throw new Error(`Horizon error: ${res.status}`)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const raw: any = await res.json()
const records = raw._embedded?.records || []
return {
page: 1,
pageSize: limit,
total: records.length,
hasMore: records.length === limit,
cursor: records[records.length - 1]?.paging_token,
transactions: records.map(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(r: any): import('@/types').Transaction => ({
id: r.id,
hash: r.hash,
createdAt: r.created_at,
type: 'payment',
status: r.successful ? 'success' : 'failed',
sourceAccount: r.source_account,
destinationAccount: publicKey,
amount: '0',
assetCode: 'XLM',
assetIssuer: null,
fee: r.fee_charged,
ledger: r.ledger,
memo: r.memo,
direction: r.source_account === publicKey ? 'sent' : 'received',
counterparty:
r.source_account === publicKey ? publicKey : r.source_account,
}),
),
}
}