@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/client.d.ts CHANGED
@@ -177,7 +177,7 @@ declare class Client extends BaseXChainClient implements ThorchainClient, XChain
177
177
  * @param {string} txId The transaction id.
178
178
  * @returns {Tx} The transaction details of the given transaction id.
179
179
  */
180
- getTransactionData(txId: string, address?: Address): Promise<Tx>;
180
+ getTransactionData(txId: string, address?: string): Promise<Tx>;
181
181
  /** This function is used when in bound or outbound tx is not of thorchain
182
182
  *
183
183
  * @param txId - transaction hash
package/lib/index.esm.js CHANGED
@@ -9870,8 +9870,12 @@ const getDepositTxDataFromLogs = (logs, address, senderAsset, receiverAsset) =>
9870
9870
  newData.sender = value;
9871
9871
  if (key === 'recipient')
9872
9872
  newData.recipient = value;
9873
- if (key === 'amount')
9874
- newData.amount = baseAmount(value.replace(/rune/, ''), RUNE_DECIMAL);
9873
+ if (key === 'amount') {
9874
+ const amountAsset = value.match(/(\d+)(\D+)/);
9875
+ if (amountAsset) {
9876
+ newData.amount = baseAmount(parseInt(amountAsset[1]));
9877
+ }
9878
+ }
9875
9879
  return acc2;
9876
9880
  }, acc);
9877
9881
  }
@@ -10208,7 +10212,10 @@ class Client extends BaseXChainClient {
10208
10212
  const txs = [];
10209
10213
  for (let i = 0; i < history.length; i += 10) {
10210
10214
  const batch = history.slice(i, i + 10);
10211
- const result = yield Promise.all(batch.map(({ hash }) => this.getTransactionData(hash, address)));
10215
+ const result = yield Promise.all(batch.map(({ hash }) => __awaiter(this, void 0, void 0, function* () {
10216
+ const data = yield this.getTransactionData(hash, address);
10217
+ return data;
10218
+ })));
10212
10219
  txs.push(...result);
10213
10220
  delay(2000); // Delay to avoid 503 from ninerealms server
10214
10221
  }
@@ -10441,65 +10448,91 @@ class Client extends BaseXChainClient {
10441
10448
  * @returns {Tx} The transaction details of the given transaction id.
10442
10449
  */
10443
10450
  getTransactionData(txId, address) {
10444
- var _a, _b, _c;
10451
+ var _a, _b, _c, _d, _e, _f;
10445
10452
  return __awaiter(this, void 0, void 0, function* () {
10446
- const response = yield this.fetchTransaction(txId);
10447
- if (response) {
10448
- const txResult = response;
10449
- const bond = txResult.logs && txResult.logs[0].events.filter((i) => i.type === 'bond');
10450
- const transfer = txResult.logs && txResult.logs[0].events.filter((i) => i.type === 'transfer');
10451
- if (!transfer)
10452
- throw new Error(`Failed to get transaction logs (tx-hash: ${txId})`);
10453
- const sender = (_a = transfer[0].attributes.find((attr) => attr.key === 'sender')) === null || _a === void 0 ? void 0 : _a.value;
10454
- const senderAmount = transfer[0].attributes.filter((attr) => attr.key === 'amount')[1].value;
10455
- const regex = /[a-zA-Z]+$/;
10456
- const match = senderAmount.match(regex);
10457
- const asset = match ? match[0] : null;
10458
- const senderAsset = asset === 'rune' ? AssetRuneNative : assetFromStringEx(`${asset}`);
10459
- const senderAddress = sender ? sender : address;
10460
- const message = txResult.logs && txResult.logs[0].events.filter((i) => i.type === 'message');
10461
- const coinSpent = txResult.logs && txResult.logs[0].events.filter((i) => i.type === 'coin_spent');
10462
- if (!message || !coinSpent)
10463
- throw new Error(`Failed to get transaction logs (tx-hash: ${txId})`);
10464
- const action = (_b = message[0].attributes.find((attr) => attr.key === 'action')) === null || _b === void 0 ? void 0 : _b.value;
10465
- if (!bond)
10466
- throw new Error(`Failed to get transaction logs (tx-hash: ${txId})`);
10467
- // Rune only transactions
10468
- if (action === 'send' || bond[0].type === 'bond') {
10469
- const assetTo = AssetRuneNative;
10470
- const txData = txResult && txResult.logs
10471
- ? getDepositTxDataFromLogs(txResult.logs, `${senderAddress}`, senderAsset, assetTo)
10453
+ const txResult = yield this.fetchTransaction(txId);
10454
+ if (txResult && txResult.logs) {
10455
+ // extract values from the response
10456
+ const transferEvent = (_a = txResult.logs[0].events) === null || _a === void 0 ? void 0 : _a.find((event) => event.type === 'transfer');
10457
+ const messageEvent = (_b = txResult.logs[0].events) === null || _b === void 0 ? void 0 : _b.find((event) => event.type === 'message');
10458
+ if (!transferEvent || !messageEvent) {
10459
+ throw new Error('Invalid transaction data');
10460
+ }
10461
+ const attributeGroups = {};
10462
+ for (const attr of transferEvent.attributes) {
10463
+ if (!attributeGroups[attr.key]) {
10464
+ attributeGroups[attr.key] = [];
10465
+ }
10466
+ attributeGroups[attr.key].push(attr.value);
10467
+ }
10468
+ const assetAmount = attributeGroups['amount'][1]
10469
+ ? attributeGroups['amount'][1].split(/(?<=\d)(?=\D)/).filter(Boolean)[0]
10470
+ : attributeGroups['amount'][0].split(/(?<=\d)(?=\D)/).filter(Boolean)[0];
10471
+ const assetString = attributeGroups['amount'][1]
10472
+ ? attributeGroups['amount'][1]
10473
+ .split(/(?<=\d)(?=\D)/)
10474
+ .filter(Boolean)[1]
10475
+ .replace(/[a-z]/g, (letter) => letter.toUpperCase())
10476
+ : attributeGroups['amount'][0]
10477
+ .split(/(?<=\d)(?=\D)/)
10478
+ .filter(Boolean)[1]
10479
+ .replace(/[a-z]/g, (letter) => letter.toUpperCase());
10480
+ const fromAddress = ((_c = transferEvent.attributes.find((attr) => attr.key === 'sender')) === null || _c === void 0 ? void 0 : _c.value)
10481
+ ? (_d = transferEvent.attributes.find((attr) => attr.key === 'sender')) === null || _d === void 0 ? void 0 : _d.value
10482
+ : address;
10483
+ const memo = ((_e = txResult.tx) === null || _e === void 0 ? void 0 : _e.body) ? txResult.tx.body.memo.split(':') : '';
10484
+ const toAddress = memo[2] ? memo[2] : '';
10485
+ const toAsset = memo[1] ? assetFromStringEx(memo[1]) : AssetRuneNative;
10486
+ const date = new Date(txResult.timestamp);
10487
+ const typeString = (_f = messageEvent.attributes.find((attr) => attr.key === 'action')) === null || _f === void 0 ? void 0 : _f.value;
10488
+ const hash = txResult.txhash;
10489
+ if (assetString && hash && fromAddress && typeString) {
10490
+ const fromAsset = assetString === 'RUNE' ? AssetRuneNative : assetFromStringEx(assetString);
10491
+ const txData = txResult && txResult.raw_log
10492
+ ? getDepositTxDataFromLogs(txResult.logs, `${fromAddress}`, fromAsset, toAsset)
10472
10493
  : null;
10473
- //console.log(JSON.stringify(txData, null, 2))
10474
10494
  if (!txData)
10475
10495
  throw new Error(`Failed to get transaction data (tx-hash: ${txId})`);
10476
- const { from, to, type } = txData;
10477
- return {
10478
- hash: txId,
10479
- asset: senderAsset,
10480
- from,
10481
- to,
10482
- date: new Date(txResult.timestamp),
10483
- type,
10496
+ if (isAssetRuneNative(toAsset) || toAsset.synth) {
10497
+ const { from, to, type } = txData;
10498
+ const tx = {
10499
+ asset: fromAsset,
10500
+ from: from,
10501
+ to: to,
10502
+ date: date,
10503
+ type: type,
10504
+ hash: hash,
10505
+ };
10506
+ return tx;
10507
+ }
10508
+ else {
10509
+ const tx = {
10510
+ asset: fromAsset,
10511
+ from: [{ from: fromAddress, amount: baseAmount(assetAmount), asset: fromAsset }],
10512
+ to: [{ to: toAddress, amount: baseAmount(memo[3]), asset: toAsset }],
10513
+ date: date,
10514
+ type: TxType.Transfer,
10515
+ hash: hash,
10516
+ };
10517
+ return tx;
10518
+ }
10519
+ }
10520
+ else {
10521
+ const tx = {
10522
+ asset: {
10523
+ chain: '',
10524
+ symbol: '',
10525
+ ticker: '',
10526
+ synth: false,
10527
+ },
10528
+ from: [],
10529
+ to: [],
10530
+ date: new Date(),
10531
+ type: TxType.Transfer,
10532
+ hash: '',
10484
10533
  };
10534
+ return tx;
10485
10535
  }
10486
- // synths and other tx types
10487
- const messageBody = JSON.stringify((_c = txResult.tx) === null || _c === void 0 ? void 0 : _c.body.messages).split(':');
10488
- const assetTo = assetFromStringEx(messageBody[7]);
10489
- const txData = txResult && txResult.logs
10490
- ? getDepositTxDataFromLogs(txResult.logs, `${senderAddress}`, senderAsset, assetTo)
10491
- : null;
10492
- if (!txData)
10493
- throw new Error(`Failed to get transaction data (tx-hash: ${txId})`);
10494
- const { from, to, type } = txData;
10495
- return {
10496
- hash: txId,
10497
- asset: senderAsset,
10498
- from,
10499
- to,
10500
- date: new Date(txResult.timestamp),
10501
- type,
10502
- };
10503
10536
  }
10504
10537
  else {
10505
10538
  return yield this.getTransactionDataThornode(txId);