@atomiqlabs/sdk 7.0.7 → 7.0.10
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 +127 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1365,6 +1365,133 @@ if(!automaticSettlementSuccess) {
|
|
|
1365
1365
|
|
|
1366
1366
|
</details>
|
|
1367
1367
|
|
|
1368
|
+
### Exact-In Smart Chain - Lightning swaps
|
|
1369
|
+
|
|
1370
|
+
The main limitation of regular lightning network swaps (Smart chains -> Lightning), is the fact that exactIn swaps are not possible (as invoices need to have a fixed amount). LNURL-pay links solve this issue, but are not supported by all the wallets. Therefore, the SDK exposes a hook/callback that can be implemented by lightning wallets directly, which request fixed amount invoices on-demand. This then makes exact input amount swaps possible. The way it works:
|
|
1371
|
+
|
|
1372
|
+
1. SDK sends a request to the LP saying it wants to swap `x` USDC to BTC, with a dummy invoice (either 1 sat or as specified in the `minMsats` parameter - this is requested from the `getInvoice()` function) - this dummy invoice is used to estimate the routing fees by the LP (extra care must be taken for both invoices, dummy and the real one to have the same destination node public key & routing hints).
|
|
1373
|
+
2. LP responds with the output amount of `y` BTC
|
|
1374
|
+
3. SDK calls the provided `getInvoice()` callback to request the real invoice for the `y` amount of BTC (in satoshis)
|
|
1375
|
+
4. SDK forwards the returned fixed amount (`y` BTC) lightning network invoice back to the LP to finish creating the quote
|
|
1376
|
+
|
|
1377
|
+
Getting swap quote
|
|
1378
|
+
|
|
1379
|
+
```typescript
|
|
1380
|
+
//Create the swap: swapping SOL to Bitcoin lightning
|
|
1381
|
+
const swap = await swapper.swap(
|
|
1382
|
+
Tokens.SOLANA.SOL, //From specified source token
|
|
1383
|
+
Tokens.BITCOIN.BTCLN, //Swap to BTC-LN
|
|
1384
|
+
1_000_000_000n, //We can specify an amount for a lightning network payment!
|
|
1385
|
+
SwapAmountType.EXACT_IN, //We can use exactIn=true here and set an amount in input token
|
|
1386
|
+
solanaSigner.getAddress(), //Source address and smart chain signer
|
|
1387
|
+
//Instead of the destination we pass a handler object
|
|
1388
|
+
{
|
|
1389
|
+
getInvoice: async (amountSats: number, abortSignal?: AbortSignal) => {
|
|
1390
|
+
//Generate invoice with fixed amountSats here!
|
|
1391
|
+
...
|
|
1392
|
+
return invoice;
|
|
1393
|
+
},
|
|
1394
|
+
//Optionally you can also specify minimum and maximum in msats (millisatoshis, 1 sat = 1000 msats)
|
|
1395
|
+
minMsats: 1_000_000n,
|
|
1396
|
+
maxMsats: 1_000_000_000n
|
|
1397
|
+
},
|
|
1398
|
+
{
|
|
1399
|
+
comment: "Hello world" //For LNURL-pay we can also pass a comment to the recipient
|
|
1400
|
+
}
|
|
1401
|
+
);
|
|
1402
|
+
|
|
1403
|
+
//Get the amount required to pay and fee
|
|
1404
|
+
const input: string = swap.getInputWithoutFee().toString(); //Input amount excluding fees
|
|
1405
|
+
const fee: string = swap.getFee().amountInSrcToken.toString(); //Fees paid on the output
|
|
1406
|
+
const inputWithFees: string = swap.getInput().toString(); //Total amount paid including fees
|
|
1407
|
+
|
|
1408
|
+
const output: string = swap.getOutput().toString(); //Total output amount
|
|
1409
|
+
|
|
1410
|
+
//Get swap expiration time
|
|
1411
|
+
const expiry: number = swap.getQuoteExpiry(); //Expiration time of the swap quote in UNIX milliseconds, swap needs to be initiated before this time
|
|
1412
|
+
|
|
1413
|
+
//Get pricing info
|
|
1414
|
+
const swapPrice = swap.getPriceInfo().swapPrice; //Price of the current swap (excluding fees)
|
|
1415
|
+
const marketPrice = swap.getPriceInfo().marketPrice; //Current market price
|
|
1416
|
+
const difference = swap.getPriceInfo().difference; //Difference between the swap price & current market price
|
|
1417
|
+
```
|
|
1418
|
+
|
|
1419
|
+
|
|
1420
|
+
Executing the swap (simple)
|
|
1421
|
+
|
|
1422
|
+
```typescript
|
|
1423
|
+
const swapSuccessful = await swap.execute(
|
|
1424
|
+
solanaSigner,
|
|
1425
|
+
{ //Callbacks
|
|
1426
|
+
onSourceTransactionSent: (txId: string) => {
|
|
1427
|
+
//Transaction on the source chain was sent
|
|
1428
|
+
},
|
|
1429
|
+
onSourceTransactionConfirmed: (txId: string) => {
|
|
1430
|
+
//Transaction on the source chain was confirmed
|
|
1431
|
+
},
|
|
1432
|
+
onSwapSettled: (destinationTxId: string) => {
|
|
1433
|
+
//Lightning payment on the destination chain was sent and swap settled
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
);
|
|
1437
|
+
|
|
1438
|
+
//Refund in case of failure
|
|
1439
|
+
if(!swapSuccessful) {
|
|
1440
|
+
//Swap failed, money can be refunded
|
|
1441
|
+
await swap.refund(solanaSigner);
|
|
1442
|
+
return;
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
//Swap successful!
|
|
1446
|
+
const lightningSecret = swap.getSecret();
|
|
1447
|
+
```
|
|
1448
|
+
|
|
1449
|
+
<details>
|
|
1450
|
+
<summary>Manual swap execution (advanced)</summary>
|
|
1451
|
+
|
|
1452
|
+
- __1.__ Initiate the swap on the smart-chain side
|
|
1453
|
+
|
|
1454
|
+
- __a.__ Commit with a signer
|
|
1455
|
+
```typescript
|
|
1456
|
+
await swap.commit(solanaSigner);
|
|
1457
|
+
```
|
|
1458
|
+
|
|
1459
|
+
- __b.__ Or get the transactions & [sign and send transaction manually](#manually-signing-smart-chain-transactions)
|
|
1460
|
+
```typescript
|
|
1461
|
+
const txsCommit = await swap.txsCommit();
|
|
1462
|
+
//Sign and send these...
|
|
1463
|
+
...
|
|
1464
|
+
//Important to wait till SDK processes the swap initialization
|
|
1465
|
+
await swap.waitTillCommited();
|
|
1466
|
+
```
|
|
1467
|
+
|
|
1468
|
+
- __2.__ Wait for the swap to execute and for the payment to be sent
|
|
1469
|
+
|
|
1470
|
+
```typescript
|
|
1471
|
+
const swapSuccessful = await swap.waitForPayment();
|
|
1472
|
+
```
|
|
1473
|
+
|
|
1474
|
+
- __3.__ In case the swap fails we can refund our funds on the source chain
|
|
1475
|
+
|
|
1476
|
+
- __a.__ Refund with a signer
|
|
1477
|
+
```typescript
|
|
1478
|
+
if(!swapSuccessful) {
|
|
1479
|
+
await swap.refund(solanaSigner);
|
|
1480
|
+
return;
|
|
1481
|
+
}
|
|
1482
|
+
```
|
|
1483
|
+
|
|
1484
|
+
- __b.__ Or get the transactions & [sign and send transaction manually](#manually-signing-smart-chain-transactions)
|
|
1485
|
+
```typescript
|
|
1486
|
+
if(!swapSuccessful) {
|
|
1487
|
+
const txsRefund = await swap.txsRefund();
|
|
1488
|
+
//Sign and send these...
|
|
1489
|
+
...
|
|
1490
|
+
}
|
|
1491
|
+
```
|
|
1492
|
+
|
|
1493
|
+
</details>
|
|
1494
|
+
|
|
1368
1495
|
### Getting state of the swap
|
|
1369
1496
|
|
|
1370
1497
|
You can get the current state of the swap with:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atomiqlabs/sdk",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.10",
|
|
4
4
|
"description": "atomiq labs SDK for cross-chain swaps between smart chains and bitcoin",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types:": "./dist/index.d.ts",
|
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
"author": "adambor",
|
|
22
22
|
"license": "ISC",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@atomiqlabs/base": "^12.0.
|
|
25
|
-
"@atomiqlabs/messenger-nostr": "^1.0.
|
|
26
|
-
"@atomiqlabs/sdk-lib": "^16.
|
|
24
|
+
"@atomiqlabs/base": "^12.0.4",
|
|
25
|
+
"@atomiqlabs/messenger-nostr": "^1.0.1",
|
|
26
|
+
"@atomiqlabs/sdk-lib": "^16.2.7"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/node": "22.13.5",
|