Skip to content

Commit

Permalink
Merge pull request #2378 from bsnowden3/input-w-tx-endpoint
Browse files Browse the repository at this point in the history
Transaction endpoint w/ inputs and outputs
  • Loading branch information
matiu authored Sep 17, 2019
2 parents a6a8572 + 68db0f4 commit a51533a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/bitcore-node/src/routes/api/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { CSP } from '../../types/namespaces/ChainStateProvider';
import { ChainStateProvider } from '../../providers/chain-state';
import logger from '../../logger';
import { CacheTimes } from '../middleware';
import { ITransaction } from '../../models/transaction';
import { ICoin } from '../../models/coin';

const router = Router({ mergeParams: true });

router.get('/', function(req, res) {
Expand Down Expand Up @@ -57,6 +60,40 @@ router.get('/:txId', async (req, res) => {
}
});

// Get transaction with input and outputs, assigned to key coins
router.get('/:txId/populated', async (req, res) => {
let { chain, network, txId } = req.params;
let txid = txId;
if (typeof txid !== 'string' || !chain || !network) {
return res.status(400).send('Missing required param');
}

try {
let tx: ITransaction & { blockHeight: number, coins?: Array<ICoin> };
let coins: any;
let tip: any;

[tx, coins, tip] = await Promise.all([ChainStateProvider.getTransaction({ chain, network, txId }), ChainStateProvider.getCoinsForTx({ chain, network, txid }),
ChainStateProvider.getLocalTip({ chain, network })]);

if (!tx) {
return res.status(404).send(`The requested txid ${txid} could not be found.`);
} else {
if (tx && tip && tip.height - tx.blockHeight > 100) {
SetCache(res, CacheTimes.Month);
}

if (!coins) {
res.status(404).send(`The requested coins for txid ${txid} could not be found.`);
}
tx.coins = coins;
return res.send(tx);
}
} catch (err) {
return res.status(500).send(err);
}
});

router.get('/:txId/authhead', async (req, res) => {
let { chain, network, txId } = req.params;
if (typeof txId !== 'string' || !chain || !network) {
Expand Down

0 comments on commit a51533a

Please sign in to comment.