'use strict' ; import * as vscode from 'vscode' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { IBlockchainQuickPickItem , UserInputUtil } from './UserInputUtil' ; import { LogType , IFabricGatewayConnection } from 'ibm-blockchain-platform-common' ; import { InstantiatedTreeItem } from '../explorer/model/InstantiatedTreeItem' ; import { ContractTreeItem } from '../explorer/model/ContractTreeItem' ; import { FabricGatewayConnectionManager } from '../fabric/FabricGatewayConnectionManager' ; import { ExtensionCommands } from '../../ExtensionCommands' ; export async function subscribeToEvent ( chaincode ? ) { let chosenChaincode ; let chaincodeName ; let chaincodeLabel ; let channelName ; const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , 'subscribeToEventCommand' ) ; if ( ! chaincode ) { if ( ! FabricGatewayConnectionManager . instance ( ) . getConnection ( ) ) { await vscode . commands . executeCommand ( ExtensionCommands . CONNECT_TO_GATEWAY ) ; if ( ! FabricGatewayConnectionManager . instance ( ) . getConnection ( ) ) { return ; } } chosenChaincode = await UserInputUtil . showClientInstantiatedSmartContractsQuickPick ( 'Choose the smart contract you want to subscribe to events from' ) ; if ( ! chosenChaincode ) { return ; } chaincodeLabel = chosenChaincode . label ; chaincodeName = chosenChaincode . data . name ; channelName = chosenChaincode . data . channel ; } else { if ( chaincode instanceof ContractTreeItem ) { chaincodeLabel = chaincode . instantiatedChaincode . label ; chaincodeName = chaincode . instantiatedChaincode . name ; channelName = chaincode . channelName ; } else { chaincodeLabel = chaincode . label ; chaincodeName = chaincode . name ; channelName = chaincode . channels [ 0 ] . label ; } } const eventName = await UserInputUtil . showInputBox ( 'Enter the name of the event in your smart contract you want to subscribe to - use regular expressions to specifiy multiple events' ) ; if ( ! eventName ) { return ; } const connection = FabricGatewayConnectionManager . instance ( ) . getConnection ( ) ; try { await connection . addContractListener ( channelName , chaincodeName , eventName , outputAdapter ) ; outputAdapter . log ( LogType . SUCCESS , `Successfully subscribed to ${ eventName } events emitted from ${ chaincodeLabel }` ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Unable to subscribe to ${ eventName } events emitted from ${ chaincodeLabel }: ${ error . message }` , `Unable to subscribe to ${ eventName } events emitted from ${ chaincodeLabel }: ${ error . toString ( ) }` ) ; } }