Tools, FAQ, Tutorials:
submit-transaction.js - Submit Chaincode Transaction
How to write a Node.js script to submit a transaction defined in the Invoke method of a chaincode?
✍: FYIcenter.com
Here is sample Node.js script, submit-transaction.js, that
uses a given user identity from the local wallet to
submit a transaction on chaincode.
/* Copyright (c) FYIcenter.com
*/
'use strict';
const { FileSystemWallet, Gateway } = require('fabric-network');
const fs = require('fs');
const ccp = JSON.parse(fs.readFileSync("connection.json", 'utf8'));
const user = process.argv[2];
const channel = process.argv[3];
const chaincode = process.argv[4];
const tx = process.argv.slice(5);
async function main() {
try {
// Create a new gateway for connecting to our peer node.
const wallet = new FileSystemWallet("wallet");
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: user, discovery: { enabled: false } });
// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork(channel);
// Get the contract from the network.
const contract = network.getContract(chaincode);
// Submit the specified transaction.
const result = await contract.submitTransaction(...tx);
console.log("Tx function and arguments:");
console.log(tx);
console.log("Tx result: %s", result);
// Disconnect from the gateway.
await gateway.disconnect();
} catch (error) {
console.error(`Failed to submit transaction: ${error}`);
process.exit(1);
}
}
main();
To run this application script on your client system, you need finish the last tutorial to register "alice" to ca.example.com, and import the certificate and the key info the local wallet.
Try the script to call the createCar() function of the "fabcar" Invoke() method.
$ node submit-transaction alice mychannel fabcar createCar CAR12 Honda Accord Black Tom Tx function and arguments: [ 'createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom' ] Tx result:
Run evaluate-transaction.js to call the queryCar() function to verify the new asset:
$ node evaluate-transaction alice mychannel fabcar queryCar CAR12
Tx function and arguments:
[ 'queryCar', 'CAR12' ]
Tx result: {"colour":"Black","make":"Honda","model":"Accord","owner":"Tom"}
⇒ Differences of evaluateTransaction() and submitTransaction()
⇐ evaluate-transaction.js - Evaluate Chaincode Transaction
2019-04-22, ∼1813🔥, 0💬
Popular Posts:
How To Read a File in Binary Mode in PHP? If you have a file that stores binary data, like an execut...
How To Pad an Array with the Same Value Multiple Times in PHP? If you want to add the same value mul...
How to use the "set-backend-service" Policy Statement for an Azure API service operation? The "set-b...
What Is session_register() in PHP? session_register() is old function that registers global variable...
What properties and functions are supported on requests.models.Response objects? "requests" module s...