@lendasat/lendaswap-sdk 0.1.65 → 0.1.68

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.
Files changed (34) hide show
  1. package/README.md +200 -126
  2. package/dist/api.d.ts +139 -103
  3. package/dist/api.d.ts.map +1 -1
  4. package/dist/api.js +236 -137
  5. package/dist/api.js.map +1 -1
  6. package/dist/index.d.ts +20 -20
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +20 -27
  9. package/dist/index.js.map +1 -1
  10. package/package.json +14 -15
  11. package/wasm/lendaswap_wasm_sdk.d.ts +133 -123
  12. package/wasm/lendaswap_wasm_sdk_bg.js +548 -248
  13. package/wasm/lendaswap_wasm_sdk_bg.wasm +0 -0
  14. package/wasm/lendaswap_wasm_sdk_bg.wasm.d.ts +45 -35
  15. package/dist/price-calculations.test.d.ts +0 -2
  16. package/dist/price-calculations.test.d.ts.map +0 -1
  17. package/dist/price-calculations.test.js +0 -173
  18. package/dist/price-calculations.test.js.map +0 -1
  19. package/dist/storage/dexieSwapStorage.d.ts +0 -113
  20. package/dist/storage/dexieSwapStorage.d.ts.map +0 -1
  21. package/dist/storage/dexieSwapStorage.js +0 -200
  22. package/dist/storage/dexieSwapStorage.js.map +0 -1
  23. package/dist/storage/dexieVtxoSwapStorage.d.ts +0 -105
  24. package/dist/storage/dexieVtxoSwapStorage.d.ts.map +0 -1
  25. package/dist/storage/dexieVtxoSwapStorage.js +0 -149
  26. package/dist/storage/dexieVtxoSwapStorage.js.map +0 -1
  27. package/dist/storage/dexieWalletStorage.d.ts +0 -99
  28. package/dist/storage/dexieWalletStorage.d.ts.map +0 -1
  29. package/dist/storage/dexieWalletStorage.js +0 -139
  30. package/dist/storage/dexieWalletStorage.js.map +0 -1
  31. package/dist/storage/index.d.ts +0 -19
  32. package/dist/storage/index.d.ts.map +0 -1
  33. package/dist/storage/index.js +0 -22
  34. package/dist/storage/index.js.map +0 -1
package/README.md CHANGED
@@ -5,7 +5,10 @@ TypeScript/JavaScript SDK for Lendaswap - Bitcoin-to-stablecoin atomic swaps.
5
5
  ## Overview
6
6
 
7
7
  This SDK provides a high-level interface for interacting with the Lendaswap API, enabling atomic swaps between Bitcoin (
8
- Lightning/Arkade) and EVM stablecoins (USDC, USDT on Polygon/Ethereum).
8
+ Lightning/Arkade/On-chain) and EVM stablecoins (USDC, USDT on Polygon/Ethereum). Built with WebAssembly for browser
9
+ environments and IndexedDB for persistent storage.
10
+
11
+ **For Node.js server-side applications**, use `@lendasat/lendaswap-sdk-native` instead.
9
12
 
10
13
  ## Installation
11
14
 
@@ -29,7 +32,7 @@ pnpm add -D vite-plugin-wasm vite-plugin-top-level-await
29
32
  // vite.config.ts
30
33
  import wasm from 'vite-plugin-wasm';
31
34
  import topLevelAwait from 'vite-plugin-top-level-await';
32
- import { defineConfig } from 'vite';
35
+ import {defineConfig} from 'vite';
33
36
 
34
37
  export default defineConfig({
35
38
  plugins: [wasm(), topLevelAwait()],
@@ -45,17 +48,17 @@ pnpm add -D wasm-loader
45
48
  ```javascript
46
49
  // webpack.config.js
47
50
  module.exports = {
48
- experiments: {
49
- asyncWebAssembly: true,
50
- },
51
- module: {
52
- rules: [
53
- {
54
- test: /\.wasm$/,
55
- type: 'webassembly/async',
56
- },
57
- ],
58
- },
51
+ experiments: {
52
+ asyncWebAssembly: true,
53
+ },
54
+ module: {
55
+ rules: [
56
+ {
57
+ test: /\.wasm$/,
58
+ type: 'webassembly/async',
59
+ },
60
+ ],
61
+ },
59
62
  };
60
63
  ```
61
64
 
@@ -64,42 +67,50 @@ module.exports = {
64
67
  ```javascript
65
68
  // next.config.js
66
69
  module.exports = {
67
- webpack: (config) => {
68
- config.experiments = {
69
- ...config.experiments,
70
- asyncWebAssembly: true,
71
- };
72
- return config;
73
- },
70
+ webpack: (config) => {
71
+ config.experiments = {
72
+ ...config.experiments,
73
+ asyncWebAssembly: true,
74
+ };
75
+ return config;
76
+ },
74
77
  };
75
78
  ```
76
79
 
77
80
  ## Quick Start
78
81
 
79
- ### Get Asset Pairs and Quote
82
+ ### Create a Client
80
83
 
81
84
  ```typescript
82
- import {
83
- Client,
84
- createDexieWalletStorage,
85
- createDexieSwapStorage,
86
- } from '@lendasat/lendaswap-sdk';
85
+ import { Client } from '@lendasat/lendaswap-sdk';
87
86
 
88
- // Create storage providers (uses IndexedDB via Dexie)
89
- const walletStorage = createDexieWalletStorage();
90
- const swapStorage = createDexieSwapStorage();
91
-
92
- // Create client
93
- const client = await Client.create(
94
- 'https://apilendaswap.lendasat.com',
95
- walletStorage,
96
- swapStorage,
97
- 'bitcoin',
98
- 'https://arkade.computer'
99
- );
87
+ // Create client using the builder pattern
88
+ const client = await Client.builder()
89
+ .url('https://apilendaswap.lendasat.com')
90
+ .withIdbStorage() // Uses IndexedDB for browser storage
91
+ .network('bitcoin')
92
+ .arkadeUrl('https://arkade.computer')
93
+ .esploraUrl('https://mempool.space/api')
94
+ .build();
100
95
 
101
96
  // Initialize wallet (generates or loads mnemonic)
102
97
  await client.init();
98
+ ```
99
+
100
+ ### Get Asset Pairs and Quote
101
+
102
+ ```typescript
103
+ import { Client } from '@lendasat/lendaswap-sdk';
104
+
105
+ const client = await Client.builder()
106
+ .url('https://apilendaswap.lendasat.com')
107
+ .withIdbStorage()
108
+ .network('bitcoin')
109
+ .arkadeUrl('https://arkade.computer')
110
+ .esploraUrl('https://mempool.space/api')
111
+ .build();
112
+
113
+ await client.init();
103
114
 
104
115
  // Get available trading pairs
105
116
  const pairs = await client.getAssetPairs();
@@ -112,28 +123,57 @@ console.log('You receive:', quote.min_amount, 'USDC');
112
123
  console.log('Protocol fee:', quote.protocol_fee);
113
124
  ```
114
125
 
126
+ ### Lightning to USDC (Polygon) Swap
127
+
128
+ This example shows how to swap BTC via Lightning to USDC on Polygon.
129
+
130
+ ```typescript
131
+ import { Client } from '@lendasat/lendaswap-sdk';
132
+
133
+ const client = await Client.builder()
134
+ .url('https://apilendaswap.lendasat.com')
135
+ .withIdbStorage()
136
+ .network('bitcoin')
137
+ .arkadeUrl('https://arkade.computer')
138
+ .esploraUrl('https://mempool.space/api')
139
+ .build();
140
+
141
+ await client.init();
142
+
143
+ // Create Lightning → USDC (Polygon) swap
144
+ const swap = await client.createLightningToEvmSwap(
145
+ {
146
+ target_address: '0xYourPolygonAddress',
147
+ source_amount: 100000n, // 100,000 sats
148
+ target_token: 'usdc_pol',
149
+ },
150
+ 'polygon'
151
+ );
152
+
153
+ console.log('Swap created:', swap.id);
154
+ console.log('Pay this Lightning invoice:', swap.lnInvoice);
155
+ console.log('You will receive:', swap.assetAmount, 'USDC');
156
+
157
+ // After paying the invoice, claim via Gelato (gasless)
158
+ await client.claimGelato(swap.id);
159
+ console.log('Swap claimed via Gelato relay!');
160
+ ```
161
+
115
162
  ### Arkade to Polygon Swap (with Gelato Auto-Redeem)
116
163
 
117
164
  This example shows how to swap BTC from Arkade to USDC on Polygon. The swap uses Gelato relay for gasless claiming on
118
165
  the EVM side.
119
166
 
120
167
  ```typescript
121
- import {
122
- Client,
123
- createDexieWalletStorage,
124
- createDexieSwapStorage,
125
- } from '@lendasat/lendaswap-sdk';
126
-
127
- const walletStorage = createDexieWalletStorage();
128
- const swapStorage = createDexieSwapStorage();
168
+ import { Client } from '@lendasat/lendaswap-sdk';
129
169
 
130
- const client = await Client.create(
131
- 'https://apilendaswap.lendasat.com',
132
- walletStorage,
133
- swapStorage,
134
- 'bitcoin',
135
- 'https://arkade.computer'
136
- );
170
+ const client = await Client.builder()
171
+ .url('https://apilendaswap.lendasat.com')
172
+ .withIdbStorage()
173
+ .network('bitcoin')
174
+ .arkadeUrl('https://arkade.computer')
175
+ .esploraUrl('https://mempool.space/api')
176
+ .build();
137
177
 
138
178
  await client.init();
139
179
 
@@ -147,12 +187,12 @@ const swap = await client.createArkadeToEvmSwap(
147
187
  'polygon'
148
188
  );
149
189
 
150
- console.log('Swap created:', swap.swap_id);
190
+ console.log('Swap created:', swap.id);
151
191
  console.log('Send BTC to Arkade VHTLC to proceed');
152
192
 
153
193
  // After sending BTC, claim via Gelato (gasless)
154
194
  // The secret is automatically derived from your wallet
155
- await client.claimGelato(swap.swap_id);
195
+ await client.claimGelato(swap.id);
156
196
  console.log('Swap claimed via Gelato relay!');
157
197
  ```
158
198
 
@@ -165,22 +205,15 @@ We recommend using [wagmi](https://wagmi.sh/) with [viem](https://viem.sh/) for
165
205
  or [ethers.js](https://docs.ethers.org/) for vanilla JS/TS.
166
206
 
167
207
  ```typescript
168
- import {
169
- Client,
170
- createDexieWalletStorage,
171
- createDexieSwapStorage,
172
- } from '@lendasat/lendaswap-sdk';
208
+ import { Client } from '@lendasat/lendaswap-sdk';
173
209
 
174
- const walletStorage = createDexieWalletStorage();
175
- const swapStorage = createDexieSwapStorage();
176
-
177
- const client = await Client.create(
178
- 'https://apilendaswap.lendasat.com',
179
- walletStorage,
180
- swapStorage,
181
- 'bitcoin',
182
- 'https://arkade.computer'
183
- );
210
+ const client = await Client.builder()
211
+ .url('https://apilendaswap.lendasat.com')
212
+ .withIdbStorage()
213
+ .network('bitcoin')
214
+ .arkadeUrl('https://arkade.computer')
215
+ .esploraUrl('https://mempool.space/api')
216
+ .build();
184
217
 
185
218
  await client.init();
186
219
 
@@ -194,9 +227,9 @@ const swap = await client.createEvmToLightningSwap(
194
227
  'ethereum'
195
228
  );
196
229
 
197
- console.log('Swap created:', swap.swap_id);
198
- console.log('Contract address:', swap.contract_address);
199
- console.log('Amount to send:', swap.source_amount);
230
+ console.log('Swap created:', swap.id);
231
+ console.log('Contract address:', swap.contractAddress);
232
+ console.log('Amount to send:', swap.sourceAmount);
200
233
 
201
234
  // Now use your wallet to send the transaction to the HTLC contract
202
235
  // Example with wagmi/viem:
@@ -205,18 +238,18 @@ console.log('Amount to send:', swap.source_amount);
205
238
  // const { writeContract } = useWriteContract();
206
239
  //
207
240
  // await writeContract({
208
- // address: swap.contract_address,
241
+ // address: swap.contractAddress,
209
242
  // abi: htlcAbi,
210
243
  // functionName: 'deposit',
211
- // args: [swap.hash_lock, swap.timelock, ...],
212
- // value: swap.source_amount,
244
+ // args: [swap.hashLock, swap.timelock, ...],
245
+ // value: swap.sourceAmount,
213
246
  // });
214
247
  //
215
248
  // Example with ethers.js:
216
249
  //
217
250
  // const signer = await provider.getSigner();
218
- // const contract = new ethers.Contract(swap.contract_address, htlcAbi, signer);
219
- // await contract.deposit(swap.hash_lock, swap.timelock, ...);
251
+ // const contract = new ethers.Contract(swap.contractAddress, htlcAbi, signer);
252
+ // await contract.deposit(swap.hashLock, swap.timelock, ...);
220
253
  ```
221
254
 
222
255
  ### Real-time Price Feed (WebSocket)
@@ -224,7 +257,7 @@ console.log('Amount to send:', swap.source_amount);
224
257
  Subscribe to real-time price updates via WebSocket:
225
258
 
226
259
  ```typescript
227
- import {PriceFeedService} from '@lendasat/lendaswap-sdk';
260
+ import { PriceFeedService } from '@lendasat/lendaswap-sdk';
228
261
 
229
262
  const priceFeed = new PriceFeedService('https://apilendaswap.lendasat.com');
230
263
 
@@ -254,7 +287,9 @@ unsubscribe();
254
287
  - **Client** - Full-featured client for the Lendaswap API with WASM-powered cryptography
255
288
  - **Wallet Management** - HD wallet derivation for swap parameters
256
289
  - **Price Feed** - Real-time WebSocket price updates with auto-reconnection
257
- - **Storage Providers** - Dexie (IndexedDB) storage for wallet and swap data
290
+ - **Price Calculations** - Helper functions for computing exchange rates and amounts
291
+ - **USD Prices** - Fetch current USD prices from CoinGecko
292
+ - **IndexedDB Storage** - Automatic storage via native Rust IndexedDB implementation
258
293
  - **Configurable Logging** - Set log level via code or localStorage
259
294
 
260
295
  ## API Reference
@@ -262,33 +297,53 @@ unsubscribe();
262
297
  ### Client
263
298
 
264
299
  ```typescript
265
- const client = await Client.create(
266
- baseUrl,
267
- walletStorage,
268
- swapStorage,
269
- network,
270
- arkadeUrl
271
- );
300
+ // Create client using builder pattern
301
+ const client = await Client.builder()
302
+ .url('https://apilendaswap.lendasat.com')
303
+ .withIdbStorage()
304
+ .network('bitcoin')
305
+ .arkadeUrl('https://arkade.computer')
306
+ .esploraUrl('https://mempool.space/api')
307
+ .build();
272
308
 
273
309
  // Initialize wallet
274
310
  await client.init();
275
311
  await client.init('your mnemonic phrase'); // Or with existing mnemonic
276
312
 
277
- // Trading pairs and quotes
313
+ // Trading pairs, tokens, and quotes
278
314
  await client.getAssetPairs();
315
+ await client.getTokens();
279
316
  await client.getQuote(from, to, amount);
317
+ await client.getVersion();
280
318
 
281
319
  // Swap operations
282
320
  await client.createArkadeToEvmSwap(request, targetNetwork);
321
+ await client.createLightningToEvmSwap(request, targetNetwork);
283
322
  await client.createEvmToArkadeSwap(request, sourceNetwork);
284
323
  await client.createEvmToLightningSwap(request, sourceNetwork);
285
324
  await client.getSwap(id);
286
325
  await client.listAllSwaps();
326
+ await client.deleteSwap(id);
327
+ await client.clearSwapStorage();
287
328
 
288
329
  // Claiming and refunding
289
330
  await client.claimGelato(swapId); // Gasless EVM claim via Gelato
290
331
  await client.claimVhtlc(swapId); // Claim Arkade VHTLC
291
332
  await client.refundVhtlc(swapId, addr); // Refund expired VHTLC
333
+ await client.amountsForSwap(swapId); // Get VHTLC amounts
334
+
335
+ // On-chain Bitcoin to Arkade swaps
336
+ await client.createBitcoinToArkadeSwap(request);
337
+ await client.claimBtcToArkadeVhtlc(swapId);
338
+ await client.refundOnchainHtlc(swapId, refundAddress);
339
+
340
+ // VTXO swaps (refresh VTXOs)
341
+ await client.estimateVtxoSwap(vtxos);
342
+ await client.createVtxoSwap(vtxos);
343
+ await client.getVtxoSwap(id);
344
+ await client.claimVtxoSwap(swap, swapParams, claimAddress);
345
+ await client.refundVtxoSwap(swapId, refundAddress);
346
+ await client.listAllVtxoSwaps();
292
347
 
293
348
  // Recovery
294
349
  await client.recoverSwaps();
@@ -298,47 +353,10 @@ await client.getMnemonic();
298
353
  await client.getUserIdXpub();
299
354
  ```
300
355
 
301
- ### Storage Providers
302
-
303
- ```typescript
304
- import {
305
- createDexieWalletStorage,
306
- createDexieSwapStorage,
307
- } from '@lendasat/lendaswap-sdk';
308
-
309
- // Pre-built Dexie (IndexedDB) storage providers
310
- const walletStorage = createDexieWalletStorage();
311
- const swapStorage = createDexieSwapStorage();
312
- ```
313
-
314
- Or implement custom storage:
315
-
316
- ```typescript
317
- import type {
318
- WalletStorageProvider,
319
- SwapStorageProvider,
320
- } from '@lendasat/lendaswap-sdk';
321
-
322
- const walletStorage: WalletStorageProvider = {
323
- getMnemonic: async () => localStorage.getItem('mnemonic'),
324
- setMnemonic: async (m) => localStorage.setItem('mnemonic', m),
325
- getKeyIndex: async () => parseInt(localStorage.getItem('idx') ?? '0'),
326
- setKeyIndex: async (i) => localStorage.setItem('idx', i.toString()),
327
- };
328
-
329
- const swapStorage: SwapStorageProvider = {
330
- get: async (id) => /* fetch from your storage */,
331
- store: async (id, data) => /* store to your storage */,
332
- delete: async (id) => /* delete from your storage */,
333
- list: async () => /* return all swap IDs */,
334
- getAll: async () => /* return all swap data */,
335
- };
336
- ```
337
-
338
356
  ### PriceFeedService
339
357
 
340
358
  ```typescript
341
- import {PriceFeedService} from '@lendasat/lendaswap-sdk';
359
+ import { PriceFeedService } from '@lendasat/lendaswap-sdk';
342
360
 
343
361
  const priceFeed = new PriceFeedService('https://apilendaswap.lendasat.com');
344
362
 
@@ -371,16 +389,72 @@ localStorage.setItem('lendaswap_log_level', 'debug');
371
389
  // Reload page for changes to take effect
372
390
  ```
373
391
 
392
+ ### Price Calculations
393
+
394
+ ```typescript
395
+ import {
396
+ calculateSourceAmount,
397
+ calculateTargetAmount,
398
+ computeExchangeRate,
399
+ selectTierRate,
400
+ } from '@lendasat/lendaswap-sdk';
401
+
402
+ // Select the appropriate tier rate based on amount
403
+ const rate = selectTierRate(priceTiers, amount);
404
+
405
+ // Calculate target amount from source
406
+ const targetAmount = calculateTargetAmount(sourceAmount, exchangeRate);
407
+
408
+ // Calculate source amount from target
409
+ const sourceAmount = calculateSourceAmount(targetAmount, exchangeRate);
410
+
411
+ // Compute exchange rate from price tiers
412
+ const exchangeRate = computeExchangeRate(priceTiers, amount);
413
+ ```
414
+
415
+ ### USD Prices
416
+
417
+ ```typescript
418
+ import {
419
+ getUsdPrice,
420
+ getUsdPrices,
421
+ getSupportedTokensForUsdPrice,
422
+ } from '@lendasat/lendaswap-sdk';
423
+
424
+ // Get USD price for a single token
425
+ const btcPrice = await getUsdPrice('btc_arkade');
426
+ console.log('BTC price:', btcPrice.usd);
427
+
428
+ // Get USD prices for multiple tokens
429
+ const prices = await getUsdPrices(['btc_arkade', 'usdc_pol']);
430
+
431
+ // Get list of supported tokens for USD price lookup
432
+ const supported = getSupportedTokensForUsdPrice();
433
+ ```
434
+
374
435
  ## Supported Tokens
375
436
 
376
437
  | Token | Chain | ID |
377
438
  | ----- | --------- | --------------- |
378
439
  | BTC | Lightning | `btc_lightning` |
379
440
  | BTC | Arkade | `btc_arkade` |
441
+ | BTC | On-chain | `btc_onchain` |
380
442
  | USDC | Polygon | `usdc_pol` |
381
443
  | USDT | Polygon | `usdt0_pol` |
382
444
  | USDC | Ethereum | `usdc_eth` |
383
445
  | USDT | Ethereum | `usdt_eth` |
446
+ | XAUT | Ethereum | `xaut_eth` |
447
+
448
+ ## Comparison with Native SDK
449
+
450
+ | Feature | This SDK (Browser) | Native SDK (Node.js) |
451
+ | -------- | ------------------------- | -------------------------------- |
452
+ | Storage | IndexedDB | SQLite |
453
+ | Platform | Browser | Node.js |
454
+ | Use case | Web apps, frontends | Servers, CLI, backends |
455
+ | Package | `@lendasat/lendaswap-sdk` | `@lendasat/lendaswap-sdk-native` |
456
+
457
+ For server-side applications, CLI tools, or backends requiring SQLite storage, use `@lendasat/lendaswap-sdk-native`.
384
458
 
385
459
  ## License
386
460