States, Contract, and Flows in Corda 5

September 28, 2021

In this blog, we will see how to write a State, Contract and Flow in Corda 5.


States in Corda 5 are similar to those in Corda 4 with subtle differences. In addition to your state implementing ContractState, you should also implement it against the JsonRepresentable interface. This interface has a toJson() method. In Corda 5, a new service called the JsonMarshellingService is provided which enables you to send your state output in a JSON format over HTTP/RPC. I have pasted a state code snippet from this sample below. You could represent this state in a JSON format and send it out to the client. This can be done by calling the jsonMarshellingService.formatJson from the flow. This internally calls the toJson() method and hence lets the flow send JSON output representing the state.

public class TemplateState implements ContractState, JsonRepresentable {

private String msg;

private Party sender;

private Party receiver;

/* Constructor of your Corda state */

public TemplateState(String msg, Party sender, Party receiver) {

this.msg = msg;

this.sender = sender;

this.receiver = receiver;



public String getMsg() {

return msg;


public Party getSender() {

return sender;


public Party getReceiver() {

return receiver;


/* This method will indicate who are the participants and required signers when

* this state is used in a transaction. */



public List<AbstractParty> getParticipants() {

return Arrays.asList(sender, receiver);




public String toJsonString() {

return “msg : ” + msg + ” sender : ” + sender.getName().toString() + ” receiver : ” + receiver.getName().toString();




There aren’t any changes introduced in Corda 5 to contracts. They are similar to Corda 4 contracts.


Below are the key changes to a flow in Corda 5.

  1. In Corda 5, bulky code written in the FlowLogic abstract class has been broken down. Now instead of extending the FlowLogic abstract class, you can implement the lightweight Flow interface. This interface has only one call method, wherein your actual flow logic will be written.
  2. All the services can now be injected using the @CordaInject annotation.
  3. If you wish to pass a JSON string as an input to your flow over HTTP RPC, mark your flow constructor using a JsonConstructor annotation. Within the flow, you can use the JsonMarshelling service to parse the input JSON and convert it to a map as shown in Figure 1 on line 38.
  4. Looking at the below code snippet you should be able to see other code differences as well, note, for example, the way sessions are now initiated with the counterparties. Also, take a look at how transactions are now constructed using TransactionFactory. 
  5. This flow returns SignedTransactionDigest. This class is a utility class which is helpful when you want to return a JSON output.
  6. With Corda 5, we can now use the PersistenceService to talk to the database instead of using EntityManager or JdbcSession directly.
Code showing a typical flow in a Corda 4 to Corda 5
Flow differences in Corda 4 and Corda 5
Code showing a typical flow in a Corda 5 CorDapp
Figure 1: A typical flow in a Corda 5 CorDapp

With Corda 5, we can now use the PersistenceService to talk to the database instead of using EntityManager or JdbcSession directly.

Next Steps
I would urge you to try writing your own CorDapp using this template – refer to this blog which talks about how to run a Corda 5 CorDapp.

— Sneha Damle is a Developer Evangelist at R3, an enterprise blockchain software firm working with a global ecosystem of more than 350 participants across multiple industries from both the private and public sectors to develop on Corda, its open-source blockchain platform, Corda Enterprise, a commercial version of Corda for enterprise usage, a confidential computing platform.
Follow Sneha on LinkedIn here.