<commit_before>const Block  = require('../../models/block.js');
const Transaction = require('../../models/transaction');
const logger = require('../logger');
const request = require('request');
const config = require('../../config');

const MAX_TXS = 20;
const MAX_BLOCKS = 1;

// Shoe horned in. Not dry, also in blocks. Make db api later
function getBlock(params, options, limit, cb) {
  const defaultOptions = { _id: 0 };

  if (!Number.isInteger(limit)) {
    limit = MAX_BLOCKS;
  }

  Object.assign(defaultOptions, options);

  Block.find(
    params,
    defaultOptions,
    cb)
    .sort({ height: -1 })
    .limit(limit);
}


function getTransactions(params, options, cb) {
  const defaultOptions = { _id: 0 };

  Object.assign(defaultOptions, options);

  Transaction.find(
    params,
    defaultOptions,
    cb)
    .sort({ height: 1 })
    .limit(MAX_TXS);
}

module.exports = function transactionAPI(router) {
  router.get('/tx/:txid', (req, res) => {
    request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/${req.params.txid}`, (err, localRes, body) => {
      if (err) {
        logger.log('error',
          `${err}`);
      }
      try {
        body = JSON.parse(body);
      } catch (e) {
        logger.log('error',
          `${err}`);
      }
      res.send({
        txid: body.hash,
        version: body.version,
        time: body.ps,
        blocktime: body.ps,
        locktime: body.locktime,
        blockhash: body.block,
        fees: body.fee / 1e8,
        valueOut: body.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
        vin: body.inputs.map(input => ({
          addr: input.coin ? input.coin.address : '',
          value: input.coin ? input.coin.value / 1e8 : 0,
        })),
        vout: body.outputs.map(output => ({
          scriptPubKey: {
            addresses: [output.address],
          },
          value: output.value / 1e8,
        })),
        isCoinbase: body.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
      });
    });
  });

  router.get('/txs', (req, res) => {
    if (req.query.block) {
      request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/block/${req.query.block}`, (err, localRes, body) => {
        if (err) {
          logger.log('error',
            `${err}`);
        }
        try {
          body = JSON.parse(body);
        } catch (e) {
          logger.log('error',
            `${err}`);
        }
        res.send({
          pagesTotal: 1,
          txs: body.txs.map(tx => ({
            txid: tx.hash,
            fees: tx.fee / 1e8,
            valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
            vin: tx.inputs.map(input => ({
              addr: input.coin ? input.coin.address : '',
              value: input.coin ? input.coin.value / 1e8 : 0,
            })),
            vout: tx.outputs.map(output => ({
              scriptPubKey: {
                addresses: [output.address],
              },
              value: output.value / 1e8,
            })),
            output: tx.outputs,
          })),
        });
      });
    } else if (req.query.address) {
      request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/address/${req.query.address}`, (err, localRes, body) => {
        if (err) {
          logger.log('error',
            `${err}`);
        }
        try {
          body = JSON.parse(body);
        } catch (e) {
          logger.log('error',
            `${err}`);
        }
        res.send({
          pagesTotal: 1,
          txs: body.map(tx => ({
            txid: tx.hash,
            fees: tx.fee / 1e8,
            valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
            vin: tx.inputs.map(input => ({
              addr: input.coin ? input.coin.address : '',
              value: input.coin ? input.coin.value / 1e8 : 0,
            })),
            vout: tx.outputs.map(output => ({
              scriptPubKey: {
                addresses: [output.address],
              },
              value: output.value / 1e8,
            })),
            output: tx.outputs,
          })),
        });
      });
    } else {
      getTransactions(
        {},
        {},
        (err, txs) => {
          if (err) {
            res.status(501).send();
          }
          res.json({
            pagesTotal: 1,
            txs: txs.map(tx => ({
              txid: tx.hash,
              version: tx.version,
              locktime: tx.locktime,
              vin: tx.inputs.map(input => ({
                coinbase: input.script,
                sequence: input.sequence,
                n: 0,
              })),
              vout: tx.outputs.map(output => ({
                value: output.value,
                n: 0,
                scriptPubKey: {
                  hex: '',
                  asm: '',
                  addresses: [output.address],<commit_msg>Confirmations wired up for blocks txs and addr views<commit_after>const Block  = require('../../models/block.js');
const Transaction = require('../../models/transaction');
const logger = require('../logger');
const request = require('request');
const config = require('../../config');

const MAX_TXS = 20;
const MAX_BLOCKS = 1;

// Shoe horned in. Not dry, also in blocks. Make db api later
function getBlock(params, options, limit, cb) {
  const defaultOptions = { _id: 0 };

  if (!Number.isInteger(limit)) {
    limit = MAX_BLOCKS;
  }

  Object.assign(defaultOptions, options);

  Block.find(
    params,
    defaultOptions,
    cb)
    .sort({ height: -1 })
    .limit(limit);
}


function getTransactions(params, options, cb) {
  const defaultOptions = { _id: 0 };

  Object.assign(defaultOptions, options);

  Transaction.find(
    params,
    defaultOptions,
    cb)
    .sort({ height: 1 })
    .limit(MAX_TXS);
}

module.exports = function transactionAPI(router) {
  router.get('/tx/:txid', (req, res) => {
    getBlock(
      {},
      { height: 1 },
      1,
      (err, block) => {
        if (err) {
          res.status(501).send();
          logger.log('err', err);
        }
        if (block[0]) {
          const height = block[0].height;
          request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/${req.params.txid}`, (err, localRes, body) => {
            if (err) {
              logger.log('error',
                `${err}`);
            }
            try {
              body = JSON.parse(body);
            } catch (e) {
              logger.log('error',
                `${err}`);
            }
            res.send({
              txid: body.hash,
              version: body.version,
              time: body.ps,
              blocktime: body.ps,
              locktime: body.locktime,
              blockhash: body.block,
              fees: body.fee / 1e8,
              confirmations: height - body.height,
              valueOut: body.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
              vin: body.inputs.map(input => ({
                addr: input.coin ? input.coin.address : '',
                value: input.coin ? input.coin.value / 1e8 : 0,
              })),
              vout: body.outputs.map(output => ({
                scriptPubKey: {
                  addresses: [output.address],
                },
                value: output.value / 1e8,
              })),
              isCoinbase: body.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
            });
          });
        }
      });
  });

  // That callback hell
  router.get('/txs', (req, res) => {
    if (req.query.block) {
      getBlock(
        {},
        { height: 1 },
        1,
        (err, block) => {
          if (err) {
            res.status(501).send();
            logger.log('err', err);
          }
          if (block[0]) {
            const height = block[0].height;
            request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/block/${req.query.block}`, (err, localRes, body) => {
              if (err) {
                logger.log('error',
                  `${err}`);
              }
              try {
                body = JSON.parse(body);
              } catch (e) {
                logger.log('error',
                  `${err}`);
              }
              res.send({
                pagesTotal: 1,
                txs: body.txs.map(tx => ({
                  txid: tx.hash,
                  fees: tx.fee / 1e8,
                  confirmations: height - body.height,
                  valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
                  vin: tx.inputs.map(input => ({
                    addr: input.coin ? input.coin.address : '',
                    value: input.coin ? input.coin.value / 1e8 : 0,
                  })),
                  vout: tx.outputs.map(output => ({
                    scriptPubKey: {
                      addresses: [output.address],
                    },
                    value: output.value / 1e8,
                  })),
                  output: tx.outputs,
                })),
              });
            });
          }
        });
    } else if (req.query.address) {
      getBlock(
        {},
        { height: 1 },
        1,
        (err, block) => {
          if (err) {
            res.status(501).send();
            logger.log('err', err);
          }
          if (block[0]) {
            const height = block[0].height;
            request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/address/${req.query.address}`, (err, localRes, body) => {
              if (err) {
                logger.log('error',
                  `${err}`);
              }
              try {
                body = JSON.parse(body);
              } catch (e) {
                logger.log('error',
                  `${err}`);
              }
              res.send({
                pagesTotal: 1,
                txs: body.map(tx => ({
                  txid: tx.hash,
                  fees: tx.fee / 1e8,
                  confirmations: height - tx.height,
                  valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
                  vin: tx.inputs.map(input => ({
                    addr: input.coin ? input.coin.address : '',
                    value: input.coin ? input.coin.value / 1e8 : 0,
                  })),
                  vout: tx.outputs.map(output => ({
                    scriptPubKey: {
                      addresses: [output.address],
                    },
                    value: output.value / 1e8,
                  })),
                  output: tx.outputs,
                })),
              });
            });
          }
        });
    } else {
      getTransactions(
        {},
        {},
        (err, txs) => {
          if (err) {
            res.status(501).send();
          }
          res.json({
            pagesTotal: 1,
            txs: txs.map(tx => ({
              txid: tx.hash,
              version: tx.version,
              locktime: tx.locktime,
              vin: tx.inputs.map(input => ({
                coinbase: input.script,
                sequence: input.sequence,
                n: 0,
              })),
              vout: tx.outputs.map(output => ({
                value: output.value,
                n: 0,
                scriptPubKey: {
                  hex: '',
                  asm: '',
                  addresses: [output.address],
{'fraction_non_alphanumeric': 0.15518585911099558, 'fraction_numerical': 0.020882072610692314, 'mean_word_length': 1.3028731045490822, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2203890', 'n_tokens_mistral': 3558, 'n_tokens_neox': 3317, 'n_words': 643}