@arkade-os/boltz-swap 0.1.5 → 0.2.0-alpha.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
@@ -24,8 +24,11 @@ npm install @arkade-os/sdk @arkade-os/boltz-swap
24
24
  ### Initializing the Lightning Swap Provider
25
25
 
26
26
  ```typescript
27
- import { Wallet } from '@arkade-os/sdk';
28
- import { ArkadeLightning, BoltzSwapProvider, StorageProvider } from '@arkade-os/boltz-swap';
27
+ import { Wallet, SingleKey } from '@arkade-os/sdk';
28
+ import { ArkadeLightning, BoltzSwapProvider } from '@arkade-os/boltz-swap';
29
+
30
+ // Create an identity
31
+ const identity = SingleKey.fromHex('your_private_key_in_hex');
29
32
 
30
33
  // Initialize your Arkade wallet
31
34
  const wallet = await Wallet.create({
@@ -39,37 +42,164 @@ const swapProvider = new BoltzSwapProvider({
39
42
  network: 'mutinynet',
40
43
  });
41
44
 
42
- // Optionally: initialize a storage provider
43
- const storageProvider = await StorageProvider.create();
44
-
45
45
  // Create the ArkadeLightning instance
46
46
  const arkadeLightning = new ArkadeLightning({
47
47
  wallet,
48
48
  swapProvider,
49
- storageProvider, // optional
50
49
  });
51
50
  ```
52
51
 
53
- ## Storage
52
+ ### Create your Wallet
53
+
54
+ ```typescript
55
+ import { Wallet } from '@arkade-os/sdk';
56
+
57
+ const wallet = await Wallet.create({
58
+ identity,
59
+ arkServerUrl: 'https://mutinynet.arkade.sh',
60
+ // storage defaults to in-memory
61
+ });
54
62
 
55
- By default this library doesn't store pending swaps.
63
+ // Wallet may have built-in providers
64
+ const arkadeLightning = new ArkadeLightning({
65
+ wallet,
66
+ swapProvider
67
+ });
68
+ ```
56
69
 
57
- If you need it you must initialize a storageProvider:
70
+ ### ServiceWorkerWallet with IndexDB
58
71
 
59
72
  ```typescript
60
- const storageProvider = await StorageProvider.create({ storagePath: './storage.json' });
73
+ import { ServiceWorkerWallet, SingleKey, RestArkProvider, RestIndexerProvider } from '@arkade-os/sdk';
74
+ import { IndexedDBStorageAdapter } from '@arkade-os/sdk/storage';
61
75
 
76
+ // Create your identity
77
+ const identity = SingleKey.fromHex('your_private_key_hex');
78
+ // Or generate a new one:
79
+ // const identity = SingleKey.fromRandomBytes();
80
+
81
+ // Configure IndexedDB storage adapter for ServiceWorker
82
+ const storage = new IndexedDBStorageAdapter('arkade-service-worker-wallet', 1);
83
+
84
+ const wallet = await ServiceWorkerWallet.setup({
85
+ serviceWorkerPath: '/service-worker.js',
86
+ arkServerUrl: 'https://mutinynet.arkade.sh',
87
+ identity,
88
+ storage, // Pass the IndexedDB storage adapter
89
+ });
90
+
91
+ // Must provide external providers for ServiceWorkerWallet (it doesn't have them)
62
92
  const arkadeLightning = new ArkadeLightning({
63
- wallet,
93
+ wallet: serviceWorkerWallet,
94
+ arkProvider: new RestArkProvider('https://mutinynet.arkade.sh'),
95
+ indexerProvider: new RestIndexerProvider('https://mutinynet.arkade.sh'),
64
96
  swapProvider,
65
- storageProvider,
66
97
  });
98
+ ```
67
99
 
68
- // you now are able to use the following methods
69
- const pendingPaymentsToLightning = arkadeLightning.getPendingSubmarineSwaps();
70
- const pendingPaymentsFromLightning = arkadeLightning.getPendingReverseSwaps();
100
+ **Storage Adapters**: The Arkade SDK provides various storage adapters for different environments. For ServiceWorker environments, use `IndexedDBStorageAdapter`. For more storage options and adapters, see the [Arkade SDK storage adapters documentation](https://github.com/arkade-os/ts-sdk).
101
+
102
+ ## Checking Swap Limits
103
+
104
+ Before creating Lightning invoices or sending payments, you can check the minimum and maximum swap amounts supported by the Boltz service. This is useful to validate that your invoice amount is within the acceptable range.
105
+
106
+ ```typescript
107
+ // Get current swap limits (in satoshis)
108
+ const limits = await arkadeLightning.getLimits();
109
+
110
+ if (limits) {
111
+ console.log('Minimum swap amount:', limits.min, 'sats');
112
+ console.log('Maximum swap amount:', limits.max, 'sats');
113
+
114
+ // Example: Validate invoice amount before creating
115
+ const invoiceAmount = 50000; // 50,000 sats
116
+
117
+ if (invoiceAmount < limits.min) {
118
+ console.error(`Amount ${invoiceAmount} is below minimum ${limits.min} sats`);
119
+ } else if (invoiceAmount > limits.max) {
120
+ console.error(`Amount ${invoiceAmount} is above maximum ${limits.max} sats`);
121
+ } else {
122
+ console.log('Amount is within valid range');
123
+ // Safe to proceed with creating invoice or payment
124
+ }
125
+ } else {
126
+ console.log('Unable to fetch limits - no swap provider configured');
127
+ }
128
+ ```
129
+
130
+ ### Validating Lightning Invoice Amounts
131
+
132
+ ```typescript
133
+ import { decodeInvoice } from '@arkade-os/boltz-swap';
134
+
135
+ // Decode an incoming Lightning invoice to check its amount
136
+ const invoice = 'lnbc500u1pj...'; // Lightning invoice string
137
+ const decodedInvoice = decodeInvoice(invoice);
138
+
139
+ console.log('Invoice amount:', decodedInvoice.amountSats, 'sats');
140
+
141
+ // Check if the invoice amount is within swap limits
142
+ const limits = await arkadeLightning.getLimits();
143
+
144
+ if (limits && decodedInvoice.amountSats >= limits.min && decodedInvoice.amountSats <= limits.max) {
145
+ // Amount is valid for swaps
146
+ const paymentResult = await arkadeLightning.sendLightningPayment({
147
+ invoice: invoice,
148
+ maxFeeSats: 1000,
149
+ });
150
+ console.log('Payment successful!');
151
+ } else {
152
+ console.error('Invoice amount is outside supported swap limits');
153
+ }
71
154
  ```
72
155
 
156
+ ## Checking Swap Fees
157
+
158
+ You can check the fee to pay for different swap amounts supported by the Boltz service.
159
+ This is useful to validate the user is willing to pay the fees.
160
+
161
+ ```typescript
162
+ // Get current swap fees
163
+ const fees: FeesResponse | null = await arkadeLightning.getFees();
164
+ if (!fees) throw new Error('something went wrong');
165
+
166
+ const calcSubmarineSwapFee = (satoshis: number): number => {
167
+ if (!satoshis) return 0;
168
+ const { percentage, minerFees } = fees.submarine;
169
+ return Math.ceil((satoshis * percentage) / 100 + minerFees);
170
+ };
171
+
172
+ const calcReverseSwapFee = (satoshis: number): number => {
173
+ if (!satoshis) return 0;
174
+ const { percentage, minerFees } = fees.reverse;
175
+ return Math.ceil((satoshis * percentage) / 100 + minerFees.claim + minerFees.lockup);
176
+ };
177
+ ```
178
+
179
+ ## Checking swap status
180
+
181
+ ```typescript
182
+ const response = await arkadeLightning.getSwapStatus('swap_id');
183
+ console.log('swap status = ', response.status);
184
+ ```
185
+
186
+ ## Storage
187
+
188
+ This library automatically stores pending swaps using the wallet's built-in contract repository. All swap data is persisted automatically and can be retrieved using the following methods:
189
+
190
+ ```typescript
191
+ // Get all pending submarine swaps (those waiting for Lightning payment)
192
+ const pendingPaymentsToLightning = await arkadeLightning.getPendingSubmarineSwaps();
193
+
194
+ // Get all pending reverse swaps (those waiting for claim)
195
+ const pendingPaymentsFromLightning = await arkadeLightning.getPendingReverseSwaps();
196
+
197
+ // Get complete swap history (both completed and pending)
198
+ const swapHistory = await arkadeLightning.getSwapHistory();
199
+ ```
200
+
201
+ **Note**: All swap data is automatically persisted and retrieved through the wallet's contract repository. No additional storage configuration is required.
202
+
73
203
  ## Receiving Lightning Payments
74
204
 
75
205
  To receive a Lightning payment into your Arkade wallet: