how to query data from hyperledger
blockchain
class BlockchainService {
async getBlock(blockNumber: number):
Promise<Block> {
const peer =
fabricClient.getPeer('peer0.org1.example.
com');
return await
peer.queryBlock(blockNumber);
}
}
Key Components to Replace Explorer
Functionality
1 Chaincode Query FunctionsImplement these in your
smart contracts:golang// Get transaction by ID
2 func (s *SmartContract) GetTx(ctx Context, txId string) (TxRecord,
error) {
3 return ctx.GetStub().GetTransactionByID(txId)
4 }
5
6 // Get block by number
7 func (s *SmartContract) GetBlock(ctx Context, blockNum uint64)
(Block, error) {
8 return ctx.GetStub().GetBlockByNumber(blockNum)
9 }
10 Optimized Backend Servicestypescriptclass
BlockchainService {
11 async getBlock(blockNumber: number): Promise<Block> {
12 const peer = fabricClient.getPeer('peer0.org1.example.com');
13 return await peer.queryBlock(blockNumber);
14 }
15 }
16 Client-Side FilteringProcess raw data in frontend:
javascript// Frontend filtering example
17 const filterTransactions = (txns, status) =>
18 txns.filter(tx =>
19 tx.status === status &&
20 tx.timestamp > Date.now() - 30*24*3600*1000
21 );