@riftresearch/sdk 0.5.0 → 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.d.ts +261 -32
- package/dist/index.js +39 -19
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,241 @@
|
|
|
1
|
-
|
|
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;
|
|
7
|
+
type U256 = string;
|
|
8
|
+
type BitcoinChain = {
|
|
9
|
+
kind: "BITCOIN";
|
|
10
|
+
};
|
|
11
|
+
type EvmChain = {
|
|
12
|
+
kind: "EVM";
|
|
13
|
+
chainId: number;
|
|
14
|
+
};
|
|
15
|
+
type Chain = BitcoinChain | EvmChain;
|
|
16
|
+
type NativeToken = {
|
|
17
|
+
kind: "NATIVE";
|
|
18
|
+
decimals: number;
|
|
19
|
+
};
|
|
20
|
+
type Erc20Token = {
|
|
21
|
+
kind: "TOKEN";
|
|
22
|
+
address: string;
|
|
23
|
+
decimals: number;
|
|
24
|
+
};
|
|
25
|
+
type TokenIdentifier = NativeToken | Erc20Token;
|
|
26
|
+
interface Currency {
|
|
27
|
+
chain: Chain;
|
|
28
|
+
token: TokenIdentifier;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A currency amount that was specified by the user (exact).
|
|
32
|
+
*/
|
|
33
|
+
interface SpecifiedAmount {
|
|
34
|
+
currency: Currency;
|
|
35
|
+
amount: U256;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A calculated output amount for exact_input mode.
|
|
39
|
+
* The user specifies exact input, and this represents the expected output.
|
|
40
|
+
*/
|
|
41
|
+
interface CalculatedOutputAmount {
|
|
42
|
+
currency: Currency;
|
|
43
|
+
/** Expected output based on current market conditions */
|
|
44
|
+
amount: U256;
|
|
45
|
+
/** Minimum guaranteed output after slippage (optional - not all routes provide this) */
|
|
46
|
+
minimum?: U256;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A calculated input amount for exact_output mode.
|
|
50
|
+
* The user specifies exact output, and this represents the expected input.
|
|
51
|
+
*/
|
|
52
|
+
interface CalculatedInputAmount {
|
|
53
|
+
currency: Currency;
|
|
54
|
+
/** Expected input based on current market conditions */
|
|
55
|
+
amount: U256;
|
|
56
|
+
/** Maximum input required after slippage (optional - not all routes provide this) */
|
|
57
|
+
maximum?: U256;
|
|
58
|
+
}
|
|
59
|
+
type QuoteQuality = "fast" | "optimal";
|
|
60
|
+
/**
|
|
61
|
+
* A single fee component with amount in native currency and USD equivalent.
|
|
62
|
+
*/
|
|
63
|
+
interface FeeComponent {
|
|
64
|
+
/** Amount in the smallest unit of the currency */
|
|
65
|
+
amount: U256;
|
|
66
|
+
/** The currency this fee is denominated in */
|
|
67
|
+
currency: Currency;
|
|
68
|
+
/** USD equivalent of the fee */
|
|
69
|
+
usd: number;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Rift protocol fees broken down by type.
|
|
73
|
+
*/
|
|
74
|
+
interface RiftFees {
|
|
75
|
+
/** Bitcoin network transaction fee */
|
|
76
|
+
network: FeeComponent;
|
|
77
|
+
/** Fee paid to liquidity providers */
|
|
78
|
+
liquidity: FeeComponent;
|
|
79
|
+
/** Rift protocol fee */
|
|
80
|
+
protocol: FeeComponent;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Complete fee breakdown for a swap quote.
|
|
84
|
+
* Shows fees from each leg of the swap in their native currencies.
|
|
85
|
+
*/
|
|
86
|
+
interface FeeBreakdown {
|
|
87
|
+
/** Fees from DEX swap BEFORE Rift (e.g., 1inch USDC → cbBTC) */
|
|
88
|
+
preswap?: FeeComponent;
|
|
89
|
+
/** Fees from the Rift protocol (optional for mono-chain swaps) */
|
|
90
|
+
rift?: RiftFees;
|
|
91
|
+
/** Fees from DEX swap AFTER Rift (e.g., Swapper cbBTC → USDC) */
|
|
92
|
+
postswap?: FeeComponent;
|
|
93
|
+
/** Total fees in USD across all legs */
|
|
94
|
+
totalUsd: number;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Base fields shared by all quote responses.
|
|
98
|
+
*/
|
|
99
|
+
interface QuoteResponseBase {
|
|
100
|
+
id: string;
|
|
101
|
+
fees: FeeBreakdown;
|
|
102
|
+
expiresAt: string;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Quote response for exact_input mode.
|
|
106
|
+
* User specifies exact input amount, output is calculated with slippage bound.
|
|
107
|
+
*/
|
|
108
|
+
interface ExactInputQuoteResponse extends QuoteResponseBase {
|
|
109
|
+
mode: "exact_input";
|
|
110
|
+
/** The exact input amount specified by the user */
|
|
111
|
+
from: SpecifiedAmount;
|
|
112
|
+
/** The calculated output amount with slippage bound */
|
|
113
|
+
to: CalculatedOutputAmount;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Quote response for exact_output mode.
|
|
117
|
+
* User specifies exact output amount, input is calculated with slippage bound.
|
|
118
|
+
*/
|
|
119
|
+
interface ExactOutputQuoteResponse extends QuoteResponseBase {
|
|
120
|
+
mode: "exact_output";
|
|
121
|
+
/** The calculated input amount with slippage bound */
|
|
122
|
+
from: CalculatedInputAmount;
|
|
123
|
+
/** The exact output amount specified by the user */
|
|
124
|
+
to: SpecifiedAmount;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Discriminated union of quote responses.
|
|
128
|
+
* Use `mode` to determine which fields are specified vs calculated.
|
|
129
|
+
*/
|
|
130
|
+
type QuoteResponse = ExactInputQuoteResponse | ExactOutputQuoteResponse;
|
|
131
|
+
declare const SWAP_STATUSES: readonly ["waiting_for_deposit", "deposit_confirming", "initiating_payout", "confirming_payout", "swap_complete", "refunding_user", "failed"];
|
|
132
|
+
type SwapStatus = (typeof SWAP_STATUSES)[number];
|
|
133
|
+
interface SwapStatusResponse {
|
|
134
|
+
status: SwapStatus;
|
|
135
|
+
destinationAddress: Address;
|
|
136
|
+
payoutTransaction?: TxHash;
|
|
137
|
+
depositTransaction?: TxHash;
|
|
138
|
+
quote: QuoteResponse;
|
|
139
|
+
}
|
|
140
|
+
interface ErrorResponse {
|
|
141
|
+
error: string;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Execution actions - the mechanism by which a step is executed.
|
|
145
|
+
*/
|
|
146
|
+
type ExecutionAction = "evm_call" | "btc_transfer";
|
|
147
|
+
/**
|
|
148
|
+
* Step kinds grouped by action type.
|
|
149
|
+
*/
|
|
150
|
+
type EvmCallKind = "approval" | "transfer_erc20" | "oneinch_swap" | "dex_swap";
|
|
151
|
+
type BtcTransferKind = "transfer_btc";
|
|
152
|
+
/**
|
|
153
|
+
* EVM Call step - execute calldata on an EVM chain.
|
|
154
|
+
* Used for: token approvals, ERC20 transfers, and DEX swaps.
|
|
155
|
+
*/
|
|
156
|
+
interface EvmCallStep {
|
|
157
|
+
/** Step ID for tracking */
|
|
158
|
+
id: string;
|
|
159
|
+
/** Execution mechanism */
|
|
160
|
+
action: "evm_call";
|
|
161
|
+
/** Specific step type */
|
|
162
|
+
kind: EvmCallKind;
|
|
163
|
+
/** Chain ID for the transaction */
|
|
164
|
+
chainId: number;
|
|
165
|
+
/** Contract address to call */
|
|
166
|
+
to: string;
|
|
167
|
+
/** Encoded calldata */
|
|
168
|
+
calldata: string;
|
|
169
|
+
/** Native ETH value to send (for swap steps) */
|
|
170
|
+
value?: U256;
|
|
171
|
+
/** Token address (for approval, transfer_evm) */
|
|
172
|
+
tokenAddress?: string;
|
|
173
|
+
/** Spender address (for approval) */
|
|
174
|
+
spenderAddress?: string;
|
|
175
|
+
/** Amount for display/verification (for approval, transfer_evm) */
|
|
176
|
+
amount?: U256;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* BTC Transfer step - send Bitcoin to an address.
|
|
180
|
+
* Used for: depositing BTC to Rift vault.
|
|
181
|
+
*/
|
|
182
|
+
interface BtcTransferStep {
|
|
183
|
+
/** Step ID for tracking */
|
|
184
|
+
id: string;
|
|
185
|
+
/** Execution mechanism */
|
|
186
|
+
action: "btc_transfer";
|
|
187
|
+
/** Specific step type */
|
|
188
|
+
kind: BtcTransferKind;
|
|
189
|
+
/** Bitcoin address to send to */
|
|
190
|
+
toAddress: string;
|
|
191
|
+
/** Amount in satoshis */
|
|
192
|
+
amountSats: U256;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Discriminated union of all execution step types.
|
|
196
|
+
* Discriminate on `action` for execution logic, or `kind` for specific handling.
|
|
197
|
+
*/
|
|
198
|
+
type ExecutionStep = EvmCallStep | BtcTransferStep;
|
|
199
|
+
/**
|
|
200
|
+
* Swap response with execution steps that the client must execute.
|
|
201
|
+
*/
|
|
202
|
+
interface SwapResponse {
|
|
203
|
+
/** The Rift swap ID */
|
|
204
|
+
swapId: string;
|
|
205
|
+
/** Normalized quote matching /quote response */
|
|
206
|
+
quote: QuoteResponse;
|
|
207
|
+
/** Ordered list of steps the client must execute */
|
|
208
|
+
executionSteps: ExecutionStep[];
|
|
209
|
+
}
|
|
210
|
+
/** Create an ERC-20 currency on an EVM chain */
|
|
211
|
+
declare function createCurrency(params: {
|
|
212
|
+
chainId: number;
|
|
213
|
+
address: string;
|
|
214
|
+
decimals: number;
|
|
215
|
+
}): Currency;
|
|
216
|
+
declare const Currencies: {
|
|
217
|
+
Bitcoin: {
|
|
218
|
+
BTC: Currency;
|
|
219
|
+
};
|
|
220
|
+
Ethereum: {
|
|
221
|
+
ETH: Currency;
|
|
222
|
+
WETH: Currency;
|
|
223
|
+
USDC: Currency;
|
|
224
|
+
USDT: Currency;
|
|
225
|
+
CBBTC: Currency;
|
|
226
|
+
WBTC: Currency;
|
|
227
|
+
};
|
|
228
|
+
Base: {
|
|
229
|
+
ETH: Currency;
|
|
230
|
+
WETH: Currency;
|
|
231
|
+
USDC: Currency;
|
|
232
|
+
CBBTC: Currency;
|
|
233
|
+
};
|
|
234
|
+
};
|
|
2
235
|
import { Treaty } from "@elysiajs/eden";
|
|
3
|
-
import { ErrorResponse } from "@riftresearch/common";
|
|
4
236
|
import { Elysia } from "elysia";
|
|
5
237
|
import { RiftOtcApi } from "../lib";
|
|
6
|
-
import {
|
|
7
|
-
import { SwapRequest } from "../../../common/types";
|
|
238
|
+
import { SwapRequest as SwapRequest2 } from "../../../common/types";
|
|
8
239
|
import { CachedQuote, ISwapMappingStore } from "../../../lib/cache";
|
|
9
240
|
import { OtcClient as OtcClient2 } from "../../../lib/rift-otc-api";
|
|
10
241
|
import { SwapperClient } from "../../../lib/rift-swapper-api";
|
|
@@ -22,7 +253,7 @@ declare class OrderService {
|
|
|
22
253
|
* Create a swap using the cached quote with raw JSON.
|
|
23
254
|
* Returns SwapResponse with execution steps the client must execute.
|
|
24
255
|
*/
|
|
25
|
-
createSwap(request:
|
|
256
|
+
createSwap(request: SwapRequest2, cachedQuote: CachedQuote): Promise<SwapResponse>;
|
|
26
257
|
/**
|
|
27
258
|
* Create a direct cbBTC ↔ BTC swap order.
|
|
28
259
|
* Returns execution steps: either DepositBtc (for BTC → cbBTC) or Approval + DepositEvm (for cbBTC → BTC)
|
|
@@ -83,7 +314,7 @@ declare class OrderService {
|
|
|
83
314
|
private normalizeTxHash;
|
|
84
315
|
private isEvmChain;
|
|
85
316
|
}
|
|
86
|
-
import { QuoteRequest, QuoteResponse } from "../../../common/types";
|
|
317
|
+
import { QuoteRequest as QuoteRequest2, QuoteResponse as QuoteResponse2 } from "../../../common/types";
|
|
87
318
|
import { CachedQuote as CachedQuote2, IQuoteCache } from "../../../lib/cache";
|
|
88
319
|
import { RfqClient as RfqClient2 } from "../../../lib/rift-otc-api";
|
|
89
320
|
import { SwapperClient as SwapperClient2 } from "../../../lib/rift-swapper-api";
|
|
@@ -98,7 +329,7 @@ declare class QuoteService {
|
|
|
98
329
|
private readonly priceOracle;
|
|
99
330
|
private readonly oneinchProvider;
|
|
100
331
|
constructor(quoteCache: IQuoteCache, rfqClient: RfqClient2, swapperClient: SwapperClient2 | null);
|
|
101
|
-
getQuote(request:
|
|
332
|
+
getQuote(request: QuoteRequest2): Promise<QuoteResponse2>;
|
|
102
333
|
/** Get the cached quote with raw JSON for order creation */
|
|
103
334
|
getCachedQuoteById(id: string): Promise<CachedQuote2 | null>;
|
|
104
335
|
/** Atomically consume a cached quote to prevent reuse */
|
|
@@ -1335,14 +1566,14 @@ declare class SwapRouterApiError extends Error {
|
|
|
1335
1566
|
/**
|
|
1336
1567
|
* Create a type-safe API client for the Rift Swap Router.
|
|
1337
1568
|
*
|
|
1338
|
-
* @param baseUrl - The base URL of the swap router API (e.g., 'https://
|
|
1569
|
+
* @param baseUrl - The base URL of the swap router API (e.g., 'https://api.rift.trade')
|
|
1339
1570
|
* @returns A fully typed Eden Treaty client
|
|
1340
1571
|
*
|
|
1341
1572
|
* @example
|
|
1342
1573
|
* ```typescript
|
|
1343
1574
|
* import { createClient } from '@riftresearch/sdk'
|
|
1344
1575
|
*
|
|
1345
|
-
* const api = createClient('https://
|
|
1576
|
+
* const api = createClient('https://api.rift.trade')
|
|
1346
1577
|
*
|
|
1347
1578
|
* // Get a quote - fully typed request and response
|
|
1348
1579
|
* const { data: quote, error } = await api.quote.post({
|
|
@@ -1384,31 +1615,30 @@ declare class SwapRouterApiError extends Error {
|
|
|
1384
1615
|
/** Type of the Rift API client */
|
|
1385
1616
|
type RiftClient = Treaty.Create<App>;
|
|
1386
1617
|
declare function createClient(baseUrl: string): RiftClient;
|
|
1387
|
-
import { Currency } from "@riftresearch/common";
|
|
1388
1618
|
/**
|
|
1389
1619
|
* Discriminated union of all possible swap routes.
|
|
1390
1620
|
*
|
|
1391
1621
|
* - direct_rift: BTC <-> EVM token (Rift handles directly)
|
|
1392
|
-
* -
|
|
1393
|
-
* -
|
|
1622
|
+
* - dex_then_rift: ERC20 (non-cbBTC) -> BTC (DEX to cbBTC, then Rift to BTC)
|
|
1623
|
+
* - dex_monochain: EVM token -> EVM token (single-chain DEX swap)
|
|
1394
1624
|
*/
|
|
1395
1625
|
type SwapRoute = {
|
|
1396
1626
|
type: "direct_rift";
|
|
1397
1627
|
direction: "to_btc" | "from_btc";
|
|
1398
1628
|
} | {
|
|
1399
|
-
type: "
|
|
1629
|
+
type: "dex_then_rift";
|
|
1400
1630
|
evmChainId: number;
|
|
1401
1631
|
} | {
|
|
1402
|
-
type: "
|
|
1632
|
+
type: "dex_monochain";
|
|
1403
1633
|
evmChainId: number;
|
|
1404
1634
|
};
|
|
1405
1635
|
/**
|
|
1406
1636
|
* Detect the appropriate swap route based on currency pair.
|
|
1407
1637
|
*
|
|
1408
1638
|
* Routes:
|
|
1409
|
-
* - EVM token → BTC: direct_rift (to_btc) for cbBTC,
|
|
1639
|
+
* - EVM token → BTC: direct_rift (to_btc) for cbBTC, dex_then_rift for other ERC20s
|
|
1410
1640
|
* - BTC → EVM token: direct_rift (from_btc) - Rift handles all BTC -> EVM swaps directly
|
|
1411
|
-
* - EVM token → EVM token (same chain):
|
|
1641
|
+
* - EVM token → EVM token (same chain): dex_monochain
|
|
1412
1642
|
*/
|
|
1413
1643
|
declare function detectRoute(from: Currency, to: Currency): SwapRoute;
|
|
1414
1644
|
interface SupportedModes {
|
|
@@ -1421,24 +1651,23 @@ interface SupportedModes {
|
|
|
1421
1651
|
* - cbBTC ↔ BTC: both modes supported
|
|
1422
1652
|
* - BTC → cbBTC: both modes supported
|
|
1423
1653
|
* - BTC → ERC20 (non-cbBTC): only exact_input (Rift doesn't support exact_output for chained swaps)
|
|
1424
|
-
* - ERC20 → BTC:
|
|
1654
|
+
* - ERC20 → BTC: both modes supported
|
|
1425
1655
|
*/
|
|
1426
1656
|
declare function getSupportedModes(from: Currency, to: Currency): SupportedModes;
|
|
1427
|
-
import { SwapStatusResponse as SwapStatusResponse3 } from "@riftresearch/common";
|
|
1428
|
-
import { SwapStatus as ApiSwapStatus, CalculatedInputAmount, CalculatedOutputAmount, Currency as Currency2, FeeBreakdown, SpecifiedAmount, SwapStatusResponse as SwapStatusResponse2 } from "@riftresearch/common";
|
|
1429
1657
|
import { Account, PublicClient, Transport, WalletClient } from "viem";
|
|
1430
|
-
import { Chain } from "viem/chains";
|
|
1431
|
-
|
|
1432
|
-
type RiftSwap = SwapStatusResponse2;
|
|
1658
|
+
import { Chain as Chain2 } from "viem/chains";
|
|
1659
|
+
type RiftSwap = SwapStatusResponse;
|
|
1433
1660
|
interface QuoteParameters {
|
|
1434
1661
|
/** The currency to swap from */
|
|
1435
|
-
from:
|
|
1662
|
+
from: Currency;
|
|
1436
1663
|
/** The currency to swap to */
|
|
1437
|
-
to:
|
|
1664
|
+
to: Currency;
|
|
1438
1665
|
/** Amount in smallest unit (sats for BTC, wei for ETH, etc.) */
|
|
1439
1666
|
amount: string;
|
|
1440
1667
|
/** Whether amount refers to input or output */
|
|
1441
1668
|
mode: "exact_input" | "exact_output";
|
|
1669
|
+
/** Optional DEX quote quality target for DEX-backed routes. */
|
|
1670
|
+
quoteQuality?: QuoteQuality;
|
|
1442
1671
|
/**
|
|
1443
1672
|
* Approval amount strategy for ERC20 swaps.
|
|
1444
1673
|
* - "full" (default): approve max uint256
|
|
@@ -1484,14 +1713,14 @@ interface ExactOutputQuoteResult extends QuoteResultBase {
|
|
|
1484
1713
|
type QuoteResult = ExactInputQuoteResult | ExactOutputQuoteResult;
|
|
1485
1714
|
interface GetQuoteResult {
|
|
1486
1715
|
quote: QuoteResult;
|
|
1487
|
-
executeSwap: <chain extends
|
|
1716
|
+
executeSwap: <chain extends Chain2 | undefined = Chain2 | undefined>(context: ExecuteSwapOptions<chain>) => Promise<SwapResult>;
|
|
1488
1717
|
}
|
|
1489
1718
|
interface SwapResult {
|
|
1490
1719
|
swapId: string;
|
|
1491
|
-
status:
|
|
1720
|
+
status: SwapStatus2;
|
|
1492
1721
|
rift: RiftSwap;
|
|
1493
1722
|
}
|
|
1494
|
-
type
|
|
1723
|
+
type SwapStatus2 = SwapStatus;
|
|
1495
1724
|
/**
|
|
1496
1725
|
* Function type for sending Bitcoin.
|
|
1497
1726
|
* Implementers provide this function to handle BTC transactions in their app.
|
|
@@ -1518,7 +1747,7 @@ type SendBitcoinFn = (params: {
|
|
|
1518
1747
|
/** Amount to send in satoshis */
|
|
1519
1748
|
amountSats: string;
|
|
1520
1749
|
}) => Promise<void>;
|
|
1521
|
-
type ExecuteSwapContext<chain extends
|
|
1750
|
+
type ExecuteSwapContext<chain extends Chain2 | undefined = Chain2 | undefined> = {
|
|
1522
1751
|
/** Viem PublicClient for reading chain data */
|
|
1523
1752
|
publicClient?: PublicClient<Transport, chain>;
|
|
1524
1753
|
/** Viem WalletClient for signing and sending transactions */
|
|
@@ -1526,7 +1755,7 @@ type ExecuteSwapContext<chain extends Chain | undefined = Chain | undefined> = {
|
|
|
1526
1755
|
/** Function to send Bitcoin (implement using your preferred wallet) */
|
|
1527
1756
|
sendBitcoin?: SendBitcoinFn;
|
|
1528
1757
|
};
|
|
1529
|
-
type ExecuteSwapOptions<chain extends
|
|
1758
|
+
type ExecuteSwapOptions<chain extends Chain2 | undefined = Chain2 | undefined> = ExecuteSwapContext<chain> & {
|
|
1530
1759
|
/** Address to receive the output tokens */
|
|
1531
1760
|
destinationAddress: string;
|
|
1532
1761
|
/** Address to receive refunds if swap fails. Defaults to source wallet address */
|
|
@@ -1580,7 +1809,7 @@ declare class RiftSdk {
|
|
|
1580
1809
|
private executeStep;
|
|
1581
1810
|
/**
|
|
1582
1811
|
* Execute an EVM call step - send a transaction with calldata.
|
|
1583
|
-
* Handles: approval, transfer_evm,
|
|
1812
|
+
* Handles: approval, transfer_evm, dex_swap
|
|
1584
1813
|
*/
|
|
1585
1814
|
private executeEvmCallStep;
|
|
1586
1815
|
/**
|
|
@@ -1600,7 +1829,7 @@ declare class RiftSdk {
|
|
|
1600
1829
|
/**
|
|
1601
1830
|
* Get the current status of a swap by its ID.
|
|
1602
1831
|
*/
|
|
1603
|
-
getSwapStatus(swapId: string): Promise<
|
|
1832
|
+
getSwapStatus(swapId: string): Promise<SwapStatusResponse>;
|
|
1604
1833
|
}
|
|
1605
1834
|
declare function createRiftSdk(options: RiftSdkOptions): RiftSdk;
|
|
1606
|
-
export { getSupportedModes, detectRoute, createRiftSdk, createCurrency, createClient, TokenIdentifier, SwapStatus, SwapRouterApiError, SwapRoute, SwapResult,
|
|
1835
|
+
export { getSupportedModes, detectRoute, createRiftSdk, createCurrency, createClient, TokenIdentifier, SwapStatus2 as SwapStatus, SwapRouterApiError, SwapRoute, SwapResult, SwapResponse, SupportedModes, SendBitcoinFn, RiftSwap, RiftSdkOptions, RiftSdk, RiftClient, QuoteResult, QuoteQuality, QuoteParameters, NativeToken, GetQuoteResult, ExecutionStep, ExecutionAction, ExecuteSwapOptions, EvmChain, EvmCallStep, EvmCallKind, Erc20Token, Currency, Currencies, Chain, BtcTransferStep, BtcTransferKind, BitcoinChain, App };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// ../common/
|
|
1
|
+
// ../common/src/constants.ts
|
|
2
2
|
var CBBTC_ADDRESS = "0xcbB7C0000aB88B473b1f5aFd9ef808440eed33Bf";
|
|
3
3
|
var CBBTC_ADDRESS_LOWER = CBBTC_ADDRESS.toLowerCase();
|
|
4
4
|
var BTC_CHAIN = {
|
|
@@ -106,35 +106,35 @@ import { match, P } from "ts-pattern";
|
|
|
106
106
|
var CBBTC_ADDRESS2 = "0xcbb7c0000ab88b473b1f5afd9ef808440eed33bf";
|
|
107
107
|
var isBitcoin = (currency) => currency.chain.kind === "BITCOIN";
|
|
108
108
|
var isCbBtc = (currency) => currency.chain.kind === "EVM" && currency.token.kind === "TOKEN" && currency.token.address.toLowerCase() === CBBTC_ADDRESS2;
|
|
109
|
-
var
|
|
110
|
-
var
|
|
109
|
+
var isEvmToken2 = (currency) => currency.chain.kind === "EVM";
|
|
110
|
+
var isSameEvmChain2 = (a, b) => a.chain.kind === "EVM" && b.chain.kind === "EVM" && a.chain.chainId === b.chain.chainId;
|
|
111
111
|
function detectRoute(from, to) {
|
|
112
112
|
return match({ from, to }).with({ from: P.when(isCbBtc), to: P.when(isBitcoin) }, () => ({
|
|
113
113
|
type: "direct_rift",
|
|
114
114
|
direction: "to_btc"
|
|
115
|
-
})).with({ from: P.when(isBitcoin), to: P.when(
|
|
115
|
+
})).with({ from: P.when(isBitcoin), to: P.when(isEvmToken2) }, () => ({
|
|
116
116
|
type: "direct_rift",
|
|
117
117
|
direction: "from_btc"
|
|
118
118
|
})).with({
|
|
119
|
-
from: P.when((c) =>
|
|
119
|
+
from: P.when((c) => isEvmToken2(c) && !isCbBtc(c)),
|
|
120
120
|
to: P.when(isBitcoin)
|
|
121
121
|
}, ({ from: from2 }) => {
|
|
122
122
|
if (from2.chain.kind !== "EVM") {
|
|
123
123
|
throw new Error("Expected EVM chain");
|
|
124
124
|
}
|
|
125
125
|
return {
|
|
126
|
-
type: "
|
|
126
|
+
type: "dex_then_rift",
|
|
127
127
|
evmChainId: from2.chain.chainId
|
|
128
128
|
};
|
|
129
129
|
}).with({
|
|
130
|
-
from: P.when(
|
|
131
|
-
to: P.when((c) =>
|
|
130
|
+
from: P.when(isEvmToken2),
|
|
131
|
+
to: P.when((c) => isEvmToken2(c) && isSameEvmChain2(from, to))
|
|
132
132
|
}, ({ from: from2 }) => {
|
|
133
133
|
if (from2.chain.kind !== "EVM") {
|
|
134
134
|
throw new Error("Expected EVM chain");
|
|
135
135
|
}
|
|
136
136
|
return {
|
|
137
|
-
type: "
|
|
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 === "
|
|
147
|
-
return { exactInput: true, exactOutput:
|
|
146
|
+
if (route.type === "dex_then_rift") {
|
|
147
|
+
return { exactInput: true, exactOutput: true };
|
|
148
148
|
}
|
|
149
|
-
if (route.type === "
|
|
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://
|
|
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,12 +203,13 @@ class RiftSdk {
|
|
|
200
203
|
}
|
|
201
204
|
async getQuote(params) {
|
|
202
205
|
const route = detectRoute(params.from, params.to);
|
|
203
|
-
const isMonochain = route.type === "
|
|
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);
|
|
@@ -257,10 +261,10 @@ class RiftSdk {
|
|
|
257
261
|
stepId: step.id,
|
|
258
262
|
txHash: result.txHash
|
|
259
263
|
});
|
|
260
|
-
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) {
|
|
261
265
|
this.logDebug("reporting step result", {
|
|
262
266
|
stepId: step.id,
|
|
263
|
-
|
|
267
|
+
dexSwap: true
|
|
264
268
|
});
|
|
265
269
|
this.unwrapEdenResult(await this.riftClient.swap({ swapId: swapResponse.swapId }).tx.post({
|
|
266
270
|
stepId: step.id,
|
|
@@ -309,13 +313,29 @@ class RiftSdk {
|
|
|
309
313
|
return {};
|
|
310
314
|
}
|
|
311
315
|
}
|
|
312
|
-
const
|
|
316
|
+
const txRequest = {
|
|
313
317
|
account,
|
|
314
318
|
to: step.to,
|
|
315
319
|
data: step.calldata,
|
|
316
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()
|
|
317
328
|
});
|
|
318
|
-
await
|
|
329
|
+
const txHash = await walletClient.sendTransaction({
|
|
330
|
+
...txRequest,
|
|
331
|
+
gas: gasLimit
|
|
332
|
+
});
|
|
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
|
+
}
|
|
319
339
|
return { txHash };
|
|
320
340
|
}
|
|
321
341
|
async executeBtcTransferStep(step, context) {
|