@atomiqlabs/sdk 1.3.13 → 1.3.15

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,60 +1,60 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SdkSolana = void 0;
4
- const chain_solana_1 = require("@atomiqlabs/chain-solana");
5
- const web3_js_1 = require("@solana/web3.js");
6
- const SolanaChains_1 = require("./SolanaChains");
7
- const SolanaChainEventsBrowser_1 = require("@atomiqlabs/chain-solana/dist/solana/events/SolanaChainEventsBrowser");
8
- const chainId = "SOLANA";
9
- function getSolanaCtorData(options, bitcoinRpc, network) {
10
- var _a, _b, _c, _d;
11
- const connection = typeof (options.chains.SOLANA.rpcUrl) === "string" ?
12
- new web3_js_1.Connection(options.chains.SOLANA.rpcUrl) :
13
- options.chains.SOLANA.rpcUrl;
14
- const Fees = (_a = options.chains.SOLANA.fees) !== null && _a !== void 0 ? _a : new chain_solana_1.SolanaFees(connection, 200000, 4, 100);
15
- const btcRelay = new chain_solana_1.SolanaBtcRelay(connection, bitcoinRpc, (_b = options.chains.SOLANA.btcRelayContract) !== null && _b !== void 0 ? _b : SolanaChains_1.SolanaChains[network].addresses.btcRelayContract, Fees);
16
- const swapContract = new chain_solana_1.SolanaSwapProgram(connection, btcRelay, options.chains.SOLANA.dataAccountStorage || options.storageCtor("solAccounts"), (_c = options.chains.SOLANA.swapContract) !== null && _c !== void 0 ? _c : SolanaChains_1.SolanaChains[network].addresses.swapContract, (_d = options.chains.SOLANA.retryPolicy) !== null && _d !== void 0 ? _d : { transactionResendInterval: 1000 }, Fees);
17
- const chainEvents = new SolanaChainEventsBrowser_1.SolanaChainEventsBrowser(connection, swapContract);
18
- return {
19
- btcRelay,
20
- swapContract,
21
- chainEvents,
22
- swapDataConstructor: chain_solana_1.SolanaSwapData,
23
- //These are defined here to keep the data from old SolLightning-sdk, not needed for other chains
24
- storage: {
25
- toBtc: options.storageCtor("SOLv4-" + options.bitcoinNetwork + "-Swaps-ToBTC"),
26
- toBtcLn: options.storageCtor("SOLv4-" + options.bitcoinNetwork + "-Swaps-ToBTCLN"),
27
- fromBtc: options.storageCtor("SOLv4-" + options.bitcoinNetwork + "-Swaps-FromBTC"),
28
- fromBtcLn: options.storageCtor("SOLv4-" + options.bitcoinNetwork + "-Swaps-FromBTCLN"),
29
- lnForGas: options.storageCtor("SOLv4-" + options.bitcoinNetwork + "-Swaps-LnForGas"),
30
- onchainForGas: options.storageCtor("SOLv4-" + options.bitcoinNetwork + "-Swaps-OnchainForGas")
31
- }
32
- };
33
- }
34
- const SolanaAssets = {
35
- WBTC: {
36
- address: "3NZ9JMVBmGAqocybic2c7LQCJScmgsAZ6vQqTDzcqmJh",
37
- decimals: 8
38
- },
39
- USDC: {
40
- address: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
41
- decimals: 6
42
- },
43
- USDT: {
44
- address: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB",
45
- decimals: 6
46
- },
47
- SOL: {
48
- address: "So11111111111111111111111111111111111111112",
49
- decimals: 9
50
- },
51
- BONK: {
52
- address: "DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263",
53
- decimals: 5
54
- }
55
- };
56
- exports.SdkSolana = {
57
- getCtorData: getSolanaCtorData,
58
- assets: SolanaAssets,
59
- chainIdentifier: chainId
60
- };
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SdkSolana = void 0;
4
+ const chain_solana_1 = require("@atomiqlabs/chain-solana");
5
+ const web3_js_1 = require("@solana/web3.js");
6
+ const SolanaChains_1 = require("./SolanaChains");
7
+ const SolanaChainEventsBrowser_1 = require("@atomiqlabs/chain-solana/dist/solana/events/SolanaChainEventsBrowser");
8
+ const chainId = "SOLANA";
9
+ function getSolanaCtorData(options, bitcoinRpc, network) {
10
+ var _a, _b, _c, _d;
11
+ const connection = typeof (options.chains.SOLANA.rpcUrl) === "string" ?
12
+ new web3_js_1.Connection(options.chains.SOLANA.rpcUrl) :
13
+ options.chains.SOLANA.rpcUrl;
14
+ const Fees = (_a = options.chains.SOLANA.fees) !== null && _a !== void 0 ? _a : new chain_solana_1.SolanaFees(connection, 200000, 4, 100);
15
+ const btcRelay = new chain_solana_1.SolanaBtcRelay(connection, bitcoinRpc, (_b = options.chains.SOLANA.btcRelayContract) !== null && _b !== void 0 ? _b : SolanaChains_1.SolanaChains[network].addresses.btcRelayContract, Fees);
16
+ const swapContract = new chain_solana_1.SolanaSwapProgram(connection, btcRelay, options.chains.SOLANA.dataAccountStorage || options.storageCtor("solAccounts"), (_c = options.chains.SOLANA.swapContract) !== null && _c !== void 0 ? _c : SolanaChains_1.SolanaChains[network].addresses.swapContract, (_d = options.chains.SOLANA.retryPolicy) !== null && _d !== void 0 ? _d : { transactionResendInterval: 1000 }, Fees);
17
+ const chainEvents = new SolanaChainEventsBrowser_1.SolanaChainEventsBrowser(connection, swapContract);
18
+ return {
19
+ btcRelay,
20
+ swapContract,
21
+ chainEvents,
22
+ swapDataConstructor: chain_solana_1.SolanaSwapData,
23
+ //These are defined here to keep the data from old SolLightning-sdk, not needed for other chains
24
+ storage: {
25
+ toBtc: options.storageCtor("SOLv4-" + options.bitcoinNetwork + "-Swaps-ToBTC"),
26
+ toBtcLn: options.storageCtor("SOLv4-" + options.bitcoinNetwork + "-Swaps-ToBTCLN"),
27
+ fromBtc: options.storageCtor("SOLv4-" + options.bitcoinNetwork + "-Swaps-FromBTC"),
28
+ fromBtcLn: options.storageCtor("SOLv4-" + options.bitcoinNetwork + "-Swaps-FromBTCLN"),
29
+ lnForGas: options.storageCtor("SOLv4-" + options.bitcoinNetwork + "-Swaps-LnForGas"),
30
+ onchainForGas: options.storageCtor("SOLv4-" + options.bitcoinNetwork + "-Swaps-OnchainForGas")
31
+ }
32
+ };
33
+ }
34
+ const SolanaAssets = {
35
+ WBTC: {
36
+ address: "3NZ9JMVBmGAqocybic2c7LQCJScmgsAZ6vQqTDzcqmJh",
37
+ decimals: 8
38
+ },
39
+ USDC: {
40
+ address: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
41
+ decimals: 6
42
+ },
43
+ USDT: {
44
+ address: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB",
45
+ decimals: 6
46
+ },
47
+ SOL: {
48
+ address: "So11111111111111111111111111111111111111112",
49
+ decimals: 9
50
+ },
51
+ BONK: {
52
+ address: "DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263",
53
+ decimals: 5
54
+ }
55
+ };
56
+ exports.SdkSolana = {
57
+ getCtorData: getSolanaCtorData,
58
+ assets: SolanaAssets,
59
+ chainIdentifier: chainId
60
+ };
@@ -1,14 +1,14 @@
1
- export declare const SolanaChains: {
2
- readonly 1: {
3
- readonly addresses: {
4
- readonly swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM";
5
- readonly btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc";
6
- };
7
- };
8
- readonly 0: {
9
- readonly addresses: {
10
- readonly swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM";
11
- readonly btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc";
12
- };
13
- };
14
- };
1
+ export declare const SolanaChains: {
2
+ readonly 1: {
3
+ readonly addresses: {
4
+ readonly swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM";
5
+ readonly btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc";
6
+ };
7
+ };
8
+ readonly 0: {
9
+ readonly addresses: {
10
+ readonly swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM";
11
+ readonly btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc";
12
+ };
13
+ };
14
+ };
@@ -1,18 +1,18 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SolanaChains = void 0;
4
- const sdk_lib_1 = require("@atomiqlabs/sdk-lib");
5
- exports.SolanaChains = {
6
- [sdk_lib_1.BitcoinNetwork.TESTNET]: {
7
- addresses: {
8
- swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM",
9
- btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc"
10
- }
11
- },
12
- [sdk_lib_1.BitcoinNetwork.MAINNET]: {
13
- addresses: {
14
- swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM",
15
- btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc"
16
- }
17
- }
18
- };
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SolanaChains = void 0;
4
+ const sdk_lib_1 = require("@atomiqlabs/sdk-lib");
5
+ exports.SolanaChains = {
6
+ [sdk_lib_1.BitcoinNetwork.TESTNET]: {
7
+ addresses: {
8
+ swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM",
9
+ btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc"
10
+ }
11
+ },
12
+ [sdk_lib_1.BitcoinNetwork.MAINNET]: {
13
+ addresses: {
14
+ swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM",
15
+ btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc"
16
+ }
17
+ }
18
+ };
@@ -1 +1 @@
1
- export {};
1
+ export {};
@@ -1,104 +1,104 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const __1 = require("../");
13
- const web3_js_1 = require("@solana/web3.js");
14
- const BN = require("bn.js");
15
- const solanaRpc = "https://api.mainnet-beta.solana.com";
16
- let solanaSwapper;
17
- function setupSwapper() {
18
- return __awaiter(this, void 0, void 0, function* () {
19
- //Setup the multichain swapper
20
- const swapper = new __1.MultichainSwapper({
21
- chains: {
22
- SOLANA: {
23
- rpcUrl: solanaRpc
24
- }
25
- }
26
- });
27
- yield swapper.init();
28
- //Create new random keypair wallet
29
- const wallet = new __1.SolanaKeypairWallet(web3_js_1.Keypair.generate()); //This is just a dummy, you should load the wallet from file, or etc.
30
- //Or in React, using solana wallet adapter
31
- //const wallet = useAnchorWallet();
32
- const signer = new __1.SolanaSigner(wallet);
33
- //Extract a Solana specific swapper (used for swapping between Solana and Bitcoin) with a defined signer
34
- solanaSwapper = swapper.withChain("SOLANA").withSigner(signer);
35
- });
36
- }
37
- function createToBtcSwap() {
38
- return __awaiter(this, void 0, void 0, function* () {
39
- const fromToken = __1.Tokens.SOLANA.SOL;
40
- const toToken = __1.Tokens.BITCOIN.BTC;
41
- const exactIn = false; //exactIn = false, so we specify the output amount
42
- const amount = new BN(10000); //Amount in BTC base units - sats
43
- const recipientBtcAddress = "bc1qtw67hj77rt8zrkkg3jgngutu0yfgt9czjwusxt"; //BTC address of the recipient
44
- const swap = yield solanaSwapper.create(fromToken, toToken, amount, exactIn, recipientBtcAddress);
45
- //Input amounts
46
- const inputTokenAmount = swap.getInput().amount;
47
- const inputValueInUsd = yield swap.getInput().usdValue();
48
- //Output amounts
49
- const outputTokenAmount = swap.getOutput().amount;
50
- const outputValueInUsd = yield swap.getOutput().usdValue();
51
- //Initiate the swap by locking up the SOL
52
- yield swap.commit();
53
- //Wait for bitcoin payout to happen
54
- const paymentSuccess = yield swap.waitForPayment();
55
- if (paymentSuccess) {
56
- //Payment was successful, we can get the transaction id
57
- const bitcoinTxId = swap.getBitcoinTxId();
58
- }
59
- else {
60
- //If payment is unsuccessful we can refund and get our funds back
61
- yield swap.refund();
62
- }
63
- });
64
- }
65
- function createFromBtcSwap() {
66
- return __awaiter(this, void 0, void 0, function* () {
67
- const fromToken = __1.Tokens.BITCOIN.BTC;
68
- const toToken = __1.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
- const swap = yield solanaSwapper.create(fromToken, toToken, amount, exactIn);
72
- //Input amounts
73
- const inputTokenAmount = swap.getInput().amount; //Human readable input token amount with decimals
74
- const inputValueInUsd = yield swap.getInput().usdValue(); //Fetches the USD value of the input
75
- //Output amounts
76
- const outputTokenAmount = swap.getOutput().amount; //Human readable output token amount with decimals
77
- const outputValueInUsd = yield swap.getOutput().usdValue(); //Fetches the USD value of the output
78
- //Initiate the swap, this will prompt a Solana transaction, as we need to open the BTC swap address
79
- yield swap.commit();
80
- const qrCodeData = swap.getQrData(); //Data that can be displayed as QR code - URL with the address and amount
81
- const bitcoinAddress = swap.getBitcoinAddress(); //Bitcoin address to send the BTC to - exact amount needs to be sent!
82
- const timeout = swap.getTimeoutTime(); //The BTC should be sent before the timeout
83
- console.log("Please send exactly " + inputTokenAmount + " BTC to " + bitcoinAddress);
84
- //Waits for bitcoin transaction to be received
85
- yield swap.waitForBitcoinTransaction(null, null, (txId, //Transaction ID received
86
- confirmations, //Current confirmation count of the transaction
87
- targetConfirmations, //Required confirmations for the transaction to be accepted
88
- transactionETAms //Estimated time in milliseconds till the transaction is accepted
89
- ) => {
90
- //This callback receives periodic updates about the incoming transaction
91
- console.log("Tx received: " + txId + " confirmations: " + confirmations + "/" + targetConfirmations + " ETA: " + transactionETAms + " ms");
92
- }); //This returns as soon as the transaction is accepted
93
- //Swap will get automatically claimed by the watchtowers
94
- yield swap.waitTillClaimed();
95
- });
96
- }
97
- function main() {
98
- return __awaiter(this, void 0, void 0, function* () {
99
- yield setupSwapper();
100
- // await createToBtcSwap();
101
- // await createFromBtcSwap();
102
- });
103
- }
104
- main();
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const __1 = require("../");
13
+ const web3_js_1 = require("@solana/web3.js");
14
+ const BN = require("bn.js");
15
+ const solanaRpc = "https://api.mainnet-beta.solana.com";
16
+ let solanaSwapper;
17
+ function setupSwapper() {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ //Setup the multichain swapper
20
+ const swapper = new __1.MultichainSwapper({
21
+ chains: {
22
+ SOLANA: {
23
+ rpcUrl: solanaRpc
24
+ }
25
+ }
26
+ });
27
+ yield swapper.init();
28
+ //Create new random keypair wallet
29
+ const wallet = new __1.SolanaKeypairWallet(web3_js_1.Keypair.generate()); //This is just a dummy, you should load the wallet from file, or etc.
30
+ //Or in React, using solana wallet adapter
31
+ //const wallet = useAnchorWallet();
32
+ const signer = new __1.SolanaSigner(wallet);
33
+ //Extract a Solana specific swapper (used for swapping between Solana and Bitcoin) with a defined signer
34
+ solanaSwapper = swapper.withChain("SOLANA").withSigner(signer);
35
+ });
36
+ }
37
+ function createToBtcSwap() {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ const fromToken = __1.Tokens.SOLANA.SOL;
40
+ const toToken = __1.Tokens.BITCOIN.BTC;
41
+ const exactIn = false; //exactIn = false, so we specify the output amount
42
+ const amount = new BN(10000); //Amount in BTC base units - sats
43
+ const recipientBtcAddress = "bc1qtw67hj77rt8zrkkg3jgngutu0yfgt9czjwusxt"; //BTC address of the recipient
44
+ const swap = yield solanaSwapper.create(fromToken, toToken, amount, exactIn, recipientBtcAddress);
45
+ //Input amounts
46
+ const inputTokenAmount = swap.getInput().amount;
47
+ const inputValueInUsd = yield swap.getInput().usdValue();
48
+ //Output amounts
49
+ const outputTokenAmount = swap.getOutput().amount;
50
+ const outputValueInUsd = yield swap.getOutput().usdValue();
51
+ //Initiate the swap by locking up the SOL
52
+ yield swap.commit();
53
+ //Wait for bitcoin payout to happen
54
+ const paymentSuccess = yield swap.waitForPayment();
55
+ if (paymentSuccess) {
56
+ //Payment was successful, we can get the transaction id
57
+ const bitcoinTxId = swap.getBitcoinTxId();
58
+ }
59
+ else {
60
+ //If payment is unsuccessful we can refund and get our funds back
61
+ yield swap.refund();
62
+ }
63
+ });
64
+ }
65
+ function createFromBtcSwap() {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ const fromToken = __1.Tokens.BITCOIN.BTC;
68
+ const toToken = __1.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
+ const swap = yield solanaSwapper.create(fromToken, toToken, amount, exactIn);
72
+ //Input amounts
73
+ const inputTokenAmount = swap.getInput().amount; //Human readable input token amount with decimals
74
+ const inputValueInUsd = yield swap.getInput().usdValue(); //Fetches the USD value of the input
75
+ //Output amounts
76
+ const outputTokenAmount = swap.getOutput().amount; //Human readable output token amount with decimals
77
+ const outputValueInUsd = yield swap.getOutput().usdValue(); //Fetches the USD value of the output
78
+ //Initiate the swap, this will prompt a Solana transaction, as we need to open the BTC swap address
79
+ yield swap.commit();
80
+ const qrCodeData = swap.getQrData(); //Data that can be displayed as QR code - URL with the address and amount
81
+ const bitcoinAddress = swap.getBitcoinAddress(); //Bitcoin address to send the BTC to - exact amount needs to be sent!
82
+ const timeout = swap.getTimeoutTime(); //The BTC should be sent before the timeout
83
+ console.log("Please send exactly " + inputTokenAmount + " BTC to " + bitcoinAddress);
84
+ //Waits for bitcoin transaction to be received
85
+ yield swap.waitForBitcoinTransaction(null, null, (txId, //Transaction ID received
86
+ confirmations, //Current confirmation count of the transaction
87
+ targetConfirmations, //Required confirmations for the transaction to be accepted
88
+ transactionETAms //Estimated time in milliseconds till the transaction is accepted
89
+ ) => {
90
+ //This callback receives periodic updates about the incoming transaction
91
+ console.log("Tx received: " + txId + " confirmations: " + confirmations + "/" + targetConfirmations + " ETA: " + transactionETAms + " ms");
92
+ }); //This returns as soon as the transaction is accepted
93
+ //Swap will get automatically claimed by the watchtowers
94
+ yield swap.waitTillClaimed();
95
+ });
96
+ }
97
+ function main() {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ yield setupSwapper();
100
+ // await createToBtcSwap();
101
+ // await createFromBtcSwap();
102
+ });
103
+ }
104
+ main();
@@ -1 +1 @@
1
- export {};
1
+ export {};
@@ -1,104 +1,104 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const __1 = require("../");
13
- const web3_js_1 = require("@solana/web3.js");
14
- const BN = require("bn.js");
15
- const fs_storage_1 = require("@atomiqlabs/sdk-lib/dist/fs-storage");
16
- const solanaRpc = "https://api.mainnet-beta.solana.com";
17
- let solanaSwapper;
18
- function setupSwapper() {
19
- return __awaiter(this, void 0, void 0, function* () {
20
- //Setup the multichain swapper
21
- const swapper = new __1.MultichainSwapper({
22
- chains: {
23
- SOLANA: {
24
- rpcUrl: solanaRpc
25
- }
26
- },
27
- //The following line is important for running on backend node.js,
28
- // because the SDK by default uses browser's Indexed DB
29
- storageCtor: (name) => new fs_storage_1.FileSystemStorageManager(name)
30
- });
31
- yield swapper.init();
32
- //Create new random keypair wallet
33
- const wallet = new __1.SolanaKeypairWallet(web3_js_1.Keypair.generate()); //This is just a dummy, you should load the wallet from file, or etc.
34
- const signer = new __1.SolanaSigner(wallet);
35
- //Extract a Solana specific swapper (used for swapping between Solana and Bitcoin) with a defined signer
36
- solanaSwapper = swapper.withChain("SOLANA").withSigner(signer);
37
- });
38
- }
39
- function createToBtcSwap() {
40
- return __awaiter(this, void 0, void 0, function* () {
41
- const exactIn = false; //exactIn = false, so we specify the output amount
42
- const amount = new BN(10000); //Amount in BTC base units - sats
43
- const recipientBtcAddress = "bc1qtw67hj77rt8zrkkg3jgngutu0yfgt9czjwusxt"; //BTC address of the recipient
44
- const swap = yield solanaSwapper.create(__1.Tokens.SOLANA.SOL, __1.Tokens.BITCOIN.BTC, amount, exactIn, recipientBtcAddress);
45
- //Input amounts
46
- const inputTokenAmount = swap.getInput().amount;
47
- const inputValueInUsd = yield swap.getInput().usdValue();
48
- //Output amounts
49
- const outputTokenAmount = swap.getOutput().amount;
50
- const outputValueInUsd = yield swap.getOutput().usdValue();
51
- //Initiate the swap by locking up the SOL
52
- yield swap.commit();
53
- //Wait for bitcoin payout to happen
54
- const paymentSuccess = yield swap.waitForPayment();
55
- if (paymentSuccess) {
56
- //Payment was successful, we can get the transaction id
57
- const bitcoinTxId = swap.getBitcoinTxId();
58
- }
59
- else {
60
- //If payment is unsuccessful we can refund and get our funds back
61
- yield swap.refund();
62
- }
63
- });
64
- }
65
- function createFromBtcSwap() {
66
- return __awaiter(this, void 0, void 0, function* () {
67
- const fromToken = __1.Tokens.BITCOIN.BTC;
68
- const toToken = __1.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
- const swap = yield solanaSwapper.create(fromToken, toToken, amount, exactIn);
72
- //Input amounts
73
- const inputTokenAmount = swap.getInput().amount; //Human readable input token amount with decimals
74
- const inputValueInUsd = yield swap.getInput().usdValue(); //Fetches the USD value of the input
75
- //Output amounts
76
- const outputTokenAmount = swap.getOutput().amount; //Human readable output token amount with decimals
77
- const outputValueInUsd = yield swap.getOutput().usdValue(); //Fetches the USD value of the output
78
- //Initiate the swap, this will prompt a Solana transaction, as we need to open the BTC swap address
79
- yield swap.commit();
80
- const qrCodeData = swap.getQrData(); //Data that can be displayed as QR code - URL with the address and amount
81
- const bitcoinAddress = swap.getBitcoinAddress(); //Bitcoin address to send the BTC to - exact amount needs to be sent!
82
- const timeout = swap.getTimeoutTime(); //The BTC should be sent before the timeout
83
- console.log("Please send exactly " + inputTokenAmount + " BTC to " + bitcoinAddress);
84
- //Waits for bitcoin transaction to be received
85
- yield swap.waitForBitcoinTransaction(null, null, (txId, //Transaction ID received
86
- confirmations, //Current confirmation count of the transaction
87
- targetConfirmations, //Required confirmations for the transaction to be accepted
88
- transactionETAms //Estimated time in milliseconds till the transaction is accepted
89
- ) => {
90
- //This callback receives periodic updates about the incoming transaction
91
- console.log("Tx received: " + txId + " confirmations: " + confirmations + "/" + targetConfirmations + " ETA: " + transactionETAms + " ms");
92
- }); //This returns as soon as the transaction is accepted
93
- //Swap will get automatically claimed by the watchtowers
94
- yield swap.waitTillClaimed();
95
- });
96
- }
97
- function main() {
98
- return __awaiter(this, void 0, void 0, function* () {
99
- yield setupSwapper();
100
- // await createToBtcSwap();
101
- // await createFromBtcSwap();
102
- });
103
- }
104
- main();
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const __1 = require("../");
13
+ const web3_js_1 = require("@solana/web3.js");
14
+ const BN = require("bn.js");
15
+ const fs_storage_1 = require("@atomiqlabs/sdk-lib/dist/fs-storage");
16
+ const solanaRpc = "https://api.mainnet-beta.solana.com";
17
+ let solanaSwapper;
18
+ function setupSwapper() {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ //Setup the multichain swapper
21
+ const swapper = new __1.MultichainSwapper({
22
+ chains: {
23
+ SOLANA: {
24
+ rpcUrl: solanaRpc
25
+ }
26
+ },
27
+ //The following line is important for running on backend node.js,
28
+ // because the SDK by default uses browser's Indexed DB
29
+ storageCtor: (name) => new fs_storage_1.FileSystemStorageManager(name)
30
+ });
31
+ yield swapper.init();
32
+ //Create new random keypair wallet
33
+ const wallet = new __1.SolanaKeypairWallet(web3_js_1.Keypair.generate()); //This is just a dummy, you should load the wallet from file, or etc.
34
+ const signer = new __1.SolanaSigner(wallet);
35
+ //Extract a Solana specific swapper (used for swapping between Solana and Bitcoin) with a defined signer
36
+ solanaSwapper = swapper.withChain("SOLANA").withSigner(signer);
37
+ });
38
+ }
39
+ function createToBtcSwap() {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ const exactIn = false; //exactIn = false, so we specify the output amount
42
+ const amount = new BN(10000); //Amount in BTC base units - sats
43
+ const recipientBtcAddress = "bc1qtw67hj77rt8zrkkg3jgngutu0yfgt9czjwusxt"; //BTC address of the recipient
44
+ const swap = yield solanaSwapper.create(__1.Tokens.SOLANA.SOL, __1.Tokens.BITCOIN.BTC, amount, exactIn, recipientBtcAddress);
45
+ //Input amounts
46
+ const inputTokenAmount = swap.getInput().amount;
47
+ const inputValueInUsd = yield swap.getInput().usdValue();
48
+ //Output amounts
49
+ const outputTokenAmount = swap.getOutput().amount;
50
+ const outputValueInUsd = yield swap.getOutput().usdValue();
51
+ //Initiate the swap by locking up the SOL
52
+ yield swap.commit();
53
+ //Wait for bitcoin payout to happen
54
+ const paymentSuccess = yield swap.waitForPayment();
55
+ if (paymentSuccess) {
56
+ //Payment was successful, we can get the transaction id
57
+ const bitcoinTxId = swap.getBitcoinTxId();
58
+ }
59
+ else {
60
+ //If payment is unsuccessful we can refund and get our funds back
61
+ yield swap.refund();
62
+ }
63
+ });
64
+ }
65
+ function createFromBtcSwap() {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ const fromToken = __1.Tokens.BITCOIN.BTC;
68
+ const toToken = __1.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
+ const swap = yield solanaSwapper.create(fromToken, toToken, amount, exactIn);
72
+ //Input amounts
73
+ const inputTokenAmount = swap.getInput().amount; //Human readable input token amount with decimals
74
+ const inputValueInUsd = yield swap.getInput().usdValue(); //Fetches the USD value of the input
75
+ //Output amounts
76
+ const outputTokenAmount = swap.getOutput().amount; //Human readable output token amount with decimals
77
+ const outputValueInUsd = yield swap.getOutput().usdValue(); //Fetches the USD value of the output
78
+ //Initiate the swap, this will prompt a Solana transaction, as we need to open the BTC swap address
79
+ yield swap.commit();
80
+ const qrCodeData = swap.getQrData(); //Data that can be displayed as QR code - URL with the address and amount
81
+ const bitcoinAddress = swap.getBitcoinAddress(); //Bitcoin address to send the BTC to - exact amount needs to be sent!
82
+ const timeout = swap.getTimeoutTime(); //The BTC should be sent before the timeout
83
+ console.log("Please send exactly " + inputTokenAmount + " BTC to " + bitcoinAddress);
84
+ //Waits for bitcoin transaction to be received
85
+ yield swap.waitForBitcoinTransaction(null, null, (txId, //Transaction ID received
86
+ confirmations, //Current confirmation count of the transaction
87
+ targetConfirmations, //Required confirmations for the transaction to be accepted
88
+ transactionETAms //Estimated time in milliseconds till the transaction is accepted
89
+ ) => {
90
+ //This callback receives periodic updates about the incoming transaction
91
+ console.log("Tx received: " + txId + " confirmations: " + confirmations + "/" + targetConfirmations + " ETA: " + transactionETAms + " ms");
92
+ }); //This returns as soon as the transaction is accepted
93
+ //Swap will get automatically claimed by the watchtowers
94
+ yield swap.waitTillClaimed();
95
+ });
96
+ }
97
+ function main() {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ yield setupSwapper();
100
+ // await createToBtcSwap();
101
+ // await createFromBtcSwap();
102
+ });
103
+ }
104
+ main();
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from "./MultichainSwapper";
2
- export * from "@atomiqlabs/base";
3
- export * from "@atomiqlabs/sdk-lib";
4
- export * from "@atomiqlabs/chain-solana";
1
+ export * from "./MultichainSwapper";
2
+ export * from "@atomiqlabs/base";
3
+ export * from "@atomiqlabs/sdk-lib";
4
+ export * from "@atomiqlabs/chain-solana";