@atomiqlabs/lp-lib 11.0.5 → 12.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/info/InfoHandler.js +3 -12
- package/dist/plugins/IPlugin.d.ts +34 -35
- package/dist/plugins/IPlugin.js +7 -8
- package/dist/plugins/PluginManager.d.ts +29 -30
- package/dist/plugins/PluginManager.js +160 -188
- package/dist/prices/BinanceSwapPrice.d.ts +1 -2
- package/dist/prices/BinanceSwapPrice.js +56 -70
- package/dist/prices/CoinGeckoSwapPrice.d.ts +2 -3
- package/dist/prices/CoinGeckoSwapPrice.js +30 -42
- package/dist/prices/OKXSwapPrice.d.ts +1 -2
- package/dist/prices/OKXSwapPrice.js +56 -70
- package/dist/storage/IIntermediaryStorage.d.ts +8 -5
- package/dist/storagemanager/IntermediaryStorageManager.d.ts +8 -5
- package/dist/storagemanager/IntermediaryStorageManager.js +52 -61
- package/dist/storagemanager/StorageManager.js +42 -59
- package/dist/swaps/FromBtcBaseSwap.d.ts +7 -8
- package/dist/swaps/FromBtcBaseSwap.js +2 -3
- package/dist/swaps/FromBtcBaseSwapHandler.d.ts +44 -31
- package/dist/swaps/FromBtcBaseSwapHandler.js +217 -196
- package/dist/swaps/FromBtcLnBaseSwapHandler.d.ts +1 -2
- package/dist/swaps/FromBtcLnBaseSwapHandler.js +13 -24
- package/dist/swaps/ISwapPrice.d.ts +4 -5
- package/dist/swaps/ISwapPrice.js +8 -30
- package/dist/swaps/SwapHandler.d.ts +26 -15
- package/dist/swaps/SwapHandler.js +107 -66
- package/dist/swaps/SwapHandlerSwap.d.ts +22 -11
- package/dist/swaps/SwapHandlerSwap.js +32 -8
- package/dist/swaps/ToBtcBaseSwap.d.ts +16 -17
- package/dist/swaps/ToBtcBaseSwap.js +4 -4
- package/dist/swaps/ToBtcBaseSwapHandler.d.ts +17 -17
- package/dist/swaps/ToBtcBaseSwapHandler.js +140 -155
- package/dist/swaps/frombtc_abstract/FromBtcAbs.d.ts +8 -15
- package/dist/swaps/frombtc_abstract/FromBtcAbs.js +131 -196
- package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.d.ts +3 -5
- package/dist/swaps/frombtc_abstract/FromBtcSwapAbs.js +4 -4
- package/dist/swaps/frombtc_trusted/FromBtcTrusted.d.ts +10 -8
- package/dist/swaps/frombtc_trusted/FromBtcTrusted.js +330 -362
- package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.d.ts +10 -10
- package/dist/swaps/frombtc_trusted/FromBtcTrustedSwap.js +7 -4
- package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.d.ts +8 -8
- package/dist/swaps/frombtcln_abstract/FromBtcLnAbs.js +330 -400
- package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +10 -4
- package/dist/swaps/frombtcln_abstract/FromBtcLnSwapAbs.js +36 -6
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.d.ts +8 -7
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrusted.js +251 -273
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +5 -5
- package/dist/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.js +7 -5
- package/dist/swaps/tobtc_abstract/ToBtcAbs.d.ts +13 -12
- package/dist/swaps/tobtc_abstract/ToBtcAbs.js +261 -323
- package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.d.ts +4 -4
- package/dist/swaps/tobtc_abstract/ToBtcSwapAbs.js +7 -6
- package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.d.ts +22 -21
- package/dist/swaps/tobtcln_abstract/ToBtcLnAbs.js +398 -453
- package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +3 -2
- package/dist/swaps/tobtcln_abstract/ToBtcLnSwapAbs.js +10 -6
- package/dist/utils/Utils.d.ts +2 -3
- package/dist/utils/Utils.js +4 -14
- package/dist/utils/paramcoders/LegacyParamEncoder.js +3 -14
- package/dist/utils/paramcoders/ParamDecoder.js +53 -65
- package/dist/utils/paramcoders/SchemaVerifier.d.ts +4 -5
- package/dist/utils/paramcoders/SchemaVerifier.js +8 -9
- package/dist/utils/paramcoders/server/ServerParamDecoder.js +1 -1
- package/dist/utils/paramcoders/server/ServerParamEncoder.js +3 -14
- package/dist/wallets/IBitcoinWallet.d.ts +4 -5
- package/dist/wallets/ILightningWallet.d.ts +20 -21
- package/dist/wallets/ILightningWallet.js +1 -1
- package/package.json +4 -6
- package/src/plugins/IPlugin.ts +28 -29
- package/src/plugins/PluginManager.ts +21 -22
- package/src/prices/BinanceSwapPrice.ts +3 -4
- package/src/prices/CoinGeckoSwapPrice.ts +4 -5
- package/src/prices/OKXSwapPrice.ts +3 -4
- package/src/storage/IIntermediaryStorage.ts +4 -5
- package/src/storagemanager/IntermediaryStorageManager.ts +17 -9
- package/src/swaps/FromBtcBaseSwap.ts +9 -10
- package/src/swaps/FromBtcBaseSwapHandler.ts +133 -91
- package/src/swaps/FromBtcLnBaseSwapHandler.ts +2 -3
- package/src/swaps/ISwapPrice.ts +10 -20
- package/src/swaps/SwapHandler.ts +101 -35
- package/src/swaps/SwapHandlerSwap.ts +42 -17
- package/src/swaps/ToBtcBaseSwap.ts +20 -18
- package/src/swaps/ToBtcBaseSwapHandler.ts +33 -33
- package/src/swaps/frombtc_abstract/FromBtcAbs.ts +64 -97
- package/src/swaps/frombtc_abstract/FromBtcSwapAbs.ts +7 -8
- package/src/swaps/frombtc_trusted/FromBtcTrusted.ts +56 -55
- package/src/swaps/frombtc_trusted/FromBtcTrustedSwap.ts +28 -21
- package/src/swaps/frombtcln_abstract/FromBtcLnAbs.ts +81 -116
- package/src/swaps/frombtcln_abstract/FromBtcLnSwapAbs.ts +73 -9
- package/src/swaps/frombtcln_trusted/FromBtcLnTrusted.ts +39 -36
- package/src/swaps/frombtcln_trusted/FromBtcLnTrustedSwap.ts +26 -12
- package/src/swaps/tobtc_abstract/ToBtcAbs.ts +82 -98
- package/src/swaps/tobtc_abstract/ToBtcSwapAbs.ts +25 -20
- package/src/swaps/tobtcln_abstract/ToBtcLnAbs.ts +106 -118
- package/src/swaps/tobtcln_abstract/ToBtcLnSwapAbs.ts +17 -9
- package/src/utils/Utils.ts +3 -4
- package/src/utils/paramcoders/ParamDecoder.ts +5 -5
- package/src/utils/paramcoders/SchemaVerifier.ts +10 -11
- package/src/utils/paramcoders/server/ServerParamDecoder.ts +1 -1
- package/src/wallets/IBitcoinWallet.ts +4 -5
- package/src/wallets/ILightningWallet.ts +21 -22
|
@@ -12,7 +12,6 @@ import {
|
|
|
12
12
|
} from "@atomiqlabs/base";
|
|
13
13
|
import {Express, Request, Response} from "express";
|
|
14
14
|
import {MultichainData, SwapHandlerType} from "../SwapHandler";
|
|
15
|
-
import * as BN from "bn.js";
|
|
16
15
|
import {IIntermediaryStorage} from "../../storage/IIntermediaryStorage";
|
|
17
16
|
import {ISwapPrice} from "../ISwapPrice";
|
|
18
17
|
import {PluginManager} from "../../plugins/PluginManager";
|
|
@@ -20,7 +19,6 @@ import {expressHandlerWrapper, HEX_REGEX} from "../../utils/Utils";
|
|
|
20
19
|
import {IParamReader} from "../../utils/paramcoders/IParamReader";
|
|
21
20
|
import {ServerParamEncoder} from "../../utils/paramcoders/server/ServerParamEncoder";
|
|
22
21
|
import {FieldTypeEnum, verifySchema} from "../../utils/paramcoders/SchemaVerifier";
|
|
23
|
-
import {serverParamDecoder} from "../../utils/paramcoders/server/ServerParamDecoder";
|
|
24
22
|
import {IBitcoinWallet} from "../../wallets/IBitcoinWallet";
|
|
25
23
|
|
|
26
24
|
export type FromBtcTrustedConfig = FromBtcBaseConfig & {
|
|
@@ -31,12 +29,15 @@ export type FromBtcTrustedConfig = FromBtcBaseConfig & {
|
|
|
31
29
|
|
|
32
30
|
export type FromBtcTrustedRequestType = {
|
|
33
31
|
address: string,
|
|
34
|
-
amount:
|
|
35
|
-
|
|
32
|
+
amount: bigint,
|
|
33
|
+
exactIn?: boolean,
|
|
34
|
+
refundAddress?: string,
|
|
35
|
+
token?: string
|
|
36
36
|
};
|
|
37
37
|
|
|
38
38
|
export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, FromBtcTrustedSwapState> {
|
|
39
39
|
readonly type: SwapHandlerType = SwapHandlerType.FROM_BTC_TRUSTED;
|
|
40
|
+
readonly swapType = null;
|
|
40
41
|
|
|
41
42
|
readonly config: FromBtcTrustedConfig;
|
|
42
43
|
readonly bitcoin: IBitcoinWallet;
|
|
@@ -47,7 +48,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
47
48
|
|
|
48
49
|
readonly refundedSwaps: Map<string, string> = new Map();
|
|
49
50
|
readonly doubleSpentSwaps: Map<string, string> = new Map();
|
|
50
|
-
readonly processedTxIds: Map<string, { scTxId: string, txId: string, adjustedAmount:
|
|
51
|
+
readonly processedTxIds: Map<string, { scTxId: string, txId: string, adjustedAmount: bigint, adjustedTotal: bigint }> = new Map();
|
|
51
52
|
|
|
52
53
|
constructor(
|
|
53
54
|
storageDirectory: IIntermediaryStorage<FromBtcTrustedSwap>,
|
|
@@ -63,9 +64,6 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
63
64
|
this.config.recommendFeeMultiplier ??= 1.25;
|
|
64
65
|
this.bitcoin = bitcoin;
|
|
65
66
|
this.bitcoinRpc = bitcoinRpc;
|
|
66
|
-
for(let chainId in chains.chains) {
|
|
67
|
-
this.allowedTokens[chainId] = new Set<string>([chains.chains[chainId].swapContract.getNativeCurrencyAddress()]);
|
|
68
|
-
}
|
|
69
67
|
}
|
|
70
68
|
|
|
71
69
|
private getAllAncestors(tx: BtcTx): Promise<{tx: BtcTx, vout: number}[]> {
|
|
@@ -78,7 +76,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
78
76
|
if(swap.refundAddress==null) {
|
|
79
77
|
if(swap.state!==FromBtcTrustedSwapState.REFUNDABLE) {
|
|
80
78
|
await swap.setState(FromBtcTrustedSwapState.REFUNDABLE);
|
|
81
|
-
await this.storageManager.saveData(swap.
|
|
79
|
+
await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
|
|
82
80
|
}
|
|
83
81
|
return;
|
|
84
82
|
}
|
|
@@ -115,7 +113,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
115
113
|
await this.bitcoin.sendRawTransaction(resp.raw);
|
|
116
114
|
this.swapLogger.debug(swap, "refundSwap(): sent refund transaction: "+refundTxId);
|
|
117
115
|
|
|
118
|
-
this.refundedSwaps.set(swap.
|
|
116
|
+
this.refundedSwaps.set(swap.getIdentifierHash(), refundTxId);
|
|
119
117
|
await this.removeSwapData(swap, FromBtcTrustedSwapState.REFUNDED);
|
|
120
118
|
unlock();
|
|
121
119
|
}
|
|
@@ -131,7 +129,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
131
129
|
if(burnTxFeeRate<initialTxFeeRate) {
|
|
132
130
|
this.swapLogger.warn(swap, "burn(): cannot send burn transaction, pays too little fee, " +
|
|
133
131
|
"initialTxId: "+swap.txId+" initialTxFeeRate: "+initialTxFeeRate+" burnTxFeeRate: "+burnTxFeeRate);
|
|
134
|
-
this.doubleSpentSwaps.set(swap.
|
|
132
|
+
this.doubleSpentSwaps.set(swap.getIdentifierHash(), null);
|
|
135
133
|
await this.removeSwapData(swap, FromBtcTrustedSwapState.DOUBLE_SPENT);
|
|
136
134
|
return;
|
|
137
135
|
}
|
|
@@ -161,7 +159,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
161
159
|
this.swapLogger.error(swap, "burn(): error sending burn package: ", e);
|
|
162
160
|
}
|
|
163
161
|
|
|
164
|
-
this.doubleSpentSwaps.set(swap.
|
|
162
|
+
this.doubleSpentSwaps.set(swap.getIdentifierHash(), burnTxId);
|
|
165
163
|
await this.removeSwapData(swap, FromBtcTrustedSwapState.DOUBLE_SPENT);
|
|
166
164
|
}
|
|
167
165
|
|
|
@@ -184,14 +182,14 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
184
182
|
}
|
|
185
183
|
return;
|
|
186
184
|
}
|
|
187
|
-
const sentSats =
|
|
188
|
-
if(sentSats
|
|
185
|
+
const sentSats = BigInt(foundVout.value);
|
|
186
|
+
if(sentSats === swap.amount) {
|
|
189
187
|
swap.adjustedInput = swap.amount;
|
|
190
188
|
swap.adjustedOutput = swap.outputTokens;
|
|
191
189
|
} else {
|
|
192
190
|
//If lower than minimum then ignore
|
|
193
|
-
if(sentSats
|
|
194
|
-
if(sentSats
|
|
191
|
+
if(sentSats < this.config.min) return;
|
|
192
|
+
if(sentSats > this.config.max) {
|
|
195
193
|
swap.adjustedInput = sentSats;
|
|
196
194
|
swap.btcTx = tx;
|
|
197
195
|
swap.txId = tx.txid;
|
|
@@ -202,27 +200,27 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
202
200
|
}
|
|
203
201
|
//Adjust the amount
|
|
204
202
|
swap.adjustedInput = sentSats;
|
|
205
|
-
swap.adjustedOutput = swap.outputTokens
|
|
203
|
+
swap.adjustedOutput = swap.outputTokens * sentSats / swap.amount;
|
|
206
204
|
}
|
|
207
205
|
swap.btcTx = tx;
|
|
208
206
|
swap.txId = tx.txid;
|
|
209
207
|
swap.vout = vout;
|
|
210
208
|
this.subscriptions.delete(outputScript);
|
|
211
209
|
await swap.setState(FromBtcTrustedSwapState.RECEIVED);
|
|
212
|
-
await this.storageManager.saveData(swap.
|
|
210
|
+
await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
|
|
213
211
|
}
|
|
214
212
|
|
|
215
213
|
if(swap.state===FromBtcTrustedSwapState.RECEIVED) {
|
|
216
214
|
//Check if transaction still exists
|
|
217
215
|
if(tx==null || foundVout==null || tx.txid!==swap.txId) {
|
|
218
216
|
await swap.setState(FromBtcTrustedSwapState.CREATED);
|
|
219
|
-
await this.storageManager.saveData(swap.
|
|
217
|
+
await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
|
|
220
218
|
return;
|
|
221
219
|
}
|
|
222
220
|
//Check if it is confirmed
|
|
223
221
|
if(tx.confirmations>0) {
|
|
224
222
|
await swap.setState(FromBtcTrustedSwapState.BTC_CONFIRMED);
|
|
225
|
-
await this.storageManager.saveData(swap.
|
|
223
|
+
await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
|
|
226
224
|
} else {
|
|
227
225
|
//Check if it pays high enough fee AND has confirmed ancestors
|
|
228
226
|
const ancestors = await this.getAllAncestors(tx);
|
|
@@ -239,7 +237,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
239
237
|
swap.txSize = tx.vsize;
|
|
240
238
|
swap.txFee = fee;
|
|
241
239
|
await swap.setState(FromBtcTrustedSwapState.BTC_CONFIRMED);
|
|
242
|
-
await this.storageManager.saveData(swap.
|
|
240
|
+
await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
|
|
243
241
|
} else {
|
|
244
242
|
return;
|
|
245
243
|
}
|
|
@@ -281,7 +279,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
281
279
|
|
|
282
280
|
if(swap.state===FromBtcTrustedSwapState.BTC_CONFIRMED) {
|
|
283
281
|
//Send gas token
|
|
284
|
-
const balance: Promise<
|
|
282
|
+
const balance: Promise<bigint> = swapContract.getBalance(signer.getAddress(), swap.token, false);
|
|
285
283
|
try {
|
|
286
284
|
await this.checkBalance(swap.adjustedOutput, balance, null);
|
|
287
285
|
if(swap.metadata!=null) swap.metadata.times.receivedBalanceChecked = Date.now();
|
|
@@ -296,13 +294,13 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
296
294
|
let unlock = swap.lock(30*1000);
|
|
297
295
|
if(unlock==null) return;
|
|
298
296
|
|
|
299
|
-
const txns = await swapContract.txsTransfer(signer.getAddress(),
|
|
297
|
+
const txns = await swapContract.txsTransfer(signer.getAddress(), swap.token, swap.adjustedOutput, swap.dstAddress);
|
|
300
298
|
await swapContract.sendAndConfirm(signer, txns, true, null, false, async (txId: string, rawTx: string) => {
|
|
301
299
|
swap.txIds = {init: txId};
|
|
302
300
|
swap.scRawTx = rawTx;
|
|
303
301
|
if(swap.state===FromBtcTrustedSwapState.BTC_CONFIRMED) {
|
|
304
302
|
await swap.setState(FromBtcTrustedSwapState.SENT);
|
|
305
|
-
await this.storageManager.saveData(swap.
|
|
303
|
+
await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
|
|
306
304
|
}
|
|
307
305
|
if(unlock!=null) unlock();
|
|
308
306
|
unlock = null;
|
|
@@ -317,7 +315,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
317
315
|
swap.txIds = {init: null};
|
|
318
316
|
swap.scRawTx = null;
|
|
319
317
|
await swap.setState(FromBtcTrustedSwapState.RECEIVED);
|
|
320
|
-
await this.storageManager.saveData(swap.
|
|
318
|
+
await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
|
|
321
319
|
break;
|
|
322
320
|
case "reverted":
|
|
323
321
|
//Cancel invoice
|
|
@@ -326,13 +324,13 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
326
324
|
break;
|
|
327
325
|
case "success":
|
|
328
326
|
await swap.setState(FromBtcTrustedSwapState.CONFIRMED);
|
|
329
|
-
await this.storageManager.saveData(swap.
|
|
327
|
+
await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
|
|
330
328
|
break;
|
|
331
329
|
}
|
|
332
330
|
}
|
|
333
331
|
|
|
334
332
|
if(swap.state===FromBtcTrustedSwapState.CONFIRMED) {
|
|
335
|
-
this.processedTxIds.set(swap.
|
|
333
|
+
this.processedTxIds.set(swap.getIdentifierHash(), {
|
|
336
334
|
txId: swap.txId,
|
|
337
335
|
scTxId: swap.txIds.init,
|
|
338
336
|
adjustedAmount: swap.adjustedInput,
|
|
@@ -357,7 +355,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
357
355
|
}
|
|
358
356
|
]);
|
|
359
357
|
|
|
360
|
-
const startingBlockheight = queriedData.reduce((prev, swap) => Math.min(prev, swap.createdHeight), Infinity);
|
|
358
|
+
const startingBlockheight = queriedData.reduce((prev, {obj: swap}) => Math.min(prev, swap.createdHeight), Infinity);
|
|
361
359
|
if(startingBlockheight===Infinity) return;
|
|
362
360
|
const transactions = await this.bitcoin.getWalletTransactions(startingBlockheight);
|
|
363
361
|
|
|
@@ -373,7 +371,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
373
371
|
})
|
|
374
372
|
});
|
|
375
373
|
|
|
376
|
-
for(let swap of queriedData) {
|
|
374
|
+
for(let {obj: swap} of queriedData) {
|
|
377
375
|
const outputScript = this.bitcoin.toOutputScript(swap.btcAddress).toString("hex");
|
|
378
376
|
const txs = map.get(outputScript) ?? [];
|
|
379
377
|
try {
|
|
@@ -412,12 +410,20 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
412
410
|
* amount: string amount (in lamports/smart chain base units) of the invoice
|
|
413
411
|
* exactOut: boolean whether to create and exact output swap
|
|
414
412
|
*/
|
|
415
|
-
|
|
413
|
+
req.query.token ??= swapContract.getNativeCurrencyAddress();
|
|
414
|
+
const parsedBody: FromBtcTrustedRequestType = verifySchema(req.query,{
|
|
416
415
|
address: (val: string) => val!=null &&
|
|
417
416
|
typeof(val)==="string" &&
|
|
418
417
|
swapContract.isValidAddress(val) ? val : null,
|
|
419
|
-
|
|
420
|
-
|
|
418
|
+
refundAddress: (val: string) => val==null ? "" :
|
|
419
|
+
typeof(val)==="string" &&
|
|
420
|
+
this.isValidBitcoinAddress(val) ? val : null,
|
|
421
|
+
token: (val: string) => val!=null &&
|
|
422
|
+
typeof(val)==="string" &&
|
|
423
|
+
this.isTokenSupported(chainIdentifier, val) ? val : null,
|
|
424
|
+
amount: FieldTypeEnum.BigInt,
|
|
425
|
+
exactIn: (val: string) => val==="true" ? true :
|
|
426
|
+
(val==="false" || val===undefined) ? false : null
|
|
421
427
|
});
|
|
422
428
|
if(parsedBody==null) throw {
|
|
423
429
|
code: 20100,
|
|
@@ -425,21 +431,16 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
425
431
|
};
|
|
426
432
|
metadata.request = parsedBody;
|
|
427
433
|
|
|
428
|
-
const
|
|
429
|
-
refundAddress: (val: string) => val!=null &&
|
|
430
|
-
typeof(val)==="string" &&
|
|
431
|
-
this.isValidBitcoinAddress(val) ? val : null
|
|
432
|
-
});
|
|
433
|
-
const refundAddress = refundAddressData?.refundAddress;
|
|
434
|
+
const refundAddress = parsedBody.refundAddress==="" ? null : parsedBody.refundAddress;
|
|
434
435
|
|
|
435
|
-
const requestedAmount = {input:
|
|
436
|
+
const requestedAmount = {input: parsedBody.exactIn, amount: parsedBody.amount};
|
|
436
437
|
const request = {
|
|
437
438
|
chainIdentifier,
|
|
438
439
|
raw: req,
|
|
439
440
|
parsed: parsedBody,
|
|
440
441
|
metadata
|
|
441
442
|
};
|
|
442
|
-
const useToken =
|
|
443
|
+
const useToken = parsedBody.token;
|
|
443
444
|
|
|
444
445
|
//Check request params
|
|
445
446
|
const fees = await this.preCheckAmounts(request, requestedAmount, useToken);
|
|
@@ -450,7 +451,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
450
451
|
const abortController = this.getAbortController(responseStream);
|
|
451
452
|
|
|
452
453
|
//Pre-fetch data
|
|
453
|
-
const {pricePrefetchPromise} = this.getFromBtcPricePrefetches(chainIdentifier, useToken, abortController);
|
|
454
|
+
const {pricePrefetchPromise} = this.getFromBtcPricePrefetches(chainIdentifier, useToken, useToken, abortController);
|
|
454
455
|
const balancePrefetch = swapContract.getBalance(signer.getAddress(), useToken, false).catch(e => {
|
|
455
456
|
this.logger.error("getBalancePrefetch(): balancePrefetch error: ", e);
|
|
456
457
|
abortController.abort(e);
|
|
@@ -467,7 +468,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
467
468
|
metadata.times.priceCalculated = Date.now();
|
|
468
469
|
|
|
469
470
|
//Make sure we have MORE THAN ENOUGH to honor the swap request
|
|
470
|
-
await this.checkBalance(totalInToken
|
|
471
|
+
await this.checkBalance(totalInToken * 4n, balancePrefetch, abortController.signal)
|
|
471
472
|
metadata.times.balanceChecked = Date.now();
|
|
472
473
|
|
|
473
474
|
const blockHeight = await this.bitcoin.getBlockheight();
|
|
@@ -496,22 +497,23 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
496
497
|
blockHeight,
|
|
497
498
|
Date.now()+(this.config.swapAddressExpiry*1000),
|
|
498
499
|
recommendedFee,
|
|
499
|
-
refundAddress
|
|
500
|
+
refundAddress,
|
|
501
|
+
useToken
|
|
500
502
|
);
|
|
501
503
|
metadata.times.swapCreated = Date.now();
|
|
502
504
|
createdSwap.metadata = metadata;
|
|
503
505
|
|
|
504
506
|
await PluginManager.swapCreate(createdSwap);
|
|
505
|
-
await this.storageManager.saveData(createdSwap.
|
|
507
|
+
await this.storageManager.saveData(createdSwap.getIdentifierHash(), createdSwap.getSequence(), createdSwap);
|
|
506
508
|
this.subscriptions.set(outputScript, createdSwap);
|
|
507
509
|
|
|
508
510
|
this.swapLogger.info(createdSwap, "REST: /getAddress: Created swap address: "+createdSwap.btcAddress+" amount: "+amountBD.toString(10));
|
|
509
511
|
|
|
510
|
-
|
|
511
|
-
msg: "Success",
|
|
512
|
+
res.status(200).json({
|
|
512
513
|
code: 10000,
|
|
514
|
+
msg: "Success",
|
|
513
515
|
data: {
|
|
514
|
-
paymentHash: createdSwap.
|
|
516
|
+
paymentHash: createdSwap.getIdentifierHash(),
|
|
515
517
|
sequence: createdSwap.getSequence().toString(10),
|
|
516
518
|
btcAddress: receiveAddress,
|
|
517
519
|
amountSats: amountBD.toString(10),
|
|
@@ -524,8 +526,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
524
526
|
}
|
|
525
527
|
});
|
|
526
528
|
});
|
|
527
|
-
restServer.
|
|
528
|
-
restServer.post(this.path+"/getAddress", getAddress);
|
|
529
|
+
restServer.get(this.path+"/getAddress", getAddress);
|
|
529
530
|
|
|
530
531
|
const getInvoiceStatus = expressHandlerWrapper(async (req, res) => {
|
|
531
532
|
/**
|
|
@@ -537,7 +538,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
537
538
|
typeof(val)==="string" &&
|
|
538
539
|
val.length===64 &&
|
|
539
540
|
HEX_REGEX.test(val) ? val: null,
|
|
540
|
-
sequence: FieldTypeEnum.
|
|
541
|
+
sequence: FieldTypeEnum.BigInt,
|
|
541
542
|
});
|
|
542
543
|
if(parsedBody==null) throw {
|
|
543
544
|
code: 20100,
|
|
@@ -658,7 +659,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
658
659
|
typeof(val)==="string" &&
|
|
659
660
|
val.length===64 &&
|
|
660
661
|
HEX_REGEX.test(val) ? val: null,
|
|
661
|
-
sequence: FieldTypeEnum.
|
|
662
|
+
sequence: FieldTypeEnum.BigInt,
|
|
662
663
|
refundAddress: (val: string) => val!=null &&
|
|
663
664
|
typeof(val)==="string" &&
|
|
664
665
|
this.isValidBitcoinAddress(val) ? val : null
|
|
@@ -669,7 +670,7 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
669
670
|
};
|
|
670
671
|
|
|
671
672
|
const invoiceData: FromBtcTrustedSwap = await this.storageManager.getData(parsedBody.paymentHash, null);
|
|
672
|
-
if (invoiceData==null ||
|
|
673
|
+
if (invoiceData==null || invoiceData.getSequence()!==parsedBody.sequence) throw {
|
|
673
674
|
code: 10001,
|
|
674
675
|
msg: "Swap not found"
|
|
675
676
|
};
|
|
@@ -747,15 +748,15 @@ export class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, F
|
|
|
747
748
|
return {};
|
|
748
749
|
}
|
|
749
750
|
|
|
750
|
-
protected processClaimEvent(chainIdentifier: string, event: ClaimEvent<SwapData>): Promise<void> {
|
|
751
|
+
protected processClaimEvent(chainIdentifier: string, swap: FromBtcTrustedSwap, event: ClaimEvent<SwapData>): Promise<void> {
|
|
751
752
|
return Promise.resolve(undefined);
|
|
752
753
|
}
|
|
753
754
|
|
|
754
|
-
protected processInitializeEvent(chainIdentifier: string, event: InitializeEvent<SwapData>): Promise<void> {
|
|
755
|
+
protected processInitializeEvent(chainIdentifier: string, swap: FromBtcTrustedSwap, event: InitializeEvent<SwapData>): Promise<void> {
|
|
755
756
|
return Promise.resolve(undefined);
|
|
756
757
|
}
|
|
757
758
|
|
|
758
|
-
protected processRefundEvent(chainIdentifier: string, event: RefundEvent<SwapData>): Promise<void> {
|
|
759
|
+
protected processRefundEvent(chainIdentifier: string, swap: FromBtcTrustedSwap, event: RefundEvent<SwapData>): Promise<void> {
|
|
759
760
|
return Promise.resolve(undefined);
|
|
760
761
|
}
|
|
761
762
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {BtcTx, SwapData} from "@atomiqlabs/base";
|
|
1
|
+
import {BigIntBufferUtils, BtcTx, SwapData} from "@atomiqlabs/base";
|
|
2
2
|
import {FromBtcBaseSwap} from "../FromBtcBaseSwap";
|
|
3
|
-
import * as BN from "bn.js";
|
|
4
3
|
import {deserializeBN, serializeBN} from "../../utils/Utils";
|
|
5
4
|
import {createHash, randomBytes} from "crypto";
|
|
6
5
|
|
|
@@ -20,19 +19,22 @@ export enum FromBtcTrustedSwapState {
|
|
|
20
19
|
|
|
21
20
|
export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBaseSwap<T, FromBtcTrustedSwapState> {
|
|
22
21
|
|
|
23
|
-
readonly sequence:
|
|
22
|
+
readonly sequence: bigint;
|
|
24
23
|
readonly btcAddress: string;
|
|
25
24
|
|
|
26
25
|
readonly dstAddress: string;
|
|
27
|
-
readonly outputTokens:
|
|
26
|
+
readonly outputTokens: bigint;
|
|
28
27
|
|
|
29
28
|
readonly createdHeight: number;
|
|
30
29
|
readonly expiresAt: number;
|
|
31
30
|
readonly recommendedFee: number;
|
|
31
|
+
|
|
32
|
+
readonly token: string;
|
|
33
|
+
|
|
32
34
|
refundAddress: string;
|
|
33
35
|
|
|
34
|
-
adjustedInput:
|
|
35
|
-
adjustedOutput:
|
|
36
|
+
adjustedInput: bigint;
|
|
37
|
+
adjustedOutput: bigint;
|
|
36
38
|
|
|
37
39
|
doubleSpent: boolean;
|
|
38
40
|
scRawTx: string;
|
|
@@ -48,37 +50,39 @@ export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBa
|
|
|
48
50
|
|
|
49
51
|
constructor(
|
|
50
52
|
chainIdentifier: string,
|
|
51
|
-
swapFee:
|
|
52
|
-
swapFeeInToken:
|
|
53
|
+
swapFee: bigint,
|
|
54
|
+
swapFeeInToken: bigint,
|
|
53
55
|
btcAddress: string,
|
|
54
|
-
inputSats:
|
|
56
|
+
inputSats: bigint,
|
|
55
57
|
dstAddress: string,
|
|
56
|
-
outputTokens:
|
|
58
|
+
outputTokens: bigint,
|
|
57
59
|
createdHeight: number,
|
|
58
60
|
expiresAt: number,
|
|
59
61
|
recommendedFee: number,
|
|
60
|
-
refundAddress: string
|
|
62
|
+
refundAddress: string,
|
|
63
|
+
token: string
|
|
61
64
|
);
|
|
62
65
|
constructor(obj: any);
|
|
63
66
|
|
|
64
67
|
constructor(
|
|
65
68
|
objOrChainIdentifier: any | string,
|
|
66
|
-
swapFee?:
|
|
67
|
-
swapFeeInToken?:
|
|
69
|
+
swapFee?: bigint,
|
|
70
|
+
swapFeeInToken?: bigint,
|
|
68
71
|
btcAddress?: string,
|
|
69
|
-
inputSats?:
|
|
72
|
+
inputSats?: bigint,
|
|
70
73
|
dstAddress?: string,
|
|
71
|
-
outputTokens?:
|
|
74
|
+
outputTokens?: bigint,
|
|
72
75
|
createdHeight?: number,
|
|
73
76
|
expiresAt?: number,
|
|
74
77
|
recommendedFee?: number,
|
|
75
|
-
refundAddress?: string
|
|
78
|
+
refundAddress?: string,
|
|
79
|
+
token?: string
|
|
76
80
|
) {
|
|
77
81
|
if(typeof(objOrChainIdentifier)==="string") {
|
|
78
82
|
super(objOrChainIdentifier, inputSats, swapFee, swapFeeInToken);
|
|
79
83
|
this.state = FromBtcTrustedSwapState.CREATED;
|
|
80
84
|
this.doubleSpent = false;
|
|
81
|
-
this.sequence =
|
|
85
|
+
this.sequence = BigIntBufferUtils.fromBuffer(randomBytes(8));
|
|
82
86
|
this.btcAddress = btcAddress;
|
|
83
87
|
this.dstAddress = dstAddress;
|
|
84
88
|
this.outputTokens = outputTokens;
|
|
@@ -86,6 +90,7 @@ export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBa
|
|
|
86
90
|
this.expiresAt = expiresAt;
|
|
87
91
|
this.recommendedFee = recommendedFee;
|
|
88
92
|
this.refundAddress = refundAddress;
|
|
93
|
+
this.token = token;
|
|
89
94
|
} else {
|
|
90
95
|
super(objOrChainIdentifier);
|
|
91
96
|
this.btcAddress = objOrChainIdentifier.btcAddress;
|
|
@@ -107,6 +112,7 @@ export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBa
|
|
|
107
112
|
this.vout = objOrChainIdentifier.vout;
|
|
108
113
|
this.burnTxId = objOrChainIdentifier.burnTxId;
|
|
109
114
|
this.refundTxId = objOrChainIdentifier.refundTxId;
|
|
115
|
+
this.token = objOrChainIdentifier.token;
|
|
110
116
|
}
|
|
111
117
|
}
|
|
112
118
|
|
|
@@ -131,22 +137,23 @@ export class FromBtcTrustedSwap<T extends SwapData = SwapData> extends FromBtcBa
|
|
|
131
137
|
partialSerialized.vout = this.vout;
|
|
132
138
|
partialSerialized.burnTxId = this.burnTxId;
|
|
133
139
|
partialSerialized.refundTxId = this.refundTxId;
|
|
140
|
+
partialSerialized.token = this.token;
|
|
134
141
|
return partialSerialized;
|
|
135
142
|
}
|
|
136
143
|
|
|
137
|
-
|
|
144
|
+
getClaimHash(): string {
|
|
138
145
|
return createHash("sha256").update(this.btcAddress).digest().toString("hex");
|
|
139
146
|
}
|
|
140
147
|
|
|
141
|
-
getSequence():
|
|
148
|
+
getSequence(): bigint {
|
|
142
149
|
return this.sequence;
|
|
143
150
|
}
|
|
144
151
|
|
|
145
|
-
getOutputAmount():
|
|
152
|
+
getOutputAmount(): bigint {
|
|
146
153
|
return this.adjustedOutput || this.outputTokens;
|
|
147
154
|
}
|
|
148
155
|
|
|
149
|
-
getTotalInputAmount():
|
|
156
|
+
getTotalInputAmount(): bigint {
|
|
150
157
|
return this.adjustedInput || this.amount;
|
|
151
158
|
}
|
|
152
159
|
|