@riftresearch/sdk 0.7.1 → 0.9.0

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
@@ -148,7 +148,7 @@ type ExecutionAction = "evm_call" | "btc_transfer";
148
148
  /**
149
149
  * Step kinds grouped by action type.
150
150
  */
151
- type EvmCallKind = "approval" | "transfer_erc20" | "oneinch_swap" | "dex_swap";
151
+ type EvmCallKind = "approval" | "transfer_erc20" | "dex_swap";
152
152
  type BtcTransferKind = "transfer_btc";
153
153
  /**
154
154
  * EVM Call step - execute calldata on an EVM chain.
@@ -376,6 +376,8 @@ type SendBitcoinFn = (params: {
376
376
  /** Amount to send in satoshis */
377
377
  amountSats: string;
378
378
  }) => Promise<void>;
379
+ type ExecuteSwapStepType = "approval" | "transaction";
380
+ type ExecuteSwapOnExecuteStepCallback = (type: ExecuteSwapStepType) => void | Promise<void>;
379
381
  type ExecuteSwapContext<chain extends Chain2 | undefined = Chain2 | undefined> = {
380
382
  /** Viem PublicClient for reading chain data */
381
383
  publicClient?: PublicClient<Transport, chain>;
@@ -383,6 +385,11 @@ type ExecuteSwapContext<chain extends Chain2 | undefined = Chain2 | undefined> =
383
385
  walletClient?: WalletClient<Transport, chain, Account | undefined>;
384
386
  /** Function to send Bitcoin (implement using your preferred wallet) */
385
387
  sendBitcoin?: SendBitcoinFn;
388
+ /**
389
+ * Optional callback invoked immediately before prompting the user's wallet to
390
+ * sign/send a transaction (after estimateGas/simulation has succeeded).
391
+ */
392
+ onExecuteStep?: ExecuteSwapOnExecuteStepCallback;
386
393
  };
387
394
  type ExecuteSwapOptions<chain extends Chain2 | undefined = Chain2 | undefined> = ExecuteSwapContext<chain> & {
388
395
  /** Address to receive the output tokens */
@@ -462,4 +469,4 @@ declare class RiftSdk {
462
469
  getSwapStatus(swapId: string): Promise<SwapStatusResponse>;
463
470
  }
464
471
  declare function createRiftSdk(options: RiftSdkOptions): RiftSdk;
465
- export { getSupportedModes, detectRoute, createRiftSdk, createCurrency, TokenIdentifier, SwapStatus, SwapRouterApiError, SwapRoute, SwapResult, SwapResponse, SupportedModes, SendBitcoinFn, RiftSwap, RiftSdkOptions, RiftSdk, QuoteResult, QuoteQuality, QuoteParameters, NativeToken, GetQuoteResult, ExecutionStep, ExecutionAction, ExecuteSwapOptions, EvmChain, EvmCallStep, EvmCallKind, Erc20Token, Currency, Currencies, Chain, BtcTransferStep, BtcTransferKind, BitcoinChain };
472
+ export { getSupportedModes, detectRoute, createRiftSdk, createCurrency, TokenIdentifier, SwapStatus, SwapRouterApiError, SwapRoute, SwapResult, SwapResponse, SupportedModes, SendBitcoinFn, RiftSwap, RiftSdkOptions, RiftSdk, QuoteResult, QuoteQuality, QuoteParameters, NativeToken, GetQuoteResult, ExecutionStep, ExecutionAction, ExecuteSwapStepType, ExecuteSwapOptions, ExecuteSwapOnExecuteStepCallback, EvmChain, EvmCallStep, EvmCallKind, Erc20Token, Currency, Currencies, Chain, BtcTransferStep, BtcTransferKind, BitcoinChain };
package/dist/index.js CHANGED
@@ -247,8 +247,10 @@ class RiftSdk {
247
247
  await this.assertSufficientBalance(params.from, quote.from.expected, context);
248
248
  }
249
249
  this.logDebug("creating swap", { quoteId: riftQuote.id });
250
+ const senderAddress = params.from.chain.kind === "EVM" ? this.getAddress(context) : undefined;
250
251
  const swapResponse = this.unwrapEdenResult(await this.riftClient.swap.post({
251
252
  id: riftQuote.id,
253
+ ...senderAddress ? { senderAddress } : {},
252
254
  destinationAddress: context.destinationAddress,
253
255
  refundAddress,
254
256
  integratorName: this.integratorName,
@@ -266,15 +268,16 @@ class RiftSdk {
266
268
  kind: "kind" in step ? step.kind : undefined,
267
269
  chainId: "chainId" in step ? step.chainId : undefined
268
270
  });
269
- const result = await this.executeStep(step, context);
271
+ const result = await this.executeStep(step, context, swapResponse.swapId);
270
272
  this.logDebug("step completed", {
271
273
  stepId: step.id,
272
274
  txHash: result.txHash
273
275
  });
274
- if (isMonochain && step.action === "evm_call" && (step.kind === "dex_swap" || step.kind === "oneinch_swap") && result.txHash) {
276
+ if (step.action === "evm_call" && step.kind === "dex_swap" && result.txHash) {
275
277
  this.logDebug("reporting step result", {
276
278
  stepId: step.id,
277
- dexSwap: true
279
+ dexSwap: true,
280
+ monochain: isMonochain
278
281
  });
279
282
  this.unwrapEdenResult(await this.riftClient.swap({ swapId: swapResponse.swapId }).tx.post({
280
283
  stepId: step.id,
@@ -297,15 +300,15 @@ class RiftSdk {
297
300
  }
298
301
  };
299
302
  }
300
- async executeStep(step, context) {
303
+ async executeStep(step, context, swapId) {
301
304
  switch (step.action) {
302
305
  case "evm_call":
303
- return this.executeEvmCallStep(step, context);
306
+ return this.executeEvmCallStep(step, context, swapId);
304
307
  case "btc_transfer":
305
308
  return this.executeBtcTransferStep(step, context);
306
309
  }
307
310
  }
308
- async executeEvmCallStep(step, context) {
311
+ async executeEvmCallStep(step, context, swapId) {
309
312
  const walletClient = this.requireWalletClient(context);
310
313
  const publicClient = this.requirePublicClient(context);
311
314
  const account = walletClient.account;
@@ -323,19 +326,53 @@ class RiftSdk {
323
326
  return {};
324
327
  }
325
328
  }
326
- const txRequest = {
329
+ let effectiveStep = step;
330
+ let txRequest = {
327
331
  account,
328
- to: step.to,
329
- data: step.calldata,
330
- value: step.value ? BigInt(step.value) : undefined
332
+ to: effectiveStep.to,
333
+ data: effectiveStep.calldata,
334
+ value: effectiveStep.value ? BigInt(effectiveStep.value) : undefined
331
335
  };
332
- const estimatedGas = await publicClient.estimateGas(txRequest);
336
+ let estimatedGas;
337
+ try {
338
+ estimatedGas = await publicClient.estimateGas(txRequest);
339
+ } catch (estimateError) {
340
+ if (effectiveStep.kind !== "dex_swap") {
341
+ throw estimateError;
342
+ }
343
+ this.logDebug("estimateGas failed; attempting refresh-step", {
344
+ swapId,
345
+ stepId: effectiveStep.id,
346
+ error: estimateError instanceof Error ? estimateError.message : String(estimateError)
347
+ });
348
+ let refreshed;
349
+ try {
350
+ refreshed = this.unwrapEdenResult(await this.riftClient.swap({ swapId })["refresh-step"].post({ stepId: effectiveStep.id }));
351
+ } catch (refreshError) {
352
+ throw new Error(`estimateGas failed for dex_swap step and refresh-step failed: ${refreshError instanceof Error ? refreshError.message : String(refreshError)}`);
353
+ }
354
+ if (!refreshed?.step) {
355
+ throw new Error("estimateGas failed for dex_swap step and refresh-step returned no step");
356
+ }
357
+ if (refreshed.step.kind !== "dex_swap") {
358
+ throw new Error(`refresh-step returned unexpected step kind: ${refreshed.step.kind}`);
359
+ }
360
+ effectiveStep = refreshed.step;
361
+ txRequest = {
362
+ account,
363
+ to: effectiveStep.to,
364
+ data: effectiveStep.calldata,
365
+ value: effectiveStep.value ? BigInt(effectiveStep.value) : undefined
366
+ };
367
+ estimatedGas = await publicClient.estimateGas(txRequest);
368
+ }
333
369
  const gasLimit = (estimatedGas * GAS_LIMIT_MULTIPLIER_NUMERATOR + GAS_LIMIT_MULTIPLIER_DENOMINATOR - 1n) / GAS_LIMIT_MULTIPLIER_DENOMINATOR;
334
370
  this.logDebug("using buffered gas limit", {
335
371
  stepId: step.id,
336
372
  estimatedGas: estimatedGas.toString(),
337
373
  gasLimit: gasLimit.toString()
338
374
  });
375
+ await context.onExecuteStep?.(effectiveStep.kind === "approval" ? "approval" : "transaction");
339
376
  const txHash = await walletClient.sendTransaction({
340
377
  ...txRequest,
341
378
  gas: gasLimit
@@ -344,12 +381,13 @@ class RiftSdk {
344
381
  hash: txHash
345
382
  });
346
383
  if (receipt.status !== "success") {
347
- throw new Error(`EVM step transaction reverted (${step.kind}) with hash ${txHash}`);
384
+ throw new Error(`EVM step transaction reverted (${effectiveStep.kind}) with hash ${txHash}`);
348
385
  }
349
386
  return { txHash };
350
387
  }
351
388
  async executeBtcTransferStep(step, context) {
352
389
  const sendBitcoin = this.requireSendBitcoin(context);
390
+ await context.onExecuteStep?.("transaction");
353
391
  await sendBitcoin({
354
392
  recipient: step.toAddress,
355
393
  amountSats: step.amountSats
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riftresearch/sdk",
3
- "version": "0.7.1",
3
+ "version": "0.9.0",
4
4
  "description": "SDK for swapping between bitcoin and evm chains",
5
5
  "license": "MIT",
6
6
  "files": [