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
- Create the BookingState class
- Create the BookingContract class
- Create the BookingInitiator flow class
- Create the BookingResponder flow class
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.
- From IntelliJ expand the source files and navigate to the following state template file: contracts / src / main / java / com.template / states / TemplateState.java.
- Right-click on TemplateState.java in the project navigation on the left. Select Refactor and Copy it.
- Paste it in the same package by right click on the package and Rename the file to BookingState and click OK. So, our project structure on the left side will look like the below figure.
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:
- Shape Rule: No input for booking requests and only one output.
- Content Rules: 1. Customer Age should be greater than 18. 2. The check-out date should be greater than a check-in date. 3. After commission price should 85% of Original room price. 4. The credit Card number length should be 16. 5. Credit Card Exp date should not be in the past.
- Signer Rules: BookMyStay party must sign the transaction.
To learn more about contracts, see the contracts documentation.
- From IntelliJ, expand the project source and navigate to: contracts / src / main / Java / com / template / contracts / TemplateContract.java
- Right-click on TemplateContract.java in the project navigation on the left. Select Refactor and then Copy.
- Rename the file to BookingContract and click OK.
- Double-click the new contract file to open it. And modify according to the below link. The BookingContract file should now appear as follows:
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:
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!