@atomiqlabs/lp-lib 11.0.5 → 12.0.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/dist/info/InfoHandler.js +3 -12
- package/dist/plugins/IPlugin.d.ts +34 -35
- package/dist/plugins/IPlugin.js +7 -8
- package/dist/plugins/PluginManager.d.ts +29 -30
- package/dist/plugins/PluginManager.js +160 -188
- package/dist/prices/BinanceSwapPrice.d.ts +1 -2
- package/dist/prices/BinanceSwapPrice.js +56 -70
- package/dist/prices/CoinGeckoSwapPrice.d.ts +2 -3
- package/dist/prices/CoinGeckoSwapPrice.js +30 -42
- package/dist/prices/OKXSwapPrice.d.ts +1 -2
- package/dist/prices/OKXSwapPrice.js +56 -70
- package/dist/storage/IIntermediaryStorage.d.ts +8 -5
- package/dist/storagemanager/IntermediaryStorageManager.d.ts +8 -5
- package/dist/storagemanager/IntermediaryStorageManager.js +52 -61
- package/dist/storagemanager/StorageManager.js +42 -59
- package/dist/swaps/FromBtcBaseSwap.d.ts +7 -8
- package/dist/swaps/FromBtcBaseSwap.js +2 -3
- package/dist/swaps/FromBtcBaseSwapHandler.d.ts +44 -31
- package/dist/swaps/FromBtcBaseSwapHandler.js +217 -196
- package/dist/swaps/FromBtcLnBaseSwapHandler.d.ts +1 -2
- package/dist/swaps/FromBtcLnBaseSwapHandler.js +13 -24
- package/dist/swaps/ISwapPrice.d.ts +4 -5
- package/dist/swaps/ISwapPrice.js +8 -30
- package/dist/swaps/SwapHandler.d.ts +26 -15
- package/dist/swaps/SwapHandler.js +107 -66
- package/dist/swaps/SwapHandlerSwap.d.ts +22 -11
- package/dist/swaps/SwapHandlerSwap.js +32 -8
- package/dist/swaps/ToBtcBaseSwap.d.ts +16 -17
- package/dist/swaps/ToBtcBaseSwap.js +4 -4
- package/dist/swaps/ToBtcBaseSwapHandler.d.ts +17 -17
- package/dist/swaps/ToBtcBaseSwapHandler.js +140 -155
- package/dist/swaps/frombtc_abstract/FromBtcAbs.d.ts +8 -15
- package/dist/swaps/frombtc_abstract/FromBtcAbs.js +131 -196
- package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.d.ts +3 -5
- package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.js +4 -4
- package/dist/swaps/frombtc_trusted/FromBtcTrusted.d.ts +10 -8
- package/dist/swaps/frombtc_trusted/FromBtcTrusted.js +330 -362
- package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.d.ts +10 -10
- package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.js +7 -4
- package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.d.ts +8 -8
- package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.js +330 -400
- package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +10 -4
- package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.js +36 -6
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.d.ts +8 -7
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.js +251 -273
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +5 -5
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.js +7 -5
- package/dist/swaps/tobtc_abstract/ToBtcAbs.d.ts +13 -12
- package/dist/swaps/tobtc_abstract/ToBtcAbs.js +261 -323
- package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.d.ts +4 -4
- package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.js +7 -6
- package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.d.ts +22 -21
- package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.js +398 -453
- package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +3 -2
- package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.js +10 -6
- package/dist/utils/Utils.d.ts +2 -3
- package/dist/utils/Utils.js +4 -14
- package/dist/utils/paramcoders/LegacyParamEncoder.js +3 -14
- package/dist/utils/paramcoders/ParamDecoder.js +53 -65
- package/dist/utils/paramcoders/SchemaVerifier.d.ts +4 -5
- package/dist/utils/paramcoders/SchemaVerifier.js +8 -9
- package/dist/utils/paramcoders/server/ServerParamDecoder.js +1 -1
- package/dist/utils/paramcoders/server/ServerParamEncoder.js +3 -14
- package/dist/wallets/IBitcoinWallet.d.ts +4 -5
- package/dist/wallets/ILightningWallet.d.ts +20 -21
- package/dist/wallets/ILightningWallet.js +1 -1
- package/package.json +4 -6
- package/src/plugins/IPlugin.ts +28 -29
- package/src/plugins/PluginManager.ts +21 -22
- package/src/prices/BinanceSwapPrice.ts +3 -4
- package/src/prices/CoinGeckoSwapPrice.ts +4 -5
- package/src/prices/OKXSwapPrice.ts +3 -4
- package/src/storage/IIntermediaryStorage.ts +4 -5
- package/src/storagemanager/IntermediaryStorageManager.ts +17 -9
- package/src/swaps/FromBtcBaseSwap.ts +9 -10
- package/src/swaps/FromBtcBaseSwapHandler.ts +133 -91
- package/src/swaps/FromBtcLnBaseSwapHandler.ts +2 -3
- package/src/swaps/ISwapPrice.ts +10 -20
- package/src/swaps/SwapHandler.ts +101 -35
- package/src/swaps/SwapHandlerSwap.ts +42 -17
- package/src/swaps/ToBtcBaseSwap.ts +20 -18
- package/src/swaps/ToBtcBaseSwapHandler.ts +33 -33
- package/src/swaps/frombtc_abstract/FromBtcAbs.ts +64 -97
- package/src/swaps/frombtc_abstract/FromBtcSwapAbs.ts +7 -8
- package/src/swaps/frombtc_trusted/FromBtcTrusted.ts +56 -55
- package/src/swaps/frombtc_trusted/FromBtcTrustedSwap.ts +28 -21
- package/src/swaps/frombtcln_abstract/FromBtcLnAbs.ts +81 -116
- package/src/swaps/frombtcln_abstract/FromBtcLnSwapAbs.ts +73 -9
- package/src/swaps/frombtcln_trusted/FromBtcLnTrusted.ts +39 -36
- package/src/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.ts +26 -12
- package/src/swaps/tobtc_abstract/ToBtcAbs.ts +82 -98
- package/src/swaps/tobtc_abstract/ToBtcSwapAbs.ts +25 -20
- package/src/swaps/tobtcln_abstract/ToBtcLnAbs.ts +106 -118
- package/src/swaps/tobtcln_abstract/ToBtcLnSwapAbs.ts +17 -9
- package/src/utils/Utils.ts +3 -4
- package/src/utils/paramcoders/ParamDecoder.ts +5 -5
- package/src/utils/paramcoders/SchemaVerifier.ts +10 -11
- package/src/utils/paramcoders/server/ServerParamDecoder.ts +1 -1
- package/src/wallets/IBitcoinWallet.ts +4 -5
- package/src/wallets/ILightningWallet.ts +21 -22
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {SwapHandlerSwap} from "./SwapHandlerSwap";
|
|
2
2
|
import {SwapData} from "@atomiqlabs/base";
|
|
3
3
|
import {RequestData, SwapBaseConfig, SwapHandler} from "./SwapHandler";
|
|
4
|
-
import * as BN from "bn.js";
|
|
5
4
|
import {IParamReader} from "../utils/paramcoders/IParamReader";
|
|
6
5
|
import {FieldTypeEnum} from "../utils/paramcoders/SchemaVerifier";
|
|
7
6
|
import {FromBtcLnRequestType} from "./frombtcln_abstract/FromBtcLnAbs";
|
|
@@ -14,7 +13,7 @@ import {
|
|
|
14
13
|
import {Request} from "express";
|
|
15
14
|
import {FromBtcLnTrustedRequestType} from "./frombtcln_trusted/FromBtcLnTrusted";
|
|
16
15
|
|
|
17
|
-
const secondsInYear =
|
|
16
|
+
const secondsInYear = BigInt(365*24*60*60);
|
|
18
17
|
|
|
19
18
|
export type FromBtcBaseConfig = SwapBaseConfig & {
|
|
20
19
|
securityDepositAPY: number
|
|
@@ -29,27 +28,35 @@ export abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData,
|
|
|
29
28
|
*
|
|
30
29
|
* @param chainIdentifier
|
|
31
30
|
* @param useToken
|
|
31
|
+
* @param depositToken
|
|
32
32
|
* @param abortController
|
|
33
33
|
*/
|
|
34
|
-
protected getFromBtcPricePrefetches(chainIdentifier: string, useToken: string, abortController: AbortController): {
|
|
35
|
-
pricePrefetchPromise: Promise<
|
|
36
|
-
|
|
34
|
+
protected getFromBtcPricePrefetches(chainIdentifier: string, useToken: string, depositToken: string, abortController: AbortController): {
|
|
35
|
+
pricePrefetchPromise: Promise<bigint>,
|
|
36
|
+
gasTokenPricePrefetchPromise: Promise<bigint>,
|
|
37
|
+
depositTokenPricePrefetchPromise: Promise<bigint>
|
|
37
38
|
} {
|
|
38
|
-
const pricePrefetchPromise: Promise<
|
|
39
|
+
const pricePrefetchPromise: Promise<bigint> = this.swapPricing.preFetchPrice(useToken, chainIdentifier).catch(e => {
|
|
39
40
|
this.logger.error("getFromBtcPricePrefetches(): pricePrefetch error: ", e);
|
|
40
41
|
abortController.abort(e);
|
|
41
42
|
return null;
|
|
42
43
|
});
|
|
43
44
|
const {swapContract} = this.getChain(chainIdentifier);
|
|
44
|
-
const
|
|
45
|
+
const gasTokenPricePrefetchPromise: Promise<bigint> = useToken.toString()===swapContract.getNativeCurrencyAddress().toString() ?
|
|
45
46
|
pricePrefetchPromise :
|
|
46
47
|
this.swapPricing.preFetchPrice(swapContract.getNativeCurrencyAddress(), chainIdentifier).catch(e => {
|
|
47
|
-
this.logger.error("getFromBtcPricePrefetches():
|
|
48
|
+
this.logger.error("getFromBtcPricePrefetches(): gasTokenPricePrefetchPromise error: ", e);
|
|
48
49
|
abortController.abort(e);
|
|
49
50
|
return null;
|
|
50
51
|
});
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
const depositTokenPricePrefetchPromise: Promise<bigint> = depositToken===swapContract.getNativeCurrencyAddress() ?
|
|
53
|
+
gasTokenPricePrefetchPromise :
|
|
54
|
+
this.swapPricing.preFetchPrice(depositToken, chainIdentifier).catch(e => {
|
|
55
|
+
this.logger.error("getFromBtcPricePrefetches(): depositTokenPricePrefetchPromise error: ", e);
|
|
56
|
+
abortController.abort(e);
|
|
57
|
+
return null;
|
|
58
|
+
});
|
|
59
|
+
return {pricePrefetchPromise, gasTokenPricePrefetchPromise, depositTokenPricePrefetchPromise};
|
|
53
60
|
}
|
|
54
61
|
|
|
55
62
|
/**
|
|
@@ -57,14 +64,23 @@ export abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData,
|
|
|
57
64
|
*
|
|
58
65
|
* @param chainIdentifier
|
|
59
66
|
* @param dummySwapData
|
|
67
|
+
* @param depositToken
|
|
68
|
+
* @param gasTokenPricePrefetchPromise
|
|
69
|
+
* @param depositTokenPricePrefetchPromise
|
|
60
70
|
* @param abortController
|
|
61
71
|
*/
|
|
62
|
-
protected async getBaseSecurityDepositPrefetch(
|
|
72
|
+
protected async getBaseSecurityDepositPrefetch(
|
|
73
|
+
chainIdentifier: string, dummySwapData: SwapData, depositToken: string,
|
|
74
|
+
gasTokenPricePrefetchPromise: Promise<bigint>, depositTokenPricePrefetchPromise: Promise<bigint>,
|
|
75
|
+
abortController: AbortController
|
|
76
|
+
): Promise<bigint> {
|
|
63
77
|
//Solana workaround
|
|
64
78
|
const {swapContract} = this.getChain(chainIdentifier);
|
|
79
|
+
let feeResult: bigint;
|
|
80
|
+
const gasToken = swapContract.getNativeCurrencyAddress();
|
|
65
81
|
if (swapContract.getRawRefundFee != null) {
|
|
66
82
|
try {
|
|
67
|
-
|
|
83
|
+
feeResult = await swapContract.getRawRefundFee(dummySwapData);
|
|
68
84
|
} catch (e) {
|
|
69
85
|
this.logger.error("getBaseSecurityDepositPrefetch(): pre-fetch error: ", e);
|
|
70
86
|
abortController.abort(e);
|
|
@@ -72,14 +88,17 @@ export abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData,
|
|
|
72
88
|
}
|
|
73
89
|
} else {
|
|
74
90
|
try {
|
|
75
|
-
|
|
76
|
-
return result.mul(new BN(2));
|
|
91
|
+
feeResult = await swapContract.getRefundFee(dummySwapData);
|
|
77
92
|
} catch (e1) {
|
|
78
93
|
this.logger.error("getBaseSecurityDepositPrefetch(): pre-fetch error: ", e1);
|
|
79
94
|
abortController.abort(e1);
|
|
80
95
|
return null;
|
|
81
96
|
}
|
|
82
97
|
}
|
|
98
|
+
feeResult = feeResult * 2n;
|
|
99
|
+
if(gasToken===depositToken) return feeResult;
|
|
100
|
+
const btcValue = await this.swapPricing.getToBtcSwapAmount(feeResult, gasToken, chainIdentifier, true, gasTokenPricePrefetchPromise);
|
|
101
|
+
return await this.swapPricing.getFromBtcSwapAmount(btcValue, depositToken, chainIdentifier, true, depositTokenPricePrefetchPromise);
|
|
83
102
|
}
|
|
84
103
|
|
|
85
104
|
/**
|
|
@@ -89,7 +108,7 @@ export abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData,
|
|
|
89
108
|
* @param useToken
|
|
90
109
|
* @param abortController
|
|
91
110
|
*/
|
|
92
|
-
protected async getBalancePrefetch(chainIdentifier: string, useToken: string, abortController: AbortController): Promise<
|
|
111
|
+
protected async getBalancePrefetch(chainIdentifier: string, useToken: string, abortController: AbortController): Promise<bigint> {
|
|
93
112
|
const {swapContract, signer} = this.getChain(chainIdentifier);
|
|
94
113
|
try {
|
|
95
114
|
return await swapContract.getBalance(signer.getAddress(), useToken, true);
|
|
@@ -108,11 +127,11 @@ export abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData,
|
|
|
108
127
|
* @param signal
|
|
109
128
|
* @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
|
|
110
129
|
*/
|
|
111
|
-
protected async checkBalance(totalInToken:
|
|
130
|
+
protected async checkBalance(totalInToken: bigint, balancePrefetch: Promise<bigint>, signal: AbortSignal | null): Promise<void> {
|
|
112
131
|
const balance = await balancePrefetch;
|
|
113
132
|
if(signal!=null) signal.throwIfAborted();
|
|
114
133
|
|
|
115
|
-
if(balance==null || balance
|
|
134
|
+
if(balance==null || balance < totalInToken) {
|
|
116
135
|
throw {
|
|
117
136
|
code: 20002,
|
|
118
137
|
msg: "Not enough liquidity"
|
|
@@ -120,6 +139,28 @@ export abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData,
|
|
|
120
139
|
}
|
|
121
140
|
}
|
|
122
141
|
|
|
142
|
+
/**
|
|
143
|
+
* Checks if the specified token is allowed as a deposit token
|
|
144
|
+
*
|
|
145
|
+
* @param chainIdentifier
|
|
146
|
+
* @param depositToken
|
|
147
|
+
* @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
|
|
148
|
+
*/
|
|
149
|
+
protected checkAllowedDepositToken(chainIdentifier: string, depositToken: string): void {
|
|
150
|
+
const {swapContract, allowedDepositTokens} = this.getChain(chainIdentifier);
|
|
151
|
+
if(allowedDepositTokens==null) {
|
|
152
|
+
if(depositToken!==swapContract.getNativeCurrencyAddress()) throw {
|
|
153
|
+
code: 20190,
|
|
154
|
+
msg: "Unsupported deposit token"
|
|
155
|
+
};
|
|
156
|
+
} else {
|
|
157
|
+
if(!allowedDepositTokens.includes(depositToken)) throw {
|
|
158
|
+
code: 20190,
|
|
159
|
+
msg: "Unsupported deposit token"
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
123
164
|
|
|
124
165
|
/**
|
|
125
166
|
* Checks minimums/maximums, calculates the fee & total amount
|
|
@@ -131,9 +172,9 @@ export abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData,
|
|
|
131
172
|
*/
|
|
132
173
|
protected async preCheckAmounts(
|
|
133
174
|
request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType>,
|
|
134
|
-
requestedAmount: {input: boolean, amount:
|
|
175
|
+
requestedAmount: {input: boolean, amount: bigint},
|
|
135
176
|
useToken: string
|
|
136
|
-
): Promise<{baseFee:
|
|
177
|
+
): Promise<{baseFee: bigint, feePPM: bigint}> {
|
|
137
178
|
const res = await PluginManager.onHandlePreFromBtcQuote(
|
|
138
179
|
request,
|
|
139
180
|
requestedAmount,
|
|
@@ -171,16 +212,16 @@ export abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData,
|
|
|
171
212
|
*/
|
|
172
213
|
protected async checkFromBtcAmount(
|
|
173
214
|
request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType>,
|
|
174
|
-
requestedAmount: {input: boolean, amount:
|
|
175
|
-
fees: {baseFee:
|
|
215
|
+
requestedAmount: {input: boolean, amount: bigint},
|
|
216
|
+
fees: {baseFee: bigint, feePPM: bigint},
|
|
176
217
|
useToken: string,
|
|
177
218
|
signal: AbortSignal,
|
|
178
|
-
pricePrefetchPromise: Promise<
|
|
219
|
+
pricePrefetchPromise: Promise<bigint> = Promise.resolve(null)
|
|
179
220
|
): Promise<{
|
|
180
|
-
amountBD:
|
|
181
|
-
swapFee:
|
|
182
|
-
swapFeeInToken:
|
|
183
|
-
totalInToken:
|
|
221
|
+
amountBD: bigint,
|
|
222
|
+
swapFee: bigint, //Swap fee in BTC
|
|
223
|
+
swapFeeInToken: bigint, //Swap fee in token on top of what should be paid out to the user
|
|
224
|
+
totalInToken: bigint //Total to be paid out to the user
|
|
184
225
|
}> {
|
|
185
226
|
const chainIdentifier = request.chainIdentifier;
|
|
186
227
|
|
|
@@ -203,7 +244,7 @@ export abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData,
|
|
|
203
244
|
if(isPluginQuote(res)) {
|
|
204
245
|
if(!requestedAmount.input) {
|
|
205
246
|
return {
|
|
206
|
-
amountBD: res.amount.amount
|
|
247
|
+
amountBD: res.amount.amount + res.swapFee.inInputTokens,
|
|
207
248
|
swapFee: res.swapFee.inInputTokens,
|
|
208
249
|
swapFeeInToken: res.swapFee.inOutputTokens,
|
|
209
250
|
totalInToken: requestedAmount.amount
|
|
@@ -219,19 +260,19 @@ export abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData,
|
|
|
219
260
|
}
|
|
220
261
|
}
|
|
221
262
|
|
|
222
|
-
let amountBD:
|
|
263
|
+
let amountBD: bigint;
|
|
223
264
|
if(!requestedAmount.input) {
|
|
224
265
|
amountBD = await this.swapPricing.getToBtcSwapAmount(requestedAmount.amount, useToken, chainIdentifier, true, pricePrefetchPromise);
|
|
225
266
|
signal.throwIfAborted();
|
|
226
267
|
|
|
227
268
|
// amt = (amt+base_fee)/(1-fee)
|
|
228
|
-
amountBD = amountBD
|
|
269
|
+
amountBD = (amountBD + fees.baseFee) * 1000000n / (1000000n - fees.feePPM);
|
|
229
270
|
|
|
230
|
-
const tooLow = amountBD
|
|
231
|
-
const tooHigh = amountBD
|
|
271
|
+
const tooLow = amountBD < (this.config.min * 95n / 100n);
|
|
272
|
+
const tooHigh = amountBD > (this.config.max * 105n / 100n);
|
|
232
273
|
if(tooLow || tooHigh) {
|
|
233
|
-
const adjustedMin = this.config.min
|
|
234
|
-
const adjustedMax = this.config.max
|
|
274
|
+
const adjustedMin = this.config.min * (1000000n - fees.feePPM) / (1000000n - fees.baseFee);
|
|
275
|
+
const adjustedMax = this.config.max * (1000000n - fees.feePPM) / (1000000n - fees.baseFee);
|
|
235
276
|
const minIn = await this.swapPricing.getFromBtcSwapAmount(
|
|
236
277
|
adjustedMin, useToken, chainIdentifier, null, pricePrefetchPromise
|
|
237
278
|
);
|
|
@@ -252,16 +293,16 @@ export abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData,
|
|
|
252
293
|
this.checkBtcAmountInBounds(amountBD);
|
|
253
294
|
}
|
|
254
295
|
|
|
255
|
-
const swapFee = fees.baseFee
|
|
296
|
+
const swapFee = fees.baseFee + (amountBD * fees.feePPM / 1000000n);
|
|
256
297
|
const swapFeeInToken = await this.swapPricing.getFromBtcSwapAmount(swapFee, useToken, chainIdentifier, true, pricePrefetchPromise);
|
|
257
298
|
signal.throwIfAborted();
|
|
258
299
|
|
|
259
|
-
let totalInToken:
|
|
300
|
+
let totalInToken: bigint;
|
|
260
301
|
if(!requestedAmount.input) {
|
|
261
302
|
totalInToken = requestedAmount.amount;
|
|
262
303
|
} else {
|
|
263
304
|
const amountInToken = await this.swapPricing.getFromBtcSwapAmount(requestedAmount.amount, useToken, chainIdentifier, null, pricePrefetchPromise);
|
|
264
|
-
totalInToken = amountInToken
|
|
305
|
+
totalInToken = amountInToken - swapFeeInToken;
|
|
265
306
|
signal.throwIfAborted();
|
|
266
307
|
}
|
|
267
308
|
|
|
@@ -273,60 +314,6 @@ export abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData,
|
|
|
273
314
|
}
|
|
274
315
|
}
|
|
275
316
|
|
|
276
|
-
/**
|
|
277
|
-
* Calculates the required security deposit
|
|
278
|
-
*
|
|
279
|
-
* @param chainIdentifier
|
|
280
|
-
* @param amountBD
|
|
281
|
-
* @param swapFee
|
|
282
|
-
* @param expiryTimeout
|
|
283
|
-
* @param baseSecurityDepositPromise
|
|
284
|
-
* @param securityDepositPricePrefetchPromise
|
|
285
|
-
* @param signal
|
|
286
|
-
* @param metadata
|
|
287
|
-
*/
|
|
288
|
-
protected async getSecurityDeposit(
|
|
289
|
-
chainIdentifier: string,
|
|
290
|
-
amountBD: BN,
|
|
291
|
-
swapFee: BN,
|
|
292
|
-
expiryTimeout: BN,
|
|
293
|
-
baseSecurityDepositPromise: Promise<BN>,
|
|
294
|
-
securityDepositPricePrefetchPromise: Promise<BN>,
|
|
295
|
-
signal: AbortSignal,
|
|
296
|
-
metadata: any
|
|
297
|
-
): Promise<BN> {
|
|
298
|
-
let baseSD: BN = await baseSecurityDepositPromise;
|
|
299
|
-
|
|
300
|
-
signal.throwIfAborted();
|
|
301
|
-
|
|
302
|
-
metadata.times.refundFeeFetched = Date.now();
|
|
303
|
-
|
|
304
|
-
const {swapContract} = this.getChain(chainIdentifier);
|
|
305
|
-
|
|
306
|
-
const swapValueInNativeCurrency = await this.swapPricing.getFromBtcSwapAmount(
|
|
307
|
-
amountBD.sub(swapFee),
|
|
308
|
-
swapContract.getNativeCurrencyAddress(),
|
|
309
|
-
chainIdentifier,
|
|
310
|
-
true,
|
|
311
|
-
securityDepositPricePrefetchPromise
|
|
312
|
-
);
|
|
313
|
-
|
|
314
|
-
signal.throwIfAborted();
|
|
315
|
-
|
|
316
|
-
const apyPPM = new BN(Math.floor(this.config.securityDepositAPY*1000000));
|
|
317
|
-
const variableSD = swapValueInNativeCurrency.mul(apyPPM).mul(expiryTimeout).div(new BN(1000000)).div(secondsInYear);
|
|
318
|
-
|
|
319
|
-
this.logger.debug(
|
|
320
|
-
"getSecurityDeposit(): base security deposit: "+baseSD.toString(10)+
|
|
321
|
-
" swap output in native: "+swapValueInNativeCurrency.toString(10)+
|
|
322
|
-
" apy ppm: "+apyPPM.toString(10)+
|
|
323
|
-
" expiry timeout: "+expiryTimeout.toString(10)+
|
|
324
|
-
" variable security deposit: "+variableSD.toString(10)
|
|
325
|
-
);
|
|
326
|
-
|
|
327
|
-
return baseSD.add(variableSD);
|
|
328
|
-
}
|
|
329
|
-
|
|
330
317
|
/**
|
|
331
318
|
* Signs the created swap
|
|
332
319
|
*
|
|
@@ -364,7 +351,7 @@ export abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData,
|
|
|
364
351
|
const sigData = await swapContract.getInitSignature(
|
|
365
352
|
signer,
|
|
366
353
|
swapObject,
|
|
367
|
-
this.
|
|
354
|
+
this.getInitAuthorizationTimeout(chainIdentifier),
|
|
368
355
|
prefetchedSignData,
|
|
369
356
|
feeRate
|
|
370
357
|
);
|
|
@@ -376,4 +363,59 @@ export abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData,
|
|
|
376
363
|
};
|
|
377
364
|
}
|
|
378
365
|
|
|
366
|
+
/**
|
|
367
|
+
* Calculates the required security deposit
|
|
368
|
+
*
|
|
369
|
+
* @param chainIdentifier
|
|
370
|
+
* @param amountBD
|
|
371
|
+
* @param swapFee
|
|
372
|
+
* @param expiryTimeout
|
|
373
|
+
* @param baseSecurityDepositPromise
|
|
374
|
+
* @param depositToken
|
|
375
|
+
* @param depositTokenPricePrefetchPromise
|
|
376
|
+
* @param signal
|
|
377
|
+
* @param metadata
|
|
378
|
+
*/
|
|
379
|
+
protected async getSecurityDeposit(
|
|
380
|
+
chainIdentifier: string,
|
|
381
|
+
amountBD: bigint,
|
|
382
|
+
swapFee: bigint,
|
|
383
|
+
expiryTimeout: bigint,
|
|
384
|
+
baseSecurityDepositPromise: Promise<bigint>,
|
|
385
|
+
depositToken: string,
|
|
386
|
+
depositTokenPricePrefetchPromise: Promise<bigint>,
|
|
387
|
+
signal: AbortSignal,
|
|
388
|
+
metadata: any
|
|
389
|
+
): Promise<bigint> {
|
|
390
|
+
let baseSD: bigint = await baseSecurityDepositPromise;
|
|
391
|
+
|
|
392
|
+
signal.throwIfAborted();
|
|
393
|
+
|
|
394
|
+
metadata.times.refundFeeFetched = Date.now();
|
|
395
|
+
|
|
396
|
+
const swapValueInDepositToken = await this.swapPricing.getFromBtcSwapAmount(
|
|
397
|
+
amountBD - swapFee,
|
|
398
|
+
depositToken,
|
|
399
|
+
chainIdentifier,
|
|
400
|
+
true,
|
|
401
|
+
depositTokenPricePrefetchPromise
|
|
402
|
+
);
|
|
403
|
+
|
|
404
|
+
signal.throwIfAborted();
|
|
405
|
+
|
|
406
|
+
const apyPPM = BigInt(Math.floor(this.config.securityDepositAPY*1000000));
|
|
407
|
+
const variableSD = swapValueInDepositToken * apyPPM * expiryTimeout / 1000000n / secondsInYear;
|
|
408
|
+
|
|
409
|
+
this.logger.debug(
|
|
410
|
+
"getSecurityDeposit(): base security deposit: "+baseSD.toString(10)+
|
|
411
|
+
" deposit token: "+depositToken+
|
|
412
|
+
" swap output in deposit token: "+swapValueInDepositToken.toString(10)+
|
|
413
|
+
" apy ppm: "+apyPPM.toString(10)+
|
|
414
|
+
" expiry timeout: "+expiryTimeout.toString(10)+
|
|
415
|
+
" variable security deposit: "+variableSD.toString(10)
|
|
416
|
+
);
|
|
417
|
+
|
|
418
|
+
return baseSD + variableSD;
|
|
419
|
+
}
|
|
420
|
+
|
|
379
421
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {SwapHandlerSwap} from "./SwapHandlerSwap";
|
|
2
2
|
import {SwapData} from "@atomiqlabs/base";
|
|
3
3
|
import {FromBtcBaseSwapHandler} from "./FromBtcBaseSwapHandler";
|
|
4
|
-
import * as BN from "bn.js";
|
|
5
4
|
import {ILightningWallet, LightningNetworkChannel} from "../wallets/ILightningWallet";
|
|
6
5
|
import {IIntermediaryStorage} from "../storage/IIntermediaryStorage";
|
|
7
6
|
import {MultichainData} from "./SwapHandler";
|
|
@@ -31,14 +30,14 @@ export abstract class FromBtcLnBaseSwapHandler<V extends SwapHandlerSwap<SwapDat
|
|
|
31
30
|
* @param signal
|
|
32
31
|
* @throws {DefinedRuntimeError} will throw an error if there isn't enough inbound liquidity to receive the LN payment
|
|
33
32
|
*/
|
|
34
|
-
protected async checkInboundLiquidity(amountBD:
|
|
33
|
+
protected async checkInboundLiquidity(amountBD: bigint, channelsPrefetch: Promise<LightningNetworkChannel[]>, signal: AbortSignal) {
|
|
35
34
|
const channelsResponse = await channelsPrefetch;
|
|
36
35
|
|
|
37
36
|
signal.throwIfAborted();
|
|
38
37
|
|
|
39
38
|
let hasEnoughInboundLiquidity = false;
|
|
40
39
|
channelsResponse.forEach(channel => {
|
|
41
|
-
if(channel.remoteBalance
|
|
40
|
+
if(channel.remoteBalance >= amountBD) hasEnoughInboundLiquidity = true;
|
|
42
41
|
});
|
|
43
42
|
if(!hasEnoughInboundLiquidity) {
|
|
44
43
|
throw {
|
package/src/swaps/ISwapPrice.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import * as BN from "bn.js";
|
|
2
|
-
|
|
3
1
|
export type ISwapPriceCoinsMap<T extends {decimals: number}> = {
|
|
4
2
|
[chainId: string]: {
|
|
5
3
|
[address: string]: T
|
|
@@ -19,7 +17,7 @@ export abstract class ISwapPrice<T extends {decimals: number} = {decimals: numbe
|
|
|
19
17
|
*
|
|
20
18
|
* @param tokenData
|
|
21
19
|
*/
|
|
22
|
-
protected abstract getPrice(tokenData: T): Promise<
|
|
20
|
+
protected abstract getPrice(tokenData: T): Promise<bigint>;
|
|
23
21
|
|
|
24
22
|
getTokenData(tokenAddress: string, chainId: string): T {
|
|
25
23
|
const chainTokens = this.coinsMap[chainId];
|
|
@@ -32,7 +30,7 @@ export abstract class ISwapPrice<T extends {decimals: number} = {decimals: numbe
|
|
|
32
30
|
return coin;
|
|
33
31
|
}
|
|
34
32
|
|
|
35
|
-
preFetchPrice(token: string, chainId: string): Promise<
|
|
33
|
+
preFetchPrice(token: string, chainId: string): Promise<bigint> {
|
|
36
34
|
const coin = this.getTokenData(token, chainId);
|
|
37
35
|
return this.getPrice(coin);
|
|
38
36
|
}
|
|
@@ -47,21 +45,17 @@ export abstract class ISwapPrice<T extends {decimals: number} = {decimals: numbe
|
|
|
47
45
|
* @param preFetch Price pre-fetch promise returned from preFetchPrice()
|
|
48
46
|
*/
|
|
49
47
|
async getToBtcSwapAmount(
|
|
50
|
-
fromAmount:
|
|
48
|
+
fromAmount:bigint,
|
|
51
49
|
fromToken: string,
|
|
52
50
|
tokenChainIdentification: string,
|
|
53
51
|
roundUp?: boolean,
|
|
54
|
-
preFetch?: Promise<
|
|
55
|
-
): Promise<
|
|
52
|
+
preFetch?: Promise<bigint>
|
|
53
|
+
): Promise<bigint> {
|
|
56
54
|
const coin = this.getTokenData(fromToken, tokenChainIdentification);
|
|
57
55
|
|
|
58
56
|
const price = (preFetch==null ? null : await preFetch) || await this.getPrice(coin);
|
|
59
57
|
|
|
60
|
-
return fromAmount
|
|
61
|
-
.mul(price)
|
|
62
|
-
.div(new BN(10).pow(new BN(coin.decimals)))
|
|
63
|
-
.add(roundUp ? new BN(999999) : new BN(0))
|
|
64
|
-
.div(new BN(1000000));
|
|
58
|
+
return ((fromAmount * price * (10n ** BigInt(coin.decimals))) + (roundUp ? 999999n : 0n)) / 1000000n;
|
|
65
59
|
}
|
|
66
60
|
|
|
67
61
|
/**
|
|
@@ -74,21 +68,17 @@ export abstract class ISwapPrice<T extends {decimals: number} = {decimals: numbe
|
|
|
74
68
|
* @param preFetch Price pre-fetch promise returned from preFetchPrice()
|
|
75
69
|
*/
|
|
76
70
|
async getFromBtcSwapAmount(
|
|
77
|
-
fromAmount:
|
|
71
|
+
fromAmount: bigint,
|
|
78
72
|
toToken: string,
|
|
79
73
|
tokenChainIdentification: string,
|
|
80
74
|
roundUp?: boolean,
|
|
81
|
-
preFetch?: Promise<
|
|
82
|
-
): Promise<
|
|
75
|
+
preFetch?: Promise<bigint>
|
|
76
|
+
): Promise<bigint> {
|
|
83
77
|
const coin = this.getTokenData(toToken, tokenChainIdentification);
|
|
84
78
|
|
|
85
79
|
const price = (preFetch==null ? null : await preFetch) || await this.getPrice(coin);
|
|
86
80
|
|
|
87
|
-
return fromAmount
|
|
88
|
-
.mul(new BN(10).pow(new BN(coin.decimals)))
|
|
89
|
-
.mul(new BN(1000000)) //To usat
|
|
90
|
-
.add(roundUp ? price.sub(new BN(1)) : new BN(0))
|
|
91
|
-
.div(price)
|
|
81
|
+
return ((fromAmount * (10n ** BigInt(coin.decimals)) * 1000000n) + (roundUp ? price - 1n : 0n)) / price;
|
|
92
82
|
}
|
|
93
83
|
|
|
94
84
|
}
|