'use strict' ; import * as vscode from 'vscode' ; import { Reporter } from '../util/Reporter' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { VSCodeBlockchainDockerOutputAdapter } from '../logging/VSCodeBlockchainDockerOutputAdapter' ; import { FabricEnvironmentRegistryEntry , IFabricEnvironmentConnection , LogType , EnvironmentType , FabricSmartContractDefinition } from 'ibm-blockchain-platform-common' ; import { FabricEnvironmentManager } from '../fabric/environments/FabricEnvironmentManager' ; import { SettingConfigurations } from '../configurations' ; export async function approveSmartContract ( ordererName , channelName , orgMap , smartContractDefinition ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , 'approveSmartContract' ) ; let connection = FabricEnvironmentManager . instance ( ) . getConnection ( ) ; if ( ! connection ) { await vscode . commands . executeCommand ( ExtensionCommands . CONNECT_TO_ENVIRONMENT ) ; connection = FabricEnvironmentManager . instance ( ) . getConnection ( ) ; if ( ! connection ) { return ; } } try { await vscode . window . withProgress ( { location : vscode . ProgressLocation . Notification , title : 'IBM Blockchain Platform Extension' , cancellable : false } , async ( progress ) => { progress . report ( { message : 'Approving Smart Contract Definition' } ) ; const fabricEnvironmentRegistryEntry = FabricEnvironmentManager . instance ( ) . getEnvironmentRegistryEntry ( ) ; if ( fabricEnvironmentRegistryEntry . environmentType === EnvironmentType . LOCAL_MICROFAB_ENVIRONMENT ) { VSCodeBlockchainDockerOutputAdapter . instance ( fabricEnvironmentRegistryEntry . name ) . show ( ) ; } let anyApproved = false ; for ( const org of orgMap . keys ( ) ) { progress . report ( { message : `Approving Smart Contract for org ${ org }` } ) ; const peers = orgMap . get ( org ) ; let timeout = vscode . workspace . getConfiguration ( ) . get ( SettingConfigurations . FABRIC_CLIENT_TIMEOUT ) ; if ( timeout ) { timeout = timeout * 1000 ; } const approved = await connection . approveSmartContractDefinition ( ordererName , channelName , peers , smartContractDefinition , timeout ) ; if ( ! approved ) { outputAdapter . log ( LogType . INFO , `Smart contract definition alreay approved by organisation ${ org }` ) ; } anyApproved = anyApproved || approved ; } Reporter . instance ( ) . sendTelemetryEvent ( 'approveCommand' ) ; if ( anyApproved ) { outputAdapter . log ( LogType . SUCCESS , 'Successfully approved smart contract definition' ) ; await vscode . commands . executeCommand ( ExtensionCommands . REFRESH_GATEWAYS ) ; await vscode . commands . executeCommand ( ExtensionCommands . REFRESH_ENVIRONMENTS ) ; } } ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Error approving smart contract: ${ error . message }` , `Error approving smart contract: ${ error . toString ( ) }` ) ; return error ; } }