@lendasat/lendaswap-sdk 0.1.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.
Files changed (46) hide show
  1. package/README.md +105 -0
  2. package/dist/api.d.ts +413 -0
  3. package/dist/api.d.ts.map +1 -0
  4. package/dist/api.js +306 -0
  5. package/dist/api.js.map +1 -0
  6. package/dist/index.d.ts +42 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +45 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/price-feed.d.ts +121 -0
  11. package/dist/price-feed.d.ts.map +1 -0
  12. package/dist/price-feed.js +178 -0
  13. package/dist/price-feed.js.map +1 -0
  14. package/dist/storage/dexieSwapStorage.d.ts +111 -0
  15. package/dist/storage/dexieSwapStorage.d.ts.map +1 -0
  16. package/dist/storage/dexieSwapStorage.js +139 -0
  17. package/dist/storage/dexieSwapStorage.js.map +1 -0
  18. package/dist/storage/dexieWalletStorage.d.ts +99 -0
  19. package/dist/storage/dexieWalletStorage.d.ts.map +1 -0
  20. package/dist/storage/dexieWalletStorage.js +139 -0
  21. package/dist/storage/dexieWalletStorage.js.map +1 -0
  22. package/dist/storage/index.d.ts +18 -0
  23. package/dist/storage/index.d.ts.map +1 -0
  24. package/dist/storage/index.js +20 -0
  25. package/dist/storage/index.js.map +1 -0
  26. package/dist/storage/indexedDB.d.ts +30 -0
  27. package/dist/storage/indexedDB.d.ts.map +1 -0
  28. package/dist/storage/indexedDB.js +108 -0
  29. package/dist/storage/indexedDB.js.map +1 -0
  30. package/dist/storage/localStorage.d.ts +32 -0
  31. package/dist/storage/localStorage.d.ts.map +1 -0
  32. package/dist/storage/localStorage.js +58 -0
  33. package/dist/storage/localStorage.js.map +1 -0
  34. package/dist/storage/memory.d.ts +35 -0
  35. package/dist/storage/memory.d.ts.map +1 -0
  36. package/dist/storage/memory.js +50 -0
  37. package/dist/storage/memory.js.map +1 -0
  38. package/dist/types.d.ts +59 -0
  39. package/dist/types.d.ts.map +1 -0
  40. package/dist/types.js +5 -0
  41. package/dist/types.js.map +1 -0
  42. package/dist/wallet.d.ts +108 -0
  43. package/dist/wallet.d.ts.map +1 -0
  44. package/dist/wallet.js +188 -0
  45. package/dist/wallet.js.map +1 -0
  46. package/package.json +58 -0
package/dist/api.js ADDED
@@ -0,0 +1,306 @@
1
+ /**
2
+ * API client wrapper for the Lendaswap REST API.
3
+ *
4
+ * This module provides a high-level TypeScript API that wraps the WASM-based
5
+ * API client for easier use in TypeScript/JavaScript applications.
6
+ */
7
+ // Import WASM types for internal use
8
+ import init, { JsSwapStorageProvider, JsWalletStorageProvider, Client as WasmClient, } from "../wasm/lendaswap_wasm_sdk.js";
9
+ // Cached initialization promise
10
+ let initPromise = null;
11
+ /**
12
+ * Initialize the WASM module.
13
+ *
14
+ * This is called automatically when creating a Wallet, but can be called
15
+ * explicitly for eager initialization.
16
+ *
17
+ * @param wasmPath - Optional path to the WASM file (for Node.js environments)
18
+ */
19
+ export async function initWasm(wasmPath) {
20
+ if (initPromise) {
21
+ return initPromise;
22
+ }
23
+ initPromise = (async () => {
24
+ // Check if we're in Node.js
25
+ const isNode = typeof process !== "undefined" &&
26
+ process.versions != null &&
27
+ process.versions.node != null;
28
+ if (isNode && !wasmPath) {
29
+ // In Node.js, try to load the WASM file directly
30
+ const { readFile } = await import("fs/promises");
31
+ const { fileURLToPath } = await import("url");
32
+ const { dirname, join } = await import("path");
33
+ // Get the directory of the current module
34
+ const __filename = fileURLToPath(import.meta.url);
35
+ const __dirname = dirname(__filename);
36
+ // The WASM file is in the wasm directory relative to src
37
+ const wasmFilePath = join(__dirname, "..", "wasm", "lendaswap_sdk_bg.wasm");
38
+ const wasmBuffer = await readFile(wasmFilePath);
39
+ await init(wasmBuffer);
40
+ }
41
+ else if (wasmPath) {
42
+ // Custom path provided
43
+ const { readFile } = await import("fs/promises");
44
+ const wasmBuffer = await readFile(wasmPath);
45
+ await init(wasmBuffer);
46
+ }
47
+ else {
48
+ // Browser environment - let init handle fetching
49
+ await init();
50
+ }
51
+ })();
52
+ return initPromise;
53
+ }
54
+ // Re-export WASM types directly
55
+ export { QuoteResponse, TokenId, Version, VhtlcAmounts, } from "../wasm/lendaswap_wasm_sdk.js";
56
+ /**
57
+ * High-level API client for the Lendaswap REST API.
58
+ *
59
+ * This provides a TypeScript-friendly interface around the WASM API client.
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * import { ApiClient } from '@lendaswap/sdk';
64
+ *
65
+ * const api = await ApiClient.create('https://api.lendaswap.com');
66
+ *
67
+ * // Get supported tokens
68
+ * const tokens = await api.getTokens();
69
+ *
70
+ * // Get a quote
71
+ * const quote = await api.getQuote('btc_arkade', 'usdc_pol', 100000n);
72
+ * console.log('Exchange rate:', quote.exchangeRate);
73
+ * ```
74
+ */
75
+ /**
76
+ * Convert a value from WASM (which may be a Map) to a plain object.
77
+ * serde_wasm_bindgen serializes structs as Maps by default.
78
+ */
79
+ function fromWasm(value) {
80
+ if (value instanceof Map) {
81
+ return Object.fromEntries(value);
82
+ }
83
+ return value;
84
+ }
85
+ export class Client {
86
+ client;
87
+ constructor(client) {
88
+ this.client = client;
89
+ }
90
+ /**
91
+ * Create a new Client instance.
92
+ *
93
+ * @param baseUrl - The base URL of the Lendaswap API
94
+ * @param walletStorage - Storage provider for persisting wallet data (mnemonic, key index)
95
+ * @param swapStorage - Storage provider for persisting swap data (uses Dexie/IndexedDB)
96
+ * @param network - Bitcoin network ("bitcoin", "testnet", "regtest", "mutinynet")
97
+ * @param arkadeUrl - Arkade's server url
98
+ * @param wasmPath - Optional path to the WASM file (for Node.js environments)
99
+ * @returns A new Client instance
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * import Dexie from 'dexie';
104
+ *
105
+ * // Wallet storage using localStorage with typed methods
106
+ * const walletStorage: WalletStorageProvider = {
107
+ * getMnemonic: async () => localStorage.getItem('mnemonic'),
108
+ * setMnemonic: async (mnemonic) => localStorage.setItem('mnemonic', mnemonic),
109
+ * getKeyIndex: async () => parseInt(localStorage.getItem('key_index') ?? '0'),
110
+ * setKeyIndex: async (index) => localStorage.setItem('key_index', index.toString()),
111
+ * };
112
+ *
113
+ * // Swap storage using Dexie (IndexedDB)
114
+ * const db = new Dexie('lendaswap');
115
+ * db.version(1).stores({ swaps: 'id' });
116
+ *
117
+ * const swapStorage: SwapStorageProvider = {
118
+ * get: async (swapId) => await db.table('swaps').get(swapId) ?? null,
119
+ * store: async (swapId, data) => await db.table('swaps').put({ id: swapId, ...data }),
120
+ * delete: async (swapId) => await db.table('swaps').delete(swapId),
121
+ * list: async () => await db.table('swaps').toCollection().primaryKeys() as string[],
122
+ * getAll: async () => await db.table('swaps').toArray(),
123
+ * };
124
+ *
125
+ * const client = await Client.create(
126
+ * 'https://api.lendaswap.com',
127
+ * walletStorage,
128
+ * swapStorage,
129
+ * 'bitcoin',
130
+ * 'https://arkade.computer'
131
+ * );
132
+ * ```
133
+ */
134
+ static async create(baseUrl, walletStorage, swapStorage, network, arkadeUrl, wasmPath) {
135
+ await initWasm(wasmPath);
136
+ // Bind wallet storage methods to preserve 'this' context when called from WASM
137
+ const jsWalletStorageProvider = new JsWalletStorageProvider(walletStorage.getMnemonic.bind(walletStorage), walletStorage.setMnemonic.bind(walletStorage), walletStorage.getKeyIndex.bind(walletStorage), walletStorage.setKeyIndex.bind(walletStorage));
138
+ // Bind swap storage methods to preserve 'this' context when called from WASM
139
+ const jsSwapStorageProvider = new JsSwapStorageProvider(swapStorage.get.bind(swapStorage), swapStorage.store.bind(swapStorage), swapStorage.delete.bind(swapStorage), swapStorage.list.bind(swapStorage), swapStorage.getAll.bind(swapStorage));
140
+ const wasmClient = new WasmClient(baseUrl, jsWalletStorageProvider, jsSwapStorageProvider, network, arkadeUrl);
141
+ return new Client(wasmClient);
142
+ }
143
+ async init(mnemonic) {
144
+ await this.client.init(mnemonic);
145
+ }
146
+ /**
147
+ * Create an Arkade to EVM swap (BTC → Token).
148
+ *
149
+ * @param request - The swap request parameters
150
+ * @param targetNetwork - Target EVM network (e.g., 'polygon', 'ethereum')
151
+ * @returns The created swap response
152
+ */
153
+ async createArkadeToEvmSwap(request, targetNetwork) {
154
+ const response = await this.client.createArkadeToEvmSwap(request.target_address, request.target_amount, request.target_token, targetNetwork, request.referral_code);
155
+ // serde_wasm_bindgen returns a Map for complex structs, convert to plain object
156
+ const obj = fromWasm(response);
157
+ return { ...obj, direction: "btc_to_evm" };
158
+ }
159
+ /**
160
+ * Create an EVM to Arkade swap (Token → BTC).
161
+ *
162
+ * @param request - The swap request parameters
163
+ * @param sourceNetwork - Source EVM network (e.g., 'polygon', 'ethereum')
164
+ * @returns The created swap response
165
+ */
166
+ async createEvmToArkadeSwap(request, sourceNetwork) {
167
+ const response = await this.client.createEvmToArkadeSwap(request.target_address, request.user_address, request.source_amount, request.source_token, sourceNetwork, request.referral_code);
168
+ // serde_wasm_bindgen returns a Map for complex structs, convert to plain object
169
+ const obj = fromWasm(response);
170
+ return { ...obj, direction: "evm_to_btc" };
171
+ }
172
+ /**
173
+ * Create an EVM to Lightning swap (Token → BTC).
174
+ *
175
+ * @param request - The swap request parameters
176
+ * @param sourceNetwork - Source EVM network (e.g., 'polygon', 'ethereum')
177
+ * @returns The created swap response
178
+ */
179
+ async createEvmToLightningSwap(request, sourceNetwork) {
180
+ const response = await this.client.createEvmToLightningSwap(request.bolt11_invoice, request.user_address, request.source_token, sourceNetwork, request.referral_code);
181
+ // serde_wasm_bindgen returns a Map for complex structs, convert to plain object
182
+ const obj = fromWasm(response);
183
+ return { ...obj, direction: "evm_to_btc" };
184
+ }
185
+ async getAssetPairs() {
186
+ return (await this.client.getAssetPairs());
187
+ }
188
+ /**
189
+ * Get a quote for a swap.
190
+ *
191
+ * @param from - Source token ID (e.g., 'btc_arkade')
192
+ * @param to - Destination token ID (e.g., 'usdc_pol')
193
+ * @param baseAmount - Amount in base units (satoshis for BTC, wei for EVM)
194
+ * @returns Quote response with exchange rate and fees
195
+ */
196
+ async getQuote(from, to, baseAmount) {
197
+ const quote = await this.client.getQuote(from, to, baseAmount);
198
+ return {
199
+ exchange_rate: quote.exchangeRate,
200
+ network_fee: Number(quote.networkFee),
201
+ protocol_fee: Number(quote.protocolFee),
202
+ protocol_fee_rate: quote.protocolFeeRate,
203
+ min_amount: Number(quote.minAmount),
204
+ max_amount: Number(quote.maxAmount),
205
+ };
206
+ }
207
+ /**
208
+ * Get a swap by its ID.
209
+ *
210
+ * @param id - The swap ID
211
+ * @returns The swap response
212
+ */
213
+ async getSwap(id) {
214
+ return (await this.client.getSwap(id));
215
+ }
216
+ /**
217
+ * Gets all stored swaps.
218
+ *
219
+ * @returns A vec of swaps
220
+ */
221
+ async listAllSwaps() {
222
+ return (await this.client.listAll());
223
+ }
224
+ /**
225
+ * Claim a swap via Gelato relay (gasless).
226
+ *
227
+ * @param swapId - The swap ID
228
+ * @param secret - The preimage secret (hex-encoded)
229
+ */
230
+ async claimGelato(swapId, secret) {
231
+ await this.client.claimGelato(swapId, secret);
232
+ }
233
+ /**
234
+ * Get the VHTLC amounts associated with a swap.
235
+ *
236
+ * @param swapId - The swap ID
237
+ * @returns VhtlcAmounts
238
+ */
239
+ async amountsForSwap(swapId) {
240
+ return (await this.client.amountsForSwap(swapId));
241
+ }
242
+ /**
243
+ * Claim a swap VHTLC
244
+ *
245
+ * @param swapId - The swap ID
246
+ */
247
+ async claimVhtlc(swapId) {
248
+ await this.client.claimVhtlc(swapId);
249
+ }
250
+ /**
251
+ * Claim a swap VHTLC
252
+ *
253
+ * @param swapId - The swap ID
254
+ * @returns The TXID of the Ark transaction which refunded the VHTLC.
255
+ */
256
+ async refundVhtlc(swapId, refundAddress) {
257
+ return await this.client.refundVhtlc(swapId, refundAddress);
258
+ }
259
+ /**
260
+ * Get the API version.
261
+ *
262
+ * @returns Version information
263
+ */
264
+ async getVersion() {
265
+ const version = await this.client.getVersion();
266
+ return {
267
+ tag: version.tag,
268
+ commit_hash: version.commitHash,
269
+ };
270
+ }
271
+ /**
272
+ * Recover swaps for the currently loaded mnemonic.
273
+ *
274
+ * @returns Response containing recovered swaps
275
+ */
276
+ async recoverSwaps() {
277
+ return (await this.client.recoverSwaps());
278
+ }
279
+ /**
280
+ * Get current loaded mnemonic
281
+ * @returns The mnemonic as string
282
+ */
283
+ async getMnemonic() {
284
+ return await this.client.getMnemonic();
285
+ }
286
+ /**
287
+ * Get current loaded user id xpub
288
+ * @returns The xpub as string
289
+ */
290
+ async getUserIdXpub() {
291
+ return await this.client.getUserIdXpub();
292
+ }
293
+ /**
294
+ * Deletes all stored swaps
295
+ */
296
+ async clearSwapStorage() {
297
+ return await this.client.clearSwapStorage();
298
+ }
299
+ /**
300
+ * Delete one particular swap by id
301
+ */
302
+ async deleteSwap(id) {
303
+ return await this.client.deleteSwap(id);
304
+ }
305
+ }
306
+ //# sourceMappingURL=api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,qCAAqC;AACrC,OAAO,IAAI,EAAE,EACX,qBAAqB,EACrB,uBAAuB,EACvB,MAAM,IAAI,UAAU,GACrB,MAAM,+BAA+B,CAAC;AAGvC,gCAAgC;AAChC,IAAI,WAAW,GAAyB,IAAI,CAAC;AAE7C;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,QAAiB;IAC9C,IAAI,WAAW,EAAE,CAAC;QAChB,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,WAAW,GAAG,CAAC,KAAK,IAAI,EAAE;QACxB,4BAA4B;QAC5B,MAAM,MAAM,GACV,OAAO,OAAO,KAAK,WAAW;YAC9B,OAAO,CAAC,QAAQ,IAAI,IAAI;YACxB,OAAO,CAAC,QAAQ,CAAC,IAAI,IAAI,IAAI,CAAC;QAEhC,IAAI,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAC;YACxB,iDAAiD;YACjD,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,MAAM,CA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OAIG;IACH,KAAK,CAAC,UAAU,CAAC,MAAc;QAC7B,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,WAAW,CAAC,MAAc,EAAE,aAAqB;QACrD,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IAC9D,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,UAAU;QACd,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;QAC/C,OAAO;YACL,GAAG,EAAE,OAAO,CAAC,GAAG;YAChB,WAAW,EAAE,OAAO,CAAC,UAAU;SAChC,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,YAAY;QAChB,OAAO,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE,CAA8B,CAAC;IACzE,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,WAAW;QACf,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC;IACzC,CAAC;IACD;;;OAGG;IACH,KAAK,CAAC,aAAa;QACjB,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC;IAC3C,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,gBAAgB;QACpB,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,gBAAgB,EAAE,CAAC;IAC9C,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,UAAU,CAAC,EAAU;QACzB,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IAC1C,CAAC;CACF"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Lendaswap Client SDK for TypeScript/JavaScript.
3
+ *
4
+ * This SDK provides a high-level interface for interacting with Lendaswap
5
+ * Bitcoin-to-USDC atomic swaps.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import {
10
+ * Client,
11
+ * createDexieWalletStorage,
12
+ * createDexieSwapStorage,
13
+ * } from '@lendaswap/sdk';
14
+ *
15
+ * // Create storage providers using Dexie (IndexedDB)
16
+ * const walletStorage = createDexieWalletStorage();
17
+ * const swapStorage = createDexieSwapStorage();
18
+ *
19
+ * // Create client
20
+ * const client = await Client.create(
21
+ * 'https://api.lendaswap.com',
22
+ * walletStorage,
23
+ * swapStorage,
24
+ * 'bitcoin',
25
+ * 'https://arkade.computer'
26
+ * );
27
+ *
28
+ * // Initialize wallet (generates mnemonic if needed)
29
+ * await client.init();
30
+ *
31
+ * // Get asset pairs
32
+ * const pairs = await client.getAssetPairs();
33
+ * ```
34
+ *
35
+ * @packageDocumentation
36
+ */
37
+ export type { QuoteResponse, QuoteResponseInfo, SwapStorageProvider, TokenInfo, Version, VersionInfo, WalletStorageProvider, } from "./api.js";
38
+ export { type AssetPair, type BtcToEvmSwapResponse, type Chain, Client, type EvmToArkadeSwapRequest, type EvmToBtcSwapResponse, type EvmToLightningSwapRequest, type ExtendedSwapStorageData, type GelatoSubmitRequest, type GelatoSubmitResponse, type GetSwapResponse, type QuoteRequest, type RecoveredSwap, type RecoverSwapsResponse, type SwapCommonFields, type SwapRequest, type SwapStatus, TokenId, type TokenIdString, } from "./api.js";
39
+ export { PriceFeedService, type PriceTiers, type PriceUpdateCallback, type PriceUpdateMessage, type TradingPairPrices, } from "./price-feed.js";
40
+ export { createDexieSwapStorage, createDexieWalletStorage, DexieSwapStorageProvider, DexieWalletStorageProvider, STORAGE_KEYS, } from "./storage/index.js";
41
+ export type { Network, SwapData, SwapParams, VhtlcAmounts } from "./types.js";
42
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAIH,YAAY,EACV,aAAa,EACb,iBAAiB,EACjB,mBAAmB,EACnB,SAAS,EACT,OAAO,EACP,WAAW,EACX,qBAAqB,GACtB,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,KAAK,SAAS,EACd,KAAK,oBAAoB,EACzB,KAAK,KAAK,EACV,MAAM,EACN,KAAK,sBAAsB,EAC3B,KAAK,oBAAoB,EACzB,KAAK,yBAAyB,EAC9B,KAAK,uBAAuB,EAC5B,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,EACzB,KAAK,eAAe,EACpB,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,OAAO,EACP,KAAK,aAAa,GACnB,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,gBAAgB,EAChB,KAAK,UAAU,EACf,KAAK,mBAAmB,EACxB,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,GACvB,MAAM,iBAAiB,CAAC;AAIzB,OAAO,EACL,sBAAsB,EACtB,wBAAwB,EACxB,wBAAwB,EACxB,0BAA0B,EAC1B,YAAY,GACb,MAAM,oBAAoB,CAAC;AAE5B,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Lendaswap Client SDK for TypeScript/JavaScript.
3
+ *
4
+ * This SDK provides a high-level interface for interacting with Lendaswap
5
+ * Bitcoin-to-USDC atomic swaps.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import {
10
+ * Client,
11
+ * createDexieWalletStorage,
12
+ * createDexieSwapStorage,
13
+ * } from '@lendaswap/sdk';
14
+ *
15
+ * // Create storage providers using Dexie (IndexedDB)
16
+ * const walletStorage = createDexieWalletStorage();
17
+ * const swapStorage = createDexieSwapStorage();
18
+ *
19
+ * // Create client
20
+ * const client = await Client.create(
21
+ * 'https://api.lendaswap.com',
22
+ * walletStorage,
23
+ * swapStorage,
24
+ * 'bitcoin',
25
+ * 'https://arkade.computer'
26
+ * );
27
+ *
28
+ * // Initialize wallet (generates mnemonic if needed)
29
+ * await client.init();
30
+ *
31
+ * // Get asset pairs
32
+ * const pairs = await client.getAssetPairs();
33
+ * ```
34
+ *
35
+ * @packageDocumentation
36
+ */
37
+ // API client
38
+ export { Client, TokenId, } from "./api.js";
39
+ // Price feed
40
+ export { PriceFeedService, } from "./price-feed.js";
41
+ // Storage (wallet data)
42
+ // Swap storage (typed swap data using Dexie/IndexedDB)
43
+ // Wallet storage (typed wallet data using Dexie/IndexedDB)
44
+ export { createDexieSwapStorage, createDexieWalletStorage, DexieSwapStorageProvider, DexieWalletStorageProvider, STORAGE_KEYS, } from "./storage/index.js";
45
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAaH,aAAa;AACb,OAAO,EAIL,MAAM,EAcN,OAAO,GAER,MAAM,UAAU,CAAC;AAClB,aAAa;AACb,OAAO,EACL,gBAAgB,GAKjB,MAAM,iBAAiB,CAAC;AACzB,wBAAwB;AACxB,uDAAuD;AACvD,2DAA2D;AAC3D,OAAO,EACL,sBAAsB,EACtB,wBAAwB,EACxB,wBAAwB,EACxB,0BAA0B,EAC1B,YAAY,GACb,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,121 @@
1
+ /**
2
+ * WebSocket price feed service for real-time price updates.
3
+ *
4
+ * This module provides a WebSocket client that connects to the Lendaswap
5
+ * price feed endpoint and delivers real-time price updates with automatic
6
+ * reconnection support.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import { PriceFeedService, type PriceUpdateMessage } from '@lendaswap/sdk';
11
+ *
12
+ * const priceFeed = new PriceFeedService('wss://api.lendaswap.com');
13
+ *
14
+ * const unsubscribe = priceFeed.subscribe((update) => {
15
+ * console.log('Price update:', update);
16
+ * });
17
+ *
18
+ * // Later, to unsubscribe:
19
+ * unsubscribe();
20
+ * ```
21
+ */
22
+ /**
23
+ * Price tiers for different USD amounts.
24
+ * Different rates apply based on swap volume.
25
+ */
26
+ export interface PriceTiers {
27
+ /** Rate for amounts < $100 */
28
+ usd_1: number;
29
+ /** Rate for amounts $100-$999 */
30
+ usd_100: number;
31
+ /** Rate for amounts $1000-$4999 */
32
+ usd_1000: number;
33
+ /** Rate for amounts >= $5000 */
34
+ usd_5000: number;
35
+ }
36
+ /**
37
+ * Trading pair prices with volume-based tiers.
38
+ */
39
+ export interface TradingPairPrices {
40
+ /** Trading pair identifier, e.g., "USDC_POL-BTC" or "USDT0_POL-BTC" */
41
+ pair: string;
42
+ /** Price tiers for this pair */
43
+ tiers: PriceTiers;
44
+ }
45
+ /**
46
+ * Price update message received from WebSocket.
47
+ */
48
+ export interface PriceUpdateMessage {
49
+ /** Unix timestamp of the update */
50
+ timestamp: number;
51
+ /** Array of trading pair prices */
52
+ pairs: TradingPairPrices[];
53
+ }
54
+ /**
55
+ * Callback type for price updates.
56
+ */
57
+ export type PriceUpdateCallback = (update: PriceUpdateMessage) => void;
58
+ /**
59
+ * WebSocket price feed service with automatic reconnection.
60
+ *
61
+ * Manages connection to the /ws/prices endpoint with exponential backoff
62
+ * reconnection. Automatically connects when the first listener subscribes
63
+ * and disconnects when the last listener unsubscribes.
64
+ *
65
+ * @example
66
+ * ```typescript
67
+ * const priceFeed = new PriceFeedService('wss://api.lendaswap.com');
68
+ *
69
+ * // Subscribe to price updates
70
+ * const unsubscribe = priceFeed.subscribe((update) => {
71
+ * console.log('Prices:', update.pairs);
72
+ * });
73
+ *
74
+ * // Unsubscribe when done
75
+ * unsubscribe();
76
+ * ```
77
+ */
78
+ export declare class PriceFeedService {
79
+ private wsUrl;
80
+ private ws;
81
+ private reconnectTimer;
82
+ private listeners;
83
+ private reconnectDelay;
84
+ private maxReconnectDelay;
85
+ private isManualClose;
86
+ /**
87
+ * Create a new PriceFeedService.
88
+ *
89
+ * @param baseUrl - The base WebSocket URL (e.g., 'wss://api.lendaswap.com')
90
+ * or HTTP URL which will be converted to WebSocket.
91
+ */
92
+ constructor(baseUrl: string);
93
+ /**
94
+ * Subscribe to price updates.
95
+ *
96
+ * @param callback - Function to call when prices are updated
97
+ * @returns Unsubscribe function
98
+ */
99
+ subscribe(callback: PriceUpdateCallback): () => void;
100
+ /**
101
+ * Check if the WebSocket is currently connected.
102
+ */
103
+ isConnected(): boolean;
104
+ /**
105
+ * Get the number of active listeners.
106
+ */
107
+ listenerCount(): number;
108
+ /**
109
+ * Connect to WebSocket price feed.
110
+ */
111
+ private connect;
112
+ /**
113
+ * Schedule reconnection with exponential backoff.
114
+ */
115
+ private scheduleReconnect;
116
+ /**
117
+ * Close WebSocket connection.
118
+ */
119
+ close(): void;
120
+ }
121
+ //# sourceMappingURL=price-feed.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"price-feed.d.ts","sourceRoot":"","sources":["../src/price-feed.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH;;;GAGG;AACH,MAAM,WAAW,UAAU;IACzB,8BAA8B;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,iCAAiC;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,mCAAmC;IACnC,QAAQ,EAAE,MAAM,CAAC;IACjB,gCAAgC;IAChC,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,uEAAuE;IACvE,IAAI,EAAE,MAAM,CAAC;IACb,gCAAgC;IAChC,KAAK,EAAE,UAAU,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,mCAAmC;IACnC,SAAS,EAAE,MAAM,CAAC;IAClB,mCAAmC;IACnC,KAAK,EAAE,iBAAiB,EAAE,CAAC;CAC5B;AAED;;GAEG;AACH,MA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