Files
js/examples/transaction-short.js
T
2019-12-26 22:05:59 +01:00

50 lines
1.6 KiB
JavaScript

var Flowee = require('bindings')('flowee')
/*
* This example uses the simplified search.
* It fetches a transaction by ID and the matching block-header for that transaction.
*
* The search() method gets a simple array with "jobs".
*/
if (process.argv.length <= 2) {
console.log("Fetch transaction\nUsage node transaction.js [txid]");
console.log("Example address: b39fa6c39b99683ac8f456721b270786c627ecb246700888315991877024b983\n");
process.exit();
}
function detailTx(txid) {
return Flowee.search([
{
// this job takes a txid and fetches it.
type: Flowee.Job.FetchTx,
value: txid,
txFilter: [ Flowee.IncludeInputs, Flowee.IncludeOutputs ]
},
{
// this job fetches the block header.
// This job type needs a block height as a value.
// Instead of passing it in, the Flowee search method will realize it can reuse the
// blockheight that the previous (the FetchTx above) job returns anyway.
type: Flowee.Job.FetchBlockHeader
}
]);
}
Flowee.connect().then(async function() {
try {
for (arg in process.argv) {
if (arg > 1) { // nodejs, skip first two args.
let result = await detailTx(process.argv[arg])
console.log("Fetched transaction");
console.log(result.transactions[0]);
console.log("BlockHeader:");
console.log(result[`block${result.transactions[0].blockHeight}`]);
}
}
} catch (error) {
console.log("Failure: " + error);
}
process.exit();
});