<commit_before>import express = require('express');
const router = express.Router({ mergeParams: true });
import { ChainStateProvider } from '../../providers/chain-state';
import { CoinStorage } from '../../models/coin';

router.get('/:address/txs', function(req, res) {
  let { address, chain, network } = req.params;
  let { unspent, limit = 10, since } = req.query;
  let payload = {
    chain,
    network,
    address,
    req,
    res,
    args: { ...req.query, unspent, limit, since }
  };
  ChainStateProvider.streamAddressTransactions(payload);
});

router.get('/:address', function(req, res) {
  let { address, chain, network } = req.params;
  let { unspent, limit = 10, since } = req.query;
  let payload = {
    chain,
    network,
    address,
    req,
    res,
    args: { unspent, limit, since }
  };
  ChainStateProvider.streamAddressUtxos(payload);
});

router.get('/:address/coins', async function (req, res) {
  let { address, chain, network } = req.params;
  // let { unspent, limit = 10, since } = req.query;
  try {
    let coins = await CoinStorage.collection.find({address, chain, network}).toArray();

    const mintTxids : string[] = [];
    const spentTxids : string[] = [];
    const mintTxidIndexes = {};
    const spentTxidIndexes = {};

    coins.forEach((coin) => {
      if(coin.mintTxid) {
        mintTxids.push(coin.mintTxid);
      } 
      
      if(coin.spentTxid) {
        spentTxids.push(coin.spentTxid);
      }

    });

    const mintTxidCoinPromises = mintTxids.map(function (txid, index) {
      mintTxidIndexes[index] = txid;
      return ChainStateProvider.getCoinsForTx({ chain, network, txid });
    });

    const spentTxidCoinPromises = spentTxids.map(function (txid, index) {
      spentTxidIndexes[index] = txid;
      return ChainStateProvider.getCoinsForTx({ chain, network, txid });
    });

    let mintTxidCoins = await Promise.all(mintTxidCoinPromises).then(
      (data) => {
        let response: any = {};
        data.forEach((coin, index) => {
          let txid: string = mintTxidIndexes[index];
          response[txid] = coin;
        });
        return response;
      }
    );

    let spentTxidCoins = await Promise.all(spentTxidCoinPromises).then(
      (data) => {
        let response: any = {};
        data.forEach((coin, index) => {
          let txid: string = spentTxidIndexes[index];
          response[txid] = coin;
        });
        return response;
      }
    );
    return res.json({mintTxidCoins, spentTxidCoins});
  } catch(err) {
    return res.status(500).send(err);
  }
  
});

router.get('/:address/balance', async function(req, res) {
  let { address, chain, network } = req.params;
  try {
    let result = await ChainStateProvider.getBalanceForAddress({
      chain,
      network,
      address,
      args: req.query
    });
    return res.send(result || { confirmed: 0, unconfirmed: 0, balance: 0 });
  } catch (err) {
    return res.status(500).send(err);
  }
});

module.exports = {
  router: router,
  path: '/address'
};<commit_msg>update :address/coins efficiency<commit_after>import express = require('express');
const router = express.Router({ mergeParams: true });
import { ChainStateProvider } from '../../providers/chain-state';
import { CoinStorage } from '../../models/coin';

router.get('/:address/txs', function(req, res) {
  let { address, chain, network } = req.params;
  let { unspent, limit = 10, since } = req.query;
  let payload = {
    chain,
    network,
    address,
    req,
    res,
    args: { ...req.query, unspent, limit, since }
  };
  ChainStateProvider.streamAddressTransactions(payload);
});

router.get('/:address', function(req, res) {
  let { address, chain, network } = req.params;
  let { unspent, limit = 10, since } = req.query;
  let payload = {
    chain,
    network,
    address,
    req,
    res,
    args: { unspent, limit, since }
  };
  ChainStateProvider.streamAddressUtxos(payload);
});

router.get('/:address/coins', async function (req, res) {
  let { address, chain, network } = req.params;

  try {
    let coins = await CoinStorage.collection.find({ address, chain, network }).toArray(); //rename coins
    let spentTxids = coins.filter((tx) => { return tx.spentTxid; }).map((tx) => tx.spentTxid);
    let mintedTxids = coins.filter((tx) => { return tx.mintTxid; }).map((tx) => tx.mintTxid);;

    let fundingTxInputs = await CoinStorage.collection.find({ chain, network, spentTxid: { $in: mintedTxids } }).toArray();
    let fundingTxOutputs = await CoinStorage.collection.find({ chain, network, mintTxid: { $in: mintedTxids } }).toArray();

    let spendingTxInputs: any = [];
    let spendingTxOutputs: any = [];

    if (!(spentTxids === null)) {
      spendingTxInputs = await CoinStorage.collection.find({ chain, network, spentTxid: { $in: spentTxids } }).toArray();
      spendingTxOutputs = await CoinStorage.collection.find({ chain, network, mintTxid: { $in: coins.map(c => c.spentTxid) } }).toArray();
    }
    return res.json({ coins, mintedTxids, fundingTxInputs, fundingTxOutputs, spentTxids, spendingTxInputs, spendingTxOutputs });
  } catch (err) {
    return res.status(500).send(err);
  }
});

router.get('/:address/balance', async function(req, res) {
  let { address, chain, network } = req.params;
  try {
    let result = await ChainStateProvider.getBalanceForAddress({
      chain,
      network,
      address,
      args: req.query
    });
    return res.send(result || { confirmed: 0, unconfirmed: 0, balance: 0 });
  } catch (err) {
    return res.status(500).send(err);
  }
});

module.exports = {
  router: router,
  path: '/address'
};
{'fraction_non_alphanumeric': 0.15516014234875444, 'fraction_numerical': 0.00498220640569395, 'mean_word_length': 3.088, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '7142580', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1813, 'n_words': 412}