Writing the createLC method

Now, we come to the primary methods of our app. We start with the createLC method, as follows:

  1. We start the app by storing the current app state in the app variable, as shown in the following code block. This will allow us to refer to the current app state during asynchronous calls:
createLC = () => {
let app = this;
  1. The contract variable is used to instantiate an LCMaster object, which points to the LCMaster smart contract we deployed earlier to our blockchain. We do so by using the web3.eth.contract method. The input parameter to this method is the contract ABI, and the second parameter is the contract address. Since we had mapped these earlier to the LCMaster object, we simply fetch these values and pass them to the method, like this:
var contract = new this.web3.eth.Contract(this.LCMaster.abi, this.LCMaster.address);
  1. Next, we fetch the user inputs while creating the LC. The date input by the user is spliced into year, month, and day, like this:
let dateExpiry = this.state.fields.DOExpiry;
let year = dateExpiry.slice(0,4);
let month = dateExpiry.slice(4,6)-1;
let day = dateExpiry.slice(6,8);
  1. This value is then converted into UTC format, which Ethereum understands and interprets, like this:
 var DateTemp = new Date(year, month, day, 23, 59, 59, 0)
var DOE = Math.floor(DateTemp.getTime()/1000.0)
  1. We use our contract object to call the createLC method in LCMaster, as shown in the following code block. The transaction is sent from the web3.defaultAccount we set earlier:
contract.methods.createLC(this.state.fields.BuyerAccount,this.state.fields.SellerAccount, 
this.state.fields.Amount,DOE).send({from: app.web3.eth.defaultAccount}).then(function(response){
  1. Lastly, we check the response from the smart contract method. The successful response, which is the contract LC number, is printed to the console, as follows:
if(response) {
console.log("LC No.");
console.log(response);
app.resetApp();
}
})

On to the next method, viewLC.

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

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