Writing the viewLC method

Next, let's look at the method that will fetch the details of the LCs issued by the bank on the blockchain. To do so, we'll fetch the LC details from the LC Master smart contract by invoking the smart contract viewLC method, as follows:

  1. We start our viewLC method by defining the contract instance, similarly to the last method, as follows:
let app = this;
var lastLC;

var contract = new this.web3.eth.Contract(this.LCMaster.abi,this.LCMaster.address);
  1. The first contract call is to the lengthLC method. This method returns the number of LCs that have been created in the LCMaster contract. This number is the length of the LCDoc array. The code can be seen here:
contract.methods.lengthLC().call().then(function(response){
  1. On a successful response, we store the length of the LCDoc array in the lastLC variable, like this:
if(response) {
lastLC = response;
  1. If lastLC is greater than 1 (that is, LCs have been issued by the LCMaster contract), we perform the next set of steps, like this:
if (lastLC > 1)
{
app.setState({
LCNew: [],
})

for (let i = 1; i < lastLC ; i++)
{
contract.methods.viewLC(i).call().then(function(response){

We first reset the LCNew state variable and clean any previous data. Next, we run a loop and iterate from 0 to lastLC, and call the viewLC method in the LCMaster smart contract. For each function call to viewLC , we send the i loop counter as the LCNo input parameter.

The resultant output response is captured by a set of local variables. Notice how the value for Status is converted using a web3 utility from hex to ASCII. This is because Ethereum stores bytes values in hex representation. The code can be seen here:

if(response) {
let LCNo = i;
let SAcc = response[0];
let BAcc = response[1];
let Amount = response[2];
let Status = app.web3.utils.hexToAscii(response[3]);
let DOI = response[4];
let DOE = response[5];
let LCAdd = response[6];
  1. LCNew is initialized as a local variable from the LCNew state variable. For each iteration of the loop, we push the viewLC response to the LCNew local variable and update the LCNew state variable, as follows:
let LCNew = app.state.LCNew;

LCNew.push({
LCNo,
BAcc,
SAcc,
Amount,
Status,
DOI,
DOE,
LCAdd
});
app.setState({
LCNew
})

That brings us to the end of the viewLC method in our App.js file.

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

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