Writing a method to submit transactions to the blockchain network

The submitTrans() method accepts the transaction details from the /payment endpoint and adds a new remittance transaction to the blockchain ledger. Let's see how the method works:

  1. The input parameters to the method are the transaction ID (txID), the trans object with the transaction details, and the hasharray object with the hash of the invoice document, the BOE/BOL document, and any other document shared for compliance purposes with the transaction request, as follows:
async function submitTrans ( txID, trans, hasharray) {
  1. We first check whether a valid user exists for Bank A for submitting transactions. We use the user1 user we created for Bank A earlier. We set the wallet object to the location of the admin and user keys we created earlier. We check whether a private key and certificate exist for the user1 user in the wallet, by running the following code:
try {
const wallet = new FileSystemWallet('~/fabric-samples/bankchain/wallet-BankA');
const userExists = await wallet.exists('user1');
if (!userExists) {
console.log('An identity for the user "user1" does not exist in the wallet');
console.log('Run the registerUser.js application before retrying');
return;
}
  1. Next, we set the Gateway object and connect to the gateway with the user1 user. The channel is set as bkchannel, our remittance channel in the Hyperledger Fabric network. The contract object is set to corprem, which is our corporate remittance chain code, as follows:
const gateway = new Gateway();
await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });
const network = await gateway.getNetwork('bkchannel');
const contract = network.getContract('corprem');
  1. We call the submitTransaction method in the contract object to submit a new transaction to the corprem chaincode, like this:
   await contract.submitTransaction('createTx',txID,trans.sname,trans.saccount,trans.sbank,trans.saddress,trans.rname,trans.raccount,trans.rbank,trans.raddress, trans.currency, trans.amount,hasharray[0],hasharray[1],hasharray[2]);

The following are the input parameters to the transaction:

  • createTx: Name of the method being invoked
  • txID: Transaction ID
  • trans.sname: Transaction sender's name
  • trans.saccount: Transaction sender's account
  • trans.sbank: Transaction sender's bank
  • trans.saddress: Registered address of the transaction sender
  • trans.rname: Transaction receiver's name
  • trans.raccount: Transaction receiver's account
  • trans.rbank: Transaction receiver's bank
  • trans.raddress: Registered address of the transaction receiver
  • trans.currency: Currency symbol of the transaction 
  • trans.amount: Transaction amount
  • hasharray[0]: Hash of the invoice document
  • hasharray[1]: Hash of the BOE/BOL
  • hasharray[2]: Hash of other compliance documents
  1. We print a message on the console after successful transaction submission and disconnect the gateway, like this:
console.log('Transaction has been submitted'); 
await gateway.disconnect();
return;
}
catch (error) {
console.error(`Failed to submit transaction: ${error}`);
return 'Failed to submit transaction: ${error}';
process.exit(1);
}
};

A catch block catches any errors while submitting the transaction to the blockchain network. That brings us to the end of the submitTrans() method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.220.120