capitalisk-dex 17.2.2 → 17.3.1
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/defaults/config.js +4 -1
- package/index.js +80 -39
- package/package.json +1 -1
package/defaults/config.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
module.exports = {
|
|
2
2
|
passiveMode: false,
|
|
3
3
|
priceDecimalPrecision: null,
|
|
4
|
-
initRetryDelay:
|
|
4
|
+
initRetryDelay: 20000,
|
|
5
5
|
multisigExpiry: 86400000,
|
|
6
6
|
multisigExpiryCheckInterval: 60000,
|
|
7
7
|
multisigFlushInterval: 15000,
|
|
8
8
|
multisigReadyDelay: 5000,
|
|
9
9
|
multisigMaxBatchSize: 25,
|
|
10
|
+
multisigRetryInterval: 60000,
|
|
10
11
|
recentTransfersExpiry: 1800000,
|
|
11
12
|
signatureFlushInterval: 5000,
|
|
13
|
+
signatureReadyDelay: 10000,
|
|
14
|
+
signatureRetryInterval: 60000,
|
|
12
15
|
signatureMaxBatchSize: 400,
|
|
13
16
|
orderBookSnapshotFinality: 303,
|
|
14
17
|
orderBookUpdateSnapshotDirPath: 'dex-update-snapshots',
|
package/index.js
CHANGED
|
@@ -21,8 +21,11 @@ const { CAPITALISK_DEX_PASSWORD } = process.env;
|
|
|
21
21
|
const CIPHER_ALGORITHM = 'aes-192-cbc';
|
|
22
22
|
const CIPHER_KEY = CAPITALISK_DEX_PASSWORD ? crypto.scryptSync(CAPITALISK_DEX_PASSWORD, 'salt', 24) : undefined;
|
|
23
23
|
const CIPHER_IV = Buffer.alloc(16, 0);
|
|
24
|
-
const DEFAULT_INIT_RETRY_DELAY =
|
|
24
|
+
const DEFAULT_INIT_RETRY_DELAY = 20000;
|
|
25
25
|
const DEFAULT_MULTISIG_READY_DELAY = 5000;
|
|
26
|
+
const DEFAULT_MULTISIG_RETRY_INTERVAL = 60000;
|
|
27
|
+
const DEFAULT_SIGNATURE_READY_DELAY = 10000;
|
|
28
|
+
const DEFAULT_SIGNATURE_RETRY_INTERVAL = 60000;
|
|
26
29
|
const DEFAULT_PROTOCOL_EXCLUDE_REASON = false;
|
|
27
30
|
const DEFAULT_PROTOCOL_MAX_ARGUMENT_LENGTH = 64;
|
|
28
31
|
const DEFAULT_PRICE_DECIMAL_PRECISION = 4;
|
|
@@ -110,6 +113,9 @@ module.exports = class CapitaliskDEXModule {
|
|
|
110
113
|
this.quoteAddress = quoteChainOptions.multisigAddress;
|
|
111
114
|
this.initRetryDelay = this.options.initRetryDelay || DEFAULT_INIT_RETRY_DELAY;
|
|
112
115
|
this.multisigReadyDelay = this.options.multisigReadyDelay || DEFAULT_MULTISIG_READY_DELAY;
|
|
116
|
+
this.signatureReadyDelay = this.options.signatureReadyDelay || DEFAULT_SIGNATURE_READY_DELAY;
|
|
117
|
+
this.multisigRetryInterval = this.options.multisigRetryInterval || DEFAULT_MULTISIG_RETRY_INTERVAL;
|
|
118
|
+
this.signatureRetryInterval = this.options.signatureRetryInterval || DEFAULT_SIGNATURE_RETRY_INTERVAL;
|
|
113
119
|
|
|
114
120
|
this.priceDecimalPrecision = this.options.priceDecimalPrecision == null ?
|
|
115
121
|
DEFAULT_PRICE_DECIMAL_PRECISION : this.options.priceDecimalPrecision;
|
|
@@ -805,9 +811,16 @@ module.exports = class CapitaliskDEXModule {
|
|
|
805
811
|
processedSignerAddressSet.add(signerAddress);
|
|
806
812
|
transaction.signatures.push(signaturePacket);
|
|
807
813
|
|
|
814
|
+
let now = Date.now();
|
|
815
|
+
|
|
816
|
+
if (transfer.signatureBroadcastTimestamps[signerAddress] == null) {
|
|
817
|
+
transfer.signatureBroadcastTimestamps[signerAddress] = now;
|
|
818
|
+
}
|
|
819
|
+
|
|
808
820
|
let signatureQuota = this._getSignatureQuota(targetChain, transaction);
|
|
809
821
|
if (signatureQuota >= 0 && transfer.readyTimestamp == null) {
|
|
810
|
-
transfer.readyTimestamp =
|
|
822
|
+
transfer.readyTimestamp = now;
|
|
823
|
+
transfer.multisigBroadcastTimestamp = now + this.multisigReadyDelay;
|
|
811
824
|
}
|
|
812
825
|
}
|
|
813
826
|
|
|
@@ -822,43 +835,61 @@ module.exports = class CapitaliskDEXModule {
|
|
|
822
835
|
}
|
|
823
836
|
|
|
824
837
|
flushPendingMultisigTransactions() {
|
|
825
|
-
let
|
|
838
|
+
let transfersToBroadcastPerChain = {};
|
|
826
839
|
let now = Date.now();
|
|
827
840
|
|
|
828
841
|
for (let transfer of this.pendingTransfers.values()) {
|
|
829
|
-
if (transfer.
|
|
830
|
-
if (!
|
|
831
|
-
|
|
842
|
+
if (transfer.multisigBroadcastTimestamp != null && transfer.multisigBroadcastTimestamp <= now) {
|
|
843
|
+
if (!transfersToBroadcastPerChain[transfer.targetChain]) {
|
|
844
|
+
transfersToBroadcastPerChain[transfer.targetChain] = [];
|
|
832
845
|
}
|
|
833
|
-
|
|
846
|
+
transfersToBroadcastPerChain[transfer.targetChain].push(transfer);
|
|
834
847
|
}
|
|
835
848
|
}
|
|
836
849
|
|
|
837
|
-
let chainSymbolList = Object.keys(
|
|
850
|
+
let chainSymbolList = Object.keys(transfersToBroadcastPerChain);
|
|
838
851
|
for (let chainSymbol of chainSymbolList) {
|
|
839
|
-
let
|
|
840
|
-
|
|
852
|
+
let transfersToBroadcast = transfersToBroadcastPerChain[chainSymbol]
|
|
853
|
+
.slice(0, this.options.multisigMaxBatchSize);
|
|
854
|
+
|
|
855
|
+
for (let transfer of transfersToBroadcast) {
|
|
856
|
+
transfer.multisigBroadcastTimestamp = now + this.multisigRetryInterval;
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
let transactionsToBroadcast = transfersToBroadcast
|
|
860
|
+
.map(transfer => transfer.transaction);
|
|
841
861
|
this._broadcastTransactionsToChain(chainSymbol, transactionsToBroadcast);
|
|
842
862
|
}
|
|
843
863
|
}
|
|
844
864
|
|
|
845
865
|
flushPendingSignatures() {
|
|
846
|
-
let
|
|
866
|
+
let pendingSignatureInfoList = [];
|
|
867
|
+
let now = Date.now();
|
|
847
868
|
|
|
848
869
|
for (let transfer of this.pendingTransfers.values()) {
|
|
849
870
|
for (let signaturePacket of transfer.transaction.signatures) {
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
871
|
+
let broadcastTimestamp = transfer.signatureBroadcastTimestamps[signaturePacket.signerAddress];
|
|
872
|
+
if (broadcastTimestamp != null && broadcastTimestamp <= now) {
|
|
873
|
+
pendingSignatureInfoList.push({
|
|
874
|
+
signaturePacket,
|
|
875
|
+
transactionId: transfer.transaction.id,
|
|
876
|
+
transfer
|
|
877
|
+
});
|
|
878
|
+
}
|
|
854
879
|
}
|
|
855
880
|
}
|
|
856
881
|
|
|
857
|
-
if (
|
|
858
|
-
let
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
882
|
+
if (pendingSignatureInfoList.length) {
|
|
883
|
+
let signatureInfosToBroadcast = pendingSignatureInfoList.slice(0, this.options.signatureMaxBatchSize);
|
|
884
|
+
for (let signatureInfo of signatureInfosToBroadcast) {
|
|
885
|
+
let { signaturePacket, transfer } = signatureInfo;
|
|
886
|
+
transfer.signatureBroadcastTimestamps[signaturePacket.signerAddress] = Date.now() + this.signatureRetryInterval;
|
|
887
|
+
}
|
|
888
|
+
let signaturesToBroadcast = signatureInfosToBroadcast.map(signatureInfo => {
|
|
889
|
+
let { transfer, ...signatureData } = signatureInfo;
|
|
890
|
+
return signatureData;
|
|
891
|
+
});
|
|
892
|
+
this._broadcastSignaturesToSubnet(signaturesToBroadcast);
|
|
862
893
|
}
|
|
863
894
|
}
|
|
864
895
|
|
|
@@ -1172,7 +1203,7 @@ module.exports = class CapitaliskDEXModule {
|
|
|
1172
1203
|
`Failed to load initial snapshot because of error: ${error.message} - DEX node will start with an empty order book`
|
|
1173
1204
|
);
|
|
1174
1205
|
|
|
1175
|
-
while (
|
|
1206
|
+
while (true) {
|
|
1176
1207
|
try {
|
|
1177
1208
|
let [baseMaxHeight, quoteMaxHeight] = await Promise.all([
|
|
1178
1209
|
this._getMaxBlockHeight(this.baseChainSymbol, false),
|
|
@@ -1185,31 +1216,37 @@ module.exports = class CapitaliskDEXModule {
|
|
|
1185
1216
|
this.logger.error(`The ${this.quoteChainSymbol} chain had a height of 0`);
|
|
1186
1217
|
}
|
|
1187
1218
|
if (!baseMaxHeight || !quoteMaxHeight) {
|
|
1188
|
-
|
|
1189
|
-
await wait(this.initRetryDelay);
|
|
1190
|
-
continue;
|
|
1219
|
+
throw new Error('Invalid chain heights');
|
|
1191
1220
|
}
|
|
1192
1221
|
this.processedHeights[this.baseChainSymbol] = baseMaxHeight;
|
|
1193
1222
|
this.processedHeights[this.quoteChainSymbol] = quoteMaxHeight;
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1223
|
+
break;
|
|
1224
|
+
} catch (initHeightError) {
|
|
1225
|
+
this.logger.error(
|
|
1226
|
+
`Failed to initialize last processed heights because of error: ${initHeightError.message}`
|
|
1197
1227
|
);
|
|
1228
|
+
this.logger.debug('Retrying initialization of last processed heights...');
|
|
1229
|
+
await wait(this.initRetryDelay);
|
|
1198
1230
|
}
|
|
1199
1231
|
}
|
|
1200
1232
|
}
|
|
1201
1233
|
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1234
|
+
while (true) {
|
|
1235
|
+
try {
|
|
1236
|
+
let [baseChainMaxBlock, quoteChainMaxBlock] = await Promise.all([
|
|
1237
|
+
this._getBlockAtHeight(this.baseChainSymbol, this.processedHeights[this.baseChainSymbol]),
|
|
1238
|
+
this._getBlockAtHeight(this.quoteChainSymbol, this.processedHeights[this.quoteChainSymbol])
|
|
1239
|
+
]);
|
|
1240
|
+
this.lastProcessedBlocks[this.baseChainSymbol] = baseChainMaxBlock;
|
|
1241
|
+
this.lastProcessedBlocks[this.quoteChainSymbol] = quoteChainMaxBlock;
|
|
1242
|
+
break;
|
|
1243
|
+
} catch (error) {
|
|
1244
|
+
this.logger.error(
|
|
1245
|
+
`Failed to initialize last processed blocks because of error: ${error.message}`
|
|
1246
|
+
);
|
|
1247
|
+
this.logger.debug('Retrying initialization of last processed blocks...');
|
|
1248
|
+
await wait(this.initRetryDelay);
|
|
1249
|
+
}
|
|
1213
1250
|
}
|
|
1214
1251
|
|
|
1215
1252
|
await Promise.all(
|
|
@@ -2723,6 +2760,7 @@ module.exports = class CapitaliskDEXModule {
|
|
|
2723
2760
|
if (this.pendingTransfers.has(preparedTxn.id)) {
|
|
2724
2761
|
this.pendingTransfers.delete(preparedTxn.id);
|
|
2725
2762
|
}
|
|
2763
|
+
let now = Date.now();
|
|
2726
2764
|
let transfer = {
|
|
2727
2765
|
id: preparedTxn.id,
|
|
2728
2766
|
transaction: preparedTxn,
|
|
@@ -2730,7 +2768,10 @@ module.exports = class CapitaliskDEXModule {
|
|
|
2730
2768
|
targetChain,
|
|
2731
2769
|
processedSignerAddressSet,
|
|
2732
2770
|
height: transactionData.height,
|
|
2733
|
-
timestamp:
|
|
2771
|
+
timestamp: now,
|
|
2772
|
+
signatureBroadcastTimestamps: {
|
|
2773
|
+
[multisigSignaturePacket.signerAddress]: now + this.signatureReadyDelay
|
|
2774
|
+
},
|
|
2734
2775
|
...extraTransferData
|
|
2735
2776
|
};
|
|
2736
2777
|
this.pendingTransfers.set(preparedTxn.id, transfer);
|