Corda contracts are smart and powerful, but after a while, they become a bit boring and repetitive to write. How many times did you find yourself writing a contract that looks similar to this:
There are a few things that repeat over and over again in many contracts:
- Looking for a single command
- Running that command through a when decision tree and invoking the right set of require statements
- Require statements that check the number of input states, number of output states, signers, statuses, coupling between input and output states based on linear id, etc.
So why wouldn’t someone create reusable blocks of code, which we can easily build up a Corda contract from so that we can stop writing and rewriting the same constructs? Ask no more, it has been done. With the Contract SDK the same contract like the one above shrinks to this:
Short and neat, right? I parametrized the annotations with concrete values to make the example even more clear.
What I especially like about writing contracts this way is that the contract can now be the first thing that you write in your CorDapp. You can have a good think about the commands, roles and statuses without moving away from the contract class and without having to write any other code first.
There’s much more in the Contract SDK that the example doesn’t show. You can annotate the contract itself, you can provide further, custom, verifications on both the command and the contract level, you can target the annotations on selected contract state types, and perhaps most importantly, there are many more annotations ready for you to use.
If you would like to write contracts this way, please navigate to https://github.com/corda/contract-sdk and take a look at the readme. Alternatively, jump straight into the sample app at https://github.com/corda/contract-sdk/tree/main/examples/test-app, it’s all pretty self-explanatory.
I have no doubt that with the Contract SDK we can all write more readable and reliable contracts, faster.
Please note that the Contract SDK is in the proof-of-concept stage and the only testing it has gone through is the unit tests present in the project and some ad-hoc testing on the deployed test-app. Bear that in mind should you want to use this project in, say, production. Always test your contract logic and your entire CorDapp independently and thoroughly. Also, please note that R3 doesn’t provide any support for this project. Responding to any issues raised will be on a best-effort basis.
Want to learn more about building awesome blockchain applications on Corda? Be sure to visit https://corda.net, check out our community page to learn how to connect with other Corda developers, and sign up for one of our newsletters for the latest updates.
— Alex Koller is a Solutions Engineer 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, and Corda Enterprise, a commercial version of Corda for enterprise usage.
Contract SDK: A faster way to write Corda contracts was originally published in Corda on Medium, where people are continuing the conversation by highlighting and responding to this story.