Skip to content

Commit c02aed9

Browse files
committed
invoke chaincode
Signed-off-by: urgetolearn <[email protected]>
1 parent f434ad2 commit c02aed9

File tree

3 files changed

+11
-53
lines changed

3 files changed

+11
-53
lines changed

extension.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,12 @@ function activate(context) {
361361

362362
vscode.window.showInformationMessage(`Arguments captured. Press "Invoke" to execute the command.`, "Invoke").then(async selection => {
363363
if (selection === "Invoke") {
364-
await invokeChaincode(functionName, argumentValues, context);
364+
try {
365+
const result = await invokeChaincode(functionName, argumentValues, context);
366+
vscode.window.showInformationMessage(`Chaincode invoked successfully: ${result}`);
367+
} catch (error) {
368+
vscode.window.showErrorMessage(`Failed to invoke chaincode: ${error.message}`);
369+
}
365370
}
366371
});
367372

src/invokechaincode/connection-profile.json

-43
This file was deleted.

src/invokechaincode/invoke.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,29 @@ const { log } = require('console');
66

77
async function invokeChaincode(functionName, args, context) {
88
try {
9-
109
const ccpPath = path.resolve(__dirname, 'connection-profile.json');
1110
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));
12-
const wallet = await Wallets.newFileSystemWallet(path.join(process.cwd(), 'wallet'));
11+
12+
const wallet = await Wallets.newFileSystemWallet(path.join(process.cwd(), 'wallet'));
1313
const gateway = new Gateway();
1414
await gateway.connect(ccp, {
1515
wallet,
1616
identity: 'admin',
1717
discovery: { enabled: true, asLocalhost: true }
1818
});
1919

20-
2120
const network = await gateway.getNetwork('mychannel');
2221
const contract = network.getContract('mychaincode');
23-
24-
const result = await contract.submitTransaction('CreateAsset', 'asset1', '100');
25-
console.log(`Chaincode invoked successfully. Result: ${result.toString()}`);
22+
const result = await contract.submitTransaction(functionName, ...args);
2623

27-
24+
console.log(`Chaincode invoked successfully. Result: ${result.toString()}`);
2825
await gateway.disconnect();
29-
3026
return result.toString();
3127
} catch (error) {
3228
console.error(`Failed to invoke chaincode: ${error.message}`);
3329
throw new Error(`Failed to invoke chaincode: ${error.message}`);
3430
}
3531
}
3632

37-
33+
module.exports = { invokeChaincode };
3834
invokeChaincode();

0 commit comments

Comments
 (0)