-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinvoke.js
More file actions
70 lines (53 loc) · 2.05 KB
/
invoke.js
File metadata and controls
70 lines (53 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
const yaml = require('js-yaml');
const { FileSystemWallet, Gateway } = require('fabric-network');
const fs = require('fs');
// A wallet stores a collection of identities for use
const wallet = new FileSystemWallet('./_idwallet');
async function main() {
// A gateway defines the peers used to access Fabric networks
const gateway = new Gateway();
// Main try/catch block
try {
const CONTRACT_NAME = 'demoContract';
const identityLabel = 'User1@org1.example.com';
let connectionProfile = yaml.safeLoad(fs.readFileSync('./network.yaml', 'utf8'));
let connectionOptions = {
identity: identityLabel,
wallet: wallet
};
// Connect to gateway using application specified parameters
await gateway.connect(connectionProfile, connectionOptions);
console.log('Connected to Fabric gateway.');
// Get addressability to PaperNet network
const network = await gateway.getNetwork('mychannel');
console.log('Connected to mychannel. ');
// Get addressability to commercial paper contract
const contract = await network.getContract(CONTRACT_NAME);
console.log('\nSubmit hello world transaction.');
// issue commercial paper
let response = await contract.submitTransaction('transaction1', 'hello');
console.log(JSON.parse(response.toString()));
const channel = network.getChannel();
let request = { chaincodeId: CONTRACT_NAME, fcn: 'getData', args: ['ITEM', ''] };
let resultBuffer = await channel.queryByChaincode(request);
let result = JSON.parse(resultBuffer.toString());
console.log(result);
} catch (error) {
console.log(`Error processing transaction. ${error}`);
console.log(error.stack);
} finally {
// Disconnect from the gateway
console.log('Disconnect from Fabric gateway.');
gateway.disconnect();
}
}
// invoke the main function, can catch any error that might escape
main().then(() => {
console.log('done');
}).catch((e) => {
console.log('Final error checking.......');
console.log(e);
console.log(e.stack);
process.exit(-1);
});