@riftresearch/sdk 0.4.3 → 0.6.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.js CHANGED
@@ -123,7 +123,7 @@ function detectRoute(from, to) {
123
123
  throw new Error("Expected EVM chain");
124
124
  }
125
125
  return {
126
- type: "oneinch_then_rift",
126
+ type: "dex_then_rift",
127
127
  evmChainId: from2.chain.chainId
128
128
  };
129
129
  }).with({
@@ -134,7 +134,7 @@ function detectRoute(from, to) {
134
134
  throw new Error("Expected EVM chain");
135
135
  }
136
136
  return {
137
- type: "oneinch_monochain",
137
+ type: "dex_monochain",
138
138
  evmChainId: from2.chain.chainId
139
139
  };
140
140
  }).otherwise(() => {
@@ -143,10 +143,10 @@ function detectRoute(from, to) {
143
143
  }
144
144
  function getSupportedModes(from, to) {
145
145
  const route = detectRoute(from, to);
146
- if (route.type === "oneinch_then_rift") {
147
- return { exactInput: true, exactOutput: false };
146
+ if (route.type === "dex_then_rift") {
147
+ return { exactInput: true, exactOutput: true };
148
148
  }
149
- if (route.type === "oneinch_monochain") {
149
+ if (route.type === "dex_monochain") {
150
150
  return { exactInput: true, exactOutput: false };
151
151
  }
152
152
  if (route.direction === "to_btc") {
@@ -159,13 +159,16 @@ function getSupportedModes(from, to) {
159
159
  }
160
160
  // src/sdk.ts
161
161
  import { erc20Abi } from "viem";
162
+ var GAS_LIMIT_MULTIPLIER_NUMERATOR = 3n;
163
+ var GAS_LIMIT_MULTIPLIER_DENOMINATOR = 2n;
164
+
162
165
  class RiftSdk {
163
166
  riftClient;
164
167
  preflightCheckBalances;
165
168
  integratorName;
166
169
  debug;
167
170
  constructor(options) {
168
- const baseUrl = (options.apiUrl ?? "https://router-api-v2-production.up.railway.app").replace(/\/$/, "");
171
+ const baseUrl = (options.apiUrl ?? "https://api.rift.trade").replace(/\/$/, "");
169
172
  this.riftClient = createClient(baseUrl);
170
173
  this.preflightCheckBalances = options.preflight?.checkBalances !== false;
171
174
  this.integratorName = options.integratorName;
@@ -200,19 +203,26 @@ class RiftSdk {
200
203
  }
201
204
  async getQuote(params) {
202
205
  const route = detectRoute(params.from, params.to);
203
- const isMonochain = route.type === "oneinch_monochain";
206
+ const isMonochain = route.type === "dex_monochain";
204
207
  const quoteRequest = {
205
208
  type: params.mode === "exact_input" ? "EXACT_INPUT" : "EXACT_OUTPUT",
206
209
  from: params.from,
207
210
  to: params.to,
208
- amount: params.amount
211
+ amount: params.amount,
212
+ quoteQuality: params.quoteQuality
209
213
  };
210
214
  const riftQuote = this.unwrapEdenResult(await this.riftClient.quote.post(quoteRequest));
211
215
  const quote = this.buildQuoteResult(riftQuote, params);
212
216
  const isChained = route.type === "direct_rift" && route.direction === "from_btc" && !isCbBtc(params.to);
213
217
  return {
214
218
  quote,
215
- executeSwap: async (context = {}) => {
219
+ executeSwap: async (context) => {
220
+ if (!context?.destinationAddress) {
221
+ throw new Error("destinationAddress is required to execute swap");
222
+ }
223
+ if (params.from.chain.kind === "BITCOIN" && !context.refundAddress) {
224
+ throw new Error("refundAddress is required for BTC swaps (Bitcoin refund address)");
225
+ }
216
226
  this.logDebug("executeSwap called", {
217
227
  route: route.type,
218
228
  mode: params.mode,
@@ -220,7 +230,7 @@ class RiftSdk {
220
230
  to: params.to,
221
231
  amount: params.amount
222
232
  });
223
- const refundAddress = params.refundAddress ?? this.getRefundAddress(params, context);
233
+ const refundAddress = context.refundAddress ?? this.getRefundAddress(params, context);
224
234
  this.logDebug("resolved refund address", { refundAddress });
225
235
  if (this.preflightCheckBalances) {
226
236
  this.logDebug("running preflight balance check");
@@ -229,7 +239,7 @@ class RiftSdk {
229
239
  this.logDebug("creating swap", { quoteId: riftQuote.id });
230
240
  const swapResponse = this.unwrapEdenResult(await this.riftClient.swap.post({
231
241
  id: riftQuote.id,
232
- destinationAddress: params.destinationAddress,
242
+ destinationAddress: context.destinationAddress,
233
243
  refundAddress,
234
244
  integratorName: this.integratorName,
235
245
  approvalMode: params.approvalMode
@@ -251,10 +261,10 @@ class RiftSdk {
251
261
  stepId: step.id,
252
262
  txHash: result.txHash
253
263
  });
254
- if (isMonochain && step.action === "evm_call" && step.kind === "oneinch_swap" && result.txHash) {
264
+ if (isMonochain && step.action === "evm_call" && (step.kind === "dex_swap" || step.kind === "oneinch_swap") && result.txHash) {
255
265
  this.logDebug("reporting step result", {
256
266
  stepId: step.id,
257
- oneinchSwap: true
267
+ dexSwap: true
258
268
  });
259
269
  this.unwrapEdenResult(await this.riftClient.swap({ swapId: swapResponse.swapId }).tx.post({
260
270
  stepId: step.id,
@@ -303,13 +313,29 @@ class RiftSdk {
303
313
  return {};
304
314
  }
305
315
  }
306
- const txHash = await walletClient.sendTransaction({
316
+ const txRequest = {
307
317
  account,
308
318
  to: step.to,
309
319
  data: step.calldata,
310
320
  value: step.value ? BigInt(step.value) : undefined
321
+ };
322
+ const estimatedGas = await publicClient.estimateGas(txRequest);
323
+ const gasLimit = (estimatedGas * GAS_LIMIT_MULTIPLIER_NUMERATOR + GAS_LIMIT_MULTIPLIER_DENOMINATOR - 1n) / GAS_LIMIT_MULTIPLIER_DENOMINATOR;
324
+ this.logDebug("using buffered gas limit", {
325
+ stepId: step.id,
326
+ estimatedGas: estimatedGas.toString(),
327
+ gasLimit: gasLimit.toString()
328
+ });
329
+ const txHash = await walletClient.sendTransaction({
330
+ ...txRequest,
331
+ gas: gasLimit
311
332
  });
312
- await publicClient.waitForTransactionReceipt({ hash: txHash });
333
+ const receipt = await publicClient.waitForTransactionReceipt({
334
+ hash: txHash
335
+ });
336
+ if (receipt.status !== "success") {
337
+ throw new Error(`EVM step transaction reverted (${step.kind}) with hash ${txHash}`);
338
+ }
313
339
  return { txHash };
314
340
  }
315
341
  async executeBtcTransferStep(step, context) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riftresearch/sdk",
3
- "version": "0.4.3",
3
+ "version": "0.6.0",
4
4
  "description": "SDK for swapping between bitcoin and evm chains",
5
5
  "license": "MIT",
6
6
  "files": [