@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.
Files changed (100) hide show
  1. package/dist/info/InfoHandler.js +3 -12
  2. package/dist/plugins/IPlugin.d.ts +34 -35
  3. package/dist/plugins/IPlugin.js +7 -8
  4. package/dist/plugins/PluginManager.d.ts +29 -30
  5. package/dist/plugins/PluginManager.js +160 -188
  6. package/dist/prices/BinanceSwapPrice.d.ts +1 -2
  7. package/dist/prices/BinanceSwapPrice.js +56 -70
  8. package/dist/prices/CoinGeckoSwapPrice.d.ts +2 -3
  9. package/dist/prices/CoinGeckoSwapPrice.js +30 -42
  10. package/dist/prices/OKXSwapPrice.d.ts +1 -2
  11. package/dist/prices/OKXSwapPrice.js +56 -70
  12. package/dist/storage/IIntermediaryStorage.d.ts +8 -5
  13. package/dist/storagemanager/IntermediaryStorageManager.d.ts +8 -5
  14. package/dist/storagemanager/IntermediaryStorageManager.js +52 -61
  15. package/dist/storagemanager/StorageManager.js +42 -59
  16. package/dist/swaps/FromBtcBaseSwap.d.ts +7 -8
  17. package/dist/swaps/FromBtcBaseSwap.js +2 -3
  18. package/dist/swaps/FromBtcBaseSwapHandler.d.ts +44 -31
  19. package/dist/swaps/FromBtcBaseSwapHandler.js +217 -196
  20. package/dist/swaps/FromBtcLnBaseSwapHandler.d.ts +1 -2
  21. package/dist/swaps/FromBtcLnBaseSwapHandler.js +13 -24
  22. package/dist/swaps/ISwapPrice.d.ts +4 -5
  23. package/dist/swaps/ISwapPrice.js +8 -30
  24. package/dist/swaps/SwapHandler.d.ts +26 -15
  25. package/dist/swaps/SwapHandler.js +107 -66
  26. package/dist/swaps/SwapHandlerSwap.d.ts +22 -11
  27. package/dist/swaps/SwapHandlerSwap.js +32 -8
  28. package/dist/swaps/ToBtcBaseSwap.d.ts +16 -17
  29. package/dist/swaps/ToBtcBaseSwap.js +4 -4
  30. package/dist/swaps/ToBtcBaseSwapHandler.d.ts +17 -17
  31. package/dist/swaps/ToBtcBaseSwapHandler.js +140 -155
  32. package/dist/swaps/frombtc_abstract/FromBtcAbs.d.ts +8 -15
  33. package/dist/swaps/frombtc_abstract/FromBtcAbs.js +131 -196
  34. package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.d.ts +3 -5
  35. package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.js +4 -4
  36. package/dist/swaps/frombtc_trusted/FromBtcTrusted.d.ts +10 -8
  37. package/dist/swaps/frombtc_trusted/FromBtcTrusted.js +330 -362
  38. package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.d.ts +10 -10
  39. package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.js +7 -4
  40. package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.d.ts +8 -8
  41. package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.js +330 -400
  42. package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +10 -4
  43. package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.js +36 -6
  44. package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.d.ts +8 -7
  45. package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.js +251 -273
  46. package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +5 -5
  47. package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.js +7 -5
  48. package/dist/swaps/tobtc_abstract/ToBtcAbs.d.ts +13 -12
  49. package/dist/swaps/tobtc_abstract/ToBtcAbs.js +261 -323
  50. package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.d.ts +4 -4
  51. package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.js +7 -6
  52. package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.d.ts +22 -21
  53. package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.js +398 -453
  54. package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +3 -2
  55. package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.js +10 -6
  56. package/dist/utils/Utils.d.ts +2 -3
  57. package/dist/utils/Utils.js +4 -14
  58. package/dist/utils/paramcoders/LegacyParamEncoder.js +3 -14
  59. package/dist/utils/paramcoders/ParamDecoder.js +53 -65
  60. package/dist/utils/paramcoders/SchemaVerifier.d.ts +4 -5
  61. package/dist/utils/paramcoders/SchemaVerifier.js +8 -9
  62. package/dist/utils/paramcoders/server/ServerParamDecoder.js +1 -1
  63. package/dist/utils/paramcoders/server/ServerParamEncoder.js +3 -14
  64. package/dist/wallets/IBitcoinWallet.d.ts +4 -5
  65. package/dist/wallets/ILightningWallet.d.ts +20 -21
  66. package/dist/wallets/ILightningWallet.js +1 -1
  67. package/package.json +4 -6
  68. package/src/plugins/IPlugin.ts +28 -29
  69. package/src/plugins/PluginManager.ts +21 -22
  70. package/src/prices/BinanceSwapPrice.ts +3 -4
  71. package/src/prices/CoinGeckoSwapPrice.ts +4 -5
  72. package/src/prices/OKXSwapPrice.ts +3 -4
  73. package/src/storage/IIntermediaryStorage.ts +4 -5
  74. package/src/storagemanager/IntermediaryStorageManager.ts +17 -9
  75. package/src/swaps/FromBtcBaseSwap.ts +9 -10
  76. package/src/swaps/FromBtcBaseSwapHandler.ts +133 -91
  77. package/src/swaps/FromBtcLnBaseSwapHandler.ts +2 -3
  78. package/src/swaps/ISwapPrice.ts +10 -20
  79. package/src/swaps/SwapHandler.ts +101 -35
  80. package/src/swaps/SwapHandlerSwap.ts +42 -17
  81. package/src/swaps/ToBtcBaseSwap.ts +20 -18
  82. package/src/swaps/ToBtcBaseSwapHandler.ts +33 -33
  83. package/src/swaps/frombtc_abstract/FromBtcAbs.ts +64 -97
  84. package/src/swaps/frombtc_abstract/FromBtcSwapAbs.ts +7 -8
  85. package/src/swaps/frombtc_trusted/FromBtcTrusted.ts +56 -55
  86. package/src/swaps/frombtc_trusted/FromBtcTrustedSwap.ts +28 -21
  87. package/src/swaps/frombtcln_abstract/FromBtcLnAbs.ts +81 -116
  88. package/src/swaps/frombtcln_abstract/FromBtcLnSwapAbs.ts +73 -9
  89. package/src/swaps/frombtcln_trusted/FromBtcLnTrusted.ts +39 -36
  90. package/src/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.ts +26 -12
  91. package/src/swaps/tobtc_abstract/ToBtcAbs.ts +82 -98
  92. package/src/swaps/tobtc_abstract/ToBtcSwapAbs.ts +25 -20
  93. package/src/swaps/tobtcln_abstract/ToBtcLnAbs.ts +106 -118
  94. package/src/swaps/tobtcln_abstract/ToBtcLnSwapAbs.ts +17 -9
  95. package/src/utils/Utils.ts +3 -4
  96. package/src/utils/paramcoders/ParamDecoder.ts +5 -5
  97. package/src/utils/paramcoders/SchemaVerifier.ts +10 -11
  98. package/src/utils/paramcoders/server/ServerParamDecoder.ts +1 -1
  99. package/src/wallets/IBitcoinWallet.ts +4 -5
  100. package/src/wallets/ILightningWallet.ts +21 -22
@@ -1,37 +1,36 @@
1
1
  import { SwapHandlerSwap } from "./SwapHandlerSwap";
2
2
  import { SwapData } from "@atomiqlabs/base";
3
- import * as BN from "bn.js";
4
3
  export declare abstract class ToBtcBaseSwap<T extends SwapData = SwapData, S = any> extends SwapHandlerSwap<T, S> {
5
- amount: BN;
6
- quotedNetworkFee: BN;
7
- readonly quotedNetworkFeeInToken: BN;
8
- realNetworkFee: BN;
9
- realNetworkFeeInToken: BN;
10
- protected constructor(chainIdentifier: string, amount: BN, swapFee: BN, swapFeeInToken: BN, quotedNetworkFee: BN, quotedNetworkFeeInToken: BN);
4
+ amount: bigint;
5
+ quotedNetworkFee: bigint;
6
+ readonly quotedNetworkFeeInToken: bigint;
7
+ realNetworkFee: bigint;
8
+ realNetworkFeeInToken: bigint;
9
+ protected constructor(chainIdentifier: string, amount: bigint, swapFee: bigint, swapFeeInToken: bigint, quotedNetworkFee: bigint, quotedNetworkFeeInToken: bigint);
11
10
  protected constructor(obj: any);
12
11
  serialize(): any;
13
- setRealNetworkFee(networkFeeInBtc: BN): void;
14
- getInputAmount(): BN;
15
- getTotalInputAmount(): BN;
12
+ setRealNetworkFee(networkFeeInBtc: bigint): void;
13
+ getInputAmount(): bigint;
14
+ getTotalInputAmount(): bigint;
16
15
  getSwapFee(): {
17
- inInputToken: BN;
18
- inOutputToken: BN;
16
+ inInputToken: bigint;
17
+ inOutputToken: bigint;
19
18
  };
20
19
  /**
21
20
  * Returns quoted (expected) network fee, denominated in input & output tokens (the fee is paid only once, it is
22
21
  * just represented here in both denomination for ease of use)
23
22
  */
24
23
  getQuotedNetworkFee(): {
25
- inInputToken: BN;
26
- inOutputToken: BN;
24
+ inInputToken: bigint;
25
+ inOutputToken: bigint;
27
26
  };
28
27
  /**
29
28
  * Returns real network fee paid for the swap, denominated in input & output tokens (the fee is paid only once, it is
30
29
  * just represented here in both denomination for ease of use)
31
30
  */
32
31
  getRealNetworkFee(): {
33
- inInputToken: BN;
34
- inOutputToken: BN;
32
+ inInputToken: bigint;
33
+ inOutputToken: bigint;
35
34
  };
36
- getOutputAmount(): BN;
35
+ getOutputAmount(): bigint;
37
36
  }
@@ -2,11 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ToBtcBaseSwap = void 0;
4
4
  const SwapHandlerSwap_1 = require("./SwapHandlerSwap");
5
- const BN = require("bn.js");
6
5
  const Utils_1 = require("../utils/Utils");
7
6
  class ToBtcBaseSwap extends SwapHandlerSwap_1.SwapHandlerSwap {
8
7
  constructor(obj, amount, swapFee, swapFeeInToken, quotedNetworkFee, quotedNetworkFeeInToken) {
9
- if (typeof (obj) === "string" && BN.isBN(amount) && BN.isBN(swapFee) && BN.isBN(swapFeeInToken) && BN.isBN(quotedNetworkFee) && BN.isBN(quotedNetworkFeeInToken)) {
8
+ if (typeof (obj) === "string" && typeof (amount) === "bigint" && typeof (swapFee) === "bigint" && typeof (swapFeeInToken) === "bigint" &&
9
+ typeof (quotedNetworkFee) === "bigint" && typeof (quotedNetworkFeeInToken) === "bigint") {
10
10
  super(obj, swapFee, swapFeeInToken);
11
11
  this.amount = amount;
12
12
  this.quotedNetworkFee = quotedNetworkFee;
@@ -34,11 +34,11 @@ class ToBtcBaseSwap extends SwapHandlerSwap_1.SwapHandlerSwap {
34
34
  setRealNetworkFee(networkFeeInBtc) {
35
35
  this.realNetworkFee = networkFeeInBtc;
36
36
  if (this.quotedNetworkFee != null && this.quotedNetworkFeeInToken != null) {
37
- this.realNetworkFeeInToken = this.realNetworkFee.mul(this.quotedNetworkFeeInToken).div(this.quotedNetworkFee);
37
+ this.realNetworkFeeInToken = this.realNetworkFee * this.quotedNetworkFeeInToken / this.quotedNetworkFee;
38
38
  }
39
39
  }
40
40
  getInputAmount() {
41
- return this.data.getAmount().sub(this.getSwapFee().inInputToken).sub(this.getQuotedNetworkFee().inInputToken);
41
+ return this.data.getAmount() - this.getSwapFee().inInputToken - this.getQuotedNetworkFee().inInputToken;
42
42
  }
43
43
  getTotalInputAmount() {
44
44
  return this.data.getAmount();
@@ -1,14 +1,14 @@
1
1
  import { RequestData, SwapBaseConfig, SwapHandler } from "./SwapHandler";
2
2
  import { SwapHandlerSwap } from "./SwapHandlerSwap";
3
3
  import { SwapData } from "@atomiqlabs/base";
4
- import * as BN from "bn.js";
5
4
  import { ServerParamEncoder } from "../utils/paramcoders/server/ServerParamEncoder";
6
5
  import { IParamReader } from "../utils/paramcoders/IParamReader";
7
6
  import { ToBtcLnRequestType } from "./tobtcln_abstract/ToBtcLnAbs";
8
7
  import { ToBtcRequestType } from "./tobtc_abstract/ToBtcAbs";
9
8
  import { Request } from "express";
10
9
  export type ToBtcBaseConfig = SwapBaseConfig & {
11
- gracePeriod: BN;
10
+ gracePeriod: bigint;
11
+ refundAuthorizationTimeout: number;
12
12
  };
13
13
  export declare abstract class ToBtcBaseSwapHandler<V extends SwapHandlerSwap<SwapData, S>, S> extends SwapHandler<V, S> {
14
14
  readonly pdaExistsForToken: {
@@ -28,10 +28,10 @@ export declare abstract class ToBtcBaseSwapHandler<V extends SwapHandlerSwap<Swa
28
28
  */
29
29
  protected preCheckAmounts(request: RequestData<ToBtcLnRequestType | ToBtcRequestType>, requestedAmount: {
30
30
  input: boolean;
31
- amount: BN;
31
+ amount: bigint;
32
32
  }, useToken: string): Promise<{
33
- baseFee: BN;
34
- feePPM: BN;
33
+ baseFee: bigint;
34
+ feePPM: bigint;
35
35
  }>;
36
36
  /**
37
37
  * Checks minimums/maximums, calculates network fee (based on the callback passed), swap fee & total amount
@@ -47,21 +47,21 @@ export declare abstract class ToBtcBaseSwapHandler<V extends SwapHandlerSwap<Swa
47
47
  * or if we don't have enough funds (getNetworkFee callback throws)
48
48
  */
49
49
  protected checkToBtcAmount<T extends {
50
- networkFee: BN;
50
+ networkFee: bigint;
51
51
  }>(request: RequestData<ToBtcLnRequestType | ToBtcRequestType>, requestedAmount: {
52
52
  input: boolean;
53
- amount: BN;
53
+ amount: bigint;
54
54
  }, fees: {
55
- baseFee: BN;
56
- feePPM: BN;
57
- }, useToken: string, getNetworkFee: (amount: BN) => Promise<T>, signal: AbortSignal, pricePrefetchPromise?: Promise<BN>): Promise<{
58
- amountBD: BN;
55
+ baseFee: bigint;
56
+ feePPM: bigint;
57
+ }, useToken: string, getNetworkFee: (amount: bigint) => Promise<T>, signal: AbortSignal, pricePrefetchPromise?: Promise<bigint>): Promise<{
58
+ amountBD: bigint;
59
59
  networkFeeData: T;
60
- swapFee: BN;
61
- swapFeeInToken: BN;
62
- networkFee: BN;
63
- networkFeeInToken: BN;
64
- totalInToken: BN;
60
+ swapFee: bigint;
61
+ swapFeeInToken: bigint;
62
+ networkFee: bigint;
63
+ networkFeeInToken: bigint;
64
+ totalInToken: bigint;
65
65
  }>;
66
66
  /**
67
67
  * Starts pre-fetches for swap pricing & signature data
@@ -72,7 +72,7 @@ export declare abstract class ToBtcBaseSwapHandler<V extends SwapHandlerSwap<Swa
72
72
  * @param abortController
73
73
  */
74
74
  protected getToBtcPrefetches(chainIdentifier: string, token: string, responseStream: ServerParamEncoder, abortController: AbortController): {
75
- pricePrefetchPromise?: Promise<BN>;
75
+ pricePrefetchPromise?: Promise<bigint>;
76
76
  signDataPrefetchPromise?: Promise<any>;
77
77
  };
78
78
  /**
@@ -1,17 +1,7 @@
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.ToBtcBaseSwapHandler = 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");
@@ -20,26 +10,24 @@ class ToBtcBaseSwapHandler extends SwapHandler_1.SwapHandler {
20
10
  super(...arguments);
21
11
  this.pdaExistsForToken = {};
22
12
  }
23
- checkVaultInitialized(chainIdentifier, token) {
24
- return __awaiter(this, void 0, void 0, function* () {
25
- if (!this.pdaExistsForToken[chainIdentifier] || !this.pdaExistsForToken[chainIdentifier][token]) {
26
- this.logger.debug("checkVaultInitialized(): checking vault exists for chain: " + chainIdentifier + " token: " + token);
27
- const { swapContract, signer } = this.getChain(chainIdentifier);
28
- const reputation = yield swapContract.getIntermediaryReputation(signer.getAddress(), token);
29
- this.logger.debug("checkVaultInitialized(): vault state, chain: " + chainIdentifier + " token: " + token + " exists: " + (reputation != null));
30
- if (reputation != null) {
31
- if (this.pdaExistsForToken[chainIdentifier] == null)
32
- this.pdaExistsForToken[chainIdentifier] = {};
33
- this.pdaExistsForToken[chainIdentifier][token] = true;
34
- }
35
- else {
36
- throw {
37
- code: 20201,
38
- msg: "Token not supported!"
39
- };
40
- }
13
+ async checkVaultInitialized(chainIdentifier, token) {
14
+ if (!this.pdaExistsForToken[chainIdentifier] || !this.pdaExistsForToken[chainIdentifier][token]) {
15
+ this.logger.debug("checkVaultInitialized(): checking vault exists for chain: " + chainIdentifier + " token: " + token);
16
+ const { swapContract, signer } = this.getChain(chainIdentifier);
17
+ const reputation = await swapContract.getIntermediaryReputation(signer.getAddress(), token);
18
+ this.logger.debug("checkVaultInitialized(): vault state, chain: " + chainIdentifier + " token: " + token + " exists: " + (reputation != null));
19
+ if (reputation != null) {
20
+ if (this.pdaExistsForToken[chainIdentifier] == null)
21
+ this.pdaExistsForToken[chainIdentifier] = {};
22
+ this.pdaExistsForToken[chainIdentifier][token] = true;
41
23
  }
42
- });
24
+ else {
25
+ throw {
26
+ code: 20201,
27
+ msg: "Token not supported!"
28
+ };
29
+ }
30
+ }
43
31
  }
44
32
  /**
45
33
  * Checks minimums/maximums, calculates the fee & total amount
@@ -49,26 +37,24 @@ class ToBtcBaseSwapHandler extends SwapHandler_1.SwapHandler {
49
37
  * @param useToken
50
38
  * @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds
51
39
  */
52
- preCheckAmounts(request, requestedAmount, useToken) {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- const res = yield PluginManager_1.PluginManager.onHandlePreToBtcQuote(request, requestedAmount, request.chainIdentifier, useToken, { minInBtc: this.config.min, maxInBtc: this.config.max }, { baseFeeInBtc: this.config.baseFee, feePPM: this.config.feePPM });
55
- if (res != null) {
56
- this.handlePluginErrorResponses(res);
57
- if ((0, IPlugin_1.isQuoteSetFees)(res)) {
58
- return {
59
- baseFee: res.baseFee || this.config.baseFee,
60
- feePPM: res.feePPM || this.config.feePPM
61
- };
62
- }
63
- }
64
- if (!requestedAmount.input) {
65
- this.checkBtcAmountInBounds(requestedAmount.amount);
40
+ async preCheckAmounts(request, requestedAmount, useToken) {
41
+ const res = await PluginManager_1.PluginManager.onHandlePreToBtcQuote(request, requestedAmount, request.chainIdentifier, useToken, { minInBtc: this.config.min, maxInBtc: this.config.max }, { baseFeeInBtc: this.config.baseFee, feePPM: this.config.feePPM });
42
+ if (res != null) {
43
+ this.handlePluginErrorResponses(res);
44
+ if ((0, IPlugin_1.isQuoteSetFees)(res)) {
45
+ return {
46
+ baseFee: res.baseFee || this.config.baseFee,
47
+ feePPM: res.feePPM || this.config.feePPM
48
+ };
66
49
  }
67
- return {
68
- baseFee: this.config.baseFee,
69
- feePPM: this.config.feePPM
70
- };
71
- });
50
+ }
51
+ if (!requestedAmount.input) {
52
+ this.checkBtcAmountInBounds(requestedAmount.amount);
53
+ }
54
+ return {
55
+ baseFee: this.config.baseFee,
56
+ feePPM: this.config.feePPM
57
+ };
72
58
  }
73
59
  /**
74
60
  * Checks minimums/maximums, calculates network fee (based on the callback passed), swap fee & total amount
@@ -83,104 +69,102 @@ class ToBtcBaseSwapHandler extends SwapHandler_1.SwapHandler {
83
69
  * @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds,
84
70
  * or if we don't have enough funds (getNetworkFee callback throws)
85
71
  */
86
- checkToBtcAmount(request, requestedAmount, fees, useToken, getNetworkFee, signal, pricePrefetchPromise) {
87
- return __awaiter(this, void 0, void 0, function* () {
88
- const chainIdentifier = request.chainIdentifier;
89
- const res = yield PluginManager_1.PluginManager.onHandlePostToBtcQuote(request, requestedAmount, request.chainIdentifier, useToken, { minInBtc: this.config.min, maxInBtc: this.config.max }, { baseFeeInBtc: fees.baseFee, feePPM: fees.feePPM, networkFeeGetter: getNetworkFee }, pricePrefetchPromise);
90
- signal.throwIfAborted();
91
- if (res != null) {
92
- this.handlePluginErrorResponses(res);
93
- if ((0, IPlugin_1.isQuoteSetFees)(res)) {
94
- if (res.baseFee != null)
95
- fees.baseFee = res.baseFee;
96
- if (res.feePPM != null)
97
- fees.feePPM = res.feePPM;
98
- }
99
- if ((0, IPlugin_1.isToBtcPluginQuote)(res)) {
100
- if (requestedAmount.input) {
101
- return {
102
- amountBD: res.amount.amount,
103
- swapFee: res.swapFee.inOutputTokens,
104
- swapFeeInToken: res.swapFee.inInputTokens,
105
- networkFee: res.networkFee.inOutputTokens,
106
- networkFeeInToken: res.networkFee.inInputTokens,
107
- networkFeeData: res.networkFeeData,
108
- totalInToken: requestedAmount.amount
109
- };
110
- }
111
- else {
112
- return {
113
- amountBD: requestedAmount.amount,
114
- swapFee: res.swapFee.inOutputTokens,
115
- swapFeeInToken: res.swapFee.inInputTokens,
116
- networkFee: res.networkFee.inOutputTokens,
117
- networkFeeInToken: res.networkFee.inInputTokens,
118
- networkFeeData: res.networkFeeData,
119
- totalInToken: res.amount.amount.add(res.swapFee.inInputTokens).add(res.networkFee.inInputTokens)
120
- };
121
- }
122
- }
72
+ async checkToBtcAmount(request, requestedAmount, fees, useToken, getNetworkFee, signal, pricePrefetchPromise) {
73
+ const chainIdentifier = request.chainIdentifier;
74
+ const res = await PluginManager_1.PluginManager.onHandlePostToBtcQuote(request, requestedAmount, request.chainIdentifier, useToken, { minInBtc: this.config.min, maxInBtc: this.config.max }, { baseFeeInBtc: fees.baseFee, feePPM: fees.feePPM, networkFeeGetter: getNetworkFee }, pricePrefetchPromise);
75
+ signal.throwIfAborted();
76
+ if (res != null) {
77
+ this.handlePluginErrorResponses(res);
78
+ if ((0, IPlugin_1.isQuoteSetFees)(res)) {
79
+ if (res.baseFee != null)
80
+ fees.baseFee = res.baseFee;
81
+ if (res.feePPM != null)
82
+ fees.feePPM = res.feePPM;
123
83
  }
124
- let amountBD;
125
- let tooLow = false;
126
- if (requestedAmount.input) {
127
- amountBD = yield this.swapPricing.getToBtcSwapAmount(requestedAmount.amount, useToken, chainIdentifier, null, pricePrefetchPromise);
128
- signal.throwIfAborted();
129
- //Decrease by base fee
130
- amountBD = amountBD.sub(fees.baseFee);
131
- //If it's already smaller than minimum, set it to minimum so we can calculate the network fee
132
- if (amountBD.lt(this.config.min)) {
133
- amountBD = this.config.min;
134
- tooLow = true;
84
+ if ((0, IPlugin_1.isToBtcPluginQuote)(res)) {
85
+ if (requestedAmount.input) {
86
+ return {
87
+ amountBD: res.amount.amount,
88
+ swapFee: res.swapFee.inOutputTokens,
89
+ swapFeeInToken: res.swapFee.inInputTokens,
90
+ networkFee: res.networkFee.inOutputTokens,
91
+ networkFeeInToken: res.networkFee.inInputTokens,
92
+ networkFeeData: res.networkFeeData,
93
+ totalInToken: requestedAmount.amount
94
+ };
135
95
  }
136
- }
137
- else {
138
- amountBD = requestedAmount.amount;
139
- this.checkBtcAmountInBounds(amountBD);
140
- }
141
- const resp = yield getNetworkFee(amountBD);
142
- this.logger.debug("checkToBtcAmount(): network fee calculated, amount: " + amountBD.toString(10) + " fee: " + resp.networkFee.toString(10));
143
- signal.throwIfAborted();
144
- if (requestedAmount.input) {
145
- //Decrease by network fee
146
- amountBD = amountBD.sub(resp.networkFee);
147
- //Decrease by percentage fee
148
- amountBD = amountBD.mul(new BN(1000000)).div(fees.feePPM.add(new BN(1000000)));
149
- const tooHigh = amountBD.gt(this.config.max.mul(new BN(105)).div(new BN(100)));
150
- tooLow || (tooLow = amountBD.lt(this.config.min.mul(new BN(95)).div(new BN(100))));
151
- if (tooLow || tooHigh) {
152
- //Compute min/max
153
- let adjustedMin = this.config.min.mul(fees.feePPM.add(new BN(1000000))).div(new BN(1000000));
154
- let adjustedMax = this.config.max.mul(fees.feePPM.add(new BN(1000000))).div(new BN(1000000));
155
- adjustedMin = adjustedMin.add(fees.baseFee).add(resp.networkFee);
156
- adjustedMax = adjustedMax.add(fees.baseFee).add(resp.networkFee);
157
- const minIn = yield this.swapPricing.getFromBtcSwapAmount(adjustedMin, useToken, chainIdentifier, null, pricePrefetchPromise);
158
- const maxIn = yield this.swapPricing.getFromBtcSwapAmount(adjustedMax, useToken, chainIdentifier, null, pricePrefetchPromise);
159
- throw {
160
- code: tooLow ? 20003 : 2004,
161
- msg: tooLow ? "Amount too low!" : "Amount too high!",
162
- data: {
163
- min: minIn.toString(10),
164
- max: maxIn.toString(10)
165
- }
96
+ else {
97
+ return {
98
+ amountBD: requestedAmount.amount,
99
+ swapFee: res.swapFee.inOutputTokens,
100
+ swapFeeInToken: res.swapFee.inInputTokens,
101
+ networkFee: res.networkFee.inOutputTokens,
102
+ networkFeeInToken: res.networkFee.inInputTokens,
103
+ networkFeeData: res.networkFeeData,
104
+ totalInToken: res.amount.amount + res.swapFee.inInputTokens + res.networkFee.inInputTokens
166
105
  };
167
106
  }
168
107
  }
169
- const swapFee = fees.baseFee.add(amountBD.mul(fees.feePPM).div(new BN(1000000)));
170
- const networkFeeInToken = yield this.swapPricing.getFromBtcSwapAmount(resp.networkFee, useToken, chainIdentifier, true, pricePrefetchPromise);
171
- const swapFeeInToken = yield this.swapPricing.getFromBtcSwapAmount(swapFee, useToken, chainIdentifier, true, pricePrefetchPromise);
108
+ }
109
+ let amountBD;
110
+ let tooLow = false;
111
+ if (requestedAmount.input) {
112
+ amountBD = await this.swapPricing.getToBtcSwapAmount(requestedAmount.amount, useToken, chainIdentifier, null, pricePrefetchPromise);
172
113
  signal.throwIfAborted();
173
- let total;
174
- if (requestedAmount.input) {
175
- total = requestedAmount.amount;
114
+ //Decrease by base fee
115
+ amountBD = amountBD - fees.baseFee;
116
+ //If it's already smaller than minimum, set it to minimum so we can calculate the network fee
117
+ if (amountBD < this.config.min) {
118
+ amountBD = this.config.min;
119
+ tooLow = true;
176
120
  }
177
- else {
178
- const amountInToken = yield this.swapPricing.getFromBtcSwapAmount(requestedAmount.amount, useToken, chainIdentifier, true, pricePrefetchPromise);
179
- signal.throwIfAborted();
180
- total = amountInToken.add(swapFeeInToken).add(networkFeeInToken);
121
+ }
122
+ else {
123
+ amountBD = requestedAmount.amount;
124
+ this.checkBtcAmountInBounds(amountBD);
125
+ }
126
+ const resp = await getNetworkFee(amountBD);
127
+ this.logger.debug("checkToBtcAmount(): network fee calculated, amount: " + amountBD.toString(10) + " fee: " + resp.networkFee.toString(10));
128
+ signal.throwIfAborted();
129
+ if (requestedAmount.input) {
130
+ //Decrease by network fee
131
+ amountBD = amountBD - resp.networkFee;
132
+ //Decrease by percentage fee
133
+ amountBD = amountBD * 1000000n / (fees.feePPM + 1000000n);
134
+ const tooHigh = amountBD > (this.config.max * 105n / 100n);
135
+ tooLow || (tooLow = amountBD < (this.config.min * 95n / 100n));
136
+ if (tooLow || tooHigh) {
137
+ //Compute min/max
138
+ let adjustedMin = this.config.min * (fees.feePPM + 1000000n) / 1000000n;
139
+ let adjustedMax = this.config.max * (fees.feePPM + 1000000n) / 1000000n;
140
+ adjustedMin = adjustedMin + fees.baseFee + resp.networkFee;
141
+ adjustedMax = adjustedMax + fees.baseFee + resp.networkFee;
142
+ const minIn = await this.swapPricing.getFromBtcSwapAmount(adjustedMin, useToken, chainIdentifier, null, pricePrefetchPromise);
143
+ const maxIn = await this.swapPricing.getFromBtcSwapAmount(adjustedMax, useToken, chainIdentifier, null, pricePrefetchPromise);
144
+ throw {
145
+ code: tooLow ? 20003 : 2004,
146
+ msg: tooLow ? "Amount too low!" : "Amount too high!",
147
+ data: {
148
+ min: minIn.toString(10),
149
+ max: maxIn.toString(10)
150
+ }
151
+ };
181
152
  }
182
- return { amountBD, networkFeeData: resp, swapFee, swapFeeInToken, networkFee: resp.networkFee, networkFeeInToken, totalInToken: total };
183
- });
153
+ }
154
+ const swapFee = fees.baseFee + (amountBD * fees.feePPM / 1000000n);
155
+ const networkFeeInToken = await this.swapPricing.getFromBtcSwapAmount(resp.networkFee, useToken, chainIdentifier, true, pricePrefetchPromise);
156
+ const swapFeeInToken = await this.swapPricing.getFromBtcSwapAmount(swapFee, useToken, chainIdentifier, true, pricePrefetchPromise);
157
+ signal.throwIfAborted();
158
+ let total;
159
+ if (requestedAmount.input) {
160
+ total = requestedAmount.amount;
161
+ }
162
+ else {
163
+ const amountInToken = await this.swapPricing.getFromBtcSwapAmount(requestedAmount.amount, useToken, chainIdentifier, true, pricePrefetchPromise);
164
+ signal.throwIfAborted();
165
+ total = amountInToken + swapFeeInToken + networkFeeInToken;
166
+ }
167
+ return { amountBD, networkFeeData: resp, swapFee, swapFeeInToken, networkFee: resp.networkFee, networkFeeInToken, totalInToken: total };
184
168
  }
185
169
  /**
186
170
  * Starts pre-fetches for swap pricing & signature data
@@ -211,23 +195,24 @@ class ToBtcBaseSwapHandler extends SwapHandler_1.SwapHandler {
211
195
  * @param abortSignal
212
196
  * @param signDataPrefetchPromise
213
197
  */
214
- getToBtcSignatureData(chainIdentifier, swapObject, req, abortSignal, signDataPrefetchPromise) {
215
- return __awaiter(this, void 0, void 0, function* () {
216
- const prefetchedSignData = signDataPrefetchPromise != null ? yield signDataPrefetchPromise : null;
217
- if (prefetchedSignData != null)
218
- this.logger.debug("getToBtcSignatureData(): pre-fetched signature data: ", prefetchedSignData);
219
- abortSignal.throwIfAborted();
220
- const feeRateObj = yield req.paramReader.getParams({
221
- feeRate: SchemaVerifier_1.FieldTypeEnum.String
222
- }).catch(() => null);
223
- abortSignal.throwIfAborted();
224
- const feeRate = (feeRateObj === null || feeRateObj === void 0 ? void 0 : feeRateObj.feeRate) != null && typeof (feeRateObj.feeRate) === "string" ? feeRateObj.feeRate : null;
225
- this.logger.debug("getToBtcSignatureData(): using fee rate from client: ", feeRate);
226
- const { swapContract, signer } = this.getChain(chainIdentifier);
227
- const sigData = yield swapContract.getInitSignature(signer, swapObject, this.config.authorizationTimeout, prefetchedSignData, feeRate);
228
- abortSignal.throwIfAborted();
229
- return Object.assign(Object.assign({}, sigData), { feeRate });
230
- });
198
+ async getToBtcSignatureData(chainIdentifier, swapObject, req, abortSignal, signDataPrefetchPromise) {
199
+ const prefetchedSignData = signDataPrefetchPromise != null ? await signDataPrefetchPromise : null;
200
+ if (prefetchedSignData != null)
201
+ this.logger.debug("getToBtcSignatureData(): pre-fetched signature data: ", prefetchedSignData);
202
+ abortSignal.throwIfAborted();
203
+ const feeRateObj = await req.paramReader.getParams({
204
+ feeRate: SchemaVerifier_1.FieldTypeEnum.String
205
+ }).catch(() => null);
206
+ abortSignal.throwIfAborted();
207
+ const feeRate = feeRateObj?.feeRate != null && typeof (feeRateObj.feeRate) === "string" ? feeRateObj.feeRate : null;
208
+ this.logger.debug("getToBtcSignatureData(): using fee rate from client: ", feeRate);
209
+ const { swapContract, signer } = this.getChain(chainIdentifier);
210
+ const sigData = await swapContract.getInitSignature(signer, swapObject, this.getInitAuthorizationTimeout(chainIdentifier), prefetchedSignData, feeRate);
211
+ abortSignal.throwIfAborted();
212
+ return {
213
+ ...sigData,
214
+ feeRate
215
+ };
231
216
  }
232
217
  }
233
218
  exports.ToBtcBaseSwapHandler = ToBtcBaseSwapHandler;
@@ -1,9 +1,8 @@
1
- import * as BN from "bn.js";
2
1
  import { Express } from "express";
3
2
  import { FromBtcSwapAbs, FromBtcSwapState } from "./FromBtcSwapAbs";
4
3
  import { MultichainData, SwapHandlerType } from "../SwapHandler";
5
4
  import { ISwapPrice } from "../ISwapPrice";
6
- import { ClaimEvent, InitializeEvent, RefundEvent, SwapData } from "@atomiqlabs/base";
5
+ import { ChainSwapType, ClaimEvent, InitializeEvent, RefundEvent, SwapData } from "@atomiqlabs/base";
7
6
  import { IIntermediaryStorage } from "../../storage/IIntermediaryStorage";
8
7
  import { FromBtcBaseConfig, FromBtcBaseSwapHandler } from "../FromBtcBaseSwapHandler";
9
8
  import { IBitcoinWallet } from "../../wallets/IBitcoinWallet";
@@ -13,9 +12,9 @@ export type FromBtcConfig = FromBtcBaseConfig & {
13
12
  };
14
13
  export type FromBtcRequestType = {
15
14
  address: string;
16
- amount: BN;
15
+ amount: bigint;
17
16
  token: string;
18
- sequence: BN;
17
+ sequence: bigint;
19
18
  exactOut?: boolean;
20
19
  };
21
20
  /**
@@ -23,18 +22,12 @@ export type FromBtcRequestType = {
23
22
  */
24
23
  export declare class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSwapState> {
25
24
  readonly type = SwapHandlerType.FROM_BTC;
25
+ readonly swapType = ChainSwapType.CHAIN;
26
26
  readonly config: FromBtcConfig & {
27
- swapTsCsvDelta: BN;
27
+ swapTsCsvDelta: bigint;
28
28
  };
29
29
  readonly bitcoin: IBitcoinWallet;
30
30
  constructor(storageDirectory: IIntermediaryStorage<FromBtcSwapAbs>, path: string, chains: MultichainData, bitcoin: IBitcoinWallet, swapPricing: ISwapPrice, config: FromBtcConfig);
31
- /**
32
- * Returns the TXO hash of the specific address and amount - sha256(u64le(amount) + outputScript(address))
33
- *
34
- * @param address
35
- * @param amount
36
- */
37
- private getTxoHash;
38
31
  /**
39
32
  * Returns the payment hash of the swap, takes swap nonce into account. Payment hash is chain-specific.
40
33
  *
@@ -62,9 +55,9 @@ export declare class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, F
62
55
  * @protected
63
56
  */
64
57
  protected refundSwaps(refundSwaps: FromBtcSwapAbs[]): Promise<void>;
65
- protected processInitializeEvent(chainIdentifier: string, event: InitializeEvent<SwapData>): Promise<void>;
66
- protected processClaimEvent(chainIdentifier: string, event: ClaimEvent<SwapData>): Promise<void>;
67
- protected processRefundEvent(chainIdentifier: string, event: RefundEvent<SwapData>): Promise<void>;
58
+ protected processInitializeEvent(chainIdentifier: string, savedSwap: FromBtcSwapAbs, event: InitializeEvent<SwapData>): Promise<void>;
59
+ protected processClaimEvent(chainIdentifier: string, savedSwap: FromBtcSwapAbs, event: ClaimEvent<SwapData>): Promise<void>;
60
+ protected processRefundEvent(chainIdentifier: string, savedSwap: FromBtcSwapAbs, event: RefundEvent<SwapData>): Promise<void>;
68
61
  /**
69
62
  * Calculates the requested claimer bounty, based on client's request
70
63
  *