@atomiqlabs/sdk 4.0.0-beta.4 → 4.0.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.
- package/LICENSE +201 -201
- package/README.md +1074 -545
- package/dist/SmartChainAssets.d.ts +73 -73
- package/dist/SmartChainAssets.js +75 -75
- package/dist/SwapperFactory.d.ts +50 -50
- package/dist/SwapperFactory.js +105 -105
- package/dist/Utils.d.ts +11 -11
- package/dist/Utils.js +37 -37
- package/dist/fs-storage/FileSystemStorageManager.d.ts +15 -15
- package/dist/fs-storage/FileSystemStorageManager.js +60 -60
- package/dist/fs-storage/index.d.ts +1 -1
- package/dist/fs-storage/index.js +17 -17
- package/dist/index.d.ts +5 -5
- package/dist/index.js +21 -21
- package/dist/storage/LocalStorageManager.d.ts +24 -24
- package/dist/storage/LocalStorageManager.js +68 -68
- package/package.json +31 -31
- package/src/SmartChainAssets.js +75 -75
- package/src/SmartChainAssets.ts +76 -76
- package/src/SwapperFactory.js +120 -120
- package/src/SwapperFactory.ts +195 -195
- package/src/Utils.js +37 -37
- package/src/Utils.ts +31 -31
- package/src/fs-storage/FileSystemStorageManager.ts +71 -71
- package/src/fs-storage/index.ts +1 -1
- package/src/index.js +21 -21
- package/src/index.ts +5 -5
- package/src/storage/LocalStorageManager.js +72 -72
- package/src/storage/LocalStorageManager.ts +81 -81
- package/src/example/BrowserExample.js +0 -199
- package/src/example/BrowserExample.ts +0 -141
- package/src/example/NodeJSExample.js +0 -206
- package/src/example/NodeJSExample.ts +0 -150
|
@@ -1,199 +0,0 @@
|
|
|
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,141 +0,0 @@
|
|
|
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();
|
|
@@ -1,206 +0,0 @@
|
|
|
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
|
-
//The following line is important for running on backend node.js,
|
|
66
|
-
// because the SDK by default uses browser's Indexed DB, which is not available in node
|
|
67
|
-
swapStorage: function (chainId) { return null; },
|
|
68
|
-
noEvents: true,
|
|
69
|
-
noTimers: true,
|
|
70
|
-
dontCheckPastSwaps: true,
|
|
71
|
-
dontFetchLPs: true
|
|
72
|
-
});
|
|
73
|
-
//Initialize the swapper
|
|
74
|
-
return [4 /*yield*/, swapper.init()];
|
|
75
|
-
case 1:
|
|
76
|
-
//Initialize the swapper
|
|
77
|
-
_a.sent();
|
|
78
|
-
solanaSwapper = swapper.withChain("SOLANA");
|
|
79
|
-
signer = solanaSwapper.randomSigner();
|
|
80
|
-
//Or in React, using solana wallet adapter
|
|
81
|
-
//const signer = new SolanaKeypairWallet(useAnchorWallet());
|
|
82
|
-
//Extract a swapper with a defined signer
|
|
83
|
-
return [2 /*return*/, solanaSwapper.withSigner(signer)];
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
function createToBtcSwap() {
|
|
89
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
90
|
-
var solanaSwapper, fromToken, toToken, exactIn, amount, recipientBtcAddress, swap, inputTokenAmount, inputValueInUsd, outputTokenAmount, outputValueInUsd, paymentSuccess, bitcoinTxId;
|
|
91
|
-
return __generator(this, function (_a) {
|
|
92
|
-
switch (_a.label) {
|
|
93
|
-
case 0: return [4 /*yield*/, setupSwapper()];
|
|
94
|
-
case 1:
|
|
95
|
-
solanaSwapper = _a.sent();
|
|
96
|
-
fromToken = Tokens.SOLANA.SOL;
|
|
97
|
-
toToken = Tokens.BITCOIN.BTC;
|
|
98
|
-
exactIn = false;
|
|
99
|
-
amount = (0, __1.fromHumanReadableString)("0.0001", toToken);
|
|
100
|
-
recipientBtcAddress = "bc1qtw67hj77rt8zrkkg3jgngutu0yfgt9czjwusxt";
|
|
101
|
-
return [4 /*yield*/, solanaSwapper.create(fromToken, toToken, amount, exactIn, recipientBtcAddress)];
|
|
102
|
-
case 2:
|
|
103
|
-
swap = _a.sent();
|
|
104
|
-
inputTokenAmount = swap.getInput().amount;
|
|
105
|
-
return [4 /*yield*/, swap.getInput().usdValue()];
|
|
106
|
-
case 3:
|
|
107
|
-
inputValueInUsd = _a.sent();
|
|
108
|
-
outputTokenAmount = swap.getOutput().amount;
|
|
109
|
-
return [4 /*yield*/, swap.getOutput().usdValue()];
|
|
110
|
-
case 4:
|
|
111
|
-
outputValueInUsd = _a.sent();
|
|
112
|
-
//Initiate the swap by locking up the SOL
|
|
113
|
-
return [4 /*yield*/, swap.commit()];
|
|
114
|
-
case 5:
|
|
115
|
-
//Initiate the swap by locking up the SOL
|
|
116
|
-
_a.sent();
|
|
117
|
-
return [4 /*yield*/, swap.waitForPayment()];
|
|
118
|
-
case 6:
|
|
119
|
-
paymentSuccess = _a.sent();
|
|
120
|
-
if (!paymentSuccess) return [3 /*break*/, 7];
|
|
121
|
-
bitcoinTxId = swap.getBitcoinTxId();
|
|
122
|
-
return [3 /*break*/, 9];
|
|
123
|
-
case 7:
|
|
124
|
-
//If payment is unsuccessful we can refund and get our funds back
|
|
125
|
-
return [4 /*yield*/, swap.refund()];
|
|
126
|
-
case 8:
|
|
127
|
-
//If payment is unsuccessful we can refund and get our funds back
|
|
128
|
-
_a.sent();
|
|
129
|
-
_a.label = 9;
|
|
130
|
-
case 9: return [2 /*return*/];
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
function createFromBtcSwap() {
|
|
136
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
137
|
-
var solanaSwapper, fromToken, toToken, exactIn, amount, swap, inputTokenAmount, inputValueInUsd, outputTokenAmount, outputValueInUsd, qrCodeData, bitcoinAddress, timeout, e_1;
|
|
138
|
-
return __generator(this, function (_a) {
|
|
139
|
-
switch (_a.label) {
|
|
140
|
-
case 0: return [4 /*yield*/, setupSwapper()];
|
|
141
|
-
case 1:
|
|
142
|
-
solanaSwapper = _a.sent();
|
|
143
|
-
fromToken = Tokens.BITCOIN.BTC;
|
|
144
|
-
toToken = Tokens.SOLANA.SOL;
|
|
145
|
-
exactIn = true;
|
|
146
|
-
amount = BigInt(10000);
|
|
147
|
-
return [4 /*yield*/, solanaSwapper.create(fromToken, toToken, amount, exactIn)];
|
|
148
|
-
case 2:
|
|
149
|
-
swap = _a.sent();
|
|
150
|
-
inputTokenAmount = swap.getInput().amount;
|
|
151
|
-
return [4 /*yield*/, swap.getInput().usdValue()];
|
|
152
|
-
case 3:
|
|
153
|
-
inputValueInUsd = _a.sent();
|
|
154
|
-
outputTokenAmount = swap.getOutput().amount;
|
|
155
|
-
return [4 /*yield*/, swap.getOutput().usdValue()];
|
|
156
|
-
case 4:
|
|
157
|
-
outputValueInUsd = _a.sent();
|
|
158
|
-
//Initiate the swap, this will prompt a Solana transaction, as we need to open the BTC swap address
|
|
159
|
-
return [4 /*yield*/, swap.commit()];
|
|
160
|
-
case 5:
|
|
161
|
-
//Initiate the swap, this will prompt a Solana transaction, as we need to open the BTC swap address
|
|
162
|
-
_a.sent();
|
|
163
|
-
qrCodeData = swap.getQrData();
|
|
164
|
-
bitcoinAddress = swap.getBitcoinAddress();
|
|
165
|
-
timeout = swap.getTimeoutTime();
|
|
166
|
-
console.log("Please send exactly " + inputTokenAmount + " BTC to " + bitcoinAddress);
|
|
167
|
-
//Waits for bitcoin transaction to be received
|
|
168
|
-
return [4 /*yield*/, swap.waitForBitcoinTransaction(null, null, function (txId, //Transaction ID received
|
|
169
|
-
confirmations, //Current confirmation count of the transaction
|
|
170
|
-
targetConfirmations, //Required confirmations for the transaction to be accepted
|
|
171
|
-
transactionETAms //Estimated time in milliseconds till the transaction is accepted
|
|
172
|
-
) {
|
|
173
|
-
//This callback receives periodic updates about the incoming transaction
|
|
174
|
-
console.log("Tx received: " + txId + " confirmations: " + confirmations + "/" + targetConfirmations + " ETA: " + transactionETAms + " ms");
|
|
175
|
-
})];
|
|
176
|
-
case 6:
|
|
177
|
-
//Waits for bitcoin transaction to be received
|
|
178
|
-
_a.sent(); //This returns as soon as the transaction is accepted
|
|
179
|
-
_a.label = 7;
|
|
180
|
-
case 7:
|
|
181
|
-
_a.trys.push([7, 9, , 11]);
|
|
182
|
-
return [4 /*yield*/, swap.waitTillClaimed((0, __1.timeoutSignal)(30 * 1000))];
|
|
183
|
-
case 8:
|
|
184
|
-
_a.sent();
|
|
185
|
-
return [3 /*break*/, 11];
|
|
186
|
-
case 9:
|
|
187
|
-
e_1 = _a.sent();
|
|
188
|
-
//Claim ourselves when automatic claim doesn't happen in 30 seconds
|
|
189
|
-
return [4 /*yield*/, swap.claim()];
|
|
190
|
-
case 10:
|
|
191
|
-
//Claim ourselves when automatic claim doesn't happen in 30 seconds
|
|
192
|
-
_a.sent();
|
|
193
|
-
return [3 /*break*/, 11];
|
|
194
|
-
case 11: return [2 /*return*/];
|
|
195
|
-
}
|
|
196
|
-
});
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
function main() {
|
|
200
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
201
|
-
return __generator(this, function (_a) {
|
|
202
|
-
return [2 /*return*/];
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
main();
|