@atomiqlabs/sdk 1.3.19 → 2.1.0-beta.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.
Files changed (49) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +545 -501
  3. package/dist/SmartChainAssets.d.ts +73 -59
  4. package/dist/SmartChainAssets.js +75 -55
  5. package/dist/SwapperFactory.d.ts +49 -0
  6. package/dist/SwapperFactory.js +84 -0
  7. package/dist/Utils.d.ts +11 -0
  8. package/dist/Utils.js +37 -0
  9. package/dist/fs-storage/FileSystemStorageManager.d.ts +15 -0
  10. package/dist/fs-storage/FileSystemStorageManager.js +60 -0
  11. package/dist/fs-storage/index.d.ts +1 -0
  12. package/dist/fs-storage/index.js +17 -0
  13. package/dist/index.d.ts +5 -4
  14. package/dist/index.js +21 -20
  15. package/dist/storage/LocalStorageManager.d.ts +24 -0
  16. package/dist/storage/LocalStorageManager.js +68 -0
  17. package/package.json +31 -32
  18. package/src/SmartChainAssets.js +75 -0
  19. package/src/SmartChainAssets.ts +76 -62
  20. package/src/SwapperFactory.js +96 -0
  21. package/src/SwapperFactory.ts +171 -0
  22. package/src/Utils.js +37 -0
  23. package/src/Utils.ts +31 -0
  24. package/src/example/BrowserExample.js +199 -0
  25. package/src/example/BrowserExample.ts +141 -120
  26. package/src/example/NodeJSExample.js +206 -0
  27. package/src/example/NodeJSExample.ts +150 -120
  28. package/src/fs-storage/FileSystemStorageManager.ts +71 -0
  29. package/src/fs-storage/index.ts +1 -0
  30. package/src/index.js +21 -0
  31. package/src/index.ts +5 -4
  32. package/src/storage/LocalStorageManager.js +72 -0
  33. package/src/storage/LocalStorageManager.ts +81 -0
  34. package/dist/MultichainSwapper.d.ts +0 -46
  35. package/dist/MultichainSwapper.js +0 -83
  36. package/dist/chains/ChainInitializer.d.ts +0 -13
  37. package/dist/chains/ChainInitializer.js +0 -2
  38. package/dist/chains/solana/SolanaChainInitializer.d.ts +0 -41
  39. package/dist/chains/solana/SolanaChainInitializer.js +0 -60
  40. package/dist/chains/solana/SolanaChains.d.ts +0 -14
  41. package/dist/chains/solana/SolanaChains.js +0 -18
  42. package/dist/example/BrowserExample.d.ts +0 -1
  43. package/dist/example/BrowserExample.js +0 -104
  44. package/dist/example/NodeJSExample.d.ts +0 -1
  45. package/dist/example/NodeJSExample.js +0 -104
  46. package/src/MultichainSwapper.ts +0 -156
  47. package/src/chains/ChainInitializer.ts +0 -16
  48. package/src/chains/solana/SolanaChainInitializer.ts +0 -99
  49. package/src/chains/solana/SolanaChains.ts +0 -16
package/src/Utils.ts ADDED
@@ -0,0 +1,31 @@
1
+ import {toDecimal, fromDecimal, Token} from "@atomiqlabs/sdk-lib";
2
+
3
+ export function toHumanReadableString(amount: bigint, currencySpec: Token): string {
4
+ if(amount==null) return null;
5
+ return toDecimal(amount, currencySpec.decimals, undefined, currencySpec.displayDecimals);
6
+ }
7
+
8
+ export function fromHumanReadableString(amount: string, currencySpec: Token): bigint {
9
+ if(amount==="" || amount==null) return null;
10
+ return fromDecimal(amount, currencySpec.decimals);
11
+ }
12
+
13
+ /**
14
+ * Returns an abort signal that aborts after a specified timeout in milliseconds
15
+ *
16
+ * @param timeout Milliseconds to wait
17
+ * @param abortReason Abort with this abort reason
18
+ * @param abortSignal Abort signal to extend
19
+ */
20
+ export function timeoutSignal(timeout: number, abortReason?: any, abortSignal?: AbortSignal): AbortSignal {
21
+ if(timeout==null) return abortSignal;
22
+ const abortController = new AbortController();
23
+ const timeoutHandle = setTimeout(() => abortController.abort(abortReason || new Error("Timed out")), timeout);
24
+ if(abortSignal!=null) {
25
+ abortSignal.addEventListener("abort", () => {
26
+ clearTimeout(timeoutHandle);
27
+ abortController.abort(abortSignal.reason);
28
+ });
29
+ }
30
+ return abortController.signal;
31
+ }
@@ -0,0 +1,199 @@
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
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ var __1 = require("../");
40
+ var chain_starknet_1 = require("@atomiqlabs/chain-starknet");
41
+ var chain_solana_1 = require("@atomiqlabs/chain-solana");
42
+ var solanaRpc = "https://api.mainnet-beta.solana.com";
43
+ var starknetRpc = "https://starknet-mainnet.public.blastapi.io/rpc/v0_7";
44
+ //We initialize a swapper factory, this is important such that we can pick and choose which chains
45
+ // we want to support and only install those specific libraries, here Solana & Starknet are used
46
+ //NOTE: The "as const" keyword is important here as to let typescript properly infer the
47
+ // generic type of the SwapperFactory, allowing you to have code-completion for Tokens and TokenResolver
48
+ var Factory = new __1.SwapperFactory([chain_solana_1.SolanaInitializer, chain_starknet_1.StarknetInitializer]);
49
+ var Tokens = Factory.Tokens;
50
+ function setupSwapper() {
51
+ return __awaiter(this, void 0, void 0, function () {
52
+ var swapper, solanaSwapper, signer;
53
+ return __generator(this, function (_a) {
54
+ switch (_a.label) {
55
+ case 0:
56
+ swapper = Factory.newSwapper({
57
+ chains: {
58
+ SOLANA: {
59
+ rpcUrl: solanaRpc
60
+ },
61
+ STARKNET: {
62
+ rpcUrl: starknetRpc
63
+ }
64
+ }
65
+ });
66
+ //Initialize the swapper
67
+ return [4 /*yield*/, swapper.init()];
68
+ case 1:
69
+ //Initialize the swapper
70
+ _a.sent();
71
+ solanaSwapper = swapper.withChain("SOLANA");
72
+ signer = solanaSwapper.randomSigner();
73
+ //Or in React, using solana wallet adapter
74
+ //const signer = new SolanaKeypairWallet(useAnchorWallet());
75
+ //Extract a swapper with a defined signer
76
+ return [2 /*return*/, solanaSwapper.withSigner(signer)];
77
+ }
78
+ });
79
+ });
80
+ }
81
+ function createToBtcSwap() {
82
+ return __awaiter(this, void 0, void 0, function () {
83
+ var solanaSwapper, fromToken, toToken, exactIn, amount, recipientBtcAddress, swap, inputTokenAmount, inputValueInUsd, outputTokenAmount, outputValueInUsd, paymentSuccess, bitcoinTxId;
84
+ return __generator(this, function (_a) {
85
+ switch (_a.label) {
86
+ case 0: return [4 /*yield*/, setupSwapper()];
87
+ case 1:
88
+ solanaSwapper = _a.sent();
89
+ fromToken = Tokens.SOLANA.SOL;
90
+ toToken = Tokens.BITCOIN.BTC;
91
+ exactIn = false;
92
+ amount = (0, __1.fromHumanReadableString)("0.0001", toToken);
93
+ recipientBtcAddress = "bc1qtw67hj77rt8zrkkg3jgngutu0yfgt9czjwusxt";
94
+ return [4 /*yield*/, solanaSwapper.create(fromToken, toToken, amount, exactIn, recipientBtcAddress)];
95
+ case 2:
96
+ swap = _a.sent();
97
+ inputTokenAmount = swap.getInput().amount;
98
+ return [4 /*yield*/, swap.getInput().usdValue()];
99
+ case 3:
100
+ inputValueInUsd = _a.sent();
101
+ outputTokenAmount = swap.getOutput().amount;
102
+ return [4 /*yield*/, swap.getOutput().usdValue()];
103
+ case 4:
104
+ outputValueInUsd = _a.sent();
105
+ //Initiate the swap by locking up the SOL
106
+ return [4 /*yield*/, swap.commit()];
107
+ case 5:
108
+ //Initiate the swap by locking up the SOL
109
+ _a.sent();
110
+ return [4 /*yield*/, swap.waitForPayment()];
111
+ case 6:
112
+ paymentSuccess = _a.sent();
113
+ if (!paymentSuccess) return [3 /*break*/, 7];
114
+ bitcoinTxId = swap.getBitcoinTxId();
115
+ return [3 /*break*/, 9];
116
+ case 7:
117
+ //If payment is unsuccessful we can refund and get our funds back
118
+ return [4 /*yield*/, swap.refund()];
119
+ case 8:
120
+ //If payment is unsuccessful we can refund and get our funds back
121
+ _a.sent();
122
+ _a.label = 9;
123
+ case 9: return [2 /*return*/];
124
+ }
125
+ });
126
+ });
127
+ }
128
+ function createFromBtcSwap() {
129
+ return __awaiter(this, void 0, void 0, function () {
130
+ var solanaSwapper, fromToken, toToken, exactIn, amount, swap, inputTokenAmount, inputValueInUsd, outputTokenAmount, outputValueInUsd, qrCodeData, bitcoinAddress, timeout, e_1;
131
+ return __generator(this, function (_a) {
132
+ switch (_a.label) {
133
+ case 0: return [4 /*yield*/, setupSwapper()];
134
+ case 1:
135
+ solanaSwapper = _a.sent();
136
+ fromToken = Tokens.BITCOIN.BTC;
137
+ toToken = Tokens.SOLANA.SOL;
138
+ exactIn = true;
139
+ amount = BigInt(10000);
140
+ return [4 /*yield*/, solanaSwapper.create(fromToken, toToken, amount, exactIn)];
141
+ case 2:
142
+ swap = _a.sent();
143
+ inputTokenAmount = swap.getInput().amount;
144
+ return [4 /*yield*/, swap.getInput().usdValue()];
145
+ case 3:
146
+ inputValueInUsd = _a.sent();
147
+ outputTokenAmount = swap.getOutput().amount;
148
+ return [4 /*yield*/, swap.getOutput().usdValue()];
149
+ case 4:
150
+ outputValueInUsd = _a.sent();
151
+ //Initiate the swap, this will prompt a Solana transaction, as we need to open the BTC swap address
152
+ return [4 /*yield*/, swap.commit()];
153
+ case 5:
154
+ //Initiate the swap, this will prompt a Solana transaction, as we need to open the BTC swap address
155
+ _a.sent();
156
+ qrCodeData = swap.getQrData();
157
+ bitcoinAddress = swap.getBitcoinAddress();
158
+ timeout = swap.getTimeoutTime();
159
+ console.log("Please send exactly " + inputTokenAmount + " BTC to " + bitcoinAddress);
160
+ //Waits for bitcoin transaction to be received
161
+ return [4 /*yield*/, swap.waitForBitcoinTransaction(null, null, function (txId, //Transaction ID received
162
+ confirmations, //Current confirmation count of the transaction
163
+ targetConfirmations, //Required confirmations for the transaction to be accepted
164
+ transactionETAms //Estimated time in milliseconds till the transaction is accepted
165
+ ) {
166
+ //This callback receives periodic updates about the incoming transaction
167
+ console.log("Tx received: " + txId + " confirmations: " + confirmations + "/" + targetConfirmations + " ETA: " + transactionETAms + " ms");
168
+ })];
169
+ case 6:
170
+ //Waits for bitcoin transaction to be received
171
+ _a.sent(); //This returns as soon as the transaction is accepted
172
+ _a.label = 7;
173
+ case 7:
174
+ _a.trys.push([7, 9, , 11]);
175
+ return [4 /*yield*/, swap.waitTillClaimed((0, __1.timeoutSignal)(30 * 1000))];
176
+ case 8:
177
+ _a.sent();
178
+ return [3 /*break*/, 11];
179
+ case 9:
180
+ e_1 = _a.sent();
181
+ //Claim ourselves when automatic claim doesn't happen in 30 seconds
182
+ return [4 /*yield*/, swap.claim()];
183
+ case 10:
184
+ //Claim ourselves when automatic claim doesn't happen in 30 seconds
185
+ _a.sent();
186
+ return [3 /*break*/, 11];
187
+ case 11: return [2 /*return*/];
188
+ }
189
+ });
190
+ });
191
+ }
192
+ function main() {
193
+ return __awaiter(this, void 0, void 0, function () {
194
+ return __generator(this, function (_a) {
195
+ return [2 /*return*/];
196
+ });
197
+ });
198
+ }
199
+ main();
@@ -1,120 +1,141 @@
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 {fromHumanReadableString, SwapperFactory, timeoutSignal} from "../";
2
+ import {StarknetInitializer, StarknetInitializerType} from "@atomiqlabs/chain-starknet";
3
+ import {SolanaInitializer, SolanaInitializerType} from "@atomiqlabs/chain-solana";
4
+
5
+ const solanaRpc = "https://api.mainnet-beta.solana.com";
6
+ const starknetRpc = "https://starknet-mainnet.public.blastapi.io/rpc/v0_7";
7
+
8
+ //We initialize a swapper factory, this is important such that we can pick and choose which chains
9
+ // we want to support and only install those specific libraries, here Solana & Starknet are used
10
+ //NOTE: The "as const" keyword is important here as to let typescript properly infer the
11
+ // generic type of the SwapperFactory, allowing you to have code-completion for Tokens and TokenResolver
12
+ const Factory = new SwapperFactory<[SolanaInitializerType, StarknetInitializerType]>([SolanaInitializer, StarknetInitializer] as const);
13
+ const Tokens = Factory.Tokens;
14
+
15
+ async function setupSwapper() {
16
+ //Setup the multichain swapper
17
+ const swapper = Factory.newSwapper({
18
+ chains: {
19
+ SOLANA: {
20
+ rpcUrl: solanaRpc
21
+ },
22
+ STARKNET: {
23
+ rpcUrl: starknetRpc
24
+ }
25
+ }
26
+ });
27
+ //Initialize the swapper
28
+ await swapper.init();
29
+
30
+ //Extract a Solana specific swapper (used for swapping between Solana and Bitcoin)
31
+ const solanaSwapper = swapper.withChain<"SOLANA">("SOLANA");
32
+
33
+ //Create new random keypair wallet
34
+ const signer = solanaSwapper.randomSigner(); //This is just a dummy, you should load the wallet from file, or etc.
35
+ //Or in React, using solana wallet adapter
36
+ //const signer = new SolanaKeypairWallet(useAnchorWallet());
37
+
38
+ //Extract a swapper with a defined signer
39
+ return solanaSwapper.withSigner(signer);
40
+ }
41
+
42
+ async function createToBtcSwap() {
43
+ //In real use-cases you would setup the swapper just once, not for every swap!
44
+ const solanaSwapper = await setupSwapper();
45
+
46
+ const fromToken = Tokens.SOLANA.SOL;
47
+ const toToken = Tokens.BITCOIN.BTC;
48
+ const exactIn = false; //exactIn = false, so we specify the output amount
49
+ const amount = fromHumanReadableString("0.0001", toToken); //Amount in BTC base units - sats, you can also use fromHumanReadable helper methods
50
+ const recipientBtcAddress = "bc1qtw67hj77rt8zrkkg3jgngutu0yfgt9czjwusxt"; //BTC address of the recipient
51
+
52
+ const swap = await solanaSwapper.create(
53
+ fromToken,
54
+ toToken,
55
+ amount,
56
+ exactIn,
57
+ recipientBtcAddress
58
+ );
59
+
60
+ //Input amounts
61
+ const inputTokenAmount = swap.getInput().amount;
62
+ const inputValueInUsd = await swap.getInput().usdValue();
63
+
64
+ //Output amounts
65
+ const outputTokenAmount = swap.getOutput().amount;
66
+ const outputValueInUsd = await swap.getOutput().usdValue();
67
+
68
+ //Initiate the swap by locking up the SOL
69
+ await swap.commit();
70
+ //Wait for bitcoin payout to happen
71
+ const paymentSuccess = await swap.waitForPayment();
72
+ if(paymentSuccess) {
73
+ //Payment was successful, we can get the transaction id
74
+ const bitcoinTxId = swap.getBitcoinTxId();
75
+ } else {
76
+ //If payment is unsuccessful we can refund and get our funds back
77
+ await swap.refund();
78
+ }
79
+ }
80
+
81
+ async function createFromBtcSwap() {
82
+ const solanaSwapper = await setupSwapper();
83
+
84
+ const fromToken = Tokens.BITCOIN.BTC;
85
+ const toToken = Tokens.SOLANA.SOL;
86
+ const exactIn = true; //exactIn = true, so we specify the input amount
87
+ const amount = BigInt(10000); //Amount in BTC base units - sats
88
+
89
+ const swap = await solanaSwapper.create(
90
+ fromToken,
91
+ toToken,
92
+ amount,
93
+ exactIn
94
+ );
95
+
96
+ //Input amounts
97
+ const inputTokenAmount = swap.getInput().amount; //Human readable input token amount with decimals
98
+ const inputValueInUsd = await swap.getInput().usdValue(); //Fetches the USD value of the input
99
+
100
+ //Output amounts
101
+ const outputTokenAmount = swap.getOutput().amount; //Human readable output token amount with decimals
102
+ const outputValueInUsd = await swap.getOutput().usdValue(); //Fetches the USD value of the output
103
+
104
+ //Initiate the swap, this will prompt a Solana transaction, as we need to open the BTC swap address
105
+ await swap.commit();
106
+
107
+ const qrCodeData = swap.getQrData(); //Data that can be displayed as QR code - URL with the address and amount
108
+ const bitcoinAddress = swap.getBitcoinAddress(); //Bitcoin address to send the BTC to - exact amount needs to be sent!
109
+ const timeout = swap.getTimeoutTime(); //The BTC should be sent before the timeout
110
+
111
+ console.log("Please send exactly "+inputTokenAmount+" BTC to "+bitcoinAddress);
112
+
113
+ //Waits for bitcoin transaction to be received
114
+ await swap.waitForBitcoinTransaction(
115
+ null, null,
116
+ (
117
+ txId: string, //Transaction ID received
118
+ confirmations: number, //Current confirmation count of the transaction
119
+ targetConfirmations: number, //Required confirmations for the transaction to be accepted
120
+ transactionETAms: number //Estimated time in milliseconds till the transaction is accepted
121
+ ) => {
122
+ //This callback receives periodic updates about the incoming transaction
123
+ console.log("Tx received: "+txId+" confirmations: "+confirmations+"/"+targetConfirmations+" ETA: "+transactionETAms+" ms");
124
+ }
125
+ ); //This returns as soon as the transaction is accepted
126
+
127
+ //Swap should get automatically claimed by the watchtowers, if not we can call swap.claim()
128
+ try {
129
+ await swap.waitTillClaimed(timeoutSignal(30*1000));
130
+ } catch (e) {
131
+ //Claim ourselves when automatic claim doesn't happen in 30 seconds
132
+ await swap.claim();
133
+ }
134
+ }
135
+
136
+ async function main() {
137
+ // await createToBtcSwap();
138
+ // await createFromBtcSwap();
139
+ }
140
+
141
+ main();