Writing the corporate remittance contract

Let's write our corprem contract, with the help of the following steps:

  1. Create a new file called CorpRem.js. We start writing the contract, by declaring the dependent fabric-contract-api library, which is used to define our contract object, like this:
/*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';

const { Contract } = require('fabric-contract-api');
class corprem extends Contract {
  1. Next, we will add the createTx method. The createTx method will create a new transaction in the ledger, as follows:
async createTx(ctx, txid, 
Sname,
Saccount,
Sbank,
Saddr,
Rname,
Raccount,
Rbank,
Raddress,
curr,
amt,
InvHash,
BOEHash,
DocHash ) {

console.info('============= START : Create Transaction ===========');

The method takes the following parameters as input:

  • ctx: The transaction context.
  • txid: The transaction ID generated by the bank's internal system.
  • Sname: The transaction sender's name.
  • Saccount: The transaction sender's bank account number.
  • Sbank: The transaction sender's bank.
  • Saddr: The transaction sender's registered office address.
  • Rname: The transaction receiver's name.
  • Raccount: The transaction receiver's bank account number.
  • Rbank: The transaction receiver's bank.
  • Raddr: The transaction receiver's registered office address.
  •  curr: The currency of the transaction amount.
  • amount: The transaction amount.
  • InvHash: The hash of the invoice document, relevant to the corporate remittance being sent.
  • BOEHash: The hash of the Bill of Entry/Bill of Lading (BOE/BOL) document, relevant to the corporate remittance being sent.
  • DocHash: The hash of any other documents, relevant to the corporate remittance being sent.

A statement is printed to the console, to indicate the start of the method.

We create the transaction object, using the input parameters. We'll write this object to the ledger, like this:

const transaction = {
txid,
Sname,
Saccount,
Sbank,
Saddr,
Rname,
Raccount,
Rbank,
Raddress,
curr,
amt,
InvHash,
BOEHash,
DocHash,
DocType: 'transaction'
};

We use putState, to write the transaction object to the ledger. The transaction object will be referred by the transaction ID (txid) in the ledger, as follows:

await ctx.stub.putState(txid, Buffer.from(JSON.stringify(transaction)));

Lastly, we fire a txCreated event, to indicate to all transaction listeners that a new transaction has been added, as follows:

ctx.stub.setEvent('txCreated', Buffer.from(JSON.stringify(transaction)));
console.info('============= END : Create Transaction ===========');
}

We also print a message on the console, to indicate the end of the createTx method.

  1. Next, we will write a queryTx method, to retrieve transactions written to the blockchain ledger, like this:
async queryTx (ctx, txid) {

The queryTx method takes the transaction ID (txid) as an input parameter.

const transaction = await ctx.stub.getState(txid); // get the transaction from chaincode state
if (!transaction || transaction.length === 0) {
throw new Error(`Transaction does not exist`);
}
console.log(transaction.toString());
return transaction.toString();
}

module.exports = corprem;

It uses getState() to fetch the transaction details, using txid (the transaction ID). If the fetched transaction object is not empty, indicating the transaction exists, the transaction object is converted into a string object from the buffer and returned back to the requestor.

Lastly, we export the corprem contract as a module. That wraps up our chaincode contract. Now, let's deploy it to the blockchain.

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

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