@riftresearch/sdk 0.8.0 → 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
@@ -268,15 +268,16 @@ class RiftSdk {
268
268
  kind: "kind" in step ? step.kind : undefined,
269
269
  chainId: "chainId" in step ? step.chainId : undefined
270
270
  });
271
- const result = await this.executeStep(step, context);
271
+ const result = await this.executeStep(step, context, swapResponse.swapId);
272
272
  this.logDebug("step completed", {
273
273
  stepId: step.id,
274
274
  txHash: result.txHash
275
275
  });
276
- 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) {
277
277
  this.logDebug("reporting step result", {
278
278
  stepId: step.id,
279
- dexSwap: true
279
+ dexSwap: true,
280
+ monochain: isMonochain
280
281
  });
281
282
  this.unwrapEdenResult(await this.riftClient.swap({ swapId: swapResponse.swapId }).tx.post({
282
283
  stepId: step.id,
@@ -299,15 +300,15 @@ class RiftSdk {
299
300
  }
300
301
  };
301
302
  }
302
- async executeStep(step, context) {
303
+ async executeStep(step, context, swapId) {
303
304
  switch (step.action) {
304
305
  case "evm_call":
305
- return this.executeEvmCallStep(step, context);
306
+ return this.executeEvmCallStep(step, context, swapId);
306
307
  case "btc_transfer":
307
308
  return this.executeBtcTransferStep(step, context);
308
309
  }
309
310
  }
310
- async executeEvmCallStep(step, context) {
311
+ async executeEvmCallStep(step, context, swapId) {
311
312
  const walletClient = this.requireWalletClient(context);
312
313
  const publicClient = this.requirePublicClient(context);
313
314
  const account = walletClient.account;
@@ -325,19 +326,53 @@ class RiftSdk {
325
326
  return {};
326
327
  }
327
328
  }
328
- const txRequest = {
329
+ let effectiveStep = step;
330
+ let txRequest = {
329
331
  account,
330
- to: step.to,
331
- data: step.calldata,
332
- value: step.value ? BigInt(step.value) : undefined
332
+ to: effectiveStep.to,
333
+ data: effectiveStep.calldata,
334
+ value: effectiveStep.value ? BigInt(effectiveStep.value) : undefined
333
335
  };
334
- 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
+ }
335
369
  const gasLimit = (estimatedGas * GAS_LIMIT_MULTIPLIER_NUMERATOR + GAS_LIMIT_MULTIPLIER_DENOMINATOR - 1n) / GAS_LIMIT_MULTIPLIER_DENOMINATOR;
336
370
  this.logDebug("using buffered gas limit", {
337
371
  stepId: step.id,
338
372
  estimatedGas: estimatedGas.toString(),
339
373
  gasLimit: gasLimit.toString()
340
374
  });
375
+ await context.onExecuteStep?.(effectiveStep.kind === "approval" ? "approval" : "transaction");
341
376
  const txHash = await walletClient.sendTransaction({
342
377
  ...txRequest,
343
378
  gas: gasLimit
@@ -346,12 +381,13 @@ class RiftSdk {
346
381
  hash: txHash
347
382
  });
348
383
  if (receipt.status !== "success") {
349
- 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}`);
350
385
  }
351
386
  return { txHash };
352
387
  }
353
388
  async executeBtcTransferStep(step, context) {
354
389
  const sendBitcoin = this.requireSendBitcoin(context);
390
+ await context.onExecuteStep?.("transaction");
355
391
  await sendBitcoin({
356
392
  recipient: step.toAddress,
357
393
  amountSats: step.amountSats
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riftresearch/sdk",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "SDK for swapping between bitcoin and evm chains",
5
5
  "license": "MIT",
6
6
  "files": [