Note — Code in this article is in continuation of the previous parts, so if you feel disconnected I would strongly recommend referring the previous part to have a better understanding. Below is the link for my previous article.
In the previous part, we have done the configuration part for our BookingCorDapp and in this part, we will be creating a CorDapp which will help us to understand some key concepts of Corda and how to start coding. I have divided this part into four steps as below, and we’ll go through each step one by one.
4 Steps in building the BookMyStay Application: BookingCorDapp
1. Create the BookingState class:
Since the CorDapp designed for requesting a stay to Hotel, a state must be created to represent Booking details. States are immutable objects representing on-ledger facts. For more information on states, see the state documentation.
Now, double-click the new BookingState.java file to open it.
Add the following imports to the top of the state file.
Update @BelongsToContract(TemplateContract::class) to specify BookingContract::class.
We’ll define our properties in the BookingState class.
Define the getter methods for the properties defined in the State, which will be used in our Contract class. Don’t worry if you’re not sure exactly how these should appear, you can check our code shortly.
Add a body to the BookingState class, which implements ContractState that overrides the getParticipants method. In R3 Corda, a transaction always requires the signature of the parties involved. In our Booking application: CorDapp, the parties involved are the BookMyStay and the HotelHeaven. So we add them to the list and return.
The BookingState file should now appear as follows:
2. Create the BookingContract class:
After creating a state, we must create a contract. Contracts define the rules that govern how states can be created and evolved. Our Contract class will hold the clauses which will be validated when a transaction proposal is submitted. Exceptions are thrown if violations are met and the transaction is rejected. Note the ID which refers to the Contract. The ID is used in our flows to refer to the Contract. We’ll modify the BookingContract class with the validations. In our corDapp, a contract should check for the following:
To learn more about contracts, see the contracts documentation.
3. Create the BookingInitiatorFlow class:
So, we have now the State and the Contract classes created. It’s time to create our flows, which will define the processes in initiating a BookingRequest transaction by the BookMyStay to HotelHeaven.
We’ll use the sample Initiator and Responder flow classes to build our custom flows. Make a copy of the flows the same as we did with State and Contract, and create BookingInitiatorFlow and BookingResponderFlow classes.
The BookingIntitiatorFlow class implements FlowLogic which overrides the call method. We will implement our transaction logic in this method. R3 Corda helps us in creating a transaction using a TransationBuilder. We define the components needed for the transaction and build them using the TransactionBuilder.
After Modification, BookingInitiatorFlow.java file should look like this:
4. Create the BookingResponderFlow class:
In our Booking Application: CorDapp, HotelHeven sends back an acknowledgment by sending a message for confirmation of booking. We will now define this logic in our BookingResponderFlow class. After Modification, BookingResponderFlow.java file should look like this:
Commit: Here you can see the commit of all these changes:
Github:: https://github.com/dhinoja15/BookMyStayCorDapp-java.git
Here, We have our code ready to deploy…
We will continue in the next article: “Steps to run our BookingCorDapp “.
If you are completely new to Corda then please refer my previous articles to understand what Corda is and use cases of Corda. So, you will get the answer to all your questions about Corda. Below is the link to the article.
And here is the other article, where I have tried to answer basic questions that arise on a learning path of Corda. “How to Get started learning Corda: 10 Beginners FAQs”
If you find something to improve please let me know, I will try my best to improve this article.
If this article was helpful to you, please do hit like and Share. Thanks!
See you in the next article!
Enjoy!
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular