@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,6 @@
|
|
|
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.FromBtcAbs = void 0;
|
|
13
|
-
const BN = require("bn.js");
|
|
14
4
|
const FromBtcSwapAbs_1 = require("./FromBtcSwapAbs");
|
|
15
5
|
const SwapHandler_1 = require("../SwapHandler");
|
|
16
6
|
const base_1 = require("@atomiqlabs/base");
|
|
@@ -27,21 +17,12 @@ class FromBtcAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
27
17
|
constructor(storageDirectory, path, chains, bitcoin, swapPricing, config) {
|
|
28
18
|
super(storageDirectory, path, chains, swapPricing);
|
|
29
19
|
this.type = SwapHandler_1.SwapHandlerType.FROM_BTC;
|
|
20
|
+
this.swapType = base_1.ChainSwapType.CHAIN;
|
|
30
21
|
this.bitcoin = bitcoin;
|
|
31
|
-
this.config =
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*
|
|
36
|
-
* @param address
|
|
37
|
-
* @param amount
|
|
38
|
-
*/
|
|
39
|
-
getTxoHash(address, amount) {
|
|
40
|
-
const parsedOutputScript = this.bitcoin.toOutputScript(address);
|
|
41
|
-
return (0, crypto_1.createHash)("sha256").update(Buffer.concat([
|
|
42
|
-
Buffer.from(amount.toArray("le", 8)),
|
|
43
|
-
parsedOutputScript
|
|
44
|
-
])).digest();
|
|
22
|
+
this.config = {
|
|
23
|
+
...config,
|
|
24
|
+
swapTsCsvDelta: BigInt(config.swapCsvDelta) * (config.bitcoinBlocktime / config.safetyFactor)
|
|
25
|
+
};
|
|
45
26
|
}
|
|
46
27
|
/**
|
|
47
28
|
* Returns the payment hash of the swap, takes swap nonce into account. Payment hash is chain-specific.
|
|
@@ -53,7 +34,7 @@ class FromBtcAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
53
34
|
getHash(chainIdentifier, address, amount) {
|
|
54
35
|
const parsedOutputScript = this.bitcoin.toOutputScript(address);
|
|
55
36
|
const { swapContract } = this.getChain(chainIdentifier);
|
|
56
|
-
return swapContract.getHashForOnchain(parsedOutputScript, amount,
|
|
37
|
+
return swapContract.getHashForOnchain(parsedOutputScript, amount, this.config.confirmations, 0n);
|
|
57
38
|
}
|
|
58
39
|
/**
|
|
59
40
|
* Processes past swap
|
|
@@ -62,61 +43,57 @@ class FromBtcAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
62
43
|
* @protected
|
|
63
44
|
* @returns true if the swap should be refunded, false if nothing should be done
|
|
64
45
|
*/
|
|
65
|
-
processPastSwap(swap) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (swap.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
yield this.storageManager.saveData(swap.getHash(), swap.getSequence(), swap);
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
this.swapLogger.info(swap, "processPastSwap(state=CREATED): removing past swap due to authorization expiry, address: " + swap.address);
|
|
80
|
-
yield this.bitcoin.addUnusedAddress(swap.address);
|
|
81
|
-
yield this.removeSwapData(swap, FromBtcSwapAbs_1.FromBtcSwapState.CANCELED);
|
|
46
|
+
async processPastSwap(swap) {
|
|
47
|
+
const { swapContract, signer } = this.getChain(swap.chainIdentifier);
|
|
48
|
+
//Once authorization expires in CREATED state, the user can no more commit it on-chain
|
|
49
|
+
if (swap.state === FromBtcSwapAbs_1.FromBtcSwapState.CREATED) {
|
|
50
|
+
if (!await swapContract.isInitAuthorizationExpired(swap.data, swap))
|
|
51
|
+
return false;
|
|
52
|
+
const isCommited = await swapContract.isCommited(swap.data);
|
|
53
|
+
if (isCommited) {
|
|
54
|
+
this.swapLogger.info(swap, "processPastSwap(state=CREATED): swap was commited, but processed from watchdog, address: " + swap.address);
|
|
55
|
+
await swap.setState(FromBtcSwapAbs_1.FromBtcSwapState.COMMITED);
|
|
56
|
+
await this.saveSwapData(swap);
|
|
82
57
|
return false;
|
|
83
58
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
this.swapLogger.warn(swap, "processPastSwap(state=COMMITED): commited swap expired and not committed anymore (already refunded?), address: " + swap.address);
|
|
94
|
-
yield this.removeSwapData(swap, FromBtcSwapAbs_1.FromBtcSwapState.CANCELED);
|
|
59
|
+
this.swapLogger.info(swap, "processPastSwap(state=CREATED): removing past swap due to authorization expiry, address: " + swap.address);
|
|
60
|
+
await this.bitcoin.addUnusedAddress(swap.address);
|
|
61
|
+
await this.removeSwapData(swap, FromBtcSwapAbs_1.FromBtcSwapState.CANCELED);
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
//Check if commited swap expired by now
|
|
65
|
+
if (swap.state === FromBtcSwapAbs_1.FromBtcSwapState.COMMITED) {
|
|
66
|
+
if (!await swapContract.isExpired(signer.getAddress(), swap.data))
|
|
95
67
|
return false;
|
|
68
|
+
const isCommited = await swapContract.isCommited(swap.data);
|
|
69
|
+
if (isCommited) {
|
|
70
|
+
this.swapLogger.info(swap, "processPastSwap(state=COMMITED): swap expired, will refund, address: " + swap.address);
|
|
71
|
+
return true;
|
|
96
72
|
}
|
|
97
|
-
|
|
73
|
+
this.swapLogger.warn(swap, "processPastSwap(state=COMMITED): commited swap expired and not committed anymore (already refunded?), address: " + swap.address);
|
|
74
|
+
await this.removeSwapData(swap, FromBtcSwapAbs_1.FromBtcSwapState.CANCELED);
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
98
77
|
}
|
|
99
78
|
/**
|
|
100
79
|
* Checks past swaps, refunds and deletes ones that are already expired.
|
|
101
80
|
*/
|
|
102
|
-
processPastSwaps() {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
]
|
|
111
|
-
}
|
|
112
|
-
]);
|
|
113
|
-
const refundSwaps = [];
|
|
114
|
-
for (let swap of queriedData) {
|
|
115
|
-
if (yield this.processPastSwap(swap))
|
|
116
|
-
refundSwaps.push(swap);
|
|
81
|
+
async processPastSwaps() {
|
|
82
|
+
const queriedData = await this.storageManager.query([
|
|
83
|
+
{
|
|
84
|
+
key: "state",
|
|
85
|
+
value: [
|
|
86
|
+
FromBtcSwapAbs_1.FromBtcSwapState.CREATED,
|
|
87
|
+
FromBtcSwapAbs_1.FromBtcSwapState.COMMITED
|
|
88
|
+
]
|
|
117
89
|
}
|
|
118
|
-
|
|
119
|
-
|
|
90
|
+
]);
|
|
91
|
+
const refundSwaps = [];
|
|
92
|
+
for (let { obj: swap } of queriedData) {
|
|
93
|
+
if (await this.processPastSwap(swap))
|
|
94
|
+
refundSwaps.push(swap);
|
|
95
|
+
}
|
|
96
|
+
await this.refundSwaps(refundSwaps);
|
|
120
97
|
}
|
|
121
98
|
/**
|
|
122
99
|
* Refunds all swaps (calls SC on-chain refund function)
|
|
@@ -124,78 +101,37 @@ class FromBtcAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
124
101
|
* @param refundSwaps
|
|
125
102
|
* @protected
|
|
126
103
|
*/
|
|
127
|
-
refundSwaps(refundSwaps) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
});
|
|
104
|
+
async refundSwaps(refundSwaps) {
|
|
105
|
+
for (let refundSwap of refundSwaps) {
|
|
106
|
+
const { swapContract, signer } = this.getChain(refundSwap.chainIdentifier);
|
|
107
|
+
const unlock = refundSwap.lock(swapContract.refundTimeout);
|
|
108
|
+
if (unlock == null)
|
|
109
|
+
continue;
|
|
110
|
+
this.swapLogger.debug(refundSwap, "refundSwaps(): initiate refund of swap");
|
|
111
|
+
await swapContract.refund(signer, refundSwap.data, true, false, { waitForConfirmation: true });
|
|
112
|
+
this.swapLogger.info(refundSwap, "refundSwaps(): swap refunded, address: " + refundSwap.address);
|
|
113
|
+
//The swap should be removed by the event handler
|
|
114
|
+
await refundSwap.setState(FromBtcSwapAbs_1.FromBtcSwapState.REFUNDED);
|
|
115
|
+
unlock();
|
|
116
|
+
}
|
|
142
117
|
}
|
|
143
|
-
processInitializeEvent(chainIdentifier, event) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const swapData = yield event.swapData();
|
|
150
|
-
const { signer } = this.getChain(chainIdentifier);
|
|
151
|
-
if (!swapData.isOfferer(signer.getAddress()))
|
|
152
|
-
return;
|
|
153
|
-
//Only process requests that don't pay in from the program
|
|
154
|
-
if (swapData.isPayIn())
|
|
155
|
-
return;
|
|
156
|
-
const paymentHash = event.paymentHash;
|
|
157
|
-
const savedSwap = yield this.storageManager.getData(paymentHash, event.sequence);
|
|
158
|
-
if (savedSwap == null || savedSwap.chainIdentifier !== chainIdentifier)
|
|
159
|
-
return;
|
|
160
|
-
savedSwap.txIds.init = (_a = event.meta) === null || _a === void 0 ? void 0 : _a.txId;
|
|
161
|
-
if (savedSwap.metadata != null)
|
|
162
|
-
savedSwap.metadata.times.initTxReceived = Date.now();
|
|
163
|
-
this.swapLogger.info(savedSwap, "SC: InitializeEvent: swap initialized by the client, address: " + savedSwap.address);
|
|
164
|
-
if (savedSwap.state === FromBtcSwapAbs_1.FromBtcSwapState.CREATED) {
|
|
165
|
-
yield savedSwap.setState(FromBtcSwapAbs_1.FromBtcSwapState.COMMITED);
|
|
166
|
-
savedSwap.data = swapData;
|
|
167
|
-
yield this.storageManager.saveData(paymentHash, event.sequence, savedSwap);
|
|
168
|
-
}
|
|
169
|
-
});
|
|
118
|
+
async processInitializeEvent(chainIdentifier, savedSwap, event) {
|
|
119
|
+
this.swapLogger.info(savedSwap, "SC: InitializeEvent: swap initialized by the client, address: " + savedSwap.address);
|
|
120
|
+
if (savedSwap.state === FromBtcSwapAbs_1.FromBtcSwapState.CREATED) {
|
|
121
|
+
await savedSwap.setState(FromBtcSwapAbs_1.FromBtcSwapState.COMMITED);
|
|
122
|
+
await this.saveSwapData(savedSwap);
|
|
123
|
+
}
|
|
170
124
|
}
|
|
171
|
-
processClaimEvent(chainIdentifier, event) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
const savedSwap = yield this.storageManager.getData(paymentHashHex, event.sequence);
|
|
176
|
-
if (savedSwap == null || savedSwap.chainIdentifier !== chainIdentifier)
|
|
177
|
-
return;
|
|
178
|
-
savedSwap.txId = Buffer.from(event.secret, "hex").reverse().toString("hex");
|
|
179
|
-
savedSwap.txIds.claim = (_a = event.meta) === null || _a === void 0 ? void 0 : _a.txId;
|
|
180
|
-
if (savedSwap.metadata != null)
|
|
181
|
-
savedSwap.metadata.times.claimTxReceived = Date.now();
|
|
182
|
-
this.swapLogger.info(savedSwap, "SC: ClaimEvent: swap successfully claimed by the client, address: " + savedSwap.address);
|
|
183
|
-
yield this.removeSwapData(savedSwap, FromBtcSwapAbs_1.FromBtcSwapState.CLAIMED);
|
|
184
|
-
});
|
|
125
|
+
async processClaimEvent(chainIdentifier, savedSwap, event) {
|
|
126
|
+
savedSwap.txId = Buffer.from(event.result, "hex").reverse().toString("hex");
|
|
127
|
+
this.swapLogger.info(savedSwap, "SC: ClaimEvent: swap successfully claimed by the client, address: " + savedSwap.address);
|
|
128
|
+
await this.removeSwapData(savedSwap, FromBtcSwapAbs_1.FromBtcSwapState.CLAIMED);
|
|
185
129
|
}
|
|
186
|
-
processRefundEvent(chainIdentifier, event) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
const savedSwap = yield this.storageManager.getData(event.paymentHash, event.sequence);
|
|
192
|
-
if (savedSwap == null || savedSwap.chainIdentifier !== chainIdentifier)
|
|
193
|
-
return;
|
|
194
|
-
savedSwap.txIds.refund = (_a = event.meta) === null || _a === void 0 ? void 0 : _a.txId;
|
|
195
|
-
this.swapLogger.info(event, "SC: RefundEvent: swap refunded, address: " + savedSwap.address);
|
|
196
|
-
yield this.bitcoin.addUnusedAddress(savedSwap.address);
|
|
197
|
-
yield this.removeSwapData(savedSwap, FromBtcSwapAbs_1.FromBtcSwapState.REFUNDED);
|
|
198
|
-
});
|
|
130
|
+
async processRefundEvent(chainIdentifier, savedSwap, event) {
|
|
131
|
+
savedSwap.txIds.refund = event.meta?.txId;
|
|
132
|
+
this.swapLogger.info(event, "SC: RefundEvent: swap refunded, address: " + savedSwap.address);
|
|
133
|
+
await this.bitcoin.addUnusedAddress(savedSwap.address);
|
|
134
|
+
await this.removeSwapData(savedSwap, FromBtcSwapAbs_1.FromBtcSwapState.REFUNDED);
|
|
199
135
|
}
|
|
200
136
|
/**
|
|
201
137
|
* Calculates the requested claimer bounty, based on client's request
|
|
@@ -206,33 +142,32 @@ class FromBtcAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
206
142
|
* @throws {DefinedRuntimeError} will throw an error if the plugin cancelled the request
|
|
207
143
|
* @returns {Promise<BN>} resulting claimer bounty to be used with the swap
|
|
208
144
|
*/
|
|
209
|
-
getClaimerBounty(req, expiry, signal) {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
return parsedClaimerBounty.claimerBounty.addFee.add(totalBlock.mul(parsedClaimerBounty.claimerBounty.feePerBlock));
|
|
231
|
-
});
|
|
145
|
+
async getClaimerBounty(req, expiry, signal) {
|
|
146
|
+
const parsedClaimerBounty = await req.paramReader.getParams({
|
|
147
|
+
claimerBounty: {
|
|
148
|
+
feePerBlock: SchemaVerifier_1.FieldTypeEnum.BigInt,
|
|
149
|
+
safetyFactor: SchemaVerifier_1.FieldTypeEnum.BigInt,
|
|
150
|
+
startTimestamp: SchemaVerifier_1.FieldTypeEnum.BigInt,
|
|
151
|
+
addBlock: SchemaVerifier_1.FieldTypeEnum.BigInt,
|
|
152
|
+
addFee: SchemaVerifier_1.FieldTypeEnum.BigInt,
|
|
153
|
+
},
|
|
154
|
+
}).catch(e => null);
|
|
155
|
+
signal.throwIfAborted();
|
|
156
|
+
if (parsedClaimerBounty == null || parsedClaimerBounty.claimerBounty == null) {
|
|
157
|
+
throw {
|
|
158
|
+
code: 20043,
|
|
159
|
+
msg: "Invalid claimerBounty"
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
const tsDelta = expiry - parsedClaimerBounty.claimerBounty.startTimestamp;
|
|
163
|
+
const blocksDelta = tsDelta / this.config.bitcoinBlocktime * parsedClaimerBounty.claimerBounty.safetyFactor;
|
|
164
|
+
const totalBlock = blocksDelta + parsedClaimerBounty.claimerBounty.addBlock;
|
|
165
|
+
return parsedClaimerBounty.claimerBounty.addFee + (totalBlock * parsedClaimerBounty.claimerBounty.feePerBlock);
|
|
232
166
|
}
|
|
233
167
|
getDummySwapData(chainIdentifier, useToken, address) {
|
|
234
168
|
const { swapContract, signer } = this.getChain(chainIdentifier);
|
|
235
|
-
|
|
169
|
+
const dummyAmount = BigInt(Math.floor(Math.random() * 0x1000000));
|
|
170
|
+
return swapContract.createSwapData(base_1.ChainSwapType.CHAIN, signer.getAddress(), address, useToken, dummyAmount, swapContract.getHashForOnchain((0, crypto_1.randomBytes)(32), dummyAmount, 3, null).toString("hex"), base_1.BigIntBufferUtils.fromBuffer((0, crypto_1.randomBytes)(8)), BigInt(Math.floor(Date.now() / 1000)) + this.config.swapTsCsvDelta, false, true, BigInt(Math.floor(Math.random() * 0x10000)), BigInt(Math.floor(Math.random() * 0x10000)));
|
|
236
171
|
}
|
|
237
172
|
/**
|
|
238
173
|
* Sets up required listeners for the REST server
|
|
@@ -241,11 +176,12 @@ class FromBtcAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
241
176
|
*/
|
|
242
177
|
startRestServer(restServer) {
|
|
243
178
|
restServer.use(this.path + "/getAddress", (0, ServerParamDecoder_1.serverParamDecoder)(10 * 1000));
|
|
244
|
-
restServer.post(this.path + "/getAddress", (0, Utils_1.expressHandlerWrapper)((req, res) =>
|
|
245
|
-
var _a;
|
|
179
|
+
restServer.post(this.path + "/getAddress", (0, Utils_1.expressHandlerWrapper)(async (req, res) => {
|
|
246
180
|
const metadata = { request: {}, times: {} };
|
|
247
|
-
const chainIdentifier =
|
|
181
|
+
const chainIdentifier = req.query.chain ?? this.chains.default;
|
|
248
182
|
const { swapContract, signer } = this.getChain(chainIdentifier);
|
|
183
|
+
const depositToken = req.query.depositToken ?? swapContract.getNativeCurrencyAddress();
|
|
184
|
+
this.checkAllowedDepositToken(chainIdentifier, depositToken);
|
|
249
185
|
metadata.times.requestReceived = Date.now();
|
|
250
186
|
/**
|
|
251
187
|
* address: string solana address of the recipient
|
|
@@ -263,15 +199,15 @@ class FromBtcAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
263
199
|
* - addFee: string Additional fee to add to the final claimer bounty
|
|
264
200
|
* feeRate: string Fee rate to be used for init signature
|
|
265
201
|
*/
|
|
266
|
-
const parsedBody =
|
|
202
|
+
const parsedBody = await req.paramReader.getParams({
|
|
267
203
|
address: (val) => val != null &&
|
|
268
204
|
typeof (val) === "string" &&
|
|
269
205
|
swapContract.isValidAddress(val) ? val : null,
|
|
270
|
-
amount: SchemaVerifier_1.FieldTypeEnum.
|
|
206
|
+
amount: SchemaVerifier_1.FieldTypeEnum.BigInt,
|
|
271
207
|
token: (val) => val != null &&
|
|
272
208
|
typeof (val) === "string" &&
|
|
273
209
|
this.isTokenSupported(chainIdentifier, val) ? val : null,
|
|
274
|
-
sequence: SchemaVerifier_1.FieldTypeEnum.
|
|
210
|
+
sequence: SchemaVerifier_1.FieldTypeEnum.BigInt,
|
|
275
211
|
exactOut: SchemaVerifier_1.FieldTypeEnum.BooleanOptional
|
|
276
212
|
});
|
|
277
213
|
if (parsedBody == null)
|
|
@@ -290,57 +226,57 @@ class FromBtcAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
290
226
|
const useToken = parsedBody.token;
|
|
291
227
|
//Check request params
|
|
292
228
|
this.checkSequence(parsedBody.sequence);
|
|
293
|
-
const fees =
|
|
229
|
+
const fees = await this.preCheckAmounts(request, requestedAmount, useToken);
|
|
294
230
|
metadata.times.requestChecked = Date.now();
|
|
295
231
|
//Create abortController for parallel prefetches
|
|
296
232
|
const responseStream = res.responseStream;
|
|
297
233
|
const abortController = this.getAbortController(responseStream);
|
|
298
234
|
//Pre-fetch data
|
|
299
|
-
const { pricePrefetchPromise,
|
|
235
|
+
const { pricePrefetchPromise, gasTokenPricePrefetchPromise, depositTokenPricePrefetchPromise } = this.getFromBtcPricePrefetches(chainIdentifier, useToken, depositToken, abortController);
|
|
300
236
|
const balancePrefetch = this.getBalancePrefetch(chainIdentifier, useToken, abortController);
|
|
301
237
|
const signDataPrefetchPromise = this.getSignDataPrefetch(chainIdentifier, abortController, responseStream);
|
|
302
|
-
const dummySwapData =
|
|
238
|
+
const dummySwapData = await this.getDummySwapData(chainIdentifier, useToken, parsedBody.address);
|
|
303
239
|
abortController.signal.throwIfAborted();
|
|
304
|
-
const baseSDPromise = this.getBaseSecurityDepositPrefetch(chainIdentifier, dummySwapData, abortController);
|
|
240
|
+
const baseSDPromise = this.getBaseSecurityDepositPrefetch(chainIdentifier, dummySwapData, depositToken, gasTokenPricePrefetchPromise, depositTokenPricePrefetchPromise, abortController);
|
|
305
241
|
//Check valid amount specified (min/max)
|
|
306
|
-
const { amountBD, swapFee, swapFeeInToken, totalInToken } =
|
|
242
|
+
const { amountBD, swapFee, swapFeeInToken, totalInToken } = await this.checkFromBtcAmount(request, requestedAmount, fees, useToken, abortController.signal, pricePrefetchPromise);
|
|
307
243
|
metadata.times.priceCalculated = Date.now();
|
|
308
244
|
//Check if we have enough funds to honor the request
|
|
309
|
-
|
|
245
|
+
await this.checkBalance(totalInToken, balancePrefetch, abortController.signal);
|
|
310
246
|
metadata.times.balanceChecked = Date.now();
|
|
311
247
|
//Create swap receive bitcoin address
|
|
312
|
-
const receiveAddress =
|
|
248
|
+
const receiveAddress = await this.bitcoin.getAddress();
|
|
313
249
|
abortController.signal.throwIfAborted();
|
|
314
250
|
metadata.times.addressCreated = Date.now();
|
|
315
251
|
const paymentHash = this.getHash(chainIdentifier, receiveAddress, amountBD);
|
|
316
|
-
const currentTimestamp =
|
|
252
|
+
const currentTimestamp = BigInt(Math.floor(Date.now() / 1000));
|
|
317
253
|
const expiryTimeout = this.config.swapTsCsvDelta;
|
|
318
|
-
const expiry = currentTimestamp
|
|
254
|
+
const expiry = currentTimestamp + expiryTimeout;
|
|
319
255
|
//Calculate security deposit
|
|
320
|
-
const totalSecurityDeposit =
|
|
256
|
+
const totalSecurityDeposit = await this.getSecurityDeposit(chainIdentifier, amountBD, swapFee, expiryTimeout, baseSDPromise, depositToken, depositTokenPricePrefetchPromise, abortController.signal, metadata);
|
|
321
257
|
metadata.times.securityDepositCalculated = Date.now();
|
|
322
258
|
//Calculate claimer bounty
|
|
323
|
-
const totalClaimerBounty =
|
|
259
|
+
const totalClaimerBounty = await this.getClaimerBounty(req, expiry, abortController.signal);
|
|
324
260
|
metadata.times.claimerBountyCalculated = Date.now();
|
|
325
261
|
//Create swap data
|
|
326
|
-
const data =
|
|
327
|
-
data.
|
|
262
|
+
const data = await swapContract.createSwapData(base_1.ChainSwapType.CHAIN, signer.getAddress(), parsedBody.address, useToken, totalInToken, paymentHash.toString("hex"), parsedBody.sequence, expiry, false, true, totalSecurityDeposit, totalClaimerBounty, depositToken);
|
|
263
|
+
data.setExtraData(swapContract.getExtraData(this.bitcoin.toOutputScript(receiveAddress), amountBD, this.config.confirmations).toString("hex"));
|
|
328
264
|
abortController.signal.throwIfAborted();
|
|
329
265
|
metadata.times.swapCreated = Date.now();
|
|
330
266
|
//Sign the swap
|
|
331
|
-
const sigData =
|
|
267
|
+
const sigData = await this.getFromBtcSignatureData(chainIdentifier, data, req, abortController.signal, signDataPrefetchPromise);
|
|
332
268
|
metadata.times.swapSigned = Date.now();
|
|
333
|
-
const createdSwap = new FromBtcSwapAbs_1.FromBtcSwapAbs(chainIdentifier, receiveAddress, amountBD, swapFee, swapFeeInToken);
|
|
269
|
+
const createdSwap = new FromBtcSwapAbs_1.FromBtcSwapAbs(chainIdentifier, receiveAddress, this.config.confirmations, amountBD, swapFee, swapFeeInToken);
|
|
334
270
|
createdSwap.data = data;
|
|
335
271
|
createdSwap.metadata = metadata;
|
|
336
272
|
createdSwap.prefix = sigData.prefix;
|
|
337
273
|
createdSwap.timeout = sigData.timeout;
|
|
338
274
|
createdSwap.signature = sigData.signature;
|
|
339
275
|
createdSwap.feeRate = sigData.feeRate;
|
|
340
|
-
|
|
341
|
-
|
|
276
|
+
await PluginManager_1.PluginManager.swapCreate(createdSwap);
|
|
277
|
+
await this.saveSwapData(createdSwap);
|
|
342
278
|
this.swapLogger.info(createdSwap, "REST: /getAddress: Created swap address: " + receiveAddress + " amount: " + amountBD.toString(10));
|
|
343
|
-
|
|
279
|
+
await responseStream.writeParamsAndEnd({
|
|
344
280
|
code: 20000,
|
|
345
281
|
msg: "Success",
|
|
346
282
|
data: {
|
|
@@ -349,30 +285,29 @@ class FromBtcAbs extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
349
285
|
address: signer.getAddress(),
|
|
350
286
|
swapFee: swapFeeInToken.toString(10),
|
|
351
287
|
total: totalInToken.toString(10),
|
|
288
|
+
confirmations: this.config.confirmations,
|
|
352
289
|
data: data.serialize(),
|
|
353
290
|
prefix: sigData.prefix,
|
|
354
291
|
timeout: sigData.timeout,
|
|
355
292
|
signature: sigData.signature
|
|
356
293
|
}
|
|
357
294
|
});
|
|
358
|
-
}))
|
|
295
|
+
}));
|
|
359
296
|
this.logger.info("REST: Started at path: ", this.path);
|
|
360
297
|
}
|
|
361
298
|
/**
|
|
362
299
|
* Initializes swap handler, loads data and subscribes to chain events
|
|
363
300
|
*/
|
|
364
|
-
init() {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
yield PluginManager_1.PluginManager.serviceInitialize(this);
|
|
369
|
-
});
|
|
301
|
+
async init() {
|
|
302
|
+
await this.loadData(FromBtcSwapAbs_1.FromBtcSwapAbs);
|
|
303
|
+
this.subscribeToEvents();
|
|
304
|
+
await PluginManager_1.PluginManager.serviceInitialize(this);
|
|
370
305
|
}
|
|
371
306
|
getInfoData() {
|
|
372
307
|
return {
|
|
373
308
|
confirmations: this.config.confirmations,
|
|
374
309
|
cltv: this.config.swapCsvDelta,
|
|
375
|
-
timestampCltv: this.config.swapTsCsvDelta
|
|
310
|
+
timestampCltv: Number(this.config.swapTsCsvDelta)
|
|
376
311
|
};
|
|
377
312
|
}
|
|
378
313
|
}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import * as BN from "bn.js";
|
|
3
1
|
import { SwapData } from "@atomiqlabs/base";
|
|
4
2
|
import { FromBtcBaseSwap } from "../FromBtcBaseSwap";
|
|
5
3
|
export declare enum FromBtcSwapState {
|
|
@@ -11,13 +9,13 @@ export declare enum FromBtcSwapState {
|
|
|
11
9
|
}
|
|
12
10
|
export declare class FromBtcSwapAbs<T extends SwapData = SwapData> extends FromBtcBaseSwap<T, FromBtcSwapState> {
|
|
13
11
|
readonly address: string;
|
|
12
|
+
readonly confirmations: number;
|
|
14
13
|
txId: string;
|
|
15
|
-
constructor(chainIdentifier: string, address: string, amount:
|
|
14
|
+
constructor(chainIdentifier: string, address: string, confirmations: number, amount: bigint, swapFee: bigint, swapFeeInToken: bigint);
|
|
16
15
|
constructor(obj: any);
|
|
17
16
|
serialize(): any;
|
|
18
|
-
getTxoHash(): Buffer;
|
|
19
17
|
isInitiated(): boolean;
|
|
20
18
|
isFailed(): boolean;
|
|
21
19
|
isSuccess(): boolean;
|
|
22
|
-
getTotalInputAmount():
|
|
20
|
+
getTotalInputAmount(): bigint;
|
|
23
21
|
}
|
|
@@ -12,15 +12,17 @@ var FromBtcSwapState;
|
|
|
12
12
|
FromBtcSwapState[FromBtcSwapState["CLAIMED"] = 2] = "CLAIMED";
|
|
13
13
|
})(FromBtcSwapState = exports.FromBtcSwapState || (exports.FromBtcSwapState = {}));
|
|
14
14
|
class FromBtcSwapAbs extends FromBtcBaseSwap_1.FromBtcBaseSwap {
|
|
15
|
-
constructor(prOrObj, address, amount, swapFee, swapFeeInToken) {
|
|
15
|
+
constructor(prOrObj, address, confirmations, amount, swapFee, swapFeeInToken) {
|
|
16
16
|
if (typeof (prOrObj) === "string") {
|
|
17
17
|
super(prOrObj, amount, swapFee, swapFeeInToken);
|
|
18
18
|
this.state = FromBtcSwapState.CREATED;
|
|
19
19
|
this.address = address;
|
|
20
|
+
this.confirmations = confirmations;
|
|
20
21
|
}
|
|
21
22
|
else {
|
|
22
23
|
super(prOrObj);
|
|
23
24
|
this.address = prOrObj.address;
|
|
25
|
+
this.confirmations = prOrObj.confirmations;
|
|
24
26
|
this.txId = prOrObj.txId;
|
|
25
27
|
}
|
|
26
28
|
this.type = SwapHandler_1.SwapHandlerType.FROM_BTC;
|
|
@@ -28,12 +30,10 @@ class FromBtcSwapAbs extends FromBtcBaseSwap_1.FromBtcBaseSwap {
|
|
|
28
30
|
serialize() {
|
|
29
31
|
const partialSerialized = super.serialize();
|
|
30
32
|
partialSerialized.address = this.address;
|
|
33
|
+
partialSerialized.confirmations = this.confirmations;
|
|
31
34
|
partialSerialized.txId = this.txId;
|
|
32
35
|
return partialSerialized;
|
|
33
36
|
}
|
|
34
|
-
getTxoHash() {
|
|
35
|
-
return Buffer.from(this.data.getTxoHash(), "hex");
|
|
36
|
-
}
|
|
37
37
|
isInitiated() {
|
|
38
38
|
return this.state !== FromBtcSwapState.CREATED;
|
|
39
39
|
}
|
|
@@ -3,7 +3,6 @@ import { FromBtcTrustedSwap, FromBtcTrustedSwapState } from "./FromBtcTrustedSwa
|
|
|
3
3
|
import { BitcoinRpc, BtcBlock, BtcTx, ClaimEvent, InitializeEvent, RefundEvent, SwapData } from "@atomiqlabs/base";
|
|
4
4
|
import { Express } from "express";
|
|
5
5
|
import { MultichainData, SwapHandlerType } from "../SwapHandler";
|
|
6
|
-
import * as BN from "bn.js";
|
|
7
6
|
import { IIntermediaryStorage } from "../../storage/IIntermediaryStorage";
|
|
8
7
|
import { ISwapPrice } from "../ISwapPrice";
|
|
9
8
|
import { IBitcoinWallet } from "../../wallets/IBitcoinWallet";
|
|
@@ -14,11 +13,14 @@ export type FromBtcTrustedConfig = FromBtcBaseConfig & {
|
|
|
14
13
|
};
|
|
15
14
|
export type FromBtcTrustedRequestType = {
|
|
16
15
|
address: string;
|
|
17
|
-
amount:
|
|
18
|
-
|
|
16
|
+
amount: bigint;
|
|
17
|
+
exactIn?: boolean;
|
|
18
|
+
refundAddress?: string;
|
|
19
|
+
token?: string;
|
|
19
20
|
};
|
|
20
21
|
export declare class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTrustedSwap, FromBtcTrustedSwapState> {
|
|
21
22
|
readonly type: SwapHandlerType;
|
|
23
|
+
readonly swapType: any;
|
|
22
24
|
readonly config: FromBtcTrustedConfig;
|
|
23
25
|
readonly bitcoin: IBitcoinWallet;
|
|
24
26
|
readonly bitcoinRpc: BitcoinRpc<BtcBlock>;
|
|
@@ -29,8 +31,8 @@ export declare class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTruste
|
|
|
29
31
|
readonly processedTxIds: Map<string, {
|
|
30
32
|
scTxId: string;
|
|
31
33
|
txId: string;
|
|
32
|
-
adjustedAmount:
|
|
33
|
-
adjustedTotal:
|
|
34
|
+
adjustedAmount: bigint;
|
|
35
|
+
adjustedTotal: bigint;
|
|
34
36
|
}>;
|
|
35
37
|
constructor(storageDirectory: IIntermediaryStorage<FromBtcTrustedSwap>, path: string, chains: MultichainData, bitcoin: IBitcoinWallet, swapPricing: ISwapPrice, bitcoinRpc: BitcoinRpc<BtcBlock>, config: FromBtcTrustedConfig);
|
|
36
38
|
private getAllAncestors;
|
|
@@ -46,7 +48,7 @@ export declare class FromBtcTrusted extends FromBtcBaseSwapHandler<FromBtcTruste
|
|
|
46
48
|
startWatchdog(): Promise<void>;
|
|
47
49
|
init(): Promise<void>;
|
|
48
50
|
getInfoData(): any;
|
|
49
|
-
protected processClaimEvent(chainIdentifier: string, event: ClaimEvent<SwapData>): Promise<void>;
|
|
50
|
-
protected processInitializeEvent(chainIdentifier: string, event: InitializeEvent<SwapData>): Promise<void>;
|
|
51
|
-
protected processRefundEvent(chainIdentifier: string, event: RefundEvent<SwapData>): Promise<void>;
|
|
51
|
+
protected processClaimEvent(chainIdentifier: string, swap: FromBtcTrustedSwap, event: ClaimEvent<SwapData>): Promise<void>;
|
|
52
|
+
protected processInitializeEvent(chainIdentifier: string, swap: FromBtcTrustedSwap, event: InitializeEvent<SwapData>): Promise<void>;
|
|
53
|
+
protected processRefundEvent(chainIdentifier: string, swap: FromBtcTrustedSwap, event: RefundEvent<SwapData>): Promise<void>;
|
|
52
54
|
}
|