@riftresearch/sdk 0.4.2 → 0.4.3

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.
package/dist/index.d.ts CHANGED
@@ -136,6 +136,9 @@ interface SwapStatusResponse {
136
136
  depositTransaction?: TxHash;
137
137
  quote: QuoteResponse;
138
138
  }
139
+ interface ErrorResponse {
140
+ error: string;
141
+ }
139
142
  /**
140
143
  * Execution actions - the mechanism by which a step is executed.
141
144
  */
@@ -228,11 +231,15 @@ declare const Currencies: {
228
231
  CBBTC: Currency;
229
232
  };
230
233
  };
231
- import { treaty } from "@elysiajs/eden";
232
- import { Elysia } from "elysia";
233
- declare const app: Elysia;
234
+ import { Treaty } from "@elysiajs/eden";
235
+ declare const app: unknown;
234
236
  declare const appTyped: typeof app;
235
237
  type App = typeof appTyped;
238
+ declare class SwapRouterApiError extends Error {
239
+ status: number;
240
+ body: ErrorResponse | undefined;
241
+ constructor(message: string, status: number, body?: ErrorResponse);
242
+ }
236
243
  /**
237
244
  * Create a type-safe API client for the Rift Swap Router.
238
245
  *
@@ -283,7 +290,7 @@ type App = typeof appTyped;
283
290
  * ```
284
291
  */
285
292
  /** Type of the Rift API client */
286
- type RiftClient = ReturnType<typeof treaty<App>>;
293
+ type RiftClient = Treaty.Create<App>;
287
294
  declare function createClient(baseUrl: string): RiftClient;
288
295
  /**
289
296
  * Discriminated union of all possible swap routes.
@@ -447,6 +454,7 @@ declare class RiftSdk {
447
454
  private debug;
448
455
  constructor(options: RiftSdkOptions);
449
456
  private logDebug;
457
+ private unwrapEdenResult;
450
458
  /**
451
459
  * Get a quote for a swap and return a function to execute it.
452
460
  *
@@ -497,4 +505,4 @@ declare class RiftSdk {
497
505
  getSwapStatus(swapId: string): Promise<SwapStatusResponse>;
498
506
  }
499
507
  declare function createRiftSdk(options: RiftSdkOptions): RiftSdk;
500
- export { getSupportedModes, detectRoute, createRiftSdk, createCurrency, createClient, TradeParameters, TokenIdentifier, SwapStatus2 as SwapStatus, SwapRoute, SwapResult, SwapResponse, SupportedModes, SendBitcoinFn, RiftSwap, RiftSdkOptions, RiftSdk, RiftClient, QuoteResult, NativeToken, GetQuoteResult, ExecutionStep, ExecutionAction, EvmChain, EvmCallStep, EvmCallKind, Erc20Token, Currency, Currencies, Chain, BtcTransferStep, BtcTransferKind, BitcoinChain, App };
508
+ export { getSupportedModes, detectRoute, createRiftSdk, createCurrency, createClient, TradeParameters, TokenIdentifier, SwapStatus2 as SwapStatus, SwapRouterApiError, SwapRoute, SwapResult, SwapResponse, SupportedModes, SendBitcoinFn, RiftSwap, RiftSdkOptions, RiftSdk, RiftClient, QuoteResult, NativeToken, GetQuoteResult, ExecutionStep, ExecutionAction, EvmChain, EvmCallStep, EvmCallKind, Erc20Token, Currency, Currencies, Chain, BtcTransferStep, BtcTransferKind, BitcoinChain, App };
package/dist/index.js CHANGED
@@ -85,7 +85,9 @@ var Currencies = {
85
85
  }
86
86
  }
87
87
  };
88
- // src/apiClient.ts
88
+ // src/client.ts
89
+ import { treaty } from "@elysiajs/eden";
90
+
89
91
  class SwapRouterApiError extends Error {
90
92
  status;
91
93
  body;
@@ -96,57 +98,6 @@ class SwapRouterApiError extends Error {
96
98
  this.body = body;
97
99
  }
98
100
  }
99
-
100
- class SwapRouterClient {
101
- baseUrl;
102
- fetch;
103
- constructor(options) {
104
- this.baseUrl = options.baseUrl.replace(/\/$/, "");
105
- this.fetch = options.fetch ?? globalThis.fetch;
106
- }
107
- async request(method, path, body) {
108
- const url = `${this.baseUrl}${path}`;
109
- const response = await this.fetch(url, {
110
- method,
111
- headers: body ? { "Content-Type": "application/json" } : undefined,
112
- body: body ? JSON.stringify(body) : undefined
113
- });
114
- const text = await response.text();
115
- let data;
116
- try {
117
- data = JSON.parse(text);
118
- } catch {
119
- if (!response.ok) {
120
- throw new SwapRouterApiError(text || `Request failed with status ${response.status}`, response.status);
121
- }
122
- throw new SwapRouterApiError(`Invalid JSON response: ${text}`, response.status);
123
- }
124
- if (!response.ok) {
125
- throw new SwapRouterApiError(data.error ?? `Request failed with status ${response.status}`, response.status, data);
126
- }
127
- return data;
128
- }
129
- async health() {
130
- return this.request("GET", "/health");
131
- }
132
- async status() {
133
- return this.request("GET", "/status");
134
- }
135
- async quote(request) {
136
- return this.request("POST", "/quote", request);
137
- }
138
- async createSwap(request) {
139
- return this.request("POST", "/swap", request);
140
- }
141
- async getSwap(swapId) {
142
- return this.request("GET", `/swap/${swapId}`);
143
- }
144
- async reportStepResult(swapId, result) {
145
- await this.request("POST", `/swap/${swapId}/tx`, result);
146
- }
147
- }
148
- // src/client.ts
149
- import { treaty } from "@elysiajs/eden";
150
101
  function createClient(baseUrl) {
151
102
  return treaty(baseUrl);
152
103
  }
@@ -214,9 +165,8 @@ class RiftSdk {
214
165
  integratorName;
215
166
  debug;
216
167
  constructor(options) {
217
- this.riftClient = new SwapRouterClient({
218
- baseUrl: options.apiUrl ?? "https://router-api-v2-production.up.railway.app"
219
- });
168
+ const baseUrl = (options.apiUrl ?? "https://router-api-v2-production.up.railway.app").replace(/\/$/, "");
169
+ this.riftClient = createClient(baseUrl);
220
170
  this.preflightCheckBalances = options.preflight?.checkBalances !== false;
221
171
  this.integratorName = options.integratorName;
222
172
  this.debug = options.debug ?? false;
@@ -230,6 +180,24 @@ class RiftSdk {
230
180
  }
231
181
  console.log(`[RiftSdk] ${message}`);
232
182
  }
183
+ unwrapEdenResult(result) {
184
+ if (!result.error) {
185
+ if (result.data === null) {
186
+ throw new SwapRouterApiError(`Request failed with status ${result.status}`, result.status);
187
+ }
188
+ return result.data;
189
+ }
190
+ const error = result.error;
191
+ const status = error.status ?? result.status;
192
+ const value = error.value;
193
+ let message = `Request failed with status ${status}`;
194
+ if (value && typeof value === "object" && "error" in value && typeof value.error === "string") {
195
+ message = value.error;
196
+ } else if (typeof error.message === "string") {
197
+ message = error.message;
198
+ }
199
+ throw new SwapRouterApiError(message, status, value);
200
+ }
233
201
  async getQuote(params) {
234
202
  const route = detectRoute(params.from, params.to);
235
203
  const isMonochain = route.type === "oneinch_monochain";
@@ -239,7 +207,7 @@ class RiftSdk {
239
207
  to: params.to,
240
208
  amount: params.amount
241
209
  };
242
- const riftQuote = await this.riftClient.quote(quoteRequest);
210
+ const riftQuote = this.unwrapEdenResult(await this.riftClient.quote.post(quoteRequest));
243
211
  const quote = this.buildQuoteResult(riftQuote, params);
244
212
  const isChained = route.type === "direct_rift" && route.direction === "from_btc" && !isCbBtc(params.to);
245
213
  return {
@@ -259,13 +227,13 @@ class RiftSdk {
259
227
  await this.assertSufficientBalance(params.from, quote.from.amount, context);
260
228
  }
261
229
  this.logDebug("creating swap", { quoteId: riftQuote.id });
262
- const swapResponse = await this.riftClient.createSwap({
230
+ const swapResponse = this.unwrapEdenResult(await this.riftClient.swap.post({
263
231
  id: riftQuote.id,
264
232
  destinationAddress: params.destinationAddress,
265
233
  refundAddress,
266
234
  integratorName: this.integratorName,
267
235
  approvalMode: params.approvalMode
268
- });
236
+ }));
269
237
  this.logDebug("swap created", {
270
238
  swapId: swapResponse.swapId,
271
239
  steps: swapResponse.executionSteps.length
@@ -288,16 +256,16 @@ class RiftSdk {
288
256
  stepId: step.id,
289
257
  oneinchSwap: true
290
258
  });
291
- await this.riftClient.reportStepResult(swapResponse.swapId, {
259
+ this.unwrapEdenResult(await this.riftClient.swap({ swapId: swapResponse.swapId }).tx.post({
292
260
  stepId: step.id,
293
261
  ...result
294
- });
262
+ }));
295
263
  }
296
264
  }
297
265
  this.logDebug("fetching swap status", {
298
266
  swapId: swapResponse.swapId
299
267
  });
300
- const swap = await this.riftClient.getSwap(swapResponse.swapId);
268
+ const swap = this.unwrapEdenResult(await this.riftClient.swap({ swapId: swapResponse.swapId }).get());
301
269
  this.logDebug("swap fetched", {
302
270
  swapId: swapResponse.swapId,
303
271
  status: swap.status
@@ -472,7 +440,7 @@ class RiftSdk {
472
440
  return context.sendBitcoin;
473
441
  }
474
442
  async getSwapStatus(swapId) {
475
- return this.riftClient.getSwap(swapId);
443
+ return this.unwrapEdenResult(await this.riftClient.swap({ swapId }).get());
476
444
  }
477
445
  }
478
446
  function createRiftSdk(options) {
@@ -484,6 +452,7 @@ export {
484
452
  createRiftSdk,
485
453
  createCurrency,
486
454
  createClient,
455
+ SwapRouterApiError,
487
456
  RiftSdk,
488
457
  Currencies
489
458
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riftresearch/sdk",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "SDK for swapping between bitcoin and evm chains",
5
5
  "license": "MIT",
6
6
  "files": [