@atomiqlabs/sdk 1.3.14 → 1.3.16

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.
@@ -1,99 +1,99 @@
1
- import {
2
- SolanaBtcRelay,
3
- SolanaChainType, SolanaFees, SolanaRetryPolicy,
4
- SolanaSwapData,
5
- SolanaSwapProgram,
6
- StoredDataAccount
7
- } from "@atomiqlabs/chain-solana";
8
- import {
9
- BitcoinNetwork,
10
- CtorChainData
11
- } from "@atomiqlabs/sdk-lib";
12
- import {BitcoinRpc, IStorageManager} from "@atomiqlabs/base";
13
- import {Connection} from "@solana/web3.js";
14
- import {SolanaChains} from "./SolanaChains";
15
- import {SolanaChainEventsBrowser} from "@atomiqlabs/chain-solana/dist/solana/events/SolanaChainEventsBrowser";
16
- import {SdkChain} from "../ChainInitializer";
17
- import {MultichainSwapperOptions} from "../../MultichainSwapper";
18
-
19
- const chainId = "SOLANA" as const;
20
-
21
- type SolanaSwapperOptions = {
22
- rpcUrl: string | Connection,
23
- dataAccountStorage?: IStorageManager<StoredDataAccount>,
24
- retryPolicy?: SolanaRetryPolicy,
25
-
26
- btcRelayContract?: string,
27
- swapContract?: string,
28
-
29
- fees?: SolanaFees
30
- };
31
-
32
- function getSolanaCtorData(options: MultichainSwapperOptions, bitcoinRpc: BitcoinRpc<any>, network: BitcoinNetwork): CtorChainData<SolanaChainType> {
33
- const connection = typeof(options.chains.SOLANA.rpcUrl)==="string" ?
34
- new Connection(options.chains.SOLANA.rpcUrl) :
35
- options.chains.SOLANA.rpcUrl;
36
-
37
- const Fees = options.chains.SOLANA.fees ?? new SolanaFees(connection, 200000, 4, 100);
38
- const btcRelay = new SolanaBtcRelay(connection, bitcoinRpc, options.chains.SOLANA.btcRelayContract ?? SolanaChains[network].addresses.btcRelayContract, Fees);
39
- const swapContract = new SolanaSwapProgram(
40
- connection,
41
- btcRelay,
42
- options.chains.SOLANA.dataAccountStorage || options.storageCtor("solAccounts"),
43
- options.chains.SOLANA.swapContract ?? SolanaChains[network].addresses.swapContract,
44
- options.chains.SOLANA.retryPolicy ?? {transactionResendInterval: 1000},
45
- Fees
46
- )
47
- const chainEvents = new SolanaChainEventsBrowser(connection, swapContract);
48
-
49
- return {
50
- btcRelay,
51
- swapContract,
52
- chainEvents,
53
- swapDataConstructor: SolanaSwapData,
54
- //These are defined here to keep the data from old SolLightning-sdk, not needed for other chains
55
- storage: {
56
- toBtc: options.storageCtor("SOLv4-"+options.bitcoinNetwork+"-Swaps-ToBTC"),
57
- toBtcLn: options.storageCtor("SOLv4-"+options.bitcoinNetwork+"-Swaps-ToBTCLN"),
58
- fromBtc: options.storageCtor("SOLv4-"+options.bitcoinNetwork+"-Swaps-FromBTC"),
59
- fromBtcLn: options.storageCtor("SOLv4-"+options.bitcoinNetwork+"-Swaps-FromBTCLN"),
60
- lnForGas: options.storageCtor("SOLv4-"+options.bitcoinNetwork+"-Swaps-LnForGas"),
61
- onchainForGas: options.storageCtor("SOLv4-"+options.bitcoinNetwork+"-Swaps-OnchainForGas")
62
- }
63
- }
64
- }
65
-
66
- const SolanaAssets = {
67
- WBTC: {
68
- address: "3NZ9JMVBmGAqocybic2c7LQCJScmgsAZ6vQqTDzcqmJh",
69
- decimals: 8
70
- },
71
- USDC: {
72
- address: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
73
- decimals: 6
74
- },
75
- USDT: {
76
- address: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB",
77
- decimals: 6
78
- },
79
- SOL: {
80
- address: "So11111111111111111111111111111111111111112",
81
- decimals: 9
82
- },
83
- BONK: {
84
- address: "DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263",
85
- decimals: 5
86
- }
87
- } as const;
88
-
89
- export type SdkSolanaType = {
90
- ChainType: SolanaChainType,
91
- Options: SolanaSwapperOptions,
92
- Assets: typeof SolanaAssets
93
- };
94
-
95
- export const SdkSolana: SdkChain<SdkSolanaType> = {
96
- getCtorData: getSolanaCtorData,
97
- assets: SolanaAssets,
98
- chainIdentifier: chainId
99
- } as const;
1
+ import {
2
+ SolanaBtcRelay,
3
+ SolanaChainType, SolanaFees, SolanaRetryPolicy,
4
+ SolanaSwapData,
5
+ SolanaSwapProgram,
6
+ StoredDataAccount
7
+ } from "@atomiqlabs/chain-solana";
8
+ import {
9
+ BitcoinNetwork,
10
+ CtorChainData
11
+ } from "@atomiqlabs/sdk-lib";
12
+ import {BitcoinRpc, IStorageManager} from "@atomiqlabs/base";
13
+ import {Connection} from "@solana/web3.js";
14
+ import {SolanaChains} from "./SolanaChains";
15
+ import {SolanaChainEventsBrowser} from "@atomiqlabs/chain-solana/dist/solana/events/SolanaChainEventsBrowser";
16
+ import {SdkChain} from "../ChainInitializer";
17
+ import {MultichainSwapperOptions} from "../../MultichainSwapper";
18
+
19
+ const chainId = "SOLANA" as const;
20
+
21
+ type SolanaSwapperOptions = {
22
+ rpcUrl: string | Connection,
23
+ dataAccountStorage?: IStorageManager<StoredDataAccount>,
24
+ retryPolicy?: SolanaRetryPolicy,
25
+
26
+ btcRelayContract?: string,
27
+ swapContract?: string,
28
+
29
+ fees?: SolanaFees
30
+ };
31
+
32
+ function getSolanaCtorData(options: MultichainSwapperOptions, bitcoinRpc: BitcoinRpc<any>, network: BitcoinNetwork): CtorChainData<SolanaChainType> {
33
+ const connection = typeof(options.chains.SOLANA.rpcUrl)==="string" ?
34
+ new Connection(options.chains.SOLANA.rpcUrl) :
35
+ options.chains.SOLANA.rpcUrl;
36
+
37
+ const Fees = options.chains.SOLANA.fees ?? new SolanaFees(connection, 200000, 4, 100);
38
+ const btcRelay = new SolanaBtcRelay(connection, bitcoinRpc, options.chains.SOLANA.btcRelayContract ?? SolanaChains[network].addresses.btcRelayContract, Fees);
39
+ const swapContract = new SolanaSwapProgram(
40
+ connection,
41
+ btcRelay,
42
+ options.chains.SOLANA.dataAccountStorage || options.storageCtor("solAccounts"),
43
+ options.chains.SOLANA.swapContract ?? SolanaChains[network].addresses.swapContract,
44
+ options.chains.SOLANA.retryPolicy ?? {transactionResendInterval: 1000},
45
+ Fees
46
+ )
47
+ const chainEvents = new SolanaChainEventsBrowser(connection, swapContract);
48
+
49
+ return {
50
+ btcRelay,
51
+ swapContract,
52
+ chainEvents,
53
+ swapDataConstructor: SolanaSwapData,
54
+ //These are defined here to keep the data from old SolLightning-sdk, not needed for other chains
55
+ storage: {
56
+ toBtc: options.storageCtor("SOLv4-"+options.bitcoinNetwork+"-Swaps-ToBTC"),
57
+ toBtcLn: options.storageCtor("SOLv4-"+options.bitcoinNetwork+"-Swaps-ToBTCLN"),
58
+ fromBtc: options.storageCtor("SOLv4-"+options.bitcoinNetwork+"-Swaps-FromBTC"),
59
+ fromBtcLn: options.storageCtor("SOLv4-"+options.bitcoinNetwork+"-Swaps-FromBTCLN"),
60
+ lnForGas: options.storageCtor("SOLv4-"+options.bitcoinNetwork+"-Swaps-LnForGas"),
61
+ onchainForGas: options.storageCtor("SOLv4-"+options.bitcoinNetwork+"-Swaps-OnchainForGas")
62
+ }
63
+ }
64
+ }
65
+
66
+ const SolanaAssets = {
67
+ WBTC: {
68
+ address: "3NZ9JMVBmGAqocybic2c7LQCJScmgsAZ6vQqTDzcqmJh",
69
+ decimals: 8
70
+ },
71
+ USDC: {
72
+ address: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
73
+ decimals: 6
74
+ },
75
+ USDT: {
76
+ address: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB",
77
+ decimals: 6
78
+ },
79
+ SOL: {
80
+ address: "So11111111111111111111111111111111111111112",
81
+ decimals: 9
82
+ },
83
+ BONK: {
84
+ address: "DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263",
85
+ decimals: 5
86
+ }
87
+ } as const;
88
+
89
+ export type SdkSolanaType = {
90
+ ChainType: SolanaChainType,
91
+ Options: SolanaSwapperOptions,
92
+ Assets: typeof SolanaAssets
93
+ };
94
+
95
+ export const SdkSolana: SdkChain<SdkSolanaType> = {
96
+ getCtorData: getSolanaCtorData,
97
+ assets: SolanaAssets,
98
+ chainIdentifier: chainId
99
+ } as const;
@@ -1,16 +1,16 @@
1
- import {BitcoinNetwork} from "@atomiqlabs/sdk-lib";
2
-
3
- export const SolanaChains = {
4
- [BitcoinNetwork.TESTNET]: {
5
- addresses: {
6
- swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM",
7
- btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc"
8
- }
9
- },
10
- [BitcoinNetwork.MAINNET]: {
11
- addresses: {
12
- swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM",
13
- btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc"
14
- }
15
- }
16
- } as const;
1
+ import {BitcoinNetwork} from "@atomiqlabs/sdk-lib";
2
+
3
+ export const SolanaChains = {
4
+ [BitcoinNetwork.TESTNET]: {
5
+ addresses: {
6
+ swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM",
7
+ btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc"
8
+ }
9
+ },
10
+ [BitcoinNetwork.MAINNET]: {
11
+ addresses: {
12
+ swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM",
13
+ btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc"
14
+ }
15
+ }
16
+ } as const;
@@ -1,120 +1,120 @@
1
- import {SolanaKeypairWallet, SolanaSigner, SolanaSwapperWithSigner, MultichainSwapper, Tokens} from "../";
2
- import {Keypair} from "@solana/web3.js";
3
- import * as BN from "bn.js";
4
-
5
- const solanaRpc = "https://api.mainnet-beta.solana.com";
6
-
7
- let solanaSwapper: SolanaSwapperWithSigner;
8
-
9
- async function setupSwapper() {
10
- //Setup the multichain swapper
11
- const swapper: MultichainSwapper = new MultichainSwapper({
12
- chains: {
13
- SOLANA: {
14
- rpcUrl: solanaRpc
15
- }
16
- }
17
- });
18
- await swapper.init();
19
-
20
- //Create new random keypair wallet
21
- const wallet = new SolanaKeypairWallet(Keypair.generate()); //This is just a dummy, you should load the wallet from file, or etc.
22
- //Or in React, using solana wallet adapter
23
- //const wallet = useAnchorWallet();
24
-
25
- const signer: SolanaSigner = new SolanaSigner(wallet);
26
- //Extract a Solana specific swapper (used for swapping between Solana and Bitcoin) with a defined signer
27
- solanaSwapper = swapper.withChain("SOLANA").withSigner(signer);
28
- }
29
-
30
- async function createToBtcSwap() {
31
- const fromToken = Tokens.SOLANA.SOL;
32
- const toToken = Tokens.BITCOIN.BTC;
33
- const exactIn = false; //exactIn = false, so we specify the output amount
34
- const amount = new BN(10000); //Amount in BTC base units - sats
35
- const recipientBtcAddress = "bc1qtw67hj77rt8zrkkg3jgngutu0yfgt9czjwusxt"; //BTC address of the recipient
36
-
37
- const swap = await solanaSwapper.create(
38
- fromToken,
39
- toToken,
40
- amount,
41
- exactIn,
42
- recipientBtcAddress
43
- );
44
-
45
- //Input amounts
46
- const inputTokenAmount = swap.getInput().amount;
47
- const inputValueInUsd = await swap.getInput().usdValue();
48
-
49
- //Output amounts
50
- const outputTokenAmount = swap.getOutput().amount;
51
- const outputValueInUsd = await swap.getOutput().usdValue();
52
-
53
- //Initiate the swap by locking up the SOL
54
- await swap.commit();
55
- //Wait for bitcoin payout to happen
56
- const paymentSuccess = await swap.waitForPayment();
57
- if(paymentSuccess) {
58
- //Payment was successful, we can get the transaction id
59
- const bitcoinTxId = swap.getBitcoinTxId();
60
- } else {
61
- //If payment is unsuccessful we can refund and get our funds back
62
- await swap.refund();
63
- }
64
- }
65
-
66
- async function createFromBtcSwap() {
67
- const fromToken = Tokens.BITCOIN.BTC;
68
- const toToken = Tokens.SOLANA.SOL;
69
- const exactIn = true; //exactIn = true, so we specify the input amount
70
- const amount = new BN(10000); //Amount in BTC base units - sats
71
-
72
- const swap = await solanaSwapper.create(
73
- fromToken,
74
- toToken,
75
- amount,
76
- exactIn
77
- );
78
-
79
- //Input amounts
80
- const inputTokenAmount = swap.getInput().amount; //Human readable input token amount with decimals
81
- const inputValueInUsd = await swap.getInput().usdValue(); //Fetches the USD value of the input
82
-
83
- //Output amounts
84
- const outputTokenAmount = swap.getOutput().amount; //Human readable output token amount with decimals
85
- const outputValueInUsd = await swap.getOutput().usdValue(); //Fetches the USD value of the output
86
-
87
- //Initiate the swap, this will prompt a Solana transaction, as we need to open the BTC swap address
88
- await swap.commit();
89
-
90
- const qrCodeData = swap.getQrData(); //Data that can be displayed as QR code - URL with the address and amount
91
- const bitcoinAddress = swap.getBitcoinAddress(); //Bitcoin address to send the BTC to - exact amount needs to be sent!
92
- const timeout = swap.getTimeoutTime(); //The BTC should be sent before the timeout
93
-
94
- console.log("Please send exactly "+inputTokenAmount+" BTC to "+bitcoinAddress);
95
-
96
- //Waits for bitcoin transaction to be received
97
- await swap.waitForBitcoinTransaction(
98
- null, null,
99
- (
100
- txId: string, //Transaction ID received
101
- confirmations: number, //Current confirmation count of the transaction
102
- targetConfirmations: number, //Required confirmations for the transaction to be accepted
103
- transactionETAms: number //Estimated time in milliseconds till the transaction is accepted
104
- ) => {
105
- //This callback receives periodic updates about the incoming transaction
106
- console.log("Tx received: "+txId+" confirmations: "+confirmations+"/"+targetConfirmations+" ETA: "+transactionETAms+" ms");
107
- }
108
- ); //This returns as soon as the transaction is accepted
109
-
110
- //Swap will get automatically claimed by the watchtowers
111
- await swap.waitTillClaimed();
112
- }
113
-
114
- async function main() {
115
- await setupSwapper();
116
- // await createToBtcSwap();
117
- // await createFromBtcSwap();
118
- }
119
-
120
- main();
1
+ import {SolanaKeypairWallet, SolanaSigner, SolanaSwapperWithSigner, MultichainSwapper, Tokens} from "../";
2
+ import {Keypair} from "@solana/web3.js";
3
+ import * as BN from "bn.js";
4
+
5
+ const solanaRpc = "https://api.mainnet-beta.solana.com";
6
+
7
+ let solanaSwapper: SolanaSwapperWithSigner;
8
+
9
+ async function setupSwapper() {
10
+ //Setup the multichain swapper
11
+ const swapper: MultichainSwapper = new MultichainSwapper({
12
+ chains: {
13
+ SOLANA: {
14
+ rpcUrl: solanaRpc
15
+ }
16
+ }
17
+ });
18
+ await swapper.init();
19
+
20
+ //Create new random keypair wallet
21
+ const wallet = new SolanaKeypairWallet(Keypair.generate()); //This is just a dummy, you should load the wallet from file, or etc.
22
+ //Or in React, using solana wallet adapter
23
+ //const wallet = useAnchorWallet();
24
+
25
+ const signer: SolanaSigner = new SolanaSigner(wallet);
26
+ //Extract a Solana specific swapper (used for swapping between Solana and Bitcoin) with a defined signer
27
+ solanaSwapper = swapper.withChain("SOLANA").withSigner(signer);
28
+ }
29
+
30
+ async function createToBtcSwap() {
31
+ const fromToken = Tokens.SOLANA.SOL;
32
+ const toToken = Tokens.BITCOIN.BTC;
33
+ const exactIn = false; //exactIn = false, so we specify the output amount
34
+ const amount = new BN(10000); //Amount in BTC base units - sats
35
+ const recipientBtcAddress = "bc1qtw67hj77rt8zrkkg3jgngutu0yfgt9czjwusxt"; //BTC address of the recipient
36
+
37
+ const swap = await solanaSwapper.create(
38
+ fromToken,
39
+ toToken,
40
+ amount,
41
+ exactIn,
42
+ recipientBtcAddress
43
+ );
44
+
45
+ //Input amounts
46
+ const inputTokenAmount = swap.getInput().amount;
47
+ const inputValueInUsd = await swap.getInput().usdValue();
48
+
49
+ //Output amounts
50
+ const outputTokenAmount = swap.getOutput().amount;
51
+ const outputValueInUsd = await swap.getOutput().usdValue();
52
+
53
+ //Initiate the swap by locking up the SOL
54
+ await swap.commit();
55
+ //Wait for bitcoin payout to happen
56
+ const paymentSuccess = await swap.waitForPayment();
57
+ if(paymentSuccess) {
58
+ //Payment was successful, we can get the transaction id
59
+ const bitcoinTxId = swap.getBitcoinTxId();
60
+ } else {
61
+ //If payment is unsuccessful we can refund and get our funds back
62
+ await swap.refund();
63
+ }
64
+ }
65
+
66
+ async function createFromBtcSwap() {
67
+ const fromToken = Tokens.BITCOIN.BTC;
68
+ const toToken = Tokens.SOLANA.SOL;
69
+ const exactIn = true; //exactIn = true, so we specify the input amount
70
+ const amount = new BN(10000); //Amount in BTC base units - sats
71
+
72
+ const swap = await solanaSwapper.create(
73
+ fromToken,
74
+ toToken,
75
+ amount,
76
+ exactIn
77
+ );
78
+
79
+ //Input amounts
80
+ const inputTokenAmount = swap.getInput().amount; //Human readable input token amount with decimals
81
+ const inputValueInUsd = await swap.getInput().usdValue(); //Fetches the USD value of the input
82
+
83
+ //Output amounts
84
+ const outputTokenAmount = swap.getOutput().amount; //Human readable output token amount with decimals
85
+ const outputValueInUsd = await swap.getOutput().usdValue(); //Fetches the USD value of the output
86
+
87
+ //Initiate the swap, this will prompt a Solana transaction, as we need to open the BTC swap address
88
+ await swap.commit();
89
+
90
+ const qrCodeData = swap.getQrData(); //Data that can be displayed as QR code - URL with the address and amount
91
+ const bitcoinAddress = swap.getBitcoinAddress(); //Bitcoin address to send the BTC to - exact amount needs to be sent!
92
+ const timeout = swap.getTimeoutTime(); //The BTC should be sent before the timeout
93
+
94
+ console.log("Please send exactly "+inputTokenAmount+" BTC to "+bitcoinAddress);
95
+
96
+ //Waits for bitcoin transaction to be received
97
+ await swap.waitForBitcoinTransaction(
98
+ null, null,
99
+ (
100
+ txId: string, //Transaction ID received
101
+ confirmations: number, //Current confirmation count of the transaction
102
+ targetConfirmations: number, //Required confirmations for the transaction to be accepted
103
+ transactionETAms: number //Estimated time in milliseconds till the transaction is accepted
104
+ ) => {
105
+ //This callback receives periodic updates about the incoming transaction
106
+ console.log("Tx received: "+txId+" confirmations: "+confirmations+"/"+targetConfirmations+" ETA: "+transactionETAms+" ms");
107
+ }
108
+ ); //This returns as soon as the transaction is accepted
109
+
110
+ //Swap will get automatically claimed by the watchtowers
111
+ await swap.waitTillClaimed();
112
+ }
113
+
114
+ async function main() {
115
+ await setupSwapper();
116
+ // await createToBtcSwap();
117
+ // await createFromBtcSwap();
118
+ }
119
+
120
+ main();