@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
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import * as BN from "bn.js";
|
|
2
1
|
import {Express, Request, Response} from "express";
|
|
3
2
|
import {FromBtcSwapAbs, FromBtcSwapState} from "./FromBtcSwapAbs";
|
|
4
3
|
import {MultichainData, SwapHandlerType} from "../SwapHandler";
|
|
5
4
|
import {ISwapPrice} from "../ISwapPrice";
|
|
6
5
|
import {
|
|
6
|
+
BigIntBufferUtils,
|
|
7
7
|
ChainSwapType,
|
|
8
8
|
ClaimEvent,
|
|
9
9
|
InitializeEvent,
|
|
10
10
|
RefundEvent,
|
|
11
11
|
SwapData
|
|
12
12
|
} from "@atomiqlabs/base";
|
|
13
|
-
import {
|
|
13
|
+
import {randomBytes} from "crypto";
|
|
14
14
|
import {expressHandlerWrapper} from "../../utils/Utils";
|
|
15
15
|
import {PluginManager} from "../../plugins/PluginManager";
|
|
16
16
|
import {IIntermediaryStorage} from "../../storage/IIntermediaryStorage";
|
|
@@ -28,9 +28,9 @@ export type FromBtcConfig = FromBtcBaseConfig & {
|
|
|
28
28
|
|
|
29
29
|
export type FromBtcRequestType = {
|
|
30
30
|
address: string,
|
|
31
|
-
amount:
|
|
31
|
+
amount: bigint,
|
|
32
32
|
token: string,
|
|
33
|
-
sequence:
|
|
33
|
+
sequence: bigint,
|
|
34
34
|
exactOut?: boolean
|
|
35
35
|
};
|
|
36
36
|
|
|
@@ -38,10 +38,10 @@ export type FromBtcRequestType = {
|
|
|
38
38
|
* Swap handler handling from BTC swaps using PTLCs (proof-time locked contracts) and btc relay (on-chain bitcoin SPV)
|
|
39
39
|
*/
|
|
40
40
|
export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSwapState> {
|
|
41
|
-
|
|
42
41
|
readonly type = SwapHandlerType.FROM_BTC;
|
|
42
|
+
readonly swapType = ChainSwapType.CHAIN;
|
|
43
43
|
|
|
44
|
-
readonly config: FromBtcConfig & {swapTsCsvDelta:
|
|
44
|
+
readonly config: FromBtcConfig & {swapTsCsvDelta: bigint};
|
|
45
45
|
|
|
46
46
|
readonly bitcoin: IBitcoinWallet;
|
|
47
47
|
|
|
@@ -57,25 +57,10 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
57
57
|
this.bitcoin = bitcoin;
|
|
58
58
|
this.config = {
|
|
59
59
|
...config,
|
|
60
|
-
swapTsCsvDelta:
|
|
60
|
+
swapTsCsvDelta: BigInt(config.swapCsvDelta) * (config.bitcoinBlocktime / config.safetyFactor)
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
/**
|
|
65
|
-
* Returns the TXO hash of the specific address and amount - sha256(u64le(amount) + outputScript(address))
|
|
66
|
-
*
|
|
67
|
-
* @param address
|
|
68
|
-
* @param amount
|
|
69
|
-
*/
|
|
70
|
-
private getTxoHash(address: string, amount: BN): Buffer {
|
|
71
|
-
const parsedOutputScript = this.bitcoin.toOutputScript(address);
|
|
72
|
-
|
|
73
|
-
return createHash("sha256").update(Buffer.concat([
|
|
74
|
-
Buffer.from(amount.toArray("le", 8)),
|
|
75
|
-
parsedOutputScript
|
|
76
|
-
])).digest();
|
|
77
|
-
}
|
|
78
|
-
|
|
79
64
|
/**
|
|
80
65
|
* Returns the payment hash of the swap, takes swap nonce into account. Payment hash is chain-specific.
|
|
81
66
|
*
|
|
@@ -83,10 +68,10 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
83
68
|
* @param address
|
|
84
69
|
* @param amount
|
|
85
70
|
*/
|
|
86
|
-
private getHash(chainIdentifier: string, address: string, amount:
|
|
71
|
+
private getHash(chainIdentifier: string, address: string, amount: bigint): Buffer {
|
|
87
72
|
const parsedOutputScript = this.bitcoin.toOutputScript(address);
|
|
88
73
|
const {swapContract} = this.getChain(chainIdentifier);
|
|
89
|
-
return swapContract.getHashForOnchain(parsedOutputScript, amount,
|
|
74
|
+
return swapContract.getHashForOnchain(parsedOutputScript, amount, this.config.confirmations, 0n);
|
|
90
75
|
}
|
|
91
76
|
|
|
92
77
|
/**
|
|
@@ -107,7 +92,7 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
107
92
|
if(isCommited) {
|
|
108
93
|
this.swapLogger.info(swap, "processPastSwap(state=CREATED): swap was commited, but processed from watchdog, address: "+swap.address);
|
|
109
94
|
await swap.setState(FromBtcSwapState.COMMITED);
|
|
110
|
-
await this.
|
|
95
|
+
await this.saveSwapData(swap);
|
|
111
96
|
return false;
|
|
112
97
|
}
|
|
113
98
|
|
|
@@ -119,7 +104,7 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
119
104
|
|
|
120
105
|
//Check if commited swap expired by now
|
|
121
106
|
if(swap.state===FromBtcSwapState.COMMITED) {
|
|
122
|
-
if(!swapContract.isExpired(signer.getAddress(), swap.data)) return false;
|
|
107
|
+
if(!await swapContract.isExpired(signer.getAddress(), swap.data)) return false;
|
|
123
108
|
|
|
124
109
|
const isCommited = await swapContract.isCommited(swap.data);
|
|
125
110
|
if(isCommited) {
|
|
@@ -150,7 +135,7 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
150
135
|
|
|
151
136
|
const refundSwaps: FromBtcSwapAbs[] = [];
|
|
152
137
|
|
|
153
|
-
for(let swap of queriedData) {
|
|
138
|
+
for(let {obj: swap} of queriedData) {
|
|
154
139
|
if(await this.processPastSwap(swap)) refundSwaps.push(swap);
|
|
155
140
|
}
|
|
156
141
|
|
|
@@ -177,54 +162,23 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
177
162
|
}
|
|
178
163
|
}
|
|
179
164
|
|
|
180
|
-
protected async processInitializeEvent(chainIdentifier: string, event: InitializeEvent<SwapData>) {
|
|
181
|
-
//Only process on-chain requests
|
|
182
|
-
if (event.swapType !== ChainSwapType.CHAIN) return;
|
|
183
|
-
|
|
184
|
-
const swapData = await event.swapData();
|
|
185
|
-
|
|
186
|
-
const {signer} = this.getChain(chainIdentifier);
|
|
187
|
-
|
|
188
|
-
if (!swapData.isOfferer(signer.getAddress())) return;
|
|
189
|
-
//Only process requests that don't pay in from the program
|
|
190
|
-
if (swapData.isPayIn()) return;
|
|
191
|
-
|
|
192
|
-
const paymentHash = event.paymentHash;
|
|
193
|
-
const savedSwap = await this.storageManager.getData(paymentHash, event.sequence);
|
|
194
|
-
if(savedSwap==null || savedSwap.chainIdentifier!==chainIdentifier) return;
|
|
195
|
-
|
|
196
|
-
savedSwap.txIds.init = (event as any).meta?.txId;
|
|
197
|
-
if(savedSwap.metadata!=null) savedSwap.metadata.times.initTxReceived = Date.now();
|
|
198
|
-
|
|
165
|
+
protected async processInitializeEvent(chainIdentifier: string, savedSwap: FromBtcSwapAbs, event: InitializeEvent<SwapData>) {
|
|
199
166
|
this.swapLogger.info(savedSwap, "SC: InitializeEvent: swap initialized by the client, address: "+savedSwap.address);
|
|
200
167
|
|
|
201
168
|
if(savedSwap.state===FromBtcSwapState.CREATED) {
|
|
202
169
|
await savedSwap.setState(FromBtcSwapState.COMMITED);
|
|
203
|
-
savedSwap
|
|
204
|
-
await this.storageManager.saveData(paymentHash, event.sequence, savedSwap);
|
|
170
|
+
await this.saveSwapData(savedSwap);
|
|
205
171
|
}
|
|
206
172
|
}
|
|
207
173
|
|
|
208
|
-
protected async processClaimEvent(chainIdentifier: string, event: ClaimEvent<SwapData>): Promise<void> {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
const savedSwap = await this.storageManager.getData(paymentHashHex, event.sequence);
|
|
212
|
-
if(savedSwap==null || savedSwap.chainIdentifier!==chainIdentifier) return;
|
|
213
|
-
|
|
214
|
-
savedSwap.txId = Buffer.from(event.secret, "hex").reverse().toString("hex");
|
|
215
|
-
savedSwap.txIds.claim = (event as any).meta?.txId;
|
|
216
|
-
if(savedSwap.metadata!=null) savedSwap.metadata.times.claimTxReceived = Date.now();
|
|
174
|
+
protected async processClaimEvent(chainIdentifier: string, savedSwap: FromBtcSwapAbs, event: ClaimEvent<SwapData>): Promise<void> {
|
|
175
|
+
savedSwap.txId = Buffer.from(event.result, "hex").reverse().toString("hex");
|
|
217
176
|
|
|
218
177
|
this.swapLogger.info(savedSwap, "SC: ClaimEvent: swap successfully claimed by the client, address: "+savedSwap.address);
|
|
219
178
|
await this.removeSwapData(savedSwap, FromBtcSwapState.CLAIMED);
|
|
220
179
|
}
|
|
221
180
|
|
|
222
|
-
protected async processRefundEvent(chainIdentifier: string, event: RefundEvent<SwapData>) {
|
|
223
|
-
if (event.paymentHash == null) return;
|
|
224
|
-
|
|
225
|
-
const savedSwap = await this.storageManager.getData(event.paymentHash, event.sequence);
|
|
226
|
-
if(savedSwap==null || savedSwap.chainIdentifier!==chainIdentifier) return;
|
|
227
|
-
|
|
181
|
+
protected async processRefundEvent(chainIdentifier: string, savedSwap: FromBtcSwapAbs, event: RefundEvent<SwapData>) {
|
|
228
182
|
savedSwap.txIds.refund = (event as any).meta?.txId;
|
|
229
183
|
|
|
230
184
|
this.swapLogger.info(event, "SC: RefundEvent: swap refunded, address: "+savedSwap.address);
|
|
@@ -241,14 +195,14 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
241
195
|
* @throws {DefinedRuntimeError} will throw an error if the plugin cancelled the request
|
|
242
196
|
* @returns {Promise<BN>} resulting claimer bounty to be used with the swap
|
|
243
197
|
*/
|
|
244
|
-
private async getClaimerBounty(req: Request & {paramReader: IParamReader}, expiry:
|
|
198
|
+
private async getClaimerBounty(req: Request & {paramReader: IParamReader}, expiry: bigint, signal: AbortSignal): Promise<bigint> {
|
|
245
199
|
const parsedClaimerBounty = await req.paramReader.getParams({
|
|
246
200
|
claimerBounty: {
|
|
247
|
-
feePerBlock: FieldTypeEnum.
|
|
248
|
-
safetyFactor: FieldTypeEnum.
|
|
249
|
-
startTimestamp: FieldTypeEnum.
|
|
250
|
-
addBlock: FieldTypeEnum.
|
|
251
|
-
addFee: FieldTypeEnum.
|
|
201
|
+
feePerBlock: FieldTypeEnum.BigInt,
|
|
202
|
+
safetyFactor: FieldTypeEnum.BigInt,
|
|
203
|
+
startTimestamp: FieldTypeEnum.BigInt,
|
|
204
|
+
addBlock: FieldTypeEnum.BigInt,
|
|
205
|
+
addFee: FieldTypeEnum.BigInt,
|
|
252
206
|
},
|
|
253
207
|
}).catch(e => null);
|
|
254
208
|
|
|
@@ -261,29 +215,28 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
261
215
|
};
|
|
262
216
|
}
|
|
263
217
|
|
|
264
|
-
const tsDelta = expiry
|
|
265
|
-
const blocksDelta = tsDelta
|
|
266
|
-
const totalBlock = blocksDelta
|
|
267
|
-
return parsedClaimerBounty.claimerBounty.addFee
|
|
218
|
+
const tsDelta: bigint = expiry - parsedClaimerBounty.claimerBounty.startTimestamp;
|
|
219
|
+
const blocksDelta: bigint = tsDelta / this.config.bitcoinBlocktime * parsedClaimerBounty.claimerBounty.safetyFactor;
|
|
220
|
+
const totalBlock: bigint = blocksDelta + parsedClaimerBounty.claimerBounty.addBlock;
|
|
221
|
+
return parsedClaimerBounty.claimerBounty.addFee + (totalBlock * parsedClaimerBounty.claimerBounty.feePerBlock);
|
|
268
222
|
}
|
|
269
223
|
|
|
270
224
|
private getDummySwapData(chainIdentifier: string, useToken: string, address: string): Promise<SwapData> {
|
|
271
225
|
const {swapContract, signer} = this.getChain(chainIdentifier);
|
|
226
|
+
const dummyAmount = BigInt(Math.floor(Math.random() * 0x1000000));
|
|
272
227
|
return swapContract.createSwapData(
|
|
273
228
|
ChainSwapType.CHAIN,
|
|
274
229
|
signer.getAddress(),
|
|
275
230
|
address,
|
|
276
231
|
useToken,
|
|
277
|
-
|
|
278
|
-
null,
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
new BN(0),
|
|
282
|
-
this.config.confirmations,
|
|
232
|
+
dummyAmount,
|
|
233
|
+
swapContract.getHashForOnchain(randomBytes(32), dummyAmount, 3, null).toString("hex"),
|
|
234
|
+
BigIntBufferUtils.fromBuffer(randomBytes(8)),
|
|
235
|
+
BigInt(Math.floor(Date.now()/1000)) + this.config.swapTsCsvDelta,
|
|
283
236
|
false,
|
|
284
237
|
true,
|
|
285
|
-
|
|
286
|
-
|
|
238
|
+
BigInt(Math.floor(Math.random() * 0x10000)),
|
|
239
|
+
BigInt(Math.floor(Math.random() * 0x10000))
|
|
287
240
|
);
|
|
288
241
|
}
|
|
289
242
|
|
|
@@ -303,6 +256,8 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
303
256
|
|
|
304
257
|
const chainIdentifier = req.query.chain as string ?? this.chains.default;
|
|
305
258
|
const {swapContract, signer} = this.getChain(chainIdentifier);
|
|
259
|
+
const depositToken = req.query.depositToken as string ?? swapContract.getNativeCurrencyAddress();
|
|
260
|
+
this.checkAllowedDepositToken(chainIdentifier, depositToken);
|
|
306
261
|
|
|
307
262
|
metadata.times.requestReceived = Date.now();
|
|
308
263
|
/**
|
|
@@ -325,11 +280,11 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
325
280
|
address: (val: string) => val!=null &&
|
|
326
281
|
typeof(val)==="string" &&
|
|
327
282
|
swapContract.isValidAddress(val) ? val : null,
|
|
328
|
-
amount: FieldTypeEnum.
|
|
283
|
+
amount: FieldTypeEnum.BigInt,
|
|
329
284
|
token: (val: string) => val!=null &&
|
|
330
285
|
typeof(val)==="string" &&
|
|
331
286
|
this.isTokenSupported(chainIdentifier, val) ? val : null,
|
|
332
|
-
sequence: FieldTypeEnum.
|
|
287
|
+
sequence: FieldTypeEnum.BigInt,
|
|
333
288
|
exactOut: FieldTypeEnum.BooleanOptional
|
|
334
289
|
});
|
|
335
290
|
if(parsedBody==null) throw {
|
|
@@ -357,13 +312,21 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
357
312
|
const abortController = this.getAbortController(responseStream);
|
|
358
313
|
|
|
359
314
|
//Pre-fetch data
|
|
360
|
-
const {
|
|
361
|
-
|
|
315
|
+
const {
|
|
316
|
+
pricePrefetchPromise,
|
|
317
|
+
gasTokenPricePrefetchPromise,
|
|
318
|
+
depositTokenPricePrefetchPromise
|
|
319
|
+
} = this.getFromBtcPricePrefetches(chainIdentifier, useToken, depositToken, abortController);
|
|
320
|
+
const balancePrefetch: Promise<bigint> = this.getBalancePrefetch(chainIdentifier, useToken, abortController);
|
|
362
321
|
const signDataPrefetchPromise: Promise<any> = this.getSignDataPrefetch(chainIdentifier, abortController, responseStream);
|
|
363
322
|
|
|
364
323
|
const dummySwapData = await this.getDummySwapData(chainIdentifier, useToken, parsedBody.address);
|
|
365
324
|
abortController.signal.throwIfAborted();
|
|
366
|
-
const baseSDPromise: Promise<
|
|
325
|
+
const baseSDPromise: Promise<bigint> = this.getBaseSecurityDepositPrefetch(
|
|
326
|
+
chainIdentifier, dummySwapData, depositToken,
|
|
327
|
+
gasTokenPricePrefetchPromise, depositTokenPricePrefetchPromise,
|
|
328
|
+
abortController
|
|
329
|
+
);
|
|
367
330
|
|
|
368
331
|
//Check valid amount specified (min/max)
|
|
369
332
|
const {
|
|
@@ -384,14 +347,14 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
384
347
|
metadata.times.addressCreated = Date.now();
|
|
385
348
|
|
|
386
349
|
const paymentHash = this.getHash(chainIdentifier, receiveAddress, amountBD);
|
|
387
|
-
const currentTimestamp =
|
|
350
|
+
const currentTimestamp = BigInt(Math.floor(Date.now()/1000));
|
|
388
351
|
const expiryTimeout = this.config.swapTsCsvDelta;
|
|
389
|
-
const expiry = currentTimestamp
|
|
352
|
+
const expiry = currentTimestamp + expiryTimeout;
|
|
390
353
|
|
|
391
354
|
//Calculate security deposit
|
|
392
355
|
const totalSecurityDeposit = await this.getSecurityDeposit(
|
|
393
356
|
chainIdentifier, amountBD, swapFee, expiryTimeout,
|
|
394
|
-
baseSDPromise,
|
|
357
|
+
baseSDPromise, depositToken, depositTokenPricePrefetchPromise,
|
|
395
358
|
abortController.signal, metadata
|
|
396
359
|
);
|
|
397
360
|
metadata.times.securityDepositCalculated = Date.now();
|
|
@@ -410,14 +373,17 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
410
373
|
paymentHash.toString("hex"),
|
|
411
374
|
parsedBody.sequence,
|
|
412
375
|
expiry,
|
|
413
|
-
new BN(0),
|
|
414
|
-
this.config.confirmations,
|
|
415
376
|
false,
|
|
416
377
|
true,
|
|
417
378
|
totalSecurityDeposit,
|
|
418
|
-
totalClaimerBounty
|
|
379
|
+
totalClaimerBounty,
|
|
380
|
+
depositToken
|
|
419
381
|
);
|
|
420
|
-
data.
|
|
382
|
+
data.setExtraData(swapContract.getExtraData(
|
|
383
|
+
this.bitcoin.toOutputScript(receiveAddress),
|
|
384
|
+
amountBD,
|
|
385
|
+
this.config.confirmations
|
|
386
|
+
).toString("hex"));
|
|
421
387
|
abortController.signal.throwIfAborted();
|
|
422
388
|
metadata.times.swapCreated = Date.now();
|
|
423
389
|
|
|
@@ -425,7 +391,7 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
425
391
|
const sigData = await this.getFromBtcSignatureData(chainIdentifier, data, req, abortController.signal, signDataPrefetchPromise);
|
|
426
392
|
metadata.times.swapSigned = Date.now();
|
|
427
393
|
|
|
428
|
-
const createdSwap: FromBtcSwapAbs = new FromBtcSwapAbs(chainIdentifier, receiveAddress, amountBD, swapFee, swapFeeInToken);
|
|
394
|
+
const createdSwap: FromBtcSwapAbs = new FromBtcSwapAbs(chainIdentifier, receiveAddress, this.config.confirmations, amountBD, swapFee, swapFeeInToken);
|
|
429
395
|
createdSwap.data = data;
|
|
430
396
|
createdSwap.metadata = metadata;
|
|
431
397
|
createdSwap.prefix = sigData.prefix;
|
|
@@ -434,7 +400,7 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
434
400
|
createdSwap.feeRate = sigData.feeRate;
|
|
435
401
|
|
|
436
402
|
await PluginManager.swapCreate(createdSwap);
|
|
437
|
-
await this.
|
|
403
|
+
await this.saveSwapData(createdSwap);
|
|
438
404
|
|
|
439
405
|
this.swapLogger.info(createdSwap, "REST: /getAddress: Created swap address: "+receiveAddress+" amount: "+amountBD.toString(10));
|
|
440
406
|
|
|
@@ -447,6 +413,7 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
447
413
|
address: signer.getAddress(),
|
|
448
414
|
swapFee: swapFeeInToken.toString(10),
|
|
449
415
|
total: totalInToken.toString(10),
|
|
416
|
+
confirmations: this.config.confirmations,
|
|
450
417
|
data: data.serialize(),
|
|
451
418
|
prefix: sigData.prefix,
|
|
452
419
|
timeout: sigData.timeout,
|
|
@@ -463,7 +430,7 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
463
430
|
* Initializes swap handler, loads data and subscribes to chain events
|
|
464
431
|
*/
|
|
465
432
|
async init() {
|
|
466
|
-
await this.
|
|
433
|
+
await this.loadData(FromBtcSwapAbs);
|
|
467
434
|
this.subscribeToEvents();
|
|
468
435
|
await PluginManager.serviceInitialize(this);
|
|
469
436
|
}
|
|
@@ -473,7 +440,7 @@ export class FromBtcAbs extends FromBtcBaseSwapHandler<FromBtcSwapAbs, FromBtcSw
|
|
|
473
440
|
confirmations: this.config.confirmations,
|
|
474
441
|
|
|
475
442
|
cltv: this.config.swapCsvDelta,
|
|
476
|
-
timestampCltv: this.config.swapTsCsvDelta
|
|
443
|
+
timestampCltv: Number(this.config.swapTsCsvDelta)
|
|
477
444
|
};
|
|
478
445
|
}
|
|
479
446
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as BN from "bn.js";
|
|
2
1
|
import {SwapData} from "@atomiqlabs/base";
|
|
3
2
|
import {SwapHandlerType} from "../SwapHandler";
|
|
4
3
|
import {FromBtcBaseSwap} from "../FromBtcBaseSwap";
|
|
@@ -14,19 +13,22 @@ export enum FromBtcSwapState {
|
|
|
14
13
|
export class FromBtcSwapAbs<T extends SwapData = SwapData> extends FromBtcBaseSwap<T, FromBtcSwapState> {
|
|
15
14
|
|
|
16
15
|
readonly address: string;
|
|
16
|
+
readonly confirmations: number;
|
|
17
17
|
txId: string;
|
|
18
18
|
|
|
19
|
-
constructor(chainIdentifier: string, address: string, amount:
|
|
19
|
+
constructor(chainIdentifier: string, address: string, confirmations: number, amount: bigint, swapFee: bigint, swapFeeInToken: bigint);
|
|
20
20
|
constructor(obj: any);
|
|
21
21
|
|
|
22
|
-
constructor(prOrObj: string | any, address?: string, amount?:
|
|
22
|
+
constructor(prOrObj: string | any, address?: string, confirmations?: number, amount?: bigint, swapFee?: bigint, swapFeeInToken?: bigint) {
|
|
23
23
|
if(typeof(prOrObj)==="string") {
|
|
24
24
|
super(prOrObj, amount, swapFee, swapFeeInToken);
|
|
25
25
|
this.state = FromBtcSwapState.CREATED;
|
|
26
26
|
this.address = address;
|
|
27
|
+
this.confirmations = confirmations;
|
|
27
28
|
} else {
|
|
28
29
|
super(prOrObj);
|
|
29
30
|
this.address = prOrObj.address;
|
|
31
|
+
this.confirmations = prOrObj.confirmations;
|
|
30
32
|
this.txId = prOrObj.txId;
|
|
31
33
|
}
|
|
32
34
|
this.type = SwapHandlerType.FROM_BTC;
|
|
@@ -35,14 +37,11 @@ export class FromBtcSwapAbs<T extends SwapData = SwapData> extends FromBtcBaseSw
|
|
|
35
37
|
serialize(): any {
|
|
36
38
|
const partialSerialized = super.serialize();
|
|
37
39
|
partialSerialized.address = this.address;
|
|
40
|
+
partialSerialized.confirmations = this.confirmations;
|
|
38
41
|
partialSerialized.txId = this.txId;
|
|
39
42
|
return partialSerialized;
|
|
40
43
|
}
|
|
41
44
|
|
|
42
|
-
getTxoHash(): Buffer {
|
|
43
|
-
return Buffer.from(this.data.getTxoHash(), "hex");
|
|
44
|
-
}
|
|
45
|
-
|
|
46
45
|
isInitiated(): boolean {
|
|
47
46
|
return this.state!==FromBtcSwapState.CREATED;
|
|
48
47
|
}
|
|
@@ -55,7 +54,7 @@ export class FromBtcSwapAbs<T extends SwapData = SwapData> extends FromBtcBaseSw
|
|
|
55
54
|
return this.state===FromBtcSwapState.CLAIMED;
|
|
56
55
|
}
|
|
57
56
|
|
|
58
|
-
getTotalInputAmount():
|
|
57
|
+
getTotalInputAmount(): bigint {
|
|
59
58
|
return this.amount;
|
|
60
59
|
}
|
|
61
60
|
|