@atomiqlabs/sdk 5.0.0-dev.1 → 5.0.0-dev.3
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 +150 -6
- package/dist/SwapperFactory.d.ts +2 -1
- package/dist/SwapperFactory.js +6 -1
- package/package.json +4 -3
- package/src/SwapperFactory.ts +9 -1
package/README.md
CHANGED
|
@@ -30,8 +30,10 @@ npm install @atomiqlabs/chain-evm@dev
|
|
|
30
30
|
- [BTC L1 -> Starknet/EVM (New swap protocol)](#swap-bitcoin-on-chain---starknetevm)
|
|
31
31
|
- [Smart Chain -> BTC Lightning network L2](#swap-smart-chain---bitcoin-lightning-network)
|
|
32
32
|
- [Smart Chain -> BTC Lightning network L2 (LNURL-pay)](#swap-smart-chain---bitcoin-lightning-network-1)
|
|
33
|
-
- [BTC Lightning network L2 ->
|
|
34
|
-
- [BTC Lightning network L2 (
|
|
33
|
+
- [BTC Lightning network L2 -> Solana (Old swap protocol)](#swap-bitcoin-lightning-network---solana)
|
|
34
|
+
- [BTC Lightning network L2 -> Starknet/EVM (New swap protocol)](#swap-bitcoin-lightning-network---starknetevm)
|
|
35
|
+
- [BTC Lightning network L2 (LNURL-withdraw) -> Solana (Old swap protocol)](#swap-bitcoin-lightning-network---solana-1)
|
|
36
|
+
- [BTC Lightning network L2 (LNURL-withdraw) -> Starknet/EVM (New swap protocol)](#swap-bitcoin-lightning-network---starknetevm-1)
|
|
35
37
|
- [Swap states](#getting-state-of-the-swap)
|
|
36
38
|
- [Swap size limits](#swap-size-limits)
|
|
37
39
|
- [Stored swaps](#stored-swaps)
|
|
@@ -587,7 +589,9 @@ if(!result) {
|
|
|
587
589
|
- ToBTCSwapState.REFUNDABLE = 4
|
|
588
590
|
- Swap was initiated but counterparty failed to process it, the user can now refund his funds
|
|
589
591
|
|
|
590
|
-
#### Swap Bitcoin lightning network ->
|
|
592
|
+
#### Swap Bitcoin lightning network -> Solana
|
|
593
|
+
|
|
594
|
+
NOTE: Solana uses an old swap protocol for Bitcoin lightning network -> Solana swaps, the flow here is different from the one for Starknet and other chains.
|
|
591
595
|
|
|
592
596
|
Getting swap quote
|
|
593
597
|
|
|
@@ -597,7 +601,7 @@ const _amount = 10000n; //Amount in BTC base units - sats
|
|
|
597
601
|
|
|
598
602
|
const swap = await swapper.swap(
|
|
599
603
|
Tokens.BITCOIN.BTCLN, //Swap from BTC-LN
|
|
600
|
-
Tokens.
|
|
604
|
+
Tokens.SOLANA.SOL, //Into specified destination token
|
|
601
605
|
_amount,
|
|
602
606
|
_exactIn, //Whether we define an input or output amount
|
|
603
607
|
undefined, //Source address for the swap, not used for swaps from BTC-LN
|
|
@@ -680,6 +684,88 @@ or [sign and send transactions manually](#manually-signing-smart-chain-transacti
|
|
|
680
684
|
- FromBTCLNSwapState.CLAIM_CLAIMED = 3
|
|
681
685
|
- Funds were successfully claimed & lightning network secret pre-image revealed, so the lightning network payment will settle now
|
|
682
686
|
|
|
687
|
+
|
|
688
|
+
#### Swap Bitcoin lightning network -> Starknet/EVM
|
|
689
|
+
|
|
690
|
+
Getting swap quote
|
|
691
|
+
|
|
692
|
+
```typescript
|
|
693
|
+
const _exactIn = true; //exactIn = true, so we specify the input amount
|
|
694
|
+
const _amount = 10000n; //Amount in BTC base units - sats
|
|
695
|
+
|
|
696
|
+
const swap = await swapper.swap(
|
|
697
|
+
Tokens.BITCOIN.BTCLN, //Swap from BTC-LN
|
|
698
|
+
Tokens.STARKNET.STRK, //Into specified destination token
|
|
699
|
+
_amount,
|
|
700
|
+
_exactIn, //Whether we define an input or output amount
|
|
701
|
+
undefined, //Source address for the swap, not used for swaps from BTC-LN
|
|
702
|
+
signer.getAddress(), //Destination address
|
|
703
|
+
{
|
|
704
|
+
gasAmount: 1_000_000_000_000_000_000n //We can also request a gas drop on the destination chain (here requesting 1 STRK)
|
|
705
|
+
}
|
|
706
|
+
);
|
|
707
|
+
|
|
708
|
+
//Get the bitcoin lightning network invoice (the invoice contains pre-entered amount)
|
|
709
|
+
const receivingLightningInvoice: string = swap.getAddress();
|
|
710
|
+
//Get the URI hyperlink (contains the lightning network invoice) which can be displayed also as QR code
|
|
711
|
+
const qrCodeData: string = swap.getHyperlink();
|
|
712
|
+
|
|
713
|
+
//Get the amount required to pay and fee
|
|
714
|
+
const input: string = swap.getInputWithoutFee().toString(); //Input amount excluding fees
|
|
715
|
+
const fee: string = swap.getFee().amountInSrcToken.toString(); //Fees paid on the output
|
|
716
|
+
const inputWithFees: string = swap.getInput().toString(); //Total amount paid including fees
|
|
717
|
+
|
|
718
|
+
const output: string = swap.getOutput().toString(); //Total output amount
|
|
719
|
+
|
|
720
|
+
//Get swap expiration time
|
|
721
|
+
const expiry: number = swap.getQuoteExpiry(); //Expiration time of the swap quote in UNIX milliseconds, swap needs to be initiated before this time
|
|
722
|
+
|
|
723
|
+
//Get pricing info
|
|
724
|
+
const swapPrice = swap.getPriceInfo().swapPrice; //Price of the current swap (excluding fees)
|
|
725
|
+
const marketPrice = swap.getPriceInfo().marketPrice; //Current market price
|
|
726
|
+
const difference = swap.getPriceInfo().difference; //Difference between the swap price & current market price
|
|
727
|
+
```
|
|
728
|
+
|
|
729
|
+
Pay the displayed lightning network invoice from an external lightning network wallet
|
|
730
|
+
|
|
731
|
+
Wait for the payment to be received and settle the swap.
|
|
732
|
+
|
|
733
|
+
```typescript
|
|
734
|
+
//Start listening to incoming lightning network payment
|
|
735
|
+
const success = await swap.waitForPayment();
|
|
736
|
+
if(!success) {
|
|
737
|
+
//Lightning network payment not received in time and quote expired!
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
//Swap should get automatically claimed by the watchtowers, if not we can call swap.claim() to claim the swap ourselves
|
|
742
|
+
try {
|
|
743
|
+
await swap.waitTillClaimed(timeoutSignal(30*1000));
|
|
744
|
+
} catch (e) {
|
|
745
|
+
//Claim ourselves when automatic claim doesn't happen in 30 seconds
|
|
746
|
+
await swap.claim(starknetSigner);
|
|
747
|
+
}
|
|
748
|
+
```
|
|
749
|
+
|
|
750
|
+
##### Swap states
|
|
751
|
+
|
|
752
|
+
- FromBTCLNAutoSwapState.FAILED = -4
|
|
753
|
+
- If the claiming of the funds was initiated, but never concluded, the user will get his lightning network payment refunded
|
|
754
|
+
- FromBTCLNAutoSwapState.QUOTE_EXPIRED = -3
|
|
755
|
+
- Swap quote expired and cannot be executed anymore
|
|
756
|
+
- FromBTCLNAutoSwapState.QUOTE_SOFT_EXPIRED = -2
|
|
757
|
+
- Swap quote soft-expired (i.e. the quote probably expired, but if there is already an initialization transaction sent it might still succeed)
|
|
758
|
+
- FromBTCLNAutoSwapState.EXPIRED = -1
|
|
759
|
+
- Lightning network invoice expired, meaning the swap is expired
|
|
760
|
+
- FromBTCLNAutoSwapState.PR_CREATED = 0
|
|
761
|
+
- Swap is created, the user should now pay the provided lightning network invoice
|
|
762
|
+
- FromBTCLNAutoSwapState.PR_PAID = 1
|
|
763
|
+
- Lightning network invoice payment was received (but cannot be settled by the counterparty yet)
|
|
764
|
+
- FromBTCLNAutoSwapState.CLAIM_COMMITED = 2
|
|
765
|
+
- A swap HTLC was offered by the LP to the user
|
|
766
|
+
- FromBTCLNAutoSwapState.CLAIM_CLAIMED = 3
|
|
767
|
+
- Funds were successfully claimed & lightning network secret pre-image revealed, so the lightning network payment will settle now
|
|
768
|
+
|
|
683
769
|
### LNURLs & readable lightning identifiers
|
|
684
770
|
|
|
685
771
|
LNURLs extend the lightning network functionality by creating static lightning addreses (LNURL-pay & static internet identifiers) and QR codes which allow you to pull funds from them (LNURL-withdraw)
|
|
@@ -776,7 +862,9 @@ if(!result) {
|
|
|
776
862
|
}
|
|
777
863
|
```
|
|
778
864
|
|
|
779
|
-
#### Swap Bitcoin lightning network ->
|
|
865
|
+
#### Swap Bitcoin lightning network -> Solana
|
|
866
|
+
|
|
867
|
+
NOTE: Solana uses an old swap protocol for Bitcoin lightning network -> Solana swaps, the flow here is different from the one for Starknet and other chains.
|
|
780
868
|
|
|
781
869
|
Getting swap quote
|
|
782
870
|
|
|
@@ -787,7 +875,7 @@ const _amount = 10000n; //Amount in BTC base units - sats
|
|
|
787
875
|
|
|
788
876
|
const swap = await swapper.swap(
|
|
789
877
|
Tokens.BITCOIN.BTCLN, //Swap from BTC-LN
|
|
790
|
-
Tokens.
|
|
878
|
+
Tokens.SOLANA.SOL, //Into specified destination token
|
|
791
879
|
_amount,
|
|
792
880
|
_exactIn, //Whether we define an input or output amount
|
|
793
881
|
_lnurl, //Source LNURL for the swap
|
|
@@ -844,6 +932,62 @@ try {
|
|
|
844
932
|
|
|
845
933
|
or [sign and send transactions manually](#manually-signing-smart-chain-transactions)
|
|
846
934
|
|
|
935
|
+
#### Swap Bitcoin lightning network -> Starknet/EVM
|
|
936
|
+
|
|
937
|
+
Getting swap quote
|
|
938
|
+
|
|
939
|
+
```typescript
|
|
940
|
+
const _lnurl: string = "lnurl1dp68gurn8ghj7ampd3kx2ar0veekzar0wd5xjtnrdakj7tnhv4kxctttdehhwm30d3h82unvwqhkx6rfvdjx2ctvxyesuk0a27"; //Destination LNURL-pay or readable identifier
|
|
941
|
+
const _exactIn = true; //exactIn = true, so we specify the input amount
|
|
942
|
+
const _amount = 10000n; //Amount in BTC base units - sats
|
|
943
|
+
|
|
944
|
+
const swap = await swapper.swap(
|
|
945
|
+
Tokens.BITCOIN.BTCLN, //Swap from BTC-LN
|
|
946
|
+
Tokens.STARKNET.STRK, //Into specified destination token
|
|
947
|
+
_amount,
|
|
948
|
+
_exactIn, //Whether we define an input or output amount
|
|
949
|
+
_lnurl, //Source LNURL for the swap
|
|
950
|
+
signer.getAddress(), //Destination address
|
|
951
|
+
{
|
|
952
|
+
gasAmount: 1_000_000_000_000_000_000n //We can also request a gas drop on the destination chain (here requesting 1 STRK)
|
|
953
|
+
}
|
|
954
|
+
);
|
|
955
|
+
|
|
956
|
+
//Get the amount required to pay and fee
|
|
957
|
+
const input: string = swap.getInputWithoutFee().toString(); //Input amount excluding fees
|
|
958
|
+
const fee: string = swap.getFee().amountInSrcToken.toString(); //Fees paid on the output
|
|
959
|
+
const inputWithFees: string = swap.getInput().toString(); //Total amount paid including fees
|
|
960
|
+
|
|
961
|
+
const output: string = swap.getOutput().toString(); //Total output amount
|
|
962
|
+
|
|
963
|
+
//Get swap expiration time
|
|
964
|
+
const expiry: number = swap.getQuoteExpiry(); //Expiration time of the swap quote in UNIX milliseconds, swap needs to be initiated before this time
|
|
965
|
+
|
|
966
|
+
//Get pricing info
|
|
967
|
+
const swapPrice = swap.getPriceInfo().swapPrice; //Price of the current swap (excluding fees)
|
|
968
|
+
const marketPrice = swap.getPriceInfo().marketPrice; //Current market price
|
|
969
|
+
const difference = swap.getPriceInfo().difference; //Difference between the swap price & current market price
|
|
970
|
+
```
|
|
971
|
+
|
|
972
|
+
Wait for the payment to be received & settle the swap.
|
|
973
|
+
|
|
974
|
+
```typescript
|
|
975
|
+
//Start listening to incoming lightning network payment
|
|
976
|
+
const success = await swap.waitForPayment();
|
|
977
|
+
if(!success) {
|
|
978
|
+
//Lightning network payment not received in time and quote expired!
|
|
979
|
+
return;
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
//Swap should get automatically claimed by the watchtowers, if not we can call swap.claim() to claim the swap ourselves
|
|
983
|
+
try {
|
|
984
|
+
await swap.waitTillClaimed(timeoutSignal(30*1000));
|
|
985
|
+
} catch (e) {
|
|
986
|
+
//Claim ourselves when automatic claim doesn't happen in 30 seconds
|
|
987
|
+
await swap.claim(starknetSigner);
|
|
988
|
+
}
|
|
989
|
+
```
|
|
990
|
+
|
|
847
991
|
### Getting state of the swap
|
|
848
992
|
|
|
849
993
|
You can get the current state of the swap with:
|
package/dist/SwapperFactory.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ChainData, BitcoinNetwork, BitcoinRpc, BaseTokenType, ChainType, StorageObject, IStorageManager } from "@atomiqlabs/base";
|
|
1
|
+
import { ChainData, BitcoinNetwork, BitcoinRpc, BaseTokenType, ChainType, StorageObject, IStorageManager, Messenger } from "@atomiqlabs/base";
|
|
2
2
|
import { BtcToken, CustomPriceFunction, MempoolApi, MempoolBitcoinRpc, SCToken, Swapper, SwapperOptions } from "@atomiqlabs/sdk-lib";
|
|
3
3
|
type ChainInitializer<O, C extends ChainType, T extends BaseTokenType> = {
|
|
4
4
|
chainId: ChainType["ChainId"];
|
|
@@ -33,6 +33,7 @@ export type MultichainSwapperOptions<T extends readonly ChainInitializer<any, an
|
|
|
33
33
|
chainStorageCtor?: <T extends StorageObject>(name: string) => IStorageManager<T>;
|
|
34
34
|
pricingFeeDifferencePPM?: bigint;
|
|
35
35
|
mempoolApi?: MempoolApi | MempoolBitcoinRpc | string | string[];
|
|
36
|
+
messenger?: Messenger;
|
|
36
37
|
getPriceFn?: CustomPriceFunction;
|
|
37
38
|
};
|
|
38
39
|
export declare class SwapperFactory<T extends readonly ChainInitializer<any, any, any>[]> {
|
package/dist/SwapperFactory.js
CHANGED
|
@@ -5,6 +5,7 @@ const base_1 = require("@atomiqlabs/base");
|
|
|
5
5
|
const sdk_lib_1 = require("@atomiqlabs/sdk-lib");
|
|
6
6
|
const SmartChainAssets_1 = require("./SmartChainAssets");
|
|
7
7
|
const LocalStorageManager_1 = require("./storage/LocalStorageManager");
|
|
8
|
+
const messenger_nostr_1 = require("@atomiqlabs/messenger-nostr");
|
|
8
9
|
const registries = {
|
|
9
10
|
[base_1.BitcoinNetwork.MAINNET]: "https://api.github.com/repos/adambor/SolLightning-registry/contents/registry-mainnet.json?ref=main",
|
|
10
11
|
[base_1.BitcoinNetwork.TESTNET]: "https://api.github.com/repos/adambor/SolLightning-registry/contents/registry.json?ref=main",
|
|
@@ -34,6 +35,9 @@ const mempoolUrls = {
|
|
|
34
35
|
"https://mempool.tk7.mempool.space/testnet4/api/"
|
|
35
36
|
]
|
|
36
37
|
};
|
|
38
|
+
const nostrUrls = [
|
|
39
|
+
"wss://relay.damus.io", "wss://nostr.einundzwanzig.space", "wss://nostr.mutinywallet.com"
|
|
40
|
+
];
|
|
37
41
|
class SwapperFactory {
|
|
38
42
|
constructor(initializers) {
|
|
39
43
|
this.initializers = initializers;
|
|
@@ -65,6 +69,7 @@ class SwapperFactory {
|
|
|
65
69
|
newSwapper(options) {
|
|
66
70
|
options.bitcoinNetwork ?? (options.bitcoinNetwork = base_1.BitcoinNetwork.MAINNET);
|
|
67
71
|
options.storagePrefix ?? (options.storagePrefix = "atomiqsdk-" + options.bitcoinNetwork + "-");
|
|
72
|
+
options.messenger ?? (options.messenger = new messenger_nostr_1.NostrMessenger(nostrUrls));
|
|
68
73
|
options.defaultTrustedIntermediaryUrl ?? (options.defaultTrustedIntermediaryUrl = trustedIntermediaries[options.bitcoinNetwork]);
|
|
69
74
|
options.registryUrl ?? (options.registryUrl = registries[options.bitcoinNetwork]);
|
|
70
75
|
const mempoolApi = options.mempoolApi ?? new sdk_lib_1.MempoolBitcoinRpc(mempoolUrls[options.bitcoinNetwork]);
|
|
@@ -99,7 +104,7 @@ class SwapperFactory {
|
|
|
99
104
|
};
|
|
100
105
|
}), options.getPriceFn)) :
|
|
101
106
|
sdk_lib_1.RedundantSwapPrice.createFromTokenMap(options.pricingFeeDifferencePPM ?? 10000n, pricingAssets);
|
|
102
|
-
return new sdk_lib_1.Swapper(bitcoinRpc, chains, swapPricing, pricingAssets, options);
|
|
107
|
+
return new sdk_lib_1.Swapper(bitcoinRpc, chains, swapPricing, pricingAssets, options.messenger, options);
|
|
103
108
|
}
|
|
104
109
|
}
|
|
105
110
|
exports.SwapperFactory = SwapperFactory;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atomiqlabs/sdk",
|
|
3
|
-
"version": "5.0.0-dev.
|
|
3
|
+
"version": "5.0.0-dev.3",
|
|
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,8 +21,9 @@
|
|
|
21
21
|
"author": "adambor",
|
|
22
22
|
"license": "ISC",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@atomiqlabs/base": "^10.0.0-dev.
|
|
25
|
-
"@atomiqlabs/
|
|
24
|
+
"@atomiqlabs/base": "^10.0.0-dev.9",
|
|
25
|
+
"@atomiqlabs/messenger-nostr": "^1.0.0-dev.2",
|
|
26
|
+
"@atomiqlabs/sdk-lib": "^14.0.0-dev.28"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"@types/node": "22.13.5",
|
package/src/SwapperFactory.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
BaseTokenType,
|
|
6
6
|
ChainType,
|
|
7
7
|
StorageObject,
|
|
8
|
-
IStorageManager
|
|
8
|
+
IStorageManager, Messenger
|
|
9
9
|
} from "@atomiqlabs/base";
|
|
10
10
|
import {
|
|
11
11
|
BitcoinTokens,
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
} from "@atomiqlabs/sdk-lib";
|
|
18
18
|
import {SmartChainAssets} from "./SmartChainAssets";
|
|
19
19
|
import {LocalStorageManager} from "./storage/LocalStorageManager";
|
|
20
|
+
import {NostrMessenger} from "@atomiqlabs/messenger-nostr";
|
|
20
21
|
|
|
21
22
|
type ChainInitializer<O, C extends ChainType, T extends BaseTokenType> = {
|
|
22
23
|
chainId: ChainType["ChainId"],
|
|
@@ -67,6 +68,7 @@ export type MultichainSwapperOptions<T extends readonly ChainInitializer<any, an
|
|
|
67
68
|
chainStorageCtor?: <T extends StorageObject>(name: string) => IStorageManager<T>,
|
|
68
69
|
pricingFeeDifferencePPM?: bigint,
|
|
69
70
|
mempoolApi?: MempoolApi | MempoolBitcoinRpc | string | string[],
|
|
71
|
+
messenger?: Messenger,
|
|
70
72
|
getPriceFn?: CustomPriceFunction
|
|
71
73
|
};
|
|
72
74
|
|
|
@@ -102,6 +104,10 @@ const mempoolUrls = {
|
|
|
102
104
|
]
|
|
103
105
|
}
|
|
104
106
|
|
|
107
|
+
const nostrUrls: string[] = [
|
|
108
|
+
"wss://relay.damus.io", "wss://nostr.einundzwanzig.space", "wss://nostr.mutinywallet.com"
|
|
109
|
+
];
|
|
110
|
+
|
|
105
111
|
export class SwapperFactory<T extends readonly ChainInitializer<any, any, any>[]> {
|
|
106
112
|
|
|
107
113
|
Tokens: GetAllTokens<T> & {
|
|
@@ -143,6 +149,7 @@ export class SwapperFactory<T extends readonly ChainInitializer<any, any, any>[]
|
|
|
143
149
|
newSwapper(options: MultichainSwapperOptions<T>) {
|
|
144
150
|
options.bitcoinNetwork ??= BitcoinNetwork.MAINNET as any;
|
|
145
151
|
options.storagePrefix ??= "atomiqsdk-"+options.bitcoinNetwork+"-";
|
|
152
|
+
options.messenger ??= new NostrMessenger(nostrUrls);
|
|
146
153
|
|
|
147
154
|
options.defaultTrustedIntermediaryUrl ??= trustedIntermediaries[options.bitcoinNetwork];
|
|
148
155
|
|
|
@@ -188,6 +195,7 @@ export class SwapperFactory<T extends readonly ChainInitializer<any, any, any>[]
|
|
|
188
195
|
chains as any,
|
|
189
196
|
swapPricing,
|
|
190
197
|
pricingAssets,
|
|
198
|
+
options.messenger,
|
|
191
199
|
options
|
|
192
200
|
);
|
|
193
201
|
}
|