@lendasat/lendaswap-sdk 0.1.65 → 0.1.67
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 +200 -126
- package/dist/api.d.ts +139 -103
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +236 -137
- package/dist/api.js.map +1 -1
- package/dist/index.d.ts +20 -20
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -27
- package/dist/index.js.map +1 -1
- package/package.json +14 -15
- package/wasm/lendaswap_wasm_sdk.d.ts +133 -123
- package/wasm/lendaswap_wasm_sdk_bg.js +548 -248
- package/wasm/lendaswap_wasm_sdk_bg.wasm +0 -0
- package/wasm/lendaswap_wasm_sdk_bg.wasm.d.ts +45 -35
- package/dist/price-calculations.test.d.ts +0 -2
- package/dist/price-calculations.test.d.ts.map +0 -1
- package/dist/price-calculations.test.js +0 -173
- package/dist/price-calculations.test.js.map +0 -1
- package/dist/storage/dexieSwapStorage.d.ts +0 -113
- package/dist/storage/dexieSwapStorage.d.ts.map +0 -1
- package/dist/storage/dexieSwapStorage.js +0 -200
- package/dist/storage/dexieSwapStorage.js.map +0 -1
- package/dist/storage/dexieVtxoSwapStorage.d.ts +0 -105
- package/dist/storage/dexieVtxoSwapStorage.d.ts.map +0 -1
- package/dist/storage/dexieVtxoSwapStorage.js +0 -149
- package/dist/storage/dexieVtxoSwapStorage.js.map +0 -1
- package/dist/storage/dexieWalletStorage.d.ts +0 -99
- package/dist/storage/dexieWalletStorage.d.ts.map +0 -1
- package/dist/storage/dexieWalletStorage.js +0 -139
- package/dist/storage/dexieWalletStorage.js.map +0 -1
- package/dist/storage/index.d.ts +0 -19
- package/dist/storage/index.d.ts.map +0 -1
- package/dist/storage/index.js +0 -22
- package/dist/storage/index.js.map +0 -1
|
Binary file
|
|
@@ -162,10 +162,19 @@ export const extendedswapstoragedata_btcToEvmResponse: (a: number) => number;
|
|
|
162
162
|
export const extendedswapstoragedata_evmToBtcResponse: (a: number) => number;
|
|
163
163
|
export const extendedswapstoragedata_btcToArkadeResponse: (a: number) => number;
|
|
164
164
|
export const extendedswapstoragedata_swapParams: (a: number) => number;
|
|
165
|
+
export const __wbg_clientbuilder_free: (a: number, b: number) => void;
|
|
166
|
+
export const clientbuilder_url: (a: number, b: number, c: number) => number;
|
|
167
|
+
export const clientbuilder_storage: (a: number, b: number) => number;
|
|
168
|
+
export const clientbuilder_network: (a: number, b: number, c: number) => number;
|
|
169
|
+
export const clientbuilder_arkadeUrl: (a: number, b: number, c: number) => number;
|
|
170
|
+
export const clientbuilder_esploraUrl: (a: number, b: number, c: number) => number;
|
|
171
|
+
export const clientbuilder_build: (a: number) => [number, number, number];
|
|
165
172
|
export const __wbg_client_free: (a: number, b: number) => void;
|
|
166
|
-
export const
|
|
173
|
+
export const client_builder: () => number;
|
|
174
|
+
export const client_new: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => [number, number, number];
|
|
167
175
|
export const client_init: (a: number, b: number, c: number) => any;
|
|
168
|
-
export const client_createArkadeToEvmSwap: (a: number, b: number, c: number, d: number, e:
|
|
176
|
+
export const client_createArkadeToEvmSwap: (a: number, b: number, c: number, d: number, e: bigint, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number) => any;
|
|
177
|
+
export const client_createLightningToEvmSwap: (a: number, b: number, c: number, d: number, e: bigint, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number) => any;
|
|
169
178
|
export const client_createEvmToArkadeSwap: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number) => any;
|
|
170
179
|
export const client_createEvmToLightningSwap: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number) => any;
|
|
171
180
|
export const client_getAssetPairs: (a: number) => any;
|
|
@@ -186,8 +195,6 @@ export const client_getMnemonic: (a: number) => any;
|
|
|
186
195
|
export const client_getUserIdXpub: (a: number) => any;
|
|
187
196
|
export const client_clearSwapStorage: (a: number) => any;
|
|
188
197
|
export const client_deleteSwap: (a: number, b: number, c: number) => any;
|
|
189
|
-
export const client_getCorruptedSwapIds: (a: number) => [number, number];
|
|
190
|
-
export const client_deleteCorruptedSwaps: (a: number) => any;
|
|
191
198
|
export const client_estimateVtxoSwap: (a: number, b: number, c: number) => any;
|
|
192
199
|
export const client_createVtxoSwap: (a: number, b: number, c: number) => any;
|
|
193
200
|
export const client_getVtxoSwap: (a: number, b: number, c: number) => any;
|
|
@@ -252,6 +259,7 @@ export const __wbg_set_btctoevmswapresponse_bitcoin_htlc_fund_txid: (a: number,
|
|
|
252
259
|
export const __wbg_set_btctoevmswapresponse_evm_htlc_claim_txid: (a: number, b: number, c: number) => void;
|
|
253
260
|
export const __wbg_set_btctoevmswapresponse_evm_htlc_fund_txid: (a: number, b: number, c: number) => void;
|
|
254
261
|
export const __wbg_set_evmtobtcswapresponse_evm_htlc_fund_txid: (a: number, b: number, c: number) => void;
|
|
262
|
+
export const clientbuilder_new: () => number;
|
|
255
263
|
export const __wbg_get_evmtobtcswapresponse_user_address_arkade: (a: number) => [number, number];
|
|
256
264
|
export const __wbg_get_evmtobtcswapresponse_bitcoin_htlc_fund_txid: (a: number) => [number, number];
|
|
257
265
|
export const __wbg_get_evmtobtcswapresponse_bitcoin_htlc_claim_txid: (a: number) => [number, number];
|
|
@@ -298,32 +306,10 @@ export const __wbg_set_extendedvtxoswapstoragedata_swap_params: (a: number, b: n
|
|
|
298
306
|
export const __wbg_get_evmtobtcswapresponse_source_token: (a: number) => number;
|
|
299
307
|
export const __wbg_get_evmtobtcswapresponse_target_token: (a: number) => number;
|
|
300
308
|
export const __wbg_get_btctoevmswapresponse_target_token: (a: number) => number;
|
|
301
|
-
export const
|
|
302
|
-
export const
|
|
303
|
-
export const
|
|
304
|
-
export const
|
|
305
|
-
export const __wbg_jsvtxoswapstorageprovider_free: (a: number, b: number) => void;
|
|
306
|
-
export const jsvtxoswapstorageprovider_new: (a: any, b: any, c: any, d: any, e: any) => number;
|
|
307
|
-
export const __wbg_swapparams_free: (a: number, b: number) => void;
|
|
308
|
-
export const __wbg_get_swapparams_own_sk: (a: number) => [number, number];
|
|
309
|
-
export const __wbg_set_swapparams_own_sk: (a: number, b: number, c: number) => void;
|
|
310
|
-
export const __wbg_get_swapparams_own_pk: (a: number) => [number, number];
|
|
311
|
-
export const __wbg_set_swapparams_own_pk: (a: number, b: number, c: number) => void;
|
|
312
|
-
export const __wbg_get_swapparams_preimage: (a: number) => [number, number];
|
|
313
|
-
export const __wbg_set_swapparams_preimage: (a: number, b: number, c: number) => void;
|
|
314
|
-
export const __wbg_get_swapparams_preimage_hash: (a: number) => [number, number];
|
|
315
|
-
export const __wbg_set_swapparams_preimage_hash: (a: number, b: number, c: number) => void;
|
|
316
|
-
export const __wbg_get_swapparams_user_id: (a: number) => [number, number];
|
|
317
|
-
export const __wbg_set_swapparams_user_id: (a: number, b: number, c: number) => void;
|
|
318
|
-
export const __wbg_get_swapparams_key_index: (a: number) => number;
|
|
319
|
-
export const __wbg_set_swapparams_key_index: (a: number, b: number) => void;
|
|
320
|
-
export const __wbg_vhtlcamounts_free: (a: number, b: number) => void;
|
|
321
|
-
export const __wbg_get_vhtlcamounts_spendable: (a: number) => bigint;
|
|
322
|
-
export const __wbg_set_vhtlcamounts_spendable: (a: number, b: bigint) => void;
|
|
323
|
-
export const __wbg_get_vhtlcamounts_spent: (a: number) => bigint;
|
|
324
|
-
export const __wbg_set_vhtlcamounts_spent: (a: number, b: bigint) => void;
|
|
325
|
-
export const __wbg_get_vhtlcamounts_recoverable: (a: number) => bigint;
|
|
326
|
-
export const __wbg_set_vhtlcamounts_recoverable: (a: number, b: bigint) => void;
|
|
309
|
+
export const __wbg_idbstoragehandle_free: (a: number, b: number) => void;
|
|
310
|
+
export const idbstoragehandle_name: (a: number) => [number, number];
|
|
311
|
+
export const idbstoragehandle_close: (a: number) => void;
|
|
312
|
+
export const openIdbDatabase: (a: number, b: number) => any;
|
|
327
313
|
export const initialize: () => void;
|
|
328
314
|
export const setLogLevel: (a: number, b: number) => [number, number];
|
|
329
315
|
export const getLogLevel: () => [number, number];
|
|
@@ -344,6 +330,26 @@ export const __wbg_get_version_tag: (a: number) => [number, number];
|
|
|
344
330
|
export const __wbg_set_version_tag: (a: number, b: number, c: number) => void;
|
|
345
331
|
export const __wbg_get_version_commitHash: (a: number) => [number, number];
|
|
346
332
|
export const __wbg_set_version_commitHash: (a: number, b: number, c: number) => void;
|
|
333
|
+
export const __wbg_swapparams_free: (a: number, b: number) => void;
|
|
334
|
+
export const __wbg_get_swapparams_own_sk: (a: number) => [number, number];
|
|
335
|
+
export const __wbg_set_swapparams_own_sk: (a: number, b: number, c: number) => void;
|
|
336
|
+
export const __wbg_get_swapparams_own_pk: (a: number) => [number, number];
|
|
337
|
+
export const __wbg_set_swapparams_own_pk: (a: number, b: number, c: number) => void;
|
|
338
|
+
export const __wbg_get_swapparams_preimage: (a: number) => [number, number];
|
|
339
|
+
export const __wbg_set_swapparams_preimage: (a: number, b: number, c: number) => void;
|
|
340
|
+
export const __wbg_get_swapparams_preimage_hash: (a: number) => [number, number];
|
|
341
|
+
export const __wbg_set_swapparams_preimage_hash: (a: number, b: number, c: number) => void;
|
|
342
|
+
export const __wbg_get_swapparams_user_id: (a: number) => [number, number];
|
|
343
|
+
export const __wbg_set_swapparams_user_id: (a: number, b: number, c: number) => void;
|
|
344
|
+
export const __wbg_get_swapparams_key_index: (a: number) => number;
|
|
345
|
+
export const __wbg_set_swapparams_key_index: (a: number, b: number) => void;
|
|
346
|
+
export const __wbg_vhtlcamounts_free: (a: number, b: number) => void;
|
|
347
|
+
export const __wbg_get_vhtlcamounts_spendable: (a: number) => bigint;
|
|
348
|
+
export const __wbg_set_vhtlcamounts_spendable: (a: number, b: bigint) => void;
|
|
349
|
+
export const __wbg_get_vhtlcamounts_spent: (a: number) => bigint;
|
|
350
|
+
export const __wbg_set_vhtlcamounts_spent: (a: number, b: bigint) => void;
|
|
351
|
+
export const __wbg_get_vhtlcamounts_recoverable: (a: number) => bigint;
|
|
352
|
+
export const __wbg_set_vhtlcamounts_recoverable: (a: number, b: bigint) => void;
|
|
347
353
|
export const __wbg_intounderlyingsink_free: (a: number, b: number) => void;
|
|
348
354
|
export const intounderlyingsink_write: (a: number, b: any) => any;
|
|
349
355
|
export const intounderlyingsink_close: (a: number) => any;
|
|
@@ -365,11 +371,15 @@ export const rustsecp256k1_v0_10_0_context_create: (a: number) => number;
|
|
|
365
371
|
export const rustsecp256k1_v0_10_0_context_destroy: (a: number) => void;
|
|
366
372
|
export const rustsecp256k1_v0_10_0_default_illegal_callback_fn: (a: number, b: number) => void;
|
|
367
373
|
export const rustsecp256k1_v0_10_0_default_error_callback_fn: (a: number, b: number) => void;
|
|
368
|
-
export const
|
|
369
|
-
export const
|
|
370
|
-
export const
|
|
371
|
-
export const
|
|
372
|
-
export const
|
|
374
|
+
export const wasm_bindgen__convert__closures_____invoke__h28f0f152c9423ebc: (a: number, b: number, c: any) => void;
|
|
375
|
+
export const wasm_bindgen__closure__destroy__hbf774c1eb42aa1c1: (a: number, b: number) => void;
|
|
376
|
+
export const wasm_bindgen__convert__closures_____invoke__h3ae63c3559e47aa7: (a: number, b: number, c: any) => void;
|
|
377
|
+
export const wasm_bindgen__closure__destroy__h3bcc7046e1647ab6: (a: number, b: number) => void;
|
|
378
|
+
export const wasm_bindgen__convert__closures_____invoke__hc679149b55f75c33: (a: number, b: number) => void;
|
|
379
|
+
export const wasm_bindgen__closure__destroy__h84bbd0c3dfad89d9: (a: number, b: number) => void;
|
|
380
|
+
export const wasm_bindgen__convert__closures_____invoke__h336ff17a2ee3a667: (a: number, b: number, c: any) => void;
|
|
381
|
+
export const wasm_bindgen__closure__destroy__hc0005a862eeb5409: (a: number, b: number) => void;
|
|
382
|
+
export const wasm_bindgen__convert__closures_____invoke__h95b85cd8e04171dd: (a: number, b: number, c: any, d: any) => void;
|
|
373
383
|
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
374
384
|
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
375
385
|
export const __wbindgen_exn_store: (a: number) => void;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"price-calculations.test.d.ts","sourceRoot":"","sources":["../src/price-calculations.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { calculateSourceAmount, calculateTargetAmount, computeExchangeRate, selectTierRate, } from "./price-calculations.js";
|
|
3
|
-
describe("selectTierRate", () => {
|
|
4
|
-
const mockTiers = {
|
|
5
|
-
tier_1: 0.000010773,
|
|
6
|
-
tier_100: 0.0000107787855,
|
|
7
|
-
tier_1000: 0.000010774087275,
|
|
8
|
-
tier_5000: 0.00001075242756,
|
|
9
|
-
};
|
|
10
|
-
it("should return tier_1 for amounts less than 100", () => {
|
|
11
|
-
expect(selectTierRate(mockTiers, 0)).toBe(mockTiers.tier_1);
|
|
12
|
-
expect(selectTierRate(mockTiers, 50)).toBe(mockTiers.tier_1);
|
|
13
|
-
expect(selectTierRate(mockTiers, 99)).toBe(mockTiers.tier_1);
|
|
14
|
-
});
|
|
15
|
-
it("should return tier_100 for amounts between 100 and 999", () => {
|
|
16
|
-
expect(selectTierRate(mockTiers, 100)).toBe(mockTiers.tier_100);
|
|
17
|
-
expect(selectTierRate(mockTiers, 500)).toBe(mockTiers.tier_100);
|
|
18
|
-
expect(selectTierRate(mockTiers, 999)).toBe(mockTiers.tier_100);
|
|
19
|
-
});
|
|
20
|
-
it("should return tier_1000 for amounts between 1000 and 4999", () => {
|
|
21
|
-
expect(selectTierRate(mockTiers, 1000)).toBe(mockTiers.tier_1000);
|
|
22
|
-
expect(selectTierRate(mockTiers, 2500)).toBe(mockTiers.tier_1000);
|
|
23
|
-
expect(selectTierRate(mockTiers, 4999)).toBe(mockTiers.tier_1000);
|
|
24
|
-
});
|
|
25
|
-
it("should return tier_5000 for amounts 5000 and above", () => {
|
|
26
|
-
expect(selectTierRate(mockTiers, 5000)).toBe(mockTiers.tier_5000);
|
|
27
|
-
expect(selectTierRate(mockTiers, 10000)).toBe(mockTiers.tier_5000);
|
|
28
|
-
expect(selectTierRate(mockTiers, 100000)).toBe(mockTiers.tier_5000);
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
describe("computeExchangeRate", () => {
|
|
32
|
-
// Rate from backend: BTC per 1 USDC (approximately 1 BTC = 92,828 USDC)
|
|
33
|
-
const btcPerUsdc = 0.000010773;
|
|
34
|
-
describe("when source is EVM token and target is BTC", () => {
|
|
35
|
-
it("should return the rate as-is (BTC per stablecoin)", () => {
|
|
36
|
-
const result = computeExchangeRate(btcPerUsdc, false, false);
|
|
37
|
-
expect(result).toBe(btcPerUsdc);
|
|
38
|
-
});
|
|
39
|
-
it("should work correctly for USDC → BTC swap calculation", () => {
|
|
40
|
-
// User wants to swap 1000 USDC for BTC
|
|
41
|
-
// Rate: 0.000010773 BTC per 1 USDC
|
|
42
|
-
// Expected BTC: 1000 * 0.000010773 = 0.010773 BTC
|
|
43
|
-
const rate = computeExchangeRate(btcPerUsdc, false, false);
|
|
44
|
-
const usdcAmount = 1000;
|
|
45
|
-
const btcReceived = usdcAmount * rate;
|
|
46
|
-
expect(btcReceived).toBeCloseTo(0.010773, 6);
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
describe("when source is BTC and target is EVM token", () => {
|
|
50
|
-
it("should invert the rate (stablecoin per BTC)", () => {
|
|
51
|
-
const result = computeExchangeRate(btcPerUsdc, true, true);
|
|
52
|
-
// 1 / 0.000010773 ≈ 92,824.65 USDC per BTC
|
|
53
|
-
expect(result).toBeCloseTo(92824.65, 0);
|
|
54
|
-
});
|
|
55
|
-
it("should work correctly for BTC → USDC swap calculation", () => {
|
|
56
|
-
// User wants to swap 0.01 BTC for USDC
|
|
57
|
-
// Inverted rate: ~92,828 USDC per 1 BTC
|
|
58
|
-
// Expected USDC: 0.01 * 92828 ≈ 928.28 USDC
|
|
59
|
-
const rate = computeExchangeRate(btcPerUsdc, true, true);
|
|
60
|
-
const btcAmount = 0.01;
|
|
61
|
-
const usdcReceived = btcAmount * rate;
|
|
62
|
-
expect(usdcReceived).toBeCloseTo(928.28, 0);
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
describe("when source is BTC and target is also BTC (Arkade ↔ Lightning)", () => {
|
|
66
|
-
it("should not invert the rate", () => {
|
|
67
|
-
// BTC to BTC swaps don't need inversion
|
|
68
|
-
const btcToBtcRate = 1.0; // 1:1 ratio
|
|
69
|
-
const result = computeExchangeRate(btcToBtcRate, true, false);
|
|
70
|
-
expect(result).toBe(btcToBtcRate);
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
describe("reverse calculation (target amount → source amount)", () => {
|
|
74
|
-
it("should correctly calculate source amount when target is known (BTC → USDC)", () => {
|
|
75
|
-
// User wants 1000 USDC, how much BTC do they need?
|
|
76
|
-
// Rate after inversion: ~92,828 USDC per BTC
|
|
77
|
-
// Source BTC = 1000 / 92828 ≈ 0.01077 BTC
|
|
78
|
-
const rate = computeExchangeRate(btcPerUsdc, true, true);
|
|
79
|
-
const targetUsdc = 1000;
|
|
80
|
-
const sourceBtc = targetUsdc / rate;
|
|
81
|
-
expect(sourceBtc).toBeCloseTo(0.01077, 4);
|
|
82
|
-
});
|
|
83
|
-
it("should correctly calculate source amount when target is known (USDC → BTC)", () => {
|
|
84
|
-
// User wants 0.01 BTC, how much USDC do they need?
|
|
85
|
-
// Rate: 0.000010773 BTC per USDC
|
|
86
|
-
// Source USDC = 0.01 / 0.000010773 ≈ 928 USDC
|
|
87
|
-
const rate = computeExchangeRate(btcPerUsdc, false, false);
|
|
88
|
-
const targetBtc = 0.01;
|
|
89
|
-
const sourceUsdc = targetBtc / rate;
|
|
90
|
-
expect(sourceUsdc).toBeCloseTo(928, 0);
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
describe("calculateTargetAmount", () => {
|
|
95
|
-
const networkFee = 0.0001; // 10,000 sats
|
|
96
|
-
describe("USDC → BTC (source is EVM, target is BTC)", () => {
|
|
97
|
-
// Rate: 0.000010773 BTC per 1 USDC
|
|
98
|
-
const exchangeRate = 0.000010773;
|
|
99
|
-
it("should convert and subtract fee from target", () => {
|
|
100
|
-
const sourceAmount = 1000; // 1000 USDC
|
|
101
|
-
const result = calculateTargetAmount(sourceAmount, exchangeRate, networkFee, false, // source is not BTC
|
|
102
|
-
true);
|
|
103
|
-
// 1000 * 0.000010773 - 0.0001 = 0.010673 BTC
|
|
104
|
-
expect(result).toBeCloseTo(0.010673, 6);
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
describe("BTC → USDC (source is BTC, target is EVM)", () => {
|
|
108
|
-
// Rate: ~92,824 USDC per 1 BTC (after inversion)
|
|
109
|
-
const exchangeRate = 92824.65;
|
|
110
|
-
it("should subtract fee from source, then convert", () => {
|
|
111
|
-
const sourceAmount = 0.01; // 0.01 BTC
|
|
112
|
-
const result = calculateTargetAmount(sourceAmount, exchangeRate, networkFee, true, // source is BTC
|
|
113
|
-
false);
|
|
114
|
-
// (0.01 - 0.0001) * 92824.65 = 918.96 USDC
|
|
115
|
-
expect(result).toBeCloseTo(918.96, 0);
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
describe("with zero fee", () => {
|
|
119
|
-
it("should just multiply source by rate", () => {
|
|
120
|
-
const result = calculateTargetAmount(100, 2.5, 0, // no fee
|
|
121
|
-
false, false);
|
|
122
|
-
expect(result).toBe(250);
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
describe("calculateSourceAmount", () => {
|
|
127
|
-
const networkFee = 0.0001; // 10,000 sats
|
|
128
|
-
describe("USDC → BTC (source is EVM, target is BTC)", () => {
|
|
129
|
-
// Rate: 0.000010773 BTC per 1 USDC
|
|
130
|
-
const exchangeRate = 0.000010773;
|
|
131
|
-
it("should add fee to target, then reverse convert", () => {
|
|
132
|
-
const targetAmount = 0.01; // Want 0.01 BTC
|
|
133
|
-
const result = calculateSourceAmount(targetAmount, exchangeRate, networkFee, false, // source is not BTC
|
|
134
|
-
true);
|
|
135
|
-
// (0.01 + 0.0001) / 0.000010773 = 937.26 USDC
|
|
136
|
-
expect(result).toBeCloseTo(937.26, 0);
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
describe("BTC → USDC (source is BTC, target is EVM)", () => {
|
|
140
|
-
// Rate: ~92,824 USDC per 1 BTC (after inversion)
|
|
141
|
-
const exchangeRate = 92824.65;
|
|
142
|
-
it("should reverse convert, then add fee to source", () => {
|
|
143
|
-
const targetAmount = 1000; // Want 1000 USDC
|
|
144
|
-
const result = calculateSourceAmount(targetAmount, exchangeRate, networkFee, true, // source is BTC
|
|
145
|
-
false);
|
|
146
|
-
// 1000 / 92824.65 + 0.0001 = 0.01088 BTC
|
|
147
|
-
expect(result).toBeCloseTo(0.01088, 4);
|
|
148
|
-
});
|
|
149
|
-
});
|
|
150
|
-
describe("round-trip consistency", () => {
|
|
151
|
-
it("should be consistent with calculateTargetAmount (USDC → BTC)", () => {
|
|
152
|
-
const exchangeRate = 0.000010773;
|
|
153
|
-
const sourceAmount = 1000;
|
|
154
|
-
// Forward: calculate target from source
|
|
155
|
-
const targetAmount = calculateTargetAmount(sourceAmount, exchangeRate, networkFee, false, true);
|
|
156
|
-
// Reverse: calculate source from target
|
|
157
|
-
const calculatedSource = calculateSourceAmount(targetAmount, exchangeRate, networkFee, false, true);
|
|
158
|
-
// Should get back original source amount
|
|
159
|
-
expect(calculatedSource).toBeCloseTo(sourceAmount, 4);
|
|
160
|
-
});
|
|
161
|
-
it("should be consistent with calculateTargetAmount (BTC → USDC)", () => {
|
|
162
|
-
const exchangeRate = 92824.65;
|
|
163
|
-
const sourceAmount = 0.01;
|
|
164
|
-
// Forward: calculate target from source
|
|
165
|
-
const targetAmount = calculateTargetAmount(sourceAmount, exchangeRate, networkFee, true, false);
|
|
166
|
-
// Reverse: calculate source from target
|
|
167
|
-
const calculatedSource = calculateSourceAmount(targetAmount, exchangeRate, networkFee, true, false);
|
|
168
|
-
// Should get back original source amount
|
|
169
|
-
expect(calculatedSource).toBeCloseTo(sourceAmount, 6);
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
//# sourceMappingURL=price-calculations.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"price-calculations.test.js","sourceRoot":"","sources":["../src/price-calculations.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EACL,qBAAqB,EACrB,qBAAqB,EACrB,mBAAmB,EACnB,cAAc,GACf,MAAM,yBAAyB,CAAC;AAEjC,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE;IAC9B,MAAM,SAAS,GAAG;QAChB,MAAM,EAAE,WAAW;QACnB,QAAQ,EAAE,eAAe;QACzB,SAAS,EAAE,iBAAiB;QAC5B,SAAS,EAAE,gBAAgB;KAC5B,CAAC;IAEF,EAAE,CAAC,gDAAgD,EAAE,GAAG,EAAE;QACxD,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QAC5D,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QAC7D,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IAC/D,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,wDAAwD,EAAE,GAAG,EAAE;QAChE,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,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AAG,qBAAqB,CACxC,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,KAAK,EACL,IAAI,CACL,CAAC;YAEF,wCAAwC;YACxC,MAAM,gBAAgB,GAAG,qBAAqB,CAC5C,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,KAAK,EACL,IAAI,CACL,CAAC;YAEF,yCAAyC;YACzC,MAAM,CAAC,gBAAgB,CAAC,CAAC,WAAW,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC;QACxD,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,8DAA8D,EAAE,GAAG,EAAE;YACtE,MAAM,YAAY,GAAG,QAAQ,CAAC;YAC9B,MAAM,YAAY,GAAG,IAAI,CAAC;YAE1B,wCAAwC;YACxC,MAAM,YAAY,GAAG,qBAAqB,CACxC,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,IAAI,EACJ,KAAK,CACN,CAAC;YAEF,wCAAwC;YACxC,MAAM,gBAAgB,GAAG,qBAAqB,CAC5C,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,IAAI,EACJ,KAAK,CACN,CAAC;YAEF,yCAAyC;YACzC,MAAM,CAAC,gBAAgB,CAAC,CAAC,WAAW,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC;QACxD,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Dexie-based swap storage provider for IndexedDB.
|
|
3
|
-
*
|
|
4
|
-
* This module provides a typed swap storage implementation using Dexie,
|
|
5
|
-
* which is a wrapper around IndexedDB that provides a simpler API.
|
|
6
|
-
*/
|
|
7
|
-
import type { ExtendedSwapStorageData } from "../api.js";
|
|
8
|
-
/**
|
|
9
|
-
* Dexie-based swap storage provider.
|
|
10
|
-
*
|
|
11
|
-
* Stores swap data as typed objects in IndexedDB using Dexie.
|
|
12
|
-
* This provides better performance and querying capabilities compared
|
|
13
|
-
* to storing serialized JSON strings.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* import { DexieSwapStorageProvider } from '@lendasat/lendaswap-sdk';
|
|
18
|
-
*
|
|
19
|
-
* const swapStorage = new DexieSwapStorageProvider();
|
|
20
|
-
*
|
|
21
|
-
* // Use with the Client
|
|
22
|
-
* const client = await Client.create(
|
|
23
|
-
* 'https://apilendaswap.lendasat.com',
|
|
24
|
-
* walletStorage,
|
|
25
|
-
* swapStorage,
|
|
26
|
-
* 'bitcoin',
|
|
27
|
-
* 'https://arkade.computer'
|
|
28
|
-
* );
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
export declare class DexieSwapStorageProvider {
|
|
32
|
-
private db;
|
|
33
|
-
/**
|
|
34
|
-
* Create a new DexieSwapStorageProvider.
|
|
35
|
-
*
|
|
36
|
-
* @param dbName - Optional database name (default: "lendaswap")
|
|
37
|
-
*/
|
|
38
|
-
constructor(dbName?: string);
|
|
39
|
-
/**
|
|
40
|
-
* Get swap data by swap ID.
|
|
41
|
-
*
|
|
42
|
-
* @param swapId - The swap ID
|
|
43
|
-
* @returns The swap data, or null if not found
|
|
44
|
-
*/
|
|
45
|
-
get(swapId: string): Promise<ExtendedSwapStorageData | null>;
|
|
46
|
-
/**
|
|
47
|
-
* Store swap data.
|
|
48
|
-
*
|
|
49
|
-
* @param swapId - The swap ID
|
|
50
|
-
* @param data - The swap data to store
|
|
51
|
-
*/
|
|
52
|
-
store(swapId: string, data: ExtendedSwapStorageData): Promise<void>;
|
|
53
|
-
/**
|
|
54
|
-
* Delete swap data by swap ID.
|
|
55
|
-
*
|
|
56
|
-
* @param swapId - The swap ID
|
|
57
|
-
*/
|
|
58
|
-
delete(swapId: string): Promise<void>;
|
|
59
|
-
/**
|
|
60
|
-
* List all stored swap IDs.
|
|
61
|
-
*
|
|
62
|
-
* @returns Array of swap IDs
|
|
63
|
-
*/
|
|
64
|
-
list(): Promise<string[]>;
|
|
65
|
-
/**
|
|
66
|
-
* Clear all swap data.
|
|
67
|
-
*/
|
|
68
|
-
clear(): Promise<void>;
|
|
69
|
-
/**
|
|
70
|
-
* Get all stored swaps.
|
|
71
|
-
*
|
|
72
|
-
* @returns Array of all swap data
|
|
73
|
-
*/
|
|
74
|
-
getAll(): Promise<ExtendedSwapStorageData[]>;
|
|
75
|
-
/**
|
|
76
|
-
* Close the database connection.
|
|
77
|
-
*/
|
|
78
|
-
close(): void;
|
|
79
|
-
/**
|
|
80
|
-
* Get raw swap_params for a potentially corrupted entry.
|
|
81
|
-
*
|
|
82
|
-
* This method reads the raw data from IndexedDB and extracts just the swap_params,
|
|
83
|
-
* which can succeed even when the full ExtendedSwapStorageData fails to deserialize.
|
|
84
|
-
*
|
|
85
|
-
* @param swapId - The swap ID
|
|
86
|
-
* @returns The swap_params as raw object, or null if not found or invalid
|
|
87
|
-
*/
|
|
88
|
-
getRawSwapParams(swapId: string): Promise<Record<string, any> | null>;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Create a Dexie-based swap storage provider.
|
|
92
|
-
*
|
|
93
|
-
* This is a convenience function for creating a DexieSwapStorageProvider.
|
|
94
|
-
*
|
|
95
|
-
* @param dbName - Optional database name (default: "lendaswap")
|
|
96
|
-
* @returns A new DexieSwapStorageProvider instance
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* ```typescript
|
|
100
|
-
* import { createDexieSwapStorage, Client } from '@lendasat/lendaswap-sdk';
|
|
101
|
-
*
|
|
102
|
-
* const swapStorage = createDexieSwapStorage();
|
|
103
|
-
* const client = await Client.create(
|
|
104
|
-
* 'https://apilendaswap.lendasat.com',
|
|
105
|
-
* walletStorage,
|
|
106
|
-
* swapStorage,
|
|
107
|
-
* 'bitcoin',
|
|
108
|
-
* 'https://arkade.computer'
|
|
109
|
-
* );
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
export declare function createDexieSwapStorage(dbName?: string): DexieSwapStorageProvider;
|
|
113
|
-
//# sourceMappingURL=dexieSwapStorage.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dexieSwapStorage.d.ts","sourceRoot":"","sources":["../../src/storage/dexieSwapStorage.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC;AAqEzD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,wBAAwB;IACnC,OAAO,CAAC,EAAE,CAAoB;IAE9B;;;;OAIG;gBACS,MAAM,CAAC,EAAE,MAAM;IAI3B;;;;;OAKG;IACG,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,uBAAuB,GAAG,IAAI,CAAC;IAUlE;;;;;OAKG;IACG,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,uBAAuB,GAAG,OAAO,CAAC,IAAI,CAAC;IAIzE;;;;OAIG;IACG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI3C;;;;OAIG;IACG,IAAI,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAI/B;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B;;;;OAIG;IACG,MAAM,IAAI,OAAO,CAAC,uBAAuB,EAAE,CAAC;IAMlD;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;;;;;;;OAQG;IAEG,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC;CAS5E;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,CAAC,EAAE,MAAM,GACd,wBAAwB,CAE1B"}
|
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Dexie-based swap storage provider for IndexedDB.
|
|
3
|
-
*
|
|
4
|
-
* This module provides a typed swap storage implementation using Dexie,
|
|
5
|
-
* which is a wrapper around IndexedDB that provides a simpler API.
|
|
6
|
-
*/
|
|
7
|
-
import Dexie from "dexie";
|
|
8
|
-
/**
|
|
9
|
-
* Dexie database for storing swap data.
|
|
10
|
-
*/
|
|
11
|
-
class LendaswapDatabase extends Dexie {
|
|
12
|
-
swaps;
|
|
13
|
-
constructor(dbName = "lendaswap") {
|
|
14
|
-
super(dbName);
|
|
15
|
-
// Version 1: Original schema
|
|
16
|
-
this.version(1).stores({
|
|
17
|
-
swaps: "id", // Primary key only, no additional indexes needed
|
|
18
|
-
});
|
|
19
|
-
// Version 2: Migrate refund_locktime field names
|
|
20
|
-
// - btc_to_evm: refund_locktime -> vhtlc_refund_locktime, add evm_refund_locktime
|
|
21
|
-
// - evm_to_btc: refund_locktime -> evm_refund_locktime, add vhtlc_refund_locktime
|
|
22
|
-
this.version(2)
|
|
23
|
-
.stores({
|
|
24
|
-
swaps: "id",
|
|
25
|
-
})
|
|
26
|
-
.upgrade(async (tx) => {
|
|
27
|
-
const TWELVE_HOURS_SECONDS = 43200;
|
|
28
|
-
await tx
|
|
29
|
-
.table("swaps")
|
|
30
|
-
.toCollection()
|
|
31
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
-
.modify((record) => {
|
|
33
|
-
const response = record.response;
|
|
34
|
-
if (!response || !("refund_locktime" in response))
|
|
35
|
-
return;
|
|
36
|
-
// Skip BtcToArkade swaps - they already have correct field names
|
|
37
|
-
if ("btc_htlc_address" in response)
|
|
38
|
-
return;
|
|
39
|
-
const oldLocktime = response.refund_locktime;
|
|
40
|
-
// Detect swap type: evm_to_btc has source_token_address field
|
|
41
|
-
const isEvmToBtc = "source_token_address" in response;
|
|
42
|
-
if (isEvmToBtc) {
|
|
43
|
-
// evm_to_btc: refund_locktime -> evm_refund_locktime
|
|
44
|
-
// vhtlc_refund_locktime = evm_refund_locktime - 12h
|
|
45
|
-
response.evm_refund_locktime = oldLocktime;
|
|
46
|
-
response.vhtlc_refund_locktime =
|
|
47
|
-
oldLocktime - TWELVE_HOURS_SECONDS;
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
// btc_to_evm: refund_locktime -> vhtlc_refund_locktime
|
|
51
|
-
// evm_refund_locktime = vhtlc_refund_locktime - 12h
|
|
52
|
-
response.vhtlc_refund_locktime = oldLocktime;
|
|
53
|
-
response.evm_refund_locktime = oldLocktime - TWELVE_HOURS_SECONDS;
|
|
54
|
-
}
|
|
55
|
-
delete response.refund_locktime;
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Dexie-based swap storage provider.
|
|
62
|
-
*
|
|
63
|
-
* Stores swap data as typed objects in IndexedDB using Dexie.
|
|
64
|
-
* This provides better performance and querying capabilities compared
|
|
65
|
-
* to storing serialized JSON strings.
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```typescript
|
|
69
|
-
* import { DexieSwapStorageProvider } from '@lendasat/lendaswap-sdk';
|
|
70
|
-
*
|
|
71
|
-
* const swapStorage = new DexieSwapStorageProvider();
|
|
72
|
-
*
|
|
73
|
-
* // Use with the Client
|
|
74
|
-
* const client = await Client.create(
|
|
75
|
-
* 'https://apilendaswap.lendasat.com',
|
|
76
|
-
* walletStorage,
|
|
77
|
-
* swapStorage,
|
|
78
|
-
* 'bitcoin',
|
|
79
|
-
* 'https://arkade.computer'
|
|
80
|
-
* );
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
export class DexieSwapStorageProvider {
|
|
84
|
-
db;
|
|
85
|
-
/**
|
|
86
|
-
* Create a new DexieSwapStorageProvider.
|
|
87
|
-
*
|
|
88
|
-
* @param dbName - Optional database name (default: "lendaswap")
|
|
89
|
-
*/
|
|
90
|
-
constructor(dbName) {
|
|
91
|
-
this.db = new LendaswapDatabase(dbName);
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Get swap data by swap ID.
|
|
95
|
-
*
|
|
96
|
-
* @param swapId - The swap ID
|
|
97
|
-
* @returns The swap data, or null if not found
|
|
98
|
-
*/
|
|
99
|
-
async get(swapId) {
|
|
100
|
-
const record = await this.db.swaps.get(swapId);
|
|
101
|
-
if (!record) {
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
// Remove the id field before returning (it's not part of ExtendedSwapStorageData)
|
|
105
|
-
const { id: _, ...data } = record;
|
|
106
|
-
return data;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Store swap data.
|
|
110
|
-
*
|
|
111
|
-
* @param swapId - The swap ID
|
|
112
|
-
* @param data - The swap data to store
|
|
113
|
-
*/
|
|
114
|
-
async store(swapId, data) {
|
|
115
|
-
await this.db.swaps.put({ id: swapId, ...data });
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Delete swap data by swap ID.
|
|
119
|
-
*
|
|
120
|
-
* @param swapId - The swap ID
|
|
121
|
-
*/
|
|
122
|
-
async delete(swapId) {
|
|
123
|
-
await this.db.swaps.delete(swapId);
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* List all stored swap IDs.
|
|
127
|
-
*
|
|
128
|
-
* @returns Array of swap IDs
|
|
129
|
-
*/
|
|
130
|
-
async list() {
|
|
131
|
-
return (await this.db.swaps.toCollection().primaryKeys());
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Clear all swap data.
|
|
135
|
-
*/
|
|
136
|
-
async clear() {
|
|
137
|
-
await this.db.swaps.clear();
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Get all stored swaps.
|
|
141
|
-
*
|
|
142
|
-
* @returns Array of all swap data
|
|
143
|
-
*/
|
|
144
|
-
async getAll() {
|
|
145
|
-
const records = await this.db.swaps.toArray();
|
|
146
|
-
// Remove the id field from each record
|
|
147
|
-
return records.map(({ id: _, ...data }) => data);
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Close the database connection.
|
|
151
|
-
*/
|
|
152
|
-
close() {
|
|
153
|
-
this.db.close();
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Get raw swap_params for a potentially corrupted entry.
|
|
157
|
-
*
|
|
158
|
-
* This method reads the raw data from IndexedDB and extracts just the swap_params,
|
|
159
|
-
* which can succeed even when the full ExtendedSwapStorageData fails to deserialize.
|
|
160
|
-
*
|
|
161
|
-
* @param swapId - The swap ID
|
|
162
|
-
* @returns The swap_params as raw object, or null if not found or invalid
|
|
163
|
-
*/
|
|
164
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
165
|
-
async getRawSwapParams(swapId) {
|
|
166
|
-
// Use raw IndexedDB access to avoid type checking issues
|
|
167
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
168
|
-
const record = (await this.db.swaps.get(swapId));
|
|
169
|
-
if (!record || !record.swap_params) {
|
|
170
|
-
return null;
|
|
171
|
-
}
|
|
172
|
-
return record.swap_params;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Create a Dexie-based swap storage provider.
|
|
177
|
-
*
|
|
178
|
-
* This is a convenience function for creating a DexieSwapStorageProvider.
|
|
179
|
-
*
|
|
180
|
-
* @param dbName - Optional database name (default: "lendaswap")
|
|
181
|
-
* @returns A new DexieSwapStorageProvider instance
|
|
182
|
-
*
|
|
183
|
-
* @example
|
|
184
|
-
* ```typescript
|
|
185
|
-
* import { createDexieSwapStorage, Client } from '@lendasat/lendaswap-sdk';
|
|
186
|
-
*
|
|
187
|
-
* const swapStorage = createDexieSwapStorage();
|
|
188
|
-
* const client = await Client.create(
|
|
189
|
-
* 'https://apilendaswap.lendasat.com',
|
|
190
|
-
* walletStorage,
|
|
191
|
-
* swapStorage,
|
|
192
|
-
* 'bitcoin',
|
|
193
|
-
* 'https://arkade.computer'
|
|
194
|
-
* );
|
|
195
|
-
* ```
|
|
196
|
-
*/
|
|
197
|
-
export function createDexieSwapStorage(dbName) {
|
|
198
|
-
return new DexieSwapStorageProvider(dbName);
|
|
199
|
-
}
|
|
200
|
-
//# sourceMappingURL=dexieSwapStorage.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dexieSwapStorage.js","sourceRoot":"","sources":["../../src/storage/dexieSwapStorage.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAqB,MAAM,OAAO,CAAC;AAW1C;;GAEG;AACH,MAAM,iBAAkB,SAAQ,KAAK;IACnC,KAAK,CAA6B;IAElC,YAAY,MAAM,GAAG,WAAW;QAC9B,KAAK,CAAC,MAAM,CAAC,CAAC;QAEd,6BAA6B;QAC7B,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;YACrB,KAAK,EAAE,IAAI,EAAE,iDAAiD;SAC/D,CAAC,CAAC;QAEH,iDAAiD;QACjD,kFAAkF;QAClF,kFAAkF;QAClF,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;aACZ,MAAM,CAAC;YACN,KAAK,EAAE,IAAI;SACZ,CAAC;aACD,OAAO,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE;YACpB,MAAM,oBAAoB,GAAG,KAAK,CAAC;YAEnC,MAAM,EAAE;iBACL,KAAK,CAAC,OAAO,CAAC;iBACd,YAAY,EAAE;gBACf,8DAA8D;iBAC7D,MAAM,CAAC,CAAC,MAAW,EAAE,EAAE;gBACtB,MAAM,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;gBACjC,IAAI,CAAC,QAAQ,IAAI,CAAC,CAAC,iBAAiB,IAAI,QAAQ,CAAC;oBAAE,OAAO;gBAE1D,iEAAiE;gBACjE,IAAI,kBAAkB,IAAI,QAAQ;oBAAE,OAAO;gBAE3C,MAAM,WAAW,GAAG,QAAQ,CAAC,eAAe,CAAC;gBAE7C,8DAA8D;gBAC9D,MAAM,UAAU,GAAG,sBAAsB,IAAI,QAAQ,CAAC;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