{"size": 1382, "ext": "js", "max_stars_repo_path": "src/types/Account/methods/getTransaction.js", "max_stars_repo_name": "DavitSo/wallet-lib", "max_stars_repo_stars_event_min_datetime": "2021-10-07T06:00:55.000Z", "max_stars_repo_stars_event_max_datetime": "2021-10-07T06:00:55.000Z", "max_issues_repo_path": "src/types/Account/methods/getTransaction.js", "max_issues_repo_name": "DavitSo/wallet-lib", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/types/Account/methods/getTransaction.js", "max_forks_repo_name": "DavitSo/wallet-lib", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 30.0434782609, "max_line_length": 87, "alphanum_fraction": 0.7149059334}
/**
 * Get a transaction from a provided txid
 * @param {transactionId} txid - Transaction Hash
 * @return {Promise<{metadata: TransactionMetaData|null, transaction: Transaction}>}
 */
async function getTransaction(txid = null) {
  const searchTransaction = await this.storage.searchTransaction(txid);
  const searchTransactionMetadata = await this.storage.searchTransactionMetadata(txid);
  if (searchTransaction.found) {
    const searchResult = { transaction: searchTransaction.result, metadata: null };
    if (searchTransactionMetadata.found) {
      searchResult.metadata = searchTransactionMetadata.result;
    }
    return searchResult;
  }
  const getTransactionResponse = await this.transport.getTransaction(txid);
  if (!getTransactionResponse) return null;
  const {
    transaction,
    blockHash,
    height,
    instantLocked,
    chainLocked,
  } = getTransactionResponse;

  const metadata = {
    blockHash,
    height,
    instantLocked,
    chainLocked,
  };
  if (this.cacheTx) {
    await this.importTransactions([[transaction, metadata]]);
    if (this.cacheBlockHeaders) {
      const searchBlockHeader = this.storage.searchBlockHeader(height);
      if (!searchBlockHeader.found) {
        // Trigger caching of blockheader
        await this.getBlockHeader(height);
      }
    }
  }
  return { transaction, metadata };
}

module.exports = getTransaction;
