Fralgo
June 21, 2022, 9:43am
1
I am trying to retrieve Algorand transactions using the v2/blocks/{round} endpoint, but the JSON response does not contain any transaction or block hashes. Is there any way to construct these using the data in the response without using the SDKs?
Check this out: vote-coin-indexer/computeTransactionId.ts at master · scholtz/vote-coin-indexer · GitHub
//console.log('block.block', block.block);
if (block.block.txns) {
let countProcessedTxns = 0;
//console.log('block.block.txns', block.block.txns.length);
for (const stxn of block.block.txns) {
countProcessedTxns++;
try {
if (!stxn.txn.note) continue;
const note = Buffer.from(stxn.txn.note).toString('utf-8');
if (!note.startsWith('avote-')) continue;
const txId = computeTransactionId(block.block.gh, block.block.gen, stxn);
const pos = note.indexOf(':');
let messageType = note.substring(0, pos);
const posSlash = note.indexOf('/');
const posSlash2 = note.indexOf('/', posSlash + 1);
if (posSlash2 > 0) {
messageType = messageType.substring(0, posSlash2);
}
//console.log('posSlash2', posSlash2);
const dataStr = note.substring(pos + 2);
//console.log(dataStr)
Another solution is to query the indexer rather than the algod endpoint.
The indexer computes the transaction ID for you.
Fralgo
June 22, 2022, 11:44am
4
Unfortunately, the indexer only seems to give the previous block hash and not the current one so that doesn’t solve every issue.
I read too fast, I thought you meant transaction ID for some reason.
You can get the current block hash as shown there:
This is available in the certificate of the block that you can get by querying the block as msgpack (?format=msgpack).
In JS:
const algosdk = require("algosdk");
const base32 = require("hi-base32");
const c = new algosdk.Algodv2("", "https://testnet.algoexplorerapi.io", "");
(async () => {
const blk = await c.block(13819291).do();
const blk_hash = base32.encode(blk["cert"]["prop"]["dig"]);
console.log(blk_hash);
})();
Other SDKs work similarly, the client methods are: GetBlock for Jav…