import { Gateway, Wallets } from 'fabric-network';
import * as common from 'fabric-common';
import * as fabproto6 from 'fabric-protos';
import * as path from 'path';
import * as fs from 'fs';
import { createQueryHandler } from './config/queryhandler/QueryHandler';
//const BlockDecoder = (common as any).BlockDecoder;
async function main() {
try {
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'Org1Wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
const connectionProfilePath = path.resolve(__dirname, '..', 'connection.json');
const connectionProfile = JSON.parse(fs.readFileSync(connectionProfilePath, 'utf8'));
const connectionOptions = {
wallet,
identity: 'Org1 Admin',
discovery: { enabled: true, asLocalhost: true },
queryHandlerOptions: {
timeout: 10, // timeout in seconds
strategy: createQueryHandler,
},
};
await gateway.connect(connectionProfile, connectionOptions);
// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('mychannel');
// Get the contract from the network.
const contract = network.getContract('qscc');
// Submit the specified transaction.
const currentTimeBefore = new Date().getMilliseconds();
const result = await contract.evaluateTransaction('GetChainInfo', 'mychannel');
const blockProto = JSON.stringify(fabproto6.common.BlockchainInfo.decode(result));
console.log(`# GetChainInfo: ${blockProto} \n`);
const resultGetBlockByNumber = await contract.evaluateTransaction('GetBlockByNumber', 'mychannel', '5');
//const resultDecoded = JSON.stringify(BlockDecoder.decode(resultGetBlockByNumber));
const resultDecoded = JSON.stringify(fabproto6.common.Block.decode(resultGetBlockByNumber));
const currentTimeAfter = new Date().getMilliseconds();
console.log(`# GetBlockByNumber: ${resultDecoded} \n`);
const totalRunTime = (currentTimeAfter - currentTimeBefore) / 1000;
console.log(`#==> Totaltime: ${totalRunTime} seconds.`);
// Disconnect from the gateway.
gateway.disconnect();
} catch (error) {
if (error instanceof Error) {
console.error(error.stack);
}
console.error('RED ALERT, SHIELDS UP, PHASER CHARGED:', error);
process.exit(1);
}
}
main();