@atomiqlabs/sdk 7.0.7 → 7.0.8
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 +1744 -1617
- package/dist/SmartChainAssets.d.ts +143 -143
- package/dist/SmartChainAssets.js +147 -147
- package/dist/SwapperFactory.d.ts +52 -52
- package/dist/SwapperFactory.js +118 -118
- 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 +32 -32
- package/src/SmartChainAssets.js +75 -75
- package/src/SmartChainAssets.ts +148 -148
- package/src/SwapperFactory.js +120 -120
- package/src/SwapperFactory.ts +212 -212
- 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/SwapperFactory.ts
CHANGED
|
@@ -1,212 +1,212 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ChainData,
|
|
3
|
-
BitcoinNetwork,
|
|
4
|
-
BitcoinRpc,
|
|
5
|
-
BaseTokenType,
|
|
6
|
-
ChainType,
|
|
7
|
-
StorageObject,
|
|
8
|
-
IStorageManager, Messenger
|
|
9
|
-
} from "@atomiqlabs/base";
|
|
10
|
-
import {
|
|
11
|
-
BitcoinTokens,
|
|
12
|
-
BtcToken, CustomPriceFunction, CustomPriceProvider,
|
|
13
|
-
MempoolApi,
|
|
14
|
-
MempoolBitcoinRpc, RedundantSwapPrice,
|
|
15
|
-
RedundantSwapPriceAssets, SCToken, SingleSwapPrice, Swapper,
|
|
16
|
-
SwapperOptions
|
|
17
|
-
} from "@atomiqlabs/sdk-lib";
|
|
18
|
-
import {SmartChainAssets} from "./SmartChainAssets";
|
|
19
|
-
import {LocalStorageManager} from "./storage/LocalStorageManager";
|
|
20
|
-
import {NostrMessenger} from "@atomiqlabs/messenger-nostr";
|
|
21
|
-
|
|
22
|
-
type ChainInitializer<O, C extends ChainType, T extends BaseTokenType> = {
|
|
23
|
-
chainId: ChainType["ChainId"],
|
|
24
|
-
chainType: ChainType,
|
|
25
|
-
initializer: (
|
|
26
|
-
options: O,
|
|
27
|
-
bitcoinRelay: BitcoinRpc<any>,
|
|
28
|
-
network: BitcoinNetwork,
|
|
29
|
-
storageCtor: <T extends StorageObject>(name: string) => IStorageManager<T>
|
|
30
|
-
) => ChainData<C>,
|
|
31
|
-
tokens: T,
|
|
32
|
-
options: O
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
type TokensDict<T extends ChainInitializer<any, any, any>> = {
|
|
36
|
-
[K in T["chainId"]]: {
|
|
37
|
-
[val in keyof T["tokens"]]: SCToken<K>
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
type GetAllTokens<T extends readonly ChainInitializer<any, any, any>[]> =
|
|
41
|
-
(T extends readonly [infer First extends ChainInitializer<any, any, any>, ...infer Rest extends ChainInitializer<any, any, any>[]]
|
|
42
|
-
? TokensDict<First> & GetAllTokens<Rest>
|
|
43
|
-
: unknown);
|
|
44
|
-
|
|
45
|
-
export type TokenResolverDict<T extends ChainInitializer<any, any, any>> = {[K in T["chainId"]]: {
|
|
46
|
-
getToken: (address: string) => SCToken<K>
|
|
47
|
-
}};
|
|
48
|
-
type GetAllTokenResolvers<T extends readonly ChainInitializer<any, any, any>[]> =
|
|
49
|
-
(T extends readonly [infer First extends ChainInitializer<any, any, any>, ...infer Rest extends ChainInitializer<any, any, any>[]]
|
|
50
|
-
? TokenResolverDict<First> & GetAllTokenResolvers<Rest>
|
|
51
|
-
: unknown);
|
|
52
|
-
|
|
53
|
-
type OptionsDict<T extends ChainInitializer<any, any, any>> = {[K in T["chainId"]]: T["options"]};
|
|
54
|
-
type GetAllOptions<T extends readonly ChainInitializer<any, any, any>[]> =
|
|
55
|
-
(T extends readonly [infer First extends ChainInitializer<any, any, any>, ...infer Rest extends ChainInitializer<any, any, any>[]]
|
|
56
|
-
? OptionsDict<First> & GetAllOptions<Rest>
|
|
57
|
-
: unknown);
|
|
58
|
-
|
|
59
|
-
type ChainTypeDict<T extends ChainInitializer<any, any, any>> = {[K in T["chainId"]]: T["chainType"]};
|
|
60
|
-
type ToMultichain<T extends readonly ChainInitializer<any, any, any>[]> =
|
|
61
|
-
(T extends readonly [infer First extends ChainInitializer<any, any, any>, ...infer Rest extends ChainInitializer<any, any, any>[]]
|
|
62
|
-
? ChainTypeDict<First> & ToMultichain<Rest>
|
|
63
|
-
: {});
|
|
64
|
-
|
|
65
|
-
export type MultichainSwapperOptions<T extends readonly ChainInitializer<any, any, any>[]> = SwapperOptions & {
|
|
66
|
-
chains: GetAllOptions<T>
|
|
67
|
-
} & {
|
|
68
|
-
chainStorageCtor?: <T extends StorageObject>(name: string) => IStorageManager<T>,
|
|
69
|
-
pricingFeeDifferencePPM?: bigint,
|
|
70
|
-
mempoolApi?: MempoolApi | MempoolBitcoinRpc | string | string[],
|
|
71
|
-
messenger?: Messenger,
|
|
72
|
-
getPriceFn?: CustomPriceFunction
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
const registries = {
|
|
76
|
-
[BitcoinNetwork.MAINNET]: "https://api.github.com/repos/adambor/SolLightning-registry/contents/registry-mainnet.json?ref=main",
|
|
77
|
-
[BitcoinNetwork.TESTNET]: "https://api.github.com/repos/adambor/SolLightning-registry/contents/registry.json?ref=main",
|
|
78
|
-
[BitcoinNetwork.TESTNET4]: "https://api.github.com/repos/adambor/SolLightning-registry/contents/registry-testnet4.json?ref=main"
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const trustedIntermediaries = {
|
|
82
|
-
[BitcoinNetwork.MAINNET]: "https://node3.gethopa.com:34100",
|
|
83
|
-
[BitcoinNetwork.TESTNET]: "https://node3.gethopa.com:24100"
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const mempoolUrls = {
|
|
87
|
-
[BitcoinNetwork.MAINNET]: [
|
|
88
|
-
"https://mempool.space/api/",
|
|
89
|
-
"https://mempool.holdings/api/",
|
|
90
|
-
"https://mempool.fra.mempool.space/api/",
|
|
91
|
-
"https://mempool.va1.mempool.space/api/",
|
|
92
|
-
"https://mempool.tk7.mempool.space/api/"
|
|
93
|
-
],
|
|
94
|
-
[BitcoinNetwork.TESTNET]: [
|
|
95
|
-
"https://mempool.space/testnet/api/",
|
|
96
|
-
"https://mempool.holdings/testnet/api/",
|
|
97
|
-
"https://mempool.fra.mempool.space/testnet/api/",
|
|
98
|
-
"https://mempool.va1.mempool.space/testnet/api/",
|
|
99
|
-
"https://mempool.tk7.mempool.space/testnet/api/"
|
|
100
|
-
],
|
|
101
|
-
[BitcoinNetwork.TESTNET4]: [
|
|
102
|
-
"https://mempool.space/testnet4/api/",
|
|
103
|
-
"https://mempool.holdings/testnet4/api/",
|
|
104
|
-
"https://mempool.fra.mempool.space/testnet4/api/",
|
|
105
|
-
"https://mempool.va1.mempool.space/testnet4/api/",
|
|
106
|
-
"https://mempool.tk7.mempool.space/testnet4/api/"
|
|
107
|
-
]
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const nostrUrls: string[] = [
|
|
111
|
-
"wss://relay.damus.io", "wss://nostr.einundzwanzig.space", "wss://relay01.lnfi.network/", "wss://relay.puresignal.news/", "wss://relay.fountain.fm/", "wss://sendit.nosflare.com/"
|
|
112
|
-
];
|
|
113
|
-
|
|
114
|
-
export class SwapperFactory<T extends readonly ChainInitializer<any, any, any>[]> {
|
|
115
|
-
|
|
116
|
-
Tokens: GetAllTokens<T> & {
|
|
117
|
-
BITCOIN: {
|
|
118
|
-
BTC: BtcToken<false>,
|
|
119
|
-
BTCLN: BtcToken<true>
|
|
120
|
-
}
|
|
121
|
-
} = {
|
|
122
|
-
BITCOIN: BitcoinTokens
|
|
123
|
-
} as any;
|
|
124
|
-
TokenResolver: GetAllTokenResolvers<T> = {} as any;
|
|
125
|
-
|
|
126
|
-
constructor(readonly initializers: T) {
|
|
127
|
-
this.initializers = initializers;
|
|
128
|
-
initializers.forEach(initializer => {
|
|
129
|
-
const addressMap: {[tokenAddress: string]: SCToken} = {};
|
|
130
|
-
|
|
131
|
-
this.Tokens[initializer.chainId] = {} as any;
|
|
132
|
-
|
|
133
|
-
for(let ticker in initializer.tokens) {
|
|
134
|
-
const assetData = initializer.tokens[ticker] as any;
|
|
135
|
-
this.Tokens[initializer.chainId][ticker] = addressMap[assetData.address] = {
|
|
136
|
-
chain: "SC",
|
|
137
|
-
chainId: initializer.chainId,
|
|
138
|
-
address: assetData.address,
|
|
139
|
-
name: SmartChainAssets[ticker]?.name ?? ticker,
|
|
140
|
-
decimals: assetData.decimals,
|
|
141
|
-
displayDecimals: assetData.displayDecimals,
|
|
142
|
-
ticker
|
|
143
|
-
} as any;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
this.TokenResolver[initializer.chainId] = {
|
|
147
|
-
getToken: (address: string) => addressMap[address]
|
|
148
|
-
} as any;
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
newSwapper(options: MultichainSwapperOptions<T>) {
|
|
153
|
-
options.bitcoinNetwork ??= BitcoinNetwork.MAINNET as any;
|
|
154
|
-
options.storagePrefix ??= "atomiqsdk-"+options.bitcoinNetwork+"-";
|
|
155
|
-
options.messenger ??= new NostrMessenger(options.bitcoinNetwork, nostrUrls);
|
|
156
|
-
|
|
157
|
-
options.defaultTrustedIntermediaryUrl ??= trustedIntermediaries[options.bitcoinNetwork];
|
|
158
|
-
|
|
159
|
-
options.registryUrl ??= registries[options.bitcoinNetwork];
|
|
160
|
-
|
|
161
|
-
const mempoolApi = options.mempoolApi ?? new MempoolBitcoinRpc(mempoolUrls[options.bitcoinNetwork]);
|
|
162
|
-
const bitcoinRpc = mempoolApi instanceof MempoolBitcoinRpc ? mempoolApi : new MempoolBitcoinRpc(mempoolApi);
|
|
163
|
-
|
|
164
|
-
const pricingAssets: (RedundantSwapPriceAssets<ToMultichain<T>>[number] & {ticker: string, name: string})[] = [];
|
|
165
|
-
Object.keys(SmartChainAssets).forEach((ticker) => {
|
|
166
|
-
const chains: any = {};
|
|
167
|
-
for(let {tokens, chainId} of this.initializers) {
|
|
168
|
-
if(tokens[ticker]!=null) chains[chainId] = tokens[ticker];
|
|
169
|
-
}
|
|
170
|
-
const assetData = SmartChainAssets[ticker];
|
|
171
|
-
pricingAssets.push({
|
|
172
|
-
...assetData.pricing,
|
|
173
|
-
chains,
|
|
174
|
-
ticker,
|
|
175
|
-
name: assetData.name
|
|
176
|
-
})
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
options.chainStorageCtor ??= (name: string) => new LocalStorageManager(name);
|
|
180
|
-
|
|
181
|
-
const chains: {[key in T[number]["chainId"]]: ChainData<any>} = {} as any;
|
|
182
|
-
for(let {initializer, chainId} of this.initializers) {
|
|
183
|
-
if(options.chains[chainId]==null) continue;
|
|
184
|
-
chains[chainId] = initializer(options.chains[chainId], bitcoinRpc, options.bitcoinNetwork, options.chainStorageCtor) as any;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const swapPricing = options.getPriceFn!=null ?
|
|
188
|
-
new SingleSwapPrice(options.pricingFeeDifferencePPM ?? 10000n, new CustomPriceProvider(pricingAssets.map(val => {
|
|
189
|
-
return {
|
|
190
|
-
coinId: val.ticker,
|
|
191
|
-
chains: val.chains
|
|
192
|
-
}
|
|
193
|
-
}), options.getPriceFn)) :
|
|
194
|
-
RedundantSwapPrice.createFromTokenMap<ToMultichain<T>>(options.pricingFeeDifferencePPM ?? 10000n, pricingAssets);
|
|
195
|
-
|
|
196
|
-
return new Swapper<ToMultichain<T>>(
|
|
197
|
-
bitcoinRpc,
|
|
198
|
-
chains as any,
|
|
199
|
-
swapPricing,
|
|
200
|
-
pricingAssets,
|
|
201
|
-
options.messenger,
|
|
202
|
-
options
|
|
203
|
-
);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
async newSwapperInitialized(options: MultichainSwapperOptions<T>) {
|
|
207
|
-
const swapper = this.newSwapper(options);
|
|
208
|
-
await swapper.init();
|
|
209
|
-
return swapper;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
ChainData,
|
|
3
|
+
BitcoinNetwork,
|
|
4
|
+
BitcoinRpc,
|
|
5
|
+
BaseTokenType,
|
|
6
|
+
ChainType,
|
|
7
|
+
StorageObject,
|
|
8
|
+
IStorageManager, Messenger
|
|
9
|
+
} from "@atomiqlabs/base";
|
|
10
|
+
import {
|
|
11
|
+
BitcoinTokens,
|
|
12
|
+
BtcToken, CustomPriceFunction, CustomPriceProvider,
|
|
13
|
+
MempoolApi,
|
|
14
|
+
MempoolBitcoinRpc, RedundantSwapPrice,
|
|
15
|
+
RedundantSwapPriceAssets, SCToken, SingleSwapPrice, Swapper,
|
|
16
|
+
SwapperOptions
|
|
17
|
+
} from "@atomiqlabs/sdk-lib";
|
|
18
|
+
import {SmartChainAssets} from "./SmartChainAssets";
|
|
19
|
+
import {LocalStorageManager} from "./storage/LocalStorageManager";
|
|
20
|
+
import {NostrMessenger} from "@atomiqlabs/messenger-nostr";
|
|
21
|
+
|
|
22
|
+
type ChainInitializer<O, C extends ChainType, T extends BaseTokenType> = {
|
|
23
|
+
chainId: ChainType["ChainId"],
|
|
24
|
+
chainType: ChainType,
|
|
25
|
+
initializer: (
|
|
26
|
+
options: O,
|
|
27
|
+
bitcoinRelay: BitcoinRpc<any>,
|
|
28
|
+
network: BitcoinNetwork,
|
|
29
|
+
storageCtor: <T extends StorageObject>(name: string) => IStorageManager<T>
|
|
30
|
+
) => ChainData<C>,
|
|
31
|
+
tokens: T,
|
|
32
|
+
options: O
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type TokensDict<T extends ChainInitializer<any, any, any>> = {
|
|
36
|
+
[K in T["chainId"]]: {
|
|
37
|
+
[val in keyof T["tokens"]]: SCToken<K>
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
type GetAllTokens<T extends readonly ChainInitializer<any, any, any>[]> =
|
|
41
|
+
(T extends readonly [infer First extends ChainInitializer<any, any, any>, ...infer Rest extends ChainInitializer<any, any, any>[]]
|
|
42
|
+
? TokensDict<First> & GetAllTokens<Rest>
|
|
43
|
+
: unknown);
|
|
44
|
+
|
|
45
|
+
export type TokenResolverDict<T extends ChainInitializer<any, any, any>> = {[K in T["chainId"]]: {
|
|
46
|
+
getToken: (address: string) => SCToken<K>
|
|
47
|
+
}};
|
|
48
|
+
type GetAllTokenResolvers<T extends readonly ChainInitializer<any, any, any>[]> =
|
|
49
|
+
(T extends readonly [infer First extends ChainInitializer<any, any, any>, ...infer Rest extends ChainInitializer<any, any, any>[]]
|
|
50
|
+
? TokenResolverDict<First> & GetAllTokenResolvers<Rest>
|
|
51
|
+
: unknown);
|
|
52
|
+
|
|
53
|
+
type OptionsDict<T extends ChainInitializer<any, any, any>> = {[K in T["chainId"]]: T["options"]};
|
|
54
|
+
type GetAllOptions<T extends readonly ChainInitializer<any, any, any>[]> =
|
|
55
|
+
(T extends readonly [infer First extends ChainInitializer<any, any, any>, ...infer Rest extends ChainInitializer<any, any, any>[]]
|
|
56
|
+
? OptionsDict<First> & GetAllOptions<Rest>
|
|
57
|
+
: unknown);
|
|
58
|
+
|
|
59
|
+
type ChainTypeDict<T extends ChainInitializer<any, any, any>> = {[K in T["chainId"]]: T["chainType"]};
|
|
60
|
+
type ToMultichain<T extends readonly ChainInitializer<any, any, any>[]> =
|
|
61
|
+
(T extends readonly [infer First extends ChainInitializer<any, any, any>, ...infer Rest extends ChainInitializer<any, any, any>[]]
|
|
62
|
+
? ChainTypeDict<First> & ToMultichain<Rest>
|
|
63
|
+
: {});
|
|
64
|
+
|
|
65
|
+
export type MultichainSwapperOptions<T extends readonly ChainInitializer<any, any, any>[]> = SwapperOptions & {
|
|
66
|
+
chains: GetAllOptions<T>
|
|
67
|
+
} & {
|
|
68
|
+
chainStorageCtor?: <T extends StorageObject>(name: string) => IStorageManager<T>,
|
|
69
|
+
pricingFeeDifferencePPM?: bigint,
|
|
70
|
+
mempoolApi?: MempoolApi | MempoolBitcoinRpc | string | string[],
|
|
71
|
+
messenger?: Messenger,
|
|
72
|
+
getPriceFn?: CustomPriceFunction
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const registries = {
|
|
76
|
+
[BitcoinNetwork.MAINNET]: "https://api.github.com/repos/adambor/SolLightning-registry/contents/registry-mainnet.json?ref=main",
|
|
77
|
+
[BitcoinNetwork.TESTNET]: "https://api.github.com/repos/adambor/SolLightning-registry/contents/registry.json?ref=main",
|
|
78
|
+
[BitcoinNetwork.TESTNET4]: "https://api.github.com/repos/adambor/SolLightning-registry/contents/registry-testnet4.json?ref=main"
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const trustedIntermediaries = {
|
|
82
|
+
[BitcoinNetwork.MAINNET]: "https://node3.gethopa.com:34100",
|
|
83
|
+
[BitcoinNetwork.TESTNET]: "https://node3.gethopa.com:24100"
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const mempoolUrls = {
|
|
87
|
+
[BitcoinNetwork.MAINNET]: [
|
|
88
|
+
"https://mempool.space/api/",
|
|
89
|
+
"https://mempool.holdings/api/",
|
|
90
|
+
"https://mempool.fra.mempool.space/api/",
|
|
91
|
+
"https://mempool.va1.mempool.space/api/",
|
|
92
|
+
"https://mempool.tk7.mempool.space/api/"
|
|
93
|
+
],
|
|
94
|
+
[BitcoinNetwork.TESTNET]: [
|
|
95
|
+
"https://mempool.space/testnet/api/",
|
|
96
|
+
"https://mempool.holdings/testnet/api/",
|
|
97
|
+
"https://mempool.fra.mempool.space/testnet/api/",
|
|
98
|
+
"https://mempool.va1.mempool.space/testnet/api/",
|
|
99
|
+
"https://mempool.tk7.mempool.space/testnet/api/"
|
|
100
|
+
],
|
|
101
|
+
[BitcoinNetwork.TESTNET4]: [
|
|
102
|
+
"https://mempool.space/testnet4/api/",
|
|
103
|
+
"https://mempool.holdings/testnet4/api/",
|
|
104
|
+
"https://mempool.fra.mempool.space/testnet4/api/",
|
|
105
|
+
"https://mempool.va1.mempool.space/testnet4/api/",
|
|
106
|
+
"https://mempool.tk7.mempool.space/testnet4/api/"
|
|
107
|
+
]
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const nostrUrls: string[] = [
|
|
111
|
+
"wss://relay.damus.io", "wss://nostr.einundzwanzig.space", "wss://relay01.lnfi.network/", "wss://relay.puresignal.news/", "wss://relay.fountain.fm/", "wss://sendit.nosflare.com/"
|
|
112
|
+
];
|
|
113
|
+
|
|
114
|
+
export class SwapperFactory<T extends readonly ChainInitializer<any, any, any>[]> {
|
|
115
|
+
|
|
116
|
+
Tokens: GetAllTokens<T> & {
|
|
117
|
+
BITCOIN: {
|
|
118
|
+
BTC: BtcToken<false>,
|
|
119
|
+
BTCLN: BtcToken<true>
|
|
120
|
+
}
|
|
121
|
+
} = {
|
|
122
|
+
BITCOIN: BitcoinTokens
|
|
123
|
+
} as any;
|
|
124
|
+
TokenResolver: GetAllTokenResolvers<T> = {} as any;
|
|
125
|
+
|
|
126
|
+
constructor(readonly initializers: T) {
|
|
127
|
+
this.initializers = initializers;
|
|
128
|
+
initializers.forEach(initializer => {
|
|
129
|
+
const addressMap: {[tokenAddress: string]: SCToken} = {};
|
|
130
|
+
|
|
131
|
+
this.Tokens[initializer.chainId] = {} as any;
|
|
132
|
+
|
|
133
|
+
for(let ticker in initializer.tokens) {
|
|
134
|
+
const assetData = initializer.tokens[ticker] as any;
|
|
135
|
+
this.Tokens[initializer.chainId][ticker] = addressMap[assetData.address] = {
|
|
136
|
+
chain: "SC",
|
|
137
|
+
chainId: initializer.chainId,
|
|
138
|
+
address: assetData.address,
|
|
139
|
+
name: SmartChainAssets[ticker]?.name ?? ticker,
|
|
140
|
+
decimals: assetData.decimals,
|
|
141
|
+
displayDecimals: assetData.displayDecimals,
|
|
142
|
+
ticker
|
|
143
|
+
} as any;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
this.TokenResolver[initializer.chainId] = {
|
|
147
|
+
getToken: (address: string) => addressMap[address]
|
|
148
|
+
} as any;
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
newSwapper(options: MultichainSwapperOptions<T>) {
|
|
153
|
+
options.bitcoinNetwork ??= BitcoinNetwork.MAINNET as any;
|
|
154
|
+
options.storagePrefix ??= "atomiqsdk-"+options.bitcoinNetwork+"-";
|
|
155
|
+
options.messenger ??= new NostrMessenger(options.bitcoinNetwork, nostrUrls);
|
|
156
|
+
|
|
157
|
+
options.defaultTrustedIntermediaryUrl ??= trustedIntermediaries[options.bitcoinNetwork];
|
|
158
|
+
|
|
159
|
+
options.registryUrl ??= registries[options.bitcoinNetwork];
|
|
160
|
+
|
|
161
|
+
const mempoolApi = options.mempoolApi ?? new MempoolBitcoinRpc(mempoolUrls[options.bitcoinNetwork]);
|
|
162
|
+
const bitcoinRpc = mempoolApi instanceof MempoolBitcoinRpc ? mempoolApi : new MempoolBitcoinRpc(mempoolApi);
|
|
163
|
+
|
|
164
|
+
const pricingAssets: (RedundantSwapPriceAssets<ToMultichain<T>>[number] & {ticker: string, name: string})[] = [];
|
|
165
|
+
Object.keys(SmartChainAssets).forEach((ticker) => {
|
|
166
|
+
const chains: any = {};
|
|
167
|
+
for(let {tokens, chainId} of this.initializers) {
|
|
168
|
+
if(tokens[ticker]!=null) chains[chainId] = tokens[ticker];
|
|
169
|
+
}
|
|
170
|
+
const assetData = SmartChainAssets[ticker];
|
|
171
|
+
pricingAssets.push({
|
|
172
|
+
...assetData.pricing,
|
|
173
|
+
chains,
|
|
174
|
+
ticker,
|
|
175
|
+
name: assetData.name
|
|
176
|
+
})
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
options.chainStorageCtor ??= (name: string) => new LocalStorageManager(name);
|
|
180
|
+
|
|
181
|
+
const chains: {[key in T[number]["chainId"]]: ChainData<any>} = {} as any;
|
|
182
|
+
for(let {initializer, chainId} of this.initializers) {
|
|
183
|
+
if(options.chains[chainId]==null) continue;
|
|
184
|
+
chains[chainId] = initializer(options.chains[chainId], bitcoinRpc, options.bitcoinNetwork, options.chainStorageCtor) as any;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const swapPricing = options.getPriceFn!=null ?
|
|
188
|
+
new SingleSwapPrice(options.pricingFeeDifferencePPM ?? 10000n, new CustomPriceProvider(pricingAssets.map(val => {
|
|
189
|
+
return {
|
|
190
|
+
coinId: val.ticker,
|
|
191
|
+
chains: val.chains
|
|
192
|
+
}
|
|
193
|
+
}), options.getPriceFn)) :
|
|
194
|
+
RedundantSwapPrice.createFromTokenMap<ToMultichain<T>>(options.pricingFeeDifferencePPM ?? 10000n, pricingAssets);
|
|
195
|
+
|
|
196
|
+
return new Swapper<ToMultichain<T>>(
|
|
197
|
+
bitcoinRpc,
|
|
198
|
+
chains as any,
|
|
199
|
+
swapPricing,
|
|
200
|
+
pricingAssets,
|
|
201
|
+
options.messenger,
|
|
202
|
+
options
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async newSwapperInitialized(options: MultichainSwapperOptions<T>) {
|
|
207
|
+
const swapper = this.newSwapper(options);
|
|
208
|
+
await swapper.init();
|
|
209
|
+
return swapper;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
}
|
package/src/Utils.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.timeoutSignal = exports.fromHumanReadableString = exports.toHumanReadableString = void 0;
|
|
4
|
-
var sdk_lib_1 = require("@atomiqlabs/sdk-lib");
|
|
5
|
-
function toHumanReadableString(amount, currencySpec) {
|
|
6
|
-
if (amount == null)
|
|
7
|
-
return null;
|
|
8
|
-
return (0, sdk_lib_1.toDecimal)(amount, currencySpec.decimals, undefined, currencySpec.displayDecimals);
|
|
9
|
-
}
|
|
10
|
-
exports.toHumanReadableString = toHumanReadableString;
|
|
11
|
-
function fromHumanReadableString(amount, currencySpec) {
|
|
12
|
-
if (amount === "" || amount == null)
|
|
13
|
-
return null;
|
|
14
|
-
return (0, sdk_lib_1.fromDecimal)(amount, currencySpec.decimals);
|
|
15
|
-
}
|
|
16
|
-
exports.fromHumanReadableString = fromHumanReadableString;
|
|
17
|
-
/**
|
|
18
|
-
* Returns an abort signal that aborts after a specified timeout in milliseconds
|
|
19
|
-
*
|
|
20
|
-
* @param timeout Milliseconds to wait
|
|
21
|
-
* @param abortReason Abort with this abort reason
|
|
22
|
-
* @param abortSignal Abort signal to extend
|
|
23
|
-
*/
|
|
24
|
-
function timeoutSignal(timeout, abortReason, abortSignal) {
|
|
25
|
-
if (timeout == null)
|
|
26
|
-
return abortSignal;
|
|
27
|
-
var abortController = new AbortController();
|
|
28
|
-
var timeoutHandle = setTimeout(function () { return abortController.abort(abortReason || new Error("Timed out")); }, timeout);
|
|
29
|
-
if (abortSignal != null) {
|
|
30
|
-
abortSignal.addEventListener("abort", function () {
|
|
31
|
-
clearTimeout(timeoutHandle);
|
|
32
|
-
abortController.abort(abortSignal.reason);
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
return abortController.signal;
|
|
36
|
-
}
|
|
37
|
-
exports.timeoutSignal = timeoutSignal;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.timeoutSignal = exports.fromHumanReadableString = exports.toHumanReadableString = void 0;
|
|
4
|
+
var sdk_lib_1 = require("@atomiqlabs/sdk-lib");
|
|
5
|
+
function toHumanReadableString(amount, currencySpec) {
|
|
6
|
+
if (amount == null)
|
|
7
|
+
return null;
|
|
8
|
+
return (0, sdk_lib_1.toDecimal)(amount, currencySpec.decimals, undefined, currencySpec.displayDecimals);
|
|
9
|
+
}
|
|
10
|
+
exports.toHumanReadableString = toHumanReadableString;
|
|
11
|
+
function fromHumanReadableString(amount, currencySpec) {
|
|
12
|
+
if (amount === "" || amount == null)
|
|
13
|
+
return null;
|
|
14
|
+
return (0, sdk_lib_1.fromDecimal)(amount, currencySpec.decimals);
|
|
15
|
+
}
|
|
16
|
+
exports.fromHumanReadableString = fromHumanReadableString;
|
|
17
|
+
/**
|
|
18
|
+
* Returns an abort signal that aborts after a specified timeout in milliseconds
|
|
19
|
+
*
|
|
20
|
+
* @param timeout Milliseconds to wait
|
|
21
|
+
* @param abortReason Abort with this abort reason
|
|
22
|
+
* @param abortSignal Abort signal to extend
|
|
23
|
+
*/
|
|
24
|
+
function timeoutSignal(timeout, abortReason, abortSignal) {
|
|
25
|
+
if (timeout == null)
|
|
26
|
+
return abortSignal;
|
|
27
|
+
var abortController = new AbortController();
|
|
28
|
+
var timeoutHandle = setTimeout(function () { return abortController.abort(abortReason || new Error("Timed out")); }, timeout);
|
|
29
|
+
if (abortSignal != null) {
|
|
30
|
+
abortSignal.addEventListener("abort", function () {
|
|
31
|
+
clearTimeout(timeoutHandle);
|
|
32
|
+
abortController.abort(abortSignal.reason);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return abortController.signal;
|
|
36
|
+
}
|
|
37
|
+
exports.timeoutSignal = timeoutSignal;
|
package/src/Utils.ts
CHANGED
|
@@ -1,31 +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
|
-
}
|
|
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
|
+
}
|