@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.
- package/LICENSE +201 -201
- package/README.md +501 -501
- package/dist/MultichainSwapper.d.ts +46 -46
- package/dist/MultichainSwapper.js +83 -85
- package/dist/SmartChainAssets.d.ts +54 -54
- package/dist/SmartChainAssets.js +50 -50
- package/dist/chains/ChainInitializer.d.ts +13 -13
- package/dist/chains/ChainInitializer.js +2 -2
- package/dist/chains/solana/SolanaChainInitializer.d.ts +41 -41
- package/dist/chains/solana/SolanaChainInitializer.js +60 -60
- package/dist/chains/solana/SolanaChains.d.ts +14 -14
- package/dist/chains/solana/SolanaChains.js +18 -18
- package/dist/example/BrowserExample.d.ts +1 -1
- package/dist/example/BrowserExample.js +104 -104
- package/dist/example/NodeJSExample.d.ts +1 -1
- package/dist/example/NodeJSExample.js +104 -104
- package/dist/index.d.ts +4 -4
- package/dist/index.js +20 -20
- package/package.json +32 -32
- package/src/MultichainSwapper.ts +156 -158
- package/src/SmartChainAssets.ts +57 -57
- package/src/chains/ChainInitializer.ts +16 -16
- package/src/chains/solana/SolanaChainInitializer.ts +99 -99
- package/src/chains/solana/SolanaChains.ts +16 -16
- package/src/example/BrowserExample.ts +120 -120
- package/src/example/NodeJSExample.ts +120 -120
- package/src/index.ts +4 -4
|
@@ -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
|
-
import {FileSystemStorageManager} from "@atomiqlabs/sdk-lib/dist/fs-storage";
|
|
5
|
-
|
|
6
|
-
const solanaRpc = "https://api.mainnet-beta.solana.com";
|
|
7
|
-
|
|
8
|
-
let solanaSwapper: SolanaSwapperWithSigner;
|
|
9
|
-
|
|
10
|
-
async function setupSwapper() {
|
|
11
|
-
//Setup the multichain swapper
|
|
12
|
-
const swapper: MultichainSwapper = new MultichainSwapper({
|
|
13
|
-
chains: {
|
|
14
|
-
SOLANA: {
|
|
15
|
-
rpcUrl: solanaRpc
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
//The following line is important for running on backend node.js,
|
|
19
|
-
// because the SDK by default uses browser's Indexed DB
|
|
20
|
-
storageCtor: (name: string) => new FileSystemStorageManager(name)
|
|
21
|
-
});
|
|
22
|
-
await swapper.init();
|
|
23
|
-
|
|
24
|
-
//Create new random keypair wallet
|
|
25
|
-
const wallet = new SolanaKeypairWallet(Keypair.generate()); //This is just a dummy, you should load the wallet from file, or etc.
|
|
26
|
-
|
|
27
|
-
const signer: SolanaSigner = new SolanaSigner(wallet);
|
|
28
|
-
//Extract a Solana specific swapper (used for swapping between Solana and Bitcoin) with a defined signer
|
|
29
|
-
solanaSwapper = swapper.withChain("SOLANA").withSigner(signer);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
async function createToBtcSwap() {
|
|
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
|
-
Tokens.SOLANA.SOL,
|
|
39
|
-
Tokens.BITCOIN.BTC,
|
|
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
|
+
import {FileSystemStorageManager} from "@atomiqlabs/sdk-lib/dist/fs-storage";
|
|
5
|
+
|
|
6
|
+
const solanaRpc = "https://api.mainnet-beta.solana.com";
|
|
7
|
+
|
|
8
|
+
let solanaSwapper: SolanaSwapperWithSigner;
|
|
9
|
+
|
|
10
|
+
async function setupSwapper() {
|
|
11
|
+
//Setup the multichain swapper
|
|
12
|
+
const swapper: MultichainSwapper = new MultichainSwapper({
|
|
13
|
+
chains: {
|
|
14
|
+
SOLANA: {
|
|
15
|
+
rpcUrl: solanaRpc
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
//The following line is important for running on backend node.js,
|
|
19
|
+
// because the SDK by default uses browser's Indexed DB
|
|
20
|
+
storageCtor: (name: string) => new FileSystemStorageManager(name)
|
|
21
|
+
});
|
|
22
|
+
await swapper.init();
|
|
23
|
+
|
|
24
|
+
//Create new random keypair wallet
|
|
25
|
+
const wallet = new SolanaKeypairWallet(Keypair.generate()); //This is just a dummy, you should load the wallet from file, or etc.
|
|
26
|
+
|
|
27
|
+
const signer: SolanaSigner = new SolanaSigner(wallet);
|
|
28
|
+
//Extract a Solana specific swapper (used for swapping between Solana and Bitcoin) with a defined signer
|
|
29
|
+
solanaSwapper = swapper.withChain("SOLANA").withSigner(signer);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function createToBtcSwap() {
|
|
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
|
+
Tokens.SOLANA.SOL,
|
|
39
|
+
Tokens.BITCOIN.BTC,
|
|
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();
|
package/src/index.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";
|