Prepared By: Sonal Dhinoja
Reviewed By: Birender Saini and Rajapandian Chellimuthu
This is part two of a multi-part series on how I created a CorDapp in 3 steps. Read the first part of this series on https://corda.net, where we went through the initial setup and configuration of our BookingCorDapp.
In this part, we will be continuing creating a CorDapp and explore some of the key concepts of Corda by apply them in code. The article is divided into four steps and we will 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
Step 1: Create the BookingState class:
Since the CorDapp designed for requesting a stay at the 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 theTemplateState.java in the project navigation on the left. Select Refactor and Copy it.
- Paste it in the same package by right-clicking on the package and rename the file to BookingState and click OK. So our project structure on left side will look like below figure.
Now, double-click the new BookingState.java file to open it.
Add 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 your 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:
Step 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, contract should check for following:
- Shape Rule: No input for booking request and only one output.
- Content Rules: 1. Customer Age should be greater than 18. 2. Check Out date should be greater than Check in date. 3. After commission price should 85% of Original room price. 4. Credit Card number length should be 16. 5. Credit Card Exp date should not be in past.
- Signer Rules: BookYourStay 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 Copy it.
- Rename the file to BookingContract and click OK.
- Double-click the new contract file to open it. And modify according below link. The BookingContract file should now appear as follows:
Step 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 same as we did with State and Contract, and create BookingInitiatorFlow and BookingResponderFlow classes.
The BookingIntitiatorFlow class implements FlowLogic which overrides 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:
Step 4: Create the BookingResponderFlow class:
In our Booking Application: CorDapp, HotelHeven sends back an acknowledgement by sending 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 “.
You can refer to the first article in the link below to understand what corda is and use cases it can provide.
And other article “How to Get started learning Corda: 10 Beginners FAQs”
If you found something that you like me to improve please let me know!
I hope these series are helpful to you, please do share. Thanks!
See you in the next article!