diff --git a/fabcar/javascript/invoke.js b/fabcar/javascript/invoke.js index 228aceb7..993959cb 100644 --- a/fabcar/javascript/invoke.js +++ b/fabcar/javascript/invoke.js @@ -31,22 +31,25 @@ async function main() { // Create a new gateway for connecting to our peer node. const gateway = new Gateway(); - await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); + try { + await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); - // Get the network (channel) our contract is deployed to. - const network = await gateway.getNetwork('mychannel'); + // 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('fabcar'); + // Get the contract from the network. + const contract = network.getContract('fabcar'); - // Submit the specified transaction. - // createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom') - // changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR12', 'Dave') - await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom'); - console.log('Transaction has been submitted'); - - // Disconnect from the gateway. - await gateway.disconnect(); + // Submit the specified transaction: The transaction function name will be evaluated on the + // endorsing peers and then submitted to the ordering service for committing to the ledger. + // createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom') + // changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR12', 'Dave') + await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom'); + console.log('Transaction has been submitted'); + } finally { + // Disconnect from the gateway. + gateway.disconnect(); + } } catch (error) { console.error(`Failed to submit transaction: ${error}`); diff --git a/fabcar/javascript/query.js b/fabcar/javascript/query.js index 82f5367f..5c964f6d 100644 --- a/fabcar/javascript/query.js +++ b/fabcar/javascript/query.js @@ -32,23 +32,26 @@ async function main() { // Create a new gateway for connecting to our peer node. const gateway = new Gateway(); - await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); + try { + await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); - // Get the network (channel) our contract is deployed to. - const network = await gateway.getNetwork('mychannel'); + // 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('fabcar'); + // Get the contract from the network. + const contract = network.getContract('fabcar'); - // Evaluate the specified transaction. - // queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4') - // queryAllCars transaction - requires no arguments, ex: ('queryAllCars') - const result = await contract.evaluateTransaction('queryAllCars'); - console.log(`Transaction has been evaluated, result is: ${result.toString()}`); - - // Disconnect from the gateway. - await gateway.disconnect(); - + // Evaluate the specified transaction. This is used for querying the world state. + // The transaction function name will be evaluated on the endorsing peers but the responses + // will not be sent to the ordering service and hence will not be committed to the ledger. + // queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4') + // queryAllCars transaction - requires no arguments, ex: ('queryAllCars') + const result = await contract.evaluateTransaction('queryAllCars'); + console.log(`Transaction has been evaluated, result is: ${result.toString()}`); + } finally { + // Disconnect from the gateway. + gateway.disconnect(); + } } catch (error) { console.error(`Failed to evaluate transaction: ${error}`); process.exit(1); diff --git a/fabcar/typescript/src/invoke.ts b/fabcar/typescript/src/invoke.ts index 762263ed..9bad015c 100644 --- a/fabcar/typescript/src/invoke.ts +++ b/fabcar/typescript/src/invoke.ts @@ -27,22 +27,24 @@ async function main() { // Create a new gateway for connecting to our peer node. const gateway = new Gateway(); - await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); + try { + await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); - // Get the network (channel) our contract is deployed to. - const network = await gateway.getNetwork('mychannel'); + // 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('fabcar'); + // Get the contract from the network. + const contract = network.getContract('fabcar'); - // Submit the specified transaction. - // createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom') - // changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR12', 'Dave') - await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom'); - console.log(`Transaction has been submitted`); - - // Disconnect from the gateway. - await gateway.disconnect(); + // Submit the specified transaction. + // createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom') + // changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR12', 'Dave') + await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom'); + console.log(`Transaction has been submitted`); + } finally { + // Disconnect from the gateway. + gateway.disconnect(); + } } catch (error) { console.error(`Failed to submit transaction: ${error}`); diff --git a/fabcar/typescript/src/query.ts b/fabcar/typescript/src/query.ts index b86677ee..82cf1901 100644 --- a/fabcar/typescript/src/query.ts +++ b/fabcar/typescript/src/query.ts @@ -28,19 +28,24 @@ async function main() { // Create a new gateway for connecting to our peer node. const gateway = new Gateway(); - await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); + try { + await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); - // Get the network (channel) our contract is deployed to. - const network = await gateway.getNetwork('mychannel'); + // 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('fabcar'); + // Get the contract from the network. + const contract = network.getContract('fabcar'); - // Evaluate the specified transaction. - // queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4') - // queryAllCars transaction - requires no arguments, ex: ('queryAllCars') - const result = await contract.evaluateTransaction('queryAllCars'); - console.log(`Transaction has been evaluated, result is: ${result.toString()}`); + // Evaluate the specified transaction. + // queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4') + // queryAllCars transaction - requires no arguments, ex: ('queryAllCars') + const result = await contract.evaluateTransaction('queryAllCars'); + console.log(`Transaction has been evaluated, result is: ${result.toString()}`); + } finally { + // Disconnect from the gateway. + gateway.disconnect(); + } } catch (error) { console.error(`Failed to evaluate transaction: ${error}`);