Future Tech Trends in Financial Services

The transformation of traditional concepts — from securities, trading stocks, and transactions to the notion of value, digital ownership, how we tax, and how we categorize income — will grow in…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Automatic visualization of Corda contracts

I love diagrams. I think there is no better way to convey information than boxes with captions, lines between them and a nice colour scheme to it.

Whilst working with Corda contracts, there are occasions when one wishes to see a diagram of what the contract is doing and enforcing. A diagram showing the state transitions, the required signers, or the available commands. It would certainly make at least the following tasks easier:

We have recently introduced something called the Contract SDK; you can read more about it in this blog: Contract SDK: A faster way to write Corda contracts. One of the advantages of the Contract SDK is that the resulting contracts have a clear structure. A structure, which even a dumb IntelliJ plugin could parse and visualize in the shape of a diagram…

Once you have installed it, you have access to this command:

Let’s have a look at a few contracts and how they translate to diagrams so that you get the idea.

And we start extremely simply. The below snippet is all the code we have written so far. There’s just one command Issue and it is used for issuance of states. We don’t care so much about the states belonging to the contract at this stage, so we haven’t bothered creating them.

And this is the diagram the plugin will create. Notice that because we don’t have any state yet, it’s just called ContractState here. The black circle means no states on the input.

Example 1 — Diagram

Let’s add two more constraints. We now want the status of the output state to be “Live” and we also need the transaction to be signed by the “Issuer” from the output state(s).

As you can see, the required status is shown in the box in between brackets and hovering your mouse over the command reveals the required signers.

Example 2 — Diagram

Now we add two more things:

Notice that on the diagram the name of the state is now “Mortgage”. The dashed line between “Live” and “Expired” indicates it’s a transition of a linear state.

Example 3— Diagram

The contract code can be much more complicated and the plugin will still be able to cope. This is generated for a contract dealing with agreements between parties.

Last Example

Please go and have a play. If your contract is especially convoluted and instead of a pretty diagram you end up with a tangled mess, please share the contract with me and I will look at how to improve it.

Since the introduction of the Contract SDK and even more now with this IntelliJ plugin, it is possible to write CorDapps as “contract-first” and go through a few iterations of your idea before you start writing states, flows and the rest of your CorDapp.

I hope you enjoy the Contract SDK and the Viewer plugin as much as I do.

Perhaps needless to say, this is not a product supported by R3. Do not rely on the accuracy of the diagrams. Answering any issues raised will be on a best-effort basis.

Add a comment

Related posts:

Understanding Communication Protocols

This article is written by Divyansh Khandelwal as an internship assignment given by OpenLabPro. This article is dedicated to making the readers understand the communication protocols used by…

How to grow your plays on Spotify with SpotiMatch

Grow your plays on Spotify with Spotimatch! SpotiMatch is a FREE tool for Artists, Playlists Curators and Labels, that allows its users to match songs with independent playlists on Spotify.

Dear Abyss

I desire not to procreate, nor propagate my genes I deny the poison to descend through another generation My art will be my only act of creation The word machine of a novel, the potency of poetry…