@atomiqlabs/lp-lib 15.0.13 → 16.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/info/InfoHandler.js +0 -3
- package/dist/plugins/IPlugin.d.ts +2 -2
- package/dist/plugins/PluginManager.d.ts +2 -2
- package/dist/storagemanager/IntermediaryStorageManager.d.ts +1 -0
- package/dist/storagemanager/IntermediaryStorageManager.js +9 -2
- package/dist/storagemanager/StorageManager.d.ts +1 -0
- package/dist/storagemanager/StorageManager.js +9 -2
- package/dist/swaps/SwapHandler.d.ts +4 -10
- package/dist/swaps/SwapHandler.js +4 -13
- package/dist/swaps/assertions/FromBtcAmountAssertions.d.ts +2 -2
- package/dist/swaps/assertions/FromBtcAmountAssertions.js +13 -5
- package/dist/swaps/escrow/EscrowHandlerSwap.js +2 -2
- package/dist/swaps/escrow/FromBtcBaseSwapHandler.d.ts +2 -1
- package/dist/swaps/escrow/FromBtcBaseSwapHandler.js +8 -5
- package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.js +2 -2
- package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.js +11 -13
- package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +1 -0
- package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.js +3 -0
- package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.d.ts +111 -0
- package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.js +682 -0
- package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.d.ts +55 -0
- package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.js +120 -0
- package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.d.ts +0 -2
- package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.js +44 -27
- package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.js +12 -10
- package/dist/swaps/spv_vault_swap/SpvVault.d.ts +2 -3
- package/dist/swaps/spv_vault_swap/SpvVault.js +2 -2
- package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.js +10 -8
- package/dist/swaps/spv_vault_swap/SpvVaults.d.ts +4 -9
- package/dist/swaps/spv_vault_swap/SpvVaults.js +114 -80
- package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.js +3 -3
- package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.js +14 -17
- package/dist/utils/Utils.d.ts +7 -7
- package/dist/utils/Utils.js +12 -11
- package/dist/utils/paramcoders/server/ServerParamDecoder.js +8 -6
- package/dist/wallets/IBitcoinWallet.d.ts +7 -0
- package/package.json +3 -3
- package/src/index.ts +2 -0
- package/src/info/InfoHandler.ts +0 -6
- package/src/plugins/IPlugin.ts +2 -2
- package/src/plugins/PluginManager.ts +2 -2
- package/src/storagemanager/IntermediaryStorageManager.ts +11 -2
- package/src/storagemanager/StorageManager.ts +12 -2
- package/src/swaps/SwapHandler.ts +6 -17
- package/src/swaps/assertions/FromBtcAmountAssertions.ts +16 -8
- package/src/swaps/escrow/EscrowHandlerSwap.ts +2 -2
- package/src/swaps/escrow/FromBtcBaseSwapHandler.ts +8 -5
- package/src/swaps/escrow/frombtc_abstract/FromBtcAbs.ts +2 -2
- package/src/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.ts +11 -12
- package/src/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.ts +4 -0
- package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.ts +847 -0
- package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.ts +196 -0
- package/src/swaps/escrow/tobtc_abstract/ToBtcAbs.ts +55 -36
- package/src/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.ts +19 -15
- package/src/swaps/spv_vault_swap/SpvVault.ts +3 -3
- package/src/swaps/spv_vault_swap/SpvVaultSwapHandler.ts +10 -8
- package/src/swaps/spv_vault_swap/SpvVaults.ts +130 -91
- package/src/swaps/trusted/frombtc_trusted/FromBtcTrusted.ts +3 -3
- package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.ts +13 -16
- package/src/utils/Utils.ts +19 -17
- package/src/utils/paramcoders/server/ServerParamDecoder.ts +9 -6
- package/src/wallets/IBitcoinWallet.ts +8 -0
- package/dist/wallets/ISpvVaultWallet.d.ts +0 -42
- package/dist/wallets/ISpvVaultWallet.js +0 -2
|
@@ -167,7 +167,7 @@ export class PluginManager {
|
|
|
167
167
|
}
|
|
168
168
|
|
|
169
169
|
static async onHandlePostFromBtcQuote(
|
|
170
|
-
swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV,
|
|
170
|
+
swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV | SwapHandlerType.FROM_BTCLN_AUTO,
|
|
171
171
|
request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>,
|
|
172
172
|
requestedAmount: {input: boolean, amount: bigint, token: string, pricePrefetch?: Promise<bigint>},
|
|
173
173
|
chainIdentifier: string,
|
|
@@ -198,7 +198,7 @@ export class PluginManager {
|
|
|
198
198
|
}
|
|
199
199
|
|
|
200
200
|
static async onHandlePreFromBtcQuote(
|
|
201
|
-
swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV,
|
|
201
|
+
swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV | SwapHandlerType.FROM_BTCLN_AUTO,
|
|
202
202
|
request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>,
|
|
203
203
|
requestedAmount: {input: boolean, amount: bigint, token: string},
|
|
204
204
|
chainIdentifier: string,
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {StorageObject} from "@atomiqlabs/base";
|
|
2
2
|
import * as fs from "fs/promises";
|
|
3
|
+
import * as fsSync from "fs";
|
|
3
4
|
import {IIntermediaryStorage, StorageQueryParam} from "../storage/IIntermediaryStorage";
|
|
5
|
+
import {getLogger, LoggerType} from "../utils/Utils";
|
|
4
6
|
|
|
5
7
|
export class IntermediaryStorageManager<T extends StorageObject> implements IIntermediaryStorage<T> {
|
|
6
8
|
|
|
@@ -9,9 +11,11 @@ export class IntermediaryStorageManager<T extends StorageObject> implements IInt
|
|
|
9
11
|
private data: {
|
|
10
12
|
[key: string]: T
|
|
11
13
|
} = {};
|
|
14
|
+
private logger: LoggerType;
|
|
12
15
|
|
|
13
16
|
constructor(directory: string) {
|
|
14
17
|
this.directory = directory;
|
|
18
|
+
this.logger = getLogger("IntermediaryStorageManager("+directory+"): ");
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
async init(): Promise<void> {
|
|
@@ -82,18 +86,23 @@ export class IntermediaryStorageManager<T extends StorageObject> implements IInt
|
|
|
82
86
|
if(this.data[identifier]!=null) delete this.data[identifier];
|
|
83
87
|
await fs.rm(this.directory+"/"+identifier+".json");
|
|
84
88
|
} catch (e) {
|
|
85
|
-
|
|
89
|
+
this.logger.error("removeData(): Error when removing data: ", e);
|
|
86
90
|
}
|
|
87
91
|
}
|
|
88
92
|
|
|
89
93
|
async loadData(type: new(data: any) => T): Promise<void> {
|
|
90
94
|
this.type = type;
|
|
91
95
|
|
|
96
|
+
if(!fsSync.existsSync(this.directory)) {
|
|
97
|
+
this.logger.debug("loadData(): Data directory not found!");
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
92
101
|
let files: string[];
|
|
93
102
|
try {
|
|
94
103
|
files = await fs.readdir(this.directory);
|
|
95
104
|
} catch (e) {
|
|
96
|
-
|
|
105
|
+
this.logger.error("loadData(): Error when checking directory: ", e);
|
|
97
106
|
return;
|
|
98
107
|
}
|
|
99
108
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {StorageObject, IStorageManager} from "@atomiqlabs/base";
|
|
2
2
|
import * as fs from "fs/promises";
|
|
3
|
+
import {getLogger, LoggerType} from "../utils/Utils";
|
|
4
|
+
import * as fsSync from "fs";
|
|
3
5
|
|
|
4
6
|
export class StorageManager<T extends StorageObject> implements IStorageManager<T> {
|
|
5
7
|
|
|
@@ -8,8 +10,11 @@ export class StorageManager<T extends StorageObject> implements IStorageManager<
|
|
|
8
10
|
[key: string]: T
|
|
9
11
|
} = {};
|
|
10
12
|
|
|
13
|
+
private logger: LoggerType;
|
|
14
|
+
|
|
11
15
|
constructor(directory: string) {
|
|
12
16
|
this.directory = directory;
|
|
17
|
+
this.logger = getLogger("StorageManager("+directory+"): ");
|
|
13
18
|
}
|
|
14
19
|
|
|
15
20
|
async init(): Promise<void> {
|
|
@@ -38,16 +43,21 @@ export class StorageManager<T extends StorageObject> implements IStorageManager<
|
|
|
38
43
|
if(this.data[paymentHash]!=null) delete this.data[paymentHash];
|
|
39
44
|
await fs.rm(this.directory+"/"+paymentHash+".json");
|
|
40
45
|
} catch (e) {
|
|
41
|
-
|
|
46
|
+
this.logger.error("removeData(): Error when removing data: ", e);
|
|
42
47
|
}
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
async loadData(type: new(data: any) => T): Promise<T[]> {
|
|
51
|
+
if(!fsSync.existsSync(this.directory)) {
|
|
52
|
+
this.logger.debug("loadData(): Data directory not found!");
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
46
56
|
let files;
|
|
47
57
|
try {
|
|
48
58
|
files = await fs.readdir(this.directory);
|
|
49
59
|
} catch (e) {
|
|
50
|
-
|
|
60
|
+
this.logger.error("loadData(): Error when checking directory: ", e);
|
|
51
61
|
return [];
|
|
52
62
|
}
|
|
53
63
|
|
package/src/swaps/SwapHandler.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {SwapHandlerSwap} from "./SwapHandlerSwap";
|
|
|
8
8
|
import {PluginManager} from "../plugins/PluginManager";
|
|
9
9
|
import {IIntermediaryStorage} from "../storage/IIntermediaryStorage";
|
|
10
10
|
import {IParamReader} from "../utils/paramcoders/IParamReader";
|
|
11
|
+
import {getLogger, LoggerType} from "../utils/Utils";
|
|
11
12
|
|
|
12
13
|
export enum SwapHandlerType {
|
|
13
14
|
TO_BTC = "TO_BTC",
|
|
@@ -16,7 +17,8 @@ export enum SwapHandlerType {
|
|
|
16
17
|
FROM_BTCLN = "FROM_BTCLN",
|
|
17
18
|
FROM_BTCLN_TRUSTED = "FROM_BTCLN_TRUSTED",
|
|
18
19
|
FROM_BTC_TRUSTED = "FROM_BTC_TRUSTED",
|
|
19
|
-
FROM_BTC_SPV = "FROM_BTC_SPV"
|
|
20
|
+
FROM_BTC_SPV = "FROM_BTC_SPV",
|
|
21
|
+
FROM_BTCLN_AUTO = "FROM_BTCLN_AUTO"
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
export type SwapHandlerInfoType = {
|
|
@@ -24,7 +26,6 @@ export type SwapHandlerInfoType = {
|
|
|
24
26
|
swapBaseFee: number,
|
|
25
27
|
min: number,
|
|
26
28
|
max: number,
|
|
27
|
-
tokens: string[],
|
|
28
29
|
chainTokens: {[chainId: string]: string[]};
|
|
29
30
|
data?: any,
|
|
30
31
|
};
|
|
@@ -46,8 +47,7 @@ export type SwapBaseConfig = {
|
|
|
46
47
|
export type MultichainData = {
|
|
47
48
|
chains: {
|
|
48
49
|
[identifier: string]: ChainData
|
|
49
|
-
}
|
|
50
|
-
default: string
|
|
50
|
+
}
|
|
51
51
|
};
|
|
52
52
|
|
|
53
53
|
export type ChainData<T extends ChainType = ChainType> = {
|
|
@@ -85,12 +85,7 @@ export abstract class SwapHandler<V extends SwapHandlerSwap<S> = SwapHandlerSwap
|
|
|
85
85
|
|
|
86
86
|
abstract config: SwapBaseConfig;
|
|
87
87
|
|
|
88
|
-
logger =
|
|
89
|
-
debug: (msg: string, ...args: any) => console.debug("SwapHandler("+this.type+"): "+msg, ...args),
|
|
90
|
-
info: (msg: string, ...args: any) => console.info("SwapHandler("+this.type+"): "+msg, ...args),
|
|
91
|
-
warn: (msg: string, ...args: any) => console.warn("SwapHandler("+this.type+"): "+msg, ...args),
|
|
92
|
-
error: (msg: string, ...args: any) => console.error("SwapHandler("+this.type+"): "+msg, ...args)
|
|
93
|
-
};
|
|
88
|
+
logger: LoggerType = getLogger(() => "SwapHandler("+this.type+"): ");
|
|
94
89
|
|
|
95
90
|
protected swapLogger = {
|
|
96
91
|
debug: (swap: SwapHandlerSwap, msg: string, ...args: any) => this.logger.debug(swap.getIdentifier()+": "+msg, ...args),
|
|
@@ -107,7 +102,6 @@ export abstract class SwapHandler<V extends SwapHandlerSwap<S> = SwapHandlerSwap
|
|
|
107
102
|
) {
|
|
108
103
|
this.storageManager = storageDirectory;
|
|
109
104
|
this.chains = chainsData;
|
|
110
|
-
if(this.chains.chains[this.chains.default]==null) throw new Error("Invalid default chain specified");
|
|
111
105
|
this.path = path;
|
|
112
106
|
this.swapPricing = swapPricing;
|
|
113
107
|
this.allowedTokens = {};
|
|
@@ -116,10 +110,6 @@ export abstract class SwapHandler<V extends SwapHandlerSwap<S> = SwapHandlerSwap
|
|
|
116
110
|
}
|
|
117
111
|
}
|
|
118
112
|
|
|
119
|
-
protected getDefaultChain(): ChainData {
|
|
120
|
-
return this.chains.chains[this.chains.default];
|
|
121
|
-
}
|
|
122
|
-
|
|
123
113
|
protected getChain(identifier: string): ChainData {
|
|
124
114
|
if(this.chains.chains[identifier]==null)
|
|
125
115
|
throw {
|
|
@@ -137,7 +127,7 @@ export abstract class SwapHandler<V extends SwapHandlerSwap<S> = SwapHandlerSwap
|
|
|
137
127
|
async startWatchdog() {
|
|
138
128
|
let rerun: () => Promise<void>;
|
|
139
129
|
rerun = async () => {
|
|
140
|
-
await this.processPastSwaps().catch( e =>
|
|
130
|
+
await this.processPastSwaps().catch( e => this.logger.error("startWatchdog(): Error when processing past swaps: ", e));
|
|
141
131
|
setTimeout(rerun, this.config.swapCheckInterval);
|
|
142
132
|
};
|
|
143
133
|
await rerun();
|
|
@@ -268,7 +258,6 @@ export abstract class SwapHandler<V extends SwapHandlerSwap<S> = SwapHandlerSwap
|
|
|
268
258
|
min: Number(this.config.min),
|
|
269
259
|
max: Number(this.config.max),
|
|
270
260
|
data: this.getInfoData(),
|
|
271
|
-
tokens: Array.from<string>(this.allowedTokens[this.chains.default]),
|
|
272
261
|
chainTokens
|
|
273
262
|
};
|
|
274
263
|
}
|
|
@@ -3,7 +3,7 @@ import {FromBtcRequestType} from "../escrow/frombtc_abstract/FromBtcAbs";
|
|
|
3
3
|
import {FromBtcLnTrustedRequestType} from "../trusted/frombtcln_trusted/FromBtcLnTrusted";
|
|
4
4
|
import {PluginManager} from "../../plugins/PluginManager";
|
|
5
5
|
import {isPluginQuote, isQuoteSetFees} from "../../plugins/IPlugin";
|
|
6
|
-
import {RequestData, SwapHandlerType} from "../SwapHandler";
|
|
6
|
+
import {RequestData, SwapHandler, SwapHandlerType} from "../SwapHandler";
|
|
7
7
|
import {AmountAssertions, AmountAssertionsConfig} from "./AmountAssertions";
|
|
8
8
|
import {ISwapPrice} from "../../prices/ISwapPrice";
|
|
9
9
|
import {FromBtcTrustedRequestType} from "../trusted/frombtc_trusted/FromBtcTrusted";
|
|
@@ -32,7 +32,7 @@ export class FromBtcAmountAssertions extends AmountAssertions {
|
|
|
32
32
|
* @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds
|
|
33
33
|
*/
|
|
34
34
|
async preCheckFromBtcAmounts(
|
|
35
|
-
swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV,
|
|
35
|
+
swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV | SwapHandlerType.FROM_BTCLN_AUTO,
|
|
36
36
|
request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>,
|
|
37
37
|
requestedAmount: {input: boolean, amount: bigint, token: string},
|
|
38
38
|
gasAmount?: {input: false, amount: bigint, token: string}
|
|
@@ -94,7 +94,7 @@ export class FromBtcAmountAssertions extends AmountAssertions {
|
|
|
94
94
|
* @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds
|
|
95
95
|
*/
|
|
96
96
|
async checkFromBtcAmount(
|
|
97
|
-
swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV,
|
|
97
|
+
swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV | SwapHandlerType.FROM_BTCLN_AUTO,
|
|
98
98
|
request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>,
|
|
99
99
|
requestedAmount: {input: boolean, amount: bigint, token: string, pricePrefetch?: Promise<bigint>},
|
|
100
100
|
fees: {baseFee: bigint, feePPM: bigint},
|
|
@@ -155,18 +155,27 @@ export class FromBtcAmountAssertions extends AmountAssertions {
|
|
|
155
155
|
}
|
|
156
156
|
|
|
157
157
|
let amountBDgas: bigint = 0n;
|
|
158
|
+
let gasSwapFee: bigint = 0n;
|
|
158
159
|
if(gasTokenAmount!=null) {
|
|
159
160
|
amountBDgas = await this.swapPricing.getToBtcSwapAmount(gasTokenAmount.amount, gasTokenAmount.token, chainIdentifier, true, gasTokenAmount.pricePrefetch);
|
|
161
|
+
signal.throwIfAborted();
|
|
162
|
+
const denominator = (1000000n - fees.feePPM);
|
|
163
|
+
const _amountBDgas = (amountBDgas * 1000000n + denominator - 1n) / denominator;
|
|
164
|
+
gasSwapFee = _amountBDgas - amountBDgas;
|
|
165
|
+
amountBDgas = _amountBDgas;
|
|
160
166
|
}
|
|
161
167
|
|
|
162
168
|
let amountBD: bigint;
|
|
169
|
+
let swapFee: bigint;
|
|
163
170
|
if(!requestedAmount.input) {
|
|
164
171
|
amountBD = await this.swapPricing.getToBtcSwapAmount(requestedAmount.amount, requestedAmount.token, chainIdentifier, true, requestedAmount.pricePrefetch);
|
|
165
172
|
signal.throwIfAborted();
|
|
166
173
|
|
|
167
174
|
// amt = (amt+base_fee)/(1-fee)
|
|
168
|
-
|
|
169
|
-
|
|
175
|
+
const denominator = (1000000n - fees.feePPM);
|
|
176
|
+
const _amountBD = ((amountBD + fees.baseFee) * 1000000n + denominator - 1n) / denominator;
|
|
177
|
+
swapFee = _amountBD - amountBD;
|
|
178
|
+
amountBD = _amountBD;
|
|
170
179
|
|
|
171
180
|
const tooLow = amountBD < (this.config.min * 95n / 100n);
|
|
172
181
|
const tooHigh = amountBD > (this.config.max * 105n / 100n);
|
|
@@ -191,6 +200,7 @@ export class FromBtcAmountAssertions extends AmountAssertions {
|
|
|
191
200
|
} else {
|
|
192
201
|
this.checkBtcAmountInBounds(requestedAmount.amount);
|
|
193
202
|
amountBD = requestedAmount.amount - amountBDgas;
|
|
203
|
+
swapFee = fees.baseFee + ((amountBD * fees.feePPM + 999_999n) / 1000000n);
|
|
194
204
|
if(amountBD < 0n) {
|
|
195
205
|
throw {
|
|
196
206
|
code: 20003,
|
|
@@ -203,11 +213,9 @@ export class FromBtcAmountAssertions extends AmountAssertions {
|
|
|
203
213
|
}
|
|
204
214
|
}
|
|
205
215
|
|
|
206
|
-
const swapFee = fees.baseFee + (amountBD * fees.feePPM / 1000000n);
|
|
207
216
|
const swapFeeInToken = await this.swapPricing.getFromBtcSwapAmount(swapFee, requestedAmount.token, chainIdentifier, true, requestedAmount.pricePrefetch);
|
|
208
217
|
signal.throwIfAborted();
|
|
209
218
|
|
|
210
|
-
const gasSwapFee = ((amountBDgas * fees.feePPM) + 999999n) / 1000000n;
|
|
211
219
|
const gasSwapFeeInToken = gasTokenAmount==null ?
|
|
212
220
|
0n :
|
|
213
221
|
await this.swapPricing.getFromBtcSwapAmount(gasSwapFee, gasTokenAmount.token, chainIdentifier, true, gasTokenAmount.pricePrefetch);
|
|
@@ -217,7 +225,7 @@ export class FromBtcAmountAssertions extends AmountAssertions {
|
|
|
217
225
|
if(!requestedAmount.input) {
|
|
218
226
|
totalInToken = requestedAmount.amount;
|
|
219
227
|
} else {
|
|
220
|
-
totalInToken = await this.swapPricing.getFromBtcSwapAmount(amountBD - swapFee
|
|
228
|
+
totalInToken = await this.swapPricing.getFromBtcSwapAmount(amountBD - swapFee, requestedAmount.token, chainIdentifier, null, requestedAmount.pricePrefetch);
|
|
221
229
|
signal.throwIfAborted();
|
|
222
230
|
}
|
|
223
231
|
|
|
@@ -55,14 +55,14 @@ export abstract class EscrowHandlerSwap<T extends SwapData = SwapData, S = any>
|
|
|
55
55
|
* Returns the escrow hash - i.e. hash of the escrow data
|
|
56
56
|
*/
|
|
57
57
|
getEscrowHash(): string {
|
|
58
|
-
return this.data
|
|
58
|
+
return this.data?.getEscrowHash();
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
62
|
* Returns the claim data hash - i.e. hash passed to the claim handler
|
|
63
63
|
*/
|
|
64
64
|
getClaimHash(): string {
|
|
65
|
-
return this.data
|
|
65
|
+
return this.data?.getClaimHash();
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
/**
|
|
@@ -84,12 +84,12 @@ export abstract class FromBtcBaseSwapHandler<V extends FromBtcBaseSwap<SwapData,
|
|
|
84
84
|
abortController: AbortController
|
|
85
85
|
): Promise<bigint> {
|
|
86
86
|
//Solana workaround
|
|
87
|
-
const {swapContract, chainInterface} = this.getChain(chainIdentifier);
|
|
87
|
+
const {swapContract, chainInterface, signer} = this.getChain(chainIdentifier);
|
|
88
88
|
let feeResult: bigint;
|
|
89
89
|
const gasToken = chainInterface.getNativeCurrencyAddress();
|
|
90
90
|
if (swapContract.getRawRefundFee != null) {
|
|
91
91
|
try {
|
|
92
|
-
feeResult = await swapContract.getRawRefundFee(dummySwapData);
|
|
92
|
+
feeResult = await swapContract.getRawRefundFee(signer.getAddress(), dummySwapData);
|
|
93
93
|
} catch (e) {
|
|
94
94
|
this.logger.error("getBaseSecurityDepositPrefetch(): pre-fetch error: ", e);
|
|
95
95
|
abortController.abort(e);
|
|
@@ -97,7 +97,7 @@ export abstract class FromBtcBaseSwapHandler<V extends FromBtcBaseSwap<SwapData,
|
|
|
97
97
|
}
|
|
98
98
|
} else {
|
|
99
99
|
try {
|
|
100
|
-
feeResult = await swapContract.getRefundFee(dummySwapData);
|
|
100
|
+
feeResult = await swapContract.getRefundFee(signer.getAddress(), dummySwapData);
|
|
101
101
|
} catch (e1) {
|
|
102
102
|
this.logger.error("getBaseSecurityDepositPrefetch(): pre-fetch error: ", e1);
|
|
103
103
|
abortController.abort(e1);
|
|
@@ -116,11 +116,12 @@ export abstract class FromBtcBaseSwapHandler<V extends FromBtcBaseSwap<SwapData,
|
|
|
116
116
|
* @param chainIdentifier
|
|
117
117
|
* @param useToken
|
|
118
118
|
* @param abortController
|
|
119
|
+
* @param inContract
|
|
119
120
|
*/
|
|
120
|
-
protected async getBalancePrefetch(chainIdentifier: string, useToken: string, abortController: AbortController): Promise<bigint> {
|
|
121
|
+
protected async getBalancePrefetch(chainIdentifier: string, useToken: string, abortController: AbortController, inContract: boolean = true): Promise<bigint> {
|
|
121
122
|
const {swapContract, signer} = this.getChain(chainIdentifier);
|
|
122
123
|
try {
|
|
123
|
-
return await swapContract.getBalance(signer.getAddress(), useToken,
|
|
124
|
+
return await swapContract.getBalance(signer.getAddress(), useToken, inContract);
|
|
124
125
|
} catch (e) {
|
|
125
126
|
this.logger.error("getBalancePrefetch(): balancePrefetch error: ", e);
|
|
126
127
|
abortController.abort(e);
|
|
@@ -137,6 +138,8 @@ export abstract class FromBtcBaseSwapHandler<V extends FromBtcBaseSwap<SwapData,
|
|
|
137
138
|
* @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
|
|
138
139
|
*/
|
|
139
140
|
protected async checkBalance(totalInToken: bigint, balancePrefetch: Promise<bigint>, signal: AbortSignal | null): Promise<void> {
|
|
141
|
+
if(totalInToken===0n) return;
|
|
142
|
+
|
|
140
143
|
const balance = await balancePrefetch;
|
|
141
144
|
if(signal!=null) signal.throwIfAborted();
|
|
142
145
|
|
|
@@ -254,7 +254,7 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
254
254
|
times: {[key: string]: number},
|
|
255
255
|
} = {request: {}, times: {}};
|
|
256
256
|
|
|
257
|
-
const chainIdentifier = req.query.chain as string
|
|
257
|
+
const chainIdentifier = req.query.chain as string;
|
|
258
258
|
const {swapContract, signer, chainInterface} = this.getChain(chainIdentifier);
|
|
259
259
|
const depositToken = req.query.depositToken as string ?? chainInterface.getNativeCurrencyAddress();
|
|
260
260
|
this.checkAllowedDepositToken(chainIdentifier, depositToken);
|
|
@@ -279,7 +279,7 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
279
279
|
const parsedBody: FromBtcRequestType = await req.paramReader.getParams({
|
|
280
280
|
address: (val: string) => val!=null &&
|
|
281
281
|
typeof(val)==="string" &&
|
|
282
|
-
chainInterface.isValidAddress(val) ? val : null,
|
|
282
|
+
chainInterface.isValidAddress(val, true) ? val : null,
|
|
283
283
|
amount: FieldTypeEnum.BigInt,
|
|
284
284
|
token: (val: string) => val!=null &&
|
|
285
285
|
typeof(val)==="string" &&
|
|
@@ -522,17 +522,16 @@ export class FromBtcLnAbs extends FromBtcBaseSwapHandler<FromBtcLnSwapAbs, FromB
|
|
|
522
522
|
};
|
|
523
523
|
|
|
524
524
|
const arr = invoice.description.split("-");
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
}
|
|
525
|
+
if(arr.length<2) throw {
|
|
526
|
+
_httpStatus: 200,
|
|
527
|
+
code: 10001,
|
|
528
|
+
msg: "Invoice expired/canceled"
|
|
529
|
+
};
|
|
530
|
+
const chainIdentifier = arr[0];
|
|
531
|
+
const address = arr[1];
|
|
532
|
+
|
|
534
533
|
const {chainInterface} = this.getChain(chainIdentifier);
|
|
535
|
-
if(!chainInterface.isValidAddress(address)) throw {
|
|
534
|
+
if(!chainInterface.isValidAddress(address, true)) throw {
|
|
536
535
|
_httpStatus: 200,
|
|
537
536
|
code: 10001,
|
|
538
537
|
msg: "Invoice expired/canceled"
|
|
@@ -573,7 +572,7 @@ export class FromBtcLnAbs extends FromBtcBaseSwapHandler<FromBtcLnSwapAbs, FromB
|
|
|
573
572
|
times: {[key: string]: number}
|
|
574
573
|
} = {request: {}, times: {}};
|
|
575
574
|
|
|
576
|
-
const chainIdentifier = req.query.chain as string
|
|
575
|
+
const chainIdentifier = req.query.chain as string;
|
|
577
576
|
const {swapContract, signer, chainInterface} = this.getChain(chainIdentifier);
|
|
578
577
|
const depositToken = req.query.depositToken as string ?? chainInterface.getNativeCurrencyAddress();
|
|
579
578
|
this.checkAllowedDepositToken(chainIdentifier, depositToken);
|
|
@@ -595,7 +594,7 @@ export class FromBtcLnAbs extends FromBtcBaseSwapHandler<FromBtcLnSwapAbs, FromB
|
|
|
595
594
|
const parsedBody: FromBtcLnRequestType = await req.paramReader.getParams({
|
|
596
595
|
address: (val: string) => val!=null &&
|
|
597
596
|
typeof(val)==="string" &&
|
|
598
|
-
chainInterface.isValidAddress(val) ? val : null,
|
|
597
|
+
chainInterface.isValidAddress(val, true) ? val : null,
|
|
599
598
|
paymentHash: (val: string) => val!=null &&
|
|
600
599
|
typeof(val)==="string" &&
|
|
601
600
|
val.length===64 &&
|
|
@@ -114,6 +114,10 @@ export class FromBtcLnSwapAbs<T extends SwapData = SwapData> extends FromBtcBase
|
|
|
114
114
|
return this.totalTokens;
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
+
getClaimHash(): string {
|
|
118
|
+
return this.claimHash;
|
|
119
|
+
}
|
|
120
|
+
|
|
117
121
|
getIdentifierHash(): string {
|
|
118
122
|
return this.lnPaymentHash;
|
|
119
123
|
}
|