@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 { FromBtcLnRequestType } from "./frombtcln_abstract/FromBtcLnAbs";
|
|
7
6
|
import { FromBtcRequestType } from "./frombtc_abstract/FromBtcAbs";
|
|
@@ -17,20 +16,25 @@ export declare abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<S
|
|
|
17
16
|
*
|
|
18
17
|
* @param chainIdentifier
|
|
19
18
|
* @param useToken
|
|
19
|
+
* @param depositToken
|
|
20
20
|
* @param abortController
|
|
21
21
|
*/
|
|
22
|
-
protected getFromBtcPricePrefetches(chainIdentifier: string, useToken: string, abortController: AbortController): {
|
|
23
|
-
pricePrefetchPromise: Promise<
|
|
24
|
-
|
|
22
|
+
protected getFromBtcPricePrefetches(chainIdentifier: string, useToken: string, depositToken: string, abortController: AbortController): {
|
|
23
|
+
pricePrefetchPromise: Promise<bigint>;
|
|
24
|
+
gasTokenPricePrefetchPromise: Promise<bigint>;
|
|
25
|
+
depositTokenPricePrefetchPromise: Promise<bigint>;
|
|
25
26
|
};
|
|
26
27
|
/**
|
|
27
28
|
* Starts a pre-fetch for base security deposit (transaction fee for refunding transaction on our side)
|
|
28
29
|
*
|
|
29
30
|
* @param chainIdentifier
|
|
30
31
|
* @param dummySwapData
|
|
32
|
+
* @param depositToken
|
|
33
|
+
* @param gasTokenPricePrefetchPromise
|
|
34
|
+
* @param depositTokenPricePrefetchPromise
|
|
31
35
|
* @param abortController
|
|
32
36
|
*/
|
|
33
|
-
protected getBaseSecurityDepositPrefetch(chainIdentifier: string, dummySwapData: SwapData, abortController: AbortController): Promise<
|
|
37
|
+
protected getBaseSecurityDepositPrefetch(chainIdentifier: string, dummySwapData: SwapData, depositToken: string, gasTokenPricePrefetchPromise: Promise<bigint>, depositTokenPricePrefetchPromise: Promise<bigint>, abortController: AbortController): Promise<bigint>;
|
|
34
38
|
/**
|
|
35
39
|
* Starts a pre-fetch for vault balance
|
|
36
40
|
*
|
|
@@ -38,7 +42,7 @@ export declare abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<S
|
|
|
38
42
|
* @param useToken
|
|
39
43
|
* @param abortController
|
|
40
44
|
*/
|
|
41
|
-
protected getBalancePrefetch(chainIdentifier: string, useToken: string, abortController: AbortController): Promise<
|
|
45
|
+
protected getBalancePrefetch(chainIdentifier: string, useToken: string, abortController: AbortController): Promise<bigint>;
|
|
42
46
|
/**
|
|
43
47
|
* Checks if we have enough balance of the token in the swap vault
|
|
44
48
|
*
|
|
@@ -47,7 +51,15 @@ export declare abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<S
|
|
|
47
51
|
* @param signal
|
|
48
52
|
* @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
|
|
49
53
|
*/
|
|
50
|
-
protected checkBalance(totalInToken:
|
|
54
|
+
protected checkBalance(totalInToken: bigint, balancePrefetch: Promise<bigint>, signal: AbortSignal | null): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Checks if the specified token is allowed as a deposit token
|
|
57
|
+
*
|
|
58
|
+
* @param chainIdentifier
|
|
59
|
+
* @param depositToken
|
|
60
|
+
* @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
|
|
61
|
+
*/
|
|
62
|
+
protected checkAllowedDepositToken(chainIdentifier: string, depositToken: string): void;
|
|
51
63
|
/**
|
|
52
64
|
* Checks minimums/maximums, calculates the fee & total amount
|
|
53
65
|
*
|
|
@@ -58,10 +70,10 @@ export declare abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<S
|
|
|
58
70
|
*/
|
|
59
71
|
protected preCheckAmounts(request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType>, requestedAmount: {
|
|
60
72
|
input: boolean;
|
|
61
|
-
amount:
|
|
73
|
+
amount: bigint;
|
|
62
74
|
}, useToken: string): Promise<{
|
|
63
|
-
baseFee:
|
|
64
|
-
feePPM:
|
|
75
|
+
baseFee: bigint;
|
|
76
|
+
feePPM: bigint;
|
|
65
77
|
}>;
|
|
66
78
|
/**
|
|
67
79
|
* Checks minimums/maximums, calculates the fee & total amount
|
|
@@ -76,29 +88,16 @@ export declare abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<S
|
|
|
76
88
|
*/
|
|
77
89
|
protected checkFromBtcAmount(request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType>, requestedAmount: {
|
|
78
90
|
input: boolean;
|
|
79
|
-
amount:
|
|
91
|
+
amount: bigint;
|
|
80
92
|
}, fees: {
|
|
81
|
-
baseFee:
|
|
82
|
-
feePPM:
|
|
83
|
-
}, useToken: string, signal: AbortSignal, pricePrefetchPromise?: Promise<
|
|
84
|
-
amountBD:
|
|
85
|
-
swapFee:
|
|
86
|
-
swapFeeInToken:
|
|
87
|
-
totalInToken:
|
|
93
|
+
baseFee: bigint;
|
|
94
|
+
feePPM: bigint;
|
|
95
|
+
}, useToken: string, signal: AbortSignal, pricePrefetchPromise?: Promise<bigint>): Promise<{
|
|
96
|
+
amountBD: bigint;
|
|
97
|
+
swapFee: bigint;
|
|
98
|
+
swapFeeInToken: bigint;
|
|
99
|
+
totalInToken: bigint;
|
|
88
100
|
}>;
|
|
89
|
-
/**
|
|
90
|
-
* Calculates the required security deposit
|
|
91
|
-
*
|
|
92
|
-
* @param chainIdentifier
|
|
93
|
-
* @param amountBD
|
|
94
|
-
* @param swapFee
|
|
95
|
-
* @param expiryTimeout
|
|
96
|
-
* @param baseSecurityDepositPromise
|
|
97
|
-
* @param securityDepositPricePrefetchPromise
|
|
98
|
-
* @param signal
|
|
99
|
-
* @param metadata
|
|
100
|
-
*/
|
|
101
|
-
protected getSecurityDeposit(chainIdentifier: string, amountBD: BN, swapFee: BN, expiryTimeout: BN, baseSecurityDepositPromise: Promise<BN>, securityDepositPricePrefetchPromise: Promise<BN>, signal: AbortSignal, metadata: any): Promise<BN>;
|
|
102
101
|
/**
|
|
103
102
|
* Signs the created swap
|
|
104
103
|
*
|
|
@@ -116,4 +115,18 @@ export declare abstract class FromBtcBaseSwapHandler<V extends SwapHandlerSwap<S
|
|
|
116
115
|
signature: string;
|
|
117
116
|
feeRate: string;
|
|
118
117
|
}>;
|
|
118
|
+
/**
|
|
119
|
+
* Calculates the required security deposit
|
|
120
|
+
*
|
|
121
|
+
* @param chainIdentifier
|
|
122
|
+
* @param amountBD
|
|
123
|
+
* @param swapFee
|
|
124
|
+
* @param expiryTimeout
|
|
125
|
+
* @param baseSecurityDepositPromise
|
|
126
|
+
* @param depositToken
|
|
127
|
+
* @param depositTokenPricePrefetchPromise
|
|
128
|
+
* @param signal
|
|
129
|
+
* @param metadata
|
|
130
|
+
*/
|
|
131
|
+
protected getSecurityDeposit(chainIdentifier: string, amountBD: bigint, swapFee: bigint, expiryTimeout: bigint, baseSecurityDepositPromise: Promise<bigint>, depositToken: string, depositTokenPricePrefetchPromise: Promise<bigint>, signal: AbortSignal, metadata: any): Promise<bigint>;
|
|
119
132
|
}
|
|
@@ -1,78 +1,83 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.FromBtcBaseSwapHandler = void 0;
|
|
13
4
|
const SwapHandler_1 = require("./SwapHandler");
|
|
14
|
-
const BN = require("bn.js");
|
|
15
5
|
const SchemaVerifier_1 = require("../utils/paramcoders/SchemaVerifier");
|
|
16
6
|
const PluginManager_1 = require("../plugins/PluginManager");
|
|
17
7
|
const IPlugin_1 = require("../plugins/IPlugin");
|
|
18
|
-
const secondsInYear =
|
|
8
|
+
const secondsInYear = BigInt(365 * 24 * 60 * 60);
|
|
19
9
|
class FromBtcBaseSwapHandler extends SwapHandler_1.SwapHandler {
|
|
20
10
|
/**
|
|
21
11
|
* Starts a pre-fetch for swap price & security deposit price
|
|
22
12
|
*
|
|
23
13
|
* @param chainIdentifier
|
|
24
14
|
* @param useToken
|
|
15
|
+
* @param depositToken
|
|
25
16
|
* @param abortController
|
|
26
17
|
*/
|
|
27
|
-
getFromBtcPricePrefetches(chainIdentifier, useToken, abortController) {
|
|
18
|
+
getFromBtcPricePrefetches(chainIdentifier, useToken, depositToken, abortController) {
|
|
28
19
|
const pricePrefetchPromise = this.swapPricing.preFetchPrice(useToken, chainIdentifier).catch(e => {
|
|
29
20
|
this.logger.error("getFromBtcPricePrefetches(): pricePrefetch error: ", e);
|
|
30
21
|
abortController.abort(e);
|
|
31
22
|
return null;
|
|
32
23
|
});
|
|
33
24
|
const { swapContract } = this.getChain(chainIdentifier);
|
|
34
|
-
const
|
|
25
|
+
const gasTokenPricePrefetchPromise = useToken.toString() === swapContract.getNativeCurrencyAddress().toString() ?
|
|
35
26
|
pricePrefetchPromise :
|
|
36
27
|
this.swapPricing.preFetchPrice(swapContract.getNativeCurrencyAddress(), chainIdentifier).catch(e => {
|
|
37
|
-
this.logger.error("getFromBtcPricePrefetches():
|
|
28
|
+
this.logger.error("getFromBtcPricePrefetches(): gasTokenPricePrefetchPromise error: ", e);
|
|
38
29
|
abortController.abort(e);
|
|
39
30
|
return null;
|
|
40
31
|
});
|
|
41
|
-
|
|
32
|
+
const depositTokenPricePrefetchPromise = depositToken === swapContract.getNativeCurrencyAddress() ?
|
|
33
|
+
gasTokenPricePrefetchPromise :
|
|
34
|
+
this.swapPricing.preFetchPrice(depositToken, chainIdentifier).catch(e => {
|
|
35
|
+
this.logger.error("getFromBtcPricePrefetches(): depositTokenPricePrefetchPromise error: ", e);
|
|
36
|
+
abortController.abort(e);
|
|
37
|
+
return null;
|
|
38
|
+
});
|
|
39
|
+
return { pricePrefetchPromise, gasTokenPricePrefetchPromise, depositTokenPricePrefetchPromise };
|
|
42
40
|
}
|
|
43
41
|
/**
|
|
44
42
|
* Starts a pre-fetch for base security deposit (transaction fee for refunding transaction on our side)
|
|
45
43
|
*
|
|
46
44
|
* @param chainIdentifier
|
|
47
45
|
* @param dummySwapData
|
|
46
|
+
* @param depositToken
|
|
47
|
+
* @param gasTokenPricePrefetchPromise
|
|
48
|
+
* @param depositTokenPricePrefetchPromise
|
|
48
49
|
* @param abortController
|
|
49
50
|
*/
|
|
50
|
-
getBaseSecurityDepositPrefetch(chainIdentifier, dummySwapData, abortController) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
catch (e) {
|
|
59
|
-
this.logger.error("getBaseSecurityDepositPrefetch(): pre-fetch error: ", e);
|
|
60
|
-
abortController.abort(e);
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
51
|
+
async getBaseSecurityDepositPrefetch(chainIdentifier, dummySwapData, depositToken, gasTokenPricePrefetchPromise, depositTokenPricePrefetchPromise, abortController) {
|
|
52
|
+
//Solana workaround
|
|
53
|
+
const { swapContract } = this.getChain(chainIdentifier);
|
|
54
|
+
let feeResult;
|
|
55
|
+
const gasToken = swapContract.getNativeCurrencyAddress();
|
|
56
|
+
if (swapContract.getRawRefundFee != null) {
|
|
57
|
+
try {
|
|
58
|
+
feeResult = await swapContract.getRawRefundFee(dummySwapData);
|
|
63
59
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
catch (e1) {
|
|
70
|
-
this.logger.error("getBaseSecurityDepositPrefetch(): pre-fetch error: ", e1);
|
|
71
|
-
abortController.abort(e1);
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
this.logger.error("getBaseSecurityDepositPrefetch(): pre-fetch error: ", e);
|
|
62
|
+
abortController.abort(e);
|
|
63
|
+
return null;
|
|
74
64
|
}
|
|
75
|
-
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
try {
|
|
68
|
+
feeResult = await swapContract.getRefundFee(dummySwapData);
|
|
69
|
+
}
|
|
70
|
+
catch (e1) {
|
|
71
|
+
this.logger.error("getBaseSecurityDepositPrefetch(): pre-fetch error: ", e1);
|
|
72
|
+
abortController.abort(e1);
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
feeResult = feeResult * 2n;
|
|
77
|
+
if (gasToken === depositToken)
|
|
78
|
+
return feeResult;
|
|
79
|
+
const btcValue = await this.swapPricing.getToBtcSwapAmount(feeResult, gasToken, chainIdentifier, true, gasTokenPricePrefetchPromise);
|
|
80
|
+
return await this.swapPricing.getFromBtcSwapAmount(btcValue, depositToken, chainIdentifier, true, depositTokenPricePrefetchPromise);
|
|
76
81
|
}
|
|
77
82
|
/**
|
|
78
83
|
* Starts a pre-fetch for vault balance
|
|
@@ -81,18 +86,16 @@ class FromBtcBaseSwapHandler extends SwapHandler_1.SwapHandler {
|
|
|
81
86
|
* @param useToken
|
|
82
87
|
* @param abortController
|
|
83
88
|
*/
|
|
84
|
-
getBalancePrefetch(chainIdentifier, useToken, abortController) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
});
|
|
89
|
+
async getBalancePrefetch(chainIdentifier, useToken, abortController) {
|
|
90
|
+
const { swapContract, signer } = this.getChain(chainIdentifier);
|
|
91
|
+
try {
|
|
92
|
+
return await swapContract.getBalance(signer.getAddress(), useToken, true);
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
this.logger.error("getBalancePrefetch(): balancePrefetch error: ", e);
|
|
96
|
+
abortController.abort(e);
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
96
99
|
}
|
|
97
100
|
/**
|
|
98
101
|
* Checks if we have enough balance of the token in the swap vault
|
|
@@ -102,18 +105,40 @@ class FromBtcBaseSwapHandler extends SwapHandler_1.SwapHandler {
|
|
|
102
105
|
* @param signal
|
|
103
106
|
* @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
|
|
104
107
|
*/
|
|
105
|
-
checkBalance(totalInToken, balancePrefetch, signal) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
108
|
+
async checkBalance(totalInToken, balancePrefetch, signal) {
|
|
109
|
+
const balance = await balancePrefetch;
|
|
110
|
+
if (signal != null)
|
|
111
|
+
signal.throwIfAborted();
|
|
112
|
+
if (balance == null || balance < totalInToken) {
|
|
113
|
+
throw {
|
|
114
|
+
code: 20002,
|
|
115
|
+
msg: "Not enough liquidity"
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Checks if the specified token is allowed as a deposit token
|
|
121
|
+
*
|
|
122
|
+
* @param chainIdentifier
|
|
123
|
+
* @param depositToken
|
|
124
|
+
* @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
|
|
125
|
+
*/
|
|
126
|
+
checkAllowedDepositToken(chainIdentifier, depositToken) {
|
|
127
|
+
const { swapContract, allowedDepositTokens } = this.getChain(chainIdentifier);
|
|
128
|
+
if (allowedDepositTokens == null) {
|
|
129
|
+
if (depositToken !== swapContract.getNativeCurrencyAddress())
|
|
111
130
|
throw {
|
|
112
|
-
code:
|
|
113
|
-
msg: "
|
|
131
|
+
code: 20190,
|
|
132
|
+
msg: "Unsupported deposit token"
|
|
114
133
|
};
|
|
115
|
-
|
|
116
|
-
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
if (!allowedDepositTokens.includes(depositToken))
|
|
137
|
+
throw {
|
|
138
|
+
code: 20190,
|
|
139
|
+
msg: "Unsupported deposit token"
|
|
140
|
+
};
|
|
141
|
+
}
|
|
117
142
|
}
|
|
118
143
|
/**
|
|
119
144
|
* Checks minimums/maximums, calculates the fee & total amount
|
|
@@ -123,25 +148,23 @@ class FromBtcBaseSwapHandler extends SwapHandler_1.SwapHandler {
|
|
|
123
148
|
* @param useToken
|
|
124
149
|
* @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds
|
|
125
150
|
*/
|
|
126
|
-
preCheckAmounts(request, requestedAmount, useToken) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
};
|
|
136
|
-
}
|
|
151
|
+
async preCheckAmounts(request, requestedAmount, useToken) {
|
|
152
|
+
const res = await PluginManager_1.PluginManager.onHandlePreFromBtcQuote(request, requestedAmount, request.chainIdentifier, useToken, { minInBtc: this.config.min, maxInBtc: this.config.max }, { baseFeeInBtc: this.config.baseFee, feePPM: this.config.feePPM });
|
|
153
|
+
if (res != null) {
|
|
154
|
+
this.handlePluginErrorResponses(res);
|
|
155
|
+
if ((0, IPlugin_1.isQuoteSetFees)(res)) {
|
|
156
|
+
return {
|
|
157
|
+
baseFee: res.baseFee || this.config.baseFee,
|
|
158
|
+
feePPM: res.feePPM || this.config.feePPM
|
|
159
|
+
};
|
|
137
160
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
161
|
+
}
|
|
162
|
+
if (requestedAmount.input)
|
|
163
|
+
this.checkBtcAmountInBounds(requestedAmount.amount);
|
|
164
|
+
return {
|
|
165
|
+
baseFee: this.config.baseFee,
|
|
166
|
+
feePPM: this.config.feePPM
|
|
167
|
+
};
|
|
145
168
|
}
|
|
146
169
|
/**
|
|
147
170
|
* Checks minimums/maximums, calculates the fee & total amount
|
|
@@ -154,84 +177,110 @@ class FromBtcBaseSwapHandler extends SwapHandler_1.SwapHandler {
|
|
|
154
177
|
* @param pricePrefetchPromise
|
|
155
178
|
* @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds
|
|
156
179
|
*/
|
|
157
|
-
checkFromBtcAmount(request, requestedAmount, fees, useToken, signal, pricePrefetchPromise = Promise.resolve(null)) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
fees.feePPM = res.feePPM;
|
|
169
|
-
}
|
|
170
|
-
if ((0, IPlugin_1.isPluginQuote)(res)) {
|
|
171
|
-
if (!requestedAmount.input) {
|
|
172
|
-
return {
|
|
173
|
-
amountBD: res.amount.amount.add(res.swapFee.inInputTokens),
|
|
174
|
-
swapFee: res.swapFee.inInputTokens,
|
|
175
|
-
swapFeeInToken: res.swapFee.inOutputTokens,
|
|
176
|
-
totalInToken: requestedAmount.amount
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
else {
|
|
180
|
-
return {
|
|
181
|
-
amountBD: requestedAmount.amount,
|
|
182
|
-
swapFee: res.swapFee.inInputTokens,
|
|
183
|
-
swapFeeInToken: res.swapFee.inOutputTokens,
|
|
184
|
-
totalInToken: res.amount.amount
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
}
|
|
180
|
+
async checkFromBtcAmount(request, requestedAmount, fees, useToken, signal, pricePrefetchPromise = Promise.resolve(null)) {
|
|
181
|
+
const chainIdentifier = request.chainIdentifier;
|
|
182
|
+
const res = await PluginManager_1.PluginManager.onHandlePostFromBtcQuote(request, requestedAmount, chainIdentifier, useToken, { minInBtc: this.config.min, maxInBtc: this.config.max }, { baseFeeInBtc: fees.baseFee, feePPM: fees.feePPM }, pricePrefetchPromise);
|
|
183
|
+
signal.throwIfAborted();
|
|
184
|
+
if (res != null) {
|
|
185
|
+
this.handlePluginErrorResponses(res);
|
|
186
|
+
if ((0, IPlugin_1.isQuoteSetFees)(res)) {
|
|
187
|
+
if (res.baseFee != null)
|
|
188
|
+
fees.baseFee = res.baseFee;
|
|
189
|
+
if (res.feePPM != null)
|
|
190
|
+
fees.feePPM = res.feePPM;
|
|
188
191
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
msg: tooLow ? "Amount too low!" : "Amount too high!",
|
|
205
|
-
data: {
|
|
206
|
-
min: minIn.toString(10),
|
|
207
|
-
max: maxIn.toString(10)
|
|
208
|
-
}
|
|
192
|
+
if ((0, IPlugin_1.isPluginQuote)(res)) {
|
|
193
|
+
if (!requestedAmount.input) {
|
|
194
|
+
return {
|
|
195
|
+
amountBD: res.amount.amount + res.swapFee.inInputTokens,
|
|
196
|
+
swapFee: res.swapFee.inInputTokens,
|
|
197
|
+
swapFeeInToken: res.swapFee.inOutputTokens,
|
|
198
|
+
totalInToken: requestedAmount.amount
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
return {
|
|
203
|
+
amountBD: requestedAmount.amount,
|
|
204
|
+
swapFee: res.swapFee.inInputTokens,
|
|
205
|
+
swapFeeInToken: res.swapFee.inOutputTokens,
|
|
206
|
+
totalInToken: res.amount.amount
|
|
209
207
|
};
|
|
210
208
|
}
|
|
211
209
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const swapFee = fees.baseFee.add(amountBD.mul(fees.feePPM).div(new BN(1000000)));
|
|
217
|
-
const swapFeeInToken = yield this.swapPricing.getFromBtcSwapAmount(swapFee, useToken, chainIdentifier, true, pricePrefetchPromise);
|
|
210
|
+
}
|
|
211
|
+
let amountBD;
|
|
212
|
+
if (!requestedAmount.input) {
|
|
213
|
+
amountBD = await this.swapPricing.getToBtcSwapAmount(requestedAmount.amount, useToken, chainIdentifier, true, pricePrefetchPromise);
|
|
218
214
|
signal.throwIfAborted();
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
|
|
215
|
+
// amt = (amt+base_fee)/(1-fee)
|
|
216
|
+
amountBD = (amountBD + fees.baseFee) * 1000000n / (1000000n - fees.feePPM);
|
|
217
|
+
const tooLow = amountBD < (this.config.min * 95n / 100n);
|
|
218
|
+
const tooHigh = amountBD > (this.config.max * 105n / 100n);
|
|
219
|
+
if (tooLow || tooHigh) {
|
|
220
|
+
const adjustedMin = this.config.min * (1000000n - fees.feePPM) / (1000000n - fees.baseFee);
|
|
221
|
+
const adjustedMax = this.config.max * (1000000n - fees.feePPM) / (1000000n - fees.baseFee);
|
|
222
|
+
const minIn = await this.swapPricing.getFromBtcSwapAmount(adjustedMin, useToken, chainIdentifier, null, pricePrefetchPromise);
|
|
223
|
+
const maxIn = await this.swapPricing.getFromBtcSwapAmount(adjustedMax, useToken, chainIdentifier, null, pricePrefetchPromise);
|
|
224
|
+
throw {
|
|
225
|
+
code: tooLow ? 20003 : 20004,
|
|
226
|
+
msg: tooLow ? "Amount too low!" : "Amount too high!",
|
|
227
|
+
data: {
|
|
228
|
+
min: minIn.toString(10),
|
|
229
|
+
max: maxIn.toString(10)
|
|
230
|
+
}
|
|
231
|
+
};
|
|
227
232
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
amountBD = requestedAmount.amount;
|
|
236
|
+
this.checkBtcAmountInBounds(amountBD);
|
|
237
|
+
}
|
|
238
|
+
const swapFee = fees.baseFee + (amountBD * fees.feePPM / 1000000n);
|
|
239
|
+
const swapFeeInToken = await this.swapPricing.getFromBtcSwapAmount(swapFee, useToken, chainIdentifier, true, pricePrefetchPromise);
|
|
240
|
+
signal.throwIfAborted();
|
|
241
|
+
let totalInToken;
|
|
242
|
+
if (!requestedAmount.input) {
|
|
243
|
+
totalInToken = requestedAmount.amount;
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
const amountInToken = await this.swapPricing.getFromBtcSwapAmount(requestedAmount.amount, useToken, chainIdentifier, null, pricePrefetchPromise);
|
|
247
|
+
totalInToken = amountInToken - swapFeeInToken;
|
|
248
|
+
signal.throwIfAborted();
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
amountBD,
|
|
252
|
+
swapFee,
|
|
253
|
+
swapFeeInToken,
|
|
254
|
+
totalInToken
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Signs the created swap
|
|
259
|
+
*
|
|
260
|
+
* @param chainIdentifier
|
|
261
|
+
* @param swapObject
|
|
262
|
+
* @param req
|
|
263
|
+
* @param abortSignal
|
|
264
|
+
* @param signDataPrefetchPromise
|
|
265
|
+
*/
|
|
266
|
+
async getFromBtcSignatureData(chainIdentifier, swapObject, req, abortSignal, signDataPrefetchPromise) {
|
|
267
|
+
const { swapContract, signer } = this.getChain(chainIdentifier);
|
|
268
|
+
const prefetchedSignData = signDataPrefetchPromise != null ? await signDataPrefetchPromise : null;
|
|
269
|
+
if (prefetchedSignData != null)
|
|
270
|
+
this.logger.debug("getFromBtcSignatureData(): pre-fetched signature data: ", prefetchedSignData);
|
|
271
|
+
abortSignal.throwIfAborted();
|
|
272
|
+
const feeRateObj = await req.paramReader.getParams({
|
|
273
|
+
feeRate: SchemaVerifier_1.FieldTypeEnum.String
|
|
274
|
+
}).catch(() => null);
|
|
275
|
+
abortSignal.throwIfAborted();
|
|
276
|
+
const feeRate = feeRateObj?.feeRate != null && typeof (feeRateObj.feeRate) === "string" ? feeRateObj.feeRate : null;
|
|
277
|
+
this.logger.debug("getFromBtcSignatureData(): using fee rate from client: ", feeRate);
|
|
278
|
+
const sigData = await swapContract.getInitSignature(signer, swapObject, this.getInitAuthorizationTimeout(chainIdentifier), prefetchedSignData, feeRate);
|
|
279
|
+
abortSignal.throwIfAborted();
|
|
280
|
+
return {
|
|
281
|
+
...sigData,
|
|
282
|
+
feeRate
|
|
283
|
+
};
|
|
235
284
|
}
|
|
236
285
|
/**
|
|
237
286
|
* Calculates the required security deposit
|
|
@@ -241,54 +290,26 @@ class FromBtcBaseSwapHandler extends SwapHandler_1.SwapHandler {
|
|
|
241
290
|
* @param swapFee
|
|
242
291
|
* @param expiryTimeout
|
|
243
292
|
* @param baseSecurityDepositPromise
|
|
244
|
-
* @param
|
|
293
|
+
* @param depositToken
|
|
294
|
+
* @param depositTokenPricePrefetchPromise
|
|
245
295
|
* @param signal
|
|
246
296
|
* @param metadata
|
|
247
297
|
*/
|
|
248
|
-
getSecurityDeposit(chainIdentifier, amountBD, swapFee, expiryTimeout, baseSecurityDepositPromise,
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
return baseSD.add(variableSD);
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Signs the created swap
|
|
268
|
-
*
|
|
269
|
-
* @param chainIdentifier
|
|
270
|
-
* @param swapObject
|
|
271
|
-
* @param req
|
|
272
|
-
* @param abortSignal
|
|
273
|
-
* @param signDataPrefetchPromise
|
|
274
|
-
*/
|
|
275
|
-
getFromBtcSignatureData(chainIdentifier, swapObject, req, abortSignal, signDataPrefetchPromise) {
|
|
276
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
277
|
-
const { swapContract, signer } = this.getChain(chainIdentifier);
|
|
278
|
-
const prefetchedSignData = signDataPrefetchPromise != null ? yield signDataPrefetchPromise : null;
|
|
279
|
-
if (prefetchedSignData != null)
|
|
280
|
-
this.logger.debug("getFromBtcSignatureData(): pre-fetched signature data: ", prefetchedSignData);
|
|
281
|
-
abortSignal.throwIfAborted();
|
|
282
|
-
const feeRateObj = yield req.paramReader.getParams({
|
|
283
|
-
feeRate: SchemaVerifier_1.FieldTypeEnum.String
|
|
284
|
-
}).catch(() => null);
|
|
285
|
-
abortSignal.throwIfAborted();
|
|
286
|
-
const feeRate = (feeRateObj === null || feeRateObj === void 0 ? void 0 : feeRateObj.feeRate) != null && typeof (feeRateObj.feeRate) === "string" ? feeRateObj.feeRate : null;
|
|
287
|
-
this.logger.debug("getFromBtcSignatureData(): using fee rate from client: ", feeRate);
|
|
288
|
-
const sigData = yield swapContract.getInitSignature(signer, swapObject, this.config.authorizationTimeout, prefetchedSignData, feeRate);
|
|
289
|
-
abortSignal.throwIfAborted();
|
|
290
|
-
return Object.assign(Object.assign({}, sigData), { feeRate });
|
|
291
|
-
});
|
|
298
|
+
async getSecurityDeposit(chainIdentifier, amountBD, swapFee, expiryTimeout, baseSecurityDepositPromise, depositToken, depositTokenPricePrefetchPromise, signal, metadata) {
|
|
299
|
+
let baseSD = await baseSecurityDepositPromise;
|
|
300
|
+
signal.throwIfAborted();
|
|
301
|
+
metadata.times.refundFeeFetched = Date.now();
|
|
302
|
+
const swapValueInDepositToken = await this.swapPricing.getFromBtcSwapAmount(amountBD - swapFee, depositToken, chainIdentifier, true, depositTokenPricePrefetchPromise);
|
|
303
|
+
signal.throwIfAborted();
|
|
304
|
+
const apyPPM = BigInt(Math.floor(this.config.securityDepositAPY * 1000000));
|
|
305
|
+
const variableSD = swapValueInDepositToken * apyPPM * expiryTimeout / 1000000n / secondsInYear;
|
|
306
|
+
this.logger.debug("getSecurityDeposit(): base security deposit: " + baseSD.toString(10) +
|
|
307
|
+
" deposit token: " + depositToken +
|
|
308
|
+
" swap output in deposit token: " + swapValueInDepositToken.toString(10) +
|
|
309
|
+
" apy ppm: " + apyPPM.toString(10) +
|
|
310
|
+
" expiry timeout: " + expiryTimeout.toString(10) +
|
|
311
|
+
" variable security deposit: " + variableSD.toString(10));
|
|
312
|
+
return baseSD + variableSD;
|
|
292
313
|
}
|
|
293
314
|
}
|
|
294
315
|
exports.FromBtcBaseSwapHandler = FromBtcBaseSwapHandler;
|
|
@@ -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";
|
|
@@ -17,7 +16,7 @@ export declare abstract class FromBtcLnBaseSwapHandler<V extends SwapHandlerSwap
|
|
|
17
16
|
* @param signal
|
|
18
17
|
* @throws {DefinedRuntimeError} will throw an error if there isn't enough inbound liquidity to receive the LN payment
|
|
19
18
|
*/
|
|
20
|
-
protected checkInboundLiquidity(amountBD:
|
|
19
|
+
protected checkInboundLiquidity(amountBD: bigint, channelsPrefetch: Promise<LightningNetworkChannel[]>, signal: AbortSignal): Promise<void>;
|
|
21
20
|
/**
|
|
22
21
|
* Starts LN channels pre-fetch
|
|
23
22
|
*
|