Writing the transaction listener method

The transaction listener will connect to the gateway and monitor it for any events created by our corprem contract by doing the following:

  1. The transaction listener will first check our wallet to see whether an identity exists for connecting to the blockchain gateway. This is the user1 identity that we created for Bank A earlier. If the user exists, we move on to the next steps, shown in the following code block:
async function Transactionlistener (){

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-BankA.js application before retrying');
return;
}
  1. Next, we connect to the Hyperledger Fabric gateway with the user1 user, like this:
const gateway = new Gateway();
await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });
console.log("Gateway Connected");
const network = await gateway.getNetwork('bkchannel')
const contract = network.getContract('corprem');
  • The network is set to our Bankchain channel, bkchannel.
  • The contract object is set to corprem, which is our corporate remittance smart contract.
  • The addContractListener method adds a new listener. This listener will listen to the txCreated event triggered by the corprem chaincode on the bkchannel chaincode, as follows:
const listener = await contract.addContractListener('contract-listener', 'txCreated', (err, event, blockNumber, transactionId, status) => {
if (err) {
console.error(err);
return;
}

On the response from the listener, we check for errors. 

In the case of no errors, the transaction payload is captured in the trans object, and the transaction block number and transaction ID are printed on the console, as shown in the following code block:

console.log(`Block Number: ${blockNumber} Transaction ID: ${transactionId} Status: ${status}`);

var trans=JSON.parse(event.payload.toString());
console.log(trans);
  1. If the receiving bank in the transaction payload (trans.Rbank) is Bank A, we fetch the balance of the receiving customer in the customers table. To do so, we use the pg client and run a SELECT query on the table filtered by the customer's account, as follows:
if(trans.Rbank == 'Bank A')
{

client.query('SELECT balance from customers where account = $1', [trans.Raccount], (error, results)=> {

if (error) {
throw error
}
  1. On a successful response, we add the transaction amount to the current balance of the customer and update the customer's new balance in the customers table, like this:
if(results)
{
var oldbal = results.rows[0].balance;
var newbal = Number(oldbal) + Number(trans.amt);

client.query('UPDATE customers set balance = $1 where account = $2', [newbal, trans.Raccount], (error, results) => {
  1. After successfully updating the customer's balance, we insert the transaction into the transactions table. Notice in the following code block that we set the transtype flag to Incoming
if (error) {
throw error
}

client.query('INSERT INTO transactions(transaction_id,saddress, saccount,sname, sbank, raddress, raccount, rname, rbank, amount, currency, invhash, boehash, dochash, transtype) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)', [trans.txid,trans.Saddr, trans.Saccount, trans.Sname, trans.Sbank, trans.Raddress, trans.Raccount, trans.Rname, trans.Rbank,trans.amt,trans.curr,trans.InvHash,trans.BOEHash,trans.DocHash, 'Incoming'], (error, results) => {
  1. After successfully updating the transaction to the transactions table, we call the iread() method to save the compliance files to the bank infrastructure's local storage. The iread() method takes the hash of the three compliance documents as input parameters, and fetches the documents from the IPFS network, as follows:
if (error) {
throw error
}

iread(trans.InvHash,trans.BOEHash,trans.DocHash);
})

That wraps up the transaction listener. Let's look at the iread() method.

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

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