@atomiqlabs/sdk 4.0.0-beta.4 → 4.0.1
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/README.md +699 -170
- package/dist/SmartChainAssets.d.ts +10 -0
- package/dist/SmartChainAssets.js +10 -0
- package/package.json +3 -3
- package/src/SmartChainAssets.ts +10 -0
- 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,150 +0,0 @@
|
|
|
1
|
-
import {fromHumanReadableString, SwapperFactory, timeoutSignal} from "../";
|
|
2
|
-
import {FileSystemStorageManager} from "@atomiqlabs/sdk-lib/dist/fs-storage";
|
|
3
|
-
import {StarknetInitializer, StarknetInitializerType} from "@atomiqlabs/chain-starknet";
|
|
4
|
-
import {SolanaInitializer, SolanaInitializerType} from "@atomiqlabs/chain-solana";
|
|
5
|
-
import {IndexedDBUnifiedStorage} from "@atomiqlabs/sdk-lib";
|
|
6
|
-
|
|
7
|
-
const solanaRpc = "https://api.mainnet-beta.solana.com";
|
|
8
|
-
const starknetRpc = "https://starknet-mainnet.public.blastapi.io/rpc/v0_7";
|
|
9
|
-
|
|
10
|
-
//We initialize a swapper factory, this is important such that we can pick and choose which chains
|
|
11
|
-
// we want to support and only install those specific libraries, here Solana & Starknet are used
|
|
12
|
-
//NOTE: The "as const" keyword is important here as to let typescript properly infer the
|
|
13
|
-
// generic type of the SwapperFactory, allowing you to have code-completion for Tokens and TokenResolver
|
|
14
|
-
const Factory = new SwapperFactory<[SolanaInitializerType, StarknetInitializerType]>([SolanaInitializer, StarknetInitializer] as const);
|
|
15
|
-
const Tokens = Factory.Tokens;
|
|
16
|
-
|
|
17
|
-
async function setupSwapper() {
|
|
18
|
-
//Setup the multichain swapper
|
|
19
|
-
const swapper = Factory.newSwapper({
|
|
20
|
-
chains: {
|
|
21
|
-
SOLANA: {
|
|
22
|
-
rpcUrl: solanaRpc
|
|
23
|
-
},
|
|
24
|
-
STARKNET: {
|
|
25
|
-
rpcUrl: starknetRpc
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
//The following line is important for running on backend node.js,
|
|
29
|
-
// because the SDK by default uses browser's Indexed DB, which is not available in node
|
|
30
|
-
swapStorage: (chainId: string) => null,
|
|
31
|
-
noEvents: true,
|
|
32
|
-
noTimers: true,
|
|
33
|
-
dontCheckPastSwaps: true,
|
|
34
|
-
dontFetchLPs: true
|
|
35
|
-
});
|
|
36
|
-
//Initialize the swapper
|
|
37
|
-
await swapper.init();
|
|
38
|
-
|
|
39
|
-
//Extract a Solana specific swapper (used for swapping between Solana and Bitcoin)
|
|
40
|
-
const solanaSwapper = swapper.withChain<"SOLANA">("SOLANA");
|
|
41
|
-
|
|
42
|
-
//Create new random keypair wallet
|
|
43
|
-
const signer = solanaSwapper.randomSigner(); //This is just a dummy, you should load the wallet from file, or etc.
|
|
44
|
-
//Or in React, using solana wallet adapter
|
|
45
|
-
//const signer = new SolanaKeypairWallet(useAnchorWallet());
|
|
46
|
-
|
|
47
|
-
//Extract a swapper with a defined signer
|
|
48
|
-
return solanaSwapper.withSigner(signer);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async function createToBtcSwap() {
|
|
52
|
-
//In real use-cases you would setup the swapper just once, not for every swap!
|
|
53
|
-
const solanaSwapper = await setupSwapper();
|
|
54
|
-
|
|
55
|
-
const fromToken = Tokens.SOLANA.SOL;
|
|
56
|
-
const toToken = Tokens.BITCOIN.BTC;
|
|
57
|
-
const exactIn = false; //exactIn = false, so we specify the output amount
|
|
58
|
-
const amount = fromHumanReadableString("0.0001", toToken); //Amount in BTC base units - sats, you can also use fromHumanReadable helper methods
|
|
59
|
-
const recipientBtcAddress = "bc1qtw67hj77rt8zrkkg3jgngutu0yfgt9czjwusxt"; //BTC address of the recipient
|
|
60
|
-
|
|
61
|
-
const swap = await solanaSwapper.create(
|
|
62
|
-
fromToken,
|
|
63
|
-
toToken,
|
|
64
|
-
amount,
|
|
65
|
-
exactIn,
|
|
66
|
-
recipientBtcAddress
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
//Input amounts
|
|
70
|
-
const inputTokenAmount = swap.getInput().amount;
|
|
71
|
-
const inputValueInUsd = await swap.getInput().usdValue();
|
|
72
|
-
|
|
73
|
-
//Output amounts
|
|
74
|
-
const outputTokenAmount = swap.getOutput().amount;
|
|
75
|
-
const outputValueInUsd = await swap.getOutput().usdValue();
|
|
76
|
-
|
|
77
|
-
//Initiate the swap by locking up the SOL
|
|
78
|
-
await swap.commit();
|
|
79
|
-
//Wait for bitcoin payout to happen
|
|
80
|
-
const paymentSuccess = await swap.waitForPayment();
|
|
81
|
-
if(paymentSuccess) {
|
|
82
|
-
//Payment was successful, we can get the transaction id
|
|
83
|
-
const bitcoinTxId = swap.getBitcoinTxId();
|
|
84
|
-
} else {
|
|
85
|
-
//If payment is unsuccessful we can refund and get our funds back
|
|
86
|
-
await swap.refund();
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
async function createFromBtcSwap() {
|
|
91
|
-
const solanaSwapper = await setupSwapper();
|
|
92
|
-
|
|
93
|
-
const fromToken = Tokens.BITCOIN.BTC;
|
|
94
|
-
const toToken = Tokens.SOLANA.SOL;
|
|
95
|
-
const exactIn = true; //exactIn = true, so we specify the input amount
|
|
96
|
-
const amount = BigInt(10000); //Amount in BTC base units - sats
|
|
97
|
-
|
|
98
|
-
const swap = await solanaSwapper.create(
|
|
99
|
-
fromToken,
|
|
100
|
-
toToken,
|
|
101
|
-
amount,
|
|
102
|
-
exactIn
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
//Input amounts
|
|
106
|
-
const inputTokenAmount = swap.getInput().amount; //Human readable input token amount with decimals
|
|
107
|
-
const inputValueInUsd = await swap.getInput().usdValue(); //Fetches the USD value of the input
|
|
108
|
-
|
|
109
|
-
//Output amounts
|
|
110
|
-
const outputTokenAmount = swap.getOutput().amount; //Human readable output token amount with decimals
|
|
111
|
-
const outputValueInUsd = await swap.getOutput().usdValue(); //Fetches the USD value of the output
|
|
112
|
-
|
|
113
|
-
//Initiate the swap, this will prompt a Solana transaction, as we need to open the BTC swap address
|
|
114
|
-
await swap.commit();
|
|
115
|
-
|
|
116
|
-
const qrCodeData = swap.getQrData(); //Data that can be displayed as QR code - URL with the address and amount
|
|
117
|
-
const bitcoinAddress = swap.getBitcoinAddress(); //Bitcoin address to send the BTC to - exact amount needs to be sent!
|
|
118
|
-
const timeout = swap.getTimeoutTime(); //The BTC should be sent before the timeout
|
|
119
|
-
|
|
120
|
-
console.log("Please send exactly "+inputTokenAmount+" BTC to "+bitcoinAddress);
|
|
121
|
-
|
|
122
|
-
//Waits for bitcoin transaction to be received
|
|
123
|
-
await swap.waitForBitcoinTransaction(
|
|
124
|
-
null, null,
|
|
125
|
-
(
|
|
126
|
-
txId: string, //Transaction ID received
|
|
127
|
-
confirmations: number, //Current confirmation count of the transaction
|
|
128
|
-
targetConfirmations: number, //Required confirmations for the transaction to be accepted
|
|
129
|
-
transactionETAms: number //Estimated time in milliseconds till the transaction is accepted
|
|
130
|
-
) => {
|
|
131
|
-
//This callback receives periodic updates about the incoming transaction
|
|
132
|
-
console.log("Tx received: "+txId+" confirmations: "+confirmations+"/"+targetConfirmations+" ETA: "+transactionETAms+" ms");
|
|
133
|
-
}
|
|
134
|
-
); //This returns as soon as the transaction is accepted
|
|
135
|
-
|
|
136
|
-
//Swap should get automatically claimed by the watchtowers, if not we can call swap.claim()
|
|
137
|
-
try {
|
|
138
|
-
await swap.waitTillClaimed(timeoutSignal(30*1000));
|
|
139
|
-
} catch (e) {
|
|
140
|
-
//Claim ourselves when automatic claim doesn't happen in 30 seconds
|
|
141
|
-
await swap.claim();
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
async function main() {
|
|
146
|
-
// await createToBtcSwap();
|
|
147
|
-
// await createFromBtcSwap();
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
main();
|