@riftresearch/sdk 0.2.1 → 0.2.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/README.md CHANGED
@@ -68,7 +68,7 @@ const swap = await executeSwap()
68
68
  console.log(`Swap ID: ${swap.swapId}`) // API order ID
69
69
 
70
70
  // Check status
71
- const status = await sdk.getSwapStatus(swap.swapId)
71
+ const status = await sdk.getOrderStatus(swap.swapId)
72
72
  console.log(`Status: ${status.status}`)
73
73
  ```
74
74
 
package/dist/index.d.ts CHANGED
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Core domain types for Rift Swap Router.
3
+ * Shared between SDK and server packages.
4
+ */
5
+ type Address = string;
6
+ type TxHash = string;
1
7
  type U256 = string;
2
8
  type BitcoinChain = {
3
9
  kind: "BITCOIN";
@@ -124,6 +130,10 @@ type QuoteResponse = ExactInputQuoteResponse | ExactOutputQuoteResponse;
124
130
  type OrderStatus = "waiting_for_deposit" | "deposit_confirming" | "initiating_transfer" | "confirming_transfer" | "swap_complete" | "refunding_user" | "failed";
125
131
  interface OrderStatusResponse {
126
132
  status: OrderStatus;
133
+ destinationAddress: Address;
134
+ payoutTransaction?: TxHash;
135
+ depositTransaction?: TxHash;
136
+ quote: QuoteResponse;
127
137
  }
128
138
  /**
129
139
  * Execution actions - the mechanism by which a step is executed.
@@ -359,9 +369,9 @@ interface ExactOutputQuoteResult extends QuoteResultBase {
359
369
  type QuoteResult = ExactInputQuoteResult | ExactOutputQuoteResult;
360
370
  interface GetQuoteResult {
361
371
  quote: QuoteResult;
362
- executeSwap: () => Promise<SwapResult>;
372
+ executeSwap: () => Promise<OrderResult>;
363
373
  }
364
- interface SwapResult {
374
+ interface OrderResult {
365
375
  swapId: string;
366
376
  status: SwapStatus2;
367
377
  rift: RiftOrder;
@@ -402,12 +412,18 @@ interface RiftSdkOptions {
402
412
  sendBitcoin: SendBitcoinFn;
403
413
  /** Rift API URL. Defaults to production API */
404
414
  apiUrl?: string;
415
+ /** Optional preflight checks before executing swaps */
416
+ preflight?: {
417
+ /** Check sender balance before executing EVM steps (default: true) */
418
+ checkBalances?: boolean;
419
+ };
405
420
  }
406
421
  declare class RiftSdk {
407
422
  private riftClient;
408
423
  private publicClient;
409
424
  private walletClient;
410
425
  private sendBitcoinFn;
426
+ private preflightCheckBalances;
411
427
  constructor(options: RiftSdkOptions);
412
428
  /**
413
429
  * Get a quote for a swap and return a function to execute it.
@@ -440,12 +456,13 @@ declare class RiftSdk {
440
456
  */
441
457
  private executeBtcTransferStep;
442
458
  private buildQuoteResult;
443
- private buildSwapResult;
459
+ private buildOrderResult;
460
+ private assertSufficientBalance;
444
461
  private getAddress;
445
462
  private getRefundAddress;
446
463
  /**
447
- * Get the current status of a swap by its ID.
464
+ * Get the current status of an order by its ID.
448
465
  */
449
- getSwapStatus(swapId: string): Promise<SwapResult>;
466
+ getOrderStatus(orderId: string): Promise<OrderStatusResponse>;
450
467
  }
451
- export { getSupportedModes, detectRoute, createClient, TradeParameters, TokenIdentifier, SwapStatus2 as SwapStatus, SwapRoute, SwapResult, SupportedModes, SendBitcoinFn, RiftSdkOptions, RiftSdk, RiftOrder, RiftClient, QuoteResult, OrderResponse, NativeToken, GetQuoteResult, ExecutionStep, ExecutionAction, EvmChain, EvmCallStep, EvmCallKind, Erc20Token, Currency, Chain, CBBTC_ETHEREUM, CBBTC_BASE, BtcTransferStep, BtcTransferKind, BitcoinChain, BTC, App };
468
+ export { getSupportedModes, detectRoute, createClient, TradeParameters, TokenIdentifier, SwapStatus2 as SwapStatus, SwapRoute, SupportedModes, SendBitcoinFn, RiftSdkOptions, RiftSdk, RiftOrder, RiftClient, QuoteResult, OrderResult, OrderResponse, NativeToken, GetQuoteResult, ExecutionStep, ExecutionAction, EvmChain, EvmCallStep, EvmCallKind, Erc20Token, Currency, Chain, CBBTC_ETHEREUM, CBBTC_BASE, BtcTransferStep, BtcTransferKind, BitcoinChain, BTC, App };
package/dist/index.js CHANGED
@@ -157,6 +157,7 @@ class RiftSdk {
157
157
  publicClient;
158
158
  walletClient;
159
159
  sendBitcoinFn;
160
+ preflightCheckBalances;
160
161
  constructor(options) {
161
162
  this.riftClient = new SwapRouterClient({
162
163
  baseUrl: options.apiUrl ?? "https://router-api-v2-production.up.railway.app"
@@ -164,6 +165,7 @@ class RiftSdk {
164
165
  this.publicClient = options.publicClient;
165
166
  this.walletClient = options.walletClient;
166
167
  this.sendBitcoinFn = options.sendBitcoin;
168
+ this.preflightCheckBalances = options.preflight?.checkBalances !== false;
167
169
  }
168
170
  async getQuote(params) {
169
171
  const route = detectRoute(params.from, params.to);
@@ -181,6 +183,9 @@ class RiftSdk {
181
183
  quote,
182
184
  executeSwap: async () => {
183
185
  const refundAddress = params.refundAddress ?? this.getRefundAddress(params);
186
+ if (this.preflightCheckBalances) {
187
+ await this.assertSufficientBalance(params.from, quote.from.amount);
188
+ }
184
189
  const orderResponse = await this.riftClient.createOrder({
185
190
  id: riftQuote.id,
186
191
  destinationAddress: params.destinationAddress,
@@ -197,7 +202,7 @@ class RiftSdk {
197
202
  }
198
203
  }
199
204
  const swap = await this.riftClient.getOrder(orderResponse.swapId);
200
- return this.buildSwapResult(swap, {
205
+ return this.buildOrderResult(swap, {
201
206
  chained: isChained,
202
207
  riftOrderId: orderResponse.swapId
203
208
  });
@@ -266,10 +271,10 @@ class RiftSdk {
266
271
  };
267
272
  }
268
273
  }
269
- buildSwapResult(swap, options) {
274
+ buildOrderResult(swap, options) {
270
275
  const riftOrderId = options?.riftOrderId;
271
276
  if (!riftOrderId) {
272
- throw new Error("Missing rift order id for swap result.");
277
+ throw new Error("Missing rift order id for order result.");
273
278
  }
274
279
  return {
275
280
  swapId: riftOrderId,
@@ -277,6 +282,28 @@ class RiftSdk {
277
282
  rift: swap
278
283
  };
279
284
  }
285
+ async assertSufficientBalance(currency, amount) {
286
+ if (currency.chain.kind !== "EVM")
287
+ return;
288
+ const required = BigInt(amount);
289
+ const owner = this.getAddress();
290
+ if (currency.token.kind === "NATIVE") {
291
+ const balance2 = await this.publicClient.getBalance({ address: owner });
292
+ if (balance2 < required) {
293
+ throw new Error(`Insufficient balance for native token. Need ${required.toString()}, have ${balance2.toString()}`);
294
+ }
295
+ return;
296
+ }
297
+ const balance = await this.publicClient.readContract({
298
+ address: currency.token.address,
299
+ abi: erc20Abi,
300
+ functionName: "balanceOf",
301
+ args: [owner]
302
+ });
303
+ if (balance < required) {
304
+ throw new Error(`Insufficient balance for token ${currency.token.address}. Need ${required.toString()}, have ${balance.toString()}`);
305
+ }
306
+ }
280
307
  getAddress() {
281
308
  const account = this.walletClient.account;
282
309
  if (!account) {
@@ -290,13 +317,8 @@ class RiftSdk {
290
317
  }
291
318
  return this.getAddress();
292
319
  }
293
- async getSwapStatus(swapId) {
294
- const rift = await this.riftClient.getOrder(swapId);
295
- return {
296
- swapId,
297
- status: rift.status,
298
- rift
299
- };
320
+ async getOrderStatus(orderId) {
321
+ return this.riftClient.getOrder(orderId);
300
322
  }
301
323
  }
302
324
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riftresearch/sdk",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "SDK for swapping between bitcoin and evm chains",
5
5
  "license": "MIT",
6
6
  "files": [