April 21, 2020
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 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.
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.
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:
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:
To learn more about contracts, see the contracts documentation.
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:
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!
Share this post
Stay up to date on the latest news and articles related to Corda.