@xchainjs/xchain-thorchain 0.28.3 → 0.28.5

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.js CHANGED
@@ -9898,8 +9898,12 @@ const getDepositTxDataFromLogs = (logs, address, senderAsset, receiverAsset) =>
9898
9898
  newData.sender = value;
9899
9899
  if (key === 'recipient')
9900
9900
  newData.recipient = value;
9901
- if (key === 'amount')
9902
- newData.amount = xchainUtil.baseAmount(value.replace(/rune/, ''), RUNE_DECIMAL);
9901
+ if (key === 'amount') {
9902
+ const amountAsset = value.match(/(\d+)(\D+)/);
9903
+ if (amountAsset) {
9904
+ newData.amount = xchainUtil.baseAmount(parseInt(amountAsset[1]));
9905
+ }
9906
+ }
9903
9907
  return acc2;
9904
9908
  }, acc);
9905
9909
  }
@@ -10236,7 +10240,10 @@ class Client extends xchainClient.BaseXChainClient {
10236
10240
  const txs = [];
10237
10241
  for (let i = 0; i < history.length; i += 10) {
10238
10242
  const batch = history.slice(i, i + 10);
10239
- const result = yield Promise.all(batch.map(({ hash }) => this.getTransactionData(hash, address)));
10243
+ const result = yield Promise.all(batch.map(({ hash }) => __awaiter(this, void 0, void 0, function* () {
10244
+ const data = yield this.getTransactionData(hash, address);
10245
+ return data;
10246
+ })));
10240
10247
  txs.push(...result);
10241
10248
  xchainUtil.delay(2000); // Delay to avoid 503 from ninerealms server
10242
10249
  }
@@ -10469,65 +10476,91 @@ class Client extends xchainClient.BaseXChainClient {
10469
10476
  * @returns {Tx} The transaction details of the given transaction id.
10470
10477
  */
10471
10478
  getTransactionData(txId, address) {
10472
- var _a, _b, _c;
10479
+ var _a, _b, _c, _d, _e, _f;
10473
10480
  return __awaiter(this, void 0, void 0, function* () {
10474
- const response = yield this.fetchTransaction(txId);
10475
- if (response) {
10476
- const txResult = response;
10477
- const bond = txResult.logs && txResult.logs[0].events.filter((i) => i.type === 'bond');
10478
- const transfer = txResult.logs && txResult.logs[0].events.filter((i) => i.type === 'transfer');
10479
- if (!transfer)
10480
- throw new Error(`Failed to get transaction logs (tx-hash: ${txId})`);
10481
- const sender = (_a = transfer[0].attributes.find((attr) => attr.key === 'sender')) === null || _a === void 0 ? void 0 : _a.value;
10482
- const senderAmount = transfer[0].attributes.filter((attr) => attr.key === 'amount')[1].value;
10483
- const regex = /[a-zA-Z]+$/;
10484
- const match = senderAmount.match(regex);
10485
- const asset = match ? match[0] : null;
10486
- const senderAsset = asset === 'rune' ? AssetRuneNative : xchainUtil.assetFromStringEx(`${asset}`);
10487
- const senderAddress = sender ? sender : address;
10488
- const message = txResult.logs && txResult.logs[0].events.filter((i) => i.type === 'message');
10489
- const coinSpent = txResult.logs && txResult.logs[0].events.filter((i) => i.type === 'coin_spent');
10490
- if (!message || !coinSpent)
10491
- throw new Error(`Failed to get transaction logs (tx-hash: ${txId})`);
10492
- const action = (_b = message[0].attributes.find((attr) => attr.key === 'action')) === null || _b === void 0 ? void 0 : _b.value;
10493
- if (!bond)
10494
- throw new Error(`Failed to get transaction logs (tx-hash: ${txId})`);
10495
- // Rune only transactions
10496
- if (action === 'send' || bond[0].type === 'bond') {
10497
- const assetTo = AssetRuneNative;
10498
- const txData = txResult && txResult.logs
10499
- ? getDepositTxDataFromLogs(txResult.logs, `${senderAddress}`, senderAsset, assetTo)
10481
+ const txResult = yield this.fetchTransaction(txId);
10482
+ if (txResult && txResult.logs) {
10483
+ // extract values from the response
10484
+ const transferEvent = (_a = txResult.logs[0].events) === null || _a === void 0 ? void 0 : _a.find((event) => event.type === 'transfer');
10485
+ const messageEvent = (_b = txResult.logs[0].events) === null || _b === void 0 ? void 0 : _b.find((event) => event.type === 'message');
10486
+ if (!transferEvent || !messageEvent) {
10487
+ throw new Error('Invalid transaction data');
10488
+ }
10489
+ const attributeGroups = {};
10490
+ for (const attr of transferEvent.attributes) {
10491
+ if (!attributeGroups[attr.key]) {
10492
+ attributeGroups[attr.key] = [];
10493
+ }
10494
+ attributeGroups[attr.key].push(attr.value);
10495
+ }
10496
+ const assetAmount = attributeGroups['amount'][1]
10497
+ ? attributeGroups['amount'][1].split(/(?<=\d)(?=\D)/).filter(Boolean)[0]
10498
+ : attributeGroups['amount'][0].split(/(?<=\d)(?=\D)/).filter(Boolean)[0];
10499
+ const assetString = attributeGroups['amount'][1]
10500
+ ? attributeGroups['amount'][1]
10501
+ .split(/(?<=\d)(?=\D)/)
10502
+ .filter(Boolean)[1]
10503
+ .replace(/[a-z]/g, (letter) => letter.toUpperCase())
10504
+ : attributeGroups['amount'][0]
10505
+ .split(/(?<=\d)(?=\D)/)
10506
+ .filter(Boolean)[1]
10507
+ .replace(/[a-z]/g, (letter) => letter.toUpperCase());
10508
+ const fromAddress = ((_c = transferEvent.attributes.find((attr) => attr.key === 'sender')) === null || _c === void 0 ? void 0 : _c.value)
10509
+ ? (_d = transferEvent.attributes.find((attr) => attr.key === 'sender')) === null || _d === void 0 ? void 0 : _d.value
10510
+ : address;
10511
+ const memo = ((_e = txResult.tx) === null || _e === void 0 ? void 0 : _e.body) ? txResult.tx.body.memo.split(':') : '';
10512
+ const toAddress = memo[2] ? memo[2] : '';
10513
+ const toAsset = memo[1] ? xchainUtil.assetFromStringEx(memo[1]) : AssetRuneNative;
10514
+ const date = new Date(txResult.timestamp);
10515
+ const typeString = (_f = messageEvent.attributes.find((attr) => attr.key === 'action')) === null || _f === void 0 ? void 0 : _f.value;
10516
+ const hash = txResult.txhash;
10517
+ if (assetString && hash && fromAddress && typeString) {
10518
+ const fromAsset = assetString === 'RUNE' ? AssetRuneNative : xchainUtil.assetFromStringEx(assetString);
10519
+ const txData = txResult && txResult.raw_log
10520
+ ? getDepositTxDataFromLogs(txResult.logs, `${fromAddress}`, fromAsset, toAsset)
10500
10521
  : null;
10501
- //console.log(JSON.stringify(txData, null, 2))
10502
10522
  if (!txData)
10503
10523
  throw new Error(`Failed to get transaction data (tx-hash: ${txId})`);
10504
- const { from, to, type } = txData;
10505
- return {
10506
- hash: txId,
10507
- asset: senderAsset,
10508
- from,
10509
- to,
10510
- date: new Date(txResult.timestamp),
10511
- type,
10524
+ if (isAssetRuneNative(toAsset) || toAsset.synth) {
10525
+ const { from, to, type } = txData;
10526
+ const tx = {
10527
+ asset: fromAsset,
10528
+ from: from,
10529
+ to: to,
10530
+ date: date,
10531
+ type: type,
10532
+ hash: hash,
10533
+ };
10534
+ return tx;
10535
+ }
10536
+ else {
10537
+ const tx = {
10538
+ asset: fromAsset,
10539
+ from: [{ from: fromAddress, amount: xchainUtil.baseAmount(assetAmount), asset: fromAsset }],
10540
+ to: [{ to: toAddress, amount: xchainUtil.baseAmount(memo[3]), asset: toAsset }],
10541
+ date: date,
10542
+ type: xchainClient.TxType.Transfer,
10543
+ hash: hash,
10544
+ };
10545
+ return tx;
10546
+ }
10547
+ }
10548
+ else {
10549
+ const tx = {
10550
+ asset: {
10551
+ chain: '',
10552
+ symbol: '',
10553
+ ticker: '',
10554
+ synth: false,
10555
+ },
10556
+ from: [],
10557
+ to: [],
10558
+ date: new Date(),
10559
+ type: xchainClient.TxType.Transfer,
10560
+ hash: '',
10512
10561
  };
10562
+ return tx;
10513
10563
  }
10514
- // synths and other tx types
10515
- const messageBody = JSON.stringify((_c = txResult.tx) === null || _c === void 0 ? void 0 : _c.body.messages).split(':');
10516
- const assetTo = xchainUtil.assetFromStringEx(messageBody[7]);
10517
- const txData = txResult && txResult.logs
10518
- ? getDepositTxDataFromLogs(txResult.logs, `${senderAddress}`, senderAsset, assetTo)
10519
- : null;
10520
- if (!txData)
10521
- throw new Error(`Failed to get transaction data (tx-hash: ${txId})`);
10522
- const { from, to, type } = txData;
10523
- return {
10524
- hash: txId,
10525
- asset: senderAsset,
10526
- from,
10527
- to,
10528
- date: new Date(txResult.timestamp),
10529
- type,
10530
- };
10531
10564
  }
10532
10565
  else {
10533
10566
  return yield this.getTransactionDataThornode(txId);