@xchainjs/xchain-thorchain-query 0.1.13 → 0.1.14

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/lib/index.esm.js CHANGED
@@ -655,35 +655,6 @@ const getChainAsset = (chain) => {
655
655
  default:
656
656
  throw Error('Unknown chain');
657
657
  }
658
- };
659
- /**
660
- *
661
- * @param chain - input chain string
662
- * @returns - returns correct chain from string
663
- */
664
- const getChain = (chain) => {
665
- switch (chain) {
666
- case 'AVAX':
667
- return AVAXChain;
668
- case 'BNB':
669
- return BNBChain;
670
- case 'BTC':
671
- return BTCChain;
672
- case 'ETH':
673
- return ETHChain;
674
- case 'THOR':
675
- return THORChain;
676
- case 'GAIA':
677
- return GAIAChain;
678
- case 'BCH':
679
- return BCHChain;
680
- case 'LTC':
681
- return LTCChain;
682
- case 'DOGE':
683
- return DOGEChain;
684
- default:
685
- throw Error('Unknown chain');
686
- }
687
658
  };
688
659
 
689
660
  const defaultThornodeConfig = {
@@ -758,6 +729,25 @@ class Thornode {
758
729
  throw new Error(`THORNode not responding`);
759
730
  });
760
731
  }
732
+ /**
733
+ *
734
+ * @param txHash - transaction hash
735
+ * @returns - transaction object
736
+ */
737
+ getTxDetail(txHash) {
738
+ return __awaiter(this, void 0, void 0, function* () {
739
+ for (const api of this.transactionsApi) {
740
+ try {
741
+ const txResponse = yield api.txSigners(txHash);
742
+ return txResponse.data;
743
+ }
744
+ catch (e) {
745
+ throw new Error(`THORNode not responding`);
746
+ }
747
+ }
748
+ throw new Error(`THORNode not responding`);
749
+ });
750
+ }
761
751
  /**
762
752
  *
763
753
  * @param height - optional thorchain height only
@@ -2240,186 +2230,388 @@ class ThorchainQuery {
2240
2230
  }
2241
2231
  }
2242
2232
 
2233
+ var TxType;
2234
+ (function (TxType) {
2235
+ TxType["Swap"] = "Swap";
2236
+ TxType["AddLP"] = "AddLP";
2237
+ TxType["WithdrawLP"] = "WithdrawLP";
2238
+ TxType["AddSaver"] = "AddSaver";
2239
+ TxType["WithdrawSaver"] = "WithdrawSaver";
2240
+ TxType["Other"] = "Other";
2241
+ TxType["Unknown"] = "Unknown";
2242
+ })(TxType || (TxType = {}));
2243
+ var InboundStatus;
2244
+ (function (InboundStatus) {
2245
+ InboundStatus["Observed_Consensus"] = "Observed_Consensus";
2246
+ InboundStatus["Observed_Incomplete"] = "Observed_Incomplete";
2247
+ InboundStatus["Unknown"] = "Unknown";
2248
+ })(InboundStatus || (InboundStatus = {}));
2249
+ var SwapStatus;
2250
+ (function (SwapStatus) {
2251
+ SwapStatus["Complete"] = "Complete";
2252
+ SwapStatus["Complete_Refunded"] = "Complete_Refunded";
2253
+ SwapStatus["Complete_Below_Dust"] = "Complete_Below_Dust";
2254
+ SwapStatus["Incomplete"] = "Incomplete";
2255
+ })(SwapStatus || (SwapStatus = {}));
2256
+ var AddLpStatus;
2257
+ (function (AddLpStatus) {
2258
+ AddLpStatus["Complete"] = "Complete";
2259
+ AddLpStatus["Complete_Refunded"] = "Complete_Refunded";
2260
+ AddLpStatus["Complete_Below_Dust"] = "Complete_Below_Dust";
2261
+ AddLpStatus["Incomplete"] = "Incomplete";
2262
+ })(AddLpStatus || (AddLpStatus = {}));
2263
+ var WithdrawStatus;
2264
+ (function (WithdrawStatus) {
2265
+ WithdrawStatus["Complete"] = "Complete";
2266
+ WithdrawStatus["Incomplete"] = "Incomplete";
2267
+ WithdrawStatus["Complete_Refunded"] = "Complete_Refunded";
2268
+ })(WithdrawStatus || (WithdrawStatus = {}));
2269
+ var AddSaverStatus;
2270
+ (function (AddSaverStatus) {
2271
+ AddSaverStatus["Complete"] = "Complete";
2272
+ AddSaverStatus["Complete_Refunded"] = "Complete_Refunded";
2273
+ AddSaverStatus["Complete_Below_Dust"] = "Complete_Below_Dust";
2274
+ AddSaverStatus["Incomplete"] = "Incomplete";
2275
+ })(AddSaverStatus || (AddSaverStatus = {}));
2243
2276
  class TransactionStage {
2244
2277
  constructor(thorchainCache, chainAttributes = DefaultChainAttributes) {
2245
2278
  this.thorchainCache = thorchainCache;
2246
2279
  this.chainAttributes = chainAttributes;
2247
2280
  }
2248
- // Functions follow this logic below
2249
- // 1. Has TC see it?
2250
- // 2. If observed, has is there inbound conf counting (for non BFT Chains)? If so, for how long
2251
- // 3. Has TC processed it?
2252
- // 4. Is it in the outbound queue? If so, what is the target block and how long will it take for that to happen?
2253
- // 5. If TC has sent it, how long will outbound conf take?
2254
- /**
2255
- *
2256
- * @param inboundTxHash - Input needed to determine the transaction stage
2257
- * @param sourceChain - Needed for faster logic
2258
- * @returns - tx stage or maybe a boolean not sure at this stage
2259
- */
2260
- checkTxProgress(inboundTxHash, progress, sourceChain) {
2261
- var _a, _b;
2281
+ checkTxProgress(inboundTxHash) {
2262
2282
  return __awaiter(this, void 0, void 0, function* () {
2263
- const transactionProgress = {
2264
- progress: 0,
2265
- seconds: 0,
2266
- errors: [],
2267
- };
2268
- if (progress) {
2269
- transactionProgress.progress = progress;
2283
+ let txData;
2284
+ try {
2285
+ if (inboundTxHash.length < 1)
2286
+ throw Error('inboundTxHash too short');
2287
+ txData = yield this.thorchainCache.thornode.getTxDetail(inboundTxHash);
2288
+ // console.log(JSON.stringify(txData, null, 2))
2289
+ }
2290
+ catch (error) {
2291
+ return {
2292
+ txType: TxType.Unknown,
2293
+ };
2270
2294
  }
2271
- else {
2272
- progress = 0;
2273
- }
2274
- const txData = yield this.thorchainCache.thornode.getTxData(inboundTxHash);
2275
- const isSynth = yield this.isSynthTransaction(txData);
2276
- if (isSynth) {
2277
- progress = 3;
2278
- }
2279
- const lastBlock = yield this.thorchainCache.thornode.getLastBlock();
2280
- if (txData.observed_tx == undefined) {
2281
- // Check inbound, if nothing check outbound queue
2282
- progress = 2;
2283
- }
2284
- switch (progress) {
2285
- case 0:
2286
- const txObserved = yield this.checkTCRecords(txData, sourceChain);
2287
- transactionProgress.seconds = txObserved.seconds;
2288
- transactionProgress.progress = 1;
2289
- transactionProgress.errors = txObserved.error;
2290
- return transactionProgress;
2291
- case 1:
2292
- if (((_b = (_a = txData.observed_tx) === null || _a === void 0 ? void 0 : _a.tx) === null || _b === void 0 ? void 0 : _b.chain) != undefined) {
2293
- sourceChain = getChain(txData.observed_tx.tx.chain);
2294
- if (sourceChain == (BTCChain || BCHChain || LTCChain)) {
2295
- const lastBlockHeight = lastBlock.find((obj) => obj.chain === sourceChain);
2296
- const checkConf = yield this.checkConfcounting(sourceChain, lastBlockHeight, txData.observed_tx);
2297
- transactionProgress.seconds = checkConf.seconds;
2298
- transactionProgress.errors = checkConf.error;
2299
- }
2300
- }
2301
- transactionProgress.progress = 2;
2302
- return transactionProgress;
2303
- case 2:
2304
- const tcBlockHeight = lastBlock.find((obj) => obj.thorchain);
2305
- const checkOutboundQueue = yield this.checkOutboundQueue(inboundTxHash, tcBlockHeight);
2306
- transactionProgress.seconds = checkOutboundQueue.seconds;
2307
- transactionProgress.errors = checkOutboundQueue.error;
2308
- transactionProgress.progress = 3;
2309
- return transactionProgress;
2310
- case 3:
2311
- const tcBlock = lastBlock.find((obj) => obj.thorchain);
2312
- const checkOutboundQue = yield this.checkOutboundQueue(inboundTxHash, tcBlock);
2313
- transactionProgress.seconds = checkOutboundQue.seconds;
2314
- transactionProgress.progress = 4;
2315
- return transactionProgress;
2316
- default:
2317
- return transactionProgress;
2295
+ //valid tx
2296
+ const progress = yield this.determineObserved(txData);
2297
+ switch (progress.txType) {
2298
+ case TxType.Swap:
2299
+ yield this.checkSwapProgress(txData, progress);
2300
+ break;
2301
+ case TxType.AddLP:
2302
+ yield this.checkAddLpProgress(txData, progress);
2303
+ break;
2304
+ case TxType.WithdrawLP:
2305
+ yield this.checkWithdrawLpProgress(txData, progress);
2306
+ break;
2307
+ case TxType.AddSaver:
2308
+ yield this.checkAddSaverProgress(txData, progress);
2309
+ break;
2310
+ case TxType.WithdrawSaver:
2311
+ yield this.checkWithdrawSaverProgress(txData, progress);
2312
+ break;
2313
+ case TxType.Other:
2314
+ break;
2315
+ }
2316
+ return progress;
2317
+ });
2318
+ }
2319
+ checkSwapProgress(txData, progress) {
2320
+ var _a, _b, _c, _d;
2321
+ return __awaiter(this, void 0, void 0, function* () {
2322
+ if (progress.inboundObserved) {
2323
+ const memo = (_a = txData.tx.tx.memo) !== null && _a !== void 0 ? _a : '';
2324
+ const memoFields = this.parseSwapMemo(memo);
2325
+ const assetOut = assetFromStringEx(memoFields.asset.toUpperCase());
2326
+ //const assetIn = assetFromStringEx(txData.tx.tx.coins?.[0].asset)
2327
+ const swapStatus = ((_b = txData.out_txs[0].memo) === null || _b === void 0 ? void 0 : _b.match('OUT')) ? SwapStatus.Complete : SwapStatus.Complete_Refunded;
2328
+ // current height of thorchain, neeed for confirmations
2329
+ const chainHeight = yield this.blockHeight(AssetRuneNative);
2330
+ // expected outbound height
2331
+ const outboundHeight = Number((_c = txData.outbound_height) !== null && _c !== void 0 ? _c : txData.finalised_height);
2332
+ const expectedOutBlock = Number((_d = txData.outbound_height) !== null && _d !== void 0 ? _d : txData.finalised_height);
2333
+ const expectedOutDate = yield this.blockToDate(THORChain, txData, outboundHeight); // height held in the scheduled queue
2334
+ const confirmations = chainHeight > outboundHeight ? chainHeight - outboundHeight : 0;
2335
+ const minimumAmountOut = memoFields.limit
2336
+ ? yield this.getCryptoAmount(memoFields.limit, assetOut)
2337
+ : yield this.getCryptoAmount('0', assetOut);
2338
+ const affliateFee = memoFields.affiliateFee
2339
+ ? yield this.getCryptoAmount(memoFields.affiliateFee, assetOut)
2340
+ : yield this.getCryptoAmount('0', assetOut);
2341
+ // TODO get out tx
2342
+ const swapInfo = {
2343
+ status: swapStatus,
2344
+ expectedOutBlock,
2345
+ expectedOutDate,
2346
+ expectedAmountOut: minimumAmountOut,
2347
+ confirmations,
2348
+ minimumAmountOut,
2349
+ affliateFee,
2350
+ toAddress: memoFields.destAddress,
2351
+ };
2352
+ progress.swapInfo = swapInfo;
2318
2353
  }
2319
2354
  });
2320
2355
  }
2321
- /** Stage 1 */
2322
- /**
2323
- * Check if transaction has been observed by bifrost or THORNode.
2324
- * If there is source chain info, use that determine the wait time else wait one minute
2325
- *
2326
- * @param observed_tx - transaction data based from an input hash
2327
- * @param sourceChain
2328
- * @returns
2329
- */
2330
- checkTCRecords(txData, sourceChain) {
2356
+ parseSwapMemo(memo) {
2357
+ //SWAP:ASSET:DESTADDR:LIM:AFFILIATE:FEE
2358
+ const parts = memo.split(`:`);
2359
+ const action = parts[0];
2360
+ const asset = parts[1];
2361
+ const destAddress = parts[2];
2362
+ const limit = parts.length > 3 && parts[3].length > 0 ? parts[3] : undefined;
2363
+ const affiliateAddress = parts.length > 4 && parts[4].length > 0 ? parts[4] : undefined;
2364
+ const affiliateFee = parts.length > 5 && parts[5].length > 0 ? parts[5] : undefined;
2365
+ return { action, asset, destAddress, limit, affiliateAddress, affiliateFee };
2366
+ }
2367
+ getCryptoAmount(baseAmt, asset) {
2331
2368
  return __awaiter(this, void 0, void 0, function* () {
2332
- const stageStatus = {
2333
- seconds: 0,
2334
- error: [],
2369
+ const decimals = THORChain === asset.chain ? 8 : Number((yield this.thorchainCache.getPoolForAsset(asset)).pool.nativeDecimal);
2370
+ return new CryptoAmount(baseAmount(baseAmt, decimals), asset);
2371
+ });
2372
+ }
2373
+ determineObserved(txData) {
2374
+ var _a, _b, _c, _d;
2375
+ return __awaiter(this, void 0, void 0, function* () {
2376
+ const progress = {
2377
+ txType: TxType.Unknown,
2335
2378
  };
2336
- if (JSON.stringify(txData.observed_tx) == undefined) {
2337
- if (sourceChain) {
2338
- stageStatus.seconds = this.chainAttributes[sourceChain].avgBlockTimeInSecs;
2339
- }
2340
- else {
2341
- stageStatus.seconds = 60;
2342
- stageStatus.error.push(`No observed tx, wait sixty seconds`);
2343
- }
2379
+ if (txData.tx) {
2380
+ const memo = (_a = txData.tx.tx.memo) !== null && _a !== void 0 ? _a : '';
2381
+ const parts = memo === null || memo === void 0 ? void 0 : memo.split(`:`);
2382
+ const operation = parts && parts[0] ? parts[0] : '';
2383
+ const assetIn = assetFromStringEx((_b = txData.tx.tx.coins) === null || _b === void 0 ? void 0 : _b[0].asset);
2384
+ const inboundAmount = (_c = txData.tx.tx.coins) === null || _c === void 0 ? void 0 : _c[0].amount;
2385
+ const fromAddress = (_d = txData.tx.tx.from_address) !== null && _d !== void 0 ? _d : 'unknkown';
2386
+ const block = txData.tx.tx.chain == THORChain ? Number(txData.finalised_height) : Number(txData.tx.block_height);
2387
+ const finalizeBlock = txData.tx.tx.chain == THORChain ? Number(txData.finalised_height) : Number(txData.tx.finalise_height);
2388
+ const status = txData.tx.status === 'done' ? InboundStatus.Observed_Consensus : InboundStatus.Observed_Incomplete;
2389
+ if (operation.match(/swap|s|=/gi))
2390
+ progress.txType = TxType.Swap;
2391
+ if ((operation.match(/add/gi) && parts[1].match(`/`)) || (operation.match(/a|[+]/) && parts[1].match(/[/]/)))
2392
+ progress.txType = TxType.AddSaver;
2393
+ if ((operation.match(/add/gi) && parts[1].match(`.`)) || (operation.match(/a|[+]/) && parts[1].match(/[.]/)))
2394
+ progress.txType = TxType.AddLP;
2395
+ if (operation.match(/withdraw|wd|-/gi) && parts[1].match(/[/]/))
2396
+ progress.txType = TxType.WithdrawSaver;
2397
+ if (operation.match(/withdraw|wd|-/gi) && parts[1].match(/[.]/))
2398
+ progress.txType = TxType.WithdrawLP;
2399
+ if (operation.match(/out|refund/gi))
2400
+ progress.txType = TxType.Other;
2401
+ const amount = yield this.getCryptoAmount(inboundAmount, assetIn);
2402
+ // find a date for when it should be competed
2403
+ const dateObserved = yield this.blockToDate(THORChain, txData);
2404
+ const expectedConfirmationDate = txData.tx.tx.chain === THORChain
2405
+ ? yield this.blockToDate(THORChain, txData)
2406
+ : yield this.blockToDate(assetIn.chain, txData);
2407
+ progress.inboundObserved = {
2408
+ status,
2409
+ date: dateObserved,
2410
+ block,
2411
+ expectedConfirmationBlock: finalizeBlock,
2412
+ expectedConfirmationDate,
2413
+ amount,
2414
+ fromAddress,
2415
+ memo,
2416
+ };
2344
2417
  }
2345
- return stageStatus;
2418
+ return progress;
2346
2419
  });
2347
2420
  }
2348
- /** Stage 2
2349
- *
2350
- * 1. Find observed_tx.finalise_height and the current observation height (observed_tx.block_height)
2351
- * 2. If last_observed_in (current observation height) is less then the finalise_height, will need to wait tillast_observed_in = finalise_height before thorchain starts processing the transaction.
2352
- * 3. If there is a wait, find the block difference (in source chain) then times by block time to find the conf counting wait time
2353
- *
2354
- * @param observed_tx - transaction data based from an input hash
2355
- * @param sourceChain - in tx chain, should be known at this point.
2356
- * @param lastSourceBlock
2357
- * @returns - transaction stage
2358
- */
2359
- checkConfcounting(sourceChain, lastSourceBlock, observed_tx) {
2421
+ checkAddLpProgress(txData, progress) {
2422
+ var _a;
2360
2423
  return __awaiter(this, void 0, void 0, function* () {
2361
- const transactionStatus = { seconds: 0, error: [] };
2362
- if (lastSourceBlock == undefined) {
2363
- transactionStatus.error.push(`could not find last source block`);
2364
- return transactionStatus;
2365
- }
2366
- if ((observed_tx === null || observed_tx === void 0 ? void 0 : observed_tx.block_height) &&
2367
- (observed_tx === null || observed_tx === void 0 ? void 0 : observed_tx.finalise_height) &&
2368
- lastSourceBlock.last_observed_in < observed_tx.finalise_height) {
2369
- // If observed but not final, need to wait till the finalised block before moving to the next stage, blocks in source chain
2370
- if (observed_tx.block_height < observed_tx.finalise_height) {
2371
- const blocksToWait = observed_tx.finalise_height - (observed_tx === null || observed_tx === void 0 ? void 0 : observed_tx.block_height); // how many source blocks to wait.
2372
- transactionStatus.seconds = blocksToWait * this.chainAttributes[sourceChain].avgBlockTimeInSecs;
2373
- }
2424
+ if (progress.inboundObserved) {
2425
+ const memo = (_a = txData.tx.tx.memo) !== null && _a !== void 0 ? _a : '';
2426
+ const memoFields = this.parseAddLpMemo(memo);
2427
+ const asset = assetFromStringEx(memoFields.asset);
2428
+ const isSymmetric = memoFields.pairedAddress ? true : false;
2429
+ const assetTx = !isAssetRuneNative(progress.inboundObserved.amount.asset) ? progress.inboundObserved : undefined;
2430
+ const runeTx = isAssetRuneNative(progress.inboundObserved.amount.asset) ? progress.inboundObserved : undefined;
2431
+ const pairedAssetExpectedConfirmationDate = assetTx ? yield this.blockToDate(asset.chain, txData) : undefined;
2432
+ const checkLpPosition = yield this.thorchainCache.thornode.getLiquidityProvider(memoFields.asset, progress.inboundObserved.fromAddress);
2433
+ const status = checkLpPosition ? AddLpStatus.Complete : AddLpStatus.Incomplete;
2434
+ const addLpInfo = {
2435
+ status,
2436
+ isSymmetric,
2437
+ assetTx,
2438
+ runeTx,
2439
+ assetConfirmationDate: pairedAssetExpectedConfirmationDate,
2440
+ pool: asset,
2441
+ };
2442
+ progress.addLpInfo = addLpInfo;
2443
+ }
2444
+ });
2445
+ }
2446
+ checkWithdrawLpProgress(txData, progress) {
2447
+ var _a, _b;
2448
+ return __awaiter(this, void 0, void 0, function* () {
2449
+ if (progress.inboundObserved) {
2450
+ const memo = (_a = txData.tx.tx.memo) !== null && _a !== void 0 ? _a : '';
2451
+ const memoFields = this.parseWithdrawLpMemo(memo);
2452
+ const asset = assetFromStringEx(memoFields.asset);
2453
+ const lastBlockObj = yield this.thorchainCache.thornode.getLastBlock();
2454
+ const currentHeight = lastBlockObj.find((obj) => obj);
2455
+ // find the date in which the asset should be seen in the wallet
2456
+ const outboundHeight = txData.tx.status === 'done' ? txData.finalised_height : Number(`${txData.outbound_height}`);
2457
+ const expectedConfirmationDate = yield this.blockToDate(THORChain, txData, outboundHeight); // always pass in thorchain
2458
+ // if the TC has process the block that the outbound tx was assigned to then its completed.
2459
+ const status = txData.tx.status === 'done' ? WithdrawStatus.Complete : WithdrawStatus.Incomplete;
2460
+ const outAmount = status === WithdrawStatus.Complete ? JSON.stringify(txData.out_txs).split(`"amount":"`)[1].split(`"`) : '';
2461
+ const outboundBlock = Number((_b = txData.outbound_height) !== null && _b !== void 0 ? _b : txData.finalised_height);
2462
+ const currentTCHeight = Number(`${currentHeight === null || currentHeight === void 0 ? void 0 : currentHeight.thorchain}`);
2463
+ const estimatedWaitTime = outboundBlock > currentTCHeight
2464
+ ? (outboundBlock - currentTCHeight) * this.chainAttributes[THORChain].avgBlockTimeInSecs
2465
+ : 0;
2466
+ const withdrawLpInfo = {
2467
+ status,
2468
+ withdrawalAmount: new CryptoAmount(baseAmount(outAmount[0]), asset),
2469
+ expectedConfirmationDate,
2470
+ thorchainHeight: currentTCHeight,
2471
+ outboundHeight: outboundBlock,
2472
+ estimatedWaitTime,
2473
+ };
2474
+ progress.withdrawLpInfo = withdrawLpInfo;
2475
+ }
2476
+ });
2477
+ }
2478
+ checkAddSaverProgress(txData, progress) {
2479
+ return __awaiter(this, void 0, void 0, function* () {
2480
+ if (progress.inboundObserved) {
2481
+ const assetTx = !isAssetRuneNative(progress.inboundObserved.amount.asset) ? progress.inboundObserved : undefined;
2482
+ const checkSaverVaults = yield this.thorchainCache.thornode.getSaver(txData.tx.tx.coins[0].asset, `${assetTx === null || assetTx === void 0 ? void 0 : assetTx.fromAddress}`);
2483
+ const status = checkSaverVaults ? AddSaverStatus.Complete : AddSaverStatus.Incomplete;
2484
+ const addSaverInfo = {
2485
+ status: status,
2486
+ assetTx,
2487
+ saverPos: checkSaverVaults,
2488
+ };
2489
+ progress.addSaverInfo = addSaverInfo;
2374
2490
  }
2375
- return transactionStatus;
2376
2491
  });
2377
2492
  }
2378
- /** Stage 3 */
2493
+ checkWithdrawSaverProgress(txData, progress) {
2494
+ var _a;
2495
+ return __awaiter(this, void 0, void 0, function* () {
2496
+ if (progress.inboundObserved) {
2497
+ const memo = (_a = txData.tx.tx.memo) !== null && _a !== void 0 ? _a : '';
2498
+ const memoFields = this.parseWithdrawLpMemo(memo);
2499
+ const asset = assetFromStringEx(memoFields.asset);
2500
+ const lastBlockObj = yield this.thorchainCache.thornode.getLastBlock();
2501
+ const currentHeight = lastBlockObj.find((obj) => obj);
2502
+ // find the date in which the asset should be seen in the wallet
2503
+ const outboundHeight = txData.tx.status === 'done' ? txData.finalised_height : Number(`${txData.outbound_height}`);
2504
+ const expectedConfirmationDate = yield this.blockToDate(THORChain, txData, outboundHeight); // always pass in thorchain
2505
+ const outAmount = txData.out_txs ? JSON.stringify(txData.out_txs).split(`"amount":"`)[1].split(`"`) : '';
2506
+ const outboundBlock = Number(txData.outbound_height);
2507
+ const finalisedHeight = Number(txData.finalised_height);
2508
+ const currentTCHeight = Number(`${currentHeight === null || currentHeight === void 0 ? void 0 : currentHeight.thorchain}`);
2509
+ const estimatedWaitTime = outboundBlock > currentTCHeight
2510
+ ? (outboundBlock - currentTCHeight) * this.chainAttributes[THORChain].avgBlockTimeInSecs +
2511
+ this.chainAttributes[asset.chain].avgBlockTimeInSecs
2512
+ : 0;
2513
+ // if the TC has process the block that the outbound tx was assigned to then its completed.
2514
+ const status = txData.out_txs ? WithdrawStatus.Complete : WithdrawStatus.Incomplete;
2515
+ const withdrawSaverInfo = {
2516
+ status,
2517
+ withdrawalAmount: new CryptoAmount(baseAmount(outAmount[0]), asset),
2518
+ expectedConfirmationDate,
2519
+ thorchainHeight: currentTCHeight,
2520
+ finalisedHeight,
2521
+ outboundBlock,
2522
+ estimatedWaitTime,
2523
+ };
2524
+ progress.withdrawSaverInfo = withdrawSaverInfo;
2525
+ }
2526
+ });
2527
+ }
2528
+ parseAddLpMemo(memo) {
2529
+ //ADD:POOL:PAIREDADDR:AFFILIATE:FEE
2530
+ const parts = memo.split(`:`);
2531
+ const action = parts[0];
2532
+ const asset = parts[1];
2533
+ //optional fields
2534
+ const pairedAddress = parts.length > 2 && parts[2].length > 0 ? parts[2] : undefined;
2535
+ const affiliateAddress = parts.length > 3 && parts[3].length > 0 ? parts[3] : undefined;
2536
+ const affiliateFee = parts.length > 4 && parts[4].length > 0 ? parts[4] : undefined;
2537
+ return { action, asset, pairedAddress, affiliateAddress, affiliateFee };
2538
+ }
2539
+ parseWithdrawLpMemo(memo) {
2540
+ //ADD:POOL:PAIREDADDR:AFFILIATE:FEE
2541
+ const parts = memo.split(`:`);
2542
+ const action = parts[0];
2543
+ const asset = parts[1];
2544
+ //optional fields
2545
+ const pairedAddress = parts.length > 2 && parts[2].length > 0 ? parts[2] : undefined;
2546
+ const affiliateAddress = parts.length > 3 && parts[3].length > 0 ? parts[3] : undefined;
2547
+ const affiliateFee = parts.length > 4 && parts[4].length > 0 ? parts[4] : undefined;
2548
+ return { action, asset, pairedAddress, affiliateAddress, affiliateFee };
2549
+ }
2379
2550
  /**
2380
- * Regardless of the transaction size, if the network is under load, Txs will need to wait. Given a size of a transaction AND the current network load, THORNode can assign a future block for the outgoing transaction.
2381
- * A user needs to wait for that block to be reached before a TX is sent.
2382
- *
2383
- * Steps.
2384
- * 1. See if there is an outbound queue. If none, outTx has been sent.
2385
- * 2. Find the targetblock the oubound Tx will be sent in for the given inbound Tx hash.
2386
- * 3. Compare that against the current TC height to see the block different then times the TC block time to get seconds.
2387
- *
2388
- * @param inboundTxHash
2389
- * @param lastBlockHeight
2390
- * @returns
2551
+ * Private function to return the date stamp from block height and chain
2552
+ * @param chain - input chain
2553
+ * @param txData - txResponse
2554
+ * @returns date()
2391
2555
  */
2392
- checkOutboundQueue(inboundTxHash, lastBlockHeight) {
2556
+ blockToDate(chain, txData, outboundBlock) {
2393
2557
  return __awaiter(this, void 0, void 0, function* () {
2394
- const stageStatus = { seconds: 0, error: [] };
2395
- const scheduledQueueItem = (yield this.thorchainCache.thornode.getscheduledQueue()).find((item) => item.in_hash === inboundTxHash);
2396
- const scheduledQueueLength = (yield this.thorchainCache.thornode.getscheduledQueue()).length;
2397
- if (scheduledQueueLength > 0 && scheduledQueueItem == undefined) {
2398
- stageStatus.error.push(`Scheduled queue count ${scheduledQueueLength}`);
2399
- stageStatus.error.push(`Could not find tx in outbound queue`);
2558
+ const lastBlockObj = yield this.thorchainCache.thornode.getLastBlock();
2559
+ const time = new Date();
2560
+ let blockDifference;
2561
+ const currentHeight = lastBlockObj.find((obj) => obj.chain == chain);
2562
+ const chainHeight = Number(`${currentHeight === null || currentHeight === void 0 ? void 0 : currentHeight.last_observed_in}`);
2563
+ const recordedChainHeight = Number(`${txData.tx.block_height}`);
2564
+ // If outbound time is required
2565
+ if (outboundBlock) {
2566
+ const currentHeight = lastBlockObj.find((obj) => obj);
2567
+ const thorchainHeight = Number(`${currentHeight === null || currentHeight === void 0 ? void 0 : currentHeight.thorchain}`);
2568
+ if (outboundBlock > thorchainHeight) {
2569
+ blockDifference = outboundBlock - thorchainHeight;
2570
+ time.setSeconds(time.getSeconds() + blockDifference * this.chainAttributes[chain].avgBlockTimeInSecs);
2571
+ console.log(time);
2572
+ }
2573
+ else {
2574
+ blockDifference = thorchainHeight - outboundBlock; // already processed find the date it was completed
2575
+ time.setSeconds(time.getSeconds() - blockDifference * this.chainAttributes[chain].avgBlockTimeInSecs);
2576
+ return time;
2577
+ }
2578
+ }
2579
+ // find out how long ago it was processed for all chains
2580
+ if (chain == THORChain) {
2581
+ const currentHeight = lastBlockObj.find((obj) => obj);
2582
+ const thorchainHeight = Number(`${currentHeight === null || currentHeight === void 0 ? void 0 : currentHeight.thorchain}`); // current height of the TC
2583
+ const finalisedHeight = Number(`${txData.finalised_height}`); // height tx was completed in
2584
+ blockDifference = thorchainHeight - finalisedHeight;
2585
+ time.setSeconds(time.getSeconds() - blockDifference * this.chainAttributes[chain].avgBlockTimeInSecs); // note if using data from a tx that was before a thorchain halt this calculation becomes inaccurate...
2400
2586
  }
2401
2587
  else {
2402
- if ((scheduledQueueItem === null || scheduledQueueItem === void 0 ? void 0 : scheduledQueueItem.height) && (lastBlockHeight === null || lastBlockHeight === void 0 ? void 0 : lastBlockHeight.thorchain)) {
2403
- stageStatus.seconds =
2404
- (scheduledQueueItem.height - (lastBlockHeight === null || lastBlockHeight === void 0 ? void 0 : lastBlockHeight.thorchain)) * this.chainAttributes[THORChain].avgBlockTimeInSecs;
2405
- }
2588
+ // set the time for all other chains
2589
+ blockDifference = chainHeight - recordedChainHeight;
2590
+ time.setSeconds(time.getSeconds() - blockDifference * this.chainAttributes[chain].avgBlockTimeInSecs);
2406
2591
  }
2407
- return stageStatus;
2592
+ return time;
2408
2593
  });
2409
2594
  }
2410
2595
  /**
2411
- * Checks too see if the transaction is synth from the memo
2412
- * @param txData - input txData
2413
- * @returns - boolean
2596
+ * Returns current block height of an asset's native chain
2597
+ * @param chain
2598
+ * @returns
2414
2599
  */
2415
- isSynthTransaction(txData) {
2416
- var _a;
2600
+ blockHeight(asset) {
2417
2601
  return __awaiter(this, void 0, void 0, function* () {
2418
- const memo = (_a = txData.observed_tx) === null || _a === void 0 ? void 0 : _a.tx.memo;
2419
- const synth = (memo === null || memo === void 0 ? void 0 : memo.split(`:`)[1].match(`/`)) ? true : false;
2420
- return synth;
2602
+ const lastBlockObj = yield this.thorchainCache.thornode.getLastBlock();
2603
+ const currentHeight = lastBlockObj.find((obj) => obj.chain == asset.chain);
2604
+ let blockHeight;
2605
+ if (asset.chain === THORChain || asset.synth) {
2606
+ const currentHeight = lastBlockObj.find((obj) => obj);
2607
+ blockHeight = Number(`${currentHeight === null || currentHeight === void 0 ? void 0 : currentHeight.thorchain}`);
2608
+ }
2609
+ else {
2610
+ blockHeight = Number(`${currentHeight === null || currentHeight === void 0 ? void 0 : currentHeight.last_observed_in}`);
2611
+ }
2612
+ return blockHeight;
2421
2613
  });
2422
2614
  }
2423
2615
  }
2424
2616
 
2425
- export { CryptoAmount, LiquidityPool, Midgard, ThorchainCache, ThorchainQuery, Thornode, TransactionStage, calcNetworkFee, getDoubleSwap, getLiquidityProtectionData, getLiquidityUnits, getPoolShare, getSingleSwap, getSlipOnLiquidity };
2617
+ export { AddLpStatus, AddSaverStatus, CryptoAmount, InboundStatus, LiquidityPool, Midgard, SwapStatus, ThorchainCache, ThorchainQuery, Thornode, TransactionStage, TxType, WithdrawStatus, calcNetworkFee, getDoubleSwap, getLiquidityProtectionData, getLiquidityUnits, getPoolShare, getSingleSwap, getSlipOnLiquidity };