@riftresearch/sdk 0.2.3 → 0.3.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 CHANGED
@@ -2,43 +2,30 @@
2
2
 
3
3
  SDK for swapping between Bitcoin and EVM chains.
4
4
 
5
- ## Installation
5
+ ## Quickstart
6
+
7
+ Install the SDK:
6
8
 
7
9
  ```bash
8
10
  npm install @riftresearch/sdk
9
- # or
10
- pnpm add @riftresearch/sdk
11
- # or
12
- yarn add @riftresearch/sdk
13
- # or
14
- bun add @riftresearch/sdk
15
11
  ```
16
12
 
17
- ## Quickstart
13
+ Then use it:
18
14
 
19
15
  ```ts
20
- import { RiftSdk, BTC, type Currency } from '@riftresearch/sdk'
16
+ import { RiftSdk, Currencies, createCurrency } from '@riftresearch/sdk'
21
17
  import { createPublicClient, createWalletClient, http } from 'viem'
22
18
  import { privateKeyToAccount } from 'viem/accounts'
23
- import { base } from 'viem/chains'
19
+ import { ethereum } from 'viem/chains'
24
20
 
25
- // Define USDC on Base
26
- const USDC_BASE: Currency = {
27
- chain: { kind: 'EVM', chainId: 8453 },
28
- token: {
29
- kind: 'TOKEN',
30
- address: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
31
- decimals: 6,
32
- },
33
- }
34
21
 
35
22
  // Setup viem clients
36
23
  const account = privateKeyToAccount('0x...')
37
- const publicClient = createPublicClient({ chain: base, transport: http() })
24
+ const publicClient = createPublicClient({ chain: ethereum, transport: http(process.env.ETH_RPC) })
38
25
  const walletClient = createWalletClient({
39
26
  account,
40
- chain: base,
41
- transport: http(),
27
+ chain: ethereum,
28
+ transport: http(process.env.ETH_RPC),
42
29
  })
43
30
 
44
31
  // Initialize SDK
@@ -46,15 +33,22 @@ const sdk = new RiftSdk({
46
33
  publicClient,
47
34
  walletClient,
48
35
  sendBitcoin: async ({ recipient, amountSats }) => {
49
- // Only needed for BTC -> ERC20 swaps
50
- throw new Error('Not implemented')
36
+ // Implement your Bitcoin wallet connection, e.g.:
37
+ // window.bitcoin.transfer({ recipient, amountSats })
51
38
  },
52
39
  })
53
40
 
41
+ // Optionally create a custom ERC-20
42
+ const ethereumPepe = createCurrency({
43
+ chainId: 1,
44
+ address: '0x6982508145454Ce325dDbE47a25d4ec3d2311933',
45
+ decimals: 18,
46
+ })
47
+
54
48
  // Get a quote
55
49
  const { quote, executeSwap } = await sdk.getQuote({
56
- from: USDC_BASE,
57
- to: BTC,
50
+ from: Currencies.Ethereum.USDC,
51
+ to: Currencies.Bitcoin.BTC,
58
52
  amount: '100000000', // 100 USDC (6 decimals)
59
53
  mode: 'exact_input',
60
54
  destinationAddress: 'bc1q...',
@@ -71,13 +65,3 @@ console.log(`Swap ID: ${swap.swapId}`) // API order ID
71
65
  const status = await sdk.getOrderStatus(swap.swapId)
72
66
  console.log(`Status: ${status.status}`)
73
67
  ```
74
-
75
- ## Build & Publish
76
-
77
- The SDK uses **bunup**.
78
-
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`
82
-
83
- The `dist/` directory is what gets published to npm.
package/dist/index.d.ts CHANGED
@@ -202,12 +202,31 @@ interface OrderResponse {
202
202
  /** Ordered list of steps the client must execute */
203
203
  executionSteps: ExecutionStep[];
204
204
  }
205
- /** Native BTC on Bitcoin */
206
- declare const BTC: Currency;
207
- /** cbBTC on Ethereum mainnet */
208
- declare const CBBTC_ETHEREUM: Currency;
209
- /** cbBTC on Base */
210
- declare const CBBTC_BASE: Currency;
205
+ /** Create an ERC-20 currency on an EVM chain */
206
+ declare function createCurrency(params: {
207
+ chainId: number;
208
+ address: string;
209
+ decimals: number;
210
+ }): Currency;
211
+ declare const Currencies: {
212
+ Bitcoin: {
213
+ BTC: Currency;
214
+ };
215
+ Ethereum: {
216
+ ETH: Currency;
217
+ WETH: Currency;
218
+ USDC: Currency;
219
+ USDT: Currency;
220
+ CBBTC: Currency;
221
+ WBTC: Currency;
222
+ };
223
+ Base: {
224
+ ETH: Currency;
225
+ WETH: Currency;
226
+ USDC: Currency;
227
+ CBBTC: Currency;
228
+ };
229
+ };
211
230
  import { treaty } from "@elysiajs/eden";
212
231
  import { Elysia } from "elysia";
213
232
  declare const app: Elysia;
@@ -430,8 +449,8 @@ declare class RiftSdk {
430
449
  *
431
450
  * @example
432
451
  * const { quote, executeSwap } = await sdk.getQuote({
433
- * from: CBBTC_BASE,
434
- * to: BTC,
452
+ * from: Currencies.Base.CBBTC,
453
+ * to: Currencies.Bitcoin.BTC,
435
454
  * amount: '100000000', // 1 cbBTC
436
455
  * mode: 'exact_input',
437
456
  * destinationAddress: 'bc1q...',
@@ -460,9 +479,11 @@ declare class RiftSdk {
460
479
  private assertSufficientBalance;
461
480
  private getAddress;
462
481
  private getRefundAddress;
482
+ private assertEvmChainMatch;
483
+ private assertEvmChainMatchForSteps;
463
484
  /**
464
485
  * Get the current status of an order by its ID.
465
486
  */
466
487
  getOrderStatus(orderId: string): Promise<OrderStatusResponse>;
467
488
  }
468
- export { getSupportedModes, detectRoute, createClient, TradeParameters, TokenIdentifier, SwapStatus2 as SwapStatus, SwapRoute, SupportedModes, SendBitcoinFn, RiftSdkOptions, RiftSdk, RiftOrder, RiftClient, QuoteResult, OrderResult, OrderResponse, NativeToken, GetQuoteResult, ExecutionStep, ExecutionAction, EvmChain, EvmCallStep, EvmCallKind, Erc20Token, Currency, Chain, CBBTC_ETHEREUM, CBBTC_BASE, BtcTransferStep, BtcTransferKind, BitcoinChain, BTC, App };
489
+ export { getSupportedModes, detectRoute, createCurrency, createClient, TradeParameters, TokenIdentifier, SwapStatus2 as SwapStatus, SwapRoute, SupportedModes, SendBitcoinFn, RiftSdkOptions, RiftSdk, RiftOrder, RiftClient, QuoteResult, OrderResult, OrderResponse, NativeToken, GetQuoteResult, ExecutionStep, ExecutionAction, EvmChain, EvmCallStep, EvmCallKind, Erc20Token, Currency, Currencies, Chain, BtcTransferStep, BtcTransferKind, BitcoinChain, App };
package/dist/index.js CHANGED
@@ -17,17 +17,73 @@ var CBBTC_TOKEN = {
17
17
  address: CBBTC_ADDRESS,
18
18
  decimals: 8
19
19
  };
20
- var BTC = {
21
- chain: BTC_CHAIN,
22
- token: { kind: "NATIVE", decimals: 8 }
23
- };
24
- var CBBTC_ETHEREUM = {
25
- chain: ETH_CHAIN,
26
- token: CBBTC_TOKEN
27
- };
28
- var CBBTC_BASE = {
29
- chain: BASE_CHAIN,
30
- token: CBBTC_TOKEN
20
+ function createCurrency(params) {
21
+ return {
22
+ chain: { kind: "EVM", chainId: params.chainId },
23
+ token: {
24
+ kind: "TOKEN",
25
+ address: params.address,
26
+ decimals: params.decimals
27
+ }
28
+ };
29
+ }
30
+ var Currencies = {
31
+ Bitcoin: {
32
+ BTC: {
33
+ chain: BTC_CHAIN,
34
+ token: { kind: "NATIVE", decimals: 8 }
35
+ }
36
+ },
37
+ Ethereum: {
38
+ ETH: {
39
+ chain: ETH_CHAIN,
40
+ token: { kind: "NATIVE", decimals: 18 }
41
+ },
42
+ WETH: createCurrency({
43
+ chainId: 1,
44
+ address: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
45
+ decimals: 18
46
+ }),
47
+ USDC: createCurrency({
48
+ chainId: 1,
49
+ address: "0xA0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
50
+ decimals: 6
51
+ }),
52
+ USDT: createCurrency({
53
+ chainId: 1,
54
+ address: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
55
+ decimals: 6
56
+ }),
57
+ CBBTC: {
58
+ chain: ETH_CHAIN,
59
+ token: CBBTC_TOKEN
60
+ },
61
+ WBTC: createCurrency({
62
+ chainId: 1,
63
+ address: "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
64
+ decimals: 8
65
+ })
66
+ },
67
+ Base: {
68
+ ETH: {
69
+ chain: BASE_CHAIN,
70
+ token: { kind: "NATIVE", decimals: 18 }
71
+ },
72
+ WETH: createCurrency({
73
+ chainId: 8453,
74
+ address: "0x4200000000000000000000000000000000000006",
75
+ decimals: 18
76
+ }),
77
+ USDC: createCurrency({
78
+ chainId: 8453,
79
+ address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
80
+ decimals: 6
81
+ }),
82
+ CBBTC: {
83
+ chain: BASE_CHAIN,
84
+ token: CBBTC_TOKEN
85
+ }
86
+ }
31
87
  };
32
88
  // src/apiClient.ts
33
89
  class SwapRouterApiError extends Error {
@@ -192,6 +248,7 @@ class RiftSdk {
192
248
  refundAddress,
193
249
  approvalMode: params.approvalMode
194
250
  });
251
+ this.assertEvmChainMatchForSteps(orderResponse.executionSteps);
195
252
  for (const step of orderResponse.executionSteps) {
196
253
  const result = await this.executeStep(step);
197
254
  if (isMonochain && step.action === "evm_call" && step.kind === "oneinch_swap" && result.txHash) {
@@ -285,6 +342,7 @@ class RiftSdk {
285
342
  async assertSufficientBalance(currency, amount) {
286
343
  if (currency.chain.kind !== "EVM")
287
344
  return;
345
+ this.assertEvmChainMatch(currency.chain.chainId);
288
346
  const required = BigInt(amount);
289
347
  const owner = this.getAddress();
290
348
  if (currency.token.kind === "NATIVE") {
@@ -317,6 +375,35 @@ class RiftSdk {
317
375
  }
318
376
  return this.getAddress();
319
377
  }
378
+ assertEvmChainMatch(expectedChainId) {
379
+ const walletChainId = this.walletClient.chain?.id;
380
+ if (!walletChainId) {
381
+ throw new Error("Wallet client is missing an EVM chain configuration");
382
+ }
383
+ if (walletChainId !== expectedChainId) {
384
+ throw new Error(`Wallet client chain mismatch. Expected ${expectedChainId}, got ${walletChainId}`);
385
+ }
386
+ const publicChainId = this.publicClient.chain?.id;
387
+ if (!publicChainId) {
388
+ throw new Error("Public client is missing an EVM chain configuration");
389
+ }
390
+ if (publicChainId !== expectedChainId) {
391
+ throw new Error(`Public client chain mismatch. Expected ${expectedChainId}, got ${publicChainId}`);
392
+ }
393
+ }
394
+ assertEvmChainMatchForSteps(steps) {
395
+ const evmSteps = steps.filter((step) => step.action === "evm_call");
396
+ const firstStep = evmSteps[0];
397
+ if (!firstStep)
398
+ return;
399
+ const expectedChainId = firstStep.chainId;
400
+ for (const step of evmSteps) {
401
+ if (step.chainId !== expectedChainId) {
402
+ throw new Error(`Mixed EVM chain IDs in execution steps. Expected ${expectedChainId}, got ${step.chainId}`);
403
+ }
404
+ }
405
+ this.assertEvmChainMatch(expectedChainId);
406
+ }
320
407
  async getOrderStatus(orderId) {
321
408
  return this.riftClient.getOrder(orderId);
322
409
  }
@@ -324,9 +411,8 @@ class RiftSdk {
324
411
  export {
325
412
  getSupportedModes,
326
413
  detectRoute,
414
+ createCurrency,
327
415
  createClient,
328
416
  RiftSdk,
329
- CBBTC_ETHEREUM,
330
- CBBTC_BASE,
331
- BTC
417
+ Currencies
332
418
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riftresearch/sdk",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "description": "SDK for swapping between bitcoin and evm chains",
5
5
  "license": "MIT",
6
6
  "files": [