@riftresearch/sdk 0.2.2 → 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/dist/index.d.ts CHANGED
@@ -412,12 +412,18 @@ interface RiftSdkOptions {
412
412
  sendBitcoin: SendBitcoinFn;
413
413
  /** Rift API URL. Defaults to production API */
414
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
+ };
415
420
  }
416
421
  declare class RiftSdk {
417
422
  private riftClient;
418
423
  private publicClient;
419
424
  private walletClient;
420
425
  private sendBitcoinFn;
426
+ private preflightCheckBalances;
421
427
  constructor(options: RiftSdkOptions);
422
428
  /**
423
429
  * Get a quote for a swap and return a function to execute it.
@@ -451,6 +457,7 @@ declare class RiftSdk {
451
457
  private executeBtcTransferStep;
452
458
  private buildQuoteResult;
453
459
  private buildOrderResult;
460
+ private assertSufficientBalance;
454
461
  private getAddress;
455
462
  private getRefundAddress;
456
463
  /**
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,
@@ -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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riftresearch/sdk",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "SDK for swapping between bitcoin and evm chains",
5
5
  "license": "MIT",
6
6
  "files": [