@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.
- package/LICENSE +201 -0
- package/dist/fees/IBtcFeeEstimator.d.ts +3 -0
- package/dist/fees/IBtcFeeEstimator.js +2 -0
- package/dist/fees/OneDollarFeeEstimator.d.ts +16 -0
- package/dist/fees/OneDollarFeeEstimator.js +71 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +52 -0
- package/dist/info/InfoHandler.d.ts +17 -0
- package/dist/info/InfoHandler.js +70 -0
- package/dist/plugins/IPlugin.d.ts +118 -0
- package/dist/plugins/IPlugin.js +33 -0
- package/dist/plugins/PluginManager.d.ts +89 -0
- package/dist/plugins/PluginManager.js +263 -0
- package/dist/prices/BinanceSwapPrice.d.ts +27 -0
- package/dist/prices/BinanceSwapPrice.js +106 -0
- package/dist/prices/CoinGeckoSwapPrice.d.ts +31 -0
- package/dist/prices/CoinGeckoSwapPrice.js +76 -0
- package/dist/storage/IIntermediaryStorage.d.ts +15 -0
- package/dist/storage/IIntermediaryStorage.js +2 -0
- package/dist/storagemanager/IntermediaryStorageManager.d.ts +15 -0
- package/dist/storagemanager/IntermediaryStorageManager.js +113 -0
- package/dist/storagemanager/StorageManager.d.ts +12 -0
- package/dist/storagemanager/StorageManager.js +74 -0
- package/dist/swaps/FromBtcBaseSwap.d.ts +12 -0
- package/dist/swaps/FromBtcBaseSwap.js +16 -0
- package/dist/swaps/FromBtcBaseSwapHandler.d.ts +118 -0
- package/dist/swaps/FromBtcBaseSwapHandler.js +294 -0
- package/dist/swaps/FromBtcLnBaseSwapHandler.d.ts +25 -0
- package/dist/swaps/FromBtcLnBaseSwapHandler.js +55 -0
- package/dist/swaps/ISwapPrice.d.ts +44 -0
- package/dist/swaps/ISwapPrice.js +73 -0
- package/dist/swaps/SwapHandler.d.ts +186 -0
- package/dist/swaps/SwapHandler.js +292 -0
- package/dist/swaps/SwapHandlerSwap.d.ts +75 -0
- package/dist/swaps/SwapHandlerSwap.js +72 -0
- package/dist/swaps/ToBtcBaseSwap.d.ts +35 -0
- package/dist/swaps/ToBtcBaseSwap.js +61 -0
- package/dist/swaps/ToBtcBaseSwapHandler.d.ts +94 -0
- package/dist/swaps/ToBtcBaseSwapHandler.js +233 -0
- package/dist/swaps/frombtc_abstract/FromBtcAbs.d.ts +92 -0
- package/dist/swaps/frombtc_abstract/FromBtcAbs.js +386 -0
- package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.d.ts +26 -0
- package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.js +63 -0
- package/dist/swaps/frombtc_trusted/FromBtcTrusted.d.ts +55 -0
- package/dist/swaps/frombtc_trusted/FromBtcTrusted.js +586 -0
- package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.d.ts +43 -0
- package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.js +99 -0
- package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.d.ts +105 -0
- package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.js +731 -0
- package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +29 -0
- package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.js +64 -0
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.d.ts +79 -0
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.js +514 -0
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +28 -0
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.js +66 -0
- package/dist/swaps/tobtc_abstract/ToBtcAbs.d.ts +290 -0
- package/dist/swaps/tobtc_abstract/ToBtcAbs.js +1056 -0
- package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.d.ts +29 -0
- package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.js +70 -0
- package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.d.ts +246 -0
- package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.js +1169 -0
- package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +27 -0
- package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.js +65 -0
- package/dist/utils/Utils.d.ts +32 -0
- package/dist/utils/Utils.js +109 -0
- package/dist/utils/coinselect2/accumulative.d.ts +6 -0
- package/dist/utils/coinselect2/accumulative.js +44 -0
- package/dist/utils/coinselect2/blackjack.d.ts +6 -0
- package/dist/utils/coinselect2/blackjack.js +41 -0
- package/dist/utils/coinselect2/index.d.ts +16 -0
- package/dist/utils/coinselect2/index.js +40 -0
- package/dist/utils/coinselect2/utils.d.ts +64 -0
- package/dist/utils/coinselect2/utils.js +121 -0
- package/dist/utils/paramcoders/IParamReader.d.ts +5 -0
- package/dist/utils/paramcoders/IParamReader.js +2 -0
- package/dist/utils/paramcoders/IParamWriter.d.ts +4 -0
- package/dist/utils/paramcoders/IParamWriter.js +2 -0
- package/dist/utils/paramcoders/LegacyParamEncoder.d.ts +10 -0
- package/dist/utils/paramcoders/LegacyParamEncoder.js +33 -0
- package/dist/utils/paramcoders/ParamDecoder.d.ts +25 -0
- package/dist/utils/paramcoders/ParamDecoder.js +234 -0
- package/dist/utils/paramcoders/ParamEncoder.d.ts +9 -0
- package/dist/utils/paramcoders/ParamEncoder.js +22 -0
- package/dist/utils/paramcoders/SchemaVerifier.d.ts +22 -0
- package/dist/utils/paramcoders/SchemaVerifier.js +85 -0
- package/dist/utils/paramcoders/server/ServerParamDecoder.d.ts +8 -0
- package/dist/utils/paramcoders/server/ServerParamDecoder.js +105 -0
- package/dist/utils/paramcoders/server/ServerParamEncoder.d.ts +11 -0
- package/dist/utils/paramcoders/server/ServerParamEncoder.js +76 -0
- package/package.json +43 -0
- package/src/fees/IBtcFeeEstimator.ts +7 -0
- package/src/fees/OneDollarFeeEstimator.ts +95 -0
- package/src/index.ts +46 -0
- package/src/info/InfoHandler.ts +106 -0
- package/src/plugins/IPlugin.ts +155 -0
- package/src/plugins/PluginManager.ts +310 -0
- package/src/prices/BinanceSwapPrice.ts +114 -0
- package/src/prices/CoinGeckoSwapPrice.ts +88 -0
- package/src/storage/IIntermediaryStorage.ts +21 -0
- package/src/storagemanager/IntermediaryStorageManager.ts +101 -0
- package/src/storagemanager/StorageManager.ts +68 -0
- package/src/swaps/FromBtcBaseSwap.ts +21 -0
- package/src/swaps/FromBtcBaseSwapHandler.ts +375 -0
- package/src/swaps/FromBtcLnBaseSwapHandler.ts +48 -0
- package/src/swaps/ISwapPrice.ts +94 -0
- package/src/swaps/SwapHandler.ts +404 -0
- package/src/swaps/SwapHandlerSwap.ts +133 -0
- package/src/swaps/ToBtcBaseSwap.ts +76 -0
- package/src/swaps/ToBtcBaseSwapHandler.ts +309 -0
- package/src/swaps/frombtc_abstract/FromBtcAbs.ts +484 -0
- package/src/swaps/frombtc_abstract/FromBtcSwapAbs.ts +77 -0
- package/src/swaps/frombtc_trusted/FromBtcTrusted.ts +661 -0
- package/src/swaps/frombtc_trusted/FromBtcTrustedSwap.ts +158 -0
- package/src/swaps/frombtcln_abstract/FromBtcLnAbs.ts +864 -0
- package/src/swaps/frombtcln_abstract/FromBtcLnSwapAbs.ts +82 -0
- package/src/swaps/frombtcln_trusted/FromBtcLnTrusted.ts +592 -0
- package/src/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.ts +90 -0
- package/src/swaps/tobtc_abstract/ToBtcAbs.ts +1249 -0
- package/src/swaps/tobtc_abstract/ToBtcSwapAbs.ts +112 -0
- package/src/swaps/tobtcln_abstract/ToBtcLnAbs.ts +1422 -0
- package/src/swaps/tobtcln_abstract/ToBtcLnSwapAbs.ts +87 -0
- package/src/utils/Utils.ts +108 -0
- package/src/utils/coinselect2/accumulative.js +32 -0
- package/src/utils/coinselect2/accumulative.ts +58 -0
- package/src/utils/coinselect2/blackjack.js +29 -0
- package/src/utils/coinselect2/blackjack.ts +54 -0
- package/src/utils/coinselect2/index.js +16 -0
- package/src/utils/coinselect2/index.ts +50 -0
- package/src/utils/coinselect2/utils.js +110 -0
- package/src/utils/coinselect2/utils.ts +183 -0
- package/src/utils/paramcoders/IParamReader.ts +8 -0
- package/src/utils/paramcoders/IParamWriter.ts +8 -0
- package/src/utils/paramcoders/LegacyParamEncoder.ts +28 -0
- package/src/utils/paramcoders/ParamDecoder.ts +219 -0
- package/src/utils/paramcoders/ParamEncoder.ts +30 -0
- package/src/utils/paramcoders/SchemaVerifier.ts +97 -0
- package/src/utils/paramcoders/server/ServerParamDecoder.ts +115 -0
- package/src/utils/paramcoders/server/ServerParamEncoder.ts +76 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { BitcoinRpc, SwapData } from "@atomiqlabs/base";
|
|
2
|
+
import { IPlugin, PluginQuote, QuoteAmountTooHigh, QuoteAmountTooLow, QuoteSetFees, QuoteThrow, ToBtcPluginQuote } from "./IPlugin";
|
|
3
|
+
import { FromBtcLnRequestType, FromBtcRequestType, ISwapPrice, MultichainData, RequestData, SwapHandler, ToBtcLnRequestType, ToBtcRequestType } from "..";
|
|
4
|
+
import { SwapHandlerSwap } from "../swaps/SwapHandlerSwap";
|
|
5
|
+
import { AuthenticatedLnd } from "lightning";
|
|
6
|
+
import * as BN from "bn.js";
|
|
7
|
+
import { FromBtcLnTrustedRequestType } from "../swaps/frombtcln_trusted/FromBtcLnTrusted";
|
|
8
|
+
export type FailSwapResponse = {
|
|
9
|
+
type: "fail";
|
|
10
|
+
code?: number;
|
|
11
|
+
msg?: string;
|
|
12
|
+
};
|
|
13
|
+
export type FeeSwapResponse = {
|
|
14
|
+
type: "fee";
|
|
15
|
+
baseFee: BN;
|
|
16
|
+
feePPM: BN;
|
|
17
|
+
};
|
|
18
|
+
export type AmountAndFeeSwapResponse = {
|
|
19
|
+
type: "amountAndFee";
|
|
20
|
+
baseFee?: BN;
|
|
21
|
+
feePPM?: BN;
|
|
22
|
+
amount: BN;
|
|
23
|
+
};
|
|
24
|
+
export type SwapResponse = FailSwapResponse | FeeSwapResponse | AmountAndFeeSwapResponse;
|
|
25
|
+
export declare class PluginManager {
|
|
26
|
+
static plugins: Map<string, IPlugin>;
|
|
27
|
+
static registerPlugin(name: string, plugin: IPlugin): void;
|
|
28
|
+
static unregisterPlugin(name: string): boolean;
|
|
29
|
+
static enable<T extends SwapData>(chainsData: MultichainData, bitcoinRpc: BitcoinRpc<any>, lnd: AuthenticatedLnd, swapPricing: ISwapPrice, tokens: {
|
|
30
|
+
[ticker: string]: {
|
|
31
|
+
[chainId: string]: {
|
|
32
|
+
address: string;
|
|
33
|
+
decimals: number;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
}, directory: string): Promise<void>;
|
|
37
|
+
static disable(): Promise<void>;
|
|
38
|
+
static serviceInitialize(handler: SwapHandler<any>): Promise<void>;
|
|
39
|
+
static onHttpServerStarted(httpServer: any): Promise<void>;
|
|
40
|
+
static swapStateChange<T extends SwapData>(swap: SwapHandlerSwap<T>, oldState?: any): Promise<void>;
|
|
41
|
+
static swapCreate<T extends SwapData>(swap: SwapHandlerSwap<T>): Promise<void>;
|
|
42
|
+
static swapRemove<T extends SwapData>(swap: SwapHandlerSwap<T>): Promise<void>;
|
|
43
|
+
static onHandlePostFromBtcQuote(request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType>, requestedAmount: {
|
|
44
|
+
input: boolean;
|
|
45
|
+
amount: BN;
|
|
46
|
+
}, chainIdentifier: string, token: string, constraints: {
|
|
47
|
+
minInBtc: BN;
|
|
48
|
+
maxInBtc: BN;
|
|
49
|
+
}, fees: {
|
|
50
|
+
baseFeeInBtc: BN;
|
|
51
|
+
feePPM: BN;
|
|
52
|
+
}, pricePrefetchPromise?: Promise<BN> | null): Promise<QuoteThrow | QuoteSetFees | QuoteAmountTooLow | QuoteAmountTooHigh | PluginQuote>;
|
|
53
|
+
static onHandlePreFromBtcQuote(request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType>, requestedAmount: {
|
|
54
|
+
input: boolean;
|
|
55
|
+
amount: BN;
|
|
56
|
+
}, chainIdentifier: string, token: string, constraints: {
|
|
57
|
+
minInBtc: BN;
|
|
58
|
+
maxInBtc: BN;
|
|
59
|
+
}, fees: {
|
|
60
|
+
baseFeeInBtc: BN;
|
|
61
|
+
feePPM: BN;
|
|
62
|
+
}): Promise<QuoteThrow | QuoteSetFees | QuoteAmountTooLow | QuoteAmountTooHigh>;
|
|
63
|
+
static onHandlePostToBtcQuote<T extends {
|
|
64
|
+
networkFee: BN;
|
|
65
|
+
}>(request: RequestData<ToBtcLnRequestType | ToBtcRequestType>, requestedAmount: {
|
|
66
|
+
input: boolean;
|
|
67
|
+
amount: BN;
|
|
68
|
+
}, chainIdentifier: string, token: string, constraints: {
|
|
69
|
+
minInBtc: BN;
|
|
70
|
+
maxInBtc: BN;
|
|
71
|
+
}, fees: {
|
|
72
|
+
baseFeeInBtc: BN;
|
|
73
|
+
feePPM: BN;
|
|
74
|
+
networkFeeGetter: (amount: BN) => Promise<T>;
|
|
75
|
+
}, pricePrefetchPromise?: Promise<BN> | null): Promise<QuoteThrow | QuoteSetFees | QuoteAmountTooLow | QuoteAmountTooHigh | (ToBtcPluginQuote & {
|
|
76
|
+
networkFeeData: T;
|
|
77
|
+
})>;
|
|
78
|
+
static onHandlePreToBtcQuote(request: RequestData<ToBtcLnRequestType | ToBtcRequestType>, requestedAmount: {
|
|
79
|
+
input: boolean;
|
|
80
|
+
amount: BN;
|
|
81
|
+
}, chainIdentifier: string, token: string, constraints: {
|
|
82
|
+
minInBtc: BN;
|
|
83
|
+
maxInBtc: BN;
|
|
84
|
+
}, fees: {
|
|
85
|
+
baseFeeInBtc: BN;
|
|
86
|
+
feePPM: BN;
|
|
87
|
+
}): Promise<QuoteThrow | QuoteSetFees | QuoteAmountTooLow | QuoteAmountTooHigh>;
|
|
88
|
+
static getWhitelistedTxIds(): Set<string>;
|
|
89
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
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.PluginManager = void 0;
|
|
13
|
+
const IPlugin_1 = require("./IPlugin");
|
|
14
|
+
const fs = require("fs");
|
|
15
|
+
const Utils_1 = require("../utils/Utils");
|
|
16
|
+
const logger = (0, Utils_1.getLogger)("PluginManager: ");
|
|
17
|
+
const pluginLogger = {
|
|
18
|
+
debug: (plugin, msg, ...args) => logger.debug(plugin.name + ": " + msg, ...args),
|
|
19
|
+
info: (plugin, msg, ...args) => logger.info(plugin.name + ": " + msg, ...args),
|
|
20
|
+
warn: (plugin, msg, ...args) => logger.warn(plugin.name + ": " + msg, ...args),
|
|
21
|
+
error: (plugin, msg, ...args) => logger.error(plugin.name + ": " + msg, ...args)
|
|
22
|
+
};
|
|
23
|
+
class PluginManager {
|
|
24
|
+
static registerPlugin(name, plugin) {
|
|
25
|
+
PluginManager.plugins.set(name, plugin);
|
|
26
|
+
}
|
|
27
|
+
static unregisterPlugin(name) {
|
|
28
|
+
return PluginManager.plugins.delete(name);
|
|
29
|
+
}
|
|
30
|
+
static enable(chainsData, bitcoinRpc, lnd, swapPricing, tokens, directory) {
|
|
31
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
32
|
+
try {
|
|
33
|
+
fs.mkdirSync(directory);
|
|
34
|
+
}
|
|
35
|
+
catch (e) { }
|
|
36
|
+
for (let [name, plugin] of PluginManager.plugins.entries()) {
|
|
37
|
+
try {
|
|
38
|
+
try {
|
|
39
|
+
fs.mkdirSync(directory + "/" + name);
|
|
40
|
+
}
|
|
41
|
+
catch (e) { }
|
|
42
|
+
yield plugin.onEnable(chainsData, bitcoinRpc, lnd, swapPricing, tokens, directory + "/" + name);
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
pluginLogger.error(plugin, "enable(): plugin enable error", e);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
static disable() {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
for (let plugin of PluginManager.plugins.values()) {
|
|
53
|
+
try {
|
|
54
|
+
yield plugin.onDisable();
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
pluginLogger.error(plugin, "disable(): plugin disable error", e);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
static serviceInitialize(handler) {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
for (let plugin of PluginManager.plugins.values()) {
|
|
65
|
+
try {
|
|
66
|
+
yield plugin.onServiceInitialize(handler);
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
pluginLogger.error(plugin, "serviceInitialize(): plugin error", e);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
static onHttpServerStarted(httpServer) {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
for (let plugin of PluginManager.plugins.values()) {
|
|
77
|
+
try {
|
|
78
|
+
if (plugin.onHttpServerStarted != null)
|
|
79
|
+
yield plugin.onHttpServerStarted(httpServer);
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
pluginLogger.error(plugin, "onHttpServerStarted(): plugin error", e);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
static swapStateChange(swap, oldState) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
for (let plugin of PluginManager.plugins.values()) {
|
|
90
|
+
try {
|
|
91
|
+
if (plugin.onSwapStateChange != null)
|
|
92
|
+
yield plugin.onSwapStateChange(swap);
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
pluginLogger.error(plugin, "swapStateChange(): plugin error", e);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
static swapCreate(swap) {
|
|
101
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
for (let plugin of PluginManager.plugins.values()) {
|
|
103
|
+
try {
|
|
104
|
+
if (plugin.onSwapCreate != null)
|
|
105
|
+
yield plugin.onSwapCreate(swap);
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
pluginLogger.error(plugin, "swapCreate(): plugin error", e);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
static swapRemove(swap) {
|
|
114
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
for (let plugin of PluginManager.plugins.values()) {
|
|
116
|
+
try {
|
|
117
|
+
if (plugin.onSwapRemove != null)
|
|
118
|
+
yield plugin.onSwapRemove(swap);
|
|
119
|
+
}
|
|
120
|
+
catch (e) {
|
|
121
|
+
pluginLogger.error(plugin, "swapRemove(): plugin error", e);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
static onHandlePostFromBtcQuote(request, requestedAmount, chainIdentifier, token, constraints, fees, pricePrefetchPromise) {
|
|
127
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
128
|
+
for (let plugin of PluginManager.plugins.values()) {
|
|
129
|
+
try {
|
|
130
|
+
if (plugin.onHandlePostFromBtcQuote != null) {
|
|
131
|
+
const result = yield plugin.onHandlePostFromBtcQuote(request, requestedAmount, chainIdentifier, token, constraints, fees, pricePrefetchPromise);
|
|
132
|
+
if (result != null) {
|
|
133
|
+
if ((0, IPlugin_1.isQuoteSetFees)(result))
|
|
134
|
+
return result;
|
|
135
|
+
if ((0, IPlugin_1.isQuoteThrow)(result))
|
|
136
|
+
return result;
|
|
137
|
+
if ((0, IPlugin_1.isQuoteAmountTooHigh)(result))
|
|
138
|
+
return result;
|
|
139
|
+
if ((0, IPlugin_1.isQuoteAmountTooLow)(result))
|
|
140
|
+
return result;
|
|
141
|
+
if ((0, IPlugin_1.isPluginQuote)(result)) {
|
|
142
|
+
if (result.amount.input === requestedAmount.input)
|
|
143
|
+
throw new Error("Invalid quoting response returned, when input is set, output must be returned, and vice-versa!");
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
catch (e) {
|
|
150
|
+
pluginLogger.error(plugin, "onSwapRequestToBtcLn(): plugin error", e);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
static onHandlePreFromBtcQuote(request, requestedAmount, chainIdentifier, token, constraints, fees) {
|
|
157
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
158
|
+
for (let plugin of PluginManager.plugins.values()) {
|
|
159
|
+
try {
|
|
160
|
+
if (plugin.onHandlePreFromBtcQuote != null) {
|
|
161
|
+
const result = yield plugin.onHandlePreFromBtcQuote(request, requestedAmount, chainIdentifier, token, constraints, fees);
|
|
162
|
+
if (result != null) {
|
|
163
|
+
if ((0, IPlugin_1.isQuoteSetFees)(result))
|
|
164
|
+
return result;
|
|
165
|
+
if ((0, IPlugin_1.isQuoteThrow)(result))
|
|
166
|
+
return result;
|
|
167
|
+
if ((0, IPlugin_1.isQuoteAmountTooHigh)(result))
|
|
168
|
+
return result;
|
|
169
|
+
if ((0, IPlugin_1.isQuoteAmountTooLow)(result))
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
catch (e) {
|
|
175
|
+
pluginLogger.error(plugin, "onSwapRequestToBtcLn(): plugin error", e);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return null;
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
static onHandlePostToBtcQuote(request, requestedAmount, chainIdentifier, token, constraints, fees, pricePrefetchPromise) {
|
|
182
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
183
|
+
for (let plugin of PluginManager.plugins.values()) {
|
|
184
|
+
try {
|
|
185
|
+
if (plugin.onHandlePostToBtcQuote != null) {
|
|
186
|
+
let networkFeeData;
|
|
187
|
+
const result = yield plugin.onHandlePostToBtcQuote(request, requestedAmount, chainIdentifier, token, constraints, {
|
|
188
|
+
baseFeeInBtc: fees.baseFeeInBtc,
|
|
189
|
+
feePPM: fees.feePPM,
|
|
190
|
+
networkFeeGetter: (amount) => __awaiter(this, void 0, void 0, function* () {
|
|
191
|
+
networkFeeData = yield fees.networkFeeGetter(amount);
|
|
192
|
+
return networkFeeData.networkFee;
|
|
193
|
+
})
|
|
194
|
+
}, pricePrefetchPromise);
|
|
195
|
+
if (result != null) {
|
|
196
|
+
if ((0, IPlugin_1.isQuoteSetFees)(result))
|
|
197
|
+
return result;
|
|
198
|
+
if ((0, IPlugin_1.isQuoteThrow)(result))
|
|
199
|
+
return result;
|
|
200
|
+
if ((0, IPlugin_1.isQuoteAmountTooHigh)(result))
|
|
201
|
+
return result;
|
|
202
|
+
if ((0, IPlugin_1.isQuoteAmountTooLow)(result))
|
|
203
|
+
return result;
|
|
204
|
+
if ((0, IPlugin_1.isToBtcPluginQuote)(result)) {
|
|
205
|
+
if (result.amount.input === requestedAmount.input)
|
|
206
|
+
throw new Error("Invalid quoting response returned, when input is set, output must be returned, and vice-versa!");
|
|
207
|
+
return Object.assign(Object.assign({}, result), { networkFeeData: networkFeeData });
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
catch (e) {
|
|
213
|
+
pluginLogger.error(plugin, "onSwapRequestToBtcLn(): plugin error", e);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return null;
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
static onHandlePreToBtcQuote(request, requestedAmount, chainIdentifier, token, constraints, fees) {
|
|
220
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
221
|
+
for (let plugin of PluginManager.plugins.values()) {
|
|
222
|
+
try {
|
|
223
|
+
if (plugin.onHandlePreToBtcQuote != null) {
|
|
224
|
+
const result = yield plugin.onHandlePreToBtcQuote(request, requestedAmount, chainIdentifier, token, constraints, fees);
|
|
225
|
+
if (result != null) {
|
|
226
|
+
if ((0, IPlugin_1.isQuoteSetFees)(result))
|
|
227
|
+
return result;
|
|
228
|
+
if ((0, IPlugin_1.isQuoteThrow)(result))
|
|
229
|
+
return result;
|
|
230
|
+
if ((0, IPlugin_1.isQuoteAmountTooHigh)(result))
|
|
231
|
+
return result;
|
|
232
|
+
if ((0, IPlugin_1.isQuoteAmountTooLow)(result))
|
|
233
|
+
return result;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
catch (e) {
|
|
238
|
+
pluginLogger.error(plugin, "onSwapRequestToBtcLn(): plugin error", e);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return null;
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
static getWhitelistedTxIds() {
|
|
245
|
+
const whitelist = new Set();
|
|
246
|
+
for (let plugin of PluginManager.plugins.values()) {
|
|
247
|
+
try {
|
|
248
|
+
if (plugin.getWhitelistedTxIds != null) {
|
|
249
|
+
const result = plugin.getWhitelistedTxIds();
|
|
250
|
+
if (result != null) {
|
|
251
|
+
result.forEach(e => whitelist.add(e));
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
catch (e) {
|
|
256
|
+
pluginLogger.error(plugin, "getWhitelistedTxIds(): plugin error", e);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return whitelist;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
exports.PluginManager = PluginManager;
|
|
263
|
+
PluginManager.plugins = new Map();
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as BN from "bn.js";
|
|
2
|
+
import { ISwapPrice } from "../swaps/ISwapPrice";
|
|
3
|
+
export type BinancePriceData = {
|
|
4
|
+
[pair: string]: {
|
|
5
|
+
[chainId: string]: {
|
|
6
|
+
address: string;
|
|
7
|
+
decimals: number;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export declare class BinanceSwapPrice extends ISwapPrice<{
|
|
12
|
+
pair: string;
|
|
13
|
+
decimals: number;
|
|
14
|
+
}> {
|
|
15
|
+
url: string;
|
|
16
|
+
cache: {
|
|
17
|
+
[pair: string]: {
|
|
18
|
+
price: number;
|
|
19
|
+
expiry: number;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
constructor(url: string, coins: BinancePriceData);
|
|
23
|
+
fetchPrice(pair: string): Promise<number>;
|
|
24
|
+
getPrice(tokenData: {
|
|
25
|
+
pair: string;
|
|
26
|
+
}): Promise<BN>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
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.BinanceSwapPrice = void 0;
|
|
13
|
+
const BN = require("bn.js");
|
|
14
|
+
const ISwapPrice_1 = require("../swaps/ISwapPrice");
|
|
15
|
+
const CACHE_DURATION = 15000;
|
|
16
|
+
class BinanceSwapPrice extends ISwapPrice_1.ISwapPrice {
|
|
17
|
+
constructor(url, coins) {
|
|
18
|
+
const coinsMap = {};
|
|
19
|
+
for (let pair in coins) {
|
|
20
|
+
const chains = coins[pair];
|
|
21
|
+
for (let chainId in chains) {
|
|
22
|
+
const tokenData = chains[chainId];
|
|
23
|
+
if (coinsMap[chainId] == null)
|
|
24
|
+
coinsMap[chainId] = {};
|
|
25
|
+
coinsMap[chainId][tokenData.address] = {
|
|
26
|
+
pair,
|
|
27
|
+
decimals: tokenData.decimals
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
super(coinsMap);
|
|
32
|
+
this.cache = {};
|
|
33
|
+
this.url = url || "https://api.binance.com/api/v3";
|
|
34
|
+
}
|
|
35
|
+
fetchPrice(pair) {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
const response = yield fetch(this.url + "/ticker/price?symbol=" + pair, {
|
|
38
|
+
method: "GET"
|
|
39
|
+
});
|
|
40
|
+
if (response.status !== 200) {
|
|
41
|
+
let resp;
|
|
42
|
+
try {
|
|
43
|
+
resp = yield response.text();
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
throw new Error(response.statusText);
|
|
47
|
+
}
|
|
48
|
+
throw new Error(resp);
|
|
49
|
+
}
|
|
50
|
+
let jsonBody = yield response.json();
|
|
51
|
+
return parseFloat(jsonBody.price);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
getPrice(tokenData) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
const pair = tokenData.pair;
|
|
57
|
+
if (pair.startsWith("$fixed-")) {
|
|
58
|
+
const amt = parseFloat(pair.substring(7));
|
|
59
|
+
return new BN(Math.floor(amt * 1000000));
|
|
60
|
+
}
|
|
61
|
+
const arr = pair.split(";");
|
|
62
|
+
const promises = [];
|
|
63
|
+
const cachedValue = this.cache[pair];
|
|
64
|
+
if (cachedValue == null || cachedValue.expiry < Date.now()) {
|
|
65
|
+
let resultPrice = 1;
|
|
66
|
+
for (let pair of arr) {
|
|
67
|
+
let invert = false;
|
|
68
|
+
if (pair.startsWith("!")) {
|
|
69
|
+
invert = true;
|
|
70
|
+
pair = pair.substring(1);
|
|
71
|
+
}
|
|
72
|
+
const cachedValue = this.cache[pair];
|
|
73
|
+
if (cachedValue == null || cachedValue.expiry < Date.now()) {
|
|
74
|
+
promises.push(this.fetchPrice(pair).then(price => {
|
|
75
|
+
this.cache[pair] = {
|
|
76
|
+
price,
|
|
77
|
+
expiry: Date.now() + CACHE_DURATION
|
|
78
|
+
};
|
|
79
|
+
if (invert) {
|
|
80
|
+
resultPrice /= price;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
resultPrice *= price;
|
|
84
|
+
}
|
|
85
|
+
}));
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
if (invert) {
|
|
89
|
+
resultPrice /= cachedValue.price;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
resultPrice *= cachedValue.price;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
yield Promise.all(promises);
|
|
97
|
+
this.cache[pair] = {
|
|
98
|
+
price: resultPrice,
|
|
99
|
+
expiry: Date.now() + CACHE_DURATION
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
return new BN(Math.floor(this.cache[pair].price * 100000000000000));
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.BinanceSwapPrice = BinanceSwapPrice;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import * as BN from "bn.js";
|
|
2
|
+
import { ISwapPrice } from "../swaps/ISwapPrice";
|
|
3
|
+
export type CoinGeckoPriceData = {
|
|
4
|
+
[coinId: string]: {
|
|
5
|
+
[chainId: string]: {
|
|
6
|
+
address: string;
|
|
7
|
+
decimals: number;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export declare class CoinGeckoSwapPrice extends ISwapPrice<{
|
|
12
|
+
coinId: string;
|
|
13
|
+
decimals: number;
|
|
14
|
+
}> {
|
|
15
|
+
url: string;
|
|
16
|
+
cache: {
|
|
17
|
+
[coinId: string]: {
|
|
18
|
+
price: BN;
|
|
19
|
+
expiry: number;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
constructor(url: string, coins: CoinGeckoPriceData);
|
|
23
|
+
/**
|
|
24
|
+
* Returns coin price in mSat
|
|
25
|
+
*
|
|
26
|
+
* @param coin
|
|
27
|
+
*/
|
|
28
|
+
getPrice(coin: {
|
|
29
|
+
coinId: string;
|
|
30
|
+
}): Promise<BN>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
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.CoinGeckoSwapPrice = void 0;
|
|
13
|
+
const BN = require("bn.js");
|
|
14
|
+
const ISwapPrice_1 = require("../swaps/ISwapPrice");
|
|
15
|
+
const CACHE_DURATION = 15000;
|
|
16
|
+
class CoinGeckoSwapPrice extends ISwapPrice_1.ISwapPrice {
|
|
17
|
+
constructor(url, coins) {
|
|
18
|
+
const coinsMap = {};
|
|
19
|
+
for (let coinId in coins) {
|
|
20
|
+
const chains = coins[coinId];
|
|
21
|
+
for (let chainId in chains) {
|
|
22
|
+
const tokenData = chains[chainId];
|
|
23
|
+
if (coinsMap[chainId] == null)
|
|
24
|
+
coinsMap[chainId] = {};
|
|
25
|
+
coinsMap[chainId][tokenData.address] = {
|
|
26
|
+
coinId,
|
|
27
|
+
decimals: tokenData.decimals
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
super(coinsMap);
|
|
32
|
+
this.cache = {};
|
|
33
|
+
this.url = url || "https://api.coingecko.com/api/v3";
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns coin price in mSat
|
|
37
|
+
*
|
|
38
|
+
* @param coin
|
|
39
|
+
*/
|
|
40
|
+
getPrice(coin) {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
const coinId = coin.coinId;
|
|
43
|
+
if (coinId.startsWith("$fixed-")) {
|
|
44
|
+
const amt = parseFloat(coinId.substring(7));
|
|
45
|
+
return new BN(Math.floor(amt * 1000));
|
|
46
|
+
}
|
|
47
|
+
const cachedValue = this.cache[coinId];
|
|
48
|
+
if (cachedValue != null && cachedValue.expiry > Date.now()) {
|
|
49
|
+
return cachedValue.price;
|
|
50
|
+
}
|
|
51
|
+
const response = yield fetch(this.url + "/simple/price?ids=" + coinId + "&vs_currencies=sats&precision=3", {
|
|
52
|
+
method: "GET",
|
|
53
|
+
headers: { 'Content-Type': 'application/json' }
|
|
54
|
+
});
|
|
55
|
+
if (response.status !== 200) {
|
|
56
|
+
let resp;
|
|
57
|
+
try {
|
|
58
|
+
resp = yield response.text();
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
throw new Error(response.statusText);
|
|
62
|
+
}
|
|
63
|
+
throw new Error(resp);
|
|
64
|
+
}
|
|
65
|
+
let jsonBody = yield response.json();
|
|
66
|
+
const amt = jsonBody[coinId].sats;
|
|
67
|
+
const result = new BN(amt * 1000);
|
|
68
|
+
this.cache[coinId] = {
|
|
69
|
+
price: result,
|
|
70
|
+
expiry: Date.now() + CACHE_DURATION
|
|
71
|
+
};
|
|
72
|
+
return result;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.CoinGeckoSwapPrice = CoinGeckoSwapPrice;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { StorageObject } from "@atomiqlabs/base";
|
|
2
|
+
import * as BN from "bn.js";
|
|
3
|
+
export type StorageQueryParam = {
|
|
4
|
+
key: string;
|
|
5
|
+
value?: any;
|
|
6
|
+
values?: any[];
|
|
7
|
+
};
|
|
8
|
+
export interface IIntermediaryStorage<T extends StorageObject> {
|
|
9
|
+
init(): Promise<void>;
|
|
10
|
+
query(params: StorageQueryParam[]): Promise<T[]>;
|
|
11
|
+
getData(hash: string, sequence: BN | null): Promise<T>;
|
|
12
|
+
saveData(hash: string, sequence: BN | null, object: T): Promise<void>;
|
|
13
|
+
removeData(hash: string, sequence: BN | null): Promise<void>;
|
|
14
|
+
loadData(type: new (data: any) => T): Promise<void>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { StorageObject } from "@atomiqlabs/base";
|
|
2
|
+
import { IIntermediaryStorage, StorageQueryParam } from "../storage/IIntermediaryStorage";
|
|
3
|
+
import * as BN from "bn.js";
|
|
4
|
+
export declare class IntermediaryStorageManager<T extends StorageObject> implements IIntermediaryStorage<T> {
|
|
5
|
+
private readonly directory;
|
|
6
|
+
private type;
|
|
7
|
+
private data;
|
|
8
|
+
constructor(directory: string);
|
|
9
|
+
init(): Promise<void>;
|
|
10
|
+
query(params: StorageQueryParam[]): Promise<T[]>;
|
|
11
|
+
getData(paymentHash: string, sequence: BN | null): Promise<T>;
|
|
12
|
+
saveData(hash: string, sequence: BN | null, object: T): Promise<void>;
|
|
13
|
+
removeData(hash: string, sequence: BN | null): Promise<void>;
|
|
14
|
+
loadData(type: new (data: any) => T): Promise<void>;
|
|
15
|
+
}
|