@atomiqlabs/lp-lib 10.3.11

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.
Files changed (138) hide show
  1. package/LICENSE +201 -0
  2. package/dist/fees/IBtcFeeEstimator.d.ts +3 -0
  3. package/dist/fees/IBtcFeeEstimator.js +2 -0
  4. package/dist/fees/OneDollarFeeEstimator.d.ts +16 -0
  5. package/dist/fees/OneDollarFeeEstimator.js +71 -0
  6. package/dist/index.d.ts +33 -0
  7. package/dist/index.js +52 -0
  8. package/dist/info/InfoHandler.d.ts +17 -0
  9. package/dist/info/InfoHandler.js +70 -0
  10. package/dist/plugins/IPlugin.d.ts +118 -0
  11. package/dist/plugins/IPlugin.js +33 -0
  12. package/dist/plugins/PluginManager.d.ts +89 -0
  13. package/dist/plugins/PluginManager.js +263 -0
  14. package/dist/prices/BinanceSwapPrice.d.ts +27 -0
  15. package/dist/prices/BinanceSwapPrice.js +106 -0
  16. package/dist/prices/CoinGeckoSwapPrice.d.ts +31 -0
  17. package/dist/prices/CoinGeckoSwapPrice.js +76 -0
  18. package/dist/storage/IIntermediaryStorage.d.ts +15 -0
  19. package/dist/storage/IIntermediaryStorage.js +2 -0
  20. package/dist/storagemanager/IntermediaryStorageManager.d.ts +15 -0
  21. package/dist/storagemanager/IntermediaryStorageManager.js +113 -0
  22. package/dist/storagemanager/StorageManager.d.ts +12 -0
  23. package/dist/storagemanager/StorageManager.js +74 -0
  24. package/dist/swaps/FromBtcBaseSwap.d.ts +12 -0
  25. package/dist/swaps/FromBtcBaseSwap.js +16 -0
  26. package/dist/swaps/FromBtcBaseSwapHandler.d.ts +118 -0
  27. package/dist/swaps/FromBtcBaseSwapHandler.js +294 -0
  28. package/dist/swaps/FromBtcLnBaseSwapHandler.d.ts +25 -0
  29. package/dist/swaps/FromBtcLnBaseSwapHandler.js +55 -0
  30. package/dist/swaps/ISwapPrice.d.ts +44 -0
  31. package/dist/swaps/ISwapPrice.js +73 -0
  32. package/dist/swaps/SwapHandler.d.ts +186 -0
  33. package/dist/swaps/SwapHandler.js +292 -0
  34. package/dist/swaps/SwapHandlerSwap.d.ts +75 -0
  35. package/dist/swaps/SwapHandlerSwap.js +72 -0
  36. package/dist/swaps/ToBtcBaseSwap.d.ts +35 -0
  37. package/dist/swaps/ToBtcBaseSwap.js +61 -0
  38. package/dist/swaps/ToBtcBaseSwapHandler.d.ts +94 -0
  39. package/dist/swaps/ToBtcBaseSwapHandler.js +233 -0
  40. package/dist/swaps/frombtc_abstract/FromBtcAbs.d.ts +92 -0
  41. package/dist/swaps/frombtc_abstract/FromBtcAbs.js +386 -0
  42. package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.d.ts +26 -0
  43. package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.js +63 -0
  44. package/dist/swaps/frombtc_trusted/FromBtcTrusted.d.ts +55 -0
  45. package/dist/swaps/frombtc_trusted/FromBtcTrusted.js +586 -0
  46. package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.d.ts +43 -0
  47. package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.js +99 -0
  48. package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.d.ts +105 -0
  49. package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.js +731 -0
  50. package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +29 -0
  51. package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.js +64 -0
  52. package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.d.ts +79 -0
  53. package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.js +514 -0
  54. package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +28 -0
  55. package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.js +66 -0
  56. package/dist/swaps/tobtc_abstract/ToBtcAbs.d.ts +290 -0
  57. package/dist/swaps/tobtc_abstract/ToBtcAbs.js +1056 -0
  58. package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.d.ts +29 -0
  59. package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.js +70 -0
  60. package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.d.ts +246 -0
  61. package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.js +1169 -0
  62. package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +27 -0
  63. package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.js +65 -0
  64. package/dist/utils/Utils.d.ts +32 -0
  65. package/dist/utils/Utils.js +109 -0
  66. package/dist/utils/coinselect2/accumulative.d.ts +6 -0
  67. package/dist/utils/coinselect2/accumulative.js +44 -0
  68. package/dist/utils/coinselect2/blackjack.d.ts +6 -0
  69. package/dist/utils/coinselect2/blackjack.js +41 -0
  70. package/dist/utils/coinselect2/index.d.ts +16 -0
  71. package/dist/utils/coinselect2/index.js +40 -0
  72. package/dist/utils/coinselect2/utils.d.ts +64 -0
  73. package/dist/utils/coinselect2/utils.js +121 -0
  74. package/dist/utils/paramcoders/IParamReader.d.ts +5 -0
  75. package/dist/utils/paramcoders/IParamReader.js +2 -0
  76. package/dist/utils/paramcoders/IParamWriter.d.ts +4 -0
  77. package/dist/utils/paramcoders/IParamWriter.js +2 -0
  78. package/dist/utils/paramcoders/LegacyParamEncoder.d.ts +10 -0
  79. package/dist/utils/paramcoders/LegacyParamEncoder.js +33 -0
  80. package/dist/utils/paramcoders/ParamDecoder.d.ts +25 -0
  81. package/dist/utils/paramcoders/ParamDecoder.js +234 -0
  82. package/dist/utils/paramcoders/ParamEncoder.d.ts +9 -0
  83. package/dist/utils/paramcoders/ParamEncoder.js +22 -0
  84. package/dist/utils/paramcoders/SchemaVerifier.d.ts +22 -0
  85. package/dist/utils/paramcoders/SchemaVerifier.js +85 -0
  86. package/dist/utils/paramcoders/server/ServerParamDecoder.d.ts +8 -0
  87. package/dist/utils/paramcoders/server/ServerParamDecoder.js +105 -0
  88. package/dist/utils/paramcoders/server/ServerParamEncoder.d.ts +11 -0
  89. package/dist/utils/paramcoders/server/ServerParamEncoder.js +76 -0
  90. package/package.json +43 -0
  91. package/src/fees/IBtcFeeEstimator.ts +7 -0
  92. package/src/fees/OneDollarFeeEstimator.ts +95 -0
  93. package/src/index.ts +46 -0
  94. package/src/info/InfoHandler.ts +106 -0
  95. package/src/plugins/IPlugin.ts +155 -0
  96. package/src/plugins/PluginManager.ts +310 -0
  97. package/src/prices/BinanceSwapPrice.ts +114 -0
  98. package/src/prices/CoinGeckoSwapPrice.ts +88 -0
  99. package/src/storage/IIntermediaryStorage.ts +21 -0
  100. package/src/storagemanager/IntermediaryStorageManager.ts +101 -0
  101. package/src/storagemanager/StorageManager.ts +68 -0
  102. package/src/swaps/FromBtcBaseSwap.ts +21 -0
  103. package/src/swaps/FromBtcBaseSwapHandler.ts +375 -0
  104. package/src/swaps/FromBtcLnBaseSwapHandler.ts +48 -0
  105. package/src/swaps/ISwapPrice.ts +94 -0
  106. package/src/swaps/SwapHandler.ts +404 -0
  107. package/src/swaps/SwapHandlerSwap.ts +133 -0
  108. package/src/swaps/ToBtcBaseSwap.ts +76 -0
  109. package/src/swaps/ToBtcBaseSwapHandler.ts +309 -0
  110. package/src/swaps/frombtc_abstract/FromBtcAbs.ts +484 -0
  111. package/src/swaps/frombtc_abstract/FromBtcSwapAbs.ts +77 -0
  112. package/src/swaps/frombtc_trusted/FromBtcTrusted.ts +661 -0
  113. package/src/swaps/frombtc_trusted/FromBtcTrustedSwap.ts +158 -0
  114. package/src/swaps/frombtcln_abstract/FromBtcLnAbs.ts +864 -0
  115. package/src/swaps/frombtcln_abstract/FromBtcLnSwapAbs.ts +82 -0
  116. package/src/swaps/frombtcln_trusted/FromBtcLnTrusted.ts +592 -0
  117. package/src/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.ts +90 -0
  118. package/src/swaps/tobtc_abstract/ToBtcAbs.ts +1249 -0
  119. package/src/swaps/tobtc_abstract/ToBtcSwapAbs.ts +112 -0
  120. package/src/swaps/tobtcln_abstract/ToBtcLnAbs.ts +1422 -0
  121. package/src/swaps/tobtcln_abstract/ToBtcLnSwapAbs.ts +87 -0
  122. package/src/utils/Utils.ts +108 -0
  123. package/src/utils/coinselect2/accumulative.js +32 -0
  124. package/src/utils/coinselect2/accumulative.ts +58 -0
  125. package/src/utils/coinselect2/blackjack.js +29 -0
  126. package/src/utils/coinselect2/blackjack.ts +54 -0
  127. package/src/utils/coinselect2/index.js +16 -0
  128. package/src/utils/coinselect2/index.ts +50 -0
  129. package/src/utils/coinselect2/utils.js +110 -0
  130. package/src/utils/coinselect2/utils.ts +183 -0
  131. package/src/utils/paramcoders/IParamReader.ts +8 -0
  132. package/src/utils/paramcoders/IParamWriter.ts +8 -0
  133. package/src/utils/paramcoders/LegacyParamEncoder.ts +28 -0
  134. package/src/utils/paramcoders/ParamDecoder.ts +219 -0
  135. package/src/utils/paramcoders/ParamEncoder.ts +30 -0
  136. package/src/utils/paramcoders/SchemaVerifier.ts +97 -0
  137. package/src/utils/paramcoders/server/ServerParamDecoder.ts +115 -0
  138. package/src/utils/paramcoders/server/ServerParamEncoder.ts +76 -0
@@ -0,0 +1,55 @@
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
+ exports.FromBtcLnBaseSwapHandler = void 0;
13
+ const FromBtcBaseSwapHandler_1 = require("./FromBtcBaseSwapHandler");
14
+ const BN = require("bn.js");
15
+ const lncli = require("ln-service");
16
+ class FromBtcLnBaseSwapHandler extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
17
+ /**
18
+ * Checks if we have enough inbound liquidity to be able to receive an LN payment (without MPP)
19
+ *
20
+ * @param amountBD
21
+ * @param channelsPrefetch
22
+ * @param signal
23
+ * @throws {DefinedRuntimeError} will throw an error if there isn't enough inbound liquidity to receive the LN payment
24
+ */
25
+ checkInboundLiquidity(amountBD, channelsPrefetch, signal) {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ const channelsResponse = yield channelsPrefetch;
28
+ signal.throwIfAborted();
29
+ let hasEnoughInboundLiquidity = false;
30
+ channelsResponse.channels.forEach(channel => {
31
+ if (new BN(channel.remote_balance).gte(amountBD))
32
+ hasEnoughInboundLiquidity = true;
33
+ });
34
+ if (!hasEnoughInboundLiquidity) {
35
+ throw {
36
+ code: 20050,
37
+ msg: "Not enough LN inbound liquidity"
38
+ };
39
+ }
40
+ });
41
+ }
42
+ /**
43
+ * Starts LN channels pre-fetch
44
+ *
45
+ * @param abortController
46
+ */
47
+ getChannelsPrefetch(abortController) {
48
+ return lncli.getChannels({ is_active: true, lnd: this.LND }).catch(e => {
49
+ this.logger.error("getChannelsPrefetch(): error", e);
50
+ abortController.abort(e);
51
+ return null;
52
+ });
53
+ }
54
+ }
55
+ exports.FromBtcLnBaseSwapHandler = FromBtcLnBaseSwapHandler;
@@ -0,0 +1,44 @@
1
+ import * as BN from "bn.js";
2
+ export type ISwapPriceCoinsMap<T extends {
3
+ decimals: number;
4
+ }> = {
5
+ [chainId: string]: {
6
+ [address: string]: T;
7
+ };
8
+ };
9
+ export declare abstract class ISwapPrice<T extends {
10
+ decimals: number;
11
+ } = {
12
+ decimals: number;
13
+ }> {
14
+ protected coinsMap: ISwapPriceCoinsMap<T>;
15
+ protected constructor(coinsMap: ISwapPriceCoinsMap<T>);
16
+ /**
17
+ * Returns coin price in mSat
18
+ *
19
+ * @param tokenData
20
+ */
21
+ protected abstract getPrice(tokenData: T): Promise<BN>;
22
+ getTokenData(tokenAddress: string, chainId: string): T;
23
+ preFetchPrice(token: string, chainId: string): Promise<BN>;
24
+ /**
25
+ * Returns amount of satoshis that are equivalent to {fromAmount} of {fromToken}
26
+ *
27
+ * @param fromAmount Amount of the token
28
+ * @param fromToken Token
29
+ * @param tokenChainIdentification Chain of the token
30
+ * @param roundUp Whether result should be rounded up
31
+ * @param preFetch Price pre-fetch promise returned from preFetchPrice()
32
+ */
33
+ getToBtcSwapAmount(fromAmount: BN, fromToken: string, tokenChainIdentification: string, roundUp?: boolean, preFetch?: Promise<BN>): Promise<BN>;
34
+ /**
35
+ * Returns amount of {toToken} that are equivalent to {fromAmount} satoshis
36
+ *
37
+ * @param fromAmount Amount of satoshis
38
+ * @param toToken Token
39
+ * @param tokenChainIdentification Chain of the token
40
+ * @param roundUp Whether result should be rounded up
41
+ * @param preFetch Price pre-fetch promise returned from preFetchPrice()
42
+ */
43
+ getFromBtcSwapAmount(fromAmount: BN, toToken: string, tokenChainIdentification: string, roundUp?: boolean, preFetch?: Promise<BN>): Promise<BN>;
44
+ }
@@ -0,0 +1,73 @@
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
+ exports.ISwapPrice = void 0;
13
+ const BN = require("bn.js");
14
+ class ISwapPrice {
15
+ constructor(coinsMap) {
16
+ this.coinsMap = coinsMap;
17
+ }
18
+ getTokenData(tokenAddress, chainId) {
19
+ const chainTokens = this.coinsMap[chainId];
20
+ if (chainTokens == null)
21
+ throw new Error("Chain not found");
22
+ const tokenAddr = tokenAddress.toString();
23
+ const coin = chainTokens[tokenAddr];
24
+ if (coin == null)
25
+ throw new Error("Token not found");
26
+ return coin;
27
+ }
28
+ preFetchPrice(token, chainId) {
29
+ const coin = this.getTokenData(token, chainId);
30
+ return this.getPrice(coin);
31
+ }
32
+ /**
33
+ * Returns amount of satoshis that are equivalent to {fromAmount} of {fromToken}
34
+ *
35
+ * @param fromAmount Amount of the token
36
+ * @param fromToken Token
37
+ * @param tokenChainIdentification Chain of the token
38
+ * @param roundUp Whether result should be rounded up
39
+ * @param preFetch Price pre-fetch promise returned from preFetchPrice()
40
+ */
41
+ getToBtcSwapAmount(fromAmount, fromToken, tokenChainIdentification, roundUp, preFetch) {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ const coin = this.getTokenData(fromToken, tokenChainIdentification);
44
+ const price = (preFetch == null ? null : yield preFetch) || (yield this.getPrice(coin));
45
+ return fromAmount
46
+ .mul(price)
47
+ .div(new BN(10).pow(new BN(coin.decimals)))
48
+ .add(roundUp ? new BN(999999) : new BN(0))
49
+ .div(new BN(1000000));
50
+ });
51
+ }
52
+ /**
53
+ * Returns amount of {toToken} that are equivalent to {fromAmount} satoshis
54
+ *
55
+ * @param fromAmount Amount of satoshis
56
+ * @param toToken Token
57
+ * @param tokenChainIdentification Chain of the token
58
+ * @param roundUp Whether result should be rounded up
59
+ * @param preFetch Price pre-fetch promise returned from preFetchPrice()
60
+ */
61
+ getFromBtcSwapAmount(fromAmount, toToken, tokenChainIdentification, roundUp, preFetch) {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ const coin = this.getTokenData(toToken, tokenChainIdentification);
64
+ const price = (preFetch == null ? null : yield preFetch) || (yield this.getPrice(coin));
65
+ return fromAmount
66
+ .mul(new BN(10).pow(new BN(coin.decimals)))
67
+ .mul(new BN(1000000)) //To usat
68
+ .add(roundUp ? price.sub(new BN(1)) : new BN(0))
69
+ .div(price);
70
+ });
71
+ }
72
+ }
73
+ exports.ISwapPrice = ISwapPrice;
@@ -0,0 +1,186 @@
1
+ import { Express, Request } from "express";
2
+ import { ISwapPrice } from "./ISwapPrice";
3
+ import { ChainType, ClaimEvent, InitializeEvent, RefundEvent, SwapData, SwapEvent } from "@atomiqlabs/base";
4
+ import { AuthenticatedLnd } from "lightning";
5
+ import { SwapHandlerSwap } from "./SwapHandlerSwap";
6
+ import { IIntermediaryStorage } from "../storage/IIntermediaryStorage";
7
+ import * as BN from "bn.js";
8
+ import { ServerParamEncoder } from "../utils/paramcoders/server/ServerParamEncoder";
9
+ import { IParamReader } from "../utils/paramcoders/IParamReader";
10
+ export declare enum SwapHandlerType {
11
+ TO_BTC = "TO_BTC",
12
+ FROM_BTC = "FROM_BTC",
13
+ TO_BTCLN = "TO_BTCLN",
14
+ FROM_BTCLN = "FROM_BTCLN",
15
+ FROM_BTCLN_TRUSTED = "FROM_BTCLN_TRUSTED",
16
+ FROM_BTC_TRUSTED = "FROM_BTC_TRUSTED"
17
+ }
18
+ export type SwapHandlerInfoType = {
19
+ swapFeePPM: number;
20
+ swapBaseFee: number;
21
+ min: number;
22
+ max: number;
23
+ tokens: string[];
24
+ chainTokens: {
25
+ [chainId: string]: string[];
26
+ };
27
+ data?: any;
28
+ };
29
+ export type SwapBaseConfig = {
30
+ authorizationTimeout: number;
31
+ bitcoinBlocktime: BN;
32
+ baseFee: BN;
33
+ feePPM: BN;
34
+ max: BN;
35
+ min: BN;
36
+ maxSkew: number;
37
+ safetyFactor: BN;
38
+ swapCheckInterval: number;
39
+ };
40
+ export type MultichainData = {
41
+ chains: {
42
+ [identifier: string]: ChainData;
43
+ };
44
+ default: string;
45
+ };
46
+ export type ChainData<T extends ChainType = ChainType> = {
47
+ signer: T["Signer"];
48
+ swapContract: T["Contract"];
49
+ chainEvents: T["Events"];
50
+ allowedTokens: string[];
51
+ btcRelay?: T["BtcRelay"];
52
+ };
53
+ export type RequestData<T> = {
54
+ chainIdentifier: string;
55
+ raw: Request & {
56
+ paramReader: IParamReader;
57
+ };
58
+ parsed: T;
59
+ metadata: any;
60
+ };
61
+ /**
62
+ * An abstract class defining a singular swap service
63
+ */
64
+ export declare abstract class SwapHandler<V extends SwapHandlerSwap<SwapData, S> = SwapHandlerSwap, S = any> {
65
+ abstract readonly type: SwapHandlerType;
66
+ readonly storageManager: IIntermediaryStorage<V>;
67
+ readonly path: string;
68
+ readonly chains: MultichainData;
69
+ readonly allowedTokens: {
70
+ [chainId: string]: Set<string>;
71
+ };
72
+ readonly swapPricing: ISwapPrice;
73
+ readonly LND: AuthenticatedLnd;
74
+ abstract config: SwapBaseConfig;
75
+ logger: {
76
+ debug: (msg: string, ...args: any) => void;
77
+ info: (msg: string, ...args: any) => void;
78
+ warn: (msg: string, ...args: any) => void;
79
+ error: (msg: string, ...args: any) => void;
80
+ };
81
+ protected swapLogger: {
82
+ debug: (swap: SwapHandlerSwap | SwapEvent<SwapData> | SwapData, msg: string, ...args: any) => void;
83
+ info: (swap: SwapHandlerSwap | SwapEvent<SwapData> | SwapData, msg: string, ...args: any) => void;
84
+ warn: (swap: SwapHandlerSwap | SwapEvent<SwapData> | SwapData, msg: string, ...args: any) => void;
85
+ error: (swap: SwapHandlerSwap | SwapEvent<SwapData> | SwapData, msg: string, ...args: any) => void;
86
+ };
87
+ protected constructor(storageDirectory: IIntermediaryStorage<V>, path: string, chainsData: MultichainData, lnd: AuthenticatedLnd, swapPricing: ISwapPrice);
88
+ protected getDefaultChain(): ChainData;
89
+ protected getChain(identifier: string): ChainData;
90
+ protected abstract processPastSwaps(): Promise<void>;
91
+ /**
92
+ * Starts the watchdog checking past swaps for expiry or claim eligibility.
93
+ */
94
+ startWatchdog(): Promise<void>;
95
+ protected abstract processInitializeEvent(chainIdentifier: string, event: InitializeEvent<SwapData>): Promise<void>;
96
+ protected abstract processClaimEvent(chainIdentifier: string, event: ClaimEvent<SwapData>): Promise<void>;
97
+ protected abstract processRefundEvent(chainIdentifier: string, event: RefundEvent<SwapData>): Promise<void>;
98
+ /**
99
+ * Chain event processor
100
+ *
101
+ * @param chainIdentifier
102
+ * @param eventData
103
+ */
104
+ protected processEvent(chainIdentifier: string, eventData: SwapEvent<SwapData>[]): Promise<boolean>;
105
+ /**
106
+ * Initializes chain events subscription
107
+ */
108
+ protected subscribeToEvents(): void;
109
+ /**
110
+ * Initializes swap handler, loads data and subscribes to chain events
111
+ */
112
+ abstract init(): Promise<void>;
113
+ /**
114
+ * Sets up required listeners for the REST server
115
+ *
116
+ * @param restServer
117
+ */
118
+ abstract startRestServer(restServer: Express): void;
119
+ /**
120
+ * Returns data to be returned in swap handler info
121
+ */
122
+ abstract getInfoData(): any;
123
+ /**
124
+ * Remove swap data
125
+ *
126
+ * @param hash
127
+ * @param sequence
128
+ */
129
+ protected removeSwapData(hash: string, sequence: BN): Promise<void>;
130
+ /**
131
+ * Remove swap data
132
+ *
133
+ * @param swap
134
+ * @param ultimateState set the ultimate state of the swap before removing
135
+ */
136
+ protected removeSwapData(swap: V, ultimateState?: S): Promise<void>;
137
+ /**
138
+ * Checks whether the bitcoin amount is within specified min/max bounds
139
+ *
140
+ * @param amount
141
+ * @protected
142
+ * @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds
143
+ */
144
+ protected checkBtcAmountInBounds(amount: BN): void;
145
+ /**
146
+ * Handles and throws plugin errors
147
+ *
148
+ * @param res Response as returned from the PluginManager.onHandlePost{To,From}BtcQuote
149
+ * @protected
150
+ * @throws {DefinedRuntimeError} will throw an error if the response is an error
151
+ */
152
+ protected handlePluginErrorResponses(res: any): void;
153
+ /**
154
+ * Creates an abort controller that extends the responseStream's abort signal
155
+ *
156
+ * @param responseStream
157
+ */
158
+ protected getAbortController(responseStream: ServerParamEncoder): AbortController;
159
+ /**
160
+ * Starts a pre-fetch for signature data
161
+ *
162
+ * @param chainIdentifier
163
+ * @param abortController
164
+ * @param responseStream
165
+ */
166
+ protected getSignDataPrefetch(chainIdentifier: string, abortController: AbortController, responseStream?: ServerParamEncoder): Promise<any>;
167
+ protected getIdentifierFromEvent(event: SwapEvent<SwapData>): string;
168
+ protected getIdentifierFromSwapData(swapData: SwapData): string;
169
+ protected getIdentifier(swap: SwapHandlerSwap | SwapEvent<SwapData> | SwapData): string;
170
+ /**
171
+ * Checks if the sequence number is between 0-2^64
172
+ *
173
+ * @param sequence
174
+ * @throws {DefinedRuntimeError} will throw an error if sequence number is out of bounds
175
+ */
176
+ protected checkSequence(sequence: BN): void;
177
+ /**
178
+ * Checks whether a given token is supported on a specified chain
179
+ *
180
+ * @param chainId
181
+ * @param token
182
+ * @protected
183
+ */
184
+ protected isTokenSupported(chainId: string, token: string): boolean;
185
+ getInfo(): SwapHandlerInfoType;
186
+ }
@@ -0,0 +1,292 @@
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
+ exports.SwapHandler = exports.SwapHandlerType = void 0;
13
+ const base_1 = require("@atomiqlabs/base");
14
+ const SwapHandlerSwap_1 = require("./SwapHandlerSwap");
15
+ const PluginManager_1 = require("../plugins/PluginManager");
16
+ const BN = require("bn.js");
17
+ const IPlugin_1 = require("../plugins/IPlugin");
18
+ var SwapHandlerType;
19
+ (function (SwapHandlerType) {
20
+ SwapHandlerType["TO_BTC"] = "TO_BTC";
21
+ SwapHandlerType["FROM_BTC"] = "FROM_BTC";
22
+ SwapHandlerType["TO_BTCLN"] = "TO_BTCLN";
23
+ SwapHandlerType["FROM_BTCLN"] = "FROM_BTCLN";
24
+ SwapHandlerType["FROM_BTCLN_TRUSTED"] = "FROM_BTCLN_TRUSTED";
25
+ SwapHandlerType["FROM_BTC_TRUSTED"] = "FROM_BTC_TRUSTED";
26
+ })(SwapHandlerType = exports.SwapHandlerType || (exports.SwapHandlerType = {}));
27
+ /**
28
+ * An abstract class defining a singular swap service
29
+ */
30
+ class SwapHandler {
31
+ constructor(storageDirectory, path, chainsData, lnd, swapPricing) {
32
+ this.logger = {
33
+ debug: (msg, ...args) => console.debug("SwapHandler(" + this.type + "): " + msg, ...args),
34
+ info: (msg, ...args) => console.info("SwapHandler(" + this.type + "): " + msg, ...args),
35
+ warn: (msg, ...args) => console.warn("SwapHandler(" + this.type + "): " + msg, ...args),
36
+ error: (msg, ...args) => console.error("SwapHandler(" + this.type + "): " + msg, ...args)
37
+ };
38
+ this.swapLogger = {
39
+ debug: (swap, msg, ...args) => this.logger.debug(this.getIdentifier(swap) + ": " + msg, ...args),
40
+ info: (swap, msg, ...args) => this.logger.info(this.getIdentifier(swap) + ": " + msg, ...args),
41
+ warn: (swap, msg, ...args) => this.logger.warn(this.getIdentifier(swap) + ": " + msg, ...args),
42
+ error: (swap, msg, ...args) => this.logger.error(this.getIdentifier(swap) + ": " + msg, ...args)
43
+ };
44
+ this.storageManager = storageDirectory;
45
+ this.chains = chainsData;
46
+ if (this.chains.chains[this.chains.default] == null)
47
+ throw new Error("Invalid default chain specified");
48
+ this.path = path;
49
+ this.LND = lnd;
50
+ this.swapPricing = swapPricing;
51
+ this.allowedTokens = {};
52
+ for (let chainId in chainsData.chains) {
53
+ this.allowedTokens[chainId] = new Set(chainsData.chains[chainId].allowedTokens);
54
+ }
55
+ }
56
+ getDefaultChain() {
57
+ return this.chains.chains[this.chains.default];
58
+ }
59
+ getChain(identifier) {
60
+ if (this.chains.chains[identifier] == null)
61
+ throw {
62
+ code: 20200,
63
+ msg: "Invalid chain specified!"
64
+ };
65
+ return this.chains.chains[identifier];
66
+ }
67
+ /**
68
+ * Starts the watchdog checking past swaps for expiry or claim eligibility.
69
+ */
70
+ startWatchdog() {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ let rerun;
73
+ rerun = () => __awaiter(this, void 0, void 0, function* () {
74
+ yield this.processPastSwaps().catch(e => console.error(e));
75
+ setTimeout(rerun, this.config.swapCheckInterval);
76
+ });
77
+ yield rerun();
78
+ });
79
+ }
80
+ /**
81
+ * Chain event processor
82
+ *
83
+ * @param chainIdentifier
84
+ * @param eventData
85
+ */
86
+ processEvent(chainIdentifier, eventData) {
87
+ return __awaiter(this, void 0, void 0, function* () {
88
+ for (let event of eventData) {
89
+ if (event instanceof base_1.InitializeEvent) {
90
+ // this.swapLogger.debug(event, "SC: InitializeEvent: swap type: "+event.swapType);
91
+ yield this.processInitializeEvent(chainIdentifier, event);
92
+ }
93
+ else if (event instanceof base_1.ClaimEvent) {
94
+ // this.swapLogger.debug(event, "SC: ClaimEvent: swap secret: "+event.secret);
95
+ yield this.processClaimEvent(chainIdentifier, event);
96
+ }
97
+ else if (event instanceof base_1.RefundEvent) {
98
+ // this.swapLogger.debug(event, "SC: RefundEvent");
99
+ yield this.processRefundEvent(chainIdentifier, event);
100
+ }
101
+ }
102
+ return true;
103
+ });
104
+ }
105
+ /**
106
+ * Initializes chain events subscription
107
+ */
108
+ subscribeToEvents() {
109
+ for (let key in this.chains.chains) {
110
+ this.chains.chains[key].chainEvents.registerListener((events) => this.processEvent(key, events));
111
+ }
112
+ this.logger.info("SC: Events: subscribed to smartchain events");
113
+ }
114
+ removeSwapData(hashOrSwap, sequenceOrUltimateState) {
115
+ return __awaiter(this, void 0, void 0, function* () {
116
+ let swap;
117
+ if (typeof (hashOrSwap) === "string") {
118
+ if (!BN.isBN(sequenceOrUltimateState))
119
+ throw new Error("Sequence must be a BN instance!");
120
+ swap = yield this.storageManager.getData(hashOrSwap, sequenceOrUltimateState);
121
+ }
122
+ else {
123
+ swap = hashOrSwap;
124
+ if (sequenceOrUltimateState != null && !BN.isBN(sequenceOrUltimateState))
125
+ yield swap.setState(sequenceOrUltimateState);
126
+ }
127
+ if (swap != null)
128
+ yield PluginManager_1.PluginManager.swapRemove(swap);
129
+ this.swapLogger.debug(swap, "removeSwapData(): removing swap final state: " + swap.state);
130
+ yield this.storageManager.removeData(swap.getHash(), swap.getSequence());
131
+ });
132
+ }
133
+ /**
134
+ * Checks whether the bitcoin amount is within specified min/max bounds
135
+ *
136
+ * @param amount
137
+ * @protected
138
+ * @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds
139
+ */
140
+ checkBtcAmountInBounds(amount) {
141
+ if (amount.lt(this.config.min)) {
142
+ throw {
143
+ code: 20003,
144
+ msg: "Amount too low!",
145
+ data: {
146
+ min: this.config.min.toString(10),
147
+ max: this.config.max.toString(10)
148
+ }
149
+ };
150
+ }
151
+ if (amount.gt(this.config.max)) {
152
+ throw {
153
+ code: 20004,
154
+ msg: "Amount too high!",
155
+ data: {
156
+ min: this.config.min.toString(10),
157
+ max: this.config.max.toString(10)
158
+ }
159
+ };
160
+ }
161
+ }
162
+ /**
163
+ * Handles and throws plugin errors
164
+ *
165
+ * @param res Response as returned from the PluginManager.onHandlePost{To,From}BtcQuote
166
+ * @protected
167
+ * @throws {DefinedRuntimeError} will throw an error if the response is an error
168
+ */
169
+ handlePluginErrorResponses(res) {
170
+ if ((0, IPlugin_1.isQuoteThrow)(res))
171
+ throw {
172
+ code: 29999,
173
+ msg: res.message
174
+ };
175
+ if ((0, IPlugin_1.isQuoteAmountTooHigh)(res))
176
+ throw {
177
+ code: 20004,
178
+ msg: "Amount too high!",
179
+ data: {
180
+ min: res.data.min.toString(10),
181
+ max: res.data.max.toString(10)
182
+ }
183
+ };
184
+ if ((0, IPlugin_1.isQuoteAmountTooLow)(res))
185
+ throw {
186
+ code: 20003,
187
+ msg: "Amount too low!",
188
+ data: {
189
+ min: res.data.min.toString(10),
190
+ max: res.data.max.toString(10)
191
+ }
192
+ };
193
+ }
194
+ /**
195
+ * Creates an abort controller that extends the responseStream's abort signal
196
+ *
197
+ * @param responseStream
198
+ */
199
+ getAbortController(responseStream) {
200
+ const abortController = new AbortController();
201
+ const responseStreamAbortController = responseStream.getAbortSignal();
202
+ responseStreamAbortController.addEventListener("abort", () => abortController.abort(responseStreamAbortController.reason));
203
+ return abortController;
204
+ }
205
+ /**
206
+ * Starts a pre-fetch for signature data
207
+ *
208
+ * @param chainIdentifier
209
+ * @param abortController
210
+ * @param responseStream
211
+ */
212
+ getSignDataPrefetch(chainIdentifier, abortController, responseStream) {
213
+ const { swapContract } = this.getChain(chainIdentifier);
214
+ let signDataPrefetchPromise = swapContract.preFetchBlockDataForSignatures != null ? swapContract.preFetchBlockDataForSignatures().catch(e => {
215
+ this.logger.error("getSignDataPrefetch(): signDataPrefetch: ", e);
216
+ abortController.abort(e);
217
+ return null;
218
+ }) : null;
219
+ if (signDataPrefetchPromise != null && responseStream != null) {
220
+ signDataPrefetchPromise = signDataPrefetchPromise.then(val => val == null || abortController.signal.aborted ? null : responseStream.writeParams({
221
+ signDataPrefetch: val
222
+ }).then(() => val).catch(e => {
223
+ this.logger.error("getSignDataPrefetch(): signDataPreFetch: error when sending sign data to the client: ", e);
224
+ abortController.abort(e);
225
+ return null;
226
+ }));
227
+ }
228
+ return signDataPrefetchPromise;
229
+ }
230
+ getIdentifierFromEvent(event) {
231
+ if (event.sequence.isZero())
232
+ return event.paymentHash;
233
+ return event.paymentHash + "_" + event.sequence.toString(16);
234
+ }
235
+ getIdentifierFromSwapData(swapData) {
236
+ if (swapData.getSequence().isZero())
237
+ return swapData.getHash();
238
+ return swapData.getHash() + "_" + swapData.getSequence().toString(16);
239
+ }
240
+ getIdentifier(swap) {
241
+ if (swap instanceof SwapHandlerSwap_1.SwapHandlerSwap) {
242
+ return swap.getIdentifier();
243
+ }
244
+ if (swap instanceof base_1.SwapEvent) {
245
+ return this.getIdentifierFromEvent(swap);
246
+ }
247
+ return this.getIdentifierFromSwapData(swap);
248
+ }
249
+ /**
250
+ * Checks if the sequence number is between 0-2^64
251
+ *
252
+ * @param sequence
253
+ * @throws {DefinedRuntimeError} will throw an error if sequence number is out of bounds
254
+ */
255
+ checkSequence(sequence) {
256
+ if (sequence.isNeg() || sequence.gte(new BN(2).pow(new BN(64)))) {
257
+ throw {
258
+ code: 20060,
259
+ msg: "Invalid sequence"
260
+ };
261
+ }
262
+ }
263
+ /**
264
+ * Checks whether a given token is supported on a specified chain
265
+ *
266
+ * @param chainId
267
+ * @param token
268
+ * @protected
269
+ */
270
+ isTokenSupported(chainId, token) {
271
+ const chainTokens = this.allowedTokens[chainId];
272
+ if (chainTokens == null)
273
+ return false;
274
+ return chainTokens.has(token);
275
+ }
276
+ getInfo() {
277
+ const chainTokens = {};
278
+ for (let chainId in this.allowedTokens) {
279
+ chainTokens[chainId] = Array.from(this.allowedTokens[chainId]);
280
+ }
281
+ return {
282
+ swapFeePPM: this.config.feePPM.toNumber(),
283
+ swapBaseFee: this.config.baseFee.toNumber(),
284
+ min: this.config.min.toNumber(),
285
+ max: this.config.max.toNumber(),
286
+ data: this.getInfoData(),
287
+ tokens: Array.from(this.allowedTokens[this.chains.default]),
288
+ chainTokens
289
+ };
290
+ }
291
+ }
292
+ exports.SwapHandler = SwapHandler;