-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.ts
More file actions
259 lines (239 loc) · 8.8 KB
/
client.ts
File metadata and controls
259 lines (239 loc) · 8.8 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
import { jsonifyError, Logger } from '@mark/logger';
import { axiosGet, axiosPost } from '@mark/core';
import axios from 'axios';
import {
Reservation,
CreateReservationParams,
ReservationStatus,
NonceAssignment,
InventoryBalance,
PendingInbound,
RegisterInboundParams,
} from './types';
/**
* HTTP client for the unified inventory service (connext-api).
*
* The inventory API is served from the same base URL as the everclear API.
* Uses the same axios helpers (axiosGet/axiosPost) as EverclearAdapter for
* connection pooling, retries, and consistent error handling.
*
* All methods are non-throwing: failures are logged and return undefined/void,
* so Mark degrades gracefully when the inventory service is unavailable.
*/
export class InventoryServiceClient {
private readonly apiUrl: string;
private readonly logger: Logger;
constructor(apiUrl: string, logger: Logger) {
this.apiUrl = apiUrl.replace(/\/$/, '');
this.logger = logger;
}
/**
* Logs API errors with full context, matching EverclearAdapter.logApiError pattern.
*/
private logApiError(message: string, url: string, params: unknown, err: unknown): void {
const errorContext = (err as { context?: Record<string, unknown> }).context;
this.logger.warn(message, {
url,
errorMessage: (err as Error).message,
apiResponseStatus: errorContext?.status,
apiResponseBody: errorContext?.data,
requestParams: params,
error: jsonifyError(err),
});
}
// ── Reservation Management ──────────────────────────────────────────
/**
* Create a reservation for an operation (rebalance, purchase, etc.).
* POST /inventory/reserve
*/
async createReservation(params: CreateReservationParams): Promise<Reservation | undefined> {
const url = `${this.apiUrl}/inventory/reserve`;
try {
const { data } = await axiosPost<Reservation>(url, params, undefined, 1, 0);
return data;
} catch (err) {
this.logApiError('Failed to create reservation (non-blocking)', url, params, err);
return undefined;
}
}
/**
* Update the status of an existing reservation.
* PUT /inventory/reserve/{reservationId}/status
*/
async updateReservationStatus(
reservationId: string,
status: ReservationStatus,
metadata?: Record<string, string>,
): Promise<Reservation | undefined> {
const url = `${this.apiUrl}/inventory/reserve/${reservationId}/status`;
const body = { status, metadata };
try {
const { data } = await axios.put<Reservation>(url, body);
return data;
} catch (err) {
this.logApiError('Failed to update reservation status (non-blocking)', url, body, err);
return undefined;
}
}
/**
* Delete (release) a reservation.
* DELETE /inventory/reserve/{reservationId}
*/
async deleteReservation(reservationId: string): Promise<boolean> {
const url = `${this.apiUrl}/inventory/reserve/${reservationId}`;
try {
const { data } = await axios.delete<{ success: boolean }>(url);
return data?.success ?? false;
} catch (err) {
this.logApiError('Failed to delete reservation (non-blocking)', url, { reservationId }, err);
return false;
}
}
/**
* Get full inventory balance for a (chain, asset) pair.
* GET /inventory/balance/{chainId}/{asset}
*
* Returns on-chain balance, available balance, reserved amounts by type,
* pending inbound, and pending intents.
*/
async getInventoryBalance(chainId: string, asset: string): Promise<InventoryBalance | undefined> {
const url = `${this.apiUrl}/inventory/balance/${chainId}/${encodeURIComponent(asset)}`;
try {
const { data } = await axiosGet<InventoryBalance>(url, undefined, 1, 0);
return data;
} catch (err) {
this.logApiError('Failed to get inventory balance (non-blocking)', url, { chainId, asset }, err);
return undefined;
}
}
/**
* Get all reservations for an operation.
* GET /inventory/operations/{operationId}
*/
async getReservationsByOperation(operationId: string): Promise<Reservation[]> {
const url = `${this.apiUrl}/inventory/operations/${encodeURIComponent(operationId)}`;
try {
const { data } = await axiosGet<{ reservations: Reservation[] }>(url, undefined, 1, 0);
return data?.reservations ?? [];
} catch (err) {
this.logApiError('Failed to get reservations by operation (non-blocking)', url, { operationId }, err);
return [];
}
}
// ── Nonce Management ────────────────────────────────────────────────
/**
* Assign the next sequential nonce for a (chainId, wallet) pair.
* POST /inventory/nonce/assign
*/
async assignNonce(chainId: string, wallet: string, operationId?: string): Promise<NonceAssignment | undefined> {
const url = `${this.apiUrl}/inventory/nonce/assign`;
const body = { chainId, wallet, operationId };
try {
const { data } = await axiosPost<NonceAssignment>(url, body, undefined, 1, 0);
return data;
} catch (err) {
this.logApiError('Failed to assign nonce from inventory service (non-blocking)', url, body, err);
return undefined;
}
}
/**
* Confirm that a previously assigned nonce was successfully included on-chain.
* POST /inventory/nonce/confirm
*/
async confirmNonce(chainId: string, wallet: string, nonce: number, txHash?: string): Promise<void> {
const url = `${this.apiUrl}/inventory/nonce/confirm`;
const body = { chainId, wallet, nonce, txHash };
try {
await axiosPost(url, body, undefined, 1, 0);
} catch (err) {
this.logApiError('Failed to confirm nonce (non-blocking)', url, body, err);
}
}
/**
* Report that a previously assigned nonce failed (transaction not mined / reverted).
* POST /inventory/nonce/fail
*/
async failNonce(chainId: string, wallet: string, nonce: number): Promise<void> {
const url = `${this.apiUrl}/inventory/nonce/fail`;
const body = { chainId, wallet, nonce };
try {
await axiosPost(url, body, undefined, 1, 0);
} catch (err) {
this.logApiError('Failed to report nonce failure (non-blocking)', url, body, err);
}
}
// ── Pending Inbound Tracking ────────────────────────────────────────
/**
* Register expected inbound funds from a cross-chain rebalance.
* POST /inventory/inbound
*/
async registerInbound(params: RegisterInboundParams): Promise<PendingInbound | undefined> {
const url = `${this.apiUrl}/inventory/inbound`;
try {
const { data } = await axiosPost<PendingInbound>(url, params, undefined, 1, 0);
return data;
} catch (err) {
this.logApiError('Failed to register pending inbound (non-blocking)', url, params, err);
return undefined;
}
}
/**
* Confirm that pending inbound funds have arrived.
* POST /inventory/inbound/{inboundId}/confirm
*/
async confirmInbound(inboundId: string, txHash?: string): Promise<PendingInbound | undefined> {
const url = `${this.apiUrl}/inventory/inbound/${inboundId}/confirm`;
const body = { txHash };
try {
const { data } = await axiosPost<PendingInbound>(url, body, undefined, 1, 0);
return data;
} catch (err) {
this.logApiError('Failed to confirm inbound (non-blocking)', url, body, err);
return undefined;
}
}
/**
* Cancel a pending inbound that won't arrive.
* POST /inventory/inbound/{inboundId}/cancel
*/
async cancelInbound(inboundId: string, reason?: string): Promise<PendingInbound | undefined> {
const url = `${this.apiUrl}/inventory/inbound/${inboundId}/cancel`;
const body = { reason };
try {
const { data } = await axiosPost<PendingInbound>(url, body, undefined, 1, 0);
return data;
} catch (err) {
this.logApiError('Failed to cancel inbound (non-blocking)', url, body, err);
return undefined;
}
}
// ── Transaction Result Reporting (convenience wrappers) ─────────────
/**
* Report a successful transaction result for a reservation.
*/
async reportTransactionSuccess(
reservationId: string,
txHash: string,
chainId: string,
metadata?: Record<string, string>,
): Promise<void> {
await this.updateReservationStatus(reservationId, 'COMPLETED', {
txHash,
chainId,
...metadata,
});
}
/**
* Report a failed transaction result for a reservation.
*/
async reportTransactionFailure(
reservationId: string,
reason: string,
metadata?: Record<string, string>,
): Promise<void> {
await this.updateReservationStatus(reservationId, 'FAILED', {
failureReason: reason,
...metadata,
});
}
}