@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.
- package/LICENSE +201 -201
- package/README.md +545 -501
- package/dist/SmartChainAssets.d.ts +73 -59
- package/dist/SmartChainAssets.js +75 -55
- package/dist/SwapperFactory.d.ts +49 -0
- package/dist/SwapperFactory.js +84 -0
- package/dist/Utils.d.ts +11 -0
- package/dist/Utils.js +37 -0
- package/dist/fs-storage/FileSystemStorageManager.d.ts +15 -0
- package/dist/fs-storage/FileSystemStorageManager.js +60 -0
- package/dist/fs-storage/index.d.ts +1 -0
- package/dist/fs-storage/index.js +17 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +21 -20
- package/dist/storage/LocalStorageManager.d.ts +24 -0
- package/dist/storage/LocalStorageManager.js +68 -0
- package/package.json +31 -32
- package/src/SmartChainAssets.js +75 -0
- package/src/SmartChainAssets.ts +76 -62
- package/src/SwapperFactory.js +96 -0
- package/src/SwapperFactory.ts +171 -0
- package/src/Utils.js +37 -0
- package/src/Utils.ts +31 -0
- package/src/example/BrowserExample.js +199 -0
- package/src/example/BrowserExample.ts +141 -120
- package/src/example/NodeJSExample.js +206 -0
- package/src/example/NodeJSExample.ts +150 -120
- package/src/fs-storage/FileSystemStorageManager.ts +71 -0
- package/src/fs-storage/index.ts +1 -0
- package/src/index.js +21 -0
- package/src/index.ts +5 -4
- package/src/storage/LocalStorageManager.js +72 -0
- package/src/storage/LocalStorageManager.ts +81 -0
- package/dist/MultichainSwapper.d.ts +0 -46
- package/dist/MultichainSwapper.js +0 -83
- package/dist/chains/ChainInitializer.d.ts +0 -13
- package/dist/chains/ChainInitializer.js +0 -2
- package/dist/chains/solana/SolanaChainInitializer.d.ts +0 -41
- package/dist/chains/solana/SolanaChainInitializer.js +0 -60
- package/dist/chains/solana/SolanaChains.d.ts +0 -14
- package/dist/chains/solana/SolanaChains.js +0 -18
- package/dist/example/BrowserExample.d.ts +0 -1
- package/dist/example/BrowserExample.js +0 -104
- package/dist/example/NodeJSExample.d.ts +0 -1
- package/dist/example/NodeJSExample.js +0 -104
- package/src/MultichainSwapper.ts +0 -156
- package/src/chains/ChainInitializer.ts +0 -16
- package/src/chains/solana/SolanaChainInitializer.ts +0 -99
- package/src/chains/solana/SolanaChains.ts +0 -16
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalStorageManager = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* StorageManager using browser's local storage API
|
|
6
|
+
*/
|
|
7
|
+
var LocalStorageManager = /** @class */ (function () {
|
|
8
|
+
function LocalStorageManager(storageKey) {
|
|
9
|
+
this.rawData = null;
|
|
10
|
+
this.data = {};
|
|
11
|
+
this.storageKey = storageKey;
|
|
12
|
+
}
|
|
13
|
+
LocalStorageManager.prototype.init = function () {
|
|
14
|
+
var completedTxt = window.localStorage.getItem(this.storageKey);
|
|
15
|
+
if (completedTxt != null) {
|
|
16
|
+
this.rawData = JSON.parse(completedTxt);
|
|
17
|
+
if (this.rawData == null)
|
|
18
|
+
this.rawData = {};
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
this.rawData = {};
|
|
22
|
+
}
|
|
23
|
+
return Promise.resolve();
|
|
24
|
+
};
|
|
25
|
+
LocalStorageManager.prototype.saveData = function (hash, object) {
|
|
26
|
+
this.data[hash] = object;
|
|
27
|
+
this.rawData[hash] = object.serialize();
|
|
28
|
+
return this.save();
|
|
29
|
+
};
|
|
30
|
+
LocalStorageManager.prototype.saveDataArr = function (arr) {
|
|
31
|
+
var _this = this;
|
|
32
|
+
arr.forEach(function (e) {
|
|
33
|
+
_this.data[e.id] = e.object;
|
|
34
|
+
_this.rawData[e.id] = e.object.serialize();
|
|
35
|
+
});
|
|
36
|
+
return this.save();
|
|
37
|
+
};
|
|
38
|
+
LocalStorageManager.prototype.removeData = function (hash) {
|
|
39
|
+
if (this.rawData[hash] != null) {
|
|
40
|
+
if (this.data[hash] != null)
|
|
41
|
+
delete this.data[hash];
|
|
42
|
+
delete this.rawData[hash];
|
|
43
|
+
return this.save();
|
|
44
|
+
}
|
|
45
|
+
return Promise.resolve();
|
|
46
|
+
};
|
|
47
|
+
LocalStorageManager.prototype.removeDataArr = function (hashArr) {
|
|
48
|
+
var _this = this;
|
|
49
|
+
hashArr.forEach(function (hash) {
|
|
50
|
+
if (_this.rawData[hash] != null) {
|
|
51
|
+
if (_this.data[hash] != null)
|
|
52
|
+
delete _this.data[hash];
|
|
53
|
+
delete _this.rawData[hash];
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return this.save();
|
|
57
|
+
};
|
|
58
|
+
LocalStorageManager.prototype.loadData = function (type) {
|
|
59
|
+
var _this = this;
|
|
60
|
+
return Promise.resolve(Object.keys(this.rawData).map(function (e) {
|
|
61
|
+
var deserialized = new type(_this.rawData[e]);
|
|
62
|
+
_this.data[e] = deserialized;
|
|
63
|
+
return deserialized;
|
|
64
|
+
}));
|
|
65
|
+
};
|
|
66
|
+
LocalStorageManager.prototype.save = function () {
|
|
67
|
+
window.localStorage.setItem(this.storageKey, JSON.stringify(this.rawData));
|
|
68
|
+
return Promise.resolve();
|
|
69
|
+
};
|
|
70
|
+
return LocalStorageManager;
|
|
71
|
+
}());
|
|
72
|
+
exports.LocalStorageManager = LocalStorageManager;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import {IStorageManager, StorageObject} from "@atomiqlabs/base";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* StorageManager using browser's local storage API
|
|
5
|
+
*/
|
|
6
|
+
export class LocalStorageManager<T extends StorageObject> implements IStorageManager<T> {
|
|
7
|
+
|
|
8
|
+
storageKey: string;
|
|
9
|
+
|
|
10
|
+
rawData: {
|
|
11
|
+
[hash: string]: any
|
|
12
|
+
} = null;
|
|
13
|
+
data: {
|
|
14
|
+
[hash: string]: T
|
|
15
|
+
} = {};
|
|
16
|
+
|
|
17
|
+
constructor(storageKey: string) {
|
|
18
|
+
this.storageKey = storageKey;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
init(): Promise<void> {
|
|
22
|
+
const completedTxt = window.localStorage.getItem(this.storageKey);
|
|
23
|
+
if(completedTxt!=null) {
|
|
24
|
+
this.rawData = JSON.parse(completedTxt);
|
|
25
|
+
if(this.rawData==null) this.rawData = {};
|
|
26
|
+
} else {
|
|
27
|
+
this.rawData = {};
|
|
28
|
+
}
|
|
29
|
+
return Promise.resolve();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
saveData(hash: string, object: T): Promise<void> {
|
|
33
|
+
this.data[hash] = object;
|
|
34
|
+
this.rawData[hash] = object.serialize();
|
|
35
|
+
|
|
36
|
+
return this.save();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
saveDataArr(arr: {id: string, object: T}[]): Promise<void> {
|
|
40
|
+
arr.forEach(e => {
|
|
41
|
+
this.data[e.id] = e.object;
|
|
42
|
+
this.rawData[e.id] = e.object.serialize();
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
return this.save();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
removeData(hash: string): Promise<void> {
|
|
49
|
+
if(this.rawData[hash]!=null) {
|
|
50
|
+
if(this.data[hash]!=null) delete this.data[hash];
|
|
51
|
+
delete this.rawData[hash];
|
|
52
|
+
return this.save();
|
|
53
|
+
}
|
|
54
|
+
return Promise.resolve();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
removeDataArr(hashArr: string[]): Promise<void> {
|
|
58
|
+
hashArr.forEach(hash => {
|
|
59
|
+
if(this.rawData[hash]!=null) {
|
|
60
|
+
if(this.data[hash]!=null) delete this.data[hash];
|
|
61
|
+
delete this.rawData[hash];
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
return this.save();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
loadData(type: new (data: any) => T): Promise<T[]> {
|
|
68
|
+
return Promise.resolve(
|
|
69
|
+
Object.keys(this.rawData).map(e => {
|
|
70
|
+
const deserialized = new type(this.rawData[e]);
|
|
71
|
+
this.data[e] = deserialized;
|
|
72
|
+
return deserialized;
|
|
73
|
+
})
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private save(): Promise<void> {
|
|
78
|
+
window.localStorage.setItem(this.storageKey, JSON.stringify(this.rawData));
|
|
79
|
+
return Promise.resolve();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { BtcToken, MempoolApi, SCToken, Swapper, SwapperOptions } from "@atomiqlabs/sdk-lib";
|
|
2
|
-
import { SdkSolanaType } from "./chains/solana/SolanaChainInitializer";
|
|
3
|
-
import { SdkChain } from "./chains/ChainInitializer";
|
|
4
|
-
import * as BN from "bn.js";
|
|
5
|
-
import { IStorageManager, StorageObject } from "@atomiqlabs/base";
|
|
6
|
-
import { SwapperWithChain } from "@atomiqlabs/sdk-lib/dist/swaps/SwapperWithChain";
|
|
7
|
-
import { SwapperWithSigner } from "@atomiqlabs/sdk-lib/dist/swaps/SwapperWithSigner";
|
|
8
|
-
type Chains = {
|
|
9
|
-
"SOLANA": SdkSolanaType;
|
|
10
|
-
};
|
|
11
|
-
declare const Chains: {
|
|
12
|
-
[C in keyof Chains]: SdkChain<Chains[C]>;
|
|
13
|
-
};
|
|
14
|
-
export type SdkMultichain = {
|
|
15
|
-
[C in keyof Chains]: Chains[C]["ChainType"];
|
|
16
|
-
};
|
|
17
|
-
export type MultichainSwapperOptions = SwapperOptions & {
|
|
18
|
-
chains: {
|
|
19
|
-
[C in keyof Chains]: Chains[C]["Options"];
|
|
20
|
-
};
|
|
21
|
-
} & {
|
|
22
|
-
storageCtor?: <T extends StorageObject>(name: string) => IStorageManager<T>;
|
|
23
|
-
pricingFeeDifferencePPM?: BN;
|
|
24
|
-
mempoolApi?: MempoolApi;
|
|
25
|
-
};
|
|
26
|
-
export declare class MultichainSwapper extends Swapper<SdkMultichain> {
|
|
27
|
-
constructor(options: MultichainSwapperOptions);
|
|
28
|
-
}
|
|
29
|
-
export declare const Tokens: {
|
|
30
|
-
[C in keyof Chains]: {
|
|
31
|
-
[T in keyof Chains[C]["Assets"]]: SCToken<C>;
|
|
32
|
-
};
|
|
33
|
-
} & {
|
|
34
|
-
BITCOIN: {
|
|
35
|
-
BTC: BtcToken<false>;
|
|
36
|
-
BTCLN: BtcToken<true>;
|
|
37
|
-
};
|
|
38
|
-
};
|
|
39
|
-
export declare const TokenResolver: {
|
|
40
|
-
[C in keyof Chains]: {
|
|
41
|
-
getToken(address: string): SCToken<C>;
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
export type SolanaSwapper = SwapperWithChain<SdkMultichain, "SOLANA">;
|
|
45
|
-
export type SolanaSwapperWithSigner = SwapperWithSigner<SdkMultichain, "SOLANA">;
|
|
46
|
-
export {};
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TokenResolver = exports.Tokens = exports.MultichainSwapper = void 0;
|
|
4
|
-
const sdk_lib_1 = require("@atomiqlabs/sdk-lib");
|
|
5
|
-
const Utils_1 = require("@atomiqlabs/sdk-lib/dist/utils/Utils");
|
|
6
|
-
const SolanaChainInitializer_1 = require("./chains/solana/SolanaChainInitializer");
|
|
7
|
-
const BN = require("bn.js");
|
|
8
|
-
const SmartChainAssets_1 = require("./SmartChainAssets");
|
|
9
|
-
const Chains = {
|
|
10
|
-
"SOLANA": SolanaChainInitializer_1.SdkSolana
|
|
11
|
-
};
|
|
12
|
-
class MultichainSwapper extends sdk_lib_1.Swapper {
|
|
13
|
-
constructor(options) {
|
|
14
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
15
|
-
(_a = options.bitcoinNetwork) !== null && _a !== void 0 ? _a : (options.bitcoinNetwork = sdk_lib_1.BitcoinNetwork.MAINNET);
|
|
16
|
-
(_b = options.storagePrefix) !== null && _b !== void 0 ? _b : (options.storagePrefix = "atomiqsdk-" + options.bitcoinNetwork);
|
|
17
|
-
(_c = options.storageCtor) !== null && _c !== void 0 ? _c : (options.storageCtor = (name) => new sdk_lib_1.IndexedDBStorageManager(name));
|
|
18
|
-
(_d = options.defaultTrustedIntermediaryUrl) !== null && _d !== void 0 ? _d : (options.defaultTrustedIntermediaryUrl = options.bitcoinNetwork === sdk_lib_1.BitcoinNetwork.MAINNET ?
|
|
19
|
-
"https://node3.gethopa.com:34100" :
|
|
20
|
-
"https://node3.gethopa.com:24100");
|
|
21
|
-
(_e = options.registryUrl) !== null && _e !== void 0 ? _e : (options.registryUrl = options.bitcoinNetwork === sdk_lib_1.BitcoinNetwork.MAINNET ?
|
|
22
|
-
"https://api.github.com/repos/adambor/SolLightning-registry/contents/registry-mainnet.json?ref=main" :
|
|
23
|
-
"https://api.github.com/repos/adambor/SolLightning-registry/contents/registry.json?ref=main");
|
|
24
|
-
const mempoolApi = (_f = options.mempoolApi) !== null && _f !== void 0 ? _f : new sdk_lib_1.MempoolApi(options.bitcoinNetwork === sdk_lib_1.BitcoinNetwork.TESTNET ?
|
|
25
|
-
[
|
|
26
|
-
"https://mempool.space/testnet/api/",
|
|
27
|
-
"https://mempool.fra.mempool.space/testnet/api/",
|
|
28
|
-
"https://mempool.va1.mempool.space/testnet/api/",
|
|
29
|
-
"https://mempool.tk7.mempool.space/testnet/api/"
|
|
30
|
-
] :
|
|
31
|
-
[
|
|
32
|
-
"https://mempool.space/api/",
|
|
33
|
-
"https://mempool.fra.mempool.space/api/",
|
|
34
|
-
"https://mempool.va1.mempool.space/api/",
|
|
35
|
-
"https://mempool.tk7.mempool.space/api/"
|
|
36
|
-
]);
|
|
37
|
-
const bitcoinRpc = new sdk_lib_1.MempoolBitcoinRpc(mempoolApi);
|
|
38
|
-
const pricingAssets = [];
|
|
39
|
-
Object.keys(SmartChainAssets_1.SmartChainAssets).forEach((ticker) => {
|
|
40
|
-
const chains = {};
|
|
41
|
-
for (let chainId in Chains) {
|
|
42
|
-
if (Chains[chainId].assets[ticker] != null)
|
|
43
|
-
chains[chainId] = Chains[chainId].assets[ticker];
|
|
44
|
-
}
|
|
45
|
-
const assetData = SmartChainAssets_1.SmartChainAssets[ticker];
|
|
46
|
-
pricingAssets.push(Object.assign(Object.assign({}, assetData.pricing), { chains,
|
|
47
|
-
ticker, name: assetData.name }));
|
|
48
|
-
});
|
|
49
|
-
const ctorChainData = (0, Utils_1.objectMap)(Chains, (value, key) => {
|
|
50
|
-
return value.getCtorData(options, bitcoinRpc, options.bitcoinNetwork);
|
|
51
|
-
});
|
|
52
|
-
super(bitcoinRpc, ctorChainData, sdk_lib_1.RedundantSwapPrice.createFromTokenMap((_g = options.pricingFeeDifferencePPM) !== null && _g !== void 0 ? _g : new BN(10000), pricingAssets), pricingAssets, options);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
exports.MultichainSwapper = MultichainSwapper;
|
|
56
|
-
exports.Tokens = Object.assign(Object.assign({}, (0, Utils_1.objectMap)(Chains, (value, key) => {
|
|
57
|
-
return (0, Utils_1.objectMap)(value.assets, (assetData, ticker) => {
|
|
58
|
-
return {
|
|
59
|
-
chain: "SC",
|
|
60
|
-
chainId: value.chainIdentifier,
|
|
61
|
-
address: assetData.address,
|
|
62
|
-
name: SmartChainAssets_1.SmartChainAssets[ticker].name,
|
|
63
|
-
decimals: assetData.decimals,
|
|
64
|
-
ticker
|
|
65
|
-
};
|
|
66
|
-
});
|
|
67
|
-
})), { BITCOIN: sdk_lib_1.BitcoinTokens });
|
|
68
|
-
exports.TokenResolver = Object.assign({}, (0, Utils_1.objectMap)(Chains, (value, key) => {
|
|
69
|
-
const addressMap = {};
|
|
70
|
-
for (let ticker in value.assets) {
|
|
71
|
-
addressMap[value.assets[ticker].address] = {
|
|
72
|
-
chain: "SC",
|
|
73
|
-
chainId: value.chainIdentifier,
|
|
74
|
-
address: value.assets[ticker].address,
|
|
75
|
-
ticker,
|
|
76
|
-
name: SmartChainAssets_1.SmartChainAssets[ticker].name,
|
|
77
|
-
decimals: value.assets[ticker].decimals
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
return {
|
|
81
|
-
getToken: (address) => addressMap[address]
|
|
82
|
-
};
|
|
83
|
-
}));
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { BitcoinRpc, ChainType } from "@atomiqlabs/base";
|
|
2
|
-
import { BitcoinNetwork, CtorChainData, SwapperOptions } from "@atomiqlabs/sdk-lib";
|
|
3
|
-
import { AssetData } from "../SmartChainAssets";
|
|
4
|
-
export type SdkChainType = {
|
|
5
|
-
ChainType: ChainType;
|
|
6
|
-
Options: {};
|
|
7
|
-
Assets: AssetData;
|
|
8
|
-
};
|
|
9
|
-
export type SdkChain<T extends SdkChainType> = {
|
|
10
|
-
getCtorData: (options: SwapperOptions, bitcoinRpc: BitcoinRpc<any>, network: BitcoinNetwork) => CtorChainData<T["ChainType"]>;
|
|
11
|
-
assets: AssetData;
|
|
12
|
-
chainIdentifier: string;
|
|
13
|
-
};
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { SolanaChainType, SolanaFees, SolanaRetryPolicy, StoredDataAccount } from "@atomiqlabs/chain-solana";
|
|
2
|
-
import { IStorageManager } from "@atomiqlabs/base";
|
|
3
|
-
import { Connection } from "@solana/web3.js";
|
|
4
|
-
import { SdkChain } from "../ChainInitializer";
|
|
5
|
-
type SolanaSwapperOptions = {
|
|
6
|
-
rpcUrl: string | Connection;
|
|
7
|
-
dataAccountStorage?: IStorageManager<StoredDataAccount>;
|
|
8
|
-
retryPolicy?: SolanaRetryPolicy;
|
|
9
|
-
btcRelayContract?: string;
|
|
10
|
-
swapContract?: string;
|
|
11
|
-
fees?: SolanaFees;
|
|
12
|
-
};
|
|
13
|
-
declare const SolanaAssets: {
|
|
14
|
-
readonly WBTC: {
|
|
15
|
-
readonly address: "3NZ9JMVBmGAqocybic2c7LQCJScmgsAZ6vQqTDzcqmJh";
|
|
16
|
-
readonly decimals: 8;
|
|
17
|
-
};
|
|
18
|
-
readonly USDC: {
|
|
19
|
-
readonly address: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
|
|
20
|
-
readonly decimals: 6;
|
|
21
|
-
};
|
|
22
|
-
readonly USDT: {
|
|
23
|
-
readonly address: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB";
|
|
24
|
-
readonly decimals: 6;
|
|
25
|
-
};
|
|
26
|
-
readonly SOL: {
|
|
27
|
-
readonly address: "So11111111111111111111111111111111111111112";
|
|
28
|
-
readonly decimals: 9;
|
|
29
|
-
};
|
|
30
|
-
readonly BONK: {
|
|
31
|
-
readonly address: "DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263";
|
|
32
|
-
readonly decimals: 5;
|
|
33
|
-
};
|
|
34
|
-
};
|
|
35
|
-
export type SdkSolanaType = {
|
|
36
|
-
ChainType: SolanaChainType;
|
|
37
|
-
Options: SolanaSwapperOptions;
|
|
38
|
-
Assets: typeof SolanaAssets;
|
|
39
|
-
};
|
|
40
|
-
export declare const SdkSolana: SdkChain<SdkSolanaType>;
|
|
41
|
-
export {};
|
|
@@ -1,60 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,104 +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
|
-
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 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,104 +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
|
-
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();
|