Improved gateway.disconnect usage, Improved comments

Signed-off-by: Sijo Cherian <sijo@ibm.com>
This commit is contained in:
Sijo Cherian 2020-07-07 09:51:07 -04:00
parent 2f58fb29c4
commit 9b14d5bfe5
2 changed files with 35 additions and 27 deletions

View file

@ -31,22 +31,26 @@ async function main() {
// Create a new gateway for connecting to our peer node. // Create a new gateway for connecting to our peer node.
const gateway = new Gateway(); 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. // Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('mychannel'); const network = await gateway.getNetwork('mychannel');
// Get the contract from the network. // Get the contract from the network.
const contract = network.getContract('fabcar'); const contract = network.getContract('fabcar');
// Submit the specified transaction. // Submit the specified transaction: The transaction function name will be evaluated on the
// createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom') // endorsing peers and then submitted to the ordering service for committing to the ledger.
// 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. // createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom')
await gateway.disconnect(); // 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) { } catch (error) {
console.error(`Failed to submit transaction: ${error}`); console.error(`Failed to submit transaction: ${error}`);

View file

@ -32,23 +32,27 @@ async function main() {
// Create a new gateway for connecting to our peer node. // Create a new gateway for connecting to our peer node.
const gateway = new Gateway(); 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. // Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('mychannel'); const network = await gateway.getNetwork('mychannel');
// Get the contract from the network. // Get the contract from the network.
const contract = network.getContract('fabcar'); const contract = network.getContract('fabcar');
// Evaluate the specified transaction. // Evaluate the specified transaction. This is used for querying the world state.
// queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4') // The transaction function name will be evaluated on the endorsing peers but the responses
// queryAllCars transaction - requires no arguments, ex: ('queryAllCars') // will not be sent to the ordering service and hence will not be committed to the ledger.
const result = await contract.evaluateTransaction('queryAllCars');
console.log(`Transaction has been evaluated, result is: ${result.toString()}`);
// Disconnect from the gateway.
await gateway.disconnect();
// 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) { } catch (error) {
console.error(`Failed to evaluate transaction: ${error}`); console.error(`Failed to evaluate transaction: ${error}`);
process.exit(1); process.exit(1);