'use strict' ; import * as vscode from 'vscode' ; import { Reporter } from '../util/Reporter' ; import { PackageRegistryEntry } from '../registries/PackageRegistryEntry' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { VSCodeBlockchainDockerOutputAdapter } from '../logging/VSCodeBlockchainDockerOutputAdapter' ; import { FabricEnvironmentRegistryEntry , IFabricEnvironmentConnection , LogType , EnvironmentType , FabricInstalledSmartContract } from 'ibm-blockchain-platform-common' ; import { FabricEnvironmentManager } from '../fabric/environments/FabricEnvironmentManager' ; export async function instantiateSmartContract ( channelName , peerNames , selectedPackage , instantiateFunctionName , instantiateFunctionArgs , endorsementPolicy , collectionConfigString ) { let smartContractName ; let smartContractVersion ; const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , 'instantiateSmartContract' ) ; const connection = FabricEnvironmentManager . instance ( ) . getConnection ( ) ; if ( ! connection ) { return ; } try { const isPackageInstalled = await checkPackageInstalled ( connection , peerNames , selectedPackage ) ; if ( ! isPackageInstalled ) { const channelMap = await connection . createChannelMap ( ) ; const installResult = await vscode . commands . executeCommand ( ExtensionCommands . INSTALL_SMART_CONTRACT , channelMap , selectedPackage ) ; if ( installResult [ 1 ] && installResult [ 1 ] !== 'none' ) { if ( installResult [ 1 ] === 'timeout' ) { const didInstallWork = await checkPackageInstalled ( connection , peerNames , selectedPackage ) ; if ( didInstallWork ) { throw new Error ( 'Chaincode installed but timed out waiting for the chaincode image to build. Please redeploy your chaincode package to attempt instantiation' ) ; } } throw new Error ( 'failed to get contract from peer after install' ) ; } } smartContractName = selectedPackage . name ; smartContractVersion = selectedPackage . version ; await vscode . window . withProgress ( { location : vscode . ProgressLocation . Notification , title : 'IBM Blockchain Platform Extension' , cancellable : false } , async ( progress ) => { progress . report ( { message : 'Instantiating Smart Contract' } ) ; const fabricEnvironmentRegistryEntry = FabricEnvironmentManager . instance ( ) . getEnvironmentRegistryEntry ( ) ; if ( fabricEnvironmentRegistryEntry . environmentType === EnvironmentType . LOCAL_MICROFAB_ENVIRONMENT ) { VSCodeBlockchainDockerOutputAdapter . instance ( fabricEnvironmentRegistryEntry . name ) . show ( ) ; } await connection . instantiateChaincode ( smartContractName , smartContractVersion , peerNames , channelName , instantiateFunctionName , instantiateFunctionArgs , collectionConfigString , endorsementPolicy ) ; Reporter . instance ( ) . sendTelemetryEvent ( 'instantiateCommand' ) ; outputAdapter . log ( LogType . SUCCESS , 'Successfully instantiated smart contract' ) ; await vscode . commands . executeCommand ( ExtensionCommands . REFRESH_GATEWAYS ) ; await vscode . commands . executeCommand ( ExtensionCommands . REFRESH_ENVIRONMENTS ) ; } ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Error instantiating smart contract: ${ error . message }` , `Error instantiating smart contract: ${ error . toString ( ) }` ) ; return ; } } async function checkPackageInstalled ( connection , peerNames , selectedPackage ) { let installedChaincode = await connection . getInstalledSmartContracts ( peerNames [ 0 ] , true ) ; installedChaincode = installedChaincode . filter ( ( chaincode ) => { return chaincode . label === `${ selectedPackage . name }@${ selectedPackage . version }` && chaincode . packageId === selectedPackage . name ; } ) ; return ( installedChaincode . length > 0 ) ; }