@riftresearch/sdk 0.1.3 → 0.2.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/README.md +13 -56
- package/dist/index.d.ts +275 -182
- package/dist/index.js +123 -1232
- package/package.json +4 -27
- package/LICENSE +0 -21
package/README.md
CHANGED
|
@@ -14,11 +14,9 @@ yarn add @riftresearch/sdk
|
|
|
14
14
|
bun add @riftresearch/sdk
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
##
|
|
17
|
+
## Quickstart
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
```typescript
|
|
19
|
+
```ts
|
|
22
20
|
import { RiftSdk, BTC, type Currency } from '@riftresearch/sdk'
|
|
23
21
|
import { createPublicClient, createWalletClient, http } from 'viem'
|
|
24
22
|
import { privateKeyToAccount } from 'viem/accounts'
|
|
@@ -48,79 +46,38 @@ const sdk = new RiftSdk({
|
|
|
48
46
|
publicClient,
|
|
49
47
|
walletClient,
|
|
50
48
|
sendBitcoin: async ({ recipient, amountSats }) => {
|
|
51
|
-
// Only needed for BTC
|
|
49
|
+
// Only needed for BTC -> ERC20 swaps
|
|
52
50
|
throw new Error('Not implemented')
|
|
53
51
|
},
|
|
54
52
|
})
|
|
55
53
|
|
|
56
|
-
// Get quote
|
|
54
|
+
// Get a quote
|
|
57
55
|
const { quote, executeSwap } = await sdk.getQuote({
|
|
58
56
|
from: USDC_BASE,
|
|
59
57
|
to: BTC,
|
|
60
58
|
amount: '100000000', // 100 USDC (6 decimals)
|
|
61
59
|
mode: 'exact_input',
|
|
62
|
-
destinationAddress: 'bc1q...',
|
|
60
|
+
destinationAddress: 'bc1q...',
|
|
63
61
|
})
|
|
64
62
|
|
|
65
63
|
console.log(`Swapping ${quote.from.amount} USDC for ${quote.to.amount} sats`)
|
|
66
64
|
console.log(`Fees: $${quote.fees.totalUsd.toFixed(2)}`)
|
|
67
65
|
|
|
68
|
-
// Execute the swap
|
|
66
|
+
// Execute the swap
|
|
69
67
|
const swap = await executeSwap()
|
|
70
|
-
console.log(`Swap ID: ${swap.swapId}`)
|
|
68
|
+
console.log(`Swap ID: ${swap.swapId}`) // API order ID
|
|
71
69
|
|
|
72
70
|
// Check status
|
|
73
71
|
const status = await sdk.getSwapStatus(swap.swapId)
|
|
74
72
|
console.log(`Status: ${status.status}`)
|
|
75
73
|
```
|
|
76
74
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
```typescript
|
|
80
|
-
import { RiftSdk, BTC, type Currency } from '@riftresearch/sdk'
|
|
81
|
-
import { createPublicClient, createWalletClient, http } from 'viem'
|
|
82
|
-
import { privateKeyToAccount } from 'viem/accounts'
|
|
83
|
-
import { mainnet } from 'viem/chains'
|
|
84
|
-
|
|
85
|
-
// Define ETH on Ethereum mainnet
|
|
86
|
-
const ETH_MAINNET: Currency = {
|
|
87
|
-
chain: { kind: 'EVM', chainId: 1 },
|
|
88
|
-
token: { kind: 'NATIVE', decimals: 18 },
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const account = privateKeyToAccount('0x...')
|
|
92
|
-
const publicClient = createPublicClient({ chain: mainnet, transport: http() })
|
|
93
|
-
const walletClient = createWalletClient({
|
|
94
|
-
account,
|
|
95
|
-
chain: mainnet,
|
|
96
|
-
transport: http(),
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
const sdk = new RiftSdk({
|
|
100
|
-
publicClient,
|
|
101
|
-
walletClient,
|
|
102
|
-
sendBitcoin: async ({ recipient, amountSats }) => {
|
|
103
|
-
// Implement using your Bitcoin wallet (Xverse, Leather, etc.)
|
|
104
|
-
await sendBitcoinTransaction(recipient, amountSats)
|
|
105
|
-
},
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
// Get quote for swapping 0.01 BTC to ETH
|
|
109
|
-
const { quote, executeSwap } = await sdk.getQuote({
|
|
110
|
-
from: BTC,
|
|
111
|
-
to: ETH_MAINNET,
|
|
112
|
-
amount: '1000000', // 0.01 BTC (8 decimals = 1,000,000 sats)
|
|
113
|
-
mode: 'exact_input',
|
|
114
|
-
destinationAddress: '0x...', // Your Ethereum address
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
console.log(`Swapping ${quote.from.amount} sats for ${quote.to.amount} wei`)
|
|
118
|
-
|
|
119
|
-
const swap = await executeSwap()
|
|
120
|
-
console.log(`Swap ID: ${swap.swapId}`)
|
|
121
|
-
```
|
|
75
|
+
## Build & Publish
|
|
122
76
|
|
|
77
|
+
The SDK uses **bunup**.
|
|
123
78
|
|
|
124
|
-
|
|
79
|
+
- Build: `bun run --filter '@riftresearch/sdk' build`
|
|
80
|
+
- Publish (from repo root): `bun run publish:sdk`
|
|
81
|
+
- Release (version bump + build + publish): `bun run release:sdk`
|
|
125
82
|
|
|
126
|
-
|
|
83
|
+
The `dist/` directory is what gets published to npm.
|
package/dist/index.d.ts
CHANGED
|
@@ -21,124 +21,264 @@ interface Currency {
|
|
|
21
21
|
chain: Chain;
|
|
22
22
|
token: TokenIdentifier;
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
/**
|
|
25
|
+
* A currency amount that was specified by the user (exact).
|
|
26
|
+
*/
|
|
27
|
+
interface SpecifiedAmount {
|
|
28
|
+
currency: Currency;
|
|
27
29
|
amount: U256;
|
|
28
|
-
deposit_detected_at: string;
|
|
29
|
-
confirmations: number;
|
|
30
|
-
last_checked: string;
|
|
31
|
-
confirmed_at?: string;
|
|
32
30
|
}
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
/**
|
|
32
|
+
* A calculated output amount for exact_input mode.
|
|
33
|
+
* The user specifies exact input, and this represents the expected output.
|
|
34
|
+
*/
|
|
35
|
+
interface CalculatedOutputAmount {
|
|
36
|
+
currency: Currency;
|
|
37
|
+
/** Expected output based on current market conditions */
|
|
35
38
|
amount: U256;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
last_checked: string;
|
|
39
|
+
/** Minimum guaranteed output after slippage (optional - not all routes provide this) */
|
|
40
|
+
minimum?: U256;
|
|
39
41
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
/**
|
|
43
|
+
* A calculated input amount for exact_output mode.
|
|
44
|
+
* The user specifies exact output, and this represents the expected input.
|
|
45
|
+
*/
|
|
46
|
+
interface CalculatedInputAmount {
|
|
47
|
+
currency: Currency;
|
|
48
|
+
/** Expected input based on current market conditions */
|
|
49
|
+
amount: U256;
|
|
50
|
+
/** Maximum input required after slippage (optional - not all routes provide this) */
|
|
51
|
+
maximum?: U256;
|
|
46
52
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
53
|
+
/**
|
|
54
|
+
* A single fee component with amount in native currency and USD equivalent.
|
|
55
|
+
*/
|
|
56
|
+
interface FeeComponent {
|
|
57
|
+
/** Amount in the smallest unit of the currency */
|
|
58
|
+
amount: U256;
|
|
59
|
+
/** The currency this fee is denominated in */
|
|
60
|
+
currency: Currency;
|
|
61
|
+
/** USD equivalent of the fee */
|
|
62
|
+
usd: number;
|
|
50
63
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Rift protocol fees broken down by type.
|
|
66
|
+
*/
|
|
67
|
+
interface RiftFees {
|
|
68
|
+
/** Bitcoin network transaction fee */
|
|
69
|
+
network: FeeComponent;
|
|
70
|
+
/** Fee paid to liquidity providers */
|
|
71
|
+
liquidity: FeeComponent;
|
|
72
|
+
/** Rift protocol fee */
|
|
73
|
+
protocol: FeeComponent;
|
|
57
74
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Complete fee breakdown for a swap quote.
|
|
77
|
+
* Shows fees from each leg of the swap in their native currencies.
|
|
78
|
+
*/
|
|
79
|
+
interface FeeBreakdown {
|
|
80
|
+
/** Fees from DEX swap BEFORE Rift (e.g., 1inch USDC → cbBTC) */
|
|
81
|
+
preswap?: FeeComponent;
|
|
82
|
+
/** Fees from the Rift protocol (optional for mono-chain swaps) */
|
|
83
|
+
rift?: RiftFees;
|
|
84
|
+
/** Fees from DEX swap AFTER Rift (e.g., Swapper cbBTC → USDC) */
|
|
85
|
+
postswap?: FeeComponent;
|
|
86
|
+
/** Total fees in USD across all legs */
|
|
87
|
+
totalUsd: number;
|
|
62
88
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Base fields shared by all quote responses.
|
|
91
|
+
*/
|
|
92
|
+
interface QuoteResponseBase {
|
|
93
|
+
id: string;
|
|
94
|
+
fees: FeeBreakdown;
|
|
95
|
+
expiresAt: string;
|
|
67
96
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Quote response for exact_input mode.
|
|
99
|
+
* User specifies exact input amount, output is calculated with slippage bound.
|
|
100
|
+
*/
|
|
101
|
+
interface ExactInputQuoteResponse extends QuoteResponseBase {
|
|
102
|
+
mode: "exact_input";
|
|
103
|
+
/** The exact input amount specified by the user */
|
|
104
|
+
from: SpecifiedAmount;
|
|
105
|
+
/** The calculated output amount with slippage bound */
|
|
106
|
+
to: CalculatedOutputAmount;
|
|
77
107
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
108
|
+
/**
|
|
109
|
+
* Quote response for exact_output mode.
|
|
110
|
+
* User specifies exact output amount, input is calculated with slippage bound.
|
|
111
|
+
*/
|
|
112
|
+
interface ExactOutputQuoteResponse extends QuoteResponseBase {
|
|
113
|
+
mode: "exact_output";
|
|
114
|
+
/** The calculated input amount with slippage bound */
|
|
115
|
+
from: CalculatedInputAmount;
|
|
116
|
+
/** The exact output amount specified by the user */
|
|
117
|
+
to: SpecifiedAmount;
|
|
81
118
|
}
|
|
82
|
-
|
|
119
|
+
/**
|
|
120
|
+
* Discriminated union of quote responses.
|
|
121
|
+
* Use `mode` to determine which fields are specified vs calculated.
|
|
122
|
+
*/
|
|
123
|
+
type QuoteResponse = ExactInputQuoteResponse | ExactOutputQuoteResponse;
|
|
124
|
+
type OrderStatus = "waiting_for_deposit" | "deposit_confirming" | "initiating_transfer" | "confirming_transfer" | "swap_complete" | "refunding_user" | "failed";
|
|
125
|
+
interface OrderStatusResponse {
|
|
126
|
+
status: OrderStatus;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Execution actions - the mechanism by which a step is executed.
|
|
130
|
+
*/
|
|
131
|
+
type ExecutionAction = "evm_call" | "btc_transfer";
|
|
132
|
+
/**
|
|
133
|
+
* Step kinds grouped by action type.
|
|
134
|
+
*/
|
|
135
|
+
type EvmCallKind = "approval" | "transfer_erc20" | "oneinch_swap";
|
|
136
|
+
type BtcTransferKind = "transfer_btc";
|
|
137
|
+
/**
|
|
138
|
+
* EVM Call step - execute calldata on an EVM chain.
|
|
139
|
+
* Used for: token approvals, ERC20 transfers, 1inch swaps.
|
|
140
|
+
*/
|
|
141
|
+
interface EvmCallStep {
|
|
142
|
+
/** Step ID for tracking */
|
|
83
143
|
id: string;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
144
|
+
/** Execution mechanism */
|
|
145
|
+
action: "evm_call";
|
|
146
|
+
/** Specific step type */
|
|
147
|
+
kind: EvmCallKind;
|
|
148
|
+
/** Chain ID for the transaction */
|
|
149
|
+
chainId: number;
|
|
150
|
+
/** Contract address to call */
|
|
151
|
+
to: string;
|
|
152
|
+
/** Encoded calldata */
|
|
153
|
+
calldata: string;
|
|
154
|
+
/** Native ETH value to send (for oneinch_swap) */
|
|
155
|
+
value?: U256;
|
|
156
|
+
/** Token address (for approval, transfer_evm) */
|
|
157
|
+
tokenAddress?: string;
|
|
158
|
+
/** Spender address (for approval) */
|
|
159
|
+
spenderAddress?: string;
|
|
160
|
+
/** Amount for display/verification (for approval, transfer_evm) */
|
|
161
|
+
amount?: U256;
|
|
94
162
|
}
|
|
95
|
-
|
|
163
|
+
/**
|
|
164
|
+
* BTC Transfer step - send Bitcoin to an address.
|
|
165
|
+
* Used for: depositing BTC to Rift vault.
|
|
166
|
+
*/
|
|
167
|
+
interface BtcTransferStep {
|
|
168
|
+
/** Step ID for tracking */
|
|
96
169
|
id: string;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
mm_nonce: string;
|
|
106
|
-
user_destination_address: string;
|
|
107
|
-
refund_address: string;
|
|
108
|
-
status: SwapStatus;
|
|
109
|
-
user_deposit_status?: UserDepositStatus | Record<string, never>;
|
|
110
|
-
mm_deposit_status?: MMDepositStatus | Record<string, never>;
|
|
111
|
-
settlement_status?: SettlementStatus | Record<string, never>;
|
|
112
|
-
latest_refund?: LatestRefund | Record<string, never>;
|
|
113
|
-
failure_reason?: string | null;
|
|
114
|
-
failure_at?: string | null;
|
|
115
|
-
mm_notified_at?: string | null;
|
|
116
|
-
mm_private_key_sent_at?: string | null;
|
|
117
|
-
created_at: string;
|
|
118
|
-
updated_at: string;
|
|
170
|
+
/** Execution mechanism */
|
|
171
|
+
action: "btc_transfer";
|
|
172
|
+
/** Specific step type */
|
|
173
|
+
kind: BtcTransferKind;
|
|
174
|
+
/** Bitcoin address to send to */
|
|
175
|
+
toAddress: string;
|
|
176
|
+
/** Amount in satoshis */
|
|
177
|
+
amountSats: U256;
|
|
119
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* Discriminated union of all execution step types.
|
|
181
|
+
* Discriminate on `action` for execution logic, or `kind` for specific handling.
|
|
182
|
+
*/
|
|
183
|
+
type ExecutionStep = EvmCallStep | BtcTransferStep;
|
|
184
|
+
/**
|
|
185
|
+
* Order response with execution steps that the client must execute.
|
|
186
|
+
*/
|
|
187
|
+
interface OrderResponse {
|
|
188
|
+
/** The Rift swap/order ID */
|
|
189
|
+
swapId: string;
|
|
190
|
+
/** Normalized quote matching /quote response */
|
|
191
|
+
quote: QuoteResponse;
|
|
192
|
+
/** Ordered list of steps the client must execute */
|
|
193
|
+
executionSteps: ExecutionStep[];
|
|
194
|
+
}
|
|
195
|
+
/** Native BTC on Bitcoin */
|
|
120
196
|
declare const BTC: Currency;
|
|
197
|
+
/** cbBTC on Ethereum mainnet */
|
|
121
198
|
declare const CBBTC_ETHEREUM: Currency;
|
|
199
|
+
/** cbBTC on Base */
|
|
122
200
|
declare const CBBTC_BASE: Currency;
|
|
201
|
+
import { treaty } from "@elysiajs/eden";
|
|
202
|
+
import { Elysia } from "elysia";
|
|
203
|
+
declare const app: Elysia;
|
|
204
|
+
declare const appTyped: typeof app;
|
|
205
|
+
type App = typeof appTyped;
|
|
206
|
+
/**
|
|
207
|
+
* Create a type-safe API client for the Rift Swap Router.
|
|
208
|
+
*
|
|
209
|
+
* @param baseUrl - The base URL of the swap router API (e.g., 'https://router-api-v2-production.up.railway.app')
|
|
210
|
+
* @returns A fully typed Eden Treaty client
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```typescript
|
|
214
|
+
* import { createClient } from '@riftresearch/sdk'
|
|
215
|
+
*
|
|
216
|
+
* const api = createClient('https://router-api-v2-production.up.railway.app')
|
|
217
|
+
*
|
|
218
|
+
* // Get a quote - fully typed request and response
|
|
219
|
+
* const { data: quote, error } = await api.quote.post({
|
|
220
|
+
* type: 'EXACT_INPUT',
|
|
221
|
+
* from: {
|
|
222
|
+
* chain: { kind: 'BITCOIN' },
|
|
223
|
+
* token: { kind: 'NATIVE', decimals: 8 },
|
|
224
|
+
* },
|
|
225
|
+
* to: {
|
|
226
|
+
* chain: { kind: 'EVM', chainId: 8453 },
|
|
227
|
+
* token: { kind: 'TOKEN', address: '0xcbB7C0000aB88B473b1f5aFd9ef808440eed33Bf', decimals: 8 },
|
|
228
|
+
* },
|
|
229
|
+
* amount: '100000', // 0.001 BTC in sats
|
|
230
|
+
* })
|
|
231
|
+
*
|
|
232
|
+
* if (error) {
|
|
233
|
+
* console.error('Failed to get quote:', error)
|
|
234
|
+
* return
|
|
235
|
+
* }
|
|
236
|
+
*
|
|
237
|
+
* console.log('Quote ID:', quote.id)
|
|
238
|
+
* console.log('You will receive:', quote.to.amount)
|
|
239
|
+
*
|
|
240
|
+
* // Create an order from the quote
|
|
241
|
+
* const { data: order, error: orderError } = await api.order.post({
|
|
242
|
+
* id: quote.id,
|
|
243
|
+
* destinationAddress: '0x...',
|
|
244
|
+
* refundAddress: 'bc1q...',
|
|
245
|
+
* })
|
|
246
|
+
*
|
|
247
|
+
* if (order) {
|
|
248
|
+
* console.log('Deposit to:', order.deposit_vault_address)
|
|
249
|
+
* }
|
|
250
|
+
*
|
|
251
|
+
* // Check order status
|
|
252
|
+
* const { data: status } = await api.order({ orderId: order.id }).get()
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
/** Type of the Rift API client */
|
|
256
|
+
type RiftClient = ReturnType<typeof treaty<App>>;
|
|
257
|
+
declare function createClient(baseUrl: string): RiftClient;
|
|
123
258
|
/**
|
|
124
259
|
* Discriminated union of all possible swap routes.
|
|
125
260
|
*
|
|
126
261
|
* - direct_rift: BTC <-> EVM token (Rift handles directly)
|
|
127
|
-
* -
|
|
262
|
+
* - oneinch_then_rift: ERC20 (non-cbBTC) -> BTC (1inch to cbBTC, then Rift to BTC)
|
|
263
|
+
* - oneinch_monochain: EVM token -> EVM token (single-chain 1inch swap)
|
|
128
264
|
*/
|
|
129
265
|
type SwapRoute = {
|
|
130
266
|
type: "direct_rift";
|
|
131
267
|
direction: "to_btc" | "from_btc";
|
|
132
268
|
} | {
|
|
133
|
-
type: "
|
|
269
|
+
type: "oneinch_then_rift";
|
|
270
|
+
evmChainId: number;
|
|
271
|
+
} | {
|
|
272
|
+
type: "oneinch_monochain";
|
|
134
273
|
evmChainId: number;
|
|
135
274
|
};
|
|
136
275
|
/**
|
|
137
276
|
* Detect the appropriate swap route based on currency pair.
|
|
138
277
|
*
|
|
139
278
|
* Routes:
|
|
140
|
-
* - EVM token → BTC: direct_rift (to_btc) for cbBTC,
|
|
279
|
+
* - EVM token → BTC: direct_rift (to_btc) for cbBTC, oneinch_then_rift for other ERC20s
|
|
141
280
|
* - BTC → EVM token: direct_rift (from_btc) - Rift handles all BTC -> EVM swaps directly
|
|
281
|
+
* - EVM token → EVM token (same chain): oneinch_monochain
|
|
142
282
|
*/
|
|
143
283
|
declare function detectRoute(from: Currency, to: Currency): SwapRoute;
|
|
144
284
|
interface SupportedModes {
|
|
@@ -151,11 +291,11 @@ interface SupportedModes {
|
|
|
151
291
|
* - cbBTC ↔ BTC: both modes supported
|
|
152
292
|
* - BTC → cbBTC: both modes supported
|
|
153
293
|
* - BTC → ERC20 (non-cbBTC): only exact_input (Rift doesn't support exact_output for chained swaps)
|
|
154
|
-
* - ERC20 → BTC:
|
|
294
|
+
* - ERC20 → BTC: exact_input only (1inch is exact-input quoting)
|
|
155
295
|
*/
|
|
156
296
|
declare function getSupportedModes(from: Currency, to: Currency): SupportedModes;
|
|
157
297
|
import { PublicClient, WalletClient } from "viem";
|
|
158
|
-
type RiftOrder =
|
|
298
|
+
type RiftOrder = OrderStatusResponse;
|
|
159
299
|
interface TradeParameters {
|
|
160
300
|
/** The currency to swap from */
|
|
161
301
|
from: Currency;
|
|
@@ -170,58 +310,63 @@ interface TradeParameters {
|
|
|
170
310
|
/** Address to receive refunds if swap fails. Defaults to source wallet address */
|
|
171
311
|
refundAddress?: string;
|
|
172
312
|
/**
|
|
173
|
-
*
|
|
313
|
+
* Approval amount strategy for ERC20 swaps.
|
|
314
|
+
* - "full" (default): approve max uint256
|
|
315
|
+
* - "partial": approve only the sell amount
|
|
316
|
+
*/
|
|
317
|
+
approvalMode?: "full" | "partial";
|
|
318
|
+
/**
|
|
319
|
+
* Slippage tolerance for the 1inch swap leg, in basis points.
|
|
174
320
|
* e.g., 50 = 0.5%, 100 = 1%
|
|
175
|
-
*
|
|
176
|
-
* If not provided, CowSwap will use auto slippage which suggests an
|
|
177
|
-
* appropriate value based on the quote and market conditions.
|
|
178
321
|
*/
|
|
179
322
|
preswapSlippageBps?: number;
|
|
180
323
|
}
|
|
181
|
-
|
|
324
|
+
/**
|
|
325
|
+
* Base fields shared by all quote results.
|
|
326
|
+
*/
|
|
327
|
+
interface QuoteResultBase {
|
|
182
328
|
quoteId: string;
|
|
183
|
-
|
|
184
|
-
currency: Currency;
|
|
185
|
-
amount: string;
|
|
186
|
-
};
|
|
187
|
-
to: {
|
|
188
|
-
currency: Currency;
|
|
189
|
-
amount: string;
|
|
190
|
-
};
|
|
191
|
-
fees: {
|
|
192
|
-
protocol: string;
|
|
193
|
-
liquidity: string;
|
|
194
|
-
network: string;
|
|
195
|
-
totalUsd: number;
|
|
196
|
-
};
|
|
329
|
+
fees: FeeBreakdown;
|
|
197
330
|
expiresAt: Date;
|
|
198
331
|
priceImpact?: number;
|
|
199
332
|
}
|
|
333
|
+
/**
|
|
334
|
+
* Quote result for exact_input mode.
|
|
335
|
+
* User specifies exact input amount, output is calculated with slippage bound.
|
|
336
|
+
*/
|
|
337
|
+
interface ExactInputQuoteResult extends QuoteResultBase {
|
|
338
|
+
mode: "exact_input";
|
|
339
|
+
/** The exact input amount specified by the user */
|
|
340
|
+
from: SpecifiedAmount;
|
|
341
|
+
/** The calculated output amount with slippage bound */
|
|
342
|
+
to: CalculatedOutputAmount;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Quote result for exact_output mode.
|
|
346
|
+
* User specifies exact output amount, input is calculated with slippage bound.
|
|
347
|
+
*/
|
|
348
|
+
interface ExactOutputQuoteResult extends QuoteResultBase {
|
|
349
|
+
mode: "exact_output";
|
|
350
|
+
/** The calculated input amount with slippage bound */
|
|
351
|
+
from: CalculatedInputAmount;
|
|
352
|
+
/** The exact output amount specified by the user */
|
|
353
|
+
to: SpecifiedAmount;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Discriminated union of quote results.
|
|
357
|
+
* Use `mode` to determine which fields are specified vs calculated.
|
|
358
|
+
*/
|
|
359
|
+
type QuoteResult = ExactInputQuoteResult | ExactOutputQuoteResult;
|
|
200
360
|
interface GetQuoteResult {
|
|
201
361
|
quote: QuoteResult;
|
|
202
362
|
executeSwap: () => Promise<SwapResult>;
|
|
203
363
|
}
|
|
204
|
-
/** Raw CowSwap order from the API */
|
|
205
|
-
interface CowSwapOrder {
|
|
206
|
-
uid: string;
|
|
207
|
-
status: string;
|
|
208
|
-
sellToken: string;
|
|
209
|
-
buyToken: string;
|
|
210
|
-
sellAmount: string;
|
|
211
|
-
buyAmount: string;
|
|
212
|
-
executedSellAmount: string;
|
|
213
|
-
executedBuyAmount: string;
|
|
214
|
-
receiver: string;
|
|
215
|
-
validTo: number;
|
|
216
|
-
creationDate: string;
|
|
217
|
-
}
|
|
218
364
|
interface SwapResult {
|
|
219
365
|
swapId: string;
|
|
220
366
|
status: SwapStatus2;
|
|
221
367
|
rift: RiftOrder;
|
|
222
|
-
preswap?: CowSwapOrder;
|
|
223
368
|
}
|
|
224
|
-
type SwapStatus2 =
|
|
369
|
+
type SwapStatus2 = OrderStatus;
|
|
225
370
|
/**
|
|
226
371
|
* Function type for sending Bitcoin.
|
|
227
372
|
* Implementers provide this function to handle BTC transactions in their app.
|
|
@@ -281,78 +426,26 @@ declare class RiftSdk {
|
|
|
281
426
|
*/
|
|
282
427
|
getQuote(params: TradeParameters): Promise<GetQuoteResult>;
|
|
283
428
|
/**
|
|
284
|
-
*
|
|
285
|
-
|
|
286
|
-
private getDirectRiftQuote;
|
|
287
|
-
/**
|
|
288
|
-
* ERC20 → cbBTC (CowSwap) → BTC (Rift)
|
|
429
|
+
* Execute a single step from the server's execution steps.
|
|
430
|
+
* Dispatch based on `action` (the execution mechanism).
|
|
289
431
|
*/
|
|
290
|
-
private
|
|
432
|
+
private executeStep;
|
|
291
433
|
/**
|
|
292
|
-
*
|
|
434
|
+
* Execute an EVM call step - send a transaction with calldata.
|
|
435
|
+
* Handles: approval, transfer_evm, oneinch_swap
|
|
293
436
|
*/
|
|
294
|
-
private
|
|
437
|
+
private executeEvmCallStep;
|
|
295
438
|
/**
|
|
296
|
-
*
|
|
439
|
+
* Execute a BTC transfer step - send BTC to the vault address.
|
|
297
440
|
*/
|
|
298
|
-
private
|
|
299
|
-
/**
|
|
300
|
-
* Execute the CowSwap → Rift swap flow.
|
|
301
|
-
*
|
|
302
|
-
* Flow:
|
|
303
|
-
* 1. Create Rift order first to get deposit vault address
|
|
304
|
-
* 2. Ensure CowSwap approval for the sell token (skip for native tokens)
|
|
305
|
-
* 3. Get fresh CowSwap quote with receiver = deposit vault
|
|
306
|
-
* 4. Post CowSwap order → cbBTC goes directly to Rift vault
|
|
307
|
-
*/
|
|
308
|
-
private executeCowswapThenRift;
|
|
441
|
+
private executeBtcTransferStep;
|
|
309
442
|
private buildQuoteResult;
|
|
310
|
-
private buildCombinedQuoteResult;
|
|
311
443
|
private buildSwapResult;
|
|
312
|
-
private executeDeposit;
|
|
313
|
-
private transferErc20;
|
|
314
444
|
private getAddress;
|
|
315
445
|
private getRefundAddress;
|
|
316
446
|
/**
|
|
317
|
-
* Map API status to SDK status.
|
|
318
|
-
*
|
|
319
|
-
* For chained swaps (BTC → non-cbBTC ERC20), the user doesn't receive funds
|
|
320
|
-
* until settlement is complete, so we use different statuses.
|
|
321
|
-
*/
|
|
322
|
-
private mapStatus;
|
|
323
|
-
/**
|
|
324
447
|
* Get the current status of a swap by its ID.
|
|
325
|
-
*
|
|
326
|
-
* For multi-leg swaps (ERC20 → BTC via CowSwap), returns both
|
|
327
|
-
* the raw CowSwap order and the raw Rift order.
|
|
328
448
|
*/
|
|
329
449
|
getSwapStatus(swapId: string): Promise<SwapResult>;
|
|
330
|
-
private getCowSwapOrder;
|
|
331
|
-
private fetchCowSwapOrder;
|
|
332
|
-
private computeOverallStatus;
|
|
333
450
|
}
|
|
334
|
-
|
|
335
|
-
* URL-safe swap ID encoding/decoding.
|
|
336
|
-
*
|
|
337
|
-
* Format: base64url encoded string containing:
|
|
338
|
-
* - Rift-only: "r|<riftOrderId>" or "r|<riftOrderId>|c" (chained)
|
|
339
|
-
* - CowSwap+Rift: "c|<cowOrderId>|<riftOrderId>"
|
|
340
|
-
*/
|
|
341
|
-
type SwapIdData = {
|
|
342
|
-
type: "rift";
|
|
343
|
-
riftOrderId: string;
|
|
344
|
-
chained?: boolean;
|
|
345
|
-
} | {
|
|
346
|
-
type: "cowswap_rift";
|
|
347
|
-
cowOrderId: string;
|
|
348
|
-
riftOrderId: string;
|
|
349
|
-
};
|
|
350
|
-
/**
|
|
351
|
-
* Encode swap ID data into a URL-safe string.
|
|
352
|
-
*/
|
|
353
|
-
declare function encodeSwapId(data: SwapIdData): string;
|
|
354
|
-
/**
|
|
355
|
-
* Decode a URL-safe swap ID string back to its components.
|
|
356
|
-
*/
|
|
357
|
-
declare function decodeSwapId(encoded: string): SwapIdData;
|
|
358
|
-
export { getSupportedModes, encodeSwapId, detectRoute, decodeSwapId, TradeParameters, TokenIdentifier, SwapStatus2 as SwapStatus, SwapRoute, SwapResult, SwapIdData, SupportedModes, SendBitcoinFn, RiftSdkOptions, RiftSdk, RiftOrder, QuoteResult, NativeToken, GetQuoteResult, EvmChain, Erc20Token, Currency, CowSwapOrder, Chain, CBBTC_ETHEREUM, CBBTC_BASE, BitcoinChain, BTC, SwapStatus as ApiSwapStatus };
|
|
451
|
+
export { getSupportedModes, detectRoute, createClient, TradeParameters, TokenIdentifier, SwapStatus2 as SwapStatus, SwapRoute, SwapResult, SupportedModes, SendBitcoinFn, RiftSdkOptions, RiftSdk, RiftOrder, RiftClient, QuoteResult, OrderResponse, NativeToken, GetQuoteResult, ExecutionStep, ExecutionAction, EvmChain, EvmCallStep, EvmCallKind, Erc20Token, Currency, Chain, CBBTC_ETHEREUM, CBBTC_BASE, BtcTransferStep, BtcTransferKind, BitcoinChain, BTC, App };
|